OSDN Git Service

cp:
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A C++ token.  */
48
49 typedef struct cp_token GTY (())
50 {
51   /* The kind of token.  */
52   ENUM_BITFIELD (cpp_ttype) type : 8;
53   /* If this token is a keyword, this value indicates which keyword.
54      Otherwise, this value is RID_MAX.  */
55   ENUM_BITFIELD (rid) keyword : 8;
56   /* Token flags.  */
57   unsigned char flags;
58   /* True if this token is from a system header.  */
59   BOOL_BITFIELD in_system_header : 1;
60   /* True if this token is from a context where it is implicitly extern "C" */
61   BOOL_BITFIELD implicit_extern_c : 1;
62   /* The value associated with this token, if any.  */
63   tree value;
64   /* The location at which this token was found.  */
65   location_t location;
66 } cp_token;
67
68 /* We use a stack of token pointer for saving token sets.  */
69 typedef struct cp_token *cp_token_position;
70 DEF_VEC_P (cp_token_position);
71 DEF_VEC_ALLOC_P (cp_token_position,heap);
72
73 static const cp_token eof_token =
74 {
75   CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76 #if USE_MAPPED_LOCATION
77   0
78 #else
79   {0, 0}
80 #endif
81 };
82
83 /* The cp_lexer structure represents the C++ lexer.  It is responsible
84    for managing the token stream from the preprocessor and supplying
85    it to the parser.  Tokens are never added to the cp_lexer after
86    it is created.  */
87
88 typedef struct cp_lexer GTY (())
89 {
90   /* The memory allocated for the buffer.  NULL if this lexer does not
91      own the token buffer.  */
92   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93   /* If the lexer owns the buffer, this is the number of tokens in the
94      buffer.  */
95   size_t buffer_length;
96
97   /* A pointer just past the last available token.  The tokens
98      in this lexer are [buffer, last_token).  */
99   cp_token_position GTY ((skip)) last_token;
100
101   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
102      no more available tokens.  */
103   cp_token_position GTY ((skip)) next_token;
104
105   /* A stack indicating positions at which cp_lexer_save_tokens was
106      called.  The top entry is the most recent position at which we
107      began saving tokens.  If the stack is non-empty, we are saving
108      tokens.  */
109   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
110
111   /* True if we should output debugging information.  */
112   bool debugging_p;
113
114   /* The next lexer in a linked list of lexers.  */
115   struct cp_lexer *next;
116 } cp_lexer;
117
118 /* cp_token_cache is a range of tokens.  There is no need to represent
119    allocate heap memory for it, since tokens are never removed from the
120    lexer's array.  There is also no need for the GC to walk through
121    a cp_token_cache, since everything in here is referenced through
122    a lexer.  */
123
124 typedef struct cp_token_cache GTY(())
125 {
126   /* The beginning of the token range.  */
127   cp_token * GTY((skip)) first;
128
129   /* Points immediately after the last token in the range.  */
130   cp_token * GTY ((skip)) last;
131 } cp_token_cache;
132
133 /* Prototypes.  */
134
135 static cp_lexer *cp_lexer_new_main
136   (void);
137 static cp_lexer *cp_lexer_new_from_tokens
138   (cp_token_cache *tokens);
139 static void cp_lexer_destroy
140   (cp_lexer *);
141 static int cp_lexer_saving_tokens
142   (const cp_lexer *);
143 static cp_token_position cp_lexer_token_position
144   (cp_lexer *, bool);
145 static cp_token *cp_lexer_token_at
146   (cp_lexer *, cp_token_position);
147 static void cp_lexer_get_preprocessor_token
148   (cp_lexer *, cp_token *);
149 static inline cp_token *cp_lexer_peek_token
150   (cp_lexer *);
151 static cp_token *cp_lexer_peek_nth_token
152   (cp_lexer *, size_t);
153 static inline bool cp_lexer_next_token_is
154   (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_not
156   (cp_lexer *, enum cpp_ttype);
157 static bool cp_lexer_next_token_is_keyword
158   (cp_lexer *, enum rid);
159 static cp_token *cp_lexer_consume_token
160   (cp_lexer *);
161 static void cp_lexer_purge_token
162   (cp_lexer *);
163 static void cp_lexer_purge_tokens_after
164   (cp_lexer *, cp_token_position);
165 static void cp_lexer_handle_pragma
166   (cp_lexer *);
167 static void cp_lexer_save_tokens
168   (cp_lexer *);
169 static void cp_lexer_commit_tokens
170   (cp_lexer *);
171 static void cp_lexer_rollback_tokens
172   (cp_lexer *);
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175   (FILE *, cp_token *);
176 static inline bool cp_lexer_debugging_p
177   (cp_lexer *);
178 static void cp_lexer_start_debugging
179   (cp_lexer *) ATTRIBUTE_UNUSED;
180 static void cp_lexer_stop_debugging
181   (cp_lexer *) ATTRIBUTE_UNUSED;
182 #else
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184    about passing NULL to functions that require non-NULL arguments
185    (fputs, fprintf).  It will never be used, so all we need is a value
186    of the right type that's guaranteed not to be NULL.  */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
191
192 static cp_token_cache *cp_token_cache_new
193   (cp_token *, cp_token *);
194
195 /* Manifest constants.  */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
198
199 /* A token type for keywords, as opposed to ordinary identifiers.  */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
201
202 /* A token type for template-ids.  If a template-id is processed while
203    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204    the value of the CPP_TEMPLATE_ID is whatever was returned by
205    cp_parser_template_id.  */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
207
208 /* A token type for nested-name-specifiers.  If a
209    nested-name-specifier is processed while parsing tentatively, it is
210    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212    cp_parser_nested_name_specifier_opt.  */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
214
215 /* A token type for tokens that are not tokens at all; these are used
216    to represent slots in the array where there used to be a token
217    that has now been deleted.  */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
219
220 /* The number of token types, including C++-specific ones.  */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
222
223 /* Variables.  */
224
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written.  */
227 static FILE *cp_lexer_debug_stream;
228 #endif /* ENABLE_CHECKING */
229
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
231    preprocessor.  */
232
233 static cp_lexer *
234 cp_lexer_new_main (void)
235 {
236   cp_token first_token;
237   cp_lexer *lexer;
238   cp_token *pos;
239   size_t alloc;
240   size_t space;
241   cp_token *buffer;
242
243   /* It's possible that lexing the first token will load a PCH file,
244      which is a GC collection point.  So we have to grab the first
245      token before allocating any memory.  Pragmas must not be deferred
246      as -fpch-preprocess can generate a pragma to load the PCH file in
247      the preprocessed output used by -save-temps.  */
248   cp_lexer_get_preprocessor_token (NULL, &first_token);
249
250   /* Tell cpplib we want CPP_PRAGMA tokens.  */
251   cpp_get_options (parse_in)->defer_pragmas = true;
252
253   /* Tell c_lex not to merge string constants.  */
254   c_lex_return_raw_strings = true;
255
256   c_common_no_more_pch ();
257
258   /* Allocate the memory.  */
259   lexer = GGC_CNEW (cp_lexer);
260
261 #ifdef ENABLE_CHECKING
262   /* Initially we are not debugging.  */
263   lexer->debugging_p = false;
264 #endif /* ENABLE_CHECKING */
265   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266                                    CP_SAVED_TOKEN_STACK);
267
268   /* Create the buffer.  */
269   alloc = CP_LEXER_BUFFER_SIZE;
270   buffer = ggc_alloc (alloc * sizeof (cp_token));
271
272   /* Put the first token in the buffer.  */
273   space = alloc;
274   pos = buffer;
275   *pos = first_token;
276
277   /* Get the remaining tokens from the preprocessor.  */
278   while (pos->type != CPP_EOF)
279     {
280       pos++;
281       if (!--space)
282         {
283           space = alloc;
284           alloc *= 2;
285           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286           pos = buffer + space;
287         }
288       cp_lexer_get_preprocessor_token (lexer, pos);
289     }
290   lexer->buffer = buffer;
291   lexer->buffer_length = alloc - space;
292   lexer->last_token = pos;
293   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
294
295   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296      direct calls to c_lex.  Those callers all expect c_lex to do
297      string constant concatenation.  */
298   c_lex_return_raw_strings = false;
299
300   gcc_assert (lexer->next_token->type != CPP_PURGED);
301   return lexer;
302 }
303
304 /* Create a new lexer whose token stream is primed with the tokens in
305    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
306
307 static cp_lexer *
308 cp_lexer_new_from_tokens (cp_token_cache *cache)
309 {
310   cp_token *first = cache->first;
311   cp_token *last = cache->last;
312   cp_lexer *lexer = GGC_CNEW (cp_lexer);
313
314   /* We do not own the buffer.  */
315   lexer->buffer = NULL;
316   lexer->buffer_length = 0;
317   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
318   lexer->last_token = last;
319
320   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321                                    CP_SAVED_TOKEN_STACK);
322
323 #ifdef ENABLE_CHECKING
324   /* Initially we are not debugging.  */
325   lexer->debugging_p = false;
326 #endif
327
328   gcc_assert (lexer->next_token->type != CPP_PURGED);
329   return lexer;
330 }
331
332 /* Frees all resources associated with LEXER.  */
333
334 static void
335 cp_lexer_destroy (cp_lexer *lexer)
336 {
337   if (lexer->buffer)
338     ggc_free (lexer->buffer);
339   VEC_free (cp_token_position, heap, lexer->saved_tokens);
340   ggc_free (lexer);
341 }
342
343 /* Returns nonzero if debugging information should be output.  */
344
345 #ifdef ENABLE_CHECKING
346
347 static inline bool
348 cp_lexer_debugging_p (cp_lexer *lexer)
349 {
350   return lexer->debugging_p;
351 }
352
353 #endif /* ENABLE_CHECKING */
354
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
357 {
358   gcc_assert (!previous_p || lexer->next_token != &eof_token);
359
360   return lexer->next_token - previous_p;
361 }
362
363 static inline cp_token *
364 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
365 {
366   return pos;
367 }
368
369 /* nonzero if we are presently saving tokens.  */
370
371 static inline int
372 cp_lexer_saving_tokens (const cp_lexer* lexer)
373 {
374   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
375 }
376
377 /* Store the next token from the preprocessor in *TOKEN.  Return true
378    if we reach EOF.  */
379
380 static void
381 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
382                                  cp_token *token)
383 {
384   static int is_extern_c = 0;
385
386    /* Get a new token from the preprocessor.  */
387   token->type
388     = c_lex_with_flags (&token->value, &token->location, &token->flags);
389   token->in_system_header = in_system_header;
390
391   /* On some systems, some header files are surrounded by an
392      implicit extern "C" block.  Set a flag in the token if it
393      comes from such a header.  */
394   is_extern_c += pending_lang_change;
395   pending_lang_change = 0;
396   token->implicit_extern_c = is_extern_c > 0;
397
398   /* Check to see if this token is a keyword.  */
399   if (token->type == CPP_NAME
400       && C_IS_RESERVED_WORD (token->value))
401     {
402       /* Mark this token as a keyword.  */
403       token->type = CPP_KEYWORD;
404       /* Record which keyword.  */
405       token->keyword = C_RID_CODE (token->value);
406       /* Update the value.  Some keywords are mapped to particular
407          entities, rather than simply having the value of the
408          corresponding IDENTIFIER_NODE.  For example, `__const' is
409          mapped to `const'.  */
410       token->value = ridpointers[token->keyword];
411     }
412   /* Handle Objective-C++ keywords.  */
413   else if (token->type == CPP_AT_NAME)
414     {
415       token->type = CPP_KEYWORD;
416       switch (C_RID_CODE (token->value))
417         {
418         /* Map 'class' to '@class', 'private' to '@private', etc.  */
419         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423         case RID_THROW: token->keyword = RID_AT_THROW; break;
424         case RID_TRY: token->keyword = RID_AT_TRY; break;
425         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426         default: token->keyword = C_RID_CODE (token->value);
427         }
428     }
429   else
430     token->keyword = RID_MAX;
431 }
432
433 /* Update the globals input_location and in_system_header from TOKEN.  */
434 static inline void
435 cp_lexer_set_source_position_from_token (cp_token *token)
436 {
437   if (token->type != CPP_EOF)
438     {
439       input_location = token->location;
440       in_system_header = token->in_system_header;
441     }
442 }
443
444 /* Return a pointer to the next token in the token stream, but do not
445    consume it.  */
446
447 static inline cp_token *
448 cp_lexer_peek_token (cp_lexer *lexer)
449 {
450   if (cp_lexer_debugging_p (lexer))
451     {
452       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454       putc ('\n', cp_lexer_debug_stream);
455     }
456   return lexer->next_token;
457 }
458
459 /* Return true if the next token has the indicated TYPE.  */
460
461 static inline bool
462 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
463 {
464   return cp_lexer_peek_token (lexer)->type == type;
465 }
466
467 /* Return true if the next token does not have the indicated TYPE.  */
468
469 static inline bool
470 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
471 {
472   return !cp_lexer_next_token_is (lexer, type);
473 }
474
475 /* Return true if the next token is the indicated KEYWORD.  */
476
477 static inline bool
478 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
479 {
480   cp_token *token;
481
482   /* Peek at the next token.  */
483   token = cp_lexer_peek_token (lexer);
484   /* Check to see if it is the indicated keyword.  */
485   return token->keyword == keyword;
486 }
487
488 /* Return a pointer to the Nth token in the token stream.  If N is 1,
489    then this is precisely equivalent to cp_lexer_peek_token (except
490    that it is not inline).  One would like to disallow that case, but
491    there is one case (cp_parser_nth_token_starts_template_id) where
492    the caller passes a variable for N and it might be 1.  */
493
494 static cp_token *
495 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
496 {
497   cp_token *token;
498
499   /* N is 1-based, not zero-based.  */
500   gcc_assert (n > 0);
501   
502   if (cp_lexer_debugging_p (lexer))
503     fprintf (cp_lexer_debug_stream,
504              "cp_lexer: peeking ahead %ld at token: ", (long)n);
505
506   --n;
507   token = lexer->next_token;
508   gcc_assert (!n || token != &eof_token);
509   while (n != 0)
510     {
511       ++token;
512       if (token == lexer->last_token)
513         {
514           token = (cp_token *)&eof_token;
515           break;
516         }
517
518       if (token->type != CPP_PURGED)
519         --n;
520     }
521
522   if (cp_lexer_debugging_p (lexer))
523     {
524       cp_lexer_print_token (cp_lexer_debug_stream, token);
525       putc ('\n', cp_lexer_debug_stream);
526     }
527
528   return token;
529 }
530
531 /* Return the next token, and advance the lexer's next_token pointer
532    to point to the next non-purged token.  */
533
534 static cp_token *
535 cp_lexer_consume_token (cp_lexer* lexer)
536 {
537   cp_token *token = lexer->next_token;
538
539   gcc_assert (token != &eof_token);
540
541   do
542     {
543       lexer->next_token++;
544       if (lexer->next_token == lexer->last_token)
545         {
546           lexer->next_token = (cp_token *)&eof_token;
547           break;
548         }
549
550     }
551   while (lexer->next_token->type == CPP_PURGED);
552
553   cp_lexer_set_source_position_from_token (token);
554
555   /* Provide debugging output.  */
556   if (cp_lexer_debugging_p (lexer))
557     {
558       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
559       cp_lexer_print_token (cp_lexer_debug_stream, token);
560       putc ('\n', cp_lexer_debug_stream);
561     }
562
563   return token;
564 }
565
566 /* Permanently remove the next token from the token stream, and
567    advance the next_token pointer to refer to the next non-purged
568    token.  */
569
570 static void
571 cp_lexer_purge_token (cp_lexer *lexer)
572 {
573   cp_token *tok = lexer->next_token;
574
575   gcc_assert (tok != &eof_token);
576   tok->type = CPP_PURGED;
577   tok->location = UNKNOWN_LOCATION;
578   tok->value = NULL_TREE;
579   tok->keyword = RID_MAX;
580
581   do
582     {
583       tok++;
584       if (tok == lexer->last_token)
585         {
586           tok = (cp_token *)&eof_token;
587           break;
588         }
589     }
590   while (tok->type == CPP_PURGED);
591   lexer->next_token = tok;
592 }
593
594 /* Permanently remove all tokens after TOK, up to, but not
595    including, the token that will be returned next by
596    cp_lexer_peek_token.  */
597
598 static void
599 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
600 {
601   cp_token *peek = lexer->next_token;
602
603   if (peek == &eof_token)
604     peek = lexer->last_token;
605
606   gcc_assert (tok < peek);
607
608   for ( tok += 1; tok != peek; tok += 1)
609     {
610       tok->type = CPP_PURGED;
611       tok->location = UNKNOWN_LOCATION;
612       tok->value = NULL_TREE;
613       tok->keyword = RID_MAX;
614     }
615 }
616
617 /* Consume and handle a pragma token.  */
618 static void
619 cp_lexer_handle_pragma (cp_lexer *lexer)
620 {
621   cpp_string s;
622   cp_token *token = cp_lexer_consume_token (lexer);
623   gcc_assert (token->type == CPP_PRAGMA);
624   gcc_assert (token->value);
625
626   s.len = TREE_STRING_LENGTH (token->value);
627   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
628
629   cpp_handle_deferred_pragma (parse_in, &s);
630
631   /* Clearing token->value here means that we will get an ICE if we
632      try to process this #pragma again (which should be impossible).  */
633   token->value = NULL;
634 }
635
636 /* Begin saving tokens.  All tokens consumed after this point will be
637    preserved.  */
638
639 static void
640 cp_lexer_save_tokens (cp_lexer* lexer)
641 {
642   /* Provide debugging output.  */
643   if (cp_lexer_debugging_p (lexer))
644     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
645
646   VEC_safe_push (cp_token_position, heap,
647                  lexer->saved_tokens, lexer->next_token);
648 }
649
650 /* Commit to the portion of the token stream most recently saved.  */
651
652 static void
653 cp_lexer_commit_tokens (cp_lexer* lexer)
654 {
655   /* Provide debugging output.  */
656   if (cp_lexer_debugging_p (lexer))
657     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
658
659   VEC_pop (cp_token_position, lexer->saved_tokens);
660 }
661
662 /* Return all tokens saved since the last call to cp_lexer_save_tokens
663    to the token stream.  Stop saving tokens.  */
664
665 static void
666 cp_lexer_rollback_tokens (cp_lexer* lexer)
667 {
668   /* Provide debugging output.  */
669   if (cp_lexer_debugging_p (lexer))
670     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
671
672   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
673 }
674
675 /* Print a representation of the TOKEN on the STREAM.  */
676
677 #ifdef ENABLE_CHECKING
678
679 static void
680 cp_lexer_print_token (FILE * stream, cp_token *token)
681 {
682   /* We don't use cpp_type2name here because the parser defines
683      a few tokens of its own.  */
684   static const char *const token_names[] = {
685     /* cpplib-defined token types */
686 #define OP(e, s) #e,
687 #define TK(e, s) #e,
688     TTYPE_TABLE
689 #undef OP
690 #undef TK
691     /* C++ parser token types - see "Manifest constants", above.  */
692     "KEYWORD",
693     "TEMPLATE_ID",
694     "NESTED_NAME_SPECIFIER",
695     "PURGED"
696   };
697
698   /* If we have a name for the token, print it out.  Otherwise, we
699      simply give the numeric code.  */
700   gcc_assert (token->type < ARRAY_SIZE(token_names));
701   fputs (token_names[token->type], stream);
702
703   /* For some tokens, print the associated data.  */
704   switch (token->type)
705     {
706     case CPP_KEYWORD:
707       /* Some keywords have a value that is not an IDENTIFIER_NODE.
708          For example, `struct' is mapped to an INTEGER_CST.  */
709       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
710         break;
711       /* else fall through */
712     case CPP_NAME:
713       fputs (IDENTIFIER_POINTER (token->value), stream);
714       break;
715
716     case CPP_STRING:
717     case CPP_WSTRING:
718     case CPP_PRAGMA:
719       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
720       break;
721
722     default:
723       break;
724     }
725 }
726
727 /* Start emitting debugging information.  */
728
729 static void
730 cp_lexer_start_debugging (cp_lexer* lexer)
731 {
732   lexer->debugging_p = true;
733 }
734
735 /* Stop emitting debugging information.  */
736
737 static void
738 cp_lexer_stop_debugging (cp_lexer* lexer)
739 {
740   lexer->debugging_p = false;
741 }
742
743 #endif /* ENABLE_CHECKING */
744
745 /* Create a new cp_token_cache, representing a range of tokens.  */
746
747 static cp_token_cache *
748 cp_token_cache_new (cp_token *first, cp_token *last)
749 {
750   cp_token_cache *cache = GGC_NEW (cp_token_cache);
751   cache->first = first;
752   cache->last = last;
753   return cache;
754 }
755
756 \f
757 /* Decl-specifiers.  */
758
759 static void clear_decl_specs
760   (cp_decl_specifier_seq *);
761
762 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
763
764 static void
765 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
766 {
767   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
768 }
769
770 /* Declarators.  */
771
772 /* Nothing other than the parser should be creating declarators;
773    declarators are a semi-syntactic representation of C++ entities.
774    Other parts of the front end that need to create entities (like
775    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
776
777 static cp_declarator *make_call_declarator
778   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
779 static cp_declarator *make_array_declarator
780   (cp_declarator *, tree);
781 static cp_declarator *make_pointer_declarator
782   (cp_cv_quals, cp_declarator *);
783 static cp_declarator *make_reference_declarator
784   (cp_cv_quals, cp_declarator *);
785 static cp_parameter_declarator *make_parameter_declarator
786   (cp_decl_specifier_seq *, cp_declarator *, tree);
787 static cp_declarator *make_ptrmem_declarator
788   (cp_cv_quals, tree, cp_declarator *);
789
790 cp_declarator *cp_error_declarator;
791
792 /* The obstack on which declarators and related data structures are
793    allocated.  */
794 static struct obstack declarator_obstack;
795
796 /* Alloc BYTES from the declarator memory pool.  */
797
798 static inline void *
799 alloc_declarator (size_t bytes)
800 {
801   return obstack_alloc (&declarator_obstack, bytes);
802 }
803
804 /* Allocate a declarator of the indicated KIND.  Clear fields that are
805    common to all declarators.  */
806
807 static cp_declarator *
808 make_declarator (cp_declarator_kind kind)
809 {
810   cp_declarator *declarator;
811
812   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
813   declarator->kind = kind;
814   declarator->attributes = NULL_TREE;
815   declarator->declarator = NULL;
816
817   return declarator;
818 }
819
820 /* Make a declarator for a generalized identifier.  If non-NULL, the
821    identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
822    just UNQUALIFIED_NAME.  */
823
824 static cp_declarator *
825 make_id_declarator (tree qualifying_scope, tree unqualified_name)
826 {
827   cp_declarator *declarator;
828
829   /* It is valid to write:
830
831        class C { void f(); };
832        typedef C D;
833        void D::f();
834
835      The standard is not clear about whether `typedef const C D' is
836      legal; as of 2002-09-15 the committee is considering that
837      question.  EDG 3.0 allows that syntax.  Therefore, we do as
838      well.  */
839   if (qualifying_scope && TYPE_P (qualifying_scope))
840     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
841
842   declarator = make_declarator (cdk_id);
843   declarator->u.id.qualifying_scope = qualifying_scope;
844   declarator->u.id.unqualified_name = unqualified_name;
845   declarator->u.id.sfk = sfk_none;
846
847   return declarator;
848 }
849
850 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
851    of modifiers such as const or volatile to apply to the pointer
852    type, represented as identifiers.  */
853
854 cp_declarator *
855 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
856 {
857   cp_declarator *declarator;
858
859   declarator = make_declarator (cdk_pointer);
860   declarator->declarator = target;
861   declarator->u.pointer.qualifiers = cv_qualifiers;
862   declarator->u.pointer.class_type = NULL_TREE;
863
864   return declarator;
865 }
866
867 /* Like make_pointer_declarator -- but for references.  */
868
869 cp_declarator *
870 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
871 {
872   cp_declarator *declarator;
873
874   declarator = make_declarator (cdk_reference);
875   declarator->declarator = target;
876   declarator->u.pointer.qualifiers = cv_qualifiers;
877   declarator->u.pointer.class_type = NULL_TREE;
878
879   return declarator;
880 }
881
882 /* Like make_pointer_declarator -- but for a pointer to a non-static
883    member of CLASS_TYPE.  */
884
885 cp_declarator *
886 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
887                         cp_declarator *pointee)
888 {
889   cp_declarator *declarator;
890
891   declarator = make_declarator (cdk_ptrmem);
892   declarator->declarator = pointee;
893   declarator->u.pointer.qualifiers = cv_qualifiers;
894   declarator->u.pointer.class_type = class_type;
895
896   return declarator;
897 }
898
899 /* Make a declarator for the function given by TARGET, with the
900    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
901    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
902    indicates what exceptions can be thrown.  */
903
904 cp_declarator *
905 make_call_declarator (cp_declarator *target,
906                       cp_parameter_declarator *parms,
907                       cp_cv_quals cv_qualifiers,
908                       tree exception_specification)
909 {
910   cp_declarator *declarator;
911
912   declarator = make_declarator (cdk_function);
913   declarator->declarator = target;
914   declarator->u.function.parameters = parms;
915   declarator->u.function.qualifiers = cv_qualifiers;
916   declarator->u.function.exception_specification = exception_specification;
917
918   return declarator;
919 }
920
921 /* Make a declarator for an array of BOUNDS elements, each of which is
922    defined by ELEMENT.  */
923
924 cp_declarator *
925 make_array_declarator (cp_declarator *element, tree bounds)
926 {
927   cp_declarator *declarator;
928
929   declarator = make_declarator (cdk_array);
930   declarator->declarator = element;
931   declarator->u.array.bounds = bounds;
932
933   return declarator;
934 }
935
936 cp_parameter_declarator *no_parameters;
937
938 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939    DECLARATOR and DEFAULT_ARGUMENT.  */
940
941 cp_parameter_declarator *
942 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
943                            cp_declarator *declarator,
944                            tree default_argument)
945 {
946   cp_parameter_declarator *parameter;
947
948   parameter = ((cp_parameter_declarator *)
949                alloc_declarator (sizeof (cp_parameter_declarator)));
950   parameter->next = NULL;
951   if (decl_specifiers)
952     parameter->decl_specifiers = *decl_specifiers;
953   else
954     clear_decl_specs (&parameter->decl_specifiers);
955   parameter->declarator = declarator;
956   parameter->default_argument = default_argument;
957   parameter->ellipsis_p = false;
958
959   return parameter;
960 }
961
962 /* The parser.  */
963
964 /* Overview
965    --------
966
967    A cp_parser parses the token stream as specified by the C++
968    grammar.  Its job is purely parsing, not semantic analysis.  For
969    example, the parser breaks the token stream into declarators,
970    expressions, statements, and other similar syntactic constructs.
971    It does not check that the types of the expressions on either side
972    of an assignment-statement are compatible, or that a function is
973    not declared with a parameter of type `void'.
974
975    The parser invokes routines elsewhere in the compiler to perform
976    semantic analysis and to build up the abstract syntax tree for the
977    code processed.
978
979    The parser (and the template instantiation code, which is, in a
980    way, a close relative of parsing) are the only parts of the
981    compiler that should be calling push_scope and pop_scope, or
982    related functions.  The parser (and template instantiation code)
983    keeps track of what scope is presently active; everything else
984    should simply honor that.  (The code that generates static
985    initializers may also need to set the scope, in order to check
986    access control correctly when emitting the initializers.)
987
988    Methodology
989    -----------
990
991    The parser is of the standard recursive-descent variety.  Upcoming
992    tokens in the token stream are examined in order to determine which
993    production to use when parsing a non-terminal.  Some C++ constructs
994    require arbitrary look ahead to disambiguate.  For example, it is
995    impossible, in the general case, to tell whether a statement is an
996    expression or declaration without scanning the entire statement.
997    Therefore, the parser is capable of "parsing tentatively."  When the
998    parser is not sure what construct comes next, it enters this mode.
999    Then, while we attempt to parse the construct, the parser queues up
1000    error messages, rather than issuing them immediately, and saves the
1001    tokens it consumes.  If the construct is parsed successfully, the
1002    parser "commits", i.e., it issues any queued error messages and
1003    the tokens that were being preserved are permanently discarded.
1004    If, however, the construct is not parsed successfully, the parser
1005    rolls back its state completely so that it can resume parsing using
1006    a different alternative.
1007
1008    Future Improvements
1009    -------------------
1010
1011    The performance of the parser could probably be improved substantially.
1012    We could often eliminate the need to parse tentatively by looking ahead
1013    a little bit.  In some places, this approach might not entirely eliminate
1014    the need to parse tentatively, but it might still speed up the average
1015    case.  */
1016
1017 /* Flags that are passed to some parsing functions.  These values can
1018    be bitwise-ored together.  */
1019
1020 typedef enum cp_parser_flags
1021 {
1022   /* No flags.  */
1023   CP_PARSER_FLAGS_NONE = 0x0,
1024   /* The construct is optional.  If it is not present, then no error
1025      should be issued.  */
1026   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1027   /* When parsing a type-specifier, do not allow user-defined types.  */
1028   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1029 } cp_parser_flags;
1030
1031 /* The different kinds of declarators we want to parse.  */
1032
1033 typedef enum cp_parser_declarator_kind
1034 {
1035   /* We want an abstract declarator.  */
1036   CP_PARSER_DECLARATOR_ABSTRACT,
1037   /* We want a named declarator.  */
1038   CP_PARSER_DECLARATOR_NAMED,
1039   /* We don't mind, but the name must be an unqualified-id.  */
1040   CP_PARSER_DECLARATOR_EITHER
1041 } cp_parser_declarator_kind;
1042
1043 /* The precedence values used to parse binary expressions.  The minimum value
1044    of PREC must be 1, because zero is reserved to quickly discriminate
1045    binary operators from other tokens.  */
1046
1047 enum cp_parser_prec
1048 {
1049   PREC_NOT_OPERATOR,
1050   PREC_LOGICAL_OR_EXPRESSION,
1051   PREC_LOGICAL_AND_EXPRESSION,
1052   PREC_INCLUSIVE_OR_EXPRESSION,
1053   PREC_EXCLUSIVE_OR_EXPRESSION,
1054   PREC_AND_EXPRESSION,
1055   PREC_EQUALITY_EXPRESSION,
1056   PREC_RELATIONAL_EXPRESSION,
1057   PREC_SHIFT_EXPRESSION,
1058   PREC_ADDITIVE_EXPRESSION,
1059   PREC_MULTIPLICATIVE_EXPRESSION,
1060   PREC_PM_EXPRESSION,
1061   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1062 };
1063
1064 /* A mapping from a token type to a corresponding tree node type, with a
1065    precedence value.  */
1066
1067 typedef struct cp_parser_binary_operations_map_node
1068 {
1069   /* The token type.  */
1070   enum cpp_ttype token_type;
1071   /* The corresponding tree code.  */
1072   enum tree_code tree_type;
1073   /* The precedence of this operator.  */
1074   enum cp_parser_prec prec;
1075 } cp_parser_binary_operations_map_node;
1076
1077 /* The status of a tentative parse.  */
1078
1079 typedef enum cp_parser_status_kind
1080 {
1081   /* No errors have occurred.  */
1082   CP_PARSER_STATUS_KIND_NO_ERROR,
1083   /* An error has occurred.  */
1084   CP_PARSER_STATUS_KIND_ERROR,
1085   /* We are committed to this tentative parse, whether or not an error
1086      has occurred.  */
1087   CP_PARSER_STATUS_KIND_COMMITTED
1088 } cp_parser_status_kind;
1089
1090 typedef struct cp_parser_expression_stack_entry
1091 {
1092   tree lhs;
1093   enum tree_code tree_type;
1094   int prec;
1095 } cp_parser_expression_stack_entry;
1096
1097 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1098    entries because precedence levels on the stack are monotonically
1099    increasing.  */
1100 typedef struct cp_parser_expression_stack_entry
1101   cp_parser_expression_stack[NUM_PREC_VALUES];
1102
1103 /* Context that is saved and restored when parsing tentatively.  */
1104 typedef struct cp_parser_context GTY (())
1105 {
1106   /* If this is a tentative parsing context, the status of the
1107      tentative parse.  */
1108   enum cp_parser_status_kind status;
1109   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1110      that are looked up in this context must be looked up both in the
1111      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112      the context of the containing expression.  */
1113   tree object_type;
1114
1115   /* The next parsing context in the stack.  */
1116   struct cp_parser_context *next;
1117 } cp_parser_context;
1118
1119 /* Prototypes.  */
1120
1121 /* Constructors and destructors.  */
1122
1123 static cp_parser_context *cp_parser_context_new
1124   (cp_parser_context *);
1125
1126 /* Class variables.  */
1127
1128 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1129
1130 /* The operator-precedence table used by cp_parser_binary_expression.
1131    Transformed into an associative array (binops_by_token) by
1132    cp_parser_new.  */
1133
1134 static const cp_parser_binary_operations_map_node binops[] = {
1135   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1136   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1137
1138   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141
1142   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144
1145   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147
1148   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1153   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1154
1155   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1156   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1157
1158   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1159
1160   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1161
1162   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1163
1164   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1165
1166   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1167 };
1168
1169 /* The same as binops, but initialized by cp_parser_new so that
1170    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1171    for speed.  */
1172 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1173
1174 /* Constructors and destructors.  */
1175
1176 /* Construct a new context.  The context below this one on the stack
1177    is given by NEXT.  */
1178
1179 static cp_parser_context *
1180 cp_parser_context_new (cp_parser_context* next)
1181 {
1182   cp_parser_context *context;
1183
1184   /* Allocate the storage.  */
1185   if (cp_parser_context_free_list != NULL)
1186     {
1187       /* Pull the first entry from the free list.  */
1188       context = cp_parser_context_free_list;
1189       cp_parser_context_free_list = context->next;
1190       memset (context, 0, sizeof (*context));
1191     }
1192   else
1193     context = GGC_CNEW (cp_parser_context);
1194
1195   /* No errors have occurred yet in this context.  */
1196   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1197   /* If this is not the bottomost context, copy information that we
1198      need from the previous context.  */
1199   if (next)
1200     {
1201       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202          expression, then we are parsing one in this context, too.  */
1203       context->object_type = next->object_type;
1204       /* Thread the stack.  */
1205       context->next = next;
1206     }
1207
1208   return context;
1209 }
1210
1211 /* The cp_parser structure represents the C++ parser.  */
1212
1213 typedef struct cp_parser GTY(())
1214 {
1215   /* The lexer from which we are obtaining tokens.  */
1216   cp_lexer *lexer;
1217
1218   /* The scope in which names should be looked up.  If NULL_TREE, then
1219      we look up names in the scope that is currently open in the
1220      source program.  If non-NULL, this is either a TYPE or
1221      NAMESPACE_DECL for the scope in which we should look.  It can
1222      also be ERROR_MARK, when we've parsed a bogus scope.
1223
1224      This value is not cleared automatically after a name is looked
1225      up, so we must be careful to clear it before starting a new look
1226      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1227      will look up `Z' in the scope of `X', rather than the current
1228      scope.)  Unfortunately, it is difficult to tell when name lookup
1229      is complete, because we sometimes peek at a token, look it up,
1230      and then decide not to consume it.   */
1231   tree scope;
1232
1233   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234      last lookup took place.  OBJECT_SCOPE is used if an expression
1235      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236      respectively.  QUALIFYING_SCOPE is used for an expression of the
1237      form "X::Y"; it refers to X.  */
1238   tree object_scope;
1239   tree qualifying_scope;
1240
1241   /* A stack of parsing contexts.  All but the bottom entry on the
1242      stack will be tentative contexts.
1243
1244      We parse tentatively in order to determine which construct is in
1245      use in some situations.  For example, in order to determine
1246      whether a statement is an expression-statement or a
1247      declaration-statement we parse it tentatively as a
1248      declaration-statement.  If that fails, we then reparse the same
1249      token stream as an expression-statement.  */
1250   cp_parser_context *context;
1251
1252   /* True if we are parsing GNU C++.  If this flag is not set, then
1253      GNU extensions are not recognized.  */
1254   bool allow_gnu_extensions_p;
1255
1256   /* TRUE if the `>' token should be interpreted as the greater-than
1257      operator.  FALSE if it is the end of a template-id or
1258      template-parameter-list.  */
1259   bool greater_than_is_operator_p;
1260
1261   /* TRUE if default arguments are allowed within a parameter list
1262      that starts at this point. FALSE if only a gnu extension makes
1263      them permissible.  */
1264   bool default_arg_ok_p;
1265
1266   /* TRUE if we are parsing an integral constant-expression.  See
1267      [expr.const] for a precise definition.  */
1268   bool integral_constant_expression_p;
1269
1270   /* TRUE if we are parsing an integral constant-expression -- but a
1271      non-constant expression should be permitted as well.  This flag
1272      is used when parsing an array bound so that GNU variable-length
1273      arrays are tolerated.  */
1274   bool allow_non_integral_constant_expression_p;
1275
1276   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277      been seen that makes the expression non-constant.  */
1278   bool non_integral_constant_expression_p;
1279
1280   /* TRUE if local variable names and `this' are forbidden in the
1281      current context.  */
1282   bool local_variables_forbidden_p;
1283
1284   /* TRUE if the declaration we are parsing is part of a
1285      linkage-specification of the form `extern string-literal
1286      declaration'.  */
1287   bool in_unbraced_linkage_specification_p;
1288
1289   /* TRUE if we are presently parsing a declarator, after the
1290      direct-declarator.  */
1291   bool in_declarator_p;
1292
1293   /* TRUE if we are presently parsing a template-argument-list.  */
1294   bool in_template_argument_list_p;
1295
1296   /* TRUE if we are presently parsing the body of an
1297      iteration-statement.  */
1298   bool in_iteration_statement_p;
1299
1300   /* TRUE if we are presently parsing the body of a switch
1301      statement.  */
1302   bool in_switch_statement_p;
1303
1304   /* TRUE if we are parsing a type-id in an expression context.  In
1305      such a situation, both "type (expr)" and "type (type)" are valid
1306      alternatives.  */
1307   bool in_type_id_in_expr_p;
1308
1309   /* TRUE if we are currently in a header file where declarations are
1310      implicitly extern "C".  */
1311   bool implicit_extern_c;
1312
1313   /* TRUE if strings in expressions should be translated to the execution
1314      character set.  */
1315   bool translate_strings_p;
1316
1317   /* If non-NULL, then we are parsing a construct where new type
1318      definitions are not permitted.  The string stored here will be
1319      issued as an error message if a type is defined.  */
1320   const char *type_definition_forbidden_message;
1321
1322   /* A list of lists. The outer list is a stack, used for member
1323      functions of local classes. At each level there are two sub-list,
1324      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1325      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1326      TREE_VALUE's. The functions are chained in reverse declaration
1327      order.
1328
1329      The TREE_PURPOSE sublist contains those functions with default
1330      arguments that need post processing, and the TREE_VALUE sublist
1331      contains those functions with definitions that need post
1332      processing.
1333
1334      These lists can only be processed once the outermost class being
1335      defined is complete.  */
1336   tree unparsed_functions_queues;
1337
1338   /* The number of classes whose definitions are currently in
1339      progress.  */
1340   unsigned num_classes_being_defined;
1341
1342   /* The number of template parameter lists that apply directly to the
1343      current declaration.  */
1344   unsigned num_template_parameter_lists;
1345 } cp_parser;
1346
1347 /* The type of a function that parses some kind of expression.  */
1348 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1349
1350 /* Prototypes.  */
1351
1352 /* Constructors and destructors.  */
1353
1354 static cp_parser *cp_parser_new
1355   (void);
1356
1357 /* Routines to parse various constructs.
1358
1359    Those that return `tree' will return the error_mark_node (rather
1360    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1361    Sometimes, they will return an ordinary node if error-recovery was
1362    attempted, even though a parse error occurred.  So, to check
1363    whether or not a parse error occurred, you should always use
1364    cp_parser_error_occurred.  If the construct is optional (indicated
1365    either by an `_opt' in the name of the function that does the
1366    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1367    the construct is not present.  */
1368
1369 /* Lexical conventions [gram.lex]  */
1370
1371 static tree cp_parser_identifier
1372   (cp_parser *);
1373 static tree cp_parser_string_literal
1374   (cp_parser *, bool, bool);
1375
1376 /* Basic concepts [gram.basic]  */
1377
1378 static bool cp_parser_translation_unit
1379   (cp_parser *);
1380
1381 /* Expressions [gram.expr]  */
1382
1383 static tree cp_parser_primary_expression
1384   (cp_parser *, bool, cp_id_kind *, tree *);
1385 static tree cp_parser_id_expression
1386   (cp_parser *, bool, bool, bool *, bool);
1387 static tree cp_parser_unqualified_id
1388   (cp_parser *, bool, bool, bool);
1389 static tree cp_parser_nested_name_specifier_opt
1390   (cp_parser *, bool, bool, bool, bool);
1391 static tree cp_parser_nested_name_specifier
1392   (cp_parser *, bool, bool, bool, bool);
1393 static tree cp_parser_class_or_namespace_name
1394   (cp_parser *, bool, bool, bool, bool, bool);
1395 static tree cp_parser_postfix_expression
1396   (cp_parser *, bool, bool);
1397 static tree cp_parser_postfix_open_square_expression
1398   (cp_parser *, tree, bool);
1399 static tree cp_parser_postfix_dot_deref_expression
1400   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1401 static tree cp_parser_parenthesized_expression_list
1402   (cp_parser *, bool, bool, bool *);
1403 static void cp_parser_pseudo_destructor_name
1404   (cp_parser *, tree *, tree *);
1405 static tree cp_parser_unary_expression
1406   (cp_parser *, bool, bool);
1407 static enum tree_code cp_parser_unary_operator
1408   (cp_token *);
1409 static tree cp_parser_new_expression
1410   (cp_parser *);
1411 static tree cp_parser_new_placement
1412   (cp_parser *);
1413 static tree cp_parser_new_type_id
1414   (cp_parser *, tree *);
1415 static cp_declarator *cp_parser_new_declarator_opt
1416   (cp_parser *);
1417 static cp_declarator *cp_parser_direct_new_declarator
1418   (cp_parser *);
1419 static tree cp_parser_new_initializer
1420   (cp_parser *);
1421 static tree cp_parser_delete_expression
1422   (cp_parser *);
1423 static tree cp_parser_cast_expression
1424   (cp_parser *, bool, bool);
1425 static tree cp_parser_binary_expression
1426   (cp_parser *, bool);
1427 static tree cp_parser_question_colon_clause
1428   (cp_parser *, tree);
1429 static tree cp_parser_assignment_expression
1430   (cp_parser *, bool);
1431 static enum tree_code cp_parser_assignment_operator_opt
1432   (cp_parser *);
1433 static tree cp_parser_expression
1434   (cp_parser *, bool);
1435 static tree cp_parser_constant_expression
1436   (cp_parser *, bool, bool *);
1437 static tree cp_parser_builtin_offsetof
1438   (cp_parser *);
1439
1440 /* Statements [gram.stmt.stmt]  */
1441
1442 static void cp_parser_statement
1443   (cp_parser *, tree);
1444 static tree cp_parser_labeled_statement
1445   (cp_parser *, tree);
1446 static tree cp_parser_expression_statement
1447   (cp_parser *, tree);
1448 static tree cp_parser_compound_statement
1449   (cp_parser *, tree, bool);
1450 static void cp_parser_statement_seq_opt
1451   (cp_parser *, tree);
1452 static tree cp_parser_selection_statement
1453   (cp_parser *);
1454 static tree cp_parser_condition
1455   (cp_parser *);
1456 static tree cp_parser_iteration_statement
1457   (cp_parser *);
1458 static void cp_parser_for_init_statement
1459   (cp_parser *);
1460 static tree cp_parser_jump_statement
1461   (cp_parser *);
1462 static void cp_parser_declaration_statement
1463   (cp_parser *);
1464
1465 static tree cp_parser_implicitly_scoped_statement
1466   (cp_parser *);
1467 static void cp_parser_already_scoped_statement
1468   (cp_parser *);
1469
1470 /* Declarations [gram.dcl.dcl] */
1471
1472 static void cp_parser_declaration_seq_opt
1473   (cp_parser *);
1474 static void cp_parser_declaration
1475   (cp_parser *);
1476 static void cp_parser_block_declaration
1477   (cp_parser *, bool);
1478 static void cp_parser_simple_declaration
1479   (cp_parser *, bool);
1480 static void cp_parser_decl_specifier_seq
1481   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1482 static tree cp_parser_storage_class_specifier_opt
1483   (cp_parser *);
1484 static tree cp_parser_function_specifier_opt
1485   (cp_parser *, cp_decl_specifier_seq *);
1486 static tree cp_parser_type_specifier
1487   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1488    int *, bool *);
1489 static tree cp_parser_simple_type_specifier
1490   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1491 static tree cp_parser_type_name
1492   (cp_parser *);
1493 static tree cp_parser_elaborated_type_specifier
1494   (cp_parser *, bool, bool);
1495 static tree cp_parser_enum_specifier
1496   (cp_parser *);
1497 static void cp_parser_enumerator_list
1498   (cp_parser *, tree);
1499 static void cp_parser_enumerator_definition
1500   (cp_parser *, tree);
1501 static tree cp_parser_namespace_name
1502   (cp_parser *);
1503 static void cp_parser_namespace_definition
1504   (cp_parser *);
1505 static void cp_parser_namespace_body
1506   (cp_parser *);
1507 static tree cp_parser_qualified_namespace_specifier
1508   (cp_parser *);
1509 static void cp_parser_namespace_alias_definition
1510   (cp_parser *);
1511 static void cp_parser_using_declaration
1512   (cp_parser *);
1513 static void cp_parser_using_directive
1514   (cp_parser *);
1515 static void cp_parser_asm_definition
1516   (cp_parser *);
1517 static void cp_parser_linkage_specification
1518   (cp_parser *);
1519
1520 /* Declarators [gram.dcl.decl] */
1521
1522 static tree cp_parser_init_declarator
1523   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1524 static cp_declarator *cp_parser_declarator
1525   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1526 static cp_declarator *cp_parser_direct_declarator
1527   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1528 static enum tree_code cp_parser_ptr_operator
1529   (cp_parser *, tree *, cp_cv_quals *);
1530 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1531   (cp_parser *);
1532 static tree cp_parser_declarator_id
1533   (cp_parser *);
1534 static tree cp_parser_type_id
1535   (cp_parser *);
1536 static void cp_parser_type_specifier_seq
1537   (cp_parser *, bool, cp_decl_specifier_seq *);
1538 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1539   (cp_parser *);
1540 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1541   (cp_parser *, bool *);
1542 static cp_parameter_declarator *cp_parser_parameter_declaration
1543   (cp_parser *, bool, bool *);
1544 static void cp_parser_function_body
1545   (cp_parser *);
1546 static tree cp_parser_initializer
1547   (cp_parser *, bool *, bool *);
1548 static tree cp_parser_initializer_clause
1549   (cp_parser *, bool *);
1550 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1551   (cp_parser *, bool *);
1552
1553 static bool cp_parser_ctor_initializer_opt_and_function_body
1554   (cp_parser *);
1555
1556 /* Classes [gram.class] */
1557
1558 static tree cp_parser_class_name
1559   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1560 static tree cp_parser_class_specifier
1561   (cp_parser *);
1562 static tree cp_parser_class_head
1563   (cp_parser *, bool *, tree *);
1564 static enum tag_types cp_parser_class_key
1565   (cp_parser *);
1566 static void cp_parser_member_specification_opt
1567   (cp_parser *);
1568 static void cp_parser_member_declaration
1569   (cp_parser *);
1570 static tree cp_parser_pure_specifier
1571   (cp_parser *);
1572 static tree cp_parser_constant_initializer
1573   (cp_parser *);
1574
1575 /* Derived classes [gram.class.derived] */
1576
1577 static tree cp_parser_base_clause
1578   (cp_parser *);
1579 static tree cp_parser_base_specifier
1580   (cp_parser *);
1581
1582 /* Special member functions [gram.special] */
1583
1584 static tree cp_parser_conversion_function_id
1585   (cp_parser *);
1586 static tree cp_parser_conversion_type_id
1587   (cp_parser *);
1588 static cp_declarator *cp_parser_conversion_declarator_opt
1589   (cp_parser *);
1590 static bool cp_parser_ctor_initializer_opt
1591   (cp_parser *);
1592 static void cp_parser_mem_initializer_list
1593   (cp_parser *);
1594 static tree cp_parser_mem_initializer
1595   (cp_parser *);
1596 static tree cp_parser_mem_initializer_id
1597   (cp_parser *);
1598
1599 /* Overloading [gram.over] */
1600
1601 static tree cp_parser_operator_function_id
1602   (cp_parser *);
1603 static tree cp_parser_operator
1604   (cp_parser *);
1605
1606 /* Templates [gram.temp] */
1607
1608 static void cp_parser_template_declaration
1609   (cp_parser *, bool);
1610 static tree cp_parser_template_parameter_list
1611   (cp_parser *);
1612 static tree cp_parser_template_parameter
1613   (cp_parser *, bool *);
1614 static tree cp_parser_type_parameter
1615   (cp_parser *);
1616 static tree cp_parser_template_id
1617   (cp_parser *, bool, bool, bool);
1618 static tree cp_parser_template_name
1619   (cp_parser *, bool, bool, bool, bool *);
1620 static tree cp_parser_template_argument_list
1621   (cp_parser *);
1622 static tree cp_parser_template_argument
1623   (cp_parser *);
1624 static void cp_parser_explicit_instantiation
1625   (cp_parser *);
1626 static void cp_parser_explicit_specialization
1627   (cp_parser *);
1628
1629 /* Exception handling [gram.exception] */
1630
1631 static tree cp_parser_try_block
1632   (cp_parser *);
1633 static bool cp_parser_function_try_block
1634   (cp_parser *);
1635 static void cp_parser_handler_seq
1636   (cp_parser *);
1637 static void cp_parser_handler
1638   (cp_parser *);
1639 static tree cp_parser_exception_declaration
1640   (cp_parser *);
1641 static tree cp_parser_throw_expression
1642   (cp_parser *);
1643 static tree cp_parser_exception_specification_opt
1644   (cp_parser *);
1645 static tree cp_parser_type_id_list
1646   (cp_parser *);
1647
1648 /* GNU Extensions */
1649
1650 static tree cp_parser_asm_specification_opt
1651   (cp_parser *);
1652 static tree cp_parser_asm_operand_list
1653   (cp_parser *);
1654 static tree cp_parser_asm_clobber_list
1655   (cp_parser *);
1656 static tree cp_parser_attributes_opt
1657   (cp_parser *);
1658 static tree cp_parser_attribute_list
1659   (cp_parser *);
1660 static bool cp_parser_extension_opt
1661   (cp_parser *, int *);
1662 static void cp_parser_label_declaration
1663   (cp_parser *);
1664
1665 /* Objective-C++ Productions */
1666
1667 static tree cp_parser_objc_message_receiver
1668   (cp_parser *);
1669 static tree cp_parser_objc_message_args
1670   (cp_parser *);
1671 static tree cp_parser_objc_message_expression
1672   (cp_parser *);
1673 static tree cp_parser_objc_encode_expression
1674   (cp_parser *);
1675 static tree cp_parser_objc_defs_expression
1676   (cp_parser *);
1677 static tree cp_parser_objc_protocol_expression
1678   (cp_parser *);
1679 static tree cp_parser_objc_selector_expression
1680   (cp_parser *);
1681 static tree cp_parser_objc_expression
1682   (cp_parser *);
1683 static bool cp_parser_objc_selector_p
1684   (enum cpp_ttype);
1685 static tree cp_parser_objc_selector
1686   (cp_parser *);
1687 static tree cp_parser_objc_protocol_refs_opt
1688   (cp_parser *);
1689 static void cp_parser_objc_declaration
1690   (cp_parser *);
1691 static tree cp_parser_objc_statement
1692   (cp_parser *);
1693
1694 /* Utility Routines */
1695
1696 static tree cp_parser_lookup_name
1697   (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1698 static tree cp_parser_lookup_name_simple
1699   (cp_parser *, tree);
1700 static tree cp_parser_maybe_treat_template_as_class
1701   (tree, bool);
1702 static bool cp_parser_check_declarator_template_parameters
1703   (cp_parser *, cp_declarator *);
1704 static bool cp_parser_check_template_parameters
1705   (cp_parser *, unsigned);
1706 static tree cp_parser_simple_cast_expression
1707   (cp_parser *);
1708 static tree cp_parser_global_scope_opt
1709   (cp_parser *, bool);
1710 static bool cp_parser_constructor_declarator_p
1711   (cp_parser *, bool);
1712 static tree cp_parser_function_definition_from_specifiers_and_declarator
1713   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1714 static tree cp_parser_function_definition_after_declarator
1715   (cp_parser *, bool);
1716 static void cp_parser_template_declaration_after_export
1717   (cp_parser *, bool);
1718 static tree cp_parser_single_declaration
1719   (cp_parser *, bool, bool *);
1720 static tree cp_parser_functional_cast
1721   (cp_parser *, tree);
1722 static tree cp_parser_save_member_function_body
1723   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1724 static tree cp_parser_enclosed_template_argument_list
1725   (cp_parser *);
1726 static void cp_parser_save_default_args
1727   (cp_parser *, tree);
1728 static void cp_parser_late_parsing_for_member
1729   (cp_parser *, tree);
1730 static void cp_parser_late_parsing_default_args
1731   (cp_parser *, tree);
1732 static tree cp_parser_sizeof_operand
1733   (cp_parser *, enum rid);
1734 static bool cp_parser_declares_only_class_p
1735   (cp_parser *);
1736 static void cp_parser_set_storage_class
1737   (cp_decl_specifier_seq *, cp_storage_class);
1738 static void cp_parser_set_decl_spec_type
1739   (cp_decl_specifier_seq *, tree, bool);
1740 static bool cp_parser_friend_p
1741   (const cp_decl_specifier_seq *);
1742 static cp_token *cp_parser_require
1743   (cp_parser *, enum cpp_ttype, const char *);
1744 static cp_token *cp_parser_require_keyword
1745   (cp_parser *, enum rid, const char *);
1746 static bool cp_parser_token_starts_function_definition_p
1747   (cp_token *);
1748 static bool cp_parser_next_token_starts_class_definition_p
1749   (cp_parser *);
1750 static bool cp_parser_next_token_ends_template_argument_p
1751   (cp_parser *);
1752 static bool cp_parser_nth_token_starts_template_argument_list_p
1753   (cp_parser *, size_t);
1754 static enum tag_types cp_parser_token_is_class_key
1755   (cp_token *);
1756 static void cp_parser_check_class_key
1757   (enum tag_types, tree type);
1758 static void cp_parser_check_access_in_redeclaration
1759   (tree type);
1760 static bool cp_parser_optional_template_keyword
1761   (cp_parser *);
1762 static void cp_parser_pre_parsed_nested_name_specifier
1763   (cp_parser *);
1764 static void cp_parser_cache_group
1765   (cp_parser *, enum cpp_ttype, unsigned);
1766 static void cp_parser_parse_tentatively
1767   (cp_parser *);
1768 static void cp_parser_commit_to_tentative_parse
1769   (cp_parser *);
1770 static void cp_parser_abort_tentative_parse
1771   (cp_parser *);
1772 static bool cp_parser_parse_definitely
1773   (cp_parser *);
1774 static inline bool cp_parser_parsing_tentatively
1775   (cp_parser *);
1776 static bool cp_parser_uncommitted_to_tentative_parse_p
1777   (cp_parser *);
1778 static void cp_parser_error
1779   (cp_parser *, const char *);
1780 static void cp_parser_name_lookup_error
1781   (cp_parser *, tree, tree, const char *);
1782 static bool cp_parser_simulate_error
1783   (cp_parser *);
1784 static void cp_parser_check_type_definition
1785   (cp_parser *);
1786 static void cp_parser_check_for_definition_in_return_type
1787   (cp_declarator *, tree);
1788 static void cp_parser_check_for_invalid_template_id
1789   (cp_parser *, tree);
1790 static bool cp_parser_non_integral_constant_expression
1791   (cp_parser *, const char *);
1792 static void cp_parser_diagnose_invalid_type_name
1793   (cp_parser *, tree, tree);
1794 static bool cp_parser_parse_and_diagnose_invalid_type_name
1795   (cp_parser *);
1796 static int cp_parser_skip_to_closing_parenthesis
1797   (cp_parser *, bool, bool, bool);
1798 static void cp_parser_skip_to_end_of_statement
1799   (cp_parser *);
1800 static void cp_parser_consume_semicolon_at_end_of_statement
1801   (cp_parser *);
1802 static void cp_parser_skip_to_end_of_block_or_statement
1803   (cp_parser *);
1804 static void cp_parser_skip_to_closing_brace
1805   (cp_parser *);
1806 static void cp_parser_skip_until_found
1807   (cp_parser *, enum cpp_ttype, const char *);
1808 static bool cp_parser_error_occurred
1809   (cp_parser *);
1810 static bool cp_parser_allow_gnu_extensions_p
1811   (cp_parser *);
1812 static bool cp_parser_is_string_literal
1813   (cp_token *);
1814 static bool cp_parser_is_keyword
1815   (cp_token *, enum rid);
1816 static tree cp_parser_make_typename_type
1817   (cp_parser *, tree, tree);
1818
1819 /* Returns nonzero if we are parsing tentatively.  */
1820
1821 static inline bool
1822 cp_parser_parsing_tentatively (cp_parser* parser)
1823 {
1824   return parser->context->next != NULL;
1825 }
1826
1827 /* Returns nonzero if TOKEN is a string literal.  */
1828
1829 static bool
1830 cp_parser_is_string_literal (cp_token* token)
1831 {
1832   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1833 }
1834
1835 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1836
1837 static bool
1838 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1839 {
1840   return token->keyword == keyword;
1841 }
1842
1843 /* A minimum or maximum operator has been seen.  As these are
1844    deprecated, issue a warning.  */
1845
1846 static inline void
1847 cp_parser_warn_min_max (void)
1848 {
1849   if (warn_deprecated && !in_system_header)
1850     warning (0, "minimum/maximum operators are deprecated");
1851 }
1852
1853 /* If not parsing tentatively, issue a diagnostic of the form
1854       FILE:LINE: MESSAGE before TOKEN
1855    where TOKEN is the next token in the input stream.  MESSAGE
1856    (specified by the caller) is usually of the form "expected
1857    OTHER-TOKEN".  */
1858
1859 static void
1860 cp_parser_error (cp_parser* parser, const char* message)
1861 {
1862   if (!cp_parser_simulate_error (parser))
1863     {
1864       cp_token *token = cp_lexer_peek_token (parser->lexer);
1865       /* This diagnostic makes more sense if it is tagged to the line
1866          of the token we just peeked at.  */
1867       cp_lexer_set_source_position_from_token (token);
1868       if (token->type == CPP_PRAGMA)
1869         {
1870           error ("%<#pragma%> is not allowed here");
1871           cp_lexer_purge_token (parser->lexer);
1872           return;
1873         }
1874       c_parse_error (message,
1875                      /* Because c_parser_error does not understand
1876                         CPP_KEYWORD, keywords are treated like
1877                         identifiers.  */
1878                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1879                      token->value);
1880     }
1881 }
1882
1883 /* Issue an error about name-lookup failing.  NAME is the
1884    IDENTIFIER_NODE DECL is the result of
1885    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1886    the thing that we hoped to find.  */
1887
1888 static void
1889 cp_parser_name_lookup_error (cp_parser* parser,
1890                              tree name,
1891                              tree decl,
1892                              const char* desired)
1893 {
1894   /* If name lookup completely failed, tell the user that NAME was not
1895      declared.  */
1896   if (decl == error_mark_node)
1897     {
1898       if (parser->scope && parser->scope != global_namespace)
1899         error ("%<%D::%D%> has not been declared",
1900                parser->scope, name);
1901       else if (parser->scope == global_namespace)
1902         error ("%<::%D%> has not been declared", name);
1903       else if (parser->object_scope
1904                && !CLASS_TYPE_P (parser->object_scope))
1905         error ("request for member %qD in non-class type %qT",
1906                name, parser->object_scope);
1907       else if (parser->object_scope)
1908         error ("%<%T::%D%> has not been declared",
1909                parser->object_scope, name);
1910       else
1911         error ("%qD has not been declared", name);
1912     }
1913   else if (parser->scope && parser->scope != global_namespace)
1914     error ("%<%D::%D%> %s", parser->scope, name, desired);
1915   else if (parser->scope == global_namespace)
1916     error ("%<::%D%> %s", name, desired);
1917   else
1918     error ("%qD %s", name, desired);
1919 }
1920
1921 /* If we are parsing tentatively, remember that an error has occurred
1922    during this tentative parse.  Returns true if the error was
1923    simulated; false if a message should be issued by the caller.  */
1924
1925 static bool
1926 cp_parser_simulate_error (cp_parser* parser)
1927 {
1928   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1929     {
1930       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1931       return true;
1932     }
1933   return false;
1934 }
1935
1936 /* This function is called when a type is defined.  If type
1937    definitions are forbidden at this point, an error message is
1938    issued.  */
1939
1940 static void
1941 cp_parser_check_type_definition (cp_parser* parser)
1942 {
1943   /* If types are forbidden here, issue a message.  */
1944   if (parser->type_definition_forbidden_message)
1945     /* Use `%s' to print the string in case there are any escape
1946        characters in the message.  */
1947     error ("%s", parser->type_definition_forbidden_message);
1948 }
1949
1950 /* This function is called when the DECLARATOR is processed.  The TYPE
1951    was a type defined in the decl-specifiers.  If it is invalid to
1952    define a type in the decl-specifiers for DECLARATOR, an error is
1953    issued.  */
1954
1955 static void
1956 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1957                                                tree type)
1958 {
1959   /* [dcl.fct] forbids type definitions in return types.
1960      Unfortunately, it's not easy to know whether or not we are
1961      processing a return type until after the fact.  */
1962   while (declarator
1963          && (declarator->kind == cdk_pointer
1964              || declarator->kind == cdk_reference
1965              || declarator->kind == cdk_ptrmem))
1966     declarator = declarator->declarator;
1967   if (declarator
1968       && declarator->kind == cdk_function)
1969     {
1970       error ("new types may not be defined in a return type");
1971       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1972               type);
1973     }
1974 }
1975
1976 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1977    "<" in any valid C++ program.  If the next token is indeed "<",
1978    issue a message warning the user about what appears to be an
1979    invalid attempt to form a template-id.  */
1980
1981 static void
1982 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1983                                          tree type)
1984 {
1985   cp_token_position start = 0;
1986
1987   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1988     {
1989       if (TYPE_P (type))
1990         error ("%qT is not a template", type);
1991       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1992         error ("%qE is not a template", type);
1993       else
1994         error ("invalid template-id");
1995       /* Remember the location of the invalid "<".  */
1996       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1997         start = cp_lexer_token_position (parser->lexer, true);
1998       /* Consume the "<".  */
1999       cp_lexer_consume_token (parser->lexer);
2000       /* Parse the template arguments.  */
2001       cp_parser_enclosed_template_argument_list (parser);
2002       /* Permanently remove the invalid template arguments so that
2003          this error message is not issued again.  */
2004       if (start)
2005         cp_lexer_purge_tokens_after (parser->lexer, start);
2006     }
2007 }
2008
2009 /* If parsing an integral constant-expression, issue an error message
2010    about the fact that THING appeared and return true.  Otherwise,
2011    return false.  In either case, set
2012    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2013
2014 static bool
2015 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2016                                             const char *thing)
2017 {
2018   parser->non_integral_constant_expression_p = true;
2019   if (parser->integral_constant_expression_p)
2020     {
2021       if (!parser->allow_non_integral_constant_expression_p)
2022         {
2023           error ("%s cannot appear in a constant-expression", thing);
2024           return true;
2025         }
2026     }
2027   return false;
2028 }
2029
2030 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2031    qualifying scope (or NULL, if none) for ID.  This function commits
2032    to the current active tentative parse, if any.  (Otherwise, the
2033    problematic construct might be encountered again later, resulting
2034    in duplicate error messages.)  */
2035
2036 static void
2037 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2038 {
2039   tree decl, old_scope;
2040   /* Try to lookup the identifier.  */
2041   old_scope = parser->scope;
2042   parser->scope = scope;
2043   decl = cp_parser_lookup_name_simple (parser, id);
2044   parser->scope = old_scope;
2045   /* If the lookup found a template-name, it means that the user forgot
2046   to specify an argument list. Emit a useful error message.  */
2047   if (TREE_CODE (decl) == TEMPLATE_DECL)
2048     error ("invalid use of template-name %qE without an argument list",
2049       decl);
2050   else if (!parser->scope || parser->scope == error_mark_node)
2051     {
2052       /* Issue an error message.  */
2053       error ("%qE does not name a type", id);
2054       /* If we're in a template class, it's possible that the user was
2055          referring to a type from a base class.  For example:
2056
2057            template <typename T> struct A { typedef T X; };
2058            template <typename T> struct B : public A<T> { X x; };
2059
2060          The user should have said "typename A<T>::X".  */
2061       if (processing_template_decl && current_class_type
2062           && TYPE_BINFO (current_class_type))
2063         {
2064           tree b;
2065
2066           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2067                b;
2068                b = TREE_CHAIN (b))
2069             {
2070               tree base_type = BINFO_TYPE (b);
2071               if (CLASS_TYPE_P (base_type)
2072                   && dependent_type_p (base_type))
2073                 {
2074                   tree field;
2075                   /* Go from a particular instantiation of the
2076                      template (which will have an empty TYPE_FIELDs),
2077                      to the main version.  */
2078                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2079                   for (field = TYPE_FIELDS (base_type);
2080                        field;
2081                        field = TREE_CHAIN (field))
2082                     if (TREE_CODE (field) == TYPE_DECL
2083                         && DECL_NAME (field) == id)
2084                       {
2085                         inform ("(perhaps %<typename %T::%E%> was intended)",
2086                                 BINFO_TYPE (b), id);
2087                         break;
2088                       }
2089                   if (field)
2090                     break;
2091                 }
2092             }
2093         }
2094     }
2095   /* Here we diagnose qualified-ids where the scope is actually correct,
2096      but the identifier does not resolve to a valid type name.  */
2097   else
2098     {
2099       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2100         error ("%qE in namespace %qE does not name a type",
2101                id, parser->scope);
2102       else if (TYPE_P (parser->scope))
2103         error ("%qE in class %qT does not name a type", id, parser->scope);
2104       else
2105         gcc_unreachable ();
2106     }
2107   cp_parser_commit_to_tentative_parse (parser);
2108 }
2109
2110 /* Check for a common situation where a type-name should be present,
2111    but is not, and issue a sensible error message.  Returns true if an
2112    invalid type-name was detected.
2113
2114    The situation handled by this function are variable declarations of the
2115    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2116    Usually, `ID' should name a type, but if we got here it means that it
2117    does not. We try to emit the best possible error message depending on
2118    how exactly the id-expression looks like.
2119 */
2120
2121 static bool
2122 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2123 {
2124   tree id;
2125
2126   cp_parser_parse_tentatively (parser);
2127   id = cp_parser_id_expression (parser,
2128                                 /*template_keyword_p=*/false,
2129                                 /*check_dependency_p=*/true,
2130                                 /*template_p=*/NULL,
2131                                 /*declarator_p=*/true);
2132   /* After the id-expression, there should be a plain identifier,
2133      otherwise this is not a simple variable declaration. Also, if
2134      the scope is dependent, we cannot do much.  */
2135   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2136       || (parser->scope && TYPE_P (parser->scope)
2137           && dependent_type_p (parser->scope)))
2138     {
2139       cp_parser_abort_tentative_parse (parser);
2140       return false;
2141     }
2142   if (!cp_parser_parse_definitely (parser)
2143       || TREE_CODE (id) != IDENTIFIER_NODE)
2144     return false;
2145
2146   /* Emit a diagnostic for the invalid type.  */
2147   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2148   /* Skip to the end of the declaration; there's no point in
2149      trying to process it.  */
2150   cp_parser_skip_to_end_of_block_or_statement (parser);
2151   return true;
2152 }
2153
2154 /* Consume tokens up to, and including, the next non-nested closing `)'.
2155    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2156    are doing error recovery. Returns -1 if OR_COMMA is true and we
2157    found an unnested comma.  */
2158
2159 static int
2160 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2161                                        bool recovering,
2162                                        bool or_comma,
2163                                        bool consume_paren)
2164 {
2165   unsigned paren_depth = 0;
2166   unsigned brace_depth = 0;
2167   int result;
2168
2169   if (recovering && !or_comma
2170       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2171     return 0;
2172
2173   while (true)
2174     {
2175       cp_token *token;
2176
2177       /* If we've run out of tokens, then there is no closing `)'.  */
2178       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2179         {
2180           result = 0;
2181           break;
2182         }
2183
2184       token = cp_lexer_peek_token (parser->lexer);
2185
2186       /* This matches the processing in skip_to_end_of_statement.  */
2187       if (token->type == CPP_SEMICOLON && !brace_depth)
2188         {
2189           result = 0;
2190           break;
2191         }
2192       if (token->type == CPP_OPEN_BRACE)
2193         ++brace_depth;
2194       if (token->type == CPP_CLOSE_BRACE)
2195         {
2196           if (!brace_depth--)
2197             {
2198               result = 0;
2199               break;
2200             }
2201         }
2202       if (recovering && or_comma && token->type == CPP_COMMA
2203           && !brace_depth && !paren_depth)
2204         {
2205           result = -1;
2206           break;
2207         }
2208
2209       if (!brace_depth)
2210         {
2211           /* If it is an `(', we have entered another level of nesting.  */
2212           if (token->type == CPP_OPEN_PAREN)
2213             ++paren_depth;
2214           /* If it is a `)', then we might be done.  */
2215           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2216             {
2217               if (consume_paren)
2218                 cp_lexer_consume_token (parser->lexer);
2219               {
2220                 result = 1;
2221                 break;
2222               }
2223             }
2224         }
2225
2226       /* Consume the token.  */
2227       cp_lexer_consume_token (parser->lexer);
2228     }
2229
2230   return result;
2231 }
2232
2233 /* Consume tokens until we reach the end of the current statement.
2234    Normally, that will be just before consuming a `;'.  However, if a
2235    non-nested `}' comes first, then we stop before consuming that.  */
2236
2237 static void
2238 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2239 {
2240   unsigned nesting_depth = 0;
2241
2242   while (true)
2243     {
2244       cp_token *token;
2245
2246       /* Peek at the next token.  */
2247       token = cp_lexer_peek_token (parser->lexer);
2248       /* If we've run out of tokens, stop.  */
2249       if (token->type == CPP_EOF)
2250         break;
2251       /* If the next token is a `;', we have reached the end of the
2252          statement.  */
2253       if (token->type == CPP_SEMICOLON && !nesting_depth)
2254         break;
2255       /* If the next token is a non-nested `}', then we have reached
2256          the end of the current block.  */
2257       if (token->type == CPP_CLOSE_BRACE)
2258         {
2259           /* If this is a non-nested `}', stop before consuming it.
2260              That way, when confronted with something like:
2261
2262                { 3 + }
2263
2264              we stop before consuming the closing `}', even though we
2265              have not yet reached a `;'.  */
2266           if (nesting_depth == 0)
2267             break;
2268           /* If it is the closing `}' for a block that we have
2269              scanned, stop -- but only after consuming the token.
2270              That way given:
2271
2272                 void f g () { ... }
2273                 typedef int I;
2274
2275              we will stop after the body of the erroneously declared
2276              function, but before consuming the following `typedef'
2277              declaration.  */
2278           if (--nesting_depth == 0)
2279             {
2280               cp_lexer_consume_token (parser->lexer);
2281               break;
2282             }
2283         }
2284       /* If it the next token is a `{', then we are entering a new
2285          block.  Consume the entire block.  */
2286       else if (token->type == CPP_OPEN_BRACE)
2287         ++nesting_depth;
2288       /* Consume the token.  */
2289       cp_lexer_consume_token (parser->lexer);
2290     }
2291 }
2292
2293 /* This function is called at the end of a statement or declaration.
2294    If the next token is a semicolon, it is consumed; otherwise, error
2295    recovery is attempted.  */
2296
2297 static void
2298 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2299 {
2300   /* Look for the trailing `;'.  */
2301   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2302     {
2303       /* If there is additional (erroneous) input, skip to the end of
2304          the statement.  */
2305       cp_parser_skip_to_end_of_statement (parser);
2306       /* If the next token is now a `;', consume it.  */
2307       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2308         cp_lexer_consume_token (parser->lexer);
2309     }
2310 }
2311
2312 /* Skip tokens until we have consumed an entire block, or until we
2313    have consumed a non-nested `;'.  */
2314
2315 static void
2316 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2317 {
2318   int nesting_depth = 0;
2319
2320   while (nesting_depth >= 0)
2321     {
2322       cp_token *token = cp_lexer_peek_token (parser->lexer);
2323
2324       if (token->type == CPP_EOF)
2325         break;
2326
2327       switch (token->type)
2328         {
2329         case CPP_EOF:
2330           /* If we've run out of tokens, stop.  */
2331           nesting_depth = -1;
2332           continue;
2333
2334         case CPP_SEMICOLON:
2335           /* Stop if this is an unnested ';'. */
2336           if (!nesting_depth)
2337             nesting_depth = -1;
2338           break;
2339
2340         case CPP_CLOSE_BRACE:
2341           /* Stop if this is an unnested '}', or closes the outermost
2342              nesting level.  */
2343           nesting_depth--;
2344           if (!nesting_depth)
2345             nesting_depth = -1;
2346           break;
2347
2348         case CPP_OPEN_BRACE:
2349           /* Nest. */
2350           nesting_depth++;
2351           break;
2352
2353         default:
2354           break;
2355         }
2356
2357       /* Consume the token.  */
2358       cp_lexer_consume_token (parser->lexer);
2359
2360     }
2361 }
2362
2363 /* Skip tokens until a non-nested closing curly brace is the next
2364    token.  */
2365
2366 static void
2367 cp_parser_skip_to_closing_brace (cp_parser *parser)
2368 {
2369   unsigned nesting_depth = 0;
2370
2371   while (true)
2372     {
2373       cp_token *token;
2374
2375       /* Peek at the next token.  */
2376       token = cp_lexer_peek_token (parser->lexer);
2377       /* If we've run out of tokens, stop.  */
2378       if (token->type == CPP_EOF)
2379         break;
2380       /* If the next token is a non-nested `}', then we have reached
2381          the end of the current block.  */
2382       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2383         break;
2384       /* If it the next token is a `{', then we are entering a new
2385          block.  Consume the entire block.  */
2386       else if (token->type == CPP_OPEN_BRACE)
2387         ++nesting_depth;
2388       /* Consume the token.  */
2389       cp_lexer_consume_token (parser->lexer);
2390     }
2391 }
2392
2393 /* This is a simple wrapper around make_typename_type. When the id is
2394    an unresolved identifier node, we can provide a superior diagnostic
2395    using cp_parser_diagnose_invalid_type_name.  */
2396
2397 static tree
2398 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2399 {
2400   tree result;
2401   if (TREE_CODE (id) == IDENTIFIER_NODE)
2402     {
2403       result = make_typename_type (scope, id, typename_type,
2404                                    /*complain=*/0);
2405       if (result == error_mark_node)
2406         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2407       return result;
2408     }
2409   return make_typename_type (scope, id, typename_type, tf_error);
2410 }
2411
2412
2413 /* Create a new C++ parser.  */
2414
2415 static cp_parser *
2416 cp_parser_new (void)
2417 {
2418   cp_parser *parser;
2419   cp_lexer *lexer;
2420   unsigned i;
2421
2422   /* cp_lexer_new_main is called before calling ggc_alloc because
2423      cp_lexer_new_main might load a PCH file.  */
2424   lexer = cp_lexer_new_main ();
2425
2426   /* Initialize the binops_by_token so that we can get the tree
2427      directly from the token.  */
2428   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2429     binops_by_token[binops[i].token_type] = binops[i];
2430
2431   parser = GGC_CNEW (cp_parser);
2432   parser->lexer = lexer;
2433   parser->context = cp_parser_context_new (NULL);
2434
2435   /* For now, we always accept GNU extensions.  */
2436   parser->allow_gnu_extensions_p = 1;
2437
2438   /* The `>' token is a greater-than operator, not the end of a
2439      template-id.  */
2440   parser->greater_than_is_operator_p = true;
2441
2442   parser->default_arg_ok_p = true;
2443
2444   /* We are not parsing a constant-expression.  */
2445   parser->integral_constant_expression_p = false;
2446   parser->allow_non_integral_constant_expression_p = false;
2447   parser->non_integral_constant_expression_p = false;
2448
2449   /* Local variable names are not forbidden.  */
2450   parser->local_variables_forbidden_p = false;
2451
2452   /* We are not processing an `extern "C"' declaration.  */
2453   parser->in_unbraced_linkage_specification_p = false;
2454
2455   /* We are not processing a declarator.  */
2456   parser->in_declarator_p = false;
2457
2458   /* We are not processing a template-argument-list.  */
2459   parser->in_template_argument_list_p = false;
2460
2461   /* We are not in an iteration statement.  */
2462   parser->in_iteration_statement_p = false;
2463
2464   /* We are not in a switch statement.  */
2465   parser->in_switch_statement_p = false;
2466
2467   /* We are not parsing a type-id inside an expression.  */
2468   parser->in_type_id_in_expr_p = false;
2469
2470   /* Declarations aren't implicitly extern "C".  */
2471   parser->implicit_extern_c = false;
2472
2473   /* String literals should be translated to the execution character set.  */
2474   parser->translate_strings_p = true;
2475
2476   /* The unparsed function queue is empty.  */
2477   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2478
2479   /* There are no classes being defined.  */
2480   parser->num_classes_being_defined = 0;
2481
2482   /* No template parameters apply.  */
2483   parser->num_template_parameter_lists = 0;
2484
2485   return parser;
2486 }
2487
2488 /* Create a cp_lexer structure which will emit the tokens in CACHE
2489    and push it onto the parser's lexer stack.  This is used for delayed
2490    parsing of in-class method bodies and default arguments, and should
2491    not be confused with tentative parsing.  */
2492 static void
2493 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2494 {
2495   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2496   lexer->next = parser->lexer;
2497   parser->lexer = lexer;
2498
2499   /* Move the current source position to that of the first token in the
2500      new lexer.  */
2501   cp_lexer_set_source_position_from_token (lexer->next_token);
2502 }
2503
2504 /* Pop the top lexer off the parser stack.  This is never used for the
2505    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2506 static void
2507 cp_parser_pop_lexer (cp_parser *parser)
2508 {
2509   cp_lexer *lexer = parser->lexer;
2510   parser->lexer = lexer->next;
2511   cp_lexer_destroy (lexer);
2512
2513   /* Put the current source position back where it was before this
2514      lexer was pushed.  */
2515   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2516 }
2517
2518 /* Lexical conventions [gram.lex]  */
2519
2520 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2521    identifier.  */
2522
2523 static tree
2524 cp_parser_identifier (cp_parser* parser)
2525 {
2526   cp_token *token;
2527
2528   /* Look for the identifier.  */
2529   token = cp_parser_require (parser, CPP_NAME, "identifier");
2530   /* Return the value.  */
2531   return token ? token->value : error_mark_node;
2532 }
2533
2534 /* Parse a sequence of adjacent string constants.  Returns a
2535    TREE_STRING representing the combined, nul-terminated string
2536    constant.  If TRANSLATE is true, translate the string to the
2537    execution character set.  If WIDE_OK is true, a wide string is
2538    invalid here.
2539
2540    C++98 [lex.string] says that if a narrow string literal token is
2541    adjacent to a wide string literal token, the behavior is undefined.
2542    However, C99 6.4.5p4 says that this results in a wide string literal.
2543    We follow C99 here, for consistency with the C front end.
2544
2545    This code is largely lifted from lex_string() in c-lex.c.
2546
2547    FUTURE: ObjC++ will need to handle @-strings here.  */
2548 static tree
2549 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2550 {
2551   tree value;
2552   bool wide = false;
2553   size_t count;
2554   struct obstack str_ob;
2555   cpp_string str, istr, *strs;
2556   cp_token *tok;
2557
2558   tok = cp_lexer_peek_token (parser->lexer);
2559   if (!cp_parser_is_string_literal (tok))
2560     {
2561       cp_parser_error (parser, "expected string-literal");
2562       return error_mark_node;
2563     }
2564
2565   /* Try to avoid the overhead of creating and destroying an obstack
2566      for the common case of just one string.  */
2567   if (!cp_parser_is_string_literal
2568       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2569     {
2570       cp_lexer_consume_token (parser->lexer);
2571
2572       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2573       str.len = TREE_STRING_LENGTH (tok->value);
2574       count = 1;
2575       if (tok->type == CPP_WSTRING)
2576         wide = true;
2577
2578       strs = &str;
2579     }
2580   else
2581     {
2582       gcc_obstack_init (&str_ob);
2583       count = 0;
2584
2585       do
2586         {
2587           cp_lexer_consume_token (parser->lexer);
2588           count++;
2589           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2590           str.len = TREE_STRING_LENGTH (tok->value);
2591           if (tok->type == CPP_WSTRING)
2592             wide = true;
2593
2594           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2595
2596           tok = cp_lexer_peek_token (parser->lexer);
2597         }
2598       while (cp_parser_is_string_literal (tok));
2599
2600       strs = (cpp_string *) obstack_finish (&str_ob);
2601     }
2602
2603   if (wide && !wide_ok)
2604     {
2605       cp_parser_error (parser, "a wide string is invalid in this context");
2606       wide = false;
2607     }
2608
2609   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2610       (parse_in, strs, count, &istr, wide))
2611     {
2612       value = build_string (istr.len, (char *)istr.text);
2613       free ((void *)istr.text);
2614
2615       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2616       value = fix_string_type (value);
2617     }
2618   else
2619     /* cpp_interpret_string has issued an error.  */
2620     value = error_mark_node;
2621
2622   if (count > 1)
2623     obstack_free (&str_ob, 0);
2624
2625   return value;
2626 }
2627
2628
2629 /* Basic concepts [gram.basic]  */
2630
2631 /* Parse a translation-unit.
2632
2633    translation-unit:
2634      declaration-seq [opt]
2635
2636    Returns TRUE if all went well.  */
2637
2638 static bool
2639 cp_parser_translation_unit (cp_parser* parser)
2640 {
2641   /* The address of the first non-permanent object on the declarator
2642      obstack.  */
2643   static void *declarator_obstack_base;
2644
2645   bool success;
2646
2647   /* Create the declarator obstack, if necessary.  */
2648   if (!cp_error_declarator)
2649     {
2650       gcc_obstack_init (&declarator_obstack);
2651       /* Create the error declarator.  */
2652       cp_error_declarator = make_declarator (cdk_error);
2653       /* Create the empty parameter list.  */
2654       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2655       /* Remember where the base of the declarator obstack lies.  */
2656       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2657     }
2658
2659   cp_parser_declaration_seq_opt (parser);
2660   
2661   /* If there are no tokens left then all went well.  */
2662   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2663     {
2664       /* Get rid of the token array; we don't need it any more.  */
2665       cp_lexer_destroy (parser->lexer);
2666       parser->lexer = NULL;
2667       
2668       /* This file might have been a context that's implicitly extern
2669          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2670       if (parser->implicit_extern_c)
2671         {
2672           pop_lang_context ();
2673           parser->implicit_extern_c = false;
2674         }
2675       
2676       /* Finish up.  */
2677       finish_translation_unit ();
2678       
2679       success = true;
2680     }
2681   else
2682     {
2683       cp_parser_error (parser, "expected declaration");
2684       success = false;
2685     }
2686   
2687   /* Make sure the declarator obstack was fully cleaned up.  */
2688   gcc_assert (obstack_next_free (&declarator_obstack)
2689               == declarator_obstack_base);
2690
2691   /* All went well.  */
2692   return success;
2693 }
2694
2695 /* Expressions [gram.expr] */
2696
2697 /* Parse a primary-expression.
2698
2699    primary-expression:
2700      literal
2701      this
2702      ( expression )
2703      id-expression
2704
2705    GNU Extensions:
2706
2707    primary-expression:
2708      ( compound-statement )
2709      __builtin_va_arg ( assignment-expression , type-id )
2710
2711    Objective-C++ Extension:
2712
2713    primary-expression:
2714      objc-expression
2715
2716    literal:
2717      __null
2718
2719    CAST_P is true if this primary expression is the target of a cast.
2720
2721    Returns a representation of the expression.
2722
2723    *IDK indicates what kind of id-expression (if any) was present.
2724
2725    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2726    used as the operand of a pointer-to-member.  In that case,
2727    *QUALIFYING_CLASS gives the class that is used as the qualifying
2728    class in the pointer-to-member.  */
2729
2730 static tree
2731 cp_parser_primary_expression (cp_parser *parser,
2732                               bool cast_p,
2733                               cp_id_kind *idk,
2734                               tree *qualifying_class)
2735 {
2736   cp_token *token;
2737
2738   /* Assume the primary expression is not an id-expression.  */
2739   *idk = CP_ID_KIND_NONE;
2740   /* And that it cannot be used as pointer-to-member.  */
2741   *qualifying_class = NULL_TREE;
2742
2743   /* Peek at the next token.  */
2744   token = cp_lexer_peek_token (parser->lexer);
2745   switch (token->type)
2746     {
2747       /* literal:
2748            integer-literal
2749            character-literal
2750            floating-literal
2751            string-literal
2752            boolean-literal  */
2753     case CPP_CHAR:
2754     case CPP_WCHAR:
2755     case CPP_NUMBER:
2756       token = cp_lexer_consume_token (parser->lexer);
2757       /* Floating-point literals are only allowed in an integral
2758          constant expression if they are cast to an integral or
2759          enumeration type.  */
2760       if (TREE_CODE (token->value) == REAL_CST
2761           && parser->integral_constant_expression_p
2762           && pedantic)
2763         {
2764           /* CAST_P will be set even in invalid code like "int(2.7 +
2765              ...)".   Therefore, we have to check that the next token
2766              is sure to end the cast.  */
2767           if (cast_p)
2768             {
2769               cp_token *next_token;
2770
2771               next_token = cp_lexer_peek_token (parser->lexer);
2772               if (/* The comma at the end of an
2773                      enumerator-definition.  */
2774                   next_token->type != CPP_COMMA
2775                   /* The curly brace at the end of an enum-specifier.  */
2776                   && next_token->type != CPP_CLOSE_BRACE
2777                   /* The end of a statement.  */
2778                   && next_token->type != CPP_SEMICOLON
2779                   /* The end of the cast-expression.  */
2780                   && next_token->type != CPP_CLOSE_PAREN
2781                   /* The end of an array bound.  */
2782                   && next_token->type != CPP_CLOSE_SQUARE
2783                   /* The closing ">" in a template-argument-list.  */
2784                   && (next_token->type != CPP_GREATER
2785                       || parser->greater_than_is_operator_p))
2786                 cast_p = false;
2787             }
2788
2789           /* If we are within a cast, then the constraint that the
2790              cast is to an integral or enumeration type will be
2791              checked at that point.  If we are not within a cast, then
2792              this code is invalid.  */
2793           if (!cast_p)
2794             cp_parser_non_integral_constant_expression
2795               (parser, "floating-point literal");
2796         }
2797       return token->value;
2798
2799     case CPP_STRING:
2800     case CPP_WSTRING:
2801       /* ??? Should wide strings be allowed when parser->translate_strings_p
2802          is false (i.e. in attributes)?  If not, we can kill the third
2803          argument to cp_parser_string_literal.  */
2804       return cp_parser_string_literal (parser,
2805                                        parser->translate_strings_p,
2806                                        true);
2807
2808     case CPP_OPEN_PAREN:
2809       {
2810         tree expr;
2811         bool saved_greater_than_is_operator_p;
2812
2813         /* Consume the `('.  */
2814         cp_lexer_consume_token (parser->lexer);
2815         /* Within a parenthesized expression, a `>' token is always
2816            the greater-than operator.  */
2817         saved_greater_than_is_operator_p
2818           = parser->greater_than_is_operator_p;
2819         parser->greater_than_is_operator_p = true;
2820         /* If we see `( { ' then we are looking at the beginning of
2821            a GNU statement-expression.  */
2822         if (cp_parser_allow_gnu_extensions_p (parser)
2823             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2824           {
2825             /* Statement-expressions are not allowed by the standard.  */
2826             if (pedantic)
2827               pedwarn ("ISO C++ forbids braced-groups within expressions");
2828
2829             /* And they're not allowed outside of a function-body; you
2830                cannot, for example, write:
2831
2832                  int i = ({ int j = 3; j + 1; });
2833
2834                at class or namespace scope.  */
2835             if (!at_function_scope_p ())
2836               error ("statement-expressions are allowed only inside functions");
2837             /* Start the statement-expression.  */
2838             expr = begin_stmt_expr ();
2839             /* Parse the compound-statement.  */
2840             cp_parser_compound_statement (parser, expr, false);
2841             /* Finish up.  */
2842             expr = finish_stmt_expr (expr, false);
2843           }
2844         else
2845           {
2846             /* Parse the parenthesized expression.  */
2847             expr = cp_parser_expression (parser, cast_p);
2848             /* Let the front end know that this expression was
2849                enclosed in parentheses. This matters in case, for
2850                example, the expression is of the form `A::B', since
2851                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2852                not.  */
2853             finish_parenthesized_expr (expr);
2854           }
2855         /* The `>' token might be the end of a template-id or
2856            template-parameter-list now.  */
2857         parser->greater_than_is_operator_p
2858           = saved_greater_than_is_operator_p;
2859         /* Consume the `)'.  */
2860         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2861           cp_parser_skip_to_end_of_statement (parser);
2862
2863         return expr;
2864       }
2865
2866     case CPP_KEYWORD:
2867       switch (token->keyword)
2868         {
2869           /* These two are the boolean literals.  */
2870         case RID_TRUE:
2871           cp_lexer_consume_token (parser->lexer);
2872           return boolean_true_node;
2873         case RID_FALSE:
2874           cp_lexer_consume_token (parser->lexer);
2875           return boolean_false_node;
2876
2877           /* The `__null' literal.  */
2878         case RID_NULL:
2879           cp_lexer_consume_token (parser->lexer);
2880           return null_node;
2881
2882           /* Recognize the `this' keyword.  */
2883         case RID_THIS:
2884           cp_lexer_consume_token (parser->lexer);
2885           if (parser->local_variables_forbidden_p)
2886             {
2887               error ("%<this%> may not be used in this context");
2888               return error_mark_node;
2889             }
2890           /* Pointers cannot appear in constant-expressions.  */
2891           if (cp_parser_non_integral_constant_expression (parser,
2892                                                           "`this'"))
2893             return error_mark_node;
2894           return finish_this_expr ();
2895
2896           /* The `operator' keyword can be the beginning of an
2897              id-expression.  */
2898         case RID_OPERATOR:
2899           goto id_expression;
2900
2901         case RID_FUNCTION_NAME:
2902         case RID_PRETTY_FUNCTION_NAME:
2903         case RID_C99_FUNCTION_NAME:
2904           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2905              __func__ are the names of variables -- but they are
2906              treated specially.  Therefore, they are handled here,
2907              rather than relying on the generic id-expression logic
2908              below.  Grammatically, these names are id-expressions.
2909
2910              Consume the token.  */
2911           token = cp_lexer_consume_token (parser->lexer);
2912           /* Look up the name.  */
2913           return finish_fname (token->value);
2914
2915         case RID_VA_ARG:
2916           {
2917             tree expression;
2918             tree type;
2919
2920             /* The `__builtin_va_arg' construct is used to handle
2921                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2922             cp_lexer_consume_token (parser->lexer);
2923             /* Look for the opening `('.  */
2924             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2925             /* Now, parse the assignment-expression.  */
2926             expression = cp_parser_assignment_expression (parser,
2927                                                           /*cast_p=*/false);
2928             /* Look for the `,'.  */
2929             cp_parser_require (parser, CPP_COMMA, "`,'");
2930             /* Parse the type-id.  */
2931             type = cp_parser_type_id (parser);
2932             /* Look for the closing `)'.  */
2933             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2934             /* Using `va_arg' in a constant-expression is not
2935                allowed.  */
2936             if (cp_parser_non_integral_constant_expression (parser,
2937                                                             "`va_arg'"))
2938               return error_mark_node;
2939             return build_x_va_arg (expression, type);
2940           }
2941
2942         case RID_OFFSETOF:
2943           return cp_parser_builtin_offsetof (parser);
2944
2945           /* Objective-C++ expressions.  */
2946         case RID_AT_ENCODE:
2947         case RID_AT_PROTOCOL:
2948         case RID_AT_SELECTOR:
2949           return cp_parser_objc_expression (parser);
2950
2951         default:
2952           cp_parser_error (parser, "expected primary-expression");
2953           return error_mark_node;
2954         }
2955
2956       /* An id-expression can start with either an identifier, a
2957          `::' as the beginning of a qualified-id, or the "operator"
2958          keyword.  */
2959     case CPP_NAME:
2960     case CPP_SCOPE:
2961     case CPP_TEMPLATE_ID:
2962     case CPP_NESTED_NAME_SPECIFIER:
2963       {
2964         tree id_expression;
2965         tree decl;
2966         const char *error_msg;
2967
2968       id_expression:
2969         /* Parse the id-expression.  */
2970         id_expression
2971           = cp_parser_id_expression (parser,
2972                                      /*template_keyword_p=*/false,
2973                                      /*check_dependency_p=*/true,
2974                                      /*template_p=*/NULL,
2975                                      /*declarator_p=*/false);
2976         if (id_expression == error_mark_node)
2977           return error_mark_node;
2978         /* If we have a template-id, then no further lookup is
2979            required.  If the template-id was for a template-class, we
2980            will sometimes have a TYPE_DECL at this point.  */
2981         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2982             || TREE_CODE (id_expression) == TYPE_DECL)
2983           decl = id_expression;
2984         /* Look up the name.  */
2985         else
2986           {
2987             bool ambiguous_p;
2988
2989             decl = cp_parser_lookup_name (parser, id_expression,
2990                                           none_type,
2991                                           /*is_template=*/false,
2992                                           /*is_namespace=*/false,
2993                                           /*check_dependency=*/true,
2994                                           &ambiguous_p);
2995             /* If the lookup was ambiguous, an error will already have
2996                been issued.  */
2997             if (ambiguous_p)
2998               return error_mark_node;
2999
3000             /* In Objective-C++, an instance variable (ivar) may be preferred
3001                to whatever cp_parser_lookup_name() found.  */
3002             decl = objc_lookup_ivar (decl, id_expression);
3003
3004             /* If name lookup gives us a SCOPE_REF, then the
3005                qualifying scope was dependent.  Just propagate the
3006                name.  */
3007             if (TREE_CODE (decl) == SCOPE_REF)
3008               {
3009                 if (TYPE_P (TREE_OPERAND (decl, 0)))
3010                   *qualifying_class = TREE_OPERAND (decl, 0);
3011                 return decl;
3012               }
3013             /* Check to see if DECL is a local variable in a context
3014                where that is forbidden.  */
3015             if (parser->local_variables_forbidden_p
3016                 && local_variable_p (decl))
3017               {
3018                 /* It might be that we only found DECL because we are
3019                    trying to be generous with pre-ISO scoping rules.
3020                    For example, consider:
3021
3022                      int i;
3023                      void g() {
3024                        for (int i = 0; i < 10; ++i) {}
3025                        extern void f(int j = i);
3026                      }
3027
3028                    Here, name look up will originally find the out
3029                    of scope `i'.  We need to issue a warning message,
3030                    but then use the global `i'.  */
3031                 decl = check_for_out_of_scope_variable (decl);
3032                 if (local_variable_p (decl))
3033                   {
3034                     error ("local variable %qD may not appear in this context",
3035                            decl);
3036                     return error_mark_node;
3037                   }
3038               }
3039           }
3040
3041         decl = finish_id_expression (id_expression, decl, parser->scope,
3042                                      idk, qualifying_class,
3043                                      parser->integral_constant_expression_p,
3044                                      parser->allow_non_integral_constant_expression_p,
3045                                      &parser->non_integral_constant_expression_p,
3046                                      &error_msg);
3047         if (error_msg)
3048           cp_parser_error (parser, error_msg);
3049         return decl;
3050       }
3051
3052       /* Anything else is an error.  */
3053     default:
3054       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3055       if (c_dialect_objc ()
3056           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3057         return cp_parser_objc_expression (parser);
3058
3059       cp_parser_error (parser, "expected primary-expression");
3060       return error_mark_node;
3061     }
3062 }
3063
3064 /* Parse an id-expression.
3065
3066    id-expression:
3067      unqualified-id
3068      qualified-id
3069
3070    qualified-id:
3071      :: [opt] nested-name-specifier template [opt] unqualified-id
3072      :: identifier
3073      :: operator-function-id
3074      :: template-id
3075
3076    Return a representation of the unqualified portion of the
3077    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3078    a `::' or nested-name-specifier.
3079
3080    Often, if the id-expression was a qualified-id, the caller will
3081    want to make a SCOPE_REF to represent the qualified-id.  This
3082    function does not do this in order to avoid wastefully creating
3083    SCOPE_REFs when they are not required.
3084
3085    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3086    `template' keyword.
3087
3088    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3089    uninstantiated templates.
3090
3091    If *TEMPLATE_P is non-NULL, it is set to true iff the
3092    `template' keyword is used to explicitly indicate that the entity
3093    named is a template.
3094
3095    If DECLARATOR_P is true, the id-expression is appearing as part of
3096    a declarator, rather than as part of an expression.  */
3097
3098 static tree
3099 cp_parser_id_expression (cp_parser *parser,
3100                          bool template_keyword_p,
3101                          bool check_dependency_p,
3102                          bool *template_p,
3103                          bool declarator_p)
3104 {
3105   bool global_scope_p;
3106   bool nested_name_specifier_p;
3107
3108   /* Assume the `template' keyword was not used.  */
3109   if (template_p)
3110     *template_p = false;
3111
3112   /* Look for the optional `::' operator.  */
3113   global_scope_p
3114     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3115        != NULL_TREE);
3116   /* Look for the optional nested-name-specifier.  */
3117   nested_name_specifier_p
3118     = (cp_parser_nested_name_specifier_opt (parser,
3119                                             /*typename_keyword_p=*/false,
3120                                             check_dependency_p,
3121                                             /*type_p=*/false,
3122                                             declarator_p)
3123        != NULL_TREE);
3124   /* If there is a nested-name-specifier, then we are looking at
3125      the first qualified-id production.  */
3126   if (nested_name_specifier_p)
3127     {
3128       tree saved_scope;
3129       tree saved_object_scope;
3130       tree saved_qualifying_scope;
3131       tree unqualified_id;
3132       bool is_template;
3133
3134       /* See if the next token is the `template' keyword.  */
3135       if (!template_p)
3136         template_p = &is_template;
3137       *template_p = cp_parser_optional_template_keyword (parser);
3138       /* Name lookup we do during the processing of the
3139          unqualified-id might obliterate SCOPE.  */
3140       saved_scope = parser->scope;
3141       saved_object_scope = parser->object_scope;
3142       saved_qualifying_scope = parser->qualifying_scope;
3143       /* Process the final unqualified-id.  */
3144       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3145                                                  check_dependency_p,
3146                                                  declarator_p);
3147       /* Restore the SAVED_SCOPE for our caller.  */
3148       parser->scope = saved_scope;
3149       parser->object_scope = saved_object_scope;
3150       parser->qualifying_scope = saved_qualifying_scope;
3151
3152       return unqualified_id;
3153     }
3154   /* Otherwise, if we are in global scope, then we are looking at one
3155      of the other qualified-id productions.  */
3156   else if (global_scope_p)
3157     {
3158       cp_token *token;
3159       tree id;
3160
3161       /* Peek at the next token.  */
3162       token = cp_lexer_peek_token (parser->lexer);
3163
3164       /* If it's an identifier, and the next token is not a "<", then
3165          we can avoid the template-id case.  This is an optimization
3166          for this common case.  */
3167       if (token->type == CPP_NAME
3168           && !cp_parser_nth_token_starts_template_argument_list_p
3169                (parser, 2))
3170         return cp_parser_identifier (parser);
3171
3172       cp_parser_parse_tentatively (parser);
3173       /* Try a template-id.  */
3174       id = cp_parser_template_id (parser,
3175                                   /*template_keyword_p=*/false,
3176                                   /*check_dependency_p=*/true,
3177                                   declarator_p);
3178       /* If that worked, we're done.  */
3179       if (cp_parser_parse_definitely (parser))
3180         return id;
3181
3182       /* Peek at the next token.  (Changes in the token buffer may
3183          have invalidated the pointer obtained above.)  */
3184       token = cp_lexer_peek_token (parser->lexer);
3185
3186       switch (token->type)
3187         {
3188         case CPP_NAME:
3189           return cp_parser_identifier (parser);
3190
3191         case CPP_KEYWORD:
3192           if (token->keyword == RID_OPERATOR)
3193             return cp_parser_operator_function_id (parser);
3194           /* Fall through.  */
3195
3196         default:
3197           cp_parser_error (parser, "expected id-expression");
3198           return error_mark_node;
3199         }
3200     }
3201   else
3202     return cp_parser_unqualified_id (parser, template_keyword_p,
3203                                      /*check_dependency_p=*/true,
3204                                      declarator_p);
3205 }
3206
3207 /* Parse an unqualified-id.
3208
3209    unqualified-id:
3210      identifier
3211      operator-function-id
3212      conversion-function-id
3213      ~ class-name
3214      template-id
3215
3216    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3217    keyword, in a construct like `A::template ...'.
3218
3219    Returns a representation of unqualified-id.  For the `identifier'
3220    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3221    production a BIT_NOT_EXPR is returned; the operand of the
3222    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3223    other productions, see the documentation accompanying the
3224    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3225    names are looked up in uninstantiated templates.  If DECLARATOR_P
3226    is true, the unqualified-id is appearing as part of a declarator,
3227    rather than as part of an expression.  */
3228
3229 static tree
3230 cp_parser_unqualified_id (cp_parser* parser,
3231                           bool template_keyword_p,
3232                           bool check_dependency_p,
3233                           bool declarator_p)
3234 {
3235   cp_token *token;
3236
3237   /* Peek at the next token.  */
3238   token = cp_lexer_peek_token (parser->lexer);
3239
3240   switch (token->type)
3241     {
3242     case CPP_NAME:
3243       {
3244         tree id;
3245
3246         /* We don't know yet whether or not this will be a
3247            template-id.  */
3248         cp_parser_parse_tentatively (parser);
3249         /* Try a template-id.  */
3250         id = cp_parser_template_id (parser, template_keyword_p,
3251                                     check_dependency_p,
3252                                     declarator_p);
3253         /* If it worked, we're done.  */
3254         if (cp_parser_parse_definitely (parser))
3255           return id;
3256         /* Otherwise, it's an ordinary identifier.  */
3257         return cp_parser_identifier (parser);
3258       }
3259
3260     case CPP_TEMPLATE_ID:
3261       return cp_parser_template_id (parser, template_keyword_p,
3262                                     check_dependency_p,
3263                                     declarator_p);
3264
3265     case CPP_COMPL:
3266       {
3267         tree type_decl;
3268         tree qualifying_scope;
3269         tree object_scope;
3270         tree scope;
3271         bool done;
3272
3273         /* Consume the `~' token.  */
3274         cp_lexer_consume_token (parser->lexer);
3275         /* Parse the class-name.  The standard, as written, seems to
3276            say that:
3277
3278              template <typename T> struct S { ~S (); };
3279              template <typename T> S<T>::~S() {}
3280
3281            is invalid, since `~' must be followed by a class-name, but
3282            `S<T>' is dependent, and so not known to be a class.
3283            That's not right; we need to look in uninstantiated
3284            templates.  A further complication arises from:
3285
3286              template <typename T> void f(T t) {
3287                t.T::~T();
3288              }
3289
3290            Here, it is not possible to look up `T' in the scope of `T'
3291            itself.  We must look in both the current scope, and the
3292            scope of the containing complete expression.
3293
3294            Yet another issue is:
3295
3296              struct S {
3297                int S;
3298                ~S();
3299              };
3300
3301              S::~S() {}
3302
3303            The standard does not seem to say that the `S' in `~S'
3304            should refer to the type `S' and not the data member
3305            `S::S'.  */
3306
3307         /* DR 244 says that we look up the name after the "~" in the
3308            same scope as we looked up the qualifying name.  That idea
3309            isn't fully worked out; it's more complicated than that.  */
3310         scope = parser->scope;
3311         object_scope = parser->object_scope;
3312         qualifying_scope = parser->qualifying_scope;
3313
3314         /* If the name is of the form "X::~X" it's OK.  */
3315         if (scope && TYPE_P (scope)
3316             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3317             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3318                 == CPP_OPEN_PAREN)
3319             && (cp_lexer_peek_token (parser->lexer)->value
3320                 == TYPE_IDENTIFIER (scope)))
3321           {
3322             cp_lexer_consume_token (parser->lexer);
3323             return build_nt (BIT_NOT_EXPR, scope);
3324           }
3325
3326         /* If there was an explicit qualification (S::~T), first look
3327            in the scope given by the qualification (i.e., S).  */
3328         done = false;
3329         type_decl = NULL_TREE;
3330         if (scope)
3331           {
3332             cp_parser_parse_tentatively (parser);
3333             type_decl = cp_parser_class_name (parser,
3334                                               /*typename_keyword_p=*/false,
3335                                               /*template_keyword_p=*/false,
3336                                               none_type,
3337                                               /*check_dependency=*/false,
3338                                               /*class_head_p=*/false,
3339                                               declarator_p);
3340             if (cp_parser_parse_definitely (parser))
3341               done = true;
3342           }
3343         /* In "N::S::~S", look in "N" as well.  */
3344         if (!done && scope && qualifying_scope)
3345           {
3346             cp_parser_parse_tentatively (parser);
3347             parser->scope = qualifying_scope;
3348             parser->object_scope = NULL_TREE;
3349             parser->qualifying_scope = NULL_TREE;
3350             type_decl
3351               = cp_parser_class_name (parser,
3352                                       /*typename_keyword_p=*/false,
3353                                       /*template_keyword_p=*/false,
3354                                       none_type,
3355                                       /*check_dependency=*/false,
3356                                       /*class_head_p=*/false,
3357                                       declarator_p);
3358             if (cp_parser_parse_definitely (parser))
3359               done = true;
3360           }
3361         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3362         else if (!done && object_scope)
3363           {
3364             cp_parser_parse_tentatively (parser);
3365             parser->scope = object_scope;
3366             parser->object_scope = NULL_TREE;
3367             parser->qualifying_scope = NULL_TREE;
3368             type_decl
3369               = cp_parser_class_name (parser,
3370                                       /*typename_keyword_p=*/false,
3371                                       /*template_keyword_p=*/false,
3372                                       none_type,
3373                                       /*check_dependency=*/false,
3374                                       /*class_head_p=*/false,
3375                                       declarator_p);
3376             if (cp_parser_parse_definitely (parser))
3377               done = true;
3378           }
3379         /* Look in the surrounding context.  */
3380         if (!done)
3381           {
3382             parser->scope = NULL_TREE;
3383             parser->object_scope = NULL_TREE;
3384             parser->qualifying_scope = NULL_TREE;
3385             type_decl
3386               = cp_parser_class_name (parser,
3387                                       /*typename_keyword_p=*/false,
3388                                       /*template_keyword_p=*/false,
3389                                       none_type,
3390                                       /*check_dependency=*/false,
3391                                       /*class_head_p=*/false,
3392                                       declarator_p);
3393           }
3394         /* If an error occurred, assume that the name of the
3395            destructor is the same as the name of the qualifying
3396            class.  That allows us to keep parsing after running
3397            into ill-formed destructor names.  */
3398         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3399           return build_nt (BIT_NOT_EXPR, scope);
3400         else if (type_decl == error_mark_node)
3401           return error_mark_node;
3402
3403         /* [class.dtor]
3404
3405            A typedef-name that names a class shall not be used as the
3406            identifier in the declarator for a destructor declaration.  */
3407         if (declarator_p
3408             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3409             && !DECL_SELF_REFERENCE_P (type_decl)
3410             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3411           error ("typedef-name %qD used as destructor declarator",
3412                  type_decl);
3413
3414         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3415       }
3416
3417     case CPP_KEYWORD:
3418       if (token->keyword == RID_OPERATOR)
3419         {
3420           tree id;
3421
3422           /* This could be a template-id, so we try that first.  */
3423           cp_parser_parse_tentatively (parser);
3424           /* Try a template-id.  */
3425           id = cp_parser_template_id (parser, template_keyword_p,
3426                                       /*check_dependency_p=*/true,
3427                                       declarator_p);
3428           /* If that worked, we're done.  */
3429           if (cp_parser_parse_definitely (parser))
3430             return id;
3431           /* We still don't know whether we're looking at an
3432              operator-function-id or a conversion-function-id.  */
3433           cp_parser_parse_tentatively (parser);
3434           /* Try an operator-function-id.  */
3435           id = cp_parser_operator_function_id (parser);
3436           /* If that didn't work, try a conversion-function-id.  */
3437           if (!cp_parser_parse_definitely (parser))
3438             id = cp_parser_conversion_function_id (parser);
3439
3440           return id;
3441         }
3442       /* Fall through.  */
3443
3444     default:
3445       cp_parser_error (parser, "expected unqualified-id");
3446       return error_mark_node;
3447     }
3448 }
3449
3450 /* Parse an (optional) nested-name-specifier.
3451
3452    nested-name-specifier:
3453      class-or-namespace-name :: nested-name-specifier [opt]
3454      class-or-namespace-name :: template nested-name-specifier [opt]
3455
3456    PARSER->SCOPE should be set appropriately before this function is
3457    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3458    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3459    in name lookups.
3460
3461    Sets PARSER->SCOPE to the class (TYPE) or namespace
3462    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3463    it unchanged if there is no nested-name-specifier.  Returns the new
3464    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3465
3466    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3467    part of a declaration and/or decl-specifier.  */
3468
3469 static tree
3470 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3471                                      bool typename_keyword_p,
3472                                      bool check_dependency_p,
3473                                      bool type_p,
3474                                      bool is_declaration)
3475 {
3476   bool success = false;
3477   tree access_check = NULL_TREE;
3478   cp_token_position start = 0;
3479   cp_token *token;
3480
3481   /* If the next token corresponds to a nested name specifier, there
3482      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3483      false, it may have been true before, in which case something
3484      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3485      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3486      CHECK_DEPENDENCY_P is false, we have to fall through into the
3487      main loop.  */
3488   if (check_dependency_p
3489       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3490     {
3491       cp_parser_pre_parsed_nested_name_specifier (parser);
3492       return parser->scope;
3493     }
3494
3495   /* Remember where the nested-name-specifier starts.  */
3496   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3497     start = cp_lexer_token_position (parser->lexer, false);
3498
3499   push_deferring_access_checks (dk_deferred);
3500
3501   while (true)
3502     {
3503       tree new_scope;
3504       tree old_scope;
3505       tree saved_qualifying_scope;
3506       bool template_keyword_p;
3507
3508       /* Spot cases that cannot be the beginning of a
3509          nested-name-specifier.  */
3510       token = cp_lexer_peek_token (parser->lexer);
3511
3512       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3513          the already parsed nested-name-specifier.  */
3514       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3515         {
3516           /* Grab the nested-name-specifier and continue the loop.  */
3517           cp_parser_pre_parsed_nested_name_specifier (parser);
3518           success = true;
3519           continue;
3520         }
3521
3522       /* Spot cases that cannot be the beginning of a
3523          nested-name-specifier.  On the second and subsequent times
3524          through the loop, we look for the `template' keyword.  */
3525       if (success && token->keyword == RID_TEMPLATE)
3526         ;
3527       /* A template-id can start a nested-name-specifier.  */
3528       else if (token->type == CPP_TEMPLATE_ID)
3529         ;
3530       else
3531         {
3532           /* If the next token is not an identifier, then it is
3533              definitely not a class-or-namespace-name.  */
3534           if (token->type != CPP_NAME)
3535             break;
3536           /* If the following token is neither a `<' (to begin a
3537              template-id), nor a `::', then we are not looking at a
3538              nested-name-specifier.  */
3539           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3540           if (token->type != CPP_SCOPE
3541               && !cp_parser_nth_token_starts_template_argument_list_p
3542                   (parser, 2))
3543             break;
3544         }
3545
3546       /* The nested-name-specifier is optional, so we parse
3547          tentatively.  */
3548       cp_parser_parse_tentatively (parser);
3549
3550       /* Look for the optional `template' keyword, if this isn't the
3551          first time through the loop.  */
3552       if (success)
3553         template_keyword_p = cp_parser_optional_template_keyword (parser);
3554       else
3555         template_keyword_p = false;
3556
3557       /* Save the old scope since the name lookup we are about to do
3558          might destroy it.  */
3559       old_scope = parser->scope;
3560       saved_qualifying_scope = parser->qualifying_scope;
3561       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3562          look up names in "X<T>::I" in order to determine that "Y" is
3563          a template.  So, if we have a typename at this point, we make
3564          an effort to look through it.  */
3565       if (is_declaration
3566           && !typename_keyword_p
3567           && parser->scope
3568           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3569         parser->scope = resolve_typename_type (parser->scope,
3570                                                /*only_current_p=*/false);
3571       /* Parse the qualifying entity.  */
3572       new_scope
3573         = cp_parser_class_or_namespace_name (parser,
3574                                              typename_keyword_p,
3575                                              template_keyword_p,
3576                                              check_dependency_p,
3577                                              type_p,
3578                                              is_declaration);
3579       /* Look for the `::' token.  */
3580       cp_parser_require (parser, CPP_SCOPE, "`::'");
3581
3582       /* If we found what we wanted, we keep going; otherwise, we're
3583          done.  */
3584       if (!cp_parser_parse_definitely (parser))
3585         {
3586           bool error_p = false;
3587
3588           /* Restore the OLD_SCOPE since it was valid before the
3589              failed attempt at finding the last
3590              class-or-namespace-name.  */
3591           parser->scope = old_scope;
3592           parser->qualifying_scope = saved_qualifying_scope;
3593           /* If the next token is an identifier, and the one after
3594              that is a `::', then any valid interpretation would have
3595              found a class-or-namespace-name.  */
3596           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3597                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3598                      == CPP_SCOPE)
3599                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3600                      != CPP_COMPL))
3601             {
3602               token = cp_lexer_consume_token (parser->lexer);
3603               if (!error_p)
3604                 {
3605                   tree decl;
3606
3607                   decl = cp_parser_lookup_name_simple (parser, token->value);
3608                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3609                     error ("%qD used without template parameters", decl);
3610                   else
3611                     cp_parser_name_lookup_error
3612                       (parser, token->value, decl,
3613                        "is not a class or namespace");
3614                   parser->scope = NULL_TREE;
3615                   error_p = true;
3616                   /* Treat this as a successful nested-name-specifier
3617                      due to:
3618
3619                      [basic.lookup.qual]
3620
3621                      If the name found is not a class-name (clause
3622                      _class_) or namespace-name (_namespace.def_), the
3623                      program is ill-formed.  */
3624                   success = true;
3625                 }
3626               cp_lexer_consume_token (parser->lexer);
3627             }
3628           break;
3629         }
3630
3631       /* We've found one valid nested-name-specifier.  */
3632       success = true;
3633       /* Make sure we look in the right scope the next time through
3634          the loop.  */
3635       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3636                        ? TREE_TYPE (new_scope)
3637                        : new_scope);
3638       /* If it is a class scope, try to complete it; we are about to
3639          be looking up names inside the class.  */
3640       if (TYPE_P (parser->scope)
3641           /* Since checking types for dependency can be expensive,
3642              avoid doing it if the type is already complete.  */
3643           && !COMPLETE_TYPE_P (parser->scope)
3644           /* Do not try to complete dependent types.  */
3645           && !dependent_type_p (parser->scope))
3646         complete_type (parser->scope);
3647     }
3648
3649   /* Retrieve any deferred checks.  Do not pop this access checks yet
3650      so the memory will not be reclaimed during token replacing below.  */
3651   access_check = get_deferred_access_checks ();
3652
3653   /* If parsing tentatively, replace the sequence of tokens that makes
3654      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3655      token.  That way, should we re-parse the token stream, we will
3656      not have to repeat the effort required to do the parse, nor will
3657      we issue duplicate error messages.  */
3658   if (success && start)
3659     {
3660       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3661
3662       /* Reset the contents of the START token.  */
3663       token->type = CPP_NESTED_NAME_SPECIFIER;
3664       token->value = build_tree_list (access_check, parser->scope);
3665       TREE_TYPE (token->value) = parser->qualifying_scope;
3666       token->keyword = RID_MAX;
3667
3668       /* Purge all subsequent tokens.  */
3669       cp_lexer_purge_tokens_after (parser->lexer, start);
3670     }
3671
3672   pop_deferring_access_checks ();
3673   return success ? parser->scope : NULL_TREE;
3674 }
3675
3676 /* Parse a nested-name-specifier.  See
3677    cp_parser_nested_name_specifier_opt for details.  This function
3678    behaves identically, except that it will an issue an error if no
3679    nested-name-specifier is present.  */
3680
3681 static tree
3682 cp_parser_nested_name_specifier (cp_parser *parser,
3683                                  bool typename_keyword_p,
3684                                  bool check_dependency_p,
3685                                  bool type_p,
3686                                  bool is_declaration)
3687 {
3688   tree scope;
3689
3690   /* Look for the nested-name-specifier.  */
3691   scope = cp_parser_nested_name_specifier_opt (parser,
3692                                                typename_keyword_p,
3693                                                check_dependency_p,
3694                                                type_p,
3695                                                is_declaration);
3696   /* If it was not present, issue an error message.  */
3697   if (!scope)
3698     {
3699       cp_parser_error (parser, "expected nested-name-specifier");
3700       parser->scope = NULL_TREE;
3701     }
3702
3703   return scope;
3704 }
3705
3706 /* Parse a class-or-namespace-name.
3707
3708    class-or-namespace-name:
3709      class-name
3710      namespace-name
3711
3712    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3713    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3714    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3715    TYPE_P is TRUE iff the next name should be taken as a class-name,
3716    even the same name is declared to be another entity in the same
3717    scope.
3718
3719    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3720    specified by the class-or-namespace-name.  If neither is found the
3721    ERROR_MARK_NODE is returned.  */
3722
3723 static tree
3724 cp_parser_class_or_namespace_name (cp_parser *parser,
3725                                    bool typename_keyword_p,
3726                                    bool template_keyword_p,
3727                                    bool check_dependency_p,
3728                                    bool type_p,
3729                                    bool is_declaration)
3730 {
3731   tree saved_scope;
3732   tree saved_qualifying_scope;
3733   tree saved_object_scope;
3734   tree scope;
3735   bool only_class_p;
3736
3737   /* Before we try to parse the class-name, we must save away the
3738      current PARSER->SCOPE since cp_parser_class_name will destroy
3739      it.  */
3740   saved_scope = parser->scope;
3741   saved_qualifying_scope = parser->qualifying_scope;
3742   saved_object_scope = parser->object_scope;
3743   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3744      there is no need to look for a namespace-name.  */
3745   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3746   if (!only_class_p)
3747     cp_parser_parse_tentatively (parser);
3748   scope = cp_parser_class_name (parser,
3749                                 typename_keyword_p,
3750                                 template_keyword_p,
3751                                 type_p ? class_type : none_type,
3752                                 check_dependency_p,
3753                                 /*class_head_p=*/false,
3754                                 is_declaration);
3755   /* If that didn't work, try for a namespace-name.  */
3756   if (!only_class_p && !cp_parser_parse_definitely (parser))
3757     {
3758       /* Restore the saved scope.  */
3759       parser->scope = saved_scope;
3760       parser->qualifying_scope = saved_qualifying_scope;
3761       parser->object_scope = saved_object_scope;
3762       /* If we are not looking at an identifier followed by the scope
3763          resolution operator, then this is not part of a
3764          nested-name-specifier.  (Note that this function is only used
3765          to parse the components of a nested-name-specifier.)  */
3766       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3767           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3768         return error_mark_node;
3769       scope = cp_parser_namespace_name (parser);
3770     }
3771
3772   return scope;
3773 }
3774
3775 /* Parse a postfix-expression.
3776
3777    postfix-expression:
3778      primary-expression
3779      postfix-expression [ expression ]
3780      postfix-expression ( expression-list [opt] )
3781      simple-type-specifier ( expression-list [opt] )
3782      typename :: [opt] nested-name-specifier identifier
3783        ( expression-list [opt] )
3784      typename :: [opt] nested-name-specifier template [opt] template-id
3785        ( expression-list [opt] )
3786      postfix-expression . template [opt] id-expression
3787      postfix-expression -> template [opt] id-expression
3788      postfix-expression . pseudo-destructor-name
3789      postfix-expression -> pseudo-destructor-name
3790      postfix-expression ++
3791      postfix-expression --
3792      dynamic_cast < type-id > ( expression )
3793      static_cast < type-id > ( expression )
3794      reinterpret_cast < type-id > ( expression )
3795      const_cast < type-id > ( expression )
3796      typeid ( expression )
3797      typeid ( type-id )
3798
3799    GNU Extension:
3800
3801    postfix-expression:
3802      ( type-id ) { initializer-list , [opt] }
3803
3804    This extension is a GNU version of the C99 compound-literal
3805    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3806    but they are essentially the same concept.)
3807
3808    If ADDRESS_P is true, the postfix expression is the operand of the
3809    `&' operator.  CAST_P is true if this expression is the target of a
3810    cast.
3811
3812    Returns a representation of the expression.  */
3813
3814 static tree
3815 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3816 {
3817   cp_token *token;
3818   enum rid keyword;
3819   cp_id_kind idk = CP_ID_KIND_NONE;
3820   tree postfix_expression = NULL_TREE;
3821   /* Non-NULL only if the current postfix-expression can be used to
3822      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3823      class used to qualify the member.  */
3824   tree qualifying_class = NULL_TREE;
3825
3826   /* Peek at the next token.  */
3827   token = cp_lexer_peek_token (parser->lexer);
3828   /* Some of the productions are determined by keywords.  */
3829   keyword = token->keyword;
3830   switch (keyword)
3831     {
3832     case RID_DYNCAST:
3833     case RID_STATCAST:
3834     case RID_REINTCAST:
3835     case RID_CONSTCAST:
3836       {
3837         tree type;
3838         tree expression;
3839         const char *saved_message;
3840
3841         /* All of these can be handled in the same way from the point
3842            of view of parsing.  Begin by consuming the token
3843            identifying the cast.  */
3844         cp_lexer_consume_token (parser->lexer);
3845
3846         /* New types cannot be defined in the cast.  */
3847         saved_message = parser->type_definition_forbidden_message;
3848         parser->type_definition_forbidden_message
3849           = "types may not be defined in casts";
3850
3851         /* Look for the opening `<'.  */
3852         cp_parser_require (parser, CPP_LESS, "`<'");
3853         /* Parse the type to which we are casting.  */
3854         type = cp_parser_type_id (parser);
3855         /* Look for the closing `>'.  */
3856         cp_parser_require (parser, CPP_GREATER, "`>'");
3857         /* Restore the old message.  */
3858         parser->type_definition_forbidden_message = saved_message;
3859
3860         /* And the expression which is being cast.  */
3861         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3862         expression = cp_parser_expression (parser, /*cast_p=*/true);
3863         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3864
3865         /* Only type conversions to integral or enumeration types
3866            can be used in constant-expressions.  */
3867         if (parser->integral_constant_expression_p
3868             && !dependent_type_p (type)
3869             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3870             && (cp_parser_non_integral_constant_expression
3871                 (parser,
3872                  "a cast to a type other than an integral or "
3873                  "enumeration type")))
3874           return error_mark_node;
3875
3876         switch (keyword)
3877           {
3878           case RID_DYNCAST:
3879             postfix_expression
3880               = build_dynamic_cast (type, expression);
3881             break;
3882           case RID_STATCAST:
3883             postfix_expression
3884               = build_static_cast (type, expression);
3885             break;
3886           case RID_REINTCAST:
3887             postfix_expression
3888               = build_reinterpret_cast (type, expression);
3889             break;
3890           case RID_CONSTCAST:
3891             postfix_expression
3892               = build_const_cast (type, expression);
3893             break;
3894           default:
3895             gcc_unreachable ();
3896           }
3897       }
3898       break;
3899
3900     case RID_TYPEID:
3901       {
3902         tree type;
3903         const char *saved_message;
3904         bool saved_in_type_id_in_expr_p;
3905
3906         /* Consume the `typeid' token.  */
3907         cp_lexer_consume_token (parser->lexer);
3908         /* Look for the `(' token.  */
3909         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3910         /* Types cannot be defined in a `typeid' expression.  */
3911         saved_message = parser->type_definition_forbidden_message;
3912         parser->type_definition_forbidden_message
3913           = "types may not be defined in a `typeid\' expression";
3914         /* We can't be sure yet whether we're looking at a type-id or an
3915            expression.  */
3916         cp_parser_parse_tentatively (parser);
3917         /* Try a type-id first.  */
3918         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3919         parser->in_type_id_in_expr_p = true;
3920         type = cp_parser_type_id (parser);
3921         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3922         /* Look for the `)' token.  Otherwise, we can't be sure that
3923            we're not looking at an expression: consider `typeid (int
3924            (3))', for example.  */
3925         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3926         /* If all went well, simply lookup the type-id.  */
3927         if (cp_parser_parse_definitely (parser))
3928           postfix_expression = get_typeid (type);
3929         /* Otherwise, fall back to the expression variant.  */
3930         else
3931           {
3932             tree expression;
3933
3934             /* Look for an expression.  */
3935             expression = cp_parser_expression (parser, /*cast_p=*/false);
3936             /* Compute its typeid.  */
3937             postfix_expression = build_typeid (expression);
3938             /* Look for the `)' token.  */
3939             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3940           }
3941         /* `typeid' may not appear in an integral constant expression.  */
3942         if (cp_parser_non_integral_constant_expression(parser,
3943                                                        "`typeid' operator"))
3944           return error_mark_node;
3945         /* Restore the saved message.  */
3946         parser->type_definition_forbidden_message = saved_message;
3947       }
3948       break;
3949
3950     case RID_TYPENAME:
3951       {
3952         bool template_p = false;
3953         tree id;
3954         tree type;
3955         tree scope;
3956
3957         /* Consume the `typename' token.  */
3958         cp_lexer_consume_token (parser->lexer);
3959
3960         /* Look for the optional `::' operator.  */
3961         cp_parser_global_scope_opt (parser,
3962                                     /*current_scope_valid_p=*/false);
3963         /* Look for the nested-name-specifier.  In case of error here,
3964            consume the trailing id to avoid subsequent error messages
3965            for usual cases.  */
3966         scope = cp_parser_nested_name_specifier (parser,
3967                                                  /*typename_keyword_p=*/true,
3968                                                  /*check_dependency_p=*/true,
3969                                                  /*type_p=*/true,
3970                                                  /*is_declaration=*/true);
3971
3972         /* Look for the optional `template' keyword.  */
3973         template_p = cp_parser_optional_template_keyword (parser);
3974         /* We don't know whether we're looking at a template-id or an
3975            identifier.  */
3976         cp_parser_parse_tentatively (parser);
3977         /* Try a template-id.  */
3978         id = cp_parser_template_id (parser, template_p,
3979                                     /*check_dependency_p=*/true,
3980                                     /*is_declaration=*/true);
3981         /* If that didn't work, try an identifier.  */
3982         if (!cp_parser_parse_definitely (parser))
3983           id = cp_parser_identifier (parser);
3984
3985         /* Don't process id if nested name specifier is invalid.  */
3986         if (!scope || scope == error_mark_node)
3987           return error_mark_node;
3988         /* If we look up a template-id in a non-dependent qualifying
3989            scope, there's no need to create a dependent type.  */
3990         if (TREE_CODE (id) == TYPE_DECL
3991             && (!TYPE_P (scope)
3992                 || !dependent_type_p (parser->scope)))
3993           type = TREE_TYPE (id);
3994         /* Create a TYPENAME_TYPE to represent the type to which the
3995            functional cast is being performed.  */
3996         else
3997           type = make_typename_type (parser->scope, id,
3998                                      typename_type,
3999                                      /*complain=*/1);
4000
4001         postfix_expression = cp_parser_functional_cast (parser, type);
4002       }
4003       break;
4004
4005     default:
4006       {
4007         tree type;
4008
4009         /* If the next thing is a simple-type-specifier, we may be
4010            looking at a functional cast.  We could also be looking at
4011            an id-expression.  So, we try the functional cast, and if
4012            that doesn't work we fall back to the primary-expression.  */
4013         cp_parser_parse_tentatively (parser);
4014         /* Look for the simple-type-specifier.  */
4015         type = cp_parser_simple_type_specifier (parser,
4016                                                 /*decl_specs=*/NULL,
4017                                                 CP_PARSER_FLAGS_NONE);
4018         /* Parse the cast itself.  */
4019         if (!cp_parser_error_occurred (parser))
4020           postfix_expression
4021             = cp_parser_functional_cast (parser, type);
4022         /* If that worked, we're done.  */
4023         if (cp_parser_parse_definitely (parser))
4024           break;
4025
4026         /* If the functional-cast didn't work out, try a
4027            compound-literal.  */
4028         if (cp_parser_allow_gnu_extensions_p (parser)
4029             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4030           {
4031             VEC(constructor_elt,gc) *initializer_list = NULL;
4032             bool saved_in_type_id_in_expr_p;
4033
4034             cp_parser_parse_tentatively (parser);
4035             /* Consume the `('.  */
4036             cp_lexer_consume_token (parser->lexer);
4037             /* Parse the type.  */
4038             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4039             parser->in_type_id_in_expr_p = true;
4040             type = cp_parser_type_id (parser);
4041             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4042             /* Look for the `)'.  */
4043             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4044             /* Look for the `{'.  */
4045             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4046             /* If things aren't going well, there's no need to
4047                keep going.  */
4048             if (!cp_parser_error_occurred (parser))
4049               {
4050                 bool non_constant_p;
4051                 /* Parse the initializer-list.  */
4052                 initializer_list
4053                   = cp_parser_initializer_list (parser, &non_constant_p);
4054                 /* Allow a trailing `,'.  */
4055                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4056                   cp_lexer_consume_token (parser->lexer);
4057                 /* Look for the final `}'.  */
4058                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4059               }
4060             /* If that worked, we're definitely looking at a
4061                compound-literal expression.  */
4062             if (cp_parser_parse_definitely (parser))
4063               {
4064                 /* Warn the user that a compound literal is not
4065                    allowed in standard C++.  */
4066                 if (pedantic)
4067                   pedwarn ("ISO C++ forbids compound-literals");
4068                 /* Form the representation of the compound-literal.  */
4069                 postfix_expression
4070                   = finish_compound_literal (type, initializer_list);
4071                 break;
4072               }
4073           }
4074
4075         /* It must be a primary-expression.  */
4076         postfix_expression = cp_parser_primary_expression (parser,
4077                                                            cast_p,
4078                                                            &idk,
4079                                                            &qualifying_class);
4080       }
4081       break;
4082     }
4083
4084   /* If we were avoiding committing to the processing of a
4085      qualified-id until we knew whether or not we had a
4086      pointer-to-member, we now know.  */
4087   if (qualifying_class)
4088     {
4089       bool done;
4090
4091       /* Peek at the next token.  */
4092       token = cp_lexer_peek_token (parser->lexer);
4093       done = (token->type != CPP_OPEN_SQUARE
4094               && token->type != CPP_OPEN_PAREN
4095               && token->type != CPP_DOT
4096               && token->type != CPP_DEREF
4097               && token->type != CPP_PLUS_PLUS
4098               && token->type != CPP_MINUS_MINUS);
4099
4100       postfix_expression = finish_qualified_id_expr (qualifying_class,
4101                                                      postfix_expression,
4102                                                      done,
4103                                                      address_p);
4104       if (done)
4105         return postfix_expression;
4106     }
4107
4108   /* Keep looping until the postfix-expression is complete.  */
4109   while (true)
4110     {
4111       if (idk == CP_ID_KIND_UNQUALIFIED
4112           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4113           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4114         /* It is not a Koenig lookup function call.  */
4115         postfix_expression
4116           = unqualified_name_lookup_error (postfix_expression);
4117
4118       /* Peek at the next token.  */
4119       token = cp_lexer_peek_token (parser->lexer);
4120
4121       switch (token->type)
4122         {
4123         case CPP_OPEN_SQUARE:
4124           postfix_expression
4125             = cp_parser_postfix_open_square_expression (parser,
4126                                                         postfix_expression,
4127                                                         false);
4128           idk = CP_ID_KIND_NONE;
4129           break;
4130
4131         case CPP_OPEN_PAREN:
4132           /* postfix-expression ( expression-list [opt] ) */
4133           {
4134             bool koenig_p;
4135             bool is_builtin_constant_p;
4136             bool saved_integral_constant_expression_p = false;
4137             bool saved_non_integral_constant_expression_p = false;
4138             tree args;
4139
4140             is_builtin_constant_p
4141               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4142             if (is_builtin_constant_p)
4143               {
4144                 /* The whole point of __builtin_constant_p is to allow
4145                    non-constant expressions to appear as arguments.  */
4146                 saved_integral_constant_expression_p
4147                   = parser->integral_constant_expression_p;
4148                 saved_non_integral_constant_expression_p
4149                   = parser->non_integral_constant_expression_p;
4150                 parser->integral_constant_expression_p = false;
4151               }
4152             args = (cp_parser_parenthesized_expression_list
4153                     (parser, /*is_attribute_list=*/false,
4154                      /*cast_p=*/false,
4155                      /*non_constant_p=*/NULL));
4156             if (is_builtin_constant_p)
4157               {
4158                 parser->integral_constant_expression_p
4159                   = saved_integral_constant_expression_p;
4160                 parser->non_integral_constant_expression_p
4161                   = saved_non_integral_constant_expression_p;
4162               }
4163
4164             if (args == error_mark_node)
4165               {
4166                 postfix_expression = error_mark_node;
4167                 break;
4168               }
4169
4170             /* Function calls are not permitted in
4171                constant-expressions.  */
4172             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4173                 && cp_parser_non_integral_constant_expression (parser,
4174                                                                "a function call"))
4175               {
4176                 postfix_expression = error_mark_node;
4177                 break;
4178               }
4179
4180             koenig_p = false;
4181             if (idk == CP_ID_KIND_UNQUALIFIED)
4182               {
4183                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4184                   {
4185                     if (args)
4186                       {
4187                         koenig_p = true;
4188                         postfix_expression
4189                           = perform_koenig_lookup (postfix_expression, args);
4190                       }
4191                     else
4192                       postfix_expression
4193                         = unqualified_fn_lookup_error (postfix_expression);
4194                   }
4195                 /* We do not perform argument-dependent lookup if
4196                    normal lookup finds a non-function, in accordance
4197                    with the expected resolution of DR 218.  */
4198                 else if (args && is_overloaded_fn (postfix_expression))
4199                   {
4200                     tree fn = get_first_fn (postfix_expression);
4201
4202                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4203                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4204
4205                     /* Only do argument dependent lookup if regular
4206                        lookup does not find a set of member functions.
4207                        [basic.lookup.koenig]/2a  */
4208                     if (!DECL_FUNCTION_MEMBER_P (fn))
4209                       {
4210                         koenig_p = true;
4211                         postfix_expression
4212                           = perform_koenig_lookup (postfix_expression, args);
4213                       }
4214                   }
4215               }
4216
4217             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4218               {
4219                 tree instance = TREE_OPERAND (postfix_expression, 0);
4220                 tree fn = TREE_OPERAND (postfix_expression, 1);
4221
4222                 if (processing_template_decl
4223                     && (type_dependent_expression_p (instance)
4224                         || (!BASELINK_P (fn)
4225                             && TREE_CODE (fn) != FIELD_DECL)
4226                         || type_dependent_expression_p (fn)
4227                         || any_type_dependent_arguments_p (args)))
4228                   {
4229                     postfix_expression
4230                       = build_min_nt (CALL_EXPR, postfix_expression,
4231                                       args, NULL_TREE);
4232                     break;
4233                   }
4234
4235                 if (BASELINK_P (fn))
4236                   postfix_expression
4237                     = (build_new_method_call
4238                        (instance, fn, args, NULL_TREE,
4239                         (idk == CP_ID_KIND_QUALIFIED
4240                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4241                 else
4242                   postfix_expression
4243                     = finish_call_expr (postfix_expression, args,
4244                                         /*disallow_virtual=*/false,
4245                                         /*koenig_p=*/false);
4246               }
4247             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4248                      || TREE_CODE (postfix_expression) == MEMBER_REF
4249                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4250               postfix_expression = (build_offset_ref_call_from_tree
4251                                     (postfix_expression, args));
4252             else if (idk == CP_ID_KIND_QUALIFIED)
4253               /* A call to a static class member, or a namespace-scope
4254                  function.  */
4255               postfix_expression
4256                 = finish_call_expr (postfix_expression, args,
4257                                     /*disallow_virtual=*/true,
4258                                     koenig_p);
4259             else
4260               /* All other function calls.  */
4261               postfix_expression
4262                 = finish_call_expr (postfix_expression, args,
4263                                     /*disallow_virtual=*/false,
4264                                     koenig_p);
4265
4266             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4267             idk = CP_ID_KIND_NONE;
4268           }
4269           break;
4270
4271         case CPP_DOT:
4272         case CPP_DEREF:
4273           /* postfix-expression . template [opt] id-expression
4274              postfix-expression . pseudo-destructor-name
4275              postfix-expression -> template [opt] id-expression
4276              postfix-expression -> pseudo-destructor-name */
4277
4278           /* Consume the `.' or `->' operator.  */
4279           cp_lexer_consume_token (parser->lexer);
4280
4281           postfix_expression
4282             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4283                                                       postfix_expression,
4284                                                       false, &idk);
4285           break;
4286
4287         case CPP_PLUS_PLUS:
4288           /* postfix-expression ++  */
4289           /* Consume the `++' token.  */
4290           cp_lexer_consume_token (parser->lexer);
4291           /* Generate a representation for the complete expression.  */
4292           postfix_expression
4293             = finish_increment_expr (postfix_expression,
4294                                      POSTINCREMENT_EXPR);
4295           /* Increments may not appear in constant-expressions.  */
4296           if (cp_parser_non_integral_constant_expression (parser,
4297                                                           "an increment"))
4298             postfix_expression = error_mark_node;
4299           idk = CP_ID_KIND_NONE;
4300           break;
4301
4302         case CPP_MINUS_MINUS:
4303           /* postfix-expression -- */
4304           /* Consume the `--' token.  */
4305           cp_lexer_consume_token (parser->lexer);
4306           /* Generate a representation for the complete expression.  */
4307           postfix_expression
4308             = finish_increment_expr (postfix_expression,
4309                                      POSTDECREMENT_EXPR);
4310           /* Decrements may not appear in constant-expressions.  */
4311           if (cp_parser_non_integral_constant_expression (parser,
4312                                                           "a decrement"))
4313             postfix_expression = error_mark_node;
4314           idk = CP_ID_KIND_NONE;
4315           break;
4316
4317         default:
4318           return postfix_expression;
4319         }
4320     }
4321
4322   /* We should never get here.  */
4323   gcc_unreachable ();
4324   return error_mark_node;
4325 }
4326
4327 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4328    by cp_parser_builtin_offsetof.  We're looking for
4329
4330      postfix-expression [ expression ]
4331
4332    FOR_OFFSETOF is set if we're being called in that context, which
4333    changes how we deal with integer constant expressions.  */
4334
4335 static tree
4336 cp_parser_postfix_open_square_expression (cp_parser *parser,
4337                                           tree postfix_expression,
4338                                           bool for_offsetof)
4339 {
4340   tree index;
4341
4342   /* Consume the `[' token.  */
4343   cp_lexer_consume_token (parser->lexer);
4344
4345   /* Parse the index expression.  */
4346   /* ??? For offsetof, there is a question of what to allow here.  If
4347      offsetof is not being used in an integral constant expression context,
4348      then we *could* get the right answer by computing the value at runtime.
4349      If we are in an integral constant expression context, then we might
4350      could accept any constant expression; hard to say without analysis.
4351      Rather than open the barn door too wide right away, allow only integer
4352      constant expressions here.  */
4353   if (for_offsetof)
4354     index = cp_parser_constant_expression (parser, false, NULL);
4355   else
4356     index = cp_parser_expression (parser, /*cast_p=*/false);
4357
4358   /* Look for the closing `]'.  */
4359   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4360
4361   /* Build the ARRAY_REF.  */
4362   postfix_expression = grok_array_decl (postfix_expression, index);
4363
4364   /* When not doing offsetof, array references are not permitted in
4365      constant-expressions.  */
4366   if (!for_offsetof
4367       && (cp_parser_non_integral_constant_expression
4368           (parser, "an array reference")))
4369     postfix_expression = error_mark_node;
4370
4371   return postfix_expression;
4372 }
4373
4374 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4375    by cp_parser_builtin_offsetof.  We're looking for
4376
4377      postfix-expression . template [opt] id-expression
4378      postfix-expression . pseudo-destructor-name
4379      postfix-expression -> template [opt] id-expression
4380      postfix-expression -> pseudo-destructor-name
4381
4382    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4383    limits what of the above we'll actually accept, but nevermind.
4384    TOKEN_TYPE is the "." or "->" token, which will already have been
4385    removed from the stream.  */
4386
4387 static tree
4388 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4389                                         enum cpp_ttype token_type,
4390                                         tree postfix_expression,
4391                                         bool for_offsetof, cp_id_kind *idk)
4392 {
4393   tree name;
4394   bool dependent_p;
4395   bool template_p;
4396   bool pseudo_destructor_p;
4397   tree scope = NULL_TREE;
4398
4399   /* If this is a `->' operator, dereference the pointer.  */
4400   if (token_type == CPP_DEREF)
4401     postfix_expression = build_x_arrow (postfix_expression);
4402   /* Check to see whether or not the expression is type-dependent.  */
4403   dependent_p = type_dependent_expression_p (postfix_expression);
4404   /* The identifier following the `->' or `.' is not qualified.  */
4405   parser->scope = NULL_TREE;
4406   parser->qualifying_scope = NULL_TREE;
4407   parser->object_scope = NULL_TREE;
4408   *idk = CP_ID_KIND_NONE;
4409   /* Enter the scope corresponding to the type of the object
4410      given by the POSTFIX_EXPRESSION.  */
4411   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4412     {
4413       scope = TREE_TYPE (postfix_expression);
4414       /* According to the standard, no expression should ever have
4415          reference type.  Unfortunately, we do not currently match
4416          the standard in this respect in that our internal representation
4417          of an expression may have reference type even when the standard
4418          says it does not.  Therefore, we have to manually obtain the
4419          underlying type here.  */
4420       scope = non_reference (scope);
4421       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4422       scope = complete_type_or_else (scope, NULL_TREE);
4423       /* Let the name lookup machinery know that we are processing a
4424          class member access expression.  */
4425       parser->context->object_type = scope;
4426       /* If something went wrong, we want to be able to discern that case,
4427          as opposed to the case where there was no SCOPE due to the type
4428          of expression being dependent.  */
4429       if (!scope)
4430         scope = error_mark_node;
4431       /* If the SCOPE was erroneous, make the various semantic analysis
4432          functions exit quickly -- and without issuing additional error
4433          messages.  */
4434       if (scope == error_mark_node)
4435         postfix_expression = error_mark_node;
4436     }
4437
4438   /* Assume this expression is not a pseudo-destructor access.  */
4439   pseudo_destructor_p = false;
4440
4441   /* If the SCOPE is a scalar type, then, if this is a valid program,
4442      we must be looking at a pseudo-destructor-name.  */
4443   if (scope && SCALAR_TYPE_P (scope))
4444     {
4445       tree s;
4446       tree type;
4447
4448       cp_parser_parse_tentatively (parser);
4449       /* Parse the pseudo-destructor-name.  */
4450       s = NULL_TREE;
4451       cp_parser_pseudo_destructor_name (parser, &s, &type);
4452       if (cp_parser_parse_definitely (parser))
4453         {
4454           pseudo_destructor_p = true;
4455           postfix_expression
4456             = finish_pseudo_destructor_expr (postfix_expression,
4457                                              s, TREE_TYPE (type));
4458         }
4459     }
4460
4461   if (!pseudo_destructor_p)
4462     {
4463       /* If the SCOPE is not a scalar type, we are looking at an
4464          ordinary class member access expression, rather than a
4465          pseudo-destructor-name.  */
4466       template_p = cp_parser_optional_template_keyword (parser);
4467       /* Parse the id-expression.  */
4468       name = cp_parser_id_expression (parser, template_p,
4469                                       /*check_dependency_p=*/true,
4470                                       /*template_p=*/NULL,
4471                                       /*declarator_p=*/false);
4472       /* In general, build a SCOPE_REF if the member name is qualified.
4473          However, if the name was not dependent and has already been
4474          resolved; there is no need to build the SCOPE_REF.  For example;
4475
4476              struct X { void f(); };
4477              template <typename T> void f(T* t) { t->X::f(); }
4478
4479          Even though "t" is dependent, "X::f" is not and has been resolved
4480          to a BASELINK; there is no need to include scope information.  */
4481
4482       /* But we do need to remember that there was an explicit scope for
4483          virtual function calls.  */
4484       if (parser->scope)
4485         *idk = CP_ID_KIND_QUALIFIED;
4486
4487       /* If the name is a template-id that names a type, we will get a
4488          TYPE_DECL here.  That is invalid code.  */
4489       if (TREE_CODE (name) == TYPE_DECL)
4490         {
4491           error ("invalid use of %qD", name);
4492           postfix_expression = error_mark_node;
4493         }
4494       else
4495         {
4496           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4497             {
4498               name = build_nt (SCOPE_REF, parser->scope, name);
4499               parser->scope = NULL_TREE;
4500               parser->qualifying_scope = NULL_TREE;
4501               parser->object_scope = NULL_TREE;
4502             }
4503           if (scope && name && BASELINK_P (name))
4504             adjust_result_of_qualified_name_lookup
4505               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4506           postfix_expression
4507             = finish_class_member_access_expr (postfix_expression, name);
4508         }
4509     }
4510
4511   /* We no longer need to look up names in the scope of the object on
4512      the left-hand side of the `.' or `->' operator.  */
4513   parser->context->object_type = NULL_TREE;
4514
4515   /* Outside of offsetof, these operators may not appear in
4516      constant-expressions.  */
4517   if (!for_offsetof
4518       && (cp_parser_non_integral_constant_expression
4519           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4520     postfix_expression = error_mark_node;
4521
4522   return postfix_expression;
4523 }
4524
4525 /* Parse a parenthesized expression-list.
4526
4527    expression-list:
4528      assignment-expression
4529      expression-list, assignment-expression
4530
4531    attribute-list:
4532      expression-list
4533      identifier
4534      identifier, expression-list
4535
4536    CAST_P is true if this expression is the target of a cast.
4537
4538    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4539    representation of an assignment-expression.  Note that a TREE_LIST
4540    is returned even if there is only a single expression in the list.
4541    error_mark_node is returned if the ( and or ) are
4542    missing. NULL_TREE is returned on no expressions. The parentheses
4543    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4544    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4545    indicates whether or not all of the expressions in the list were
4546    constant.  */
4547
4548 static tree
4549 cp_parser_parenthesized_expression_list (cp_parser* parser,
4550                                          bool is_attribute_list,
4551                                          bool cast_p,
4552                                          bool *non_constant_p)
4553 {
4554   tree expression_list = NULL_TREE;
4555   bool fold_expr_p = is_attribute_list;
4556   tree identifier = NULL_TREE;
4557
4558   /* Assume all the expressions will be constant.  */
4559   if (non_constant_p)
4560     *non_constant_p = false;
4561
4562   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4563     return error_mark_node;
4564
4565   /* Consume expressions until there are no more.  */
4566   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4567     while (true)
4568       {
4569         tree expr;
4570
4571         /* At the beginning of attribute lists, check to see if the
4572            next token is an identifier.  */
4573         if (is_attribute_list
4574             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4575           {
4576             cp_token *token;
4577
4578             /* Consume the identifier.  */
4579             token = cp_lexer_consume_token (parser->lexer);
4580             /* Save the identifier.  */
4581             identifier = token->value;
4582           }
4583         else
4584           {
4585             /* Parse the next assignment-expression.  */
4586             if (non_constant_p)
4587               {
4588                 bool expr_non_constant_p;
4589                 expr = (cp_parser_constant_expression
4590                         (parser, /*allow_non_constant_p=*/true,
4591                          &expr_non_constant_p));
4592                 if (expr_non_constant_p)
4593                   *non_constant_p = true;
4594               }
4595             else
4596               expr = cp_parser_assignment_expression (parser, cast_p);
4597
4598             if (fold_expr_p)
4599               expr = fold_non_dependent_expr (expr);
4600
4601              /* Add it to the list.  We add error_mark_node
4602                 expressions to the list, so that we can still tell if
4603                 the correct form for a parenthesized expression-list
4604                 is found. That gives better errors.  */
4605             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4606
4607             if (expr == error_mark_node)
4608               goto skip_comma;
4609           }
4610
4611         /* After the first item, attribute lists look the same as
4612            expression lists.  */
4613         is_attribute_list = false;
4614
4615       get_comma:;
4616         /* If the next token isn't a `,', then we are done.  */
4617         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4618           break;
4619
4620         /* Otherwise, consume the `,' and keep going.  */
4621         cp_lexer_consume_token (parser->lexer);
4622       }
4623
4624   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4625     {
4626       int ending;
4627
4628     skip_comma:;
4629       /* We try and resync to an unnested comma, as that will give the
4630          user better diagnostics.  */
4631       ending = cp_parser_skip_to_closing_parenthesis (parser,
4632                                                       /*recovering=*/true,
4633                                                       /*or_comma=*/true,
4634                                                       /*consume_paren=*/true);
4635       if (ending < 0)
4636         goto get_comma;
4637       if (!ending)
4638         return error_mark_node;
4639     }
4640
4641   /* We built up the list in reverse order so we must reverse it now.  */
4642   expression_list = nreverse (expression_list);
4643   if (identifier)
4644     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4645
4646   return expression_list;
4647 }
4648
4649 /* Parse a pseudo-destructor-name.
4650
4651    pseudo-destructor-name:
4652      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4653      :: [opt] nested-name-specifier template template-id :: ~ type-name
4654      :: [opt] nested-name-specifier [opt] ~ type-name
4655
4656    If either of the first two productions is used, sets *SCOPE to the
4657    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4658    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4659    or ERROR_MARK_NODE if the parse fails.  */
4660
4661 static void
4662 cp_parser_pseudo_destructor_name (cp_parser* parser,
4663                                   tree* scope,
4664                                   tree* type)
4665 {
4666   bool nested_name_specifier_p;
4667
4668   /* Assume that things will not work out.  */
4669   *type = error_mark_node;
4670
4671   /* Look for the optional `::' operator.  */
4672   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4673   /* Look for the optional nested-name-specifier.  */
4674   nested_name_specifier_p
4675     = (cp_parser_nested_name_specifier_opt (parser,
4676                                             /*typename_keyword_p=*/false,
4677                                             /*check_dependency_p=*/true,
4678                                             /*type_p=*/false,
4679                                             /*is_declaration=*/true)
4680        != NULL_TREE);
4681   /* Now, if we saw a nested-name-specifier, we might be doing the
4682      second production.  */
4683   if (nested_name_specifier_p
4684       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4685     {
4686       /* Consume the `template' keyword.  */
4687       cp_lexer_consume_token (parser->lexer);
4688       /* Parse the template-id.  */
4689       cp_parser_template_id (parser,
4690                              /*template_keyword_p=*/true,
4691                              /*check_dependency_p=*/false,
4692                              /*is_declaration=*/true);
4693       /* Look for the `::' token.  */
4694       cp_parser_require (parser, CPP_SCOPE, "`::'");
4695     }
4696   /* If the next token is not a `~', then there might be some
4697      additional qualification.  */
4698   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4699     {
4700       /* Look for the type-name.  */
4701       *scope = TREE_TYPE (cp_parser_type_name (parser));
4702
4703       if (*scope == error_mark_node)
4704         return;
4705
4706       /* If we don't have ::~, then something has gone wrong.  Since
4707          the only caller of this function is looking for something
4708          after `.' or `->' after a scalar type, most likely the
4709          program is trying to get a member of a non-aggregate
4710          type.  */
4711       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4712           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4713         {
4714           cp_parser_error (parser, "request for member of non-aggregate type");
4715           return;
4716         }
4717
4718       /* Look for the `::' token.  */
4719       cp_parser_require (parser, CPP_SCOPE, "`::'");
4720     }
4721   else
4722     *scope = NULL_TREE;
4723
4724   /* Look for the `~'.  */
4725   cp_parser_require (parser, CPP_COMPL, "`~'");
4726   /* Look for the type-name again.  We are not responsible for
4727      checking that it matches the first type-name.  */
4728   *type = cp_parser_type_name (parser);
4729 }
4730
4731 /* Parse a unary-expression.
4732
4733    unary-expression:
4734      postfix-expression
4735      ++ cast-expression
4736      -- cast-expression
4737      unary-operator cast-expression
4738      sizeof unary-expression
4739      sizeof ( type-id )
4740      new-expression
4741      delete-expression
4742
4743    GNU Extensions:
4744
4745    unary-expression:
4746      __extension__ cast-expression
4747      __alignof__ unary-expression
4748      __alignof__ ( type-id )
4749      __real__ cast-expression
4750      __imag__ cast-expression
4751      && identifier
4752
4753    ADDRESS_P is true iff the unary-expression is appearing as the
4754    operand of the `&' operator.   CAST_P is true if this expression is
4755    the target of a cast.
4756
4757    Returns a representation of the expression.  */
4758
4759 static tree
4760 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4761 {
4762   cp_token *token;
4763   enum tree_code unary_operator;
4764
4765   /* Peek at the next token.  */
4766   token = cp_lexer_peek_token (parser->lexer);
4767   /* Some keywords give away the kind of expression.  */
4768   if (token->type == CPP_KEYWORD)
4769     {
4770       enum rid keyword = token->keyword;
4771
4772       switch (keyword)
4773         {
4774         case RID_ALIGNOF:
4775         case RID_SIZEOF:
4776           {
4777             tree operand;
4778             enum tree_code op;
4779
4780             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4781             /* Consume the token.  */
4782             cp_lexer_consume_token (parser->lexer);
4783             /* Parse the operand.  */
4784             operand = cp_parser_sizeof_operand (parser, keyword);
4785
4786             if (TYPE_P (operand))
4787               return cxx_sizeof_or_alignof_type (operand, op, true);
4788             else
4789               return cxx_sizeof_or_alignof_expr (operand, op);
4790           }
4791
4792         case RID_NEW:
4793           return cp_parser_new_expression (parser);
4794
4795         case RID_DELETE:
4796           return cp_parser_delete_expression (parser);
4797
4798         case RID_EXTENSION:
4799           {
4800             /* The saved value of the PEDANTIC flag.  */
4801             int saved_pedantic;
4802             tree expr;
4803
4804             /* Save away the PEDANTIC flag.  */
4805             cp_parser_extension_opt (parser, &saved_pedantic);
4806             /* Parse the cast-expression.  */
4807             expr = cp_parser_simple_cast_expression (parser);
4808             /* Restore the PEDANTIC flag.  */
4809             pedantic = saved_pedantic;
4810
4811             return expr;
4812           }
4813
4814         case RID_REALPART:
4815         case RID_IMAGPART:
4816           {
4817             tree expression;
4818
4819             /* Consume the `__real__' or `__imag__' token.  */
4820             cp_lexer_consume_token (parser->lexer);
4821             /* Parse the cast-expression.  */
4822             expression = cp_parser_simple_cast_expression (parser);
4823             /* Create the complete representation.  */
4824             return build_x_unary_op ((keyword == RID_REALPART
4825                                       ? REALPART_EXPR : IMAGPART_EXPR),
4826                                      expression);
4827           }
4828           break;
4829
4830         default:
4831           break;
4832         }
4833     }
4834
4835   /* Look for the `:: new' and `:: delete', which also signal the
4836      beginning of a new-expression, or delete-expression,
4837      respectively.  If the next token is `::', then it might be one of
4838      these.  */
4839   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4840     {
4841       enum rid keyword;
4842
4843       /* See if the token after the `::' is one of the keywords in
4844          which we're interested.  */
4845       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4846       /* If it's `new', we have a new-expression.  */
4847       if (keyword == RID_NEW)
4848         return cp_parser_new_expression (parser);
4849       /* Similarly, for `delete'.  */
4850       else if (keyword == RID_DELETE)
4851         return cp_parser_delete_expression (parser);
4852     }
4853
4854   /* Look for a unary operator.  */
4855   unary_operator = cp_parser_unary_operator (token);
4856   /* The `++' and `--' operators can be handled similarly, even though
4857      they are not technically unary-operators in the grammar.  */
4858   if (unary_operator == ERROR_MARK)
4859     {
4860       if (token->type == CPP_PLUS_PLUS)
4861         unary_operator = PREINCREMENT_EXPR;
4862       else if (token->type == CPP_MINUS_MINUS)
4863         unary_operator = PREDECREMENT_EXPR;
4864       /* Handle the GNU address-of-label extension.  */
4865       else if (cp_parser_allow_gnu_extensions_p (parser)
4866                && token->type == CPP_AND_AND)
4867         {
4868           tree identifier;
4869
4870           /* Consume the '&&' token.  */
4871           cp_lexer_consume_token (parser->lexer);
4872           /* Look for the identifier.  */
4873           identifier = cp_parser_identifier (parser);
4874           /* Create an expression representing the address.  */
4875           return finish_label_address_expr (identifier);
4876         }
4877     }
4878   if (unary_operator != ERROR_MARK)
4879     {
4880       tree cast_expression;
4881       tree expression = error_mark_node;
4882       const char *non_constant_p = NULL;
4883
4884       /* Consume the operator token.  */
4885       token = cp_lexer_consume_token (parser->lexer);
4886       /* Parse the cast-expression.  */
4887       cast_expression
4888         = cp_parser_cast_expression (parser,
4889                                      unary_operator == ADDR_EXPR,
4890                                      /*cast_p=*/false);
4891       /* Now, build an appropriate representation.  */
4892       switch (unary_operator)
4893         {
4894         case INDIRECT_REF:
4895           non_constant_p = "`*'";
4896           expression = build_x_indirect_ref (cast_expression, "unary *");
4897           break;
4898
4899         case ADDR_EXPR:
4900           non_constant_p = "`&'";
4901           /* Fall through.  */
4902         case BIT_NOT_EXPR:
4903           expression = build_x_unary_op (unary_operator, cast_expression);
4904           break;
4905
4906         case PREINCREMENT_EXPR:
4907         case PREDECREMENT_EXPR:
4908           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4909                             ? "`++'" : "`--'");
4910           /* Fall through.  */
4911         case UNARY_PLUS_EXPR:
4912         case NEGATE_EXPR:
4913         case TRUTH_NOT_EXPR:
4914           expression = finish_unary_op_expr (unary_operator, cast_expression);
4915           break;
4916
4917         default:
4918           gcc_unreachable ();
4919         }
4920
4921       if (non_constant_p
4922           && cp_parser_non_integral_constant_expression (parser,
4923                                                          non_constant_p))
4924         expression = error_mark_node;
4925
4926       return expression;
4927     }
4928
4929   return cp_parser_postfix_expression (parser, address_p, cast_p);
4930 }
4931
4932 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4933    unary-operator, the corresponding tree code is returned.  */
4934
4935 static enum tree_code
4936 cp_parser_unary_operator (cp_token* token)
4937 {
4938   switch (token->type)
4939     {
4940     case CPP_MULT:
4941       return INDIRECT_REF;
4942
4943     case CPP_AND:
4944       return ADDR_EXPR;
4945
4946     case CPP_PLUS:
4947       return UNARY_PLUS_EXPR;
4948
4949     case CPP_MINUS:
4950       return NEGATE_EXPR;
4951
4952     case CPP_NOT:
4953       return TRUTH_NOT_EXPR;
4954
4955     case CPP_COMPL:
4956       return BIT_NOT_EXPR;
4957
4958     default:
4959       return ERROR_MARK;
4960     }
4961 }
4962
4963 /* Parse a new-expression.
4964
4965    new-expression:
4966      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4967      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4968
4969    Returns a representation of the expression.  */
4970
4971 static tree
4972 cp_parser_new_expression (cp_parser* parser)
4973 {
4974   bool global_scope_p;
4975   tree placement;
4976   tree type;
4977   tree initializer;
4978   tree nelts;
4979
4980   /* Look for the optional `::' operator.  */
4981   global_scope_p
4982     = (cp_parser_global_scope_opt (parser,
4983                                    /*current_scope_valid_p=*/false)
4984        != NULL_TREE);
4985   /* Look for the `new' operator.  */
4986   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4987   /* There's no easy way to tell a new-placement from the
4988      `( type-id )' construct.  */
4989   cp_parser_parse_tentatively (parser);
4990   /* Look for a new-placement.  */
4991   placement = cp_parser_new_placement (parser);
4992   /* If that didn't work out, there's no new-placement.  */
4993   if (!cp_parser_parse_definitely (parser))
4994     placement = NULL_TREE;
4995
4996   /* If the next token is a `(', then we have a parenthesized
4997      type-id.  */
4998   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4999     {
5000       /* Consume the `('.  */
5001       cp_lexer_consume_token (parser->lexer);
5002       /* Parse the type-id.  */
5003       type = cp_parser_type_id (parser);
5004       /* Look for the closing `)'.  */
5005       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5006       /* There should not be a direct-new-declarator in this production,
5007          but GCC used to allowed this, so we check and emit a sensible error
5008          message for this case.  */
5009       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5010         {
5011           error ("array bound forbidden after parenthesized type-id");
5012           inform ("try removing the parentheses around the type-id");
5013           cp_parser_direct_new_declarator (parser);
5014         }
5015       nelts = NULL_TREE;
5016     }
5017   /* Otherwise, there must be a new-type-id.  */
5018   else
5019     type = cp_parser_new_type_id (parser, &nelts);
5020
5021   /* If the next token is a `(', then we have a new-initializer.  */
5022   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5023     initializer = cp_parser_new_initializer (parser);
5024   else
5025     initializer = NULL_TREE;
5026
5027   /* A new-expression may not appear in an integral constant
5028      expression.  */
5029   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5030     return error_mark_node;
5031
5032   /* Create a representation of the new-expression.  */
5033   return build_new (placement, type, nelts, initializer, global_scope_p);
5034 }
5035
5036 /* Parse a new-placement.
5037
5038    new-placement:
5039      ( expression-list )
5040
5041    Returns the same representation as for an expression-list.  */
5042
5043 static tree
5044 cp_parser_new_placement (cp_parser* parser)
5045 {
5046   tree expression_list;
5047
5048   /* Parse the expression-list.  */
5049   expression_list = (cp_parser_parenthesized_expression_list
5050                      (parser, false, /*cast_p=*/false,
5051                       /*non_constant_p=*/NULL));
5052
5053   return expression_list;
5054 }
5055
5056 /* Parse a new-type-id.
5057
5058    new-type-id:
5059      type-specifier-seq new-declarator [opt]
5060
5061    Returns the TYPE allocated.  If the new-type-id indicates an array
5062    type, *NELTS is set to the number of elements in the last array
5063    bound; the TYPE will not include the last array bound.  */
5064
5065 static tree
5066 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5067 {
5068   cp_decl_specifier_seq type_specifier_seq;
5069   cp_declarator *new_declarator;
5070   cp_declarator *declarator;
5071   cp_declarator *outer_declarator;
5072   const char *saved_message;
5073   tree type;
5074
5075   /* The type-specifier sequence must not contain type definitions.
5076      (It cannot contain declarations of new types either, but if they
5077      are not definitions we will catch that because they are not
5078      complete.)  */
5079   saved_message = parser->type_definition_forbidden_message;
5080   parser->type_definition_forbidden_message
5081     = "types may not be defined in a new-type-id";
5082   /* Parse the type-specifier-seq.  */
5083   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5084                                 &type_specifier_seq);
5085   /* Restore the old message.  */
5086   parser->type_definition_forbidden_message = saved_message;
5087   /* Parse the new-declarator.  */
5088   new_declarator = cp_parser_new_declarator_opt (parser);
5089
5090   /* Determine the number of elements in the last array dimension, if
5091      any.  */
5092   *nelts = NULL_TREE;
5093   /* Skip down to the last array dimension.  */
5094   declarator = new_declarator;
5095   outer_declarator = NULL;
5096   while (declarator && (declarator->kind == cdk_pointer
5097                         || declarator->kind == cdk_ptrmem))
5098     {
5099       outer_declarator = declarator;
5100       declarator = declarator->declarator;
5101     }
5102   while (declarator
5103          && declarator->kind == cdk_array
5104          && declarator->declarator
5105          && declarator->declarator->kind == cdk_array)
5106     {
5107       outer_declarator = declarator;
5108       declarator = declarator->declarator;
5109     }
5110
5111   if (declarator && declarator->kind == cdk_array)
5112     {
5113       *nelts = declarator->u.array.bounds;
5114       if (*nelts == error_mark_node)
5115         *nelts = integer_one_node;
5116
5117       if (outer_declarator)
5118         outer_declarator->declarator = declarator->declarator;
5119       else
5120         new_declarator = NULL;
5121     }
5122
5123   type = groktypename (&type_specifier_seq, new_declarator);
5124   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5125     {
5126       *nelts = array_type_nelts_top (type);
5127       type = TREE_TYPE (type);
5128     }
5129   return type;
5130 }
5131
5132 /* Parse an (optional) new-declarator.
5133
5134    new-declarator:
5135      ptr-operator new-declarator [opt]
5136      direct-new-declarator
5137
5138    Returns the declarator.  */
5139
5140 static cp_declarator *
5141 cp_parser_new_declarator_opt (cp_parser* parser)
5142 {
5143   enum tree_code code;
5144   tree type;
5145   cp_cv_quals cv_quals;
5146
5147   /* We don't know if there's a ptr-operator next, or not.  */
5148   cp_parser_parse_tentatively (parser);
5149   /* Look for a ptr-operator.  */
5150   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5151   /* If that worked, look for more new-declarators.  */
5152   if (cp_parser_parse_definitely (parser))
5153     {
5154       cp_declarator *declarator;
5155
5156       /* Parse another optional declarator.  */
5157       declarator = cp_parser_new_declarator_opt (parser);
5158
5159       /* Create the representation of the declarator.  */
5160       if (type)
5161         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5162       else if (code == INDIRECT_REF)
5163         declarator = make_pointer_declarator (cv_quals, declarator);
5164       else
5165         declarator = make_reference_declarator (cv_quals, declarator);
5166
5167       return declarator;
5168     }
5169
5170   /* If the next token is a `[', there is a direct-new-declarator.  */
5171   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5172     return cp_parser_direct_new_declarator (parser);
5173
5174   return NULL;
5175 }
5176
5177 /* Parse a direct-new-declarator.
5178
5179    direct-new-declarator:
5180      [ expression ]
5181      direct-new-declarator [constant-expression]
5182
5183    */
5184
5185 static cp_declarator *
5186 cp_parser_direct_new_declarator (cp_parser* parser)
5187 {
5188   cp_declarator *declarator = NULL;
5189
5190   while (true)
5191     {
5192       tree expression;
5193
5194       /* Look for the opening `['.  */
5195       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5196       /* The first expression is not required to be constant.  */
5197       if (!declarator)
5198         {
5199           expression = cp_parser_expression (parser, /*cast_p=*/false);
5200           /* The standard requires that the expression have integral
5201              type.  DR 74 adds enumeration types.  We believe that the
5202              real intent is that these expressions be handled like the
5203              expression in a `switch' condition, which also allows
5204              classes with a single conversion to integral or
5205              enumeration type.  */
5206           if (!processing_template_decl)
5207             {
5208               expression
5209                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5210                                               expression,
5211                                               /*complain=*/true);
5212               if (!expression)
5213                 {
5214                   error ("expression in new-declarator must have integral "
5215                          "or enumeration type");
5216                   expression = error_mark_node;
5217                 }
5218             }
5219         }
5220       /* But all the other expressions must be.  */
5221       else
5222         expression
5223           = cp_parser_constant_expression (parser,
5224                                            /*allow_non_constant=*/false,
5225                                            NULL);
5226       /* Look for the closing `]'.  */
5227       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5228
5229       /* Add this bound to the declarator.  */
5230       declarator = make_array_declarator (declarator, expression);
5231
5232       /* If the next token is not a `[', then there are no more
5233          bounds.  */
5234       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5235         break;
5236     }
5237
5238   return declarator;
5239 }
5240
5241 /* Parse a new-initializer.
5242
5243    new-initializer:
5244      ( expression-list [opt] )
5245
5246    Returns a representation of the expression-list.  If there is no
5247    expression-list, VOID_ZERO_NODE is returned.  */
5248
5249 static tree
5250 cp_parser_new_initializer (cp_parser* parser)
5251 {
5252   tree expression_list;
5253
5254   expression_list = (cp_parser_parenthesized_expression_list
5255                      (parser, false, /*cast_p=*/false,
5256                       /*non_constant_p=*/NULL));
5257   if (!expression_list)
5258     expression_list = void_zero_node;
5259
5260   return expression_list;
5261 }
5262
5263 /* Parse a delete-expression.
5264
5265    delete-expression:
5266      :: [opt] delete cast-expression
5267      :: [opt] delete [ ] cast-expression
5268
5269    Returns a representation of the expression.  */
5270
5271 static tree
5272 cp_parser_delete_expression (cp_parser* parser)
5273 {
5274   bool global_scope_p;
5275   bool array_p;
5276   tree expression;
5277
5278   /* Look for the optional `::' operator.  */
5279   global_scope_p
5280     = (cp_parser_global_scope_opt (parser,
5281                                    /*current_scope_valid_p=*/false)
5282        != NULL_TREE);
5283   /* Look for the `delete' keyword.  */
5284   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5285   /* See if the array syntax is in use.  */
5286   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5287     {
5288       /* Consume the `[' token.  */
5289       cp_lexer_consume_token (parser->lexer);
5290       /* Look for the `]' token.  */
5291       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5292       /* Remember that this is the `[]' construct.  */
5293       array_p = true;
5294     }
5295   else
5296     array_p = false;
5297
5298   /* Parse the cast-expression.  */
5299   expression = cp_parser_simple_cast_expression (parser);
5300
5301   /* A delete-expression may not appear in an integral constant
5302      expression.  */
5303   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5304     return error_mark_node;
5305
5306   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5307 }
5308
5309 /* Parse a cast-expression.
5310
5311    cast-expression:
5312      unary-expression
5313      ( type-id ) cast-expression
5314
5315    ADDRESS_P is true iff the unary-expression is appearing as the
5316    operand of the `&' operator.   CAST_P is true if this expression is
5317    the target of a cast.
5318
5319    Returns a representation of the expression.  */
5320
5321 static tree
5322 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5323 {
5324   /* If it's a `(', then we might be looking at a cast.  */
5325   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5326     {
5327       tree type = NULL_TREE;
5328       tree expr = NULL_TREE;
5329       bool compound_literal_p;
5330       const char *saved_message;
5331
5332       /* There's no way to know yet whether or not this is a cast.
5333          For example, `(int (3))' is a unary-expression, while `(int)
5334          3' is a cast.  So, we resort to parsing tentatively.  */
5335       cp_parser_parse_tentatively (parser);
5336       /* Types may not be defined in a cast.  */
5337       saved_message = parser->type_definition_forbidden_message;
5338       parser->type_definition_forbidden_message
5339         = "types may not be defined in casts";
5340       /* Consume the `('.  */
5341       cp_lexer_consume_token (parser->lexer);
5342       /* A very tricky bit is that `(struct S) { 3 }' is a
5343          compound-literal (which we permit in C++ as an extension).
5344          But, that construct is not a cast-expression -- it is a
5345          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5346          is legal; if the compound-literal were a cast-expression,
5347          you'd need an extra set of parentheses.)  But, if we parse
5348          the type-id, and it happens to be a class-specifier, then we
5349          will commit to the parse at that point, because we cannot
5350          undo the action that is done when creating a new class.  So,
5351          then we cannot back up and do a postfix-expression.
5352
5353          Therefore, we scan ahead to the closing `)', and check to see
5354          if the token after the `)' is a `{'.  If so, we are not
5355          looking at a cast-expression.
5356
5357          Save tokens so that we can put them back.  */
5358       cp_lexer_save_tokens (parser->lexer);
5359       /* Skip tokens until the next token is a closing parenthesis.
5360          If we find the closing `)', and the next token is a `{', then
5361          we are looking at a compound-literal.  */
5362       compound_literal_p
5363         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5364                                                   /*consume_paren=*/true)
5365            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5366       /* Roll back the tokens we skipped.  */
5367       cp_lexer_rollback_tokens (parser->lexer);
5368       /* If we were looking at a compound-literal, simulate an error
5369          so that the call to cp_parser_parse_definitely below will
5370          fail.  */
5371       if (compound_literal_p)
5372         cp_parser_simulate_error (parser);
5373       else
5374         {
5375           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5376           parser->in_type_id_in_expr_p = true;
5377           /* Look for the type-id.  */
5378           type = cp_parser_type_id (parser);
5379           /* Look for the closing `)'.  */
5380           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5381           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5382         }
5383
5384       /* Restore the saved message.  */
5385       parser->type_definition_forbidden_message = saved_message;
5386
5387       /* If ok so far, parse the dependent expression. We cannot be
5388          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5389          ctor of T, but looks like a cast to function returning T
5390          without a dependent expression.  */
5391       if (!cp_parser_error_occurred (parser))
5392         expr = cp_parser_cast_expression (parser,
5393                                           /*address_p=*/false,
5394                                           /*cast_p=*/true);
5395
5396       if (cp_parser_parse_definitely (parser))
5397         {
5398           /* Warn about old-style casts, if so requested.  */
5399           if (warn_old_style_cast
5400               && !in_system_header
5401               && !VOID_TYPE_P (type)
5402               && current_lang_name != lang_name_c)
5403             warning (0, "use of old-style cast");
5404
5405           /* Only type conversions to integral or enumeration types
5406              can be used in constant-expressions.  */
5407           if (parser->integral_constant_expression_p
5408               && !dependent_type_p (type)
5409               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5410               && (cp_parser_non_integral_constant_expression
5411                   (parser,
5412                    "a cast to a type other than an integral or "
5413                    "enumeration type")))
5414             return error_mark_node;
5415
5416           /* Perform the cast.  */
5417           expr = build_c_cast (type, expr);
5418           return expr;
5419         }
5420     }
5421
5422   /* If we get here, then it's not a cast, so it must be a
5423      unary-expression.  */
5424   return cp_parser_unary_expression (parser, address_p, cast_p);
5425 }
5426
5427 /* Parse a binary expression of the general form:
5428
5429    pm-expression:
5430      cast-expression
5431      pm-expression .* cast-expression
5432      pm-expression ->* cast-expression
5433
5434    multiplicative-expression:
5435      pm-expression
5436      multiplicative-expression * pm-expression
5437      multiplicative-expression / pm-expression
5438      multiplicative-expression % pm-expression
5439
5440    additive-expression:
5441      multiplicative-expression
5442      additive-expression + multiplicative-expression
5443      additive-expression - multiplicative-expression
5444
5445    shift-expression:
5446      additive-expression
5447      shift-expression << additive-expression
5448      shift-expression >> additive-expression
5449
5450    relational-expression:
5451      shift-expression
5452      relational-expression < shift-expression
5453      relational-expression > shift-expression
5454      relational-expression <= shift-expression
5455      relational-expression >= shift-expression
5456
5457   GNU Extension:
5458
5459    relational-expression:
5460      relational-expression <? shift-expression
5461      relational-expression >? shift-expression
5462
5463    equality-expression:
5464      relational-expression
5465      equality-expression == relational-expression
5466      equality-expression != relational-expression
5467
5468    and-expression:
5469      equality-expression
5470      and-expression & equality-expression
5471
5472    exclusive-or-expression:
5473      and-expression
5474      exclusive-or-expression ^ and-expression
5475
5476    inclusive-or-expression:
5477      exclusive-or-expression
5478      inclusive-or-expression | exclusive-or-expression
5479
5480    logical-and-expression:
5481      inclusive-or-expression
5482      logical-and-expression && inclusive-or-expression
5483
5484    logical-or-expression:
5485      logical-and-expression
5486      logical-or-expression || logical-and-expression
5487
5488    All these are implemented with a single function like:
5489
5490    binary-expression:
5491      simple-cast-expression
5492      binary-expression <token> binary-expression
5493
5494    CAST_P is true if this expression is the target of a cast.
5495
5496    The binops_by_token map is used to get the tree codes for each <token> type.
5497    binary-expressions are associated according to a precedence table.  */
5498
5499 #define TOKEN_PRECEDENCE(token) \
5500   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5501    ? PREC_NOT_OPERATOR \
5502    : binops_by_token[token->type].prec)
5503
5504 static tree
5505 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5506 {
5507   cp_parser_expression_stack stack;
5508   cp_parser_expression_stack_entry *sp = &stack[0];
5509   tree lhs, rhs;
5510   cp_token *token;
5511   enum tree_code tree_type;
5512   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5513   bool overloaded_p;
5514
5515   /* Parse the first expression.  */
5516   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5517
5518   for (;;)
5519     {
5520       /* Get an operator token.  */
5521       token = cp_lexer_peek_token (parser->lexer);
5522       if (token->type == CPP_MIN || token->type == CPP_MAX)
5523         cp_parser_warn_min_max ();
5524
5525       new_prec = TOKEN_PRECEDENCE (token);
5526
5527       /* Popping an entry off the stack means we completed a subexpression:
5528          - either we found a token which is not an operator (`>' where it is not
5529            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5530            will happen repeatedly;
5531          - or, we found an operator which has lower priority.  This is the case
5532            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5533            parsing `3 * 4'.  */
5534       if (new_prec <= prec)
5535         {
5536           if (sp == stack)
5537             break;
5538           else
5539             goto pop;
5540         }
5541
5542      get_rhs:
5543       tree_type = binops_by_token[token->type].tree_type;
5544
5545       /* We used the operator token.  */
5546       cp_lexer_consume_token (parser->lexer);
5547
5548       /* Extract another operand.  It may be the RHS of this expression
5549          or the LHS of a new, higher priority expression.  */
5550       rhs = cp_parser_simple_cast_expression (parser);
5551
5552       /* Get another operator token.  Look up its precedence to avoid
5553          building a useless (immediately popped) stack entry for common
5554          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5555       token = cp_lexer_peek_token (parser->lexer);
5556       lookahead_prec = TOKEN_PRECEDENCE (token);
5557       if (lookahead_prec > new_prec)
5558         {
5559           /* ... and prepare to parse the RHS of the new, higher priority
5560              expression.  Since precedence levels on the stack are
5561              monotonically increasing, we do not have to care about
5562              stack overflows.  */
5563           sp->prec = prec;
5564           sp->tree_type = tree_type;
5565           sp->lhs = lhs;
5566           sp++;
5567           lhs = rhs;
5568           prec = new_prec;
5569           new_prec = lookahead_prec;
5570           goto get_rhs;
5571
5572          pop:
5573           /* If the stack is not empty, we have parsed into LHS the right side
5574              (`4' in the example above) of an expression we had suspended.
5575              We can use the information on the stack to recover the LHS (`3')
5576              from the stack together with the tree code (`MULT_EXPR'), and
5577              the precedence of the higher level subexpression
5578              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5579              which will be used to actually build the additive expression.  */
5580           --sp;
5581           prec = sp->prec;
5582           tree_type = sp->tree_type;
5583           rhs = lhs;
5584           lhs = sp->lhs;
5585         }
5586
5587       overloaded_p = false;
5588       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5589
5590       /* If the binary operator required the use of an overloaded operator,
5591          then this expression cannot be an integral constant-expression.
5592          An overloaded operator can be used even if both operands are
5593          otherwise permissible in an integral constant-expression if at
5594          least one of the operands is of enumeration type.  */
5595
5596       if (overloaded_p
5597           && (cp_parser_non_integral_constant_expression
5598               (parser, "calls to overloaded operators")))
5599         return error_mark_node;
5600     }
5601
5602   return lhs;
5603 }
5604
5605
5606 /* Parse the `? expression : assignment-expression' part of a
5607    conditional-expression.  The LOGICAL_OR_EXPR is the
5608    logical-or-expression that started the conditional-expression.
5609    Returns a representation of the entire conditional-expression.
5610
5611    This routine is used by cp_parser_assignment_expression.
5612
5613      ? expression : assignment-expression
5614
5615    GNU Extensions:
5616
5617      ? : assignment-expression */
5618
5619 static tree
5620 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5621 {
5622   tree expr;
5623   tree assignment_expr;
5624
5625   /* Consume the `?' token.  */
5626   cp_lexer_consume_token (parser->lexer);
5627   if (cp_parser_allow_gnu_extensions_p (parser)
5628       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5629     /* Implicit true clause.  */
5630     expr = NULL_TREE;
5631   else
5632     /* Parse the expression.  */
5633     expr = cp_parser_expression (parser, /*cast_p=*/false);
5634
5635   /* The next token should be a `:'.  */
5636   cp_parser_require (parser, CPP_COLON, "`:'");
5637   /* Parse the assignment-expression.  */
5638   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5639
5640   /* Build the conditional-expression.  */
5641   return build_x_conditional_expr (logical_or_expr,
5642                                    expr,
5643                                    assignment_expr);
5644 }
5645
5646 /* Parse an assignment-expression.
5647
5648    assignment-expression:
5649      conditional-expression
5650      logical-or-expression assignment-operator assignment_expression
5651      throw-expression
5652
5653    CAST_P is true if this expression is the target of a cast.
5654
5655    Returns a representation for the expression.  */
5656
5657 static tree
5658 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5659 {
5660   tree expr;
5661
5662   /* If the next token is the `throw' keyword, then we're looking at
5663      a throw-expression.  */
5664   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5665     expr = cp_parser_throw_expression (parser);
5666   /* Otherwise, it must be that we are looking at a
5667      logical-or-expression.  */
5668   else
5669     {
5670       /* Parse the binary expressions (logical-or-expression).  */
5671       expr = cp_parser_binary_expression (parser, cast_p);
5672       /* If the next token is a `?' then we're actually looking at a
5673          conditional-expression.  */
5674       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5675         return cp_parser_question_colon_clause (parser, expr);
5676       else
5677         {
5678           enum tree_code assignment_operator;
5679
5680           /* If it's an assignment-operator, we're using the second
5681              production.  */
5682           assignment_operator
5683             = cp_parser_assignment_operator_opt (parser);
5684           if (assignment_operator != ERROR_MARK)
5685             {
5686               tree rhs;
5687
5688               /* Parse the right-hand side of the assignment.  */
5689               rhs = cp_parser_assignment_expression (parser, cast_p);
5690               /* An assignment may not appear in a
5691                  constant-expression.  */
5692               if (cp_parser_non_integral_constant_expression (parser,
5693                                                               "an assignment"))
5694                 return error_mark_node;
5695               /* Build the assignment expression.  */
5696               expr = build_x_modify_expr (expr,
5697                                           assignment_operator,
5698                                           rhs);
5699             }
5700         }
5701     }
5702
5703   return expr;
5704 }
5705
5706 /* Parse an (optional) assignment-operator.
5707
5708    assignment-operator: one of
5709      = *= /= %= += -= >>= <<= &= ^= |=
5710
5711    GNU Extension:
5712
5713    assignment-operator: one of
5714      <?= >?=
5715
5716    If the next token is an assignment operator, the corresponding tree
5717    code is returned, and the token is consumed.  For example, for
5718    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5719    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5720    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5721    operator, ERROR_MARK is returned.  */
5722
5723 static enum tree_code
5724 cp_parser_assignment_operator_opt (cp_parser* parser)
5725 {
5726   enum tree_code op;
5727   cp_token *token;
5728
5729   /* Peek at the next toen.  */
5730   token = cp_lexer_peek_token (parser->lexer);
5731
5732   switch (token->type)
5733     {
5734     case CPP_EQ:
5735       op = NOP_EXPR;
5736       break;
5737
5738     case CPP_MULT_EQ:
5739       op = MULT_EXPR;
5740       break;
5741
5742     case CPP_DIV_EQ:
5743       op = TRUNC_DIV_EXPR;
5744       break;
5745
5746     case CPP_MOD_EQ:
5747       op = TRUNC_MOD_EXPR;
5748       break;
5749
5750     case CPP_PLUS_EQ:
5751       op = PLUS_EXPR;
5752       break;
5753
5754     case CPP_MINUS_EQ:
5755       op = MINUS_EXPR;
5756       break;
5757
5758     case CPP_RSHIFT_EQ:
5759       op = RSHIFT_EXPR;
5760       break;
5761
5762     case CPP_LSHIFT_EQ:
5763       op = LSHIFT_EXPR;
5764       break;
5765
5766     case CPP_AND_EQ:
5767       op = BIT_AND_EXPR;
5768       break;
5769
5770     case CPP_XOR_EQ:
5771       op = BIT_XOR_EXPR;
5772       break;
5773
5774     case CPP_OR_EQ:
5775       op = BIT_IOR_EXPR;
5776       break;
5777
5778     case CPP_MIN_EQ:
5779       op = MIN_EXPR;
5780       cp_parser_warn_min_max ();
5781       break;
5782
5783     case CPP_MAX_EQ:
5784       op = MAX_EXPR;
5785       cp_parser_warn_min_max ();
5786       break;
5787
5788     default:
5789       /* Nothing else is an assignment operator.  */
5790       op = ERROR_MARK;
5791     }
5792
5793   /* If it was an assignment operator, consume it.  */
5794   if (op != ERROR_MARK)
5795     cp_lexer_consume_token (parser->lexer);
5796
5797   return op;
5798 }
5799
5800 /* Parse an expression.
5801
5802    expression:
5803      assignment-expression
5804      expression , assignment-expression
5805
5806    CAST_P is true if this expression is the target of a cast.
5807
5808    Returns a representation of the expression.  */
5809
5810 static tree
5811 cp_parser_expression (cp_parser* parser, bool cast_p)
5812 {
5813   tree expression = NULL_TREE;
5814
5815   while (true)
5816     {
5817       tree assignment_expression;
5818
5819       /* Parse the next assignment-expression.  */
5820       assignment_expression
5821         = cp_parser_assignment_expression (parser, cast_p);
5822       /* If this is the first assignment-expression, we can just
5823          save it away.  */
5824       if (!expression)
5825         expression = assignment_expression;
5826       else
5827         expression = build_x_compound_expr (expression,
5828                                             assignment_expression);
5829       /* If the next token is not a comma, then we are done with the
5830          expression.  */
5831       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5832         break;
5833       /* Consume the `,'.  */
5834       cp_lexer_consume_token (parser->lexer);
5835       /* A comma operator cannot appear in a constant-expression.  */
5836       if (cp_parser_non_integral_constant_expression (parser,
5837                                                       "a comma operator"))
5838         expression = error_mark_node;
5839     }
5840
5841   return expression;
5842 }
5843
5844 /* Parse a constant-expression.
5845
5846    constant-expression:
5847      conditional-expression
5848
5849   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5850   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5851   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5852   is false, NON_CONSTANT_P should be NULL.  */
5853
5854 static tree
5855 cp_parser_constant_expression (cp_parser* parser,
5856                                bool allow_non_constant_p,
5857                                bool *non_constant_p)
5858 {
5859   bool saved_integral_constant_expression_p;
5860   bool saved_allow_non_integral_constant_expression_p;
5861   bool saved_non_integral_constant_expression_p;
5862   tree expression;
5863
5864   /* It might seem that we could simply parse the
5865      conditional-expression, and then check to see if it were
5866      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5867      one that the compiler can figure out is constant, possibly after
5868      doing some simplifications or optimizations.  The standard has a
5869      precise definition of constant-expression, and we must honor
5870      that, even though it is somewhat more restrictive.
5871
5872      For example:
5873
5874        int i[(2, 3)];
5875
5876      is not a legal declaration, because `(2, 3)' is not a
5877      constant-expression.  The `,' operator is forbidden in a
5878      constant-expression.  However, GCC's constant-folding machinery
5879      will fold this operation to an INTEGER_CST for `3'.  */
5880
5881   /* Save the old settings.  */
5882   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5883   saved_allow_non_integral_constant_expression_p
5884     = parser->allow_non_integral_constant_expression_p;
5885   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5886   /* We are now parsing a constant-expression.  */
5887   parser->integral_constant_expression_p = true;
5888   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5889   parser->non_integral_constant_expression_p = false;
5890   /* Although the grammar says "conditional-expression", we parse an
5891      "assignment-expression", which also permits "throw-expression"
5892      and the use of assignment operators.  In the case that
5893      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5894      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5895      actually essential that we look for an assignment-expression.
5896      For example, cp_parser_initializer_clauses uses this function to
5897      determine whether a particular assignment-expression is in fact
5898      constant.  */
5899   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5900   /* Restore the old settings.  */
5901   parser->integral_constant_expression_p
5902     = saved_integral_constant_expression_p;
5903   parser->allow_non_integral_constant_expression_p
5904     = saved_allow_non_integral_constant_expression_p;
5905   if (allow_non_constant_p)
5906     *non_constant_p = parser->non_integral_constant_expression_p;
5907   else if (parser->non_integral_constant_expression_p)
5908     expression = error_mark_node;
5909   parser->non_integral_constant_expression_p
5910     = saved_non_integral_constant_expression_p;
5911
5912   return expression;
5913 }
5914
5915 /* Parse __builtin_offsetof.
5916
5917    offsetof-expression:
5918      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5919
5920    offsetof-member-designator:
5921      id-expression
5922      | offsetof-member-designator "." id-expression
5923      | offsetof-member-designator "[" expression "]"
5924 */
5925
5926 static tree
5927 cp_parser_builtin_offsetof (cp_parser *parser)
5928 {
5929   int save_ice_p, save_non_ice_p;
5930   tree type, expr;
5931   cp_id_kind dummy;
5932
5933   /* We're about to accept non-integral-constant things, but will
5934      definitely yield an integral constant expression.  Save and
5935      restore these values around our local parsing.  */
5936   save_ice_p = parser->integral_constant_expression_p;
5937   save_non_ice_p = parser->non_integral_constant_expression_p;
5938
5939   /* Consume the "__builtin_offsetof" token.  */
5940   cp_lexer_consume_token (parser->lexer);
5941   /* Consume the opening `('.  */
5942   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5943   /* Parse the type-id.  */
5944   type = cp_parser_type_id (parser);
5945   /* Look for the `,'.  */
5946   cp_parser_require (parser, CPP_COMMA, "`,'");
5947
5948   /* Build the (type *)null that begins the traditional offsetof macro.  */
5949   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5950
5951   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5952   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5953                                                  true, &dummy);
5954   while (true)
5955     {
5956       cp_token *token = cp_lexer_peek_token (parser->lexer);
5957       switch (token->type)
5958         {
5959         case CPP_OPEN_SQUARE:
5960           /* offsetof-member-designator "[" expression "]" */
5961           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5962           break;
5963
5964         case CPP_DOT:
5965           /* offsetof-member-designator "." identifier */
5966           cp_lexer_consume_token (parser->lexer);
5967           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5968                                                          true, &dummy);
5969           break;
5970
5971         case CPP_CLOSE_PAREN:
5972           /* Consume the ")" token.  */
5973           cp_lexer_consume_token (parser->lexer);
5974           goto success;
5975
5976         default:
5977           /* Error.  We know the following require will fail, but
5978              that gives the proper error message.  */
5979           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5980           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5981           expr = error_mark_node;
5982           goto failure;
5983         }
5984     }
5985
5986  success:
5987   /* If we're processing a template, we can't finish the semantics yet.
5988      Otherwise we can fold the entire expression now.  */
5989   if (processing_template_decl)
5990     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5991   else
5992     expr = fold_offsetof (expr);
5993
5994  failure:
5995   parser->integral_constant_expression_p = save_ice_p;
5996   parser->non_integral_constant_expression_p = save_non_ice_p;
5997
5998   return expr;
5999 }
6000
6001 /* Statements [gram.stmt.stmt]  */
6002
6003 /* Parse a statement.
6004
6005    statement:
6006      labeled-statement
6007      expression-statement
6008      compound-statement
6009      selection-statement
6010      iteration-statement
6011      jump-statement
6012      declaration-statement
6013      try-block  */
6014
6015 static void
6016 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6017 {
6018   tree statement;
6019   cp_token *token;
6020   location_t statement_location;
6021
6022   /* There is no statement yet.  */
6023   statement = NULL_TREE;
6024   /* Peek at the next token.  */
6025   token = cp_lexer_peek_token (parser->lexer);
6026   /* Remember the location of the first token in the statement.  */
6027   statement_location = token->location;
6028   /* If this is a keyword, then that will often determine what kind of
6029      statement we have.  */
6030   if (token->type == CPP_KEYWORD)
6031     {
6032       enum rid keyword = token->keyword;
6033
6034       switch (keyword)
6035         {
6036         case RID_CASE:
6037         case RID_DEFAULT:
6038           statement = cp_parser_labeled_statement (parser,
6039                                                    in_statement_expr);
6040           break;
6041
6042         case RID_IF:
6043         case RID_SWITCH:
6044           statement = cp_parser_selection_statement (parser);
6045           break;
6046
6047         case RID_WHILE:
6048         case RID_DO:
6049         case RID_FOR:
6050           statement = cp_parser_iteration_statement (parser);
6051           break;
6052
6053         case RID_BREAK:
6054         case RID_CONTINUE:
6055         case RID_RETURN:
6056         case RID_GOTO:
6057           statement = cp_parser_jump_statement (parser);
6058           break;
6059
6060           /* Objective-C++ exception-handling constructs.  */
6061         case RID_AT_TRY:
6062         case RID_AT_CATCH:
6063         case RID_AT_FINALLY:
6064         case RID_AT_SYNCHRONIZED:
6065         case RID_AT_THROW:
6066           statement = cp_parser_objc_statement (parser);
6067           break;
6068
6069         case RID_TRY:
6070           statement = cp_parser_try_block (parser);
6071           break;
6072
6073         default:
6074           /* It might be a keyword like `int' that can start a
6075              declaration-statement.  */
6076           break;
6077         }
6078     }
6079   else if (token->type == CPP_NAME)
6080     {
6081       /* If the next token is a `:', then we are looking at a
6082          labeled-statement.  */
6083       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6084       if (token->type == CPP_COLON)
6085         statement = cp_parser_labeled_statement (parser, in_statement_expr);
6086     }
6087   /* Anything that starts with a `{' must be a compound-statement.  */
6088   else if (token->type == CPP_OPEN_BRACE)
6089     statement = cp_parser_compound_statement (parser, NULL, false);
6090   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6091      a statement all its own.  */
6092   else if (token->type == CPP_PRAGMA)
6093     {
6094       cp_lexer_handle_pragma (parser->lexer);
6095       return;
6096     }
6097
6098   /* Everything else must be a declaration-statement or an
6099      expression-statement.  Try for the declaration-statement
6100      first, unless we are looking at a `;', in which case we know that
6101      we have an expression-statement.  */
6102   if (!statement)
6103     {
6104       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6105         {
6106           cp_parser_parse_tentatively (parser);
6107           /* Try to parse the declaration-statement.  */
6108           cp_parser_declaration_statement (parser);
6109           /* If that worked, we're done.  */
6110           if (cp_parser_parse_definitely (parser))
6111             return;
6112         }
6113       /* Look for an expression-statement instead.  */
6114       statement = cp_parser_expression_statement (parser, in_statement_expr);
6115     }
6116
6117   /* Set the line number for the statement.  */
6118   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6119     SET_EXPR_LOCATION (statement, statement_location);
6120 }
6121
6122 /* Parse a labeled-statement.
6123
6124    labeled-statement:
6125      identifier : statement
6126      case constant-expression : statement
6127      default : statement
6128
6129    GNU Extension:
6130
6131    labeled-statement:
6132      case constant-expression ... constant-expression : statement
6133
6134    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6135    For an ordinary label, returns a LABEL_EXPR.  */
6136
6137 static tree
6138 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6139 {
6140   cp_token *token;
6141   tree statement = error_mark_node;
6142
6143   /* The next token should be an identifier.  */
6144   token = cp_lexer_peek_token (parser->lexer);
6145   if (token->type != CPP_NAME
6146       && token->type != CPP_KEYWORD)
6147     {
6148       cp_parser_error (parser, "expected labeled-statement");
6149       return error_mark_node;
6150     }
6151
6152   switch (token->keyword)
6153     {
6154     case RID_CASE:
6155       {
6156         tree expr, expr_hi;
6157         cp_token *ellipsis;
6158
6159         /* Consume the `case' token.  */
6160         cp_lexer_consume_token (parser->lexer);
6161         /* Parse the constant-expression.  */
6162         expr = cp_parser_constant_expression (parser,
6163                                               /*allow_non_constant_p=*/false,
6164                                               NULL);
6165
6166         ellipsis = cp_lexer_peek_token (parser->lexer);
6167         if (ellipsis->type == CPP_ELLIPSIS)
6168           {
6169             /* Consume the `...' token.  */
6170             cp_lexer_consume_token (parser->lexer);
6171             expr_hi =
6172               cp_parser_constant_expression (parser,
6173                                              /*allow_non_constant_p=*/false,
6174                                              NULL);
6175             /* We don't need to emit warnings here, as the common code
6176                will do this for us.  */
6177           }
6178         else
6179           expr_hi = NULL_TREE;
6180
6181         if (!parser->in_switch_statement_p)
6182           error ("case label %qE not within a switch statement", expr);
6183         else
6184           statement = finish_case_label (expr, expr_hi);
6185       }
6186       break;
6187
6188     case RID_DEFAULT:
6189       /* Consume the `default' token.  */
6190       cp_lexer_consume_token (parser->lexer);
6191       if (!parser->in_switch_statement_p)
6192         error ("case label not within a switch statement");
6193       else
6194         statement = finish_case_label (NULL_TREE, NULL_TREE);
6195       break;
6196
6197     default:
6198       /* Anything else must be an ordinary label.  */
6199       statement = finish_label_stmt (cp_parser_identifier (parser));
6200       break;
6201     }
6202
6203   /* Require the `:' token.  */
6204   cp_parser_require (parser, CPP_COLON, "`:'");
6205   /* Parse the labeled statement.  */
6206   cp_parser_statement (parser, in_statement_expr);
6207
6208   /* Return the label, in the case of a `case' or `default' label.  */
6209   return statement;
6210 }
6211
6212 /* Parse an expression-statement.
6213
6214    expression-statement:
6215      expression [opt] ;
6216
6217    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6218    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6219    indicates whether this expression-statement is part of an
6220    expression statement.  */
6221
6222 static tree
6223 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6224 {
6225   tree statement = NULL_TREE;
6226
6227   /* If the next token is a ';', then there is no expression
6228      statement.  */
6229   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6230     statement = cp_parser_expression (parser, /*cast_p=*/false);
6231
6232   /* Consume the final `;'.  */
6233   cp_parser_consume_semicolon_at_end_of_statement (parser);
6234
6235   if (in_statement_expr
6236       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6237     /* This is the final expression statement of a statement
6238        expression.  */
6239     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6240   else if (statement)
6241     statement = finish_expr_stmt (statement);
6242   else
6243     finish_stmt ();
6244
6245   return statement;
6246 }
6247
6248 /* Parse a compound-statement.
6249
6250    compound-statement:
6251      { statement-seq [opt] }
6252
6253    Returns a tree representing the statement.  */
6254
6255 static tree
6256 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6257                               bool in_try)
6258 {
6259   tree compound_stmt;
6260
6261   /* Consume the `{'.  */
6262   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6263     return error_mark_node;
6264   /* Begin the compound-statement.  */
6265   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6266   /* Parse an (optional) statement-seq.  */
6267   cp_parser_statement_seq_opt (parser, in_statement_expr);
6268   /* Finish the compound-statement.  */
6269   finish_compound_stmt (compound_stmt);
6270   /* Consume the `}'.  */
6271   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6272
6273   return compound_stmt;
6274 }
6275
6276 /* Parse an (optional) statement-seq.
6277
6278    statement-seq:
6279      statement
6280      statement-seq [opt] statement  */
6281
6282 static void
6283 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6284 {
6285   /* Scan statements until there aren't any more.  */
6286   while (true)
6287     {
6288       /* If we're looking at a `}', then we've run out of statements.  */
6289       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6290           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6291         break;
6292
6293       /* Parse the statement.  */
6294       cp_parser_statement (parser, in_statement_expr);
6295     }
6296 }
6297
6298 /* Parse a selection-statement.
6299
6300    selection-statement:
6301      if ( condition ) statement
6302      if ( condition ) statement else statement
6303      switch ( condition ) statement
6304
6305    Returns the new IF_STMT or SWITCH_STMT.  */
6306
6307 static tree
6308 cp_parser_selection_statement (cp_parser* parser)
6309 {
6310   cp_token *token;
6311   enum rid keyword;
6312
6313   /* Peek at the next token.  */
6314   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6315
6316   /* See what kind of keyword it is.  */
6317   keyword = token->keyword;
6318   switch (keyword)
6319     {
6320     case RID_IF:
6321     case RID_SWITCH:
6322       {
6323         tree statement;
6324         tree condition;
6325
6326         /* Look for the `('.  */
6327         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6328           {
6329             cp_parser_skip_to_end_of_statement (parser);
6330             return error_mark_node;
6331           }
6332
6333         /* Begin the selection-statement.  */
6334         if (keyword == RID_IF)
6335           statement = begin_if_stmt ();
6336         else
6337           statement = begin_switch_stmt ();
6338
6339         /* Parse the condition.  */
6340         condition = cp_parser_condition (parser);
6341         /* Look for the `)'.  */
6342         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6343           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6344                                                  /*consume_paren=*/true);
6345
6346         if (keyword == RID_IF)
6347           {
6348             /* Add the condition.  */
6349             finish_if_stmt_cond (condition, statement);
6350
6351             /* Parse the then-clause.  */
6352             cp_parser_implicitly_scoped_statement (parser);
6353             finish_then_clause (statement);
6354
6355             /* If the next token is `else', parse the else-clause.  */
6356             if (cp_lexer_next_token_is_keyword (parser->lexer,
6357                                                 RID_ELSE))
6358               {
6359                 /* Consume the `else' keyword.  */
6360                 cp_lexer_consume_token (parser->lexer);
6361                 begin_else_clause (statement);
6362                 /* Parse the else-clause.  */
6363                 cp_parser_implicitly_scoped_statement (parser);
6364                 finish_else_clause (statement);
6365               }
6366
6367             /* Now we're all done with the if-statement.  */
6368             finish_if_stmt (statement);
6369           }
6370         else
6371           {
6372             bool in_switch_statement_p;
6373
6374             /* Add the condition.  */
6375             finish_switch_cond (condition, statement);
6376
6377             /* Parse the body of the switch-statement.  */
6378             in_switch_statement_p = parser->in_switch_statement_p;
6379             parser->in_switch_statement_p = true;
6380             cp_parser_implicitly_scoped_statement (parser);
6381             parser->in_switch_statement_p = in_switch_statement_p;
6382
6383             /* Now we're all done with the switch-statement.  */
6384             finish_switch_stmt (statement);
6385           }
6386
6387         return statement;
6388       }
6389       break;
6390
6391     default:
6392       cp_parser_error (parser, "expected selection-statement");
6393       return error_mark_node;
6394     }
6395 }
6396
6397 /* Parse a condition.
6398
6399    condition:
6400      expression
6401      type-specifier-seq declarator = assignment-expression
6402
6403    GNU Extension:
6404
6405    condition:
6406      type-specifier-seq declarator asm-specification [opt]
6407        attributes [opt] = assignment-expression
6408
6409    Returns the expression that should be tested.  */
6410
6411 static tree
6412 cp_parser_condition (cp_parser* parser)
6413 {
6414   cp_decl_specifier_seq type_specifiers;
6415   const char *saved_message;
6416
6417   /* Try the declaration first.  */
6418   cp_parser_parse_tentatively (parser);
6419   /* New types are not allowed in the type-specifier-seq for a
6420      condition.  */
6421   saved_message = parser->type_definition_forbidden_message;
6422   parser->type_definition_forbidden_message
6423     = "types may not be defined in conditions";
6424   /* Parse the type-specifier-seq.  */
6425   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6426                                 &type_specifiers);
6427   /* Restore the saved message.  */
6428   parser->type_definition_forbidden_message = saved_message;
6429   /* If all is well, we might be looking at a declaration.  */
6430   if (!cp_parser_error_occurred (parser))
6431     {
6432       tree decl;
6433       tree asm_specification;
6434       tree attributes;
6435       cp_declarator *declarator;
6436       tree initializer = NULL_TREE;
6437
6438       /* Parse the declarator.  */
6439       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6440                                          /*ctor_dtor_or_conv_p=*/NULL,
6441                                          /*parenthesized_p=*/NULL,
6442                                          /*member_p=*/false);
6443       /* Parse the attributes.  */
6444       attributes = cp_parser_attributes_opt (parser);
6445       /* Parse the asm-specification.  */
6446       asm_specification = cp_parser_asm_specification_opt (parser);
6447       /* If the next token is not an `=', then we might still be
6448          looking at an expression.  For example:
6449
6450            if (A(a).x)
6451
6452          looks like a decl-specifier-seq and a declarator -- but then
6453          there is no `=', so this is an expression.  */
6454       cp_parser_require (parser, CPP_EQ, "`='");
6455       /* If we did see an `=', then we are looking at a declaration
6456          for sure.  */
6457       if (cp_parser_parse_definitely (parser))
6458         {
6459           tree pushed_scope;
6460
6461           /* Create the declaration.  */
6462           decl = start_decl (declarator, &type_specifiers,
6463                              /*initialized_p=*/true,
6464                              attributes, /*prefix_attributes=*/NULL_TREE,
6465                              &pushed_scope);
6466           /* Parse the assignment-expression.  */
6467           initializer = cp_parser_assignment_expression (parser,
6468                                                          /*cast_p=*/false);
6469
6470           /* Process the initializer.  */
6471           cp_finish_decl (decl,
6472                           initializer,
6473                           asm_specification,
6474                           LOOKUP_ONLYCONVERTING);
6475
6476           if (pushed_scope)
6477             pop_scope (pushed_scope);
6478
6479           return convert_from_reference (decl);
6480         }
6481     }
6482   /* If we didn't even get past the declarator successfully, we are
6483      definitely not looking at a declaration.  */
6484   else
6485     cp_parser_abort_tentative_parse (parser);
6486
6487   /* Otherwise, we are looking at an expression.  */
6488   return cp_parser_expression (parser, /*cast_p=*/false);
6489 }
6490
6491 /* Parse an iteration-statement.
6492
6493    iteration-statement:
6494      while ( condition ) statement
6495      do statement while ( expression ) ;
6496      for ( for-init-statement condition [opt] ; expression [opt] )
6497        statement
6498
6499    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6500
6501 static tree
6502 cp_parser_iteration_statement (cp_parser* parser)
6503 {
6504   cp_token *token;
6505   enum rid keyword;
6506   tree statement;
6507   bool in_iteration_statement_p;
6508
6509
6510   /* Peek at the next token.  */
6511   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6512   if (!token)
6513     return error_mark_node;
6514
6515   /* Remember whether or not we are already within an iteration
6516      statement.  */
6517   in_iteration_statement_p = parser->in_iteration_statement_p;
6518
6519   /* See what kind of keyword it is.  */
6520   keyword = token->keyword;
6521   switch (keyword)
6522     {
6523     case RID_WHILE:
6524       {
6525         tree condition;
6526
6527         /* Begin the while-statement.  */
6528         statement = begin_while_stmt ();
6529         /* Look for the `('.  */
6530         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6531         /* Parse the condition.  */
6532         condition = cp_parser_condition (parser);
6533         finish_while_stmt_cond (condition, statement);
6534         /* Look for the `)'.  */
6535         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6536         /* Parse the dependent statement.  */
6537         parser->in_iteration_statement_p = true;
6538         cp_parser_already_scoped_statement (parser);
6539         parser->in_iteration_statement_p = in_iteration_statement_p;
6540         /* We're done with the while-statement.  */
6541         finish_while_stmt (statement);
6542       }
6543       break;
6544
6545     case RID_DO:
6546       {
6547         tree expression;
6548
6549         /* Begin the do-statement.  */
6550         statement = begin_do_stmt ();
6551         /* Parse the body of the do-statement.  */
6552         parser->in_iteration_statement_p = true;
6553         cp_parser_implicitly_scoped_statement (parser);
6554         parser->in_iteration_statement_p = in_iteration_statement_p;
6555         finish_do_body (statement);
6556         /* Look for the `while' keyword.  */
6557         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6558         /* Look for the `('.  */
6559         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6560         /* Parse the expression.  */
6561         expression = cp_parser_expression (parser, /*cast_p=*/false);
6562         /* We're done with the do-statement.  */
6563         finish_do_stmt (expression, statement);
6564         /* Look for the `)'.  */
6565         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6566         /* Look for the `;'.  */
6567         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6568       }
6569       break;
6570
6571     case RID_FOR:
6572       {
6573         tree condition = NULL_TREE;
6574         tree expression = NULL_TREE;
6575
6576         /* Begin the for-statement.  */
6577         statement = begin_for_stmt ();
6578         /* Look for the `('.  */
6579         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6580         /* Parse the initialization.  */
6581         cp_parser_for_init_statement (parser);
6582         finish_for_init_stmt (statement);
6583
6584         /* If there's a condition, process it.  */
6585         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6586           condition = cp_parser_condition (parser);
6587         finish_for_cond (condition, statement);
6588         /* Look for the `;'.  */
6589         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6590
6591         /* If there's an expression, process it.  */
6592         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6593           expression = cp_parser_expression (parser, /*cast_p=*/false);
6594         finish_for_expr (expression, statement);
6595         /* Look for the `)'.  */
6596         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6597
6598         /* Parse the body of the for-statement.  */
6599         parser->in_iteration_statement_p = true;
6600         cp_parser_already_scoped_statement (parser);
6601         parser->in_iteration_statement_p = in_iteration_statement_p;
6602
6603         /* We're done with the for-statement.  */
6604         finish_for_stmt (statement);
6605       }
6606       break;
6607
6608     default:
6609       cp_parser_error (parser, "expected iteration-statement");
6610       statement = error_mark_node;
6611       break;
6612     }
6613
6614   return statement;
6615 }
6616
6617 /* Parse a for-init-statement.
6618
6619    for-init-statement:
6620      expression-statement
6621      simple-declaration  */
6622
6623 static void
6624 cp_parser_for_init_statement (cp_parser* parser)
6625 {
6626   /* If the next token is a `;', then we have an empty
6627      expression-statement.  Grammatically, this is also a
6628      simple-declaration, but an invalid one, because it does not
6629      declare anything.  Therefore, if we did not handle this case
6630      specially, we would issue an error message about an invalid
6631      declaration.  */
6632   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6633     {
6634       /* We're going to speculatively look for a declaration, falling back
6635          to an expression, if necessary.  */
6636       cp_parser_parse_tentatively (parser);
6637       /* Parse the declaration.  */
6638       cp_parser_simple_declaration (parser,
6639                                     /*function_definition_allowed_p=*/false);
6640       /* If the tentative parse failed, then we shall need to look for an
6641          expression-statement.  */
6642       if (cp_parser_parse_definitely (parser))
6643         return;
6644     }
6645
6646   cp_parser_expression_statement (parser, false);
6647 }
6648
6649 /* Parse a jump-statement.
6650
6651    jump-statement:
6652      break ;
6653      continue ;
6654      return expression [opt] ;
6655      goto identifier ;
6656
6657    GNU extension:
6658
6659    jump-statement:
6660      goto * expression ;
6661
6662    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6663
6664 static tree
6665 cp_parser_jump_statement (cp_parser* parser)
6666 {
6667   tree statement = error_mark_node;
6668   cp_token *token;
6669   enum rid keyword;
6670
6671   /* Peek at the next token.  */
6672   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6673   if (!token)
6674     return error_mark_node;
6675
6676   /* See what kind of keyword it is.  */
6677   keyword = token->keyword;
6678   switch (keyword)
6679     {
6680     case RID_BREAK:
6681       if (!parser->in_switch_statement_p
6682           && !parser->in_iteration_statement_p)
6683         {
6684           error ("break statement not within loop or switch");
6685           statement = error_mark_node;
6686         }
6687       else
6688         statement = finish_break_stmt ();
6689       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6690       break;
6691
6692     case RID_CONTINUE:
6693       if (!parser->in_iteration_statement_p)
6694         {
6695           error ("continue statement not within a loop");
6696           statement = error_mark_node;
6697         }
6698       else
6699         statement = finish_continue_stmt ();
6700       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6701       break;
6702
6703     case RID_RETURN:
6704       {
6705         tree expr;
6706
6707         /* If the next token is a `;', then there is no
6708            expression.  */
6709         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6710           expr = cp_parser_expression (parser, /*cast_p=*/false);
6711         else
6712           expr = NULL_TREE;
6713         /* Build the return-statement.  */
6714         statement = finish_return_stmt (expr);
6715         /* Look for the final `;'.  */
6716         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6717       }
6718       break;
6719
6720     case RID_GOTO:
6721       /* Create the goto-statement.  */
6722       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6723         {
6724           /* Issue a warning about this use of a GNU extension.  */
6725           if (pedantic)
6726             pedwarn ("ISO C++ forbids computed gotos");
6727           /* Consume the '*' token.  */
6728           cp_lexer_consume_token (parser->lexer);
6729           /* Parse the dependent expression.  */
6730           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6731         }
6732       else
6733         finish_goto_stmt (cp_parser_identifier (parser));
6734       /* Look for the final `;'.  */
6735       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6736       break;
6737
6738     default:
6739       cp_parser_error (parser, "expected jump-statement");
6740       break;
6741     }
6742
6743   return statement;
6744 }
6745
6746 /* Parse a declaration-statement.
6747
6748    declaration-statement:
6749      block-declaration  */
6750
6751 static void
6752 cp_parser_declaration_statement (cp_parser* parser)
6753 {
6754   void *p;
6755
6756   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6757   p = obstack_alloc (&declarator_obstack, 0);
6758
6759  /* Parse the block-declaration.  */
6760   cp_parser_block_declaration (parser, /*statement_p=*/true);
6761
6762   /* Free any declarators allocated.  */
6763   obstack_free (&declarator_obstack, p);
6764
6765   /* Finish off the statement.  */
6766   finish_stmt ();
6767 }
6768
6769 /* Some dependent statements (like `if (cond) statement'), are
6770    implicitly in their own scope.  In other words, if the statement is
6771    a single statement (as opposed to a compound-statement), it is
6772    none-the-less treated as if it were enclosed in braces.  Any
6773    declarations appearing in the dependent statement are out of scope
6774    after control passes that point.  This function parses a statement,
6775    but ensures that is in its own scope, even if it is not a
6776    compound-statement.
6777
6778    Returns the new statement.  */
6779
6780 static tree
6781 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6782 {
6783   tree statement;
6784
6785   /* If the token is not a `{', then we must take special action.  */
6786   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6787     {
6788       /* Create a compound-statement.  */
6789       statement = begin_compound_stmt (0);
6790       /* Parse the dependent-statement.  */
6791       cp_parser_statement (parser, false);
6792       /* Finish the dummy compound-statement.  */
6793       finish_compound_stmt (statement);
6794     }
6795   /* Otherwise, we simply parse the statement directly.  */
6796   else
6797     statement = cp_parser_compound_statement (parser, NULL, false);
6798
6799   /* Return the statement.  */
6800   return statement;
6801 }
6802
6803 /* For some dependent statements (like `while (cond) statement'), we
6804    have already created a scope.  Therefore, even if the dependent
6805    statement is a compound-statement, we do not want to create another
6806    scope.  */
6807
6808 static void
6809 cp_parser_already_scoped_statement (cp_parser* parser)
6810 {
6811   /* If the token is a `{', then we must take special action.  */
6812   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6813     cp_parser_statement (parser, false);
6814   else
6815     {
6816       /* Avoid calling cp_parser_compound_statement, so that we
6817          don't create a new scope.  Do everything else by hand.  */
6818       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6819       cp_parser_statement_seq_opt (parser, false);
6820       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6821     }
6822 }
6823
6824 /* Declarations [gram.dcl.dcl] */
6825
6826 /* Parse an optional declaration-sequence.
6827
6828    declaration-seq:
6829      declaration
6830      declaration-seq declaration  */
6831
6832 static void
6833 cp_parser_declaration_seq_opt (cp_parser* parser)
6834 {
6835   while (true)
6836     {
6837       cp_token *token;
6838
6839       token = cp_lexer_peek_token (parser->lexer);
6840
6841       if (token->type == CPP_CLOSE_BRACE
6842           || token->type == CPP_EOF)
6843         break;
6844
6845       if (token->type == CPP_SEMICOLON)
6846         {
6847           /* A declaration consisting of a single semicolon is
6848              invalid.  Allow it unless we're being pedantic.  */
6849           cp_lexer_consume_token (parser->lexer);
6850           if (pedantic && !in_system_header)
6851             pedwarn ("extra %<;%>");
6852           continue;
6853         }
6854
6855       /* If we're entering or exiting a region that's implicitly
6856          extern "C", modify the lang context appropriately.  */
6857       if (!parser->implicit_extern_c && token->implicit_extern_c)
6858         {
6859           push_lang_context (lang_name_c);
6860           parser->implicit_extern_c = true;
6861         }
6862       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6863         {
6864           pop_lang_context ();
6865           parser->implicit_extern_c = false;
6866         }
6867
6868       if (token->type == CPP_PRAGMA)
6869         {
6870           /* A top-level declaration can consist solely of a #pragma.
6871              A nested declaration cannot, so this is done here and not
6872              in cp_parser_declaration.  (A #pragma at block scope is
6873              handled in cp_parser_statement.)  */
6874           cp_lexer_handle_pragma (parser->lexer);
6875           continue;
6876         }
6877
6878       /* Parse the declaration itself.  */
6879       cp_parser_declaration (parser);
6880     }
6881 }
6882
6883 /* Parse a declaration.
6884
6885    declaration:
6886      block-declaration
6887      function-definition
6888      template-declaration
6889      explicit-instantiation
6890      explicit-specialization
6891      linkage-specification
6892      namespace-definition
6893
6894    GNU extension:
6895
6896    declaration:
6897       __extension__ declaration */
6898
6899 static void
6900 cp_parser_declaration (cp_parser* parser)
6901 {
6902   cp_token token1;
6903   cp_token token2;
6904   int saved_pedantic;
6905   void *p;
6906
6907   /* Check for the `__extension__' keyword.  */
6908   if (cp_parser_extension_opt (parser, &saved_pedantic))
6909     {
6910       /* Parse the qualified declaration.  */
6911       cp_parser_declaration (parser);
6912       /* Restore the PEDANTIC flag.  */
6913       pedantic = saved_pedantic;
6914
6915       return;
6916     }
6917
6918   /* Try to figure out what kind of declaration is present.  */
6919   token1 = *cp_lexer_peek_token (parser->lexer);
6920
6921   if (token1.type != CPP_EOF)
6922     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6923   else
6924     token2.type = token2.keyword = RID_MAX;
6925
6926   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6927   p = obstack_alloc (&declarator_obstack, 0);
6928
6929   /* If the next token is `extern' and the following token is a string
6930      literal, then we have a linkage specification.  */
6931   if (token1.keyword == RID_EXTERN
6932       && cp_parser_is_string_literal (&token2))
6933     cp_parser_linkage_specification (parser);
6934   /* If the next token is `template', then we have either a template
6935      declaration, an explicit instantiation, or an explicit
6936      specialization.  */
6937   else if (token1.keyword == RID_TEMPLATE)
6938     {
6939       /* `template <>' indicates a template specialization.  */
6940       if (token2.type == CPP_LESS
6941           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6942         cp_parser_explicit_specialization (parser);
6943       /* `template <' indicates a template declaration.  */
6944       else if (token2.type == CPP_LESS)
6945         cp_parser_template_declaration (parser, /*member_p=*/false);
6946       /* Anything else must be an explicit instantiation.  */
6947       else
6948         cp_parser_explicit_instantiation (parser);
6949     }
6950   /* If the next token is `export', then we have a template
6951      declaration.  */
6952   else if (token1.keyword == RID_EXPORT)
6953     cp_parser_template_declaration (parser, /*member_p=*/false);
6954   /* If the next token is `extern', 'static' or 'inline' and the one
6955      after that is `template', we have a GNU extended explicit
6956      instantiation directive.  */
6957   else if (cp_parser_allow_gnu_extensions_p (parser)
6958            && (token1.keyword == RID_EXTERN
6959                || token1.keyword == RID_STATIC
6960                || token1.keyword == RID_INLINE)
6961            && token2.keyword == RID_TEMPLATE)
6962     cp_parser_explicit_instantiation (parser);
6963   /* If the next token is `namespace', check for a named or unnamed
6964      namespace definition.  */
6965   else if (token1.keyword == RID_NAMESPACE
6966            && (/* A named namespace definition.  */
6967                (token2.type == CPP_NAME
6968                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6969                     == CPP_OPEN_BRACE))
6970                /* An unnamed namespace definition.  */
6971                || token2.type == CPP_OPEN_BRACE))
6972     cp_parser_namespace_definition (parser);
6973   /* Objective-C++ declaration/definition.  */
6974   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6975     cp_parser_objc_declaration (parser);
6976   /* We must have either a block declaration or a function
6977      definition.  */
6978   else
6979     /* Try to parse a block-declaration, or a function-definition.  */
6980     cp_parser_block_declaration (parser, /*statement_p=*/false);
6981
6982   /* Free any declarators allocated.  */
6983   obstack_free (&declarator_obstack, p);
6984 }
6985
6986 /* Parse a block-declaration.
6987
6988    block-declaration:
6989      simple-declaration
6990      asm-definition
6991      namespace-alias-definition
6992      using-declaration
6993      using-directive
6994
6995    GNU Extension:
6996
6997    block-declaration:
6998      __extension__ block-declaration
6999      label-declaration
7000
7001    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7002    part of a declaration-statement.  */
7003
7004 static void
7005 cp_parser_block_declaration (cp_parser *parser,
7006                              bool      statement_p)
7007 {
7008   cp_token *token1;
7009   int saved_pedantic;
7010
7011   /* Check for the `__extension__' keyword.  */
7012   if (cp_parser_extension_opt (parser, &saved_pedantic))
7013     {
7014       /* Parse the qualified declaration.  */
7015       cp_parser_block_declaration (parser, statement_p);
7016       /* Restore the PEDANTIC flag.  */
7017       pedantic = saved_pedantic;
7018
7019       return;
7020     }
7021
7022   /* Peek at the next token to figure out which kind of declaration is
7023      present.  */
7024   token1 = cp_lexer_peek_token (parser->lexer);
7025
7026   /* If the next keyword is `asm', we have an asm-definition.  */
7027   if (token1->keyword == RID_ASM)
7028     {
7029       if (statement_p)
7030         cp_parser_commit_to_tentative_parse (parser);
7031       cp_parser_asm_definition (parser);
7032     }
7033   /* If the next keyword is `namespace', we have a
7034      namespace-alias-definition.  */
7035   else if (token1->keyword == RID_NAMESPACE)
7036     cp_parser_namespace_alias_definition (parser);
7037   /* If the next keyword is `using', we have either a
7038      using-declaration or a using-directive.  */
7039   else if (token1->keyword == RID_USING)
7040     {
7041       cp_token *token2;
7042
7043       if (statement_p)
7044         cp_parser_commit_to_tentative_parse (parser);
7045       /* If the token after `using' is `namespace', then we have a
7046          using-directive.  */
7047       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7048       if (token2->keyword == RID_NAMESPACE)
7049         cp_parser_using_directive (parser);
7050       /* Otherwise, it's a using-declaration.  */
7051       else
7052         cp_parser_using_declaration (parser);
7053     }
7054   /* If the next keyword is `__label__' we have a label declaration.  */
7055   else if (token1->keyword == RID_LABEL)
7056     {
7057       if (statement_p)
7058         cp_parser_commit_to_tentative_parse (parser);
7059       cp_parser_label_declaration (parser);
7060     }
7061   /* Anything else must be a simple-declaration.  */
7062   else
7063     cp_parser_simple_declaration (parser, !statement_p);
7064 }
7065
7066 /* Parse a simple-declaration.
7067
7068    simple-declaration:
7069      decl-specifier-seq [opt] init-declarator-list [opt] ;
7070
7071    init-declarator-list:
7072      init-declarator
7073      init-declarator-list , init-declarator
7074
7075    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7076    function-definition as a simple-declaration.  */
7077
7078 static void
7079 cp_parser_simple_declaration (cp_parser* parser,
7080                               bool function_definition_allowed_p)
7081 {
7082   cp_decl_specifier_seq decl_specifiers;
7083   int declares_class_or_enum;
7084   bool saw_declarator;
7085
7086   /* Defer access checks until we know what is being declared; the
7087      checks for names appearing in the decl-specifier-seq should be
7088      done as if we were in the scope of the thing being declared.  */
7089   push_deferring_access_checks (dk_deferred);
7090
7091   /* Parse the decl-specifier-seq.  We have to keep track of whether
7092      or not the decl-specifier-seq declares a named class or
7093      enumeration type, since that is the only case in which the
7094      init-declarator-list is allowed to be empty.
7095
7096      [dcl.dcl]
7097
7098      In a simple-declaration, the optional init-declarator-list can be
7099      omitted only when declaring a class or enumeration, that is when
7100      the decl-specifier-seq contains either a class-specifier, an
7101      elaborated-type-specifier, or an enum-specifier.  */
7102   cp_parser_decl_specifier_seq (parser,
7103                                 CP_PARSER_FLAGS_OPTIONAL,
7104                                 &decl_specifiers,
7105                                 &declares_class_or_enum);
7106   /* We no longer need to defer access checks.  */
7107   stop_deferring_access_checks ();
7108
7109   /* In a block scope, a valid declaration must always have a
7110      decl-specifier-seq.  By not trying to parse declarators, we can
7111      resolve the declaration/expression ambiguity more quickly.  */
7112   if (!function_definition_allowed_p
7113       && !decl_specifiers.any_specifiers_p)
7114     {
7115       cp_parser_error (parser, "expected declaration");
7116       goto done;
7117     }
7118
7119   /* If the next two tokens are both identifiers, the code is
7120      erroneous. The usual cause of this situation is code like:
7121
7122        T t;
7123
7124      where "T" should name a type -- but does not.  */
7125   if (!decl_specifiers.type
7126       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7127     {
7128       /* If parsing tentatively, we should commit; we really are
7129          looking at a declaration.  */
7130       cp_parser_commit_to_tentative_parse (parser);
7131       /* Give up.  */
7132       goto done;
7133     }
7134
7135   /* If we have seen at least one decl-specifier, and the next token
7136      is not a parenthesis, then we must be looking at a declaration.
7137      (After "int (" we might be looking at a functional cast.)  */
7138   if (decl_specifiers.any_specifiers_p
7139       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7140     cp_parser_commit_to_tentative_parse (parser);
7141
7142   /* Keep going until we hit the `;' at the end of the simple
7143      declaration.  */
7144   saw_declarator = false;
7145   while (cp_lexer_next_token_is_not (parser->lexer,
7146                                      CPP_SEMICOLON))
7147     {
7148       cp_token *token;
7149       bool function_definition_p;
7150       tree decl;
7151
7152       saw_declarator = true;
7153       /* Parse the init-declarator.  */
7154       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7155                                         function_definition_allowed_p,
7156                                         /*member_p=*/false,
7157                                         declares_class_or_enum,
7158                                         &function_definition_p);
7159       /* If an error occurred while parsing tentatively, exit quickly.
7160          (That usually happens when in the body of a function; each
7161          statement is treated as a declaration-statement until proven
7162          otherwise.)  */
7163       if (cp_parser_error_occurred (parser))
7164         goto done;
7165       /* Handle function definitions specially.  */
7166       if (function_definition_p)
7167         {
7168           /* If the next token is a `,', then we are probably
7169              processing something like:
7170
7171                void f() {}, *p;
7172
7173              which is erroneous.  */
7174           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7175             error ("mixing declarations and function-definitions is forbidden");
7176           /* Otherwise, we're done with the list of declarators.  */
7177           else
7178             {
7179               pop_deferring_access_checks ();
7180               return;
7181             }
7182         }
7183       /* The next token should be either a `,' or a `;'.  */
7184       token = cp_lexer_peek_token (parser->lexer);
7185       /* If it's a `,', there are more declarators to come.  */
7186       if (token->type == CPP_COMMA)
7187         cp_lexer_consume_token (parser->lexer);
7188       /* If it's a `;', we are done.  */
7189       else if (token->type == CPP_SEMICOLON)
7190         break;
7191       /* Anything else is an error.  */
7192       else
7193         {
7194           /* If we have already issued an error message we don't need
7195              to issue another one.  */
7196           if (decl != error_mark_node
7197               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7198             cp_parser_error (parser, "expected %<,%> or %<;%>");
7199           /* Skip tokens until we reach the end of the statement.  */
7200           cp_parser_skip_to_end_of_statement (parser);
7201           /* If the next token is now a `;', consume it.  */
7202           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7203             cp_lexer_consume_token (parser->lexer);
7204           goto done;
7205         }
7206       /* After the first time around, a function-definition is not
7207          allowed -- even if it was OK at first.  For example:
7208
7209            int i, f() {}
7210
7211          is not valid.  */
7212       function_definition_allowed_p = false;
7213     }
7214
7215   /* Issue an error message if no declarators are present, and the
7216      decl-specifier-seq does not itself declare a class or
7217      enumeration.  */
7218   if (!saw_declarator)
7219     {
7220       if (cp_parser_declares_only_class_p (parser))
7221         shadow_tag (&decl_specifiers);
7222       /* Perform any deferred access checks.  */
7223       perform_deferred_access_checks ();
7224     }
7225
7226   /* Consume the `;'.  */
7227   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7228
7229  done:
7230   pop_deferring_access_checks ();
7231 }
7232
7233 /* Parse a decl-specifier-seq.
7234
7235    decl-specifier-seq:
7236      decl-specifier-seq [opt] decl-specifier
7237
7238    decl-specifier:
7239      storage-class-specifier
7240      type-specifier
7241      function-specifier
7242      friend
7243      typedef
7244
7245    GNU Extension:
7246
7247    decl-specifier:
7248      attributes
7249
7250    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7251
7252    The parser flags FLAGS is used to control type-specifier parsing.
7253
7254    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7255    flags:
7256
7257      1: one of the decl-specifiers is an elaborated-type-specifier
7258         (i.e., a type declaration)
7259      2: one of the decl-specifiers is an enum-specifier or a
7260         class-specifier (i.e., a type definition)
7261
7262    */
7263
7264 static void
7265 cp_parser_decl_specifier_seq (cp_parser* parser,
7266                               cp_parser_flags flags,
7267                               cp_decl_specifier_seq *decl_specs,
7268                               int* declares_class_or_enum)
7269 {
7270   bool constructor_possible_p = !parser->in_declarator_p;
7271
7272   /* Clear DECL_SPECS.  */
7273   clear_decl_specs (decl_specs);
7274
7275   /* Assume no class or enumeration type is declared.  */
7276   *declares_class_or_enum = 0;
7277
7278   /* Keep reading specifiers until there are no more to read.  */
7279   while (true)
7280     {
7281       bool constructor_p;
7282       bool found_decl_spec;
7283       cp_token *token;
7284
7285       /* Peek at the next token.  */
7286       token = cp_lexer_peek_token (parser->lexer);
7287       /* Handle attributes.  */
7288       if (token->keyword == RID_ATTRIBUTE)
7289         {
7290           /* Parse the attributes.  */
7291           decl_specs->attributes
7292             = chainon (decl_specs->attributes,
7293                        cp_parser_attributes_opt (parser));
7294           continue;
7295         }
7296       /* Assume we will find a decl-specifier keyword.  */
7297       found_decl_spec = true;
7298       /* If the next token is an appropriate keyword, we can simply
7299          add it to the list.  */
7300       switch (token->keyword)
7301         {
7302           /* decl-specifier:
7303                friend  */
7304         case RID_FRIEND:
7305           if (decl_specs->specs[(int) ds_friend]++)
7306             error ("duplicate %<friend%>");
7307           /* Consume the token.  */
7308           cp_lexer_consume_token (parser->lexer);
7309           break;
7310
7311           /* function-specifier:
7312                inline
7313                virtual
7314                explicit  */
7315         case RID_INLINE:
7316         case RID_VIRTUAL:
7317         case RID_EXPLICIT:
7318           cp_parser_function_specifier_opt (parser, decl_specs);
7319           break;
7320
7321           /* decl-specifier:
7322                typedef  */
7323         case RID_TYPEDEF:
7324           ++decl_specs->specs[(int) ds_typedef];
7325           /* Consume the token.  */
7326           cp_lexer_consume_token (parser->lexer);
7327           /* A constructor declarator cannot appear in a typedef.  */
7328           constructor_possible_p = false;
7329           /* The "typedef" keyword can only occur in a declaration; we
7330              may as well commit at this point.  */
7331           cp_parser_commit_to_tentative_parse (parser);
7332           break;
7333
7334           /* storage-class-specifier:
7335                auto
7336                register
7337                static
7338                extern
7339                mutable
7340
7341              GNU Extension:
7342                thread  */
7343         case RID_AUTO:
7344           /* Consume the token.  */
7345           cp_lexer_consume_token (parser->lexer);
7346           cp_parser_set_storage_class (decl_specs, sc_auto);
7347           break;
7348         case RID_REGISTER:
7349           /* Consume the token.  */
7350           cp_lexer_consume_token (parser->lexer);
7351           cp_parser_set_storage_class (decl_specs, sc_register);
7352           break;
7353         case RID_STATIC:
7354           /* Consume the token.  */
7355           cp_lexer_consume_token (parser->lexer);
7356           if (decl_specs->specs[(int) ds_thread])
7357             {
7358               error ("%<__thread%> before %<static%>");
7359               decl_specs->specs[(int) ds_thread] = 0;
7360             }
7361           cp_parser_set_storage_class (decl_specs, sc_static);
7362           break;
7363         case RID_EXTERN:
7364           /* Consume the token.  */
7365           cp_lexer_consume_token (parser->lexer);
7366           if (decl_specs->specs[(int) ds_thread])
7367             {
7368               error ("%<__thread%> before %<extern%>");
7369               decl_specs->specs[(int) ds_thread] = 0;
7370             }
7371           cp_parser_set_storage_class (decl_specs, sc_extern);
7372           break;
7373         case RID_MUTABLE:
7374           /* Consume the token.  */
7375           cp_lexer_consume_token (parser->lexer);
7376           cp_parser_set_storage_class (decl_specs, sc_mutable);
7377           break;
7378         case RID_THREAD:
7379           /* Consume the token.  */
7380           cp_lexer_consume_token (parser->lexer);
7381           ++decl_specs->specs[(int) ds_thread];
7382           break;
7383
7384         default:
7385           /* We did not yet find a decl-specifier yet.  */
7386           found_decl_spec = false;
7387           break;
7388         }
7389
7390       /* Constructors are a special case.  The `S' in `S()' is not a
7391          decl-specifier; it is the beginning of the declarator.  */
7392       constructor_p
7393         = (!found_decl_spec
7394            && constructor_possible_p
7395            && (cp_parser_constructor_declarator_p
7396                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7397
7398       /* If we don't have a DECL_SPEC yet, then we must be looking at
7399          a type-specifier.  */
7400       if (!found_decl_spec && !constructor_p)
7401         {
7402           int decl_spec_declares_class_or_enum;
7403           bool is_cv_qualifier;
7404           tree type_spec;
7405
7406           type_spec
7407             = cp_parser_type_specifier (parser, flags,
7408                                         decl_specs,
7409                                         /*is_declaration=*/true,
7410                                         &decl_spec_declares_class_or_enum,
7411                                         &is_cv_qualifier);
7412
7413           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7414
7415           /* If this type-specifier referenced a user-defined type
7416              (a typedef, class-name, etc.), then we can't allow any
7417              more such type-specifiers henceforth.
7418
7419              [dcl.spec]
7420
7421              The longest sequence of decl-specifiers that could
7422              possibly be a type name is taken as the
7423              decl-specifier-seq of a declaration.  The sequence shall
7424              be self-consistent as described below.
7425
7426              [dcl.type]
7427
7428              As a general rule, at most one type-specifier is allowed
7429              in the complete decl-specifier-seq of a declaration.  The
7430              only exceptions are the following:
7431
7432              -- const or volatile can be combined with any other
7433                 type-specifier.
7434
7435              -- signed or unsigned can be combined with char, long,
7436                 short, or int.
7437
7438              -- ..
7439
7440              Example:
7441
7442                typedef char* Pc;
7443                void g (const int Pc);
7444
7445              Here, Pc is *not* part of the decl-specifier seq; it's
7446              the declarator.  Therefore, once we see a type-specifier
7447              (other than a cv-qualifier), we forbid any additional
7448              user-defined types.  We *do* still allow things like `int
7449              int' to be considered a decl-specifier-seq, and issue the
7450              error message later.  */
7451           if (type_spec && !is_cv_qualifier)
7452             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7453           /* A constructor declarator cannot follow a type-specifier.  */
7454           if (type_spec)
7455             {
7456               constructor_possible_p = false;
7457               found_decl_spec = true;
7458             }
7459         }
7460
7461       /* If we still do not have a DECL_SPEC, then there are no more
7462          decl-specifiers.  */
7463       if (!found_decl_spec)
7464         break;
7465
7466       decl_specs->any_specifiers_p = true;
7467       /* After we see one decl-specifier, further decl-specifiers are
7468          always optional.  */
7469       flags |= CP_PARSER_FLAGS_OPTIONAL;
7470     }
7471
7472   /* Don't allow a friend specifier with a class definition.  */
7473   if (decl_specs->specs[(int) ds_friend] != 0
7474       && (*declares_class_or_enum & 2))
7475     error ("class definition may not be declared a friend");
7476 }
7477
7478 /* Parse an (optional) storage-class-specifier.
7479
7480    storage-class-specifier:
7481      auto
7482      register
7483      static
7484      extern
7485      mutable
7486
7487    GNU Extension:
7488
7489    storage-class-specifier:
7490      thread
7491
7492    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7493
7494 static tree
7495 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7496 {
7497   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7498     {
7499     case RID_AUTO:
7500     case RID_REGISTER:
7501     case RID_STATIC:
7502     case RID_EXTERN:
7503     case RID_MUTABLE:
7504     case RID_THREAD:
7505       /* Consume the token.  */
7506       return cp_lexer_consume_token (parser->lexer)->value;
7507
7508     default:
7509       return NULL_TREE;
7510     }
7511 }
7512
7513 /* Parse an (optional) function-specifier.
7514
7515    function-specifier:
7516      inline
7517      virtual
7518      explicit
7519
7520    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7521    Updates DECL_SPECS, if it is non-NULL.  */
7522
7523 static tree
7524 cp_parser_function_specifier_opt (cp_parser* parser,
7525                                   cp_decl_specifier_seq *decl_specs)
7526 {
7527   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7528     {
7529     case RID_INLINE:
7530       if (decl_specs)
7531         ++decl_specs->specs[(int) ds_inline];
7532       break;
7533
7534     case RID_VIRTUAL:
7535       if (decl_specs)
7536         ++decl_specs->specs[(int) ds_virtual];
7537       break;
7538
7539     case RID_EXPLICIT:
7540       if (decl_specs)
7541         ++decl_specs->specs[(int) ds_explicit];
7542       break;
7543
7544     default:
7545       return NULL_TREE;
7546     }
7547
7548   /* Consume the token.  */
7549   return cp_lexer_consume_token (parser->lexer)->value;
7550 }
7551
7552 /* Parse a linkage-specification.
7553
7554    linkage-specification:
7555      extern string-literal { declaration-seq [opt] }
7556      extern string-literal declaration  */
7557
7558 static void
7559 cp_parser_linkage_specification (cp_parser* parser)
7560 {
7561   tree linkage;
7562
7563   /* Look for the `extern' keyword.  */
7564   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7565
7566   /* Look for the string-literal.  */
7567   linkage = cp_parser_string_literal (parser, false, false);
7568
7569   /* Transform the literal into an identifier.  If the literal is a
7570      wide-character string, or contains embedded NULs, then we can't
7571      handle it as the user wants.  */
7572   if (strlen (TREE_STRING_POINTER (linkage))
7573       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7574     {
7575       cp_parser_error (parser, "invalid linkage-specification");
7576       /* Assume C++ linkage.  */
7577       linkage = lang_name_cplusplus;
7578     }
7579   else
7580     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7581
7582   /* We're now using the new linkage.  */
7583   push_lang_context (linkage);
7584
7585   /* If the next token is a `{', then we're using the first
7586      production.  */
7587   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7588     {
7589       /* Consume the `{' token.  */
7590       cp_lexer_consume_token (parser->lexer);
7591       /* Parse the declarations.  */
7592       cp_parser_declaration_seq_opt (parser);
7593       /* Look for the closing `}'.  */
7594       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7595     }
7596   /* Otherwise, there's just one declaration.  */
7597   else
7598     {
7599       bool saved_in_unbraced_linkage_specification_p;
7600
7601       saved_in_unbraced_linkage_specification_p
7602         = parser->in_unbraced_linkage_specification_p;
7603       parser->in_unbraced_linkage_specification_p = true;
7604       have_extern_spec = true;
7605       cp_parser_declaration (parser);
7606       have_extern_spec = false;
7607       parser->in_unbraced_linkage_specification_p
7608         = saved_in_unbraced_linkage_specification_p;
7609     }
7610
7611   /* We're done with the linkage-specification.  */
7612   pop_lang_context ();
7613 }
7614
7615 /* Special member functions [gram.special] */
7616
7617 /* Parse a conversion-function-id.
7618
7619    conversion-function-id:
7620      operator conversion-type-id
7621
7622    Returns an IDENTIFIER_NODE representing the operator.  */
7623
7624 static tree
7625 cp_parser_conversion_function_id (cp_parser* parser)
7626 {
7627   tree type;
7628   tree saved_scope;
7629   tree saved_qualifying_scope;
7630   tree saved_object_scope;
7631   tree pushed_scope = NULL_TREE;
7632
7633   /* Look for the `operator' token.  */
7634   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7635     return error_mark_node;
7636   /* When we parse the conversion-type-id, the current scope will be
7637      reset.  However, we need that information in able to look up the
7638      conversion function later, so we save it here.  */
7639   saved_scope = parser->scope;
7640   saved_qualifying_scope = parser->qualifying_scope;
7641   saved_object_scope = parser->object_scope;
7642   /* We must enter the scope of the class so that the names of
7643      entities declared within the class are available in the
7644      conversion-type-id.  For example, consider:
7645
7646        struct S {
7647          typedef int I;
7648          operator I();
7649        };
7650
7651        S::operator I() { ... }
7652
7653      In order to see that `I' is a type-name in the definition, we
7654      must be in the scope of `S'.  */
7655   if (saved_scope)
7656     pushed_scope = push_scope (saved_scope);
7657   /* Parse the conversion-type-id.  */
7658   type = cp_parser_conversion_type_id (parser);
7659   /* Leave the scope of the class, if any.  */
7660   if (pushed_scope)
7661     pop_scope (pushed_scope);
7662   /* Restore the saved scope.  */
7663   parser->scope = saved_scope;
7664   parser->qualifying_scope = saved_qualifying_scope;
7665   parser->object_scope = saved_object_scope;
7666   /* If the TYPE is invalid, indicate failure.  */
7667   if (type == error_mark_node)
7668     return error_mark_node;
7669   return mangle_conv_op_name_for_type (type);
7670 }
7671
7672 /* Parse a conversion-type-id:
7673
7674    conversion-type-id:
7675      type-specifier-seq conversion-declarator [opt]
7676
7677    Returns the TYPE specified.  */
7678
7679 static tree
7680 cp_parser_conversion_type_id (cp_parser* parser)
7681 {
7682   tree attributes;
7683   cp_decl_specifier_seq type_specifiers;
7684   cp_declarator *declarator;
7685   tree type_specified;
7686
7687   /* Parse the attributes.  */
7688   attributes = cp_parser_attributes_opt (parser);
7689   /* Parse the type-specifiers.  */
7690   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7691                                 &type_specifiers);
7692   /* If that didn't work, stop.  */
7693   if (type_specifiers.type == error_mark_node)
7694     return error_mark_node;
7695   /* Parse the conversion-declarator.  */
7696   declarator = cp_parser_conversion_declarator_opt (parser);
7697
7698   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7699                                     /*initialized=*/0, &attributes);
7700   if (attributes)
7701     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7702   return type_specified;
7703 }
7704
7705 /* Parse an (optional) conversion-declarator.
7706
7707    conversion-declarator:
7708      ptr-operator conversion-declarator [opt]
7709
7710    */
7711
7712 static cp_declarator *
7713 cp_parser_conversion_declarator_opt (cp_parser* parser)
7714 {
7715   enum tree_code code;
7716   tree class_type;
7717   cp_cv_quals cv_quals;
7718
7719   /* We don't know if there's a ptr-operator next, or not.  */
7720   cp_parser_parse_tentatively (parser);
7721   /* Try the ptr-operator.  */
7722   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7723   /* If it worked, look for more conversion-declarators.  */
7724   if (cp_parser_parse_definitely (parser))
7725     {
7726       cp_declarator *declarator;
7727
7728       /* Parse another optional declarator.  */
7729       declarator = cp_parser_conversion_declarator_opt (parser);
7730
7731       /* Create the representation of the declarator.  */
7732       if (class_type)
7733         declarator = make_ptrmem_declarator (cv_quals, class_type,
7734                                              declarator);
7735       else if (code == INDIRECT_REF)
7736         declarator = make_pointer_declarator (cv_quals, declarator);
7737       else
7738         declarator = make_reference_declarator (cv_quals, declarator);
7739
7740       return declarator;
7741    }
7742
7743   return NULL;
7744 }
7745
7746 /* Parse an (optional) ctor-initializer.
7747
7748    ctor-initializer:
7749      : mem-initializer-list
7750
7751    Returns TRUE iff the ctor-initializer was actually present.  */
7752
7753 static bool
7754 cp_parser_ctor_initializer_opt (cp_parser* parser)
7755 {
7756   /* If the next token is not a `:', then there is no
7757      ctor-initializer.  */
7758   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7759     {
7760       /* Do default initialization of any bases and members.  */
7761       if (DECL_CONSTRUCTOR_P (current_function_decl))
7762         finish_mem_initializers (NULL_TREE);
7763
7764       return false;
7765     }
7766
7767   /* Consume the `:' token.  */
7768   cp_lexer_consume_token (parser->lexer);
7769   /* And the mem-initializer-list.  */
7770   cp_parser_mem_initializer_list (parser);
7771
7772   return true;
7773 }
7774
7775 /* Parse a mem-initializer-list.
7776
7777    mem-initializer-list:
7778      mem-initializer
7779      mem-initializer , mem-initializer-list  */
7780
7781 static void
7782 cp_parser_mem_initializer_list (cp_parser* parser)
7783 {
7784   tree mem_initializer_list = NULL_TREE;
7785
7786   /* Let the semantic analysis code know that we are starting the
7787      mem-initializer-list.  */
7788   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7789     error ("only constructors take base initializers");
7790
7791   /* Loop through the list.  */
7792   while (true)
7793     {
7794       tree mem_initializer;
7795
7796       /* Parse the mem-initializer.  */
7797       mem_initializer = cp_parser_mem_initializer (parser);
7798       /* Add it to the list, unless it was erroneous.  */
7799       if (mem_initializer)
7800         {
7801           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7802           mem_initializer_list = mem_initializer;
7803         }
7804       /* If the next token is not a `,', we're done.  */
7805       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7806         break;
7807       /* Consume the `,' token.  */
7808       cp_lexer_consume_token (parser->lexer);
7809     }
7810
7811   /* Perform semantic analysis.  */
7812   if (DECL_CONSTRUCTOR_P (current_function_decl))
7813     finish_mem_initializers (mem_initializer_list);
7814 }
7815
7816 /* Parse a mem-initializer.
7817
7818    mem-initializer:
7819      mem-initializer-id ( expression-list [opt] )
7820
7821    GNU extension:
7822
7823    mem-initializer:
7824      ( expression-list [opt] )
7825
7826    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7827    class) or FIELD_DECL (for a non-static data member) to initialize;
7828    the TREE_VALUE is the expression-list.  */
7829
7830 static tree
7831 cp_parser_mem_initializer (cp_parser* parser)
7832 {
7833   tree mem_initializer_id;
7834   tree expression_list;
7835   tree member;
7836
7837   /* Find out what is being initialized.  */
7838   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7839     {
7840       pedwarn ("anachronistic old-style base class initializer");
7841       mem_initializer_id = NULL_TREE;
7842     }
7843   else
7844     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7845   member = expand_member_init (mem_initializer_id);
7846   if (member && !DECL_P (member))
7847     in_base_initializer = 1;
7848
7849   expression_list
7850     = cp_parser_parenthesized_expression_list (parser, false,
7851                                                /*cast_p=*/false,
7852                                                /*non_constant_p=*/NULL);
7853   if (!expression_list)
7854     expression_list = void_type_node;
7855
7856   in_base_initializer = 0;
7857
7858   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7859 }
7860
7861 /* Parse a mem-initializer-id.
7862
7863    mem-initializer-id:
7864      :: [opt] nested-name-specifier [opt] class-name
7865      identifier
7866
7867    Returns a TYPE indicating the class to be initializer for the first
7868    production.  Returns an IDENTIFIER_NODE indicating the data member
7869    to be initialized for the second production.  */
7870
7871 static tree
7872 cp_parser_mem_initializer_id (cp_parser* parser)
7873 {
7874   bool global_scope_p;
7875   bool nested_name_specifier_p;
7876   bool template_p = false;
7877   tree id;
7878
7879   /* `typename' is not allowed in this context ([temp.res]).  */
7880   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7881     {
7882       error ("keyword %<typename%> not allowed in this context (a qualified "
7883              "member initializer is implicitly a type)");
7884       cp_lexer_consume_token (parser->lexer);
7885     }
7886   /* Look for the optional `::' operator.  */
7887   global_scope_p
7888     = (cp_parser_global_scope_opt (parser,
7889                                    /*current_scope_valid_p=*/false)
7890        != NULL_TREE);
7891   /* Look for the optional nested-name-specifier.  The simplest way to
7892      implement:
7893
7894        [temp.res]
7895
7896        The keyword `typename' is not permitted in a base-specifier or
7897        mem-initializer; in these contexts a qualified name that
7898        depends on a template-parameter is implicitly assumed to be a
7899        type name.
7900
7901      is to assume that we have seen the `typename' keyword at this
7902      point.  */
7903   nested_name_specifier_p
7904     = (cp_parser_nested_name_specifier_opt (parser,
7905                                             /*typename_keyword_p=*/true,
7906                                             /*check_dependency_p=*/true,
7907                                             /*type_p=*/true,
7908                                             /*is_declaration=*/true)
7909        != NULL_TREE);
7910   if (nested_name_specifier_p)
7911     template_p = cp_parser_optional_template_keyword (parser);
7912   /* If there is a `::' operator or a nested-name-specifier, then we
7913      are definitely looking for a class-name.  */
7914   if (global_scope_p || nested_name_specifier_p)
7915     return cp_parser_class_name (parser,
7916                                  /*typename_keyword_p=*/true,
7917                                  /*template_keyword_p=*/template_p,
7918                                  none_type,
7919                                  /*check_dependency_p=*/true,
7920                                  /*class_head_p=*/false,
7921                                  /*is_declaration=*/true);
7922   /* Otherwise, we could also be looking for an ordinary identifier.  */
7923   cp_parser_parse_tentatively (parser);
7924   /* Try a class-name.  */
7925   id = cp_parser_class_name (parser,
7926                              /*typename_keyword_p=*/true,
7927                              /*template_keyword_p=*/false,
7928                              none_type,
7929                              /*check_dependency_p=*/true,
7930                              /*class_head_p=*/false,
7931                              /*is_declaration=*/true);
7932   /* If we found one, we're done.  */
7933   if (cp_parser_parse_definitely (parser))
7934     return id;
7935   /* Otherwise, look for an ordinary identifier.  */
7936   return cp_parser_identifier (parser);
7937 }
7938
7939 /* Overloading [gram.over] */
7940
7941 /* Parse an operator-function-id.
7942
7943    operator-function-id:
7944      operator operator
7945
7946    Returns an IDENTIFIER_NODE for the operator which is a
7947    human-readable spelling of the identifier, e.g., `operator +'.  */
7948
7949 static tree
7950 cp_parser_operator_function_id (cp_parser* parser)
7951 {
7952   /* Look for the `operator' keyword.  */
7953   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7954     return error_mark_node;
7955   /* And then the name of the operator itself.  */
7956   return cp_parser_operator (parser);
7957 }
7958
7959 /* Parse an operator.
7960
7961    operator:
7962      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7963      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7964      || ++ -- , ->* -> () []
7965
7966    GNU Extensions:
7967
7968    operator:
7969      <? >? <?= >?=
7970
7971    Returns an IDENTIFIER_NODE for the operator which is a
7972    human-readable spelling of the identifier, e.g., `operator +'.  */
7973
7974 static tree
7975 cp_parser_operator (cp_parser* parser)
7976 {
7977   tree id = NULL_TREE;
7978   cp_token *token;
7979
7980   /* Peek at the next token.  */
7981   token = cp_lexer_peek_token (parser->lexer);
7982   /* Figure out which operator we have.  */
7983   switch (token->type)
7984     {
7985     case CPP_KEYWORD:
7986       {
7987         enum tree_code op;
7988
7989         /* The keyword should be either `new' or `delete'.  */
7990         if (token->keyword == RID_NEW)
7991           op = NEW_EXPR;
7992         else if (token->keyword == RID_DELETE)
7993           op = DELETE_EXPR;
7994         else
7995           break;
7996
7997         /* Consume the `new' or `delete' token.  */
7998         cp_lexer_consume_token (parser->lexer);
7999
8000         /* Peek at the next token.  */
8001         token = cp_lexer_peek_token (parser->lexer);
8002         /* If it's a `[' token then this is the array variant of the
8003            operator.  */
8004         if (token->type == CPP_OPEN_SQUARE)
8005           {
8006             /* Consume the `[' token.  */
8007             cp_lexer_consume_token (parser->lexer);
8008             /* Look for the `]' token.  */
8009             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8010             id = ansi_opname (op == NEW_EXPR
8011                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8012           }
8013         /* Otherwise, we have the non-array variant.  */
8014         else
8015           id = ansi_opname (op);
8016
8017         return id;
8018       }
8019
8020     case CPP_PLUS:
8021       id = ansi_opname (PLUS_EXPR);
8022       break;
8023
8024     case CPP_MINUS:
8025       id = ansi_opname (MINUS_EXPR);
8026       break;
8027
8028     case CPP_MULT:
8029       id = ansi_opname (MULT_EXPR);
8030       break;
8031
8032     case CPP_DIV:
8033       id = ansi_opname (TRUNC_DIV_EXPR);
8034       break;
8035
8036     case CPP_MOD:
8037       id = ansi_opname (TRUNC_MOD_EXPR);
8038       break;
8039
8040     case CPP_XOR:
8041       id = ansi_opname (BIT_XOR_EXPR);
8042       break;
8043
8044     case CPP_AND:
8045       id = ansi_opname (BIT_AND_EXPR);
8046       break;
8047
8048     case CPP_OR:
8049       id = ansi_opname (BIT_IOR_EXPR);
8050       break;
8051
8052     case CPP_COMPL:
8053       id = ansi_opname (BIT_NOT_EXPR);
8054       break;
8055
8056     case CPP_NOT:
8057       id = ansi_opname (TRUTH_NOT_EXPR);
8058       break;
8059
8060     case CPP_EQ:
8061       id = ansi_assopname (NOP_EXPR);
8062       break;
8063
8064     case CPP_LESS:
8065       id = ansi_opname (LT_EXPR);
8066       break;
8067
8068     case CPP_GREATER:
8069       id = ansi_opname (GT_EXPR);
8070       break;
8071
8072     case CPP_PLUS_EQ:
8073       id = ansi_assopname (PLUS_EXPR);
8074       break;
8075
8076     case CPP_MINUS_EQ:
8077       id = ansi_assopname (MINUS_EXPR);
8078       break;
8079
8080     case CPP_MULT_EQ:
8081       id = ansi_assopname (MULT_EXPR);
8082       break;
8083
8084     case CPP_DIV_EQ:
8085       id = ansi_assopname (TRUNC_DIV_EXPR);
8086       break;
8087
8088     case CPP_MOD_EQ:
8089       id = ansi_assopname (TRUNC_MOD_EXPR);
8090       break;
8091
8092     case CPP_XOR_EQ:
8093       id = ansi_assopname (BIT_XOR_EXPR);
8094       break;
8095
8096     case CPP_AND_EQ:
8097       id = ansi_assopname (BIT_AND_EXPR);
8098       break;
8099
8100     case CPP_OR_EQ:
8101       id = ansi_assopname (BIT_IOR_EXPR);
8102       break;
8103
8104     case CPP_LSHIFT:
8105       id = ansi_opname (LSHIFT_EXPR);
8106       break;
8107
8108     case CPP_RSHIFT:
8109       id = ansi_opname (RSHIFT_EXPR);
8110       break;
8111
8112     case CPP_LSHIFT_EQ:
8113       id = ansi_assopname (LSHIFT_EXPR);
8114       break;
8115
8116     case CPP_RSHIFT_EQ:
8117       id = ansi_assopname (RSHIFT_EXPR);
8118       break;
8119
8120     case CPP_EQ_EQ:
8121       id = ansi_opname (EQ_EXPR);
8122       break;
8123
8124     case CPP_NOT_EQ:
8125       id = ansi_opname (NE_EXPR);
8126       break;
8127
8128     case CPP_LESS_EQ:
8129       id = ansi_opname (LE_EXPR);
8130       break;
8131
8132     case CPP_GREATER_EQ:
8133       id = ansi_opname (GE_EXPR);
8134       break;
8135
8136     case CPP_AND_AND:
8137       id = ansi_opname (TRUTH_ANDIF_EXPR);
8138       break;
8139
8140     case CPP_OR_OR:
8141       id = ansi_opname (TRUTH_ORIF_EXPR);
8142       break;
8143
8144     case CPP_PLUS_PLUS:
8145       id = ansi_opname (POSTINCREMENT_EXPR);
8146       break;
8147
8148     case CPP_MINUS_MINUS:
8149       id = ansi_opname (PREDECREMENT_EXPR);
8150       break;
8151
8152     case CPP_COMMA:
8153       id = ansi_opname (COMPOUND_EXPR);
8154       break;
8155
8156     case CPP_DEREF_STAR:
8157       id = ansi_opname (MEMBER_REF);
8158       break;
8159
8160     case CPP_DEREF:
8161       id = ansi_opname (COMPONENT_REF);
8162       break;
8163
8164     case CPP_OPEN_PAREN:
8165       /* Consume the `('.  */
8166       cp_lexer_consume_token (parser->lexer);
8167       /* Look for the matching `)'.  */
8168       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8169       return ansi_opname (CALL_EXPR);
8170
8171     case CPP_OPEN_SQUARE:
8172       /* Consume the `['.  */
8173       cp_lexer_consume_token (parser->lexer);
8174       /* Look for the matching `]'.  */
8175       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8176       return ansi_opname (ARRAY_REF);
8177
8178       /* Extensions.  */
8179     case CPP_MIN:
8180       id = ansi_opname (MIN_EXPR);
8181       cp_parser_warn_min_max ();
8182       break;
8183
8184     case CPP_MAX:
8185       id = ansi_opname (MAX_EXPR);
8186       cp_parser_warn_min_max ();
8187       break;
8188
8189     case CPP_MIN_EQ:
8190       id = ansi_assopname (MIN_EXPR);
8191       cp_parser_warn_min_max ();
8192       break;
8193
8194     case CPP_MAX_EQ:
8195       id = ansi_assopname (MAX_EXPR);
8196       cp_parser_warn_min_max ();
8197       break;
8198
8199     default:
8200       /* Anything else is an error.  */
8201       break;
8202     }
8203
8204   /* If we have selected an identifier, we need to consume the
8205      operator token.  */
8206   if (id)
8207     cp_lexer_consume_token (parser->lexer);
8208   /* Otherwise, no valid operator name was present.  */
8209   else
8210     {
8211       cp_parser_error (parser, "expected operator");
8212       id = error_mark_node;
8213     }
8214
8215   return id;
8216 }
8217
8218 /* Parse a template-declaration.
8219
8220    template-declaration:
8221      export [opt] template < template-parameter-list > declaration
8222
8223    If MEMBER_P is TRUE, this template-declaration occurs within a
8224    class-specifier.
8225
8226    The grammar rule given by the standard isn't correct.  What
8227    is really meant is:
8228
8229    template-declaration:
8230      export [opt] template-parameter-list-seq
8231        decl-specifier-seq [opt] init-declarator [opt] ;
8232      export [opt] template-parameter-list-seq
8233        function-definition
8234
8235    template-parameter-list-seq:
8236      template-parameter-list-seq [opt]
8237      template < template-parameter-list >  */
8238
8239 static void
8240 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8241 {
8242   /* Check for `export'.  */
8243   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8244     {
8245       /* Consume the `export' token.  */
8246       cp_lexer_consume_token (parser->lexer);
8247       /* Warn that we do not support `export'.  */
8248       warning (0, "keyword %<export%> not implemented, and will be ignored");
8249     }
8250
8251   cp_parser_template_declaration_after_export (parser, member_p);
8252 }
8253
8254 /* Parse a template-parameter-list.
8255
8256    template-parameter-list:
8257      template-parameter
8258      template-parameter-list , template-parameter
8259
8260    Returns a TREE_LIST.  Each node represents a template parameter.
8261    The nodes are connected via their TREE_CHAINs.  */
8262
8263 static tree
8264 cp_parser_template_parameter_list (cp_parser* parser)
8265 {
8266   tree parameter_list = NULL_TREE;
8267
8268   while (true)
8269     {
8270       tree parameter;
8271       cp_token *token;
8272       bool is_non_type;
8273
8274       /* Parse the template-parameter.  */
8275       parameter = cp_parser_template_parameter (parser, &is_non_type);
8276       /* Add it to the list.  */
8277       if (parameter != error_mark_node)
8278         parameter_list = process_template_parm (parameter_list,
8279                                                 parameter,
8280                                                 is_non_type);
8281       /* Peek at the next token.  */
8282       token = cp_lexer_peek_token (parser->lexer);
8283       /* If it's not a `,', we're done.  */
8284       if (token->type != CPP_COMMA)
8285         break;
8286       /* Otherwise, consume the `,' token.  */
8287       cp_lexer_consume_token (parser->lexer);
8288     }
8289
8290   return parameter_list;
8291 }
8292
8293 /* Parse a template-parameter.
8294
8295    template-parameter:
8296      type-parameter
8297      parameter-declaration
8298
8299    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8300    the parameter.  The TREE_PURPOSE is the default value, if any.
8301    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8302    iff this parameter is a non-type parameter.  */
8303
8304 static tree
8305 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8306 {
8307   cp_token *token;
8308   cp_parameter_declarator *parameter_declarator;
8309   tree parm;
8310
8311   /* Assume it is a type parameter or a template parameter.  */
8312   *is_non_type = false;
8313   /* Peek at the next token.  */
8314   token = cp_lexer_peek_token (parser->lexer);
8315   /* If it is `class' or `template', we have a type-parameter.  */
8316   if (token->keyword == RID_TEMPLATE)
8317     return cp_parser_type_parameter (parser);
8318   /* If it is `class' or `typename' we do not know yet whether it is a
8319      type parameter or a non-type parameter.  Consider:
8320
8321        template <typename T, typename T::X X> ...
8322
8323      or:
8324
8325        template <class C, class D*> ...
8326
8327      Here, the first parameter is a type parameter, and the second is
8328      a non-type parameter.  We can tell by looking at the token after
8329      the identifier -- if it is a `,', `=', or `>' then we have a type
8330      parameter.  */
8331   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8332     {
8333       /* Peek at the token after `class' or `typename'.  */
8334       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8335       /* If it's an identifier, skip it.  */
8336       if (token->type == CPP_NAME)
8337         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8338       /* Now, see if the token looks like the end of a template
8339          parameter.  */
8340       if (token->type == CPP_COMMA
8341           || token->type == CPP_EQ
8342           || token->type == CPP_GREATER)
8343         return cp_parser_type_parameter (parser);
8344     }
8345
8346   /* Otherwise, it is a non-type parameter.
8347
8348      [temp.param]
8349
8350      When parsing a default template-argument for a non-type
8351      template-parameter, the first non-nested `>' is taken as the end
8352      of the template parameter-list rather than a greater-than
8353      operator.  */
8354   *is_non_type = true;
8355   parameter_declarator
8356      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8357                                         /*parenthesized_p=*/NULL);
8358   parm = grokdeclarator (parameter_declarator->declarator,
8359                          &parameter_declarator->decl_specifiers,
8360                          PARM, /*initialized=*/0,
8361                          /*attrlist=*/NULL);
8362   if (parm == error_mark_node)
8363     return error_mark_node;
8364   return build_tree_list (parameter_declarator->default_argument, parm);
8365 }
8366
8367 /* Parse a type-parameter.
8368
8369    type-parameter:
8370      class identifier [opt]
8371      class identifier [opt] = type-id
8372      typename identifier [opt]
8373      typename identifier [opt] = type-id
8374      template < template-parameter-list > class identifier [opt]
8375      template < template-parameter-list > class identifier [opt]
8376        = id-expression
8377
8378    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8379    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8380    the declaration of the parameter.  */
8381
8382 static tree
8383 cp_parser_type_parameter (cp_parser* parser)
8384 {
8385   cp_token *token;
8386   tree parameter;
8387
8388   /* Look for a keyword to tell us what kind of parameter this is.  */
8389   token = cp_parser_require (parser, CPP_KEYWORD,
8390                              "`class', `typename', or `template'");
8391   if (!token)
8392     return error_mark_node;
8393
8394   switch (token->keyword)
8395     {
8396     case RID_CLASS:
8397     case RID_TYPENAME:
8398       {
8399         tree identifier;
8400         tree default_argument;
8401
8402         /* If the next token is an identifier, then it names the
8403            parameter.  */
8404         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8405           identifier = cp_parser_identifier (parser);
8406         else
8407           identifier = NULL_TREE;
8408
8409         /* Create the parameter.  */
8410         parameter = finish_template_type_parm (class_type_node, identifier);
8411
8412         /* If the next token is an `=', we have a default argument.  */
8413         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8414           {
8415             /* Consume the `=' token.  */
8416             cp_lexer_consume_token (parser->lexer);
8417             /* Parse the default-argument.  */
8418             default_argument = cp_parser_type_id (parser);
8419           }
8420         else
8421           default_argument = NULL_TREE;
8422
8423         /* Create the combined representation of the parameter and the
8424            default argument.  */
8425         parameter = build_tree_list (default_argument, parameter);
8426       }
8427       break;
8428
8429     case RID_TEMPLATE:
8430       {
8431         tree parameter_list;
8432         tree identifier;
8433         tree default_argument;
8434
8435         /* Look for the `<'.  */
8436         cp_parser_require (parser, CPP_LESS, "`<'");
8437         /* Parse the template-parameter-list.  */
8438         begin_template_parm_list ();
8439         parameter_list
8440           = cp_parser_template_parameter_list (parser);
8441         parameter_list = end_template_parm_list (parameter_list);
8442         /* Look for the `>'.  */
8443         cp_parser_require (parser, CPP_GREATER, "`>'");
8444         /* Look for the `class' keyword.  */
8445         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8446         /* If the next token is an `=', then there is a
8447            default-argument.  If the next token is a `>', we are at
8448            the end of the parameter-list.  If the next token is a `,',
8449            then we are at the end of this parameter.  */
8450         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8451             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8452             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8453           {
8454             identifier = cp_parser_identifier (parser);
8455             /* Treat invalid names as if the parameter were nameless.  */
8456             if (identifier == error_mark_node)
8457               identifier = NULL_TREE;
8458           }
8459         else
8460           identifier = NULL_TREE;
8461
8462         /* Create the template parameter.  */
8463         parameter = finish_template_template_parm (class_type_node,
8464                                                    identifier);
8465
8466         /* If the next token is an `=', then there is a
8467            default-argument.  */
8468         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8469           {
8470             bool is_template;
8471
8472             /* Consume the `='.  */
8473             cp_lexer_consume_token (parser->lexer);
8474             /* Parse the id-expression.  */
8475             default_argument
8476               = cp_parser_id_expression (parser,
8477                                          /*template_keyword_p=*/false,
8478                                          /*check_dependency_p=*/true,
8479                                          /*template_p=*/&is_template,
8480                                          /*declarator_p=*/false);
8481             if (TREE_CODE (default_argument) == TYPE_DECL)
8482               /* If the id-expression was a template-id that refers to
8483                  a template-class, we already have the declaration here,
8484                  so no further lookup is needed.  */
8485                  ;
8486             else
8487               /* Look up the name.  */
8488               default_argument
8489                 = cp_parser_lookup_name (parser, default_argument,
8490                                          none_type,
8491                                          /*is_template=*/is_template,
8492                                          /*is_namespace=*/false,
8493                                          /*check_dependency=*/true,
8494                                          /*ambiguous_p=*/NULL);
8495             /* See if the default argument is valid.  */
8496             default_argument
8497               = check_template_template_default_arg (default_argument);
8498           }
8499         else
8500           default_argument = NULL_TREE;
8501
8502         /* Create the combined representation of the parameter and the
8503            default argument.  */
8504         parameter = build_tree_list (default_argument, parameter);
8505       }
8506       break;
8507
8508     default:
8509       gcc_unreachable ();
8510       break;
8511     }
8512
8513   return parameter;
8514 }
8515
8516 /* Parse a template-id.
8517
8518    template-id:
8519      template-name < template-argument-list [opt] >
8520
8521    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8522    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8523    returned.  Otherwise, if the template-name names a function, or set
8524    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8525    names a class, returns a TYPE_DECL for the specialization.
8526
8527    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8528    uninstantiated templates.  */
8529
8530 static tree
8531 cp_parser_template_id (cp_parser *parser,
8532                        bool template_keyword_p,
8533                        bool check_dependency_p,
8534                        bool is_declaration)
8535 {
8536   tree template;
8537   tree arguments;
8538   tree template_id;
8539   cp_token_position start_of_id = 0;
8540   tree access_check = NULL_TREE;
8541   cp_token *next_token, *next_token_2;
8542   bool is_identifier;
8543
8544   /* If the next token corresponds to a template-id, there is no need
8545      to reparse it.  */
8546   next_token = cp_lexer_peek_token (parser->lexer);
8547   if (next_token->type == CPP_TEMPLATE_ID)
8548     {
8549       tree value;
8550       tree check;
8551
8552       /* Get the stored value.  */
8553       value = cp_lexer_consume_token (parser->lexer)->value;
8554       /* Perform any access checks that were deferred.  */
8555       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8556         perform_or_defer_access_check (TREE_PURPOSE (check),
8557                                        TREE_VALUE (check));
8558       /* Return the stored value.  */
8559       return TREE_VALUE (value);
8560     }
8561
8562   /* Avoid performing name lookup if there is no possibility of
8563      finding a template-id.  */
8564   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8565       || (next_token->type == CPP_NAME
8566           && !cp_parser_nth_token_starts_template_argument_list_p
8567                (parser, 2)))
8568     {
8569       cp_parser_error (parser, "expected template-id");
8570       return error_mark_node;
8571     }
8572
8573   /* Remember where the template-id starts.  */
8574   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8575     start_of_id = cp_lexer_token_position (parser->lexer, false);
8576
8577   push_deferring_access_checks (dk_deferred);
8578
8579   /* Parse the template-name.  */
8580   is_identifier = false;
8581   template = cp_parser_template_name (parser, template_keyword_p,
8582                                       check_dependency_p,
8583                                       is_declaration,
8584                                       &is_identifier);
8585   if (template == error_mark_node || is_identifier)
8586     {
8587       pop_deferring_access_checks ();
8588       return template;
8589     }
8590
8591   /* If we find the sequence `[:' after a template-name, it's probably
8592      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8593      parse correctly the argument list.  */
8594   next_token = cp_lexer_peek_token (parser->lexer);
8595   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8596   if (next_token->type == CPP_OPEN_SQUARE
8597       && next_token->flags & DIGRAPH
8598       && next_token_2->type == CPP_COLON
8599       && !(next_token_2->flags & PREV_WHITE))
8600     {
8601       cp_parser_parse_tentatively (parser);
8602       /* Change `:' into `::'.  */
8603       next_token_2->type = CPP_SCOPE;
8604       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8605          CPP_LESS.  */
8606       cp_lexer_consume_token (parser->lexer);
8607       /* Parse the arguments.  */
8608       arguments = cp_parser_enclosed_template_argument_list (parser);
8609       if (!cp_parser_parse_definitely (parser))
8610         {
8611           /* If we couldn't parse an argument list, then we revert our changes
8612              and return simply an error. Maybe this is not a template-id
8613              after all.  */
8614           next_token_2->type = CPP_COLON;
8615           cp_parser_error (parser, "expected %<<%>");
8616           pop_deferring_access_checks ();
8617           return error_mark_node;
8618         }
8619       /* Otherwise, emit an error about the invalid digraph, but continue
8620          parsing because we got our argument list.  */
8621       pedwarn ("%<<::%> cannot begin a template-argument list");
8622       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8623               "between %<<%> and %<::%>");
8624       if (!flag_permissive)
8625         {
8626           static bool hint;
8627           if (!hint)
8628             {
8629               inform ("(if you use -fpermissive G++ will accept your code)");
8630               hint = true;
8631             }
8632         }
8633     }
8634   else
8635     {
8636       /* Look for the `<' that starts the template-argument-list.  */
8637       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8638         {
8639           pop_deferring_access_checks ();
8640           return error_mark_node;
8641         }
8642       /* Parse the arguments.  */
8643       arguments = cp_parser_enclosed_template_argument_list (parser);
8644     }
8645
8646   /* Build a representation of the specialization.  */
8647   if (TREE_CODE (template) == IDENTIFIER_NODE)
8648     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8649   else if (DECL_CLASS_TEMPLATE_P (template)
8650            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8651     template_id
8652       = finish_template_type (template, arguments,
8653                               cp_lexer_next_token_is (parser->lexer,
8654                                                       CPP_SCOPE));
8655   else
8656     {
8657       /* If it's not a class-template or a template-template, it should be
8658          a function-template.  */
8659       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8660                    || TREE_CODE (template) == OVERLOAD
8661                    || BASELINK_P (template)));
8662
8663       template_id = lookup_template_function (template, arguments);
8664     }
8665
8666   /* Retrieve any deferred checks.  Do not pop this access checks yet
8667      so the memory will not be reclaimed during token replacing below.  */
8668   access_check = get_deferred_access_checks ();
8669
8670   /* If parsing tentatively, replace the sequence of tokens that makes
8671      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8672      should we re-parse the token stream, we will not have to repeat
8673      the effort required to do the parse, nor will we issue duplicate
8674      error messages about problems during instantiation of the
8675      template.  */
8676   if (start_of_id)
8677     {
8678       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8679
8680       /* Reset the contents of the START_OF_ID token.  */
8681       token->type = CPP_TEMPLATE_ID;
8682       token->value = build_tree_list (access_check, template_id);
8683       token->keyword = RID_MAX;
8684
8685       /* Purge all subsequent tokens.  */
8686       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8687
8688       /* ??? Can we actually assume that, if template_id ==
8689          error_mark_node, we will have issued a diagnostic to the
8690          user, as opposed to simply marking the tentative parse as
8691          failed?  */
8692       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8693         error ("parse error in template argument list");
8694     }
8695
8696   pop_deferring_access_checks ();
8697   return template_id;
8698 }
8699
8700 /* Parse a template-name.
8701
8702    template-name:
8703      identifier
8704
8705    The standard should actually say:
8706
8707    template-name:
8708      identifier
8709      operator-function-id
8710
8711    A defect report has been filed about this issue.
8712
8713    A conversion-function-id cannot be a template name because they cannot
8714    be part of a template-id. In fact, looking at this code:
8715
8716    a.operator K<int>()
8717
8718    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8719    It is impossible to call a templated conversion-function-id with an
8720    explicit argument list, since the only allowed template parameter is
8721    the type to which it is converting.
8722
8723    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8724    `template' keyword, in a construction like:
8725
8726      T::template f<3>()
8727
8728    In that case `f' is taken to be a template-name, even though there
8729    is no way of knowing for sure.
8730
8731    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8732    name refers to a set of overloaded functions, at least one of which
8733    is a template, or an IDENTIFIER_NODE with the name of the template,
8734    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8735    names are looked up inside uninstantiated templates.  */
8736
8737 static tree
8738 cp_parser_template_name (cp_parser* parser,
8739                          bool template_keyword_p,
8740                          bool check_dependency_p,
8741                          bool is_declaration,
8742                          bool *is_identifier)
8743 {
8744   tree identifier;
8745   tree decl;
8746   tree fns;
8747
8748   /* If the next token is `operator', then we have either an
8749      operator-function-id or a conversion-function-id.  */
8750   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8751     {
8752       /* We don't know whether we're looking at an
8753          operator-function-id or a conversion-function-id.  */
8754       cp_parser_parse_tentatively (parser);
8755       /* Try an operator-function-id.  */
8756       identifier = cp_parser_operator_function_id (parser);
8757       /* If that didn't work, try a conversion-function-id.  */
8758       if (!cp_parser_parse_definitely (parser))
8759         {
8760           cp_parser_error (parser, "expected template-name");
8761           return error_mark_node;
8762         }
8763     }
8764   /* Look for the identifier.  */
8765   else
8766     identifier = cp_parser_identifier (parser);
8767
8768   /* If we didn't find an identifier, we don't have a template-id.  */
8769   if (identifier == error_mark_node)
8770     return error_mark_node;
8771
8772   /* If the name immediately followed the `template' keyword, then it
8773      is a template-name.  However, if the next token is not `<', then
8774      we do not treat it as a template-name, since it is not being used
8775      as part of a template-id.  This enables us to handle constructs
8776      like:
8777
8778        template <typename T> struct S { S(); };
8779        template <typename T> S<T>::S();
8780
8781      correctly.  We would treat `S' as a template -- if it were `S<T>'
8782      -- but we do not if there is no `<'.  */
8783
8784   if (processing_template_decl
8785       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8786     {
8787       /* In a declaration, in a dependent context, we pretend that the
8788          "template" keyword was present in order to improve error
8789          recovery.  For example, given:
8790
8791            template <typename T> void f(T::X<int>);
8792
8793          we want to treat "X<int>" as a template-id.  */
8794       if (is_declaration
8795           && !template_keyword_p
8796           && parser->scope && TYPE_P (parser->scope)
8797           && check_dependency_p
8798           && dependent_type_p (parser->scope)
8799           /* Do not do this for dtors (or ctors), since they never
8800              need the template keyword before their name.  */
8801           && !constructor_name_p (identifier, parser->scope))
8802         {
8803           cp_token_position start = 0;
8804
8805           /* Explain what went wrong.  */
8806           error ("non-template %qD used as template", identifier);
8807           inform ("use %<%T::template %D%> to indicate that it is a template",
8808                   parser->scope, identifier);
8809           /* If parsing tentatively, find the location of the "<" token.  */
8810           if (cp_parser_simulate_error (parser))
8811             start = cp_lexer_token_position (parser->lexer, true);
8812           /* Parse the template arguments so that we can issue error
8813              messages about them.  */
8814           cp_lexer_consume_token (parser->lexer);
8815           cp_parser_enclosed_template_argument_list (parser);
8816           /* Skip tokens until we find a good place from which to
8817              continue parsing.  */
8818           cp_parser_skip_to_closing_parenthesis (parser,
8819                                                  /*recovering=*/true,
8820                                                  /*or_comma=*/true,
8821                                                  /*consume_paren=*/false);
8822           /* If parsing tentatively, permanently remove the
8823              template argument list.  That will prevent duplicate
8824              error messages from being issued about the missing
8825              "template" keyword.  */
8826           if (start)
8827             cp_lexer_purge_tokens_after (parser->lexer, start);
8828           if (is_identifier)
8829             *is_identifier = true;
8830           return identifier;
8831         }
8832
8833       /* If the "template" keyword is present, then there is generally
8834          no point in doing name-lookup, so we just return IDENTIFIER.
8835          But, if the qualifying scope is non-dependent then we can
8836          (and must) do name-lookup normally.  */
8837       if (template_keyword_p
8838           && (!parser->scope
8839               || (TYPE_P (parser->scope)
8840                   && dependent_type_p (parser->scope))))
8841         return identifier;
8842     }
8843
8844   /* Look up the name.  */
8845   decl = cp_parser_lookup_name (parser, identifier,
8846                                 none_type,
8847                                 /*is_template=*/false,
8848                                 /*is_namespace=*/false,
8849                                 check_dependency_p,
8850                                 /*ambiguous_p=*/NULL);
8851   decl = maybe_get_template_decl_from_type_decl (decl);
8852
8853   /* If DECL is a template, then the name was a template-name.  */
8854   if (TREE_CODE (decl) == TEMPLATE_DECL)
8855     ;
8856   else
8857     {
8858       tree fn = NULL_TREE;
8859
8860       /* The standard does not explicitly indicate whether a name that
8861          names a set of overloaded declarations, some of which are
8862          templates, is a template-name.  However, such a name should
8863          be a template-name; otherwise, there is no way to form a
8864          template-id for the overloaded templates.  */
8865       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8866       if (TREE_CODE (fns) == OVERLOAD)
8867         for (fn = fns; fn; fn = OVL_NEXT (fn))
8868           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8869             break;
8870
8871       if (!fn)
8872         {
8873           /* The name does not name a template.  */
8874           cp_parser_error (parser, "expected template-name");
8875           return error_mark_node;
8876         }
8877     }
8878
8879   /* If DECL is dependent, and refers to a function, then just return
8880      its name; we will look it up again during template instantiation.  */
8881   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8882     {
8883       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8884       if (TYPE_P (scope) && dependent_type_p (scope))
8885         return identifier;
8886     }
8887
8888   return decl;
8889 }
8890
8891 /* Parse a template-argument-list.
8892
8893    template-argument-list:
8894      template-argument
8895      template-argument-list , template-argument
8896
8897    Returns a TREE_VEC containing the arguments.  */
8898
8899 static tree
8900 cp_parser_template_argument_list (cp_parser* parser)
8901 {
8902   tree fixed_args[10];
8903   unsigned n_args = 0;
8904   unsigned alloced = 10;
8905   tree *arg_ary = fixed_args;
8906   tree vec;
8907   bool saved_in_template_argument_list_p;
8908   bool saved_ice_p;
8909   bool saved_non_ice_p;
8910
8911   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8912   parser->in_template_argument_list_p = true;
8913   /* Even if the template-id appears in an integral
8914      constant-expression, the contents of the argument list do 
8915      not.  */ 
8916   saved_ice_p = parser->integral_constant_expression_p;
8917   parser->integral_constant_expression_p = false;
8918   saved_non_ice_p = parser->non_integral_constant_expression_p;
8919   parser->non_integral_constant_expression_p = false;
8920   do
8921     {
8922       tree argument;
8923
8924       if (n_args)
8925         /* Consume the comma.  */
8926         cp_lexer_consume_token (parser->lexer);
8927
8928       /* Parse the template-argument.  */
8929       argument = cp_parser_template_argument (parser);
8930       if (n_args == alloced)
8931         {
8932           alloced *= 2;
8933
8934           if (arg_ary == fixed_args)
8935             {
8936               arg_ary = xmalloc (sizeof (tree) * alloced);
8937               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8938             }
8939           else
8940             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8941         }
8942       arg_ary[n_args++] = argument;
8943     }
8944   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8945
8946   vec = make_tree_vec (n_args);
8947
8948   while (n_args--)
8949     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8950
8951   if (arg_ary != fixed_args)
8952     free (arg_ary);
8953   parser->non_integral_constant_expression_p = saved_non_ice_p;
8954   parser->integral_constant_expression_p = saved_ice_p;
8955   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8956   return vec;
8957 }
8958
8959 /* Parse a template-argument.
8960
8961    template-argument:
8962      assignment-expression
8963      type-id
8964      id-expression
8965
8966    The representation is that of an assignment-expression, type-id, or
8967    id-expression -- except that the qualified id-expression is
8968    evaluated, so that the value returned is either a DECL or an
8969    OVERLOAD.
8970
8971    Although the standard says "assignment-expression", it forbids
8972    throw-expressions or assignments in the template argument.
8973    Therefore, we use "conditional-expression" instead.  */
8974
8975 static tree
8976 cp_parser_template_argument (cp_parser* parser)
8977 {
8978   tree argument;
8979   bool template_p;
8980   bool address_p;
8981   bool maybe_type_id = false;
8982   cp_token *token;
8983   cp_id_kind idk;
8984   tree qualifying_class;
8985
8986   /* There's really no way to know what we're looking at, so we just
8987      try each alternative in order.
8988
8989        [temp.arg]
8990
8991        In a template-argument, an ambiguity between a type-id and an
8992        expression is resolved to a type-id, regardless of the form of
8993        the corresponding template-parameter.
8994
8995      Therefore, we try a type-id first.  */
8996   cp_parser_parse_tentatively (parser);
8997   argument = cp_parser_type_id (parser);
8998   /* If there was no error parsing the type-id but the next token is a '>>',
8999      we probably found a typo for '> >'. But there are type-id which are
9000      also valid expressions. For instance:
9001
9002      struct X { int operator >> (int); };
9003      template <int V> struct Foo {};
9004      Foo<X () >> 5> r;
9005
9006      Here 'X()' is a valid type-id of a function type, but the user just
9007      wanted to write the expression "X() >> 5". Thus, we remember that we
9008      found a valid type-id, but we still try to parse the argument as an
9009      expression to see what happens.  */
9010   if (!cp_parser_error_occurred (parser)
9011       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9012     {
9013       maybe_type_id = true;
9014       cp_parser_abort_tentative_parse (parser);
9015     }
9016   else
9017     {
9018       /* If the next token isn't a `,' or a `>', then this argument wasn't
9019       really finished. This means that the argument is not a valid
9020       type-id.  */
9021       if (!cp_parser_next_token_ends_template_argument_p (parser))
9022         cp_parser_error (parser, "expected template-argument");
9023       /* If that worked, we're done.  */
9024       if (cp_parser_parse_definitely (parser))
9025         return argument;
9026     }
9027   /* We're still not sure what the argument will be.  */
9028   cp_parser_parse_tentatively (parser);
9029   /* Try a template.  */
9030   argument = cp_parser_id_expression (parser,
9031                                       /*template_keyword_p=*/false,
9032                                       /*check_dependency_p=*/true,
9033                                       &template_p,
9034                                       /*declarator_p=*/false);
9035   /* If the next token isn't a `,' or a `>', then this argument wasn't
9036      really finished.  */
9037   if (!cp_parser_next_token_ends_template_argument_p (parser))
9038     cp_parser_error (parser, "expected template-argument");
9039   if (!cp_parser_error_occurred (parser))
9040     {
9041       /* Figure out what is being referred to.  If the id-expression
9042          was for a class template specialization, then we will have a
9043          TYPE_DECL at this point.  There is no need to do name lookup
9044          at this point in that case.  */
9045       if (TREE_CODE (argument) != TYPE_DECL)
9046         argument = cp_parser_lookup_name (parser, argument,
9047                                           none_type,
9048                                           /*is_template=*/template_p,
9049                                           /*is_namespace=*/false,
9050                                           /*check_dependency=*/true,
9051                                           /*ambiguous_p=*/NULL);
9052       if (TREE_CODE (argument) != TEMPLATE_DECL
9053           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9054         cp_parser_error (parser, "expected template-name");
9055     }
9056   if (cp_parser_parse_definitely (parser))
9057     return argument;
9058   /* It must be a non-type argument.  There permitted cases are given
9059      in [temp.arg.nontype]:
9060
9061      -- an integral constant-expression of integral or enumeration
9062         type; or
9063
9064      -- the name of a non-type template-parameter; or
9065
9066      -- the name of an object or function with external linkage...
9067
9068      -- the address of an object or function with external linkage...
9069
9070      -- a pointer to member...  */
9071   /* Look for a non-type template parameter.  */
9072   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9073     {
9074       cp_parser_parse_tentatively (parser);
9075       argument = cp_parser_primary_expression (parser,
9076                                                /*cast_p=*/false,
9077                                                &idk,
9078                                                &qualifying_class);
9079       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9080           || !cp_parser_next_token_ends_template_argument_p (parser))
9081         cp_parser_simulate_error (parser);
9082       if (cp_parser_parse_definitely (parser))
9083         return argument;
9084     }
9085
9086   /* If the next token is "&", the argument must be the address of an
9087      object or function with external linkage.  */
9088   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9089   if (address_p)
9090     cp_lexer_consume_token (parser->lexer);
9091   /* See if we might have an id-expression.  */
9092   token = cp_lexer_peek_token (parser->lexer);
9093   if (token->type == CPP_NAME
9094       || token->keyword == RID_OPERATOR
9095       || token->type == CPP_SCOPE
9096       || token->type == CPP_TEMPLATE_ID
9097       || token->type == CPP_NESTED_NAME_SPECIFIER)
9098     {
9099       cp_parser_parse_tentatively (parser);
9100       argument = cp_parser_primary_expression (parser,
9101                                                /*cast_p=*/false,
9102                                                &idk,
9103                                                &qualifying_class);
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 ADDRESS_P, then we use finish_qualified_id_expr so
9116              that we get a pointer-to-member, if appropriate.
9117              However, if ADDRESS_P is false, we don't want to turn
9118              "T::f" into "(*this).T::f".  */
9119           if (qualifying_class && address_p)
9120             argument = finish_qualified_id_expr (qualifying_class,
9121                                                  argument,
9122                                                  /*done=*/true,
9123                                                  /*address_p=*/true);
9124           else if (TREE_CODE (argument) == BASELINK)
9125             /* We don't need the information about what class was used
9126                to name the overloaded functions.  */  
9127             argument = BASELINK_FUNCTIONS (argument);
9128
9129           if (TREE_CODE (argument) == VAR_DECL)
9130             {
9131               /* A variable without external linkage might still be a
9132                  valid constant-expression, so no error is issued here
9133                  if the external-linkage check fails.  */
9134               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9135                 cp_parser_simulate_error (parser);
9136             }
9137           else if (is_overloaded_fn (argument))
9138             /* All overloaded functions are allowed; if the external
9139                linkage test does not pass, an error will be issued
9140                later.  */
9141             ;
9142           else if (address_p
9143                    && (TREE_CODE (argument) == OFFSET_REF
9144                        || TREE_CODE (argument) == SCOPE_REF))
9145             /* A pointer-to-member.  */
9146             ;
9147           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9148             ;
9149           else
9150             cp_parser_simulate_error (parser);
9151
9152           if (cp_parser_parse_definitely (parser))
9153             {
9154               if (address_p)
9155                 argument = build_x_unary_op (ADDR_EXPR, argument);
9156               return argument;
9157             }
9158         }
9159     }
9160   /* If the argument started with "&", there are no other valid
9161      alternatives at this point.  */
9162   if (address_p)
9163     {
9164       cp_parser_error (parser, "invalid non-type template argument");
9165       return error_mark_node;
9166     }
9167
9168   /* If the argument wasn't successfully parsed as a type-id followed
9169      by '>>', the argument can only be a constant expression now.
9170      Otherwise, we try parsing the constant-expression tentatively,
9171      because the argument could really be a type-id.  */
9172   if (maybe_type_id)
9173     cp_parser_parse_tentatively (parser);
9174   argument = cp_parser_constant_expression (parser,
9175                                             /*allow_non_constant_p=*/false,
9176                                             /*non_constant_p=*/NULL);
9177   argument = fold_non_dependent_expr (argument);
9178   if (!maybe_type_id)
9179     return argument;
9180   if (!cp_parser_next_token_ends_template_argument_p (parser))
9181     cp_parser_error (parser, "expected template-argument");
9182   if (cp_parser_parse_definitely (parser))
9183     return argument;
9184   /* We did our best to parse the argument as a non type-id, but that
9185      was the only alternative that matched (albeit with a '>' after
9186      it). We can assume it's just a typo from the user, and a
9187      diagnostic will then be issued.  */
9188   return cp_parser_type_id (parser);
9189 }
9190
9191 /* Parse an explicit-instantiation.
9192
9193    explicit-instantiation:
9194      template declaration
9195
9196    Although the standard says `declaration', what it really means is:
9197
9198    explicit-instantiation:
9199      template decl-specifier-seq [opt] declarator [opt] ;
9200
9201    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9202    supposed to be allowed.  A defect report has been filed about this
9203    issue.
9204
9205    GNU Extension:
9206
9207    explicit-instantiation:
9208      storage-class-specifier template
9209        decl-specifier-seq [opt] declarator [opt] ;
9210      function-specifier template
9211        decl-specifier-seq [opt] declarator [opt] ;  */
9212
9213 static void
9214 cp_parser_explicit_instantiation (cp_parser* parser)
9215 {
9216   int declares_class_or_enum;
9217   cp_decl_specifier_seq decl_specifiers;
9218   tree extension_specifier = NULL_TREE;
9219
9220   /* Look for an (optional) storage-class-specifier or
9221      function-specifier.  */
9222   if (cp_parser_allow_gnu_extensions_p (parser))
9223     {
9224       extension_specifier
9225         = cp_parser_storage_class_specifier_opt (parser);
9226       if (!extension_specifier)
9227         extension_specifier
9228           = cp_parser_function_specifier_opt (parser,
9229                                               /*decl_specs=*/NULL);
9230     }
9231
9232   /* Look for the `template' keyword.  */
9233   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9234   /* Let the front end know that we are processing an explicit
9235      instantiation.  */
9236   begin_explicit_instantiation ();
9237   /* [temp.explicit] says that we are supposed to ignore access
9238      control while processing explicit instantiation directives.  */
9239   push_deferring_access_checks (dk_no_check);
9240   /* Parse a decl-specifier-seq.  */
9241   cp_parser_decl_specifier_seq (parser,
9242                                 CP_PARSER_FLAGS_OPTIONAL,
9243                                 &decl_specifiers,
9244                                 &declares_class_or_enum);
9245   /* If there was exactly one decl-specifier, and it declared a class,
9246      and there's no declarator, then we have an explicit type
9247      instantiation.  */
9248   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9249     {
9250       tree type;
9251
9252       type = check_tag_decl (&decl_specifiers);
9253       /* Turn access control back on for names used during
9254          template instantiation.  */
9255       pop_deferring_access_checks ();
9256       if (type)
9257         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9258     }
9259   else
9260     {
9261       cp_declarator *declarator;
9262       tree decl;
9263
9264       /* Parse the declarator.  */
9265       declarator
9266         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9267                                 /*ctor_dtor_or_conv_p=*/NULL,
9268                                 /*parenthesized_p=*/NULL,
9269                                 /*member_p=*/false);
9270       if (declares_class_or_enum & 2)
9271         cp_parser_check_for_definition_in_return_type (declarator,
9272                                                        decl_specifiers.type);
9273       if (declarator != cp_error_declarator)
9274         {
9275           decl = grokdeclarator (declarator, &decl_specifiers,
9276                                  NORMAL, 0, NULL);
9277           /* Turn access control back on for names used during
9278              template instantiation.  */
9279           pop_deferring_access_checks ();
9280           /* Do the explicit instantiation.  */
9281           do_decl_instantiation (decl, extension_specifier);
9282         }
9283       else
9284         {
9285           pop_deferring_access_checks ();
9286           /* Skip the body of the explicit instantiation.  */
9287           cp_parser_skip_to_end_of_statement (parser);
9288         }
9289     }
9290   /* We're done with the instantiation.  */
9291   end_explicit_instantiation ();
9292
9293   cp_parser_consume_semicolon_at_end_of_statement (parser);
9294 }
9295
9296 /* Parse an explicit-specialization.
9297
9298    explicit-specialization:
9299      template < > declaration
9300
9301    Although the standard says `declaration', what it really means is:
9302
9303    explicit-specialization:
9304      template <> decl-specifier [opt] init-declarator [opt] ;
9305      template <> function-definition
9306      template <> explicit-specialization
9307      template <> template-declaration  */
9308
9309 static void
9310 cp_parser_explicit_specialization (cp_parser* parser)
9311 {
9312   /* Look for the `template' keyword.  */
9313   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9314   /* Look for the `<'.  */
9315   cp_parser_require (parser, CPP_LESS, "`<'");
9316   /* Look for the `>'.  */
9317   cp_parser_require (parser, CPP_GREATER, "`>'");
9318   /* We have processed another parameter list.  */
9319   ++parser->num_template_parameter_lists;
9320   /* Let the front end know that we are beginning a specialization.  */
9321   begin_specialization ();
9322
9323   /* If the next keyword is `template', we need to figure out whether
9324      or not we're looking a template-declaration.  */
9325   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9326     {
9327       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9328           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9329         cp_parser_template_declaration_after_export (parser,
9330                                                      /*member_p=*/false);
9331       else
9332         cp_parser_explicit_specialization (parser);
9333     }
9334   else
9335     /* Parse the dependent declaration.  */
9336     cp_parser_single_declaration (parser,
9337                                   /*member_p=*/false,
9338                                   /*friend_p=*/NULL);
9339
9340   /* We're done with the specialization.  */
9341   end_specialization ();
9342   /* We're done with this parameter list.  */
9343   --parser->num_template_parameter_lists;
9344 }
9345
9346 /* Parse a type-specifier.
9347
9348    type-specifier:
9349      simple-type-specifier
9350      class-specifier
9351      enum-specifier
9352      elaborated-type-specifier
9353      cv-qualifier
9354
9355    GNU Extension:
9356
9357    type-specifier:
9358      __complex__
9359
9360    Returns a representation of the type-specifier.  For a
9361    class-specifier, enum-specifier, or elaborated-type-specifier, a
9362    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9363
9364    The parser flags FLAGS is used to control type-specifier parsing.
9365
9366    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9367    in a decl-specifier-seq.
9368
9369    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9370    class-specifier, enum-specifier, or elaborated-type-specifier, then
9371    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9372    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9373    zero.
9374
9375    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9376    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9377    is set to FALSE.  */
9378
9379 static tree
9380 cp_parser_type_specifier (cp_parser* parser,
9381                           cp_parser_flags flags,
9382                           cp_decl_specifier_seq *decl_specs,
9383                           bool is_declaration,
9384                           int* declares_class_or_enum,
9385                           bool* is_cv_qualifier)
9386 {
9387   tree type_spec = NULL_TREE;
9388   cp_token *token;
9389   enum rid keyword;
9390   cp_decl_spec ds = ds_last;
9391
9392   /* Assume this type-specifier does not declare a new type.  */
9393   if (declares_class_or_enum)
9394     *declares_class_or_enum = 0;
9395   /* And that it does not specify a cv-qualifier.  */
9396   if (is_cv_qualifier)
9397     *is_cv_qualifier = false;
9398   /* Peek at the next token.  */
9399   token = cp_lexer_peek_token (parser->lexer);
9400
9401   /* If we're looking at a keyword, we can use that to guide the
9402      production we choose.  */
9403   keyword = token->keyword;
9404   switch (keyword)
9405     {
9406     case RID_ENUM:
9407       /* 'enum' [identifier] '{' introduces an enum-specifier;
9408          'enum' <anything else> introduces an elaborated-type-specifier.  */
9409       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9410           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9411               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9412                  == CPP_OPEN_BRACE))
9413         {
9414           if (parser->num_template_parameter_lists)
9415             {
9416               error ("template declaration of %qs", "enum");
9417               cp_parser_skip_to_end_of_block_or_statement (parser);
9418               type_spec = error_mark_node;
9419             }
9420           else
9421             type_spec = cp_parser_enum_specifier (parser);
9422
9423           if (declares_class_or_enum)
9424             *declares_class_or_enum = 2;
9425           if (decl_specs)
9426             cp_parser_set_decl_spec_type (decl_specs,
9427                                           type_spec,
9428                                           /*user_defined_p=*/true);
9429           return type_spec;
9430         }
9431       else
9432         goto elaborated_type_specifier;
9433
9434       /* Any of these indicate either a class-specifier, or an
9435          elaborated-type-specifier.  */
9436     case RID_CLASS:
9437     case RID_STRUCT:
9438     case RID_UNION:
9439       /* Parse tentatively so that we can back up if we don't find a
9440          class-specifier.  */
9441       cp_parser_parse_tentatively (parser);
9442       /* Look for the class-specifier.  */
9443       type_spec = cp_parser_class_specifier (parser);
9444       /* If that worked, we're done.  */
9445       if (cp_parser_parse_definitely (parser))
9446         {
9447           if (declares_class_or_enum)
9448             *declares_class_or_enum = 2;
9449           if (decl_specs)
9450             cp_parser_set_decl_spec_type (decl_specs,
9451                                           type_spec,
9452                                           /*user_defined_p=*/true);
9453           return type_spec;
9454         }
9455
9456       /* Fall through.  */
9457     elaborated_type_specifier:
9458       /* We're declaring (not defining) a class or enum.  */
9459       if (declares_class_or_enum)
9460         *declares_class_or_enum = 1;
9461
9462       /* Fall through.  */
9463     case RID_TYPENAME:
9464       /* Look for an elaborated-type-specifier.  */
9465       type_spec
9466         = (cp_parser_elaborated_type_specifier
9467            (parser,
9468             decl_specs && decl_specs->specs[(int) ds_friend],
9469             is_declaration));
9470       if (decl_specs)
9471         cp_parser_set_decl_spec_type (decl_specs,
9472                                       type_spec,
9473                                       /*user_defined_p=*/true);
9474       return type_spec;
9475
9476     case RID_CONST:
9477       ds = ds_const;
9478       if (is_cv_qualifier)
9479         *is_cv_qualifier = true;
9480       break;
9481
9482     case RID_VOLATILE:
9483       ds = ds_volatile;
9484       if (is_cv_qualifier)
9485         *is_cv_qualifier = true;
9486       break;
9487
9488     case RID_RESTRICT:
9489       ds = ds_restrict;
9490       if (is_cv_qualifier)
9491         *is_cv_qualifier = true;
9492       break;
9493
9494     case RID_COMPLEX:
9495       /* The `__complex__' keyword is a GNU extension.  */
9496       ds = ds_complex;
9497       break;
9498
9499     default:
9500       break;
9501     }
9502
9503   /* Handle simple keywords.  */
9504   if (ds != ds_last)
9505     {
9506       if (decl_specs)
9507         {
9508           ++decl_specs->specs[(int)ds];
9509           decl_specs->any_specifiers_p = true;
9510         }
9511       return cp_lexer_consume_token (parser->lexer)->value;
9512     }
9513
9514   /* If we do not already have a type-specifier, assume we are looking
9515      at a simple-type-specifier.  */
9516   type_spec = cp_parser_simple_type_specifier (parser,
9517                                                decl_specs,
9518                                                flags);
9519
9520   /* If we didn't find a type-specifier, and a type-specifier was not
9521      optional in this context, issue an error message.  */
9522   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9523     {
9524       cp_parser_error (parser, "expected type specifier");
9525       return error_mark_node;
9526     }
9527
9528   return type_spec;
9529 }
9530
9531 /* Parse a simple-type-specifier.
9532
9533    simple-type-specifier:
9534      :: [opt] nested-name-specifier [opt] type-name
9535      :: [opt] nested-name-specifier template template-id
9536      char
9537      wchar_t
9538      bool
9539      short
9540      int
9541      long
9542      signed
9543      unsigned
9544      float
9545      double
9546      void
9547
9548    GNU Extension:
9549
9550    simple-type-specifier:
9551      __typeof__ unary-expression
9552      __typeof__ ( type-id )
9553
9554    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9555    appropriately updated.  */
9556
9557 static tree
9558 cp_parser_simple_type_specifier (cp_parser* parser,
9559                                  cp_decl_specifier_seq *decl_specs,
9560                                  cp_parser_flags flags)
9561 {
9562   tree type = NULL_TREE;
9563   cp_token *token;
9564
9565   /* Peek at the next token.  */
9566   token = cp_lexer_peek_token (parser->lexer);
9567
9568   /* If we're looking at a keyword, things are easy.  */
9569   switch (token->keyword)
9570     {
9571     case RID_CHAR:
9572       if (decl_specs)
9573         decl_specs->explicit_char_p = true;
9574       type = char_type_node;
9575       break;
9576     case RID_WCHAR:
9577       type = wchar_type_node;
9578       break;
9579     case RID_BOOL:
9580       type = boolean_type_node;
9581       break;
9582     case RID_SHORT:
9583       if (decl_specs)
9584         ++decl_specs->specs[(int) ds_short];
9585       type = short_integer_type_node;
9586       break;
9587     case RID_INT:
9588       if (decl_specs)
9589         decl_specs->explicit_int_p = true;
9590       type = integer_type_node;
9591       break;
9592     case RID_LONG:
9593       if (decl_specs)
9594         ++decl_specs->specs[(int) ds_long];
9595       type = long_integer_type_node;
9596       break;
9597     case RID_SIGNED:
9598       if (decl_specs)
9599         ++decl_specs->specs[(int) ds_signed];
9600       type = integer_type_node;
9601       break;
9602     case RID_UNSIGNED:
9603       if (decl_specs)
9604         ++decl_specs->specs[(int) ds_unsigned];
9605       type = unsigned_type_node;
9606       break;
9607     case RID_FLOAT:
9608       type = float_type_node;
9609       break;
9610     case RID_DOUBLE:
9611       type = double_type_node;
9612       break;
9613     case RID_VOID:
9614       type = void_type_node;
9615       break;
9616
9617     case RID_TYPEOF:
9618       /* Consume the `typeof' token.  */
9619       cp_lexer_consume_token (parser->lexer);
9620       /* Parse the operand to `typeof'.  */
9621       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9622       /* If it is not already a TYPE, take its type.  */
9623       if (!TYPE_P (type))
9624         type = finish_typeof (type);
9625
9626       if (decl_specs)
9627         cp_parser_set_decl_spec_type (decl_specs, type,
9628                                       /*user_defined_p=*/true);
9629
9630       return type;
9631
9632     default:
9633       break;
9634     }
9635
9636   /* If the type-specifier was for a built-in type, we're done.  */
9637   if (type)
9638     {
9639       tree id;
9640
9641       /* Record the type.  */
9642       if (decl_specs
9643           && (token->keyword != RID_SIGNED
9644               && token->keyword != RID_UNSIGNED
9645               && token->keyword != RID_SHORT
9646               && token->keyword != RID_LONG))
9647         cp_parser_set_decl_spec_type (decl_specs,
9648                                       type,
9649                                       /*user_defined=*/false);
9650       if (decl_specs)
9651         decl_specs->any_specifiers_p = true;
9652
9653       /* Consume the token.  */
9654       id = cp_lexer_consume_token (parser->lexer)->value;
9655
9656       /* There is no valid C++ program where a non-template type is
9657          followed by a "<".  That usually indicates that the user thought
9658          that the type was a template.  */
9659       cp_parser_check_for_invalid_template_id (parser, type);
9660
9661       return TYPE_NAME (type);
9662     }
9663
9664   /* The type-specifier must be a user-defined type.  */
9665   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9666     {
9667       bool qualified_p;
9668       bool global_p;
9669
9670       /* Don't gobble tokens or issue error messages if this is an
9671          optional type-specifier.  */
9672       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9673         cp_parser_parse_tentatively (parser);
9674
9675       /* Look for the optional `::' operator.  */
9676       global_p
9677         = (cp_parser_global_scope_opt (parser,
9678                                        /*current_scope_valid_p=*/false)
9679            != NULL_TREE);
9680       /* Look for the nested-name specifier.  */
9681       qualified_p
9682         = (cp_parser_nested_name_specifier_opt (parser,
9683                                                 /*typename_keyword_p=*/false,
9684                                                 /*check_dependency_p=*/true,
9685                                                 /*type_p=*/false,
9686                                                 /*is_declaration=*/false)
9687            != NULL_TREE);
9688       /* If we have seen a nested-name-specifier, and the next token
9689          is `template', then we are using the template-id production.  */
9690       if (parser->scope
9691           && cp_parser_optional_template_keyword (parser))
9692         {
9693           /* Look for the template-id.  */
9694           type = cp_parser_template_id (parser,
9695                                         /*template_keyword_p=*/true,
9696                                         /*check_dependency_p=*/true,
9697                                         /*is_declaration=*/false);
9698           /* If the template-id did not name a type, we are out of
9699              luck.  */
9700           if (TREE_CODE (type) != TYPE_DECL)
9701             {
9702               cp_parser_error (parser, "expected template-id for type");
9703               type = NULL_TREE;
9704             }
9705         }
9706       /* Otherwise, look for a type-name.  */
9707       else
9708         type = cp_parser_type_name (parser);
9709       /* Keep track of all name-lookups performed in class scopes.  */
9710       if (type
9711           && !global_p
9712           && !qualified_p
9713           && TREE_CODE (type) == TYPE_DECL
9714           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9715         maybe_note_name_used_in_class (DECL_NAME (type), type);
9716       /* If it didn't work out, we don't have a TYPE.  */
9717       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9718           && !cp_parser_parse_definitely (parser))
9719         type = NULL_TREE;
9720       if (type && decl_specs)
9721         cp_parser_set_decl_spec_type (decl_specs, type,
9722                                       /*user_defined=*/true);
9723     }
9724
9725   /* If we didn't get a type-name, issue an error message.  */
9726   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9727     {
9728       cp_parser_error (parser, "expected type-name");
9729       return error_mark_node;
9730     }
9731
9732   /* There is no valid C++ program where a non-template type is
9733      followed by a "<".  That usually indicates that the user thought
9734      that the type was a template.  */
9735   if (type && type != error_mark_node)
9736     {
9737       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9738          If it is, then the '<'...'>' enclose protocol names rather than
9739          template arguments, and so everything is fine.  */
9740       if (c_dialect_objc ()
9741           && (objc_is_id (type) || objc_is_class_name (type)))
9742         {
9743           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9744           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9745
9746           /* Clobber the "unqualified" type previously entered into
9747              DECL_SPECS with the new, improved protocol-qualified version.  */
9748           if (decl_specs)
9749             decl_specs->type = qual_type;
9750
9751           return qual_type;
9752         }
9753
9754       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9755     }
9756
9757   return type;
9758 }
9759
9760 /* Parse a type-name.
9761
9762    type-name:
9763      class-name
9764      enum-name
9765      typedef-name
9766
9767    enum-name:
9768      identifier
9769
9770    typedef-name:
9771      identifier
9772
9773    Returns a TYPE_DECL for the type.  */
9774
9775 static tree
9776 cp_parser_type_name (cp_parser* parser)
9777 {
9778   tree type_decl;
9779   tree identifier;
9780
9781   /* We can't know yet whether it is a class-name or not.  */
9782   cp_parser_parse_tentatively (parser);
9783   /* Try a class-name.  */
9784   type_decl = cp_parser_class_name (parser,
9785                                     /*typename_keyword_p=*/false,
9786                                     /*template_keyword_p=*/false,
9787                                     none_type,
9788                                     /*check_dependency_p=*/true,
9789                                     /*class_head_p=*/false,
9790                                     /*is_declaration=*/false);
9791   /* If it's not a class-name, keep looking.  */
9792   if (!cp_parser_parse_definitely (parser))
9793     {
9794       /* It must be a typedef-name or an enum-name.  */
9795       identifier = cp_parser_identifier (parser);
9796       if (identifier == error_mark_node)
9797         return error_mark_node;
9798
9799       /* Look up the type-name.  */
9800       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9801
9802       if (TREE_CODE (type_decl) != TYPE_DECL
9803           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9804         {
9805           /* See if this is an Objective-C type.  */
9806           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9807           tree type = objc_get_protocol_qualified_type (identifier, protos);
9808           if (type)
9809             type_decl = TYPE_NAME (type);
9810         }
9811
9812       /* Issue an error if we did not find a type-name.  */
9813       if (TREE_CODE (type_decl) != TYPE_DECL)
9814         {
9815           if (!cp_parser_simulate_error (parser))
9816             cp_parser_name_lookup_error (parser, identifier, type_decl,
9817                                          "is not a type");
9818           type_decl = error_mark_node;
9819         }
9820       /* Remember that the name was used in the definition of the
9821          current class so that we can check later to see if the
9822          meaning would have been different after the class was
9823          entirely defined.  */
9824       else if (type_decl != error_mark_node
9825                && !parser->scope)
9826         maybe_note_name_used_in_class (identifier, type_decl);
9827     }
9828
9829   return type_decl;
9830 }
9831
9832
9833 /* Parse an elaborated-type-specifier.  Note that the grammar given
9834    here incorporates the resolution to DR68.
9835
9836    elaborated-type-specifier:
9837      class-key :: [opt] nested-name-specifier [opt] identifier
9838      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9839      enum :: [opt] nested-name-specifier [opt] identifier
9840      typename :: [opt] nested-name-specifier identifier
9841      typename :: [opt] nested-name-specifier template [opt]
9842        template-id
9843
9844    GNU extension:
9845
9846    elaborated-type-specifier:
9847      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9848      class-key attributes :: [opt] nested-name-specifier [opt]
9849                template [opt] template-id
9850      enum attributes :: [opt] nested-name-specifier [opt] identifier
9851
9852    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9853    declared `friend'.  If IS_DECLARATION is TRUE, then this
9854    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9855    something is being declared.
9856
9857    Returns the TYPE specified.  */
9858
9859 static tree
9860 cp_parser_elaborated_type_specifier (cp_parser* parser,
9861                                      bool is_friend,
9862                                      bool is_declaration)
9863 {
9864   enum tag_types tag_type;
9865   tree identifier;
9866   tree type = NULL_TREE;
9867   tree attributes = NULL_TREE;
9868
9869   /* See if we're looking at the `enum' keyword.  */
9870   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9871     {
9872       /* Consume the `enum' token.  */
9873       cp_lexer_consume_token (parser->lexer);
9874       /* Remember that it's an enumeration type.  */
9875       tag_type = enum_type;
9876       /* Parse the attributes.  */
9877       attributes = cp_parser_attributes_opt (parser);
9878     }
9879   /* Or, it might be `typename'.  */
9880   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9881                                            RID_TYPENAME))
9882     {
9883       /* Consume the `typename' token.  */
9884       cp_lexer_consume_token (parser->lexer);
9885       /* Remember that it's a `typename' type.  */
9886       tag_type = typename_type;
9887       /* The `typename' keyword is only allowed in templates.  */
9888       if (!processing_template_decl)
9889         pedwarn ("using %<typename%> outside of template");
9890     }
9891   /* Otherwise it must be a class-key.  */
9892   else
9893     {
9894       tag_type = cp_parser_class_key (parser);
9895       if (tag_type == none_type)
9896         return error_mark_node;
9897       /* Parse the attributes.  */
9898       attributes = cp_parser_attributes_opt (parser);
9899     }
9900
9901   /* Look for the `::' operator.  */
9902   cp_parser_global_scope_opt (parser,
9903                               /*current_scope_valid_p=*/false);
9904   /* Look for the nested-name-specifier.  */
9905   if (tag_type == typename_type)
9906     {
9907       if (!cp_parser_nested_name_specifier (parser,
9908                                            /*typename_keyword_p=*/true,
9909                                            /*check_dependency_p=*/true,
9910                                            /*type_p=*/true,
9911                                             is_declaration))
9912         return error_mark_node;
9913     }
9914   else
9915     /* Even though `typename' is not present, the proposed resolution
9916        to Core Issue 180 says that in `class A<T>::B', `B' should be
9917        considered a type-name, even if `A<T>' is dependent.  */
9918     cp_parser_nested_name_specifier_opt (parser,
9919                                          /*typename_keyword_p=*/true,
9920                                          /*check_dependency_p=*/true,
9921                                          /*type_p=*/true,
9922                                          is_declaration);
9923   /* For everything but enumeration types, consider a template-id.  */
9924   if (tag_type != enum_type)
9925     {
9926       bool template_p = false;
9927       tree decl;
9928
9929       /* Allow the `template' keyword.  */
9930       template_p = cp_parser_optional_template_keyword (parser);
9931       /* If we didn't see `template', we don't know if there's a
9932          template-id or not.  */
9933       if (!template_p)
9934         cp_parser_parse_tentatively (parser);
9935       /* Parse the template-id.  */
9936       decl = cp_parser_template_id (parser, template_p,
9937                                     /*check_dependency_p=*/true,
9938                                     is_declaration);
9939       /* If we didn't find a template-id, look for an ordinary
9940          identifier.  */
9941       if (!template_p && !cp_parser_parse_definitely (parser))
9942         ;
9943       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9944          in effect, then we must assume that, upon instantiation, the
9945          template will correspond to a class.  */
9946       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9947                && tag_type == typename_type)
9948         type = make_typename_type (parser->scope, decl,
9949                                    typename_type,
9950                                    /*complain=*/1);
9951       else
9952         type = TREE_TYPE (decl);
9953     }
9954
9955   /* For an enumeration type, consider only a plain identifier.  */
9956   if (!type)
9957     {
9958       identifier = cp_parser_identifier (parser);
9959
9960       if (identifier == error_mark_node)
9961         {
9962           parser->scope = NULL_TREE;
9963           return error_mark_node;
9964         }
9965
9966       /* For a `typename', we needn't call xref_tag.  */
9967       if (tag_type == typename_type
9968           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9969         return cp_parser_make_typename_type (parser, parser->scope,
9970                                              identifier);
9971       /* Look up a qualified name in the usual way.  */
9972       if (parser->scope)
9973         {
9974           tree decl;
9975
9976           decl = cp_parser_lookup_name (parser, identifier,
9977                                         tag_type,
9978                                         /*is_template=*/false,
9979                                         /*is_namespace=*/false,
9980                                         /*check_dependency=*/true,
9981                                         /*ambiguous_p=*/NULL);
9982
9983           /* If we are parsing friend declaration, DECL may be a
9984              TEMPLATE_DECL tree node here.  However, we need to check
9985              whether this TEMPLATE_DECL results in valid code.  Consider
9986              the following example:
9987
9988                namespace N {
9989                  template <class T> class C {};
9990                }
9991                class X {
9992                  template <class T> friend class N::C; // #1, valid code
9993                };
9994                template <class T> class Y {
9995                  friend class N::C;                    // #2, invalid code
9996                };
9997
9998              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9999              name lookup of `N::C'.  We see that friend declaration must
10000              be template for the code to be valid.  Note that
10001              processing_template_decl does not work here since it is
10002              always 1 for the above two cases.  */
10003
10004           decl = (cp_parser_maybe_treat_template_as_class
10005                   (decl, /*tag_name_p=*/is_friend
10006                          && parser->num_template_parameter_lists));
10007
10008           if (TREE_CODE (decl) != TYPE_DECL)
10009             {
10010               cp_parser_diagnose_invalid_type_name (parser,
10011                                                     parser->scope,
10012                                                     identifier);
10013               return error_mark_node;
10014             }
10015
10016           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10017             check_elaborated_type_specifier
10018               (tag_type, decl,
10019                (parser->num_template_parameter_lists
10020                 || DECL_SELF_REFERENCE_P (decl)));
10021
10022           type = TREE_TYPE (decl);
10023         }
10024       else
10025         {
10026           /* An elaborated-type-specifier sometimes introduces a new type and
10027              sometimes names an existing type.  Normally, the rule is that it
10028              introduces a new type only if there is not an existing type of
10029              the same name already in scope.  For example, given:
10030
10031                struct S {};
10032                void f() { struct S s; }
10033
10034              the `struct S' in the body of `f' is the same `struct S' as in
10035              the global scope; the existing definition is used.  However, if
10036              there were no global declaration, this would introduce a new
10037              local class named `S'.
10038
10039              An exception to this rule applies to the following code:
10040
10041                namespace N { struct S; }
10042
10043              Here, the elaborated-type-specifier names a new type
10044              unconditionally; even if there is already an `S' in the
10045              containing scope this declaration names a new type.
10046              This exception only applies if the elaborated-type-specifier
10047              forms the complete declaration:
10048
10049                [class.name]
10050
10051                A declaration consisting solely of `class-key identifier ;' is
10052                either a redeclaration of the name in the current scope or a
10053                forward declaration of the identifier as a class name.  It
10054                introduces the name into the current scope.
10055
10056              We are in this situation precisely when the next token is a `;'.
10057
10058              An exception to the exception is that a `friend' declaration does
10059              *not* name a new type; i.e., given:
10060
10061                struct S { friend struct T; };
10062
10063              `T' is not a new type in the scope of `S'.
10064
10065              Also, `new struct S' or `sizeof (struct S)' never results in the
10066              definition of a new type; a new type can only be declared in a
10067              declaration context.  */
10068
10069           tag_scope ts;
10070           bool template_p;
10071
10072           if (is_friend)
10073             /* Friends have special name lookup rules.  */
10074             ts = ts_within_enclosing_non_class;
10075           else if (is_declaration
10076                    && cp_lexer_next_token_is (parser->lexer,
10077                                               CPP_SEMICOLON))
10078             /* This is a `class-key identifier ;' */
10079             ts = ts_current;
10080           else
10081             ts = ts_global;
10082
10083           /* Warn about attributes. They are ignored.  */
10084           if (attributes)
10085             warning (OPT_Wattributes,
10086                      "type attributes are honored only at type definition");
10087
10088           template_p = 
10089             (parser->num_template_parameter_lists
10090              && (cp_parser_next_token_starts_class_definition_p (parser)
10091                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10092           type = xref_tag (tag_type, identifier, ts, template_p);
10093         }
10094     }
10095   if (tag_type != enum_type)
10096     cp_parser_check_class_key (tag_type, type);
10097
10098   /* A "<" cannot follow an elaborated type specifier.  If that
10099      happens, the user was probably trying to form a template-id.  */
10100   cp_parser_check_for_invalid_template_id (parser, type);
10101
10102   return type;
10103 }
10104
10105 /* Parse an enum-specifier.
10106
10107    enum-specifier:
10108      enum identifier [opt] { enumerator-list [opt] }
10109
10110    GNU Extensions:
10111      enum identifier [opt] { enumerator-list [opt] } attributes
10112
10113    Returns an ENUM_TYPE representing the enumeration.  */
10114
10115 static tree
10116 cp_parser_enum_specifier (cp_parser* parser)
10117 {
10118   tree identifier;
10119   tree type;
10120
10121   /* Caller guarantees that the current token is 'enum', an identifier
10122      possibly follows, and the token after that is an opening brace.
10123      If we don't have an identifier, fabricate an anonymous name for
10124      the enumeration being defined.  */
10125   cp_lexer_consume_token (parser->lexer);
10126
10127   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10128     identifier = cp_parser_identifier (parser);
10129   else
10130     identifier = make_anon_name ();
10131
10132   /* Issue an error message if type-definitions are forbidden here.  */
10133   cp_parser_check_type_definition (parser);
10134
10135   /* Create the new type.  We do this before consuming the opening brace
10136      so the enum will be recorded as being on the line of its tag (or the
10137      'enum' keyword, if there is no tag).  */
10138   type = start_enum (identifier);
10139
10140   /* Consume the opening brace.  */
10141   cp_lexer_consume_token (parser->lexer);
10142
10143   /* If the next token is not '}', then there are some enumerators.  */
10144   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10145     cp_parser_enumerator_list (parser, type);
10146
10147   /* Consume the final '}'.  */
10148   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10149
10150   /* Look for trailing attributes to apply to this enumeration, and
10151      apply them if appropriate.  */
10152   if (cp_parser_allow_gnu_extensions_p (parser))
10153     {
10154       tree trailing_attr = cp_parser_attributes_opt (parser);
10155       cplus_decl_attributes (&type,
10156                              trailing_attr,
10157                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10158     }
10159
10160   /* Finish up the enumeration.  */
10161   finish_enum (type);
10162
10163   return type;
10164 }
10165
10166 /* Parse an enumerator-list.  The enumerators all have the indicated
10167    TYPE.
10168
10169    enumerator-list:
10170      enumerator-definition
10171      enumerator-list , enumerator-definition  */
10172
10173 static void
10174 cp_parser_enumerator_list (cp_parser* parser, tree type)
10175 {
10176   while (true)
10177     {
10178       /* Parse an enumerator-definition.  */
10179       cp_parser_enumerator_definition (parser, type);
10180
10181       /* If the next token is not a ',', we've reached the end of
10182          the list.  */
10183       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10184         break;
10185       /* Otherwise, consume the `,' and keep going.  */
10186       cp_lexer_consume_token (parser->lexer);
10187       /* If the next token is a `}', there is a trailing comma.  */
10188       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10189         {
10190           if (pedantic && !in_system_header)
10191             pedwarn ("comma at end of enumerator list");
10192           break;
10193         }
10194     }
10195 }
10196
10197 /* Parse an enumerator-definition.  The enumerator has the indicated
10198    TYPE.
10199
10200    enumerator-definition:
10201      enumerator
10202      enumerator = constant-expression
10203
10204    enumerator:
10205      identifier  */
10206
10207 static void
10208 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10209 {
10210   tree identifier;
10211   tree value;
10212
10213   /* Look for the identifier.  */
10214   identifier = cp_parser_identifier (parser);
10215   if (identifier == error_mark_node)
10216     return;
10217
10218   /* If the next token is an '=', then there is an explicit value.  */
10219   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10220     {
10221       /* Consume the `=' token.  */
10222       cp_lexer_consume_token (parser->lexer);
10223       /* Parse the value.  */
10224       value = cp_parser_constant_expression (parser,
10225                                              /*allow_non_constant_p=*/false,
10226                                              NULL);
10227     }
10228   else
10229     value = NULL_TREE;
10230
10231   /* Create the enumerator.  */
10232   build_enumerator (identifier, value, type);
10233 }
10234
10235 /* Parse a namespace-name.
10236
10237    namespace-name:
10238      original-namespace-name
10239      namespace-alias
10240
10241    Returns the NAMESPACE_DECL for the namespace.  */
10242
10243 static tree
10244 cp_parser_namespace_name (cp_parser* parser)
10245 {
10246   tree identifier;
10247   tree namespace_decl;
10248
10249   /* Get the name of the namespace.  */
10250   identifier = cp_parser_identifier (parser);
10251   if (identifier == error_mark_node)
10252     return error_mark_node;
10253
10254   /* Look up the identifier in the currently active scope.  Look only
10255      for namespaces, due to:
10256
10257        [basic.lookup.udir]
10258
10259        When looking up a namespace-name in a using-directive or alias
10260        definition, only namespace names are considered.
10261
10262      And:
10263
10264        [basic.lookup.qual]
10265
10266        During the lookup of a name preceding the :: scope resolution
10267        operator, object, function, and enumerator names are ignored.
10268
10269      (Note that cp_parser_class_or_namespace_name only calls this
10270      function if the token after the name is the scope resolution
10271      operator.)  */
10272   namespace_decl = cp_parser_lookup_name (parser, identifier,
10273                                           none_type,
10274                                           /*is_template=*/false,
10275                                           /*is_namespace=*/true,
10276                                           /*check_dependency=*/true,
10277                                           /*ambiguous_p=*/NULL);
10278   /* If it's not a namespace, issue an error.  */
10279   if (namespace_decl == error_mark_node
10280       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10281     {
10282       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10283         error ("%qD is not a namespace-name", identifier);
10284       cp_parser_error (parser, "expected namespace-name");
10285       namespace_decl = error_mark_node;
10286     }
10287
10288   return namespace_decl;
10289 }
10290
10291 /* Parse a namespace-definition.
10292
10293    namespace-definition:
10294      named-namespace-definition
10295      unnamed-namespace-definition
10296
10297    named-namespace-definition:
10298      original-namespace-definition
10299      extension-namespace-definition
10300
10301    original-namespace-definition:
10302      namespace identifier { namespace-body }
10303
10304    extension-namespace-definition:
10305      namespace original-namespace-name { namespace-body }
10306
10307    unnamed-namespace-definition:
10308      namespace { namespace-body } */
10309
10310 static void
10311 cp_parser_namespace_definition (cp_parser* parser)
10312 {
10313   tree identifier;
10314
10315   /* Look for the `namespace' keyword.  */
10316   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10317
10318   /* Get the name of the namespace.  We do not attempt to distinguish
10319      between an original-namespace-definition and an
10320      extension-namespace-definition at this point.  The semantic
10321      analysis routines are responsible for that.  */
10322   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10323     identifier = cp_parser_identifier (parser);
10324   else
10325     identifier = NULL_TREE;
10326
10327   /* Look for the `{' to start the namespace.  */
10328   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10329   /* Start the namespace.  */
10330   push_namespace (identifier);
10331   /* Parse the body of the namespace.  */
10332   cp_parser_namespace_body (parser);
10333   /* Finish the namespace.  */
10334   pop_namespace ();
10335   /* Look for the final `}'.  */
10336   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10337 }
10338
10339 /* Parse a namespace-body.
10340
10341    namespace-body:
10342      declaration-seq [opt]  */
10343
10344 static void
10345 cp_parser_namespace_body (cp_parser* parser)
10346 {
10347   cp_parser_declaration_seq_opt (parser);
10348 }
10349
10350 /* Parse a namespace-alias-definition.
10351
10352    namespace-alias-definition:
10353      namespace identifier = qualified-namespace-specifier ;  */
10354
10355 static void
10356 cp_parser_namespace_alias_definition (cp_parser* parser)
10357 {
10358   tree identifier;
10359   tree namespace_specifier;
10360
10361   /* Look for the `namespace' keyword.  */
10362   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10363   /* Look for the identifier.  */
10364   identifier = cp_parser_identifier (parser);
10365   if (identifier == error_mark_node)
10366     return;
10367   /* Look for the `=' token.  */
10368   cp_parser_require (parser, CPP_EQ, "`='");
10369   /* Look for the qualified-namespace-specifier.  */
10370   namespace_specifier
10371     = cp_parser_qualified_namespace_specifier (parser);
10372   /* Look for the `;' token.  */
10373   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10374
10375   /* Register the alias in the symbol table.  */
10376   do_namespace_alias (identifier, namespace_specifier);
10377 }
10378
10379 /* Parse a qualified-namespace-specifier.
10380
10381    qualified-namespace-specifier:
10382      :: [opt] nested-name-specifier [opt] namespace-name
10383
10384    Returns a NAMESPACE_DECL corresponding to the specified
10385    namespace.  */
10386
10387 static tree
10388 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10389 {
10390   /* Look for the optional `::'.  */
10391   cp_parser_global_scope_opt (parser,
10392                               /*current_scope_valid_p=*/false);
10393
10394   /* Look for the optional nested-name-specifier.  */
10395   cp_parser_nested_name_specifier_opt (parser,
10396                                        /*typename_keyword_p=*/false,
10397                                        /*check_dependency_p=*/true,
10398                                        /*type_p=*/false,
10399                                        /*is_declaration=*/true);
10400
10401   return cp_parser_namespace_name (parser);
10402 }
10403
10404 /* Parse a using-declaration.
10405
10406    using-declaration:
10407      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10408      using :: unqualified-id ;  */
10409
10410 static void
10411 cp_parser_using_declaration (cp_parser* parser)
10412 {
10413   cp_token *token;
10414   bool typename_p = false;
10415   bool global_scope_p;
10416   tree decl;
10417   tree identifier;
10418   tree qscope;
10419
10420   /* Look for the `using' keyword.  */
10421   cp_parser_require_keyword (parser, RID_USING, "`using'");
10422
10423   /* Peek at the next token.  */
10424   token = cp_lexer_peek_token (parser->lexer);
10425   /* See if it's `typename'.  */
10426   if (token->keyword == RID_TYPENAME)
10427     {
10428       /* Remember that we've seen it.  */
10429       typename_p = true;
10430       /* Consume the `typename' token.  */
10431       cp_lexer_consume_token (parser->lexer);
10432     }
10433
10434   /* Look for the optional global scope qualification.  */
10435   global_scope_p
10436     = (cp_parser_global_scope_opt (parser,
10437                                    /*current_scope_valid_p=*/false)
10438        != NULL_TREE);
10439
10440   /* If we saw `typename', or didn't see `::', then there must be a
10441      nested-name-specifier present.  */
10442   if (typename_p || !global_scope_p)
10443     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10444                                               /*check_dependency_p=*/true,
10445                                               /*type_p=*/false,
10446                                               /*is_declaration=*/true);
10447   /* Otherwise, we could be in either of the two productions.  In that
10448      case, treat the nested-name-specifier as optional.  */
10449   else
10450     qscope = cp_parser_nested_name_specifier_opt (parser,
10451                                                   /*typename_keyword_p=*/false,
10452                                                   /*check_dependency_p=*/true,
10453                                                   /*type_p=*/false,
10454                                                   /*is_declaration=*/true);
10455   if (!qscope)
10456     qscope = global_namespace;
10457
10458   /* Parse the unqualified-id.  */
10459   identifier = cp_parser_unqualified_id (parser,
10460                                          /*template_keyword_p=*/false,
10461                                          /*check_dependency_p=*/true,
10462                                          /*declarator_p=*/true);
10463
10464   /* The function we call to handle a using-declaration is different
10465      depending on what scope we are in.  */
10466   if (identifier == error_mark_node)
10467     ;
10468   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10469            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10470     /* [namespace.udecl]
10471
10472        A using declaration shall not name a template-id.  */
10473     error ("a template-id may not appear in a using-declaration");
10474   else
10475     {
10476       if (at_class_scope_p ())
10477         {
10478           /* Create the USING_DECL.  */
10479           decl = do_class_using_decl (parser->scope, identifier);
10480           /* Add it to the list of members in this class.  */
10481           finish_member_declaration (decl);
10482         }
10483       else
10484         {
10485           decl = cp_parser_lookup_name_simple (parser, identifier);
10486           if (decl == error_mark_node)
10487             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10488           else if (!at_namespace_scope_p ())
10489             do_local_using_decl (decl, qscope, identifier);
10490           else
10491             do_toplevel_using_decl (decl, qscope, identifier);
10492         }
10493     }
10494
10495   /* Look for the final `;'.  */
10496   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10497 }
10498
10499 /* Parse a using-directive.
10500
10501    using-directive:
10502      using namespace :: [opt] nested-name-specifier [opt]
10503        namespace-name ;  */
10504
10505 static void
10506 cp_parser_using_directive (cp_parser* parser)
10507 {
10508   tree namespace_decl;
10509   tree attribs;
10510
10511   /* Look for the `using' keyword.  */
10512   cp_parser_require_keyword (parser, RID_USING, "`using'");
10513   /* And the `namespace' keyword.  */
10514   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10515   /* Look for the optional `::' operator.  */
10516   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10517   /* And the optional nested-name-specifier.  */
10518   cp_parser_nested_name_specifier_opt (parser,
10519                                        /*typename_keyword_p=*/false,
10520                                        /*check_dependency_p=*/true,
10521                                        /*type_p=*/false,
10522                                        /*is_declaration=*/true);
10523   /* Get the namespace being used.  */
10524   namespace_decl = cp_parser_namespace_name (parser);
10525   /* And any specified attributes.  */
10526   attribs = cp_parser_attributes_opt (parser);
10527   /* Update the symbol table.  */
10528   parse_using_directive (namespace_decl, attribs);
10529   /* Look for the final `;'.  */
10530   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10531 }
10532
10533 /* Parse an asm-definition.
10534
10535    asm-definition:
10536      asm ( string-literal ) ;
10537
10538    GNU Extension:
10539
10540    asm-definition:
10541      asm volatile [opt] ( string-literal ) ;
10542      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10543      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10544                           : asm-operand-list [opt] ) ;
10545      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10546                           : asm-operand-list [opt]
10547                           : asm-operand-list [opt] ) ;  */
10548
10549 static void
10550 cp_parser_asm_definition (cp_parser* parser)
10551 {
10552   tree string;
10553   tree outputs = NULL_TREE;
10554   tree inputs = NULL_TREE;
10555   tree clobbers = NULL_TREE;
10556   tree asm_stmt;
10557   bool volatile_p = false;
10558   bool extended_p = false;
10559
10560   /* Look for the `asm' keyword.  */
10561   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10562   /* See if the next token is `volatile'.  */
10563   if (cp_parser_allow_gnu_extensions_p (parser)
10564       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10565     {
10566       /* Remember that we saw the `volatile' keyword.  */
10567       volatile_p = true;
10568       /* Consume the token.  */
10569       cp_lexer_consume_token (parser->lexer);
10570     }
10571   /* Look for the opening `('.  */
10572   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10573     return;
10574   /* Look for the string.  */
10575   string = cp_parser_string_literal (parser, false, false);
10576   if (string == error_mark_node)
10577     {
10578       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10579                                              /*consume_paren=*/true);
10580       return;
10581     }
10582
10583   /* If we're allowing GNU extensions, check for the extended assembly
10584      syntax.  Unfortunately, the `:' tokens need not be separated by
10585      a space in C, and so, for compatibility, we tolerate that here
10586      too.  Doing that means that we have to treat the `::' operator as
10587      two `:' tokens.  */
10588   if (cp_parser_allow_gnu_extensions_p (parser)
10589       && at_function_scope_p ()
10590       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10591           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10592     {
10593       bool inputs_p = false;
10594       bool clobbers_p = false;
10595
10596       /* The extended syntax was used.  */
10597       extended_p = true;
10598
10599       /* Look for outputs.  */
10600       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10601         {
10602           /* Consume the `:'.  */
10603           cp_lexer_consume_token (parser->lexer);
10604           /* Parse the output-operands.  */
10605           if (cp_lexer_next_token_is_not (parser->lexer,
10606                                           CPP_COLON)
10607               && cp_lexer_next_token_is_not (parser->lexer,
10608                                              CPP_SCOPE)
10609               && cp_lexer_next_token_is_not (parser->lexer,
10610                                              CPP_CLOSE_PAREN))
10611             outputs = cp_parser_asm_operand_list (parser);
10612         }
10613       /* If the next token is `::', there are no outputs, and the
10614          next token is the beginning of the inputs.  */
10615       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10616         /* The inputs are coming next.  */
10617         inputs_p = true;
10618
10619       /* Look for inputs.  */
10620       if (inputs_p
10621           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10622         {
10623           /* Consume the `:' or `::'.  */
10624           cp_lexer_consume_token (parser->lexer);
10625           /* Parse the output-operands.  */
10626           if (cp_lexer_next_token_is_not (parser->lexer,
10627                                           CPP_COLON)
10628               && cp_lexer_next_token_is_not (parser->lexer,
10629                                              CPP_CLOSE_PAREN))
10630             inputs = cp_parser_asm_operand_list (parser);
10631         }
10632       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10633         /* The clobbers are coming next.  */
10634         clobbers_p = true;
10635
10636       /* Look for clobbers.  */
10637       if (clobbers_p
10638           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10639         {
10640           /* Consume the `:' or `::'.  */
10641           cp_lexer_consume_token (parser->lexer);
10642           /* Parse the clobbers.  */
10643           if (cp_lexer_next_token_is_not (parser->lexer,
10644                                           CPP_CLOSE_PAREN))
10645             clobbers = cp_parser_asm_clobber_list (parser);
10646         }
10647     }
10648   /* Look for the closing `)'.  */
10649   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10650     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10651                                            /*consume_paren=*/true);
10652   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10653
10654   /* Create the ASM_EXPR.  */
10655   if (at_function_scope_p ())
10656     {
10657       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10658                                   inputs, clobbers);
10659       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10660       if (!extended_p)
10661         {
10662           tree temp = asm_stmt;
10663           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10664             temp = TREE_OPERAND (temp, 0);
10665
10666           ASM_INPUT_P (temp) = 1;
10667         }
10668     }
10669   else
10670     assemble_asm (string);
10671 }
10672
10673 /* Declarators [gram.dcl.decl] */
10674
10675 /* Parse an init-declarator.
10676
10677    init-declarator:
10678      declarator initializer [opt]
10679
10680    GNU Extension:
10681
10682    init-declarator:
10683      declarator asm-specification [opt] attributes [opt] initializer [opt]
10684
10685    function-definition:
10686      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10687        function-body
10688      decl-specifier-seq [opt] declarator function-try-block
10689
10690    GNU Extension:
10691
10692    function-definition:
10693      __extension__ function-definition
10694
10695    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10696    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10697    then this declarator appears in a class scope.  The new DECL created
10698    by this declarator is returned.
10699
10700    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10701    for a function-definition here as well.  If the declarator is a
10702    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10703    be TRUE upon return.  By that point, the function-definition will
10704    have been completely parsed.
10705
10706    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10707    is FALSE.  */
10708
10709 static tree
10710 cp_parser_init_declarator (cp_parser* parser,
10711                            cp_decl_specifier_seq *decl_specifiers,
10712                            bool function_definition_allowed_p,
10713                            bool member_p,
10714                            int declares_class_or_enum,
10715                            bool* function_definition_p)
10716 {
10717   cp_token *token;
10718   cp_declarator *declarator;
10719   tree prefix_attributes;
10720   tree attributes;
10721   tree asm_specification;
10722   tree initializer;
10723   tree decl = NULL_TREE;
10724   tree scope;
10725   bool is_initialized;
10726   bool is_parenthesized_init;
10727   bool is_non_constant_init;
10728   int ctor_dtor_or_conv_p;
10729   bool friend_p;
10730   tree pushed_scope = NULL;
10731
10732   /* Gather the attributes that were provided with the
10733      decl-specifiers.  */
10734   prefix_attributes = decl_specifiers->attributes;
10735
10736   /* Assume that this is not the declarator for a function
10737      definition.  */
10738   if (function_definition_p)
10739     *function_definition_p = false;
10740
10741   /* Defer access checks while parsing the declarator; we cannot know
10742      what names are accessible until we know what is being
10743      declared.  */
10744   resume_deferring_access_checks ();
10745
10746   /* Parse the declarator.  */
10747   declarator
10748     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10749                             &ctor_dtor_or_conv_p,
10750                             /*parenthesized_p=*/NULL,
10751                             /*member_p=*/false);
10752   /* Gather up the deferred checks.  */
10753   stop_deferring_access_checks ();
10754
10755   /* If the DECLARATOR was erroneous, there's no need to go
10756      further.  */
10757   if (declarator == cp_error_declarator)
10758     return error_mark_node;
10759
10760   if (declares_class_or_enum & 2)
10761     cp_parser_check_for_definition_in_return_type (declarator,
10762                                                    decl_specifiers->type);
10763
10764   /* Figure out what scope the entity declared by the DECLARATOR is
10765      located in.  `grokdeclarator' sometimes changes the scope, so
10766      we compute it now.  */
10767   scope = get_scope_of_declarator (declarator);
10768
10769   /* If we're allowing GNU extensions, look for an asm-specification
10770      and attributes.  */
10771   if (cp_parser_allow_gnu_extensions_p (parser))
10772     {
10773       /* Look for an asm-specification.  */
10774       asm_specification = cp_parser_asm_specification_opt (parser);
10775       /* And attributes.  */
10776       attributes = cp_parser_attributes_opt (parser);
10777     }
10778   else
10779     {
10780       asm_specification = NULL_TREE;
10781       attributes = NULL_TREE;
10782     }
10783
10784   /* Peek at the next token.  */
10785   token = cp_lexer_peek_token (parser->lexer);
10786   /* Check to see if the token indicates the start of a
10787      function-definition.  */
10788   if (cp_parser_token_starts_function_definition_p (token))
10789     {
10790       if (!function_definition_allowed_p)
10791         {
10792           /* If a function-definition should not appear here, issue an
10793              error message.  */
10794           cp_parser_error (parser,
10795                            "a function-definition is not allowed here");
10796           return error_mark_node;
10797         }
10798       else
10799         {
10800           /* Neither attributes nor an asm-specification are allowed
10801              on a function-definition.  */
10802           if (asm_specification)
10803             error ("an asm-specification is not allowed on a function-definition");
10804           if (attributes)
10805             error ("attributes are not allowed on a function-definition");
10806           /* This is a function-definition.  */
10807           *function_definition_p = true;
10808
10809           /* Parse the function definition.  */
10810           if (member_p)
10811             decl = cp_parser_save_member_function_body (parser,
10812                                                         decl_specifiers,
10813                                                         declarator,
10814                                                         prefix_attributes);
10815           else
10816             decl
10817               = (cp_parser_function_definition_from_specifiers_and_declarator
10818                  (parser, decl_specifiers, prefix_attributes, declarator));
10819
10820           return decl;
10821         }
10822     }
10823
10824   /* [dcl.dcl]
10825
10826      Only in function declarations for constructors, destructors, and
10827      type conversions can the decl-specifier-seq be omitted.
10828
10829      We explicitly postpone this check past the point where we handle
10830      function-definitions because we tolerate function-definitions
10831      that are missing their return types in some modes.  */
10832   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10833     {
10834       cp_parser_error (parser,
10835                        "expected constructor, destructor, or type conversion");
10836       return error_mark_node;
10837     }
10838
10839   /* An `=' or an `(' indicates an initializer.  */
10840   is_initialized = (token->type == CPP_EQ
10841                      || token->type == CPP_OPEN_PAREN);
10842   /* If the init-declarator isn't initialized and isn't followed by a
10843      `,' or `;', it's not a valid init-declarator.  */
10844   if (!is_initialized
10845       && token->type != CPP_COMMA
10846       && token->type != CPP_SEMICOLON)
10847     {
10848       cp_parser_error (parser, "expected initializer");
10849       return error_mark_node;
10850     }
10851
10852   /* Because start_decl has side-effects, we should only call it if we
10853      know we're going ahead.  By this point, we know that we cannot
10854      possibly be looking at any other construct.  */
10855   cp_parser_commit_to_tentative_parse (parser);
10856
10857   /* If the decl specifiers were bad, issue an error now that we're
10858      sure this was intended to be a declarator.  Then continue
10859      declaring the variable(s), as int, to try to cut down on further
10860      errors.  */
10861   if (decl_specifiers->any_specifiers_p
10862       && decl_specifiers->type == error_mark_node)
10863     {
10864       cp_parser_error (parser, "invalid type in declaration");
10865       decl_specifiers->type = integer_type_node;
10866     }
10867
10868   /* Check to see whether or not this declaration is a friend.  */
10869   friend_p = cp_parser_friend_p (decl_specifiers);
10870
10871   /* Check that the number of template-parameter-lists is OK.  */
10872   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10873     return error_mark_node;
10874
10875   /* Enter the newly declared entry in the symbol table.  If we're
10876      processing a declaration in a class-specifier, we wait until
10877      after processing the initializer.  */
10878   if (!member_p)
10879     {
10880       if (parser->in_unbraced_linkage_specification_p)
10881         {
10882           decl_specifiers->storage_class = sc_extern;
10883           have_extern_spec = false;
10884         }
10885       decl = start_decl (declarator, decl_specifiers,
10886                          is_initialized, attributes, prefix_attributes,
10887                          &pushed_scope);
10888     }
10889   else if (scope)
10890     /* Enter the SCOPE.  That way unqualified names appearing in the
10891        initializer will be looked up in SCOPE.  */
10892     pushed_scope = push_scope (scope);
10893
10894   /* Perform deferred access control checks, now that we know in which
10895      SCOPE the declared entity resides.  */
10896   if (!member_p && decl)
10897     {
10898       tree saved_current_function_decl = NULL_TREE;
10899
10900       /* If the entity being declared is a function, pretend that we
10901          are in its scope.  If it is a `friend', it may have access to
10902          things that would not otherwise be accessible.  */
10903       if (TREE_CODE (decl) == FUNCTION_DECL)
10904         {
10905           saved_current_function_decl = current_function_decl;
10906           current_function_decl = decl;
10907         }
10908
10909       /* Perform the access control checks for the declarator and the
10910          the decl-specifiers.  */
10911       perform_deferred_access_checks ();
10912
10913       /* Restore the saved value.  */
10914       if (TREE_CODE (decl) == FUNCTION_DECL)
10915         current_function_decl = saved_current_function_decl;
10916     }
10917
10918   /* Parse the initializer.  */
10919   if (is_initialized)
10920     initializer = cp_parser_initializer (parser,
10921                                          &is_parenthesized_init,
10922                                          &is_non_constant_init);
10923   else
10924     {
10925       initializer = NULL_TREE;
10926       is_parenthesized_init = false;
10927       is_non_constant_init = true;
10928     }
10929
10930   /* The old parser allows attributes to appear after a parenthesized
10931      initializer.  Mark Mitchell proposed removing this functionality
10932      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10933      attributes -- but ignores them.  */
10934   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10935     if (cp_parser_attributes_opt (parser))
10936       warning (OPT_Wattributes,
10937                "attributes after parenthesized initializer ignored");
10938
10939   /* For an in-class declaration, use `grokfield' to create the
10940      declaration.  */
10941   if (member_p)
10942     {
10943       if (pushed_scope)
10944         {
10945           pop_scope (pushed_scope);
10946           pushed_scope = false;
10947         }
10948       decl = grokfield (declarator, decl_specifiers,
10949                         initializer, /*asmspec=*/NULL_TREE,
10950                         /*attributes=*/NULL_TREE);
10951       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10952         cp_parser_save_default_args (parser, decl);
10953     }
10954
10955   /* Finish processing the declaration.  But, skip friend
10956      declarations.  */
10957   if (!friend_p && decl && decl != error_mark_node)
10958     {
10959       cp_finish_decl (decl,
10960                       initializer,
10961                       asm_specification,
10962                       /* If the initializer is in parentheses, then this is
10963                          a direct-initialization, which means that an
10964                          `explicit' constructor is OK.  Otherwise, an
10965                          `explicit' constructor cannot be used.  */
10966                       ((is_parenthesized_init || !is_initialized)
10967                      ? 0 : LOOKUP_ONLYCONVERTING));
10968     }
10969   if (!friend_p && pushed_scope)
10970     pop_scope (pushed_scope);
10971
10972   /* Remember whether or not variables were initialized by
10973      constant-expressions.  */
10974   if (decl && TREE_CODE (decl) == VAR_DECL
10975       && is_initialized && !is_non_constant_init)
10976     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10977
10978   return decl;
10979 }
10980
10981 /* Parse a declarator.
10982
10983    declarator:
10984      direct-declarator
10985      ptr-operator declarator
10986
10987    abstract-declarator:
10988      ptr-operator abstract-declarator [opt]
10989      direct-abstract-declarator
10990
10991    GNU Extensions:
10992
10993    declarator:
10994      attributes [opt] direct-declarator
10995      attributes [opt] ptr-operator declarator
10996
10997    abstract-declarator:
10998      attributes [opt] ptr-operator abstract-declarator [opt]
10999      attributes [opt] direct-abstract-declarator
11000
11001    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11002    detect constructor, destructor or conversion operators. It is set
11003    to -1 if the declarator is a name, and +1 if it is a
11004    function. Otherwise it is set to zero. Usually you just want to
11005    test for >0, but internally the negative value is used.
11006
11007    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11008    a decl-specifier-seq unless it declares a constructor, destructor,
11009    or conversion.  It might seem that we could check this condition in
11010    semantic analysis, rather than parsing, but that makes it difficult
11011    to handle something like `f()'.  We want to notice that there are
11012    no decl-specifiers, and therefore realize that this is an
11013    expression, not a declaration.)
11014
11015    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11016    the declarator is a direct-declarator of the form "(...)".
11017
11018    MEMBER_P is true iff this declarator is a member-declarator.  */
11019
11020 static cp_declarator *
11021 cp_parser_declarator (cp_parser* parser,
11022                       cp_parser_declarator_kind dcl_kind,
11023                       int* ctor_dtor_or_conv_p,
11024                       bool* parenthesized_p,
11025                       bool member_p)
11026 {
11027   cp_token *token;
11028   cp_declarator *declarator;
11029   enum tree_code code;
11030   cp_cv_quals cv_quals;
11031   tree class_type;
11032   tree attributes = NULL_TREE;
11033
11034   /* Assume this is not a constructor, destructor, or type-conversion
11035      operator.  */
11036   if (ctor_dtor_or_conv_p)
11037     *ctor_dtor_or_conv_p = 0;
11038
11039   if (cp_parser_allow_gnu_extensions_p (parser))
11040     attributes = cp_parser_attributes_opt (parser);
11041
11042   /* Peek at the next token.  */
11043   token = cp_lexer_peek_token (parser->lexer);
11044
11045   /* Check for the ptr-operator production.  */
11046   cp_parser_parse_tentatively (parser);
11047   /* Parse the ptr-operator.  */
11048   code = cp_parser_ptr_operator (parser,
11049                                  &class_type,
11050                                  &cv_quals);
11051   /* If that worked, then we have a ptr-operator.  */
11052   if (cp_parser_parse_definitely (parser))
11053     {
11054       /* If a ptr-operator was found, then this declarator was not
11055          parenthesized.  */
11056       if (parenthesized_p)
11057         *parenthesized_p = true;
11058       /* The dependent declarator is optional if we are parsing an
11059          abstract-declarator.  */
11060       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11061         cp_parser_parse_tentatively (parser);
11062
11063       /* Parse the dependent declarator.  */
11064       declarator = cp_parser_declarator (parser, dcl_kind,
11065                                          /*ctor_dtor_or_conv_p=*/NULL,
11066                                          /*parenthesized_p=*/NULL,
11067                                          /*member_p=*/false);
11068
11069       /* If we are parsing an abstract-declarator, we must handle the
11070          case where the dependent declarator is absent.  */
11071       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11072           && !cp_parser_parse_definitely (parser))
11073         declarator = NULL;
11074
11075       /* Build the representation of the ptr-operator.  */
11076       if (class_type)
11077         declarator = make_ptrmem_declarator (cv_quals,
11078                                              class_type,
11079                                              declarator);
11080       else if (code == INDIRECT_REF)
11081         declarator = make_pointer_declarator (cv_quals, declarator);
11082       else
11083         declarator = make_reference_declarator (cv_quals, declarator);
11084     }
11085   /* Everything else is a direct-declarator.  */
11086   else
11087     {
11088       if (parenthesized_p)
11089         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11090                                                    CPP_OPEN_PAREN);
11091       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11092                                                 ctor_dtor_or_conv_p,
11093                                                 member_p);
11094     }
11095
11096   if (attributes && declarator != cp_error_declarator)
11097     declarator->attributes = attributes;
11098
11099   return declarator;
11100 }
11101
11102 /* Parse a direct-declarator or direct-abstract-declarator.
11103
11104    direct-declarator:
11105      declarator-id
11106      direct-declarator ( parameter-declaration-clause )
11107        cv-qualifier-seq [opt]
11108        exception-specification [opt]
11109      direct-declarator [ constant-expression [opt] ]
11110      ( declarator )
11111
11112    direct-abstract-declarator:
11113      direct-abstract-declarator [opt]
11114        ( parameter-declaration-clause )
11115        cv-qualifier-seq [opt]
11116        exception-specification [opt]
11117      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11118      ( abstract-declarator )
11119
11120    Returns a representation of the declarator.  DCL_KIND is
11121    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11122    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11123    we are parsing a direct-declarator.  It is
11124    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11125    of ambiguity we prefer an abstract declarator, as per
11126    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11127    cp_parser_declarator.  */
11128
11129 static cp_declarator *
11130 cp_parser_direct_declarator (cp_parser* parser,
11131                              cp_parser_declarator_kind dcl_kind,
11132                              int* ctor_dtor_or_conv_p,
11133                              bool member_p)
11134 {
11135   cp_token *token;
11136   cp_declarator *declarator = NULL;
11137   tree scope = NULL_TREE;
11138   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11139   bool saved_in_declarator_p = parser->in_declarator_p;
11140   bool first = true;
11141   tree pushed_scope = NULL_TREE;
11142
11143   while (true)
11144     {
11145       /* Peek at the next token.  */
11146       token = cp_lexer_peek_token (parser->lexer);
11147       if (token->type == CPP_OPEN_PAREN)
11148         {
11149           /* This is either a parameter-declaration-clause, or a
11150              parenthesized declarator. When we know we are parsing a
11151              named declarator, it must be a parenthesized declarator
11152              if FIRST is true. For instance, `(int)' is a
11153              parameter-declaration-clause, with an omitted
11154              direct-abstract-declarator. But `((*))', is a
11155              parenthesized abstract declarator. Finally, when T is a
11156              template parameter `(T)' is a
11157              parameter-declaration-clause, and not a parenthesized
11158              named declarator.
11159
11160              We first try and parse a parameter-declaration-clause,
11161              and then try a nested declarator (if FIRST is true).
11162
11163              It is not an error for it not to be a
11164              parameter-declaration-clause, even when FIRST is
11165              false. Consider,
11166
11167                int i (int);
11168                int i (3);
11169
11170              The first is the declaration of a function while the
11171              second is a the definition of a variable, including its
11172              initializer.
11173
11174              Having seen only the parenthesis, we cannot know which of
11175              these two alternatives should be selected.  Even more
11176              complex are examples like:
11177
11178                int i (int (a));
11179                int i (int (3));
11180
11181              The former is a function-declaration; the latter is a
11182              variable initialization.
11183
11184              Thus again, we try a parameter-declaration-clause, and if
11185              that fails, we back out and return.  */
11186
11187           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11188             {
11189               cp_parameter_declarator *params;
11190               unsigned saved_num_template_parameter_lists;
11191
11192               /* In a member-declarator, the only valid interpretation
11193                  of a parenthesis is the start of a
11194                  parameter-declaration-clause.  (It is invalid to
11195                  initialize a static data member with a parenthesized
11196                  initializer; only the "=" form of initialization is
11197                  permitted.)  */
11198               if (!member_p)
11199                 cp_parser_parse_tentatively (parser);
11200
11201               /* Consume the `('.  */
11202               cp_lexer_consume_token (parser->lexer);
11203               if (first)
11204                 {
11205                   /* If this is going to be an abstract declarator, we're
11206                      in a declarator and we can't have default args.  */
11207                   parser->default_arg_ok_p = false;
11208                   parser->in_declarator_p = true;
11209                 }
11210
11211               /* Inside the function parameter list, surrounding
11212                  template-parameter-lists do not apply.  */
11213               saved_num_template_parameter_lists
11214                 = parser->num_template_parameter_lists;
11215               parser->num_template_parameter_lists = 0;
11216
11217               /* Parse the parameter-declaration-clause.  */
11218               params = cp_parser_parameter_declaration_clause (parser);
11219
11220               parser->num_template_parameter_lists
11221                 = saved_num_template_parameter_lists;
11222
11223               /* If all went well, parse the cv-qualifier-seq and the
11224                  exception-specification.  */
11225               if (member_p || cp_parser_parse_definitely (parser))
11226                 {
11227                   cp_cv_quals cv_quals;
11228                   tree exception_specification;
11229
11230                   if (ctor_dtor_or_conv_p)
11231                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11232                   first = false;
11233                   /* Consume the `)'.  */
11234                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11235
11236                   /* Parse the cv-qualifier-seq.  */
11237                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11238                   /* And the exception-specification.  */
11239                   exception_specification
11240                     = cp_parser_exception_specification_opt (parser);
11241
11242                   /* Create the function-declarator.  */
11243                   declarator = make_call_declarator (declarator,
11244                                                      params,
11245                                                      cv_quals,
11246                                                      exception_specification);
11247                   /* Any subsequent parameter lists are to do with
11248                      return type, so are not those of the declared
11249                      function.  */
11250                   parser->default_arg_ok_p = false;
11251
11252                   /* Repeat the main loop.  */
11253                   continue;
11254                 }
11255             }
11256
11257           /* If this is the first, we can try a parenthesized
11258              declarator.  */
11259           if (first)
11260             {
11261               bool saved_in_type_id_in_expr_p;
11262
11263               parser->default_arg_ok_p = saved_default_arg_ok_p;
11264               parser->in_declarator_p = saved_in_declarator_p;
11265
11266               /* Consume the `('.  */
11267               cp_lexer_consume_token (parser->lexer);
11268               /* Parse the nested declarator.  */
11269               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11270               parser->in_type_id_in_expr_p = true;
11271               declarator
11272                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11273                                         /*parenthesized_p=*/NULL,
11274                                         member_p);
11275               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11276               first = false;
11277               /* Expect a `)'.  */
11278               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11279                 declarator = cp_error_declarator;
11280               if (declarator == cp_error_declarator)
11281                 break;
11282
11283               goto handle_declarator;
11284             }
11285           /* Otherwise, we must be done.  */
11286           else
11287             break;
11288         }
11289       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11290                && token->type == CPP_OPEN_SQUARE)
11291         {
11292           /* Parse an array-declarator.  */
11293           tree bounds;
11294
11295           if (ctor_dtor_or_conv_p)
11296             *ctor_dtor_or_conv_p = 0;
11297
11298           first = false;
11299           parser->default_arg_ok_p = false;
11300           parser->in_declarator_p = true;
11301           /* Consume the `['.  */
11302           cp_lexer_consume_token (parser->lexer);
11303           /* Peek at the next token.  */
11304           token = cp_lexer_peek_token (parser->lexer);
11305           /* If the next token is `]', then there is no
11306              constant-expression.  */
11307           if (token->type != CPP_CLOSE_SQUARE)
11308             {
11309               bool non_constant_p;
11310
11311               bounds
11312                 = cp_parser_constant_expression (parser,
11313                                                  /*allow_non_constant=*/true,
11314                                                  &non_constant_p);
11315               if (!non_constant_p)
11316                 bounds = fold_non_dependent_expr (bounds);
11317               /* Normally, the array bound must be an integral constant
11318                  expression.  However, as an extension, we allow VLAs
11319                  in function scopes.  */
11320               else if (!at_function_scope_p ())
11321                 {
11322                   error ("array bound is not an integer constant");
11323                   bounds = error_mark_node;
11324                 }
11325             }
11326           else
11327             bounds = NULL_TREE;
11328           /* Look for the closing `]'.  */
11329           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11330             {
11331               declarator = cp_error_declarator;
11332               break;
11333             }
11334
11335           declarator = make_array_declarator (declarator, bounds);
11336         }
11337       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11338         {
11339           tree qualifying_scope;
11340           tree unqualified_name;
11341
11342           /* Parse a declarator-id */
11343           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11344             cp_parser_parse_tentatively (parser);
11345           unqualified_name = cp_parser_declarator_id (parser);
11346           qualifying_scope = parser->scope;
11347           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11348             {
11349               if (!cp_parser_parse_definitely (parser))
11350                 unqualified_name = error_mark_node;
11351               else if (qualifying_scope
11352                        || (TREE_CODE (unqualified_name)
11353                            != IDENTIFIER_NODE))
11354                 {
11355                   cp_parser_error (parser, "expected unqualified-id");
11356                   unqualified_name = error_mark_node;
11357                 }
11358             }
11359
11360           if (unqualified_name == error_mark_node)
11361             {
11362               declarator = cp_error_declarator;
11363               break;
11364             }
11365
11366           if (qualifying_scope && at_namespace_scope_p ()
11367               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11368             {
11369               /* In the declaration of a member of a template class
11370                  outside of the class itself, the SCOPE will sometimes
11371                  be a TYPENAME_TYPE.  For example, given:
11372
11373                  template <typename T>
11374                  int S<T>::R::i = 3;
11375
11376                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11377                  this context, we must resolve S<T>::R to an ordinary
11378                  type, rather than a typename type.
11379
11380                  The reason we normally avoid resolving TYPENAME_TYPEs
11381                  is that a specialization of `S' might render
11382                  `S<T>::R' not a type.  However, if `S' is
11383                  specialized, then this `i' will not be used, so there
11384                  is no harm in resolving the types here.  */
11385               tree type;
11386
11387               /* Resolve the TYPENAME_TYPE.  */
11388               type = resolve_typename_type (qualifying_scope,
11389                                             /*only_current_p=*/false);
11390               /* If that failed, the declarator is invalid.  */
11391               if (type == error_mark_node)
11392                 error ("%<%T::%D%> is not a type",
11393                        TYPE_CONTEXT (qualifying_scope),
11394                        TYPE_IDENTIFIER (qualifying_scope));
11395               qualifying_scope = type;
11396             }
11397
11398           declarator = make_id_declarator (qualifying_scope,
11399                                            unqualified_name);
11400           declarator->id_loc = token->location;
11401           if (unqualified_name)
11402             {
11403               tree class_type;
11404
11405               if (qualifying_scope
11406                   && CLASS_TYPE_P (qualifying_scope))
11407                 class_type = qualifying_scope;
11408               else
11409                 class_type = current_class_type;
11410
11411               if (class_type)
11412                 {
11413                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11414                     declarator->u.id.sfk = sfk_destructor;
11415                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11416                     declarator->u.id.sfk = sfk_conversion;
11417                   else if (/* There's no way to declare a constructor
11418                               for an anonymous type, even if the type
11419                               got a name for linkage purposes.  */
11420                            !TYPE_WAS_ANONYMOUS (class_type)
11421                            && (constructor_name_p (unqualified_name,
11422                                                    class_type)
11423                                || (TREE_CODE (unqualified_name) == TYPE_DECL
11424                                    && (same_type_p
11425                                        (TREE_TYPE (unqualified_name),
11426                                         class_type)))))
11427                     declarator->u.id.sfk = sfk_constructor;
11428
11429                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11430                     *ctor_dtor_or_conv_p = -1;
11431                   if (qualifying_scope
11432                       && TREE_CODE (unqualified_name) == TYPE_DECL
11433                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11434                     {
11435                       error ("invalid use of constructor as a template");
11436                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11437                               "the constructor in a qualified name",
11438                               class_type,
11439                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11440                               class_type, class_type);
11441                     }
11442                 }
11443             }
11444
11445         handle_declarator:;
11446           scope = get_scope_of_declarator (declarator);
11447           if (scope)
11448             /* Any names that appear after the declarator-id for a
11449                member are looked up in the containing scope.  */
11450             pushed_scope = push_scope (scope);
11451           parser->in_declarator_p = true;
11452           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11453               || (declarator && declarator->kind == cdk_id))
11454             /* Default args are only allowed on function
11455                declarations.  */
11456             parser->default_arg_ok_p = saved_default_arg_ok_p;
11457           else
11458             parser->default_arg_ok_p = false;
11459
11460           first = false;
11461         }
11462       /* We're done.  */
11463       else
11464         break;
11465     }
11466
11467   /* For an abstract declarator, we might wind up with nothing at this
11468      point.  That's an error; the declarator is not optional.  */
11469   if (!declarator)
11470     cp_parser_error (parser, "expected declarator");
11471
11472   /* If we entered a scope, we must exit it now.  */
11473   if (pushed_scope)
11474     pop_scope (pushed_scope);
11475
11476   parser->default_arg_ok_p = saved_default_arg_ok_p;
11477   parser->in_declarator_p = saved_in_declarator_p;
11478
11479   return declarator;
11480 }
11481
11482 /* Parse a ptr-operator.
11483
11484    ptr-operator:
11485      * cv-qualifier-seq [opt]
11486      &
11487      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11488
11489    GNU Extension:
11490
11491    ptr-operator:
11492      & cv-qualifier-seq [opt]
11493
11494    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11495    Returns ADDR_EXPR if a reference was used.  In the case of a
11496    pointer-to-member, *TYPE is filled in with the TYPE containing the
11497    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11498    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11499    ERROR_MARK if an error occurred.  */
11500
11501 static enum tree_code
11502 cp_parser_ptr_operator (cp_parser* parser,
11503                         tree* type,
11504                         cp_cv_quals *cv_quals)
11505 {
11506   enum tree_code code = ERROR_MARK;
11507   cp_token *token;
11508
11509   /* Assume that it's not a pointer-to-member.  */
11510   *type = NULL_TREE;
11511   /* And that there are no cv-qualifiers.  */
11512   *cv_quals = TYPE_UNQUALIFIED;
11513
11514   /* Peek at the next token.  */
11515   token = cp_lexer_peek_token (parser->lexer);
11516   /* If it's a `*' or `&' we have a pointer or reference.  */
11517   if (token->type == CPP_MULT || token->type == CPP_AND)
11518     {
11519       /* Remember which ptr-operator we were processing.  */
11520       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11521
11522       /* Consume the `*' or `&'.  */
11523       cp_lexer_consume_token (parser->lexer);
11524
11525       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11526          `&', if we are allowing GNU extensions.  (The only qualifier
11527          that can legally appear after `&' is `restrict', but that is
11528          enforced during semantic analysis.  */
11529       if (code == INDIRECT_REF
11530           || cp_parser_allow_gnu_extensions_p (parser))
11531         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11532     }
11533   else
11534     {
11535       /* Try the pointer-to-member case.  */
11536       cp_parser_parse_tentatively (parser);
11537       /* Look for the optional `::' operator.  */
11538       cp_parser_global_scope_opt (parser,
11539                                   /*current_scope_valid_p=*/false);
11540       /* Look for the nested-name specifier.  */
11541       cp_parser_nested_name_specifier (parser,
11542                                        /*typename_keyword_p=*/false,
11543                                        /*check_dependency_p=*/true,
11544                                        /*type_p=*/false,
11545                                        /*is_declaration=*/false);
11546       /* If we found it, and the next token is a `*', then we are
11547          indeed looking at a pointer-to-member operator.  */
11548       if (!cp_parser_error_occurred (parser)
11549           && cp_parser_require (parser, CPP_MULT, "`*'"))
11550         {
11551           /* The type of which the member is a member is given by the
11552              current SCOPE.  */
11553           *type = parser->scope;
11554           /* The next name will not be qualified.  */
11555           parser->scope = NULL_TREE;
11556           parser->qualifying_scope = NULL_TREE;
11557           parser->object_scope = NULL_TREE;
11558           /* Indicate that the `*' operator was used.  */
11559           code = INDIRECT_REF;
11560           /* Look for the optional cv-qualifier-seq.  */
11561           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11562         }
11563       /* If that didn't work we don't have a ptr-operator.  */
11564       if (!cp_parser_parse_definitely (parser))
11565         cp_parser_error (parser, "expected ptr-operator");
11566     }
11567
11568   return code;
11569 }
11570
11571 /* Parse an (optional) cv-qualifier-seq.
11572
11573    cv-qualifier-seq:
11574      cv-qualifier cv-qualifier-seq [opt]
11575
11576    cv-qualifier:
11577      const
11578      volatile
11579
11580    GNU Extension:
11581
11582    cv-qualifier:
11583      __restrict__
11584
11585    Returns a bitmask representing the cv-qualifiers.  */
11586
11587 static cp_cv_quals
11588 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11589 {
11590   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11591
11592   while (true)
11593     {
11594       cp_token *token;
11595       cp_cv_quals cv_qualifier;
11596
11597       /* Peek at the next token.  */
11598       token = cp_lexer_peek_token (parser->lexer);
11599       /* See if it's a cv-qualifier.  */
11600       switch (token->keyword)
11601         {
11602         case RID_CONST:
11603           cv_qualifier = TYPE_QUAL_CONST;
11604           break;
11605
11606         case RID_VOLATILE:
11607           cv_qualifier = TYPE_QUAL_VOLATILE;
11608           break;
11609
11610         case RID_RESTRICT:
11611           cv_qualifier = TYPE_QUAL_RESTRICT;
11612           break;
11613
11614         default:
11615           cv_qualifier = TYPE_UNQUALIFIED;
11616           break;
11617         }
11618
11619       if (!cv_qualifier)
11620         break;
11621
11622       if (cv_quals & cv_qualifier)
11623         {
11624           error ("duplicate cv-qualifier");
11625           cp_lexer_purge_token (parser->lexer);
11626         }
11627       else
11628         {
11629           cp_lexer_consume_token (parser->lexer);
11630           cv_quals |= cv_qualifier;
11631         }
11632     }
11633
11634   return cv_quals;
11635 }
11636
11637 /* Parse a declarator-id.
11638
11639    declarator-id:
11640      id-expression
11641      :: [opt] nested-name-specifier [opt] type-name
11642
11643    In the `id-expression' case, the value returned is as for
11644    cp_parser_id_expression if the id-expression was an unqualified-id.
11645    If the id-expression was a qualified-id, then a SCOPE_REF is
11646    returned.  The first operand is the scope (either a NAMESPACE_DECL
11647    or TREE_TYPE), but the second is still just a representation of an
11648    unqualified-id.  */
11649
11650 static tree
11651 cp_parser_declarator_id (cp_parser* parser)
11652 {
11653   /* The expression must be an id-expression.  Assume that qualified
11654      names are the names of types so that:
11655
11656        template <class T>
11657        int S<T>::R::i = 3;
11658
11659      will work; we must treat `S<T>::R' as the name of a type.
11660      Similarly, assume that qualified names are templates, where
11661      required, so that:
11662
11663        template <class T>
11664        int S<T>::R<T>::i = 3;
11665
11666      will work, too.  */
11667   return cp_parser_id_expression (parser,
11668                                   /*template_keyword_p=*/false,
11669                                   /*check_dependency_p=*/false,
11670                                   /*template_p=*/NULL,
11671                                   /*declarator_p=*/true);
11672 }
11673
11674 /* Parse a type-id.
11675
11676    type-id:
11677      type-specifier-seq abstract-declarator [opt]
11678
11679    Returns the TYPE specified.  */
11680
11681 static tree
11682 cp_parser_type_id (cp_parser* parser)
11683 {
11684   cp_decl_specifier_seq type_specifier_seq;
11685   cp_declarator *abstract_declarator;
11686
11687   /* Parse the type-specifier-seq.  */
11688   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11689                                 &type_specifier_seq);
11690   if (type_specifier_seq.type == error_mark_node)
11691     return error_mark_node;
11692
11693   /* There might or might not be an abstract declarator.  */
11694   cp_parser_parse_tentatively (parser);
11695   /* Look for the declarator.  */
11696   abstract_declarator
11697     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11698                             /*parenthesized_p=*/NULL,
11699                             /*member_p=*/false);
11700   /* Check to see if there really was a declarator.  */
11701   if (!cp_parser_parse_definitely (parser))
11702     abstract_declarator = NULL;
11703
11704   return groktypename (&type_specifier_seq, abstract_declarator);
11705 }
11706
11707 /* Parse a type-specifier-seq.
11708
11709    type-specifier-seq:
11710      type-specifier type-specifier-seq [opt]
11711
11712    GNU extension:
11713
11714    type-specifier-seq:
11715      attributes type-specifier-seq [opt]
11716
11717    If IS_CONDITION is true, we are at the start of a "condition",
11718    e.g., we've just seen "if (".
11719
11720    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11721
11722 static void
11723 cp_parser_type_specifier_seq (cp_parser* parser,
11724                               bool is_condition,
11725                               cp_decl_specifier_seq *type_specifier_seq)
11726 {
11727   bool seen_type_specifier = false;
11728   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11729
11730   /* Clear the TYPE_SPECIFIER_SEQ.  */
11731   clear_decl_specs (type_specifier_seq);
11732
11733   /* Parse the type-specifiers and attributes.  */
11734   while (true)
11735     {
11736       tree type_specifier;
11737       bool is_cv_qualifier;
11738
11739       /* Check for attributes first.  */
11740       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11741         {
11742           type_specifier_seq->attributes =
11743             chainon (type_specifier_seq->attributes,
11744                      cp_parser_attributes_opt (parser));
11745           continue;
11746         }
11747
11748       /* Look for the type-specifier.  */
11749       type_specifier = cp_parser_type_specifier (parser,
11750                                                  flags,
11751                                                  type_specifier_seq,
11752                                                  /*is_declaration=*/false,
11753                                                  NULL,
11754                                                  &is_cv_qualifier);
11755       if (!type_specifier)
11756         {
11757           /* If the first type-specifier could not be found, this is not a
11758              type-specifier-seq at all.  */
11759           if (!seen_type_specifier)
11760             {
11761               cp_parser_error (parser, "expected type-specifier");
11762               type_specifier_seq->type = error_mark_node;
11763               return;
11764             }
11765           /* If subsequent type-specifiers could not be found, the
11766              type-specifier-seq is complete.  */
11767           break;
11768         }
11769
11770       seen_type_specifier = true;
11771       /* The standard says that a condition can be:
11772
11773             type-specifier-seq declarator = assignment-expression
11774
11775          However, given:
11776
11777            struct S {};
11778            if (int S = ...)
11779
11780          we should treat the "S" as a declarator, not as a
11781          type-specifier.  The standard doesn't say that explicitly for
11782          type-specifier-seq, but it does say that for
11783          decl-specifier-seq in an ordinary declaration.  Perhaps it
11784          would be clearer just to allow a decl-specifier-seq here, and
11785          then add a semantic restriction that if any decl-specifiers
11786          that are not type-specifiers appear, the program is invalid.  */
11787       if (is_condition && !is_cv_qualifier)
11788         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11789     }
11790
11791   return;
11792 }
11793
11794 /* Parse a parameter-declaration-clause.
11795
11796    parameter-declaration-clause:
11797      parameter-declaration-list [opt] ... [opt]
11798      parameter-declaration-list , ...
11799
11800    Returns a representation for the parameter declarations.  A return
11801    value of NULL indicates a parameter-declaration-clause consisting
11802    only of an ellipsis.  */
11803
11804 static cp_parameter_declarator *
11805 cp_parser_parameter_declaration_clause (cp_parser* parser)
11806 {
11807   cp_parameter_declarator *parameters;
11808   cp_token *token;
11809   bool ellipsis_p;
11810   bool is_error;
11811
11812   /* Peek at the next token.  */
11813   token = cp_lexer_peek_token (parser->lexer);
11814   /* Check for trivial parameter-declaration-clauses.  */
11815   if (token->type == CPP_ELLIPSIS)
11816     {
11817       /* Consume the `...' token.  */
11818       cp_lexer_consume_token (parser->lexer);
11819       return NULL;
11820     }
11821   else if (token->type == CPP_CLOSE_PAREN)
11822     /* There are no parameters.  */
11823     {
11824 #ifndef NO_IMPLICIT_EXTERN_C
11825       if (in_system_header && current_class_type == NULL
11826           && current_lang_name == lang_name_c)
11827         return NULL;
11828       else
11829 #endif
11830         return no_parameters;
11831     }
11832   /* Check for `(void)', too, which is a special case.  */
11833   else if (token->keyword == RID_VOID
11834            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11835                == CPP_CLOSE_PAREN))
11836     {
11837       /* Consume the `void' token.  */
11838       cp_lexer_consume_token (parser->lexer);
11839       /* There are no parameters.  */
11840       return no_parameters;
11841     }
11842
11843   /* Parse the parameter-declaration-list.  */
11844   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11845   /* If a parse error occurred while parsing the
11846      parameter-declaration-list, then the entire
11847      parameter-declaration-clause is erroneous.  */
11848   if (is_error)
11849     return NULL;
11850
11851   /* Peek at the next token.  */
11852   token = cp_lexer_peek_token (parser->lexer);
11853   /* If it's a `,', the clause should terminate with an ellipsis.  */
11854   if (token->type == CPP_COMMA)
11855     {
11856       /* Consume the `,'.  */
11857       cp_lexer_consume_token (parser->lexer);
11858       /* Expect an ellipsis.  */
11859       ellipsis_p
11860         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11861     }
11862   /* It might also be `...' if the optional trailing `,' was
11863      omitted.  */
11864   else if (token->type == CPP_ELLIPSIS)
11865     {
11866       /* Consume the `...' token.  */
11867       cp_lexer_consume_token (parser->lexer);
11868       /* And remember that we saw it.  */
11869       ellipsis_p = true;
11870     }
11871   else
11872     ellipsis_p = false;
11873
11874   /* Finish the parameter list.  */
11875   if (parameters && ellipsis_p)
11876     parameters->ellipsis_p = true;
11877
11878   return parameters;
11879 }
11880
11881 /* Parse a parameter-declaration-list.
11882
11883    parameter-declaration-list:
11884      parameter-declaration
11885      parameter-declaration-list , parameter-declaration
11886
11887    Returns a representation of the parameter-declaration-list, as for
11888    cp_parser_parameter_declaration_clause.  However, the
11889    `void_list_node' is never appended to the list.  Upon return,
11890    *IS_ERROR will be true iff an error occurred.  */
11891
11892 static cp_parameter_declarator *
11893 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11894 {
11895   cp_parameter_declarator *parameters = NULL;
11896   cp_parameter_declarator **tail = &parameters;
11897
11898   /* Assume all will go well.  */
11899   *is_error = false;
11900
11901   /* Look for more parameters.  */
11902   while (true)
11903     {
11904       cp_parameter_declarator *parameter;
11905       bool parenthesized_p;
11906       /* Parse the parameter.  */
11907       parameter
11908         = cp_parser_parameter_declaration (parser,
11909                                            /*template_parm_p=*/false,
11910                                            &parenthesized_p);
11911
11912       /* If a parse error occurred parsing the parameter declaration,
11913          then the entire parameter-declaration-list is erroneous.  */
11914       if (!parameter)
11915         {
11916           *is_error = true;
11917           parameters = NULL;
11918           break;
11919         }
11920       /* Add the new parameter to the list.  */
11921       *tail = parameter;
11922       tail = &parameter->next;
11923
11924       /* Peek at the next token.  */
11925       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11926           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11927           /* These are for Objective-C++ */
11928           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11929           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11930         /* The parameter-declaration-list is complete.  */
11931         break;
11932       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11933         {
11934           cp_token *token;
11935
11936           /* Peek at the next token.  */
11937           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11938           /* If it's an ellipsis, then the list is complete.  */
11939           if (token->type == CPP_ELLIPSIS)
11940             break;
11941           /* Otherwise, there must be more parameters.  Consume the
11942              `,'.  */
11943           cp_lexer_consume_token (parser->lexer);
11944           /* When parsing something like:
11945
11946                 int i(float f, double d)
11947
11948              we can tell after seeing the declaration for "f" that we
11949              are not looking at an initialization of a variable "i",
11950              but rather at the declaration of a function "i".
11951
11952              Due to the fact that the parsing of template arguments
11953              (as specified to a template-id) requires backtracking we
11954              cannot use this technique when inside a template argument
11955              list.  */
11956           if (!parser->in_template_argument_list_p
11957               && !parser->in_type_id_in_expr_p
11958               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11959               /* However, a parameter-declaration of the form
11960                  "foat(f)" (which is a valid declaration of a
11961                  parameter "f") can also be interpreted as an
11962                  expression (the conversion of "f" to "float").  */
11963               && !parenthesized_p)
11964             cp_parser_commit_to_tentative_parse (parser);
11965         }
11966       else
11967         {
11968           cp_parser_error (parser, "expected %<,%> or %<...%>");
11969           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11970             cp_parser_skip_to_closing_parenthesis (parser,
11971                                                    /*recovering=*/true,
11972                                                    /*or_comma=*/false,
11973                                                    /*consume_paren=*/false);
11974           break;
11975         }
11976     }
11977
11978   return parameters;
11979 }
11980
11981 /* Parse a parameter declaration.
11982
11983    parameter-declaration:
11984      decl-specifier-seq declarator
11985      decl-specifier-seq declarator = assignment-expression
11986      decl-specifier-seq abstract-declarator [opt]
11987      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11988
11989    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11990    declares a template parameter.  (In that case, a non-nested `>'
11991    token encountered during the parsing of the assignment-expression
11992    is not interpreted as a greater-than operator.)
11993
11994    Returns a representation of the parameter, or NULL if an error
11995    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11996    true iff the declarator is of the form "(p)".  */
11997
11998 static cp_parameter_declarator *
11999 cp_parser_parameter_declaration (cp_parser *parser,
12000                                  bool template_parm_p,
12001                                  bool *parenthesized_p)
12002 {
12003   int declares_class_or_enum;
12004   bool greater_than_is_operator_p;
12005   cp_decl_specifier_seq decl_specifiers;
12006   cp_declarator *declarator;
12007   tree default_argument;
12008   cp_token *token;
12009   const char *saved_message;
12010
12011   /* In a template parameter, `>' is not an operator.
12012
12013      [temp.param]
12014
12015      When parsing a default template-argument for a non-type
12016      template-parameter, the first non-nested `>' is taken as the end
12017      of the template parameter-list rather than a greater-than
12018      operator.  */
12019   greater_than_is_operator_p = !template_parm_p;
12020
12021   /* Type definitions may not appear in parameter types.  */
12022   saved_message = parser->type_definition_forbidden_message;
12023   parser->type_definition_forbidden_message
12024     = "types may not be defined in parameter types";
12025
12026   /* Parse the declaration-specifiers.  */
12027   cp_parser_decl_specifier_seq (parser,
12028                                 CP_PARSER_FLAGS_NONE,
12029                                 &decl_specifiers,
12030                                 &declares_class_or_enum);
12031   /* If an error occurred, there's no reason to attempt to parse the
12032      rest of the declaration.  */
12033   if (cp_parser_error_occurred (parser))
12034     {
12035       parser->type_definition_forbidden_message = saved_message;
12036       return NULL;
12037     }
12038
12039   /* Peek at the next token.  */
12040   token = cp_lexer_peek_token (parser->lexer);
12041   /* If the next token is a `)', `,', `=', `>', or `...', then there
12042      is no declarator.  */
12043   if (token->type == CPP_CLOSE_PAREN
12044       || token->type == CPP_COMMA
12045       || token->type == CPP_EQ
12046       || token->type == CPP_ELLIPSIS
12047       || token->type == CPP_GREATER)
12048     {
12049       declarator = NULL;
12050       if (parenthesized_p)
12051         *parenthesized_p = false;
12052     }
12053   /* Otherwise, there should be a declarator.  */
12054   else
12055     {
12056       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12057       parser->default_arg_ok_p = false;
12058
12059       /* After seeing a decl-specifier-seq, if the next token is not a
12060          "(", there is no possibility that the code is a valid
12061          expression.  Therefore, if parsing tentatively, we commit at
12062          this point.  */
12063       if (!parser->in_template_argument_list_p
12064           /* In an expression context, having seen:
12065
12066                (int((char ...
12067
12068              we cannot be sure whether we are looking at a
12069              function-type (taking a "char" as a parameter) or a cast
12070              of some object of type "char" to "int".  */
12071           && !parser->in_type_id_in_expr_p
12072           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12073           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12074         cp_parser_commit_to_tentative_parse (parser);
12075       /* Parse the declarator.  */
12076       declarator = cp_parser_declarator (parser,
12077                                          CP_PARSER_DECLARATOR_EITHER,
12078                                          /*ctor_dtor_or_conv_p=*/NULL,
12079                                          parenthesized_p,
12080                                          /*member_p=*/false);
12081       parser->default_arg_ok_p = saved_default_arg_ok_p;
12082       /* After the declarator, allow more attributes.  */
12083       decl_specifiers.attributes
12084         = chainon (decl_specifiers.attributes,
12085                    cp_parser_attributes_opt (parser));
12086     }
12087
12088   /* The restriction on defining new types applies only to the type
12089      of the parameter, not to the default argument.  */
12090   parser->type_definition_forbidden_message = saved_message;
12091
12092   /* If the next token is `=', then process a default argument.  */
12093   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12094     {
12095       bool saved_greater_than_is_operator_p;
12096       /* Consume the `='.  */
12097       cp_lexer_consume_token (parser->lexer);
12098
12099       /* If we are defining a class, then the tokens that make up the
12100          default argument must be saved and processed later.  */
12101       if (!template_parm_p && at_class_scope_p ()
12102           && TYPE_BEING_DEFINED (current_class_type))
12103         {
12104           unsigned depth = 0;
12105           cp_token *first_token;
12106           cp_token *token;
12107
12108           /* Add tokens until we have processed the entire default
12109              argument.  We add the range [first_token, token).  */
12110           first_token = cp_lexer_peek_token (parser->lexer);
12111           while (true)
12112             {
12113               bool done = false;
12114
12115               /* Peek at the next token.  */
12116               token = cp_lexer_peek_token (parser->lexer);
12117               /* What we do depends on what token we have.  */
12118               switch (token->type)
12119                 {
12120                   /* In valid code, a default argument must be
12121                      immediately followed by a `,' `)', or `...'.  */
12122                 case CPP_COMMA:
12123                 case CPP_CLOSE_PAREN:
12124                 case CPP_ELLIPSIS:
12125                   /* If we run into a non-nested `;', `}', or `]',
12126                      then the code is invalid -- but the default
12127                      argument is certainly over.  */
12128                 case CPP_SEMICOLON:
12129                 case CPP_CLOSE_BRACE:
12130                 case CPP_CLOSE_SQUARE:
12131                   if (depth == 0)
12132                     done = true;
12133                   /* Update DEPTH, if necessary.  */
12134                   else if (token->type == CPP_CLOSE_PAREN
12135                            || token->type == CPP_CLOSE_BRACE
12136                            || token->type == CPP_CLOSE_SQUARE)
12137                     --depth;
12138                   break;
12139
12140                 case CPP_OPEN_PAREN:
12141                 case CPP_OPEN_SQUARE:
12142                 case CPP_OPEN_BRACE:
12143                   ++depth;
12144                   break;
12145
12146                 case CPP_GREATER:
12147                   /* If we see a non-nested `>', and `>' is not an
12148                      operator, then it marks the end of the default
12149                      argument.  */
12150                   if (!depth && !greater_than_is_operator_p)
12151                     done = true;
12152                   break;
12153
12154                   /* If we run out of tokens, issue an error message.  */
12155                 case CPP_EOF:
12156                   error ("file ends in default argument");
12157                   done = true;
12158                   break;
12159
12160                 case CPP_NAME:
12161                 case CPP_SCOPE:
12162                   /* In these cases, we should look for template-ids.
12163                      For example, if the default argument is
12164                      `X<int, double>()', we need to do name lookup to
12165                      figure out whether or not `X' is a template; if
12166                      so, the `,' does not end the default argument.
12167
12168                      That is not yet done.  */
12169                   break;
12170
12171                 default:
12172                   break;
12173                 }
12174
12175               /* If we've reached the end, stop.  */
12176               if (done)
12177                 break;
12178
12179               /* Add the token to the token block.  */
12180               token = cp_lexer_consume_token (parser->lexer);
12181             }
12182
12183           /* Create a DEFAULT_ARG to represented the unparsed default
12184              argument.  */
12185           default_argument = make_node (DEFAULT_ARG);
12186           DEFARG_TOKENS (default_argument)
12187             = cp_token_cache_new (first_token, token);
12188           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12189         }
12190       /* Outside of a class definition, we can just parse the
12191          assignment-expression.  */
12192       else
12193         {
12194           bool saved_local_variables_forbidden_p;
12195
12196           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12197              set correctly.  */
12198           saved_greater_than_is_operator_p
12199             = parser->greater_than_is_operator_p;
12200           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12201           /* Local variable names (and the `this' keyword) may not
12202              appear in a default argument.  */
12203           saved_local_variables_forbidden_p
12204             = parser->local_variables_forbidden_p;
12205           parser->local_variables_forbidden_p = true;
12206           /* Parse the assignment-expression.  */
12207           default_argument
12208             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12209           /* Restore saved state.  */
12210           parser->greater_than_is_operator_p
12211             = saved_greater_than_is_operator_p;
12212           parser->local_variables_forbidden_p
12213             = saved_local_variables_forbidden_p;
12214         }
12215       if (!parser->default_arg_ok_p)
12216         {
12217           if (!flag_pedantic_errors)
12218             warning (0, "deprecated use of default argument for parameter of non-function");
12219           else
12220             {
12221               error ("default arguments are only permitted for function parameters");
12222               default_argument = NULL_TREE;
12223             }
12224         }
12225     }
12226   else
12227     default_argument = NULL_TREE;
12228
12229   return make_parameter_declarator (&decl_specifiers,
12230                                     declarator,
12231                                     default_argument);
12232 }
12233
12234 /* Parse a function-body.
12235
12236    function-body:
12237      compound_statement  */
12238
12239 static void
12240 cp_parser_function_body (cp_parser *parser)
12241 {
12242   cp_parser_compound_statement (parser, NULL, false);
12243 }
12244
12245 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12246    true if a ctor-initializer was present.  */
12247
12248 static bool
12249 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12250 {
12251   tree body;
12252   bool ctor_initializer_p;
12253
12254   /* Begin the function body.  */
12255   body = begin_function_body ();
12256   /* Parse the optional ctor-initializer.  */
12257   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12258   /* Parse the function-body.  */
12259   cp_parser_function_body (parser);
12260   /* Finish the function body.  */
12261   finish_function_body (body);
12262
12263   return ctor_initializer_p;
12264 }
12265
12266 /* Parse an initializer.
12267
12268    initializer:
12269      = initializer-clause
12270      ( expression-list )
12271
12272    Returns an expression representing the initializer.  If no
12273    initializer is present, NULL_TREE is returned.
12274
12275    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12276    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12277    set to FALSE if there is no initializer present.  If there is an
12278    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12279    is set to true; otherwise it is set to false.  */
12280
12281 static tree
12282 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12283                        bool* non_constant_p)
12284 {
12285   cp_token *token;
12286   tree init;
12287
12288   /* Peek at the next token.  */
12289   token = cp_lexer_peek_token (parser->lexer);
12290
12291   /* Let our caller know whether or not this initializer was
12292      parenthesized.  */
12293   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12294   /* Assume that the initializer is constant.  */
12295   *non_constant_p = false;
12296
12297   if (token->type == CPP_EQ)
12298     {
12299       /* Consume the `='.  */
12300       cp_lexer_consume_token (parser->lexer);
12301       /* Parse the initializer-clause.  */
12302       init = cp_parser_initializer_clause (parser, non_constant_p);
12303     }
12304   else if (token->type == CPP_OPEN_PAREN)
12305     init = cp_parser_parenthesized_expression_list (parser, false,
12306                                                     /*cast_p=*/false,
12307                                                     non_constant_p);
12308   else
12309     {
12310       /* Anything else is an error.  */
12311       cp_parser_error (parser, "expected initializer");
12312       init = error_mark_node;
12313     }
12314
12315   return init;
12316 }
12317
12318 /* Parse an initializer-clause.
12319
12320    initializer-clause:
12321      assignment-expression
12322      { initializer-list , [opt] }
12323      { }
12324
12325    Returns an expression representing the initializer.
12326
12327    If the `assignment-expression' production is used the value
12328    returned is simply a representation for the expression.
12329
12330    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12331    the elements of the initializer-list (or NULL, if the last
12332    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12333    NULL_TREE.  There is no way to detect whether or not the optional
12334    trailing `,' was provided.  NON_CONSTANT_P is as for
12335    cp_parser_initializer.  */
12336
12337 static tree
12338 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12339 {
12340   tree initializer;
12341
12342   /* Assume the expression is constant.  */
12343   *non_constant_p = false;
12344
12345   /* If it is not a `{', then we are looking at an
12346      assignment-expression.  */
12347   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12348     {
12349       initializer
12350         = cp_parser_constant_expression (parser,
12351                                         /*allow_non_constant_p=*/true,
12352                                         non_constant_p);
12353       if (!*non_constant_p)
12354         initializer = fold_non_dependent_expr (initializer);
12355     }
12356   else
12357     {
12358       /* Consume the `{' token.  */
12359       cp_lexer_consume_token (parser->lexer);
12360       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12361       initializer = make_node (CONSTRUCTOR);
12362       /* If it's not a `}', then there is a non-trivial initializer.  */
12363       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12364         {
12365           /* Parse the initializer list.  */
12366           CONSTRUCTOR_ELTS (initializer)
12367             = cp_parser_initializer_list (parser, non_constant_p);
12368           /* A trailing `,' token is allowed.  */
12369           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12370             cp_lexer_consume_token (parser->lexer);
12371         }
12372       /* Now, there should be a trailing `}'.  */
12373       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12374     }
12375
12376   return initializer;
12377 }
12378
12379 /* Parse an initializer-list.
12380
12381    initializer-list:
12382      initializer-clause
12383      initializer-list , initializer-clause
12384
12385    GNU Extension:
12386
12387    initializer-list:
12388      identifier : initializer-clause
12389      initializer-list, identifier : initializer-clause
12390
12391    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12392    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12393    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12394    as for cp_parser_initializer.  */
12395
12396 static VEC(constructor_elt,gc) *
12397 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12398 {
12399   VEC(constructor_elt,gc) *v = NULL;
12400
12401   /* Assume all of the expressions are constant.  */
12402   *non_constant_p = false;
12403
12404   /* Parse the rest of the list.  */
12405   while (true)
12406     {
12407       cp_token *token;
12408       tree identifier;
12409       tree initializer;
12410       bool clause_non_constant_p;
12411
12412       /* If the next token is an identifier and the following one is a
12413          colon, we are looking at the GNU designated-initializer
12414          syntax.  */
12415       if (cp_parser_allow_gnu_extensions_p (parser)
12416           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12417           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12418         {
12419           /* Consume the identifier.  */
12420           identifier = cp_lexer_consume_token (parser->lexer)->value;
12421           /* Consume the `:'.  */
12422           cp_lexer_consume_token (parser->lexer);
12423         }
12424       else
12425         identifier = NULL_TREE;
12426
12427       /* Parse the initializer.  */
12428       initializer = cp_parser_initializer_clause (parser,
12429                                                   &clause_non_constant_p);
12430       /* If any clause is non-constant, so is the entire initializer.  */
12431       if (clause_non_constant_p)
12432         *non_constant_p = true;
12433
12434       /* Add it to the vector.  */
12435       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12436
12437       /* If the next token is not a comma, we have reached the end of
12438          the list.  */
12439       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12440         break;
12441
12442       /* Peek at the next token.  */
12443       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12444       /* If the next token is a `}', then we're still done.  An
12445          initializer-clause can have a trailing `,' after the
12446          initializer-list and before the closing `}'.  */
12447       if (token->type == CPP_CLOSE_BRACE)
12448         break;
12449
12450       /* Consume the `,' token.  */
12451       cp_lexer_consume_token (parser->lexer);
12452     }
12453
12454   return v;
12455 }
12456
12457 /* Classes [gram.class] */
12458
12459 /* Parse a class-name.
12460
12461    class-name:
12462      identifier
12463      template-id
12464
12465    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12466    to indicate that names looked up in dependent types should be
12467    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12468    keyword has been used to indicate that the name that appears next
12469    is a template.  TAG_TYPE indicates the explicit tag given before
12470    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12471    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12472    is the class being defined in a class-head.
12473
12474    Returns the TYPE_DECL representing the class.  */
12475
12476 static tree
12477 cp_parser_class_name (cp_parser *parser,
12478                       bool typename_keyword_p,
12479                       bool template_keyword_p,
12480                       enum tag_types tag_type,
12481                       bool check_dependency_p,
12482                       bool class_head_p,
12483                       bool is_declaration)
12484 {
12485   tree decl;
12486   tree scope;
12487   bool typename_p;
12488   cp_token *token;
12489
12490   /* All class-names start with an identifier.  */
12491   token = cp_lexer_peek_token (parser->lexer);
12492   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12493     {
12494       cp_parser_error (parser, "expected class-name");
12495       return error_mark_node;
12496     }
12497
12498   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12499      to a template-id, so we save it here.  */
12500   scope = parser->scope;
12501   if (scope == error_mark_node)
12502     return error_mark_node;
12503
12504   /* Any name names a type if we're following the `typename' keyword
12505      in a qualified name where the enclosing scope is type-dependent.  */
12506   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12507                 && dependent_type_p (scope));
12508   /* Handle the common case (an identifier, but not a template-id)
12509      efficiently.  */
12510   if (token->type == CPP_NAME
12511       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12512     {
12513       tree identifier;
12514
12515       /* Look for the identifier.  */
12516       identifier = cp_parser_identifier (parser);
12517       /* If the next token isn't an identifier, we are certainly not
12518          looking at a class-name.  */
12519       if (identifier == error_mark_node)
12520         decl = error_mark_node;
12521       /* If we know this is a type-name, there's no need to look it
12522          up.  */
12523       else if (typename_p)
12524         decl = identifier;
12525       else
12526         {
12527           /* If the next token is a `::', then the name must be a type
12528              name.
12529
12530              [basic.lookup.qual]
12531
12532              During the lookup for a name preceding the :: scope
12533              resolution operator, object, function, and enumerator
12534              names are ignored.  */
12535           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12536             tag_type = typename_type;
12537           /* Look up the name.  */
12538           decl = cp_parser_lookup_name (parser, identifier,
12539                                         tag_type,
12540                                         /*is_template=*/false,
12541                                         /*is_namespace=*/false,
12542                                         check_dependency_p,
12543                                         /*ambiguous_p=*/NULL);
12544         }
12545     }
12546   else
12547     {
12548       /* Try a template-id.  */
12549       decl = cp_parser_template_id (parser, template_keyword_p,
12550                                     check_dependency_p,
12551                                     is_declaration);
12552       if (decl == error_mark_node)
12553         return error_mark_node;
12554     }
12555
12556   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12557
12558   /* If this is a typename, create a TYPENAME_TYPE.  */
12559   if (typename_p && decl != error_mark_node)
12560     {
12561       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12562       if (decl != error_mark_node)
12563         decl = TYPE_NAME (decl);
12564     }
12565
12566   /* Check to see that it is really the name of a class.  */
12567   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12568       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12569       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12570     /* Situations like this:
12571
12572          template <typename T> struct A {
12573            typename T::template X<int>::I i;
12574          };
12575
12576        are problematic.  Is `T::template X<int>' a class-name?  The
12577        standard does not seem to be definitive, but there is no other
12578        valid interpretation of the following `::'.  Therefore, those
12579        names are considered class-names.  */
12580     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12581   else if (decl == error_mark_node
12582            || TREE_CODE (decl) != TYPE_DECL
12583            || TREE_TYPE (decl) == error_mark_node
12584            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12585     {
12586       cp_parser_error (parser, "expected class-name");
12587       return error_mark_node;
12588     }
12589
12590   return decl;
12591 }
12592
12593 /* Parse a class-specifier.
12594
12595    class-specifier:
12596      class-head { member-specification [opt] }
12597
12598    Returns the TREE_TYPE representing the class.  */
12599
12600 static tree
12601 cp_parser_class_specifier (cp_parser* parser)
12602 {
12603   cp_token *token;
12604   tree type;
12605   tree attributes = NULL_TREE;
12606   int has_trailing_semicolon;
12607   bool nested_name_specifier_p;
12608   unsigned saved_num_template_parameter_lists;
12609   tree old_scope = NULL_TREE;
12610   tree scope = NULL_TREE;
12611
12612   push_deferring_access_checks (dk_no_deferred);
12613
12614   /* Parse the class-head.  */
12615   type = cp_parser_class_head (parser,
12616                                &nested_name_specifier_p,
12617                                &attributes);
12618   /* If the class-head was a semantic disaster, skip the entire body
12619      of the class.  */
12620   if (!type)
12621     {
12622       cp_parser_skip_to_end_of_block_or_statement (parser);
12623       pop_deferring_access_checks ();
12624       return error_mark_node;
12625     }
12626
12627   /* Look for the `{'.  */
12628   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12629     {
12630       pop_deferring_access_checks ();
12631       return error_mark_node;
12632     }
12633
12634   /* Issue an error message if type-definitions are forbidden here.  */
12635   cp_parser_check_type_definition (parser);
12636   /* Remember that we are defining one more class.  */
12637   ++parser->num_classes_being_defined;
12638   /* Inside the class, surrounding template-parameter-lists do not
12639      apply.  */
12640   saved_num_template_parameter_lists
12641     = parser->num_template_parameter_lists;
12642   parser->num_template_parameter_lists = 0;
12643
12644   /* Start the class.  */
12645   if (nested_name_specifier_p)
12646     {
12647       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12648       old_scope = push_inner_scope (scope);
12649     }
12650   type = begin_class_definition (type);
12651
12652   if (type == error_mark_node)
12653     /* If the type is erroneous, skip the entire body of the class.  */
12654     cp_parser_skip_to_closing_brace (parser);
12655   else
12656     /* Parse the member-specification.  */
12657     cp_parser_member_specification_opt (parser);
12658
12659   /* Look for the trailing `}'.  */
12660   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12661   /* We get better error messages by noticing a common problem: a
12662      missing trailing `;'.  */
12663   token = cp_lexer_peek_token (parser->lexer);
12664   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12665   /* Look for trailing attributes to apply to this class.  */
12666   if (cp_parser_allow_gnu_extensions_p (parser))
12667     {
12668       tree sub_attr = cp_parser_attributes_opt (parser);
12669       attributes = chainon (attributes, sub_attr);
12670     }
12671   if (type != error_mark_node)
12672     type = finish_struct (type, attributes);
12673   if (nested_name_specifier_p)
12674     pop_inner_scope (old_scope, scope);
12675   /* If this class is not itself within the scope of another class,
12676      then we need to parse the bodies of all of the queued function
12677      definitions.  Note that the queued functions defined in a class
12678      are not always processed immediately following the
12679      class-specifier for that class.  Consider:
12680
12681        struct A {
12682          struct B { void f() { sizeof (A); } };
12683        };
12684
12685      If `f' were processed before the processing of `A' were
12686      completed, there would be no way to compute the size of `A'.
12687      Note that the nesting we are interested in here is lexical --
12688      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12689      for:
12690
12691        struct A { struct B; };
12692        struct A::B { void f() { } };
12693
12694      there is no need to delay the parsing of `A::B::f'.  */
12695   if (--parser->num_classes_being_defined == 0)
12696     {
12697       tree queue_entry;
12698       tree fn;
12699       tree class_type = NULL_TREE;
12700       tree pushed_scope = NULL_TREE;
12701  
12702       /* In a first pass, parse default arguments to the functions.
12703          Then, in a second pass, parse the bodies of the functions.
12704          This two-phased approach handles cases like:
12705
12706             struct S {
12707               void f() { g(); }
12708               void g(int i = 3);
12709             };
12710
12711          */
12712       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12713              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12714            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12715            TREE_PURPOSE (parser->unparsed_functions_queues)
12716              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12717         {
12718           fn = TREE_VALUE (queue_entry);
12719           /* If there are default arguments that have not yet been processed,
12720              take care of them now.  */
12721           if (class_type != TREE_PURPOSE (queue_entry))
12722             {
12723               if (pushed_scope)
12724                 pop_scope (pushed_scope);
12725               class_type = TREE_PURPOSE (queue_entry);
12726               pushed_scope = push_scope (class_type);
12727             }
12728           /* Make sure that any template parameters are in scope.  */
12729           maybe_begin_member_template_processing (fn);
12730           /* Parse the default argument expressions.  */
12731           cp_parser_late_parsing_default_args (parser, fn);
12732           /* Remove any template parameters from the symbol table.  */
12733           maybe_end_member_template_processing ();
12734         }
12735       if (pushed_scope)
12736         pop_scope (pushed_scope);
12737       /* Now parse the body of the functions.  */
12738       for (TREE_VALUE (parser->unparsed_functions_queues)
12739              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12740            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12741            TREE_VALUE (parser->unparsed_functions_queues)
12742              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12743         {
12744           /* Figure out which function we need to process.  */
12745           fn = TREE_VALUE (queue_entry);
12746           /* Parse the function.  */
12747           cp_parser_late_parsing_for_member (parser, fn);
12748         }
12749     }
12750
12751   /* Put back any saved access checks.  */
12752   pop_deferring_access_checks ();
12753
12754   /* Restore the count of active template-parameter-lists.  */
12755   parser->num_template_parameter_lists
12756     = saved_num_template_parameter_lists;
12757
12758   return type;
12759 }
12760
12761 /* Parse a class-head.
12762
12763    class-head:
12764      class-key identifier [opt] base-clause [opt]
12765      class-key nested-name-specifier identifier base-clause [opt]
12766      class-key nested-name-specifier [opt] template-id
12767        base-clause [opt]
12768
12769    GNU Extensions:
12770      class-key attributes identifier [opt] base-clause [opt]
12771      class-key attributes nested-name-specifier identifier base-clause [opt]
12772      class-key attributes nested-name-specifier [opt] template-id
12773        base-clause [opt]
12774
12775    Returns the TYPE of the indicated class.  Sets
12776    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12777    involving a nested-name-specifier was used, and FALSE otherwise.
12778
12779    Returns error_mark_node if this is not a class-head.
12780
12781    Returns NULL_TREE if the class-head is syntactically valid, but
12782    semantically invalid in a way that means we should skip the entire
12783    body of the class.  */
12784
12785 static tree
12786 cp_parser_class_head (cp_parser* parser,
12787                       bool* nested_name_specifier_p,
12788                       tree *attributes_p)
12789 {
12790   tree nested_name_specifier;
12791   enum tag_types class_key;
12792   tree id = NULL_TREE;
12793   tree type = NULL_TREE;
12794   tree attributes;
12795   bool template_id_p = false;
12796   bool qualified_p = false;
12797   bool invalid_nested_name_p = false;
12798   bool invalid_explicit_specialization_p = false;
12799   tree pushed_scope = NULL_TREE;
12800   unsigned num_templates;
12801   tree bases;
12802
12803   /* Assume no nested-name-specifier will be present.  */
12804   *nested_name_specifier_p = false;
12805   /* Assume no template parameter lists will be used in defining the
12806      type.  */
12807   num_templates = 0;
12808
12809   /* Look for the class-key.  */
12810   class_key = cp_parser_class_key (parser);
12811   if (class_key == none_type)
12812     return error_mark_node;
12813
12814   /* Parse the attributes.  */
12815   attributes = cp_parser_attributes_opt (parser);
12816
12817   /* If the next token is `::', that is invalid -- but sometimes
12818      people do try to write:
12819
12820        struct ::S {};
12821
12822      Handle this gracefully by accepting the extra qualifier, and then
12823      issuing an error about it later if this really is a
12824      class-head.  If it turns out just to be an elaborated type
12825      specifier, remain silent.  */
12826   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12827     qualified_p = true;
12828
12829   push_deferring_access_checks (dk_no_check);
12830
12831   /* Determine the name of the class.  Begin by looking for an
12832      optional nested-name-specifier.  */
12833   nested_name_specifier
12834     = cp_parser_nested_name_specifier_opt (parser,
12835                                            /*typename_keyword_p=*/false,
12836                                            /*check_dependency_p=*/false,
12837                                            /*type_p=*/false,
12838                                            /*is_declaration=*/false);
12839   /* If there was a nested-name-specifier, then there *must* be an
12840      identifier.  */
12841   if (nested_name_specifier)
12842     {
12843       /* Although the grammar says `identifier', it really means
12844          `class-name' or `template-name'.  You are only allowed to
12845          define a class that has already been declared with this
12846          syntax.
12847
12848          The proposed resolution for Core Issue 180 says that whever
12849          you see `class T::X' you should treat `X' as a type-name.
12850
12851          It is OK to define an inaccessible class; for example:
12852
12853            class A { class B; };
12854            class A::B {};
12855
12856          We do not know if we will see a class-name, or a
12857          template-name.  We look for a class-name first, in case the
12858          class-name is a template-id; if we looked for the
12859          template-name first we would stop after the template-name.  */
12860       cp_parser_parse_tentatively (parser);
12861       type = cp_parser_class_name (parser,
12862                                    /*typename_keyword_p=*/false,
12863                                    /*template_keyword_p=*/false,
12864                                    class_type,
12865                                    /*check_dependency_p=*/false,
12866                                    /*class_head_p=*/true,
12867                                    /*is_declaration=*/false);
12868       /* If that didn't work, ignore the nested-name-specifier.  */
12869       if (!cp_parser_parse_definitely (parser))
12870         {
12871           invalid_nested_name_p = true;
12872           id = cp_parser_identifier (parser);
12873           if (id == error_mark_node)
12874             id = NULL_TREE;
12875         }
12876       /* If we could not find a corresponding TYPE, treat this
12877          declaration like an unqualified declaration.  */
12878       if (type == error_mark_node)
12879         nested_name_specifier = NULL_TREE;
12880       /* Otherwise, count the number of templates used in TYPE and its
12881          containing scopes.  */
12882       else
12883         {
12884           tree scope;
12885
12886           for (scope = TREE_TYPE (type);
12887                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12888                scope = (TYPE_P (scope)
12889                         ? TYPE_CONTEXT (scope)
12890                         : DECL_CONTEXT (scope)))
12891             if (TYPE_P (scope)
12892                 && CLASS_TYPE_P (scope)
12893                 && CLASSTYPE_TEMPLATE_INFO (scope)
12894                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12895                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12896               ++num_templates;
12897         }
12898     }
12899   /* Otherwise, the identifier is optional.  */
12900   else
12901     {
12902       /* We don't know whether what comes next is a template-id,
12903          an identifier, or nothing at all.  */
12904       cp_parser_parse_tentatively (parser);
12905       /* Check for a template-id.  */
12906       id = cp_parser_template_id (parser,
12907                                   /*template_keyword_p=*/false,
12908                                   /*check_dependency_p=*/true,
12909                                   /*is_declaration=*/true);
12910       /* If that didn't work, it could still be an identifier.  */
12911       if (!cp_parser_parse_definitely (parser))
12912         {
12913           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12914             id = cp_parser_identifier (parser);
12915           else
12916             id = NULL_TREE;
12917         }
12918       else
12919         {
12920           template_id_p = true;
12921           ++num_templates;
12922         }
12923     }
12924
12925   pop_deferring_access_checks ();
12926
12927   if (id)
12928     cp_parser_check_for_invalid_template_id (parser, id);
12929
12930   /* If it's not a `:' or a `{' then we can't really be looking at a
12931      class-head, since a class-head only appears as part of a
12932      class-specifier.  We have to detect this situation before calling
12933      xref_tag, since that has irreversible side-effects.  */
12934   if (!cp_parser_next_token_starts_class_definition_p (parser))
12935     {
12936       cp_parser_error (parser, "expected %<{%> or %<:%>");
12937       return error_mark_node;
12938     }
12939
12940   /* At this point, we're going ahead with the class-specifier, even
12941      if some other problem occurs.  */
12942   cp_parser_commit_to_tentative_parse (parser);
12943   /* Issue the error about the overly-qualified name now.  */
12944   if (qualified_p)
12945     cp_parser_error (parser,
12946                      "global qualification of class name is invalid");
12947   else if (invalid_nested_name_p)
12948     cp_parser_error (parser,
12949                      "qualified name does not name a class");
12950   else if (nested_name_specifier)
12951     {
12952       tree scope;
12953
12954       /* Reject typedef-names in class heads.  */
12955       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12956         {
12957           error ("invalid class name in declaration of %qD", type);
12958           type = NULL_TREE;
12959           goto done;
12960         }
12961
12962       /* Figure out in what scope the declaration is being placed.  */
12963       scope = current_scope ();
12964       /* If that scope does not contain the scope in which the
12965          class was originally declared, the program is invalid.  */
12966       if (scope && !is_ancestor (scope, nested_name_specifier))
12967         {
12968           error ("declaration of %qD in %qD which does not enclose %qD",
12969                  type, scope, nested_name_specifier);
12970           type = NULL_TREE;
12971           goto done;
12972         }
12973       /* [dcl.meaning]
12974
12975          A declarator-id shall not be qualified exception of the
12976          definition of a ... nested class outside of its class
12977          ... [or] a the definition or explicit instantiation of a
12978          class member of a namespace outside of its namespace.  */
12979       if (scope == nested_name_specifier)
12980         {
12981           pedwarn ("extra qualification ignored");
12982           nested_name_specifier = NULL_TREE;
12983           num_templates = 0;
12984         }
12985     }
12986   /* An explicit-specialization must be preceded by "template <>".  If
12987      it is not, try to recover gracefully.  */
12988   if (at_namespace_scope_p ()
12989       && parser->num_template_parameter_lists == 0
12990       && template_id_p)
12991     {
12992       error ("an explicit specialization must be preceded by %<template <>%>");
12993       invalid_explicit_specialization_p = true;
12994       /* Take the same action that would have been taken by
12995          cp_parser_explicit_specialization.  */
12996       ++parser->num_template_parameter_lists;
12997       begin_specialization ();
12998     }
12999   /* There must be no "return" statements between this point and the
13000      end of this function; set "type "to the correct return value and
13001      use "goto done;" to return.  */
13002   /* Make sure that the right number of template parameters were
13003      present.  */
13004   if (!cp_parser_check_template_parameters (parser, num_templates))
13005     {
13006       /* If something went wrong, there is no point in even trying to
13007          process the class-definition.  */
13008       type = NULL_TREE;
13009       goto done;
13010     }
13011
13012   /* Look up the type.  */
13013   if (template_id_p)
13014     {
13015       type = TREE_TYPE (id);
13016       maybe_process_partial_specialization (type);
13017       if (nested_name_specifier)
13018         pushed_scope = push_scope (nested_name_specifier);
13019     }
13020   else if (nested_name_specifier)
13021     {
13022       tree class_type;
13023
13024       /* Given:
13025
13026             template <typename T> struct S { struct T };
13027             template <typename T> struct S<T>::T { };
13028
13029          we will get a TYPENAME_TYPE when processing the definition of
13030          `S::T'.  We need to resolve it to the actual type before we
13031          try to define it.  */
13032       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13033         {
13034           class_type = resolve_typename_type (TREE_TYPE (type),
13035                                               /*only_current_p=*/false);
13036           if (class_type != error_mark_node)
13037             type = TYPE_NAME (class_type);
13038           else
13039             {
13040               cp_parser_error (parser, "could not resolve typename type");
13041               type = error_mark_node;
13042             }
13043         }
13044
13045       maybe_process_partial_specialization (TREE_TYPE (type));
13046       class_type = current_class_type;
13047       /* Enter the scope indicated by the nested-name-specifier.  */
13048       pushed_scope = push_scope (nested_name_specifier);
13049       /* Get the canonical version of this type.  */
13050       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13051       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13052           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13053         {
13054           type = push_template_decl (type);
13055           if (type == error_mark_node)
13056             {
13057               type = NULL_TREE;
13058               goto done;
13059             }
13060         }
13061
13062       type = TREE_TYPE (type);
13063       *nested_name_specifier_p = true;
13064     }
13065   else      /* The name is not a nested name.  */
13066     {
13067       /* If the class was unnamed, create a dummy name.  */
13068       if (!id)
13069         id = make_anon_name ();
13070       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13071                        parser->num_template_parameter_lists);
13072     }
13073
13074   /* Indicate whether this class was declared as a `class' or as a
13075      `struct'.  */
13076   if (TREE_CODE (type) == RECORD_TYPE)
13077     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13078   cp_parser_check_class_key (class_key, type);
13079
13080   /* If this type was already complete, and we see another definition,
13081      that's an error.  */
13082   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13083     {
13084       error ("redefinition of %q#T", type);
13085       error ("previous definition of %q+#T", type);
13086       type = NULL_TREE;
13087       goto done;
13088     }
13089
13090   /* We will have entered the scope containing the class; the names of
13091      base classes should be looked up in that context.  For example:
13092
13093        struct A { struct B {}; struct C; };
13094        struct A::C : B {};
13095
13096      is valid.  */
13097   bases = NULL_TREE;
13098
13099   /* Get the list of base-classes, if there is one.  */
13100   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13101     bases = cp_parser_base_clause (parser);
13102
13103   /* Process the base classes.  */
13104   xref_basetypes (type, bases);
13105
13106  done:
13107   /* Leave the scope given by the nested-name-specifier.  We will
13108      enter the class scope itself while processing the members.  */
13109   if (pushed_scope)
13110     pop_scope (pushed_scope);
13111
13112   if (invalid_explicit_specialization_p)
13113     {
13114       end_specialization ();
13115       --parser->num_template_parameter_lists;
13116     }
13117   *attributes_p = attributes;
13118   return type;
13119 }
13120
13121 /* Parse a class-key.
13122
13123    class-key:
13124      class
13125      struct
13126      union
13127
13128    Returns the kind of class-key specified, or none_type to indicate
13129    error.  */
13130
13131 static enum tag_types
13132 cp_parser_class_key (cp_parser* parser)
13133 {
13134   cp_token *token;
13135   enum tag_types tag_type;
13136
13137   /* Look for the class-key.  */
13138   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13139   if (!token)
13140     return none_type;
13141
13142   /* Check to see if the TOKEN is a class-key.  */
13143   tag_type = cp_parser_token_is_class_key (token);
13144   if (!tag_type)
13145     cp_parser_error (parser, "expected class-key");
13146   return tag_type;
13147 }
13148
13149 /* Parse an (optional) member-specification.
13150
13151    member-specification:
13152      member-declaration member-specification [opt]
13153      access-specifier : member-specification [opt]  */
13154
13155 static void
13156 cp_parser_member_specification_opt (cp_parser* parser)
13157 {
13158   while (true)
13159     {
13160       cp_token *token;
13161       enum rid keyword;
13162
13163       /* Peek at the next token.  */
13164       token = cp_lexer_peek_token (parser->lexer);
13165       /* If it's a `}', or EOF then we've seen all the members.  */
13166       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13167         break;
13168
13169       /* See if this token is a keyword.  */
13170       keyword = token->keyword;
13171       switch (keyword)
13172         {
13173         case RID_PUBLIC:
13174         case RID_PROTECTED:
13175         case RID_PRIVATE:
13176           /* Consume the access-specifier.  */
13177           cp_lexer_consume_token (parser->lexer);
13178           /* Remember which access-specifier is active.  */
13179           current_access_specifier = token->value;
13180           /* Look for the `:'.  */
13181           cp_parser_require (parser, CPP_COLON, "`:'");
13182           break;
13183
13184         default:
13185           /* Accept #pragmas at class scope.  */
13186           if (token->type == CPP_PRAGMA)
13187             {
13188               cp_lexer_handle_pragma (parser->lexer);
13189               break;
13190             }
13191
13192           /* Otherwise, the next construction must be a
13193              member-declaration.  */
13194           cp_parser_member_declaration (parser);
13195         }
13196     }
13197 }
13198
13199 /* Parse a member-declaration.
13200
13201    member-declaration:
13202      decl-specifier-seq [opt] member-declarator-list [opt] ;
13203      function-definition ; [opt]
13204      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13205      using-declaration
13206      template-declaration
13207
13208    member-declarator-list:
13209      member-declarator
13210      member-declarator-list , member-declarator
13211
13212    member-declarator:
13213      declarator pure-specifier [opt]
13214      declarator constant-initializer [opt]
13215      identifier [opt] : constant-expression
13216
13217    GNU Extensions:
13218
13219    member-declaration:
13220      __extension__ member-declaration
13221
13222    member-declarator:
13223      declarator attributes [opt] pure-specifier [opt]
13224      declarator attributes [opt] constant-initializer [opt]
13225      identifier [opt] attributes [opt] : constant-expression  */
13226
13227 static void
13228 cp_parser_member_declaration (cp_parser* parser)
13229 {
13230   cp_decl_specifier_seq decl_specifiers;
13231   tree prefix_attributes;
13232   tree decl;
13233   int declares_class_or_enum;
13234   bool friend_p;
13235   cp_token *token;
13236   int saved_pedantic;
13237
13238   /* Check for the `__extension__' keyword.  */
13239   if (cp_parser_extension_opt (parser, &saved_pedantic))
13240     {
13241       /* Recurse.  */
13242       cp_parser_member_declaration (parser);
13243       /* Restore the old value of the PEDANTIC flag.  */
13244       pedantic = saved_pedantic;
13245
13246       return;
13247     }
13248
13249   /* Check for a template-declaration.  */
13250   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13251     {
13252       /* Parse the template-declaration.  */
13253       cp_parser_template_declaration (parser, /*member_p=*/true);
13254
13255       return;
13256     }
13257
13258   /* Check for a using-declaration.  */
13259   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13260     {
13261       /* Parse the using-declaration.  */
13262       cp_parser_using_declaration (parser);
13263
13264       return;
13265     }
13266
13267   /* Check for @defs.  */
13268   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13269     {
13270       tree ivar, member;
13271       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13272       ivar = ivar_chains;
13273       while (ivar)
13274         {
13275           member = ivar;
13276           ivar = TREE_CHAIN (member);
13277           TREE_CHAIN (member) = NULL_TREE;
13278           finish_member_declaration (member);
13279         }
13280       return;
13281     }
13282
13283   /* Parse the decl-specifier-seq.  */
13284   cp_parser_decl_specifier_seq (parser,
13285                                 CP_PARSER_FLAGS_OPTIONAL,
13286                                 &decl_specifiers,
13287                                 &declares_class_or_enum);
13288   prefix_attributes = decl_specifiers.attributes;
13289   decl_specifiers.attributes = NULL_TREE;
13290   /* Check for an invalid type-name.  */
13291   if (!decl_specifiers.type
13292       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13293     return;
13294   /* If there is no declarator, then the decl-specifier-seq should
13295      specify a type.  */
13296   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13297     {
13298       /* If there was no decl-specifier-seq, and the next token is a
13299          `;', then we have something like:
13300
13301            struct S { ; };
13302
13303          [class.mem]
13304
13305          Each member-declaration shall declare at least one member
13306          name of the class.  */
13307       if (!decl_specifiers.any_specifiers_p)
13308         {
13309           cp_token *token = cp_lexer_peek_token (parser->lexer);
13310           if (pedantic && !token->in_system_header)
13311             pedwarn ("%Hextra %<;%>", &token->location);
13312         }
13313       else
13314         {
13315           tree type;
13316
13317           /* See if this declaration is a friend.  */
13318           friend_p = cp_parser_friend_p (&decl_specifiers);
13319           /* If there were decl-specifiers, check to see if there was
13320              a class-declaration.  */
13321           type = check_tag_decl (&decl_specifiers);
13322           /* Nested classes have already been added to the class, but
13323              a `friend' needs to be explicitly registered.  */
13324           if (friend_p)
13325             {
13326               /* If the `friend' keyword was present, the friend must
13327                  be introduced with a class-key.  */
13328                if (!declares_class_or_enum)
13329                  error ("a class-key must be used when declaring a friend");
13330                /* In this case:
13331
13332                     template <typename T> struct A {
13333                       friend struct A<T>::B;
13334                     };
13335
13336                   A<T>::B will be represented by a TYPENAME_TYPE, and
13337                   therefore not recognized by check_tag_decl.  */
13338                if (!type
13339                    && decl_specifiers.type
13340                    && TYPE_P (decl_specifiers.type))
13341                  type = decl_specifiers.type;
13342                if (!type || !TYPE_P (type))
13343                  error ("friend declaration does not name a class or "
13344                         "function");
13345                else
13346                  make_friend_class (current_class_type, type,
13347                                     /*complain=*/true);
13348             }
13349           /* If there is no TYPE, an error message will already have
13350              been issued.  */
13351           else if (!type || type == error_mark_node)
13352             ;
13353           /* An anonymous aggregate has to be handled specially; such
13354              a declaration really declares a data member (with a
13355              particular type), as opposed to a nested class.  */
13356           else if (ANON_AGGR_TYPE_P (type))
13357             {
13358               /* Remove constructors and such from TYPE, now that we
13359                  know it is an anonymous aggregate.  */
13360               fixup_anonymous_aggr (type);
13361               /* And make the corresponding data member.  */
13362               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13363               /* Add it to the class.  */
13364               finish_member_declaration (decl);
13365             }
13366           else
13367             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13368         }
13369     }
13370   else
13371     {
13372       /* See if these declarations will be friends.  */
13373       friend_p = cp_parser_friend_p (&decl_specifiers);
13374
13375       /* Keep going until we hit the `;' at the end of the
13376          declaration.  */
13377       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13378         {
13379           tree attributes = NULL_TREE;
13380           tree first_attribute;
13381
13382           /* Peek at the next token.  */
13383           token = cp_lexer_peek_token (parser->lexer);
13384
13385           /* Check for a bitfield declaration.  */
13386           if (token->type == CPP_COLON
13387               || (token->type == CPP_NAME
13388                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13389                   == CPP_COLON))
13390             {
13391               tree identifier;
13392               tree width;
13393
13394               /* Get the name of the bitfield.  Note that we cannot just
13395                  check TOKEN here because it may have been invalidated by
13396                  the call to cp_lexer_peek_nth_token above.  */
13397               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13398                 identifier = cp_parser_identifier (parser);
13399               else
13400                 identifier = NULL_TREE;
13401
13402               /* Consume the `:' token.  */
13403               cp_lexer_consume_token (parser->lexer);
13404               /* Get the width of the bitfield.  */
13405               width
13406                 = cp_parser_constant_expression (parser,
13407                                                  /*allow_non_constant=*/false,
13408                                                  NULL);
13409
13410               /* Look for attributes that apply to the bitfield.  */
13411               attributes = cp_parser_attributes_opt (parser);
13412               /* Remember which attributes are prefix attributes and
13413                  which are not.  */
13414               first_attribute = attributes;
13415               /* Combine the attributes.  */
13416               attributes = chainon (prefix_attributes, attributes);
13417
13418               /* Create the bitfield declaration.  */
13419               decl = grokbitfield (identifier
13420                                    ? make_id_declarator (NULL_TREE,
13421                                                          identifier)
13422                                    : NULL,
13423                                    &decl_specifiers,
13424                                    width);
13425               /* Apply the attributes.  */
13426               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13427             }
13428           else
13429             {
13430               cp_declarator *declarator;
13431               tree initializer;
13432               tree asm_specification;
13433               int ctor_dtor_or_conv_p;
13434
13435               /* Parse the declarator.  */
13436               declarator
13437                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13438                                         &ctor_dtor_or_conv_p,
13439                                         /*parenthesized_p=*/NULL,
13440                                         /*member_p=*/true);
13441
13442               /* If something went wrong parsing the declarator, make sure
13443                  that we at least consume some tokens.  */
13444               if (declarator == cp_error_declarator)
13445                 {
13446                   /* Skip to the end of the statement.  */
13447                   cp_parser_skip_to_end_of_statement (parser);
13448                   /* If the next token is not a semicolon, that is
13449                      probably because we just skipped over the body of
13450                      a function.  So, we consume a semicolon if
13451                      present, but do not issue an error message if it
13452                      is not present.  */
13453                   if (cp_lexer_next_token_is (parser->lexer,
13454                                               CPP_SEMICOLON))
13455                     cp_lexer_consume_token (parser->lexer);
13456                   return;
13457                 }
13458
13459               if (declares_class_or_enum & 2)
13460                 cp_parser_check_for_definition_in_return_type
13461                   (declarator, decl_specifiers.type);
13462
13463               /* Look for an asm-specification.  */
13464               asm_specification = cp_parser_asm_specification_opt (parser);
13465               /* Look for attributes that apply to the declaration.  */
13466               attributes = cp_parser_attributes_opt (parser);
13467               /* Remember which attributes are prefix attributes and
13468                  which are not.  */
13469               first_attribute = attributes;
13470               /* Combine the attributes.  */
13471               attributes = chainon (prefix_attributes, attributes);
13472
13473               /* If it's an `=', then we have a constant-initializer or a
13474                  pure-specifier.  It is not correct to parse the
13475                  initializer before registering the member declaration
13476                  since the member declaration should be in scope while
13477                  its initializer is processed.  However, the rest of the
13478                  front end does not yet provide an interface that allows
13479                  us to handle this correctly.  */
13480               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13481                 {
13482                   /* In [class.mem]:
13483
13484                      A pure-specifier shall be used only in the declaration of
13485                      a virtual function.
13486
13487                      A member-declarator can contain a constant-initializer
13488                      only if it declares a static member of integral or
13489                      enumeration type.
13490
13491                      Therefore, if the DECLARATOR is for a function, we look
13492                      for a pure-specifier; otherwise, we look for a
13493                      constant-initializer.  When we call `grokfield', it will
13494                      perform more stringent semantics checks.  */
13495                   if (declarator->kind == cdk_function)
13496                     initializer = cp_parser_pure_specifier (parser);
13497                   else
13498                     /* Parse the initializer.  */
13499                     initializer = cp_parser_constant_initializer (parser);
13500                 }
13501               /* Otherwise, there is no initializer.  */
13502               else
13503                 initializer = NULL_TREE;
13504
13505               /* See if we are probably looking at a function
13506                  definition.  We are certainly not looking at a
13507                  member-declarator.  Calling `grokfield' has
13508                  side-effects, so we must not do it unless we are sure
13509                  that we are looking at a member-declarator.  */
13510               if (cp_parser_token_starts_function_definition_p
13511                   (cp_lexer_peek_token (parser->lexer)))
13512                 {
13513                   /* The grammar does not allow a pure-specifier to be
13514                      used when a member function is defined.  (It is
13515                      possible that this fact is an oversight in the
13516                      standard, since a pure function may be defined
13517                      outside of the class-specifier.  */
13518                   if (initializer)
13519                     error ("pure-specifier on function-definition");
13520                   decl = cp_parser_save_member_function_body (parser,
13521                                                               &decl_specifiers,
13522                                                               declarator,
13523                                                               attributes);
13524                   /* If the member was not a friend, declare it here.  */
13525                   if (!friend_p)
13526                     finish_member_declaration (decl);
13527                   /* Peek at the next token.  */
13528                   token = cp_lexer_peek_token (parser->lexer);
13529                   /* If the next token is a semicolon, consume it.  */
13530                   if (token->type == CPP_SEMICOLON)
13531                     cp_lexer_consume_token (parser->lexer);
13532                   return;
13533                 }
13534               else
13535                 {
13536                   /* Create the declaration.  */
13537                   decl = grokfield (declarator, &decl_specifiers,
13538                                     initializer, asm_specification,
13539                                     attributes);
13540                   /* Any initialization must have been from a
13541                      constant-expression.  */
13542                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13543                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13544                 }
13545             }
13546
13547           /* Reset PREFIX_ATTRIBUTES.  */
13548           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13549             attributes = TREE_CHAIN (attributes);
13550           if (attributes)
13551             TREE_CHAIN (attributes) = NULL_TREE;
13552
13553           /* If there is any qualification still in effect, clear it
13554              now; we will be starting fresh with the next declarator.  */
13555           parser->scope = NULL_TREE;
13556           parser->qualifying_scope = NULL_TREE;
13557           parser->object_scope = NULL_TREE;
13558           /* If it's a `,', then there are more declarators.  */
13559           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13560             cp_lexer_consume_token (parser->lexer);
13561           /* If the next token isn't a `;', then we have a parse error.  */
13562           else if (cp_lexer_next_token_is_not (parser->lexer,
13563                                                CPP_SEMICOLON))
13564             {
13565               cp_parser_error (parser, "expected %<;%>");
13566               /* Skip tokens until we find a `;'.  */
13567               cp_parser_skip_to_end_of_statement (parser);
13568
13569               break;
13570             }
13571
13572           if (decl)
13573             {
13574               /* Add DECL to the list of members.  */
13575               if (!friend_p)
13576                 finish_member_declaration (decl);
13577
13578               if (TREE_CODE (decl) == FUNCTION_DECL)
13579                 cp_parser_save_default_args (parser, decl);
13580             }
13581         }
13582     }
13583
13584   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13585 }
13586
13587 /* Parse a pure-specifier.
13588
13589    pure-specifier:
13590      = 0
13591
13592    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13593    Otherwise, ERROR_MARK_NODE is returned.  */
13594
13595 static tree
13596 cp_parser_pure_specifier (cp_parser* parser)
13597 {
13598   cp_token *token;
13599
13600   /* Look for the `=' token.  */
13601   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13602     return error_mark_node;
13603   /* Look for the `0' token.  */
13604   token = cp_lexer_consume_token (parser->lexer);
13605   if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13606     {
13607       cp_parser_error (parser,
13608                        "invalid pure specifier (only `= 0' is allowed)");
13609       cp_parser_skip_to_end_of_statement (parser);
13610       return error_mark_node;
13611     }
13612
13613   /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13614      We need to get information from the lexer about how the number
13615      was spelled in order to fix this problem.  */
13616   return integer_zero_node;
13617 }
13618
13619 /* Parse a constant-initializer.
13620
13621    constant-initializer:
13622      = constant-expression
13623
13624    Returns a representation of the constant-expression.  */
13625
13626 static tree
13627 cp_parser_constant_initializer (cp_parser* parser)
13628 {
13629   /* Look for the `=' token.  */
13630   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13631     return error_mark_node;
13632
13633   /* It is invalid to write:
13634
13635        struct S { static const int i = { 7 }; };
13636
13637      */
13638   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13639     {
13640       cp_parser_error (parser,
13641                        "a brace-enclosed initializer is not allowed here");
13642       /* Consume the opening brace.  */
13643       cp_lexer_consume_token (parser->lexer);
13644       /* Skip the initializer.  */
13645       cp_parser_skip_to_closing_brace (parser);
13646       /* Look for the trailing `}'.  */
13647       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13648
13649       return error_mark_node;
13650     }
13651
13652   return cp_parser_constant_expression (parser,
13653                                         /*allow_non_constant=*/false,
13654                                         NULL);
13655 }
13656
13657 /* Derived classes [gram.class.derived] */
13658
13659 /* Parse a base-clause.
13660
13661    base-clause:
13662      : base-specifier-list
13663
13664    base-specifier-list:
13665      base-specifier
13666      base-specifier-list , base-specifier
13667
13668    Returns a TREE_LIST representing the base-classes, in the order in
13669    which they were declared.  The representation of each node is as
13670    described by cp_parser_base_specifier.
13671
13672    In the case that no bases are specified, this function will return
13673    NULL_TREE, not ERROR_MARK_NODE.  */
13674
13675 static tree
13676 cp_parser_base_clause (cp_parser* parser)
13677 {
13678   tree bases = NULL_TREE;
13679
13680   /* Look for the `:' that begins the list.  */
13681   cp_parser_require (parser, CPP_COLON, "`:'");
13682
13683   /* Scan the base-specifier-list.  */
13684   while (true)
13685     {
13686       cp_token *token;
13687       tree base;
13688
13689       /* Look for the base-specifier.  */
13690       base = cp_parser_base_specifier (parser);
13691       /* Add BASE to the front of the list.  */
13692       if (base != error_mark_node)
13693         {
13694           TREE_CHAIN (base) = bases;
13695           bases = base;
13696         }
13697       /* Peek at the next token.  */
13698       token = cp_lexer_peek_token (parser->lexer);
13699       /* If it's not a comma, then the list is complete.  */
13700       if (token->type != CPP_COMMA)
13701         break;
13702       /* Consume the `,'.  */
13703       cp_lexer_consume_token (parser->lexer);
13704     }
13705
13706   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13707      base class had a qualified name.  However, the next name that
13708      appears is certainly not qualified.  */
13709   parser->scope = NULL_TREE;
13710   parser->qualifying_scope = NULL_TREE;
13711   parser->object_scope = NULL_TREE;
13712
13713   return nreverse (bases);
13714 }
13715
13716 /* Parse a base-specifier.
13717
13718    base-specifier:
13719      :: [opt] nested-name-specifier [opt] class-name
13720      virtual access-specifier [opt] :: [opt] nested-name-specifier
13721        [opt] class-name
13722      access-specifier virtual [opt] :: [opt] nested-name-specifier
13723        [opt] class-name
13724
13725    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13726    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13727    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13728    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13729
13730 static tree
13731 cp_parser_base_specifier (cp_parser* parser)
13732 {
13733   cp_token *token;
13734   bool done = false;
13735   bool virtual_p = false;
13736   bool duplicate_virtual_error_issued_p = false;
13737   bool duplicate_access_error_issued_p = false;
13738   bool class_scope_p, template_p;
13739   tree access = access_default_node;
13740   tree type;
13741
13742   /* Process the optional `virtual' and `access-specifier'.  */
13743   while (!done)
13744     {
13745       /* Peek at the next token.  */
13746       token = cp_lexer_peek_token (parser->lexer);
13747       /* Process `virtual'.  */
13748       switch (token->keyword)
13749         {
13750         case RID_VIRTUAL:
13751           /* If `virtual' appears more than once, issue an error.  */
13752           if (virtual_p && !duplicate_virtual_error_issued_p)
13753             {
13754               cp_parser_error (parser,
13755                                "%<virtual%> specified more than once in base-specified");
13756               duplicate_virtual_error_issued_p = true;
13757             }
13758
13759           virtual_p = true;
13760
13761           /* Consume the `virtual' token.  */
13762           cp_lexer_consume_token (parser->lexer);
13763
13764           break;
13765
13766         case RID_PUBLIC:
13767         case RID_PROTECTED:
13768         case RID_PRIVATE:
13769           /* If more than one access specifier appears, issue an
13770              error.  */
13771           if (access != access_default_node
13772               && !duplicate_access_error_issued_p)
13773             {
13774               cp_parser_error (parser,
13775                                "more than one access specifier in base-specified");
13776               duplicate_access_error_issued_p = true;
13777             }
13778
13779           access = ridpointers[(int) token->keyword];
13780
13781           /* Consume the access-specifier.  */
13782           cp_lexer_consume_token (parser->lexer);
13783
13784           break;
13785
13786         default:
13787           done = true;
13788           break;
13789         }
13790     }
13791   /* It is not uncommon to see programs mechanically, erroneously, use
13792      the 'typename' keyword to denote (dependent) qualified types
13793      as base classes.  */
13794   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13795     {
13796       if (!processing_template_decl)
13797         error ("keyword %<typename%> not allowed outside of templates");
13798       else
13799         error ("keyword %<typename%> not allowed in this context "
13800                "(the base class is implicitly a type)");
13801       cp_lexer_consume_token (parser->lexer);
13802     }
13803
13804   /* Look for the optional `::' operator.  */
13805   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13806   /* Look for the nested-name-specifier.  The simplest way to
13807      implement:
13808
13809        [temp.res]
13810
13811        The keyword `typename' is not permitted in a base-specifier or
13812        mem-initializer; in these contexts a qualified name that
13813        depends on a template-parameter is implicitly assumed to be a
13814        type name.
13815
13816      is to pretend that we have seen the `typename' keyword at this
13817      point.  */
13818   cp_parser_nested_name_specifier_opt (parser,
13819                                        /*typename_keyword_p=*/true,
13820                                        /*check_dependency_p=*/true,
13821                                        typename_type,
13822                                        /*is_declaration=*/true);
13823   /* If the base class is given by a qualified name, assume that names
13824      we see are type names or templates, as appropriate.  */
13825   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13826   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13827
13828   /* Finally, look for the class-name.  */
13829   type = cp_parser_class_name (parser,
13830                                class_scope_p,
13831                                template_p,
13832                                typename_type,
13833                                /*check_dependency_p=*/true,
13834                                /*class_head_p=*/false,
13835                                /*is_declaration=*/true);
13836
13837   if (type == error_mark_node)
13838     return error_mark_node;
13839
13840   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13841 }
13842
13843 /* Exception handling [gram.exception] */
13844
13845 /* Parse an (optional) exception-specification.
13846
13847    exception-specification:
13848      throw ( type-id-list [opt] )
13849
13850    Returns a TREE_LIST representing the exception-specification.  The
13851    TREE_VALUE of each node is a type.  */
13852
13853 static tree
13854 cp_parser_exception_specification_opt (cp_parser* parser)
13855 {
13856   cp_token *token;
13857   tree type_id_list;
13858
13859   /* Peek at the next token.  */
13860   token = cp_lexer_peek_token (parser->lexer);
13861   /* If it's not `throw', then there's no exception-specification.  */
13862   if (!cp_parser_is_keyword (token, RID_THROW))
13863     return NULL_TREE;
13864
13865   /* Consume the `throw'.  */
13866   cp_lexer_consume_token (parser->lexer);
13867
13868   /* Look for the `('.  */
13869   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13870
13871   /* Peek at the next token.  */
13872   token = cp_lexer_peek_token (parser->lexer);
13873   /* If it's not a `)', then there is a type-id-list.  */
13874   if (token->type != CPP_CLOSE_PAREN)
13875     {
13876       const char *saved_message;
13877
13878       /* Types may not be defined in an exception-specification.  */
13879       saved_message = parser->type_definition_forbidden_message;
13880       parser->type_definition_forbidden_message
13881         = "types may not be defined in an exception-specification";
13882       /* Parse the type-id-list.  */
13883       type_id_list = cp_parser_type_id_list (parser);
13884       /* Restore the saved message.  */
13885       parser->type_definition_forbidden_message = saved_message;
13886     }
13887   else
13888     type_id_list = empty_except_spec;
13889
13890   /* Look for the `)'.  */
13891   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13892
13893   return type_id_list;
13894 }
13895
13896 /* Parse an (optional) type-id-list.
13897
13898    type-id-list:
13899      type-id
13900      type-id-list , type-id
13901
13902    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13903    in the order that the types were presented.  */
13904
13905 static tree
13906 cp_parser_type_id_list (cp_parser* parser)
13907 {
13908   tree types = NULL_TREE;
13909
13910   while (true)
13911     {
13912       cp_token *token;
13913       tree type;
13914
13915       /* Get the next type-id.  */
13916       type = cp_parser_type_id (parser);
13917       /* Add it to the list.  */
13918       types = add_exception_specifier (types, type, /*complain=*/1);
13919       /* Peek at the next token.  */
13920       token = cp_lexer_peek_token (parser->lexer);
13921       /* If it is not a `,', we are done.  */
13922       if (token->type != CPP_COMMA)
13923         break;
13924       /* Consume the `,'.  */
13925       cp_lexer_consume_token (parser->lexer);
13926     }
13927
13928   return nreverse (types);
13929 }
13930
13931 /* Parse a try-block.
13932
13933    try-block:
13934      try compound-statement handler-seq  */
13935
13936 static tree
13937 cp_parser_try_block (cp_parser* parser)
13938 {
13939   tree try_block;
13940
13941   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13942   try_block = begin_try_block ();
13943   cp_parser_compound_statement (parser, NULL, true);
13944   finish_try_block (try_block);
13945   cp_parser_handler_seq (parser);
13946   finish_handler_sequence (try_block);
13947
13948   return try_block;
13949 }
13950
13951 /* Parse a function-try-block.
13952
13953    function-try-block:
13954      try ctor-initializer [opt] function-body handler-seq  */
13955
13956 static bool
13957 cp_parser_function_try_block (cp_parser* parser)
13958 {
13959   tree try_block;
13960   bool ctor_initializer_p;
13961
13962   /* Look for the `try' keyword.  */
13963   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13964     return false;
13965   /* Let the rest of the front-end know where we are.  */
13966   try_block = begin_function_try_block ();
13967   /* Parse the function-body.  */
13968   ctor_initializer_p
13969     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13970   /* We're done with the `try' part.  */
13971   finish_function_try_block (try_block);
13972   /* Parse the handlers.  */
13973   cp_parser_handler_seq (parser);
13974   /* We're done with the handlers.  */
13975   finish_function_handler_sequence (try_block);
13976
13977   return ctor_initializer_p;
13978 }
13979
13980 /* Parse a handler-seq.
13981
13982    handler-seq:
13983      handler handler-seq [opt]  */
13984
13985 static void
13986 cp_parser_handler_seq (cp_parser* parser)
13987 {
13988   while (true)
13989     {
13990       cp_token *token;
13991
13992       /* Parse the handler.  */
13993       cp_parser_handler (parser);
13994       /* Peek at the next token.  */
13995       token = cp_lexer_peek_token (parser->lexer);
13996       /* If it's not `catch' then there are no more handlers.  */
13997       if (!cp_parser_is_keyword (token, RID_CATCH))
13998         break;
13999     }
14000 }
14001
14002 /* Parse a handler.
14003
14004    handler:
14005      catch ( exception-declaration ) compound-statement  */
14006
14007 static void
14008 cp_parser_handler (cp_parser* parser)
14009 {
14010   tree handler;
14011   tree declaration;
14012
14013   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14014   handler = begin_handler ();
14015   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14016   declaration = cp_parser_exception_declaration (parser);
14017   finish_handler_parms (declaration, handler);
14018   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14019   cp_parser_compound_statement (parser, NULL, false);
14020   finish_handler (handler);
14021 }
14022
14023 /* Parse an exception-declaration.
14024
14025    exception-declaration:
14026      type-specifier-seq declarator
14027      type-specifier-seq abstract-declarator
14028      type-specifier-seq
14029      ...
14030
14031    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14032    ellipsis variant is used.  */
14033
14034 static tree
14035 cp_parser_exception_declaration (cp_parser* parser)
14036 {
14037   tree decl;
14038   cp_decl_specifier_seq type_specifiers;
14039   cp_declarator *declarator;
14040   const char *saved_message;
14041
14042   /* If it's an ellipsis, it's easy to handle.  */
14043   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14044     {
14045       /* Consume the `...' token.  */
14046       cp_lexer_consume_token (parser->lexer);
14047       return NULL_TREE;
14048     }
14049
14050   /* Types may not be defined in exception-declarations.  */
14051   saved_message = parser->type_definition_forbidden_message;
14052   parser->type_definition_forbidden_message
14053     = "types may not be defined in exception-declarations";
14054
14055   /* Parse the type-specifier-seq.  */
14056   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14057                                 &type_specifiers);
14058   /* If it's a `)', then there is no declarator.  */
14059   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14060     declarator = NULL;
14061   else
14062     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14063                                        /*ctor_dtor_or_conv_p=*/NULL,
14064                                        /*parenthesized_p=*/NULL,
14065                                        /*member_p=*/false);
14066
14067   /* Restore the saved message.  */
14068   parser->type_definition_forbidden_message = saved_message;
14069
14070   if (type_specifiers.any_specifiers_p)
14071     {
14072       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14073       if (decl == NULL_TREE)
14074         error ("invalid catch parameter");
14075     }
14076   else
14077     decl = NULL_TREE;
14078
14079   return decl;
14080 }
14081
14082 /* Parse a throw-expression.
14083
14084    throw-expression:
14085      throw assignment-expression [opt]
14086
14087    Returns a THROW_EXPR representing the throw-expression.  */
14088
14089 static tree
14090 cp_parser_throw_expression (cp_parser* parser)
14091 {
14092   tree expression;
14093   cp_token* token;
14094
14095   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14096   token = cp_lexer_peek_token (parser->lexer);
14097   /* Figure out whether or not there is an assignment-expression
14098      following the "throw" keyword.  */
14099   if (token->type == CPP_COMMA
14100       || token->type == CPP_SEMICOLON
14101       || token->type == CPP_CLOSE_PAREN
14102       || token->type == CPP_CLOSE_SQUARE
14103       || token->type == CPP_CLOSE_BRACE
14104       || token->type == CPP_COLON)
14105     expression = NULL_TREE;
14106   else
14107     expression = cp_parser_assignment_expression (parser,
14108                                                   /*cast_p=*/false);
14109
14110   return build_throw (expression);
14111 }
14112
14113 /* GNU Extensions */
14114
14115 /* Parse an (optional) asm-specification.
14116
14117    asm-specification:
14118      asm ( string-literal )
14119
14120    If the asm-specification is present, returns a STRING_CST
14121    corresponding to the string-literal.  Otherwise, returns
14122    NULL_TREE.  */
14123
14124 static tree
14125 cp_parser_asm_specification_opt (cp_parser* parser)
14126 {
14127   cp_token *token;
14128   tree asm_specification;
14129
14130   /* Peek at the next token.  */
14131   token = cp_lexer_peek_token (parser->lexer);
14132   /* If the next token isn't the `asm' keyword, then there's no
14133      asm-specification.  */
14134   if (!cp_parser_is_keyword (token, RID_ASM))
14135     return NULL_TREE;
14136
14137   /* Consume the `asm' token.  */
14138   cp_lexer_consume_token (parser->lexer);
14139   /* Look for the `('.  */
14140   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14141
14142   /* Look for the string-literal.  */
14143   asm_specification = cp_parser_string_literal (parser, false, false);
14144
14145   /* Look for the `)'.  */
14146   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14147
14148   return asm_specification;
14149 }
14150
14151 /* Parse an asm-operand-list.
14152
14153    asm-operand-list:
14154      asm-operand
14155      asm-operand-list , asm-operand
14156
14157    asm-operand:
14158      string-literal ( expression )
14159      [ string-literal ] string-literal ( expression )
14160
14161    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14162    each node is the expression.  The TREE_PURPOSE is itself a
14163    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14164    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14165    is a STRING_CST for the string literal before the parenthesis.  */
14166
14167 static tree
14168 cp_parser_asm_operand_list (cp_parser* parser)
14169 {
14170   tree asm_operands = NULL_TREE;
14171
14172   while (true)
14173     {
14174       tree string_literal;
14175       tree expression;
14176       tree name;
14177
14178       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14179         {
14180           /* Consume the `[' token.  */
14181           cp_lexer_consume_token (parser->lexer);
14182           /* Read the operand name.  */
14183           name = cp_parser_identifier (parser);
14184           if (name != error_mark_node)
14185             name = build_string (IDENTIFIER_LENGTH (name),
14186                                  IDENTIFIER_POINTER (name));
14187           /* Look for the closing `]'.  */
14188           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14189         }
14190       else
14191         name = NULL_TREE;
14192       /* Look for the string-literal.  */
14193       string_literal = cp_parser_string_literal (parser, false, false);
14194
14195       /* Look for the `('.  */
14196       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14197       /* Parse the expression.  */
14198       expression = cp_parser_expression (parser, /*cast_p=*/false);
14199       /* Look for the `)'.  */
14200       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14201
14202       /* Add this operand to the list.  */
14203       asm_operands = tree_cons (build_tree_list (name, string_literal),
14204                                 expression,
14205                                 asm_operands);
14206       /* If the next token is not a `,', there are no more
14207          operands.  */
14208       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14209         break;
14210       /* Consume the `,'.  */
14211       cp_lexer_consume_token (parser->lexer);
14212     }
14213
14214   return nreverse (asm_operands);
14215 }
14216
14217 /* Parse an asm-clobber-list.
14218
14219    asm-clobber-list:
14220      string-literal
14221      asm-clobber-list , string-literal
14222
14223    Returns a TREE_LIST, indicating the clobbers in the order that they
14224    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14225
14226 static tree
14227 cp_parser_asm_clobber_list (cp_parser* parser)
14228 {
14229   tree clobbers = NULL_TREE;
14230
14231   while (true)
14232     {
14233       tree string_literal;
14234
14235       /* Look for the string literal.  */
14236       string_literal = cp_parser_string_literal (parser, false, false);
14237       /* Add it to the list.  */
14238       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14239       /* If the next token is not a `,', then the list is
14240          complete.  */
14241       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14242         break;
14243       /* Consume the `,' token.  */
14244       cp_lexer_consume_token (parser->lexer);
14245     }
14246
14247   return clobbers;
14248 }
14249
14250 /* Parse an (optional) series of attributes.
14251
14252    attributes:
14253      attributes attribute
14254
14255    attribute:
14256      __attribute__ (( attribute-list [opt] ))
14257
14258    The return value is as for cp_parser_attribute_list.  */
14259
14260 static tree
14261 cp_parser_attributes_opt (cp_parser* parser)
14262 {
14263   tree attributes = NULL_TREE;
14264
14265   while (true)
14266     {
14267       cp_token *token;
14268       tree attribute_list;
14269
14270       /* Peek at the next token.  */
14271       token = cp_lexer_peek_token (parser->lexer);
14272       /* If it's not `__attribute__', then we're done.  */
14273       if (token->keyword != RID_ATTRIBUTE)
14274         break;
14275
14276       /* Consume the `__attribute__' keyword.  */
14277       cp_lexer_consume_token (parser->lexer);
14278       /* Look for the two `(' tokens.  */
14279       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14280       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14281
14282       /* Peek at the next token.  */
14283       token = cp_lexer_peek_token (parser->lexer);
14284       if (token->type != CPP_CLOSE_PAREN)
14285         /* Parse the attribute-list.  */
14286         attribute_list = cp_parser_attribute_list (parser);
14287       else
14288         /* If the next token is a `)', then there is no attribute
14289            list.  */
14290         attribute_list = NULL;
14291
14292       /* Look for the two `)' tokens.  */
14293       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14294       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14295
14296       /* Add these new attributes to the list.  */
14297       attributes = chainon (attributes, attribute_list);
14298     }
14299
14300   return attributes;
14301 }
14302
14303 /* Parse an attribute-list.
14304
14305    attribute-list:
14306      attribute
14307      attribute-list , attribute
14308
14309    attribute:
14310      identifier
14311      identifier ( identifier )
14312      identifier ( identifier , expression-list )
14313      identifier ( expression-list )
14314
14315    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14316    to an attribute.  The TREE_PURPOSE of each node is the identifier
14317    indicating which attribute is in use.  The TREE_VALUE represents
14318    the arguments, if any.  */
14319
14320 static tree
14321 cp_parser_attribute_list (cp_parser* parser)
14322 {
14323   tree attribute_list = NULL_TREE;
14324   bool save_translate_strings_p = parser->translate_strings_p;
14325
14326   parser->translate_strings_p = false;
14327   while (true)
14328     {
14329       cp_token *token;
14330       tree identifier;
14331       tree attribute;
14332
14333       /* Look for the identifier.  We also allow keywords here; for
14334          example `__attribute__ ((const))' is legal.  */
14335       token = cp_lexer_peek_token (parser->lexer);
14336       if (token->type == CPP_NAME
14337           || token->type == CPP_KEYWORD)
14338         {
14339           /* Consume the token.  */
14340           token = cp_lexer_consume_token (parser->lexer);
14341
14342           /* Save away the identifier that indicates which attribute
14343              this is.  */
14344           identifier = token->value;
14345           attribute = build_tree_list (identifier, NULL_TREE);
14346
14347           /* Peek at the next token.  */
14348           token = cp_lexer_peek_token (parser->lexer);
14349           /* If it's an `(', then parse the attribute arguments.  */
14350           if (token->type == CPP_OPEN_PAREN)
14351             {
14352               tree arguments;
14353
14354               arguments = (cp_parser_parenthesized_expression_list
14355                            (parser, true, /*cast_p=*/false,
14356                             /*non_constant_p=*/NULL));
14357               /* Save the identifier and arguments away.  */
14358               TREE_VALUE (attribute) = arguments;
14359             }
14360
14361           /* Add this attribute to the list.  */
14362           TREE_CHAIN (attribute) = attribute_list;
14363           attribute_list = attribute;
14364
14365           token = cp_lexer_peek_token (parser->lexer);
14366         }
14367       /* Now, look for more attributes.  If the next token isn't a
14368          `,', we're done.  */
14369       if (token->type != CPP_COMMA)
14370         break;
14371
14372       /* Consume the comma and keep going.  */
14373       cp_lexer_consume_token (parser->lexer);
14374     }
14375   parser->translate_strings_p = save_translate_strings_p;
14376
14377   /* We built up the list in reverse order.  */
14378   return nreverse (attribute_list);
14379 }
14380
14381 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14382    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14383    current value of the PEDANTIC flag, regardless of whether or not
14384    the `__extension__' keyword is present.  The caller is responsible
14385    for restoring the value of the PEDANTIC flag.  */
14386
14387 static bool
14388 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14389 {
14390   /* Save the old value of the PEDANTIC flag.  */
14391   *saved_pedantic = pedantic;
14392
14393   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14394     {
14395       /* Consume the `__extension__' token.  */
14396       cp_lexer_consume_token (parser->lexer);
14397       /* We're not being pedantic while the `__extension__' keyword is
14398          in effect.  */
14399       pedantic = 0;
14400
14401       return true;
14402     }
14403
14404   return false;
14405 }
14406
14407 /* Parse a label declaration.
14408
14409    label-declaration:
14410      __label__ label-declarator-seq ;
14411
14412    label-declarator-seq:
14413      identifier , label-declarator-seq
14414      identifier  */
14415
14416 static void
14417 cp_parser_label_declaration (cp_parser* parser)
14418 {
14419   /* Look for the `__label__' keyword.  */
14420   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14421
14422   while (true)
14423     {
14424       tree identifier;
14425
14426       /* Look for an identifier.  */
14427       identifier = cp_parser_identifier (parser);
14428       /* If we failed, stop.  */
14429       if (identifier == error_mark_node)
14430         break;
14431       /* Declare it as a label.  */
14432       finish_label_decl (identifier);
14433       /* If the next token is a `;', stop.  */
14434       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14435         break;
14436       /* Look for the `,' separating the label declarations.  */
14437       cp_parser_require (parser, CPP_COMMA, "`,'");
14438     }
14439
14440   /* Look for the final `;'.  */
14441   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14442 }
14443
14444 /* Support Functions */
14445
14446 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14447    NAME should have one of the representations used for an
14448    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14449    is returned.  If PARSER->SCOPE is a dependent type, then a
14450    SCOPE_REF is returned.
14451
14452    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14453    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14454    was formed.  Abstractly, such entities should not be passed to this
14455    function, because they do not need to be looked up, but it is
14456    simpler to check for this special case here, rather than at the
14457    call-sites.
14458
14459    In cases not explicitly covered above, this function returns a
14460    DECL, OVERLOAD, or baselink representing the result of the lookup.
14461    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14462    is returned.
14463
14464    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14465    (e.g., "struct") that was used.  In that case bindings that do not
14466    refer to types are ignored.
14467
14468    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14469    ignored.
14470
14471    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14472    are ignored.
14473
14474    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14475    types.
14476
14477    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14478    results in an ambiguity, and false otherwise.  */
14479
14480 static tree
14481 cp_parser_lookup_name (cp_parser *parser, tree name,
14482                        enum tag_types tag_type,
14483                        bool is_template, bool is_namespace,
14484                        bool check_dependency,
14485                        bool *ambiguous_p)
14486 {
14487   int flags = 0;
14488   tree decl;
14489   tree object_type = parser->context->object_type;
14490
14491   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14492     flags |= LOOKUP_COMPLAIN;
14493
14494   /* Assume that the lookup will be unambiguous.  */
14495   if (ambiguous_p)
14496     *ambiguous_p = false;
14497
14498   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14499      no longer valid.  Note that if we are parsing tentatively, and
14500      the parse fails, OBJECT_TYPE will be automatically restored.  */
14501   parser->context->object_type = NULL_TREE;
14502
14503   if (name == error_mark_node)
14504     return error_mark_node;
14505
14506   /* A template-id has already been resolved; there is no lookup to
14507      do.  */
14508   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14509     return name;
14510   if (BASELINK_P (name))
14511     {
14512       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14513                   == TEMPLATE_ID_EXPR);
14514       return name;
14515     }
14516
14517   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14518      it should already have been checked to make sure that the name
14519      used matches the type being destroyed.  */
14520   if (TREE_CODE (name) == BIT_NOT_EXPR)
14521     {
14522       tree type;
14523
14524       /* Figure out to which type this destructor applies.  */
14525       if (parser->scope)
14526         type = parser->scope;
14527       else if (object_type)
14528         type = object_type;
14529       else
14530         type = current_class_type;
14531       /* If that's not a class type, there is no destructor.  */
14532       if (!type || !CLASS_TYPE_P (type))
14533         return error_mark_node;
14534       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14535         lazily_declare_fn (sfk_destructor, type);
14536       if (!CLASSTYPE_DESTRUCTORS (type))
14537           return error_mark_node;
14538       /* If it was a class type, return the destructor.  */
14539       return CLASSTYPE_DESTRUCTORS (type);
14540     }
14541
14542   /* By this point, the NAME should be an ordinary identifier.  If
14543      the id-expression was a qualified name, the qualifying scope is
14544      stored in PARSER->SCOPE at this point.  */
14545   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14546
14547   /* Perform the lookup.  */
14548   if (parser->scope)
14549     {
14550       bool dependent_p;
14551
14552       if (parser->scope == error_mark_node)
14553         return error_mark_node;
14554
14555       /* If the SCOPE is dependent, the lookup must be deferred until
14556          the template is instantiated -- unless we are explicitly
14557          looking up names in uninstantiated templates.  Even then, we
14558          cannot look up the name if the scope is not a class type; it
14559          might, for example, be a template type parameter.  */
14560       dependent_p = (TYPE_P (parser->scope)
14561                      && !(parser->in_declarator_p
14562                           && currently_open_class (parser->scope))
14563                      && dependent_type_p (parser->scope));
14564       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14565            && dependent_p)
14566         {
14567           if (tag_type)
14568             {
14569               tree type;
14570
14571               /* The resolution to Core Issue 180 says that `struct
14572                  A::B' should be considered a type-name, even if `A'
14573                  is dependent.  */
14574               type = make_typename_type (parser->scope, name, tag_type,
14575                                          /*complain=*/1);
14576               decl = TYPE_NAME (type);
14577             }
14578           else if (is_template)
14579             decl = make_unbound_class_template (parser->scope,
14580                                                 name, NULL_TREE,
14581                                                 /*complain=*/1);
14582           else
14583             decl = build_nt (SCOPE_REF, parser->scope, name);
14584         }
14585       else
14586         {
14587           tree pushed_scope = NULL_TREE;
14588
14589           /* If PARSER->SCOPE is a dependent type, then it must be a
14590              class type, and we must not be checking dependencies;
14591              otherwise, we would have processed this lookup above.  So
14592              that PARSER->SCOPE is not considered a dependent base by
14593              lookup_member, we must enter the scope here.  */
14594           if (dependent_p)
14595             pushed_scope = push_scope (parser->scope);
14596           /* If the PARSER->SCOPE is a template specialization, it
14597              may be instantiated during name lookup.  In that case,
14598              errors may be issued.  Even if we rollback the current
14599              tentative parse, those errors are valid.  */
14600           decl = lookup_qualified_name (parser->scope, name,
14601                                         tag_type != none_type,
14602                                         /*complain=*/true);
14603           if (pushed_scope)
14604             pop_scope (pushed_scope);
14605         }
14606       parser->qualifying_scope = parser->scope;
14607       parser->object_scope = NULL_TREE;
14608     }
14609   else if (object_type)
14610     {
14611       tree object_decl = NULL_TREE;
14612       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14613          OBJECT_TYPE is not a class.  */
14614       if (CLASS_TYPE_P (object_type))
14615         /* If the OBJECT_TYPE is a template specialization, it may
14616            be instantiated during name lookup.  In that case, errors
14617            may be issued.  Even if we rollback the current tentative
14618            parse, those errors are valid.  */
14619         object_decl = lookup_member (object_type,
14620                                      name,
14621                                      /*protect=*/0,
14622                                      tag_type != none_type);
14623       /* Look it up in the enclosing context, too.  */
14624       decl = lookup_name_real (name, tag_type != none_type,
14625                                /*nonclass=*/0,
14626                                /*block_p=*/true, is_namespace, flags);
14627       parser->object_scope = object_type;
14628       parser->qualifying_scope = NULL_TREE;
14629       if (object_decl)
14630         decl = object_decl;
14631     }
14632   else
14633     {
14634       decl = lookup_name_real (name, tag_type != none_type,
14635                                /*nonclass=*/0,
14636                                /*block_p=*/true, is_namespace, flags);
14637       parser->qualifying_scope = NULL_TREE;
14638       parser->object_scope = NULL_TREE;
14639     }
14640
14641   /* If the lookup failed, let our caller know.  */
14642   if (!decl || decl == error_mark_node)
14643     return error_mark_node;
14644
14645   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14646   if (TREE_CODE (decl) == TREE_LIST)
14647     {
14648       if (ambiguous_p)
14649         *ambiguous_p = true;
14650       /* The error message we have to print is too complicated for
14651          cp_parser_error, so we incorporate its actions directly.  */
14652       if (!cp_parser_simulate_error (parser))
14653         {
14654           error ("reference to %qD is ambiguous", name);
14655           print_candidates (decl);
14656         }
14657       return error_mark_node;
14658     }
14659
14660   gcc_assert (DECL_P (decl)
14661               || TREE_CODE (decl) == OVERLOAD
14662               || TREE_CODE (decl) == SCOPE_REF
14663               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14664               || BASELINK_P (decl));
14665
14666   /* If we have resolved the name of a member declaration, check to
14667      see if the declaration is accessible.  When the name resolves to
14668      set of overloaded functions, accessibility is checked when
14669      overload resolution is done.
14670
14671      During an explicit instantiation, access is not checked at all,
14672      as per [temp.explicit].  */
14673   if (DECL_P (decl))
14674     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14675
14676   return decl;
14677 }
14678
14679 /* Like cp_parser_lookup_name, but for use in the typical case where
14680    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14681    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14682
14683 static tree
14684 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14685 {
14686   return cp_parser_lookup_name (parser, name,
14687                                 none_type,
14688                                 /*is_template=*/false,
14689                                 /*is_namespace=*/false,
14690                                 /*check_dependency=*/true,
14691                                 /*ambiguous_p=*/NULL);
14692 }
14693
14694 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14695    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14696    true, the DECL indicates the class being defined in a class-head,
14697    or declared in an elaborated-type-specifier.
14698
14699    Otherwise, return DECL.  */
14700
14701 static tree
14702 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14703 {
14704   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14705      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14706
14707        struct A {
14708          template <typename T> struct B;
14709        };
14710
14711        template <typename T> struct A::B {};
14712
14713      Similarly, in an elaborated-type-specifier:
14714
14715        namespace N { struct X{}; }
14716
14717        struct A {
14718          template <typename T> friend struct N::X;
14719        };
14720
14721      However, if the DECL refers to a class type, and we are in
14722      the scope of the class, then the name lookup automatically
14723      finds the TYPE_DECL created by build_self_reference rather
14724      than a TEMPLATE_DECL.  For example, in:
14725
14726        template <class T> struct S {
14727          S s;
14728        };
14729
14730      there is no need to handle such case.  */
14731
14732   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14733     return DECL_TEMPLATE_RESULT (decl);
14734
14735   return decl;
14736 }
14737
14738 /* If too many, or too few, template-parameter lists apply to the
14739    declarator, issue an error message.  Returns TRUE if all went well,
14740    and FALSE otherwise.  */
14741
14742 static bool
14743 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14744                                                 cp_declarator *declarator)
14745 {
14746   unsigned num_templates;
14747
14748   /* We haven't seen any classes that involve template parameters yet.  */
14749   num_templates = 0;
14750
14751   switch (declarator->kind)
14752     {
14753     case cdk_id:
14754       if (declarator->u.id.qualifying_scope)
14755         {
14756           tree scope;
14757           tree member;
14758
14759           scope = declarator->u.id.qualifying_scope;
14760           member = declarator->u.id.unqualified_name;
14761
14762           while (scope && CLASS_TYPE_P (scope))
14763             {
14764               /* You're supposed to have one `template <...>'
14765                  for every template class, but you don't need one
14766                  for a full specialization.  For example:
14767
14768                  template <class T> struct S{};
14769                  template <> struct S<int> { void f(); };
14770                  void S<int>::f () {}
14771
14772                  is correct; there shouldn't be a `template <>' for
14773                  the definition of `S<int>::f'.  */
14774               if (CLASSTYPE_TEMPLATE_INFO (scope)
14775                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14776                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14777                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14778                 ++num_templates;
14779
14780               scope = TYPE_CONTEXT (scope);
14781             }
14782         }
14783       else if (TREE_CODE (declarator->u.id.unqualified_name)
14784                == TEMPLATE_ID_EXPR)
14785         /* If the DECLARATOR has the form `X<y>' then it uses one
14786            additional level of template parameters.  */
14787         ++num_templates;
14788
14789       return cp_parser_check_template_parameters (parser,
14790                                                   num_templates);
14791
14792     case cdk_function:
14793     case cdk_array:
14794     case cdk_pointer:
14795     case cdk_reference:
14796     case cdk_ptrmem:
14797       return (cp_parser_check_declarator_template_parameters
14798               (parser, declarator->declarator));
14799
14800     case cdk_error:
14801       return true;
14802
14803     default:
14804       gcc_unreachable ();
14805     }
14806   return false;
14807 }
14808
14809 /* NUM_TEMPLATES were used in the current declaration.  If that is
14810    invalid, return FALSE and issue an error messages.  Otherwise,
14811    return TRUE.  */
14812
14813 static bool
14814 cp_parser_check_template_parameters (cp_parser* parser,
14815                                      unsigned num_templates)
14816 {
14817   /* If there are more template classes than parameter lists, we have
14818      something like:
14819
14820        template <class T> void S<T>::R<T>::f ();  */
14821   if (parser->num_template_parameter_lists < num_templates)
14822     {
14823       error ("too few template-parameter-lists");
14824       return false;
14825     }
14826   /* If there are the same number of template classes and parameter
14827      lists, that's OK.  */
14828   if (parser->num_template_parameter_lists == num_templates)
14829     return true;
14830   /* If there are more, but only one more, then we are referring to a
14831      member template.  That's OK too.  */
14832   if (parser->num_template_parameter_lists == num_templates + 1)
14833       return true;
14834   /* Otherwise, there are too many template parameter lists.  We have
14835      something like:
14836
14837      template <class T> template <class U> void S::f();  */
14838   error ("too many template-parameter-lists");
14839   return false;
14840 }
14841
14842 /* Parse an optional `::' token indicating that the following name is
14843    from the global namespace.  If so, PARSER->SCOPE is set to the
14844    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14845    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14846    Returns the new value of PARSER->SCOPE, if the `::' token is
14847    present, and NULL_TREE otherwise.  */
14848
14849 static tree
14850 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14851 {
14852   cp_token *token;
14853
14854   /* Peek at the next token.  */
14855   token = cp_lexer_peek_token (parser->lexer);
14856   /* If we're looking at a `::' token then we're starting from the
14857      global namespace, not our current location.  */
14858   if (token->type == CPP_SCOPE)
14859     {
14860       /* Consume the `::' token.  */
14861       cp_lexer_consume_token (parser->lexer);
14862       /* Set the SCOPE so that we know where to start the lookup.  */
14863       parser->scope = global_namespace;
14864       parser->qualifying_scope = global_namespace;
14865       parser->object_scope = NULL_TREE;
14866
14867       return parser->scope;
14868     }
14869   else if (!current_scope_valid_p)
14870     {
14871       parser->scope = NULL_TREE;
14872       parser->qualifying_scope = NULL_TREE;
14873       parser->object_scope = NULL_TREE;
14874     }
14875
14876   return NULL_TREE;
14877 }
14878
14879 /* Returns TRUE if the upcoming token sequence is the start of a
14880    constructor declarator.  If FRIEND_P is true, the declarator is
14881    preceded by the `friend' specifier.  */
14882
14883 static bool
14884 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14885 {
14886   bool constructor_p;
14887   tree type_decl = NULL_TREE;
14888   bool nested_name_p;
14889   cp_token *next_token;
14890
14891   /* The common case is that this is not a constructor declarator, so
14892      try to avoid doing lots of work if at all possible.  It's not
14893      valid declare a constructor at function scope.  */
14894   if (at_function_scope_p ())
14895     return false;
14896   /* And only certain tokens can begin a constructor declarator.  */
14897   next_token = cp_lexer_peek_token (parser->lexer);
14898   if (next_token->type != CPP_NAME
14899       && next_token->type != CPP_SCOPE
14900       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14901       && next_token->type != CPP_TEMPLATE_ID)
14902     return false;
14903
14904   /* Parse tentatively; we are going to roll back all of the tokens
14905      consumed here.  */
14906   cp_parser_parse_tentatively (parser);
14907   /* Assume that we are looking at a constructor declarator.  */
14908   constructor_p = true;
14909
14910   /* Look for the optional `::' operator.  */
14911   cp_parser_global_scope_opt (parser,
14912                               /*current_scope_valid_p=*/false);
14913   /* Look for the nested-name-specifier.  */
14914   nested_name_p
14915     = (cp_parser_nested_name_specifier_opt (parser,
14916                                             /*typename_keyword_p=*/false,
14917                                             /*check_dependency_p=*/false,
14918                                             /*type_p=*/false,
14919                                             /*is_declaration=*/false)
14920        != NULL_TREE);
14921   /* Outside of a class-specifier, there must be a
14922      nested-name-specifier.  */
14923   if (!nested_name_p &&
14924       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14925        || friend_p))
14926     constructor_p = false;
14927   /* If we still think that this might be a constructor-declarator,
14928      look for a class-name.  */
14929   if (constructor_p)
14930     {
14931       /* If we have:
14932
14933            template <typename T> struct S { S(); };
14934            template <typename T> S<T>::S ();
14935
14936          we must recognize that the nested `S' names a class.
14937          Similarly, for:
14938
14939            template <typename T> S<T>::S<T> ();
14940
14941          we must recognize that the nested `S' names a template.  */
14942       type_decl = cp_parser_class_name (parser,
14943                                         /*typename_keyword_p=*/false,
14944                                         /*template_keyword_p=*/false,
14945                                         none_type,
14946                                         /*check_dependency_p=*/false,
14947                                         /*class_head_p=*/false,
14948                                         /*is_declaration=*/false);
14949       /* If there was no class-name, then this is not a constructor.  */
14950       constructor_p = !cp_parser_error_occurred (parser);
14951     }
14952
14953   /* If we're still considering a constructor, we have to see a `(',
14954      to begin the parameter-declaration-clause, followed by either a
14955      `)', an `...', or a decl-specifier.  We need to check for a
14956      type-specifier to avoid being fooled into thinking that:
14957
14958        S::S (f) (int);
14959
14960      is a constructor.  (It is actually a function named `f' that
14961      takes one parameter (of type `int') and returns a value of type
14962      `S::S'.  */
14963   if (constructor_p
14964       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14965     {
14966       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14967           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14968           /* A parameter declaration begins with a decl-specifier,
14969              which is either the "attribute" keyword, a storage class
14970              specifier, or (usually) a type-specifier.  */
14971           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14972           && !cp_parser_storage_class_specifier_opt (parser))
14973         {
14974           tree type;
14975           tree pushed_scope = NULL_TREE;
14976           unsigned saved_num_template_parameter_lists;
14977
14978           /* Names appearing in the type-specifier should be looked up
14979              in the scope of the class.  */
14980           if (current_class_type)
14981             type = NULL_TREE;
14982           else
14983             {
14984               type = TREE_TYPE (type_decl);
14985               if (TREE_CODE (type) == TYPENAME_TYPE)
14986                 {
14987                   type = resolve_typename_type (type,
14988                                                 /*only_current_p=*/false);
14989                   if (type == error_mark_node)
14990                     {
14991                       cp_parser_abort_tentative_parse (parser);
14992                       return false;
14993                     }
14994                 }
14995               pushed_scope = push_scope (type);
14996             }
14997
14998           /* Inside the constructor parameter list, surrounding
14999              template-parameter-lists do not apply.  */
15000           saved_num_template_parameter_lists
15001             = parser->num_template_parameter_lists;
15002           parser->num_template_parameter_lists = 0;
15003
15004           /* Look for the type-specifier.  */
15005           cp_parser_type_specifier (parser,
15006                                     CP_PARSER_FLAGS_NONE,
15007                                     /*decl_specs=*/NULL,
15008                                     /*is_declarator=*/true,
15009                                     /*declares_class_or_enum=*/NULL,
15010                                     /*is_cv_qualifier=*/NULL);
15011
15012           parser->num_template_parameter_lists
15013             = saved_num_template_parameter_lists;
15014
15015           /* Leave the scope of the class.  */
15016           if (pushed_scope)
15017             pop_scope (pushed_scope);
15018
15019           constructor_p = !cp_parser_error_occurred (parser);
15020         }
15021     }
15022   else
15023     constructor_p = false;
15024   /* We did not really want to consume any tokens.  */
15025   cp_parser_abort_tentative_parse (parser);
15026
15027   return constructor_p;
15028 }
15029
15030 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15031    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15032    they must be performed once we are in the scope of the function.
15033
15034    Returns the function defined.  */
15035
15036 static tree
15037 cp_parser_function_definition_from_specifiers_and_declarator
15038   (cp_parser* parser,
15039    cp_decl_specifier_seq *decl_specifiers,
15040    tree attributes,
15041    const cp_declarator *declarator)
15042 {
15043   tree fn;
15044   bool success_p;
15045
15046   /* Begin the function-definition.  */
15047   success_p = start_function (decl_specifiers, declarator, attributes);
15048
15049   /* The things we're about to see are not directly qualified by any
15050      template headers we've seen thus far.  */
15051   reset_specialization ();
15052
15053   /* If there were names looked up in the decl-specifier-seq that we
15054      did not check, check them now.  We must wait until we are in the
15055      scope of the function to perform the checks, since the function
15056      might be a friend.  */
15057   perform_deferred_access_checks ();
15058
15059   if (!success_p)
15060     {
15061       /* Skip the entire function.  */
15062       error ("invalid function declaration");
15063       cp_parser_skip_to_end_of_block_or_statement (parser);
15064       fn = error_mark_node;
15065     }
15066   else
15067     fn = cp_parser_function_definition_after_declarator (parser,
15068                                                          /*inline_p=*/false);
15069
15070   return fn;
15071 }
15072
15073 /* Parse the part of a function-definition that follows the
15074    declarator.  INLINE_P is TRUE iff this function is an inline
15075    function defined with a class-specifier.
15076
15077    Returns the function defined.  */
15078
15079 static tree
15080 cp_parser_function_definition_after_declarator (cp_parser* parser,
15081                                                 bool inline_p)
15082 {
15083   tree fn;
15084   bool ctor_initializer_p = false;
15085   bool saved_in_unbraced_linkage_specification_p;
15086   unsigned saved_num_template_parameter_lists;
15087
15088   /* If the next token is `return', then the code may be trying to
15089      make use of the "named return value" extension that G++ used to
15090      support.  */
15091   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15092     {
15093       /* Consume the `return' keyword.  */
15094       cp_lexer_consume_token (parser->lexer);
15095       /* Look for the identifier that indicates what value is to be
15096          returned.  */
15097       cp_parser_identifier (parser);
15098       /* Issue an error message.  */
15099       error ("named return values are no longer supported");
15100       /* Skip tokens until we reach the start of the function body.  */
15101       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15102              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15103         cp_lexer_consume_token (parser->lexer);
15104     }
15105   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15106      anything declared inside `f'.  */
15107   saved_in_unbraced_linkage_specification_p
15108     = parser->in_unbraced_linkage_specification_p;
15109   parser->in_unbraced_linkage_specification_p = false;
15110   /* Inside the function, surrounding template-parameter-lists do not
15111      apply.  */
15112   saved_num_template_parameter_lists
15113     = parser->num_template_parameter_lists;
15114   parser->num_template_parameter_lists = 0;
15115   /* If the next token is `try', then we are looking at a
15116      function-try-block.  */
15117   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15118     ctor_initializer_p = cp_parser_function_try_block (parser);
15119   /* A function-try-block includes the function-body, so we only do
15120      this next part if we're not processing a function-try-block.  */
15121   else
15122     ctor_initializer_p
15123       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15124
15125   /* Finish the function.  */
15126   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15127                         (inline_p ? 2 : 0));
15128   /* Generate code for it, if necessary.  */
15129   expand_or_defer_fn (fn);
15130   /* Restore the saved values.  */
15131   parser->in_unbraced_linkage_specification_p
15132     = saved_in_unbraced_linkage_specification_p;
15133   parser->num_template_parameter_lists
15134     = saved_num_template_parameter_lists;
15135
15136   return fn;
15137 }
15138
15139 /* Parse a template-declaration, assuming that the `export' (and
15140    `extern') keywords, if present, has already been scanned.  MEMBER_P
15141    is as for cp_parser_template_declaration.  */
15142
15143 static void
15144 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15145 {
15146   tree decl = NULL_TREE;
15147   tree parameter_list;
15148   bool friend_p = false;
15149
15150   /* Look for the `template' keyword.  */
15151   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15152     return;
15153
15154   /* And the `<'.  */
15155   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15156     return;
15157
15158   /* If the next token is `>', then we have an invalid
15159      specialization.  Rather than complain about an invalid template
15160      parameter, issue an error message here.  */
15161   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15162     {
15163       cp_parser_error (parser, "invalid explicit specialization");
15164       begin_specialization ();
15165       parameter_list = NULL_TREE;
15166     }
15167   else
15168     {
15169       /* Parse the template parameters.  */
15170       begin_template_parm_list ();
15171       parameter_list = cp_parser_template_parameter_list (parser);
15172       parameter_list = end_template_parm_list (parameter_list);
15173     }
15174
15175   /* Look for the `>'.  */
15176   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15177   /* We just processed one more parameter list.  */
15178   ++parser->num_template_parameter_lists;
15179   /* If the next token is `template', there are more template
15180      parameters.  */
15181   if (cp_lexer_next_token_is_keyword (parser->lexer,
15182                                       RID_TEMPLATE))
15183     cp_parser_template_declaration_after_export (parser, member_p);
15184   else
15185     {
15186       /* There are no access checks when parsing a template, as we do not
15187          know if a specialization will be a friend.  */
15188       push_deferring_access_checks (dk_no_check);
15189
15190       decl = cp_parser_single_declaration (parser,
15191                                            member_p,
15192                                            &friend_p);
15193
15194       pop_deferring_access_checks ();
15195
15196       /* If this is a member template declaration, let the front
15197          end know.  */
15198       if (member_p && !friend_p && decl)
15199         {
15200           if (TREE_CODE (decl) == TYPE_DECL)
15201             cp_parser_check_access_in_redeclaration (decl);
15202
15203           decl = finish_member_template_decl (decl);
15204         }
15205       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15206         make_friend_class (current_class_type, TREE_TYPE (decl),
15207                            /*complain=*/true);
15208     }
15209   /* We are done with the current parameter list.  */
15210   --parser->num_template_parameter_lists;
15211
15212   /* Finish up.  */
15213   finish_template_decl (parameter_list);
15214
15215   /* Register member declarations.  */
15216   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15217     finish_member_declaration (decl);
15218
15219   /* If DECL is a function template, we must return to parse it later.
15220      (Even though there is no definition, there might be default
15221      arguments that need handling.)  */
15222   if (member_p && decl
15223       && (TREE_CODE (decl) == FUNCTION_DECL
15224           || DECL_FUNCTION_TEMPLATE_P (decl)))
15225     TREE_VALUE (parser->unparsed_functions_queues)
15226       = tree_cons (NULL_TREE, decl,
15227                    TREE_VALUE (parser->unparsed_functions_queues));
15228 }
15229
15230 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15231    `function-definition' sequence.  MEMBER_P is true, this declaration
15232    appears in a class scope.
15233
15234    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15235    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15236
15237 static tree
15238 cp_parser_single_declaration (cp_parser* parser,
15239                               bool member_p,
15240                               bool* friend_p)
15241 {
15242   int declares_class_or_enum;
15243   tree decl = NULL_TREE;
15244   cp_decl_specifier_seq decl_specifiers;
15245   bool function_definition_p = false;
15246
15247   /* This function is only used when processing a template
15248      declaration.  */
15249   gcc_assert (innermost_scope_kind () == sk_template_parms
15250               || innermost_scope_kind () == sk_template_spec);
15251
15252   /* Defer access checks until we know what is being declared.  */
15253   push_deferring_access_checks (dk_deferred);
15254
15255   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15256      alternative.  */
15257   cp_parser_decl_specifier_seq (parser,
15258                                 CP_PARSER_FLAGS_OPTIONAL,
15259                                 &decl_specifiers,
15260                                 &declares_class_or_enum);
15261   if (friend_p)
15262     *friend_p = cp_parser_friend_p (&decl_specifiers);
15263
15264   /* There are no template typedefs.  */
15265   if (decl_specifiers.specs[(int) ds_typedef])
15266     {
15267       error ("template declaration of %qs", "typedef");
15268       decl = error_mark_node;
15269     }
15270
15271   /* Gather up the access checks that occurred the
15272      decl-specifier-seq.  */
15273   stop_deferring_access_checks ();
15274
15275   /* Check for the declaration of a template class.  */
15276   if (declares_class_or_enum)
15277     {
15278       if (cp_parser_declares_only_class_p (parser))
15279         {
15280           decl = shadow_tag (&decl_specifiers);
15281
15282           /* In this case:
15283
15284                struct C {
15285                  friend template <typename T> struct A<T>::B;
15286                };
15287
15288              A<T>::B will be represented by a TYPENAME_TYPE, and
15289              therefore not recognized by shadow_tag.  */
15290           if (friend_p && *friend_p
15291               && !decl
15292               && decl_specifiers.type
15293               && TYPE_P (decl_specifiers.type))
15294             decl = decl_specifiers.type;
15295
15296           if (decl && decl != error_mark_node)
15297             decl = TYPE_NAME (decl);
15298           else
15299             decl = error_mark_node;
15300         }
15301     }
15302   /* If it's not a template class, try for a template function.  If
15303      the next token is a `;', then this declaration does not declare
15304      anything.  But, if there were errors in the decl-specifiers, then
15305      the error might well have come from an attempted class-specifier.
15306      In that case, there's no need to warn about a missing declarator.  */
15307   if (!decl
15308       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15309           || decl_specifiers.type != error_mark_node))
15310     decl = cp_parser_init_declarator (parser,
15311                                       &decl_specifiers,
15312                                       /*function_definition_allowed_p=*/true,
15313                                       member_p,
15314                                       declares_class_or_enum,
15315                                       &function_definition_p);
15316
15317   pop_deferring_access_checks ();
15318
15319   /* Clear any current qualification; whatever comes next is the start
15320      of something new.  */
15321   parser->scope = NULL_TREE;
15322   parser->qualifying_scope = NULL_TREE;
15323   parser->object_scope = NULL_TREE;
15324   /* Look for a trailing `;' after the declaration.  */
15325   if (!function_definition_p
15326       && (decl == error_mark_node
15327           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15328     cp_parser_skip_to_end_of_block_or_statement (parser);
15329
15330   return decl;
15331 }
15332
15333 /* Parse a cast-expression that is not the operand of a unary "&".  */
15334
15335 static tree
15336 cp_parser_simple_cast_expression (cp_parser *parser)
15337 {
15338   return cp_parser_cast_expression (parser, /*address_p=*/false,
15339                                     /*cast_p=*/false);
15340 }
15341
15342 /* Parse a functional cast to TYPE.  Returns an expression
15343    representing the cast.  */
15344
15345 static tree
15346 cp_parser_functional_cast (cp_parser* parser, tree type)
15347 {
15348   tree expression_list;
15349   tree cast;
15350
15351   expression_list
15352     = cp_parser_parenthesized_expression_list (parser, false,
15353                                                /*cast_p=*/true,
15354                                                /*non_constant_p=*/NULL);
15355
15356   cast = build_functional_cast (type, expression_list);
15357   /* [expr.const]/1: In an integral constant expression "only type
15358      conversions to integral or enumeration type can be used".  */
15359   if (TREE_CODE (type) == TYPE_DECL)
15360     type = TREE_TYPE (type);
15361   if (cast != error_mark_node && !dependent_type_p (type)
15362       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15363     {
15364       if (cp_parser_non_integral_constant_expression
15365           (parser, "a call to a constructor"))
15366         return error_mark_node;
15367     }
15368   return cast;
15369 }
15370
15371 /* Save the tokens that make up the body of a member function defined
15372    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15373    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15374    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15375    for the member function.  */
15376
15377 static tree
15378 cp_parser_save_member_function_body (cp_parser* parser,
15379                                      cp_decl_specifier_seq *decl_specifiers,
15380                                      cp_declarator *declarator,
15381                                      tree attributes)
15382 {
15383   cp_token *first;
15384   cp_token *last;
15385   tree fn;
15386
15387   /* Create the function-declaration.  */
15388   fn = start_method (decl_specifiers, declarator, attributes);
15389   /* If something went badly wrong, bail out now.  */
15390   if (fn == error_mark_node)
15391     {
15392       /* If there's a function-body, skip it.  */
15393       if (cp_parser_token_starts_function_definition_p
15394           (cp_lexer_peek_token (parser->lexer)))
15395         cp_parser_skip_to_end_of_block_or_statement (parser);
15396       return error_mark_node;
15397     }
15398
15399   /* Remember it, if there default args to post process.  */
15400   cp_parser_save_default_args (parser, fn);
15401
15402   /* Save away the tokens that make up the body of the
15403      function.  */
15404   first = parser->lexer->next_token;
15405   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15406   /* Handle function try blocks.  */
15407   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15408     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15409   last = parser->lexer->next_token;
15410
15411   /* Save away the inline definition; we will process it when the
15412      class is complete.  */
15413   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15414   DECL_PENDING_INLINE_P (fn) = 1;
15415
15416   /* We need to know that this was defined in the class, so that
15417      friend templates are handled correctly.  */
15418   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15419
15420   /* We're done with the inline definition.  */
15421   finish_method (fn);
15422
15423   /* Add FN to the queue of functions to be parsed later.  */
15424   TREE_VALUE (parser->unparsed_functions_queues)
15425     = tree_cons (NULL_TREE, fn,
15426                  TREE_VALUE (parser->unparsed_functions_queues));
15427
15428   return fn;
15429 }
15430
15431 /* Parse a template-argument-list, as well as the trailing ">" (but
15432    not the opening ">").  See cp_parser_template_argument_list for the
15433    return value.  */
15434
15435 static tree
15436 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15437 {
15438   tree arguments;
15439   tree saved_scope;
15440   tree saved_qualifying_scope;
15441   tree saved_object_scope;
15442   bool saved_greater_than_is_operator_p;
15443   bool saved_skip_evaluation;
15444
15445   /* [temp.names]
15446
15447      When parsing a template-id, the first non-nested `>' is taken as
15448      the end of the template-argument-list rather than a greater-than
15449      operator.  */
15450   saved_greater_than_is_operator_p
15451     = parser->greater_than_is_operator_p;
15452   parser->greater_than_is_operator_p = false;
15453   /* Parsing the argument list may modify SCOPE, so we save it
15454      here.  */
15455   saved_scope = parser->scope;
15456   saved_qualifying_scope = parser->qualifying_scope;
15457   saved_object_scope = parser->object_scope;
15458   /* We need to evaluate the template arguments, even though this
15459      template-id may be nested within a "sizeof".  */
15460   saved_skip_evaluation = skip_evaluation;
15461   skip_evaluation = false;
15462   /* Parse the template-argument-list itself.  */
15463   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15464     arguments = NULL_TREE;
15465   else
15466     arguments = cp_parser_template_argument_list (parser);
15467   /* Look for the `>' that ends the template-argument-list. If we find
15468      a '>>' instead, it's probably just a typo.  */
15469   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15470     {
15471       if (!saved_greater_than_is_operator_p)
15472         {
15473           /* If we're in a nested template argument list, the '>>' has
15474             to be a typo for '> >'. We emit the error message, but we
15475             continue parsing and we push a '>' as next token, so that
15476             the argument list will be parsed correctly.  Note that the
15477             global source location is still on the token before the
15478             '>>', so we need to say explicitly where we want it.  */
15479           cp_token *token = cp_lexer_peek_token (parser->lexer);
15480           error ("%H%<>>%> should be %<> >%> "
15481                  "within a nested template argument list",
15482                  &token->location);
15483
15484           /* ??? Proper recovery should terminate two levels of
15485              template argument list here.  */
15486           token->type = CPP_GREATER;
15487         }
15488       else
15489         {
15490           /* If this is not a nested template argument list, the '>>'
15491             is a typo for '>'. Emit an error message and continue.
15492             Same deal about the token location, but here we can get it
15493             right by consuming the '>>' before issuing the diagnostic.  */
15494           cp_lexer_consume_token (parser->lexer);
15495           error ("spurious %<>>%>, use %<>%> to terminate "
15496                  "a template argument list");
15497         }
15498     }
15499   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15500     error ("missing %<>%> to terminate the template argument list");
15501   else
15502     /* It's what we want, a '>'; consume it.  */
15503     cp_lexer_consume_token (parser->lexer);
15504   /* The `>' token might be a greater-than operator again now.  */
15505   parser->greater_than_is_operator_p
15506     = saved_greater_than_is_operator_p;
15507   /* Restore the SAVED_SCOPE.  */
15508   parser->scope = saved_scope;
15509   parser->qualifying_scope = saved_qualifying_scope;
15510   parser->object_scope = saved_object_scope;
15511   skip_evaluation = saved_skip_evaluation;
15512
15513   return arguments;
15514 }
15515
15516 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15517    arguments, or the body of the function have not yet been parsed,
15518    parse them now.  */
15519
15520 static void
15521 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15522 {
15523   /* If this member is a template, get the underlying
15524      FUNCTION_DECL.  */
15525   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15526     member_function = DECL_TEMPLATE_RESULT (member_function);
15527
15528   /* There should not be any class definitions in progress at this
15529      point; the bodies of members are only parsed outside of all class
15530      definitions.  */
15531   gcc_assert (parser->num_classes_being_defined == 0);
15532   /* While we're parsing the member functions we might encounter more
15533      classes.  We want to handle them right away, but we don't want
15534      them getting mixed up with functions that are currently in the
15535      queue.  */
15536   parser->unparsed_functions_queues
15537     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15538
15539   /* Make sure that any template parameters are in scope.  */
15540   maybe_begin_member_template_processing (member_function);
15541
15542   /* If the body of the function has not yet been parsed, parse it
15543      now.  */
15544   if (DECL_PENDING_INLINE_P (member_function))
15545     {
15546       tree function_scope;
15547       cp_token_cache *tokens;
15548
15549       /* The function is no longer pending; we are processing it.  */
15550       tokens = DECL_PENDING_INLINE_INFO (member_function);
15551       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15552       DECL_PENDING_INLINE_P (member_function) = 0;
15553
15554       /* If this is a local class, enter the scope of the containing
15555          function.  */
15556       function_scope = current_function_decl;
15557       if (function_scope)
15558         push_function_context_to (function_scope);
15559
15560
15561       /* Push the body of the function onto the lexer stack.  */
15562       cp_parser_push_lexer_for_tokens (parser, tokens);
15563
15564       /* Let the front end know that we going to be defining this
15565          function.  */
15566       start_preparsed_function (member_function, NULL_TREE,
15567                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15568
15569       /* Don't do access checking if it is a templated function.  */
15570       if (processing_template_decl)
15571         push_deferring_access_checks (dk_no_check);
15572
15573       /* Now, parse the body of the function.  */
15574       cp_parser_function_definition_after_declarator (parser,
15575                                                       /*inline_p=*/true);
15576
15577       if (processing_template_decl)
15578         pop_deferring_access_checks ();
15579
15580       /* Leave the scope of the containing function.  */
15581       if (function_scope)
15582         pop_function_context_from (function_scope);
15583       cp_parser_pop_lexer (parser);
15584     }
15585
15586   /* Remove any template parameters from the symbol table.  */
15587   maybe_end_member_template_processing ();
15588
15589   /* Restore the queue.  */
15590   parser->unparsed_functions_queues
15591     = TREE_CHAIN (parser->unparsed_functions_queues);
15592 }
15593
15594 /* If DECL contains any default args, remember it on the unparsed
15595    functions queue.  */
15596
15597 static void
15598 cp_parser_save_default_args (cp_parser* parser, tree decl)
15599 {
15600   tree probe;
15601
15602   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15603        probe;
15604        probe = TREE_CHAIN (probe))
15605     if (TREE_PURPOSE (probe))
15606       {
15607         TREE_PURPOSE (parser->unparsed_functions_queues)
15608           = tree_cons (current_class_type, decl,
15609                        TREE_PURPOSE (parser->unparsed_functions_queues));
15610         break;
15611       }
15612   return;
15613 }
15614
15615 /* FN is a FUNCTION_DECL which may contains a parameter with an
15616    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15617    assumes that the current scope is the scope in which the default
15618    argument should be processed.  */
15619
15620 static void
15621 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15622 {
15623   bool saved_local_variables_forbidden_p;
15624   tree parm;
15625
15626   /* While we're parsing the default args, we might (due to the
15627      statement expression extension) encounter more classes.  We want
15628      to handle them right away, but we don't want them getting mixed
15629      up with default args that are currently in the queue.  */
15630   parser->unparsed_functions_queues
15631     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15632
15633   /* Local variable names (and the `this' keyword) may not appear
15634      in a default argument.  */
15635   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15636   parser->local_variables_forbidden_p = true;
15637
15638   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15639        parm;
15640        parm = TREE_CHAIN (parm))
15641     {
15642       cp_token_cache *tokens;
15643       tree default_arg = TREE_PURPOSE (parm);
15644       tree parsed_arg;
15645       VEC(tree,gc) *insts;
15646       tree copy;
15647       unsigned ix;
15648
15649       if (!default_arg)
15650         continue;
15651
15652       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15653         /* This can happen for a friend declaration for a function
15654            already declared with default arguments.  */
15655         continue;
15656
15657        /* Push the saved tokens for the default argument onto the parser's
15658           lexer stack.  */
15659       tokens = DEFARG_TOKENS (default_arg);
15660       cp_parser_push_lexer_for_tokens (parser, tokens);
15661
15662       /* Parse the assignment-expression.  */
15663       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
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"