OSDN Git Service

* c.opt, common.opt, config/bfin/bfin.opt, config/pa/pa.opt,
[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 && lexer->next_token != &eof_token);
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   while (n != 0)
509     {
510       ++token;
511       if (token == lexer->last_token)
512         {
513           token = (cp_token *)&eof_token;
514           break;
515         }
516
517       if (token->type != CPP_PURGED)
518         --n;
519     }
520
521   if (cp_lexer_debugging_p (lexer))
522     {
523       cp_lexer_print_token (cp_lexer_debug_stream, token);
524       putc ('\n', cp_lexer_debug_stream);
525     }
526
527   return token;
528 }
529
530 /* Return the next token, and advance the lexer's next_token pointer
531    to point to the next non-purged token.  */
532
533 static cp_token *
534 cp_lexer_consume_token (cp_lexer* lexer)
535 {
536   cp_token *token = lexer->next_token;
537
538   gcc_assert (token != &eof_token);
539
540   do
541     {
542       lexer->next_token++;
543       if (lexer->next_token == lexer->last_token)
544         {
545           lexer->next_token = (cp_token *)&eof_token;
546           break;
547         }
548
549     }
550   while (lexer->next_token->type == CPP_PURGED);
551
552   cp_lexer_set_source_position_from_token (token);
553
554   /* Provide debugging output.  */
555   if (cp_lexer_debugging_p (lexer))
556     {
557       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
558       cp_lexer_print_token (cp_lexer_debug_stream, token);
559       putc ('\n', cp_lexer_debug_stream);
560     }
561
562   return token;
563 }
564
565 /* Permanently remove the next token from the token stream, and
566    advance the next_token pointer to refer to the next non-purged
567    token.  */
568
569 static void
570 cp_lexer_purge_token (cp_lexer *lexer)
571 {
572   cp_token *tok = lexer->next_token;
573
574   gcc_assert (tok != &eof_token);
575   tok->type = CPP_PURGED;
576   tok->location = UNKNOWN_LOCATION;
577   tok->value = NULL_TREE;
578   tok->keyword = RID_MAX;
579
580   do
581     {
582       tok++;
583       if (tok == lexer->last_token)
584         {
585           tok = (cp_token *)&eof_token;
586           break;
587         }
588     }
589   while (tok->type == CPP_PURGED);
590   lexer->next_token = tok;
591 }
592
593 /* Permanently remove all tokens after TOK, up to, but not
594    including, the token that will be returned next by
595    cp_lexer_peek_token.  */
596
597 static void
598 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
599 {
600   cp_token *peek = lexer->next_token;
601
602   if (peek == &eof_token)
603     peek = lexer->last_token;
604
605   gcc_assert (tok < peek);
606
607   for ( tok += 1; tok != peek; tok += 1)
608     {
609       tok->type = CPP_PURGED;
610       tok->location = UNKNOWN_LOCATION;
611       tok->value = NULL_TREE;
612       tok->keyword = RID_MAX;
613     }
614 }
615
616 /* Consume and handle a pragma token.  */
617 static void
618 cp_lexer_handle_pragma (cp_lexer *lexer)
619 {
620   cpp_string s;
621   cp_token *token = cp_lexer_consume_token (lexer);
622   gcc_assert (token->type == CPP_PRAGMA);
623   gcc_assert (token->value);
624
625   s.len = TREE_STRING_LENGTH (token->value);
626   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
627
628   cpp_handle_deferred_pragma (parse_in, &s);
629
630   /* Clearing token->value here means that we will get an ICE if we
631      try to process this #pragma again (which should be impossible).  */
632   token->value = NULL;
633 }
634
635 /* Begin saving tokens.  All tokens consumed after this point will be
636    preserved.  */
637
638 static void
639 cp_lexer_save_tokens (cp_lexer* lexer)
640 {
641   /* Provide debugging output.  */
642   if (cp_lexer_debugging_p (lexer))
643     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
644
645   VEC_safe_push (cp_token_position, heap,
646                  lexer->saved_tokens, lexer->next_token);
647 }
648
649 /* Commit to the portion of the token stream most recently saved.  */
650
651 static void
652 cp_lexer_commit_tokens (cp_lexer* lexer)
653 {
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
657
658   VEC_pop (cp_token_position, lexer->saved_tokens);
659 }
660
661 /* Return all tokens saved since the last call to cp_lexer_save_tokens
662    to the token stream.  Stop saving tokens.  */
663
664 static void
665 cp_lexer_rollback_tokens (cp_lexer* lexer)
666 {
667   /* Provide debugging output.  */
668   if (cp_lexer_debugging_p (lexer))
669     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
670
671   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
672 }
673
674 /* Print a representation of the TOKEN on the STREAM.  */
675
676 #ifdef ENABLE_CHECKING
677
678 static void
679 cp_lexer_print_token (FILE * stream, cp_token *token)
680 {
681   /* We don't use cpp_type2name here because the parser defines
682      a few tokens of its own.  */
683   static const char *const token_names[] = {
684     /* cpplib-defined token types */
685 #define OP(e, s) #e,
686 #define TK(e, s) #e,
687     TTYPE_TABLE
688 #undef OP
689 #undef TK
690     /* C++ parser token types - see "Manifest constants", above.  */
691     "KEYWORD",
692     "TEMPLATE_ID",
693     "NESTED_NAME_SPECIFIER",
694     "PURGED"
695   };
696
697   /* If we have a name for the token, print it out.  Otherwise, we
698      simply give the numeric code.  */
699   gcc_assert (token->type < ARRAY_SIZE(token_names));
700   fputs (token_names[token->type], stream);
701
702   /* For some tokens, print the associated data.  */
703   switch (token->type)
704     {
705     case CPP_KEYWORD:
706       /* Some keywords have a value that is not an IDENTIFIER_NODE.
707          For example, `struct' is mapped to an INTEGER_CST.  */
708       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
709         break;
710       /* else fall through */
711     case CPP_NAME:
712       fputs (IDENTIFIER_POINTER (token->value), stream);
713       break;
714
715     case CPP_STRING:
716     case CPP_WSTRING:
717     case CPP_PRAGMA:
718       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
719       break;
720
721     default:
722       break;
723     }
724 }
725
726 /* Start emitting debugging information.  */
727
728 static void
729 cp_lexer_start_debugging (cp_lexer* lexer)
730 {
731   lexer->debugging_p = true;
732 }
733
734 /* Stop emitting debugging information.  */
735
736 static void
737 cp_lexer_stop_debugging (cp_lexer* lexer)
738 {
739   lexer->debugging_p = false;
740 }
741
742 #endif /* ENABLE_CHECKING */
743
744 /* Create a new cp_token_cache, representing a range of tokens.  */
745
746 static cp_token_cache *
747 cp_token_cache_new (cp_token *first, cp_token *last)
748 {
749   cp_token_cache *cache = GGC_NEW (cp_token_cache);
750   cache->first = first;
751   cache->last = last;
752   return cache;
753 }
754
755 \f
756 /* Decl-specifiers.  */
757
758 static void clear_decl_specs
759   (cp_decl_specifier_seq *);
760
761 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
762
763 static void
764 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
765 {
766   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
767 }
768
769 /* Declarators.  */
770
771 /* Nothing other than the parser should be creating declarators;
772    declarators are a semi-syntactic representation of C++ entities.
773    Other parts of the front end that need to create entities (like
774    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
775
776 static cp_declarator *make_call_declarator
777   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
778 static cp_declarator *make_array_declarator
779   (cp_declarator *, tree);
780 static cp_declarator *make_pointer_declarator
781   (cp_cv_quals, cp_declarator *);
782 static cp_declarator *make_reference_declarator
783   (cp_cv_quals, cp_declarator *);
784 static cp_parameter_declarator *make_parameter_declarator
785   (cp_decl_specifier_seq *, cp_declarator *, tree);
786 static cp_declarator *make_ptrmem_declarator
787   (cp_cv_quals, tree, cp_declarator *);
788
789 cp_declarator *cp_error_declarator;
790
791 /* The obstack on which declarators and related data structures are
792    allocated.  */
793 static struct obstack declarator_obstack;
794
795 /* Alloc BYTES from the declarator memory pool.  */
796
797 static inline void *
798 alloc_declarator (size_t bytes)
799 {
800   return obstack_alloc (&declarator_obstack, bytes);
801 }
802
803 /* Allocate a declarator of the indicated KIND.  Clear fields that are
804    common to all declarators.  */
805
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
808 {
809   cp_declarator *declarator;
810
811   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812   declarator->kind = kind;
813   declarator->attributes = NULL_TREE;
814   declarator->declarator = NULL;
815
816   return declarator;
817 }
818
819 /* Make a declarator for a generalized identifier.  If non-NULL, the
820    identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
821    just UNQUALIFIED_NAME.  */
822
823 static cp_declarator *
824 make_id_declarator (tree qualifying_scope, tree unqualified_name)
825 {
826   cp_declarator *declarator;
827
828   /* It is valid to write:
829
830        class C { void f(); };
831        typedef C D;
832        void D::f();
833
834      The standard is not clear about whether `typedef const C D' is
835      legal; as of 2002-09-15 the committee is considering that
836      question.  EDG 3.0 allows that syntax.  Therefore, we do as
837      well.  */
838   if (qualifying_scope && TYPE_P (qualifying_scope))
839     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
840
841   declarator = make_declarator (cdk_id);
842   declarator->u.id.qualifying_scope = qualifying_scope;
843   declarator->u.id.unqualified_name = unqualified_name;
844   declarator->u.id.sfk = sfk_none;
845
846   return declarator;
847 }
848
849 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
850    of modifiers such as const or volatile to apply to the pointer
851    type, represented as identifiers.  */
852
853 cp_declarator *
854 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
855 {
856   cp_declarator *declarator;
857
858   declarator = make_declarator (cdk_pointer);
859   declarator->declarator = target;
860   declarator->u.pointer.qualifiers = cv_qualifiers;
861   declarator->u.pointer.class_type = NULL_TREE;
862
863   return declarator;
864 }
865
866 /* Like make_pointer_declarator -- but for references.  */
867
868 cp_declarator *
869 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
870 {
871   cp_declarator *declarator;
872
873   declarator = make_declarator (cdk_reference);
874   declarator->declarator = target;
875   declarator->u.pointer.qualifiers = cv_qualifiers;
876   declarator->u.pointer.class_type = NULL_TREE;
877
878   return declarator;
879 }
880
881 /* Like make_pointer_declarator -- but for a pointer to a non-static
882    member of CLASS_TYPE.  */
883
884 cp_declarator *
885 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
886                         cp_declarator *pointee)
887 {
888   cp_declarator *declarator;
889
890   declarator = make_declarator (cdk_ptrmem);
891   declarator->declarator = pointee;
892   declarator->u.pointer.qualifiers = cv_qualifiers;
893   declarator->u.pointer.class_type = class_type;
894
895   return declarator;
896 }
897
898 /* Make a declarator for the function given by TARGET, with the
899    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
900    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
901    indicates what exceptions can be thrown.  */
902
903 cp_declarator *
904 make_call_declarator (cp_declarator *target,
905                       cp_parameter_declarator *parms,
906                       cp_cv_quals cv_qualifiers,
907                       tree exception_specification)
908 {
909   cp_declarator *declarator;
910
911   declarator = make_declarator (cdk_function);
912   declarator->declarator = target;
913   declarator->u.function.parameters = parms;
914   declarator->u.function.qualifiers = cv_qualifiers;
915   declarator->u.function.exception_specification = exception_specification;
916
917   return declarator;
918 }
919
920 /* Make a declarator for an array of BOUNDS elements, each of which is
921    defined by ELEMENT.  */
922
923 cp_declarator *
924 make_array_declarator (cp_declarator *element, tree bounds)
925 {
926   cp_declarator *declarator;
927
928   declarator = make_declarator (cdk_array);
929   declarator->declarator = element;
930   declarator->u.array.bounds = bounds;
931
932   return declarator;
933 }
934
935 cp_parameter_declarator *no_parameters;
936
937 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
938    DECLARATOR and DEFAULT_ARGUMENT.  */
939
940 cp_parameter_declarator *
941 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
942                            cp_declarator *declarator,
943                            tree default_argument)
944 {
945   cp_parameter_declarator *parameter;
946
947   parameter = ((cp_parameter_declarator *)
948                alloc_declarator (sizeof (cp_parameter_declarator)));
949   parameter->next = NULL;
950   if (decl_specifiers)
951     parameter->decl_specifiers = *decl_specifiers;
952   else
953     clear_decl_specs (&parameter->decl_specifiers);
954   parameter->declarator = declarator;
955   parameter->default_argument = default_argument;
956   parameter->ellipsis_p = false;
957
958   return parameter;
959 }
960
961 /* The parser.  */
962
963 /* Overview
964    --------
965
966    A cp_parser parses the token stream as specified by the C++
967    grammar.  Its job is purely parsing, not semantic analysis.  For
968    example, the parser breaks the token stream into declarators,
969    expressions, statements, and other similar syntactic constructs.
970    It does not check that the types of the expressions on either side
971    of an assignment-statement are compatible, or that a function is
972    not declared with a parameter of type `void'.
973
974    The parser invokes routines elsewhere in the compiler to perform
975    semantic analysis and to build up the abstract syntax tree for the
976    code processed.
977
978    The parser (and the template instantiation code, which is, in a
979    way, a close relative of parsing) are the only parts of the
980    compiler that should be calling push_scope and pop_scope, or
981    related functions.  The parser (and template instantiation code)
982    keeps track of what scope is presently active; everything else
983    should simply honor that.  (The code that generates static
984    initializers may also need to set the scope, in order to check
985    access control correctly when emitting the initializers.)
986
987    Methodology
988    -----------
989
990    The parser is of the standard recursive-descent variety.  Upcoming
991    tokens in the token stream are examined in order to determine which
992    production to use when parsing a non-terminal.  Some C++ constructs
993    require arbitrary look ahead to disambiguate.  For example, it is
994    impossible, in the general case, to tell whether a statement is an
995    expression or declaration without scanning the entire statement.
996    Therefore, the parser is capable of "parsing tentatively."  When the
997    parser is not sure what construct comes next, it enters this mode.
998    Then, while we attempt to parse the construct, the parser queues up
999    error messages, rather than issuing them immediately, and saves the
1000    tokens it consumes.  If the construct is parsed successfully, the
1001    parser "commits", i.e., it issues any queued error messages and
1002    the tokens that were being preserved are permanently discarded.
1003    If, however, the construct is not parsed successfully, the parser
1004    rolls back its state completely so that it can resume parsing using
1005    a different alternative.
1006
1007    Future Improvements
1008    -------------------
1009
1010    The performance of the parser could probably be improved substantially.
1011    We could often eliminate the need to parse tentatively by looking ahead
1012    a little bit.  In some places, this approach might not entirely eliminate
1013    the need to parse tentatively, but it might still speed up the average
1014    case.  */
1015
1016 /* Flags that are passed to some parsing functions.  These values can
1017    be bitwise-ored together.  */
1018
1019 typedef enum cp_parser_flags
1020 {
1021   /* No flags.  */
1022   CP_PARSER_FLAGS_NONE = 0x0,
1023   /* The construct is optional.  If it is not present, then no error
1024      should be issued.  */
1025   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1026   /* When parsing a type-specifier, do not allow user-defined types.  */
1027   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1028 } cp_parser_flags;
1029
1030 /* The different kinds of declarators we want to parse.  */
1031
1032 typedef enum cp_parser_declarator_kind
1033 {
1034   /* We want an abstract declarator.  */
1035   CP_PARSER_DECLARATOR_ABSTRACT,
1036   /* We want a named declarator.  */
1037   CP_PARSER_DECLARATOR_NAMED,
1038   /* We don't mind, but the name must be an unqualified-id.  */
1039   CP_PARSER_DECLARATOR_EITHER
1040 } cp_parser_declarator_kind;
1041
1042 /* The precedence values used to parse binary expressions.  The minimum value
1043    of PREC must be 1, because zero is reserved to quickly discriminate
1044    binary operators from other tokens.  */
1045
1046 enum cp_parser_prec
1047 {
1048   PREC_NOT_OPERATOR,
1049   PREC_LOGICAL_OR_EXPRESSION,
1050   PREC_LOGICAL_AND_EXPRESSION,
1051   PREC_INCLUSIVE_OR_EXPRESSION,
1052   PREC_EXCLUSIVE_OR_EXPRESSION,
1053   PREC_AND_EXPRESSION,
1054   PREC_EQUALITY_EXPRESSION,
1055   PREC_RELATIONAL_EXPRESSION,
1056   PREC_SHIFT_EXPRESSION,
1057   PREC_ADDITIVE_EXPRESSION,
1058   PREC_MULTIPLICATIVE_EXPRESSION,
1059   PREC_PM_EXPRESSION,
1060   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1061 };
1062
1063 /* A mapping from a token type to a corresponding tree node type, with a
1064    precedence value.  */
1065
1066 typedef struct cp_parser_binary_operations_map_node
1067 {
1068   /* The token type.  */
1069   enum cpp_ttype token_type;
1070   /* The corresponding tree code.  */
1071   enum tree_code tree_type;
1072   /* The precedence of this operator.  */
1073   enum cp_parser_prec prec;
1074 } cp_parser_binary_operations_map_node;
1075
1076 /* The status of a tentative parse.  */
1077
1078 typedef enum cp_parser_status_kind
1079 {
1080   /* No errors have occurred.  */
1081   CP_PARSER_STATUS_KIND_NO_ERROR,
1082   /* An error has occurred.  */
1083   CP_PARSER_STATUS_KIND_ERROR,
1084   /* We are committed to this tentative parse, whether or not an error
1085      has occurred.  */
1086   CP_PARSER_STATUS_KIND_COMMITTED
1087 } cp_parser_status_kind;
1088
1089 typedef struct cp_parser_expression_stack_entry
1090 {
1091   tree lhs;
1092   enum tree_code tree_type;
1093   int prec;
1094 } cp_parser_expression_stack_entry;
1095
1096 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1097    entries because precedence levels on the stack are monotonically
1098    increasing.  */
1099 typedef struct cp_parser_expression_stack_entry
1100   cp_parser_expression_stack[NUM_PREC_VALUES];
1101
1102 /* Context that is saved and restored when parsing tentatively.  */
1103 typedef struct cp_parser_context GTY (())
1104 {
1105   /* If this is a tentative parsing context, the status of the
1106      tentative parse.  */
1107   enum cp_parser_status_kind status;
1108   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1109      that are looked up in this context must be looked up both in the
1110      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1111      the context of the containing expression.  */
1112   tree object_type;
1113
1114   /* The next parsing context in the stack.  */
1115   struct cp_parser_context *next;
1116 } cp_parser_context;
1117
1118 /* Prototypes.  */
1119
1120 /* Constructors and destructors.  */
1121
1122 static cp_parser_context *cp_parser_context_new
1123   (cp_parser_context *);
1124
1125 /* Class variables.  */
1126
1127 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1128
1129 /* The operator-precedence table used by cp_parser_binary_expression.
1130    Transformed into an associative array (binops_by_token) by
1131    cp_parser_new.  */
1132
1133 static const cp_parser_binary_operations_map_node binops[] = {
1134   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1135   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1136
1137   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1138   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140
1141   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1142   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143
1144   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1145   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146
1147   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1148   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1150   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1152   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1153
1154   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1156
1157   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1158
1159   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1160
1161   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1162
1163   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1164
1165   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1166 };
1167
1168 /* The same as binops, but initialized by cp_parser_new so that
1169    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1170    for speed.  */
1171 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1172
1173 /* Constructors and destructors.  */
1174
1175 /* Construct a new context.  The context below this one on the stack
1176    is given by NEXT.  */
1177
1178 static cp_parser_context *
1179 cp_parser_context_new (cp_parser_context* next)
1180 {
1181   cp_parser_context *context;
1182
1183   /* Allocate the storage.  */
1184   if (cp_parser_context_free_list != NULL)
1185     {
1186       /* Pull the first entry from the free list.  */
1187       context = cp_parser_context_free_list;
1188       cp_parser_context_free_list = context->next;
1189       memset (context, 0, sizeof (*context));
1190     }
1191   else
1192     context = GGC_CNEW (cp_parser_context);
1193
1194   /* No errors have occurred yet in this context.  */
1195   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196   /* If this is not the bottomost context, copy information that we
1197      need from the previous context.  */
1198   if (next)
1199     {
1200       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201          expression, then we are parsing one in this context, too.  */
1202       context->object_type = next->object_type;
1203       /* Thread the stack.  */
1204       context->next = next;
1205     }
1206
1207   return context;
1208 }
1209
1210 /* The cp_parser structure represents the C++ parser.  */
1211
1212 typedef struct cp_parser GTY(())
1213 {
1214   /* The lexer from which we are obtaining tokens.  */
1215   cp_lexer *lexer;
1216
1217   /* The scope in which names should be looked up.  If NULL_TREE, then
1218      we look up names in the scope that is currently open in the
1219      source program.  If non-NULL, this is either a TYPE or
1220      NAMESPACE_DECL for the scope in which we should look.  It can
1221      also be ERROR_MARK, when we've parsed a bogus scope.
1222
1223      This value is not cleared automatically after a name is looked
1224      up, so we must be careful to clear it before starting a new look
1225      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1226      will look up `Z' in the scope of `X', rather than the current
1227      scope.)  Unfortunately, it is difficult to tell when name lookup
1228      is complete, because we sometimes peek at a token, look it up,
1229      and then decide not to consume it.   */
1230   tree scope;
1231
1232   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1233      last lookup took place.  OBJECT_SCOPE is used if an expression
1234      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1235      respectively.  QUALIFYING_SCOPE is used for an expression of the
1236      form "X::Y"; it refers to X.  */
1237   tree object_scope;
1238   tree qualifying_scope;
1239
1240   /* A stack of parsing contexts.  All but the bottom entry on the
1241      stack will be tentative contexts.
1242
1243      We parse tentatively in order to determine which construct is in
1244      use in some situations.  For example, in order to determine
1245      whether a statement is an expression-statement or a
1246      declaration-statement we parse it tentatively as a
1247      declaration-statement.  If that fails, we then reparse the same
1248      token stream as an expression-statement.  */
1249   cp_parser_context *context;
1250
1251   /* True if we are parsing GNU C++.  If this flag is not set, then
1252      GNU extensions are not recognized.  */
1253   bool allow_gnu_extensions_p;
1254
1255   /* TRUE if the `>' token should be interpreted as the greater-than
1256      operator.  FALSE if it is the end of a template-id or
1257      template-parameter-list.  */
1258   bool greater_than_is_operator_p;
1259
1260   /* TRUE if default arguments are allowed within a parameter list
1261      that starts at this point. FALSE if only a gnu extension makes
1262      them permissible.  */
1263   bool default_arg_ok_p;
1264
1265   /* TRUE if we are parsing an integral constant-expression.  See
1266      [expr.const] for a precise definition.  */
1267   bool integral_constant_expression_p;
1268
1269   /* TRUE if we are parsing an integral constant-expression -- but a
1270      non-constant expression should be permitted as well.  This flag
1271      is used when parsing an array bound so that GNU variable-length
1272      arrays are tolerated.  */
1273   bool allow_non_integral_constant_expression_p;
1274
1275   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1276      been seen that makes the expression non-constant.  */
1277   bool non_integral_constant_expression_p;
1278
1279   /* TRUE if local variable names and `this' are forbidden in the
1280      current context.  */
1281   bool local_variables_forbidden_p;
1282
1283   /* TRUE if the declaration we are parsing is part of a
1284      linkage-specification of the form `extern string-literal
1285      declaration'.  */
1286   bool in_unbraced_linkage_specification_p;
1287
1288   /* TRUE if we are presently parsing a declarator, after the
1289      direct-declarator.  */
1290   bool in_declarator_p;
1291
1292   /* TRUE if we are presently parsing a template-argument-list.  */
1293   bool in_template_argument_list_p;
1294
1295   /* TRUE if we are presently parsing the body of an
1296      iteration-statement.  */
1297   bool in_iteration_statement_p;
1298
1299   /* TRUE if we are presently parsing the body of a switch
1300      statement.  */
1301   bool in_switch_statement_p;
1302
1303   /* TRUE if we are parsing a type-id in an expression context.  In
1304      such a situation, both "type (expr)" and "type (type)" are valid
1305      alternatives.  */
1306   bool in_type_id_in_expr_p;
1307
1308   /* TRUE if we are currently in a header file where declarations are
1309      implicitly extern "C".  */
1310   bool implicit_extern_c;
1311
1312   /* TRUE if strings in expressions should be translated to the execution
1313      character set.  */
1314   bool translate_strings_p;
1315
1316   /* If non-NULL, then we are parsing a construct where new type
1317      definitions are not permitted.  The string stored here will be
1318      issued as an error message if a type is defined.  */
1319   const char *type_definition_forbidden_message;
1320
1321   /* A list of lists. The outer list is a stack, used for member
1322      functions of local classes. At each level there are two sub-list,
1323      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1324      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1325      TREE_VALUE's. The functions are chained in reverse declaration
1326      order.
1327
1328      The TREE_PURPOSE sublist contains those functions with default
1329      arguments that need post processing, and the TREE_VALUE sublist
1330      contains those functions with definitions that need post
1331      processing.
1332
1333      These lists can only be processed once the outermost class being
1334      defined is complete.  */
1335   tree unparsed_functions_queues;
1336
1337   /* The number of classes whose definitions are currently in
1338      progress.  */
1339   unsigned num_classes_being_defined;
1340
1341   /* The number of template parameter lists that apply directly to the
1342      current declaration.  */
1343   unsigned num_template_parameter_lists;
1344 } cp_parser;
1345
1346 /* The type of a function that parses some kind of expression.  */
1347 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1348
1349 /* Prototypes.  */
1350
1351 /* Constructors and destructors.  */
1352
1353 static cp_parser *cp_parser_new
1354   (void);
1355
1356 /* Routines to parse various constructs.
1357
1358    Those that return `tree' will return the error_mark_node (rather
1359    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1360    Sometimes, they will return an ordinary node if error-recovery was
1361    attempted, even though a parse error occurred.  So, to check
1362    whether or not a parse error occurred, you should always use
1363    cp_parser_error_occurred.  If the construct is optional (indicated
1364    either by an `_opt' in the name of the function that does the
1365    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1366    the construct is not present.  */
1367
1368 /* Lexical conventions [gram.lex]  */
1369
1370 static tree cp_parser_identifier
1371   (cp_parser *);
1372 static tree cp_parser_string_literal
1373   (cp_parser *, bool, bool);
1374
1375 /* Basic concepts [gram.basic]  */
1376
1377 static bool cp_parser_translation_unit
1378   (cp_parser *);
1379
1380 /* Expressions [gram.expr]  */
1381
1382 static tree cp_parser_primary_expression
1383   (cp_parser *, bool, cp_id_kind *, tree *);
1384 static tree cp_parser_id_expression
1385   (cp_parser *, bool, bool, bool *, bool);
1386 static tree cp_parser_unqualified_id
1387   (cp_parser *, bool, bool, bool);
1388 static tree cp_parser_nested_name_specifier_opt
1389   (cp_parser *, bool, bool, bool, bool);
1390 static tree cp_parser_nested_name_specifier
1391   (cp_parser *, bool, bool, bool, bool);
1392 static tree cp_parser_class_or_namespace_name
1393   (cp_parser *, bool, bool, bool, bool, bool);
1394 static tree cp_parser_postfix_expression
1395   (cp_parser *, bool, bool);
1396 static tree cp_parser_postfix_open_square_expression
1397   (cp_parser *, tree, bool);
1398 static tree cp_parser_postfix_dot_deref_expression
1399   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1400 static tree cp_parser_parenthesized_expression_list
1401   (cp_parser *, bool, bool, bool *);
1402 static void cp_parser_pseudo_destructor_name
1403   (cp_parser *, tree *, tree *);
1404 static tree cp_parser_unary_expression
1405   (cp_parser *, bool, bool);
1406 static enum tree_code cp_parser_unary_operator
1407   (cp_token *);
1408 static tree cp_parser_new_expression
1409   (cp_parser *);
1410 static tree cp_parser_new_placement
1411   (cp_parser *);
1412 static tree cp_parser_new_type_id
1413   (cp_parser *, tree *);
1414 static cp_declarator *cp_parser_new_declarator_opt
1415   (cp_parser *);
1416 static cp_declarator *cp_parser_direct_new_declarator
1417   (cp_parser *);
1418 static tree cp_parser_new_initializer
1419   (cp_parser *);
1420 static tree cp_parser_delete_expression
1421   (cp_parser *);
1422 static tree cp_parser_cast_expression
1423   (cp_parser *, bool, bool);
1424 static tree cp_parser_binary_expression
1425   (cp_parser *, bool);
1426 static tree cp_parser_question_colon_clause
1427   (cp_parser *, tree);
1428 static tree cp_parser_assignment_expression
1429   (cp_parser *, bool);
1430 static enum tree_code cp_parser_assignment_operator_opt
1431   (cp_parser *);
1432 static tree cp_parser_expression
1433   (cp_parser *, bool);
1434 static tree cp_parser_constant_expression
1435   (cp_parser *, bool, bool *);
1436 static tree cp_parser_builtin_offsetof
1437   (cp_parser *);
1438
1439 /* Statements [gram.stmt.stmt]  */
1440
1441 static void cp_parser_statement
1442   (cp_parser *, tree);
1443 static tree cp_parser_labeled_statement
1444   (cp_parser *, tree);
1445 static tree cp_parser_expression_statement
1446   (cp_parser *, tree);
1447 static tree cp_parser_compound_statement
1448   (cp_parser *, tree, bool);
1449 static void cp_parser_statement_seq_opt
1450   (cp_parser *, tree);
1451 static tree cp_parser_selection_statement
1452   (cp_parser *);
1453 static tree cp_parser_condition
1454   (cp_parser *);
1455 static tree cp_parser_iteration_statement
1456   (cp_parser *);
1457 static void cp_parser_for_init_statement
1458   (cp_parser *);
1459 static tree cp_parser_jump_statement
1460   (cp_parser *);
1461 static void cp_parser_declaration_statement
1462   (cp_parser *);
1463
1464 static tree cp_parser_implicitly_scoped_statement
1465   (cp_parser *);
1466 static void cp_parser_already_scoped_statement
1467   (cp_parser *);
1468
1469 /* Declarations [gram.dcl.dcl] */
1470
1471 static void cp_parser_declaration_seq_opt
1472   (cp_parser *);
1473 static void cp_parser_declaration
1474   (cp_parser *);
1475 static void cp_parser_block_declaration
1476   (cp_parser *, bool);
1477 static void cp_parser_simple_declaration
1478   (cp_parser *, bool);
1479 static void cp_parser_decl_specifier_seq
1480   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1481 static tree cp_parser_storage_class_specifier_opt
1482   (cp_parser *);
1483 static tree cp_parser_function_specifier_opt
1484   (cp_parser *, cp_decl_specifier_seq *);
1485 static tree cp_parser_type_specifier
1486   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1487    int *, bool *);
1488 static tree cp_parser_simple_type_specifier
1489   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1490 static tree cp_parser_type_name
1491   (cp_parser *);
1492 static tree cp_parser_elaborated_type_specifier
1493   (cp_parser *, bool, bool);
1494 static tree cp_parser_enum_specifier
1495   (cp_parser *);
1496 static void cp_parser_enumerator_list
1497   (cp_parser *, tree);
1498 static void cp_parser_enumerator_definition
1499   (cp_parser *, tree);
1500 static tree cp_parser_namespace_name
1501   (cp_parser *);
1502 static void cp_parser_namespace_definition
1503   (cp_parser *);
1504 static void cp_parser_namespace_body
1505   (cp_parser *);
1506 static tree cp_parser_qualified_namespace_specifier
1507   (cp_parser *);
1508 static void cp_parser_namespace_alias_definition
1509   (cp_parser *);
1510 static void cp_parser_using_declaration
1511   (cp_parser *);
1512 static void cp_parser_using_directive
1513   (cp_parser *);
1514 static void cp_parser_asm_definition
1515   (cp_parser *);
1516 static void cp_parser_linkage_specification
1517   (cp_parser *);
1518
1519 /* Declarators [gram.dcl.decl] */
1520
1521 static tree cp_parser_init_declarator
1522   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1523 static cp_declarator *cp_parser_declarator
1524   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1525 static cp_declarator *cp_parser_direct_declarator
1526   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1527 static enum tree_code cp_parser_ptr_operator
1528   (cp_parser *, tree *, cp_cv_quals *);
1529 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1530   (cp_parser *);
1531 static tree cp_parser_declarator_id
1532   (cp_parser *);
1533 static tree cp_parser_type_id
1534   (cp_parser *);
1535 static void cp_parser_type_specifier_seq
1536   (cp_parser *, bool, cp_decl_specifier_seq *);
1537 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1538   (cp_parser *);
1539 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1540   (cp_parser *, bool *);
1541 static cp_parameter_declarator *cp_parser_parameter_declaration
1542   (cp_parser *, bool, bool *);
1543 static void cp_parser_function_body
1544   (cp_parser *);
1545 static tree cp_parser_initializer
1546   (cp_parser *, bool *, bool *);
1547 static tree cp_parser_initializer_clause
1548   (cp_parser *, bool *);
1549 static tree cp_parser_initializer_list
1550   (cp_parser *, bool *);
1551
1552 static bool cp_parser_ctor_initializer_opt_and_function_body
1553   (cp_parser *);
1554
1555 /* Classes [gram.class] */
1556
1557 static tree cp_parser_class_name
1558   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1559 static tree cp_parser_class_specifier
1560   (cp_parser *);
1561 static tree cp_parser_class_head
1562   (cp_parser *, bool *, tree *);
1563 static enum tag_types cp_parser_class_key
1564   (cp_parser *);
1565 static void cp_parser_member_specification_opt
1566   (cp_parser *);
1567 static void cp_parser_member_declaration
1568   (cp_parser *);
1569 static tree cp_parser_pure_specifier
1570   (cp_parser *);
1571 static tree cp_parser_constant_initializer
1572   (cp_parser *);
1573
1574 /* Derived classes [gram.class.derived] */
1575
1576 static tree cp_parser_base_clause
1577   (cp_parser *);
1578 static tree cp_parser_base_specifier
1579   (cp_parser *);
1580
1581 /* Special member functions [gram.special] */
1582
1583 static tree cp_parser_conversion_function_id
1584   (cp_parser *);
1585 static tree cp_parser_conversion_type_id
1586   (cp_parser *);
1587 static cp_declarator *cp_parser_conversion_declarator_opt
1588   (cp_parser *);
1589 static bool cp_parser_ctor_initializer_opt
1590   (cp_parser *);
1591 static void cp_parser_mem_initializer_list
1592   (cp_parser *);
1593 static tree cp_parser_mem_initializer
1594   (cp_parser *);
1595 static tree cp_parser_mem_initializer_id
1596   (cp_parser *);
1597
1598 /* Overloading [gram.over] */
1599
1600 static tree cp_parser_operator_function_id
1601   (cp_parser *);
1602 static tree cp_parser_operator
1603   (cp_parser *);
1604
1605 /* Templates [gram.temp] */
1606
1607 static void cp_parser_template_declaration
1608   (cp_parser *, bool);
1609 static tree cp_parser_template_parameter_list
1610   (cp_parser *);
1611 static tree cp_parser_template_parameter
1612   (cp_parser *, bool *);
1613 static tree cp_parser_type_parameter
1614   (cp_parser *);
1615 static tree cp_parser_template_id
1616   (cp_parser *, bool, bool, bool);
1617 static tree cp_parser_template_name
1618   (cp_parser *, bool, bool, bool, bool *);
1619 static tree cp_parser_template_argument_list
1620   (cp_parser *);
1621 static tree cp_parser_template_argument
1622   (cp_parser *);
1623 static void cp_parser_explicit_instantiation
1624   (cp_parser *);
1625 static void cp_parser_explicit_specialization
1626   (cp_parser *);
1627
1628 /* Exception handling [gram.exception] */
1629
1630 static tree cp_parser_try_block
1631   (cp_parser *);
1632 static bool cp_parser_function_try_block
1633   (cp_parser *);
1634 static void cp_parser_handler_seq
1635   (cp_parser *);
1636 static void cp_parser_handler
1637   (cp_parser *);
1638 static tree cp_parser_exception_declaration
1639   (cp_parser *);
1640 static tree cp_parser_throw_expression
1641   (cp_parser *);
1642 static tree cp_parser_exception_specification_opt
1643   (cp_parser *);
1644 static tree cp_parser_type_id_list
1645   (cp_parser *);
1646
1647 /* GNU Extensions */
1648
1649 static tree cp_parser_asm_specification_opt
1650   (cp_parser *);
1651 static tree cp_parser_asm_operand_list
1652   (cp_parser *);
1653 static tree cp_parser_asm_clobber_list
1654   (cp_parser *);
1655 static tree cp_parser_attributes_opt
1656   (cp_parser *);
1657 static tree cp_parser_attribute_list
1658   (cp_parser *);
1659 static bool cp_parser_extension_opt
1660   (cp_parser *, int *);
1661 static void cp_parser_label_declaration
1662   (cp_parser *);
1663
1664 /* Objective-C++ Productions */
1665
1666 static tree cp_parser_objc_message_receiver
1667   (cp_parser *);
1668 static tree cp_parser_objc_message_args
1669   (cp_parser *);
1670 static tree cp_parser_objc_message_expression
1671   (cp_parser *);
1672 static tree cp_parser_objc_encode_expression
1673   (cp_parser *);
1674 static tree cp_parser_objc_defs_expression
1675   (cp_parser *);
1676 static tree cp_parser_objc_protocol_expression
1677   (cp_parser *);
1678 static tree cp_parser_objc_selector_expression
1679   (cp_parser *);
1680 static tree cp_parser_objc_expression
1681   (cp_parser *);
1682 static bool cp_parser_objc_selector_p
1683   (enum cpp_ttype);
1684 static tree cp_parser_objc_selector
1685   (cp_parser *);
1686 static tree cp_parser_objc_protocol_refs_opt
1687   (cp_parser *);
1688 static void cp_parser_objc_declaration
1689   (cp_parser *);
1690 static tree cp_parser_objc_statement
1691   (cp_parser *);
1692
1693 /* Utility Routines */
1694
1695 static tree cp_parser_lookup_name
1696   (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1697 static tree cp_parser_lookup_name_simple
1698   (cp_parser *, tree);
1699 static tree cp_parser_maybe_treat_template_as_class
1700   (tree, bool);
1701 static bool cp_parser_check_declarator_template_parameters
1702   (cp_parser *, cp_declarator *);
1703 static bool cp_parser_check_template_parameters
1704   (cp_parser *, unsigned);
1705 static tree cp_parser_simple_cast_expression
1706   (cp_parser *);
1707 static tree cp_parser_global_scope_opt
1708   (cp_parser *, bool);
1709 static bool cp_parser_constructor_declarator_p
1710   (cp_parser *, bool);
1711 static tree cp_parser_function_definition_from_specifiers_and_declarator
1712   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1713 static tree cp_parser_function_definition_after_declarator
1714   (cp_parser *, bool);
1715 static void cp_parser_template_declaration_after_export
1716   (cp_parser *, bool);
1717 static tree cp_parser_single_declaration
1718   (cp_parser *, bool, bool *);
1719 static tree cp_parser_functional_cast
1720   (cp_parser *, tree);
1721 static tree cp_parser_save_member_function_body
1722   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1723 static tree cp_parser_enclosed_template_argument_list
1724   (cp_parser *);
1725 static void cp_parser_save_default_args
1726   (cp_parser *, tree);
1727 static void cp_parser_late_parsing_for_member
1728   (cp_parser *, tree);
1729 static void cp_parser_late_parsing_default_args
1730   (cp_parser *, tree);
1731 static tree cp_parser_sizeof_operand
1732   (cp_parser *, enum rid);
1733 static bool cp_parser_declares_only_class_p
1734   (cp_parser *);
1735 static void cp_parser_set_storage_class
1736   (cp_decl_specifier_seq *, cp_storage_class);
1737 static void cp_parser_set_decl_spec_type
1738   (cp_decl_specifier_seq *, tree, bool);
1739 static bool cp_parser_friend_p
1740   (const cp_decl_specifier_seq *);
1741 static cp_token *cp_parser_require
1742   (cp_parser *, enum cpp_ttype, const char *);
1743 static cp_token *cp_parser_require_keyword
1744   (cp_parser *, enum rid, const char *);
1745 static bool cp_parser_token_starts_function_definition_p
1746   (cp_token *);
1747 static bool cp_parser_next_token_starts_class_definition_p
1748   (cp_parser *);
1749 static bool cp_parser_next_token_ends_template_argument_p
1750   (cp_parser *);
1751 static bool cp_parser_nth_token_starts_template_argument_list_p
1752   (cp_parser *, size_t);
1753 static enum tag_types cp_parser_token_is_class_key
1754   (cp_token *);
1755 static void cp_parser_check_class_key
1756   (enum tag_types, tree type);
1757 static void cp_parser_check_access_in_redeclaration
1758   (tree type);
1759 static bool cp_parser_optional_template_keyword
1760   (cp_parser *);
1761 static void cp_parser_pre_parsed_nested_name_specifier
1762   (cp_parser *);
1763 static void cp_parser_cache_group
1764   (cp_parser *, enum cpp_ttype, unsigned);
1765 static void cp_parser_parse_tentatively
1766   (cp_parser *);
1767 static void cp_parser_commit_to_tentative_parse
1768   (cp_parser *);
1769 static void cp_parser_abort_tentative_parse
1770   (cp_parser *);
1771 static bool cp_parser_parse_definitely
1772   (cp_parser *);
1773 static inline bool cp_parser_parsing_tentatively
1774   (cp_parser *);
1775 static bool cp_parser_uncommitted_to_tentative_parse_p
1776   (cp_parser *);
1777 static void cp_parser_error
1778   (cp_parser *, const char *);
1779 static void cp_parser_name_lookup_error
1780   (cp_parser *, tree, tree, const char *);
1781 static bool cp_parser_simulate_error
1782   (cp_parser *);
1783 static void cp_parser_check_type_definition
1784   (cp_parser *);
1785 static void cp_parser_check_for_definition_in_return_type
1786   (cp_declarator *, tree);
1787 static void cp_parser_check_for_invalid_template_id
1788   (cp_parser *, tree);
1789 static bool cp_parser_non_integral_constant_expression
1790   (cp_parser *, const char *);
1791 static void cp_parser_diagnose_invalid_type_name
1792   (cp_parser *, tree, tree);
1793 static bool cp_parser_parse_and_diagnose_invalid_type_name
1794   (cp_parser *);
1795 static int cp_parser_skip_to_closing_parenthesis
1796   (cp_parser *, bool, bool, bool);
1797 static void cp_parser_skip_to_end_of_statement
1798   (cp_parser *);
1799 static void cp_parser_consume_semicolon_at_end_of_statement
1800   (cp_parser *);
1801 static void cp_parser_skip_to_end_of_block_or_statement
1802   (cp_parser *);
1803 static void cp_parser_skip_to_closing_brace
1804   (cp_parser *);
1805 static void cp_parser_skip_until_found
1806   (cp_parser *, enum cpp_ttype, const char *);
1807 static bool cp_parser_error_occurred
1808   (cp_parser *);
1809 static bool cp_parser_allow_gnu_extensions_p
1810   (cp_parser *);
1811 static bool cp_parser_is_string_literal
1812   (cp_token *);
1813 static bool cp_parser_is_keyword
1814   (cp_token *, enum rid);
1815 static tree cp_parser_make_typename_type
1816   (cp_parser *, tree, tree);
1817
1818 /* Returns nonzero if we are parsing tentatively.  */
1819
1820 static inline bool
1821 cp_parser_parsing_tentatively (cp_parser* parser)
1822 {
1823   return parser->context->next != NULL;
1824 }
1825
1826 /* Returns nonzero if TOKEN is a string literal.  */
1827
1828 static bool
1829 cp_parser_is_string_literal (cp_token* token)
1830 {
1831   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1832 }
1833
1834 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1835
1836 static bool
1837 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1838 {
1839   return token->keyword == keyword;
1840 }
1841
1842 /* A minimum or maximum operator has been seen.  As these are
1843    deprecated, issue a warning.  */
1844
1845 static inline void
1846 cp_parser_warn_min_max (void)
1847 {
1848   if (warn_deprecated && !in_system_header)
1849     warning (0, "minimum/maximum operators are deprecated");
1850 }
1851
1852 /* If not parsing tentatively, issue a diagnostic of the form
1853       FILE:LINE: MESSAGE before TOKEN
1854    where TOKEN is the next token in the input stream.  MESSAGE
1855    (specified by the caller) is usually of the form "expected
1856    OTHER-TOKEN".  */
1857
1858 static void
1859 cp_parser_error (cp_parser* parser, const char* message)
1860 {
1861   if (!cp_parser_simulate_error (parser))
1862     {
1863       cp_token *token = cp_lexer_peek_token (parser->lexer);
1864       /* This diagnostic makes more sense if it is tagged to the line
1865          of the token we just peeked at.  */
1866       cp_lexer_set_source_position_from_token (token);
1867       if (token->type == CPP_PRAGMA)
1868         {
1869           error ("%<#pragma%> is not allowed here");
1870           cp_lexer_purge_token (parser->lexer);
1871           return;
1872         }
1873       c_parse_error (message,
1874                      /* Because c_parser_error does not understand
1875                         CPP_KEYWORD, keywords are treated like
1876                         identifiers.  */
1877                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1878                      token->value);
1879     }
1880 }
1881
1882 /* Issue an error about name-lookup failing.  NAME is the
1883    IDENTIFIER_NODE DECL is the result of
1884    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1885    the thing that we hoped to find.  */
1886
1887 static void
1888 cp_parser_name_lookup_error (cp_parser* parser,
1889                              tree name,
1890                              tree decl,
1891                              const char* desired)
1892 {
1893   /* If name lookup completely failed, tell the user that NAME was not
1894      declared.  */
1895   if (decl == error_mark_node)
1896     {
1897       if (parser->scope && parser->scope != global_namespace)
1898         error ("%<%D::%D%> has not been declared",
1899                parser->scope, name);
1900       else if (parser->scope == global_namespace)
1901         error ("%<::%D%> has not been declared", name);
1902       else if (parser->object_scope
1903                && !CLASS_TYPE_P (parser->object_scope))
1904         error ("request for member %qD in non-class type %qT",
1905                name, parser->object_scope);
1906       else if (parser->object_scope)
1907         error ("%<%T::%D%> has not been declared",
1908                parser->object_scope, name);
1909       else
1910         error ("%qD has not been declared", name);
1911     }
1912   else if (parser->scope && parser->scope != global_namespace)
1913     error ("%<%D::%D%> %s", parser->scope, name, desired);
1914   else if (parser->scope == global_namespace)
1915     error ("%<::%D%> %s", name, desired);
1916   else
1917     error ("%qD %s", name, desired);
1918 }
1919
1920 /* If we are parsing tentatively, remember that an error has occurred
1921    during this tentative parse.  Returns true if the error was
1922    simulated; false if a message should be issued by the caller.  */
1923
1924 static bool
1925 cp_parser_simulate_error (cp_parser* parser)
1926 {
1927   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1928     {
1929       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1930       return true;
1931     }
1932   return false;
1933 }
1934
1935 /* This function is called when a type is defined.  If type
1936    definitions are forbidden at this point, an error message is
1937    issued.  */
1938
1939 static void
1940 cp_parser_check_type_definition (cp_parser* parser)
1941 {
1942   /* If types are forbidden here, issue a message.  */
1943   if (parser->type_definition_forbidden_message)
1944     /* Use `%s' to print the string in case there are any escape
1945        characters in the message.  */
1946     error ("%s", parser->type_definition_forbidden_message);
1947 }
1948
1949 /* This function is called when the DECLARATOR is processed.  The TYPE
1950    was a type defined in the decl-specifiers.  If it is invalid to
1951    define a type in the decl-specifiers for DECLARATOR, an error is
1952    issued.  */
1953
1954 static void
1955 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1956                                                tree type)
1957 {
1958   /* [dcl.fct] forbids type definitions in return types.
1959      Unfortunately, it's not easy to know whether or not we are
1960      processing a return type until after the fact.  */
1961   while (declarator
1962          && (declarator->kind == cdk_pointer
1963              || declarator->kind == cdk_reference
1964              || declarator->kind == cdk_ptrmem))
1965     declarator = declarator->declarator;
1966   if (declarator
1967       && declarator->kind == cdk_function)
1968     {
1969       error ("new types may not be defined in a return type");
1970       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1971               type);
1972     }
1973 }
1974
1975 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1976    "<" in any valid C++ program.  If the next token is indeed "<",
1977    issue a message warning the user about what appears to be an
1978    invalid attempt to form a template-id.  */
1979
1980 static void
1981 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1982                                          tree type)
1983 {
1984   cp_token_position start = 0;
1985
1986   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1987     {
1988       if (TYPE_P (type))
1989         error ("%qT is not a template", type);
1990       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1991         error ("%qE is not a template", type);
1992       else
1993         error ("invalid template-id");
1994       /* Remember the location of the invalid "<".  */
1995       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1996         start = cp_lexer_token_position (parser->lexer, true);
1997       /* Consume the "<".  */
1998       cp_lexer_consume_token (parser->lexer);
1999       /* Parse the template arguments.  */
2000       cp_parser_enclosed_template_argument_list (parser);
2001       /* Permanently remove the invalid template arguments so that
2002          this error message is not issued again.  */
2003       if (start)
2004         cp_lexer_purge_tokens_after (parser->lexer, start);
2005     }
2006 }
2007
2008 /* If parsing an integral constant-expression, issue an error message
2009    about the fact that THING appeared and return true.  Otherwise,
2010    return false.  In either case, set
2011    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2012
2013 static bool
2014 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2015                                             const char *thing)
2016 {
2017   parser->non_integral_constant_expression_p = true;
2018   if (parser->integral_constant_expression_p)
2019     {
2020       if (!parser->allow_non_integral_constant_expression_p)
2021         {
2022           error ("%s cannot appear in a constant-expression", thing);
2023           return true;
2024         }
2025     }
2026   return false;
2027 }
2028
2029 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2030    qualifying scope (or NULL, if none) for ID.  This function commits
2031    to the current active tentative parse, if any.  (Otherwise, the
2032    problematic construct might be encountered again later, resulting
2033    in duplicate error messages.)  */
2034
2035 static void
2036 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2037 {
2038   tree decl, old_scope;
2039   /* Try to lookup the identifier.  */
2040   old_scope = parser->scope;
2041   parser->scope = scope;
2042   decl = cp_parser_lookup_name_simple (parser, id);
2043   parser->scope = old_scope;
2044   /* If the lookup found a template-name, it means that the user forgot
2045   to specify an argument list. Emit an useful error message.  */
2046   if (TREE_CODE (decl) == TEMPLATE_DECL)
2047     error ("invalid use of template-name %qE without an argument list",
2048       decl);
2049   else if (!parser->scope || parser->scope == error_mark_node)
2050     {
2051       /* Issue an error message.  */
2052       error ("%qE does not name a type", id);
2053       /* If we're in a template class, it's possible that the user was
2054          referring to a type from a base class.  For example:
2055
2056            template <typename T> struct A { typedef T X; };
2057            template <typename T> struct B : public A<T> { X x; };
2058
2059          The user should have said "typename A<T>::X".  */
2060       if (processing_template_decl && current_class_type
2061           && TYPE_BINFO (current_class_type))
2062         {
2063           tree b;
2064
2065           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2066                b;
2067                b = TREE_CHAIN (b))
2068             {
2069               tree base_type = BINFO_TYPE (b);
2070               if (CLASS_TYPE_P (base_type)
2071                   && dependent_type_p (base_type))
2072                 {
2073                   tree field;
2074                   /* Go from a particular instantiation of the
2075                      template (which will have an empty TYPE_FIELDs),
2076                      to the main version.  */
2077                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2078                   for (field = TYPE_FIELDS (base_type);
2079                        field;
2080                        field = TREE_CHAIN (field))
2081                     if (TREE_CODE (field) == TYPE_DECL
2082                         && DECL_NAME (field) == id)
2083                       {
2084                         inform ("(perhaps %<typename %T::%E%> was intended)",
2085                                 BINFO_TYPE (b), id);
2086                         break;
2087                       }
2088                   if (field)
2089                     break;
2090                 }
2091             }
2092         }
2093     }
2094   /* Here we diagnose qualified-ids where the scope is actually correct,
2095      but the identifier does not resolve to a valid type name.  */
2096   else
2097     {
2098       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2099         error ("%qE in namespace %qE does not name a type",
2100                id, parser->scope);
2101       else if (TYPE_P (parser->scope))
2102         error ("%qE in class %qT does not name a type", id, parser->scope);
2103       else
2104         gcc_unreachable ();
2105     }
2106   cp_parser_commit_to_tentative_parse (parser);
2107 }
2108
2109 /* Check for a common situation where a type-name should be present,
2110    but is not, and issue a sensible error message.  Returns true if an
2111    invalid type-name was detected.
2112
2113    The situation handled by this function are variable declarations of the
2114    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2115    Usually, `ID' should name a type, but if we got here it means that it
2116    does not. We try to emit the best possible error message depending on
2117    how exactly the id-expression looks like.
2118 */
2119
2120 static bool
2121 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2122 {
2123   tree id;
2124
2125   cp_parser_parse_tentatively (parser);
2126   id = cp_parser_id_expression (parser,
2127                                 /*template_keyword_p=*/false,
2128                                 /*check_dependency_p=*/true,
2129                                 /*template_p=*/NULL,
2130                                 /*declarator_p=*/true);
2131   /* After the id-expression, there should be a plain identifier,
2132      otherwise this is not a simple variable declaration. Also, if
2133      the scope is dependent, we cannot do much.  */
2134   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2135       || (parser->scope && TYPE_P (parser->scope)
2136           && dependent_type_p (parser->scope)))
2137     {
2138       cp_parser_abort_tentative_parse (parser);
2139       return false;
2140     }
2141   if (!cp_parser_parse_definitely (parser)
2142       || TREE_CODE (id) != IDENTIFIER_NODE)
2143     return false;
2144
2145   /* Emit a diagnostic for the invalid type.  */
2146   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2147   /* Skip to the end of the declaration; there's no point in
2148      trying to process it.  */
2149   cp_parser_skip_to_end_of_block_or_statement (parser);
2150   return true;
2151 }
2152
2153 /* Consume tokens up to, and including, the next non-nested closing `)'.
2154    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2155    are doing error recovery. Returns -1 if OR_COMMA is true and we
2156    found an unnested comma.  */
2157
2158 static int
2159 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2160                                        bool recovering,
2161                                        bool or_comma,
2162                                        bool consume_paren)
2163 {
2164   unsigned paren_depth = 0;
2165   unsigned brace_depth = 0;
2166   int result;
2167
2168   if (recovering && !or_comma
2169       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2170     return 0;
2171
2172   while (true)
2173     {
2174       cp_token *token;
2175
2176       /* If we've run out of tokens, then there is no closing `)'.  */
2177       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2178         {
2179           result = 0;
2180           break;
2181         }
2182
2183       token = cp_lexer_peek_token (parser->lexer);
2184
2185       /* This matches the processing in skip_to_end_of_statement.  */
2186       if (token->type == CPP_SEMICOLON && !brace_depth)
2187         {
2188           result = 0;
2189           break;
2190         }
2191       if (token->type == CPP_OPEN_BRACE)
2192         ++brace_depth;
2193       if (token->type == CPP_CLOSE_BRACE)
2194         {
2195           if (!brace_depth--)
2196             {
2197               result = 0;
2198               break;
2199             }
2200         }
2201       if (recovering && or_comma && token->type == CPP_COMMA
2202           && !brace_depth && !paren_depth)
2203         {
2204           result = -1;
2205           break;
2206         }
2207
2208       if (!brace_depth)
2209         {
2210           /* If it is an `(', we have entered another level of nesting.  */
2211           if (token->type == CPP_OPEN_PAREN)
2212             ++paren_depth;
2213           /* If it is a `)', then we might be done.  */
2214           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2215             {
2216               if (consume_paren)
2217                 cp_lexer_consume_token (parser->lexer);
2218               {
2219                 result = 1;
2220                 break;
2221               }
2222             }
2223         }
2224
2225       /* Consume the token.  */
2226       cp_lexer_consume_token (parser->lexer);
2227     }
2228
2229   return result;
2230 }
2231
2232 /* Consume tokens until we reach the end of the current statement.
2233    Normally, that will be just before consuming a `;'.  However, if a
2234    non-nested `}' comes first, then we stop before consuming that.  */
2235
2236 static void
2237 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2238 {
2239   unsigned nesting_depth = 0;
2240
2241   while (true)
2242     {
2243       cp_token *token;
2244
2245       /* Peek at the next token.  */
2246       token = cp_lexer_peek_token (parser->lexer);
2247       /* If we've run out of tokens, stop.  */
2248       if (token->type == CPP_EOF)
2249         break;
2250       /* If the next token is a `;', we have reached the end of the
2251          statement.  */
2252       if (token->type == CPP_SEMICOLON && !nesting_depth)
2253         break;
2254       /* If the next token is a non-nested `}', then we have reached
2255          the end of the current block.  */
2256       if (token->type == CPP_CLOSE_BRACE)
2257         {
2258           /* If this is a non-nested `}', stop before consuming it.
2259              That way, when confronted with something like:
2260
2261                { 3 + }
2262
2263              we stop before consuming the closing `}', even though we
2264              have not yet reached a `;'.  */
2265           if (nesting_depth == 0)
2266             break;
2267           /* If it is the closing `}' for a block that we have
2268              scanned, stop -- but only after consuming the token.
2269              That way given:
2270
2271                 void f g () { ... }
2272                 typedef int I;
2273
2274              we will stop after the body of the erroneously declared
2275              function, but before consuming the following `typedef'
2276              declaration.  */
2277           if (--nesting_depth == 0)
2278             {
2279               cp_lexer_consume_token (parser->lexer);
2280               break;
2281             }
2282         }
2283       /* If it the next token is a `{', then we are entering a new
2284          block.  Consume the entire block.  */
2285       else if (token->type == CPP_OPEN_BRACE)
2286         ++nesting_depth;
2287       /* Consume the token.  */
2288       cp_lexer_consume_token (parser->lexer);
2289     }
2290 }
2291
2292 /* This function is called at the end of a statement or declaration.
2293    If the next token is a semicolon, it is consumed; otherwise, error
2294    recovery is attempted.  */
2295
2296 static void
2297 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2298 {
2299   /* Look for the trailing `;'.  */
2300   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2301     {
2302       /* If there is additional (erroneous) input, skip to the end of
2303          the statement.  */
2304       cp_parser_skip_to_end_of_statement (parser);
2305       /* If the next token is now a `;', consume it.  */
2306       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2307         cp_lexer_consume_token (parser->lexer);
2308     }
2309 }
2310
2311 /* Skip tokens until we have consumed an entire block, or until we
2312    have consumed a non-nested `;'.  */
2313
2314 static void
2315 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2316 {
2317   int nesting_depth = 0;
2318
2319   while (nesting_depth >= 0)
2320     {
2321       cp_token *token = cp_lexer_peek_token (parser->lexer);
2322
2323       if (token->type == CPP_EOF)
2324         break;
2325
2326       switch (token->type)
2327         {
2328         case CPP_EOF:
2329           /* If we've run out of tokens, stop.  */
2330           nesting_depth = -1;
2331           continue;
2332
2333         case CPP_SEMICOLON:
2334           /* Stop if this is an unnested ';'. */
2335           if (!nesting_depth)
2336             nesting_depth = -1;
2337           break;
2338
2339         case CPP_CLOSE_BRACE:
2340           /* Stop if this is an unnested '}', or closes the outermost
2341              nesting level.  */
2342           nesting_depth--;
2343           if (!nesting_depth)
2344             nesting_depth = -1;
2345           break;
2346
2347         case CPP_OPEN_BRACE:
2348           /* Nest. */
2349           nesting_depth++;
2350           break;
2351
2352         default:
2353           break;
2354         }
2355
2356       /* Consume the token.  */
2357       cp_lexer_consume_token (parser->lexer);
2358
2359     }
2360 }
2361
2362 /* Skip tokens until a non-nested closing curly brace is the next
2363    token.  */
2364
2365 static void
2366 cp_parser_skip_to_closing_brace (cp_parser *parser)
2367 {
2368   unsigned nesting_depth = 0;
2369
2370   while (true)
2371     {
2372       cp_token *token;
2373
2374       /* Peek at the next token.  */
2375       token = cp_lexer_peek_token (parser->lexer);
2376       /* If we've run out of tokens, stop.  */
2377       if (token->type == CPP_EOF)
2378         break;
2379       /* If the next token is a non-nested `}', then we have reached
2380          the end of the current block.  */
2381       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2382         break;
2383       /* If it the next token is a `{', then we are entering a new
2384          block.  Consume the entire block.  */
2385       else if (token->type == CPP_OPEN_BRACE)
2386         ++nesting_depth;
2387       /* Consume the token.  */
2388       cp_lexer_consume_token (parser->lexer);
2389     }
2390 }
2391
2392 /* This is a simple wrapper around make_typename_type. When the id is
2393    an unresolved identifier node, we can provide a superior diagnostic
2394    using cp_parser_diagnose_invalid_type_name.  */
2395
2396 static tree
2397 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2398 {
2399   tree result;
2400   if (TREE_CODE (id) == IDENTIFIER_NODE)
2401     {
2402       result = make_typename_type (scope, id, typename_type,
2403                                    /*complain=*/0);
2404       if (result == error_mark_node)
2405         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2406       return result;
2407     }
2408   return make_typename_type (scope, id, typename_type, tf_error);
2409 }
2410
2411
2412 /* Create a new C++ parser.  */
2413
2414 static cp_parser *
2415 cp_parser_new (void)
2416 {
2417   cp_parser *parser;
2418   cp_lexer *lexer;
2419   unsigned i;
2420
2421   /* cp_lexer_new_main is called before calling ggc_alloc because
2422      cp_lexer_new_main might load a PCH file.  */
2423   lexer = cp_lexer_new_main ();
2424
2425   /* Initialize the binops_by_token so that we can get the tree
2426      directly from the token.  */
2427   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2428     binops_by_token[binops[i].token_type] = binops[i];
2429
2430   parser = GGC_CNEW (cp_parser);
2431   parser->lexer = lexer;
2432   parser->context = cp_parser_context_new (NULL);
2433
2434   /* For now, we always accept GNU extensions.  */
2435   parser->allow_gnu_extensions_p = 1;
2436
2437   /* The `>' token is a greater-than operator, not the end of a
2438      template-id.  */
2439   parser->greater_than_is_operator_p = true;
2440
2441   parser->default_arg_ok_p = true;
2442
2443   /* We are not parsing a constant-expression.  */
2444   parser->integral_constant_expression_p = false;
2445   parser->allow_non_integral_constant_expression_p = false;
2446   parser->non_integral_constant_expression_p = false;
2447
2448   /* Local variable names are not forbidden.  */
2449   parser->local_variables_forbidden_p = false;
2450
2451   /* We are not processing an `extern "C"' declaration.  */
2452   parser->in_unbraced_linkage_specification_p = false;
2453
2454   /* We are not processing a declarator.  */
2455   parser->in_declarator_p = false;
2456
2457   /* We are not processing a template-argument-list.  */
2458   parser->in_template_argument_list_p = false;
2459
2460   /* We are not in an iteration statement.  */
2461   parser->in_iteration_statement_p = false;
2462
2463   /* We are not in a switch statement.  */
2464   parser->in_switch_statement_p = false;
2465
2466   /* We are not parsing a type-id inside an expression.  */
2467   parser->in_type_id_in_expr_p = false;
2468
2469   /* Declarations aren't implicitly extern "C".  */
2470   parser->implicit_extern_c = false;
2471
2472   /* String literals should be translated to the execution character set.  */
2473   parser->translate_strings_p = true;
2474
2475   /* The unparsed function queue is empty.  */
2476   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2477
2478   /* There are no classes being defined.  */
2479   parser->num_classes_being_defined = 0;
2480
2481   /* No template parameters apply.  */
2482   parser->num_template_parameter_lists = 0;
2483
2484   return parser;
2485 }
2486
2487 /* Create a cp_lexer structure which will emit the tokens in CACHE
2488    and push it onto the parser's lexer stack.  This is used for delayed
2489    parsing of in-class method bodies and default arguments, and should
2490    not be confused with tentative parsing.  */
2491 static void
2492 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2493 {
2494   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2495   lexer->next = parser->lexer;
2496   parser->lexer = lexer;
2497
2498   /* Move the current source position to that of the first token in the
2499      new lexer.  */
2500   cp_lexer_set_source_position_from_token (lexer->next_token);
2501 }
2502
2503 /* Pop the top lexer off the parser stack.  This is never used for the
2504    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2505 static void
2506 cp_parser_pop_lexer (cp_parser *parser)
2507 {
2508   cp_lexer *lexer = parser->lexer;
2509   parser->lexer = lexer->next;
2510   cp_lexer_destroy (lexer);
2511
2512   /* Put the current source position back where it was before this
2513      lexer was pushed.  */
2514   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2515 }
2516
2517 /* Lexical conventions [gram.lex]  */
2518
2519 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2520    identifier.  */
2521
2522 static tree
2523 cp_parser_identifier (cp_parser* parser)
2524 {
2525   cp_token *token;
2526
2527   /* Look for the identifier.  */
2528   token = cp_parser_require (parser, CPP_NAME, "identifier");
2529   /* Return the value.  */
2530   return token ? token->value : error_mark_node;
2531 }
2532
2533 /* Parse a sequence of adjacent string constants.  Returns a
2534    TREE_STRING representing the combined, nul-terminated string
2535    constant.  If TRANSLATE is true, translate the string to the
2536    execution character set.  If WIDE_OK is true, a wide string is
2537    invalid here.
2538
2539    C++98 [lex.string] says that if a narrow string literal token is
2540    adjacent to a wide string literal token, the behavior is undefined.
2541    However, C99 6.4.5p4 says that this results in a wide string literal.
2542    We follow C99 here, for consistency with the C front end.
2543
2544    This code is largely lifted from lex_string() in c-lex.c.
2545
2546    FUTURE: ObjC++ will need to handle @-strings here.  */
2547 static tree
2548 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2549 {
2550   tree value;
2551   bool wide = false;
2552   size_t count;
2553   struct obstack str_ob;
2554   cpp_string str, istr, *strs;
2555   cp_token *tok;
2556
2557   tok = cp_lexer_peek_token (parser->lexer);
2558   if (!cp_parser_is_string_literal (tok))
2559     {
2560       cp_parser_error (parser, "expected string-literal");
2561       return error_mark_node;
2562     }
2563
2564   /* Try to avoid the overhead of creating and destroying an obstack
2565      for the common case of just one string.  */
2566   if (!cp_parser_is_string_literal
2567       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2568     {
2569       cp_lexer_consume_token (parser->lexer);
2570
2571       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2572       str.len = TREE_STRING_LENGTH (tok->value);
2573       count = 1;
2574       if (tok->type == CPP_WSTRING)
2575         wide = true;
2576
2577       strs = &str;
2578     }
2579   else
2580     {
2581       gcc_obstack_init (&str_ob);
2582       count = 0;
2583
2584       do
2585         {
2586           cp_lexer_consume_token (parser->lexer);
2587           count++;
2588           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2589           str.len = TREE_STRING_LENGTH (tok->value);
2590           if (tok->type == CPP_WSTRING)
2591             wide = true;
2592
2593           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2594
2595           tok = cp_lexer_peek_token (parser->lexer);
2596         }
2597       while (cp_parser_is_string_literal (tok));
2598
2599       strs = (cpp_string *) obstack_finish (&str_ob);
2600     }
2601
2602   if (wide && !wide_ok)
2603     {
2604       cp_parser_error (parser, "a wide string is invalid in this context");
2605       wide = false;
2606     }
2607
2608   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2609       (parse_in, strs, count, &istr, wide))
2610     {
2611       value = build_string (istr.len, (char *)istr.text);
2612       free ((void *)istr.text);
2613
2614       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2615       value = fix_string_type (value);
2616     }
2617   else
2618     /* cpp_interpret_string has issued an error.  */
2619     value = error_mark_node;
2620
2621   if (count > 1)
2622     obstack_free (&str_ob, 0);
2623
2624   return value;
2625 }
2626
2627
2628 /* Basic concepts [gram.basic]  */
2629
2630 /* Parse a translation-unit.
2631
2632    translation-unit:
2633      declaration-seq [opt]
2634
2635    Returns TRUE if all went well.  */
2636
2637 static bool
2638 cp_parser_translation_unit (cp_parser* parser)
2639 {
2640   /* The address of the first non-permanent object on the declarator
2641      obstack.  */
2642   static void *declarator_obstack_base;
2643
2644   bool success;
2645
2646   /* Create the declarator obstack, if necessary.  */
2647   if (!cp_error_declarator)
2648     {
2649       gcc_obstack_init (&declarator_obstack);
2650       /* Create the error declarator.  */
2651       cp_error_declarator = make_declarator (cdk_error);
2652       /* Create the empty parameter list.  */
2653       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2654       /* Remember where the base of the declarator obstack lies.  */
2655       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2656     }
2657
2658   while (true)
2659     {
2660       cp_parser_declaration_seq_opt (parser);
2661
2662       /* If there are no tokens left then all went well.  */
2663       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2664         {
2665           /* Get rid of the token array; we don't need it any more.  */
2666           cp_lexer_destroy (parser->lexer);
2667           parser->lexer = NULL;
2668
2669           /* This file might have been a context that's implicitly extern
2670              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2671           if (parser->implicit_extern_c)
2672             {
2673               pop_lang_context ();
2674               parser->implicit_extern_c = false;
2675             }
2676
2677           /* Finish up.  */
2678           finish_translation_unit ();
2679
2680           success = true;
2681           break;
2682         }
2683       else
2684         {
2685           cp_parser_error (parser, "expected declaration");
2686           success = false;
2687           break;
2688         }
2689     }
2690
2691   /* Make sure the declarator obstack was fully cleaned up.  */
2692   gcc_assert (obstack_next_free (&declarator_obstack)
2693               == declarator_obstack_base);
2694
2695   /* All went well.  */
2696   return success;
2697 }
2698
2699 /* Expressions [gram.expr] */
2700
2701 /* Parse a primary-expression.
2702
2703    primary-expression:
2704      literal
2705      this
2706      ( expression )
2707      id-expression
2708
2709    GNU Extensions:
2710
2711    primary-expression:
2712      ( compound-statement )
2713      __builtin_va_arg ( assignment-expression , type-id )
2714
2715    Objective-C++ Extension:
2716
2717    primary-expression:
2718      objc-expression
2719
2720    literal:
2721      __null
2722
2723    CAST_P is true if this primary expression is the target of a cast.
2724
2725    Returns a representation of the expression.
2726
2727    *IDK indicates what kind of id-expression (if any) was present.
2728
2729    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2730    used as the operand of a pointer-to-member.  In that case,
2731    *QUALIFYING_CLASS gives the class that is used as the qualifying
2732    class in the pointer-to-member.  */
2733
2734 static tree
2735 cp_parser_primary_expression (cp_parser *parser,
2736                               bool cast_p,
2737                               cp_id_kind *idk,
2738                               tree *qualifying_class)
2739 {
2740   cp_token *token;
2741
2742   /* Assume the primary expression is not an id-expression.  */
2743   *idk = CP_ID_KIND_NONE;
2744   /* And that it cannot be used as pointer-to-member.  */
2745   *qualifying_class = NULL_TREE;
2746
2747   /* Peek at the next token.  */
2748   token = cp_lexer_peek_token (parser->lexer);
2749   switch (token->type)
2750     {
2751       /* literal:
2752            integer-literal
2753            character-literal
2754            floating-literal
2755            string-literal
2756            boolean-literal  */
2757     case CPP_CHAR:
2758     case CPP_WCHAR:
2759     case CPP_NUMBER:
2760       token = cp_lexer_consume_token (parser->lexer);
2761       /* Floating-point literals are only allowed in an integral
2762          constant expression if they are cast to an integral or
2763          enumeration type.  */
2764       if (TREE_CODE (token->value) == REAL_CST
2765           && parser->integral_constant_expression_p
2766           && pedantic)
2767         {
2768           /* CAST_P will be set even in invalid code like "int(2.7 +
2769              ...)".   Therefore, we have to check that the next token
2770              is sure to end the cast.  */
2771           if (cast_p)
2772             {
2773               cp_token *next_token;
2774
2775               next_token = cp_lexer_peek_token (parser->lexer);
2776               if (/* The comma at the end of an
2777                      enumerator-definition.  */
2778                   next_token->type != CPP_COMMA
2779                   /* The curly brace at the end of an enum-specifier.  */
2780                   && next_token->type != CPP_CLOSE_BRACE
2781                   /* The end of a statement.  */
2782                   && next_token->type != CPP_SEMICOLON
2783                   /* The end of the cast-expression.  */
2784                   && next_token->type != CPP_CLOSE_PAREN
2785                   /* The end of an array bound.  */
2786                   && next_token->type != CPP_CLOSE_SQUARE)
2787                 cast_p = false;
2788             }
2789
2790           /* If we are within a cast, then the constraint that the
2791              cast is to an integral or enumeration type will be
2792              checked at that point.  If we are not within a cast, then
2793              this code is invalid.  */
2794           if (!cast_p)
2795             cp_parser_non_integral_constant_expression
2796               (parser, "floating-point literal");
2797         }
2798       return token->value;
2799
2800     case CPP_STRING:
2801     case CPP_WSTRING:
2802       /* ??? Should wide strings be allowed when parser->translate_strings_p
2803          is false (i.e. in attributes)?  If not, we can kill the third
2804          argument to cp_parser_string_literal.  */
2805       return cp_parser_string_literal (parser,
2806                                        parser->translate_strings_p,
2807                                        true);
2808
2809     case CPP_OPEN_PAREN:
2810       {
2811         tree expr;
2812         bool saved_greater_than_is_operator_p;
2813
2814         /* Consume the `('.  */
2815         cp_lexer_consume_token (parser->lexer);
2816         /* Within a parenthesized expression, a `>' token is always
2817            the greater-than operator.  */
2818         saved_greater_than_is_operator_p
2819           = parser->greater_than_is_operator_p;
2820         parser->greater_than_is_operator_p = true;
2821         /* If we see `( { ' then we are looking at the beginning of
2822            a GNU statement-expression.  */
2823         if (cp_parser_allow_gnu_extensions_p (parser)
2824             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2825           {
2826             /* Statement-expressions are not allowed by the standard.  */
2827             if (pedantic)
2828               pedwarn ("ISO C++ forbids braced-groups within expressions");
2829
2830             /* And they're not allowed outside of a function-body; you
2831                cannot, for example, write:
2832
2833                  int i = ({ int j = 3; j + 1; });
2834
2835                at class or namespace scope.  */
2836             if (!at_function_scope_p ())
2837               error ("statement-expressions are allowed only inside functions");
2838             /* Start the statement-expression.  */
2839             expr = begin_stmt_expr ();
2840             /* Parse the compound-statement.  */
2841             cp_parser_compound_statement (parser, expr, false);
2842             /* Finish up.  */
2843             expr = finish_stmt_expr (expr, false);
2844           }
2845         else
2846           {
2847             /* Parse the parenthesized expression.  */
2848             expr = cp_parser_expression (parser, cast_p);
2849             /* Let the front end know that this expression was
2850                enclosed in parentheses. This matters in case, for
2851                example, the expression is of the form `A::B', since
2852                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2853                not.  */
2854             finish_parenthesized_expr (expr);
2855           }
2856         /* The `>' token might be the end of a template-id or
2857            template-parameter-list now.  */
2858         parser->greater_than_is_operator_p
2859           = saved_greater_than_is_operator_p;
2860         /* Consume the `)'.  */
2861         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2862           cp_parser_skip_to_end_of_statement (parser);
2863
2864         return expr;
2865       }
2866
2867     case CPP_KEYWORD:
2868       switch (token->keyword)
2869         {
2870           /* These two are the boolean literals.  */
2871         case RID_TRUE:
2872           cp_lexer_consume_token (parser->lexer);
2873           return boolean_true_node;
2874         case RID_FALSE:
2875           cp_lexer_consume_token (parser->lexer);
2876           return boolean_false_node;
2877
2878           /* The `__null' literal.  */
2879         case RID_NULL:
2880           cp_lexer_consume_token (parser->lexer);
2881           return null_node;
2882
2883           /* Recognize the `this' keyword.  */
2884         case RID_THIS:
2885           cp_lexer_consume_token (parser->lexer);
2886           if (parser->local_variables_forbidden_p)
2887             {
2888               error ("%<this%> may not be used in this context");
2889               return error_mark_node;
2890             }
2891           /* Pointers cannot appear in constant-expressions.  */
2892           if (cp_parser_non_integral_constant_expression (parser,
2893                                                           "`this'"))
2894             return error_mark_node;
2895           return finish_this_expr ();
2896
2897           /* The `operator' keyword can be the beginning of an
2898              id-expression.  */
2899         case RID_OPERATOR:
2900           goto id_expression;
2901
2902         case RID_FUNCTION_NAME:
2903         case RID_PRETTY_FUNCTION_NAME:
2904         case RID_C99_FUNCTION_NAME:
2905           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2906              __func__ are the names of variables -- but they are
2907              treated specially.  Therefore, they are handled here,
2908              rather than relying on the generic id-expression logic
2909              below.  Grammatically, these names are id-expressions.
2910
2911              Consume the token.  */
2912           token = cp_lexer_consume_token (parser->lexer);
2913           /* Look up the name.  */
2914           return finish_fname (token->value);
2915
2916         case RID_VA_ARG:
2917           {
2918             tree expression;
2919             tree type;
2920
2921             /* The `__builtin_va_arg' construct is used to handle
2922                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2923             cp_lexer_consume_token (parser->lexer);
2924             /* Look for the opening `('.  */
2925             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2926             /* Now, parse the assignment-expression.  */
2927             expression = cp_parser_assignment_expression (parser,
2928                                                           /*cast_p=*/false);
2929             /* Look for the `,'.  */
2930             cp_parser_require (parser, CPP_COMMA, "`,'");
2931             /* Parse the type-id.  */
2932             type = cp_parser_type_id (parser);
2933             /* Look for the closing `)'.  */
2934             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2935             /* Using `va_arg' in a constant-expression is not
2936                allowed.  */
2937             if (cp_parser_non_integral_constant_expression (parser,
2938                                                             "`va_arg'"))
2939               return error_mark_node;
2940             return build_x_va_arg (expression, type);
2941           }
2942
2943         case RID_OFFSETOF:
2944           return cp_parser_builtin_offsetof (parser);
2945
2946           /* Objective-C++ expressions.  */
2947         case RID_AT_ENCODE:
2948         case RID_AT_PROTOCOL:
2949         case RID_AT_SELECTOR:
2950           return cp_parser_objc_expression (parser);
2951
2952         default:
2953           cp_parser_error (parser, "expected primary-expression");
2954           return error_mark_node;
2955         }
2956
2957       /* An id-expression can start with either an identifier, a
2958          `::' as the beginning of a qualified-id, or the "operator"
2959          keyword.  */
2960     case CPP_NAME:
2961     case CPP_SCOPE:
2962     case CPP_TEMPLATE_ID:
2963     case CPP_NESTED_NAME_SPECIFIER:
2964       {
2965         tree id_expression;
2966         tree decl;
2967         const char *error_msg;
2968
2969       id_expression:
2970         /* Parse the id-expression.  */
2971         id_expression
2972           = cp_parser_id_expression (parser,
2973                                      /*template_keyword_p=*/false,
2974                                      /*check_dependency_p=*/true,
2975                                      /*template_p=*/NULL,
2976                                      /*declarator_p=*/false);
2977         if (id_expression == error_mark_node)
2978           return error_mark_node;
2979         /* If we have a template-id, then no further lookup is
2980            required.  If the template-id was for a template-class, we
2981            will sometimes have a TYPE_DECL at this point.  */
2982         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2983             || TREE_CODE (id_expression) == TYPE_DECL)
2984           decl = id_expression;
2985         /* Look up the name.  */
2986         else
2987           {
2988             bool ambiguous_p;
2989
2990             decl = cp_parser_lookup_name (parser, id_expression,
2991                                           none_type,
2992                                           /*is_template=*/false,
2993                                           /*is_namespace=*/false,
2994                                           /*check_dependency=*/true,
2995                                           &ambiguous_p);
2996             /* If the lookup was ambiguous, an error will already have
2997                been issued.  */
2998             if (ambiguous_p)
2999               return error_mark_node;
3000
3001             /* In Objective-C++, an instance variable (ivar) may be preferred
3002                to whatever cp_parser_lookup_name() found.  */
3003             decl = objc_lookup_ivar (decl, id_expression);
3004
3005             /* If name lookup gives us a SCOPE_REF, then the
3006                qualifying scope was dependent.  Just propagate the
3007                name.  */
3008             if (TREE_CODE (decl) == SCOPE_REF)
3009               {
3010                 if (TYPE_P (TREE_OPERAND (decl, 0)))
3011                   *qualifying_class = TREE_OPERAND (decl, 0);
3012                 return decl;
3013               }
3014             /* Check to see if DECL is a local variable in a context
3015                where that is forbidden.  */
3016             if (parser->local_variables_forbidden_p
3017                 && local_variable_p (decl))
3018               {
3019                 /* It might be that we only found DECL because we are
3020                    trying to be generous with pre-ISO scoping rules.
3021                    For example, consider:
3022
3023                      int i;
3024                      void g() {
3025                        for (int i = 0; i < 10; ++i) {}
3026                        extern void f(int j = i);
3027                      }
3028
3029                    Here, name look up will originally find the out
3030                    of scope `i'.  We need to issue a warning message,
3031                    but then use the global `i'.  */
3032                 decl = check_for_out_of_scope_variable (decl);
3033                 if (local_variable_p (decl))
3034                   {
3035                     error ("local variable %qD may not appear in this context",
3036                            decl);
3037                     return error_mark_node;
3038                   }
3039               }
3040           }
3041
3042         decl = finish_id_expression (id_expression, decl, parser->scope,
3043                                      idk, qualifying_class,
3044                                      parser->integral_constant_expression_p,
3045                                      parser->allow_non_integral_constant_expression_p,
3046                                      &parser->non_integral_constant_expression_p,
3047                                      &error_msg);
3048         if (error_msg)
3049           cp_parser_error (parser, error_msg);
3050         return decl;
3051       }
3052
3053       /* Anything else is an error.  */
3054     default:
3055       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3056       if (c_dialect_objc ()
3057           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3058         return cp_parser_objc_expression (parser);
3059
3060       cp_parser_error (parser, "expected primary-expression");
3061       return error_mark_node;
3062     }
3063 }
3064
3065 /* Parse an id-expression.
3066
3067    id-expression:
3068      unqualified-id
3069      qualified-id
3070
3071    qualified-id:
3072      :: [opt] nested-name-specifier template [opt] unqualified-id
3073      :: identifier
3074      :: operator-function-id
3075      :: template-id
3076
3077    Return a representation of the unqualified portion of the
3078    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3079    a `::' or nested-name-specifier.
3080
3081    Often, if the id-expression was a qualified-id, the caller will
3082    want to make a SCOPE_REF to represent the qualified-id.  This
3083    function does not do this in order to avoid wastefully creating
3084    SCOPE_REFs when they are not required.
3085
3086    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3087    `template' keyword.
3088
3089    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3090    uninstantiated templates.
3091
3092    If *TEMPLATE_P is non-NULL, it is set to true iff the
3093    `template' keyword is used to explicitly indicate that the entity
3094    named is a template.
3095
3096    If DECLARATOR_P is true, the id-expression is appearing as part of
3097    a declarator, rather than as part of an expression.  */
3098
3099 static tree
3100 cp_parser_id_expression (cp_parser *parser,
3101                          bool template_keyword_p,
3102                          bool check_dependency_p,
3103                          bool *template_p,
3104                          bool declarator_p)
3105 {
3106   bool global_scope_p;
3107   bool nested_name_specifier_p;
3108
3109   /* Assume the `template' keyword was not used.  */
3110   if (template_p)
3111     *template_p = false;
3112
3113   /* Look for the optional `::' operator.  */
3114   global_scope_p
3115     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3116        != NULL_TREE);
3117   /* Look for the optional nested-name-specifier.  */
3118   nested_name_specifier_p
3119     = (cp_parser_nested_name_specifier_opt (parser,
3120                                             /*typename_keyword_p=*/false,
3121                                             check_dependency_p,
3122                                             /*type_p=*/false,
3123                                             declarator_p)
3124        != NULL_TREE);
3125   /* If there is a nested-name-specifier, then we are looking at
3126      the first qualified-id production.  */
3127   if (nested_name_specifier_p)
3128     {
3129       tree saved_scope;
3130       tree saved_object_scope;
3131       tree saved_qualifying_scope;
3132       tree unqualified_id;
3133       bool is_template;
3134
3135       /* See if the next token is the `template' keyword.  */
3136       if (!template_p)
3137         template_p = &is_template;
3138       *template_p = cp_parser_optional_template_keyword (parser);
3139       /* Name lookup we do during the processing of the
3140          unqualified-id might obliterate SCOPE.  */
3141       saved_scope = parser->scope;
3142       saved_object_scope = parser->object_scope;
3143       saved_qualifying_scope = parser->qualifying_scope;
3144       /* Process the final unqualified-id.  */
3145       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3146                                                  check_dependency_p,
3147                                                  declarator_p);
3148       /* Restore the SAVED_SCOPE for our caller.  */
3149       parser->scope = saved_scope;
3150       parser->object_scope = saved_object_scope;
3151       parser->qualifying_scope = saved_qualifying_scope;
3152
3153       return unqualified_id;
3154     }
3155   /* Otherwise, if we are in global scope, then we are looking at one
3156      of the other qualified-id productions.  */
3157   else if (global_scope_p)
3158     {
3159       cp_token *token;
3160       tree id;
3161
3162       /* Peek at the next token.  */
3163       token = cp_lexer_peek_token (parser->lexer);
3164
3165       /* If it's an identifier, and the next token is not a "<", then
3166          we can avoid the template-id case.  This is an optimization
3167          for this common case.  */
3168       if (token->type == CPP_NAME
3169           && !cp_parser_nth_token_starts_template_argument_list_p
3170                (parser, 2))
3171         return cp_parser_identifier (parser);
3172
3173       cp_parser_parse_tentatively (parser);
3174       /* Try a template-id.  */
3175       id = cp_parser_template_id (parser,
3176                                   /*template_keyword_p=*/false,
3177                                   /*check_dependency_p=*/true,
3178                                   declarator_p);
3179       /* If that worked, we're done.  */
3180       if (cp_parser_parse_definitely (parser))
3181         return id;
3182
3183       /* Peek at the next token.  (Changes in the token buffer may
3184          have invalidated the pointer obtained above.)  */
3185       token = cp_lexer_peek_token (parser->lexer);
3186
3187       switch (token->type)
3188         {
3189         case CPP_NAME:
3190           return cp_parser_identifier (parser);
3191
3192         case CPP_KEYWORD:
3193           if (token->keyword == RID_OPERATOR)
3194             return cp_parser_operator_function_id (parser);
3195           /* Fall through.  */
3196
3197         default:
3198           cp_parser_error (parser, "expected id-expression");
3199           return error_mark_node;
3200         }
3201     }
3202   else
3203     return cp_parser_unqualified_id (parser, template_keyword_p,
3204                                      /*check_dependency_p=*/true,
3205                                      declarator_p);
3206 }
3207
3208 /* Parse an unqualified-id.
3209
3210    unqualified-id:
3211      identifier
3212      operator-function-id
3213      conversion-function-id
3214      ~ class-name
3215      template-id
3216
3217    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3218    keyword, in a construct like `A::template ...'.
3219
3220    Returns a representation of unqualified-id.  For the `identifier'
3221    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3222    production a BIT_NOT_EXPR is returned; the operand of the
3223    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3224    other productions, see the documentation accompanying the
3225    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3226    names are looked up in uninstantiated templates.  If DECLARATOR_P
3227    is true, the unqualified-id is appearing as part of a declarator,
3228    rather than as part of an expression.  */
3229
3230 static tree
3231 cp_parser_unqualified_id (cp_parser* parser,
3232                           bool template_keyword_p,
3233                           bool check_dependency_p,
3234                           bool declarator_p)
3235 {
3236   cp_token *token;
3237
3238   /* Peek at the next token.  */
3239   token = cp_lexer_peek_token (parser->lexer);
3240
3241   switch (token->type)
3242     {
3243     case CPP_NAME:
3244       {
3245         tree id;
3246
3247         /* We don't know yet whether or not this will be a
3248            template-id.  */
3249         cp_parser_parse_tentatively (parser);
3250         /* Try a template-id.  */
3251         id = cp_parser_template_id (parser, template_keyword_p,
3252                                     check_dependency_p,
3253                                     declarator_p);
3254         /* If it worked, we're done.  */
3255         if (cp_parser_parse_definitely (parser))
3256           return id;
3257         /* Otherwise, it's an ordinary identifier.  */
3258         return cp_parser_identifier (parser);
3259       }
3260
3261     case CPP_TEMPLATE_ID:
3262       return cp_parser_template_id (parser, template_keyword_p,
3263                                     check_dependency_p,
3264                                     declarator_p);
3265
3266     case CPP_COMPL:
3267       {
3268         tree type_decl;
3269         tree qualifying_scope;
3270         tree object_scope;
3271         tree scope;
3272         bool done;
3273
3274         /* Consume the `~' token.  */
3275         cp_lexer_consume_token (parser->lexer);
3276         /* Parse the class-name.  The standard, as written, seems to
3277            say that:
3278
3279              template <typename T> struct S { ~S (); };
3280              template <typename T> S<T>::~S() {}
3281
3282            is invalid, since `~' must be followed by a class-name, but
3283            `S<T>' is dependent, and so not known to be a class.
3284            That's not right; we need to look in uninstantiated
3285            templates.  A further complication arises from:
3286
3287              template <typename T> void f(T t) {
3288                t.T::~T();
3289              }
3290
3291            Here, it is not possible to look up `T' in the scope of `T'
3292            itself.  We must look in both the current scope, and the
3293            scope of the containing complete expression.
3294
3295            Yet another issue is:
3296
3297              struct S {
3298                int S;
3299                ~S();
3300              };
3301
3302              S::~S() {}
3303
3304            The standard does not seem to say that the `S' in `~S'
3305            should refer to the type `S' and not the data member
3306            `S::S'.  */
3307
3308         /* DR 244 says that we look up the name after the "~" in the
3309            same scope as we looked up the qualifying name.  That idea
3310            isn't fully worked out; it's more complicated than that.  */
3311         scope = parser->scope;
3312         object_scope = parser->object_scope;
3313         qualifying_scope = parser->qualifying_scope;
3314
3315         /* If the name is of the form "X::~X" it's OK.  */
3316         if (scope && TYPE_P (scope)
3317             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3318             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3319                 == CPP_OPEN_PAREN)
3320             && (cp_lexer_peek_token (parser->lexer)->value
3321                 == TYPE_IDENTIFIER (scope)))
3322           {
3323             cp_lexer_consume_token (parser->lexer);
3324             return build_nt (BIT_NOT_EXPR, scope);
3325           }
3326
3327         /* If there was an explicit qualification (S::~T), first look
3328            in the scope given by the qualification (i.e., S).  */
3329         done = false;
3330         type_decl = NULL_TREE;
3331         if (scope)
3332           {
3333             cp_parser_parse_tentatively (parser);
3334             type_decl = cp_parser_class_name (parser,
3335                                               /*typename_keyword_p=*/false,
3336                                               /*template_keyword_p=*/false,
3337                                               none_type,
3338                                               /*check_dependency=*/false,
3339                                               /*class_head_p=*/false,
3340                                               declarator_p);
3341             if (cp_parser_parse_definitely (parser))
3342               done = true;
3343           }
3344         /* In "N::S::~S", look in "N" as well.  */
3345         if (!done && scope && qualifying_scope)
3346           {
3347             cp_parser_parse_tentatively (parser);
3348             parser->scope = qualifying_scope;
3349             parser->object_scope = NULL_TREE;
3350             parser->qualifying_scope = NULL_TREE;
3351             type_decl
3352               = cp_parser_class_name (parser,
3353                                       /*typename_keyword_p=*/false,
3354                                       /*template_keyword_p=*/false,
3355                                       none_type,
3356                                       /*check_dependency=*/false,
3357                                       /*class_head_p=*/false,
3358                                       declarator_p);
3359             if (cp_parser_parse_definitely (parser))
3360               done = true;
3361           }
3362         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3363         else if (!done && object_scope)
3364           {
3365             cp_parser_parse_tentatively (parser);
3366             parser->scope = object_scope;
3367             parser->object_scope = NULL_TREE;
3368             parser->qualifying_scope = NULL_TREE;
3369             type_decl
3370               = cp_parser_class_name (parser,
3371                                       /*typename_keyword_p=*/false,
3372                                       /*template_keyword_p=*/false,
3373                                       none_type,
3374                                       /*check_dependency=*/false,
3375                                       /*class_head_p=*/false,
3376                                       declarator_p);
3377             if (cp_parser_parse_definitely (parser))
3378               done = true;
3379           }
3380         /* Look in the surrounding context.  */
3381         if (!done)
3382           {
3383             parser->scope = NULL_TREE;
3384             parser->object_scope = NULL_TREE;
3385             parser->qualifying_scope = NULL_TREE;
3386             type_decl
3387               = cp_parser_class_name (parser,
3388                                       /*typename_keyword_p=*/false,
3389                                       /*template_keyword_p=*/false,
3390                                       none_type,
3391                                       /*check_dependency=*/false,
3392                                       /*class_head_p=*/false,
3393                                       declarator_p);
3394           }
3395         /* If an error occurred, assume that the name of the
3396            destructor is the same as the name of the qualifying
3397            class.  That allows us to keep parsing after running
3398            into ill-formed destructor names.  */
3399         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3400           return build_nt (BIT_NOT_EXPR, scope);
3401         else if (type_decl == error_mark_node)
3402           return error_mark_node;
3403
3404         /* [class.dtor]
3405
3406            A typedef-name that names a class shall not be used as the
3407            identifier in the declarator for a destructor declaration.  */
3408         if (declarator_p
3409             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3410             && !DECL_SELF_REFERENCE_P (type_decl)
3411             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3412           error ("typedef-name %qD used as destructor declarator",
3413                  type_decl);
3414
3415         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3416       }
3417
3418     case CPP_KEYWORD:
3419       if (token->keyword == RID_OPERATOR)
3420         {
3421           tree id;
3422
3423           /* This could be a template-id, so we try that first.  */
3424           cp_parser_parse_tentatively (parser);
3425           /* Try a template-id.  */
3426           id = cp_parser_template_id (parser, template_keyword_p,
3427                                       /*check_dependency_p=*/true,
3428                                       declarator_p);
3429           /* If that worked, we're done.  */
3430           if (cp_parser_parse_definitely (parser))
3431             return id;
3432           /* We still don't know whether we're looking at an
3433              operator-function-id or a conversion-function-id.  */
3434           cp_parser_parse_tentatively (parser);
3435           /* Try an operator-function-id.  */
3436           id = cp_parser_operator_function_id (parser);
3437           /* If that didn't work, try a conversion-function-id.  */
3438           if (!cp_parser_parse_definitely (parser))
3439             id = cp_parser_conversion_function_id (parser);
3440
3441           return id;
3442         }
3443       /* Fall through.  */
3444
3445     default:
3446       cp_parser_error (parser, "expected unqualified-id");
3447       return error_mark_node;
3448     }
3449 }
3450
3451 /* Parse an (optional) nested-name-specifier.
3452
3453    nested-name-specifier:
3454      class-or-namespace-name :: nested-name-specifier [opt]
3455      class-or-namespace-name :: template nested-name-specifier [opt]
3456
3457    PARSER->SCOPE should be set appropriately before this function is
3458    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3459    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3460    in name lookups.
3461
3462    Sets PARSER->SCOPE to the class (TYPE) or namespace
3463    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3464    it unchanged if there is no nested-name-specifier.  Returns the new
3465    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3466
3467    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3468    part of a declaration and/or decl-specifier.  */
3469
3470 static tree
3471 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3472                                      bool typename_keyword_p,
3473                                      bool check_dependency_p,
3474                                      bool type_p,
3475                                      bool is_declaration)
3476 {
3477   bool success = false;
3478   tree access_check = NULL_TREE;
3479   cp_token_position start = 0;
3480   cp_token *token;
3481
3482   /* If the next token corresponds to a nested name specifier, there
3483      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3484      false, it may have been true before, in which case something
3485      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3486      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3487      CHECK_DEPENDENCY_P is false, we have to fall through into the
3488      main loop.  */
3489   if (check_dependency_p
3490       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3491     {
3492       cp_parser_pre_parsed_nested_name_specifier (parser);
3493       return parser->scope;
3494     }
3495
3496   /* Remember where the nested-name-specifier starts.  */
3497   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3498     start = cp_lexer_token_position (parser->lexer, false);
3499
3500   push_deferring_access_checks (dk_deferred);
3501
3502   while (true)
3503     {
3504       tree new_scope;
3505       tree old_scope;
3506       tree saved_qualifying_scope;
3507       bool template_keyword_p;
3508
3509       /* Spot cases that cannot be the beginning of a
3510          nested-name-specifier.  */
3511       token = cp_lexer_peek_token (parser->lexer);
3512
3513       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3514          the already parsed nested-name-specifier.  */
3515       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3516         {
3517           /* Grab the nested-name-specifier and continue the loop.  */
3518           cp_parser_pre_parsed_nested_name_specifier (parser);
3519           success = true;
3520           continue;
3521         }
3522
3523       /* Spot cases that cannot be the beginning of a
3524          nested-name-specifier.  On the second and subsequent times
3525          through the loop, we look for the `template' keyword.  */
3526       if (success && token->keyword == RID_TEMPLATE)
3527         ;
3528       /* A template-id can start a nested-name-specifier.  */
3529       else if (token->type == CPP_TEMPLATE_ID)
3530         ;
3531       else
3532         {
3533           /* If the next token is not an identifier, then it is
3534              definitely not a class-or-namespace-name.  */
3535           if (token->type != CPP_NAME)
3536             break;
3537           /* If the following token is neither a `<' (to begin a
3538              template-id), nor a `::', then we are not looking at a
3539              nested-name-specifier.  */
3540           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3541           if (token->type != CPP_SCOPE
3542               && !cp_parser_nth_token_starts_template_argument_list_p
3543                   (parser, 2))
3544             break;
3545         }
3546
3547       /* The nested-name-specifier is optional, so we parse
3548          tentatively.  */
3549       cp_parser_parse_tentatively (parser);
3550
3551       /* Look for the optional `template' keyword, if this isn't the
3552          first time through the loop.  */
3553       if (success)
3554         template_keyword_p = cp_parser_optional_template_keyword (parser);
3555       else
3556         template_keyword_p = false;
3557
3558       /* Save the old scope since the name lookup we are about to do
3559          might destroy it.  */
3560       old_scope = parser->scope;
3561       saved_qualifying_scope = parser->qualifying_scope;
3562       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3563          look up names in "X<T>::I" in order to determine that "Y" is
3564          a template.  So, if we have a typename at this point, we make
3565          an effort to look through it.  */
3566       if (is_declaration
3567           && !typename_keyword_p
3568           && parser->scope
3569           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3570         parser->scope = resolve_typename_type (parser->scope,
3571                                                /*only_current_p=*/false);
3572       /* Parse the qualifying entity.  */
3573       new_scope
3574         = cp_parser_class_or_namespace_name (parser,
3575                                              typename_keyword_p,
3576                                              template_keyword_p,
3577                                              check_dependency_p,
3578                                              type_p,
3579                                              is_declaration);
3580       /* Look for the `::' token.  */
3581       cp_parser_require (parser, CPP_SCOPE, "`::'");
3582
3583       /* If we found what we wanted, we keep going; otherwise, we're
3584          done.  */
3585       if (!cp_parser_parse_definitely (parser))
3586         {
3587           bool error_p = false;
3588
3589           /* Restore the OLD_SCOPE since it was valid before the
3590              failed attempt at finding the last
3591              class-or-namespace-name.  */
3592           parser->scope = old_scope;
3593           parser->qualifying_scope = saved_qualifying_scope;
3594           /* If the next token is an identifier, and the one after
3595              that is a `::', then any valid interpretation would have
3596              found a class-or-namespace-name.  */
3597           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3598                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3599                      == CPP_SCOPE)
3600                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3601                      != CPP_COMPL))
3602             {
3603               token = cp_lexer_consume_token (parser->lexer);
3604               if (!error_p)
3605                 {
3606                   tree decl;
3607
3608                   decl = cp_parser_lookup_name_simple (parser, token->value);
3609                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3610                     error ("%qD used without template parameters", decl);
3611                   else
3612                     cp_parser_name_lookup_error
3613                       (parser, token->value, decl,
3614                        "is not a class or namespace");
3615                   parser->scope = NULL_TREE;
3616                   error_p = true;
3617                   /* Treat this as a successful nested-name-specifier
3618                      due to:
3619
3620                      [basic.lookup.qual]
3621
3622                      If the name found is not a class-name (clause
3623                      _class_) or namespace-name (_namespace.def_), the
3624                      program is ill-formed.  */
3625                   success = true;
3626                 }
3627               cp_lexer_consume_token (parser->lexer);
3628             }
3629           break;
3630         }
3631
3632       /* We've found one valid nested-name-specifier.  */
3633       success = true;
3634       /* Make sure we look in the right scope the next time through
3635          the loop.  */
3636       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3637                        ? TREE_TYPE (new_scope)
3638                        : new_scope);
3639       /* If it is a class scope, try to complete it; we are about to
3640          be looking up names inside the class.  */
3641       if (TYPE_P (parser->scope)
3642           /* Since checking types for dependency can be expensive,
3643              avoid doing it if the type is already complete.  */
3644           && !COMPLETE_TYPE_P (parser->scope)
3645           /* Do not try to complete dependent types.  */
3646           && !dependent_type_p (parser->scope))
3647         complete_type (parser->scope);
3648     }
3649
3650   /* Retrieve any deferred checks.  Do not pop this access checks yet
3651      so the memory will not be reclaimed during token replacing below.  */
3652   access_check = get_deferred_access_checks ();
3653
3654   /* If parsing tentatively, replace the sequence of tokens that makes
3655      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3656      token.  That way, should we re-parse the token stream, we will
3657      not have to repeat the effort required to do the parse, nor will
3658      we issue duplicate error messages.  */
3659   if (success && start)
3660     {
3661       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3662
3663       /* Reset the contents of the START token.  */
3664       token->type = CPP_NESTED_NAME_SPECIFIER;
3665       token->value = build_tree_list (access_check, parser->scope);
3666       TREE_TYPE (token->value) = parser->qualifying_scope;
3667       token->keyword = RID_MAX;
3668
3669       /* Purge all subsequent tokens.  */
3670       cp_lexer_purge_tokens_after (parser->lexer, start);
3671     }
3672
3673   pop_deferring_access_checks ();
3674   return success ? parser->scope : NULL_TREE;
3675 }
3676
3677 /* Parse a nested-name-specifier.  See
3678    cp_parser_nested_name_specifier_opt for details.  This function
3679    behaves identically, except that it will an issue an error if no
3680    nested-name-specifier is present.  */
3681
3682 static tree
3683 cp_parser_nested_name_specifier (cp_parser *parser,
3684                                  bool typename_keyword_p,
3685                                  bool check_dependency_p,
3686                                  bool type_p,
3687                                  bool is_declaration)
3688 {
3689   tree scope;
3690
3691   /* Look for the nested-name-specifier.  */
3692   scope = cp_parser_nested_name_specifier_opt (parser,
3693                                                typename_keyword_p,
3694                                                check_dependency_p,
3695                                                type_p,
3696                                                is_declaration);
3697   /* If it was not present, issue an error message.  */
3698   if (!scope)
3699     {
3700       cp_parser_error (parser, "expected nested-name-specifier");
3701       parser->scope = NULL_TREE;
3702     }
3703
3704   return scope;
3705 }
3706
3707 /* Parse a class-or-namespace-name.
3708
3709    class-or-namespace-name:
3710      class-name
3711      namespace-name
3712
3713    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3714    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3715    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3716    TYPE_P is TRUE iff the next name should be taken as a class-name,
3717    even the same name is declared to be another entity in the same
3718    scope.
3719
3720    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3721    specified by the class-or-namespace-name.  If neither is found the
3722    ERROR_MARK_NODE is returned.  */
3723
3724 static tree
3725 cp_parser_class_or_namespace_name (cp_parser *parser,
3726                                    bool typename_keyword_p,
3727                                    bool template_keyword_p,
3728                                    bool check_dependency_p,
3729                                    bool type_p,
3730                                    bool is_declaration)
3731 {
3732   tree saved_scope;
3733   tree saved_qualifying_scope;
3734   tree saved_object_scope;
3735   tree scope;
3736   bool only_class_p;
3737
3738   /* Before we try to parse the class-name, we must save away the
3739      current PARSER->SCOPE since cp_parser_class_name will destroy
3740      it.  */
3741   saved_scope = parser->scope;
3742   saved_qualifying_scope = parser->qualifying_scope;
3743   saved_object_scope = parser->object_scope;
3744   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3745      there is no need to look for a namespace-name.  */
3746   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3747   if (!only_class_p)
3748     cp_parser_parse_tentatively (parser);
3749   scope = cp_parser_class_name (parser,
3750                                 typename_keyword_p,
3751                                 template_keyword_p,
3752                                 type_p ? class_type : none_type,
3753                                 check_dependency_p,
3754                                 /*class_head_p=*/false,
3755                                 is_declaration);
3756   /* If that didn't work, try for a namespace-name.  */
3757   if (!only_class_p && !cp_parser_parse_definitely (parser))
3758     {
3759       /* Restore the saved scope.  */
3760       parser->scope = saved_scope;
3761       parser->qualifying_scope = saved_qualifying_scope;
3762       parser->object_scope = saved_object_scope;
3763       /* If we are not looking at an identifier followed by the scope
3764          resolution operator, then this is not part of a
3765          nested-name-specifier.  (Note that this function is only used
3766          to parse the components of a nested-name-specifier.)  */
3767       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3768           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3769         return error_mark_node;
3770       scope = cp_parser_namespace_name (parser);
3771     }
3772
3773   return scope;
3774 }
3775
3776 /* Parse a postfix-expression.
3777
3778    postfix-expression:
3779      primary-expression
3780      postfix-expression [ expression ]
3781      postfix-expression ( expression-list [opt] )
3782      simple-type-specifier ( expression-list [opt] )
3783      typename :: [opt] nested-name-specifier identifier
3784        ( expression-list [opt] )
3785      typename :: [opt] nested-name-specifier template [opt] template-id
3786        ( expression-list [opt] )
3787      postfix-expression . template [opt] id-expression
3788      postfix-expression -> template [opt] id-expression
3789      postfix-expression . pseudo-destructor-name
3790      postfix-expression -> pseudo-destructor-name
3791      postfix-expression ++
3792      postfix-expression --
3793      dynamic_cast < type-id > ( expression )
3794      static_cast < type-id > ( expression )
3795      reinterpret_cast < type-id > ( expression )
3796      const_cast < type-id > ( expression )
3797      typeid ( expression )
3798      typeid ( type-id )
3799
3800    GNU Extension:
3801
3802    postfix-expression:
3803      ( type-id ) { initializer-list , [opt] }
3804
3805    This extension is a GNU version of the C99 compound-literal
3806    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3807    but they are essentially the same concept.)
3808
3809    If ADDRESS_P is true, the postfix expression is the operand of the
3810    `&' operator.  CAST_P is true if this expression is the target of a
3811    cast.
3812
3813    Returns a representation of the expression.  */
3814
3815 static tree
3816 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3817 {
3818   cp_token *token;
3819   enum rid keyword;
3820   cp_id_kind idk = CP_ID_KIND_NONE;
3821   tree postfix_expression = NULL_TREE;
3822   /* Non-NULL only if the current postfix-expression can be used to
3823      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3824      class used to qualify the member.  */
3825   tree qualifying_class = NULL_TREE;
3826
3827   /* Peek at the next token.  */
3828   token = cp_lexer_peek_token (parser->lexer);
3829   /* Some of the productions are determined by keywords.  */
3830   keyword = token->keyword;
3831   switch (keyword)
3832     {
3833     case RID_DYNCAST:
3834     case RID_STATCAST:
3835     case RID_REINTCAST:
3836     case RID_CONSTCAST:
3837       {
3838         tree type;
3839         tree expression;
3840         const char *saved_message;
3841
3842         /* All of these can be handled in the same way from the point
3843            of view of parsing.  Begin by consuming the token
3844            identifying the cast.  */
3845         cp_lexer_consume_token (parser->lexer);
3846
3847         /* New types cannot be defined in the cast.  */
3848         saved_message = parser->type_definition_forbidden_message;
3849         parser->type_definition_forbidden_message
3850           = "types may not be defined in casts";
3851
3852         /* Look for the opening `<'.  */
3853         cp_parser_require (parser, CPP_LESS, "`<'");
3854         /* Parse the type to which we are casting.  */
3855         type = cp_parser_type_id (parser);
3856         /* Look for the closing `>'.  */
3857         cp_parser_require (parser, CPP_GREATER, "`>'");
3858         /* Restore the old message.  */
3859         parser->type_definition_forbidden_message = saved_message;
3860
3861         /* And the expression which is being cast.  */
3862         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3863         expression = cp_parser_expression (parser, /*cast_p=*/true);
3864         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3865
3866         /* Only type conversions to integral or enumeration types
3867            can be used in constant-expressions.  */
3868         if (parser->integral_constant_expression_p
3869             && !dependent_type_p (type)
3870             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3871             && (cp_parser_non_integral_constant_expression
3872                 (parser,
3873                  "a cast to a type other than an integral or "
3874                  "enumeration type")))
3875           return error_mark_node;
3876
3877         switch (keyword)
3878           {
3879           case RID_DYNCAST:
3880             postfix_expression
3881               = build_dynamic_cast (type, expression);
3882             break;
3883           case RID_STATCAST:
3884             postfix_expression
3885               = build_static_cast (type, expression);
3886             break;
3887           case RID_REINTCAST:
3888             postfix_expression
3889               = build_reinterpret_cast (type, expression);
3890             break;
3891           case RID_CONSTCAST:
3892             postfix_expression
3893               = build_const_cast (type, expression);
3894             break;
3895           default:
3896             gcc_unreachable ();
3897           }
3898       }
3899       break;
3900
3901     case RID_TYPEID:
3902       {
3903         tree type;
3904         const char *saved_message;
3905         bool saved_in_type_id_in_expr_p;
3906
3907         /* Consume the `typeid' token.  */
3908         cp_lexer_consume_token (parser->lexer);
3909         /* Look for the `(' token.  */
3910         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3911         /* Types cannot be defined in a `typeid' expression.  */
3912         saved_message = parser->type_definition_forbidden_message;
3913         parser->type_definition_forbidden_message
3914           = "types may not be defined in a `typeid\' expression";
3915         /* We can't be sure yet whether we're looking at a type-id or an
3916            expression.  */
3917         cp_parser_parse_tentatively (parser);
3918         /* Try a type-id first.  */
3919         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3920         parser->in_type_id_in_expr_p = true;
3921         type = cp_parser_type_id (parser);
3922         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3923         /* Look for the `)' token.  Otherwise, we can't be sure that
3924            we're not looking at an expression: consider `typeid (int
3925            (3))', for example.  */
3926         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3927         /* If all went well, simply lookup the type-id.  */
3928         if (cp_parser_parse_definitely (parser))
3929           postfix_expression = get_typeid (type);
3930         /* Otherwise, fall back to the expression variant.  */
3931         else
3932           {
3933             tree expression;
3934
3935             /* Look for an expression.  */
3936             expression = cp_parser_expression (parser, /*cast_p=*/false);
3937             /* Compute its typeid.  */
3938             postfix_expression = build_typeid (expression);
3939             /* Look for the `)' token.  */
3940             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3941           }
3942         /* `typeid' may not appear in an integral constant expression.  */
3943         if (cp_parser_non_integral_constant_expression(parser,
3944                                                        "`typeid' operator"))
3945           return error_mark_node;
3946         /* Restore the saved message.  */
3947         parser->type_definition_forbidden_message = saved_message;
3948       }
3949       break;
3950
3951     case RID_TYPENAME:
3952       {
3953         bool template_p = false;
3954         tree id;
3955         tree type;
3956         tree scope;
3957
3958         /* Consume the `typename' token.  */
3959         cp_lexer_consume_token (parser->lexer);
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         else if (TREE_CODE (id) == TYPE_DECL
3991             && !dependent_type_p (parser->scope))
3992           type = TREE_TYPE (id);
3993         /* Create a TYPENAME_TYPE to represent the type to which the
3994            functional cast is being performed.  */
3995         else
3996           type = make_typename_type (parser->scope, id,
3997                                      typename_type,
3998                                      /*complain=*/1);
3999
4000         postfix_expression = cp_parser_functional_cast (parser, type);
4001       }
4002       break;
4003
4004     default:
4005       {
4006         tree type;
4007
4008         /* If the next thing is a simple-type-specifier, we may be
4009            looking at a functional cast.  We could also be looking at
4010            an id-expression.  So, we try the functional cast, and if
4011            that doesn't work we fall back to the primary-expression.  */
4012         cp_parser_parse_tentatively (parser);
4013         /* Look for the simple-type-specifier.  */
4014         type = cp_parser_simple_type_specifier (parser,
4015                                                 /*decl_specs=*/NULL,
4016                                                 CP_PARSER_FLAGS_NONE);
4017         /* Parse the cast itself.  */
4018         if (!cp_parser_error_occurred (parser))
4019           postfix_expression
4020             = cp_parser_functional_cast (parser, type);
4021         /* If that worked, we're done.  */
4022         if (cp_parser_parse_definitely (parser))
4023           break;
4024
4025         /* If the functional-cast didn't work out, try a
4026            compound-literal.  */
4027         if (cp_parser_allow_gnu_extensions_p (parser)
4028             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4029           {
4030             tree initializer_list = NULL_TREE;
4031             bool saved_in_type_id_in_expr_p;
4032
4033             cp_parser_parse_tentatively (parser);
4034             /* Consume the `('.  */
4035             cp_lexer_consume_token (parser->lexer);
4036             /* Parse the type.  */
4037             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4038             parser->in_type_id_in_expr_p = true;
4039             type = cp_parser_type_id (parser);
4040             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4041             /* Look for the `)'.  */
4042             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4043             /* Look for the `{'.  */
4044             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4045             /* If things aren't going well, there's no need to
4046                keep going.  */
4047             if (!cp_parser_error_occurred (parser))
4048               {
4049                 bool non_constant_p;
4050                 /* Parse the initializer-list.  */
4051                 initializer_list
4052                   = cp_parser_initializer_list (parser, &non_constant_p);
4053                 /* Allow a trailing `,'.  */
4054                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4055                   cp_lexer_consume_token (parser->lexer);
4056                 /* Look for the final `}'.  */
4057                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4058               }
4059             /* If that worked, we're definitely looking at a
4060                compound-literal expression.  */
4061             if (cp_parser_parse_definitely (parser))
4062               {
4063                 /* Warn the user that a compound literal is not
4064                    allowed in standard C++.  */
4065                 if (pedantic)
4066                   pedwarn ("ISO C++ forbids compound-literals");
4067                 /* Form the representation of the compound-literal.  */
4068                 postfix_expression
4069                   = finish_compound_literal (type, initializer_list);
4070                 break;
4071               }
4072           }
4073
4074         /* It must be a primary-expression.  */
4075         postfix_expression = cp_parser_primary_expression (parser,
4076                                                            cast_p,
4077                                                            &idk,
4078                                                            &qualifying_class);
4079       }
4080       break;
4081     }
4082
4083   /* If we were avoiding committing to the processing of a
4084      qualified-id until we knew whether or not we had a
4085      pointer-to-member, we now know.  */
4086   if (qualifying_class)
4087     {
4088       bool done;
4089
4090       /* Peek at the next token.  */
4091       token = cp_lexer_peek_token (parser->lexer);
4092       done = (token->type != CPP_OPEN_SQUARE
4093               && token->type != CPP_OPEN_PAREN
4094               && token->type != CPP_DOT
4095               && token->type != CPP_DEREF
4096               && token->type != CPP_PLUS_PLUS
4097               && token->type != CPP_MINUS_MINUS);
4098
4099       postfix_expression = finish_qualified_id_expr (qualifying_class,
4100                                                      postfix_expression,
4101                                                      done,
4102                                                      address_p);
4103       if (done)
4104         return postfix_expression;
4105     }
4106
4107   /* Keep looping until the postfix-expression is complete.  */
4108   while (true)
4109     {
4110       if (idk == CP_ID_KIND_UNQUALIFIED
4111           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4112           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4113         /* It is not a Koenig lookup function call.  */
4114         postfix_expression
4115           = unqualified_name_lookup_error (postfix_expression);
4116
4117       /* Peek at the next token.  */
4118       token = cp_lexer_peek_token (parser->lexer);
4119
4120       switch (token->type)
4121         {
4122         case CPP_OPEN_SQUARE:
4123           postfix_expression
4124             = cp_parser_postfix_open_square_expression (parser,
4125                                                         postfix_expression,
4126                                                         false);
4127           idk = CP_ID_KIND_NONE;
4128           break;
4129
4130         case CPP_OPEN_PAREN:
4131           /* postfix-expression ( expression-list [opt] ) */
4132           {
4133             bool koenig_p;
4134             bool is_builtin_constant_p;
4135             bool saved_integral_constant_expression_p = false;
4136             bool saved_non_integral_constant_expression_p = false;
4137             tree args;
4138
4139             is_builtin_constant_p
4140               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4141             if (is_builtin_constant_p)
4142               {
4143                 /* The whole point of __builtin_constant_p is to allow
4144                    non-constant expressions to appear as arguments.  */
4145                 saved_integral_constant_expression_p
4146                   = parser->integral_constant_expression_p;
4147                 saved_non_integral_constant_expression_p
4148                   = parser->non_integral_constant_expression_p;
4149                 parser->integral_constant_expression_p = false;
4150               }
4151             args = (cp_parser_parenthesized_expression_list
4152                     (parser, /*is_attribute_list=*/false,
4153                      /*cast_p=*/false,
4154                      /*non_constant_p=*/NULL));
4155             if (is_builtin_constant_p)
4156               {
4157                 parser->integral_constant_expression_p
4158                   = saved_integral_constant_expression_p;
4159                 parser->non_integral_constant_expression_p
4160                   = saved_non_integral_constant_expression_p;
4161               }
4162
4163             if (args == error_mark_node)
4164               {
4165                 postfix_expression = error_mark_node;
4166                 break;
4167               }
4168
4169             /* Function calls are not permitted in
4170                constant-expressions.  */
4171             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4172                 && cp_parser_non_integral_constant_expression (parser,
4173                                                                "a function call"))
4174               {
4175                 postfix_expression = error_mark_node;
4176                 break;
4177               }
4178
4179             koenig_p = false;
4180             if (idk == CP_ID_KIND_UNQUALIFIED)
4181               {
4182                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4183                   {
4184                     if (args)
4185                       {
4186                         koenig_p = true;
4187                         postfix_expression
4188                           = perform_koenig_lookup (postfix_expression, args);
4189                       }
4190                     else
4191                       postfix_expression
4192                         = unqualified_fn_lookup_error (postfix_expression);
4193                   }
4194                 /* We do not perform argument-dependent lookup if
4195                    normal lookup finds a non-function, in accordance
4196                    with the expected resolution of DR 218.  */
4197                 else if (args && is_overloaded_fn (postfix_expression))
4198                   {
4199                     tree fn = get_first_fn (postfix_expression);
4200
4201                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4202                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4203
4204                     /* Only do argument dependent lookup if regular
4205                        lookup does not find a set of member functions.
4206                        [basic.lookup.koenig]/2a  */
4207                     if (!DECL_FUNCTION_MEMBER_P (fn))
4208                       {
4209                         koenig_p = true;
4210                         postfix_expression
4211                           = perform_koenig_lookup (postfix_expression, args);
4212                       }
4213                   }
4214               }
4215
4216             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4217               {
4218                 tree instance = TREE_OPERAND (postfix_expression, 0);
4219                 tree fn = TREE_OPERAND (postfix_expression, 1);
4220
4221                 if (processing_template_decl
4222                     && (type_dependent_expression_p (instance)
4223                         || (!BASELINK_P (fn)
4224                             && TREE_CODE (fn) != FIELD_DECL)
4225                         || type_dependent_expression_p (fn)
4226                         || any_type_dependent_arguments_p (args)))
4227                   {
4228                     postfix_expression
4229                       = build_min_nt (CALL_EXPR, postfix_expression,
4230                                       args, NULL_TREE);
4231                     break;
4232                   }
4233
4234                 if (BASELINK_P (fn))
4235                   postfix_expression
4236                     = (build_new_method_call
4237                        (instance, fn, args, NULL_TREE,
4238                         (idk == CP_ID_KIND_QUALIFIED
4239                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4240                 else
4241                   postfix_expression
4242                     = finish_call_expr (postfix_expression, args,
4243                                         /*disallow_virtual=*/false,
4244                                         /*koenig_p=*/false);
4245               }
4246             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4247                      || TREE_CODE (postfix_expression) == MEMBER_REF
4248                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4249               postfix_expression = (build_offset_ref_call_from_tree
4250                                     (postfix_expression, args));
4251             else if (idk == CP_ID_KIND_QUALIFIED)
4252               /* A call to a static class member, or a namespace-scope
4253                  function.  */
4254               postfix_expression
4255                 = finish_call_expr (postfix_expression, args,
4256                                     /*disallow_virtual=*/true,
4257                                     koenig_p);
4258             else
4259               /* All other function calls.  */
4260               postfix_expression
4261                 = finish_call_expr (postfix_expression, args,
4262                                     /*disallow_virtual=*/false,
4263                                     koenig_p);
4264
4265             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4266             idk = CP_ID_KIND_NONE;
4267           }
4268           break;
4269
4270         case CPP_DOT:
4271         case CPP_DEREF:
4272           /* postfix-expression . template [opt] id-expression
4273              postfix-expression . pseudo-destructor-name
4274              postfix-expression -> template [opt] id-expression
4275              postfix-expression -> pseudo-destructor-name */
4276
4277           /* Consume the `.' or `->' operator.  */
4278           cp_lexer_consume_token (parser->lexer);
4279
4280           postfix_expression
4281             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4282                                                       postfix_expression,
4283                                                       false, &idk);
4284           break;
4285
4286         case CPP_PLUS_PLUS:
4287           /* postfix-expression ++  */
4288           /* Consume the `++' token.  */
4289           cp_lexer_consume_token (parser->lexer);
4290           /* Generate a representation for the complete expression.  */
4291           postfix_expression
4292             = finish_increment_expr (postfix_expression,
4293                                      POSTINCREMENT_EXPR);
4294           /* Increments may not appear in constant-expressions.  */
4295           if (cp_parser_non_integral_constant_expression (parser,
4296                                                           "an increment"))
4297             postfix_expression = error_mark_node;
4298           idk = CP_ID_KIND_NONE;
4299           break;
4300
4301         case CPP_MINUS_MINUS:
4302           /* postfix-expression -- */
4303           /* Consume the `--' token.  */
4304           cp_lexer_consume_token (parser->lexer);
4305           /* Generate a representation for the complete expression.  */
4306           postfix_expression
4307             = finish_increment_expr (postfix_expression,
4308                                      POSTDECREMENT_EXPR);
4309           /* Decrements may not appear in constant-expressions.  */
4310           if (cp_parser_non_integral_constant_expression (parser,
4311                                                           "a decrement"))
4312             postfix_expression = error_mark_node;
4313           idk = CP_ID_KIND_NONE;
4314           break;
4315
4316         default:
4317           return postfix_expression;
4318         }
4319     }
4320
4321   /* We should never get here.  */
4322   gcc_unreachable ();
4323   return error_mark_node;
4324 }
4325
4326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4327    by cp_parser_builtin_offsetof.  We're looking for
4328
4329      postfix-expression [ expression ]
4330
4331    FOR_OFFSETOF is set if we're being called in that context, which
4332    changes how we deal with integer constant expressions.  */
4333
4334 static tree
4335 cp_parser_postfix_open_square_expression (cp_parser *parser,
4336                                           tree postfix_expression,
4337                                           bool for_offsetof)
4338 {
4339   tree index;
4340
4341   /* Consume the `[' token.  */
4342   cp_lexer_consume_token (parser->lexer);
4343
4344   /* Parse the index expression.  */
4345   /* ??? For offsetof, there is a question of what to allow here.  If
4346      offsetof is not being used in an integral constant expression context,
4347      then we *could* get the right answer by computing the value at runtime.
4348      If we are in an integral constant expression context, then we might
4349      could accept any constant expression; hard to say without analysis.
4350      Rather than open the barn door too wide right away, allow only integer
4351      constant expressions here.  */
4352   if (for_offsetof)
4353     index = cp_parser_constant_expression (parser, false, NULL);
4354   else
4355     index = cp_parser_expression (parser, /*cast_p=*/false);
4356
4357   /* Look for the closing `]'.  */
4358   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4359
4360   /* Build the ARRAY_REF.  */
4361   postfix_expression = grok_array_decl (postfix_expression, index);
4362
4363   /* When not doing offsetof, array references are not permitted in
4364      constant-expressions.  */
4365   if (!for_offsetof
4366       && (cp_parser_non_integral_constant_expression
4367           (parser, "an array reference")))
4368     postfix_expression = error_mark_node;
4369
4370   return postfix_expression;
4371 }
4372
4373 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4374    by cp_parser_builtin_offsetof.  We're looking for
4375
4376      postfix-expression . template [opt] id-expression
4377      postfix-expression . pseudo-destructor-name
4378      postfix-expression -> template [opt] id-expression
4379      postfix-expression -> pseudo-destructor-name
4380
4381    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4382    limits what of the above we'll actually accept, but nevermind.
4383    TOKEN_TYPE is the "." or "->" token, which will already have been
4384    removed from the stream.  */
4385
4386 static tree
4387 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4388                                         enum cpp_ttype token_type,
4389                                         tree postfix_expression,
4390                                         bool for_offsetof, cp_id_kind *idk)
4391 {
4392   tree name;
4393   bool dependent_p;
4394   bool template_p;
4395   bool pseudo_destructor_p;
4396   tree scope = NULL_TREE;
4397
4398   /* If this is a `->' operator, dereference the pointer.  */
4399   if (token_type == CPP_DEREF)
4400     postfix_expression = build_x_arrow (postfix_expression);
4401   /* Check to see whether or not the expression is type-dependent.  */
4402   dependent_p = type_dependent_expression_p (postfix_expression);
4403   /* The identifier following the `->' or `.' is not qualified.  */
4404   parser->scope = NULL_TREE;
4405   parser->qualifying_scope = NULL_TREE;
4406   parser->object_scope = NULL_TREE;
4407   *idk = CP_ID_KIND_NONE;
4408   /* Enter the scope corresponding to the type of the object
4409      given by the POSTFIX_EXPRESSION.  */
4410   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4411     {
4412       scope = TREE_TYPE (postfix_expression);
4413       /* According to the standard, no expression should ever have
4414          reference type.  Unfortunately, we do not currently match
4415          the standard in this respect in that our internal representation
4416          of an expression may have reference type even when the standard
4417          says it does not.  Therefore, we have to manually obtain the
4418          underlying type here.  */
4419       scope = non_reference (scope);
4420       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4421       scope = complete_type_or_else (scope, NULL_TREE);
4422       /* Let the name lookup machinery know that we are processing a
4423          class member access expression.  */
4424       parser->context->object_type = scope;
4425       /* If something went wrong, we want to be able to discern that case,
4426          as opposed to the case where there was no SCOPE due to the type
4427          of expression being dependent.  */
4428       if (!scope)
4429         scope = error_mark_node;
4430       /* If the SCOPE was erroneous, make the various semantic analysis
4431          functions exit quickly -- and without issuing additional error
4432          messages.  */
4433       if (scope == error_mark_node)
4434         postfix_expression = error_mark_node;
4435     }
4436
4437   /* Assume this expression is not a pseudo-destructor access.  */
4438   pseudo_destructor_p = false;
4439
4440   /* If the SCOPE is a scalar type, then, if this is a valid program,
4441      we must be looking at a pseudo-destructor-name.  */
4442   if (scope && SCALAR_TYPE_P (scope))
4443     {
4444       tree s;
4445       tree type;
4446
4447       cp_parser_parse_tentatively (parser);
4448       /* Parse the pseudo-destructor-name.  */
4449       s = NULL_TREE;
4450       cp_parser_pseudo_destructor_name (parser, &s, &type);
4451       if (cp_parser_parse_definitely (parser))
4452         {
4453           pseudo_destructor_p = true;
4454           postfix_expression
4455             = finish_pseudo_destructor_expr (postfix_expression,
4456                                              s, TREE_TYPE (type));
4457         }
4458     }
4459
4460   if (!pseudo_destructor_p)
4461     {
4462       /* If the SCOPE is not a scalar type, we are looking at an
4463          ordinary class member access expression, rather than a
4464          pseudo-destructor-name.  */
4465       template_p = cp_parser_optional_template_keyword (parser);
4466       /* Parse the id-expression.  */
4467       name = cp_parser_id_expression (parser, template_p,
4468                                       /*check_dependency_p=*/true,
4469                                       /*template_p=*/NULL,
4470                                       /*declarator_p=*/false);
4471       /* In general, build a SCOPE_REF if the member name is qualified.
4472          However, if the name was not dependent and has already been
4473          resolved; there is no need to build the SCOPE_REF.  For example;
4474
4475              struct X { void f(); };
4476              template <typename T> void f(T* t) { t->X::f(); }
4477
4478          Even though "t" is dependent, "X::f" is not and has been resolved
4479          to a BASELINK; there is no need to include scope information.  */
4480
4481       /* But we do need to remember that there was an explicit scope for
4482          virtual function calls.  */
4483       if (parser->scope)
4484         *idk = CP_ID_KIND_QUALIFIED;
4485
4486       /* If the name is a template-id that names a type, we will get a
4487          TYPE_DECL here.  That is invalid code.  */
4488       if (TREE_CODE (name) == TYPE_DECL)
4489         {
4490           error ("invalid use of %qD", name);
4491           postfix_expression = error_mark_node;
4492         }
4493       else
4494         {
4495           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4496             {
4497               name = build_nt (SCOPE_REF, parser->scope, name);
4498               parser->scope = NULL_TREE;
4499               parser->qualifying_scope = NULL_TREE;
4500               parser->object_scope = NULL_TREE;
4501             }
4502           if (scope && name && BASELINK_P (name))
4503             adjust_result_of_qualified_name_lookup
4504               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4505           postfix_expression
4506             = finish_class_member_access_expr (postfix_expression, name);
4507         }
4508     }
4509
4510   /* We no longer need to look up names in the scope of the object on
4511      the left-hand side of the `.' or `->' operator.  */
4512   parser->context->object_type = NULL_TREE;
4513
4514   /* Outside of offsetof, these operators may not appear in
4515      constant-expressions.  */
4516   if (!for_offsetof
4517       && (cp_parser_non_integral_constant_expression
4518           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4519     postfix_expression = error_mark_node;
4520
4521   return postfix_expression;
4522 }
4523
4524 /* Parse a parenthesized expression-list.
4525
4526    expression-list:
4527      assignment-expression
4528      expression-list, assignment-expression
4529
4530    attribute-list:
4531      expression-list
4532      identifier
4533      identifier, expression-list
4534
4535    CAST_P is true if this expression is the target of a cast.
4536
4537    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4538    representation of an assignment-expression.  Note that a TREE_LIST
4539    is returned even if there is only a single expression in the list.
4540    error_mark_node is returned if the ( and or ) are
4541    missing. NULL_TREE is returned on no expressions. The parentheses
4542    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4543    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4544    indicates whether or not all of the expressions in the list were
4545    constant.  */
4546
4547 static tree
4548 cp_parser_parenthesized_expression_list (cp_parser* parser,
4549                                          bool is_attribute_list,
4550                                          bool cast_p,
4551                                          bool *non_constant_p)
4552 {
4553   tree expression_list = NULL_TREE;
4554   bool fold_expr_p = is_attribute_list;
4555   tree identifier = NULL_TREE;
4556
4557   /* Assume all the expressions will be constant.  */
4558   if (non_constant_p)
4559     *non_constant_p = false;
4560
4561   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4562     return error_mark_node;
4563
4564   /* Consume expressions until there are no more.  */
4565   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4566     while (true)
4567       {
4568         tree expr;
4569
4570         /* At the beginning of attribute lists, check to see if the
4571            next token is an identifier.  */
4572         if (is_attribute_list
4573             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4574           {
4575             cp_token *token;
4576
4577             /* Consume the identifier.  */
4578             token = cp_lexer_consume_token (parser->lexer);
4579             /* Save the identifier.  */
4580             identifier = token->value;
4581           }
4582         else
4583           {
4584             /* Parse the next assignment-expression.  */
4585             if (non_constant_p)
4586               {
4587                 bool expr_non_constant_p;
4588                 expr = (cp_parser_constant_expression
4589                         (parser, /*allow_non_constant_p=*/true,
4590                          &expr_non_constant_p));
4591                 if (expr_non_constant_p)
4592                   *non_constant_p = true;
4593               }
4594             else
4595               expr = cp_parser_assignment_expression (parser, cast_p);
4596
4597             if (fold_expr_p)
4598               expr = fold_non_dependent_expr (expr);
4599
4600              /* Add it to the list.  We add error_mark_node
4601                 expressions to the list, so that we can still tell if
4602                 the correct form for a parenthesized expression-list
4603                 is found. That gives better errors.  */
4604             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4605
4606             if (expr == error_mark_node)
4607               goto skip_comma;
4608           }
4609
4610         /* After the first item, attribute lists look the same as
4611            expression lists.  */
4612         is_attribute_list = false;
4613
4614       get_comma:;
4615         /* If the next token isn't a `,', then we are done.  */
4616         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4617           break;
4618
4619         /* Otherwise, consume the `,' and keep going.  */
4620         cp_lexer_consume_token (parser->lexer);
4621       }
4622
4623   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4624     {
4625       int ending;
4626
4627     skip_comma:;
4628       /* We try and resync to an unnested comma, as that will give the
4629          user better diagnostics.  */
4630       ending = cp_parser_skip_to_closing_parenthesis (parser,
4631                                                       /*recovering=*/true,
4632                                                       /*or_comma=*/true,
4633                                                       /*consume_paren=*/true);
4634       if (ending < 0)
4635         goto get_comma;
4636       if (!ending)
4637         return error_mark_node;
4638     }
4639
4640   /* We built up the list in reverse order so we must reverse it now.  */
4641   expression_list = nreverse (expression_list);
4642   if (identifier)
4643     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4644
4645   return expression_list;
4646 }
4647
4648 /* Parse a pseudo-destructor-name.
4649
4650    pseudo-destructor-name:
4651      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4652      :: [opt] nested-name-specifier template template-id :: ~ type-name
4653      :: [opt] nested-name-specifier [opt] ~ type-name
4654
4655    If either of the first two productions is used, sets *SCOPE to the
4656    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4657    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4658    or ERROR_MARK_NODE if the parse fails.  */
4659
4660 static void
4661 cp_parser_pseudo_destructor_name (cp_parser* parser,
4662                                   tree* scope,
4663                                   tree* type)
4664 {
4665   bool nested_name_specifier_p;
4666
4667   /* Assume that things will not work out.  */
4668   *type = error_mark_node;
4669
4670   /* Look for the optional `::' operator.  */
4671   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4672   /* Look for the optional nested-name-specifier.  */
4673   nested_name_specifier_p
4674     = (cp_parser_nested_name_specifier_opt (parser,
4675                                             /*typename_keyword_p=*/false,
4676                                             /*check_dependency_p=*/true,
4677                                             /*type_p=*/false,
4678                                             /*is_declaration=*/true)
4679        != NULL_TREE);
4680   /* Now, if we saw a nested-name-specifier, we might be doing the
4681      second production.  */
4682   if (nested_name_specifier_p
4683       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4684     {
4685       /* Consume the `template' keyword.  */
4686       cp_lexer_consume_token (parser->lexer);
4687       /* Parse the template-id.  */
4688       cp_parser_template_id (parser,
4689                              /*template_keyword_p=*/true,
4690                              /*check_dependency_p=*/false,
4691                              /*is_declaration=*/true);
4692       /* Look for the `::' token.  */
4693       cp_parser_require (parser, CPP_SCOPE, "`::'");
4694     }
4695   /* If the next token is not a `~', then there might be some
4696      additional qualification.  */
4697   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4698     {
4699       /* Look for the type-name.  */
4700       *scope = TREE_TYPE (cp_parser_type_name (parser));
4701
4702       if (*scope == error_mark_node)
4703         return;
4704
4705       /* If we don't have ::~, then something has gone wrong.  Since
4706          the only caller of this function is looking for something
4707          after `.' or `->' after a scalar type, most likely the
4708          program is trying to get a member of a non-aggregate
4709          type.  */
4710       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4711           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4712         {
4713           cp_parser_error (parser, "request for member of non-aggregate type");
4714           return;
4715         }
4716
4717       /* Look for the `::' token.  */
4718       cp_parser_require (parser, CPP_SCOPE, "`::'");
4719     }
4720   else
4721     *scope = NULL_TREE;
4722
4723   /* Look for the `~'.  */
4724   cp_parser_require (parser, CPP_COMPL, "`~'");
4725   /* Look for the type-name again.  We are not responsible for
4726      checking that it matches the first type-name.  */
4727   *type = cp_parser_type_name (parser);
4728 }
4729
4730 /* Parse a unary-expression.
4731
4732    unary-expression:
4733      postfix-expression
4734      ++ cast-expression
4735      -- cast-expression
4736      unary-operator cast-expression
4737      sizeof unary-expression
4738      sizeof ( type-id )
4739      new-expression
4740      delete-expression
4741
4742    GNU Extensions:
4743
4744    unary-expression:
4745      __extension__ cast-expression
4746      __alignof__ unary-expression
4747      __alignof__ ( type-id )
4748      __real__ cast-expression
4749      __imag__ cast-expression
4750      && identifier
4751
4752    ADDRESS_P is true iff the unary-expression is appearing as the
4753    operand of the `&' operator.   CAST_P is true if this expression is
4754    the target of a cast.
4755
4756    Returns a representation of the expression.  */
4757
4758 static tree
4759 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4760 {
4761   cp_token *token;
4762   enum tree_code unary_operator;
4763
4764   /* Peek at the next token.  */
4765   token = cp_lexer_peek_token (parser->lexer);
4766   /* Some keywords give away the kind of expression.  */
4767   if (token->type == CPP_KEYWORD)
4768     {
4769       enum rid keyword = token->keyword;
4770
4771       switch (keyword)
4772         {
4773         case RID_ALIGNOF:
4774         case RID_SIZEOF:
4775           {
4776             tree operand;
4777             enum tree_code op;
4778
4779             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4780             /* Consume the token.  */
4781             cp_lexer_consume_token (parser->lexer);
4782             /* Parse the operand.  */
4783             operand = cp_parser_sizeof_operand (parser, keyword);
4784
4785             if (TYPE_P (operand))
4786               return cxx_sizeof_or_alignof_type (operand, op, true);
4787             else
4788               return cxx_sizeof_or_alignof_expr (operand, op);
4789           }
4790
4791         case RID_NEW:
4792           return cp_parser_new_expression (parser);
4793
4794         case RID_DELETE:
4795           return cp_parser_delete_expression (parser);
4796
4797         case RID_EXTENSION:
4798           {
4799             /* The saved value of the PEDANTIC flag.  */
4800             int saved_pedantic;
4801             tree expr;
4802
4803             /* Save away the PEDANTIC flag.  */
4804             cp_parser_extension_opt (parser, &saved_pedantic);
4805             /* Parse the cast-expression.  */
4806             expr = cp_parser_simple_cast_expression (parser);
4807             /* Restore the PEDANTIC flag.  */
4808             pedantic = saved_pedantic;
4809
4810             return expr;
4811           }
4812
4813         case RID_REALPART:
4814         case RID_IMAGPART:
4815           {
4816             tree expression;
4817
4818             /* Consume the `__real__' or `__imag__' token.  */
4819             cp_lexer_consume_token (parser->lexer);
4820             /* Parse the cast-expression.  */
4821             expression = cp_parser_simple_cast_expression (parser);
4822             /* Create the complete representation.  */
4823             return build_x_unary_op ((keyword == RID_REALPART
4824                                       ? REALPART_EXPR : IMAGPART_EXPR),
4825                                      expression);
4826           }
4827           break;
4828
4829         default:
4830           break;
4831         }
4832     }
4833
4834   /* Look for the `:: new' and `:: delete', which also signal the
4835      beginning of a new-expression, or delete-expression,
4836      respectively.  If the next token is `::', then it might be one of
4837      these.  */
4838   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4839     {
4840       enum rid keyword;
4841
4842       /* See if the token after the `::' is one of the keywords in
4843          which we're interested.  */
4844       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4845       /* If it's `new', we have a new-expression.  */
4846       if (keyword == RID_NEW)
4847         return cp_parser_new_expression (parser);
4848       /* Similarly, for `delete'.  */
4849       else if (keyword == RID_DELETE)
4850         return cp_parser_delete_expression (parser);
4851     }
4852
4853   /* Look for a unary operator.  */
4854   unary_operator = cp_parser_unary_operator (token);
4855   /* The `++' and `--' operators can be handled similarly, even though
4856      they are not technically unary-operators in the grammar.  */
4857   if (unary_operator == ERROR_MARK)
4858     {
4859       if (token->type == CPP_PLUS_PLUS)
4860         unary_operator = PREINCREMENT_EXPR;
4861       else if (token->type == CPP_MINUS_MINUS)
4862         unary_operator = PREDECREMENT_EXPR;
4863       /* Handle the GNU address-of-label extension.  */
4864       else if (cp_parser_allow_gnu_extensions_p (parser)
4865                && token->type == CPP_AND_AND)
4866         {
4867           tree identifier;
4868
4869           /* Consume the '&&' token.  */
4870           cp_lexer_consume_token (parser->lexer);
4871           /* Look for the identifier.  */
4872           identifier = cp_parser_identifier (parser);
4873           /* Create an expression representing the address.  */
4874           return finish_label_address_expr (identifier);
4875         }
4876     }
4877   if (unary_operator != ERROR_MARK)
4878     {
4879       tree cast_expression;
4880       tree expression = error_mark_node;
4881       const char *non_constant_p = NULL;
4882
4883       /* Consume the operator token.  */
4884       token = cp_lexer_consume_token (parser->lexer);
4885       /* Parse the cast-expression.  */
4886       cast_expression
4887         = cp_parser_cast_expression (parser,
4888                                      unary_operator == ADDR_EXPR,
4889                                      /*cast_p=*/false);
4890       /* Now, build an appropriate representation.  */
4891       switch (unary_operator)
4892         {
4893         case INDIRECT_REF:
4894           non_constant_p = "`*'";
4895           expression = build_x_indirect_ref (cast_expression, "unary *");
4896           break;
4897
4898         case ADDR_EXPR:
4899           non_constant_p = "`&'";
4900           /* Fall through.  */
4901         case BIT_NOT_EXPR:
4902           expression = build_x_unary_op (unary_operator, cast_expression);
4903           break;
4904
4905         case PREINCREMENT_EXPR:
4906         case PREDECREMENT_EXPR:
4907           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4908                             ? "`++'" : "`--'");
4909           /* Fall through.  */
4910         case UNARY_PLUS_EXPR:
4911         case NEGATE_EXPR:
4912         case TRUTH_NOT_EXPR:
4913           expression = finish_unary_op_expr (unary_operator, cast_expression);
4914           break;
4915
4916         default:
4917           gcc_unreachable ();
4918         }
4919
4920       if (non_constant_p
4921           && cp_parser_non_integral_constant_expression (parser,
4922                                                          non_constant_p))
4923         expression = error_mark_node;
4924
4925       return expression;
4926     }
4927
4928   return cp_parser_postfix_expression (parser, address_p, cast_p);
4929 }
4930
4931 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4932    unary-operator, the corresponding tree code is returned.  */
4933
4934 static enum tree_code
4935 cp_parser_unary_operator (cp_token* token)
4936 {
4937   switch (token->type)
4938     {
4939     case CPP_MULT:
4940       return INDIRECT_REF;
4941
4942     case CPP_AND:
4943       return ADDR_EXPR;
4944
4945     case CPP_PLUS:
4946       return UNARY_PLUS_EXPR;
4947
4948     case CPP_MINUS:
4949       return NEGATE_EXPR;
4950
4951     case CPP_NOT:
4952       return TRUTH_NOT_EXPR;
4953
4954     case CPP_COMPL:
4955       return BIT_NOT_EXPR;
4956
4957     default:
4958       return ERROR_MARK;
4959     }
4960 }
4961
4962 /* Parse a new-expression.
4963
4964    new-expression:
4965      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4966      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4967
4968    Returns a representation of the expression.  */
4969
4970 static tree
4971 cp_parser_new_expression (cp_parser* parser)
4972 {
4973   bool global_scope_p;
4974   tree placement;
4975   tree type;
4976   tree initializer;
4977   tree nelts;
4978
4979   /* Look for the optional `::' operator.  */
4980   global_scope_p
4981     = (cp_parser_global_scope_opt (parser,
4982                                    /*current_scope_valid_p=*/false)
4983        != NULL_TREE);
4984   /* Look for the `new' operator.  */
4985   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4986   /* There's no easy way to tell a new-placement from the
4987      `( type-id )' construct.  */
4988   cp_parser_parse_tentatively (parser);
4989   /* Look for a new-placement.  */
4990   placement = cp_parser_new_placement (parser);
4991   /* If that didn't work out, there's no new-placement.  */
4992   if (!cp_parser_parse_definitely (parser))
4993     placement = NULL_TREE;
4994
4995   /* If the next token is a `(', then we have a parenthesized
4996      type-id.  */
4997   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4998     {
4999       /* Consume the `('.  */
5000       cp_lexer_consume_token (parser->lexer);
5001       /* Parse the type-id.  */
5002       type = cp_parser_type_id (parser);
5003       /* Look for the closing `)'.  */
5004       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5005       /* There should not be a direct-new-declarator in this production,
5006          but GCC used to allowed this, so we check and emit a sensible error
5007          message for this case.  */
5008       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5009         {
5010           error ("array bound forbidden after parenthesized type-id");
5011           inform ("try removing the parentheses around the type-id");
5012           cp_parser_direct_new_declarator (parser);
5013         }
5014       nelts = NULL_TREE;
5015     }
5016   /* Otherwise, there must be a new-type-id.  */
5017   else
5018     type = cp_parser_new_type_id (parser, &nelts);
5019
5020   /* If the next token is a `(', then we have a new-initializer.  */
5021   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5022     initializer = cp_parser_new_initializer (parser);
5023   else
5024     initializer = NULL_TREE;
5025
5026   /* A new-expression may not appear in an integral constant
5027      expression.  */
5028   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5029     return error_mark_node;
5030
5031   /* Create a representation of the new-expression.  */
5032   return build_new (placement, type, nelts, initializer, global_scope_p);
5033 }
5034
5035 /* Parse a new-placement.
5036
5037    new-placement:
5038      ( expression-list )
5039
5040    Returns the same representation as for an expression-list.  */
5041
5042 static tree
5043 cp_parser_new_placement (cp_parser* parser)
5044 {
5045   tree expression_list;
5046
5047   /* Parse the expression-list.  */
5048   expression_list = (cp_parser_parenthesized_expression_list
5049                      (parser, false, /*cast_p=*/false,
5050                       /*non_constant_p=*/NULL));
5051
5052   return expression_list;
5053 }
5054
5055 /* Parse a new-type-id.
5056
5057    new-type-id:
5058      type-specifier-seq new-declarator [opt]
5059
5060    Returns the TYPE allocated.  If the new-type-id indicates an array
5061    type, *NELTS is set to the number of elements in the last array
5062    bound; the TYPE will not include the last array bound.  */
5063
5064 static tree
5065 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5066 {
5067   cp_decl_specifier_seq type_specifier_seq;
5068   cp_declarator *new_declarator;
5069   cp_declarator *declarator;
5070   cp_declarator *outer_declarator;
5071   const char *saved_message;
5072   tree type;
5073
5074   /* The type-specifier sequence must not contain type definitions.
5075      (It cannot contain declarations of new types either, but if they
5076      are not definitions we will catch that because they are not
5077      complete.)  */
5078   saved_message = parser->type_definition_forbidden_message;
5079   parser->type_definition_forbidden_message
5080     = "types may not be defined in a new-type-id";
5081   /* Parse the type-specifier-seq.  */
5082   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5083                                 &type_specifier_seq);
5084   /* Restore the old message.  */
5085   parser->type_definition_forbidden_message = saved_message;
5086   /* Parse the new-declarator.  */
5087   new_declarator = cp_parser_new_declarator_opt (parser);
5088
5089   /* Determine the number of elements in the last array dimension, if
5090      any.  */
5091   *nelts = NULL_TREE;
5092   /* Skip down to the last array dimension.  */
5093   declarator = new_declarator;
5094   outer_declarator = NULL;
5095   while (declarator && (declarator->kind == cdk_pointer
5096                         || declarator->kind == cdk_ptrmem))
5097     {
5098       outer_declarator = declarator;
5099       declarator = declarator->declarator;
5100     }
5101   while (declarator
5102          && declarator->kind == cdk_array
5103          && declarator->declarator
5104          && declarator->declarator->kind == cdk_array)
5105     {
5106       outer_declarator = declarator;
5107       declarator = declarator->declarator;
5108     }
5109
5110   if (declarator && declarator->kind == cdk_array)
5111     {
5112       *nelts = declarator->u.array.bounds;
5113       if (*nelts == error_mark_node)
5114         *nelts = integer_one_node;
5115
5116       if (outer_declarator)
5117         outer_declarator->declarator = declarator->declarator;
5118       else
5119         new_declarator = NULL;
5120     }
5121
5122   type = groktypename (&type_specifier_seq, new_declarator);
5123   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5124     {
5125       *nelts = array_type_nelts_top (type);
5126       type = TREE_TYPE (type);
5127     }
5128   return type;
5129 }
5130
5131 /* Parse an (optional) new-declarator.
5132
5133    new-declarator:
5134      ptr-operator new-declarator [opt]
5135      direct-new-declarator
5136
5137    Returns the declarator.  */
5138
5139 static cp_declarator *
5140 cp_parser_new_declarator_opt (cp_parser* parser)
5141 {
5142   enum tree_code code;
5143   tree type;
5144   cp_cv_quals cv_quals;
5145
5146   /* We don't know if there's a ptr-operator next, or not.  */
5147   cp_parser_parse_tentatively (parser);
5148   /* Look for a ptr-operator.  */
5149   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5150   /* If that worked, look for more new-declarators.  */
5151   if (cp_parser_parse_definitely (parser))
5152     {
5153       cp_declarator *declarator;
5154
5155       /* Parse another optional declarator.  */
5156       declarator = cp_parser_new_declarator_opt (parser);
5157
5158       /* Create the representation of the declarator.  */
5159       if (type)
5160         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5161       else if (code == INDIRECT_REF)
5162         declarator = make_pointer_declarator (cv_quals, declarator);
5163       else
5164         declarator = make_reference_declarator (cv_quals, declarator);
5165
5166       return declarator;
5167     }
5168
5169   /* If the next token is a `[', there is a direct-new-declarator.  */
5170   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5171     return cp_parser_direct_new_declarator (parser);
5172
5173   return NULL;
5174 }
5175
5176 /* Parse a direct-new-declarator.
5177
5178    direct-new-declarator:
5179      [ expression ]
5180      direct-new-declarator [constant-expression]
5181
5182    */
5183
5184 static cp_declarator *
5185 cp_parser_direct_new_declarator (cp_parser* parser)
5186 {
5187   cp_declarator *declarator = NULL;
5188
5189   while (true)
5190     {
5191       tree expression;
5192
5193       /* Look for the opening `['.  */
5194       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5195       /* The first expression is not required to be constant.  */
5196       if (!declarator)
5197         {
5198           expression = cp_parser_expression (parser, /*cast_p=*/false);
5199           /* The standard requires that the expression have integral
5200              type.  DR 74 adds enumeration types.  We believe that the
5201              real intent is that these expressions be handled like the
5202              expression in a `switch' condition, which also allows
5203              classes with a single conversion to integral or
5204              enumeration type.  */
5205           if (!processing_template_decl)
5206             {
5207               expression
5208                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5209                                               expression,
5210                                               /*complain=*/true);
5211               if (!expression)
5212                 {
5213                   error ("expression in new-declarator must have integral "
5214                          "or enumeration type");
5215                   expression = error_mark_node;
5216                 }
5217             }
5218         }
5219       /* But all the other expressions must be.  */
5220       else
5221         expression
5222           = cp_parser_constant_expression (parser,
5223                                            /*allow_non_constant=*/false,
5224                                            NULL);
5225       /* Look for the closing `]'.  */
5226       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5227
5228       /* Add this bound to the declarator.  */
5229       declarator = make_array_declarator (declarator, expression);
5230
5231       /* If the next token is not a `[', then there are no more
5232          bounds.  */
5233       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5234         break;
5235     }
5236
5237   return declarator;
5238 }
5239
5240 /* Parse a new-initializer.
5241
5242    new-initializer:
5243      ( expression-list [opt] )
5244
5245    Returns a representation of the expression-list.  If there is no
5246    expression-list, VOID_ZERO_NODE is returned.  */
5247
5248 static tree
5249 cp_parser_new_initializer (cp_parser* parser)
5250 {
5251   tree expression_list;
5252
5253   expression_list = (cp_parser_parenthesized_expression_list
5254                      (parser, false, /*cast_p=*/false,
5255                       /*non_constant_p=*/NULL));
5256   if (!expression_list)
5257     expression_list = void_zero_node;
5258
5259   return expression_list;
5260 }
5261
5262 /* Parse a delete-expression.
5263
5264    delete-expression:
5265      :: [opt] delete cast-expression
5266      :: [opt] delete [ ] cast-expression
5267
5268    Returns a representation of the expression.  */
5269
5270 static tree
5271 cp_parser_delete_expression (cp_parser* parser)
5272 {
5273   bool global_scope_p;
5274   bool array_p;
5275   tree expression;
5276
5277   /* Look for the optional `::' operator.  */
5278   global_scope_p
5279     = (cp_parser_global_scope_opt (parser,
5280                                    /*current_scope_valid_p=*/false)
5281        != NULL_TREE);
5282   /* Look for the `delete' keyword.  */
5283   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5284   /* See if the array syntax is in use.  */
5285   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5286     {
5287       /* Consume the `[' token.  */
5288       cp_lexer_consume_token (parser->lexer);
5289       /* Look for the `]' token.  */
5290       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5291       /* Remember that this is the `[]' construct.  */
5292       array_p = true;
5293     }
5294   else
5295     array_p = false;
5296
5297   /* Parse the cast-expression.  */
5298   expression = cp_parser_simple_cast_expression (parser);
5299
5300   /* A delete-expression may not appear in an integral constant
5301      expression.  */
5302   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5303     return error_mark_node;
5304
5305   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5306 }
5307
5308 /* Parse a cast-expression.
5309
5310    cast-expression:
5311      unary-expression
5312      ( type-id ) cast-expression
5313
5314    ADDRESS_P is true iff the unary-expression is appearing as the
5315    operand of the `&' operator.   CAST_P is true if this expression is
5316    the target of a cast.
5317
5318    Returns a representation of the expression.  */
5319
5320 static tree
5321 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5322 {
5323   /* If it's a `(', then we might be looking at a cast.  */
5324   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5325     {
5326       tree type = NULL_TREE;
5327       tree expr = NULL_TREE;
5328       bool compound_literal_p;
5329       const char *saved_message;
5330
5331       /* There's no way to know yet whether or not this is a cast.
5332          For example, `(int (3))' is a unary-expression, while `(int)
5333          3' is a cast.  So, we resort to parsing tentatively.  */
5334       cp_parser_parse_tentatively (parser);
5335       /* Types may not be defined in a cast.  */
5336       saved_message = parser->type_definition_forbidden_message;
5337       parser->type_definition_forbidden_message
5338         = "types may not be defined in casts";
5339       /* Consume the `('.  */
5340       cp_lexer_consume_token (parser->lexer);
5341       /* A very tricky bit is that `(struct S) { 3 }' is a
5342          compound-literal (which we permit in C++ as an extension).
5343          But, that construct is not a cast-expression -- it is a
5344          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5345          is legal; if the compound-literal were a cast-expression,
5346          you'd need an extra set of parentheses.)  But, if we parse
5347          the type-id, and it happens to be a class-specifier, then we
5348          will commit to the parse at that point, because we cannot
5349          undo the action that is done when creating a new class.  So,
5350          then we cannot back up and do a postfix-expression.
5351
5352          Therefore, we scan ahead to the closing `)', and check to see
5353          if the token after the `)' is a `{'.  If so, we are not
5354          looking at a cast-expression.
5355
5356          Save tokens so that we can put them back.  */
5357       cp_lexer_save_tokens (parser->lexer);
5358       /* Skip tokens until the next token is a closing parenthesis.
5359          If we find the closing `)', and the next token is a `{', then
5360          we are looking at a compound-literal.  */
5361       compound_literal_p
5362         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5363                                                   /*consume_paren=*/true)
5364            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5365       /* Roll back the tokens we skipped.  */
5366       cp_lexer_rollback_tokens (parser->lexer);
5367       /* If we were looking at a compound-literal, simulate an error
5368          so that the call to cp_parser_parse_definitely below will
5369          fail.  */
5370       if (compound_literal_p)
5371         cp_parser_simulate_error (parser);
5372       else
5373         {
5374           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5375           parser->in_type_id_in_expr_p = true;
5376           /* Look for the type-id.  */
5377           type = cp_parser_type_id (parser);
5378           /* Look for the closing `)'.  */
5379           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5380           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5381         }
5382
5383       /* Restore the saved message.  */
5384       parser->type_definition_forbidden_message = saved_message;
5385
5386       /* If ok so far, parse the dependent expression. We cannot be
5387          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5388          ctor of T, but looks like a cast to function returning T
5389          without a dependent expression.  */
5390       if (!cp_parser_error_occurred (parser))
5391         expr = cp_parser_cast_expression (parser,
5392                                           /*address_p=*/false,
5393                                           /*cast_p=*/true);
5394
5395       if (cp_parser_parse_definitely (parser))
5396         {
5397           /* Warn about old-style casts, if so requested.  */
5398           if (warn_old_style_cast
5399               && !in_system_header
5400               && !VOID_TYPE_P (type)
5401               && current_lang_name != lang_name_c)
5402             warning (0, "use of old-style cast");
5403
5404           /* Only type conversions to integral or enumeration types
5405              can be used in constant-expressions.  */
5406           if (parser->integral_constant_expression_p
5407               && !dependent_type_p (type)
5408               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5409               && (cp_parser_non_integral_constant_expression
5410                   (parser,
5411                    "a cast to a type other than an integral or "
5412                    "enumeration type")))
5413             return error_mark_node;
5414
5415           /* Perform the cast.  */
5416           expr = build_c_cast (type, expr);
5417           return expr;
5418         }
5419     }
5420
5421   /* If we get here, then it's not a cast, so it must be a
5422      unary-expression.  */
5423   return cp_parser_unary_expression (parser, address_p, cast_p);
5424 }
5425
5426 /* Parse a binary expression of the general form:
5427
5428    pm-expression:
5429      cast-expression
5430      pm-expression .* cast-expression
5431      pm-expression ->* cast-expression
5432
5433    multiplicative-expression:
5434      pm-expression
5435      multiplicative-expression * pm-expression
5436      multiplicative-expression / pm-expression
5437      multiplicative-expression % pm-expression
5438
5439    additive-expression:
5440      multiplicative-expression
5441      additive-expression + multiplicative-expression
5442      additive-expression - multiplicative-expression
5443
5444    shift-expression:
5445      additive-expression
5446      shift-expression << additive-expression
5447      shift-expression >> additive-expression
5448
5449    relational-expression:
5450      shift-expression
5451      relational-expression < shift-expression
5452      relational-expression > shift-expression
5453      relational-expression <= shift-expression
5454      relational-expression >= shift-expression
5455
5456   GNU Extension:
5457
5458    relational-expression:
5459      relational-expression <? shift-expression
5460      relational-expression >? shift-expression
5461
5462    equality-expression:
5463      relational-expression
5464      equality-expression == relational-expression
5465      equality-expression != relational-expression
5466
5467    and-expression:
5468      equality-expression
5469      and-expression & equality-expression
5470
5471    exclusive-or-expression:
5472      and-expression
5473      exclusive-or-expression ^ and-expression
5474
5475    inclusive-or-expression:
5476      exclusive-or-expression
5477      inclusive-or-expression | exclusive-or-expression
5478
5479    logical-and-expression:
5480      inclusive-or-expression
5481      logical-and-expression && inclusive-or-expression
5482
5483    logical-or-expression:
5484      logical-and-expression
5485      logical-or-expression || logical-and-expression
5486
5487    All these are implemented with a single function like:
5488
5489    binary-expression:
5490      simple-cast-expression
5491      binary-expression <token> binary-expression
5492
5493    CAST_P is true if this expression is the target of a cast.
5494
5495    The binops_by_token map is used to get the tree codes for each <token> type.
5496    binary-expressions are associated according to a precedence table.  */
5497
5498 #define TOKEN_PRECEDENCE(token) \
5499   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5500    ? PREC_NOT_OPERATOR \
5501    : binops_by_token[token->type].prec)
5502
5503 static tree
5504 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5505 {
5506   cp_parser_expression_stack stack;
5507   cp_parser_expression_stack_entry *sp = &stack[0];
5508   tree lhs, rhs;
5509   cp_token *token;
5510   enum tree_code tree_type;
5511   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5512   bool overloaded_p;
5513
5514   /* Parse the first expression.  */
5515   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5516
5517   for (;;)
5518     {
5519       /* Get an operator token.  */
5520       token = cp_lexer_peek_token (parser->lexer);
5521       if (token->type == CPP_MIN || token->type == CPP_MAX)
5522         cp_parser_warn_min_max ();
5523
5524       new_prec = TOKEN_PRECEDENCE (token);
5525
5526       /* Popping an entry off the stack means we completed a subexpression:
5527          - either we found a token which is not an operator (`>' where it is not
5528            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5529            will happen repeatedly;
5530          - or, we found an operator which has lower priority.  This is the case
5531            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5532            parsing `3 * 4'.  */
5533       if (new_prec <= prec)
5534         {
5535           if (sp == stack)
5536             break;
5537           else
5538             goto pop;
5539         }
5540
5541      get_rhs:
5542       tree_type = binops_by_token[token->type].tree_type;
5543
5544       /* We used the operator token.  */
5545       cp_lexer_consume_token (parser->lexer);
5546
5547       /* Extract another operand.  It may be the RHS of this expression
5548          or the LHS of a new, higher priority expression.  */
5549       rhs = cp_parser_simple_cast_expression (parser);
5550
5551       /* Get another operator token.  Look up its precedence to avoid
5552          building a useless (immediately popped) stack entry for common
5553          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5554       token = cp_lexer_peek_token (parser->lexer);
5555       lookahead_prec = TOKEN_PRECEDENCE (token);
5556       if (lookahead_prec > new_prec)
5557         {
5558           /* ... and prepare to parse the RHS of the new, higher priority
5559              expression.  Since precedence levels on the stack are
5560              monotonically increasing, we do not have to care about
5561              stack overflows.  */
5562           sp->prec = prec;
5563           sp->tree_type = tree_type;
5564           sp->lhs = lhs;
5565           sp++;
5566           lhs = rhs;
5567           prec = new_prec;
5568           new_prec = lookahead_prec;
5569           goto get_rhs;
5570
5571          pop:
5572           /* If the stack is not empty, we have parsed into LHS the right side
5573              (`4' in the example above) of an expression we had suspended.
5574              We can use the information on the stack to recover the LHS (`3')
5575              from the stack together with the tree code (`MULT_EXPR'), and
5576              the precedence of the higher level subexpression
5577              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5578              which will be used to actually build the additive expression.  */
5579           --sp;
5580           prec = sp->prec;
5581           tree_type = sp->tree_type;
5582           rhs = lhs;
5583           lhs = sp->lhs;
5584         }
5585
5586       overloaded_p = false;
5587       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5588
5589       /* If the binary operator required the use of an overloaded operator,
5590          then this expression cannot be an integral constant-expression.
5591          An overloaded operator can be used even if both operands are
5592          otherwise permissible in an integral constant-expression if at
5593          least one of the operands is of enumeration type.  */
5594
5595       if (overloaded_p
5596           && (cp_parser_non_integral_constant_expression
5597               (parser, "calls to overloaded operators")))
5598         return error_mark_node;
5599     }
5600
5601   return lhs;
5602 }
5603
5604
5605 /* Parse the `? expression : assignment-expression' part of a
5606    conditional-expression.  The LOGICAL_OR_EXPR is the
5607    logical-or-expression that started the conditional-expression.
5608    Returns a representation of the entire conditional-expression.
5609
5610    This routine is used by cp_parser_assignment_expression.
5611
5612      ? expression : assignment-expression
5613
5614    GNU Extensions:
5615
5616      ? : assignment-expression */
5617
5618 static tree
5619 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5620 {
5621   tree expr;
5622   tree assignment_expr;
5623
5624   /* Consume the `?' token.  */
5625   cp_lexer_consume_token (parser->lexer);
5626   if (cp_parser_allow_gnu_extensions_p (parser)
5627       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5628     /* Implicit true clause.  */
5629     expr = NULL_TREE;
5630   else
5631     /* Parse the expression.  */
5632     expr = cp_parser_expression (parser, /*cast_p=*/false);
5633
5634   /* The next token should be a `:'.  */
5635   cp_parser_require (parser, CPP_COLON, "`:'");
5636   /* Parse the assignment-expression.  */
5637   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5638
5639   /* Build the conditional-expression.  */
5640   return build_x_conditional_expr (logical_or_expr,
5641                                    expr,
5642                                    assignment_expr);
5643 }
5644
5645 /* Parse an assignment-expression.
5646
5647    assignment-expression:
5648      conditional-expression
5649      logical-or-expression assignment-operator assignment_expression
5650      throw-expression
5651
5652    CAST_P is true if this expression is the target of a cast.
5653
5654    Returns a representation for the expression.  */
5655
5656 static tree
5657 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5658 {
5659   tree expr;
5660
5661   /* If the next token is the `throw' keyword, then we're looking at
5662      a throw-expression.  */
5663   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5664     expr = cp_parser_throw_expression (parser);
5665   /* Otherwise, it must be that we are looking at a
5666      logical-or-expression.  */
5667   else
5668     {
5669       /* Parse the binary expressions (logical-or-expression).  */
5670       expr = cp_parser_binary_expression (parser, cast_p);
5671       /* If the next token is a `?' then we're actually looking at a
5672          conditional-expression.  */
5673       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5674         return cp_parser_question_colon_clause (parser, expr);
5675       else
5676         {
5677           enum tree_code assignment_operator;
5678
5679           /* If it's an assignment-operator, we're using the second
5680              production.  */
5681           assignment_operator
5682             = cp_parser_assignment_operator_opt (parser);
5683           if (assignment_operator != ERROR_MARK)
5684             {
5685               tree rhs;
5686
5687               /* Parse the right-hand side of the assignment.  */
5688               rhs = cp_parser_assignment_expression (parser, cast_p);
5689               /* An assignment may not appear in a
5690                  constant-expression.  */
5691               if (cp_parser_non_integral_constant_expression (parser,
5692                                                               "an assignment"))
5693                 return error_mark_node;
5694               /* Build the assignment expression.  */
5695               expr = build_x_modify_expr (expr,
5696                                           assignment_operator,
5697                                           rhs);
5698             }
5699         }
5700     }
5701
5702   return expr;
5703 }
5704
5705 /* Parse an (optional) assignment-operator.
5706
5707    assignment-operator: one of
5708      = *= /= %= += -= >>= <<= &= ^= |=
5709
5710    GNU Extension:
5711
5712    assignment-operator: one of
5713      <?= >?=
5714
5715    If the next token is an assignment operator, the corresponding tree
5716    code is returned, and the token is consumed.  For example, for
5717    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5718    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5719    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5720    operator, ERROR_MARK is returned.  */
5721
5722 static enum tree_code
5723 cp_parser_assignment_operator_opt (cp_parser* parser)
5724 {
5725   enum tree_code op;
5726   cp_token *token;
5727
5728   /* Peek at the next toen.  */
5729   token = cp_lexer_peek_token (parser->lexer);
5730
5731   switch (token->type)
5732     {
5733     case CPP_EQ:
5734       op = NOP_EXPR;
5735       break;
5736
5737     case CPP_MULT_EQ:
5738       op = MULT_EXPR;
5739       break;
5740
5741     case CPP_DIV_EQ:
5742       op = TRUNC_DIV_EXPR;
5743       break;
5744
5745     case CPP_MOD_EQ:
5746       op = TRUNC_MOD_EXPR;
5747       break;
5748
5749     case CPP_PLUS_EQ:
5750       op = PLUS_EXPR;
5751       break;
5752
5753     case CPP_MINUS_EQ:
5754       op = MINUS_EXPR;
5755       break;
5756
5757     case CPP_RSHIFT_EQ:
5758       op = RSHIFT_EXPR;
5759       break;
5760
5761     case CPP_LSHIFT_EQ:
5762       op = LSHIFT_EXPR;
5763       break;
5764
5765     case CPP_AND_EQ:
5766       op = BIT_AND_EXPR;
5767       break;
5768
5769     case CPP_XOR_EQ:
5770       op = BIT_XOR_EXPR;
5771       break;
5772
5773     case CPP_OR_EQ:
5774       op = BIT_IOR_EXPR;
5775       break;
5776
5777     case CPP_MIN_EQ:
5778       op = MIN_EXPR;
5779       cp_parser_warn_min_max ();
5780       break;
5781
5782     case CPP_MAX_EQ:
5783       op = MAX_EXPR;
5784       cp_parser_warn_min_max ();
5785       break;
5786
5787     default:
5788       /* Nothing else is an assignment operator.  */
5789       op = ERROR_MARK;
5790     }
5791
5792   /* If it was an assignment operator, consume it.  */
5793   if (op != ERROR_MARK)
5794     cp_lexer_consume_token (parser->lexer);
5795
5796   return op;
5797 }
5798
5799 /* Parse an expression.
5800
5801    expression:
5802      assignment-expression
5803      expression , assignment-expression
5804
5805    CAST_P is true if this expression is the target of a cast.
5806
5807    Returns a representation of the expression.  */
5808
5809 static tree
5810 cp_parser_expression (cp_parser* parser, bool cast_p)
5811 {
5812   tree expression = NULL_TREE;
5813
5814   while (true)
5815     {
5816       tree assignment_expression;
5817
5818       /* Parse the next assignment-expression.  */
5819       assignment_expression
5820         = cp_parser_assignment_expression (parser, cast_p);
5821       /* If this is the first assignment-expression, we can just
5822          save it away.  */
5823       if (!expression)
5824         expression = assignment_expression;
5825       else
5826         expression = build_x_compound_expr (expression,
5827                                             assignment_expression);
5828       /* If the next token is not a comma, then we are done with the
5829          expression.  */
5830       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5831         break;
5832       /* Consume the `,'.  */
5833       cp_lexer_consume_token (parser->lexer);
5834       /* A comma operator cannot appear in a constant-expression.  */
5835       if (cp_parser_non_integral_constant_expression (parser,
5836                                                       "a comma operator"))
5837         expression = error_mark_node;
5838     }
5839
5840   return expression;
5841 }
5842
5843 /* Parse a constant-expression.
5844
5845    constant-expression:
5846      conditional-expression
5847
5848   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5849   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5850   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5851   is false, NON_CONSTANT_P should be NULL.  */
5852
5853 static tree
5854 cp_parser_constant_expression (cp_parser* parser,
5855                                bool allow_non_constant_p,
5856                                bool *non_constant_p)
5857 {
5858   bool saved_integral_constant_expression_p;
5859   bool saved_allow_non_integral_constant_expression_p;
5860   bool saved_non_integral_constant_expression_p;
5861   tree expression;
5862
5863   /* It might seem that we could simply parse the
5864      conditional-expression, and then check to see if it were
5865      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5866      one that the compiler can figure out is constant, possibly after
5867      doing some simplifications or optimizations.  The standard has a
5868      precise definition of constant-expression, and we must honor
5869      that, even though it is somewhat more restrictive.
5870
5871      For example:
5872
5873        int i[(2, 3)];
5874
5875      is not a legal declaration, because `(2, 3)' is not a
5876      constant-expression.  The `,' operator is forbidden in a
5877      constant-expression.  However, GCC's constant-folding machinery
5878      will fold this operation to an INTEGER_CST for `3'.  */
5879
5880   /* Save the old settings.  */
5881   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5882   saved_allow_non_integral_constant_expression_p
5883     = parser->allow_non_integral_constant_expression_p;
5884   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5885   /* We are now parsing a constant-expression.  */
5886   parser->integral_constant_expression_p = true;
5887   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5888   parser->non_integral_constant_expression_p = false;
5889   /* Although the grammar says "conditional-expression", we parse an
5890      "assignment-expression", which also permits "throw-expression"
5891      and the use of assignment operators.  In the case that
5892      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5893      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5894      actually essential that we look for an assignment-expression.
5895      For example, cp_parser_initializer_clauses uses this function to
5896      determine whether a particular assignment-expression is in fact
5897      constant.  */
5898   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5899   /* Restore the old settings.  */
5900   parser->integral_constant_expression_p
5901     = saved_integral_constant_expression_p;
5902   parser->allow_non_integral_constant_expression_p
5903     = saved_allow_non_integral_constant_expression_p;
5904   if (allow_non_constant_p)
5905     *non_constant_p = parser->non_integral_constant_expression_p;
5906   else if (parser->non_integral_constant_expression_p)
5907     expression = error_mark_node;
5908   parser->non_integral_constant_expression_p
5909     = saved_non_integral_constant_expression_p;
5910
5911   return expression;
5912 }
5913
5914 /* Parse __builtin_offsetof.
5915
5916    offsetof-expression:
5917      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5918
5919    offsetof-member-designator:
5920      id-expression
5921      | offsetof-member-designator "." id-expression
5922      | offsetof-member-designator "[" expression "]"
5923 */
5924
5925 static tree
5926 cp_parser_builtin_offsetof (cp_parser *parser)
5927 {
5928   int save_ice_p, save_non_ice_p;
5929   tree type, expr;
5930   cp_id_kind dummy;
5931
5932   /* We're about to accept non-integral-constant things, but will
5933      definitely yield an integral constant expression.  Save and
5934      restore these values around our local parsing.  */
5935   save_ice_p = parser->integral_constant_expression_p;
5936   save_non_ice_p = parser->non_integral_constant_expression_p;
5937
5938   /* Consume the "__builtin_offsetof" token.  */
5939   cp_lexer_consume_token (parser->lexer);
5940   /* Consume the opening `('.  */
5941   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5942   /* Parse the type-id.  */
5943   type = cp_parser_type_id (parser);
5944   /* Look for the `,'.  */
5945   cp_parser_require (parser, CPP_COMMA, "`,'");
5946
5947   /* Build the (type *)null that begins the traditional offsetof macro.  */
5948   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5949
5950   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5951   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5952                                                  true, &dummy);
5953   while (true)
5954     {
5955       cp_token *token = cp_lexer_peek_token (parser->lexer);
5956       switch (token->type)
5957         {
5958         case CPP_OPEN_SQUARE:
5959           /* offsetof-member-designator "[" expression "]" */
5960           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5961           break;
5962
5963         case CPP_DOT:
5964           /* offsetof-member-designator "." identifier */
5965           cp_lexer_consume_token (parser->lexer);
5966           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5967                                                          true, &dummy);
5968           break;
5969
5970         case CPP_CLOSE_PAREN:
5971           /* Consume the ")" token.  */
5972           cp_lexer_consume_token (parser->lexer);
5973           goto success;
5974
5975         default:
5976           /* Error.  We know the following require will fail, but
5977              that gives the proper error message.  */
5978           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5979           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5980           expr = error_mark_node;
5981           goto failure;
5982         }
5983     }
5984
5985  success:
5986   /* If we're processing a template, we can't finish the semantics yet.
5987      Otherwise we can fold the entire expression now.  */
5988   if (processing_template_decl)
5989     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5990   else
5991     expr = fold_offsetof (expr);
5992
5993  failure:
5994   parser->integral_constant_expression_p = save_ice_p;
5995   parser->non_integral_constant_expression_p = save_non_ice_p;
5996
5997   return expr;
5998 }
5999
6000 /* Statements [gram.stmt.stmt]  */
6001
6002 /* Parse a statement.
6003
6004    statement:
6005      labeled-statement
6006      expression-statement
6007      compound-statement
6008      selection-statement
6009      iteration-statement
6010      jump-statement
6011      declaration-statement
6012      try-block  */
6013
6014 static void
6015 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6016 {
6017   tree statement;
6018   cp_token *token;
6019   location_t statement_location;
6020
6021   /* There is no statement yet.  */
6022   statement = NULL_TREE;
6023   /* Peek at the next token.  */
6024   token = cp_lexer_peek_token (parser->lexer);
6025   /* Remember the location of the first token in the statement.  */
6026   statement_location = token->location;
6027   /* If this is a keyword, then that will often determine what kind of
6028      statement we have.  */
6029   if (token->type == CPP_KEYWORD)
6030     {
6031       enum rid keyword = token->keyword;
6032
6033       switch (keyword)
6034         {
6035         case RID_CASE:
6036         case RID_DEFAULT:
6037           statement = cp_parser_labeled_statement (parser,
6038                                                    in_statement_expr);
6039           break;
6040
6041         case RID_IF:
6042         case RID_SWITCH:
6043           statement = cp_parser_selection_statement (parser);
6044           break;
6045
6046         case RID_WHILE:
6047         case RID_DO:
6048         case RID_FOR:
6049           statement = cp_parser_iteration_statement (parser);
6050           break;
6051
6052         case RID_BREAK:
6053         case RID_CONTINUE:
6054         case RID_RETURN:
6055         case RID_GOTO:
6056           statement = cp_parser_jump_statement (parser);
6057           break;
6058
6059           /* Objective-C++ exception-handling constructs.  */
6060         case RID_AT_TRY:
6061         case RID_AT_CATCH:
6062         case RID_AT_FINALLY:
6063         case RID_AT_SYNCHRONIZED:
6064         case RID_AT_THROW:
6065           statement = cp_parser_objc_statement (parser);
6066           break;
6067
6068         case RID_TRY:
6069           statement = cp_parser_try_block (parser);
6070           break;
6071
6072         default:
6073           /* It might be a keyword like `int' that can start a
6074              declaration-statement.  */
6075           break;
6076         }
6077     }
6078   else if (token->type == CPP_NAME)
6079     {
6080       /* If the next token is a `:', then we are looking at a
6081          labeled-statement.  */
6082       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6083       if (token->type == CPP_COLON)
6084         statement = cp_parser_labeled_statement (parser, in_statement_expr);
6085     }
6086   /* Anything that starts with a `{' must be a compound-statement.  */
6087   else if (token->type == CPP_OPEN_BRACE)
6088     statement = cp_parser_compound_statement (parser, NULL, false);
6089   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6090      a statement all its own.  */
6091   else if (token->type == CPP_PRAGMA)
6092     {
6093       cp_lexer_handle_pragma (parser->lexer);
6094       return;
6095     }
6096
6097   /* Everything else must be a declaration-statement or an
6098      expression-statement.  Try for the declaration-statement
6099      first, unless we are looking at a `;', in which case we know that
6100      we have an expression-statement.  */
6101   if (!statement)
6102     {
6103       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6104         {
6105           cp_parser_parse_tentatively (parser);
6106           /* Try to parse the declaration-statement.  */
6107           cp_parser_declaration_statement (parser);
6108           /* If that worked, we're done.  */
6109           if (cp_parser_parse_definitely (parser))
6110             return;
6111         }
6112       /* Look for an expression-statement instead.  */
6113       statement = cp_parser_expression_statement (parser, in_statement_expr);
6114     }
6115
6116   /* Set the line number for the statement.  */
6117   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6118     SET_EXPR_LOCATION (statement, statement_location);
6119 }
6120
6121 /* Parse a labeled-statement.
6122
6123    labeled-statement:
6124      identifier : statement
6125      case constant-expression : statement
6126      default : statement
6127
6128    GNU Extension:
6129
6130    labeled-statement:
6131      case constant-expression ... constant-expression : statement
6132
6133    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6134    For an ordinary label, returns a LABEL_EXPR.  */
6135
6136 static tree
6137 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6138 {
6139   cp_token *token;
6140   tree statement = error_mark_node;
6141
6142   /* The next token should be an identifier.  */
6143   token = cp_lexer_peek_token (parser->lexer);
6144   if (token->type != CPP_NAME
6145       && token->type != CPP_KEYWORD)
6146     {
6147       cp_parser_error (parser, "expected labeled-statement");
6148       return error_mark_node;
6149     }
6150
6151   switch (token->keyword)
6152     {
6153     case RID_CASE:
6154       {
6155         tree expr, expr_hi;
6156         cp_token *ellipsis;
6157
6158         /* Consume the `case' token.  */
6159         cp_lexer_consume_token (parser->lexer);
6160         /* Parse the constant-expression.  */
6161         expr = cp_parser_constant_expression (parser,
6162                                               /*allow_non_constant_p=*/false,
6163                                               NULL);
6164
6165         ellipsis = cp_lexer_peek_token (parser->lexer);
6166         if (ellipsis->type == CPP_ELLIPSIS)
6167           {
6168             /* Consume the `...' token.  */
6169             cp_lexer_consume_token (parser->lexer);
6170             expr_hi =
6171               cp_parser_constant_expression (parser,
6172                                              /*allow_non_constant_p=*/false,
6173                                              NULL);
6174             /* We don't need to emit warnings here, as the common code
6175                will do this for us.  */
6176           }
6177         else
6178           expr_hi = NULL_TREE;
6179
6180         if (!parser->in_switch_statement_p)
6181           error ("case label %qE not within a switch statement", expr);
6182         else
6183           statement = finish_case_label (expr, expr_hi);
6184       }
6185       break;
6186
6187     case RID_DEFAULT:
6188       /* Consume the `default' token.  */
6189       cp_lexer_consume_token (parser->lexer);
6190       if (!parser->in_switch_statement_p)
6191         error ("case label not within a switch statement");
6192       else
6193         statement = finish_case_label (NULL_TREE, NULL_TREE);
6194       break;
6195
6196     default:
6197       /* Anything else must be an ordinary label.  */
6198       statement = finish_label_stmt (cp_parser_identifier (parser));
6199       break;
6200     }
6201
6202   /* Require the `:' token.  */
6203   cp_parser_require (parser, CPP_COLON, "`:'");
6204   /* Parse the labeled statement.  */
6205   cp_parser_statement (parser, in_statement_expr);
6206
6207   /* Return the label, in the case of a `case' or `default' label.  */
6208   return statement;
6209 }
6210
6211 /* Parse an expression-statement.
6212
6213    expression-statement:
6214      expression [opt] ;
6215
6216    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6217    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6218    indicates whether this expression-statement is part of an
6219    expression statement.  */
6220
6221 static tree
6222 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6223 {
6224   tree statement = NULL_TREE;
6225
6226   /* If the next token is a ';', then there is no expression
6227      statement.  */
6228   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6229     statement = cp_parser_expression (parser, /*cast_p=*/false);
6230
6231   /* Consume the final `;'.  */
6232   cp_parser_consume_semicolon_at_end_of_statement (parser);
6233
6234   if (in_statement_expr
6235       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6236     /* This is the final expression statement of a statement
6237        expression.  */
6238     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6239   else if (statement)
6240     statement = finish_expr_stmt (statement);
6241   else
6242     finish_stmt ();
6243
6244   return statement;
6245 }
6246
6247 /* Parse a compound-statement.
6248
6249    compound-statement:
6250      { statement-seq [opt] }
6251
6252    Returns a tree representing the statement.  */
6253
6254 static tree
6255 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6256                               bool in_try)
6257 {
6258   tree compound_stmt;
6259
6260   /* Consume the `{'.  */
6261   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6262     return error_mark_node;
6263   /* Begin the compound-statement.  */
6264   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6265   /* Parse an (optional) statement-seq.  */
6266   cp_parser_statement_seq_opt (parser, in_statement_expr);
6267   /* Finish the compound-statement.  */
6268   finish_compound_stmt (compound_stmt);
6269   /* Consume the `}'.  */
6270   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6271
6272   return compound_stmt;
6273 }
6274
6275 /* Parse an (optional) statement-seq.
6276
6277    statement-seq:
6278      statement
6279      statement-seq [opt] statement  */
6280
6281 static void
6282 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6283 {
6284   /* Scan statements until there aren't any more.  */
6285   while (true)
6286     {
6287       /* If we're looking at a `}', then we've run out of statements.  */
6288       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6289           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6290         break;
6291
6292       /* Parse the statement.  */
6293       cp_parser_statement (parser, in_statement_expr);
6294     }
6295 }
6296
6297 /* Parse a selection-statement.
6298
6299    selection-statement:
6300      if ( condition ) statement
6301      if ( condition ) statement else statement
6302      switch ( condition ) statement
6303
6304    Returns the new IF_STMT or SWITCH_STMT.  */
6305
6306 static tree
6307 cp_parser_selection_statement (cp_parser* parser)
6308 {
6309   cp_token *token;
6310   enum rid keyword;
6311
6312   /* Peek at the next token.  */
6313   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6314
6315   /* See what kind of keyword it is.  */
6316   keyword = token->keyword;
6317   switch (keyword)
6318     {
6319     case RID_IF:
6320     case RID_SWITCH:
6321       {
6322         tree statement;
6323         tree condition;
6324
6325         /* Look for the `('.  */
6326         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6327           {
6328             cp_parser_skip_to_end_of_statement (parser);
6329             return error_mark_node;
6330           }
6331
6332         /* Begin the selection-statement.  */
6333         if (keyword == RID_IF)
6334           statement = begin_if_stmt ();
6335         else
6336           statement = begin_switch_stmt ();
6337
6338         /* Parse the condition.  */
6339         condition = cp_parser_condition (parser);
6340         /* Look for the `)'.  */
6341         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6342           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6343                                                  /*consume_paren=*/true);
6344
6345         if (keyword == RID_IF)
6346           {
6347             /* Add the condition.  */
6348             finish_if_stmt_cond (condition, statement);
6349
6350             /* Parse the then-clause.  */
6351             cp_parser_implicitly_scoped_statement (parser);
6352             finish_then_clause (statement);
6353
6354             /* If the next token is `else', parse the else-clause.  */
6355             if (cp_lexer_next_token_is_keyword (parser->lexer,
6356                                                 RID_ELSE))
6357               {
6358                 /* Consume the `else' keyword.  */
6359                 cp_lexer_consume_token (parser->lexer);
6360                 begin_else_clause (statement);
6361                 /* Parse the else-clause.  */
6362                 cp_parser_implicitly_scoped_statement (parser);
6363                 finish_else_clause (statement);
6364               }
6365
6366             /* Now we're all done with the if-statement.  */
6367             finish_if_stmt (statement);
6368           }
6369         else
6370           {
6371             bool in_switch_statement_p;
6372
6373             /* Add the condition.  */
6374             finish_switch_cond (condition, statement);
6375
6376             /* Parse the body of the switch-statement.  */
6377             in_switch_statement_p = parser->in_switch_statement_p;
6378             parser->in_switch_statement_p = true;
6379             cp_parser_implicitly_scoped_statement (parser);
6380             parser->in_switch_statement_p = in_switch_statement_p;
6381
6382             /* Now we're all done with the switch-statement.  */
6383             finish_switch_stmt (statement);
6384           }
6385
6386         return statement;
6387       }
6388       break;
6389
6390     default:
6391       cp_parser_error (parser, "expected selection-statement");
6392       return error_mark_node;
6393     }
6394 }
6395
6396 /* Parse a condition.
6397
6398    condition:
6399      expression
6400      type-specifier-seq declarator = assignment-expression
6401
6402    GNU Extension:
6403
6404    condition:
6405      type-specifier-seq declarator asm-specification [opt]
6406        attributes [opt] = assignment-expression
6407
6408    Returns the expression that should be tested.  */
6409
6410 static tree
6411 cp_parser_condition (cp_parser* parser)
6412 {
6413   cp_decl_specifier_seq type_specifiers;
6414   const char *saved_message;
6415
6416   /* Try the declaration first.  */
6417   cp_parser_parse_tentatively (parser);
6418   /* New types are not allowed in the type-specifier-seq for a
6419      condition.  */
6420   saved_message = parser->type_definition_forbidden_message;
6421   parser->type_definition_forbidden_message
6422     = "types may not be defined in conditions";
6423   /* Parse the type-specifier-seq.  */
6424   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6425                                 &type_specifiers);
6426   /* Restore the saved message.  */
6427   parser->type_definition_forbidden_message = saved_message;
6428   /* If all is well, we might be looking at a declaration.  */
6429   if (!cp_parser_error_occurred (parser))
6430     {
6431       tree decl;
6432       tree asm_specification;
6433       tree attributes;
6434       cp_declarator *declarator;
6435       tree initializer = NULL_TREE;
6436
6437       /* Parse the declarator.  */
6438       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6439                                          /*ctor_dtor_or_conv_p=*/NULL,
6440                                          /*parenthesized_p=*/NULL,
6441                                          /*member_p=*/false);
6442       /* Parse the attributes.  */
6443       attributes = cp_parser_attributes_opt (parser);
6444       /* Parse the asm-specification.  */
6445       asm_specification = cp_parser_asm_specification_opt (parser);
6446       /* If the next token is not an `=', then we might still be
6447          looking at an expression.  For example:
6448
6449            if (A(a).x)
6450
6451          looks like a decl-specifier-seq and a declarator -- but then
6452          there is no `=', so this is an expression.  */
6453       cp_parser_require (parser, CPP_EQ, "`='");
6454       /* If we did see an `=', then we are looking at a declaration
6455          for sure.  */
6456       if (cp_parser_parse_definitely (parser))
6457         {
6458           tree pushed_scope;
6459
6460           /* Create the declaration.  */
6461           decl = start_decl (declarator, &type_specifiers,
6462                              /*initialized_p=*/true,
6463                              attributes, /*prefix_attributes=*/NULL_TREE,
6464                              &pushed_scope);
6465           /* Parse the assignment-expression.  */
6466           initializer = cp_parser_assignment_expression (parser,
6467                                                          /*cast_p=*/false);
6468
6469           /* Process the initializer.  */
6470           cp_finish_decl (decl,
6471                           initializer,
6472                           asm_specification,
6473                           LOOKUP_ONLYCONVERTING);
6474
6475           if (pushed_scope)
6476             pop_scope (pushed_scope);
6477
6478           return convert_from_reference (decl);
6479         }
6480     }
6481   /* If we didn't even get past the declarator successfully, we are
6482      definitely not looking at a declaration.  */
6483   else
6484     cp_parser_abort_tentative_parse (parser);
6485
6486   /* Otherwise, we are looking at an expression.  */
6487   return cp_parser_expression (parser, /*cast_p=*/false);
6488 }
6489
6490 /* Parse an iteration-statement.
6491
6492    iteration-statement:
6493      while ( condition ) statement
6494      do statement while ( expression ) ;
6495      for ( for-init-statement condition [opt] ; expression [opt] )
6496        statement
6497
6498    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6499
6500 static tree
6501 cp_parser_iteration_statement (cp_parser* parser)
6502 {
6503   cp_token *token;
6504   enum rid keyword;
6505   tree statement;
6506   bool in_iteration_statement_p;
6507
6508
6509   /* Peek at the next token.  */
6510   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6511   if (!token)
6512     return error_mark_node;
6513
6514   /* Remember whether or not we are already within an iteration
6515      statement.  */
6516   in_iteration_statement_p = parser->in_iteration_statement_p;
6517
6518   /* See what kind of keyword it is.  */
6519   keyword = token->keyword;
6520   switch (keyword)
6521     {
6522     case RID_WHILE:
6523       {
6524         tree condition;
6525
6526         /* Begin the while-statement.  */
6527         statement = begin_while_stmt ();
6528         /* Look for the `('.  */
6529         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6530         /* Parse the condition.  */
6531         condition = cp_parser_condition (parser);
6532         finish_while_stmt_cond (condition, statement);
6533         /* Look for the `)'.  */
6534         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6535         /* Parse the dependent statement.  */
6536         parser->in_iteration_statement_p = true;
6537         cp_parser_already_scoped_statement (parser);
6538         parser->in_iteration_statement_p = in_iteration_statement_p;
6539         /* We're done with the while-statement.  */
6540         finish_while_stmt (statement);
6541       }
6542       break;
6543
6544     case RID_DO:
6545       {
6546         tree expression;
6547
6548         /* Begin the do-statement.  */
6549         statement = begin_do_stmt ();
6550         /* Parse the body of the do-statement.  */
6551         parser->in_iteration_statement_p = true;
6552         cp_parser_implicitly_scoped_statement (parser);
6553         parser->in_iteration_statement_p = in_iteration_statement_p;
6554         finish_do_body (statement);
6555         /* Look for the `while' keyword.  */
6556         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6557         /* Look for the `('.  */
6558         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6559         /* Parse the expression.  */
6560         expression = cp_parser_expression (parser, /*cast_p=*/false);
6561         /* We're done with the do-statement.  */
6562         finish_do_stmt (expression, statement);
6563         /* Look for the `)'.  */
6564         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6565         /* Look for the `;'.  */
6566         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6567       }
6568       break;
6569
6570     case RID_FOR:
6571       {
6572         tree condition = NULL_TREE;
6573         tree expression = NULL_TREE;
6574
6575         /* Begin the for-statement.  */
6576         statement = begin_for_stmt ();
6577         /* Look for the `('.  */
6578         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6579         /* Parse the initialization.  */
6580         cp_parser_for_init_statement (parser);
6581         finish_for_init_stmt (statement);
6582
6583         /* If there's a condition, process it.  */
6584         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6585           condition = cp_parser_condition (parser);
6586         finish_for_cond (condition, statement);
6587         /* Look for the `;'.  */
6588         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6589
6590         /* If there's an expression, process it.  */
6591         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6592           expression = cp_parser_expression (parser, /*cast_p=*/false);
6593         finish_for_expr (expression, statement);
6594         /* Look for the `)'.  */
6595         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6596
6597         /* Parse the body of the for-statement.  */
6598         parser->in_iteration_statement_p = true;
6599         cp_parser_already_scoped_statement (parser);
6600         parser->in_iteration_statement_p = in_iteration_statement_p;
6601
6602         /* We're done with the for-statement.  */
6603         finish_for_stmt (statement);
6604       }
6605       break;
6606
6607     default:
6608       cp_parser_error (parser, "expected iteration-statement");
6609       statement = error_mark_node;
6610       break;
6611     }
6612
6613   return statement;
6614 }
6615
6616 /* Parse a for-init-statement.
6617
6618    for-init-statement:
6619      expression-statement
6620      simple-declaration  */
6621
6622 static void
6623 cp_parser_for_init_statement (cp_parser* parser)
6624 {
6625   /* If the next token is a `;', then we have an empty
6626      expression-statement.  Grammatically, this is also a
6627      simple-declaration, but an invalid one, because it does not
6628      declare anything.  Therefore, if we did not handle this case
6629      specially, we would issue an error message about an invalid
6630      declaration.  */
6631   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6632     {
6633       /* We're going to speculatively look for a declaration, falling back
6634          to an expression, if necessary.  */
6635       cp_parser_parse_tentatively (parser);
6636       /* Parse the declaration.  */
6637       cp_parser_simple_declaration (parser,
6638                                     /*function_definition_allowed_p=*/false);
6639       /* If the tentative parse failed, then we shall need to look for an
6640          expression-statement.  */
6641       if (cp_parser_parse_definitely (parser))
6642         return;
6643     }
6644
6645   cp_parser_expression_statement (parser, false);
6646 }
6647
6648 /* Parse a jump-statement.
6649
6650    jump-statement:
6651      break ;
6652      continue ;
6653      return expression [opt] ;
6654      goto identifier ;
6655
6656    GNU extension:
6657
6658    jump-statement:
6659      goto * expression ;
6660
6661    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6662
6663 static tree
6664 cp_parser_jump_statement (cp_parser* parser)
6665 {
6666   tree statement = error_mark_node;
6667   cp_token *token;
6668   enum rid keyword;
6669
6670   /* Peek at the next token.  */
6671   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6672   if (!token)
6673     return error_mark_node;
6674
6675   /* See what kind of keyword it is.  */
6676   keyword = token->keyword;
6677   switch (keyword)
6678     {
6679     case RID_BREAK:
6680       if (!parser->in_switch_statement_p
6681           && !parser->in_iteration_statement_p)
6682         {
6683           error ("break statement not within loop or switch");
6684           statement = error_mark_node;
6685         }
6686       else
6687         statement = finish_break_stmt ();
6688       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6689       break;
6690
6691     case RID_CONTINUE:
6692       if (!parser->in_iteration_statement_p)
6693         {
6694           error ("continue statement not within a loop");
6695           statement = error_mark_node;
6696         }
6697       else
6698         statement = finish_continue_stmt ();
6699       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6700       break;
6701
6702     case RID_RETURN:
6703       {
6704         tree expr;
6705
6706         /* If the next token is a `;', then there is no
6707            expression.  */
6708         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6709           expr = cp_parser_expression (parser, /*cast_p=*/false);
6710         else
6711           expr = NULL_TREE;
6712         /* Build the return-statement.  */
6713         statement = finish_return_stmt (expr);
6714         /* Look for the final `;'.  */
6715         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6716       }
6717       break;
6718
6719     case RID_GOTO:
6720       /* Create the goto-statement.  */
6721       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6722         {
6723           /* Issue a warning about this use of a GNU extension.  */
6724           if (pedantic)
6725             pedwarn ("ISO C++ forbids computed gotos");
6726           /* Consume the '*' token.  */
6727           cp_lexer_consume_token (parser->lexer);
6728           /* Parse the dependent expression.  */
6729           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6730         }
6731       else
6732         finish_goto_stmt (cp_parser_identifier (parser));
6733       /* Look for the final `;'.  */
6734       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6735       break;
6736
6737     default:
6738       cp_parser_error (parser, "expected jump-statement");
6739       break;
6740     }
6741
6742   return statement;
6743 }
6744
6745 /* Parse a declaration-statement.
6746
6747    declaration-statement:
6748      block-declaration  */
6749
6750 static void
6751 cp_parser_declaration_statement (cp_parser* parser)
6752 {
6753   void *p;
6754
6755   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6756   p = obstack_alloc (&declarator_obstack, 0);
6757
6758  /* Parse the block-declaration.  */
6759   cp_parser_block_declaration (parser, /*statement_p=*/true);
6760
6761   /* Free any declarators allocated.  */
6762   obstack_free (&declarator_obstack, p);
6763
6764   /* Finish off the statement.  */
6765   finish_stmt ();
6766 }
6767
6768 /* Some dependent statements (like `if (cond) statement'), are
6769    implicitly in their own scope.  In other words, if the statement is
6770    a single statement (as opposed to a compound-statement), it is
6771    none-the-less treated as if it were enclosed in braces.  Any
6772    declarations appearing in the dependent statement are out of scope
6773    after control passes that point.  This function parses a statement,
6774    but ensures that is in its own scope, even if it is not a
6775    compound-statement.
6776
6777    Returns the new statement.  */
6778
6779 static tree
6780 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6781 {
6782   tree statement;
6783
6784   /* If the token is not a `{', then we must take special action.  */
6785   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6786     {
6787       /* Create a compound-statement.  */
6788       statement = begin_compound_stmt (0);
6789       /* Parse the dependent-statement.  */
6790       cp_parser_statement (parser, false);
6791       /* Finish the dummy compound-statement.  */
6792       finish_compound_stmt (statement);
6793     }
6794   /* Otherwise, we simply parse the statement directly.  */
6795   else
6796     statement = cp_parser_compound_statement (parser, NULL, false);
6797
6798   /* Return the statement.  */
6799   return statement;
6800 }
6801
6802 /* For some dependent statements (like `while (cond) statement'), we
6803    have already created a scope.  Therefore, even if the dependent
6804    statement is a compound-statement, we do not want to create another
6805    scope.  */
6806
6807 static void
6808 cp_parser_already_scoped_statement (cp_parser* parser)
6809 {
6810   /* If the token is a `{', then we must take special action.  */
6811   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6812     cp_parser_statement (parser, false);
6813   else
6814     {
6815       /* Avoid calling cp_parser_compound_statement, so that we
6816          don't create a new scope.  Do everything else by hand.  */
6817       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6818       cp_parser_statement_seq_opt (parser, false);
6819       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6820     }
6821 }
6822
6823 /* Declarations [gram.dcl.dcl] */
6824
6825 /* Parse an optional declaration-sequence.
6826
6827    declaration-seq:
6828      declaration
6829      declaration-seq declaration  */
6830
6831 static void
6832 cp_parser_declaration_seq_opt (cp_parser* parser)
6833 {
6834   while (true)
6835     {
6836       cp_token *token;
6837
6838       token = cp_lexer_peek_token (parser->lexer);
6839
6840       if (token->type == CPP_CLOSE_BRACE
6841           || token->type == CPP_EOF)
6842         break;
6843
6844       if (token->type == CPP_SEMICOLON)
6845         {
6846           /* A declaration consisting of a single semicolon is
6847              invalid.  Allow it unless we're being pedantic.  */
6848           cp_lexer_consume_token (parser->lexer);
6849           if (pedantic && !in_system_header)
6850             pedwarn ("extra %<;%>");
6851           continue;
6852         }
6853
6854       /* If we're entering or exiting a region that's implicitly
6855          extern "C", modify the lang context appropriately.  */
6856       if (!parser->implicit_extern_c && token->implicit_extern_c)
6857         {
6858           push_lang_context (lang_name_c);
6859           parser->implicit_extern_c = true;
6860         }
6861       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6862         {
6863           pop_lang_context ();
6864           parser->implicit_extern_c = false;
6865         }
6866
6867       if (token->type == CPP_PRAGMA)
6868         {
6869           /* A top-level declaration can consist solely of a #pragma.
6870              A nested declaration cannot, so this is done here and not
6871              in cp_parser_declaration.  (A #pragma at block scope is
6872              handled in cp_parser_statement.)  */
6873           cp_lexer_handle_pragma (parser->lexer);
6874           continue;
6875         }
6876
6877       /* Parse the declaration itself.  */
6878       cp_parser_declaration (parser);
6879     }
6880 }
6881
6882 /* Parse a declaration.
6883
6884    declaration:
6885      block-declaration
6886      function-definition
6887      template-declaration
6888      explicit-instantiation
6889      explicit-specialization
6890      linkage-specification
6891      namespace-definition
6892
6893    GNU extension:
6894
6895    declaration:
6896       __extension__ declaration */
6897
6898 static void
6899 cp_parser_declaration (cp_parser* parser)
6900 {
6901   cp_token token1;
6902   cp_token token2;
6903   int saved_pedantic;
6904   void *p;
6905
6906   /* Check for the `__extension__' keyword.  */
6907   if (cp_parser_extension_opt (parser, &saved_pedantic))
6908     {
6909       /* Parse the qualified declaration.  */
6910       cp_parser_declaration (parser);
6911       /* Restore the PEDANTIC flag.  */
6912       pedantic = saved_pedantic;
6913
6914       return;
6915     }
6916
6917   /* Try to figure out what kind of declaration is present.  */
6918   token1 = *cp_lexer_peek_token (parser->lexer);
6919
6920   if (token1.type != CPP_EOF)
6921     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6922
6923   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6924   p = obstack_alloc (&declarator_obstack, 0);
6925
6926   /* If the next token is `extern' and the following token is a string
6927      literal, then we have a linkage specification.  */
6928   if (token1.keyword == RID_EXTERN
6929       && cp_parser_is_string_literal (&token2))
6930     cp_parser_linkage_specification (parser);
6931   /* If the next token is `template', then we have either a template
6932      declaration, an explicit instantiation, or an explicit
6933      specialization.  */
6934   else if (token1.keyword == RID_TEMPLATE)
6935     {
6936       /* `template <>' indicates a template specialization.  */
6937       if (token2.type == CPP_LESS
6938           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6939         cp_parser_explicit_specialization (parser);
6940       /* `template <' indicates a template declaration.  */
6941       else if (token2.type == CPP_LESS)
6942         cp_parser_template_declaration (parser, /*member_p=*/false);
6943       /* Anything else must be an explicit instantiation.  */
6944       else
6945         cp_parser_explicit_instantiation (parser);
6946     }
6947   /* If the next token is `export', then we have a template
6948      declaration.  */
6949   else if (token1.keyword == RID_EXPORT)
6950     cp_parser_template_declaration (parser, /*member_p=*/false);
6951   /* If the next token is `extern', 'static' or 'inline' and the one
6952      after that is `template', we have a GNU extended explicit
6953      instantiation directive.  */
6954   else if (cp_parser_allow_gnu_extensions_p (parser)
6955            && (token1.keyword == RID_EXTERN
6956                || token1.keyword == RID_STATIC
6957                || token1.keyword == RID_INLINE)
6958            && token2.keyword == RID_TEMPLATE)
6959     cp_parser_explicit_instantiation (parser);
6960   /* If the next token is `namespace', check for a named or unnamed
6961      namespace definition.  */
6962   else if (token1.keyword == RID_NAMESPACE
6963            && (/* A named namespace definition.  */
6964                (token2.type == CPP_NAME
6965                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6966                     == CPP_OPEN_BRACE))
6967                /* An unnamed namespace definition.  */
6968                || token2.type == CPP_OPEN_BRACE))
6969     cp_parser_namespace_definition (parser);
6970   /* Objective-C++ declaration/definition.  */
6971   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6972     cp_parser_objc_declaration (parser);
6973   /* We must have either a block declaration or a function
6974      definition.  */
6975   else
6976     /* Try to parse a block-declaration, or a function-definition.  */
6977     cp_parser_block_declaration (parser, /*statement_p=*/false);
6978
6979   /* Free any declarators allocated.  */
6980   obstack_free (&declarator_obstack, p);
6981 }
6982
6983 /* Parse a block-declaration.
6984
6985    block-declaration:
6986      simple-declaration
6987      asm-definition
6988      namespace-alias-definition
6989      using-declaration
6990      using-directive
6991
6992    GNU Extension:
6993
6994    block-declaration:
6995      __extension__ block-declaration
6996      label-declaration
6997
6998    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6999    part of a declaration-statement.  */
7000
7001 static void
7002 cp_parser_block_declaration (cp_parser *parser,
7003                              bool      statement_p)
7004 {
7005   cp_token *token1;
7006   int saved_pedantic;
7007
7008   /* Check for the `__extension__' keyword.  */
7009   if (cp_parser_extension_opt (parser, &saved_pedantic))
7010     {
7011       /* Parse the qualified declaration.  */
7012       cp_parser_block_declaration (parser, statement_p);
7013       /* Restore the PEDANTIC flag.  */
7014       pedantic = saved_pedantic;
7015
7016       return;
7017     }
7018
7019   /* Peek at the next token to figure out which kind of declaration is
7020      present.  */
7021   token1 = cp_lexer_peek_token (parser->lexer);
7022
7023   /* If the next keyword is `asm', we have an asm-definition.  */
7024   if (token1->keyword == RID_ASM)
7025     {
7026       if (statement_p)
7027         cp_parser_commit_to_tentative_parse (parser);
7028       cp_parser_asm_definition (parser);
7029     }
7030   /* If the next keyword is `namespace', we have a
7031      namespace-alias-definition.  */
7032   else if (token1->keyword == RID_NAMESPACE)
7033     cp_parser_namespace_alias_definition (parser);
7034   /* If the next keyword is `using', we have either a
7035      using-declaration or a using-directive.  */
7036   else if (token1->keyword == RID_USING)
7037     {
7038       cp_token *token2;
7039
7040       if (statement_p)
7041         cp_parser_commit_to_tentative_parse (parser);
7042       /* If the token after `using' is `namespace', then we have a
7043          using-directive.  */
7044       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7045       if (token2->keyword == RID_NAMESPACE)
7046         cp_parser_using_directive (parser);
7047       /* Otherwise, it's a using-declaration.  */
7048       else
7049         cp_parser_using_declaration (parser);
7050     }
7051   /* If the next keyword is `__label__' we have a label declaration.  */
7052   else if (token1->keyword == RID_LABEL)
7053     {
7054       if (statement_p)
7055         cp_parser_commit_to_tentative_parse (parser);
7056       cp_parser_label_declaration (parser);
7057     }
7058   /* Anything else must be a simple-declaration.  */
7059   else
7060     cp_parser_simple_declaration (parser, !statement_p);
7061 }
7062
7063 /* Parse a simple-declaration.
7064
7065    simple-declaration:
7066      decl-specifier-seq [opt] init-declarator-list [opt] ;
7067
7068    init-declarator-list:
7069      init-declarator
7070      init-declarator-list , init-declarator
7071
7072    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7073    function-definition as a simple-declaration.  */
7074
7075 static void
7076 cp_parser_simple_declaration (cp_parser* parser,
7077                               bool function_definition_allowed_p)
7078 {
7079   cp_decl_specifier_seq decl_specifiers;
7080   int declares_class_or_enum;
7081   bool saw_declarator;
7082
7083   /* Defer access checks until we know what is being declared; the
7084      checks for names appearing in the decl-specifier-seq should be
7085      done as if we were in the scope of the thing being declared.  */
7086   push_deferring_access_checks (dk_deferred);
7087
7088   /* Parse the decl-specifier-seq.  We have to keep track of whether
7089      or not the decl-specifier-seq declares a named class or
7090      enumeration type, since that is the only case in which the
7091      init-declarator-list is allowed to be empty.
7092
7093      [dcl.dcl]
7094
7095      In a simple-declaration, the optional init-declarator-list can be
7096      omitted only when declaring a class or enumeration, that is when
7097      the decl-specifier-seq contains either a class-specifier, an
7098      elaborated-type-specifier, or an enum-specifier.  */
7099   cp_parser_decl_specifier_seq (parser,
7100                                 CP_PARSER_FLAGS_OPTIONAL,
7101                                 &decl_specifiers,
7102                                 &declares_class_or_enum);
7103   /* We no longer need to defer access checks.  */
7104   stop_deferring_access_checks ();
7105
7106   /* In a block scope, a valid declaration must always have a
7107      decl-specifier-seq.  By not trying to parse declarators, we can
7108      resolve the declaration/expression ambiguity more quickly.  */
7109   if (!function_definition_allowed_p
7110       && !decl_specifiers.any_specifiers_p)
7111     {
7112       cp_parser_error (parser, "expected declaration");
7113       goto done;
7114     }
7115
7116   /* If the next two tokens are both identifiers, the code is
7117      erroneous. The usual cause of this situation is code like:
7118
7119        T t;
7120
7121      where "T" should name a type -- but does not.  */
7122   if (!decl_specifiers.type
7123       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7124     {
7125       /* If parsing tentatively, we should commit; we really are
7126          looking at a declaration.  */
7127       cp_parser_commit_to_tentative_parse (parser);
7128       /* Give up.  */
7129       goto done;
7130     }
7131
7132   /* If we have seen at least one decl-specifier, and the next token
7133      is not a parenthesis, then we must be looking at a declaration.
7134      (After "int (" we might be looking at a functional cast.)  */
7135   if (decl_specifiers.any_specifiers_p
7136       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7137     cp_parser_commit_to_tentative_parse (parser);
7138
7139   /* Keep going until we hit the `;' at the end of the simple
7140      declaration.  */
7141   saw_declarator = false;
7142   while (cp_lexer_next_token_is_not (parser->lexer,
7143                                      CPP_SEMICOLON))
7144     {
7145       cp_token *token;
7146       bool function_definition_p;
7147       tree decl;
7148
7149       saw_declarator = true;
7150       /* Parse the init-declarator.  */
7151       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7152                                         function_definition_allowed_p,
7153                                         /*member_p=*/false,
7154                                         declares_class_or_enum,
7155                                         &function_definition_p);
7156       /* If an error occurred while parsing tentatively, exit quickly.
7157          (That usually happens when in the body of a function; each
7158          statement is treated as a declaration-statement until proven
7159          otherwise.)  */
7160       if (cp_parser_error_occurred (parser))
7161         goto done;
7162       /* Handle function definitions specially.  */
7163       if (function_definition_p)
7164         {
7165           /* If the next token is a `,', then we are probably
7166              processing something like:
7167
7168                void f() {}, *p;
7169
7170              which is erroneous.  */
7171           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7172             error ("mixing declarations and function-definitions is forbidden");
7173           /* Otherwise, we're done with the list of declarators.  */
7174           else
7175             {
7176               pop_deferring_access_checks ();
7177               return;
7178             }
7179         }
7180       /* The next token should be either a `,' or a `;'.  */
7181       token = cp_lexer_peek_token (parser->lexer);
7182       /* If it's a `,', there are more declarators to come.  */
7183       if (token->type == CPP_COMMA)
7184         cp_lexer_consume_token (parser->lexer);
7185       /* If it's a `;', we are done.  */
7186       else if (token->type == CPP_SEMICOLON)
7187         break;
7188       /* Anything else is an error.  */
7189       else
7190         {
7191           /* If we have already issued an error message we don't need
7192              to issue another one.  */
7193           if (decl != error_mark_node
7194               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7195             cp_parser_error (parser, "expected %<,%> or %<;%>");
7196           /* Skip tokens until we reach the end of the statement.  */
7197           cp_parser_skip_to_end_of_statement (parser);
7198           /* If the next token is now a `;', consume it.  */
7199           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7200             cp_lexer_consume_token (parser->lexer);
7201           goto done;
7202         }
7203       /* After the first time around, a function-definition is not
7204          allowed -- even if it was OK at first.  For example:
7205
7206            int i, f() {}
7207
7208          is not valid.  */
7209       function_definition_allowed_p = false;
7210     }
7211
7212   /* Issue an error message if no declarators are present, and the
7213      decl-specifier-seq does not itself declare a class or
7214      enumeration.  */
7215   if (!saw_declarator)
7216     {
7217       if (cp_parser_declares_only_class_p (parser))
7218         shadow_tag (&decl_specifiers);
7219       /* Perform any deferred access checks.  */
7220       perform_deferred_access_checks ();
7221     }
7222
7223   /* Consume the `;'.  */
7224   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7225
7226  done:
7227   pop_deferring_access_checks ();
7228 }
7229
7230 /* Parse a decl-specifier-seq.
7231
7232    decl-specifier-seq:
7233      decl-specifier-seq [opt] decl-specifier
7234
7235    decl-specifier:
7236      storage-class-specifier
7237      type-specifier
7238      function-specifier
7239      friend
7240      typedef
7241
7242    GNU Extension:
7243
7244    decl-specifier:
7245      attributes
7246
7247    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7248
7249    The parser flags FLAGS is used to control type-specifier parsing.
7250
7251    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7252    flags:
7253
7254      1: one of the decl-specifiers is an elaborated-type-specifier
7255         (i.e., a type declaration)
7256      2: one of the decl-specifiers is an enum-specifier or a
7257         class-specifier (i.e., a type definition)
7258
7259    */
7260
7261 static void
7262 cp_parser_decl_specifier_seq (cp_parser* parser,
7263                               cp_parser_flags flags,
7264                               cp_decl_specifier_seq *decl_specs,
7265                               int* declares_class_or_enum)
7266 {
7267   bool constructor_possible_p = !parser->in_declarator_p;
7268
7269   /* Clear DECL_SPECS.  */
7270   clear_decl_specs (decl_specs);
7271
7272   /* Assume no class or enumeration type is declared.  */
7273   *declares_class_or_enum = 0;
7274
7275   /* Keep reading specifiers until there are no more to read.  */
7276   while (true)
7277     {
7278       bool constructor_p;
7279       bool found_decl_spec;
7280       cp_token *token;
7281
7282       /* Peek at the next token.  */
7283       token = cp_lexer_peek_token (parser->lexer);
7284       /* Handle attributes.  */
7285       if (token->keyword == RID_ATTRIBUTE)
7286         {
7287           /* Parse the attributes.  */
7288           decl_specs->attributes
7289             = chainon (decl_specs->attributes,
7290                        cp_parser_attributes_opt (parser));
7291           continue;
7292         }
7293       /* Assume we will find a decl-specifier keyword.  */
7294       found_decl_spec = true;
7295       /* If the next token is an appropriate keyword, we can simply
7296          add it to the list.  */
7297       switch (token->keyword)
7298         {
7299           /* decl-specifier:
7300                friend  */
7301         case RID_FRIEND:
7302           if (decl_specs->specs[(int) ds_friend]++)
7303             error ("duplicate %<friend%>");
7304           /* Consume the token.  */
7305           cp_lexer_consume_token (parser->lexer);
7306           break;
7307
7308           /* function-specifier:
7309                inline
7310                virtual
7311                explicit  */
7312         case RID_INLINE:
7313         case RID_VIRTUAL:
7314         case RID_EXPLICIT:
7315           cp_parser_function_specifier_opt (parser, decl_specs);
7316           break;
7317
7318           /* decl-specifier:
7319                typedef  */
7320         case RID_TYPEDEF:
7321           ++decl_specs->specs[(int) ds_typedef];
7322           /* Consume the token.  */
7323           cp_lexer_consume_token (parser->lexer);
7324           /* A constructor declarator cannot appear in a typedef.  */
7325           constructor_possible_p = false;
7326           /* The "typedef" keyword can only occur in a declaration; we
7327              may as well commit at this point.  */
7328           cp_parser_commit_to_tentative_parse (parser);
7329           break;
7330
7331           /* storage-class-specifier:
7332                auto
7333                register
7334                static
7335                extern
7336                mutable
7337
7338              GNU Extension:
7339                thread  */
7340         case RID_AUTO:
7341           /* Consume the token.  */
7342           cp_lexer_consume_token (parser->lexer);
7343           cp_parser_set_storage_class (decl_specs, sc_auto);
7344           break;
7345         case RID_REGISTER:
7346           /* Consume the token.  */
7347           cp_lexer_consume_token (parser->lexer);
7348           cp_parser_set_storage_class (decl_specs, sc_register);
7349           break;
7350         case RID_STATIC:
7351           /* Consume the token.  */
7352           cp_lexer_consume_token (parser->lexer);
7353           if (decl_specs->specs[(int) ds_thread])
7354             {
7355               error ("%<__thread%> before %<static%>");
7356               decl_specs->specs[(int) ds_thread] = 0;
7357             }
7358           cp_parser_set_storage_class (decl_specs, sc_static);
7359           break;
7360         case RID_EXTERN:
7361           /* Consume the token.  */
7362           cp_lexer_consume_token (parser->lexer);
7363           if (decl_specs->specs[(int) ds_thread])
7364             {
7365               error ("%<__thread%> before %<extern%>");
7366               decl_specs->specs[(int) ds_thread] = 0;
7367             }
7368           cp_parser_set_storage_class (decl_specs, sc_extern);
7369           break;
7370         case RID_MUTABLE:
7371           /* Consume the token.  */
7372           cp_lexer_consume_token (parser->lexer);
7373           cp_parser_set_storage_class (decl_specs, sc_mutable);
7374           break;
7375         case RID_THREAD:
7376           /* Consume the token.  */
7377           cp_lexer_consume_token (parser->lexer);
7378           ++decl_specs->specs[(int) ds_thread];
7379           break;
7380
7381         default:
7382           /* We did not yet find a decl-specifier yet.  */
7383           found_decl_spec = false;
7384           break;
7385         }
7386
7387       /* Constructors are a special case.  The `S' in `S()' is not a
7388          decl-specifier; it is the beginning of the declarator.  */
7389       constructor_p
7390         = (!found_decl_spec
7391            && constructor_possible_p
7392            && (cp_parser_constructor_declarator_p
7393                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7394
7395       /* If we don't have a DECL_SPEC yet, then we must be looking at
7396          a type-specifier.  */
7397       if (!found_decl_spec && !constructor_p)
7398         {
7399           int decl_spec_declares_class_or_enum;
7400           bool is_cv_qualifier;
7401           tree type_spec;
7402
7403           type_spec
7404             = cp_parser_type_specifier (parser, flags,
7405                                         decl_specs,
7406                                         /*is_declaration=*/true,
7407                                         &decl_spec_declares_class_or_enum,
7408                                         &is_cv_qualifier);
7409
7410           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7411
7412           /* If this type-specifier referenced a user-defined type
7413              (a typedef, class-name, etc.), then we can't allow any
7414              more such type-specifiers henceforth.
7415
7416              [dcl.spec]
7417
7418              The longest sequence of decl-specifiers that could
7419              possibly be a type name is taken as the
7420              decl-specifier-seq of a declaration.  The sequence shall
7421              be self-consistent as described below.
7422
7423              [dcl.type]
7424
7425              As a general rule, at most one type-specifier is allowed
7426              in the complete decl-specifier-seq of a declaration.  The
7427              only exceptions are the following:
7428
7429              -- const or volatile can be combined with any other
7430                 type-specifier.
7431
7432              -- signed or unsigned can be combined with char, long,
7433                 short, or int.
7434
7435              -- ..
7436
7437              Example:
7438
7439                typedef char* Pc;
7440                void g (const int Pc);
7441
7442              Here, Pc is *not* part of the decl-specifier seq; it's
7443              the declarator.  Therefore, once we see a type-specifier
7444              (other than a cv-qualifier), we forbid any additional
7445              user-defined types.  We *do* still allow things like `int
7446              int' to be considered a decl-specifier-seq, and issue the
7447              error message later.  */
7448           if (type_spec && !is_cv_qualifier)
7449             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7450           /* A constructor declarator cannot follow a type-specifier.  */
7451           if (type_spec)
7452             {
7453               constructor_possible_p = false;
7454               found_decl_spec = true;
7455             }
7456         }
7457
7458       /* If we still do not have a DECL_SPEC, then there are no more
7459          decl-specifiers.  */
7460       if (!found_decl_spec)
7461         break;
7462
7463       decl_specs->any_specifiers_p = true;
7464       /* After we see one decl-specifier, further decl-specifiers are
7465          always optional.  */
7466       flags |= CP_PARSER_FLAGS_OPTIONAL;
7467     }
7468
7469   /* Don't allow a friend specifier with a class definition.  */
7470   if (decl_specs->specs[(int) ds_friend] != 0
7471       && (*declares_class_or_enum & 2))
7472     error ("class definition may not be declared a friend");
7473 }
7474
7475 /* Parse an (optional) storage-class-specifier.
7476
7477    storage-class-specifier:
7478      auto
7479      register
7480      static
7481      extern
7482      mutable
7483
7484    GNU Extension:
7485
7486    storage-class-specifier:
7487      thread
7488
7489    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7490
7491 static tree
7492 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7493 {
7494   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7495     {
7496     case RID_AUTO:
7497     case RID_REGISTER:
7498     case RID_STATIC:
7499     case RID_EXTERN:
7500     case RID_MUTABLE:
7501     case RID_THREAD:
7502       /* Consume the token.  */
7503       return cp_lexer_consume_token (parser->lexer)->value;
7504
7505     default:
7506       return NULL_TREE;
7507     }
7508 }
7509
7510 /* Parse an (optional) function-specifier.
7511
7512    function-specifier:
7513      inline
7514      virtual
7515      explicit
7516
7517    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7518    Updates DECL_SPECS, if it is non-NULL.  */
7519
7520 static tree
7521 cp_parser_function_specifier_opt (cp_parser* parser,
7522                                   cp_decl_specifier_seq *decl_specs)
7523 {
7524   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7525     {
7526     case RID_INLINE:
7527       if (decl_specs)
7528         ++decl_specs->specs[(int) ds_inline];
7529       break;
7530
7531     case RID_VIRTUAL:
7532       if (decl_specs)
7533         ++decl_specs->specs[(int) ds_virtual];
7534       break;
7535
7536     case RID_EXPLICIT:
7537       if (decl_specs)
7538         ++decl_specs->specs[(int) ds_explicit];
7539       break;
7540
7541     default:
7542       return NULL_TREE;
7543     }
7544
7545   /* Consume the token.  */
7546   return cp_lexer_consume_token (parser->lexer)->value;
7547 }
7548
7549 /* Parse a linkage-specification.
7550
7551    linkage-specification:
7552      extern string-literal { declaration-seq [opt] }
7553      extern string-literal declaration  */
7554
7555 static void
7556 cp_parser_linkage_specification (cp_parser* parser)
7557 {
7558   tree linkage;
7559
7560   /* Look for the `extern' keyword.  */
7561   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7562
7563   /* Look for the string-literal.  */
7564   linkage = cp_parser_string_literal (parser, false, false);
7565
7566   /* Transform the literal into an identifier.  If the literal is a
7567      wide-character string, or contains embedded NULs, then we can't
7568      handle it as the user wants.  */
7569   if (strlen (TREE_STRING_POINTER (linkage))
7570       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7571     {
7572       cp_parser_error (parser, "invalid linkage-specification");
7573       /* Assume C++ linkage.  */
7574       linkage = lang_name_cplusplus;
7575     }
7576   else
7577     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7578
7579   /* We're now using the new linkage.  */
7580   push_lang_context (linkage);
7581
7582   /* If the next token is a `{', then we're using the first
7583      production.  */
7584   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7585     {
7586       /* Consume the `{' token.  */
7587       cp_lexer_consume_token (parser->lexer);
7588       /* Parse the declarations.  */
7589       cp_parser_declaration_seq_opt (parser);
7590       /* Look for the closing `}'.  */
7591       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7592     }
7593   /* Otherwise, there's just one declaration.  */
7594   else
7595     {
7596       bool saved_in_unbraced_linkage_specification_p;
7597
7598       saved_in_unbraced_linkage_specification_p
7599         = parser->in_unbraced_linkage_specification_p;
7600       parser->in_unbraced_linkage_specification_p = true;
7601       have_extern_spec = true;
7602       cp_parser_declaration (parser);
7603       have_extern_spec = false;
7604       parser->in_unbraced_linkage_specification_p
7605         = saved_in_unbraced_linkage_specification_p;
7606     }
7607
7608   /* We're done with the linkage-specification.  */
7609   pop_lang_context ();
7610 }
7611
7612 /* Special member functions [gram.special] */
7613
7614 /* Parse a conversion-function-id.
7615
7616    conversion-function-id:
7617      operator conversion-type-id
7618
7619    Returns an IDENTIFIER_NODE representing the operator.  */
7620
7621 static tree
7622 cp_parser_conversion_function_id (cp_parser* parser)
7623 {
7624   tree type;
7625   tree saved_scope;
7626   tree saved_qualifying_scope;
7627   tree saved_object_scope;
7628   tree pushed_scope = NULL_TREE;
7629
7630   /* Look for the `operator' token.  */
7631   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7632     return error_mark_node;
7633   /* When we parse the conversion-type-id, the current scope will be
7634      reset.  However, we need that information in able to look up the
7635      conversion function later, so we save it here.  */
7636   saved_scope = parser->scope;
7637   saved_qualifying_scope = parser->qualifying_scope;
7638   saved_object_scope = parser->object_scope;
7639   /* We must enter the scope of the class so that the names of
7640      entities declared within the class are available in the
7641      conversion-type-id.  For example, consider:
7642
7643        struct S {
7644          typedef int I;
7645          operator I();
7646        };
7647
7648        S::operator I() { ... }
7649
7650      In order to see that `I' is a type-name in the definition, we
7651      must be in the scope of `S'.  */
7652   if (saved_scope)
7653     pushed_scope = push_scope (saved_scope);
7654   /* Parse the conversion-type-id.  */
7655   type = cp_parser_conversion_type_id (parser);
7656   /* Leave the scope of the class, if any.  */
7657   if (pushed_scope)
7658     pop_scope (pushed_scope);
7659   /* Restore the saved scope.  */
7660   parser->scope = saved_scope;
7661   parser->qualifying_scope = saved_qualifying_scope;
7662   parser->object_scope = saved_object_scope;
7663   /* If the TYPE is invalid, indicate failure.  */
7664   if (type == error_mark_node)
7665     return error_mark_node;
7666   return mangle_conv_op_name_for_type (type);
7667 }
7668
7669 /* Parse a conversion-type-id:
7670
7671    conversion-type-id:
7672      type-specifier-seq conversion-declarator [opt]
7673
7674    Returns the TYPE specified.  */
7675
7676 static tree
7677 cp_parser_conversion_type_id (cp_parser* parser)
7678 {
7679   tree attributes;
7680   cp_decl_specifier_seq type_specifiers;
7681   cp_declarator *declarator;
7682   tree type_specified;
7683
7684   /* Parse the attributes.  */
7685   attributes = cp_parser_attributes_opt (parser);
7686   /* Parse the type-specifiers.  */
7687   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7688                                 &type_specifiers);
7689   /* If that didn't work, stop.  */
7690   if (type_specifiers.type == error_mark_node)
7691     return error_mark_node;
7692   /* Parse the conversion-declarator.  */
7693   declarator = cp_parser_conversion_declarator_opt (parser);
7694
7695   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7696                                     /*initialized=*/0, &attributes);
7697   if (attributes)
7698     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7699   return type_specified;
7700 }
7701
7702 /* Parse an (optional) conversion-declarator.
7703
7704    conversion-declarator:
7705      ptr-operator conversion-declarator [opt]
7706
7707    */
7708
7709 static cp_declarator *
7710 cp_parser_conversion_declarator_opt (cp_parser* parser)
7711 {
7712   enum tree_code code;
7713   tree class_type;
7714   cp_cv_quals cv_quals;
7715
7716   /* We don't know if there's a ptr-operator next, or not.  */
7717   cp_parser_parse_tentatively (parser);
7718   /* Try the ptr-operator.  */
7719   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7720   /* If it worked, look for more conversion-declarators.  */
7721   if (cp_parser_parse_definitely (parser))
7722     {
7723       cp_declarator *declarator;
7724
7725       /* Parse another optional declarator.  */
7726       declarator = cp_parser_conversion_declarator_opt (parser);
7727
7728       /* Create the representation of the declarator.  */
7729       if (class_type)
7730         declarator = make_ptrmem_declarator (cv_quals, class_type,
7731                                              declarator);
7732       else if (code == INDIRECT_REF)
7733         declarator = make_pointer_declarator (cv_quals, declarator);
7734       else
7735         declarator = make_reference_declarator (cv_quals, declarator);
7736
7737       return declarator;
7738    }
7739
7740   return NULL;
7741 }
7742
7743 /* Parse an (optional) ctor-initializer.
7744
7745    ctor-initializer:
7746      : mem-initializer-list
7747
7748    Returns TRUE iff the ctor-initializer was actually present.  */
7749
7750 static bool
7751 cp_parser_ctor_initializer_opt (cp_parser* parser)
7752 {
7753   /* If the next token is not a `:', then there is no
7754      ctor-initializer.  */
7755   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7756     {
7757       /* Do default initialization of any bases and members.  */
7758       if (DECL_CONSTRUCTOR_P (current_function_decl))
7759         finish_mem_initializers (NULL_TREE);
7760
7761       return false;
7762     }
7763
7764   /* Consume the `:' token.  */
7765   cp_lexer_consume_token (parser->lexer);
7766   /* And the mem-initializer-list.  */
7767   cp_parser_mem_initializer_list (parser);
7768
7769   return true;
7770 }
7771
7772 /* Parse a mem-initializer-list.
7773
7774    mem-initializer-list:
7775      mem-initializer
7776      mem-initializer , mem-initializer-list  */
7777
7778 static void
7779 cp_parser_mem_initializer_list (cp_parser* parser)
7780 {
7781   tree mem_initializer_list = NULL_TREE;
7782
7783   /* Let the semantic analysis code know that we are starting the
7784      mem-initializer-list.  */
7785   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7786     error ("only constructors take base initializers");
7787
7788   /* Loop through the list.  */
7789   while (true)
7790     {
7791       tree mem_initializer;
7792
7793       /* Parse the mem-initializer.  */
7794       mem_initializer = cp_parser_mem_initializer (parser);
7795       /* Add it to the list, unless it was erroneous.  */
7796       if (mem_initializer)
7797         {
7798           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7799           mem_initializer_list = mem_initializer;
7800         }
7801       /* If the next token is not a `,', we're done.  */
7802       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7803         break;
7804       /* Consume the `,' token.  */
7805       cp_lexer_consume_token (parser->lexer);
7806     }
7807
7808   /* Perform semantic analysis.  */
7809   if (DECL_CONSTRUCTOR_P (current_function_decl))
7810     finish_mem_initializers (mem_initializer_list);
7811 }
7812
7813 /* Parse a mem-initializer.
7814
7815    mem-initializer:
7816      mem-initializer-id ( expression-list [opt] )
7817
7818    GNU extension:
7819
7820    mem-initializer:
7821      ( expression-list [opt] )
7822
7823    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7824    class) or FIELD_DECL (for a non-static data member) to initialize;
7825    the TREE_VALUE is the expression-list.  */
7826
7827 static tree
7828 cp_parser_mem_initializer (cp_parser* parser)
7829 {
7830   tree mem_initializer_id;
7831   tree expression_list;
7832   tree member;
7833
7834   /* Find out what is being initialized.  */
7835   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7836     {
7837       pedwarn ("anachronistic old-style base class initializer");
7838       mem_initializer_id = NULL_TREE;
7839     }
7840   else
7841     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7842   member = expand_member_init (mem_initializer_id);
7843   if (member && !DECL_P (member))
7844     in_base_initializer = 1;
7845
7846   expression_list
7847     = cp_parser_parenthesized_expression_list (parser, false,
7848                                                /*cast_p=*/false,
7849                                                /*non_constant_p=*/NULL);
7850   if (!expression_list)
7851     expression_list = void_type_node;
7852
7853   in_base_initializer = 0;
7854
7855   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7856 }
7857
7858 /* Parse a mem-initializer-id.
7859
7860    mem-initializer-id:
7861      :: [opt] nested-name-specifier [opt] class-name
7862      identifier
7863
7864    Returns a TYPE indicating the class to be initializer for the first
7865    production.  Returns an IDENTIFIER_NODE indicating the data member
7866    to be initialized for the second production.  */
7867
7868 static tree
7869 cp_parser_mem_initializer_id (cp_parser* parser)
7870 {
7871   bool global_scope_p;
7872   bool nested_name_specifier_p;
7873   bool template_p = false;
7874   tree id;
7875
7876   /* `typename' is not allowed in this context ([temp.res]).  */
7877   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7878     {
7879       error ("keyword %<typename%> not allowed in this context (a qualified "
7880              "member initializer is implicitly a type)");
7881       cp_lexer_consume_token (parser->lexer);
7882     }
7883   /* Look for the optional `::' operator.  */
7884   global_scope_p
7885     = (cp_parser_global_scope_opt (parser,
7886                                    /*current_scope_valid_p=*/false)
7887        != NULL_TREE);
7888   /* Look for the optional nested-name-specifier.  The simplest way to
7889      implement:
7890
7891        [temp.res]
7892
7893        The keyword `typename' is not permitted in a base-specifier or
7894        mem-initializer; in these contexts a qualified name that
7895        depends on a template-parameter is implicitly assumed to be a
7896        type name.
7897
7898      is to assume that we have seen the `typename' keyword at this
7899      point.  */
7900   nested_name_specifier_p
7901     = (cp_parser_nested_name_specifier_opt (parser,
7902                                             /*typename_keyword_p=*/true,
7903                                             /*check_dependency_p=*/true,
7904                                             /*type_p=*/true,
7905                                             /*is_declaration=*/true)
7906        != NULL_TREE);
7907   if (nested_name_specifier_p)
7908     template_p = cp_parser_optional_template_keyword (parser);
7909   /* If there is a `::' operator or a nested-name-specifier, then we
7910      are definitely looking for a class-name.  */
7911   if (global_scope_p || nested_name_specifier_p)
7912     return cp_parser_class_name (parser,
7913                                  /*typename_keyword_p=*/true,
7914                                  /*template_keyword_p=*/template_p,
7915                                  none_type,
7916                                  /*check_dependency_p=*/true,
7917                                  /*class_head_p=*/false,
7918                                  /*is_declaration=*/true);
7919   /* Otherwise, we could also be looking for an ordinary identifier.  */
7920   cp_parser_parse_tentatively (parser);
7921   /* Try a class-name.  */
7922   id = cp_parser_class_name (parser,
7923                              /*typename_keyword_p=*/true,
7924                              /*template_keyword_p=*/false,
7925                              none_type,
7926                              /*check_dependency_p=*/true,
7927                              /*class_head_p=*/false,
7928                              /*is_declaration=*/true);
7929   /* If we found one, we're done.  */
7930   if (cp_parser_parse_definitely (parser))
7931     return id;
7932   /* Otherwise, look for an ordinary identifier.  */
7933   return cp_parser_identifier (parser);
7934 }
7935
7936 /* Overloading [gram.over] */
7937
7938 /* Parse an operator-function-id.
7939
7940    operator-function-id:
7941      operator operator
7942
7943    Returns an IDENTIFIER_NODE for the operator which is a
7944    human-readable spelling of the identifier, e.g., `operator +'.  */
7945
7946 static tree
7947 cp_parser_operator_function_id (cp_parser* parser)
7948 {
7949   /* Look for the `operator' keyword.  */
7950   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7951     return error_mark_node;
7952   /* And then the name of the operator itself.  */
7953   return cp_parser_operator (parser);
7954 }
7955
7956 /* Parse an operator.
7957
7958    operator:
7959      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7960      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7961      || ++ -- , ->* -> () []
7962
7963    GNU Extensions:
7964
7965    operator:
7966      <? >? <?= >?=
7967
7968    Returns an IDENTIFIER_NODE for the operator which is a
7969    human-readable spelling of the identifier, e.g., `operator +'.  */
7970
7971 static tree
7972 cp_parser_operator (cp_parser* parser)
7973 {
7974   tree id = NULL_TREE;
7975   cp_token *token;
7976
7977   /* Peek at the next token.  */
7978   token = cp_lexer_peek_token (parser->lexer);
7979   /* Figure out which operator we have.  */
7980   switch (token->type)
7981     {
7982     case CPP_KEYWORD:
7983       {
7984         enum tree_code op;
7985
7986         /* The keyword should be either `new' or `delete'.  */
7987         if (token->keyword == RID_NEW)
7988           op = NEW_EXPR;
7989         else if (token->keyword == RID_DELETE)
7990           op = DELETE_EXPR;
7991         else
7992           break;
7993
7994         /* Consume the `new' or `delete' token.  */
7995         cp_lexer_consume_token (parser->lexer);
7996
7997         /* Peek at the next token.  */
7998         token = cp_lexer_peek_token (parser->lexer);
7999         /* If it's a `[' token then this is the array variant of the
8000            operator.  */
8001         if (token->type == CPP_OPEN_SQUARE)
8002           {
8003             /* Consume the `[' token.  */
8004             cp_lexer_consume_token (parser->lexer);
8005             /* Look for the `]' token.  */
8006             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8007             id = ansi_opname (op == NEW_EXPR
8008                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8009           }
8010         /* Otherwise, we have the non-array variant.  */
8011         else
8012           id = ansi_opname (op);
8013
8014         return id;
8015       }
8016
8017     case CPP_PLUS:
8018       id = ansi_opname (PLUS_EXPR);
8019       break;
8020
8021     case CPP_MINUS:
8022       id = ansi_opname (MINUS_EXPR);
8023       break;
8024
8025     case CPP_MULT:
8026       id = ansi_opname (MULT_EXPR);
8027       break;
8028
8029     case CPP_DIV:
8030       id = ansi_opname (TRUNC_DIV_EXPR);
8031       break;
8032
8033     case CPP_MOD:
8034       id = ansi_opname (TRUNC_MOD_EXPR);
8035       break;
8036
8037     case CPP_XOR:
8038       id = ansi_opname (BIT_XOR_EXPR);
8039       break;
8040
8041     case CPP_AND:
8042       id = ansi_opname (BIT_AND_EXPR);
8043       break;
8044
8045     case CPP_OR:
8046       id = ansi_opname (BIT_IOR_EXPR);
8047       break;
8048
8049     case CPP_COMPL:
8050       id = ansi_opname (BIT_NOT_EXPR);
8051       break;
8052
8053     case CPP_NOT:
8054       id = ansi_opname (TRUTH_NOT_EXPR);
8055       break;
8056
8057     case CPP_EQ:
8058       id = ansi_assopname (NOP_EXPR);
8059       break;
8060
8061     case CPP_LESS:
8062       id = ansi_opname (LT_EXPR);
8063       break;
8064
8065     case CPP_GREATER:
8066       id = ansi_opname (GT_EXPR);
8067       break;
8068
8069     case CPP_PLUS_EQ:
8070       id = ansi_assopname (PLUS_EXPR);
8071       break;
8072
8073     case CPP_MINUS_EQ:
8074       id = ansi_assopname (MINUS_EXPR);
8075       break;
8076
8077     case CPP_MULT_EQ:
8078       id = ansi_assopname (MULT_EXPR);
8079       break;
8080
8081     case CPP_DIV_EQ:
8082       id = ansi_assopname (TRUNC_DIV_EXPR);
8083       break;
8084
8085     case CPP_MOD_EQ:
8086       id = ansi_assopname (TRUNC_MOD_EXPR);
8087       break;
8088
8089     case CPP_XOR_EQ:
8090       id = ansi_assopname (BIT_XOR_EXPR);
8091       break;
8092
8093     case CPP_AND_EQ:
8094       id = ansi_assopname (BIT_AND_EXPR);
8095       break;
8096
8097     case CPP_OR_EQ:
8098       id = ansi_assopname (BIT_IOR_EXPR);
8099       break;
8100
8101     case CPP_LSHIFT:
8102       id = ansi_opname (LSHIFT_EXPR);
8103       break;
8104
8105     case CPP_RSHIFT:
8106       id = ansi_opname (RSHIFT_EXPR);
8107       break;
8108
8109     case CPP_LSHIFT_EQ:
8110       id = ansi_assopname (LSHIFT_EXPR);
8111       break;
8112
8113     case CPP_RSHIFT_EQ:
8114       id = ansi_assopname (RSHIFT_EXPR);
8115       break;
8116
8117     case CPP_EQ_EQ:
8118       id = ansi_opname (EQ_EXPR);
8119       break;
8120
8121     case CPP_NOT_EQ:
8122       id = ansi_opname (NE_EXPR);
8123       break;
8124
8125     case CPP_LESS_EQ:
8126       id = ansi_opname (LE_EXPR);
8127       break;
8128
8129     case CPP_GREATER_EQ:
8130       id = ansi_opname (GE_EXPR);
8131       break;
8132
8133     case CPP_AND_AND:
8134       id = ansi_opname (TRUTH_ANDIF_EXPR);
8135       break;
8136
8137     case CPP_OR_OR:
8138       id = ansi_opname (TRUTH_ORIF_EXPR);
8139       break;
8140
8141     case CPP_PLUS_PLUS:
8142       id = ansi_opname (POSTINCREMENT_EXPR);
8143       break;
8144
8145     case CPP_MINUS_MINUS:
8146       id = ansi_opname (PREDECREMENT_EXPR);
8147       break;
8148
8149     case CPP_COMMA:
8150       id = ansi_opname (COMPOUND_EXPR);
8151       break;
8152
8153     case CPP_DEREF_STAR:
8154       id = ansi_opname (MEMBER_REF);
8155       break;
8156
8157     case CPP_DEREF:
8158       id = ansi_opname (COMPONENT_REF);
8159       break;
8160
8161     case CPP_OPEN_PAREN:
8162       /* Consume the `('.  */
8163       cp_lexer_consume_token (parser->lexer);
8164       /* Look for the matching `)'.  */
8165       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8166       return ansi_opname (CALL_EXPR);
8167
8168     case CPP_OPEN_SQUARE:
8169       /* Consume the `['.  */
8170       cp_lexer_consume_token (parser->lexer);
8171       /* Look for the matching `]'.  */
8172       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8173       return ansi_opname (ARRAY_REF);
8174
8175       /* Extensions.  */
8176     case CPP_MIN:
8177       id = ansi_opname (MIN_EXPR);
8178       cp_parser_warn_min_max ();
8179       break;
8180
8181     case CPP_MAX:
8182       id = ansi_opname (MAX_EXPR);
8183       cp_parser_warn_min_max ();
8184       break;
8185
8186     case CPP_MIN_EQ:
8187       id = ansi_assopname (MIN_EXPR);
8188       cp_parser_warn_min_max ();
8189       break;
8190
8191     case CPP_MAX_EQ:
8192       id = ansi_assopname (MAX_EXPR);
8193       cp_parser_warn_min_max ();
8194       break;
8195
8196     default:
8197       /* Anything else is an error.  */
8198       break;
8199     }
8200
8201   /* If we have selected an identifier, we need to consume the
8202      operator token.  */
8203   if (id)
8204     cp_lexer_consume_token (parser->lexer);
8205   /* Otherwise, no valid operator name was present.  */
8206   else
8207     {
8208       cp_parser_error (parser, "expected operator");
8209       id = error_mark_node;
8210     }
8211
8212   return id;
8213 }
8214
8215 /* Parse a template-declaration.
8216
8217    template-declaration:
8218      export [opt] template < template-parameter-list > declaration
8219
8220    If MEMBER_P is TRUE, this template-declaration occurs within a
8221    class-specifier.
8222
8223    The grammar rule given by the standard isn't correct.  What
8224    is really meant is:
8225
8226    template-declaration:
8227      export [opt] template-parameter-list-seq
8228        decl-specifier-seq [opt] init-declarator [opt] ;
8229      export [opt] template-parameter-list-seq
8230        function-definition
8231
8232    template-parameter-list-seq:
8233      template-parameter-list-seq [opt]
8234      template < template-parameter-list >  */
8235
8236 static void
8237 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8238 {
8239   /* Check for `export'.  */
8240   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8241     {
8242       /* Consume the `export' token.  */
8243       cp_lexer_consume_token (parser->lexer);
8244       /* Warn that we do not support `export'.  */
8245       warning (0, "keyword %<export%> not implemented, and will be ignored");
8246     }
8247
8248   cp_parser_template_declaration_after_export (parser, member_p);
8249 }
8250
8251 /* Parse a template-parameter-list.
8252
8253    template-parameter-list:
8254      template-parameter
8255      template-parameter-list , template-parameter
8256
8257    Returns a TREE_LIST.  Each node represents a template parameter.
8258    The nodes are connected via their TREE_CHAINs.  */
8259
8260 static tree
8261 cp_parser_template_parameter_list (cp_parser* parser)
8262 {
8263   tree parameter_list = NULL_TREE;
8264
8265   while (true)
8266     {
8267       tree parameter;
8268       cp_token *token;
8269       bool is_non_type;
8270
8271       /* Parse the template-parameter.  */
8272       parameter = cp_parser_template_parameter (parser, &is_non_type);
8273       /* Add it to the list.  */
8274       if (parameter != error_mark_node)
8275         parameter_list = process_template_parm (parameter_list,
8276                                                 parameter,
8277                                                 is_non_type);
8278       /* Peek at the next token.  */
8279       token = cp_lexer_peek_token (parser->lexer);
8280       /* If it's not a `,', we're done.  */
8281       if (token->type != CPP_COMMA)
8282         break;
8283       /* Otherwise, consume the `,' token.  */
8284       cp_lexer_consume_token (parser->lexer);
8285     }
8286
8287   return parameter_list;
8288 }
8289
8290 /* Parse a template-parameter.
8291
8292    template-parameter:
8293      type-parameter
8294      parameter-declaration
8295
8296    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8297    the parameter.  The TREE_PURPOSE is the default value, if any.
8298    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8299    iff this parameter is a non-type parameter.  */
8300
8301 static tree
8302 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8303 {
8304   cp_token *token;
8305   cp_parameter_declarator *parameter_declarator;
8306   tree parm;
8307
8308   /* Assume it is a type parameter or a template parameter.  */
8309   *is_non_type = false;
8310   /* Peek at the next token.  */
8311   token = cp_lexer_peek_token (parser->lexer);
8312   /* If it is `class' or `template', we have a type-parameter.  */
8313   if (token->keyword == RID_TEMPLATE)
8314     return cp_parser_type_parameter (parser);
8315   /* If it is `class' or `typename' we do not know yet whether it is a
8316      type parameter or a non-type parameter.  Consider:
8317
8318        template <typename T, typename T::X X> ...
8319
8320      or:
8321
8322        template <class C, class D*> ...
8323
8324      Here, the first parameter is a type parameter, and the second is
8325      a non-type parameter.  We can tell by looking at the token after
8326      the identifier -- if it is a `,', `=', or `>' then we have a type
8327      parameter.  */
8328   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8329     {
8330       /* Peek at the token after `class' or `typename'.  */
8331       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8332       /* If it's an identifier, skip it.  */
8333       if (token->type == CPP_NAME)
8334         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8335       /* Now, see if the token looks like the end of a template
8336          parameter.  */
8337       if (token->type == CPP_COMMA
8338           || token->type == CPP_EQ
8339           || token->type == CPP_GREATER)
8340         return cp_parser_type_parameter (parser);
8341     }
8342
8343   /* Otherwise, it is a non-type parameter.
8344
8345      [temp.param]
8346
8347      When parsing a default template-argument for a non-type
8348      template-parameter, the first non-nested `>' is taken as the end
8349      of the template parameter-list rather than a greater-than
8350      operator.  */
8351   *is_non_type = true;
8352   parameter_declarator
8353      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8354                                         /*parenthesized_p=*/NULL);
8355   parm = grokdeclarator (parameter_declarator->declarator,
8356                          &parameter_declarator->decl_specifiers,
8357                          PARM, /*initialized=*/0,
8358                          /*attrlist=*/NULL);
8359   if (parm == error_mark_node)
8360     return error_mark_node;
8361   return build_tree_list (parameter_declarator->default_argument, parm);
8362 }
8363
8364 /* Parse a type-parameter.
8365
8366    type-parameter:
8367      class identifier [opt]
8368      class identifier [opt] = type-id
8369      typename identifier [opt]
8370      typename identifier [opt] = type-id
8371      template < template-parameter-list > class identifier [opt]
8372      template < template-parameter-list > class identifier [opt]
8373        = id-expression
8374
8375    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8376    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8377    the declaration of the parameter.  */
8378
8379 static tree
8380 cp_parser_type_parameter (cp_parser* parser)
8381 {
8382   cp_token *token;
8383   tree parameter;
8384
8385   /* Look for a keyword to tell us what kind of parameter this is.  */
8386   token = cp_parser_require (parser, CPP_KEYWORD,
8387                              "`class', `typename', or `template'");
8388   if (!token)
8389     return error_mark_node;
8390
8391   switch (token->keyword)
8392     {
8393     case RID_CLASS:
8394     case RID_TYPENAME:
8395       {
8396         tree identifier;
8397         tree default_argument;
8398
8399         /* If the next token is an identifier, then it names the
8400            parameter.  */
8401         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8402           identifier = cp_parser_identifier (parser);
8403         else
8404           identifier = NULL_TREE;
8405
8406         /* Create the parameter.  */
8407         parameter = finish_template_type_parm (class_type_node, identifier);
8408
8409         /* If the next token is an `=', we have a default argument.  */
8410         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8411           {
8412             /* Consume the `=' token.  */
8413             cp_lexer_consume_token (parser->lexer);
8414             /* Parse the default-argument.  */
8415             default_argument = cp_parser_type_id (parser);
8416           }
8417         else
8418           default_argument = NULL_TREE;
8419
8420         /* Create the combined representation of the parameter and the
8421            default argument.  */
8422         parameter = build_tree_list (default_argument, parameter);
8423       }
8424       break;
8425
8426     case RID_TEMPLATE:
8427       {
8428         tree parameter_list;
8429         tree identifier;
8430         tree default_argument;
8431
8432         /* Look for the `<'.  */
8433         cp_parser_require (parser, CPP_LESS, "`<'");
8434         /* Parse the template-parameter-list.  */
8435         begin_template_parm_list ();
8436         parameter_list
8437           = cp_parser_template_parameter_list (parser);
8438         parameter_list = end_template_parm_list (parameter_list);
8439         /* Look for the `>'.  */
8440         cp_parser_require (parser, CPP_GREATER, "`>'");
8441         /* Look for the `class' keyword.  */
8442         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8443         /* If the next token is an `=', then there is a
8444            default-argument.  If the next token is a `>', we are at
8445            the end of the parameter-list.  If the next token is a `,',
8446            then we are at the end of this parameter.  */
8447         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8448             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8449             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8450           {
8451             identifier = cp_parser_identifier (parser);
8452             /* Treat invalid names as if the parameter were nameless.  */
8453             if (identifier == error_mark_node)
8454               identifier = NULL_TREE;
8455           }
8456         else
8457           identifier = NULL_TREE;
8458
8459         /* Create the template parameter.  */
8460         parameter = finish_template_template_parm (class_type_node,
8461                                                    identifier);
8462
8463         /* If the next token is an `=', then there is a
8464            default-argument.  */
8465         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8466           {
8467             bool is_template;
8468
8469             /* Consume the `='.  */
8470             cp_lexer_consume_token (parser->lexer);
8471             /* Parse the id-expression.  */
8472             default_argument
8473               = cp_parser_id_expression (parser,
8474                                          /*template_keyword_p=*/false,
8475                                          /*check_dependency_p=*/true,
8476                                          /*template_p=*/&is_template,
8477                                          /*declarator_p=*/false);
8478             if (TREE_CODE (default_argument) == TYPE_DECL)
8479               /* If the id-expression was a template-id that refers to
8480                  a template-class, we already have the declaration here,
8481                  so no further lookup is needed.  */
8482                  ;
8483             else
8484               /* Look up the name.  */
8485               default_argument
8486                 = cp_parser_lookup_name (parser, default_argument,
8487                                          none_type,
8488                                          /*is_template=*/is_template,
8489                                          /*is_namespace=*/false,
8490                                          /*check_dependency=*/true,
8491                                          /*ambiguous_p=*/NULL);
8492             /* See if the default argument is valid.  */
8493             default_argument
8494               = check_template_template_default_arg (default_argument);
8495           }
8496         else
8497           default_argument = NULL_TREE;
8498
8499         /* Create the combined representation of the parameter and the
8500            default argument.  */
8501         parameter = build_tree_list (default_argument, parameter);
8502       }
8503       break;
8504
8505     default:
8506       gcc_unreachable ();
8507       break;
8508     }
8509
8510   return parameter;
8511 }
8512
8513 /* Parse a template-id.
8514
8515    template-id:
8516      template-name < template-argument-list [opt] >
8517
8518    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8519    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8520    returned.  Otherwise, if the template-name names a function, or set
8521    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8522    names a class, returns a TYPE_DECL for the specialization.
8523
8524    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8525    uninstantiated templates.  */
8526
8527 static tree
8528 cp_parser_template_id (cp_parser *parser,
8529                        bool template_keyword_p,
8530                        bool check_dependency_p,
8531                        bool is_declaration)
8532 {
8533   tree template;
8534   tree arguments;
8535   tree template_id;
8536   cp_token_position start_of_id = 0;
8537   tree access_check = NULL_TREE;
8538   cp_token *next_token, *next_token_2;
8539   bool is_identifier;
8540
8541   /* If the next token corresponds to a template-id, there is no need
8542      to reparse it.  */
8543   next_token = cp_lexer_peek_token (parser->lexer);
8544   if (next_token->type == CPP_TEMPLATE_ID)
8545     {
8546       tree value;
8547       tree check;
8548
8549       /* Get the stored value.  */
8550       value = cp_lexer_consume_token (parser->lexer)->value;
8551       /* Perform any access checks that were deferred.  */
8552       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8553         perform_or_defer_access_check (TREE_PURPOSE (check),
8554                                        TREE_VALUE (check));
8555       /* Return the stored value.  */
8556       return TREE_VALUE (value);
8557     }
8558
8559   /* Avoid performing name lookup if there is no possibility of
8560      finding a template-id.  */
8561   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8562       || (next_token->type == CPP_NAME
8563           && !cp_parser_nth_token_starts_template_argument_list_p
8564                (parser, 2)))
8565     {
8566       cp_parser_error (parser, "expected template-id");
8567       return error_mark_node;
8568     }
8569
8570   /* Remember where the template-id starts.  */
8571   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8572     start_of_id = cp_lexer_token_position (parser->lexer, false);
8573
8574   push_deferring_access_checks (dk_deferred);
8575
8576   /* Parse the template-name.  */
8577   is_identifier = false;
8578   template = cp_parser_template_name (parser, template_keyword_p,
8579                                       check_dependency_p,
8580                                       is_declaration,
8581                                       &is_identifier);
8582   if (template == error_mark_node || is_identifier)
8583     {
8584       pop_deferring_access_checks ();
8585       return template;
8586     }
8587
8588   /* If we find the sequence `[:' after a template-name, it's probably
8589      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8590      parse correctly the argument list.  */
8591   next_token = cp_lexer_peek_token (parser->lexer);
8592   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8593   if (next_token->type == CPP_OPEN_SQUARE
8594       && next_token->flags & DIGRAPH
8595       && next_token_2->type == CPP_COLON
8596       && !(next_token_2->flags & PREV_WHITE))
8597     {
8598       cp_parser_parse_tentatively (parser);
8599       /* Change `:' into `::'.  */
8600       next_token_2->type = CPP_SCOPE;
8601       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8602          CPP_LESS.  */
8603       cp_lexer_consume_token (parser->lexer);
8604       /* Parse the arguments.  */
8605       arguments = cp_parser_enclosed_template_argument_list (parser);
8606       if (!cp_parser_parse_definitely (parser))
8607         {
8608           /* If we couldn't parse an argument list, then we revert our changes
8609              and return simply an error. Maybe this is not a template-id
8610              after all.  */
8611           next_token_2->type = CPP_COLON;
8612           cp_parser_error (parser, "expected %<<%>");
8613           pop_deferring_access_checks ();
8614           return error_mark_node;
8615         }
8616       /* Otherwise, emit an error about the invalid digraph, but continue
8617          parsing because we got our argument list.  */
8618       pedwarn ("%<<::%> cannot begin a template-argument list");
8619       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8620               "between %<<%> and %<::%>");
8621       if (!flag_permissive)
8622         {
8623           static bool hint;
8624           if (!hint)
8625             {
8626               inform ("(if you use -fpermissive G++ will accept your code)");
8627               hint = true;
8628             }
8629         }
8630     }
8631   else
8632     {
8633       /* Look for the `<' that starts the template-argument-list.  */
8634       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8635         {
8636           pop_deferring_access_checks ();
8637           return error_mark_node;
8638         }
8639       /* Parse the arguments.  */
8640       arguments = cp_parser_enclosed_template_argument_list (parser);
8641     }
8642
8643   /* Build a representation of the specialization.  */
8644   if (TREE_CODE (template) == IDENTIFIER_NODE)
8645     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8646   else if (DECL_CLASS_TEMPLATE_P (template)
8647            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8648     template_id
8649       = finish_template_type (template, arguments,
8650                               cp_lexer_next_token_is (parser->lexer,
8651                                                       CPP_SCOPE));
8652   else
8653     {
8654       /* If it's not a class-template or a template-template, it should be
8655          a function-template.  */
8656       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8657                    || TREE_CODE (template) == OVERLOAD
8658                    || BASELINK_P (template)));
8659
8660       template_id = lookup_template_function (template, arguments);
8661     }
8662
8663   /* Retrieve any deferred checks.  Do not pop this access checks yet
8664      so the memory will not be reclaimed during token replacing below.  */
8665   access_check = get_deferred_access_checks ();
8666
8667   /* If parsing tentatively, replace the sequence of tokens that makes
8668      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8669      should we re-parse the token stream, we will not have to repeat
8670      the effort required to do the parse, nor will we issue duplicate
8671      error messages about problems during instantiation of the
8672      template.  */
8673   if (start_of_id)
8674     {
8675       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8676
8677       /* Reset the contents of the START_OF_ID token.  */
8678       token->type = CPP_TEMPLATE_ID;
8679       token->value = build_tree_list (access_check, template_id);
8680       token->keyword = RID_MAX;
8681
8682       /* Purge all subsequent tokens.  */
8683       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8684
8685       /* ??? Can we actually assume that, if template_id ==
8686          error_mark_node, we will have issued a diagnostic to the
8687          user, as opposed to simply marking the tentative parse as
8688          failed?  */
8689       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8690         error ("parse error in template argument list");
8691     }
8692
8693   pop_deferring_access_checks ();
8694   return template_id;
8695 }
8696
8697 /* Parse a template-name.
8698
8699    template-name:
8700      identifier
8701
8702    The standard should actually say:
8703
8704    template-name:
8705      identifier
8706      operator-function-id
8707
8708    A defect report has been filed about this issue.
8709
8710    A conversion-function-id cannot be a template name because they cannot
8711    be part of a template-id. In fact, looking at this code:
8712
8713    a.operator K<int>()
8714
8715    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8716    It is impossible to call a templated conversion-function-id with an
8717    explicit argument list, since the only allowed template parameter is
8718    the type to which it is converting.
8719
8720    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8721    `template' keyword, in a construction like:
8722
8723      T::template f<3>()
8724
8725    In that case `f' is taken to be a template-name, even though there
8726    is no way of knowing for sure.
8727
8728    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8729    name refers to a set of overloaded functions, at least one of which
8730    is a template, or an IDENTIFIER_NODE with the name of the template,
8731    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8732    names are looked up inside uninstantiated templates.  */
8733
8734 static tree
8735 cp_parser_template_name (cp_parser* parser,
8736                          bool template_keyword_p,
8737                          bool check_dependency_p,
8738                          bool is_declaration,
8739                          bool *is_identifier)
8740 {
8741   tree identifier;
8742   tree decl;
8743   tree fns;
8744
8745   /* If the next token is `operator', then we have either an
8746      operator-function-id or a conversion-function-id.  */
8747   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8748     {
8749       /* We don't know whether we're looking at an
8750          operator-function-id or a conversion-function-id.  */
8751       cp_parser_parse_tentatively (parser);
8752       /* Try an operator-function-id.  */
8753       identifier = cp_parser_operator_function_id (parser);
8754       /* If that didn't work, try a conversion-function-id.  */
8755       if (!cp_parser_parse_definitely (parser))
8756         {
8757           cp_parser_error (parser, "expected template-name");
8758           return error_mark_node;
8759         }
8760     }
8761   /* Look for the identifier.  */
8762   else
8763     identifier = cp_parser_identifier (parser);
8764
8765   /* If we didn't find an identifier, we don't have a template-id.  */
8766   if (identifier == error_mark_node)
8767     return error_mark_node;
8768
8769   /* If the name immediately followed the `template' keyword, then it
8770      is a template-name.  However, if the next token is not `<', then
8771      we do not treat it as a template-name, since it is not being used
8772      as part of a template-id.  This enables us to handle constructs
8773      like:
8774
8775        template <typename T> struct S { S(); };
8776        template <typename T> S<T>::S();
8777
8778      correctly.  We would treat `S' as a template -- if it were `S<T>'
8779      -- but we do not if there is no `<'.  */
8780
8781   if (processing_template_decl
8782       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8783     {
8784       /* In a declaration, in a dependent context, we pretend that the
8785          "template" keyword was present in order to improve error
8786          recovery.  For example, given:
8787
8788            template <typename T> void f(T::X<int>);
8789
8790          we want to treat "X<int>" as a template-id.  */
8791       if (is_declaration
8792           && !template_keyword_p
8793           && parser->scope && TYPE_P (parser->scope)
8794           && check_dependency_p
8795           && dependent_type_p (parser->scope)
8796           /* Do not do this for dtors (or ctors), since they never
8797              need the template keyword before their name.  */
8798           && !constructor_name_p (identifier, parser->scope))
8799         {
8800           cp_token_position start = 0;
8801
8802           /* Explain what went wrong.  */
8803           error ("non-template %qD used as template", identifier);
8804           inform ("use %<%T::template %D%> to indicate that it is a template",
8805                   parser->scope, identifier);
8806           /* If parsing tentatively, find the location of the "<" token.  */
8807           if (cp_parser_simulate_error (parser))
8808             start = cp_lexer_token_position (parser->lexer, true);
8809           /* Parse the template arguments so that we can issue error
8810              messages about them.  */
8811           cp_lexer_consume_token (parser->lexer);
8812           cp_parser_enclosed_template_argument_list (parser);
8813           /* Skip tokens until we find a good place from which to
8814              continue parsing.  */
8815           cp_parser_skip_to_closing_parenthesis (parser,
8816                                                  /*recovering=*/true,
8817                                                  /*or_comma=*/true,
8818                                                  /*consume_paren=*/false);
8819           /* If parsing tentatively, permanently remove the
8820              template argument list.  That will prevent duplicate
8821              error messages from being issued about the missing
8822              "template" keyword.  */
8823           if (start)
8824             cp_lexer_purge_tokens_after (parser->lexer, start);
8825           if (is_identifier)
8826             *is_identifier = true;
8827           return identifier;
8828         }
8829
8830       /* If the "template" keyword is present, then there is generally
8831          no point in doing name-lookup, so we just return IDENTIFIER.
8832          But, if the qualifying scope is non-dependent then we can
8833          (and must) do name-lookup normally.  */
8834       if (template_keyword_p
8835           && (!parser->scope
8836               || (TYPE_P (parser->scope)
8837                   && dependent_type_p (parser->scope))))
8838         return identifier;
8839     }
8840
8841   /* Look up the name.  */
8842   decl = cp_parser_lookup_name (parser, identifier,
8843                                 none_type,
8844                                 /*is_template=*/false,
8845                                 /*is_namespace=*/false,
8846                                 check_dependency_p,
8847                                 /*ambiguous_p=*/NULL);
8848   decl = maybe_get_template_decl_from_type_decl (decl);
8849
8850   /* If DECL is a template, then the name was a template-name.  */
8851   if (TREE_CODE (decl) == TEMPLATE_DECL)
8852     ;
8853   else
8854     {
8855       tree fn = NULL_TREE;
8856
8857       /* The standard does not explicitly indicate whether a name that
8858          names a set of overloaded declarations, some of which are
8859          templates, is a template-name.  However, such a name should
8860          be a template-name; otherwise, there is no way to form a
8861          template-id for the overloaded templates.  */
8862       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8863       if (TREE_CODE (fns) == OVERLOAD)
8864         for (fn = fns; fn; fn = OVL_NEXT (fn))
8865           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8866             break;
8867
8868       if (!fn)
8869         {
8870           /* The name does not name a template.  */
8871           cp_parser_error (parser, "expected template-name");
8872           return error_mark_node;
8873         }
8874     }
8875
8876   /* If DECL is dependent, and refers to a function, then just return
8877      its name; we will look it up again during template instantiation.  */
8878   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8879     {
8880       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8881       if (TYPE_P (scope) && dependent_type_p (scope))
8882         return identifier;
8883     }
8884
8885   return decl;
8886 }
8887
8888 /* Parse a template-argument-list.
8889
8890    template-argument-list:
8891      template-argument
8892      template-argument-list , template-argument
8893
8894    Returns a TREE_VEC containing the arguments.  */
8895
8896 static tree
8897 cp_parser_template_argument_list (cp_parser* parser)
8898 {
8899   tree fixed_args[10];
8900   unsigned n_args = 0;
8901   unsigned alloced = 10;
8902   tree *arg_ary = fixed_args;
8903   tree vec;
8904   bool saved_in_template_argument_list_p;
8905
8906   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8907   parser->in_template_argument_list_p = true;
8908   do
8909     {
8910       tree argument;
8911
8912       if (n_args)
8913         /* Consume the comma.  */
8914         cp_lexer_consume_token (parser->lexer);
8915
8916       /* Parse the template-argument.  */
8917       argument = cp_parser_template_argument (parser);
8918       if (n_args == alloced)
8919         {
8920           alloced *= 2;
8921
8922           if (arg_ary == fixed_args)
8923             {
8924               arg_ary = xmalloc (sizeof (tree) * alloced);
8925               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8926             }
8927           else
8928             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8929         }
8930       arg_ary[n_args++] = argument;
8931     }
8932   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8933
8934   vec = make_tree_vec (n_args);
8935
8936   while (n_args--)
8937     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8938
8939   if (arg_ary != fixed_args)
8940     free (arg_ary);
8941   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8942   return vec;
8943 }
8944
8945 /* Parse a template-argument.
8946
8947    template-argument:
8948      assignment-expression
8949      type-id
8950      id-expression
8951
8952    The representation is that of an assignment-expression, type-id, or
8953    id-expression -- except that the qualified id-expression is
8954    evaluated, so that the value returned is either a DECL or an
8955    OVERLOAD.
8956
8957    Although the standard says "assignment-expression", it forbids
8958    throw-expressions or assignments in the template argument.
8959    Therefore, we use "conditional-expression" instead.  */
8960
8961 static tree
8962 cp_parser_template_argument (cp_parser* parser)
8963 {
8964   tree argument;
8965   bool template_p;
8966   bool address_p;
8967   bool maybe_type_id = false;
8968   cp_token *token;
8969   cp_id_kind idk;
8970   tree qualifying_class;
8971
8972   /* There's really no way to know what we're looking at, so we just
8973      try each alternative in order.
8974
8975        [temp.arg]
8976
8977        In a template-argument, an ambiguity between a type-id and an
8978        expression is resolved to a type-id, regardless of the form of
8979        the corresponding template-parameter.
8980
8981      Therefore, we try a type-id first.  */
8982   cp_parser_parse_tentatively (parser);
8983   argument = cp_parser_type_id (parser);
8984   /* If there was no error parsing the type-id but the next token is a '>>',
8985      we probably found a typo for '> >'. But there are type-id which are
8986      also valid expressions. For instance:
8987
8988      struct X { int operator >> (int); };
8989      template <int V> struct Foo {};
8990      Foo<X () >> 5> r;
8991
8992      Here 'X()' is a valid type-id of a function type, but the user just
8993      wanted to write the expression "X() >> 5". Thus, we remember that we
8994      found a valid type-id, but we still try to parse the argument as an
8995      expression to see what happens.  */
8996   if (!cp_parser_error_occurred (parser)
8997       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8998     {
8999       maybe_type_id = true;
9000       cp_parser_abort_tentative_parse (parser);
9001     }
9002   else
9003     {
9004       /* If the next token isn't a `,' or a `>', then this argument wasn't
9005       really finished. This means that the argument is not a valid
9006       type-id.  */
9007       if (!cp_parser_next_token_ends_template_argument_p (parser))
9008         cp_parser_error (parser, "expected template-argument");
9009       /* If that worked, we're done.  */
9010       if (cp_parser_parse_definitely (parser))
9011         return argument;
9012     }
9013   /* We're still not sure what the argument will be.  */
9014   cp_parser_parse_tentatively (parser);
9015   /* Try a template.  */
9016   argument = cp_parser_id_expression (parser,
9017                                       /*template_keyword_p=*/false,
9018                                       /*check_dependency_p=*/true,
9019                                       &template_p,
9020                                       /*declarator_p=*/false);
9021   /* If the next token isn't a `,' or a `>', then this argument wasn't
9022      really finished.  */
9023   if (!cp_parser_next_token_ends_template_argument_p (parser))
9024     cp_parser_error (parser, "expected template-argument");
9025   if (!cp_parser_error_occurred (parser))
9026     {
9027       /* Figure out what is being referred to.  If the id-expression
9028          was for a class template specialization, then we will have a
9029          TYPE_DECL at this point.  There is no need to do name lookup
9030          at this point in that case.  */
9031       if (TREE_CODE (argument) != TYPE_DECL)
9032         argument = cp_parser_lookup_name (parser, argument,
9033                                           none_type,
9034                                           /*is_template=*/template_p,
9035                                           /*is_namespace=*/false,
9036                                           /*check_dependency=*/true,
9037                                           /*ambiguous_p=*/NULL);
9038       if (TREE_CODE (argument) != TEMPLATE_DECL
9039           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9040         cp_parser_error (parser, "expected template-name");
9041     }
9042   if (cp_parser_parse_definitely (parser))
9043     return argument;
9044   /* It must be a non-type argument.  There permitted cases are given
9045      in [temp.arg.nontype]:
9046
9047      -- an integral constant-expression of integral or enumeration
9048         type; or
9049
9050      -- the name of a non-type template-parameter; or
9051
9052      -- the name of an object or function with external linkage...
9053
9054      -- the address of an object or function with external linkage...
9055
9056      -- a pointer to member...  */
9057   /* Look for a non-type template parameter.  */
9058   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9059     {
9060       cp_parser_parse_tentatively (parser);
9061       argument = cp_parser_primary_expression (parser,
9062                                                /*cast_p=*/false,
9063                                                &idk,
9064                                                &qualifying_class);
9065       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9066           || !cp_parser_next_token_ends_template_argument_p (parser))
9067         cp_parser_simulate_error (parser);
9068       if (cp_parser_parse_definitely (parser))
9069         return argument;
9070     }
9071
9072   /* If the next token is "&", the argument must be the address of an
9073      object or function with external linkage.  */
9074   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9075   if (address_p)
9076     cp_lexer_consume_token (parser->lexer);
9077   /* See if we might have an id-expression.  */
9078   token = cp_lexer_peek_token (parser->lexer);
9079   if (token->type == CPP_NAME
9080       || token->keyword == RID_OPERATOR
9081       || token->type == CPP_SCOPE
9082       || token->type == CPP_TEMPLATE_ID
9083       || token->type == CPP_NESTED_NAME_SPECIFIER)
9084     {
9085       cp_parser_parse_tentatively (parser);
9086       argument = cp_parser_primary_expression (parser,
9087                                                /*cast_p=*/false,
9088                                                &idk,
9089                                                &qualifying_class);
9090       if (cp_parser_error_occurred (parser)
9091           || !cp_parser_next_token_ends_template_argument_p (parser))
9092         cp_parser_abort_tentative_parse (parser);
9093       else
9094         {
9095           if (TREE_CODE (argument) == INDIRECT_REF)
9096             {
9097               gcc_assert (REFERENCE_REF_P (argument));
9098               argument = TREE_OPERAND (argument, 0);
9099             }
9100
9101           if (qualifying_class)
9102             argument = finish_qualified_id_expr (qualifying_class,
9103                                                  argument,
9104                                                  /*done=*/true,
9105                                                  address_p);
9106           if (TREE_CODE (argument) == VAR_DECL)
9107             {
9108               /* A variable without external linkage might still be a
9109                  valid constant-expression, so no error is issued here
9110                  if the external-linkage check fails.  */
9111               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9112                 cp_parser_simulate_error (parser);
9113             }
9114           else if (is_overloaded_fn (argument))
9115             /* All overloaded functions are allowed; if the external
9116                linkage test does not pass, an error will be issued
9117                later.  */
9118             ;
9119           else if (address_p
9120                    && (TREE_CODE (argument) == OFFSET_REF
9121                        || TREE_CODE (argument) == SCOPE_REF))
9122             /* A pointer-to-member.  */
9123             ;
9124           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9125             ;
9126           else
9127             cp_parser_simulate_error (parser);
9128
9129           if (cp_parser_parse_definitely (parser))
9130             {
9131               if (address_p)
9132                 argument = build_x_unary_op (ADDR_EXPR, argument);
9133               return argument;
9134             }
9135         }
9136     }
9137   /* If the argument started with "&", there are no other valid
9138      alternatives at this point.  */
9139   if (address_p)
9140     {
9141       cp_parser_error (parser, "invalid non-type template argument");
9142       return error_mark_node;
9143     }
9144
9145   /* If the argument wasn't successfully parsed as a type-id followed
9146      by '>>', the argument can only be a constant expression now.
9147      Otherwise, we try parsing the constant-expression tentatively,
9148      because the argument could really be a type-id.  */
9149   if (maybe_type_id)
9150     cp_parser_parse_tentatively (parser);
9151   argument = cp_parser_constant_expression (parser,
9152                                             /*allow_non_constant_p=*/false,
9153                                             /*non_constant_p=*/NULL);
9154   argument = fold_non_dependent_expr (argument);
9155   if (!maybe_type_id)
9156     return argument;
9157   if (!cp_parser_next_token_ends_template_argument_p (parser))
9158     cp_parser_error (parser, "expected template-argument");
9159   if (cp_parser_parse_definitely (parser))
9160     return argument;
9161   /* We did our best to parse the argument as a non type-id, but that
9162      was the only alternative that matched (albeit with a '>' after
9163      it). We can assume it's just a typo from the user, and a
9164      diagnostic will then be issued.  */
9165   return cp_parser_type_id (parser);
9166 }
9167
9168 /* Parse an explicit-instantiation.
9169
9170    explicit-instantiation:
9171      template declaration
9172
9173    Although the standard says `declaration', what it really means is:
9174
9175    explicit-instantiation:
9176      template decl-specifier-seq [opt] declarator [opt] ;
9177
9178    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9179    supposed to be allowed.  A defect report has been filed about this
9180    issue.
9181
9182    GNU Extension:
9183
9184    explicit-instantiation:
9185      storage-class-specifier template
9186        decl-specifier-seq [opt] declarator [opt] ;
9187      function-specifier template
9188        decl-specifier-seq [opt] declarator [opt] ;  */
9189
9190 static void
9191 cp_parser_explicit_instantiation (cp_parser* parser)
9192 {
9193   int declares_class_or_enum;
9194   cp_decl_specifier_seq decl_specifiers;
9195   tree extension_specifier = NULL_TREE;
9196
9197   /* Look for an (optional) storage-class-specifier or
9198      function-specifier.  */
9199   if (cp_parser_allow_gnu_extensions_p (parser))
9200     {
9201       extension_specifier
9202         = cp_parser_storage_class_specifier_opt (parser);
9203       if (!extension_specifier)
9204         extension_specifier
9205           = cp_parser_function_specifier_opt (parser,
9206                                               /*decl_specs=*/NULL);
9207     }
9208
9209   /* Look for the `template' keyword.  */
9210   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9211   /* Let the front end know that we are processing an explicit
9212      instantiation.  */
9213   begin_explicit_instantiation ();
9214   /* [temp.explicit] says that we are supposed to ignore access
9215      control while processing explicit instantiation directives.  */
9216   push_deferring_access_checks (dk_no_check);
9217   /* Parse a decl-specifier-seq.  */
9218   cp_parser_decl_specifier_seq (parser,
9219                                 CP_PARSER_FLAGS_OPTIONAL,
9220                                 &decl_specifiers,
9221                                 &declares_class_or_enum);
9222   /* If there was exactly one decl-specifier, and it declared a class,
9223      and there's no declarator, then we have an explicit type
9224      instantiation.  */
9225   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9226     {
9227       tree type;
9228
9229       type = check_tag_decl (&decl_specifiers);
9230       /* Turn access control back on for names used during
9231          template instantiation.  */
9232       pop_deferring_access_checks ();
9233       if (type)
9234         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9235     }
9236   else
9237     {
9238       cp_declarator *declarator;
9239       tree decl;
9240
9241       /* Parse the declarator.  */
9242       declarator
9243         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9244                                 /*ctor_dtor_or_conv_p=*/NULL,
9245                                 /*parenthesized_p=*/NULL,
9246                                 /*member_p=*/false);
9247       if (declares_class_or_enum & 2)
9248         cp_parser_check_for_definition_in_return_type (declarator,
9249                                                        decl_specifiers.type);
9250       if (declarator != cp_error_declarator)
9251         {
9252           decl = grokdeclarator (declarator, &decl_specifiers,
9253                                  NORMAL, 0, NULL);
9254           /* Turn access control back on for names used during
9255              template instantiation.  */
9256           pop_deferring_access_checks ();
9257           /* Do the explicit instantiation.  */
9258           do_decl_instantiation (decl, extension_specifier);
9259         }
9260       else
9261         {
9262           pop_deferring_access_checks ();
9263           /* Skip the body of the explicit instantiation.  */
9264           cp_parser_skip_to_end_of_statement (parser);
9265         }
9266     }
9267   /* We're done with the instantiation.  */
9268   end_explicit_instantiation ();
9269
9270   cp_parser_consume_semicolon_at_end_of_statement (parser);
9271 }
9272
9273 /* Parse an explicit-specialization.
9274
9275    explicit-specialization:
9276      template < > declaration
9277
9278    Although the standard says `declaration', what it really means is:
9279
9280    explicit-specialization:
9281      template <> decl-specifier [opt] init-declarator [opt] ;
9282      template <> function-definition
9283      template <> explicit-specialization
9284      template <> template-declaration  */
9285
9286 static void
9287 cp_parser_explicit_specialization (cp_parser* parser)
9288 {
9289   /* Look for the `template' keyword.  */
9290   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9291   /* Look for the `<'.  */
9292   cp_parser_require (parser, CPP_LESS, "`<'");
9293   /* Look for the `>'.  */
9294   cp_parser_require (parser, CPP_GREATER, "`>'");
9295   /* We have processed another parameter list.  */
9296   ++parser->num_template_parameter_lists;
9297   /* Let the front end know that we are beginning a specialization.  */
9298   begin_specialization ();
9299
9300   /* If the next keyword is `template', we need to figure out whether
9301      or not we're looking a template-declaration.  */
9302   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9303     {
9304       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9305           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9306         cp_parser_template_declaration_after_export (parser,
9307                                                      /*member_p=*/false);
9308       else
9309         cp_parser_explicit_specialization (parser);
9310     }
9311   else
9312     /* Parse the dependent declaration.  */
9313     cp_parser_single_declaration (parser,
9314                                   /*member_p=*/false,
9315                                   /*friend_p=*/NULL);
9316
9317   /* We're done with the specialization.  */
9318   end_specialization ();
9319   /* We're done with this parameter list.  */
9320   --parser->num_template_parameter_lists;
9321 }
9322
9323 /* Parse a type-specifier.
9324
9325    type-specifier:
9326      simple-type-specifier
9327      class-specifier
9328      enum-specifier
9329      elaborated-type-specifier
9330      cv-qualifier
9331
9332    GNU Extension:
9333
9334    type-specifier:
9335      __complex__
9336
9337    Returns a representation of the type-specifier.  For a
9338    class-specifier, enum-specifier, or elaborated-type-specifier, a
9339    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9340
9341    The parser flags FLAGS is used to control type-specifier parsing.
9342
9343    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9344    in a decl-specifier-seq.
9345
9346    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9347    class-specifier, enum-specifier, or elaborated-type-specifier, then
9348    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9349    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9350    zero.
9351
9352    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9353    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9354    is set to FALSE.  */
9355
9356 static tree
9357 cp_parser_type_specifier (cp_parser* parser,
9358                           cp_parser_flags flags,
9359                           cp_decl_specifier_seq *decl_specs,
9360                           bool is_declaration,
9361                           int* declares_class_or_enum,
9362                           bool* is_cv_qualifier)
9363 {
9364   tree type_spec = NULL_TREE;
9365   cp_token *token;
9366   enum rid keyword;
9367   cp_decl_spec ds = ds_last;
9368
9369   /* Assume this type-specifier does not declare a new type.  */
9370   if (declares_class_or_enum)
9371     *declares_class_or_enum = 0;
9372   /* And that it does not specify a cv-qualifier.  */
9373   if (is_cv_qualifier)
9374     *is_cv_qualifier = false;
9375   /* Peek at the next token.  */
9376   token = cp_lexer_peek_token (parser->lexer);
9377
9378   /* If we're looking at a keyword, we can use that to guide the
9379      production we choose.  */
9380   keyword = token->keyword;
9381   switch (keyword)
9382     {
9383     case RID_ENUM:
9384       /* 'enum' [identifier] '{' introduces an enum-specifier;
9385          'enum' <anything else> introduces an elaborated-type-specifier.  */
9386       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9387           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9388               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9389                  == CPP_OPEN_BRACE))
9390         {
9391           if (parser->num_template_parameter_lists)
9392             {
9393               error ("template declaration of %qs", "enum");
9394               cp_parser_skip_to_end_of_block_or_statement (parser);
9395               type_spec = error_mark_node;
9396             }
9397           else
9398             type_spec = cp_parser_enum_specifier (parser);
9399
9400           if (declares_class_or_enum)
9401             *declares_class_or_enum = 2;
9402           if (decl_specs)
9403             cp_parser_set_decl_spec_type (decl_specs,
9404                                           type_spec,
9405                                           /*user_defined_p=*/true);
9406           return type_spec;
9407         }
9408       else
9409         goto elaborated_type_specifier;
9410
9411       /* Any of these indicate either a class-specifier, or an
9412          elaborated-type-specifier.  */
9413     case RID_CLASS:
9414     case RID_STRUCT:
9415     case RID_UNION:
9416       /* Parse tentatively so that we can back up if we don't find a
9417          class-specifier.  */
9418       cp_parser_parse_tentatively (parser);
9419       /* Look for the class-specifier.  */
9420       type_spec = cp_parser_class_specifier (parser);
9421       /* If that worked, we're done.  */
9422       if (cp_parser_parse_definitely (parser))
9423         {
9424           if (declares_class_or_enum)
9425             *declares_class_or_enum = 2;
9426           if (decl_specs)
9427             cp_parser_set_decl_spec_type (decl_specs,
9428                                           type_spec,
9429                                           /*user_defined_p=*/true);
9430           return type_spec;
9431         }
9432
9433       /* Fall through.  */
9434     elaborated_type_specifier:
9435       /* We're declaring (not defining) a class or enum.  */
9436       if (declares_class_or_enum)
9437         *declares_class_or_enum = 1;
9438
9439       /* Fall through.  */
9440     case RID_TYPENAME:
9441       /* Look for an elaborated-type-specifier.  */
9442       type_spec
9443         = (cp_parser_elaborated_type_specifier
9444            (parser,
9445             decl_specs && decl_specs->specs[(int) ds_friend],
9446             is_declaration));
9447       if (decl_specs)
9448         cp_parser_set_decl_spec_type (decl_specs,
9449                                       type_spec,
9450                                       /*user_defined_p=*/true);
9451       return type_spec;
9452
9453     case RID_CONST:
9454       ds = ds_const;
9455       if (is_cv_qualifier)
9456         *is_cv_qualifier = true;
9457       break;
9458
9459     case RID_VOLATILE:
9460       ds = ds_volatile;
9461       if (is_cv_qualifier)
9462         *is_cv_qualifier = true;
9463       break;
9464
9465     case RID_RESTRICT:
9466       ds = ds_restrict;
9467       if (is_cv_qualifier)
9468         *is_cv_qualifier = true;
9469       break;
9470
9471     case RID_COMPLEX:
9472       /* The `__complex__' keyword is a GNU extension.  */
9473       ds = ds_complex;
9474       break;
9475
9476     default:
9477       break;
9478     }
9479
9480   /* Handle simple keywords.  */
9481   if (ds != ds_last)
9482     {
9483       if (decl_specs)
9484         {
9485           ++decl_specs->specs[(int)ds];
9486           decl_specs->any_specifiers_p = true;
9487         }
9488       return cp_lexer_consume_token (parser->lexer)->value;
9489     }
9490
9491   /* If we do not already have a type-specifier, assume we are looking
9492      at a simple-type-specifier.  */
9493   type_spec = cp_parser_simple_type_specifier (parser,
9494                                                decl_specs,
9495                                                flags);
9496
9497   /* If we didn't find a type-specifier, and a type-specifier was not
9498      optional in this context, issue an error message.  */
9499   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9500     {
9501       cp_parser_error (parser, "expected type specifier");
9502       return error_mark_node;
9503     }
9504
9505   return type_spec;
9506 }
9507
9508 /* Parse a simple-type-specifier.
9509
9510    simple-type-specifier:
9511      :: [opt] nested-name-specifier [opt] type-name
9512      :: [opt] nested-name-specifier template template-id
9513      char
9514      wchar_t
9515      bool
9516      short
9517      int
9518      long
9519      signed
9520      unsigned
9521      float
9522      double
9523      void
9524
9525    GNU Extension:
9526
9527    simple-type-specifier:
9528      __typeof__ unary-expression
9529      __typeof__ ( type-id )
9530
9531    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9532    appropriately updated.  */
9533
9534 static tree
9535 cp_parser_simple_type_specifier (cp_parser* parser,
9536                                  cp_decl_specifier_seq *decl_specs,
9537                                  cp_parser_flags flags)
9538 {
9539   tree type = NULL_TREE;
9540   cp_token *token;
9541
9542   /* Peek at the next token.  */
9543   token = cp_lexer_peek_token (parser->lexer);
9544
9545   /* If we're looking at a keyword, things are easy.  */
9546   switch (token->keyword)
9547     {
9548     case RID_CHAR:
9549       if (decl_specs)
9550         decl_specs->explicit_char_p = true;
9551       type = char_type_node;
9552       break;
9553     case RID_WCHAR:
9554       type = wchar_type_node;
9555       break;
9556     case RID_BOOL:
9557       type = boolean_type_node;
9558       break;
9559     case RID_SHORT:
9560       if (decl_specs)
9561         ++decl_specs->specs[(int) ds_short];
9562       type = short_integer_type_node;
9563       break;
9564     case RID_INT:
9565       if (decl_specs)
9566         decl_specs->explicit_int_p = true;
9567       type = integer_type_node;
9568       break;
9569     case RID_LONG:
9570       if (decl_specs)
9571         ++decl_specs->specs[(int) ds_long];
9572       type = long_integer_type_node;
9573       break;
9574     case RID_SIGNED:
9575       if (decl_specs)
9576         ++decl_specs->specs[(int) ds_signed];
9577       type = integer_type_node;
9578       break;
9579     case RID_UNSIGNED:
9580       if (decl_specs)
9581         ++decl_specs->specs[(int) ds_unsigned];
9582       type = unsigned_type_node;
9583       break;
9584     case RID_FLOAT:
9585       type = float_type_node;
9586       break;
9587     case RID_DOUBLE:
9588       type = double_type_node;
9589       break;
9590     case RID_VOID:
9591       type = void_type_node;
9592       break;
9593
9594     case RID_TYPEOF:
9595       /* Consume the `typeof' token.  */
9596       cp_lexer_consume_token (parser->lexer);
9597       /* Parse the operand to `typeof'.  */
9598       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9599       /* If it is not already a TYPE, take its type.  */
9600       if (!TYPE_P (type))
9601         type = finish_typeof (type);
9602
9603       if (decl_specs)
9604         cp_parser_set_decl_spec_type (decl_specs, type,
9605                                       /*user_defined_p=*/true);
9606
9607       return type;
9608
9609     default:
9610       break;
9611     }
9612
9613   /* If the type-specifier was for a built-in type, we're done.  */
9614   if (type)
9615     {
9616       tree id;
9617
9618       /* Record the type.  */
9619       if (decl_specs
9620           && (token->keyword != RID_SIGNED
9621               && token->keyword != RID_UNSIGNED
9622               && token->keyword != RID_SHORT
9623               && token->keyword != RID_LONG))
9624         cp_parser_set_decl_spec_type (decl_specs,
9625                                       type,
9626                                       /*user_defined=*/false);
9627       if (decl_specs)
9628         decl_specs->any_specifiers_p = true;
9629
9630       /* Consume the token.  */
9631       id = cp_lexer_consume_token (parser->lexer)->value;
9632
9633       /* There is no valid C++ program where a non-template type is
9634          followed by a "<".  That usually indicates that the user thought
9635          that the type was a template.  */
9636       cp_parser_check_for_invalid_template_id (parser, type);
9637
9638       return TYPE_NAME (type);
9639     }
9640
9641   /* The type-specifier must be a user-defined type.  */
9642   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9643     {
9644       bool qualified_p;
9645       bool global_p;
9646
9647       /* Don't gobble tokens or issue error messages if this is an
9648          optional type-specifier.  */
9649       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9650         cp_parser_parse_tentatively (parser);
9651
9652       /* Look for the optional `::' operator.  */
9653       global_p
9654         = (cp_parser_global_scope_opt (parser,
9655                                        /*current_scope_valid_p=*/false)
9656            != NULL_TREE);
9657       /* Look for the nested-name specifier.  */
9658       qualified_p
9659         = (cp_parser_nested_name_specifier_opt (parser,
9660                                                 /*typename_keyword_p=*/false,
9661                                                 /*check_dependency_p=*/true,
9662                                                 /*type_p=*/false,
9663                                                 /*is_declaration=*/false)
9664            != NULL_TREE);
9665       /* If we have seen a nested-name-specifier, and the next token
9666          is `template', then we are using the template-id production.  */
9667       if (parser->scope
9668           && cp_parser_optional_template_keyword (parser))
9669         {
9670           /* Look for the template-id.  */
9671           type = cp_parser_template_id (parser,
9672                                         /*template_keyword_p=*/true,
9673                                         /*check_dependency_p=*/true,
9674                                         /*is_declaration=*/false);
9675           /* If the template-id did not name a type, we are out of
9676              luck.  */
9677           if (TREE_CODE (type) != TYPE_DECL)
9678             {
9679               cp_parser_error (parser, "expected template-id for type");
9680               type = NULL_TREE;
9681             }
9682         }
9683       /* Otherwise, look for a type-name.  */
9684       else
9685         type = cp_parser_type_name (parser);
9686       /* Keep track of all name-lookups performed in class scopes.  */
9687       if (type
9688           && !global_p
9689           && !qualified_p
9690           && TREE_CODE (type) == TYPE_DECL
9691           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9692         maybe_note_name_used_in_class (DECL_NAME (type), type);
9693       /* If it didn't work out, we don't have a TYPE.  */
9694       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9695           && !cp_parser_parse_definitely (parser))
9696         type = NULL_TREE;
9697       if (type && decl_specs)
9698         cp_parser_set_decl_spec_type (decl_specs, type,
9699                                       /*user_defined=*/true);
9700     }
9701
9702   /* If we didn't get a type-name, issue an error message.  */
9703   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9704     {
9705       cp_parser_error (parser, "expected type-name");
9706       return error_mark_node;
9707     }
9708
9709   /* There is no valid C++ program where a non-template type is
9710      followed by a "<".  That usually indicates that the user thought
9711      that the type was a template.  */
9712   if (type && type != error_mark_node)
9713     {
9714       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9715          If it is, then the '<'...'>' enclose protocol names rather than
9716          template arguments, and so everything is fine.  */
9717       if (c_dialect_objc ()
9718           && (objc_is_id (type) || objc_is_class_name (type)))
9719         {
9720           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9721           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9722
9723           /* Clobber the "unqualified" type previously entered into
9724              DECL_SPECS with the new, improved protocol-qualified version.  */
9725           if (decl_specs)
9726             decl_specs->type = qual_type;
9727
9728           return qual_type;
9729         }
9730
9731       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9732     }
9733
9734   return type;
9735 }
9736
9737 /* Parse a type-name.
9738
9739    type-name:
9740      class-name
9741      enum-name
9742      typedef-name
9743
9744    enum-name:
9745      identifier
9746
9747    typedef-name:
9748      identifier
9749
9750    Returns a TYPE_DECL for the type.  */
9751
9752 static tree
9753 cp_parser_type_name (cp_parser* parser)
9754 {
9755   tree type_decl;
9756   tree identifier;
9757
9758   /* We can't know yet whether it is a class-name or not.  */
9759   cp_parser_parse_tentatively (parser);
9760   /* Try a class-name.  */
9761   type_decl = cp_parser_class_name (parser,
9762                                     /*typename_keyword_p=*/false,
9763                                     /*template_keyword_p=*/false,
9764                                     none_type,
9765                                     /*check_dependency_p=*/true,
9766                                     /*class_head_p=*/false,
9767                                     /*is_declaration=*/false);
9768   /* If it's not a class-name, keep looking.  */
9769   if (!cp_parser_parse_definitely (parser))
9770     {
9771       /* It must be a typedef-name or an enum-name.  */
9772       identifier = cp_parser_identifier (parser);
9773       if (identifier == error_mark_node)
9774         return error_mark_node;
9775
9776       /* Look up the type-name.  */
9777       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9778
9779       if (TREE_CODE (type_decl) != TYPE_DECL
9780           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9781         {
9782           /* See if this is an Objective-C type.  */
9783           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9784           tree type = objc_get_protocol_qualified_type (identifier, protos);
9785           if (type)
9786             type_decl = TYPE_NAME (type);
9787         }
9788
9789       /* Issue an error if we did not find a type-name.  */
9790       if (TREE_CODE (type_decl) != TYPE_DECL)
9791         {
9792           if (!cp_parser_simulate_error (parser))
9793             cp_parser_name_lookup_error (parser, identifier, type_decl,
9794                                          "is not a type");
9795           type_decl = error_mark_node;
9796         }
9797       /* Remember that the name was used in the definition of the
9798          current class so that we can check later to see if the
9799          meaning would have been different after the class was
9800          entirely defined.  */
9801       else if (type_decl != error_mark_node
9802                && !parser->scope)
9803         maybe_note_name_used_in_class (identifier, type_decl);
9804     }
9805
9806   return type_decl;
9807 }
9808
9809
9810 /* Parse an elaborated-type-specifier.  Note that the grammar given
9811    here incorporates the resolution to DR68.
9812
9813    elaborated-type-specifier:
9814      class-key :: [opt] nested-name-specifier [opt] identifier
9815      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9816      enum :: [opt] nested-name-specifier [opt] identifier
9817      typename :: [opt] nested-name-specifier identifier
9818      typename :: [opt] nested-name-specifier template [opt]
9819        template-id
9820
9821    GNU extension:
9822
9823    elaborated-type-specifier:
9824      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9825      class-key attributes :: [opt] nested-name-specifier [opt]
9826                template [opt] template-id
9827      enum attributes :: [opt] nested-name-specifier [opt] identifier
9828
9829    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9830    declared `friend'.  If IS_DECLARATION is TRUE, then this
9831    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9832    something is being declared.
9833
9834    Returns the TYPE specified.  */
9835
9836 static tree
9837 cp_parser_elaborated_type_specifier (cp_parser* parser,
9838                                      bool is_friend,
9839                                      bool is_declaration)
9840 {
9841   enum tag_types tag_type;
9842   tree identifier;
9843   tree type = NULL_TREE;
9844   tree attributes = NULL_TREE;
9845
9846   /* See if we're looking at the `enum' keyword.  */
9847   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9848     {
9849       /* Consume the `enum' token.  */
9850       cp_lexer_consume_token (parser->lexer);
9851       /* Remember that it's an enumeration type.  */
9852       tag_type = enum_type;
9853       /* Parse the attributes.  */
9854       attributes = cp_parser_attributes_opt (parser);
9855     }
9856   /* Or, it might be `typename'.  */
9857   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9858                                            RID_TYPENAME))
9859     {
9860       /* Consume the `typename' token.  */
9861       cp_lexer_consume_token (parser->lexer);
9862       /* Remember that it's a `typename' type.  */
9863       tag_type = typename_type;
9864       /* The `typename' keyword is only allowed in templates.  */
9865       if (!processing_template_decl)
9866         pedwarn ("using %<typename%> outside of template");
9867     }
9868   /* Otherwise it must be a class-key.  */
9869   else
9870     {
9871       tag_type = cp_parser_class_key (parser);
9872       if (tag_type == none_type)
9873         return error_mark_node;
9874       /* Parse the attributes.  */
9875       attributes = cp_parser_attributes_opt (parser);
9876     }
9877
9878   /* Look for the `::' operator.  */
9879   cp_parser_global_scope_opt (parser,
9880                               /*current_scope_valid_p=*/false);
9881   /* Look for the nested-name-specifier.  */
9882   if (tag_type == typename_type)
9883     {
9884       if (!cp_parser_nested_name_specifier (parser,
9885                                            /*typename_keyword_p=*/true,
9886                                            /*check_dependency_p=*/true,
9887                                            /*type_p=*/true,
9888                                             is_declaration))
9889         return error_mark_node;
9890     }
9891   else
9892     /* Even though `typename' is not present, the proposed resolution
9893        to Core Issue 180 says that in `class A<T>::B', `B' should be
9894        considered a type-name, even if `A<T>' is dependent.  */
9895     cp_parser_nested_name_specifier_opt (parser,
9896                                          /*typename_keyword_p=*/true,
9897                                          /*check_dependency_p=*/true,
9898                                          /*type_p=*/true,
9899                                          is_declaration);
9900   /* For everything but enumeration types, consider a template-id.  */
9901   if (tag_type != enum_type)
9902     {
9903       bool template_p = false;
9904       tree decl;
9905
9906       /* Allow the `template' keyword.  */
9907       template_p = cp_parser_optional_template_keyword (parser);
9908       /* If we didn't see `template', we don't know if there's a
9909          template-id or not.  */
9910       if (!template_p)
9911         cp_parser_parse_tentatively (parser);
9912       /* Parse the template-id.  */
9913       decl = cp_parser_template_id (parser, template_p,
9914                                     /*check_dependency_p=*/true,
9915                                     is_declaration);
9916       /* If we didn't find a template-id, look for an ordinary
9917          identifier.  */
9918       if (!template_p && !cp_parser_parse_definitely (parser))
9919         ;
9920       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9921          in effect, then we must assume that, upon instantiation, the
9922          template will correspond to a class.  */
9923       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9924                && tag_type == typename_type)
9925         type = make_typename_type (parser->scope, decl,
9926                                    typename_type,
9927                                    /*complain=*/1);
9928       else
9929         type = TREE_TYPE (decl);
9930     }
9931
9932   /* For an enumeration type, consider only a plain identifier.  */
9933   if (!type)
9934     {
9935       identifier = cp_parser_identifier (parser);
9936
9937       if (identifier == error_mark_node)
9938         {
9939           parser->scope = NULL_TREE;
9940           return error_mark_node;
9941         }
9942
9943       /* For a `typename', we needn't call xref_tag.  */
9944       if (tag_type == typename_type
9945           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9946         return cp_parser_make_typename_type (parser, parser->scope,
9947                                              identifier);
9948       /* Look up a qualified name in the usual way.  */
9949       if (parser->scope)
9950         {
9951           tree decl;
9952
9953           decl = cp_parser_lookup_name (parser, identifier,
9954                                         tag_type,
9955                                         /*is_template=*/false,
9956                                         /*is_namespace=*/false,
9957                                         /*check_dependency=*/true,
9958                                         /*ambiguous_p=*/NULL);
9959
9960           /* If we are parsing friend declaration, DECL may be a
9961              TEMPLATE_DECL tree node here.  However, we need to check
9962              whether this TEMPLATE_DECL results in valid code.  Consider
9963              the following example:
9964
9965                namespace N {
9966                  template <class T> class C {};
9967                }
9968                class X {
9969                  template <class T> friend class N::C; // #1, valid code
9970                };
9971                template <class T> class Y {
9972                  friend class N::C;                    // #2, invalid code
9973                };
9974
9975              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9976              name lookup of `N::C'.  We see that friend declaration must
9977              be template for the code to be valid.  Note that
9978              processing_template_decl does not work here since it is
9979              always 1 for the above two cases.  */
9980
9981           decl = (cp_parser_maybe_treat_template_as_class
9982                   (decl, /*tag_name_p=*/is_friend
9983                          && parser->num_template_parameter_lists));
9984
9985           if (TREE_CODE (decl) != TYPE_DECL)
9986             {
9987               cp_parser_diagnose_invalid_type_name (parser,
9988                                                     parser->scope,
9989                                                     identifier);
9990               return error_mark_node;
9991             }
9992
9993           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9994             check_elaborated_type_specifier
9995               (tag_type, decl,
9996                (parser->num_template_parameter_lists
9997                 || DECL_SELF_REFERENCE_P (decl)));
9998
9999           type = TREE_TYPE (decl);
10000         }
10001       else
10002         {
10003           /* An elaborated-type-specifier sometimes introduces a new type and
10004              sometimes names an existing type.  Normally, the rule is that it
10005              introduces a new type only if there is not an existing type of
10006              the same name already in scope.  For example, given:
10007
10008                struct S {};
10009                void f() { struct S s; }
10010
10011              the `struct S' in the body of `f' is the same `struct S' as in
10012              the global scope; the existing definition is used.  However, if
10013              there were no global declaration, this would introduce a new
10014              local class named `S'.
10015
10016              An exception to this rule applies to the following code:
10017
10018                namespace N { struct S; }
10019
10020              Here, the elaborated-type-specifier names a new type
10021              unconditionally; even if there is already an `S' in the
10022              containing scope this declaration names a new type.
10023              This exception only applies if the elaborated-type-specifier
10024              forms the complete declaration:
10025
10026                [class.name]
10027
10028                A declaration consisting solely of `class-key identifier ;' is
10029                either a redeclaration of the name in the current scope or a
10030                forward declaration of the identifier as a class name.  It
10031                introduces the name into the current scope.
10032
10033              We are in this situation precisely when the next token is a `;'.
10034
10035              An exception to the exception is that a `friend' declaration does
10036              *not* name a new type; i.e., given:
10037
10038                struct S { friend struct T; };
10039
10040              `T' is not a new type in the scope of `S'.
10041
10042              Also, `new struct S' or `sizeof (struct S)' never results in the
10043              definition of a new type; a new type can only be declared in a
10044              declaration context.  */
10045
10046           tag_scope ts;
10047           if (is_friend)
10048             /* Friends have special name lookup rules.  */
10049             ts = ts_within_enclosing_non_class;
10050           else if (is_declaration
10051                    && cp_lexer_next_token_is (parser->lexer,
10052                                               CPP_SEMICOLON))
10053             /* This is a `class-key identifier ;' */
10054             ts = ts_current;
10055           else
10056             ts = ts_global;
10057
10058           /* Warn about attributes. They are ignored.  */
10059           if (attributes)
10060             warning (OPT_Wattributes,
10061                      "type attributes are honored only at type definition");
10062
10063           type = xref_tag (tag_type, identifier, ts,
10064                            parser->num_template_parameter_lists);
10065         }
10066     }
10067   if (tag_type != enum_type)
10068     cp_parser_check_class_key (tag_type, type);
10069
10070   /* A "<" cannot follow an elaborated type specifier.  If that
10071      happens, the user was probably trying to form a template-id.  */
10072   cp_parser_check_for_invalid_template_id (parser, type);
10073
10074   return type;
10075 }
10076
10077 /* Parse an enum-specifier.
10078
10079    enum-specifier:
10080      enum identifier [opt] { enumerator-list [opt] }
10081
10082    GNU Extensions:
10083      enum identifier [opt] { enumerator-list [opt] } attributes
10084
10085    Returns an ENUM_TYPE representing the enumeration.  */
10086
10087 static tree
10088 cp_parser_enum_specifier (cp_parser* parser)
10089 {
10090   tree identifier;
10091   tree type;
10092
10093   /* Caller guarantees that the current token is 'enum', an identifier
10094      possibly follows, and the token after that is an opening brace.
10095      If we don't have an identifier, fabricate an anonymous name for
10096      the enumeration being defined.  */
10097   cp_lexer_consume_token (parser->lexer);
10098
10099   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10100     identifier = cp_parser_identifier (parser);
10101   else
10102     identifier = make_anon_name ();
10103
10104   /* Issue an error message if type-definitions are forbidden here.  */
10105   cp_parser_check_type_definition (parser);
10106
10107   /* Create the new type.  We do this before consuming the opening brace
10108      so the enum will be recorded as being on the line of its tag (or the
10109      'enum' keyword, if there is no tag).  */
10110   type = start_enum (identifier);
10111
10112   /* Consume the opening brace.  */
10113   cp_lexer_consume_token (parser->lexer);
10114
10115   /* If the next token is not '}', then there are some enumerators.  */
10116   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10117     cp_parser_enumerator_list (parser, type);
10118
10119   /* Consume the final '}'.  */
10120   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10121
10122   /* Look for trailing attributes to apply to this enumeration, and
10123      apply them if appropriate.  */
10124   if (cp_parser_allow_gnu_extensions_p (parser))
10125     {
10126       tree trailing_attr = cp_parser_attributes_opt (parser);
10127       cplus_decl_attributes (&type,
10128                              trailing_attr,
10129                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10130     }
10131
10132   /* Finish up the enumeration.  */
10133   finish_enum (type);
10134
10135   return type;
10136 }
10137
10138 /* Parse an enumerator-list.  The enumerators all have the indicated
10139    TYPE.
10140
10141    enumerator-list:
10142      enumerator-definition
10143      enumerator-list , enumerator-definition  */
10144
10145 static void
10146 cp_parser_enumerator_list (cp_parser* parser, tree type)
10147 {
10148   while (true)
10149     {
10150       /* Parse an enumerator-definition.  */
10151       cp_parser_enumerator_definition (parser, type);
10152
10153       /* If the next token is not a ',', we've reached the end of
10154          the list.  */
10155       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10156         break;
10157       /* Otherwise, consume the `,' and keep going.  */
10158       cp_lexer_consume_token (parser->lexer);
10159       /* If the next token is a `}', there is a trailing comma.  */
10160       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10161         {
10162           if (pedantic && !in_system_header)
10163             pedwarn ("comma at end of enumerator list");
10164           break;
10165         }
10166     }
10167 }
10168
10169 /* Parse an enumerator-definition.  The enumerator has the indicated
10170    TYPE.
10171
10172    enumerator-definition:
10173      enumerator
10174      enumerator = constant-expression
10175
10176    enumerator:
10177      identifier  */
10178
10179 static void
10180 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10181 {
10182   tree identifier;
10183   tree value;
10184
10185   /* Look for the identifier.  */
10186   identifier = cp_parser_identifier (parser);
10187   if (identifier == error_mark_node)
10188     return;
10189
10190   /* If the next token is an '=', then there is an explicit value.  */
10191   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10192     {
10193       /* Consume the `=' token.  */
10194       cp_lexer_consume_token (parser->lexer);
10195       /* Parse the value.  */
10196       value = cp_parser_constant_expression (parser,
10197                                              /*allow_non_constant_p=*/false,
10198                                              NULL);
10199     }
10200   else
10201     value = NULL_TREE;
10202
10203   /* Create the enumerator.  */
10204   build_enumerator (identifier, value, type);
10205 }
10206
10207 /* Parse a namespace-name.
10208
10209    namespace-name:
10210      original-namespace-name
10211      namespace-alias
10212
10213    Returns the NAMESPACE_DECL for the namespace.  */
10214
10215 static tree
10216 cp_parser_namespace_name (cp_parser* parser)
10217 {
10218   tree identifier;
10219   tree namespace_decl;
10220
10221   /* Get the name of the namespace.  */
10222   identifier = cp_parser_identifier (parser);
10223   if (identifier == error_mark_node)
10224     return error_mark_node;
10225
10226   /* Look up the identifier in the currently active scope.  Look only
10227      for namespaces, due to:
10228
10229        [basic.lookup.udir]
10230
10231        When looking up a namespace-name in a using-directive or alias
10232        definition, only namespace names are considered.
10233
10234      And:
10235
10236        [basic.lookup.qual]
10237
10238        During the lookup of a name preceding the :: scope resolution
10239        operator, object, function, and enumerator names are ignored.
10240
10241      (Note that cp_parser_class_or_namespace_name only calls this
10242      function if the token after the name is the scope resolution
10243      operator.)  */
10244   namespace_decl = cp_parser_lookup_name (parser, identifier,
10245                                           none_type,
10246                                           /*is_template=*/false,
10247                                           /*is_namespace=*/true,
10248                                           /*check_dependency=*/true,
10249                                           /*ambiguous_p=*/NULL);
10250   /* If it's not a namespace, issue an error.  */
10251   if (namespace_decl == error_mark_node
10252       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10253     {
10254       cp_parser_error (parser, "expected namespace-name");
10255       namespace_decl = error_mark_node;
10256     }
10257
10258   return namespace_decl;
10259 }
10260
10261 /* Parse a namespace-definition.
10262
10263    namespace-definition:
10264      named-namespace-definition
10265      unnamed-namespace-definition
10266
10267    named-namespace-definition:
10268      original-namespace-definition
10269      extension-namespace-definition
10270
10271    original-namespace-definition:
10272      namespace identifier { namespace-body }
10273
10274    extension-namespace-definition:
10275      namespace original-namespace-name { namespace-body }
10276
10277    unnamed-namespace-definition:
10278      namespace { namespace-body } */
10279
10280 static void
10281 cp_parser_namespace_definition (cp_parser* parser)
10282 {
10283   tree identifier;
10284
10285   /* Look for the `namespace' keyword.  */
10286   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10287
10288   /* Get the name of the namespace.  We do not attempt to distinguish
10289      between an original-namespace-definition and an
10290      extension-namespace-definition at this point.  The semantic
10291      analysis routines are responsible for that.  */
10292   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10293     identifier = cp_parser_identifier (parser);
10294   else
10295     identifier = NULL_TREE;
10296
10297   /* Look for the `{' to start the namespace.  */
10298   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10299   /* Start the namespace.  */
10300   push_namespace (identifier);
10301   /* Parse the body of the namespace.  */
10302   cp_parser_namespace_body (parser);
10303   /* Finish the namespace.  */
10304   pop_namespace ();
10305   /* Look for the final `}'.  */
10306   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10307 }
10308
10309 /* Parse a namespace-body.
10310
10311    namespace-body:
10312      declaration-seq [opt]  */
10313
10314 static void
10315 cp_parser_namespace_body (cp_parser* parser)
10316 {
10317   cp_parser_declaration_seq_opt (parser);
10318 }
10319
10320 /* Parse a namespace-alias-definition.
10321
10322    namespace-alias-definition:
10323      namespace identifier = qualified-namespace-specifier ;  */
10324
10325 static void
10326 cp_parser_namespace_alias_definition (cp_parser* parser)
10327 {
10328   tree identifier;
10329   tree namespace_specifier;
10330
10331   /* Look for the `namespace' keyword.  */
10332   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10333   /* Look for the identifier.  */
10334   identifier = cp_parser_identifier (parser);
10335   if (identifier == error_mark_node)
10336     return;
10337   /* Look for the `=' token.  */
10338   cp_parser_require (parser, CPP_EQ, "`='");
10339   /* Look for the qualified-namespace-specifier.  */
10340   namespace_specifier
10341     = cp_parser_qualified_namespace_specifier (parser);
10342   /* Look for the `;' token.  */
10343   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10344
10345   /* Register the alias in the symbol table.  */
10346   do_namespace_alias (identifier, namespace_specifier);
10347 }
10348
10349 /* Parse a qualified-namespace-specifier.
10350
10351    qualified-namespace-specifier:
10352      :: [opt] nested-name-specifier [opt] namespace-name
10353
10354    Returns a NAMESPACE_DECL corresponding to the specified
10355    namespace.  */
10356
10357 static tree
10358 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10359 {
10360   /* Look for the optional `::'.  */
10361   cp_parser_global_scope_opt (parser,
10362                               /*current_scope_valid_p=*/false);
10363
10364   /* Look for the optional nested-name-specifier.  */
10365   cp_parser_nested_name_specifier_opt (parser,
10366                                        /*typename_keyword_p=*/false,
10367                                        /*check_dependency_p=*/true,
10368                                        /*type_p=*/false,
10369                                        /*is_declaration=*/true);
10370
10371   return cp_parser_namespace_name (parser);
10372 }
10373
10374 /* Parse a using-declaration.
10375
10376    using-declaration:
10377      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10378      using :: unqualified-id ;  */
10379
10380 static void
10381 cp_parser_using_declaration (cp_parser* parser)
10382 {
10383   cp_token *token;
10384   bool typename_p = false;
10385   bool global_scope_p;
10386   tree decl;
10387   tree identifier;
10388   tree qscope;
10389
10390   /* Look for the `using' keyword.  */
10391   cp_parser_require_keyword (parser, RID_USING, "`using'");
10392
10393   /* Peek at the next token.  */
10394   token = cp_lexer_peek_token (parser->lexer);
10395   /* See if it's `typename'.  */
10396   if (token->keyword == RID_TYPENAME)
10397     {
10398       /* Remember that we've seen it.  */
10399       typename_p = true;
10400       /* Consume the `typename' token.  */
10401       cp_lexer_consume_token (parser->lexer);
10402     }
10403
10404   /* Look for the optional global scope qualification.  */
10405   global_scope_p
10406     = (cp_parser_global_scope_opt (parser,
10407                                    /*current_scope_valid_p=*/false)
10408        != NULL_TREE);
10409
10410   /* If we saw `typename', or didn't see `::', then there must be a
10411      nested-name-specifier present.  */
10412   if (typename_p || !global_scope_p)
10413     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10414                                               /*check_dependency_p=*/true,
10415                                               /*type_p=*/false,
10416                                               /*is_declaration=*/true);
10417   /* Otherwise, we could be in either of the two productions.  In that
10418      case, treat the nested-name-specifier as optional.  */
10419   else
10420     qscope = cp_parser_nested_name_specifier_opt (parser,
10421                                                   /*typename_keyword_p=*/false,
10422                                                   /*check_dependency_p=*/true,
10423                                                   /*type_p=*/false,
10424                                                   /*is_declaration=*/true);
10425   if (!qscope)
10426     qscope = global_namespace;
10427
10428   /* Parse the unqualified-id.  */
10429   identifier = cp_parser_unqualified_id (parser,
10430                                          /*template_keyword_p=*/false,
10431                                          /*check_dependency_p=*/true,
10432                                          /*declarator_p=*/true);
10433
10434   /* The function we call to handle a using-declaration is different
10435      depending on what scope we are in.  */
10436   if (identifier == error_mark_node)
10437     ;
10438   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10439            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10440     /* [namespace.udecl]
10441
10442        A using declaration shall not name a template-id.  */
10443     error ("a template-id may not appear in a using-declaration");
10444   else
10445     {
10446       if (at_class_scope_p ())
10447         {
10448           /* Create the USING_DECL.  */
10449           decl = do_class_using_decl (parser->scope, identifier);
10450           /* Add it to the list of members in this class.  */
10451           finish_member_declaration (decl);
10452         }
10453       else
10454         {
10455           decl = cp_parser_lookup_name_simple (parser, identifier);
10456           if (decl == error_mark_node)
10457             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10458           else if (!at_namespace_scope_p ())
10459             do_local_using_decl (decl, qscope, identifier);
10460           else
10461             do_toplevel_using_decl (decl, qscope, identifier);
10462         }
10463     }
10464
10465   /* Look for the final `;'.  */
10466   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10467 }
10468
10469 /* Parse a using-directive.
10470
10471    using-directive:
10472      using namespace :: [opt] nested-name-specifier [opt]
10473        namespace-name ;  */
10474
10475 static void
10476 cp_parser_using_directive (cp_parser* parser)
10477 {
10478   tree namespace_decl;
10479   tree attribs;
10480
10481   /* Look for the `using' keyword.  */
10482   cp_parser_require_keyword (parser, RID_USING, "`using'");
10483   /* And the `namespace' keyword.  */
10484   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10485   /* Look for the optional `::' operator.  */
10486   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10487   /* And the optional nested-name-specifier.  */
10488   cp_parser_nested_name_specifier_opt (parser,
10489                                        /*typename_keyword_p=*/false,
10490                                        /*check_dependency_p=*/true,
10491                                        /*type_p=*/false,
10492                                        /*is_declaration=*/true);
10493   /* Get the namespace being used.  */
10494   namespace_decl = cp_parser_namespace_name (parser);
10495   /* And any specified attributes.  */
10496   attribs = cp_parser_attributes_opt (parser);
10497   /* Update the symbol table.  */
10498   parse_using_directive (namespace_decl, attribs);
10499   /* Look for the final `;'.  */
10500   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10501 }
10502
10503 /* Parse an asm-definition.
10504
10505    asm-definition:
10506      asm ( string-literal ) ;
10507
10508    GNU Extension:
10509
10510    asm-definition:
10511      asm volatile [opt] ( string-literal ) ;
10512      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10513      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10514                           : asm-operand-list [opt] ) ;
10515      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10516                           : asm-operand-list [opt]
10517                           : asm-operand-list [opt] ) ;  */
10518
10519 static void
10520 cp_parser_asm_definition (cp_parser* parser)
10521 {
10522   tree string;
10523   tree outputs = NULL_TREE;
10524   tree inputs = NULL_TREE;
10525   tree clobbers = NULL_TREE;
10526   tree asm_stmt;
10527   bool volatile_p = false;
10528   bool extended_p = false;
10529
10530   /* Look for the `asm' keyword.  */
10531   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10532   /* See if the next token is `volatile'.  */
10533   if (cp_parser_allow_gnu_extensions_p (parser)
10534       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10535     {
10536       /* Remember that we saw the `volatile' keyword.  */
10537       volatile_p = true;
10538       /* Consume the token.  */
10539       cp_lexer_consume_token (parser->lexer);
10540     }
10541   /* Look for the opening `('.  */
10542   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10543     return;
10544   /* Look for the string.  */
10545   string = cp_parser_string_literal (parser, false, false);
10546   if (string == error_mark_node)
10547     {
10548       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10549                                              /*consume_paren=*/true);
10550       return;
10551     }
10552
10553   /* If we're allowing GNU extensions, check for the extended assembly
10554      syntax.  Unfortunately, the `:' tokens need not be separated by
10555      a space in C, and so, for compatibility, we tolerate that here
10556      too.  Doing that means that we have to treat the `::' operator as
10557      two `:' tokens.  */
10558   if (cp_parser_allow_gnu_extensions_p (parser)
10559       && at_function_scope_p ()
10560       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10561           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10562     {
10563       bool inputs_p = false;
10564       bool clobbers_p = false;
10565
10566       /* The extended syntax was used.  */
10567       extended_p = true;
10568
10569       /* Look for outputs.  */
10570       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10571         {
10572           /* Consume the `:'.  */
10573           cp_lexer_consume_token (parser->lexer);
10574           /* Parse the output-operands.  */
10575           if (cp_lexer_next_token_is_not (parser->lexer,
10576                                           CPP_COLON)
10577               && cp_lexer_next_token_is_not (parser->lexer,
10578                                              CPP_SCOPE)
10579               && cp_lexer_next_token_is_not (parser->lexer,
10580                                              CPP_CLOSE_PAREN))
10581             outputs = cp_parser_asm_operand_list (parser);
10582         }
10583       /* If the next token is `::', there are no outputs, and the
10584          next token is the beginning of the inputs.  */
10585       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10586         /* The inputs are coming next.  */
10587         inputs_p = true;
10588
10589       /* Look for inputs.  */
10590       if (inputs_p
10591           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10592         {
10593           /* Consume the `:' or `::'.  */
10594           cp_lexer_consume_token (parser->lexer);
10595           /* Parse the output-operands.  */
10596           if (cp_lexer_next_token_is_not (parser->lexer,
10597                                           CPP_COLON)
10598               && cp_lexer_next_token_is_not (parser->lexer,
10599                                              CPP_CLOSE_PAREN))
10600             inputs = cp_parser_asm_operand_list (parser);
10601         }
10602       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10603         /* The clobbers are coming next.  */
10604         clobbers_p = true;
10605
10606       /* Look for clobbers.  */
10607       if (clobbers_p
10608           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10609         {
10610           /* Consume the `:' or `::'.  */
10611           cp_lexer_consume_token (parser->lexer);
10612           /* Parse the clobbers.  */
10613           if (cp_lexer_next_token_is_not (parser->lexer,
10614                                           CPP_CLOSE_PAREN))
10615             clobbers = cp_parser_asm_clobber_list (parser);
10616         }
10617     }
10618   /* Look for the closing `)'.  */
10619   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10620     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10621                                            /*consume_paren=*/true);
10622   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10623
10624   /* Create the ASM_EXPR.  */
10625   if (at_function_scope_p ())
10626     {
10627       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10628                                   inputs, clobbers);
10629       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10630       if (!extended_p)
10631         {
10632           tree temp = asm_stmt;
10633           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10634             temp = TREE_OPERAND (temp, 0);
10635
10636           ASM_INPUT_P (temp) = 1;
10637         }
10638     }
10639   else
10640     assemble_asm (string);
10641 }
10642
10643 /* Declarators [gram.dcl.decl] */
10644
10645 /* Parse an init-declarator.
10646
10647    init-declarator:
10648      declarator initializer [opt]
10649
10650    GNU Extension:
10651
10652    init-declarator:
10653      declarator asm-specification [opt] attributes [opt] initializer [opt]
10654
10655    function-definition:
10656      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10657        function-body
10658      decl-specifier-seq [opt] declarator function-try-block
10659
10660    GNU Extension:
10661
10662    function-definition:
10663      __extension__ function-definition
10664
10665    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10666    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10667    then this declarator appears in a class scope.  The new DECL created
10668    by this declarator is returned.
10669
10670    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10671    for a function-definition here as well.  If the declarator is a
10672    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10673    be TRUE upon return.  By that point, the function-definition will
10674    have been completely parsed.
10675
10676    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10677    is FALSE.  */
10678
10679 static tree
10680 cp_parser_init_declarator (cp_parser* parser,
10681                            cp_decl_specifier_seq *decl_specifiers,
10682                            bool function_definition_allowed_p,
10683                            bool member_p,
10684                            int declares_class_or_enum,
10685                            bool* function_definition_p)
10686 {
10687   cp_token *token;
10688   cp_declarator *declarator;
10689   tree prefix_attributes;
10690   tree attributes;
10691   tree asm_specification;
10692   tree initializer;
10693   tree decl = NULL_TREE;
10694   tree scope;
10695   bool is_initialized;
10696   bool is_parenthesized_init;
10697   bool is_non_constant_init;
10698   int ctor_dtor_or_conv_p;
10699   bool friend_p;
10700   tree pushed_scope = NULL;
10701
10702   /* Gather the attributes that were provided with the
10703      decl-specifiers.  */
10704   prefix_attributes = decl_specifiers->attributes;
10705
10706   /* Assume that this is not the declarator for a function
10707      definition.  */
10708   if (function_definition_p)
10709     *function_definition_p = false;
10710
10711   /* Defer access checks while parsing the declarator; we cannot know
10712      what names are accessible until we know what is being
10713      declared.  */
10714   resume_deferring_access_checks ();
10715
10716   /* Parse the declarator.  */
10717   declarator
10718     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10719                             &ctor_dtor_or_conv_p,
10720                             /*parenthesized_p=*/NULL,
10721                             /*member_p=*/false);
10722   /* Gather up the deferred checks.  */
10723   stop_deferring_access_checks ();
10724
10725   /* If the DECLARATOR was erroneous, there's no need to go
10726      further.  */
10727   if (declarator == cp_error_declarator)
10728     return error_mark_node;
10729
10730   if (declares_class_or_enum & 2)
10731     cp_parser_check_for_definition_in_return_type (declarator,
10732                                                    decl_specifiers->type);
10733
10734   /* Figure out what scope the entity declared by the DECLARATOR is
10735      located in.  `grokdeclarator' sometimes changes the scope, so
10736      we compute it now.  */
10737   scope = get_scope_of_declarator (declarator);
10738
10739   /* If we're allowing GNU extensions, look for an asm-specification
10740      and attributes.  */
10741   if (cp_parser_allow_gnu_extensions_p (parser))
10742     {
10743       /* Look for an asm-specification.  */
10744       asm_specification = cp_parser_asm_specification_opt (parser);
10745       /* And attributes.  */
10746       attributes = cp_parser_attributes_opt (parser);
10747     }
10748   else
10749     {
10750       asm_specification = NULL_TREE;
10751       attributes = NULL_TREE;
10752     }
10753
10754   /* Peek at the next token.  */
10755   token = cp_lexer_peek_token (parser->lexer);
10756   /* Check to see if the token indicates the start of a
10757      function-definition.  */
10758   if (cp_parser_token_starts_function_definition_p (token))
10759     {
10760       if (!function_definition_allowed_p)
10761         {
10762           /* If a function-definition should not appear here, issue an
10763              error message.  */
10764           cp_parser_error (parser,
10765                            "a function-definition is not allowed here");
10766           return error_mark_node;
10767         }
10768       else
10769         {
10770           /* Neither attributes nor an asm-specification are allowed
10771              on a function-definition.  */
10772           if (asm_specification)
10773             error ("an asm-specification is not allowed on a function-definition");
10774           if (attributes)
10775             error ("attributes are not allowed on a function-definition");
10776           /* This is a function-definition.  */
10777           *function_definition_p = true;
10778
10779           /* Parse the function definition.  */
10780           if (member_p)
10781             decl = cp_parser_save_member_function_body (parser,
10782                                                         decl_specifiers,
10783                                                         declarator,
10784                                                         prefix_attributes);
10785           else
10786             decl
10787               = (cp_parser_function_definition_from_specifiers_and_declarator
10788                  (parser, decl_specifiers, prefix_attributes, declarator));
10789
10790           return decl;
10791         }
10792     }
10793
10794   /* [dcl.dcl]
10795
10796      Only in function declarations for constructors, destructors, and
10797      type conversions can the decl-specifier-seq be omitted.
10798
10799      We explicitly postpone this check past the point where we handle
10800      function-definitions because we tolerate function-definitions
10801      that are missing their return types in some modes.  */
10802   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10803     {
10804       cp_parser_error (parser,
10805                        "expected constructor, destructor, or type conversion");
10806       return error_mark_node;
10807     }
10808
10809   /* An `=' or an `(' indicates an initializer.  */
10810   is_initialized = (token->type == CPP_EQ
10811                      || token->type == CPP_OPEN_PAREN);
10812   /* If the init-declarator isn't initialized and isn't followed by a
10813      `,' or `;', it's not a valid init-declarator.  */
10814   if (!is_initialized
10815       && token->type != CPP_COMMA
10816       && token->type != CPP_SEMICOLON)
10817     {
10818       cp_parser_error (parser, "expected initializer");
10819       return error_mark_node;
10820     }
10821
10822   /* Because start_decl has side-effects, we should only call it if we
10823      know we're going ahead.  By this point, we know that we cannot
10824      possibly be looking at any other construct.  */
10825   cp_parser_commit_to_tentative_parse (parser);
10826
10827   /* If the decl specifiers were bad, issue an error now that we're
10828      sure this was intended to be a declarator.  Then continue
10829      declaring the variable(s), as int, to try to cut down on further
10830      errors.  */
10831   if (decl_specifiers->any_specifiers_p
10832       && decl_specifiers->type == error_mark_node)
10833     {
10834       cp_parser_error (parser, "invalid type in declaration");
10835       decl_specifiers->type = integer_type_node;
10836     }
10837
10838   /* Check to see whether or not this declaration is a friend.  */
10839   friend_p = cp_parser_friend_p (decl_specifiers);
10840
10841   /* Check that the number of template-parameter-lists is OK.  */
10842   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10843     return error_mark_node;
10844
10845   /* Enter the newly declared entry in the symbol table.  If we're
10846      processing a declaration in a class-specifier, we wait until
10847      after processing the initializer.  */
10848   if (!member_p)
10849     {
10850       if (parser->in_unbraced_linkage_specification_p)
10851         {
10852           decl_specifiers->storage_class = sc_extern;
10853           have_extern_spec = false;
10854         }
10855       decl = start_decl (declarator, decl_specifiers,
10856                          is_initialized, attributes, prefix_attributes,
10857                          &pushed_scope);
10858     }
10859   else if (scope)
10860     /* Enter the SCOPE.  That way unqualified names appearing in the
10861        initializer will be looked up in SCOPE.  */
10862     pushed_scope = push_scope (scope);
10863
10864   /* Perform deferred access control checks, now that we know in which
10865      SCOPE the declared entity resides.  */
10866   if (!member_p && decl)
10867     {
10868       tree saved_current_function_decl = NULL_TREE;
10869
10870       /* If the entity being declared is a function, pretend that we
10871          are in its scope.  If it is a `friend', it may have access to
10872          things that would not otherwise be accessible.  */
10873       if (TREE_CODE (decl) == FUNCTION_DECL)
10874         {
10875           saved_current_function_decl = current_function_decl;
10876           current_function_decl = decl;
10877         }
10878
10879       /* Perform the access control checks for the declarator and the
10880          the decl-specifiers.  */
10881       perform_deferred_access_checks ();
10882
10883       /* Restore the saved value.  */
10884       if (TREE_CODE (decl) == FUNCTION_DECL)
10885         current_function_decl = saved_current_function_decl;
10886     }
10887
10888   /* Parse the initializer.  */
10889   if (is_initialized)
10890     initializer = cp_parser_initializer (parser,
10891                                          &is_parenthesized_init,
10892                                          &is_non_constant_init);
10893   else
10894     {
10895       initializer = NULL_TREE;
10896       is_parenthesized_init = false;
10897       is_non_constant_init = true;
10898     }
10899
10900   /* The old parser allows attributes to appear after a parenthesized
10901      initializer.  Mark Mitchell proposed removing this functionality
10902      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10903      attributes -- but ignores them.  */
10904   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10905     if (cp_parser_attributes_opt (parser))
10906       warning (OPT_Wattributes,
10907                "attributes after parenthesized initializer ignored");
10908
10909   /* For an in-class declaration, use `grokfield' to create the
10910      declaration.  */
10911   if (member_p)
10912     {
10913       if (pushed_scope)
10914         {
10915           pop_scope (pushed_scope);
10916           pushed_scope = false;
10917         }
10918       decl = grokfield (declarator, decl_specifiers,
10919                         initializer, /*asmspec=*/NULL_TREE,
10920                         /*attributes=*/NULL_TREE);
10921       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10922         cp_parser_save_default_args (parser, decl);
10923     }
10924
10925   /* Finish processing the declaration.  But, skip friend
10926      declarations.  */
10927   if (!friend_p && decl && decl != error_mark_node)
10928     {
10929       cp_finish_decl (decl,
10930                       initializer,
10931                       asm_specification,
10932                       /* If the initializer is in parentheses, then this is
10933                          a direct-initialization, which means that an
10934                          `explicit' constructor is OK.  Otherwise, an
10935                          `explicit' constructor cannot be used.  */
10936                       ((is_parenthesized_init || !is_initialized)
10937                      ? 0 : LOOKUP_ONLYCONVERTING));
10938     }
10939   if (!friend_p && pushed_scope)
10940     pop_scope (pushed_scope);
10941
10942   /* Remember whether or not variables were initialized by
10943      constant-expressions.  */
10944   if (decl && TREE_CODE (decl) == VAR_DECL
10945       && is_initialized && !is_non_constant_init)
10946     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10947
10948   return decl;
10949 }
10950
10951 /* Parse a declarator.
10952
10953    declarator:
10954      direct-declarator
10955      ptr-operator declarator
10956
10957    abstract-declarator:
10958      ptr-operator abstract-declarator [opt]
10959      direct-abstract-declarator
10960
10961    GNU Extensions:
10962
10963    declarator:
10964      attributes [opt] direct-declarator
10965      attributes [opt] ptr-operator declarator
10966
10967    abstract-declarator:
10968      attributes [opt] ptr-operator abstract-declarator [opt]
10969      attributes [opt] direct-abstract-declarator
10970
10971    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10972    detect constructor, destructor or conversion operators. It is set
10973    to -1 if the declarator is a name, and +1 if it is a
10974    function. Otherwise it is set to zero. Usually you just want to
10975    test for >0, but internally the negative value is used.
10976
10977    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10978    a decl-specifier-seq unless it declares a constructor, destructor,
10979    or conversion.  It might seem that we could check this condition in
10980    semantic analysis, rather than parsing, but that makes it difficult
10981    to handle something like `f()'.  We want to notice that there are
10982    no decl-specifiers, and therefore realize that this is an
10983    expression, not a declaration.)
10984
10985    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10986    the declarator is a direct-declarator of the form "(...)".
10987
10988    MEMBER_P is true iff this declarator is a member-declarator.  */
10989
10990 static cp_declarator *
10991 cp_parser_declarator (cp_parser* parser,
10992                       cp_parser_declarator_kind dcl_kind,
10993                       int* ctor_dtor_or_conv_p,
10994                       bool* parenthesized_p,
10995                       bool member_p)
10996 {
10997   cp_token *token;
10998   cp_declarator *declarator;
10999   enum tree_code code;
11000   cp_cv_quals cv_quals;
11001   tree class_type;
11002   tree attributes = NULL_TREE;
11003
11004   /* Assume this is not a constructor, destructor, or type-conversion
11005      operator.  */
11006   if (ctor_dtor_or_conv_p)
11007     *ctor_dtor_or_conv_p = 0;
11008
11009   if (cp_parser_allow_gnu_extensions_p (parser))
11010     attributes = cp_parser_attributes_opt (parser);
11011
11012   /* Peek at the next token.  */
11013   token = cp_lexer_peek_token (parser->lexer);
11014
11015   /* Check for the ptr-operator production.  */
11016   cp_parser_parse_tentatively (parser);
11017   /* Parse the ptr-operator.  */
11018   code = cp_parser_ptr_operator (parser,
11019                                  &class_type,
11020                                  &cv_quals);
11021   /* If that worked, then we have a ptr-operator.  */
11022   if (cp_parser_parse_definitely (parser))
11023     {
11024       /* If a ptr-operator was found, then this declarator was not
11025          parenthesized.  */
11026       if (parenthesized_p)
11027         *parenthesized_p = true;
11028       /* The dependent declarator is optional if we are parsing an
11029          abstract-declarator.  */
11030       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11031         cp_parser_parse_tentatively (parser);
11032
11033       /* Parse the dependent declarator.  */
11034       declarator = cp_parser_declarator (parser, dcl_kind,
11035                                          /*ctor_dtor_or_conv_p=*/NULL,
11036                                          /*parenthesized_p=*/NULL,
11037                                          /*member_p=*/false);
11038
11039       /* If we are parsing an abstract-declarator, we must handle the
11040          case where the dependent declarator is absent.  */
11041       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11042           && !cp_parser_parse_definitely (parser))
11043         declarator = NULL;
11044
11045       /* Build the representation of the ptr-operator.  */
11046       if (class_type)
11047         declarator = make_ptrmem_declarator (cv_quals,
11048                                              class_type,
11049                                              declarator);
11050       else if (code == INDIRECT_REF)
11051         declarator = make_pointer_declarator (cv_quals, declarator);
11052       else
11053         declarator = make_reference_declarator (cv_quals, declarator);
11054     }
11055   /* Everything else is a direct-declarator.  */
11056   else
11057     {
11058       if (parenthesized_p)
11059         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11060                                                    CPP_OPEN_PAREN);
11061       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11062                                                 ctor_dtor_or_conv_p,
11063                                                 member_p);
11064     }
11065
11066   if (attributes && declarator != cp_error_declarator)
11067     declarator->attributes = attributes;
11068
11069   return declarator;
11070 }
11071
11072 /* Parse a direct-declarator or direct-abstract-declarator.
11073
11074    direct-declarator:
11075      declarator-id
11076      direct-declarator ( parameter-declaration-clause )
11077        cv-qualifier-seq [opt]
11078        exception-specification [opt]
11079      direct-declarator [ constant-expression [opt] ]
11080      ( declarator )
11081
11082    direct-abstract-declarator:
11083      direct-abstract-declarator [opt]
11084        ( parameter-declaration-clause )
11085        cv-qualifier-seq [opt]
11086        exception-specification [opt]
11087      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11088      ( abstract-declarator )
11089
11090    Returns a representation of the declarator.  DCL_KIND is
11091    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11092    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11093    we are parsing a direct-declarator.  It is
11094    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11095    of ambiguity we prefer an abstract declarator, as per
11096    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11097    cp_parser_declarator.  */
11098
11099 static cp_declarator *
11100 cp_parser_direct_declarator (cp_parser* parser,
11101                              cp_parser_declarator_kind dcl_kind,
11102                              int* ctor_dtor_or_conv_p,
11103                              bool member_p)
11104 {
11105   cp_token *token;
11106   cp_declarator *declarator = NULL;
11107   tree scope = NULL_TREE;
11108   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11109   bool saved_in_declarator_p = parser->in_declarator_p;
11110   bool first = true;
11111   tree pushed_scope = NULL_TREE;
11112
11113   while (true)
11114     {
11115       /* Peek at the next token.  */
11116       token = cp_lexer_peek_token (parser->lexer);
11117       if (token->type == CPP_OPEN_PAREN)
11118         {
11119           /* This is either a parameter-declaration-clause, or a
11120              parenthesized declarator. When we know we are parsing a
11121              named declarator, it must be a parenthesized declarator
11122              if FIRST is true. For instance, `(int)' is a
11123              parameter-declaration-clause, with an omitted
11124              direct-abstract-declarator. But `((*))', is a
11125              parenthesized abstract declarator. Finally, when T is a
11126              template parameter `(T)' is a
11127              parameter-declaration-clause, and not a parenthesized
11128              named declarator.
11129
11130              We first try and parse a parameter-declaration-clause,
11131              and then try a nested declarator (if FIRST is true).
11132
11133              It is not an error for it not to be a
11134              parameter-declaration-clause, even when FIRST is
11135              false. Consider,
11136
11137                int i (int);
11138                int i (3);
11139
11140              The first is the declaration of a function while the
11141              second is a the definition of a variable, including its
11142              initializer.
11143
11144              Having seen only the parenthesis, we cannot know which of
11145              these two alternatives should be selected.  Even more
11146              complex are examples like:
11147
11148                int i (int (a));
11149                int i (int (3));
11150
11151              The former is a function-declaration; the latter is a
11152              variable initialization.
11153
11154              Thus again, we try a parameter-declaration-clause, and if
11155              that fails, we back out and return.  */
11156
11157           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11158             {
11159               cp_parameter_declarator *params;
11160               unsigned saved_num_template_parameter_lists;
11161
11162               /* In a member-declarator, the only valid interpretation
11163                  of a parenthesis is the start of a
11164                  parameter-declaration-clause.  (It is invalid to
11165                  initialize a static data member with a parenthesized
11166                  initializer; only the "=" form of initialization is
11167                  permitted.)  */
11168               if (!member_p)
11169                 cp_parser_parse_tentatively (parser);
11170
11171               /* Consume the `('.  */
11172               cp_lexer_consume_token (parser->lexer);
11173               if (first)
11174                 {
11175                   /* If this is going to be an abstract declarator, we're
11176                      in a declarator and we can't have default args.  */
11177                   parser->default_arg_ok_p = false;
11178                   parser->in_declarator_p = true;
11179                 }
11180
11181               /* Inside the function parameter list, surrounding
11182                  template-parameter-lists do not apply.  */
11183               saved_num_template_parameter_lists
11184                 = parser->num_template_parameter_lists;
11185               parser->num_template_parameter_lists = 0;
11186
11187               /* Parse the parameter-declaration-clause.  */
11188               params = cp_parser_parameter_declaration_clause (parser);
11189
11190               parser->num_template_parameter_lists
11191                 = saved_num_template_parameter_lists;
11192
11193               /* If all went well, parse the cv-qualifier-seq and the
11194                  exception-specification.  */
11195               if (member_p || cp_parser_parse_definitely (parser))
11196                 {
11197                   cp_cv_quals cv_quals;
11198                   tree exception_specification;
11199
11200                   if (ctor_dtor_or_conv_p)
11201                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11202                   first = false;
11203                   /* Consume the `)'.  */
11204                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11205
11206                   /* Parse the cv-qualifier-seq.  */
11207                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11208                   /* And the exception-specification.  */
11209                   exception_specification
11210                     = cp_parser_exception_specification_opt (parser);
11211
11212                   /* Create the function-declarator.  */
11213                   declarator = make_call_declarator (declarator,
11214                                                      params,
11215                                                      cv_quals,
11216                                                      exception_specification);
11217                   /* Any subsequent parameter lists are to do with
11218                      return type, so are not those of the declared
11219                      function.  */
11220                   parser->default_arg_ok_p = false;
11221
11222                   /* Repeat the main loop.  */
11223                   continue;
11224                 }
11225             }
11226
11227           /* If this is the first, we can try a parenthesized
11228              declarator.  */
11229           if (first)
11230             {
11231               bool saved_in_type_id_in_expr_p;
11232
11233               parser->default_arg_ok_p = saved_default_arg_ok_p;
11234               parser->in_declarator_p = saved_in_declarator_p;
11235
11236               /* Consume the `('.  */
11237               cp_lexer_consume_token (parser->lexer);
11238               /* Parse the nested declarator.  */
11239               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11240               parser->in_type_id_in_expr_p = true;
11241               declarator
11242                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11243                                         /*parenthesized_p=*/NULL,
11244                                         member_p);
11245               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11246               first = false;
11247               /* Expect a `)'.  */
11248               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11249                 declarator = cp_error_declarator;
11250               if (declarator == cp_error_declarator)
11251                 break;
11252
11253               goto handle_declarator;
11254             }
11255           /* Otherwise, we must be done.  */
11256           else
11257             break;
11258         }
11259       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11260                && token->type == CPP_OPEN_SQUARE)
11261         {
11262           /* Parse an array-declarator.  */
11263           tree bounds;
11264
11265           if (ctor_dtor_or_conv_p)
11266             *ctor_dtor_or_conv_p = 0;
11267
11268           first = false;
11269           parser->default_arg_ok_p = false;
11270           parser->in_declarator_p = true;
11271           /* Consume the `['.  */
11272           cp_lexer_consume_token (parser->lexer);
11273           /* Peek at the next token.  */
11274           token = cp_lexer_peek_token (parser->lexer);
11275           /* If the next token is `]', then there is no
11276              constant-expression.  */
11277           if (token->type != CPP_CLOSE_SQUARE)
11278             {
11279               bool non_constant_p;
11280
11281               bounds
11282                 = cp_parser_constant_expression (parser,
11283                                                  /*allow_non_constant=*/true,
11284                                                  &non_constant_p);
11285               if (!non_constant_p)
11286                 bounds = fold_non_dependent_expr (bounds);
11287               /* Normally, the array bound must be an integral constant
11288                  expression.  However, as an extension, we allow VLAs
11289                  in function scopes.  */
11290               else if (!at_function_scope_p ())
11291                 {
11292                   error ("array bound is not an integer constant");
11293                   bounds = error_mark_node;
11294                 }
11295             }
11296           else
11297             bounds = NULL_TREE;
11298           /* Look for the closing `]'.  */
11299           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11300             {
11301               declarator = cp_error_declarator;
11302               break;
11303             }
11304
11305           declarator = make_array_declarator (declarator, bounds);
11306         }
11307       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11308         {
11309           tree qualifying_scope;
11310           tree unqualified_name;
11311
11312           /* Parse a declarator-id */
11313           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11314             cp_parser_parse_tentatively (parser);
11315           unqualified_name = cp_parser_declarator_id (parser);
11316           qualifying_scope = parser->scope;
11317           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11318             {
11319               if (!cp_parser_parse_definitely (parser))
11320                 unqualified_name = error_mark_node;
11321               else if (qualifying_scope
11322                        || (TREE_CODE (unqualified_name)
11323                            != IDENTIFIER_NODE))
11324                 {
11325                   cp_parser_error (parser, "expected unqualified-id");
11326                   unqualified_name = error_mark_node;
11327                 }
11328             }
11329
11330           if (unqualified_name == error_mark_node)
11331             {
11332               declarator = cp_error_declarator;
11333               break;
11334             }
11335
11336           if (qualifying_scope && at_namespace_scope_p ()
11337               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11338             {
11339               /* In the declaration of a member of a template class
11340                  outside of the class itself, the SCOPE will sometimes
11341                  be a TYPENAME_TYPE.  For example, given:
11342
11343                  template <typename T>
11344                  int S<T>::R::i = 3;
11345
11346                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11347                  this context, we must resolve S<T>::R to an ordinary
11348                  type, rather than a typename type.
11349
11350                  The reason we normally avoid resolving TYPENAME_TYPEs
11351                  is that a specialization of `S' might render
11352                  `S<T>::R' not a type.  However, if `S' is
11353                  specialized, then this `i' will not be used, so there
11354                  is no harm in resolving the types here.  */
11355               tree type;
11356
11357               /* Resolve the TYPENAME_TYPE.  */
11358               type = resolve_typename_type (qualifying_scope,
11359                                             /*only_current_p=*/false);
11360               /* If that failed, the declarator is invalid.  */
11361               if (type == error_mark_node)
11362                 error ("%<%T::%D%> is not a type",
11363                        TYPE_CONTEXT (qualifying_scope),
11364                        TYPE_IDENTIFIER (qualifying_scope));
11365               qualifying_scope = type;
11366             }
11367
11368           declarator = make_id_declarator (qualifying_scope,
11369                                            unqualified_name);
11370           declarator->id_loc = token->location;
11371           if (unqualified_name)
11372             {
11373               tree class_type;
11374
11375               if (qualifying_scope
11376                   && CLASS_TYPE_P (qualifying_scope))
11377                 class_type = qualifying_scope;
11378               else
11379                 class_type = current_class_type;
11380
11381               if (class_type)
11382                 {
11383                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11384                     declarator->u.id.sfk = sfk_destructor;
11385                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11386                     declarator->u.id.sfk = sfk_conversion;
11387                   else if (/* There's no way to declare a constructor
11388                               for an anonymous type, even if the type
11389                               got a name for linkage purposes.  */
11390                            !TYPE_WAS_ANONYMOUS (class_type)
11391                            && (constructor_name_p (unqualified_name,
11392                                                    class_type)
11393                                || (TREE_CODE (unqualified_name) == TYPE_DECL
11394                                    && (same_type_p
11395                                        (TREE_TYPE (unqualified_name),
11396                                         class_type)))))
11397                     declarator->u.id.sfk = sfk_constructor;
11398
11399                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11400                     *ctor_dtor_or_conv_p = -1;
11401                   if (qualifying_scope
11402                       && TREE_CODE (unqualified_name) == TYPE_DECL
11403                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11404                     {
11405                       error ("invalid use of constructor as a template");
11406                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11407                               "the constructor in a qualified name",
11408                               class_type,
11409                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11410                               class_type, class_type);
11411                     }
11412                 }
11413             }
11414
11415         handle_declarator:;
11416           scope = get_scope_of_declarator (declarator);
11417           if (scope)
11418             /* Any names that appear after the declarator-id for a
11419                member are looked up in the containing scope.  */
11420             pushed_scope = push_scope (scope);
11421           parser->in_declarator_p = true;
11422           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11423               || (declarator && declarator->kind == cdk_id))
11424             /* Default args are only allowed on function
11425                declarations.  */
11426             parser->default_arg_ok_p = saved_default_arg_ok_p;
11427           else
11428             parser->default_arg_ok_p = false;
11429
11430           first = false;
11431         }
11432       /* We're done.  */
11433       else
11434         break;
11435     }
11436
11437   /* For an abstract declarator, we might wind up with nothing at this
11438      point.  That's an error; the declarator is not optional.  */
11439   if (!declarator)
11440     cp_parser_error (parser, "expected declarator");
11441
11442   /* If we entered a scope, we must exit it now.  */
11443   if (pushed_scope)
11444     pop_scope (pushed_scope);
11445
11446   parser->default_arg_ok_p = saved_default_arg_ok_p;
11447   parser->in_declarator_p = saved_in_declarator_p;
11448
11449   return declarator;
11450 }
11451
11452 /* Parse a ptr-operator.
11453
11454    ptr-operator:
11455      * cv-qualifier-seq [opt]
11456      &
11457      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11458
11459    GNU Extension:
11460
11461    ptr-operator:
11462      & cv-qualifier-seq [opt]
11463
11464    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11465    Returns ADDR_EXPR if a reference was used.  In the case of a
11466    pointer-to-member, *TYPE is filled in with the TYPE containing the
11467    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11468    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11469    ERROR_MARK if an error occurred.  */
11470
11471 static enum tree_code
11472 cp_parser_ptr_operator (cp_parser* parser,
11473                         tree* type,
11474                         cp_cv_quals *cv_quals)
11475 {
11476   enum tree_code code = ERROR_MARK;
11477   cp_token *token;
11478
11479   /* Assume that it's not a pointer-to-member.  */
11480   *type = NULL_TREE;
11481   /* And that there are no cv-qualifiers.  */
11482   *cv_quals = TYPE_UNQUALIFIED;
11483
11484   /* Peek at the next token.  */
11485   token = cp_lexer_peek_token (parser->lexer);
11486   /* If it's a `*' or `&' we have a pointer or reference.  */
11487   if (token->type == CPP_MULT || token->type == CPP_AND)
11488     {
11489       /* Remember which ptr-operator we were processing.  */
11490       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11491
11492       /* Consume the `*' or `&'.  */
11493       cp_lexer_consume_token (parser->lexer);
11494
11495       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11496          `&', if we are allowing GNU extensions.  (The only qualifier
11497          that can legally appear after `&' is `restrict', but that is
11498          enforced during semantic analysis.  */
11499       if (code == INDIRECT_REF
11500           || cp_parser_allow_gnu_extensions_p (parser))
11501         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11502     }
11503   else
11504     {
11505       /* Try the pointer-to-member case.  */
11506       cp_parser_parse_tentatively (parser);
11507       /* Look for the optional `::' operator.  */
11508       cp_parser_global_scope_opt (parser,
11509                                   /*current_scope_valid_p=*/false);
11510       /* Look for the nested-name specifier.  */
11511       cp_parser_nested_name_specifier (parser,
11512                                        /*typename_keyword_p=*/false,
11513                                        /*check_dependency_p=*/true,
11514                                        /*type_p=*/false,
11515                                        /*is_declaration=*/false);
11516       /* If we found it, and the next token is a `*', then we are
11517          indeed looking at a pointer-to-member operator.  */
11518       if (!cp_parser_error_occurred (parser)
11519           && cp_parser_require (parser, CPP_MULT, "`*'"))
11520         {
11521           /* The type of which the member is a member is given by the
11522              current SCOPE.  */
11523           *type = parser->scope;
11524           /* The next name will not be qualified.  */
11525           parser->scope = NULL_TREE;
11526           parser->qualifying_scope = NULL_TREE;
11527           parser->object_scope = NULL_TREE;
11528           /* Indicate that the `*' operator was used.  */
11529           code = INDIRECT_REF;
11530           /* Look for the optional cv-qualifier-seq.  */
11531           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11532         }
11533       /* If that didn't work we don't have a ptr-operator.  */
11534       if (!cp_parser_parse_definitely (parser))
11535         cp_parser_error (parser, "expected ptr-operator");
11536     }
11537
11538   return code;
11539 }
11540
11541 /* Parse an (optional) cv-qualifier-seq.
11542
11543    cv-qualifier-seq:
11544      cv-qualifier cv-qualifier-seq [opt]
11545
11546    cv-qualifier:
11547      const
11548      volatile
11549
11550    GNU Extension:
11551
11552    cv-qualifier:
11553      __restrict__
11554
11555    Returns a bitmask representing the cv-qualifiers.  */
11556
11557 static cp_cv_quals
11558 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11559 {
11560   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11561
11562   while (true)
11563     {
11564       cp_token *token;
11565       cp_cv_quals cv_qualifier;
11566
11567       /* Peek at the next token.  */
11568       token = cp_lexer_peek_token (parser->lexer);
11569       /* See if it's a cv-qualifier.  */
11570       switch (token->keyword)
11571         {
11572         case RID_CONST:
11573           cv_qualifier = TYPE_QUAL_CONST;
11574           break;
11575
11576         case RID_VOLATILE:
11577           cv_qualifier = TYPE_QUAL_VOLATILE;
11578           break;
11579
11580         case RID_RESTRICT:
11581           cv_qualifier = TYPE_QUAL_RESTRICT;
11582           break;
11583
11584         default:
11585           cv_qualifier = TYPE_UNQUALIFIED;
11586           break;
11587         }
11588
11589       if (!cv_qualifier)
11590         break;
11591
11592       if (cv_quals & cv_qualifier)
11593         {
11594           error ("duplicate cv-qualifier");
11595           cp_lexer_purge_token (parser->lexer);
11596         }
11597       else
11598         {
11599           cp_lexer_consume_token (parser->lexer);
11600           cv_quals |= cv_qualifier;
11601         }
11602     }
11603
11604   return cv_quals;
11605 }
11606
11607 /* Parse a declarator-id.
11608
11609    declarator-id:
11610      id-expression
11611      :: [opt] nested-name-specifier [opt] type-name
11612
11613    In the `id-expression' case, the value returned is as for
11614    cp_parser_id_expression if the id-expression was an unqualified-id.
11615    If the id-expression was a qualified-id, then a SCOPE_REF is
11616    returned.  The first operand is the scope (either a NAMESPACE_DECL
11617    or TREE_TYPE), but the second is still just a representation of an
11618    unqualified-id.  */
11619
11620 static tree
11621 cp_parser_declarator_id (cp_parser* parser)
11622 {
11623   /* The expression must be an id-expression.  Assume that qualified
11624      names are the names of types so that:
11625
11626        template <class T>
11627        int S<T>::R::i = 3;
11628
11629      will work; we must treat `S<T>::R' as the name of a type.
11630      Similarly, assume that qualified names are templates, where
11631      required, so that:
11632
11633        template <class T>
11634        int S<T>::R<T>::i = 3;
11635
11636      will work, too.  */
11637   return cp_parser_id_expression (parser,
11638                                   /*template_keyword_p=*/false,
11639                                   /*check_dependency_p=*/false,
11640                                   /*template_p=*/NULL,
11641                                   /*declarator_p=*/true);
11642 }
11643
11644 /* Parse a type-id.
11645
11646    type-id:
11647      type-specifier-seq abstract-declarator [opt]
11648
11649    Returns the TYPE specified.  */
11650
11651 static tree
11652 cp_parser_type_id (cp_parser* parser)
11653 {
11654   cp_decl_specifier_seq type_specifier_seq;
11655   cp_declarator *abstract_declarator;
11656
11657   /* Parse the type-specifier-seq.  */
11658   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11659                                 &type_specifier_seq);
11660   if (type_specifier_seq.type == error_mark_node)
11661     return error_mark_node;
11662
11663   /* There might or might not be an abstract declarator.  */
11664   cp_parser_parse_tentatively (parser);
11665   /* Look for the declarator.  */
11666   abstract_declarator
11667     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11668                             /*parenthesized_p=*/NULL,
11669                             /*member_p=*/false);
11670   /* Check to see if there really was a declarator.  */
11671   if (!cp_parser_parse_definitely (parser))
11672     abstract_declarator = NULL;
11673
11674   return groktypename (&type_specifier_seq, abstract_declarator);
11675 }
11676
11677 /* Parse a type-specifier-seq.
11678
11679    type-specifier-seq:
11680      type-specifier type-specifier-seq [opt]
11681
11682    GNU extension:
11683
11684    type-specifier-seq:
11685      attributes type-specifier-seq [opt]
11686
11687    If IS_CONDITION is true, we are at the start of a "condition",
11688    e.g., we've just seen "if (".
11689
11690    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11691
11692 static void
11693 cp_parser_type_specifier_seq (cp_parser* parser,
11694                               bool is_condition,
11695                               cp_decl_specifier_seq *type_specifier_seq)
11696 {
11697   bool seen_type_specifier = false;
11698   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11699
11700   /* Clear the TYPE_SPECIFIER_SEQ.  */
11701   clear_decl_specs (type_specifier_seq);
11702
11703   /* Parse the type-specifiers and attributes.  */
11704   while (true)
11705     {
11706       tree type_specifier;
11707       bool is_cv_qualifier;
11708
11709       /* Check for attributes first.  */
11710       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11711         {
11712           type_specifier_seq->attributes =
11713             chainon (type_specifier_seq->attributes,
11714                      cp_parser_attributes_opt (parser));
11715           continue;
11716         }
11717
11718       /* Look for the type-specifier.  */
11719       type_specifier = cp_parser_type_specifier (parser,
11720                                                  flags,
11721                                                  type_specifier_seq,
11722                                                  /*is_declaration=*/false,
11723                                                  NULL,
11724                                                  &is_cv_qualifier);
11725       if (!type_specifier)
11726         {
11727           /* If the first type-specifier could not be found, this is not a
11728              type-specifier-seq at all.  */
11729           if (!seen_type_specifier)
11730             {
11731               cp_parser_error (parser, "expected type-specifier");
11732               type_specifier_seq->type = error_mark_node;
11733               return;
11734             }
11735           /* If subsequent type-specifiers could not be found, the
11736              type-specifier-seq is complete.  */
11737           break;
11738         }
11739
11740       seen_type_specifier = true;
11741       /* The standard says that a condition can be:
11742
11743             type-specifier-seq declarator = assignment-expression
11744
11745          However, given:
11746
11747            struct S {};
11748            if (int S = ...)
11749
11750          we should treat the "S" as a declarator, not as a
11751          type-specifier.  The standard doesn't say that explicitly for
11752          type-specifier-seq, but it does say that for
11753          decl-specifier-seq in an ordinary declaration.  Perhaps it
11754          would be clearer just to allow a decl-specifier-seq here, and
11755          then add a semantic restriction that if any decl-specifiers
11756          that are not type-specifiers appear, the program is invalid.  */
11757       if (is_condition && !is_cv_qualifier)
11758         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11759     }
11760
11761   return;
11762 }
11763
11764 /* Parse a parameter-declaration-clause.
11765
11766    parameter-declaration-clause:
11767      parameter-declaration-list [opt] ... [opt]
11768      parameter-declaration-list , ...
11769
11770    Returns a representation for the parameter declarations.  A return
11771    value of NULL indicates a parameter-declaration-clause consisting
11772    only of an ellipsis.  */
11773
11774 static cp_parameter_declarator *
11775 cp_parser_parameter_declaration_clause (cp_parser* parser)
11776 {
11777   cp_parameter_declarator *parameters;
11778   cp_token *token;
11779   bool ellipsis_p;
11780   bool is_error;
11781
11782   /* Peek at the next token.  */
11783   token = cp_lexer_peek_token (parser->lexer);
11784   /* Check for trivial parameter-declaration-clauses.  */
11785   if (token->type == CPP_ELLIPSIS)
11786     {
11787       /* Consume the `...' token.  */
11788       cp_lexer_consume_token (parser->lexer);
11789       return NULL;
11790     }
11791   else if (token->type == CPP_CLOSE_PAREN)
11792     /* There are no parameters.  */
11793     {
11794 #ifndef NO_IMPLICIT_EXTERN_C
11795       if (in_system_header && current_class_type == NULL
11796           && current_lang_name == lang_name_c)
11797         return NULL;
11798       else
11799 #endif
11800         return no_parameters;
11801     }
11802   /* Check for `(void)', too, which is a special case.  */
11803   else if (token->keyword == RID_VOID
11804            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11805                == CPP_CLOSE_PAREN))
11806     {
11807       /* Consume the `void' token.  */
11808       cp_lexer_consume_token (parser->lexer);
11809       /* There are no parameters.  */
11810       return no_parameters;
11811     }
11812
11813   /* Parse the parameter-declaration-list.  */
11814   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11815   /* If a parse error occurred while parsing the
11816      parameter-declaration-list, then the entire
11817      parameter-declaration-clause is erroneous.  */
11818   if (is_error)
11819     return NULL;
11820
11821   /* Peek at the next token.  */
11822   token = cp_lexer_peek_token (parser->lexer);
11823   /* If it's a `,', the clause should terminate with an ellipsis.  */
11824   if (token->type == CPP_COMMA)
11825     {
11826       /* Consume the `,'.  */
11827       cp_lexer_consume_token (parser->lexer);
11828       /* Expect an ellipsis.  */
11829       ellipsis_p
11830         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11831     }
11832   /* It might also be `...' if the optional trailing `,' was
11833      omitted.  */
11834   else if (token->type == CPP_ELLIPSIS)
11835     {
11836       /* Consume the `...' token.  */
11837       cp_lexer_consume_token (parser->lexer);
11838       /* And remember that we saw it.  */
11839       ellipsis_p = true;
11840     }
11841   else
11842     ellipsis_p = false;
11843
11844   /* Finish the parameter list.  */
11845   if (parameters && ellipsis_p)
11846     parameters->ellipsis_p = true;
11847
11848   return parameters;
11849 }
11850
11851 /* Parse a parameter-declaration-list.
11852
11853    parameter-declaration-list:
11854      parameter-declaration
11855      parameter-declaration-list , parameter-declaration
11856
11857    Returns a representation of the parameter-declaration-list, as for
11858    cp_parser_parameter_declaration_clause.  However, the
11859    `void_list_node' is never appended to the list.  Upon return,
11860    *IS_ERROR will be true iff an error occurred.  */
11861
11862 static cp_parameter_declarator *
11863 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11864 {
11865   cp_parameter_declarator *parameters = NULL;
11866   cp_parameter_declarator **tail = &parameters;
11867
11868   /* Assume all will go well.  */
11869   *is_error = false;
11870
11871   /* Look for more parameters.  */
11872   while (true)
11873     {
11874       cp_parameter_declarator *parameter;
11875       bool parenthesized_p;
11876       /* Parse the parameter.  */
11877       parameter
11878         = cp_parser_parameter_declaration (parser,
11879                                            /*template_parm_p=*/false,
11880                                            &parenthesized_p);
11881
11882       /* If a parse error occurred parsing the parameter declaration,
11883          then the entire parameter-declaration-list is erroneous.  */
11884       if (!parameter)
11885         {
11886           *is_error = true;
11887           parameters = NULL;
11888           break;
11889         }
11890       /* Add the new parameter to the list.  */
11891       *tail = parameter;
11892       tail = &parameter->next;
11893
11894       /* Peek at the next token.  */
11895       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11896           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11897           /* These are for Objective-C++ */
11898           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11899           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11900         /* The parameter-declaration-list is complete.  */
11901         break;
11902       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11903         {
11904           cp_token *token;
11905
11906           /* Peek at the next token.  */
11907           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11908           /* If it's an ellipsis, then the list is complete.  */
11909           if (token->type == CPP_ELLIPSIS)
11910             break;
11911           /* Otherwise, there must be more parameters.  Consume the
11912              `,'.  */
11913           cp_lexer_consume_token (parser->lexer);
11914           /* When parsing something like:
11915
11916                 int i(float f, double d)
11917
11918              we can tell after seeing the declaration for "f" that we
11919              are not looking at an initialization of a variable "i",
11920              but rather at the declaration of a function "i".
11921
11922              Due to the fact that the parsing of template arguments
11923              (as specified to a template-id) requires backtracking we
11924              cannot use this technique when inside a template argument
11925              list.  */
11926           if (!parser->in_template_argument_list_p
11927               && !parser->in_type_id_in_expr_p
11928               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11929               /* However, a parameter-declaration of the form
11930                  "foat(f)" (which is a valid declaration of a
11931                  parameter "f") can also be interpreted as an
11932                  expression (the conversion of "f" to "float").  */
11933               && !parenthesized_p)
11934             cp_parser_commit_to_tentative_parse (parser);
11935         }
11936       else
11937         {
11938           cp_parser_error (parser, "expected %<,%> or %<...%>");
11939           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11940             cp_parser_skip_to_closing_parenthesis (parser,
11941                                                    /*recovering=*/true,
11942                                                    /*or_comma=*/false,
11943                                                    /*consume_paren=*/false);
11944           break;
11945         }
11946     }
11947
11948   return parameters;
11949 }
11950
11951 /* Parse a parameter declaration.
11952
11953    parameter-declaration:
11954      decl-specifier-seq declarator
11955      decl-specifier-seq declarator = assignment-expression
11956      decl-specifier-seq abstract-declarator [opt]
11957      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11958
11959    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11960    declares a template parameter.  (In that case, a non-nested `>'
11961    token encountered during the parsing of the assignment-expression
11962    is not interpreted as a greater-than operator.)
11963
11964    Returns a representation of the parameter, or NULL if an error
11965    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11966    true iff the declarator is of the form "(p)".  */
11967
11968 static cp_parameter_declarator *
11969 cp_parser_parameter_declaration (cp_parser *parser,
11970                                  bool template_parm_p,
11971                                  bool *parenthesized_p)
11972 {
11973   int declares_class_or_enum;
11974   bool greater_than_is_operator_p;
11975   cp_decl_specifier_seq decl_specifiers;
11976   cp_declarator *declarator;
11977   tree default_argument;
11978   cp_token *token;
11979   const char *saved_message;
11980
11981   /* In a template parameter, `>' is not an operator.
11982
11983      [temp.param]
11984
11985      When parsing a default template-argument for a non-type
11986      template-parameter, the first non-nested `>' is taken as the end
11987      of the template parameter-list rather than a greater-than
11988      operator.  */
11989   greater_than_is_operator_p = !template_parm_p;
11990
11991   /* Type definitions may not appear in parameter types.  */
11992   saved_message = parser->type_definition_forbidden_message;
11993   parser->type_definition_forbidden_message
11994     = "types may not be defined in parameter types";
11995
11996   /* Parse the declaration-specifiers.  */
11997   cp_parser_decl_specifier_seq (parser,
11998                                 CP_PARSER_FLAGS_NONE,
11999                                 &decl_specifiers,
12000                                 &declares_class_or_enum);
12001   /* If an error occurred, there's no reason to attempt to parse the
12002      rest of the declaration.  */
12003   if (cp_parser_error_occurred (parser))
12004     {
12005       parser->type_definition_forbidden_message = saved_message;
12006       return NULL;
12007     }
12008
12009   /* Peek at the next token.  */
12010   token = cp_lexer_peek_token (parser->lexer);
12011   /* If the next token is a `)', `,', `=', `>', or `...', then there
12012      is no declarator.  */
12013   if (token->type == CPP_CLOSE_PAREN
12014       || token->type == CPP_COMMA
12015       || token->type == CPP_EQ
12016       || token->type == CPP_ELLIPSIS
12017       || token->type == CPP_GREATER)
12018     {
12019       declarator = NULL;
12020       if (parenthesized_p)
12021         *parenthesized_p = false;
12022     }
12023   /* Otherwise, there should be a declarator.  */
12024   else
12025     {
12026       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12027       parser->default_arg_ok_p = false;
12028
12029       /* After seeing a decl-specifier-seq, if the next token is not a
12030          "(", there is no possibility that the code is a valid
12031          expression.  Therefore, if parsing tentatively, we commit at
12032          this point.  */
12033       if (!parser->in_template_argument_list_p
12034           /* In an expression context, having seen:
12035
12036                (int((char ...
12037
12038              we cannot be sure whether we are looking at a
12039              function-type (taking a "char" as a parameter) or a cast
12040              of some object of type "char" to "int".  */
12041           && !parser->in_type_id_in_expr_p
12042           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12043           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12044         cp_parser_commit_to_tentative_parse (parser);
12045       /* Parse the declarator.  */
12046       declarator = cp_parser_declarator (parser,
12047                                          CP_PARSER_DECLARATOR_EITHER,
12048                                          /*ctor_dtor_or_conv_p=*/NULL,
12049                                          parenthesized_p,
12050                                          /*member_p=*/false);
12051       parser->default_arg_ok_p = saved_default_arg_ok_p;
12052       /* After the declarator, allow more attributes.  */
12053       decl_specifiers.attributes
12054         = chainon (decl_specifiers.attributes,
12055                    cp_parser_attributes_opt (parser));
12056     }
12057
12058   /* The restriction on defining new types applies only to the type
12059      of the parameter, not to the default argument.  */
12060   parser->type_definition_forbidden_message = saved_message;
12061
12062   /* If the next token is `=', then process a default argument.  */
12063   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12064     {
12065       bool saved_greater_than_is_operator_p;
12066       /* Consume the `='.  */
12067       cp_lexer_consume_token (parser->lexer);
12068
12069       /* If we are defining a class, then the tokens that make up the
12070          default argument must be saved and processed later.  */
12071       if (!template_parm_p && at_class_scope_p ()
12072           && TYPE_BEING_DEFINED (current_class_type))
12073         {
12074           unsigned depth = 0;
12075           cp_token *first_token;
12076           cp_token *token;
12077
12078           /* Add tokens until we have processed the entire default
12079              argument.  We add the range [first_token, token).  */
12080           first_token = cp_lexer_peek_token (parser->lexer);
12081           while (true)
12082             {
12083               bool done = false;
12084
12085               /* Peek at the next token.  */
12086               token = cp_lexer_peek_token (parser->lexer);
12087               /* What we do depends on what token we have.  */
12088               switch (token->type)
12089                 {
12090                   /* In valid code, a default argument must be
12091                      immediately followed by a `,' `)', or `...'.  */
12092                 case CPP_COMMA:
12093                 case CPP_CLOSE_PAREN:
12094                 case CPP_ELLIPSIS:
12095                   /* If we run into a non-nested `;', `}', or `]',
12096                      then the code is invalid -- but the default
12097                      argument is certainly over.  */
12098                 case CPP_SEMICOLON:
12099                 case CPP_CLOSE_BRACE:
12100                 case CPP_CLOSE_SQUARE:
12101                   if (depth == 0)
12102                     done = true;
12103                   /* Update DEPTH, if necessary.  */
12104                   else if (token->type == CPP_CLOSE_PAREN
12105                            || token->type == CPP_CLOSE_BRACE
12106                            || token->type == CPP_CLOSE_SQUARE)
12107                     --depth;
12108                   break;
12109
12110                 case CPP_OPEN_PAREN:
12111                 case CPP_OPEN_SQUARE:
12112                 case CPP_OPEN_BRACE:
12113                   ++depth;
12114                   break;
12115
12116                 case CPP_GREATER:
12117                   /* If we see a non-nested `>', and `>' is not an
12118                      operator, then it marks the end of the default
12119                      argument.  */
12120                   if (!depth && !greater_than_is_operator_p)
12121                     done = true;
12122                   break;
12123
12124                   /* If we run out of tokens, issue an error message.  */
12125                 case CPP_EOF:
12126                   error ("file ends in default argument");
12127                   done = true;
12128                   break;
12129
12130                 case CPP_NAME:
12131                 case CPP_SCOPE:
12132                   /* In these cases, we should look for template-ids.
12133                      For example, if the default argument is
12134                      `X<int, double>()', we need to do name lookup to
12135                      figure out whether or not `X' is a template; if
12136                      so, the `,' does not end the default argument.
12137
12138                      That is not yet done.  */
12139                   break;
12140
12141                 default:
12142                   break;
12143                 }
12144
12145               /* If we've reached the end, stop.  */
12146               if (done)
12147                 break;
12148
12149               /* Add the token to the token block.  */
12150               token = cp_lexer_consume_token (parser->lexer);
12151             }
12152
12153           /* Create a DEFAULT_ARG to represented the unparsed default
12154              argument.  */
12155           default_argument = make_node (DEFAULT_ARG);
12156           DEFARG_TOKENS (default_argument)
12157             = cp_token_cache_new (first_token, token);
12158           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12159         }
12160       /* Outside of a class definition, we can just parse the
12161          assignment-expression.  */
12162       else
12163         {
12164           bool saved_local_variables_forbidden_p;
12165
12166           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12167              set correctly.  */
12168           saved_greater_than_is_operator_p
12169             = parser->greater_than_is_operator_p;
12170           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12171           /* Local variable names (and the `this' keyword) may not
12172              appear in a default argument.  */
12173           saved_local_variables_forbidden_p
12174             = parser->local_variables_forbidden_p;
12175           parser->local_variables_forbidden_p = true;
12176           /* Parse the assignment-expression.  */
12177           default_argument
12178             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12179           /* Restore saved state.  */
12180           parser->greater_than_is_operator_p
12181             = saved_greater_than_is_operator_p;
12182           parser->local_variables_forbidden_p
12183             = saved_local_variables_forbidden_p;
12184         }
12185       if (!parser->default_arg_ok_p)
12186         {
12187           if (!flag_pedantic_errors)
12188             warning (0, "deprecated use of default argument for parameter of non-function");
12189           else
12190             {
12191               error ("default arguments are only permitted for function parameters");
12192               default_argument = NULL_TREE;
12193             }
12194         }
12195     }
12196   else
12197     default_argument = NULL_TREE;
12198
12199   return make_parameter_declarator (&decl_specifiers,
12200                                     declarator,
12201                                     default_argument);
12202 }
12203
12204 /* Parse a function-body.
12205
12206    function-body:
12207      compound_statement  */
12208
12209 static void
12210 cp_parser_function_body (cp_parser *parser)
12211 {
12212   cp_parser_compound_statement (parser, NULL, false);
12213 }
12214
12215 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12216    true if a ctor-initializer was present.  */
12217
12218 static bool
12219 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12220 {
12221   tree body;
12222   bool ctor_initializer_p;
12223
12224   /* Begin the function body.  */
12225   body = begin_function_body ();
12226   /* Parse the optional ctor-initializer.  */
12227   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12228   /* Parse the function-body.  */
12229   cp_parser_function_body (parser);
12230   /* Finish the function body.  */
12231   finish_function_body (body);
12232
12233   return ctor_initializer_p;
12234 }
12235
12236 /* Parse an initializer.
12237
12238    initializer:
12239      = initializer-clause
12240      ( expression-list )
12241
12242    Returns a expression representing the initializer.  If no
12243    initializer is present, NULL_TREE is returned.
12244
12245    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12246    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12247    set to FALSE if there is no initializer present.  If there is an
12248    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12249    is set to true; otherwise it is set to false.  */
12250
12251 static tree
12252 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12253                        bool* non_constant_p)
12254 {
12255   cp_token *token;
12256   tree init;
12257
12258   /* Peek at the next token.  */
12259   token = cp_lexer_peek_token (parser->lexer);
12260
12261   /* Let our caller know whether or not this initializer was
12262      parenthesized.  */
12263   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12264   /* Assume that the initializer is constant.  */
12265   *non_constant_p = false;
12266
12267   if (token->type == CPP_EQ)
12268     {
12269       /* Consume the `='.  */
12270       cp_lexer_consume_token (parser->lexer);
12271       /* Parse the initializer-clause.  */
12272       init = cp_parser_initializer_clause (parser, non_constant_p);
12273     }
12274   else if (token->type == CPP_OPEN_PAREN)
12275     init = cp_parser_parenthesized_expression_list (parser, false,
12276                                                     /*cast_p=*/false,
12277                                                     non_constant_p);
12278   else
12279     {
12280       /* Anything else is an error.  */
12281       cp_parser_error (parser, "expected initializer");
12282       init = error_mark_node;
12283     }
12284
12285   return init;
12286 }
12287
12288 /* Parse an initializer-clause.
12289
12290    initializer-clause:
12291      assignment-expression
12292      { initializer-list , [opt] }
12293      { }
12294
12295    Returns an expression representing the initializer.
12296
12297    If the `assignment-expression' production is used the value
12298    returned is simply a representation for the expression.
12299
12300    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12301    the elements of the initializer-list (or NULL_TREE, if the last
12302    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12303    NULL_TREE.  There is no way to detect whether or not the optional
12304    trailing `,' was provided.  NON_CONSTANT_P is as for
12305    cp_parser_initializer.  */
12306
12307 static tree
12308 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12309 {
12310   tree initializer;
12311
12312   /* Assume the expression is constant.  */
12313   *non_constant_p = false;
12314
12315   /* If it is not a `{', then we are looking at an
12316      assignment-expression.  */
12317   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12318     {
12319       initializer
12320         = cp_parser_constant_expression (parser,
12321                                         /*allow_non_constant_p=*/true,
12322                                         non_constant_p);
12323       if (!*non_constant_p)
12324         initializer = fold_non_dependent_expr (initializer);
12325     }
12326   else
12327     {
12328       /* Consume the `{' token.  */
12329       cp_lexer_consume_token (parser->lexer);
12330       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12331       initializer = make_node (CONSTRUCTOR);
12332       /* If it's not a `}', then there is a non-trivial initializer.  */
12333       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12334         {
12335           /* Parse the initializer list.  */
12336           CONSTRUCTOR_ELTS (initializer)
12337             = cp_parser_initializer_list (parser, non_constant_p);
12338           /* A trailing `,' token is allowed.  */
12339           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12340             cp_lexer_consume_token (parser->lexer);
12341         }
12342       /* Now, there should be a trailing `}'.  */
12343       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12344     }
12345
12346   return initializer;
12347 }
12348
12349 /* Parse an initializer-list.
12350
12351    initializer-list:
12352      initializer-clause
12353      initializer-list , initializer-clause
12354
12355    GNU Extension:
12356
12357    initializer-list:
12358      identifier : initializer-clause
12359      initializer-list, identifier : initializer-clause
12360
12361    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12362    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12363    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12364    as for cp_parser_initializer.  */
12365
12366 static tree
12367 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12368 {
12369   tree initializers = NULL_TREE;
12370
12371   /* Assume all of the expressions are constant.  */
12372   *non_constant_p = false;
12373
12374   /* Parse the rest of the list.  */
12375   while (true)
12376     {
12377       cp_token *token;
12378       tree identifier;
12379       tree initializer;
12380       bool clause_non_constant_p;
12381
12382       /* If the next token is an identifier and the following one is a
12383          colon, we are looking at the GNU designated-initializer
12384          syntax.  */
12385       if (cp_parser_allow_gnu_extensions_p (parser)
12386           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12387           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12388         {
12389           /* Consume the identifier.  */
12390           identifier = cp_lexer_consume_token (parser->lexer)->value;
12391           /* Consume the `:'.  */
12392           cp_lexer_consume_token (parser->lexer);
12393         }
12394       else
12395         identifier = NULL_TREE;
12396
12397       /* Parse the initializer.  */
12398       initializer = cp_parser_initializer_clause (parser,
12399                                                   &clause_non_constant_p);
12400       /* If any clause is non-constant, so is the entire initializer.  */
12401       if (clause_non_constant_p)
12402         *non_constant_p = true;
12403       /* Add it to the list.  */
12404       initializers = tree_cons (identifier, initializer, initializers);
12405
12406       /* If the next token is not a comma, we have reached the end of
12407          the list.  */
12408       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12409         break;
12410
12411       /* Peek at the next token.  */
12412       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12413       /* If the next token is a `}', then we're still done.  An
12414          initializer-clause can have a trailing `,' after the
12415          initializer-list and before the closing `}'.  */
12416       if (token->type == CPP_CLOSE_BRACE)
12417         break;
12418
12419       /* Consume the `,' token.  */
12420       cp_lexer_consume_token (parser->lexer);
12421     }
12422
12423   /* The initializers were built up in reverse order, so we need to
12424      reverse them now.  */
12425   return nreverse (initializers);
12426 }
12427
12428 /* Classes [gram.class] */
12429
12430 /* Parse a class-name.
12431
12432    class-name:
12433      identifier
12434      template-id
12435
12436    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12437    to indicate that names looked up in dependent types should be
12438    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12439    keyword has been used to indicate that the name that appears next
12440    is a template.  TAG_TYPE indicates the explicit tag given before
12441    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12442    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12443    is the class being defined in a class-head.
12444
12445    Returns the TYPE_DECL representing the class.  */
12446
12447 static tree
12448 cp_parser_class_name (cp_parser *parser,
12449                       bool typename_keyword_p,
12450                       bool template_keyword_p,
12451                       enum tag_types tag_type,
12452                       bool check_dependency_p,
12453                       bool class_head_p,
12454                       bool is_declaration)
12455 {
12456   tree decl;
12457   tree scope;
12458   bool typename_p;
12459   cp_token *token;
12460
12461   /* All class-names start with an identifier.  */
12462   token = cp_lexer_peek_token (parser->lexer);
12463   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12464     {
12465       cp_parser_error (parser, "expected class-name");
12466       return error_mark_node;
12467     }
12468
12469   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12470      to a template-id, so we save it here.  */
12471   scope = parser->scope;
12472   if (scope == error_mark_node)
12473     return error_mark_node;
12474
12475   /* Any name names a type if we're following the `typename' keyword
12476      in a qualified name where the enclosing scope is type-dependent.  */
12477   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12478                 && dependent_type_p (scope));
12479   /* Handle the common case (an identifier, but not a template-id)
12480      efficiently.  */
12481   if (token->type == CPP_NAME
12482       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12483     {
12484       tree identifier;
12485
12486       /* Look for the identifier.  */
12487       identifier = cp_parser_identifier (parser);
12488       /* If the next token isn't an identifier, we are certainly not
12489          looking at a class-name.  */
12490       if (identifier == error_mark_node)
12491         decl = error_mark_node;
12492       /* If we know this is a type-name, there's no need to look it
12493          up.  */
12494       else if (typename_p)
12495         decl = identifier;
12496       else
12497         {
12498           /* If the next token is a `::', then the name must be a type
12499              name.
12500
12501              [basic.lookup.qual]
12502
12503              During the lookup for a name preceding the :: scope
12504              resolution operator, object, function, and enumerator
12505              names are ignored.  */
12506           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12507             tag_type = typename_type;
12508           /* Look up the name.  */
12509           decl = cp_parser_lookup_name (parser, identifier,
12510                                         tag_type,
12511                                         /*is_template=*/false,
12512                                         /*is_namespace=*/false,
12513                                         check_dependency_p,
12514                                         /*ambiguous_p=*/NULL);
12515         }
12516     }
12517   else
12518     {
12519       /* Try a template-id.  */
12520       decl = cp_parser_template_id (parser, template_keyword_p,
12521                                     check_dependency_p,
12522                                     is_declaration);
12523       if (decl == error_mark_node)
12524         return error_mark_node;
12525     }
12526
12527   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12528
12529   /* If this is a typename, create a TYPENAME_TYPE.  */
12530   if (typename_p && decl != error_mark_node)
12531     {
12532       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12533       if (decl != error_mark_node)
12534         decl = TYPE_NAME (decl);
12535     }
12536
12537   /* Check to see that it is really the name of a class.  */
12538   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12539       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12540       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12541     /* Situations like this:
12542
12543          template <typename T> struct A {
12544            typename T::template X<int>::I i;
12545          };
12546
12547        are problematic.  Is `T::template X<int>' a class-name?  The
12548        standard does not seem to be definitive, but there is no other
12549        valid interpretation of the following `::'.  Therefore, those
12550        names are considered class-names.  */
12551     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12552   else if (decl == error_mark_node
12553            || TREE_CODE (decl) != TYPE_DECL
12554            || TREE_TYPE (decl) == error_mark_node
12555            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12556     {
12557       cp_parser_error (parser, "expected class-name");
12558       return error_mark_node;
12559     }
12560
12561   return decl;
12562 }
12563
12564 /* Parse a class-specifier.
12565
12566    class-specifier:
12567      class-head { member-specification [opt] }
12568
12569    Returns the TREE_TYPE representing the class.  */
12570
12571 static tree
12572 cp_parser_class_specifier (cp_parser* parser)
12573 {
12574   cp_token *token;
12575   tree type;
12576   tree attributes = NULL_TREE;
12577   int has_trailing_semicolon;
12578   bool nested_name_specifier_p;
12579   unsigned saved_num_template_parameter_lists;
12580   tree old_scope = NULL_TREE;
12581   tree scope = NULL_TREE;
12582
12583   push_deferring_access_checks (dk_no_deferred);
12584
12585   /* Parse the class-head.  */
12586   type = cp_parser_class_head (parser,
12587                                &nested_name_specifier_p,
12588                                &attributes);
12589   /* If the class-head was a semantic disaster, skip the entire body
12590      of the class.  */
12591   if (!type)
12592     {
12593       cp_parser_skip_to_end_of_block_or_statement (parser);
12594       pop_deferring_access_checks ();
12595       return error_mark_node;
12596     }
12597
12598   /* Look for the `{'.  */
12599   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12600     {
12601       pop_deferring_access_checks ();
12602       return error_mark_node;
12603     }
12604
12605   /* Issue an error message if type-definitions are forbidden here.  */
12606   cp_parser_check_type_definition (parser);
12607   /* Remember that we are defining one more class.  */
12608   ++parser->num_classes_being_defined;
12609   /* Inside the class, surrounding template-parameter-lists do not
12610      apply.  */
12611   saved_num_template_parameter_lists
12612     = parser->num_template_parameter_lists;
12613   parser->num_template_parameter_lists = 0;
12614
12615   /* Start the class.  */
12616   if (nested_name_specifier_p)
12617     {
12618       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12619       old_scope = push_inner_scope (scope);
12620     }
12621   type = begin_class_definition (type);
12622
12623   if (type == error_mark_node)
12624     /* If the type is erroneous, skip the entire body of the class.  */
12625     cp_parser_skip_to_closing_brace (parser);
12626   else
12627     /* Parse the member-specification.  */
12628     cp_parser_member_specification_opt (parser);
12629
12630   /* Look for the trailing `}'.  */
12631   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12632   /* We get better error messages by noticing a common problem: a
12633      missing trailing `;'.  */
12634   token = cp_lexer_peek_token (parser->lexer);
12635   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12636   /* Look for trailing attributes to apply to this class.  */
12637   if (cp_parser_allow_gnu_extensions_p (parser))
12638     {
12639       tree sub_attr = cp_parser_attributes_opt (parser);
12640       attributes = chainon (attributes, sub_attr);
12641     }
12642   if (type != error_mark_node)
12643     type = finish_struct (type, attributes);
12644   if (nested_name_specifier_p)
12645     pop_inner_scope (old_scope, scope);
12646   /* If this class is not itself within the scope of another class,
12647      then we need to parse the bodies of all of the queued function
12648      definitions.  Note that the queued functions defined in a class
12649      are not always processed immediately following the
12650      class-specifier for that class.  Consider:
12651
12652        struct A {
12653          struct B { void f() { sizeof (A); } };
12654        };
12655
12656      If `f' were processed before the processing of `A' were
12657      completed, there would be no way to compute the size of `A'.
12658      Note that the nesting we are interested in here is lexical --
12659      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12660      for:
12661
12662        struct A { struct B; };
12663        struct A::B { void f() { } };
12664
12665      there is no need to delay the parsing of `A::B::f'.  */
12666   if (--parser->num_classes_being_defined == 0)
12667     {
12668       tree queue_entry;
12669       tree fn;
12670       tree class_type = NULL_TREE;
12671       tree pushed_scope = NULL_TREE;
12672
12673       /* In a first pass, parse default arguments to the functions.
12674          Then, in a second pass, parse the bodies of the functions.
12675          This two-phased approach handles cases like:
12676
12677             struct S {
12678               void f() { g(); }
12679               void g(int i = 3);
12680             };
12681
12682          */
12683       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12684              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12685            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12686            TREE_PURPOSE (parser->unparsed_functions_queues)
12687              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12688         {
12689           fn = TREE_VALUE (queue_entry);
12690           /* If there are default arguments that have not yet been processed,
12691              take care of them now.  */
12692           if (class_type != TREE_PURPOSE (queue_entry))
12693             {
12694               if (pushed_scope)
12695                 pop_scope (pushed_scope);
12696               class_type = TREE_PURPOSE (queue_entry);
12697               pushed_scope = push_scope (class_type);
12698             }
12699           /* Make sure that any template parameters are in scope.  */
12700           maybe_begin_member_template_processing (fn);
12701           /* Parse the default argument expressions.  */
12702           cp_parser_late_parsing_default_args (parser, fn);
12703           /* Remove any template parameters from the symbol table.  */
12704           maybe_end_member_template_processing ();
12705         }
12706       if (pushed_scope)
12707         pop_scope (pushed_scope);
12708       /* Now parse the body of the functions.  */
12709       for (TREE_VALUE (parser->unparsed_functions_queues)
12710              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12711            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12712            TREE_VALUE (parser->unparsed_functions_queues)
12713              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12714         {
12715           /* Figure out which function we need to process.  */
12716           fn = TREE_VALUE (queue_entry);
12717
12718           /* A hack to prevent garbage collection.  */
12719           function_depth++;
12720
12721           /* Parse the function.  */
12722           cp_parser_late_parsing_for_member (parser, fn);
12723           function_depth--;
12724         }
12725     }
12726
12727   /* Put back any saved access checks.  */
12728   pop_deferring_access_checks ();
12729
12730   /* Restore the count of active template-parameter-lists.  */
12731   parser->num_template_parameter_lists
12732     = saved_num_template_parameter_lists;
12733
12734   return type;
12735 }
12736
12737 /* Parse a class-head.
12738
12739    class-head:
12740      class-key identifier [opt] base-clause [opt]
12741      class-key nested-name-specifier identifier base-clause [opt]
12742      class-key nested-name-specifier [opt] template-id
12743        base-clause [opt]
12744
12745    GNU Extensions:
12746      class-key attributes identifier [opt] base-clause [opt]
12747      class-key attributes nested-name-specifier identifier base-clause [opt]
12748      class-key attributes nested-name-specifier [opt] template-id
12749        base-clause [opt]
12750
12751    Returns the TYPE of the indicated class.  Sets
12752    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12753    involving a nested-name-specifier was used, and FALSE otherwise.
12754
12755    Returns error_mark_node if this is not a class-head.
12756
12757    Returns NULL_TREE if the class-head is syntactically valid, but
12758    semantically invalid in a way that means we should skip the entire
12759    body of the class.  */
12760
12761 static tree
12762 cp_parser_class_head (cp_parser* parser,
12763                       bool* nested_name_specifier_p,
12764                       tree *attributes_p)
12765 {
12766   tree nested_name_specifier;
12767   enum tag_types class_key;
12768   tree id = NULL_TREE;
12769   tree type = NULL_TREE;
12770   tree attributes;
12771   bool template_id_p = false;
12772   bool qualified_p = false;
12773   bool invalid_nested_name_p = false;
12774   bool invalid_explicit_specialization_p = false;
12775   tree pushed_scope = NULL_TREE;
12776   unsigned num_templates;
12777   tree bases;
12778
12779   /* Assume no nested-name-specifier will be present.  */
12780   *nested_name_specifier_p = false;
12781   /* Assume no template parameter lists will be used in defining the
12782      type.  */
12783   num_templates = 0;
12784
12785   /* Look for the class-key.  */
12786   class_key = cp_parser_class_key (parser);
12787   if (class_key == none_type)
12788     return error_mark_node;
12789
12790   /* Parse the attributes.  */
12791   attributes = cp_parser_attributes_opt (parser);
12792
12793   /* If the next token is `::', that is invalid -- but sometimes
12794      people do try to write:
12795
12796        struct ::S {};
12797
12798      Handle this gracefully by accepting the extra qualifier, and then
12799      issuing an error about it later if this really is a
12800      class-head.  If it turns out just to be an elaborated type
12801      specifier, remain silent.  */
12802   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12803     qualified_p = true;
12804
12805   push_deferring_access_checks (dk_no_check);
12806
12807   /* Determine the name of the class.  Begin by looking for an
12808      optional nested-name-specifier.  */
12809   nested_name_specifier
12810     = cp_parser_nested_name_specifier_opt (parser,
12811                                            /*typename_keyword_p=*/false,
12812                                            /*check_dependency_p=*/false,
12813                                            /*type_p=*/false,
12814                                            /*is_declaration=*/false);
12815   /* If there was a nested-name-specifier, then there *must* be an
12816      identifier.  */
12817   if (nested_name_specifier)
12818     {
12819       /* Although the grammar says `identifier', it really means
12820          `class-name' or `template-name'.  You are only allowed to
12821          define a class that has already been declared with this
12822          syntax.
12823
12824          The proposed resolution for Core Issue 180 says that whever
12825          you see `class T::X' you should treat `X' as a type-name.
12826
12827          It is OK to define an inaccessible class; for example:
12828
12829            class A { class B; };
12830            class A::B {};
12831
12832          We do not know if we will see a class-name, or a
12833          template-name.  We look for a class-name first, in case the
12834          class-name is a template-id; if we looked for the
12835          template-name first we would stop after the template-name.  */
12836       cp_parser_parse_tentatively (parser);
12837       type = cp_parser_class_name (parser,
12838                                    /*typename_keyword_p=*/false,
12839                                    /*template_keyword_p=*/false,
12840                                    class_type,
12841                                    /*check_dependency_p=*/false,
12842                                    /*class_head_p=*/true,
12843                                    /*is_declaration=*/false);
12844       /* If that didn't work, ignore the nested-name-specifier.  */
12845       if (!cp_parser_parse_definitely (parser))
12846         {
12847           invalid_nested_name_p = true;
12848           id = cp_parser_identifier (parser);
12849           if (id == error_mark_node)
12850             id = NULL_TREE;
12851         }
12852       /* If we could not find a corresponding TYPE, treat this
12853          declaration like an unqualified declaration.  */
12854       if (type == error_mark_node)
12855         nested_name_specifier = NULL_TREE;
12856       /* Otherwise, count the number of templates used in TYPE and its
12857          containing scopes.  */
12858       else
12859         {
12860           tree scope;
12861
12862           for (scope = TREE_TYPE (type);
12863                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12864                scope = (TYPE_P (scope)
12865                         ? TYPE_CONTEXT (scope)
12866                         : DECL_CONTEXT (scope)))
12867             if (TYPE_P (scope)
12868                 && CLASS_TYPE_P (scope)
12869                 && CLASSTYPE_TEMPLATE_INFO (scope)
12870                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12871                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12872               ++num_templates;
12873         }
12874     }
12875   /* Otherwise, the identifier is optional.  */
12876   else
12877     {
12878       /* We don't know whether what comes next is a template-id,
12879          an identifier, or nothing at all.  */
12880       cp_parser_parse_tentatively (parser);
12881       /* Check for a template-id.  */
12882       id = cp_parser_template_id (parser,
12883                                   /*template_keyword_p=*/false,
12884                                   /*check_dependency_p=*/true,
12885                                   /*is_declaration=*/true);
12886       /* If that didn't work, it could still be an identifier.  */
12887       if (!cp_parser_parse_definitely (parser))
12888         {
12889           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12890             id = cp_parser_identifier (parser);
12891           else
12892             id = NULL_TREE;
12893         }
12894       else
12895         {
12896           template_id_p = true;
12897           ++num_templates;
12898         }
12899     }
12900
12901   pop_deferring_access_checks ();
12902
12903   if (id)
12904     cp_parser_check_for_invalid_template_id (parser, id);
12905
12906   /* If it's not a `:' or a `{' then we can't really be looking at a
12907      class-head, since a class-head only appears as part of a
12908      class-specifier.  We have to detect this situation before calling
12909      xref_tag, since that has irreversible side-effects.  */
12910   if (!cp_parser_next_token_starts_class_definition_p (parser))
12911     {
12912       cp_parser_error (parser, "expected %<{%> or %<:%>");
12913       return error_mark_node;
12914     }
12915
12916   /* At this point, we're going ahead with the class-specifier, even
12917      if some other problem occurs.  */
12918   cp_parser_commit_to_tentative_parse (parser);
12919   /* Issue the error about the overly-qualified name now.  */
12920   if (qualified_p)
12921     cp_parser_error (parser,
12922                      "global qualification of class name is invalid");
12923   else if (invalid_nested_name_p)
12924     cp_parser_error (parser,
12925                      "qualified name does not name a class");
12926   else if (nested_name_specifier)
12927     {
12928       tree scope;
12929
12930       /* Reject typedef-names in class heads.  */
12931       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12932         {
12933           error ("invalid class name in declaration of %qD", type);
12934           type = NULL_TREE;
12935           goto done;
12936         }
12937
12938       /* Figure out in what scope the declaration is being placed.  */
12939       scope = current_scope ();
12940       /* If that scope does not contain the scope in which the
12941          class was originally declared, the program is invalid.  */
12942       if (scope && !is_ancestor (scope, nested_name_specifier))
12943         {
12944           error ("declaration of %qD in %qD which does not enclose %qD",
12945                  type, scope, nested_name_specifier);
12946           type = NULL_TREE;
12947           goto done;
12948         }
12949       /* [dcl.meaning]
12950
12951          A declarator-id shall not be qualified exception of the
12952          definition of a ... nested class outside of its class
12953          ... [or] a the definition or explicit instantiation of a
12954          class member of a namespace outside of its namespace.  */
12955       if (scope == nested_name_specifier)
12956         {
12957           pedwarn ("extra qualification ignored");
12958           nested_name_specifier = NULL_TREE;
12959           num_templates = 0;
12960         }
12961     }
12962   /* An explicit-specialization must be preceded by "template <>".  If
12963      it is not, try to recover gracefully.  */
12964   if (at_namespace_scope_p ()
12965       && parser->num_template_parameter_lists == 0
12966       && template_id_p)
12967     {
12968       error ("an explicit specialization must be preceded by %<template <>%>");
12969       invalid_explicit_specialization_p = true;
12970       /* Take the same action that would have been taken by
12971          cp_parser_explicit_specialization.  */
12972       ++parser->num_template_parameter_lists;
12973       begin_specialization ();
12974     }
12975   /* There must be no "return" statements between this point and the
12976      end of this function; set "type "to the correct return value and
12977      use "goto done;" to return.  */
12978   /* Make sure that the right number of template parameters were
12979      present.  */
12980   if (!cp_parser_check_template_parameters (parser, num_templates))
12981     {
12982       /* If something went wrong, there is no point in even trying to
12983          process the class-definition.  */
12984       type = NULL_TREE;
12985       goto done;
12986     }
12987
12988   /* Look up the type.  */
12989   if (template_id_p)
12990     {
12991       type = TREE_TYPE (id);
12992       maybe_process_partial_specialization (type);
12993       if (nested_name_specifier)
12994         pushed_scope = push_scope (nested_name_specifier);
12995     }
12996   else if (nested_name_specifier)
12997     {
12998       tree class_type;
12999
13000       /* Given:
13001
13002             template <typename T> struct S { struct T };
13003             template <typename T> struct S<T>::T { };
13004
13005          we will get a TYPENAME_TYPE when processing the definition of
13006          `S::T'.  We need to resolve it to the actual type before we
13007          try to define it.  */
13008       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13009         {
13010           class_type = resolve_typename_type (TREE_TYPE (type),
13011                                               /*only_current_p=*/false);
13012           if (class_type != error_mark_node)
13013             type = TYPE_NAME (class_type);
13014           else
13015             {
13016               cp_parser_error (parser, "could not resolve typename type");
13017               type = error_mark_node;
13018             }
13019         }
13020
13021       maybe_process_partial_specialization (TREE_TYPE (type));
13022       class_type = current_class_type;
13023       /* Enter the scope indicated by the nested-name-specifier.  */
13024       pushed_scope = push_scope (nested_name_specifier);
13025       /* Get the canonical version of this type.  */
13026       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13027       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13028           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13029         {
13030           type = push_template_decl (type);
13031           if (type == error_mark_node)
13032             {
13033               type = NULL_TREE;
13034               goto done;
13035             }
13036         }
13037
13038       type = TREE_TYPE (type);
13039       *nested_name_specifier_p = true;
13040     }
13041   else      /* The name is not a nested name.  */
13042     {
13043       /* If the class was unnamed, create a dummy name.  */
13044       if (!id)
13045         id = make_anon_name ();
13046       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13047                        parser->num_template_parameter_lists);
13048     }
13049
13050   /* Indicate whether this class was declared as a `class' or as a
13051      `struct'.  */
13052   if (TREE_CODE (type) == RECORD_TYPE)
13053     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13054   cp_parser_check_class_key (class_key, type);
13055
13056   /* If this type was already complete, and we see another definition,
13057      that's an error.  */
13058   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13059     {
13060       error ("redefinition of %q#T", type);
13061       error ("previous definition of %q+#T", type);
13062       type = NULL_TREE;
13063       goto done;
13064     }
13065
13066   /* We will have entered the scope containing the class; the names of
13067      base classes should be looked up in that context.  For example:
13068
13069        struct A { struct B {}; struct C; };
13070        struct A::C : B {};
13071
13072      is valid.  */
13073   bases = NULL_TREE;
13074
13075   /* Get the list of base-classes, if there is one.  */
13076   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13077     bases = cp_parser_base_clause (parser);
13078
13079   /* Process the base classes.  */
13080   xref_basetypes (type, bases);
13081
13082  done:
13083   /* Leave the scope given by the nested-name-specifier.  We will
13084      enter the class scope itself while processing the members.  */
13085   if (pushed_scope)
13086     pop_scope (pushed_scope);
13087
13088   if (invalid_explicit_specialization_p)
13089     {
13090       end_specialization ();
13091       --parser->num_template_parameter_lists;
13092     }
13093   *attributes_p = attributes;
13094   return type;
13095 }
13096
13097 /* Parse a class-key.
13098
13099    class-key:
13100      class
13101      struct
13102      union
13103
13104    Returns the kind of class-key specified, or none_type to indicate
13105    error.  */
13106
13107 static enum tag_types
13108 cp_parser_class_key (cp_parser* parser)
13109 {
13110   cp_token *token;
13111   enum tag_types tag_type;
13112
13113   /* Look for the class-key.  */
13114   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13115   if (!token)
13116     return none_type;
13117
13118   /* Check to see if the TOKEN is a class-key.  */
13119   tag_type = cp_parser_token_is_class_key (token);
13120   if (!tag_type)
13121     cp_parser_error (parser, "expected class-key");
13122   return tag_type;
13123 }
13124
13125 /* Parse an (optional) member-specification.
13126
13127    member-specification:
13128      member-declaration member-specification [opt]
13129      access-specifier : member-specification [opt]  */
13130
13131 static void
13132 cp_parser_member_specification_opt (cp_parser* parser)
13133 {
13134   while (true)
13135     {
13136       cp_token *token;
13137       enum rid keyword;
13138
13139       /* Peek at the next token.  */
13140       token = cp_lexer_peek_token (parser->lexer);
13141       /* If it's a `}', or EOF then we've seen all the members.  */
13142       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13143         break;
13144
13145       /* See if this token is a keyword.  */
13146       keyword = token->keyword;
13147       switch (keyword)
13148         {
13149         case RID_PUBLIC:
13150         case RID_PROTECTED:
13151         case RID_PRIVATE:
13152           /* Consume the access-specifier.  */
13153           cp_lexer_consume_token (parser->lexer);
13154           /* Remember which access-specifier is active.  */
13155           current_access_specifier = token->value;
13156           /* Look for the `:'.  */
13157           cp_parser_require (parser, CPP_COLON, "`:'");
13158           break;
13159
13160         default:
13161           /* Accept #pragmas at class scope.  */
13162           if (token->type == CPP_PRAGMA)
13163             {
13164               cp_lexer_handle_pragma (parser->lexer);
13165               break;
13166             }
13167
13168           /* Otherwise, the next construction must be a
13169              member-declaration.  */
13170           cp_parser_member_declaration (parser);
13171         }
13172     }
13173 }
13174
13175 /* Parse a member-declaration.
13176
13177    member-declaration:
13178      decl-specifier-seq [opt] member-declarator-list [opt] ;
13179      function-definition ; [opt]
13180      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13181      using-declaration
13182      template-declaration
13183
13184    member-declarator-list:
13185      member-declarator
13186      member-declarator-list , member-declarator
13187
13188    member-declarator:
13189      declarator pure-specifier [opt]
13190      declarator constant-initializer [opt]
13191      identifier [opt] : constant-expression
13192
13193    GNU Extensions:
13194
13195    member-declaration:
13196      __extension__ member-declaration
13197
13198    member-declarator:
13199      declarator attributes [opt] pure-specifier [opt]
13200      declarator attributes [opt] constant-initializer [opt]
13201      identifier [opt] attributes [opt] : constant-expression  */
13202
13203 static void
13204 cp_parser_member_declaration (cp_parser* parser)
13205 {
13206   cp_decl_specifier_seq decl_specifiers;
13207   tree prefix_attributes;
13208   tree decl;
13209   int declares_class_or_enum;
13210   bool friend_p;
13211   cp_token *token;
13212   int saved_pedantic;
13213
13214   /* Check for the `__extension__' keyword.  */
13215   if (cp_parser_extension_opt (parser, &saved_pedantic))
13216     {
13217       /* Recurse.  */
13218       cp_parser_member_declaration (parser);
13219       /* Restore the old value of the PEDANTIC flag.  */
13220       pedantic = saved_pedantic;
13221
13222       return;
13223     }
13224
13225   /* Check for a template-declaration.  */
13226   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13227     {
13228       /* Parse the template-declaration.  */
13229       cp_parser_template_declaration (parser, /*member_p=*/true);
13230
13231       return;
13232     }
13233
13234   /* Check for a using-declaration.  */
13235   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13236     {
13237       /* Parse the using-declaration.  */
13238       cp_parser_using_declaration (parser);
13239
13240       return;
13241     }
13242
13243   /* Check for @defs.  */
13244   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13245     {
13246       tree ivar, member;
13247       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13248       ivar = ivar_chains;
13249       while (ivar)
13250         {
13251           member = ivar;
13252           ivar = TREE_CHAIN (member);
13253           TREE_CHAIN (member) = NULL_TREE;
13254           finish_member_declaration (member);
13255         }
13256       return;
13257     }
13258
13259   /* Parse the decl-specifier-seq.  */
13260   cp_parser_decl_specifier_seq (parser,
13261                                 CP_PARSER_FLAGS_OPTIONAL,
13262                                 &decl_specifiers,
13263                                 &declares_class_or_enum);
13264   prefix_attributes = decl_specifiers.attributes;
13265   decl_specifiers.attributes = NULL_TREE;
13266   /* Check for an invalid type-name.  */
13267   if (!decl_specifiers.type
13268       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13269     return;
13270   /* If there is no declarator, then the decl-specifier-seq should
13271      specify a type.  */
13272   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13273     {
13274       /* If there was no decl-specifier-seq, and the next token is a
13275          `;', then we have something like:
13276
13277            struct S { ; };
13278
13279          [class.mem]
13280
13281          Each member-declaration shall declare at least one member
13282          name of the class.  */
13283       if (!decl_specifiers.any_specifiers_p)
13284         {
13285           cp_token *token = cp_lexer_peek_token (parser->lexer);
13286           if (pedantic && !token->in_system_header)
13287             pedwarn ("%Hextra %<;%>", &token->location);
13288         }
13289       else
13290         {
13291           tree type;
13292
13293           /* See if this declaration is a friend.  */
13294           friend_p = cp_parser_friend_p (&decl_specifiers);
13295           /* If there were decl-specifiers, check to see if there was
13296              a class-declaration.  */
13297           type = check_tag_decl (&decl_specifiers);
13298           /* Nested classes have already been added to the class, but
13299              a `friend' needs to be explicitly registered.  */
13300           if (friend_p)
13301             {
13302               /* If the `friend' keyword was present, the friend must
13303                  be introduced with a class-key.  */
13304                if (!declares_class_or_enum)
13305                  error ("a class-key must be used when declaring a friend");
13306                /* In this case:
13307
13308                     template <typename T> struct A {
13309                       friend struct A<T>::B;
13310                     };
13311
13312                   A<T>::B will be represented by a TYPENAME_TYPE, and
13313                   therefore not recognized by check_tag_decl.  */
13314                if (!type
13315                    && decl_specifiers.type
13316                    && TYPE_P (decl_specifiers.type))
13317                  type = decl_specifiers.type;
13318                if (!type || !TYPE_P (type))
13319                  error ("friend declaration does not name a class or "
13320                         "function");
13321                else
13322                  make_friend_class (current_class_type, type,
13323                                     /*complain=*/true);
13324             }
13325           /* If there is no TYPE, an error message will already have
13326              been issued.  */
13327           else if (!type || type == error_mark_node)
13328             ;
13329           /* An anonymous aggregate has to be handled specially; such
13330              a declaration really declares a data member (with a
13331              particular type), as opposed to a nested class.  */
13332           else if (ANON_AGGR_TYPE_P (type))
13333             {
13334               /* Remove constructors and such from TYPE, now that we
13335                  know it is an anonymous aggregate.  */
13336               fixup_anonymous_aggr (type);
13337               /* And make the corresponding data member.  */
13338               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13339               /* Add it to the class.  */
13340               finish_member_declaration (decl);
13341             }
13342           else
13343             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13344         }
13345     }
13346   else
13347     {
13348       /* See if these declarations will be friends.  */
13349       friend_p = cp_parser_friend_p (&decl_specifiers);
13350
13351       /* Keep going until we hit the `;' at the end of the
13352          declaration.  */
13353       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13354         {
13355           tree attributes = NULL_TREE;
13356           tree first_attribute;
13357
13358           /* Peek at the next token.  */
13359           token = cp_lexer_peek_token (parser->lexer);
13360
13361           /* Check for a bitfield declaration.  */
13362           if (token->type == CPP_COLON
13363               || (token->type == CPP_NAME
13364                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13365                   == CPP_COLON))
13366             {
13367               tree identifier;
13368               tree width;
13369
13370               /* Get the name of the bitfield.  Note that we cannot just
13371                  check TOKEN here because it may have been invalidated by
13372                  the call to cp_lexer_peek_nth_token above.  */
13373               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13374                 identifier = cp_parser_identifier (parser);
13375               else
13376                 identifier = NULL_TREE;
13377
13378               /* Consume the `:' token.  */
13379               cp_lexer_consume_token (parser->lexer);
13380               /* Get the width of the bitfield.  */
13381               width
13382                 = cp_parser_constant_expression (parser,
13383                                                  /*allow_non_constant=*/false,
13384                                                  NULL);
13385
13386               /* Look for attributes that apply to the bitfield.  */
13387               attributes = cp_parser_attributes_opt (parser);
13388               /* Remember which attributes are prefix attributes and
13389                  which are not.  */
13390               first_attribute = attributes;
13391               /* Combine the attributes.  */
13392               attributes = chainon (prefix_attributes, attributes);
13393
13394               /* Create the bitfield declaration.  */
13395               decl = grokbitfield (identifier
13396                                    ? make_id_declarator (NULL_TREE,
13397                                                          identifier)
13398                                    : NULL,
13399                                    &decl_specifiers,
13400                                    width);
13401               /* Apply the attributes.  */
13402               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13403             }
13404           else
13405             {
13406               cp_declarator *declarator;
13407               tree initializer;
13408               tree asm_specification;
13409               int ctor_dtor_or_conv_p;
13410
13411               /* Parse the declarator.  */
13412               declarator
13413                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13414                                         &ctor_dtor_or_conv_p,
13415                                         /*parenthesized_p=*/NULL,
13416                                         /*member_p=*/true);
13417
13418               /* If something went wrong parsing the declarator, make sure
13419                  that we at least consume some tokens.  */
13420               if (declarator == cp_error_declarator)
13421                 {
13422                   /* Skip to the end of the statement.  */
13423                   cp_parser_skip_to_end_of_statement (parser);
13424                   /* If the next token is not a semicolon, that is
13425                      probably because we just skipped over the body of
13426                      a function.  So, we consume a semicolon if
13427                      present, but do not issue an error message if it
13428                      is not present.  */
13429                   if (cp_lexer_next_token_is (parser->lexer,
13430                                               CPP_SEMICOLON))
13431                     cp_lexer_consume_token (parser->lexer);
13432                   return;
13433                 }
13434
13435               if (declares_class_or_enum & 2)
13436                 cp_parser_check_for_definition_in_return_type
13437                   (declarator, decl_specifiers.type);
13438
13439               /* Look for an asm-specification.  */
13440               asm_specification = cp_parser_asm_specification_opt (parser);
13441               /* Look for attributes that apply to the declaration.  */
13442               attributes = cp_parser_attributes_opt (parser);
13443               /* Remember which attributes are prefix attributes and
13444                  which are not.  */
13445               first_attribute = attributes;
13446               /* Combine the attributes.  */
13447               attributes = chainon (prefix_attributes, attributes);
13448
13449               /* If it's an `=', then we have a constant-initializer or a
13450                  pure-specifier.  It is not correct to parse the
13451                  initializer before registering the member declaration
13452                  since the member declaration should be in scope while
13453                  its initializer is processed.  However, the rest of the
13454                  front end does not yet provide an interface that allows
13455                  us to handle this correctly.  */
13456               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13457                 {
13458                   /* In [class.mem]:
13459
13460                      A pure-specifier shall be used only in the declaration of
13461                      a virtual function.
13462
13463                      A member-declarator can contain a constant-initializer
13464                      only if it declares a static member of integral or
13465                      enumeration type.
13466
13467                      Therefore, if the DECLARATOR is for a function, we look
13468                      for a pure-specifier; otherwise, we look for a
13469                      constant-initializer.  When we call `grokfield', it will
13470                      perform more stringent semantics checks.  */
13471                   if (declarator->kind == cdk_function)
13472                     initializer = cp_parser_pure_specifier (parser);
13473                   else
13474                     /* Parse the initializer.  */
13475                     initializer = cp_parser_constant_initializer (parser);
13476                 }
13477               /* Otherwise, there is no initializer.  */
13478               else
13479                 initializer = NULL_TREE;
13480
13481               /* See if we are probably looking at a function
13482                  definition.  We are certainly not looking at a
13483                  member-declarator.  Calling `grokfield' has
13484                  side-effects, so we must not do it unless we are sure
13485                  that we are looking at a member-declarator.  */
13486               if (cp_parser_token_starts_function_definition_p
13487                   (cp_lexer_peek_token (parser->lexer)))
13488                 {
13489                   /* The grammar does not allow a pure-specifier to be
13490                      used when a member function is defined.  (It is
13491                      possible that this fact is an oversight in the
13492                      standard, since a pure function may be defined
13493                      outside of the class-specifier.  */
13494                   if (initializer)
13495                     error ("pure-specifier on function-definition");
13496                   decl = cp_parser_save_member_function_body (parser,
13497                                                               &decl_specifiers,
13498                                                               declarator,
13499                                                               attributes);
13500                   /* If the member was not a friend, declare it here.  */
13501                   if (!friend_p)
13502                     finish_member_declaration (decl);
13503                   /* Peek at the next token.  */
13504                   token = cp_lexer_peek_token (parser->lexer);
13505                   /* If the next token is a semicolon, consume it.  */
13506                   if (token->type == CPP_SEMICOLON)
13507                     cp_lexer_consume_token (parser->lexer);
13508                   return;
13509                 }
13510               else
13511                 {
13512                   /* Create the declaration.  */
13513                   decl = grokfield (declarator, &decl_specifiers,
13514                                     initializer, asm_specification,
13515                                     attributes);
13516                   /* Any initialization must have been from a
13517                      constant-expression.  */
13518                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13519                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13520                 }
13521             }
13522
13523           /* Reset PREFIX_ATTRIBUTES.  */
13524           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13525             attributes = TREE_CHAIN (attributes);
13526           if (attributes)
13527             TREE_CHAIN (attributes) = NULL_TREE;
13528
13529           /* If there is any qualification still in effect, clear it
13530              now; we will be starting fresh with the next declarator.  */
13531           parser->scope = NULL_TREE;
13532           parser->qualifying_scope = NULL_TREE;
13533           parser->object_scope = NULL_TREE;
13534           /* If it's a `,', then there are more declarators.  */
13535           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13536             cp_lexer_consume_token (parser->lexer);
13537           /* If the next token isn't a `;', then we have a parse error.  */
13538           else if (cp_lexer_next_token_is_not (parser->lexer,
13539                                                CPP_SEMICOLON))
13540             {
13541               cp_parser_error (parser, "expected %<;%>");
13542               /* Skip tokens until we find a `;'.  */
13543               cp_parser_skip_to_end_of_statement (parser);
13544
13545               break;
13546             }
13547
13548           if (decl)
13549             {
13550               /* Add DECL to the list of members.  */
13551               if (!friend_p)
13552                 finish_member_declaration (decl);
13553
13554               if (TREE_CODE (decl) == FUNCTION_DECL)
13555                 cp_parser_save_default_args (parser, decl);
13556             }
13557         }
13558     }
13559
13560   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13561 }
13562
13563 /* Parse a pure-specifier.
13564
13565    pure-specifier:
13566      = 0
13567
13568    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13569    Otherwise, ERROR_MARK_NODE is returned.  */
13570
13571 static tree
13572 cp_parser_pure_specifier (cp_parser* parser)
13573 {
13574   cp_token *token;
13575
13576   /* Look for the `=' token.  */
13577   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13578     return error_mark_node;
13579   /* Look for the `0' token.  */
13580   token = cp_lexer_consume_token (parser->lexer);
13581   if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13582     {
13583       cp_parser_error (parser,
13584                        "invalid pure specifier (only `= 0' is allowed)");
13585       cp_parser_skip_to_end_of_statement (parser);
13586       return error_mark_node;
13587     }
13588
13589   /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13590      We need to get information from the lexer about how the number
13591      was spelled in order to fix this problem.  */
13592   return integer_zero_node;
13593 }
13594
13595 /* Parse a constant-initializer.
13596
13597    constant-initializer:
13598      = constant-expression
13599
13600    Returns a representation of the constant-expression.  */
13601
13602 static tree
13603 cp_parser_constant_initializer (cp_parser* parser)
13604 {
13605   /* Look for the `=' token.  */
13606   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13607     return error_mark_node;
13608
13609   /* It is invalid to write:
13610
13611        struct S { static const int i = { 7 }; };
13612
13613      */
13614   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13615     {
13616       cp_parser_error (parser,
13617                        "a brace-enclosed initializer is not allowed here");
13618       /* Consume the opening brace.  */
13619       cp_lexer_consume_token (parser->lexer);
13620       /* Skip the initializer.  */
13621       cp_parser_skip_to_closing_brace (parser);
13622       /* Look for the trailing `}'.  */
13623       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13624
13625       return error_mark_node;
13626     }
13627
13628   return cp_parser_constant_expression (parser,
13629                                         /*allow_non_constant=*/false,
13630                                         NULL);
13631 }
13632
13633 /* Derived classes [gram.class.derived] */
13634
13635 /* Parse a base-clause.
13636
13637    base-clause:
13638      : base-specifier-list
13639
13640    base-specifier-list:
13641      base-specifier
13642      base-specifier-list , base-specifier
13643
13644    Returns a TREE_LIST representing the base-classes, in the order in
13645    which they were declared.  The representation of each node is as
13646    described by cp_parser_base_specifier.
13647
13648    In the case that no bases are specified, this function will return
13649    NULL_TREE, not ERROR_MARK_NODE.  */
13650
13651 static tree
13652 cp_parser_base_clause (cp_parser* parser)
13653 {
13654   tree bases = NULL_TREE;
13655
13656   /* Look for the `:' that begins the list.  */
13657   cp_parser_require (parser, CPP_COLON, "`:'");
13658
13659   /* Scan the base-specifier-list.  */
13660   while (true)
13661     {
13662       cp_token *token;
13663       tree base;
13664
13665       /* Look for the base-specifier.  */
13666       base = cp_parser_base_specifier (parser);
13667       /* Add BASE to the front of the list.  */
13668       if (base != error_mark_node)
13669         {
13670           TREE_CHAIN (base) = bases;
13671           bases = base;
13672         }
13673       /* Peek at the next token.  */
13674       token = cp_lexer_peek_token (parser->lexer);
13675       /* If it's not a comma, then the list is complete.  */
13676       if (token->type != CPP_COMMA)
13677         break;
13678       /* Consume the `,'.  */
13679       cp_lexer_consume_token (parser->lexer);
13680     }
13681
13682   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13683      base class had a qualified name.  However, the next name that
13684      appears is certainly not qualified.  */
13685   parser->scope = NULL_TREE;
13686   parser->qualifying_scope = NULL_TREE;
13687   parser->object_scope = NULL_TREE;
13688
13689   return nreverse (bases);
13690 }
13691
13692 /* Parse a base-specifier.
13693
13694    base-specifier:
13695      :: [opt] nested-name-specifier [opt] class-name
13696      virtual access-specifier [opt] :: [opt] nested-name-specifier
13697        [opt] class-name
13698      access-specifier virtual [opt] :: [opt] nested-name-specifier
13699        [opt] class-name
13700
13701    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13702    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13703    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13704    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13705
13706 static tree
13707 cp_parser_base_specifier (cp_parser* parser)
13708 {
13709   cp_token *token;
13710   bool done = false;
13711   bool virtual_p = false;
13712   bool duplicate_virtual_error_issued_p = false;
13713   bool duplicate_access_error_issued_p = false;
13714   bool class_scope_p, template_p;
13715   tree access = access_default_node;
13716   tree type;
13717
13718   /* Process the optional `virtual' and `access-specifier'.  */
13719   while (!done)
13720     {
13721       /* Peek at the next token.  */
13722       token = cp_lexer_peek_token (parser->lexer);
13723       /* Process `virtual'.  */
13724       switch (token->keyword)
13725         {
13726         case RID_VIRTUAL:
13727           /* If `virtual' appears more than once, issue an error.  */
13728           if (virtual_p && !duplicate_virtual_error_issued_p)
13729             {
13730               cp_parser_error (parser,
13731                                "%<virtual%> specified more than once in base-specified");
13732               duplicate_virtual_error_issued_p = true;
13733             }
13734
13735           virtual_p = true;
13736
13737           /* Consume the `virtual' token.  */
13738           cp_lexer_consume_token (parser->lexer);
13739
13740           break;
13741
13742         case RID_PUBLIC:
13743         case RID_PROTECTED:
13744         case RID_PRIVATE:
13745           /* If more than one access specifier appears, issue an
13746              error.  */
13747           if (access != access_default_node
13748               && !duplicate_access_error_issued_p)
13749             {
13750               cp_parser_error (parser,
13751                                "more than one access specifier in base-specified");
13752               duplicate_access_error_issued_p = true;
13753             }
13754
13755           access = ridpointers[(int) token->keyword];
13756
13757           /* Consume the access-specifier.  */
13758           cp_lexer_consume_token (parser->lexer);
13759
13760           break;
13761
13762         default:
13763           done = true;
13764           break;
13765         }
13766     }
13767   /* It is not uncommon to see programs mechanically, erroneously, use
13768      the 'typename' keyword to denote (dependent) qualified types
13769      as base classes.  */
13770   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13771     {
13772       if (!processing_template_decl)
13773         error ("keyword %<typename%> not allowed outside of templates");
13774       else
13775         error ("keyword %<typename%> not allowed in this context "
13776                "(the base class is implicitly a type)");
13777       cp_lexer_consume_token (parser->lexer);
13778     }
13779
13780   /* Look for the optional `::' operator.  */
13781   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13782   /* Look for the nested-name-specifier.  The simplest way to
13783      implement:
13784
13785        [temp.res]
13786
13787        The keyword `typename' is not permitted in a base-specifier or
13788        mem-initializer; in these contexts a qualified name that
13789        depends on a template-parameter is implicitly assumed to be a
13790        type name.
13791
13792      is to pretend that we have seen the `typename' keyword at this
13793      point.  */
13794   cp_parser_nested_name_specifier_opt (parser,
13795                                        /*typename_keyword_p=*/true,
13796                                        /*check_dependency_p=*/true,
13797                                        typename_type,
13798                                        /*is_declaration=*/true);
13799   /* If the base class is given by a qualified name, assume that names
13800      we see are type names or templates, as appropriate.  */
13801   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13802   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13803
13804   /* Finally, look for the class-name.  */
13805   type = cp_parser_class_name (parser,
13806                                class_scope_p,
13807                                template_p,
13808                                typename_type,
13809                                /*check_dependency_p=*/true,
13810                                /*class_head_p=*/false,
13811                                /*is_declaration=*/true);
13812
13813   if (type == error_mark_node)
13814     return error_mark_node;
13815
13816   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13817 }
13818
13819 /* Exception handling [gram.exception] */
13820
13821 /* Parse an (optional) exception-specification.
13822
13823    exception-specification:
13824      throw ( type-id-list [opt] )
13825
13826    Returns a TREE_LIST representing the exception-specification.  The
13827    TREE_VALUE of each node is a type.  */
13828
13829 static tree
13830 cp_parser_exception_specification_opt (cp_parser* parser)
13831 {
13832   cp_token *token;
13833   tree type_id_list;
13834
13835   /* Peek at the next token.  */
13836   token = cp_lexer_peek_token (parser->lexer);
13837   /* If it's not `throw', then there's no exception-specification.  */
13838   if (!cp_parser_is_keyword (token, RID_THROW))
13839     return NULL_TREE;
13840
13841   /* Consume the `throw'.  */
13842   cp_lexer_consume_token (parser->lexer);
13843
13844   /* Look for the `('.  */
13845   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13846
13847   /* Peek at the next token.  */
13848   token = cp_lexer_peek_token (parser->lexer);
13849   /* If it's not a `)', then there is a type-id-list.  */
13850   if (token->type != CPP_CLOSE_PAREN)
13851     {
13852       const char *saved_message;
13853
13854       /* Types may not be defined in an exception-specification.  */
13855       saved_message = parser->type_definition_forbidden_message;
13856       parser->type_definition_forbidden_message
13857         = "types may not be defined in an exception-specification";
13858       /* Parse the type-id-list.  */
13859       type_id_list = cp_parser_type_id_list (parser);
13860       /* Restore the saved message.  */
13861       parser->type_definition_forbidden_message = saved_message;
13862     }
13863   else
13864     type_id_list = empty_except_spec;
13865
13866   /* Look for the `)'.  */
13867   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13868
13869   return type_id_list;
13870 }
13871
13872 /* Parse an (optional) type-id-list.
13873
13874    type-id-list:
13875      type-id
13876      type-id-list , type-id
13877
13878    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13879    in the order that the types were presented.  */
13880
13881 static tree
13882 cp_parser_type_id_list (cp_parser* parser)
13883 {
13884   tree types = NULL_TREE;
13885
13886   while (true)
13887     {
13888       cp_token *token;
13889       tree type;
13890
13891       /* Get the next type-id.  */
13892       type = cp_parser_type_id (parser);
13893       /* Add it to the list.  */
13894       types = add_exception_specifier (types, type, /*complain=*/1);
13895       /* Peek at the next token.  */
13896       token = cp_lexer_peek_token (parser->lexer);
13897       /* If it is not a `,', we are done.  */
13898       if (token->type != CPP_COMMA)
13899         break;
13900       /* Consume the `,'.  */
13901       cp_lexer_consume_token (parser->lexer);
13902     }
13903
13904   return nreverse (types);
13905 }
13906
13907 /* Parse a try-block.
13908
13909    try-block:
13910      try compound-statement handler-seq  */
13911
13912 static tree
13913 cp_parser_try_block (cp_parser* parser)
13914 {
13915   tree try_block;
13916
13917   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13918   try_block = begin_try_block ();
13919   cp_parser_compound_statement (parser, NULL, true);
13920   finish_try_block (try_block);
13921   cp_parser_handler_seq (parser);
13922   finish_handler_sequence (try_block);
13923
13924   return try_block;
13925 }
13926
13927 /* Parse a function-try-block.
13928
13929    function-try-block:
13930      try ctor-initializer [opt] function-body handler-seq  */
13931
13932 static bool
13933 cp_parser_function_try_block (cp_parser* parser)
13934 {
13935   tree try_block;
13936   bool ctor_initializer_p;
13937
13938   /* Look for the `try' keyword.  */
13939   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13940     return false;
13941   /* Let the rest of the front-end know where we are.  */
13942   try_block = begin_function_try_block ();
13943   /* Parse the function-body.  */
13944   ctor_initializer_p
13945     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13946   /* We're done with the `try' part.  */
13947   finish_function_try_block (try_block);
13948   /* Parse the handlers.  */
13949   cp_parser_handler_seq (parser);
13950   /* We're done with the handlers.  */
13951   finish_function_handler_sequence (try_block);
13952
13953   return ctor_initializer_p;
13954 }
13955
13956 /* Parse a handler-seq.
13957
13958    handler-seq:
13959      handler handler-seq [opt]  */
13960
13961 static void
13962 cp_parser_handler_seq (cp_parser* parser)
13963 {
13964   while (true)
13965     {
13966       cp_token *token;
13967
13968       /* Parse the handler.  */
13969       cp_parser_handler (parser);
13970       /* Peek at the next token.  */
13971       token = cp_lexer_peek_token (parser->lexer);
13972       /* If it's not `catch' then there are no more handlers.  */
13973       if (!cp_parser_is_keyword (token, RID_CATCH))
13974         break;
13975     }
13976 }
13977
13978 /* Parse a handler.
13979
13980    handler:
13981      catch ( exception-declaration ) compound-statement  */
13982
13983 static void
13984 cp_parser_handler (cp_parser* parser)
13985 {
13986   tree handler;
13987   tree declaration;
13988
13989   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13990   handler = begin_handler ();
13991   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13992   declaration = cp_parser_exception_declaration (parser);
13993   finish_handler_parms (declaration, handler);
13994   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13995   cp_parser_compound_statement (parser, NULL, false);
13996   finish_handler (handler);
13997 }
13998
13999 /* Parse an exception-declaration.
14000
14001    exception-declaration:
14002      type-specifier-seq declarator
14003      type-specifier-seq abstract-declarator
14004      type-specifier-seq
14005      ...
14006
14007    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14008    ellipsis variant is used.  */
14009
14010 static tree
14011 cp_parser_exception_declaration (cp_parser* parser)
14012 {
14013   tree decl;
14014   cp_decl_specifier_seq type_specifiers;
14015   cp_declarator *declarator;
14016   const char *saved_message;
14017
14018   /* If it's an ellipsis, it's easy to handle.  */
14019   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14020     {
14021       /* Consume the `...' token.  */
14022       cp_lexer_consume_token (parser->lexer);
14023       return NULL_TREE;
14024     }
14025
14026   /* Types may not be defined in exception-declarations.  */
14027   saved_message = parser->type_definition_forbidden_message;
14028   parser->type_definition_forbidden_message
14029     = "types may not be defined in exception-declarations";
14030
14031   /* Parse the type-specifier-seq.  */
14032   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14033                                 &type_specifiers);
14034   /* If it's a `)', then there is no declarator.  */
14035   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14036     declarator = NULL;
14037   else
14038     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14039                                        /*ctor_dtor_or_conv_p=*/NULL,
14040                                        /*parenthesized_p=*/NULL,
14041                                        /*member_p=*/false);
14042
14043   /* Restore the saved message.  */
14044   parser->type_definition_forbidden_message = saved_message;
14045
14046   if (type_specifiers.any_specifiers_p)
14047     {
14048       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14049       if (decl == NULL_TREE)
14050         error ("invalid catch parameter");
14051     }
14052   else
14053     decl = NULL_TREE;
14054
14055   return decl;
14056 }
14057
14058 /* Parse a throw-expression.
14059
14060    throw-expression:
14061      throw assignment-expression [opt]
14062
14063    Returns a THROW_EXPR representing the throw-expression.  */
14064
14065 static tree
14066 cp_parser_throw_expression (cp_parser* parser)
14067 {
14068   tree expression;
14069   cp_token* token;
14070
14071   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14072   token = cp_lexer_peek_token (parser->lexer);
14073   /* Figure out whether or not there is an assignment-expression
14074      following the "throw" keyword.  */
14075   if (token->type == CPP_COMMA
14076       || token->type == CPP_SEMICOLON
14077       || token->type == CPP_CLOSE_PAREN
14078       || token->type == CPP_CLOSE_SQUARE
14079       || token->type == CPP_CLOSE_BRACE
14080       || token->type == CPP_COLON)
14081     expression = NULL_TREE;
14082   else
14083     expression = cp_parser_assignment_expression (parser,
14084                                                   /*cast_p=*/false);
14085
14086   return build_throw (expression);
14087 }
14088
14089 /* GNU Extensions */
14090
14091 /* Parse an (optional) asm-specification.
14092
14093    asm-specification:
14094      asm ( string-literal )
14095
14096    If the asm-specification is present, returns a STRING_CST
14097    corresponding to the string-literal.  Otherwise, returns
14098    NULL_TREE.  */
14099
14100 static tree
14101 cp_parser_asm_specification_opt (cp_parser* parser)
14102 {
14103   cp_token *token;
14104   tree asm_specification;
14105
14106   /* Peek at the next token.  */
14107   token = cp_lexer_peek_token (parser->lexer);
14108   /* If the next token isn't the `asm' keyword, then there's no
14109      asm-specification.  */
14110   if (!cp_parser_is_keyword (token, RID_ASM))
14111     return NULL_TREE;
14112
14113   /* Consume the `asm' token.  */
14114   cp_lexer_consume_token (parser->lexer);
14115   /* Look for the `('.  */
14116   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14117
14118   /* Look for the string-literal.  */
14119   asm_specification = cp_parser_string_literal (parser, false, false);
14120
14121   /* Look for the `)'.  */
14122   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14123
14124   return asm_specification;
14125 }
14126
14127 /* Parse an asm-operand-list.
14128
14129    asm-operand-list:
14130      asm-operand
14131      asm-operand-list , asm-operand
14132
14133    asm-operand:
14134      string-literal ( expression )
14135      [ string-literal ] string-literal ( expression )
14136
14137    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14138    each node is the expression.  The TREE_PURPOSE is itself a
14139    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14140    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14141    is a STRING_CST for the string literal before the parenthesis.  */
14142
14143 static tree
14144 cp_parser_asm_operand_list (cp_parser* parser)
14145 {
14146   tree asm_operands = NULL_TREE;
14147
14148   while (true)
14149     {
14150       tree string_literal;
14151       tree expression;
14152       tree name;
14153
14154       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14155         {
14156           /* Consume the `[' token.  */
14157           cp_lexer_consume_token (parser->lexer);
14158           /* Read the operand name.  */
14159           name = cp_parser_identifier (parser);
14160           if (name != error_mark_node)
14161             name = build_string (IDENTIFIER_LENGTH (name),
14162                                  IDENTIFIER_POINTER (name));
14163           /* Look for the closing `]'.  */
14164           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14165         }
14166       else
14167         name = NULL_TREE;
14168       /* Look for the string-literal.  */
14169       string_literal = cp_parser_string_literal (parser, false, false);
14170
14171       /* Look for the `('.  */
14172       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14173       /* Parse the expression.  */
14174       expression = cp_parser_expression (parser, /*cast_p=*/false);
14175       /* Look for the `)'.  */
14176       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14177
14178       /* Add this operand to the list.  */
14179       asm_operands = tree_cons (build_tree_list (name, string_literal),
14180                                 expression,
14181                                 asm_operands);
14182       /* If the next token is not a `,', there are no more
14183          operands.  */
14184       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14185         break;
14186       /* Consume the `,'.  */
14187       cp_lexer_consume_token (parser->lexer);
14188     }
14189
14190   return nreverse (asm_operands);
14191 }
14192
14193 /* Parse an asm-clobber-list.
14194
14195    asm-clobber-list:
14196      string-literal
14197      asm-clobber-list , string-literal
14198
14199    Returns a TREE_LIST, indicating the clobbers in the order that they
14200    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14201
14202 static tree
14203 cp_parser_asm_clobber_list (cp_parser* parser)
14204 {
14205   tree clobbers = NULL_TREE;
14206
14207   while (true)
14208     {
14209       tree string_literal;
14210
14211       /* Look for the string literal.  */
14212       string_literal = cp_parser_string_literal (parser, false, false);
14213       /* Add it to the list.  */
14214       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14215       /* If the next token is not a `,', then the list is
14216          complete.  */
14217       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14218         break;
14219       /* Consume the `,' token.  */
14220       cp_lexer_consume_token (parser->lexer);
14221     }
14222
14223   return clobbers;
14224 }
14225
14226 /* Parse an (optional) series of attributes.
14227
14228    attributes:
14229      attributes attribute
14230
14231    attribute:
14232      __attribute__ (( attribute-list [opt] ))
14233
14234    The return value is as for cp_parser_attribute_list.  */
14235
14236 static tree
14237 cp_parser_attributes_opt (cp_parser* parser)
14238 {
14239   tree attributes = NULL_TREE;
14240
14241   while (true)
14242     {
14243       cp_token *token;
14244       tree attribute_list;
14245
14246       /* Peek at the next token.  */
14247       token = cp_lexer_peek_token (parser->lexer);
14248       /* If it's not `__attribute__', then we're done.  */
14249       if (token->keyword != RID_ATTRIBUTE)
14250         break;
14251
14252       /* Consume the `__attribute__' keyword.  */
14253       cp_lexer_consume_token (parser->lexer);
14254       /* Look for the two `(' tokens.  */
14255       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14256       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14257
14258       /* Peek at the next token.  */
14259       token = cp_lexer_peek_token (parser->lexer);
14260       if (token->type != CPP_CLOSE_PAREN)
14261         /* Parse the attribute-list.  */
14262         attribute_list = cp_parser_attribute_list (parser);
14263       else
14264         /* If the next token is a `)', then there is no attribute
14265            list.  */
14266         attribute_list = NULL;
14267
14268       /* Look for the two `)' tokens.  */
14269       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14270       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14271
14272       /* Add these new attributes to the list.  */
14273       attributes = chainon (attributes, attribute_list);
14274     }
14275
14276   return attributes;
14277 }
14278
14279 /* Parse an attribute-list.
14280
14281    attribute-list:
14282      attribute
14283      attribute-list , attribute
14284
14285    attribute:
14286      identifier
14287      identifier ( identifier )
14288      identifier ( identifier , expression-list )
14289      identifier ( expression-list )
14290
14291    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14292    to an attribute.  The TREE_PURPOSE of each node is the identifier
14293    indicating which attribute is in use.  The TREE_VALUE represents
14294    the arguments, if any.  */
14295
14296 static tree
14297 cp_parser_attribute_list (cp_parser* parser)
14298 {
14299   tree attribute_list = NULL_TREE;
14300   bool save_translate_strings_p = parser->translate_strings_p;
14301
14302   parser->translate_strings_p = false;
14303   while (true)
14304     {
14305       cp_token *token;
14306       tree identifier;
14307       tree attribute;
14308
14309       /* Look for the identifier.  We also allow keywords here; for
14310          example `__attribute__ ((const))' is legal.  */
14311       token = cp_lexer_peek_token (parser->lexer);
14312       if (token->type == CPP_NAME
14313           || token->type == CPP_KEYWORD)
14314         {
14315           /* Consume the token.  */
14316           token = cp_lexer_consume_token (parser->lexer);
14317
14318           /* Save away the identifier that indicates which attribute
14319              this is.  */
14320           identifier = token->value;
14321           attribute = build_tree_list (identifier, NULL_TREE);
14322
14323           /* Peek at the next token.  */
14324           token = cp_lexer_peek_token (parser->lexer);
14325           /* If it's an `(', then parse the attribute arguments.  */
14326           if (token->type == CPP_OPEN_PAREN)
14327             {
14328               tree arguments;
14329
14330               arguments = (cp_parser_parenthesized_expression_list
14331                            (parser, true, /*cast_p=*/false,
14332                             /*non_constant_p=*/NULL));
14333               /* Save the identifier and arguments away.  */
14334               TREE_VALUE (attribute) = arguments;
14335             }
14336
14337           /* Add this attribute to the list.  */
14338           TREE_CHAIN (attribute) = attribute_list;
14339           attribute_list = attribute;
14340
14341           token = cp_lexer_peek_token (parser->lexer);
14342         }
14343       /* Now, look for more attributes.  If the next token isn't a
14344          `,', we're done.  */
14345       if (token->type != CPP_COMMA)
14346         break;
14347
14348       /* Consume the comma and keep going.  */
14349       cp_lexer_consume_token (parser->lexer);
14350     }
14351   parser->translate_strings_p = save_translate_strings_p;
14352
14353   /* We built up the list in reverse order.  */
14354   return nreverse (attribute_list);
14355 }
14356
14357 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14358    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14359    current value of the PEDANTIC flag, regardless of whether or not
14360    the `__extension__' keyword is present.  The caller is responsible
14361    for restoring the value of the PEDANTIC flag.  */
14362
14363 static bool
14364 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14365 {
14366   /* Save the old value of the PEDANTIC flag.  */
14367   *saved_pedantic = pedantic;
14368
14369   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14370     {
14371       /* Consume the `__extension__' token.  */
14372       cp_lexer_consume_token (parser->lexer);
14373       /* We're not being pedantic while the `__extension__' keyword is
14374          in effect.  */
14375       pedantic = 0;
14376
14377       return true;
14378     }
14379
14380   return false;
14381 }
14382
14383 /* Parse a label declaration.
14384
14385    label-declaration:
14386      __label__ label-declarator-seq ;
14387
14388    label-declarator-seq:
14389      identifier , label-declarator-seq
14390      identifier  */
14391
14392 static void
14393 cp_parser_label_declaration (cp_parser* parser)
14394 {
14395   /* Look for the `__label__' keyword.  */
14396   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14397
14398   while (true)
14399     {
14400       tree identifier;
14401
14402       /* Look for an identifier.  */
14403       identifier = cp_parser_identifier (parser);
14404       /* If we failed, stop.  */
14405       if (identifier == error_mark_node)
14406         break;
14407       /* Declare it as a label.  */
14408       finish_label_decl (identifier);
14409       /* If the next token is a `;', stop.  */
14410       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14411         break;
14412       /* Look for the `,' separating the label declarations.  */
14413       cp_parser_require (parser, CPP_COMMA, "`,'");
14414     }
14415
14416   /* Look for the final `;'.  */
14417   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14418 }
14419
14420 /* Support Functions */
14421
14422 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14423    NAME should have one of the representations used for an
14424    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14425    is returned.  If PARSER->SCOPE is a dependent type, then a
14426    SCOPE_REF is returned.
14427
14428    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14429    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14430    was formed.  Abstractly, such entities should not be passed to this
14431    function, because they do not need to be looked up, but it is
14432    simpler to check for this special case here, rather than at the
14433    call-sites.
14434
14435    In cases not explicitly covered above, this function returns a
14436    DECL, OVERLOAD, or baselink representing the result of the lookup.
14437    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14438    is returned.
14439
14440    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14441    (e.g., "struct") that was used.  In that case bindings that do not
14442    refer to types are ignored.
14443
14444    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14445    ignored.
14446
14447    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14448    are ignored.
14449
14450    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14451    types.
14452
14453    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14454    results in an ambiguity, and false otherwise.  */
14455
14456 static tree
14457 cp_parser_lookup_name (cp_parser *parser, tree name,
14458                        enum tag_types tag_type,
14459                        bool is_template, bool is_namespace,
14460                        bool check_dependency,
14461                        bool *ambiguous_p)
14462 {
14463   tree decl;
14464   tree object_type = parser->context->object_type;
14465
14466   /* Assume that the lookup will be unambiguous.  */
14467   if (ambiguous_p)
14468     *ambiguous_p = false;
14469
14470   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14471      no longer valid.  Note that if we are parsing tentatively, and
14472      the parse fails, OBJECT_TYPE will be automatically restored.  */
14473   parser->context->object_type = NULL_TREE;
14474
14475   if (name == error_mark_node)
14476     return error_mark_node;
14477
14478   /* A template-id has already been resolved; there is no lookup to
14479      do.  */
14480   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14481     return name;
14482   if (BASELINK_P (name))
14483     {
14484       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14485                   == TEMPLATE_ID_EXPR);
14486       return name;
14487     }
14488
14489   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14490      it should already have been checked to make sure that the name
14491      used matches the type being destroyed.  */
14492   if (TREE_CODE (name) == BIT_NOT_EXPR)
14493     {
14494       tree type;
14495
14496       /* Figure out to which type this destructor applies.  */
14497       if (parser->scope)
14498         type = parser->scope;
14499       else if (object_type)
14500         type = object_type;
14501       else
14502         type = current_class_type;
14503       /* If that's not a class type, there is no destructor.  */
14504       if (!type || !CLASS_TYPE_P (type))
14505         return error_mark_node;
14506       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14507         lazily_declare_fn (sfk_destructor, type);
14508       if (!CLASSTYPE_DESTRUCTORS (type))
14509           return error_mark_node;
14510       /* If it was a class type, return the destructor.  */
14511       return CLASSTYPE_DESTRUCTORS (type);
14512     }
14513
14514   /* By this point, the NAME should be an ordinary identifier.  If
14515      the id-expression was a qualified name, the qualifying scope is
14516      stored in PARSER->SCOPE at this point.  */
14517   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14518
14519   /* Perform the lookup.  */
14520   if (parser->scope)
14521     {
14522       bool dependent_p;
14523
14524       if (parser->scope == error_mark_node)
14525         return error_mark_node;
14526
14527       /* If the SCOPE is dependent, the lookup must be deferred until
14528          the template is instantiated -- unless we are explicitly
14529          looking up names in uninstantiated templates.  Even then, we
14530          cannot look up the name if the scope is not a class type; it
14531          might, for example, be a template type parameter.  */
14532       dependent_p = (TYPE_P (parser->scope)
14533                      && !(parser->in_declarator_p
14534                           && currently_open_class (parser->scope))
14535                      && dependent_type_p (parser->scope));
14536       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14537            && dependent_p)
14538         {
14539           if (tag_type)
14540             {
14541               tree type;
14542
14543               /* The resolution to Core Issue 180 says that `struct
14544                  A::B' should be considered a type-name, even if `A'
14545                  is dependent.  */
14546               type = make_typename_type (parser->scope, name, tag_type,
14547                                          /*complain=*/1);
14548               decl = TYPE_NAME (type);
14549             }
14550           else if (is_template)
14551             decl = make_unbound_class_template (parser->scope,
14552                                                 name, NULL_TREE,
14553                                                 /*complain=*/1);
14554           else
14555             decl = build_nt (SCOPE_REF, parser->scope, name);
14556         }
14557       else
14558         {
14559           tree pushed_scope = NULL_TREE;
14560
14561           /* If PARSER->SCOPE is a dependent type, then it must be a
14562              class type, and we must not be checking dependencies;
14563              otherwise, we would have processed this lookup above.  So
14564              that PARSER->SCOPE is not considered a dependent base by
14565              lookup_member, we must enter the scope here.  */
14566           if (dependent_p)
14567             pushed_scope = push_scope (parser->scope);
14568           /* If the PARSER->SCOPE is a template specialization, it
14569              may be instantiated during name lookup.  In that case,
14570              errors may be issued.  Even if we rollback the current
14571              tentative parse, those errors are valid.  */
14572           decl = lookup_qualified_name (parser->scope, name,
14573                                         tag_type != none_type,
14574                                         /*complain=*/true);
14575           if (pushed_scope)
14576             pop_scope (pushed_scope);
14577         }
14578       parser->qualifying_scope = parser->scope;
14579       parser->object_scope = NULL_TREE;
14580     }
14581   else if (object_type)
14582     {
14583       tree object_decl = NULL_TREE;
14584       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14585          OBJECT_TYPE is not a class.  */
14586       if (CLASS_TYPE_P (object_type))
14587         /* If the OBJECT_TYPE is a template specialization, it may
14588            be instantiated during name lookup.  In that case, errors
14589            may be issued.  Even if we rollback the current tentative
14590            parse, those errors are valid.  */
14591         object_decl = lookup_member (object_type,
14592                                      name,
14593                                      /*protect=*/0,
14594                                      tag_type != none_type);
14595       /* Look it up in the enclosing context, too.  */
14596       decl = lookup_name_real (name, tag_type != none_type,
14597                                /*nonclass=*/0,
14598                                /*block_p=*/true, is_namespace,
14599                                /*flags=*/0);
14600       parser->object_scope = object_type;
14601       parser->qualifying_scope = NULL_TREE;
14602       if (object_decl)
14603         decl = object_decl;
14604     }
14605   else
14606     {
14607       decl = lookup_name_real (name, tag_type != none_type,
14608                                /*nonclass=*/0,
14609                                /*block_p=*/true, is_namespace,
14610                                /*flags=*/0);
14611       parser->qualifying_scope = NULL_TREE;
14612       parser->object_scope = NULL_TREE;
14613     }
14614
14615   /* If the lookup failed, let our caller know.  */
14616   if (!decl || decl == error_mark_node)
14617     return error_mark_node;
14618
14619   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14620   if (TREE_CODE (decl) == TREE_LIST)
14621     {
14622       if (ambiguous_p)
14623         *ambiguous_p = true;
14624       /* The error message we have to print is too complicated for
14625          cp_parser_error, so we incorporate its actions directly.  */
14626       if (!cp_parser_simulate_error (parser))
14627         {
14628           error ("reference to %qD is ambiguous", name);
14629           print_candidates (decl);
14630         }
14631       return error_mark_node;
14632     }
14633
14634   gcc_assert (DECL_P (decl)
14635               || TREE_CODE (decl) == OVERLOAD
14636               || TREE_CODE (decl) == SCOPE_REF
14637               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14638               || BASELINK_P (decl));
14639
14640   /* If we have resolved the name of a member declaration, check to
14641      see if the declaration is accessible.  When the name resolves to
14642      set of overloaded functions, accessibility is checked when
14643      overload resolution is done.
14644
14645      During an explicit instantiation, access is not checked at all,
14646      as per [temp.explicit].  */
14647   if (DECL_P (decl))
14648     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14649
14650   return decl;
14651 }
14652
14653 /* Like cp_parser_lookup_name, but for use in the typical case where
14654    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14655    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14656
14657 static tree
14658 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14659 {
14660   return cp_parser_lookup_name (parser, name,
14661                                 none_type,
14662                                 /*is_template=*/false,
14663                                 /*is_namespace=*/false,
14664                                 /*check_dependency=*/true,
14665                                 /*ambiguous_p=*/NULL);
14666 }
14667
14668 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14669    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14670    true, the DECL indicates the class being defined in a class-head,
14671    or declared in an elaborated-type-specifier.
14672
14673    Otherwise, return DECL.  */
14674
14675 static tree
14676 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14677 {
14678   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14679      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14680
14681        struct A {
14682          template <typename T> struct B;
14683        };
14684
14685        template <typename T> struct A::B {};
14686
14687      Similarly, in a elaborated-type-specifier:
14688
14689        namespace N { struct X{}; }
14690
14691        struct A {
14692          template <typename T> friend struct N::X;
14693        };
14694
14695      However, if the DECL refers to a class type, and we are in
14696      the scope of the class, then the name lookup automatically
14697      finds the TYPE_DECL created by build_self_reference rather
14698      than a TEMPLATE_DECL.  For example, in:
14699
14700        template <class T> struct S {
14701          S s;
14702        };
14703
14704      there is no need to handle such case.  */
14705
14706   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14707     return DECL_TEMPLATE_RESULT (decl);
14708
14709   return decl;
14710 }
14711
14712 /* If too many, or too few, template-parameter lists apply to the
14713    declarator, issue an error message.  Returns TRUE if all went well,
14714    and FALSE otherwise.  */
14715
14716 static bool
14717 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14718                                                 cp_declarator *declarator)
14719 {
14720   unsigned num_templates;
14721
14722   /* We haven't seen any classes that involve template parameters yet.  */
14723   num_templates = 0;
14724
14725   switch (declarator->kind)
14726     {
14727     case cdk_id:
14728       if (declarator->u.id.qualifying_scope)
14729         {
14730           tree scope;
14731           tree member;
14732
14733           scope = declarator->u.id.qualifying_scope;
14734           member = declarator->u.id.unqualified_name;
14735
14736           while (scope && CLASS_TYPE_P (scope))
14737             {
14738               /* You're supposed to have one `template <...>'
14739                  for every template class, but you don't need one
14740                  for a full specialization.  For example:
14741
14742                  template <class T> struct S{};
14743                  template <> struct S<int> { void f(); };
14744                  void S<int>::f () {}
14745
14746                  is correct; there shouldn't be a `template <>' for
14747                  the definition of `S<int>::f'.  */
14748               if (CLASSTYPE_TEMPLATE_INFO (scope)
14749                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14750                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14751                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14752                 ++num_templates;
14753
14754               scope = TYPE_CONTEXT (scope);
14755             }
14756         }
14757       else if (TREE_CODE (declarator->u.id.unqualified_name)
14758                == TEMPLATE_ID_EXPR)
14759         /* If the DECLARATOR has the form `X<y>' then it uses one
14760            additional level of template parameters.  */
14761         ++num_templates;
14762
14763       return cp_parser_check_template_parameters (parser,
14764                                                   num_templates);
14765
14766     case cdk_function:
14767     case cdk_array:
14768     case cdk_pointer:
14769     case cdk_reference:
14770     case cdk_ptrmem:
14771       return (cp_parser_check_declarator_template_parameters
14772               (parser, declarator->declarator));
14773
14774     case cdk_error:
14775       return true;
14776
14777     default:
14778       gcc_unreachable ();
14779     }
14780   return false;
14781 }
14782
14783 /* NUM_TEMPLATES were used in the current declaration.  If that is
14784    invalid, return FALSE and issue an error messages.  Otherwise,
14785    return TRUE.  */
14786
14787 static bool
14788 cp_parser_check_template_parameters (cp_parser* parser,
14789                                      unsigned num_templates)
14790 {
14791   /* If there are more template classes than parameter lists, we have
14792      something like:
14793
14794        template <class T> void S<T>::R<T>::f ();  */
14795   if (parser->num_template_parameter_lists < num_templates)
14796     {
14797       error ("too few template-parameter-lists");
14798       return false;
14799     }
14800   /* If there are the same number of template classes and parameter
14801      lists, that's OK.  */
14802   if (parser->num_template_parameter_lists == num_templates)
14803     return true;
14804   /* If there are more, but only one more, then we are referring to a
14805      member template.  That's OK too.  */
14806   if (parser->num_template_parameter_lists == num_templates + 1)
14807       return true;
14808   /* Otherwise, there are too many template parameter lists.  We have
14809      something like:
14810
14811      template <class T> template <class U> void S::f();  */
14812   error ("too many template-parameter-lists");
14813   return false;
14814 }
14815
14816 /* Parse an optional `::' token indicating that the following name is
14817    from the global namespace.  If so, PARSER->SCOPE is set to the
14818    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14819    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14820    Returns the new value of PARSER->SCOPE, if the `::' token is
14821    present, and NULL_TREE otherwise.  */
14822
14823 static tree
14824 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14825 {
14826   cp_token *token;
14827
14828   /* Peek at the next token.  */
14829   token = cp_lexer_peek_token (parser->lexer);
14830   /* If we're looking at a `::' token then we're starting from the
14831      global namespace, not our current location.  */
14832   if (token->type == CPP_SCOPE)
14833     {
14834       /* Consume the `::' token.  */
14835       cp_lexer_consume_token (parser->lexer);
14836       /* Set the SCOPE so that we know where to start the lookup.  */
14837       parser->scope = global_namespace;
14838       parser->qualifying_scope = global_namespace;
14839       parser->object_scope = NULL_TREE;
14840
14841       return parser->scope;
14842     }
14843   else if (!current_scope_valid_p)
14844     {
14845       parser->scope = NULL_TREE;
14846       parser->qualifying_scope = NULL_TREE;
14847       parser->object_scope = NULL_TREE;
14848     }
14849
14850   return NULL_TREE;
14851 }
14852
14853 /* Returns TRUE if the upcoming token sequence is the start of a
14854    constructor declarator.  If FRIEND_P is true, the declarator is
14855    preceded by the `friend' specifier.  */
14856
14857 static bool
14858 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14859 {
14860   bool constructor_p;
14861   tree type_decl = NULL_TREE;
14862   bool nested_name_p;
14863   cp_token *next_token;
14864
14865   /* The common case is that this is not a constructor declarator, so
14866      try to avoid doing lots of work if at all possible.  It's not
14867      valid declare a constructor at function scope.  */
14868   if (at_function_scope_p ())
14869     return false;
14870   /* And only certain tokens can begin a constructor declarator.  */
14871   next_token = cp_lexer_peek_token (parser->lexer);
14872   if (next_token->type != CPP_NAME
14873       && next_token->type != CPP_SCOPE
14874       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14875       && next_token->type != CPP_TEMPLATE_ID)
14876     return false;
14877
14878   /* Parse tentatively; we are going to roll back all of the tokens
14879      consumed here.  */
14880   cp_parser_parse_tentatively (parser);
14881   /* Assume that we are looking at a constructor declarator.  */
14882   constructor_p = true;
14883
14884   /* Look for the optional `::' operator.  */
14885   cp_parser_global_scope_opt (parser,
14886                               /*current_scope_valid_p=*/false);
14887   /* Look for the nested-name-specifier.  */
14888   nested_name_p
14889     = (cp_parser_nested_name_specifier_opt (parser,
14890                                             /*typename_keyword_p=*/false,
14891                                             /*check_dependency_p=*/false,
14892                                             /*type_p=*/false,
14893                                             /*is_declaration=*/false)
14894        != NULL_TREE);
14895   /* Outside of a class-specifier, there must be a
14896      nested-name-specifier.  */
14897   if (!nested_name_p &&
14898       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14899        || friend_p))
14900     constructor_p = false;
14901   /* If we still think that this might be a constructor-declarator,
14902      look for a class-name.  */
14903   if (constructor_p)
14904     {
14905       /* If we have:
14906
14907            template <typename T> struct S { S(); };
14908            template <typename T> S<T>::S ();
14909
14910          we must recognize that the nested `S' names a class.
14911          Similarly, for:
14912
14913            template <typename T> S<T>::S<T> ();
14914
14915          we must recognize that the nested `S' names a template.  */
14916       type_decl = cp_parser_class_name (parser,
14917                                         /*typename_keyword_p=*/false,
14918                                         /*template_keyword_p=*/false,
14919                                         none_type,
14920                                         /*check_dependency_p=*/false,
14921                                         /*class_head_p=*/false,
14922                                         /*is_declaration=*/false);
14923       /* If there was no class-name, then this is not a constructor.  */
14924       constructor_p = !cp_parser_error_occurred (parser);
14925     }
14926
14927   /* If we're still considering a constructor, we have to see a `(',
14928      to begin the parameter-declaration-clause, followed by either a
14929      `)', an `...', or a decl-specifier.  We need to check for a
14930      type-specifier to avoid being fooled into thinking that:
14931
14932        S::S (f) (int);
14933
14934      is a constructor.  (It is actually a function named `f' that
14935      takes one parameter (of type `int') and returns a value of type
14936      `S::S'.  */
14937   if (constructor_p
14938       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14939     {
14940       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14941           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14942           /* A parameter declaration begins with a decl-specifier,
14943              which is either the "attribute" keyword, a storage class
14944              specifier, or (usually) a type-specifier.  */
14945           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14946           && !cp_parser_storage_class_specifier_opt (parser))
14947         {
14948           tree type;
14949           tree pushed_scope = NULL_TREE;
14950           unsigned saved_num_template_parameter_lists;
14951
14952           /* Names appearing in the type-specifier should be looked up
14953              in the scope of the class.  */
14954           if (current_class_type)
14955             type = NULL_TREE;
14956           else
14957             {
14958               type = TREE_TYPE (type_decl);
14959               if (TREE_CODE (type) == TYPENAME_TYPE)
14960                 {
14961                   type = resolve_typename_type (type,
14962                                                 /*only_current_p=*/false);
14963                   if (type == error_mark_node)
14964                     {
14965                       cp_parser_abort_tentative_parse (parser);
14966                       return false;
14967                     }
14968                 }
14969               pushed_scope = push_scope (type);
14970             }
14971
14972           /* Inside the constructor parameter list, surrounding
14973              template-parameter-lists do not apply.  */
14974           saved_num_template_parameter_lists
14975             = parser->num_template_parameter_lists;
14976           parser->num_template_parameter_lists = 0;
14977
14978           /* Look for the type-specifier.  */
14979           cp_parser_type_specifier (parser,
14980                                     CP_PARSER_FLAGS_NONE,
14981                                     /*decl_specs=*/NULL,
14982                                     /*is_declarator=*/true,
14983                                     /*declares_class_or_enum=*/NULL,
14984                                     /*is_cv_qualifier=*/NULL);
14985
14986           parser->num_template_parameter_lists
14987             = saved_num_template_parameter_lists;
14988
14989           /* Leave the scope of the class.  */
14990           if (pushed_scope)
14991             pop_scope (pushed_scope);
14992
14993           constructor_p = !cp_parser_error_occurred (parser);
14994         }
14995     }
14996   else
14997     constructor_p = false;
14998   /* We did not really want to consume any tokens.  */
14999   cp_parser_abort_tentative_parse (parser);
15000
15001   return constructor_p;
15002 }
15003
15004 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15005    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15006    they must be performed once we are in the scope of the function.
15007
15008    Returns the function defined.  */
15009
15010 static tree
15011 cp_parser_function_definition_from_specifiers_and_declarator
15012   (cp_parser* parser,
15013    cp_decl_specifier_seq *decl_specifiers,
15014    tree attributes,
15015    const cp_declarator *declarator)
15016 {
15017   tree fn;
15018   bool success_p;
15019
15020   /* Begin the function-definition.  */
15021   success_p = start_function (decl_specifiers, declarator, attributes);
15022
15023   /* The things we're about to see are not directly qualified by any
15024      template headers we've seen thus far.  */
15025   reset_specialization ();
15026
15027   /* If there were names looked up in the decl-specifier-seq that we
15028      did not check, check them now.  We must wait until we are in the
15029      scope of the function to perform the checks, since the function
15030      might be a friend.  */
15031   perform_deferred_access_checks ();
15032
15033   if (!success_p)
15034     {
15035       /* Skip the entire function.  */
15036       error ("invalid function declaration");
15037       cp_parser_skip_to_end_of_block_or_statement (parser);
15038       fn = error_mark_node;
15039     }
15040   else
15041     fn = cp_parser_function_definition_after_declarator (parser,
15042                                                          /*inline_p=*/false);
15043
15044   return fn;
15045 }
15046
15047 /* Parse the part of a function-definition that follows the
15048    declarator.  INLINE_P is TRUE iff this function is an inline
15049    function defined with a class-specifier.
15050
15051    Returns the function defined.  */
15052
15053 static tree
15054 cp_parser_function_definition_after_declarator (cp_parser* parser,
15055                                                 bool inline_p)
15056 {
15057   tree fn;
15058   bool ctor_initializer_p = false;
15059   bool saved_in_unbraced_linkage_specification_p;
15060   unsigned saved_num_template_parameter_lists;
15061
15062   /* If the next token is `return', then the code may be trying to
15063      make use of the "named return value" extension that G++ used to
15064      support.  */
15065   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15066     {
15067       /* Consume the `return' keyword.  */
15068       cp_lexer_consume_token (parser->lexer);
15069       /* Look for the identifier that indicates what value is to be
15070          returned.  */
15071       cp_parser_identifier (parser);
15072       /* Issue an error message.  */
15073       error ("named return values are no longer supported");
15074       /* Skip tokens until we reach the start of the function body.  */
15075       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15076              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15077         cp_lexer_consume_token (parser->lexer);
15078     }
15079   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15080      anything declared inside `f'.  */
15081   saved_in_unbraced_linkage_specification_p
15082     = parser->in_unbraced_linkage_specification_p;
15083   parser->in_unbraced_linkage_specification_p = false;
15084   /* Inside the function, surrounding template-parameter-lists do not
15085      apply.  */
15086   saved_num_template_parameter_lists
15087     = parser->num_template_parameter_lists;
15088   parser->num_template_parameter_lists = 0;
15089   /* If the next token is `try', then we are looking at a
15090      function-try-block.  */
15091   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15092     ctor_initializer_p = cp_parser_function_try_block (parser);
15093   /* A function-try-block includes the function-body, so we only do
15094      this next part if we're not processing a function-try-block.  */
15095   else
15096     ctor_initializer_p
15097       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15098
15099   /* Finish the function.  */
15100   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15101                         (inline_p ? 2 : 0));
15102   /* Generate code for it, if necessary.  */
15103   expand_or_defer_fn (fn);
15104   /* Restore the saved values.  */
15105   parser->in_unbraced_linkage_specification_p
15106     = saved_in_unbraced_linkage_specification_p;
15107   parser->num_template_parameter_lists
15108     = saved_num_template_parameter_lists;
15109
15110   return fn;
15111 }
15112
15113 /* Parse a template-declaration, assuming that the `export' (and
15114    `extern') keywords, if present, has already been scanned.  MEMBER_P
15115    is as for cp_parser_template_declaration.  */
15116
15117 static void
15118 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15119 {
15120   tree decl = NULL_TREE;
15121   tree parameter_list;
15122   bool friend_p = false;
15123
15124   /* Look for the `template' keyword.  */
15125   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15126     return;
15127
15128   /* And the `<'.  */
15129   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15130     return;
15131
15132   /* If the next token is `>', then we have an invalid
15133      specialization.  Rather than complain about an invalid template
15134      parameter, issue an error message here.  */
15135   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15136     {
15137       cp_parser_error (parser, "invalid explicit specialization");
15138       begin_specialization ();
15139       parameter_list = NULL_TREE;
15140     }
15141   else
15142     {
15143       /* Parse the template parameters.  */
15144       begin_template_parm_list ();
15145       parameter_list = cp_parser_template_parameter_list (parser);
15146       parameter_list = end_template_parm_list (parameter_list);
15147     }
15148
15149   /* Look for the `>'.  */
15150   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15151   /* We just processed one more parameter list.  */
15152   ++parser->num_template_parameter_lists;
15153   /* If the next token is `template', there are more template
15154      parameters.  */
15155   if (cp_lexer_next_token_is_keyword (parser->lexer,
15156                                       RID_TEMPLATE))
15157     cp_parser_template_declaration_after_export (parser, member_p);
15158   else
15159     {
15160       /* There are no access checks when parsing a template, as we do not
15161          know if a specialization will be a friend.  */
15162       push_deferring_access_checks (dk_no_check);
15163
15164       decl = cp_parser_single_declaration (parser,
15165                                            member_p,
15166                                            &friend_p);
15167
15168       pop_deferring_access_checks ();
15169
15170       /* If this is a member template declaration, let the front
15171          end know.  */
15172       if (member_p && !friend_p && decl)
15173         {
15174           if (TREE_CODE (decl) == TYPE_DECL)
15175             cp_parser_check_access_in_redeclaration (decl);
15176
15177           decl = finish_member_template_decl (decl);
15178         }
15179       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15180         make_friend_class (current_class_type, TREE_TYPE (decl),
15181                            /*complain=*/true);
15182     }
15183   /* We are done with the current parameter list.  */
15184   --parser->num_template_parameter_lists;
15185
15186   /* Finish up.  */
15187   finish_template_decl (parameter_list);
15188
15189   /* Register member declarations.  */
15190   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15191     finish_member_declaration (decl);
15192
15193   /* If DECL is a function template, we must return to parse it later.
15194      (Even though there is no definition, there might be default
15195      arguments that need handling.)  */
15196   if (member_p && decl
15197       && (TREE_CODE (decl) == FUNCTION_DECL
15198           || DECL_FUNCTION_TEMPLATE_P (decl)))
15199     TREE_VALUE (parser->unparsed_functions_queues)
15200       = tree_cons (NULL_TREE, decl,
15201                    TREE_VALUE (parser->unparsed_functions_queues));
15202 }
15203
15204 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15205    `function-definition' sequence.  MEMBER_P is true, this declaration
15206    appears in a class scope.
15207
15208    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15209    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15210
15211 static tree
15212 cp_parser_single_declaration (cp_parser* parser,
15213                               bool member_p,
15214                               bool* friend_p)
15215 {
15216   int declares_class_or_enum;
15217   tree decl = NULL_TREE;
15218   cp_decl_specifier_seq decl_specifiers;
15219   bool function_definition_p = false;
15220
15221   /* This function is only used when processing a template
15222      declaration.  */
15223   gcc_assert (innermost_scope_kind () == sk_template_parms
15224               || innermost_scope_kind () == sk_template_spec);
15225
15226   /* Defer access checks until we know what is being declared.  */
15227   push_deferring_access_checks (dk_deferred);
15228
15229   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15230      alternative.  */
15231   cp_parser_decl_specifier_seq (parser,
15232                                 CP_PARSER_FLAGS_OPTIONAL,
15233                                 &decl_specifiers,
15234                                 &declares_class_or_enum);
15235   if (friend_p)
15236     *friend_p = cp_parser_friend_p (&decl_specifiers);
15237
15238   /* There are no template typedefs.  */
15239   if (decl_specifiers.specs[(int) ds_typedef])
15240     {
15241       error ("template declaration of %qs", "typedef");
15242       decl = error_mark_node;
15243     }
15244
15245   /* Gather up the access checks that occurred the
15246      decl-specifier-seq.  */
15247   stop_deferring_access_checks ();
15248
15249   /* Check for the declaration of a template class.  */
15250   if (declares_class_or_enum)
15251     {
15252       if (cp_parser_declares_only_class_p (parser))
15253         {
15254           decl = shadow_tag (&decl_specifiers);
15255
15256           /* In this case:
15257
15258                struct C {
15259                  friend template <typename T> struct A<T>::B;
15260                };
15261
15262              A<T>::B will be represented by a TYPENAME_TYPE, and
15263              therefore not recognized by shadow_tag.  */
15264           if (friend_p && *friend_p
15265               && !decl
15266               && decl_specifiers.type
15267               && TYPE_P (decl_specifiers.type))
15268             decl = decl_specifiers.type;
15269
15270           if (decl && decl != error_mark_node)
15271             decl = TYPE_NAME (decl);
15272           else
15273             decl = error_mark_node;
15274         }
15275     }
15276   /* If it's not a template class, try for a template function.  If
15277      the next token is a `;', then this declaration does not declare
15278      anything.  But, if there were errors in the decl-specifiers, then
15279      the error might well have come from an attempted class-specifier.
15280      In that case, there's no need to warn about a missing declarator.  */
15281   if (!decl
15282       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15283           || decl_specifiers.type != error_mark_node))
15284     decl = cp_parser_init_declarator (parser,
15285                                       &decl_specifiers,
15286                                       /*function_definition_allowed_p=*/true,
15287                                       member_p,
15288                                       declares_class_or_enum,
15289                                       &function_definition_p);
15290
15291   pop_deferring_access_checks ();
15292
15293   /* Clear any current qualification; whatever comes next is the start
15294      of something new.  */
15295   parser->scope = NULL_TREE;
15296   parser->qualifying_scope = NULL_TREE;
15297   parser->object_scope = NULL_TREE;
15298   /* Look for a trailing `;' after the declaration.  */
15299   if (!function_definition_p
15300       && (decl == error_mark_node
15301           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15302     cp_parser_skip_to_end_of_block_or_statement (parser);
15303
15304   return decl;
15305 }
15306
15307 /* Parse a cast-expression that is not the operand of a unary "&".  */
15308
15309 static tree
15310 cp_parser_simple_cast_expression (cp_parser *parser)
15311 {
15312   return cp_parser_cast_expression (parser, /*address_p=*/false,
15313                                     /*cast_p=*/false);
15314 }
15315
15316 /* Parse a functional cast to TYPE.  Returns an expression
15317    representing the cast.  */
15318
15319 static tree
15320 cp_parser_functional_cast (cp_parser* parser, tree type)
15321 {
15322   tree expression_list;
15323   tree cast;
15324
15325   expression_list
15326     = cp_parser_parenthesized_expression_list (parser, false,
15327                                                /*cast_p=*/true,
15328                                                /*non_constant_p=*/NULL);
15329
15330   cast = build_functional_cast (type, expression_list);
15331   /* [expr.const]/1: In an integral constant expression "only type
15332      conversions to integral or enumeration type can be used".  */
15333   if (cast != error_mark_node && !type_dependent_expression_p (type)
15334       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15335     {
15336       if (cp_parser_non_integral_constant_expression
15337           (parser, "a call to a constructor"))
15338         return error_mark_node;
15339     }
15340   return cast;
15341 }
15342
15343 /* Save the tokens that make up the body of a member function defined
15344    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15345    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15346    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15347    for the member function.  */
15348
15349 static tree
15350 cp_parser_save_member_function_body (cp_parser* parser,
15351                                      cp_decl_specifier_seq *decl_specifiers,
15352                                      cp_declarator *declarator,
15353                                      tree attributes)
15354 {
15355   cp_token *first;
15356   cp_token *last;
15357   tree fn;
15358
15359   /* Create the function-declaration.  */
15360   fn = start_method (decl_specifiers, declarator, attributes);
15361   /* If something went badly wrong, bail out now.  */
15362   if (fn == error_mark_node)
15363     {
15364       /* If there's a function-body, skip it.  */
15365       if (cp_parser_token_starts_function_definition_p
15366           (cp_lexer_peek_token (parser->lexer)))
15367         cp_parser_skip_to_end_of_block_or_statement (parser);
15368       return error_mark_node;
15369     }
15370
15371   /* Remember it, if there default args to post process.  */
15372   cp_parser_save_default_args (parser, fn);
15373
15374   /* Save away the tokens that make up the body of the
15375      function.  */
15376   first = parser->lexer->next_token;
15377   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15378   /* Handle function try blocks.  */
15379   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15380     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15381   last = parser->lexer->next_token;
15382
15383   /* Save away the inline definition; we will process it when the
15384      class is complete.  */
15385   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15386   DECL_PENDING_INLINE_P (fn) = 1;
15387
15388   /* We need to know that this was defined in the class, so that
15389      friend templates are handled correctly.  */
15390   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15391
15392   /* We're done with the inline definition.  */
15393   finish_method (fn);
15394
15395   /* Add FN to the queue of functions to be parsed later.  */
15396   TREE_VALUE (parser->unparsed_functions_queues)
15397     = tree_cons (NULL_TREE, fn,
15398                  TREE_VALUE (parser->unparsed_functions_queues));
15399
15400   return fn;
15401 }
15402
15403 /* Parse a template-argument-list, as well as the trailing ">" (but
15404    not the opening ">").  See cp_parser_template_argument_list for the
15405    return value.  */
15406
15407 static tree
15408 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15409 {
15410   tree arguments;
15411   tree saved_scope;
15412   tree saved_qualifying_scope;
15413   tree saved_object_scope;
15414   bool saved_greater_than_is_operator_p;
15415
15416   /* [temp.names]
15417
15418      When parsing a template-id, the first non-nested `>' is taken as
15419      the end of the template-argument-list rather than a greater-than
15420      operator.  */
15421   saved_greater_than_is_operator_p
15422     = parser->greater_than_is_operator_p;
15423   parser->greater_than_is_operator_p = false;
15424   /* Parsing the argument list may modify SCOPE, so we save it
15425      here.  */
15426   saved_scope = parser->scope;
15427   saved_qualifying_scope = parser->qualifying_scope;
15428   saved_object_scope = parser->object_scope;
15429   /* Parse the template-argument-list itself.  */
15430   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15431     arguments = NULL_TREE;
15432   else
15433     arguments = cp_parser_template_argument_list (parser);
15434   /* Look for the `>' that ends the template-argument-list. If we find
15435      a '>>' instead, it's probably just a typo.  */
15436   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15437     {
15438       if (!saved_greater_than_is_operator_p)
15439         {
15440           /* If we're in a nested template argument list, the '>>' has
15441             to be a typo for '> >'. We emit the error message, but we
15442             continue parsing and we push a '>' as next token, so that
15443             the argument list will be parsed correctly.  Note that the
15444             global source location is still on the token before the
15445             '>>', so we need to say explicitly where we want it.  */
15446           cp_token *token = cp_lexer_peek_token (parser->lexer);
15447           error ("%H%<>>%> should be %<> >%> "
15448                  "within a nested template argument list",
15449                  &token->location);
15450
15451           /* ??? Proper recovery should terminate two levels of
15452              template argument list here.  */
15453           token->type = CPP_GREATER;
15454         }
15455       else
15456         {
15457           /* If this is not a nested template argument list, the '>>'
15458             is a typo for '>'. Emit an error message and continue.
15459             Same deal about the token location, but here we can get it
15460             right by consuming the '>>' before issuing the diagnostic.  */
15461           cp_lexer_consume_token (parser->lexer);
15462           error ("spurious %<>>%>, use %<>%> to terminate "
15463                  "a template argument list");
15464         }
15465     }
15466   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15467     error ("missing %<>%> to terminate the template argument list");
15468   else
15469     /* It's what we want, a '>'; consume it.  */
15470     cp_lexer_consume_token (parser->lexer);
15471   /* The `>' token might be a greater-than operator again now.  */
15472   parser->greater_than_is_operator_p
15473     = saved_greater_than_is_operator_p;
15474   /* Restore the SAVED_SCOPE.  */
15475   parser->scope = saved_scope;
15476   parser->qualifying_scope = saved_qualifying_scope;
15477   parser->object_scope = saved_object_scope;
15478
15479   return arguments;
15480 }
15481
15482 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15483    arguments, or the body of the function have not yet been parsed,
15484    parse them now.  */
15485
15486 static void
15487 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15488 {
15489   /* If this member is a template, get the underlying
15490      FUNCTION_DECL.  */
15491   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15492     member_function = DECL_TEMPLATE_RESULT (member_function);
15493
15494   /* There should not be any class definitions in progress at this
15495      point; the bodies of members are only parsed outside of all class
15496      definitions.  */
15497   gcc_assert (parser->num_classes_being_defined == 0);
15498   /* While we're parsing the member functions we might encounter more
15499      classes.  We want to handle them right away, but we don't want
15500      them getting mixed up with functions that are currently in the
15501      queue.  */
15502   parser->unparsed_functions_queues
15503     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15504
15505   /* Make sure that any template parameters are in scope.  */
15506   maybe_begin_member_template_processing (member_function);
15507
15508   /* If the body of the function has not yet been parsed, parse it
15509      now.  */
15510   if (DECL_PENDING_INLINE_P (member_function))
15511     {
15512       tree function_scope;
15513       cp_token_cache *tokens;
15514
15515       /* The function is no longer pending; we are processing it.  */
15516       tokens = DECL_PENDING_INLINE_INFO (member_function);
15517       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15518       DECL_PENDING_INLINE_P (member_function) = 0;
15519
15520       /* If this is a local class, enter the scope of the containing
15521          function.  */
15522       function_scope = current_function_decl;
15523       if (function_scope)
15524         push_function_context_to (function_scope);
15525
15526
15527       /* Push the body of the function onto the lexer stack.  */
15528       cp_parser_push_lexer_for_tokens (parser, tokens);
15529
15530       /* Let the front end know that we going to be defining this
15531          function.  */
15532       start_preparsed_function (member_function, NULL_TREE,
15533                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15534
15535       /* Don't do access checking if it is a templated function.  */
15536       if (processing_template_decl)
15537         push_deferring_access_checks (dk_no_check);
15538
15539       /* Now, parse the body of the function.  */
15540       cp_parser_function_definition_after_declarator (parser,
15541                                                       /*inline_p=*/true);
15542
15543       if (processing_template_decl)
15544         pop_deferring_access_checks ();
15545
15546       /* Leave the scope of the containing function.  */
15547       if (function_scope)
15548         pop_function_context_from (function_scope);
15549       cp_parser_pop_lexer (parser);
15550     }
15551
15552   /* Remove any template parameters from the symbol table.  */
15553   maybe_end_member_template_processing ();
15554
15555   /* Restore the queue.  */
15556   parser->unparsed_functions_queues
15557     = TREE_CHAIN (parser->unparsed_functions_queues);
15558 }
15559
15560 /* If DECL contains any default args, remember it on the unparsed
15561    functions queue.  */
15562
15563 static void
15564 cp_parser_save_default_args (cp_parser* parser, tree decl)
15565 {
15566   tree probe;
15567
15568   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15569        probe;
15570        probe = TREE_CHAIN (probe))
15571     if (TREE_PURPOSE (probe))
15572       {
15573         TREE_PURPOSE (parser->unparsed_functions_queues)
15574           = tree_cons (current_class_type, decl,
15575                        TREE_PURPOSE (parser->unparsed_functions_queues));
15576         break;
15577       }
15578   return;
15579 }
15580
15581 /* FN is a FUNCTION_DECL which may contains a parameter with an
15582    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15583    assumes that the current scope is the scope in which the default
15584    argument should be processed.  */
15585
15586 static void
15587 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15588 {
15589   bool saved_local_variables_forbidden_p;
15590   tree parm;
15591
15592   /* While we're parsing the default args, we might (due to the
15593      statement expression extension) encounter more classes.  We want
15594      to handle them right away, but we don't want them getting mixed
15595      up with default args that are currently in the queue.  */
15596   parser->unparsed_functions_queues
15597     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15598
15599   /* Local variable names (and the `this' keyword) may not appear
15600      in a default argument.  */
15601   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15602   parser->local_variables_forbidden_p = true;
15603
15604   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15605        parm;
15606        parm = TREE_CHAIN (parm))
15607     {
15608       cp_token_cache *tokens;
15609       tree default_arg = TREE_PURPOSE (parm);
15610       tree parsed_arg;
15611       VEC(tree,gc) *insts;
15612       tree copy;
15613       unsigned ix;
15614
15615       if (!default_arg)
15616         continue;
15617
15618       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15619         /* This can happen for a friend declaration for a function
15620            already declared with default arguments.  */
15621         continue;
15622
15623        /* Push the saved tokens for the default argument onto the parser's
15624           lexer stack.  */
15625       tokens = DEFARG_TOKENS (default_arg);
15626       cp_parser_push_lexer_for_tokens (parser, tokens);
15627
15628       /* Parse the assignment-expression.  */
15629       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15630
15631       TREE_PURPOSE (parm) = parsed_arg;
15632
15633       /* Update any instantiations we've already created.  */
15634       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15635            VEC_iterate (tree, insts, ix, copy); ix++)
15636         TREE_PURPOSE (copy) = parsed_arg;
15637
15638       /* If the token stream has not been completely used up, then
15639          there was extra junk after the end of the default
15640          argument.  */
15641       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15642         cp_parser_error (parser, "expected %<,%>");
15643
15644       /* Revert to the main lexer.  */
15645       cp_parser_pop_lexer (parser);
15646     }
15647
15648   /* Restore the state of local_variables_forbidden_p.  */
15649   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15650
15651   /* Restore the queue.  */
15652   parser->unparsed_functions_queues
15653     = TREE_CHAIN (parser->unparsed_functions_queues);
15654 }
15655
15656 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15657    either a TYPE or an expression, depending on the form of the
15658    input.  The KEYWORD indicates which kind of expression we have
15659    encountered.  */
15660
15661 static tree
15662 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15663 {
15664   static const char *format;
15665   tree expr = NULL_TREE;
15666   const char *saved_message;
15667   bool saved_integral_constant_expression_p;
15668   bool saved_non_integral_constant_expression_p;
15669
15670   /* Initialize FORMAT the first time we get here.  */
15671   if (!format)
15672     format = "types may not be defined in '%s' expressions";
15673
15674   /* Types cannot be defined in a `sizeof' expression.  Save away the
15675      old message.  */
15676   saved_message = parser->type_definition_forbidden_message;
15677   /* And create the new one.  */
15678   parser->type_definition_forbidden_message
15679     = xmalloc (strlen (format)
15680                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15681                + 1 /* `\0' */);
15682   sprintf ((char *) parser->type_definition_forbidden_message,
15683            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15684
15685   /* The restrictions on constant-expressions do not apply inside
15686      sizeof expressions.  */
15687   saved_integral_constant_expression_p
15688     = parser->integral_constant_expression_p;
15689   saved_non_integral_constant_expression_p
15690     = parser->non_integral_constant_expression_p;
15691   parser->integral_constant_expression_p = false;
15692
15693   /* Do not actually evaluate the expression.  */
15694   ++skip_evaluation;
15695   /* If it's a `(', then we might be looking at the type-id
15696      construction.  */
15697   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15698     {
15699       tree type;
15700       bool saved_in_type_id_in_expr_p;
15701
15702       /* We can't be sure yet whether we're looking at a type-id or an
15703          expression.  */
15704       cp_parser_parse_tentatively (parser);
15705       /* Consume the `('.  */
15706       cp_lexer_consume_token (parser->lexer);
15707       /* Parse the type-id.  */
15708       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15709       parser->in_type_id_in_expr_p = true;
15710       type = cp_parser_type_id (parser);
15711       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15712       /* Now, look for the trailing `)'.  */
15713       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15714       /* If all went well, then we're done.  */
15715       if (cp_parser_parse_definitely (parser))
15716         {
15717           cp_decl_specifier_seq decl_specs;
15718
15719           /* Build a trivial decl-specifier-seq.  */
15720           clear_decl_specs (&decl_specs);
15721           decl_specs.type = type;
15722
15723           /* Call grokdeclarator to figure out what type this is.  */
15724           expr = grokdeclarator (NULL,
15725                                  &decl_specs,
15726                                  TYPENAME,
15727                                  /*initialized=*/0,
15728                                  /*attrlist=*/NULL);
15729         }
15730     }
15731
15732   /* If the type-id production did not work out, then we must be
15733      looking at the unary-expression production.  */
15734   if (!expr)
15735     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15736                                        /*cast_p=*/false);
15737   /* Go back to evaluating expressions.  */
15738   --skip_evaluation;
15739
15740   /* Free the message we created.  */
15741   free ((char *) parser->type_definition_forbidden_message);
15742   /* And restore the old one.  */
15743   parser->type_definition_forbidden_message = saved_message;
15744   parser->integral_constant_expression_p
15745     = saved_integral_constant_expression_p;
15746   parser->non_integral_constant_expression_p
15747     = saved_non_integral_constant_expression_p;
15748
15749   return expr;
15750 }
15751
15752 /* If the current declaration has no declarator, return true.  */
15753
15754 static bool
15755 cp_parser_declares_only_class_p (cp_parser *parser)
15756 {
15757   /* If the next token is a `;' or a `,' then there is no
15758      declarator.  */
15759   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15760           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15761 }
15762
15763 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15764
15765 static void
15766 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15767                              cp_storage_class storage_class)
15768 {
15769   if (decl_specs->storage_class != sc_none)
15770     decl_specs->multiple_storage_classes_p = true;
15771   else
15772     decl_specs->storage_class = storage_class;
15773 }
15774
15775 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15776    is true, the type is a user-defined type; otherwise it is a
15777    built-in type specified by a keyword.  */
15778
15779 static void
15780 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15781                               tree type_spec,
15782                               bool user_defined_p)
15783 {
15784   decl_specs->any_specifiers_p = true;
15785
15786   /* If the user tries to redeclare bool or wchar_t (with, for
15787      example, in "typedef int wchar_t;") we remember that this is what
15788      happened.  In system headers, we ignore these declarations so
15789      that G++ can work with system headers that are not C++-safe.  */
15790   if (decl_specs->specs[(int) ds_typedef]
15791       && !user_defined_p
15792       && (type_spec == boolean_type_node
15793           || type_spec == wchar_type_node)
15794       && (decl_specs->type
15795           || decl_specs->specs[(int) ds_long]
15796           || decl_specs->specs[(int) ds_short]
15797           || decl_specs->specs[(int) ds_unsigned]
15798           || decl_specs->specs[(int) ds_signed]))
15799     {
15800       decl_specs->redefined_builtin_type = type_spec;
15801       if (!decl_specs->type)
15802         {
15803           decl_specs->type = type_spec;
15804           decl_specs->user_defined_type_p = false;
15805         }
15806     }
15807   else if (decl_specs->type)
15808     decl_specs->multiple_types_p = true;
15809   else
15810     {
15811       decl_specs->type = type_spec;
15812       decl_specs->user_defined_type_p = user_defined_p;
15813       decl_specs->redefined_builtin_type = NULL_TREE;
15814     }
15815 }
15816
15817 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15818    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15819
15820 static bool
15821 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15822 {
15823   return decl_specifiers->specs[(int) ds_friend] != 0;
15824 }
15825
15826 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15827    issue an error message indicating that TOKEN_DESC was expected.
15828
15829    Returns the token consumed, if the token had the appropriate type.
15830    Otherwise, returns NULL.  */
15831
15832 static cp_token *
15833 cp_parser_require (cp_parser* parser,
15834                    enum cpp_ttype type,
15835                    const char* token_desc)
15836 {
15837   if (cp_lexer_next_token_is (parser->lexer, type))
15838     return cp_lexer_consume_token (parser->lexer);
15839   else
15840     {
15841       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15842       if (!cp_parser_simulate_error (parser))
15843         {
15844           char *message = concat ("expected ", token_desc, NULL);
15845           cp_parser_error (parser, message);
15846           free (message);
15847         }
15848       return NULL;
15849     }
15850 }
15851
15852 /* Like cp_parser_require, except that tokens will be skipped until
15853    the desired token is found.  An error message is still produced if
15854    the next token is not as expected.  */
15855
15856 static void
15857 cp_parser_skip_until_found (cp_parser* parser,
15858                             enum cpp_ttype type,
15859                             const char* token_desc)
15860 {
15861   cp_token *token;
15862   unsigned nesting_depth = 0;
15863
15864   if (cp_parser_require (parser, type, token_desc))
15865     return;
15866
15867   /* Skip tokens until the desired token is found.  */
15868   while (true)
15869     {
15870       /* Peek at the next token.  */
15871       token = cp_lexer_peek_token (parser->lexer);
15872       /* If we've reached the token we want, consume it and
15873          stop.  */
15874       if (token->type == type && !nesting_depth)
15875         {
15876           cp_lexer_consume_token (parser->lexer);
15877           return;
15878         }
15879       /* If we've run out of tokens, stop.  */
15880       if (token->type == CPP_EOF)
15881         return;
15882       if (token->type == CPP_OPEN_BRACE
15883           || token->type == CPP_OPEN_PAREN
15884           || token->type == CPP_OPEN_SQUARE)
15885         ++nesting_depth;
15886       else if (token->type == CPP_CLOSE_BRACE
15887                || token->type == CPP_CLOSE_PAREN
15888                || token->type == CPP_CLOSE_SQUARE)
15889         {
15890           if (nesting_depth-- == 0)
15891             return;
15892         }
15893       /* Consume this token.  */
15894       cp_lexer_consume_token (parser->lexer);
15895     }
15896 }
15897
15898 /* If the next token is the indicated keyword, consume it.  Otherwise,
15899    issue an error message indicating that TOKEN_DESC was expected.
15900
15901    Returns the token consumed, if the token had the appropriate type.
15902    Otherwise, returns NULL.  */
15903
15904 static cp_token *
15905 cp_parser_require_keyword (cp_parser* parser,
15906                            enum rid keyword,
15907                            const char* token_desc)
15908 {
15909   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15910
15911   if (token && token->keyword != keyword)
15912     {
15913       dyn_string_t error_msg;
15914
15915       /* Format the error message.  */
15916       error_msg = dyn_string_new (0);
15917       dyn_string_append_cstr (error_msg, "expected ");
15918       dyn_string_append_cstr (error_msg, token_desc);
15919       cp_parser_error (parser, error_msg->s);
15920       dyn_string_delete (error_msg);
15921       return NULL;
15922     }
15923
15924   return token;
15925 }
15926
15927 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15928    function-definition.  */
15929
15930 static bool
15931 cp_parser_token_starts_function_definition_p (cp_token* token)
15932 {
15933   return (/* An ordinary function-body begins with an `{'.  */
15934           token->type == CPP_OPEN_BRACE
15935           /* A ctor-initializer begins with a `:'.  */
15936           || token->type == CPP_COLON
15937           /* A function-try-block begins with `try'.  */
15938           || token->keyword == RID_TRY
15939           /* The named return value extension begins with `return'.  */
15940           || token->keyword == RID_RETURN);
15941 }
15942
15943 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15944    definition.  */
15945
15946 static bool
15947 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15948 {
15949   cp_token *token;
15950
15951   token = cp_lexer_peek_token (parser->lexer);
15952   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15953 }
15954
15955 /* Returns TRUE iff the next token is the "," or ">" ending a
15956    template-argument.  */
15957
15958 static bool
15959 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15960 {
15961   cp_token *token;
15962
15963   token = cp_lexer_peek_token (parser->lexer);
15964   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15965 }
15966
15967 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15968    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15969
15970 static bool
15971 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15972                                                      size_t n)
15973 {
15974   cp_token *token;
15975
15976   token = cp_lexer_peek_nth_token (parser->lexer, n);
15977   if (token->type == CPP_LESS)
15978     return true;
15979   /* Check for the sequence `<::' in the original code. It would be lexed as
15980      `[:', where `[' is a digraph, and there is no whitespace before
15981      `:'.  */
15982   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15983     {
15984       cp_token *token2;
15985       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15986       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15987         return true;
15988     }
15989   return false;
15990 }
15991
15992 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15993    or none_type otherwise.  */
15994
15995 static enum tag_types
15996 cp_parser_token_is_class_key (cp_token* token)
15997 {
15998   switch (token->keyword)
15999     {
16000     case RID_CLASS:
16001       return class_type;
16002     case RID_STRUCT:
16003       return record_type;
16004     case RID_UNION:
16005       return union_type;
16006
16007     default:
16008       return none_type;
16009     }
16010 }
16011
16012 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16013
16014 static void
16015 cp_parser_check_class_key (enum tag_types class_key, tree type)
16016 {
16017   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16018     pedwarn ("%qs tag used in naming %q#T",
16019             class_key == union_type ? "union"
16020              : class_key == record_type ? "struct" : "class",
16021              type);
16022 }
16023
16024 /* Issue an error message if DECL is redeclared with different
16025    access than its original declaration [class.access.spec/3].
16026    This applies to nested classes and nested class templates.
16027    [class.mem/1].  */
16028
16029 static void
16030 cp_parser_check_access_in_redeclaration (tree decl)
16031 {
16032   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16033     return;
16034
16035   if ((TREE_PRIVATE (decl)
16036        != (current_access_specifier == access_private_node))
16037       || (TREE_PROTECTED (decl)
16038           != (current_access_specifier == access_protected_node)))
16039     error ("%qD redeclared with different access", decl);
16040 }
16041
16042 /* Look for the `template' keyword, as a syntactic disambiguator.
16043    Return TRUE iff it is present, in which case it will be
16044    consumed.  */
16045
16046 static bool
16047 cp_parser_optional_template_keyword (cp_parser *parser)
16048 {
16049   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16050     {
16051       /* The `template' keyword can only be used within templates;
16052          outside templates the parser can always figure out what is a
16053          template and what is not.  */
16054       if (!processing_template_decl)
16055         {
16056           error ("%<template%> (as a disambiguator) is only allowed "
16057                  "within templates");
16058           /* If this part of the token stream is rescanned, the same
16059              error message would be generated.  So, we purge the token
16060              from the stream.  */
16061           cp_lexer_purge_token (parser->lexer);
16062           return false;
16063         }
16064       else
16065         {
16066           /* Consume the `template' keyword.  */
16067           cp_lexer_consume_token (parser->lexer);
16068           return true;
16069         }
16070     }
16071
16072   return false;
16073 }
16074
16075 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16076    set PARSER->SCOPE, and perform other related actions.  */
16077
16078 static void
16079 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16080 {
16081   tree value;
16082   tree check;
16083
16084   /* Get the stored value.  */
16085   value = cp_lexer_consume_token (parser->lexer)->value;
16086   /* Perform any access checks that were deferred.  */
16087   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16088     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16089   /* Set the scope from the stored value.  */
16090   parser->scope = TREE_VALUE (value);
16091   parser->qualifying_scope = TREE_TYPE (value);
16092   parser->object_scope = NULL_TREE;
16093 }
16094
16095 /* Consume tokens up through a non-nested END token.  */
16096
16097 static void
16098 cp_parser_cache_group (cp_parser *parser,
16099                        enum cpp_ttype end,
16100                        unsigned depth)
16101 {
16102   while (true)
16103     {
16104       cp_token *token;
16105
16106       /* Abort a parenthesized expression if we encounter a brace.  */
16107       if ((end == CPP_CLOSE_PAREN || depth == 0)
16108           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16109         return;
16110       /* If we've reached the end of the file, stop.  */
16111       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16112         return;
16113       /* Consume the next token.  */
16114       token = cp_lexer_consume_token (parser->lexer);
16115       /* See if it starts a new group.  */
16116       if (token->type == CPP_OPEN_BRACE)
16117         {
16118           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16119           if (depth == 0)
16120             return;
16121         }
16122       else if (token->type == CPP_OPEN_PAREN)
16123         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16124       else if (token->type == end)
16125         return;
16126     }
16127 }
16128
16129 /* Begin parsing tentatively.  We always save tokens while parsing
16130    tentatively so that if the tentative parsing fails we can restore the
16131    tokens.  */
16132
16133 static void
16134 cp_parser_parse_tentatively (cp_parser* parser)
16135 {
16136   /* Enter a new parsing context.  */
16137   parser->context = cp_parser_context_new (parser->context);
16138   /* Begin saving tokens.  */
16139   cp_lexer_save_tokens (parser->lexer);
16140   /* In order to avoid repetitive access control error messages,
16141      access checks are queued up until we are no longer parsing
16142      tentatively.  */
16143   push_deferring_access_checks (dk_deferred);
16144 }
16145
16146 /* Commit to the currently active tentative parse.  */
16147
16148 static void
16149 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16150 {
16151   cp_parser_context *context;
16152   cp_lexer *lexer;
16153
16154   /* Mark all of the levels as committed.  */
16155   lexer = parser->lexer;
16156   for (context = parser->context; context->next; context = context->next)
16157     {
16158       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16159         break;
16160       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16161       while (!cp_lexer_saving_tokens (lexer))
16162         lexer = lexer->next;
16163       cp_lexer_commit_tokens (lexer);
16164     }
16165 }
16166
16167 /* Abort the currently active tentative parse.  All consumed tokens
16168    will be rolled back, and no diagnostics will be issued.  */
16169
16170 static void
16171 cp_parser_abort_tentative_parse (cp_parser* parser)
16172 {
16173   cp_parser_simulate_error (parser);
16174   /* Now, pretend that we want to see if the construct was
16175      successfully parsed.  */
16176   cp_parser_parse_definitely (parser);
16177 }
16178
16179 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16180    token stream.  Otherwise, commit to the tokens we have consumed.
16181    Returns true if no error occurred; false otherwise.  */
16182
16183 static bool
16184 cp_parser_parse_definitely (cp_parser* parser)
16185 {
16186   bool error_occurred;
16187   cp_parser_context *context;
16188
16189   /* Remember whether or not an error occurred, since we are about to
16190      destroy that information.  */
16191   error_occurred = cp_parser_error_occurred (parser);
16192   /* Remove the topmost context from the stack.  */
16193   context = parser->context;
16194   parser->context = context->next;
16195   /* If no parse errors occurred, commit to the tentative parse.  */
16196   if (!error_occurred)
16197     {
16198       /* Commit to the tokens read tentatively, unless that was
16199          already done.  */
16200       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16201         cp_lexer_commit_tokens (parser->lexer);
16202
16203       pop_to_parent_deferring_access_checks ();
16204     }
16205   /* Otherwise, if errors occurred, roll back our state so that things
16206      are just as they were before we began the tentative parse.  */
16207   else
16208     {
16209       cp_lexer_rollback_tokens (parser->lexer);
16210       pop_deferring_access_checks ();
16211     }
16212   /* Add the context to the front of the free list.  */
16213   context->next = cp_parser_context_free_list;
16214   cp_parser_context_free_list = context;
16215
16216   return !error_occurred;
16217 }
16218
16219 /* Returns true if we are parsing tentatively and are not committed to
16220    this tentative parse.  */
16221
16222 static bool
16223 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16224 {
16225   return (cp_parser_parsing_tentatively (parser)
16226           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16227 }
16228
16229 /* Returns nonzero iff an error has occurred during the most recent
16230    tentative parse.  */
16231
16232 static bool
16233 cp_parser_error_occurred (cp_parser* parser)
16234 {
16235   return (cp_parser_parsing_tentatively (parser)
16236           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16237 }
16238
16239 /* Returns nonzero if GNU extensions are allowed.  */
16240
16241 static bool
16242 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16243 {
16244   return parser->allow_gnu_extensions_p;
16245 }
16246 \f
16247 /* Objective-C++ Productions */
16248
16249
16250 /* Parse an Objective-C expression, which feeds into a primary-expression
16251    above.
16252
16253    objc-expression:
16254      objc-message-expression
16255      objc-string-literal
16256      objc-encode-expression
16257      objc-protocol-expression
16258      objc-selector-expression
16259
16260   Returns a tree representation of the expression.  */
16261
16262 static tree
16263 cp_parser_objc_expression (cp_parser* parser)
16264 {
16265   /* Try to figure out what kind of declaration is present.  */
16266   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16267
16268   switch (kwd->type)
16269     {
16270     case CPP_OPEN_SQUARE:
16271       return cp_parser_objc_message_expression (parser);
16272
16273     case CPP_OBJC_STRING:
16274       kwd = cp_lexer_consume_token (parser->lexer);
16275       return objc_build_string_object (kwd->value);
16276
16277     case CPP_KEYWORD:
16278       switch (kwd->keyword)
16279         {
16280         case RID_AT_ENCODE:
16281           return cp_parser_objc_encode_expression (parser);
16282
16283         case RID_AT_PROTOCOL:
16284           return cp_parser_objc_protocol_expression (parser);
16285
16286         case RID_AT_SELECTOR:
16287           return cp_parser_objc_selector_expression (parser);
16288
16289         default:
16290           break;
16291         }
16292     default:
16293       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16294       cp_parser_skip_to_end_of_block_or_statement (parser);
16295     }
16296
16297   return error_mark_node;
16298 }
16299
16300 /* Parse an Objective-C message expression.
16301
16302    objc-message-expression:
16303      [ objc-message-receiver objc-message-args ]
16304
16305    Returns a representation of an Objective-C message.  */
16306
16307 static tree
16308 cp_parser_objc_message_expression (cp_parser* parser)
16309 {
16310   tree receiver, messageargs;
16311
16312   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16313   receiver = cp_parser_objc_message_receiver (parser);
16314   messageargs = cp_parser_objc_message_args (parser);
16315   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16316
16317   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16318 }
16319
16320 /* Parse an objc-message-receiver.
16321
16322    objc-message-receiver:
16323      expression
16324      simple-type-specifier
16325
16326   Returns a representation of the type or expression.  */
16327
16328 static tree
16329 cp_parser_objc_message_receiver (cp_parser* parser)
16330 {
16331   tree rcv;
16332
16333   /* An Objective-C message receiver may be either (1) a type
16334      or (2) an expression.  */
16335   cp_parser_parse_tentatively (parser);
16336   rcv = cp_parser_expression (parser, false);
16337
16338   if (cp_parser_parse_definitely (parser))
16339     return rcv;
16340
16341   rcv = cp_parser_simple_type_specifier (parser,
16342                                          /*decl_specs=*/NULL,
16343                                          CP_PARSER_FLAGS_NONE);
16344
16345   return objc_get_class_reference (rcv);
16346 }
16347
16348 /* Parse the arguments and selectors comprising an Objective-C message.
16349
16350    objc-message-args:
16351      objc-selector
16352      objc-selector-args
16353      objc-selector-args , objc-comma-args
16354
16355    objc-selector-args:
16356      objc-selector [opt] : assignment-expression
16357      objc-selector-args objc-selector [opt] : assignment-expression
16358
16359    objc-comma-args:
16360      assignment-expression
16361      objc-comma-args , assignment-expression
16362
16363    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16364    selector arguments and TREE_VALUE containing a list of comma
16365    arguments.  */
16366
16367 static tree
16368 cp_parser_objc_message_args (cp_parser* parser)
16369 {
16370   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16371   bool maybe_unary_selector_p = true;
16372   cp_token *token = cp_lexer_peek_token (parser->lexer);
16373
16374   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16375     {
16376       tree selector = NULL_TREE, arg;
16377
16378       if (token->type != CPP_COLON)
16379         selector = cp_parser_objc_selector (parser);
16380
16381       /* Detect if we have a unary selector.  */
16382       if (maybe_unary_selector_p
16383           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16384         return build_tree_list (selector, NULL_TREE);
16385
16386       maybe_unary_selector_p = false;
16387       cp_parser_require (parser, CPP_COLON, "`:'");
16388       arg = cp_parser_assignment_expression (parser, false);
16389
16390       sel_args
16391         = chainon (sel_args,
16392                    build_tree_list (selector, arg));
16393
16394       token = cp_lexer_peek_token (parser->lexer);
16395     }
16396
16397   /* Handle non-selector arguments, if any. */
16398   while (token->type == CPP_COMMA)
16399     {
16400       tree arg;
16401
16402       cp_lexer_consume_token (parser->lexer);
16403       arg = cp_parser_assignment_expression (parser, false);
16404
16405       addl_args
16406         = chainon (addl_args,
16407                    build_tree_list (NULL_TREE, arg));
16408
16409       token = cp_lexer_peek_token (parser->lexer);
16410     }
16411
16412   return build_tree_list (sel_args, addl_args);
16413 }
16414
16415 /* Parse an Objective-C encode expression.
16416
16417    objc-encode-expression:
16418      @encode objc-typename
16419
16420    Returns an encoded representation of the type argument.  */
16421
16422 static tree
16423 cp_parser_objc_encode_expression (cp_parser* parser)
16424 {
16425   tree type;
16426
16427   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16428   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16429   type = complete_type (cp_parser_type_id (parser));
16430   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16431
16432   if (!type)
16433     {
16434       error ("%<@encode%> must specify a type as an argument");
16435       return error_mark_node;
16436     }
16437
16438   return objc_build_encode_expr (type);
16439 }
16440
16441 /* Parse an Objective-C @defs expression.  */
16442
16443 static tree
16444 cp_parser_objc_defs_expression (cp_parser *parser)
16445 {
16446   tree name;
16447
16448   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16449   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16450   name = cp_parser_identifier (parser);
16451   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16452
16453   return objc_get_class_ivars (name);
16454 }
16455
16456 /* Parse an Objective-C protocol expression.
16457
16458   objc-protocol-expression:
16459     @protocol ( identifier )
16460
16461   Returns a representation of the protocol expression.  */
16462
16463 static tree
16464 cp_parser_objc_protocol_expression (cp_parser* parser)
16465 {
16466   tree proto;
16467
16468   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16469   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16470   proto = cp_parser_identifier (parser);
16471   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16472
16473   return objc_build_protocol_expr (proto);
16474 }
16475
16476 /* Parse an Objective-C selector expression.
16477
16478    objc-selector-expression:
16479      @selector ( objc-method-signature )
16480
16481    objc-method-signature:
16482      objc-selector
16483      objc-selector-seq
16484
16485    objc-selector-seq:
16486      objc-selector :
16487      objc-selector-seq objc-selector :
16488
16489   Returns a representation of the method selector.  */
16490
16491 static tree
16492 cp_parser_objc_selector_expression (cp_parser* parser)
16493 {
16494   tree sel_seq = NULL_TREE;
16495   bool maybe_unary_selector_p = true;
16496   cp_token *token;
16497
16498   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16499   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16500   token = cp_lexer_peek_token (parser->lexer);
16501
16502   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16503     {
16504       tree selector = NULL_TREE;
16505
16506       if (token->type != CPP_COLON)
16507         selector = cp_parser_objc_selector (parser);
16508
16509       /* Detect if we have a unary selector.  */
16510       if (maybe_unary_selector_p
16511           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16512         {
16513           sel_seq = selector;
16514           goto finish_selector;
16515         }
16516
16517       maybe_unary_selector_p = false;
16518       cp_parser_require (parser, CPP_COLON, "`:'");
16519
16520       sel_seq
16521         = chainon (sel_seq,
16522                    build_tree_list (selector, NULL_TREE));
16523
16524       token = cp_lexer_peek_token (parser->lexer);
16525     }
16526
16527  finish_selector:
16528   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16529
16530   return objc_build_selector_expr (sel_seq);
16531 }
16532
16533 /* Parse a list of identifiers.
16534
16535    objc-identifier-list:
16536      identifier
16537      objc-identifier-list , identifier
16538
16539    Returns a TREE_LIST of identifier nodes.  */
16540
16541 static tree
16542 cp_parser_objc_identifier_list (cp_parser* parser)
16543 {
16544   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16545   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16546
16547   while (sep->type == CPP_COMMA)
16548     {
16549       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16550       list = chainon (list,
16551                       build_tree_list (NULL_TREE,
16552                                        cp_parser_identifier (parser)));
16553       sep = cp_lexer_peek_token (parser->lexer);
16554     }
16555
16556   return list;
16557 }
16558
16559 /* Parse an Objective-C alias declaration.
16560
16561    objc-alias-declaration:
16562      @compatibility_alias identifier identifier ;
16563
16564    This function registers the alias mapping with the Objective-C front-end.
16565    It returns nothing.  */
16566
16567 static void
16568 cp_parser_objc_alias_declaration (cp_parser* parser)
16569 {
16570   tree alias, orig;
16571
16572   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16573   alias = cp_parser_identifier (parser);
16574   orig = cp_parser_identifier (parser);
16575   objc_declare_alias (alias, orig);
16576   cp_parser_consume_semicolon_at_end_of_statement (parser);
16577 }
16578
16579 /* Parse an Objective-C class forward-declaration.
16580
16581    objc-class-declaration:
16582      @class objc-identifier-list ;
16583
16584    The function registers the forward declarations with the Objective-C
16585    front-end.  It returns nothing.  */
16586
16587 static void
16588 cp_parser_objc_class_declaration (cp_parser* parser)
16589 {
16590   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16591   objc_declare_class (cp_parser_objc_identifier_list (parser));
16592   cp_parser_consume_semicolon_at_end_of_statement (parser);
16593 }
16594
16595 /* Parse a list of Objective-C protocol references.
16596
16597    objc-protocol-refs-opt:
16598      objc-protocol-refs [opt]
16599
16600    objc-protocol-refs:
16601      < objc-identifier-list >
16602
16603    Returns a TREE_LIST of identifiers, if any.  */
16604
16605 static tree
16606 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16607 {
16608   tree protorefs = NULL_TREE;
16609
16610   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16611     {
16612       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16613       protorefs = cp_parser_objc_identifier_list (parser);
16614       cp_parser_require (parser, CPP_GREATER, "`>'");
16615     }
16616
16617   return protorefs;
16618 }
16619
16620 /* Parse a Objective-C visibility specification.  */
16621
16622 static void
16623 cp_parser_objc_visibility_spec (cp_parser* parser)
16624 {
16625   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16626
16627   switch (vis->keyword)
16628     {
16629     case RID_AT_PRIVATE:
16630       objc_set_visibility (2);
16631       break;
16632     case RID_AT_PROTECTED:
16633       objc_set_visibility (0);
16634       break;
16635     case RID_AT_PUBLIC:
16636       objc_set_visibility (1);
16637       break;
16638     default:
16639       return;
16640     }
16641
16642   /* Eat '@private'/'@protected'/'@public'.  */
16643   cp_lexer_consume_token (parser->lexer);
16644 }
16645
16646 /* Parse an Objective-C method type.  */
16647
16648 static void
16649 cp_parser_objc_method_type (cp_parser* parser)
16650 {
16651   objc_set_method_type
16652    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16653     ? PLUS_EXPR
16654     : MINUS_EXPR);
16655 }
16656
16657 /* Parse an Objective-C protocol qualifier.  */
16658
16659 static tree
16660 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16661 {
16662   tree quals = NULL_TREE, node;
16663   cp_token *token = cp_lexer_peek_token (parser->lexer);
16664
16665   node = token->value;
16666
16667   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16668          && (node == ridpointers [(int) RID_IN]
16669              || node == ridpointers [(int) RID_OUT]
16670              || node == ridpointers [(int) RID_INOUT]
16671              || node == ridpointers [(int) RID_BYCOPY]
16672              || node == ridpointers [(int) RID_BYREF]
16673              || node == ridpointers [(int) RID_ONEWAY]))
16674     {
16675       quals = tree_cons (NULL_TREE, node, quals);
16676       cp_lexer_consume_token (parser->lexer);
16677       token = cp_lexer_peek_token (parser->lexer);
16678       node = token->value;
16679     }
16680
16681   return quals;
16682 }
16683
16684 /* Parse an Objective-C typename.  */
16685
16686 static tree
16687 cp_parser_objc_typename (cp_parser* parser)
16688 {
16689   tree typename = NULL_TREE;
16690
16691   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16692     {
16693       tree proto_quals, cp_type = NULL_TREE;
16694
16695       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16696       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16697
16698       /* An ObjC type name may consist of just protocol qualifiers, in which
16699          case the type shall default to 'id'.  */
16700       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16701         cp_type = cp_parser_type_id (parser);
16702
16703       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16704       typename = build_tree_list (proto_quals, cp_type);
16705     }
16706
16707   return typename;
16708 }
16709
16710 /* Check to see if TYPE refers to an Objective-C selector name.  */
16711
16712 static bool
16713 cp_parser_objc_selector_p (enum cpp_ttype type)
16714 {
16715   return (type == CPP_NAME || type == CPP_KEYWORD
16716           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16717           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16718           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16719           || type == CPP_XOR || type == CPP_XOR_EQ);
16720 }
16721
16722 /* Parse an Objective-C selector.  */
16723
16724 static tree
16725 cp_parser_objc_selector (cp_parser* parser)
16726 {
16727   cp_token *token = cp_lexer_consume_token (parser->lexer);
16728
16729   if (!cp_parser_objc_selector_p (token->type))
16730     {
16731       error ("invalid Objective-C++ selector name");
16732       return error_mark_node;
16733     }
16734
16735   /* C++ operator names are allowed to appear in ObjC selectors.  */
16736   switch (token->type)
16737     {
16738     case CPP_AND_AND: return get_identifier ("and");
16739     case CPP_AND_EQ: return get_identifier ("and_eq");
16740     case CPP_AND: return get_identifier ("bitand");
16741     case CPP_OR: return get_identifier ("bitor");
16742     case CPP_COMPL: return get_identifier ("compl");
16743     case CPP_NOT: return get_identifier ("not");
16744     case CPP_NOT_EQ: return get_identifier ("not_eq");
16745     case CPP_OR_OR: return get_identifier ("or");
16746     case CPP_OR_EQ: return get_identifier ("or_eq");
16747     case CPP_XOR: return get_identifier ("xor");
16748     case CPP_XOR_EQ: return get_identifier ("xor_eq");
16749     default: return token->value;
16750     }
16751 }
16752
16753 /* Parse an Objective-C params list.  */
16754
16755 static tree
16756 cp_parser_objc_method_keyword_params (cp_parser* parser)
16757 {
16758   tree params = NULL_TREE;
16759   bool maybe_unary_selector_p = true;
16760   cp_token *token = cp_lexer_peek_token (parser->lexer);
16761
16762   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16763     {
16764       tree selector = NULL_TREE, typename, identifier;
16765
16766       if (token->type != CPP_COLON)
16767         selector = cp_parser_objc_selector (parser);
16768
16769       /* Detect if we have a unary selector.  */
16770       if (maybe_unary_selector_p
16771           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16772         return selector;
16773
16774       maybe_unary_selector_p = false;
16775       cp_parser_require (parser, CPP_COLON, "`:'");
16776       typename = cp_parser_objc_typename (parser);
16777       identifier = cp_parser_identifier (parser);
16778
16779       params
16780         = chainon (params,
16781                    objc_build_keyword_decl (selector,
16782                                             typename,
16783                                             identifier));
16784
16785       token = cp_lexer_peek_token (parser->lexer);
16786     }
16787
16788   return params;
16789 }
16790
16791 /* Parse the non-keyword Objective-C params.  */
16792
16793 static tree
16794 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16795 {
16796   tree params = make_node (TREE_LIST);
16797   cp_token *token = cp_lexer_peek_token (parser->lexer);
16798   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
16799
16800   while (token->type == CPP_COMMA)
16801     {
16802       cp_parameter_declarator *parmdecl;
16803       tree parm;
16804
16805       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16806       token = cp_lexer_peek_token (parser->lexer);
16807
16808       if (token->type == CPP_ELLIPSIS)
16809         {
16810           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
16811           *ellipsisp = true;
16812           break;
16813         }
16814
16815       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16816       parm = grokdeclarator (parmdecl->declarator,
16817                              &parmdecl->decl_specifiers,
16818                              PARM, /*initialized=*/0,
16819                              /*attrlist=*/NULL);
16820
16821       chainon (params, build_tree_list (NULL_TREE, parm));
16822       token = cp_lexer_peek_token (parser->lexer);
16823     }
16824
16825   return params;
16826 }
16827
16828 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
16829
16830 static void
16831 cp_parser_objc_interstitial_code (cp_parser* parser)
16832 {
16833   cp_token *token = cp_lexer_peek_token (parser->lexer);
16834
16835   /* If the next token is `extern' and the following token is a string
16836      literal, then we have a linkage specification.  */
16837   if (token->keyword == RID_EXTERN
16838       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16839     cp_parser_linkage_specification (parser);
16840   /* Handle #pragma, if any.  */
16841   else if (token->type == CPP_PRAGMA)
16842     cp_lexer_handle_pragma (parser->lexer);
16843   /* Allow stray semicolons.  */
16844   else if (token->type == CPP_SEMICOLON)
16845     cp_lexer_consume_token (parser->lexer);
16846   /* Finally, try to parse a block-declaration, or a function-definition.  */
16847   else
16848     cp_parser_block_declaration (parser, /*statement_p=*/false);
16849 }
16850
16851 /* Parse a method signature.  */
16852
16853 static tree
16854 cp_parser_objc_method_signature (cp_parser* parser)
16855 {
16856   tree rettype, kwdparms, optparms;
16857   bool ellipsis = false;
16858
16859   cp_parser_objc_method_type (parser);
16860   rettype = cp_parser_objc_typename (parser);
16861   kwdparms = cp_parser_objc_method_keyword_params (parser);
16862   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16863
16864   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16865 }
16866
16867 /* Pars an Objective-C method prototype list.  */
16868
16869 static void
16870 cp_parser_objc_method_prototype_list (cp_parser* parser)
16871 {
16872   cp_token *token = cp_lexer_peek_token (parser->lexer);
16873
16874   while (token->keyword != RID_AT_END)
16875     {
16876       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16877         {
16878           objc_add_method_declaration
16879            (cp_parser_objc_method_signature (parser));
16880           cp_parser_consume_semicolon_at_end_of_statement (parser);
16881         }
16882       else
16883         /* Allow for interspersed non-ObjC++ code.  */
16884         cp_parser_objc_interstitial_code (parser);
16885
16886       token = cp_lexer_peek_token (parser->lexer);
16887     }
16888
16889   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16890   objc_finish_interface ();
16891 }
16892
16893 /* Parse an Objective-C method definition list.  */
16894
16895 static void
16896 cp_parser_objc_method_definition_list (cp_parser* parser)
16897 {
16898   cp_token *token = cp_lexer_peek_token (parser->lexer);
16899
16900   while (token->keyword != RID_AT_END)
16901     {
16902       tree meth;
16903
16904       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16905         {
16906           push_deferring_access_checks (dk_deferred);
16907           objc_start_method_definition
16908            (cp_parser_objc_method_signature (parser));
16909
16910           /* For historical reasons, we accept an optional semicolon.  */
16911           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16912             cp_lexer_consume_token (parser->lexer);
16913
16914           perform_deferred_access_checks ();
16915           stop_deferring_access_checks ();
16916           meth = cp_parser_function_definition_after_declarator (parser,
16917                                                                  false);
16918           pop_deferring_access_checks ();
16919           objc_finish_method_definition (meth);
16920         }
16921       else
16922         /* Allow for interspersed non-ObjC++ code.  */
16923         cp_parser_objc_interstitial_code (parser);
16924
16925       token = cp_lexer_peek_token (parser->lexer);
16926     }
16927
16928   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16929   objc_finish_implementation ();
16930 }
16931
16932 /* Parse Objective-C ivars.  */
16933
16934 static void
16935 cp_parser_objc_class_ivars (cp_parser* parser)
16936 {
16937   cp_token *token = cp_lexer_peek_token (parser->lexer);
16938
16939   if (token->type != CPP_OPEN_BRACE)
16940     return;     /* No ivars specified.  */
16941
16942   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
16943   token = cp_lexer_peek_token (parser->lexer);
16944
16945   while (token->type != CPP_CLOSE_BRACE)
16946     {
16947       cp_decl_specifier_seq declspecs;
16948       int decl_class_or_enum_p;
16949       tree prefix_attributes;
16950
16951       cp_parser_objc_visibility_spec (parser);
16952
16953       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16954         break;
16955
16956       cp_parser_decl_specifier_seq (parser,
16957                                     CP_PARSER_FLAGS_OPTIONAL,
16958                                     &declspecs,
16959                                     &decl_class_or_enum_p);
16960       prefix_attributes = declspecs.attributes;
16961       declspecs.attributes = NULL_TREE;
16962
16963       /* Keep going until we hit the `;' at the end of the
16964          declaration.  */
16965       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16966         {
16967           tree width = NULL_TREE, attributes, first_attribute, decl;
16968           cp_declarator *declarator = NULL;
16969           int ctor_dtor_or_conv_p;
16970
16971           /* Check for a (possibly unnamed) bitfield declaration.  */
16972           token = cp_lexer_peek_token (parser->lexer);
16973           if (token->type == CPP_COLON)
16974             goto eat_colon;
16975
16976           if (token->type == CPP_NAME
16977               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16978                   == CPP_COLON))
16979             {
16980               /* Get the name of the bitfield.  */
16981               declarator = make_id_declarator (NULL_TREE,
16982                                                cp_parser_identifier (parser));
16983
16984              eat_colon:
16985               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
16986               /* Get the width of the bitfield.  */
16987               width
16988                 = cp_parser_constant_expression (parser,
16989                                                  /*allow_non_constant=*/false,
16990                                                  NULL);
16991             }
16992           else
16993             {
16994               /* Parse the declarator.  */
16995               declarator
16996                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16997                                         &ctor_dtor_or_conv_p,
16998                                         /*parenthesized_p=*/NULL,
16999                                         /*member_p=*/false);
17000             }
17001
17002           /* Look for attributes that apply to the ivar.  */
17003           attributes = cp_parser_attributes_opt (parser);
17004           /* Remember which attributes are prefix attributes and
17005              which are not.  */
17006           first_attribute = attributes;
17007           /* Combine the attributes.  */
17008           attributes = chainon (prefix_attributes, attributes);
17009
17010           if (width)
17011             {
17012               /* Create the bitfield declaration.  */
17013               decl = grokbitfield (declarator, &declspecs, width);
17014               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17015             }
17016           else
17017             decl = grokfield (declarator, &declspecs, NULL_TREE,
17018                               NULL_TREE, attributes);
17019
17020           /* Add the instance variable.  */
17021           objc_add_instance_variable (decl);
17022
17023           /* Reset PREFIX_ATTRIBUTES.  */
17024           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17025             attributes = TREE_CHAIN (attributes);
17026           if (attributes)
17027             TREE_CHAIN (attributes) = NULL_TREE;
17028
17029           token = cp_lexer_peek_token (parser->lexer);
17030
17031           if (token->type == CPP_COMMA)
17032             {
17033               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17034               continue;
17035             }
17036           break;
17037         }
17038
17039       cp_parser_consume_semicolon_at_end_of_statement (parser);
17040       token = cp_lexer_peek_token (parser->lexer);
17041     }
17042
17043   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17044   /* For historical reasons, we accept an optional semicolon.  */
17045   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17046     cp_lexer_consume_token (parser->lexer);
17047 }
17048
17049 /* Parse an Objective-C protocol declaration.  */
17050
17051 static void
17052 cp_parser_objc_protocol_declaration (cp_parser* parser)
17053 {
17054   tree proto, protorefs;
17055   cp_token *tok;
17056
17057   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17058   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17059     {
17060       error ("identifier expected after %<@protocol%>");
17061       goto finish;
17062     }
17063
17064   /* See if we have a forward declaration or a definition.  */
17065   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17066
17067   /* Try a forward declaration first.  */
17068   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17069     {
17070       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17071      finish:
17072       cp_parser_consume_semicolon_at_end_of_statement (parser);
17073     }
17074
17075   /* Ok, we got a full-fledged definition (or at least should).  */
17076   else
17077     {
17078       proto = cp_parser_identifier (parser);
17079       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17080       objc_start_protocol (proto, protorefs);
17081       cp_parser_objc_method_prototype_list (parser);
17082     }
17083 }
17084
17085 /* Parse an Objective-C superclass or category.  */
17086
17087 static void
17088 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17089                                                           tree *categ)
17090 {
17091   cp_token *next = cp_lexer_peek_token (parser->lexer);
17092
17093   *super = *categ = NULL_TREE;
17094   if (next->type == CPP_COLON)
17095     {
17096       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17097       *super = cp_parser_identifier (parser);
17098     }
17099   else if (next->type == CPP_OPEN_PAREN)
17100     {
17101       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17102       *categ = cp_parser_identifier (parser);
17103       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17104     }
17105 }
17106
17107 /* Parse an Objective-C class interface.  */
17108
17109 static void
17110 cp_parser_objc_class_interface (cp_parser* parser)
17111 {
17112   tree name, super, categ, protos;
17113
17114   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17115   name = cp_parser_identifier (parser);
17116   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17117   protos = cp_parser_objc_protocol_refs_opt (parser);
17118
17119   /* We have either a class or a category on our hands.  */
17120   if (categ)
17121     objc_start_category_interface (name, categ, protos);
17122   else
17123     {
17124       objc_start_class_interface (name, super, protos);
17125       /* Handle instance variable declarations, if any.  */
17126       cp_parser_objc_class_ivars (parser);
17127       objc_continue_interface ();
17128     }
17129
17130   cp_parser_objc_method_prototype_list (parser);
17131 }
17132
17133 /* Parse an Objective-C class implementation.  */
17134
17135 static void
17136 cp_parser_objc_class_implementation (cp_parser* parser)
17137 {
17138   tree name, super, categ;
17139
17140   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17141   name = cp_parser_identifier (parser);
17142   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17143
17144   /* We have either a class or a category on our hands.  */
17145   if (categ)
17146     objc_start_category_implementation (name, categ);
17147   else
17148     {
17149       objc_start_class_implementation (name, super);
17150       /* Handle instance variable declarations, if any.  */
17151       cp_parser_objc_class_ivars (parser);
17152       objc_continue_implementation ();
17153     }
17154
17155   cp_parser_objc_method_definition_list (parser);
17156 }
17157
17158 /* Consume the @end token and finish off the implementation.  */
17159
17160 static void
17161 cp_parser_objc_end_implementation (cp_parser* parser)
17162 {
17163   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17164   objc_finish_implementation ();
17165 }
17166
17167 /* Parse an Objective-C declaration.  */
17168
17169 static void
17170 cp_parser_objc_declaration (cp_parser* parser)
17171 {
17172   /* Try to figure out what kind of declaration is present.  */
17173   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17174
17175   switch (kwd->keyword)
17176     {
17177     case RID_AT_ALIAS:
17178       cp_parser_objc_alias_declaration (parser);
17179       break;
17180     case RID_AT_CLASS:
17181       cp_parser_objc_class_declaration (parser);
17182       break;
17183     case RID_AT_PROTOCOL:
17184       cp_parser_objc_protocol_declaration (parser);
17185       break;
17186     case RID_AT_INTERFACE:
17187       cp_parser_objc_class_interface (parser);
17188       break;
17189     case RID_AT_IMPLEMENTATION:
17190       cp_parser_objc_class_implementation (parser);
17191       break;
17192     case RID_AT_END:
17193       cp_parser_objc_end_implementation (parser);
17194       break;
17195     default:
17196       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17197       cp_parser_skip_to_end_of_block_or_statement (parser);
17198     }
17199 }
17200
17201 /* Parse an Objective-C try-catch-finally statement.
17202
17203    objc-try-catch-finally-stmt:
17204      @try compound-statement objc-catch-clause-seq [opt]
17205        objc-finally-clause [opt]
17206
17207    objc-catch-clause-seq:
17208      objc-catch-clause objc-catch-clause-seq [opt]
17209
17210    objc-catch-clause:
17211      @catch ( exception-declaration ) compound-statement
17212
17213    objc-finally-clause
17214      @finally compound-statement
17215
17216    Returns NULL_TREE.  */
17217
17218 static tree
17219 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17220   location_t location;
17221   tree stmt;
17222
17223   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17224   location = cp_lexer_peek_token (parser->lexer)->location;
17225   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17226      node, lest it get absorbed into the surrounding block.  */
17227   stmt = push_stmt_list ();
17228   cp_parser_compound_statement (parser, NULL, false);
17229   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17230
17231   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17232     {
17233       cp_parameter_declarator *parmdecl;
17234       tree parm;
17235
17236       cp_lexer_consume_token (parser->lexer);
17237       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17238       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17239       parm = grokdeclarator (parmdecl->declarator,
17240                              &parmdecl->decl_specifiers,
17241                              PARM, /*initialized=*/0,
17242                              /*attrlist=*/NULL);
17243       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17244       objc_begin_catch_clause (parm);
17245       cp_parser_compound_statement (parser, NULL, false);
17246       objc_finish_catch_clause ();
17247     }
17248
17249   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17250     {
17251       cp_lexer_consume_token (parser->lexer);
17252       location = cp_lexer_peek_token (parser->lexer)->location;
17253       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17254          node, lest it get absorbed into the surrounding block.  */
17255       stmt = push_stmt_list ();
17256       cp_parser_compound_statement (parser, NULL, false);
17257       objc_build_finally_clause (location, pop_stmt_list (stmt));
17258     }
17259
17260   return objc_finish_try_stmt ();
17261 }
17262
17263 /* Parse an Objective-C synchronized statement.
17264
17265    objc-synchronized-stmt:
17266      @synchronized ( expression ) compound-statement
17267
17268    Returns NULL_TREE.  */
17269
17270 static tree
17271 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17272   location_t location;
17273   tree lock, stmt;
17274
17275   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17276
17277   location = cp_lexer_peek_token (parser->lexer)->location;
17278   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17279   lock = cp_parser_expression (parser, false);
17280   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17281
17282   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17283      node, lest it get absorbed into the surrounding block.  */
17284   stmt = push_stmt_list ();
17285   cp_parser_compound_statement (parser, NULL, false);
17286
17287   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17288 }
17289
17290 /* Parse an Objective-C throw statement.
17291
17292    objc-throw-stmt:
17293      @throw assignment-expression [opt] ;
17294
17295    Returns a constructed '@throw' statement.  */
17296
17297 static tree
17298 cp_parser_objc_throw_statement (cp_parser *parser) {
17299   tree expr = NULL_TREE;
17300
17301   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17302
17303   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17304     expr = cp_parser_assignment_expression (parser, false);
17305
17306   cp_parser_consume_semicolon_at_end_of_statement (parser);
17307
17308   return objc_build_throw_stmt (expr);
17309 }
17310
17311 /* Parse an Objective-C statement.  */
17312
17313 static tree
17314 cp_parser_objc_statement (cp_parser * parser) {
17315   /* Try to figure out what kind of declaration is present.  */
17316   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17317
17318   switch (kwd->keyword)
17319     {
17320     case RID_AT_TRY:
17321       return cp_parser_objc_try_catch_finally_statement (parser);
17322     case RID_AT_SYNCHRONIZED:
17323       return cp_parser_objc_synchronized_statement (parser);
17324     case RID_AT_THROW:
17325       return cp_parser_objc_throw_statement (parser);
17326     default:
17327       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17328       cp_parser_skip_to_end_of_block_or_statement (parser);
17329     }
17330
17331   return error_mark_node;
17332 }
17333 \f
17334 /* The parser.  */
17335
17336 static GTY (()) cp_parser *the_parser;
17337
17338 /* External interface.  */
17339
17340 /* Parse one entire translation unit.  */
17341
17342 void
17343 c_parse_file (void)
17344 {
17345   bool error_occurred;
17346   static bool already_called = false;
17347
17348   if (already_called)
17349     {
17350       sorry ("inter-module optimizations not implemented for C++");
17351       return;
17352     }
17353   already_called = true;
17354
17355   the_parser = cp_parser_new ();
17356   push_deferring_access_checks (flag_access_control
17357                                 ? dk_no_deferred : dk_no_check);
17358   error_occurred = cp_parser_translation_unit (the_parser);
17359   the_parser = NULL;
17360 }
17361
17362 /* This variable must be provided by every front end.  */
17363
17364 int yydebug;
17365
17366 #include "gt-cp-parser.h"