Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / gcc-4.1 / 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   /* True for a CPP_NAME token that is not a keyword (i.e., for which
63      KEYWORD is RID_MAX) iff this name was looked up and found to be
64      ambiguous.  An error has already been reported.  */
65   BOOL_BITFIELD ambiguous_p : 1;
66   /* The value associated with this token, if any.  */
67   tree value;
68   /* The location at which this token was found.  */
69   location_t location;
70 } cp_token;
71
72 /* We use a stack of token pointer for saving token sets.  */
73 typedef struct cp_token *cp_token_position;
74 DEF_VEC_P (cp_token_position);
75 DEF_VEC_ALLOC_P (cp_token_position,heap);
76
77 static const cp_token eof_token =
78 {
79   CPP_EOF, RID_MAX, 0, 0, 0, false, NULL_TREE,
80 #if USE_MAPPED_LOCATION
81   0
82 #else
83   {0, 0}
84 #endif
85 };
86
87 /* The cp_lexer structure represents the C++ lexer.  It is responsible
88    for managing the token stream from the preprocessor and supplying
89    it to the parser.  Tokens are never added to the cp_lexer after
90    it is created.  */
91
92 typedef struct cp_lexer GTY (())
93 {
94   /* The memory allocated for the buffer.  NULL if this lexer does not
95      own the token buffer.  */
96   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
97   /* If the lexer owns the buffer, this is the number of tokens in the
98      buffer.  */
99   size_t buffer_length;
100
101   /* A pointer just past the last available token.  The tokens
102      in this lexer are [buffer, last_token).  */
103   cp_token_position GTY ((skip)) last_token;
104
105   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
106      no more available tokens.  */
107   cp_token_position GTY ((skip)) next_token;
108
109   /* A stack indicating positions at which cp_lexer_save_tokens was
110      called.  The top entry is the most recent position at which we
111      began saving tokens.  If the stack is non-empty, we are saving
112      tokens.  */
113   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
114
115   /* True if we should output debugging information.  */
116   bool debugging_p;
117
118   /* The next lexer in a linked list of lexers.  */
119   struct cp_lexer *next;
120 } cp_lexer;
121
122 /* cp_token_cache is a range of tokens.  There is no need to represent
123    allocate heap memory for it, since tokens are never removed from the
124    lexer's array.  There is also no need for the GC to walk through
125    a cp_token_cache, since everything in here is referenced through
126    a lexer.  */
127
128 typedef struct cp_token_cache GTY(())
129 {
130   /* The beginning of the token range.  */
131   cp_token * GTY((skip)) first;
132
133   /* Points immediately after the last token in the range.  */
134   cp_token * GTY ((skip)) last;
135 } cp_token_cache;
136
137 /* Prototypes.  */
138
139 static cp_lexer *cp_lexer_new_main
140   (void);
141 static cp_lexer *cp_lexer_new_from_tokens
142   (cp_token_cache *tokens);
143 static void cp_lexer_destroy
144   (cp_lexer *);
145 static int cp_lexer_saving_tokens
146   (const cp_lexer *);
147 static cp_token_position cp_lexer_token_position
148   (cp_lexer *, bool);
149 static cp_token *cp_lexer_token_at
150   (cp_lexer *, cp_token_position);
151 static void cp_lexer_get_preprocessor_token
152   (cp_lexer *, cp_token *);
153 static inline cp_token *cp_lexer_peek_token
154   (cp_lexer *);
155 static cp_token *cp_lexer_peek_nth_token
156   (cp_lexer *, size_t);
157 static inline bool cp_lexer_next_token_is
158   (cp_lexer *, enum cpp_ttype);
159 static bool cp_lexer_next_token_is_not
160   (cp_lexer *, enum cpp_ttype);
161 static bool cp_lexer_next_token_is_keyword
162   (cp_lexer *, enum rid);
163 static cp_token *cp_lexer_consume_token
164   (cp_lexer *);
165 static void cp_lexer_purge_token
166   (cp_lexer *);
167 static void cp_lexer_purge_tokens_after
168   (cp_lexer *, cp_token_position);
169 static void cp_lexer_handle_pragma
170   (cp_lexer *);
171 static void cp_lexer_save_tokens
172   (cp_lexer *);
173 static void cp_lexer_commit_tokens
174   (cp_lexer *);
175 static void cp_lexer_rollback_tokens
176   (cp_lexer *);
177 #ifdef ENABLE_CHECKING
178 static void cp_lexer_print_token
179   (FILE *, cp_token *);
180 static inline bool cp_lexer_debugging_p
181   (cp_lexer *);
182 static void cp_lexer_start_debugging
183   (cp_lexer *) ATTRIBUTE_UNUSED;
184 static void cp_lexer_stop_debugging
185   (cp_lexer *) ATTRIBUTE_UNUSED;
186 #else
187 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
188    about passing NULL to functions that require non-NULL arguments
189    (fputs, fprintf).  It will never be used, so all we need is a value
190    of the right type that's guaranteed not to be NULL.  */
191 #define cp_lexer_debug_stream stdout
192 #define cp_lexer_print_token(str, tok) (void) 0
193 #define cp_lexer_debugging_p(lexer) 0
194 #endif /* ENABLE_CHECKING */
195
196 static cp_token_cache *cp_token_cache_new
197   (cp_token *, cp_token *);
198
199 /* Manifest constants.  */
200 #define CP_LEXER_BUFFER_SIZE 10000
201 #define CP_SAVED_TOKEN_STACK 5
202
203 /* A token type for keywords, as opposed to ordinary identifiers.  */
204 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
205
206 /* A token type for template-ids.  If a template-id is processed while
207    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
208    the value of the CPP_TEMPLATE_ID is whatever was returned by
209    cp_parser_template_id.  */
210 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
211
212 /* A token type for nested-name-specifiers.  If a
213    nested-name-specifier is processed while parsing tentatively, it is
214    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
215    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
216    cp_parser_nested_name_specifier_opt.  */
217 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
218
219 /* A token type for tokens that are not tokens at all; these are used
220    to represent slots in the array where there used to be a token
221    that has now been deleted.  */
222 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
223
224 /* The number of token types, including C++-specific ones.  */
225 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
226
227 /* Variables.  */
228
229 #ifdef ENABLE_CHECKING
230 /* The stream to which debugging output should be written.  */
231 static FILE *cp_lexer_debug_stream;
232 #endif /* ENABLE_CHECKING */
233
234 /* Create a new main C++ lexer, the lexer that gets tokens from the
235    preprocessor.  */
236
237 static cp_lexer *
238 cp_lexer_new_main (void)
239 {
240   cp_token first_token;
241   cp_lexer *lexer;
242   cp_token *pos;
243   size_t alloc;
244   size_t space;
245   cp_token *buffer;
246
247   /* It's possible that lexing the first token will load a PCH file,
248      which is a GC collection point.  So we have to grab the first
249      token before allocating any memory.  Pragmas must not be deferred
250      as -fpch-preprocess can generate a pragma to load the PCH file in
251      the preprocessed output used by -save-temps.  */
252   cp_lexer_get_preprocessor_token (NULL, &first_token);
253
254   /* Tell cpplib we want CPP_PRAGMA tokens.  */
255   cpp_get_options (parse_in)->defer_pragmas = true;
256
257   /* Tell c_lex not to merge string constants.  */
258   c_lex_return_raw_strings = true;
259
260   c_common_no_more_pch ();
261
262   /* Allocate the memory.  */
263   lexer = GGC_CNEW (cp_lexer);
264
265 #ifdef ENABLE_CHECKING
266   /* Initially we are not debugging.  */
267   lexer->debugging_p = false;
268 #endif /* ENABLE_CHECKING */
269   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
270                                    CP_SAVED_TOKEN_STACK);
271
272   /* Create the buffer.  */
273   alloc = CP_LEXER_BUFFER_SIZE;
274   buffer = ggc_alloc (alloc * sizeof (cp_token));
275
276   /* Put the first token in the buffer.  */
277   space = alloc;
278   pos = buffer;
279   *pos = first_token;
280
281   /* Get the remaining tokens from the preprocessor.  */
282   while (pos->type != CPP_EOF)
283     {
284       pos++;
285       if (!--space)
286         {
287           space = alloc;
288           alloc *= 2;
289           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
290           pos = buffer + space;
291         }
292       cp_lexer_get_preprocessor_token (lexer, pos);
293     }
294   lexer->buffer = buffer;
295   lexer->buffer_length = alloc - space;
296   lexer->last_token = pos;
297   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
298
299   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
300      direct calls to c_lex.  Those callers all expect c_lex to do
301      string constant concatenation.  */
302   c_lex_return_raw_strings = false;
303
304   /* Subsequent preprocessor diagnostics should use compiler
305      diagnostic functions to get the compiler source location.  */
306   cpp_get_options (parse_in)->client_diagnostic = true;
307   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
308
309   gcc_assert (lexer->next_token->type != CPP_PURGED);
310   return lexer;
311 }
312
313 /* Create a new lexer whose token stream is primed with the tokens in
314    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
315
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
318 {
319   cp_token *first = cache->first;
320   cp_token *last = cache->last;
321   cp_lexer *lexer = GGC_CNEW (cp_lexer);
322
323   /* We do not own the buffer.  */
324   lexer->buffer = NULL;
325   lexer->buffer_length = 0;
326   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327   lexer->last_token = last;
328
329   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330                                    CP_SAVED_TOKEN_STACK);
331
332 #ifdef ENABLE_CHECKING
333   /* Initially we are not debugging.  */
334   lexer->debugging_p = false;
335 #endif
336
337   gcc_assert (lexer->next_token->type != CPP_PURGED);
338   return lexer;
339 }
340
341 /* Frees all resources associated with LEXER.  */
342
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
345 {
346   if (lexer->buffer)
347     ggc_free (lexer->buffer);
348   VEC_free (cp_token_position, heap, lexer->saved_tokens);
349   ggc_free (lexer);
350 }
351
352 /* Returns nonzero if debugging information should be output.  */
353
354 #ifdef ENABLE_CHECKING
355
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
358 {
359   return lexer->debugging_p;
360 }
361
362 #endif /* ENABLE_CHECKING */
363
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
366 {
367   gcc_assert (!previous_p || lexer->next_token != &eof_token);
368
369   return lexer->next_token - previous_p;
370 }
371
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
374 {
375   return pos;
376 }
377
378 /* nonzero if we are presently saving tokens.  */
379
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
382 {
383   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 }
385
386 /* Store the next token from the preprocessor in *TOKEN.  Return true
387    if we reach EOF.  */
388
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391                                  cp_token *token)
392 {
393   static int is_extern_c = 0;
394
395    /* Get a new token from the preprocessor.  */
396   token->type
397     = c_lex_with_flags (&token->value, &token->location, &token->flags);
398   token->in_system_header = in_system_header;
399
400   /* On some systems, some header files are surrounded by an
401      implicit extern "C" block.  Set a flag in the token if it
402      comes from such a header.  */
403   is_extern_c += pending_lang_change;
404   pending_lang_change = 0;
405   token->implicit_extern_c = is_extern_c > 0;
406
407   /* Check to see if this token is a keyword.  */
408   if (token->type == CPP_NAME)
409     {
410       if (C_IS_RESERVED_WORD (token->value))
411         {
412           /* Mark this token as a keyword.  */
413           token->type = CPP_KEYWORD;
414           /* Record which keyword.  */
415           token->keyword = C_RID_CODE (token->value);
416           /* Update the value.  Some keywords are mapped to particular
417              entities, rather than simply having the value of the
418              corresponding IDENTIFIER_NODE.  For example, `__const' is
419              mapped to `const'.  */
420           token->value = ridpointers[token->keyword];
421         }
422       else
423         {
424           token->ambiguous_p = false;
425           token->keyword = RID_MAX;
426         }
427     }
428   /* Handle Objective-C++ keywords.  */
429   else if (token->type == CPP_AT_NAME)
430     {
431       token->type = CPP_KEYWORD;
432       switch (C_RID_CODE (token->value))
433         {
434         /* Map 'class' to '@class', 'private' to '@private', etc.  */
435         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439         case RID_THROW: token->keyword = RID_AT_THROW; break;
440         case RID_TRY: token->keyword = RID_AT_TRY; break;
441         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442         default: token->keyword = C_RID_CODE (token->value);
443         }
444     }
445   else
446     token->keyword = RID_MAX;
447 }
448
449 /* Update the globals input_location and in_system_header from TOKEN.  */
450 static inline void
451 cp_lexer_set_source_position_from_token (cp_token *token)
452 {
453   if (token->type != CPP_EOF)
454     {
455       input_location = token->location;
456       in_system_header = token->in_system_header;
457     }
458 }
459
460 /* Return a pointer to the next token in the token stream, but do not
461    consume it.  */
462
463 static inline cp_token *
464 cp_lexer_peek_token (cp_lexer *lexer)
465 {
466   if (cp_lexer_debugging_p (lexer))
467     {
468       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
469       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
470       putc ('\n', cp_lexer_debug_stream);
471     }
472   return lexer->next_token;
473 }
474
475 /* Return true if the next token has the indicated TYPE.  */
476
477 static inline bool
478 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
479 {
480   return cp_lexer_peek_token (lexer)->type == type;
481 }
482
483 /* Return true if the next token does not have the indicated TYPE.  */
484
485 static inline bool
486 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
487 {
488   return !cp_lexer_next_token_is (lexer, type);
489 }
490
491 /* Return true if the next token is the indicated KEYWORD.  */
492
493 static inline bool
494 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
495 {
496   cp_token *token;
497
498   /* Peek at the next token.  */
499   token = cp_lexer_peek_token (lexer);
500   /* Check to see if it is the indicated keyword.  */
501   return token->keyword == keyword;
502 }
503
504 static bool
505 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
506 {
507   cp_token *token;
508
509   token = cp_lexer_peek_token (lexer);
510   switch (token->keyword) 
511     {
512       /* Storage classes.  */
513     case RID_AUTO:
514     case RID_REGISTER:
515     case RID_STATIC:
516     case RID_EXTERN:
517     case RID_MUTABLE:
518     case RID_THREAD:
519       /* Elaborated type specifiers.  */
520     case RID_ENUM:
521     case RID_CLASS:
522     case RID_STRUCT:
523     case RID_UNION:
524     case RID_TYPENAME:
525       /* Simple type specifiers.  */
526     case RID_CHAR:
527     case RID_WCHAR:
528     case RID_BOOL:
529     case RID_SHORT:
530     case RID_INT:
531     case RID_LONG:
532     case RID_SIGNED:
533     case RID_UNSIGNED:
534     case RID_FLOAT:
535     case RID_DOUBLE:
536     case RID_VOID:
537       /* GNU extensions.  */ 
538     case RID_ATTRIBUTE:
539     case RID_TYPEOF:
540       return true;
541
542     default:
543       return false;
544     }
545 }
546
547 /* Return a pointer to the Nth token in the token stream.  If N is 1,
548    then this is precisely equivalent to cp_lexer_peek_token (except
549    that it is not inline).  One would like to disallow that case, but
550    there is one case (cp_parser_nth_token_starts_template_id) where
551    the caller passes a variable for N and it might be 1.  */
552
553 static cp_token *
554 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
555 {
556   cp_token *token;
557
558   /* N is 1-based, not zero-based.  */
559   gcc_assert (n > 0);
560   
561   if (cp_lexer_debugging_p (lexer))
562     fprintf (cp_lexer_debug_stream,
563              "cp_lexer: peeking ahead %ld at token: ", (long)n);
564
565   --n;
566   token = lexer->next_token;
567   gcc_assert (!n || token != &eof_token);
568   while (n != 0)
569     {
570       ++token;
571       if (token == lexer->last_token)
572         {
573           token = (cp_token *)&eof_token;
574           break;
575         }
576
577       if (token->type != CPP_PURGED)
578         --n;
579     }
580
581   if (cp_lexer_debugging_p (lexer))
582     {
583       cp_lexer_print_token (cp_lexer_debug_stream, token);
584       putc ('\n', cp_lexer_debug_stream);
585     }
586
587   return token;
588 }
589
590 /* Return the next token, and advance the lexer's next_token pointer
591    to point to the next non-purged token.  */
592
593 static cp_token *
594 cp_lexer_consume_token (cp_lexer* lexer)
595 {
596   cp_token *token = lexer->next_token;
597
598   gcc_assert (token != &eof_token);
599
600   do
601     {
602       lexer->next_token++;
603       if (lexer->next_token == lexer->last_token)
604         {
605           lexer->next_token = (cp_token *)&eof_token;
606           break;
607         }
608
609     }
610   while (lexer->next_token->type == CPP_PURGED);
611
612   cp_lexer_set_source_position_from_token (token);
613
614   /* Provide debugging output.  */
615   if (cp_lexer_debugging_p (lexer))
616     {
617       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
618       cp_lexer_print_token (cp_lexer_debug_stream, token);
619       putc ('\n', cp_lexer_debug_stream);
620     }
621
622   return token;
623 }
624
625 /* Permanently remove the next token from the token stream, and
626    advance the next_token pointer to refer to the next non-purged
627    token.  */
628
629 static void
630 cp_lexer_purge_token (cp_lexer *lexer)
631 {
632   cp_token *tok = lexer->next_token;
633
634   gcc_assert (tok != &eof_token);
635   tok->type = CPP_PURGED;
636   tok->location = UNKNOWN_LOCATION;
637   tok->value = NULL_TREE;
638   tok->keyword = RID_MAX;
639
640   do
641     {
642       tok++;
643       if (tok == lexer->last_token)
644         {
645           tok = (cp_token *)&eof_token;
646           break;
647         }
648     }
649   while (tok->type == CPP_PURGED);
650   lexer->next_token = tok;
651 }
652
653 /* Permanently remove all tokens after TOK, up to, but not
654    including, the token that will be returned next by
655    cp_lexer_peek_token.  */
656
657 static void
658 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
659 {
660   cp_token *peek = lexer->next_token;
661
662   if (peek == &eof_token)
663     peek = lexer->last_token;
664
665   gcc_assert (tok < peek);
666
667   for ( tok += 1; tok != peek; tok += 1)
668     {
669       tok->type = CPP_PURGED;
670       tok->location = UNKNOWN_LOCATION;
671       tok->value = NULL_TREE;
672       tok->keyword = RID_MAX;
673     }
674 }
675
676 /* Consume and handle a pragma token.  */
677 static void
678 cp_lexer_handle_pragma (cp_lexer *lexer)
679 {
680   cpp_string s;
681   cp_token *token = cp_lexer_consume_token (lexer);
682   gcc_assert (token->type == CPP_PRAGMA);
683   gcc_assert (token->value);
684
685   s.len = TREE_STRING_LENGTH (token->value);
686   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
687
688   cpp_handle_deferred_pragma (parse_in, &s);
689
690   /* Clearing token->value here means that we will get an ICE if we
691      try to process this #pragma again (which should be impossible).  */
692   token->value = NULL;
693 }
694
695 /* Begin saving tokens.  All tokens consumed after this point will be
696    preserved.  */
697
698 static void
699 cp_lexer_save_tokens (cp_lexer* lexer)
700 {
701   /* Provide debugging output.  */
702   if (cp_lexer_debugging_p (lexer))
703     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
704
705   VEC_safe_push (cp_token_position, heap,
706                  lexer->saved_tokens, lexer->next_token);
707 }
708
709 /* Commit to the portion of the token stream most recently saved.  */
710
711 static void
712 cp_lexer_commit_tokens (cp_lexer* lexer)
713 {
714   /* Provide debugging output.  */
715   if (cp_lexer_debugging_p (lexer))
716     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
717
718   VEC_pop (cp_token_position, lexer->saved_tokens);
719 }
720
721 /* Return all tokens saved since the last call to cp_lexer_save_tokens
722    to the token stream.  Stop saving tokens.  */
723
724 static void
725 cp_lexer_rollback_tokens (cp_lexer* lexer)
726 {
727   /* Provide debugging output.  */
728   if (cp_lexer_debugging_p (lexer))
729     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
730
731   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
732 }
733
734 /* Print a representation of the TOKEN on the STREAM.  */
735
736 #ifdef ENABLE_CHECKING
737
738 static void
739 cp_lexer_print_token (FILE * stream, cp_token *token)
740 {
741   /* We don't use cpp_type2name here because the parser defines
742      a few tokens of its own.  */
743   static const char *const token_names[] = {
744     /* cpplib-defined token types */
745 #define OP(e, s) #e,
746 #define TK(e, s) #e,
747     TTYPE_TABLE
748 #undef OP
749 #undef TK
750     /* C++ parser token types - see "Manifest constants", above.  */
751     "KEYWORD",
752     "TEMPLATE_ID",
753     "NESTED_NAME_SPECIFIER",
754     "PURGED"
755   };
756
757   /* If we have a name for the token, print it out.  Otherwise, we
758      simply give the numeric code.  */
759   gcc_assert (token->type < ARRAY_SIZE(token_names));
760   fputs (token_names[token->type], stream);
761
762   /* For some tokens, print the associated data.  */
763   switch (token->type)
764     {
765     case CPP_KEYWORD:
766       /* Some keywords have a value that is not an IDENTIFIER_NODE.
767          For example, `struct' is mapped to an INTEGER_CST.  */
768       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
769         break;
770       /* else fall through */
771     case CPP_NAME:
772       fputs (IDENTIFIER_POINTER (token->value), stream);
773       break;
774
775     case CPP_STRING:
776     case CPP_WSTRING:
777     case CPP_PRAGMA:
778       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
779       break;
780
781     default:
782       break;
783     }
784 }
785
786 /* Start emitting debugging information.  */
787
788 static void
789 cp_lexer_start_debugging (cp_lexer* lexer)
790 {
791   lexer->debugging_p = true;
792 }
793
794 /* Stop emitting debugging information.  */
795
796 static void
797 cp_lexer_stop_debugging (cp_lexer* lexer)
798 {
799   lexer->debugging_p = false;
800 }
801
802 #endif /* ENABLE_CHECKING */
803
804 /* Create a new cp_token_cache, representing a range of tokens.  */
805
806 static cp_token_cache *
807 cp_token_cache_new (cp_token *first, cp_token *last)
808 {
809   cp_token_cache *cache = GGC_NEW (cp_token_cache);
810   cache->first = first;
811   cache->last = last;
812   return cache;
813 }
814
815 \f
816 /* Decl-specifiers.  */
817
818 static void clear_decl_specs
819   (cp_decl_specifier_seq *);
820
821 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
822
823 static void
824 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
825 {
826   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
827 }
828
829 /* Declarators.  */
830
831 /* Nothing other than the parser should be creating declarators;
832    declarators are a semi-syntactic representation of C++ entities.
833    Other parts of the front end that need to create entities (like
834    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
835
836 static cp_declarator *make_call_declarator
837   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
838 static cp_declarator *make_array_declarator
839   (cp_declarator *, tree);
840 static cp_declarator *make_pointer_declarator
841   (cp_cv_quals, cp_declarator *);
842 static cp_declarator *make_reference_declarator
843   (cp_cv_quals, cp_declarator *);
844 static cp_parameter_declarator *make_parameter_declarator
845   (cp_decl_specifier_seq *, cp_declarator *, tree);
846 static cp_declarator *make_ptrmem_declarator
847   (cp_cv_quals, tree, cp_declarator *);
848
849 cp_declarator *cp_error_declarator;
850
851 /* The obstack on which declarators and related data structures are
852    allocated.  */
853 static struct obstack declarator_obstack;
854
855 /* Alloc BYTES from the declarator memory pool.  */
856
857 static inline void *
858 alloc_declarator (size_t bytes)
859 {
860   return obstack_alloc (&declarator_obstack, bytes);
861 }
862
863 /* Allocate a declarator of the indicated KIND.  Clear fields that are
864    common to all declarators.  */
865
866 static cp_declarator *
867 make_declarator (cp_declarator_kind kind)
868 {
869   cp_declarator *declarator;
870
871   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
872   declarator->kind = kind;
873   declarator->attributes = NULL_TREE;
874   declarator->declarator = NULL;
875
876   return declarator;
877 }
878
879 /* Make a declarator for a generalized identifier.  If
880    QUALIFYING_SCOPE is non-NULL, the identifier is
881    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
882    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
883    is, if any.   */
884
885 static cp_declarator *
886 make_id_declarator (tree qualifying_scope, tree unqualified_name,
887                     special_function_kind sfk)
888 {
889   cp_declarator *declarator;
890
891   /* It is valid to write:
892
893        class C { void f(); };
894        typedef C D;
895        void D::f();
896
897      The standard is not clear about whether `typedef const C D' is
898      legal; as of 2002-09-15 the committee is considering that
899      question.  EDG 3.0 allows that syntax.  Therefore, we do as
900      well.  */
901   if (qualifying_scope && TYPE_P (qualifying_scope))
902     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
903
904   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
905               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
906               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
907
908   declarator = make_declarator (cdk_id);
909   declarator->u.id.qualifying_scope = qualifying_scope;
910   declarator->u.id.unqualified_name = unqualified_name;
911   declarator->u.id.sfk = sfk;
912
913   return declarator;
914 }
915
916 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
917    of modifiers such as const or volatile to apply to the pointer
918    type, represented as identifiers.  */
919
920 cp_declarator *
921 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
922 {
923   cp_declarator *declarator;
924
925   declarator = make_declarator (cdk_pointer);
926   declarator->declarator = target;
927   declarator->u.pointer.qualifiers = cv_qualifiers;
928   declarator->u.pointer.class_type = NULL_TREE;
929
930   return declarator;
931 }
932
933 /* Like make_pointer_declarator -- but for references.  */
934
935 cp_declarator *
936 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
937 {
938   cp_declarator *declarator;
939
940   declarator = make_declarator (cdk_reference);
941   declarator->declarator = target;
942   declarator->u.pointer.qualifiers = cv_qualifiers;
943   declarator->u.pointer.class_type = NULL_TREE;
944
945   return declarator;
946 }
947
948 /* Like make_pointer_declarator -- but for a pointer to a non-static
949    member of CLASS_TYPE.  */
950
951 cp_declarator *
952 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
953                         cp_declarator *pointee)
954 {
955   cp_declarator *declarator;
956
957   declarator = make_declarator (cdk_ptrmem);
958   declarator->declarator = pointee;
959   declarator->u.pointer.qualifiers = cv_qualifiers;
960   declarator->u.pointer.class_type = class_type;
961
962   return declarator;
963 }
964
965 /* Make a declarator for the function given by TARGET, with the
966    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
967    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
968    indicates what exceptions can be thrown.  */
969
970 cp_declarator *
971 make_call_declarator (cp_declarator *target,
972                       cp_parameter_declarator *parms,
973                       cp_cv_quals cv_qualifiers,
974                       tree exception_specification)
975 {
976   cp_declarator *declarator;
977
978   declarator = make_declarator (cdk_function);
979   declarator->declarator = target;
980   declarator->u.function.parameters = parms;
981   declarator->u.function.qualifiers = cv_qualifiers;
982   declarator->u.function.exception_specification = exception_specification;
983
984   return declarator;
985 }
986
987 /* Make a declarator for an array of BOUNDS elements, each of which is
988    defined by ELEMENT.  */
989
990 cp_declarator *
991 make_array_declarator (cp_declarator *element, tree bounds)
992 {
993   cp_declarator *declarator;
994
995   declarator = make_declarator (cdk_array);
996   declarator->declarator = element;
997   declarator->u.array.bounds = bounds;
998
999   return declarator;
1000 }
1001
1002 cp_parameter_declarator *no_parameters;
1003
1004 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1005    DECLARATOR and DEFAULT_ARGUMENT.  */
1006
1007 cp_parameter_declarator *
1008 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1009                            cp_declarator *declarator,
1010                            tree default_argument)
1011 {
1012   cp_parameter_declarator *parameter;
1013
1014   parameter = ((cp_parameter_declarator *)
1015                alloc_declarator (sizeof (cp_parameter_declarator)));
1016   parameter->next = NULL;
1017   if (decl_specifiers)
1018     parameter->decl_specifiers = *decl_specifiers;
1019   else
1020     clear_decl_specs (&parameter->decl_specifiers);
1021   parameter->declarator = declarator;
1022   parameter->default_argument = default_argument;
1023   parameter->ellipsis_p = false;
1024
1025   return parameter;
1026 }
1027
1028 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1029
1030 static bool
1031 function_declarator_p (const cp_declarator *declarator)
1032 {
1033   while (declarator)
1034     {
1035       if (declarator->kind == cdk_function
1036           && declarator->declarator->kind == cdk_id)
1037         return true;
1038       if (declarator->kind == cdk_id
1039           || declarator->kind == cdk_error)
1040         return false;
1041       declarator = declarator->declarator;
1042     }
1043   return false;
1044 }
1045  
1046 /* The parser.  */
1047
1048 /* Overview
1049    --------
1050
1051    A cp_parser parses the token stream as specified by the C++
1052    grammar.  Its job is purely parsing, not semantic analysis.  For
1053    example, the parser breaks the token stream into declarators,
1054    expressions, statements, and other similar syntactic constructs.
1055    It does not check that the types of the expressions on either side
1056    of an assignment-statement are compatible, or that a function is
1057    not declared with a parameter of type `void'.
1058
1059    The parser invokes routines elsewhere in the compiler to perform
1060    semantic analysis and to build up the abstract syntax tree for the
1061    code processed.
1062
1063    The parser (and the template instantiation code, which is, in a
1064    way, a close relative of parsing) are the only parts of the
1065    compiler that should be calling push_scope and pop_scope, or
1066    related functions.  The parser (and template instantiation code)
1067    keeps track of what scope is presently active; everything else
1068    should simply honor that.  (The code that generates static
1069    initializers may also need to set the scope, in order to check
1070    access control correctly when emitting the initializers.)
1071
1072    Methodology
1073    -----------
1074
1075    The parser is of the standard recursive-descent variety.  Upcoming
1076    tokens in the token stream are examined in order to determine which
1077    production to use when parsing a non-terminal.  Some C++ constructs
1078    require arbitrary look ahead to disambiguate.  For example, it is
1079    impossible, in the general case, to tell whether a statement is an
1080    expression or declaration without scanning the entire statement.
1081    Therefore, the parser is capable of "parsing tentatively."  When the
1082    parser is not sure what construct comes next, it enters this mode.
1083    Then, while we attempt to parse the construct, the parser queues up
1084    error messages, rather than issuing them immediately, and saves the
1085    tokens it consumes.  If the construct is parsed successfully, the
1086    parser "commits", i.e., it issues any queued error messages and
1087    the tokens that were being preserved are permanently discarded.
1088    If, however, the construct is not parsed successfully, the parser
1089    rolls back its state completely so that it can resume parsing using
1090    a different alternative.
1091
1092    Future Improvements
1093    -------------------
1094
1095    The performance of the parser could probably be improved substantially.
1096    We could often eliminate the need to parse tentatively by looking ahead
1097    a little bit.  In some places, this approach might not entirely eliminate
1098    the need to parse tentatively, but it might still speed up the average
1099    case.  */
1100
1101 /* Flags that are passed to some parsing functions.  These values can
1102    be bitwise-ored together.  */
1103
1104 typedef enum cp_parser_flags
1105 {
1106   /* No flags.  */
1107   CP_PARSER_FLAGS_NONE = 0x0,
1108   /* The construct is optional.  If it is not present, then no error
1109      should be issued.  */
1110   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1111   /* When parsing a type-specifier, do not allow user-defined types.  */
1112   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1113 } cp_parser_flags;
1114
1115 /* The different kinds of declarators we want to parse.  */
1116
1117 typedef enum cp_parser_declarator_kind
1118 {
1119   /* We want an abstract declarator.  */
1120   CP_PARSER_DECLARATOR_ABSTRACT,
1121   /* We want a named declarator.  */
1122   CP_PARSER_DECLARATOR_NAMED,
1123   /* We don't mind, but the name must be an unqualified-id.  */
1124   CP_PARSER_DECLARATOR_EITHER
1125 } cp_parser_declarator_kind;
1126
1127 /* The precedence values used to parse binary expressions.  The minimum value
1128    of PREC must be 1, because zero is reserved to quickly discriminate
1129    binary operators from other tokens.  */
1130
1131 enum cp_parser_prec
1132 {
1133   PREC_NOT_OPERATOR,
1134   PREC_LOGICAL_OR_EXPRESSION,
1135   PREC_LOGICAL_AND_EXPRESSION,
1136   PREC_INCLUSIVE_OR_EXPRESSION,
1137   PREC_EXCLUSIVE_OR_EXPRESSION,
1138   PREC_AND_EXPRESSION,
1139   PREC_EQUALITY_EXPRESSION,
1140   PREC_RELATIONAL_EXPRESSION,
1141   PREC_SHIFT_EXPRESSION,
1142   PREC_ADDITIVE_EXPRESSION,
1143   PREC_MULTIPLICATIVE_EXPRESSION,
1144   PREC_PM_EXPRESSION,
1145   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1146 };
1147
1148 /* A mapping from a token type to a corresponding tree node type, with a
1149    precedence value.  */
1150
1151 typedef struct cp_parser_binary_operations_map_node
1152 {
1153   /* The token type.  */
1154   enum cpp_ttype token_type;
1155   /* The corresponding tree code.  */
1156   enum tree_code tree_type;
1157   /* The precedence of this operator.  */
1158   enum cp_parser_prec prec;
1159 } cp_parser_binary_operations_map_node;
1160
1161 /* The status of a tentative parse.  */
1162
1163 typedef enum cp_parser_status_kind
1164 {
1165   /* No errors have occurred.  */
1166   CP_PARSER_STATUS_KIND_NO_ERROR,
1167   /* An error has occurred.  */
1168   CP_PARSER_STATUS_KIND_ERROR,
1169   /* We are committed to this tentative parse, whether or not an error
1170      has occurred.  */
1171   CP_PARSER_STATUS_KIND_COMMITTED
1172 } cp_parser_status_kind;
1173
1174 typedef struct cp_parser_expression_stack_entry
1175 {
1176   tree lhs;
1177   enum tree_code tree_type;
1178   int prec;
1179 } cp_parser_expression_stack_entry;
1180
1181 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1182    entries because precedence levels on the stack are monotonically
1183    increasing.  */
1184 typedef struct cp_parser_expression_stack_entry
1185   cp_parser_expression_stack[NUM_PREC_VALUES];
1186
1187 /* Context that is saved and restored when parsing tentatively.  */
1188 typedef struct cp_parser_context GTY (())
1189 {
1190   /* If this is a tentative parsing context, the status of the
1191      tentative parse.  */
1192   enum cp_parser_status_kind status;
1193   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1194      that are looked up in this context must be looked up both in the
1195      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1196      the context of the containing expression.  */
1197   tree object_type;
1198
1199   /* The next parsing context in the stack.  */
1200   struct cp_parser_context *next;
1201 } cp_parser_context;
1202
1203 /* Prototypes.  */
1204
1205 /* Constructors and destructors.  */
1206
1207 static cp_parser_context *cp_parser_context_new
1208   (cp_parser_context *);
1209
1210 /* Class variables.  */
1211
1212 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1213
1214 /* The operator-precedence table used by cp_parser_binary_expression.
1215    Transformed into an associative array (binops_by_token) by
1216    cp_parser_new.  */
1217
1218 static const cp_parser_binary_operations_map_node binops[] = {
1219   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1220   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1221
1222   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1223   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1224   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1225
1226   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1227   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1228
1229   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1230   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1231
1232   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1233   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1234   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1235   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1236   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1237   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1238
1239   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1240   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1241
1242   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1243
1244   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1245
1246   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1247
1248   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1249
1250   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1251 };
1252
1253 /* The same as binops, but initialized by cp_parser_new so that
1254    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1255    for speed.  */
1256 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1257
1258 /* Constructors and destructors.  */
1259
1260 /* Construct a new context.  The context below this one on the stack
1261    is given by NEXT.  */
1262
1263 static cp_parser_context *
1264 cp_parser_context_new (cp_parser_context* next)
1265 {
1266   cp_parser_context *context;
1267
1268   /* Allocate the storage.  */
1269   if (cp_parser_context_free_list != NULL)
1270     {
1271       /* Pull the first entry from the free list.  */
1272       context = cp_parser_context_free_list;
1273       cp_parser_context_free_list = context->next;
1274       memset (context, 0, sizeof (*context));
1275     }
1276   else
1277     context = GGC_CNEW (cp_parser_context);
1278
1279   /* No errors have occurred yet in this context.  */
1280   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1281   /* If this is not the bottomost context, copy information that we
1282      need from the previous context.  */
1283   if (next)
1284     {
1285       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1286          expression, then we are parsing one in this context, too.  */
1287       context->object_type = next->object_type;
1288       /* Thread the stack.  */
1289       context->next = next;
1290     }
1291
1292   return context;
1293 }
1294
1295 /* The cp_parser structure represents the C++ parser.  */
1296
1297 typedef struct cp_parser GTY(())
1298 {
1299   /* The lexer from which we are obtaining tokens.  */
1300   cp_lexer *lexer;
1301
1302   /* The scope in which names should be looked up.  If NULL_TREE, then
1303      we look up names in the scope that is currently open in the
1304      source program.  If non-NULL, this is either a TYPE or
1305      NAMESPACE_DECL for the scope in which we should look.  It can
1306      also be ERROR_MARK, when we've parsed a bogus scope.
1307
1308      This value is not cleared automatically after a name is looked
1309      up, so we must be careful to clear it before starting a new look
1310      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1311      will look up `Z' in the scope of `X', rather than the current
1312      scope.)  Unfortunately, it is difficult to tell when name lookup
1313      is complete, because we sometimes peek at a token, look it up,
1314      and then decide not to consume it.   */
1315   tree scope;
1316
1317   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1318      last lookup took place.  OBJECT_SCOPE is used if an expression
1319      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1320      respectively.  QUALIFYING_SCOPE is used for an expression of the
1321      form "X::Y"; it refers to X.  */
1322   tree object_scope;
1323   tree qualifying_scope;
1324
1325   /* A stack of parsing contexts.  All but the bottom entry on the
1326      stack will be tentative contexts.
1327
1328      We parse tentatively in order to determine which construct is in
1329      use in some situations.  For example, in order to determine
1330      whether a statement is an expression-statement or a
1331      declaration-statement we parse it tentatively as a
1332      declaration-statement.  If that fails, we then reparse the same
1333      token stream as an expression-statement.  */
1334   cp_parser_context *context;
1335
1336   /* True if we are parsing GNU C++.  If this flag is not set, then
1337      GNU extensions are not recognized.  */
1338   bool allow_gnu_extensions_p;
1339
1340   /* TRUE if the `>' token should be interpreted as the greater-than
1341      operator.  FALSE if it is the end of a template-id or
1342      template-parameter-list.  */
1343   bool greater_than_is_operator_p;
1344
1345   /* TRUE if default arguments are allowed within a parameter list
1346      that starts at this point. FALSE if only a gnu extension makes
1347      them permissible.  */
1348   bool default_arg_ok_p;
1349
1350   /* TRUE if we are parsing an integral constant-expression.  See
1351      [expr.const] for a precise definition.  */
1352   bool integral_constant_expression_p;
1353
1354   /* TRUE if we are parsing an integral constant-expression -- but a
1355      non-constant expression should be permitted as well.  This flag
1356      is used when parsing an array bound so that GNU variable-length
1357      arrays are tolerated.  */
1358   bool allow_non_integral_constant_expression_p;
1359
1360   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1361      been seen that makes the expression non-constant.  */
1362   bool non_integral_constant_expression_p;
1363
1364   /* TRUE if local variable names and `this' are forbidden in the
1365      current context.  */
1366   bool local_variables_forbidden_p;
1367
1368   /* TRUE if the declaration we are parsing is part of a
1369      linkage-specification of the form `extern string-literal
1370      declaration'.  */
1371   bool in_unbraced_linkage_specification_p;
1372
1373   /* TRUE if we are presently parsing a declarator, after the
1374      direct-declarator.  */
1375   bool in_declarator_p;
1376
1377   /* TRUE if we are presently parsing a template-argument-list.  */
1378   bool in_template_argument_list_p;
1379
1380   /* TRUE if we are presently parsing the body of an
1381      iteration-statement.  */
1382   bool in_iteration_statement_p;
1383
1384   /* TRUE if we are presently parsing the body of a switch
1385      statement.  */
1386   bool in_switch_statement_p;
1387
1388   /* TRUE if we are parsing a type-id in an expression context.  In
1389      such a situation, both "type (expr)" and "type (type)" are valid
1390      alternatives.  */
1391   bool in_type_id_in_expr_p;
1392
1393   /* TRUE if we are currently in a header file where declarations are
1394      implicitly extern "C".  */
1395   bool implicit_extern_c;
1396
1397   /* TRUE if strings in expressions should be translated to the execution
1398      character set.  */
1399   bool translate_strings_p;
1400
1401   /* TRUE if we are presently parsing the body of a function, but not
1402      a local class.  */
1403   bool in_function_body;
1404
1405   /* If non-NULL, then we are parsing a construct where new type
1406      definitions are not permitted.  The string stored here will be
1407      issued as an error message if a type is defined.  */
1408   const char *type_definition_forbidden_message;
1409
1410   /* A list of lists. The outer list is a stack, used for member
1411      functions of local classes. At each level there are two sub-list,
1412      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1413      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1414      TREE_VALUE's. The functions are chained in reverse declaration
1415      order.
1416
1417      The TREE_PURPOSE sublist contains those functions with default
1418      arguments that need post processing, and the TREE_VALUE sublist
1419      contains those functions with definitions that need post
1420      processing.
1421
1422      These lists can only be processed once the outermost class being
1423      defined is complete.  */
1424   tree unparsed_functions_queues;
1425
1426   /* The number of classes whose definitions are currently in
1427      progress.  */
1428   unsigned num_classes_being_defined;
1429
1430   /* The number of template parameter lists that apply directly to the
1431      current declaration.  */
1432   unsigned num_template_parameter_lists;
1433 } cp_parser;
1434
1435 /* The type of a function that parses some kind of expression.  */
1436 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1437
1438 /* Prototypes.  */
1439
1440 /* Constructors and destructors.  */
1441
1442 static cp_parser *cp_parser_new
1443   (void);
1444
1445 /* Routines to parse various constructs.
1446
1447    Those that return `tree' will return the error_mark_node (rather
1448    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1449    Sometimes, they will return an ordinary node if error-recovery was
1450    attempted, even though a parse error occurred.  So, to check
1451    whether or not a parse error occurred, you should always use
1452    cp_parser_error_occurred.  If the construct is optional (indicated
1453    either by an `_opt' in the name of the function that does the
1454    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1455    the construct is not present.  */
1456
1457 /* Lexical conventions [gram.lex]  */
1458
1459 static tree cp_parser_identifier
1460   (cp_parser *);
1461 static tree cp_parser_string_literal
1462   (cp_parser *, bool, bool);
1463
1464 /* Basic concepts [gram.basic]  */
1465
1466 static bool cp_parser_translation_unit
1467   (cp_parser *);
1468
1469 /* Expressions [gram.expr]  */
1470
1471 static tree cp_parser_primary_expression
1472   (cp_parser *, bool, bool, bool, cp_id_kind *);
1473 static tree cp_parser_id_expression
1474   (cp_parser *, bool, bool, bool *, bool);
1475 static tree cp_parser_unqualified_id
1476   (cp_parser *, bool, bool, bool);
1477 static tree cp_parser_nested_name_specifier_opt
1478   (cp_parser *, bool, bool, bool, bool);
1479 static tree cp_parser_nested_name_specifier
1480   (cp_parser *, bool, bool, bool, bool);
1481 static tree cp_parser_class_or_namespace_name
1482   (cp_parser *, bool, bool, bool, bool, bool);
1483 static tree cp_parser_postfix_expression
1484   (cp_parser *, bool, bool);
1485 static tree cp_parser_postfix_open_square_expression
1486   (cp_parser *, tree, bool);
1487 static tree cp_parser_postfix_dot_deref_expression
1488   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1489 static tree cp_parser_parenthesized_expression_list
1490   (cp_parser *, bool, bool, bool *);
1491 static void cp_parser_pseudo_destructor_name
1492   (cp_parser *, tree *, tree *);
1493 static tree cp_parser_unary_expression
1494   (cp_parser *, bool, bool);
1495 static enum tree_code cp_parser_unary_operator
1496   (cp_token *);
1497 static tree cp_parser_new_expression
1498   (cp_parser *);
1499 static tree cp_parser_new_placement
1500   (cp_parser *);
1501 static tree cp_parser_new_type_id
1502   (cp_parser *, tree *);
1503 static cp_declarator *cp_parser_new_declarator_opt
1504   (cp_parser *);
1505 static cp_declarator *cp_parser_direct_new_declarator
1506   (cp_parser *);
1507 static tree cp_parser_new_initializer
1508   (cp_parser *);
1509 static tree cp_parser_delete_expression
1510   (cp_parser *);
1511 static tree cp_parser_cast_expression
1512   (cp_parser *, bool, bool);
1513 static tree cp_parser_binary_expression
1514   (cp_parser *, bool);
1515 static tree cp_parser_question_colon_clause
1516   (cp_parser *, tree);
1517 static tree cp_parser_assignment_expression
1518   (cp_parser *, bool);
1519 static enum tree_code cp_parser_assignment_operator_opt
1520   (cp_parser *);
1521 static tree cp_parser_expression
1522   (cp_parser *, bool);
1523 static tree cp_parser_constant_expression
1524   (cp_parser *, bool, bool *);
1525 static tree cp_parser_builtin_offsetof
1526   (cp_parser *);
1527
1528 /* Statements [gram.stmt.stmt]  */
1529
1530 static void cp_parser_statement
1531   (cp_parser *, tree);
1532 static void cp_parser_label_for_labeled_statement
1533   (cp_parser *);
1534 static tree cp_parser_expression_statement
1535   (cp_parser *, tree);
1536 static tree cp_parser_compound_statement
1537   (cp_parser *, tree, bool);
1538 static void cp_parser_statement_seq_opt
1539   (cp_parser *, tree);
1540 static tree cp_parser_selection_statement
1541   (cp_parser *);
1542 static tree cp_parser_condition
1543   (cp_parser *);
1544 static tree cp_parser_iteration_statement
1545   (cp_parser *);
1546 static void cp_parser_for_init_statement
1547   (cp_parser *);
1548 static tree cp_parser_jump_statement
1549   (cp_parser *);
1550 static void cp_parser_declaration_statement
1551   (cp_parser *);
1552
1553 static tree cp_parser_implicitly_scoped_statement
1554   (cp_parser *);
1555 static void cp_parser_already_scoped_statement
1556   (cp_parser *);
1557
1558 /* Declarations [gram.dcl.dcl] */
1559
1560 static void cp_parser_declaration_seq_opt
1561   (cp_parser *);
1562 static void cp_parser_declaration
1563   (cp_parser *);
1564 static void cp_parser_block_declaration
1565   (cp_parser *, bool);
1566 static void cp_parser_simple_declaration
1567   (cp_parser *, bool);
1568 static void cp_parser_decl_specifier_seq
1569   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1570 static tree cp_parser_storage_class_specifier_opt
1571   (cp_parser *);
1572 static tree cp_parser_function_specifier_opt
1573   (cp_parser *, cp_decl_specifier_seq *);
1574 static tree cp_parser_type_specifier
1575   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1576    int *, bool *);
1577 static tree cp_parser_simple_type_specifier
1578   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1579 static tree cp_parser_type_name
1580   (cp_parser *);
1581 static tree cp_parser_elaborated_type_specifier
1582   (cp_parser *, bool, bool);
1583 static tree cp_parser_enum_specifier
1584   (cp_parser *);
1585 static void cp_parser_enumerator_list
1586   (cp_parser *, tree);
1587 static void cp_parser_enumerator_definition
1588   (cp_parser *, tree);
1589 static tree cp_parser_namespace_name
1590   (cp_parser *);
1591 static void cp_parser_namespace_definition
1592   (cp_parser *);
1593 static void cp_parser_namespace_body
1594   (cp_parser *);
1595 static tree cp_parser_qualified_namespace_specifier
1596   (cp_parser *);
1597 static void cp_parser_namespace_alias_definition
1598   (cp_parser *);
1599 static bool cp_parser_using_declaration
1600   (cp_parser *, bool);
1601 static void cp_parser_using_directive
1602   (cp_parser *);
1603 static void cp_parser_asm_definition
1604   (cp_parser *);
1605 static void cp_parser_linkage_specification
1606   (cp_parser *);
1607
1608 /* Declarators [gram.dcl.decl] */
1609
1610 static tree cp_parser_init_declarator
1611   (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1612 static cp_declarator *cp_parser_declarator
1613   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1614 static cp_declarator *cp_parser_direct_declarator
1615   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1616 static enum tree_code cp_parser_ptr_operator
1617   (cp_parser *, tree *, cp_cv_quals *);
1618 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1619   (cp_parser *);
1620 static tree cp_parser_declarator_id
1621   (cp_parser *);
1622 static tree cp_parser_type_id
1623   (cp_parser *);
1624 static void cp_parser_type_specifier_seq
1625   (cp_parser *, bool, cp_decl_specifier_seq *);
1626 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1627   (cp_parser *);
1628 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1629   (cp_parser *, bool *);
1630 static cp_parameter_declarator *cp_parser_parameter_declaration
1631   (cp_parser *, bool, bool *);
1632 static void cp_parser_function_body
1633   (cp_parser *);
1634 static tree cp_parser_initializer
1635   (cp_parser *, bool *, bool *);
1636 static tree cp_parser_initializer_clause
1637   (cp_parser *, bool *);
1638 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1639   (cp_parser *, bool *);
1640
1641 static bool cp_parser_ctor_initializer_opt_and_function_body
1642   (cp_parser *);
1643
1644 /* Classes [gram.class] */
1645
1646 static tree cp_parser_class_name
1647   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1648 static tree cp_parser_class_specifier
1649   (cp_parser *);
1650 static tree cp_parser_class_head
1651   (cp_parser *, bool *, tree *, tree *);
1652 static enum tag_types cp_parser_class_key
1653   (cp_parser *);
1654 static void cp_parser_member_specification_opt
1655   (cp_parser *);
1656 static void cp_parser_member_declaration
1657   (cp_parser *);
1658 static tree cp_parser_pure_specifier
1659   (cp_parser *);
1660 static tree cp_parser_constant_initializer
1661   (cp_parser *);
1662
1663 /* Derived classes [gram.class.derived] */
1664
1665 static tree cp_parser_base_clause
1666   (cp_parser *);
1667 static tree cp_parser_base_specifier
1668   (cp_parser *);
1669
1670 /* Special member functions [gram.special] */
1671
1672 static tree cp_parser_conversion_function_id
1673   (cp_parser *);
1674 static tree cp_parser_conversion_type_id
1675   (cp_parser *);
1676 static cp_declarator *cp_parser_conversion_declarator_opt
1677   (cp_parser *);
1678 static bool cp_parser_ctor_initializer_opt
1679   (cp_parser *);
1680 static void cp_parser_mem_initializer_list
1681   (cp_parser *);
1682 static tree cp_parser_mem_initializer
1683   (cp_parser *);
1684 static tree cp_parser_mem_initializer_id
1685   (cp_parser *);
1686
1687 /* Overloading [gram.over] */
1688
1689 static tree cp_parser_operator_function_id
1690   (cp_parser *);
1691 static tree cp_parser_operator
1692   (cp_parser *);
1693
1694 /* Templates [gram.temp] */
1695
1696 static void cp_parser_template_declaration
1697   (cp_parser *, bool);
1698 static tree cp_parser_template_parameter_list
1699   (cp_parser *);
1700 static tree cp_parser_template_parameter
1701   (cp_parser *, bool *);
1702 static tree cp_parser_type_parameter
1703   (cp_parser *);
1704 static tree cp_parser_template_id
1705   (cp_parser *, bool, bool, bool);
1706 static tree cp_parser_template_name
1707   (cp_parser *, bool, bool, bool, bool *);
1708 static tree cp_parser_template_argument_list
1709   (cp_parser *);
1710 static tree cp_parser_template_argument
1711   (cp_parser *);
1712 static void cp_parser_explicit_instantiation
1713   (cp_parser *);
1714 static void cp_parser_explicit_specialization
1715   (cp_parser *);
1716
1717 /* Exception handling [gram.exception] */
1718
1719 static tree cp_parser_try_block
1720   (cp_parser *);
1721 static bool cp_parser_function_try_block
1722   (cp_parser *);
1723 static void cp_parser_handler_seq
1724   (cp_parser *);
1725 static void cp_parser_handler
1726   (cp_parser *);
1727 static tree cp_parser_exception_declaration
1728   (cp_parser *);
1729 static tree cp_parser_throw_expression
1730   (cp_parser *);
1731 static tree cp_parser_exception_specification_opt
1732   (cp_parser *);
1733 static tree cp_parser_type_id_list
1734   (cp_parser *);
1735
1736 /* GNU Extensions */
1737
1738 static tree cp_parser_asm_specification_opt
1739   (cp_parser *);
1740 static tree cp_parser_asm_operand_list
1741   (cp_parser *);
1742 static tree cp_parser_asm_clobber_list
1743   (cp_parser *);
1744 static tree cp_parser_attributes_opt
1745   (cp_parser *);
1746 static tree cp_parser_attribute_list
1747   (cp_parser *);
1748 static bool cp_parser_extension_opt
1749   (cp_parser *, int *);
1750 static void cp_parser_label_declaration
1751   (cp_parser *);
1752
1753 /* Objective-C++ Productions */
1754
1755 static tree cp_parser_objc_message_receiver
1756   (cp_parser *);
1757 static tree cp_parser_objc_message_args
1758   (cp_parser *);
1759 static tree cp_parser_objc_message_expression
1760   (cp_parser *);
1761 static tree cp_parser_objc_encode_expression
1762   (cp_parser *);
1763 static tree cp_parser_objc_defs_expression
1764   (cp_parser *);
1765 static tree cp_parser_objc_protocol_expression
1766   (cp_parser *);
1767 static tree cp_parser_objc_selector_expression
1768   (cp_parser *);
1769 static tree cp_parser_objc_expression
1770   (cp_parser *);
1771 static bool cp_parser_objc_selector_p
1772   (enum cpp_ttype);
1773 static tree cp_parser_objc_selector
1774   (cp_parser *);
1775 static tree cp_parser_objc_protocol_refs_opt
1776   (cp_parser *);
1777 static void cp_parser_objc_declaration
1778   (cp_parser *);
1779 static tree cp_parser_objc_statement
1780   (cp_parser *);
1781
1782 /* Utility Routines */
1783
1784 static tree cp_parser_lookup_name
1785   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1786 static tree cp_parser_lookup_name_simple
1787   (cp_parser *, tree);
1788 static tree cp_parser_maybe_treat_template_as_class
1789   (tree, bool);
1790 static bool cp_parser_check_declarator_template_parameters
1791   (cp_parser *, cp_declarator *);
1792 static bool cp_parser_check_template_parameters
1793   (cp_parser *, unsigned);
1794 static tree cp_parser_simple_cast_expression
1795   (cp_parser *);
1796 static tree cp_parser_global_scope_opt
1797   (cp_parser *, bool);
1798 static bool cp_parser_constructor_declarator_p
1799   (cp_parser *, bool);
1800 static tree cp_parser_function_definition_from_specifiers_and_declarator
1801   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1802 static tree cp_parser_function_definition_after_declarator
1803   (cp_parser *, bool);
1804 static void cp_parser_template_declaration_after_export
1805   (cp_parser *, bool);
1806 static void cp_parser_perform_template_parameter_access_checks
1807   (tree);
1808 static tree cp_parser_single_declaration
1809   (cp_parser *, tree, bool, bool *);
1810 static tree cp_parser_functional_cast
1811   (cp_parser *, tree);
1812 static tree cp_parser_save_member_function_body
1813   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1814 static tree cp_parser_enclosed_template_argument_list
1815   (cp_parser *);
1816 static void cp_parser_save_default_args
1817   (cp_parser *, tree);
1818 static void cp_parser_late_parsing_for_member
1819   (cp_parser *, tree);
1820 static void cp_parser_late_parsing_default_args
1821   (cp_parser *, tree);
1822 static tree cp_parser_sizeof_operand
1823   (cp_parser *, enum rid);
1824 static bool cp_parser_declares_only_class_p
1825   (cp_parser *);
1826 static void cp_parser_set_storage_class
1827   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1828 static void cp_parser_set_decl_spec_type
1829   (cp_decl_specifier_seq *, tree, bool);
1830 static bool cp_parser_friend_p
1831   (const cp_decl_specifier_seq *);
1832 static cp_token *cp_parser_require
1833   (cp_parser *, enum cpp_ttype, const char *);
1834 static cp_token *cp_parser_require_keyword
1835   (cp_parser *, enum rid, const char *);
1836 static bool cp_parser_token_starts_function_definition_p
1837   (cp_token *);
1838 static bool cp_parser_next_token_starts_class_definition_p
1839   (cp_parser *);
1840 static bool cp_parser_next_token_ends_template_argument_p
1841   (cp_parser *);
1842 static bool cp_parser_nth_token_starts_template_argument_list_p
1843   (cp_parser *, size_t);
1844 static enum tag_types cp_parser_token_is_class_key
1845   (cp_token *);
1846 static void cp_parser_check_class_key
1847   (enum tag_types, tree type);
1848 static void cp_parser_check_access_in_redeclaration
1849   (tree type);
1850 static bool cp_parser_optional_template_keyword
1851   (cp_parser *);
1852 static void cp_parser_pre_parsed_nested_name_specifier
1853   (cp_parser *);
1854 static void cp_parser_cache_group
1855   (cp_parser *, enum cpp_ttype, unsigned);
1856 static void cp_parser_parse_tentatively
1857   (cp_parser *);
1858 static void cp_parser_commit_to_tentative_parse
1859   (cp_parser *);
1860 static void cp_parser_abort_tentative_parse
1861   (cp_parser *);
1862 static bool cp_parser_parse_definitely
1863   (cp_parser *);
1864 static inline bool cp_parser_parsing_tentatively
1865   (cp_parser *);
1866 static bool cp_parser_uncommitted_to_tentative_parse_p
1867   (cp_parser *);
1868 static void cp_parser_error
1869   (cp_parser *, const char *);
1870 static void cp_parser_name_lookup_error
1871   (cp_parser *, tree, tree, const char *);
1872 static bool cp_parser_simulate_error
1873   (cp_parser *);
1874 static bool cp_parser_check_type_definition
1875   (cp_parser *);
1876 static void cp_parser_check_for_definition_in_return_type
1877   (cp_declarator *, tree);
1878 static void cp_parser_check_for_invalid_template_id
1879   (cp_parser *, tree);
1880 static bool cp_parser_non_integral_constant_expression
1881   (cp_parser *, const char *);
1882 static void cp_parser_diagnose_invalid_type_name
1883   (cp_parser *, tree, tree);
1884 static bool cp_parser_parse_and_diagnose_invalid_type_name
1885   (cp_parser *);
1886 static int cp_parser_skip_to_closing_parenthesis
1887   (cp_parser *, bool, bool, bool);
1888 static void cp_parser_skip_to_end_of_statement
1889   (cp_parser *);
1890 static void cp_parser_consume_semicolon_at_end_of_statement
1891   (cp_parser *);
1892 static void cp_parser_skip_to_end_of_block_or_statement
1893   (cp_parser *);
1894 static void cp_parser_skip_to_closing_brace
1895   (cp_parser *);
1896 static void cp_parser_skip_until_found
1897   (cp_parser *, enum cpp_ttype, const char *);
1898 static bool cp_parser_error_occurred
1899   (cp_parser *);
1900 static bool cp_parser_allow_gnu_extensions_p
1901   (cp_parser *);
1902 static bool cp_parser_is_string_literal
1903   (cp_token *);
1904 static bool cp_parser_is_keyword
1905   (cp_token *, enum rid);
1906 static tree cp_parser_make_typename_type
1907   (cp_parser *, tree, tree);
1908
1909 /* Returns nonzero if we are parsing tentatively.  */
1910
1911 static inline bool
1912 cp_parser_parsing_tentatively (cp_parser* parser)
1913 {
1914   return parser->context->next != NULL;
1915 }
1916
1917 /* Returns nonzero if TOKEN is a string literal.  */
1918
1919 static bool
1920 cp_parser_is_string_literal (cp_token* token)
1921 {
1922   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1923 }
1924
1925 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1926
1927 static bool
1928 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1929 {
1930   return token->keyword == keyword;
1931 }
1932
1933 /* A minimum or maximum operator has been seen.  As these are
1934    deprecated, issue a warning.  */
1935
1936 static inline void
1937 cp_parser_warn_min_max (void)
1938 {
1939   if (warn_deprecated && !in_system_header)
1940     warning (0, "minimum/maximum operators are deprecated");
1941 }
1942
1943 /* If not parsing tentatively, issue a diagnostic of the form
1944       FILE:LINE: MESSAGE before TOKEN
1945    where TOKEN is the next token in the input stream.  MESSAGE
1946    (specified by the caller) is usually of the form "expected
1947    OTHER-TOKEN".  */
1948
1949 static void
1950 cp_parser_error (cp_parser* parser, const char* message)
1951 {
1952   if (!cp_parser_simulate_error (parser))
1953     {
1954       cp_token *token = cp_lexer_peek_token (parser->lexer);
1955       /* This diagnostic makes more sense if it is tagged to the line
1956          of the token we just peeked at.  */
1957       cp_lexer_set_source_position_from_token (token);
1958       if (token->type == CPP_PRAGMA)
1959         {
1960           error ("%<#pragma%> is not allowed here");
1961           cp_lexer_purge_token (parser->lexer);
1962           return;
1963         }
1964       c_parse_error (message,
1965                      /* Because c_parser_error does not understand
1966                         CPP_KEYWORD, keywords are treated like
1967                         identifiers.  */
1968                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1969                      token->value);
1970     }
1971 }
1972
1973 /* Issue an error about name-lookup failing.  NAME is the
1974    IDENTIFIER_NODE DECL is the result of
1975    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1976    the thing that we hoped to find.  */
1977
1978 static void
1979 cp_parser_name_lookup_error (cp_parser* parser,
1980                              tree name,
1981                              tree decl,
1982                              const char* desired)
1983 {
1984   /* If name lookup completely failed, tell the user that NAME was not
1985      declared.  */
1986   if (decl == error_mark_node)
1987     {
1988       if (parser->scope && parser->scope != global_namespace)
1989         error ("%<%D::%D%> has not been declared",
1990                parser->scope, name);
1991       else if (parser->scope == global_namespace)
1992         error ("%<::%D%> has not been declared", name);
1993       else if (parser->object_scope
1994                && !CLASS_TYPE_P (parser->object_scope))
1995         error ("request for member %qD in non-class type %qT",
1996                name, parser->object_scope);
1997       else if (parser->object_scope)
1998         error ("%<%T::%D%> has not been declared",
1999                parser->object_scope, name);
2000       else
2001         error ("%qD has not been declared", name);
2002     }
2003   else if (parser->scope && parser->scope != global_namespace)
2004     error ("%<%D::%D%> %s", parser->scope, name, desired);
2005   else if (parser->scope == global_namespace)
2006     error ("%<::%D%> %s", name, desired);
2007   else
2008     error ("%qD %s", name, desired);
2009 }
2010
2011 /* If we are parsing tentatively, remember that an error has occurred
2012    during this tentative parse.  Returns true if the error was
2013    simulated; false if a message should be issued by the caller.  */
2014
2015 static bool
2016 cp_parser_simulate_error (cp_parser* parser)
2017 {
2018   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2019     {
2020       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2021       return true;
2022     }
2023   return false;
2024 }
2025
2026 /* Check for repeated decl-specifiers.  */
2027
2028 static void
2029 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2030 {
2031   cp_decl_spec ds;
2032
2033   for (ds = ds_first; ds != ds_last; ++ds)
2034     {
2035       unsigned count = decl_specs->specs[(int)ds];
2036       if (count < 2)
2037         continue;
2038       /* The "long" specifier is a special case because of "long long".  */
2039       if (ds == ds_long)
2040         {
2041           if (count > 2)
2042             error ("%<long long long%> is too long for GCC");
2043           else if (pedantic && !in_system_header && warn_long_long)
2044             pedwarn ("ISO C++ does not support %<long long%>");
2045         }
2046       else if (count > 1)
2047         {
2048           static const char *const decl_spec_names[] = {
2049             "signed",
2050             "unsigned",
2051             "short",
2052             "long",
2053             "const",
2054             "volatile",
2055             "restrict",
2056             "inline",
2057             "virtual",
2058             "explicit",
2059             "friend",
2060             "typedef",
2061             "__complex",
2062             "__thread"
2063           };
2064           error ("duplicate %qs", decl_spec_names[(int)ds]);
2065         }
2066     }
2067 }
2068
2069 /* This function is called when a type is defined.  If type
2070    definitions are forbidden at this point, an error message is
2071    issued.  */
2072
2073 static bool
2074 cp_parser_check_type_definition (cp_parser* parser)
2075 {
2076   /* If types are forbidden here, issue a message.  */
2077   if (parser->type_definition_forbidden_message)
2078     {
2079       /* Use `%s' to print the string in case there are any escape
2080          characters in the message.  */
2081       error ("%s", parser->type_definition_forbidden_message);
2082       return false;
2083     }
2084   return true;
2085 }
2086
2087 /* This function is called when the DECLARATOR is processed.  The TYPE
2088    was a type defined in the decl-specifiers.  If it is invalid to
2089    define a type in the decl-specifiers for DECLARATOR, an error is
2090    issued.  */
2091
2092 static void
2093 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2094                                                tree type)
2095 {
2096   /* [dcl.fct] forbids type definitions in return types.
2097      Unfortunately, it's not easy to know whether or not we are
2098      processing a return type until after the fact.  */
2099   while (declarator
2100          && (declarator->kind == cdk_pointer
2101              || declarator->kind == cdk_reference
2102              || declarator->kind == cdk_ptrmem))
2103     declarator = declarator->declarator;
2104   if (declarator
2105       && declarator->kind == cdk_function)
2106     {
2107       error ("new types may not be defined in a return type");
2108       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2109               type);
2110     }
2111 }
2112
2113 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2114    "<" in any valid C++ program.  If the next token is indeed "<",
2115    issue a message warning the user about what appears to be an
2116    invalid attempt to form a template-id.  */
2117
2118 static void
2119 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2120                                          tree type)
2121 {
2122   cp_token_position start = 0;
2123
2124   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2125     {
2126       if (TYPE_P (type))
2127         error ("%qT is not a template", type);
2128       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2129         error ("%qE is not a template", type);
2130       else
2131         error ("invalid template-id");
2132       /* Remember the location of the invalid "<".  */
2133       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2134         start = cp_lexer_token_position (parser->lexer, true);
2135       /* Consume the "<".  */
2136       cp_lexer_consume_token (parser->lexer);
2137       /* Parse the template arguments.  */
2138       cp_parser_enclosed_template_argument_list (parser);
2139       /* Permanently remove the invalid template arguments so that
2140          this error message is not issued again.  */
2141       if (start)
2142         cp_lexer_purge_tokens_after (parser->lexer, start);
2143     }
2144 }
2145
2146 /* If parsing an integral constant-expression, issue an error message
2147    about the fact that THING appeared and return true.  Otherwise,
2148    return false.  In either case, set
2149    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2150
2151 static bool
2152 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2153                                             const char *thing)
2154 {
2155   parser->non_integral_constant_expression_p = true;
2156   if (parser->integral_constant_expression_p)
2157     {
2158       if (!parser->allow_non_integral_constant_expression_p)
2159         {
2160           error ("%s cannot appear in a constant-expression", thing);
2161           return true;
2162         }
2163     }
2164   return false;
2165 }
2166
2167 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2168    qualifying scope (or NULL, if none) for ID.  This function commits
2169    to the current active tentative parse, if any.  (Otherwise, the
2170    problematic construct might be encountered again later, resulting
2171    in duplicate error messages.)  */
2172
2173 static void
2174 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2175 {
2176   tree decl, old_scope;
2177   /* Try to lookup the identifier.  */
2178   old_scope = parser->scope;
2179   parser->scope = scope;
2180   decl = cp_parser_lookup_name_simple (parser, id);
2181   parser->scope = old_scope;
2182   /* If the lookup found a template-name, it means that the user forgot
2183   to specify an argument list. Emit a useful error message.  */
2184   if (TREE_CODE (decl) == TEMPLATE_DECL)
2185     error ("invalid use of template-name %qE without an argument list", decl);
2186   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2187     error ("invalid use of destructor %qD as a type", id);
2188   else if (TREE_CODE (decl) == TYPE_DECL)
2189     /* Something like 'unsigned A a;'  */
2190     error ("invalid combination of multiple type-specifiers");
2191   else if (!parser->scope)
2192     {
2193       /* Issue an error message.  */
2194       error ("%qE does not name a type", id);
2195       /* If we're in a template class, it's possible that the user was
2196          referring to a type from a base class.  For example:
2197
2198            template <typename T> struct A { typedef T X; };
2199            template <typename T> struct B : public A<T> { X x; };
2200
2201          The user should have said "typename A<T>::X".  */
2202       if (processing_template_decl && current_class_type
2203           && TYPE_BINFO (current_class_type))
2204         {
2205           tree b;
2206
2207           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2208                b;
2209                b = TREE_CHAIN (b))
2210             {
2211               tree base_type = BINFO_TYPE (b);
2212               if (CLASS_TYPE_P (base_type)
2213                   && dependent_type_p (base_type))
2214                 {
2215                   tree field;
2216                   /* Go from a particular instantiation of the
2217                      template (which will have an empty TYPE_FIELDs),
2218                      to the main version.  */
2219                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2220                   for (field = TYPE_FIELDS (base_type);
2221                        field;
2222                        field = TREE_CHAIN (field))
2223                     if (TREE_CODE (field) == TYPE_DECL
2224                         && DECL_NAME (field) == id)
2225                       {
2226                         inform ("(perhaps %<typename %T::%E%> was intended)",
2227                                 BINFO_TYPE (b), id);
2228                         break;
2229                       }
2230                   if (field)
2231                     break;
2232                 }
2233             }
2234         }
2235     }
2236   /* Here we diagnose qualified-ids where the scope is actually correct,
2237      but the identifier does not resolve to a valid type name.  */
2238   else if (parser->scope != error_mark_node)
2239     {
2240       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2241         error ("%qE in namespace %qE does not name a type",
2242                id, parser->scope);
2243       else if (TYPE_P (parser->scope))
2244         error ("%qE in class %qT does not name a type", id, parser->scope);
2245       else
2246         gcc_unreachable ();
2247     }
2248   cp_parser_commit_to_tentative_parse (parser);
2249 }
2250
2251 /* Check for a common situation where a type-name should be present,
2252    but is not, and issue a sensible error message.  Returns true if an
2253    invalid type-name was detected.
2254
2255    The situation handled by this function are variable declarations of the
2256    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2257    Usually, `ID' should name a type, but if we got here it means that it
2258    does not. We try to emit the best possible error message depending on
2259    how exactly the id-expression looks like.
2260 */
2261
2262 static bool
2263 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2264 {
2265   tree id;
2266
2267   cp_parser_parse_tentatively (parser);
2268   id = cp_parser_id_expression (parser,
2269                                 /*template_keyword_p=*/false,
2270                                 /*check_dependency_p=*/true,
2271                                 /*template_p=*/NULL,
2272                                 /*declarator_p=*/true);
2273   /* After the id-expression, there should be a plain identifier,
2274      otherwise this is not a simple variable declaration. Also, if
2275      the scope is dependent, we cannot do much.  */
2276   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2277       || (parser->scope && TYPE_P (parser->scope)
2278           && dependent_type_p (parser->scope)))
2279     {
2280       cp_parser_abort_tentative_parse (parser);
2281       return false;
2282     }
2283   if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2284     return false;
2285
2286   /* Emit a diagnostic for the invalid type.  */
2287   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2288   /* Skip to the end of the declaration; there's no point in
2289      trying to process it.  */
2290   cp_parser_skip_to_end_of_block_or_statement (parser);
2291   return true;
2292 }
2293
2294 /* Consume tokens up to, and including, the next non-nested closing `)'.
2295    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2296    are doing error recovery. Returns -1 if OR_COMMA is true and we
2297    found an unnested comma.  */
2298
2299 static int
2300 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2301                                        bool recovering,
2302                                        bool or_comma,
2303                                        bool consume_paren)
2304 {
2305   unsigned paren_depth = 0;
2306   unsigned brace_depth = 0;
2307   int result;
2308
2309   if (recovering && !or_comma
2310       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2311     return 0;
2312
2313   while (true)
2314     {
2315       cp_token *token;
2316
2317       /* If we've run out of tokens, then there is no closing `)'.  */
2318       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2319         {
2320           result = 0;
2321           break;
2322         }
2323
2324       token = cp_lexer_peek_token (parser->lexer);
2325
2326       /* This matches the processing in skip_to_end_of_statement.  */
2327       if (token->type == CPP_SEMICOLON && !brace_depth)
2328         {
2329           result = 0;
2330           break;
2331         }
2332       if (token->type == CPP_OPEN_BRACE)
2333         ++brace_depth;
2334       if (token->type == CPP_CLOSE_BRACE)
2335         {
2336           if (!brace_depth--)
2337             {
2338               result = 0;
2339               break;
2340             }
2341         }
2342       if (recovering && or_comma && token->type == CPP_COMMA
2343           && !brace_depth && !paren_depth)
2344         {
2345           result = -1;
2346           break;
2347         }
2348
2349       if (!brace_depth)
2350         {
2351           /* If it is an `(', we have entered another level of nesting.  */
2352           if (token->type == CPP_OPEN_PAREN)
2353             ++paren_depth;
2354           /* If it is a `)', then we might be done.  */
2355           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2356             {
2357               if (consume_paren)
2358                 cp_lexer_consume_token (parser->lexer);
2359               {
2360                 result = 1;
2361                 break;
2362               }
2363             }
2364         }
2365
2366       /* Consume the token.  */
2367       cp_lexer_consume_token (parser->lexer);
2368     }
2369
2370   return result;
2371 }
2372
2373 /* Consume tokens until we reach the end of the current statement.
2374    Normally, that will be just before consuming a `;'.  However, if a
2375    non-nested `}' comes first, then we stop before consuming that.  */
2376
2377 static void
2378 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2379 {
2380   unsigned nesting_depth = 0;
2381
2382   while (true)
2383     {
2384       cp_token *token;
2385
2386       /* Peek at the next token.  */
2387       token = cp_lexer_peek_token (parser->lexer);
2388       /* If we've run out of tokens, stop.  */
2389       if (token->type == CPP_EOF)
2390         break;
2391       /* If the next token is a `;', we have reached the end of the
2392          statement.  */
2393       if (token->type == CPP_SEMICOLON && !nesting_depth)
2394         break;
2395       /* If the next token is a non-nested `}', then we have reached
2396          the end of the current block.  */
2397       if (token->type == CPP_CLOSE_BRACE)
2398         {
2399           /* If this is a non-nested `}', stop before consuming it.
2400              That way, when confronted with something like:
2401
2402                { 3 + }
2403
2404              we stop before consuming the closing `}', even though we
2405              have not yet reached a `;'.  */
2406           if (nesting_depth == 0)
2407             break;
2408           /* If it is the closing `}' for a block that we have
2409              scanned, stop -- but only after consuming the token.
2410              That way given:
2411
2412                 void f g () { ... }
2413                 typedef int I;
2414
2415              we will stop after the body of the erroneously declared
2416              function, but before consuming the following `typedef'
2417              declaration.  */
2418           if (--nesting_depth == 0)
2419             {
2420               cp_lexer_consume_token (parser->lexer);
2421               break;
2422             }
2423         }
2424       /* If it the next token is a `{', then we are entering a new
2425          block.  Consume the entire block.  */
2426       else if (token->type == CPP_OPEN_BRACE)
2427         ++nesting_depth;
2428       /* Consume the token.  */
2429       cp_lexer_consume_token (parser->lexer);
2430     }
2431 }
2432
2433 /* This function is called at the end of a statement or declaration.
2434    If the next token is a semicolon, it is consumed; otherwise, error
2435    recovery is attempted.  */
2436
2437 static void
2438 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2439 {
2440   /* Look for the trailing `;'.  */
2441   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2442     {
2443       /* If there is additional (erroneous) input, skip to the end of
2444          the statement.  */
2445       cp_parser_skip_to_end_of_statement (parser);
2446       /* If the next token is now a `;', consume it.  */
2447       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2448         cp_lexer_consume_token (parser->lexer);
2449     }
2450 }
2451
2452 /* Skip tokens until we have consumed an entire block, or until we
2453    have consumed a non-nested `;'.  */
2454
2455 static void
2456 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2457 {
2458   int nesting_depth = 0;
2459
2460   while (nesting_depth >= 0)
2461     {
2462       cp_token *token = cp_lexer_peek_token (parser->lexer);
2463
2464       if (token->type == CPP_EOF)
2465         break;
2466
2467       switch (token->type)
2468         {
2469         case CPP_EOF:
2470           /* If we've run out of tokens, stop.  */
2471           nesting_depth = -1;
2472           continue;
2473
2474         case CPP_SEMICOLON:
2475           /* Stop if this is an unnested ';'. */
2476           if (!nesting_depth)
2477             nesting_depth = -1;
2478           break;
2479
2480         case CPP_CLOSE_BRACE:
2481           /* Stop if this is an unnested '}', or closes the outermost
2482              nesting level.  */
2483           nesting_depth--;
2484           if (!nesting_depth)
2485             nesting_depth = -1;
2486           break;
2487
2488         case CPP_OPEN_BRACE:
2489           /* Nest. */
2490           nesting_depth++;
2491           break;
2492
2493         default:
2494           break;
2495         }
2496
2497       /* Consume the token.  */
2498       cp_lexer_consume_token (parser->lexer);
2499
2500     }
2501 }
2502
2503 /* Skip tokens until a non-nested closing curly brace is the next
2504    token.  */
2505
2506 static void
2507 cp_parser_skip_to_closing_brace (cp_parser *parser)
2508 {
2509   unsigned nesting_depth = 0;
2510
2511   while (true)
2512     {
2513       cp_token *token;
2514
2515       /* Peek at the next token.  */
2516       token = cp_lexer_peek_token (parser->lexer);
2517       /* If we've run out of tokens, stop.  */
2518       if (token->type == CPP_EOF)
2519         break;
2520       /* If the next token is a non-nested `}', then we have reached
2521          the end of the current block.  */
2522       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2523         break;
2524       /* If it the next token is a `{', then we are entering a new
2525          block.  Consume the entire block.  */
2526       else if (token->type == CPP_OPEN_BRACE)
2527         ++nesting_depth;
2528       /* Consume the token.  */
2529       cp_lexer_consume_token (parser->lexer);
2530     }
2531 }
2532
2533 /* This is a simple wrapper around make_typename_type. When the id is
2534    an unresolved identifier node, we can provide a superior diagnostic
2535    using cp_parser_diagnose_invalid_type_name.  */
2536
2537 static tree
2538 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2539 {
2540   tree result;
2541   if (TREE_CODE (id) == IDENTIFIER_NODE)
2542     {
2543       result = make_typename_type (scope, id, typename_type,
2544                                    /*complain=*/0);
2545       if (result == error_mark_node)
2546         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2547       return result;
2548     }
2549   return make_typename_type (scope, id, typename_type, tf_error);
2550 }
2551
2552
2553 /* Create a new C++ parser.  */
2554
2555 static cp_parser *
2556 cp_parser_new (void)
2557 {
2558   cp_parser *parser;
2559   cp_lexer *lexer;
2560   unsigned i;
2561
2562   /* cp_lexer_new_main is called before calling ggc_alloc because
2563      cp_lexer_new_main might load a PCH file.  */
2564   lexer = cp_lexer_new_main ();
2565
2566   /* Initialize the binops_by_token so that we can get the tree
2567      directly from the token.  */
2568   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2569     binops_by_token[binops[i].token_type] = binops[i];
2570
2571   parser = GGC_CNEW (cp_parser);
2572   parser->lexer = lexer;
2573   parser->context = cp_parser_context_new (NULL);
2574
2575   /* For now, we always accept GNU extensions.  */
2576   parser->allow_gnu_extensions_p = 1;
2577
2578   /* The `>' token is a greater-than operator, not the end of a
2579      template-id.  */
2580   parser->greater_than_is_operator_p = true;
2581
2582   parser->default_arg_ok_p = true;
2583
2584   /* We are not parsing a constant-expression.  */
2585   parser->integral_constant_expression_p = false;
2586   parser->allow_non_integral_constant_expression_p = false;
2587   parser->non_integral_constant_expression_p = false;
2588
2589   /* Local variable names are not forbidden.  */
2590   parser->local_variables_forbidden_p = false;
2591
2592   /* We are not processing an `extern "C"' declaration.  */
2593   parser->in_unbraced_linkage_specification_p = false;
2594
2595   /* We are not processing a declarator.  */
2596   parser->in_declarator_p = false;
2597
2598   /* We are not processing a template-argument-list.  */
2599   parser->in_template_argument_list_p = false;
2600
2601   /* We are not in an iteration statement.  */
2602   parser->in_iteration_statement_p = false;
2603
2604   /* We are not in a switch statement.  */
2605   parser->in_switch_statement_p = false;
2606
2607   /* We are not parsing a type-id inside an expression.  */
2608   parser->in_type_id_in_expr_p = false;
2609
2610   /* Declarations aren't implicitly extern "C".  */
2611   parser->implicit_extern_c = false;
2612
2613   /* String literals should be translated to the execution character set.  */
2614   parser->translate_strings_p = true;
2615
2616   /* We are not parsing a function body.  */
2617   parser->in_function_body = false;
2618
2619   /* The unparsed function queue is empty.  */
2620   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2621
2622   /* There are no classes being defined.  */
2623   parser->num_classes_being_defined = 0;
2624
2625   /* No template parameters apply.  */
2626   parser->num_template_parameter_lists = 0;
2627
2628   return parser;
2629 }
2630
2631 /* Create a cp_lexer structure which will emit the tokens in CACHE
2632    and push it onto the parser's lexer stack.  This is used for delayed
2633    parsing of in-class method bodies and default arguments, and should
2634    not be confused with tentative parsing.  */
2635 static void
2636 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2637 {
2638   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2639   lexer->next = parser->lexer;
2640   parser->lexer = lexer;
2641
2642   /* Move the current source position to that of the first token in the
2643      new lexer.  */
2644   cp_lexer_set_source_position_from_token (lexer->next_token);
2645 }
2646
2647 /* Pop the top lexer off the parser stack.  This is never used for the
2648    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2649 static void
2650 cp_parser_pop_lexer (cp_parser *parser)
2651 {
2652   cp_lexer *lexer = parser->lexer;
2653   parser->lexer = lexer->next;
2654   cp_lexer_destroy (lexer);
2655
2656   /* Put the current source position back where it was before this
2657      lexer was pushed.  */
2658   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2659 }
2660
2661 /* Lexical conventions [gram.lex]  */
2662
2663 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2664    identifier.  */
2665
2666 static tree
2667 cp_parser_identifier (cp_parser* parser)
2668 {
2669   cp_token *token;
2670
2671   /* Look for the identifier.  */
2672   token = cp_parser_require (parser, CPP_NAME, "identifier");
2673   /* Return the value.  */
2674   return token ? token->value : error_mark_node;
2675 }
2676
2677 /* Parse a sequence of adjacent string constants.  Returns a
2678    TREE_STRING representing the combined, nul-terminated string
2679    constant.  If TRANSLATE is true, translate the string to the
2680    execution character set.  If WIDE_OK is true, a wide string is
2681    invalid here.
2682
2683    C++98 [lex.string] says that if a narrow string literal token is
2684    adjacent to a wide string literal token, the behavior is undefined.
2685    However, C99 6.4.5p4 says that this results in a wide string literal.
2686    We follow C99 here, for consistency with the C front end.
2687
2688    This code is largely lifted from lex_string() in c-lex.c.
2689
2690    FUTURE: ObjC++ will need to handle @-strings here.  */
2691 static tree
2692 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2693 {
2694   tree value;
2695   bool wide = false;
2696   size_t count;
2697   struct obstack str_ob;
2698   cpp_string str, istr, *strs;
2699   cp_token *tok;
2700
2701   tok = cp_lexer_peek_token (parser->lexer);
2702   if (!cp_parser_is_string_literal (tok))
2703     {
2704       cp_parser_error (parser, "expected string-literal");
2705       return error_mark_node;
2706     }
2707
2708   /* Try to avoid the overhead of creating and destroying an obstack
2709      for the common case of just one string.  */
2710   if (!cp_parser_is_string_literal
2711       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2712     {
2713       cp_lexer_consume_token (parser->lexer);
2714
2715       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2716       str.len = TREE_STRING_LENGTH (tok->value);
2717       count = 1;
2718       if (tok->type == CPP_WSTRING)
2719         wide = true;
2720
2721       strs = &str;
2722     }
2723   else
2724     {
2725       gcc_obstack_init (&str_ob);
2726       count = 0;
2727
2728       do
2729         {
2730           cp_lexer_consume_token (parser->lexer);
2731           count++;
2732           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2733           str.len = TREE_STRING_LENGTH (tok->value);
2734           if (tok->type == CPP_WSTRING)
2735             wide = true;
2736
2737           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2738
2739           tok = cp_lexer_peek_token (parser->lexer);
2740         }
2741       while (cp_parser_is_string_literal (tok));
2742
2743       strs = (cpp_string *) obstack_finish (&str_ob);
2744     }
2745
2746   if (wide && !wide_ok)
2747     {
2748       cp_parser_error (parser, "a wide string is invalid in this context");
2749       wide = false;
2750     }
2751
2752   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2753       (parse_in, strs, count, &istr, wide))
2754     {
2755       value = build_string (istr.len, (char *)istr.text);
2756       free ((void *)istr.text);
2757
2758       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2759       value = fix_string_type (value);
2760     }
2761   else
2762     /* cpp_interpret_string has issued an error.  */
2763     value = error_mark_node;
2764
2765   if (count > 1)
2766     obstack_free (&str_ob, 0);
2767
2768   return value;
2769 }
2770
2771
2772 /* Basic concepts [gram.basic]  */
2773
2774 /* Parse a translation-unit.
2775
2776    translation-unit:
2777      declaration-seq [opt]
2778
2779    Returns TRUE if all went well.  */
2780
2781 static bool
2782 cp_parser_translation_unit (cp_parser* parser)
2783 {
2784   /* The address of the first non-permanent object on the declarator
2785      obstack.  */
2786   static void *declarator_obstack_base;
2787
2788   bool success;
2789
2790   /* Create the declarator obstack, if necessary.  */
2791   if (!cp_error_declarator)
2792     {
2793       gcc_obstack_init (&declarator_obstack);
2794       /* Create the error declarator.  */
2795       cp_error_declarator = make_declarator (cdk_error);
2796       /* Create the empty parameter list.  */
2797       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2798       /* Remember where the base of the declarator obstack lies.  */
2799       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2800     }
2801
2802   cp_parser_declaration_seq_opt (parser);
2803   
2804   /* If there are no tokens left then all went well.  */
2805   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2806     {
2807       /* Get rid of the token array; we don't need it any more.  */
2808       cp_lexer_destroy (parser->lexer);
2809       parser->lexer = NULL;
2810       
2811       /* This file might have been a context that's implicitly extern
2812          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2813       if (parser->implicit_extern_c)
2814         {
2815           pop_lang_context ();
2816           parser->implicit_extern_c = false;
2817         }
2818       
2819       /* Finish up.  */
2820       finish_translation_unit ();
2821       
2822       success = true;
2823     }
2824   else
2825     {
2826       cp_parser_error (parser, "expected declaration");
2827       success = false;
2828     }
2829   
2830   /* Make sure the declarator obstack was fully cleaned up.  */
2831   gcc_assert (obstack_next_free (&declarator_obstack)
2832               == declarator_obstack_base);
2833
2834   /* All went well.  */
2835   return success;
2836 }
2837
2838 /* Expressions [gram.expr] */
2839
2840 /* Parse a primary-expression.
2841
2842    primary-expression:
2843      literal
2844      this
2845      ( expression )
2846      id-expression
2847
2848    GNU Extensions:
2849
2850    primary-expression:
2851      ( compound-statement )
2852      __builtin_va_arg ( assignment-expression , type-id )
2853
2854    Objective-C++ Extension:
2855
2856    primary-expression:
2857      objc-expression
2858
2859    literal:
2860      __null
2861
2862    ADDRESS_P is true iff this expression was immediately preceded by
2863    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2864    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2865    true iff this expression is a tempalte argument.
2866
2867    Returns a representation of the expression.  Upon return, *IDK
2868    indicates what kind of id-expression (if any) was present.  */
2869
2870 static tree
2871 cp_parser_primary_expression (cp_parser *parser,
2872                               bool address_p,
2873                               bool cast_p,
2874                               bool template_arg_p,
2875                               cp_id_kind *idk)
2876 {
2877   cp_token *token;
2878
2879   /* Assume the primary expression is not an id-expression.  */
2880   *idk = CP_ID_KIND_NONE;
2881
2882   /* Peek at the next token.  */
2883   token = cp_lexer_peek_token (parser->lexer);
2884   switch (token->type)
2885     {
2886       /* literal:
2887            integer-literal
2888            character-literal
2889            floating-literal
2890            string-literal
2891            boolean-literal  */
2892     case CPP_CHAR:
2893     case CPP_WCHAR:
2894     case CPP_NUMBER:
2895       token = cp_lexer_consume_token (parser->lexer);
2896       /* Floating-point literals are only allowed in an integral
2897          constant expression if they are cast to an integral or
2898          enumeration type.  */
2899       if (TREE_CODE (token->value) == REAL_CST
2900           && parser->integral_constant_expression_p
2901           && pedantic)
2902         {
2903           /* CAST_P will be set even in invalid code like "int(2.7 +
2904              ...)".   Therefore, we have to check that the next token
2905              is sure to end the cast.  */
2906           if (cast_p)
2907             {
2908               cp_token *next_token;
2909
2910               next_token = cp_lexer_peek_token (parser->lexer);
2911               if (/* The comma at the end of an
2912                      enumerator-definition.  */
2913                   next_token->type != CPP_COMMA
2914                   /* The curly brace at the end of an enum-specifier.  */
2915                   && next_token->type != CPP_CLOSE_BRACE
2916                   /* The end of a statement.  */
2917                   && next_token->type != CPP_SEMICOLON
2918                   /* The end of the cast-expression.  */
2919                   && next_token->type != CPP_CLOSE_PAREN
2920                   /* The end of an array bound.  */
2921                   && next_token->type != CPP_CLOSE_SQUARE
2922                   /* The closing ">" in a template-argument-list.  */
2923                   && (next_token->type != CPP_GREATER
2924                       || parser->greater_than_is_operator_p))
2925                 cast_p = false;
2926             }
2927
2928           /* If we are within a cast, then the constraint that the
2929              cast is to an integral or enumeration type will be
2930              checked at that point.  If we are not within a cast, then
2931              this code is invalid.  */
2932           if (!cast_p)
2933             cp_parser_non_integral_constant_expression
2934               (parser, "floating-point literal");
2935         }
2936       return token->value;
2937
2938     case CPP_STRING:
2939     case CPP_WSTRING:
2940       /* ??? Should wide strings be allowed when parser->translate_strings_p
2941          is false (i.e. in attributes)?  If not, we can kill the third
2942          argument to cp_parser_string_literal.  */
2943       return cp_parser_string_literal (parser,
2944                                        parser->translate_strings_p,
2945                                        true);
2946
2947     case CPP_OPEN_PAREN:
2948       {
2949         tree expr;
2950         bool saved_greater_than_is_operator_p;
2951
2952         /* Consume the `('.  */
2953         cp_lexer_consume_token (parser->lexer);
2954         /* Within a parenthesized expression, a `>' token is always
2955            the greater-than operator.  */
2956         saved_greater_than_is_operator_p
2957           = parser->greater_than_is_operator_p;
2958         parser->greater_than_is_operator_p = true;
2959         /* If we see `( { ' then we are looking at the beginning of
2960            a GNU statement-expression.  */
2961         if (cp_parser_allow_gnu_extensions_p (parser)
2962             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2963           {
2964             /* Statement-expressions are not allowed by the standard.  */
2965             if (pedantic)
2966               pedwarn ("ISO C++ forbids braced-groups within expressions");
2967
2968             /* And they're not allowed outside of a function-body; you
2969                cannot, for example, write:
2970
2971                  int i = ({ int j = 3; j + 1; });
2972
2973                at class or namespace scope.  */
2974             if (!parser->in_function_body)
2975               error ("statement-expressions are allowed only inside functions");
2976             /* Start the statement-expression.  */
2977             expr = begin_stmt_expr ();
2978             /* Parse the compound-statement.  */
2979             cp_parser_compound_statement (parser, expr, false);
2980             /* Finish up.  */
2981             expr = finish_stmt_expr (expr, false);
2982           }
2983         else
2984           {
2985             /* Parse the parenthesized expression.  */
2986             expr = cp_parser_expression (parser, cast_p);
2987             /* Let the front end know that this expression was
2988                enclosed in parentheses. This matters in case, for
2989                example, the expression is of the form `A::B', since
2990                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2991                not.  */
2992             finish_parenthesized_expr (expr);
2993           }
2994         /* The `>' token might be the end of a template-id or
2995            template-parameter-list now.  */
2996         parser->greater_than_is_operator_p
2997           = saved_greater_than_is_operator_p;
2998         /* Consume the `)'.  */
2999         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3000           cp_parser_skip_to_end_of_statement (parser);
3001
3002         return expr;
3003       }
3004
3005     case CPP_KEYWORD:
3006       switch (token->keyword)
3007         {
3008           /* These two are the boolean literals.  */
3009         case RID_TRUE:
3010           cp_lexer_consume_token (parser->lexer);
3011           return boolean_true_node;
3012         case RID_FALSE:
3013           cp_lexer_consume_token (parser->lexer);
3014           return boolean_false_node;
3015
3016           /* The `__null' literal.  */
3017         case RID_NULL:
3018           cp_lexer_consume_token (parser->lexer);
3019           return null_node;
3020
3021           /* Recognize the `this' keyword.  */
3022         case RID_THIS:
3023           cp_lexer_consume_token (parser->lexer);
3024           if (parser->local_variables_forbidden_p)
3025             {
3026               error ("%<this%> may not be used in this context");
3027               return error_mark_node;
3028             }
3029           /* Pointers cannot appear in constant-expressions.  */
3030           if (cp_parser_non_integral_constant_expression (parser,
3031                                                           "`this'"))
3032             return error_mark_node;
3033           return finish_this_expr ();
3034
3035           /* The `operator' keyword can be the beginning of an
3036              id-expression.  */
3037         case RID_OPERATOR:
3038           goto id_expression;
3039
3040         case RID_FUNCTION_NAME:
3041         case RID_PRETTY_FUNCTION_NAME:
3042         case RID_C99_FUNCTION_NAME:
3043           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3044              __func__ are the names of variables -- but they are
3045              treated specially.  Therefore, they are handled here,
3046              rather than relying on the generic id-expression logic
3047              below.  Grammatically, these names are id-expressions.
3048
3049              Consume the token.  */
3050           token = cp_lexer_consume_token (parser->lexer);
3051           /* Look up the name.  */
3052           return finish_fname (token->value);
3053
3054         case RID_VA_ARG:
3055           {
3056             tree expression;
3057             tree type;
3058
3059             /* The `__builtin_va_arg' construct is used to handle
3060                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3061             cp_lexer_consume_token (parser->lexer);
3062             /* Look for the opening `('.  */
3063             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3064             /* Now, parse the assignment-expression.  */
3065             expression = cp_parser_assignment_expression (parser,
3066                                                           /*cast_p=*/false);
3067             /* Look for the `,'.  */
3068             cp_parser_require (parser, CPP_COMMA, "`,'");
3069             /* Parse the type-id.  */
3070             type = cp_parser_type_id (parser);
3071             /* Look for the closing `)'.  */
3072             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3073             /* Using `va_arg' in a constant-expression is not
3074                allowed.  */
3075             if (cp_parser_non_integral_constant_expression (parser,
3076                                                             "`va_arg'"))
3077               return error_mark_node;
3078             return build_x_va_arg (expression, type);
3079           }
3080
3081         case RID_OFFSETOF:
3082           return cp_parser_builtin_offsetof (parser);
3083
3084           /* Objective-C++ expressions.  */
3085         case RID_AT_ENCODE:
3086         case RID_AT_PROTOCOL:
3087         case RID_AT_SELECTOR:
3088           return cp_parser_objc_expression (parser);
3089
3090         default:
3091           cp_parser_error (parser, "expected primary-expression");
3092           return error_mark_node;
3093         }
3094
3095       /* An id-expression can start with either an identifier, a
3096          `::' as the beginning of a qualified-id, or the "operator"
3097          keyword.  */
3098     case CPP_NAME:
3099     case CPP_SCOPE:
3100     case CPP_TEMPLATE_ID:
3101     case CPP_NESTED_NAME_SPECIFIER:
3102       {
3103         tree id_expression;
3104         tree decl;
3105         const char *error_msg;
3106         bool template_p;
3107         bool done;
3108
3109       id_expression:
3110         /* Parse the id-expression.  */
3111         id_expression
3112           = cp_parser_id_expression (parser,
3113                                      /*template_keyword_p=*/false,
3114                                      /*check_dependency_p=*/true,
3115                                      &template_p,
3116                                      /*declarator_p=*/false);
3117         if (id_expression == error_mark_node)
3118           return error_mark_node;
3119         token = cp_lexer_peek_token (parser->lexer);
3120         done = (token->type != CPP_OPEN_SQUARE
3121                 && token->type != CPP_OPEN_PAREN
3122                 && token->type != CPP_DOT
3123                 && token->type != CPP_DEREF
3124                 && token->type != CPP_PLUS_PLUS
3125                 && token->type != CPP_MINUS_MINUS);
3126         /* If we have a template-id, then no further lookup is
3127            required.  If the template-id was for a template-class, we
3128            will sometimes have a TYPE_DECL at this point.  */
3129         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3130                  || TREE_CODE (id_expression) == TYPE_DECL)
3131           decl = id_expression;
3132         /* Look up the name.  */
3133         else
3134           {
3135             tree ambiguous_decls;
3136
3137             decl = cp_parser_lookup_name (parser, id_expression,
3138                                           none_type,
3139                                           template_p,
3140                                           /*is_namespace=*/false,
3141                                           /*check_dependency=*/true,
3142                                           &ambiguous_decls);
3143             /* If the lookup was ambiguous, an error will already have
3144                been issued.  */
3145             if (ambiguous_decls)
3146               return error_mark_node;
3147
3148             /* In Objective-C++, an instance variable (ivar) may be preferred
3149                to whatever cp_parser_lookup_name() found.  */
3150             decl = objc_lookup_ivar (decl, id_expression);
3151
3152             /* If name lookup gives us a SCOPE_REF, then the
3153                qualifying scope was dependent.  */
3154             if (TREE_CODE (decl) == SCOPE_REF)
3155               return decl;
3156             /* Check to see if DECL is a local variable in a context
3157                where that is forbidden.  */
3158             if (parser->local_variables_forbidden_p
3159                 && local_variable_p (decl))
3160               {
3161                 /* It might be that we only found DECL because we are
3162                    trying to be generous with pre-ISO scoping rules.
3163                    For example, consider:
3164
3165                      int i;
3166                      void g() {
3167                        for (int i = 0; i < 10; ++i) {}
3168                        extern void f(int j = i);
3169                      }
3170
3171                    Here, name look up will originally find the out
3172                    of scope `i'.  We need to issue a warning message,
3173                    but then use the global `i'.  */
3174                 decl = check_for_out_of_scope_variable (decl);
3175                 if (local_variable_p (decl))
3176                   {
3177                     error ("local variable %qD may not appear in this context",
3178                            decl);
3179                     return error_mark_node;
3180                   }
3181               }
3182           }
3183
3184         decl = (finish_id_expression 
3185                 (id_expression, decl, parser->scope,
3186                  idk,
3187                  parser->integral_constant_expression_p,
3188                  parser->allow_non_integral_constant_expression_p,
3189                  &parser->non_integral_constant_expression_p,
3190                  template_p, done, address_p,
3191                  template_arg_p,
3192                  &error_msg));
3193         if (error_msg)
3194           cp_parser_error (parser, error_msg);
3195         return decl;
3196       }
3197
3198       /* Anything else is an error.  */
3199     default:
3200       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3201       if (c_dialect_objc ()
3202           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3203         return cp_parser_objc_expression (parser);
3204
3205       cp_parser_error (parser, "expected primary-expression");
3206       return error_mark_node;
3207     }
3208 }
3209
3210 /* Parse an id-expression.
3211
3212    id-expression:
3213      unqualified-id
3214      qualified-id
3215
3216    qualified-id:
3217      :: [opt] nested-name-specifier template [opt] unqualified-id
3218      :: identifier
3219      :: operator-function-id
3220      :: template-id
3221
3222    Return a representation of the unqualified portion of the
3223    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3224    a `::' or nested-name-specifier.
3225
3226    Often, if the id-expression was a qualified-id, the caller will
3227    want to make a SCOPE_REF to represent the qualified-id.  This
3228    function does not do this in order to avoid wastefully creating
3229    SCOPE_REFs when they are not required.
3230
3231    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3232    `template' keyword.
3233
3234    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3235    uninstantiated templates.
3236
3237    If *TEMPLATE_P is non-NULL, it is set to true iff the
3238    `template' keyword is used to explicitly indicate that the entity
3239    named is a template.
3240
3241    If DECLARATOR_P is true, the id-expression is appearing as part of
3242    a declarator, rather than as part of an expression.  */
3243
3244 static tree
3245 cp_parser_id_expression (cp_parser *parser,
3246                          bool template_keyword_p,
3247                          bool check_dependency_p,
3248                          bool *template_p,
3249                          bool declarator_p)
3250 {
3251   bool global_scope_p;
3252   bool nested_name_specifier_p;
3253
3254   /* Assume the `template' keyword was not used.  */
3255   if (template_p)
3256     *template_p = template_keyword_p;
3257
3258   /* Look for the optional `::' operator.  */
3259   global_scope_p
3260     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3261        != NULL_TREE);
3262   /* Look for the optional nested-name-specifier.  */
3263   nested_name_specifier_p
3264     = (cp_parser_nested_name_specifier_opt (parser,
3265                                             /*typename_keyword_p=*/false,
3266                                             check_dependency_p,
3267                                             /*type_p=*/false,
3268                                             declarator_p)
3269        != NULL_TREE);
3270   /* If there is a nested-name-specifier, then we are looking at
3271      the first qualified-id production.  */
3272   if (nested_name_specifier_p)
3273     {
3274       tree saved_scope;
3275       tree saved_object_scope;
3276       tree saved_qualifying_scope;
3277       tree unqualified_id;
3278       bool is_template;
3279
3280       /* See if the next token is the `template' keyword.  */
3281       if (!template_p)
3282         template_p = &is_template;
3283       *template_p = cp_parser_optional_template_keyword (parser);
3284       /* Name lookup we do during the processing of the
3285          unqualified-id might obliterate SCOPE.  */
3286       saved_scope = parser->scope;
3287       saved_object_scope = parser->object_scope;
3288       saved_qualifying_scope = parser->qualifying_scope;
3289       /* Process the final unqualified-id.  */
3290       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3291                                                  check_dependency_p,
3292                                                  declarator_p);
3293       /* Restore the SAVED_SCOPE for our caller.  */
3294       parser->scope = saved_scope;
3295       parser->object_scope = saved_object_scope;
3296       parser->qualifying_scope = saved_qualifying_scope;
3297
3298       return unqualified_id;
3299     }
3300   /* Otherwise, if we are in global scope, then we are looking at one
3301      of the other qualified-id productions.  */
3302   else if (global_scope_p)
3303     {
3304       cp_token *token;
3305       tree id;
3306
3307       /* Peek at the next token.  */
3308       token = cp_lexer_peek_token (parser->lexer);
3309
3310       /* If it's an identifier, and the next token is not a "<", then
3311          we can avoid the template-id case.  This is an optimization
3312          for this common case.  */
3313       if (token->type == CPP_NAME
3314           && !cp_parser_nth_token_starts_template_argument_list_p
3315                (parser, 2))
3316         return cp_parser_identifier (parser);
3317
3318       cp_parser_parse_tentatively (parser);
3319       /* Try a template-id.  */
3320       id = cp_parser_template_id (parser,
3321                                   /*template_keyword_p=*/false,
3322                                   /*check_dependency_p=*/true,
3323                                   declarator_p);
3324       /* If that worked, we're done.  */
3325       if (cp_parser_parse_definitely (parser))
3326         return id;
3327
3328       /* Peek at the next token.  (Changes in the token buffer may
3329          have invalidated the pointer obtained above.)  */
3330       token = cp_lexer_peek_token (parser->lexer);
3331
3332       switch (token->type)
3333         {
3334         case CPP_NAME:
3335           return cp_parser_identifier (parser);
3336
3337         case CPP_KEYWORD:
3338           if (token->keyword == RID_OPERATOR)
3339             return cp_parser_operator_function_id (parser);
3340           /* Fall through.  */
3341
3342         default:
3343           cp_parser_error (parser, "expected id-expression");
3344           return error_mark_node;
3345         }
3346     }
3347   else
3348     return cp_parser_unqualified_id (parser, template_keyword_p,
3349                                      /*check_dependency_p=*/true,
3350                                      declarator_p);
3351 }
3352
3353 /* Parse an unqualified-id.
3354
3355    unqualified-id:
3356      identifier
3357      operator-function-id
3358      conversion-function-id
3359      ~ class-name
3360      template-id
3361
3362    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3363    keyword, in a construct like `A::template ...'.
3364
3365    Returns a representation of unqualified-id.  For the `identifier'
3366    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3367    production a BIT_NOT_EXPR is returned; the operand of the
3368    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3369    other productions, see the documentation accompanying the
3370    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3371    names are looked up in uninstantiated templates.  If DECLARATOR_P
3372    is true, the unqualified-id is appearing as part of a declarator,
3373    rather than as part of an expression.  */
3374
3375 static tree
3376 cp_parser_unqualified_id (cp_parser* parser,
3377                           bool template_keyword_p,
3378                           bool check_dependency_p,
3379                           bool declarator_p)
3380 {
3381   cp_token *token;
3382
3383   /* Peek at the next token.  */
3384   token = cp_lexer_peek_token (parser->lexer);
3385
3386   switch (token->type)
3387     {
3388     case CPP_NAME:
3389       {
3390         tree id;
3391
3392         /* We don't know yet whether or not this will be a
3393            template-id.  */
3394         cp_parser_parse_tentatively (parser);
3395         /* Try a template-id.  */
3396         id = cp_parser_template_id (parser, template_keyword_p,
3397                                     check_dependency_p,
3398                                     declarator_p);
3399         /* If it worked, we're done.  */
3400         if (cp_parser_parse_definitely (parser))
3401           return id;
3402         /* Otherwise, it's an ordinary identifier.  */
3403         return cp_parser_identifier (parser);
3404       }
3405
3406     case CPP_TEMPLATE_ID:
3407       return cp_parser_template_id (parser, template_keyword_p,
3408                                     check_dependency_p,
3409                                     declarator_p);
3410
3411     case CPP_COMPL:
3412       {
3413         tree type_decl;
3414         tree qualifying_scope;
3415         tree object_scope;
3416         tree scope;
3417         bool done;
3418
3419         /* Consume the `~' token.  */
3420         cp_lexer_consume_token (parser->lexer);
3421         /* Parse the class-name.  The standard, as written, seems to
3422            say that:
3423
3424              template <typename T> struct S { ~S (); };
3425              template <typename T> S<T>::~S() {}
3426
3427            is invalid, since `~' must be followed by a class-name, but
3428            `S<T>' is dependent, and so not known to be a class.
3429            That's not right; we need to look in uninstantiated
3430            templates.  A further complication arises from:
3431
3432              template <typename T> void f(T t) {
3433                t.T::~T();
3434              }
3435
3436            Here, it is not possible to look up `T' in the scope of `T'
3437            itself.  We must look in both the current scope, and the
3438            scope of the containing complete expression.
3439
3440            Yet another issue is:
3441
3442              struct S {
3443                int S;
3444                ~S();
3445              };
3446
3447              S::~S() {}
3448
3449            The standard does not seem to say that the `S' in `~S'
3450            should refer to the type `S' and not the data member
3451            `S::S'.  */
3452
3453         /* DR 244 says that we look up the name after the "~" in the
3454            same scope as we looked up the qualifying name.  That idea
3455            isn't fully worked out; it's more complicated than that.  */
3456         scope = parser->scope;
3457         object_scope = parser->object_scope;
3458         qualifying_scope = parser->qualifying_scope;
3459
3460         /* Check for invalid scopes.  */
3461         if (scope == error_mark_node)
3462           {
3463             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3464               cp_lexer_consume_token (parser->lexer);
3465             return error_mark_node;
3466           }
3467         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3468           {
3469             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3470               error ("scope %qT before %<~%> is not a class-name", scope);
3471             cp_parser_simulate_error (parser);
3472             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3473               cp_lexer_consume_token (parser->lexer);
3474             return error_mark_node;
3475           }
3476         gcc_assert (!scope || TYPE_P (scope));
3477
3478         /* If the name is of the form "X::~X" it's OK.  */
3479         token = cp_lexer_peek_token (parser->lexer);
3480         if (scope
3481             && token->type == CPP_NAME
3482             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3483                 == CPP_OPEN_PAREN)
3484             && constructor_name_p (token->value, scope))
3485           {
3486             cp_lexer_consume_token (parser->lexer);
3487             return build_nt (BIT_NOT_EXPR, scope);
3488           }
3489
3490         /* If there was an explicit qualification (S::~T), first look
3491            in the scope given by the qualification (i.e., S).  */
3492         done = false;
3493         type_decl = NULL_TREE;
3494         if (scope)
3495           {
3496             cp_parser_parse_tentatively (parser);
3497             type_decl = cp_parser_class_name (parser,
3498                                               /*typename_keyword_p=*/false,
3499                                               /*template_keyword_p=*/false,
3500                                               none_type,
3501                                               /*check_dependency=*/false,
3502                                               /*class_head_p=*/false,
3503                                               declarator_p);
3504             if (cp_parser_parse_definitely (parser))
3505               done = true;
3506           }
3507         /* In "N::S::~S", look in "N" as well.  */
3508         if (!done && scope && qualifying_scope)
3509           {
3510             cp_parser_parse_tentatively (parser);
3511             parser->scope = qualifying_scope;
3512             parser->object_scope = NULL_TREE;
3513             parser->qualifying_scope = NULL_TREE;
3514             type_decl
3515               = cp_parser_class_name (parser,
3516                                       /*typename_keyword_p=*/false,
3517                                       /*template_keyword_p=*/false,
3518                                       none_type,
3519                                       /*check_dependency=*/false,
3520                                       /*class_head_p=*/false,
3521                                       declarator_p);
3522             if (cp_parser_parse_definitely (parser))
3523               done = true;
3524           }
3525         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3526         else if (!done && object_scope)
3527           {
3528             cp_parser_parse_tentatively (parser);
3529             parser->scope = object_scope;
3530             parser->object_scope = NULL_TREE;
3531             parser->qualifying_scope = NULL_TREE;
3532             type_decl
3533               = cp_parser_class_name (parser,
3534                                       /*typename_keyword_p=*/false,
3535                                       /*template_keyword_p=*/false,
3536                                       none_type,
3537                                       /*check_dependency=*/false,
3538                                       /*class_head_p=*/false,
3539                                       declarator_p);
3540             if (cp_parser_parse_definitely (parser))
3541               done = true;
3542           }
3543         /* Look in the surrounding context.  */
3544         if (!done)
3545           {
3546             parser->scope = NULL_TREE;
3547             parser->object_scope = NULL_TREE;
3548             parser->qualifying_scope = NULL_TREE;
3549             type_decl
3550               = cp_parser_class_name (parser,
3551                                       /*typename_keyword_p=*/false,
3552                                       /*template_keyword_p=*/false,
3553                                       none_type,
3554                                       /*check_dependency=*/false,
3555                                       /*class_head_p=*/false,
3556                                       declarator_p);
3557           }
3558         /* If an error occurred, assume that the name of the
3559            destructor is the same as the name of the qualifying
3560            class.  That allows us to keep parsing after running
3561            into ill-formed destructor names.  */
3562         if (type_decl == error_mark_node && scope)
3563           return build_nt (BIT_NOT_EXPR, scope);
3564         else if (type_decl == error_mark_node)
3565           return error_mark_node;
3566
3567         /* Check that destructor name and scope match.  */
3568         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3569           {
3570             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3571               error ("declaration of %<~%T%> as member of %qT",
3572                      type_decl, scope);
3573             cp_parser_simulate_error (parser);
3574             return error_mark_node;
3575           }
3576
3577         /* [class.dtor]
3578
3579            A typedef-name that names a class shall not be used as the
3580            identifier in the declarator for a destructor declaration.  */
3581         if (declarator_p
3582             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3583             && !DECL_SELF_REFERENCE_P (type_decl)
3584             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3585           error ("typedef-name %qD used as destructor declarator",
3586                  type_decl);
3587
3588         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3589       }
3590
3591     case CPP_KEYWORD:
3592       if (token->keyword == RID_OPERATOR)
3593         {
3594           tree id;
3595
3596           /* This could be a template-id, so we try that first.  */
3597           cp_parser_parse_tentatively (parser);
3598           /* Try a template-id.  */
3599           id = cp_parser_template_id (parser, template_keyword_p,
3600                                       /*check_dependency_p=*/true,
3601                                       declarator_p);
3602           /* If that worked, we're done.  */
3603           if (cp_parser_parse_definitely (parser))
3604             return id;
3605           /* We still don't know whether we're looking at an
3606              operator-function-id or a conversion-function-id.  */
3607           cp_parser_parse_tentatively (parser);
3608           /* Try an operator-function-id.  */
3609           id = cp_parser_operator_function_id (parser);
3610           /* If that didn't work, try a conversion-function-id.  */
3611           if (!cp_parser_parse_definitely (parser))
3612             id = cp_parser_conversion_function_id (parser);
3613
3614           return id;
3615         }
3616       /* Fall through.  */
3617
3618     default:
3619       cp_parser_error (parser, "expected unqualified-id");
3620       return error_mark_node;
3621     }
3622 }
3623
3624 /* Parse an (optional) nested-name-specifier.
3625
3626    nested-name-specifier:
3627      class-or-namespace-name :: nested-name-specifier [opt]
3628      class-or-namespace-name :: template nested-name-specifier [opt]
3629
3630    PARSER->SCOPE should be set appropriately before this function is
3631    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3632    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3633    in name lookups.
3634
3635    Sets PARSER->SCOPE to the class (TYPE) or namespace
3636    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3637    it unchanged if there is no nested-name-specifier.  Returns the new
3638    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3639
3640    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3641    part of a declaration and/or decl-specifier.  */
3642
3643 static tree
3644 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3645                                      bool typename_keyword_p,
3646                                      bool check_dependency_p,
3647                                      bool type_p,
3648                                      bool is_declaration)
3649 {
3650   bool success = false;
3651   cp_token_position start = 0;
3652   cp_token *token;
3653
3654   /* Remember where the nested-name-specifier starts.  */
3655   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3656     {
3657       start = cp_lexer_token_position (parser->lexer, false);
3658       push_deferring_access_checks (dk_deferred);
3659     }
3660
3661   while (true)
3662     {
3663       tree new_scope;
3664       tree old_scope;
3665       tree saved_qualifying_scope;
3666       bool template_keyword_p;
3667
3668       /* Spot cases that cannot be the beginning of a
3669          nested-name-specifier.  */
3670       token = cp_lexer_peek_token (parser->lexer);
3671
3672       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3673          the already parsed nested-name-specifier.  */
3674       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3675         {
3676           /* Grab the nested-name-specifier and continue the loop.  */
3677           cp_parser_pre_parsed_nested_name_specifier (parser);
3678           /* If we originally encountered this nested-name-specifier
3679              with IS_DECLARATION set to false, we will not have
3680              resolved TYPENAME_TYPEs, so we must do so here.  */
3681           if (is_declaration
3682               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3683             {
3684               new_scope = resolve_typename_type (parser->scope,
3685                                                  /*only_current_p=*/false);
3686               if (new_scope != error_mark_node)
3687                 parser->scope = new_scope;
3688             }
3689           success = true;
3690           continue;
3691         }
3692
3693       /* Spot cases that cannot be the beginning of a
3694          nested-name-specifier.  On the second and subsequent times
3695          through the loop, we look for the `template' keyword.  */
3696       if (success && token->keyword == RID_TEMPLATE)
3697         ;
3698       /* A template-id can start a nested-name-specifier.  */
3699       else if (token->type == CPP_TEMPLATE_ID)
3700         ;
3701       else
3702         {
3703           /* If the next token is not an identifier, then it is
3704              definitely not a class-or-namespace-name.  */
3705           if (token->type != CPP_NAME)
3706             break;
3707           /* If the following token is neither a `<' (to begin a
3708              template-id), nor a `::', then we are not looking at a
3709              nested-name-specifier.  */
3710           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3711           if (token->type != CPP_SCOPE
3712               && !cp_parser_nth_token_starts_template_argument_list_p
3713                   (parser, 2))
3714             break;
3715         }
3716
3717       /* The nested-name-specifier is optional, so we parse
3718          tentatively.  */
3719       cp_parser_parse_tentatively (parser);
3720
3721       /* Look for the optional `template' keyword, if this isn't the
3722          first time through the loop.  */
3723       if (success)
3724         template_keyword_p = cp_parser_optional_template_keyword (parser);
3725       else
3726         template_keyword_p = false;
3727
3728       /* Save the old scope since the name lookup we are about to do
3729          might destroy it.  */
3730       old_scope = parser->scope;
3731       saved_qualifying_scope = parser->qualifying_scope;
3732       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3733          look up names in "X<T>::I" in order to determine that "Y" is
3734          a template.  So, if we have a typename at this point, we make
3735          an effort to look through it.  */
3736       if (is_declaration
3737           && !typename_keyword_p
3738           && parser->scope
3739           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3740         parser->scope = resolve_typename_type (parser->scope,
3741                                                /*only_current_p=*/false);
3742       /* Parse the qualifying entity.  */
3743       new_scope
3744         = cp_parser_class_or_namespace_name (parser,
3745                                              typename_keyword_p,
3746                                              template_keyword_p,
3747                                              check_dependency_p,
3748                                              type_p,
3749                                              is_declaration);
3750       /* Look for the `::' token.  */
3751       cp_parser_require (parser, CPP_SCOPE, "`::'");
3752
3753       /* If we found what we wanted, we keep going; otherwise, we're
3754          done.  */
3755       if (!cp_parser_parse_definitely (parser))
3756         {
3757           bool error_p = false;
3758
3759           /* Restore the OLD_SCOPE since it was valid before the
3760              failed attempt at finding the last
3761              class-or-namespace-name.  */
3762           parser->scope = old_scope;
3763           parser->qualifying_scope = saved_qualifying_scope;
3764           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3765             break;
3766           /* If the next token is an identifier, and the one after
3767              that is a `::', then any valid interpretation would have
3768              found a class-or-namespace-name.  */
3769           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3770                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3771                      == CPP_SCOPE)
3772                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3773                      != CPP_COMPL))
3774             {
3775               token = cp_lexer_consume_token (parser->lexer);
3776               if (!error_p)
3777                 {
3778                   if (!token->ambiguous_p)
3779                     {
3780                       tree decl;
3781                       tree ambiguous_decls;
3782
3783                       decl = cp_parser_lookup_name (parser, token->value,
3784                                                     none_type,
3785                                                     /*is_template=*/false,
3786                                                     /*is_namespace=*/false,
3787                                                     /*check_dependency=*/true,
3788                                                     &ambiguous_decls);
3789                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3790                         error ("%qD used without template parameters", decl);
3791                       else if (ambiguous_decls)
3792                         {
3793                           error ("reference to %qD is ambiguous", 
3794                                  token->value);
3795                           print_candidates (ambiguous_decls);
3796                           decl = error_mark_node;
3797                         }
3798                       else
3799                         cp_parser_name_lookup_error
3800                           (parser, token->value, decl,
3801                            "is not a class or namespace");
3802                     }
3803                   parser->scope = error_mark_node;
3804                   error_p = true;
3805                   /* Treat this as a successful nested-name-specifier
3806                      due to:
3807
3808                      [basic.lookup.qual]
3809
3810                      If the name found is not a class-name (clause
3811                      _class_) or namespace-name (_namespace.def_), the
3812                      program is ill-formed.  */
3813                   success = true;
3814                 }
3815               cp_lexer_consume_token (parser->lexer);
3816             }
3817           break;
3818         }
3819       /* We've found one valid nested-name-specifier.  */
3820       success = true;
3821       /* Name lookup always gives us a DECL.  */
3822       if (TREE_CODE (new_scope) == TYPE_DECL)
3823         new_scope = TREE_TYPE (new_scope);
3824       /* Uses of "template" must be followed by actual templates.  */
3825       if (template_keyword_p
3826           && !(CLASS_TYPE_P (new_scope)
3827                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3828                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3829                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3830           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3831                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3832                    == TEMPLATE_ID_EXPR)))
3833         pedwarn (TYPE_P (new_scope)
3834                  ? "%qT is not a template"
3835                  : "%qD is not a template",
3836                  new_scope);
3837       /* If it is a class scope, try to complete it; we are about to
3838          be looking up names inside the class.  */
3839       if (TYPE_P (new_scope)
3840           /* Since checking types for dependency can be expensive,
3841              avoid doing it if the type is already complete.  */
3842           && !COMPLETE_TYPE_P (new_scope)
3843           /* Do not try to complete dependent types.  */
3844           && !dependent_type_p (new_scope))
3845         new_scope = complete_type (new_scope);
3846       /* Make sure we look in the right scope the next time through
3847          the loop.  */
3848       parser->scope = new_scope;
3849     }
3850
3851   /* If parsing tentatively, replace the sequence of tokens that makes
3852      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3853      token.  That way, should we re-parse the token stream, we will
3854      not have to repeat the effort required to do the parse, nor will
3855      we issue duplicate error messages.  */
3856   if (success && start)
3857     {
3858       cp_token *token;
3859       tree access_checks;
3860
3861       token = cp_lexer_token_at (parser->lexer, start);
3862       /* Reset the contents of the START token.  */
3863       token->type = CPP_NESTED_NAME_SPECIFIER;
3864       /* Retrieve any deferred checks.  Do not pop this access checks yet
3865          so the memory will not be reclaimed during token replacing below.  */
3866       access_checks = get_deferred_access_checks ();
3867       token->value = build_tree_list (copy_list (access_checks),
3868                                       parser->scope);
3869       TREE_TYPE (token->value) = parser->qualifying_scope;
3870       token->keyword = RID_MAX;
3871
3872       /* Purge all subsequent tokens.  */
3873       cp_lexer_purge_tokens_after (parser->lexer, start);
3874     }
3875   
3876   if (start)
3877     pop_to_parent_deferring_access_checks ();
3878
3879   return success ? parser->scope : NULL_TREE;
3880 }
3881
3882 /* Parse a nested-name-specifier.  See
3883    cp_parser_nested_name_specifier_opt for details.  This function
3884    behaves identically, except that it will an issue an error if no
3885    nested-name-specifier is present.  */
3886
3887 static tree
3888 cp_parser_nested_name_specifier (cp_parser *parser,
3889                                  bool typename_keyword_p,
3890                                  bool check_dependency_p,
3891                                  bool type_p,
3892                                  bool is_declaration)
3893 {
3894   tree scope;
3895
3896   /* Look for the nested-name-specifier.  */
3897   scope = cp_parser_nested_name_specifier_opt (parser,
3898                                                typename_keyword_p,
3899                                                check_dependency_p,
3900                                                type_p,
3901                                                is_declaration);
3902   /* If it was not present, issue an error message.  */
3903   if (!scope)
3904     {
3905       cp_parser_error (parser, "expected nested-name-specifier");
3906       parser->scope = NULL_TREE;
3907     }
3908
3909   return scope;
3910 }
3911
3912 /* Parse a class-or-namespace-name.
3913
3914    class-or-namespace-name:
3915      class-name
3916      namespace-name
3917
3918    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3919    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3920    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3921    TYPE_P is TRUE iff the next name should be taken as a class-name,
3922    even the same name is declared to be another entity in the same
3923    scope.
3924
3925    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3926    specified by the class-or-namespace-name.  If neither is found the
3927    ERROR_MARK_NODE is returned.  */
3928
3929 static tree
3930 cp_parser_class_or_namespace_name (cp_parser *parser,
3931                                    bool typename_keyword_p,
3932                                    bool template_keyword_p,
3933                                    bool check_dependency_p,
3934                                    bool type_p,
3935                                    bool is_declaration)
3936 {
3937   tree saved_scope;
3938   tree saved_qualifying_scope;
3939   tree saved_object_scope;
3940   tree scope;
3941   bool only_class_p;
3942
3943   /* Before we try to parse the class-name, we must save away the
3944      current PARSER->SCOPE since cp_parser_class_name will destroy
3945      it.  */
3946   saved_scope = parser->scope;
3947   saved_qualifying_scope = parser->qualifying_scope;
3948   saved_object_scope = parser->object_scope;
3949   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3950      there is no need to look for a namespace-name.  */
3951   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3952   if (!only_class_p)
3953     cp_parser_parse_tentatively (parser);
3954   scope = cp_parser_class_name (parser,
3955                                 typename_keyword_p,
3956                                 template_keyword_p,
3957                                 type_p ? class_type : none_type,
3958                                 check_dependency_p,
3959                                 /*class_head_p=*/false,
3960                                 is_declaration);
3961   /* If that didn't work, try for a namespace-name.  */
3962   if (!only_class_p && !cp_parser_parse_definitely (parser))
3963     {
3964       /* Restore the saved scope.  */
3965       parser->scope = saved_scope;
3966       parser->qualifying_scope = saved_qualifying_scope;
3967       parser->object_scope = saved_object_scope;
3968       /* If we are not looking at an identifier followed by the scope
3969          resolution operator, then this is not part of a
3970          nested-name-specifier.  (Note that this function is only used
3971          to parse the components of a nested-name-specifier.)  */
3972       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3973           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3974         return error_mark_node;
3975       scope = cp_parser_namespace_name (parser);
3976     }
3977
3978   return scope;
3979 }
3980
3981 /* Parse a postfix-expression.
3982
3983    postfix-expression:
3984      primary-expression
3985      postfix-expression [ expression ]
3986      postfix-expression ( expression-list [opt] )
3987      simple-type-specifier ( expression-list [opt] )
3988      typename :: [opt] nested-name-specifier identifier
3989        ( expression-list [opt] )
3990      typename :: [opt] nested-name-specifier template [opt] template-id
3991        ( expression-list [opt] )
3992      postfix-expression . template [opt] id-expression
3993      postfix-expression -> template [opt] id-expression
3994      postfix-expression . pseudo-destructor-name
3995      postfix-expression -> pseudo-destructor-name
3996      postfix-expression ++
3997      postfix-expression --
3998      dynamic_cast < type-id > ( expression )
3999      static_cast < type-id > ( expression )
4000      reinterpret_cast < type-id > ( expression )
4001      const_cast < type-id > ( expression )
4002      typeid ( expression )
4003      typeid ( type-id )
4004
4005    GNU Extension:
4006
4007    postfix-expression:
4008      ( type-id ) { initializer-list , [opt] }
4009
4010    This extension is a GNU version of the C99 compound-literal
4011    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4012    but they are essentially the same concept.)
4013
4014    If ADDRESS_P is true, the postfix expression is the operand of the
4015    `&' operator.  CAST_P is true if this expression is the target of a
4016    cast.
4017
4018    Returns a representation of the expression.  */
4019
4020 static tree
4021 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4022 {
4023   cp_token *token;
4024   enum rid keyword;
4025   cp_id_kind idk = CP_ID_KIND_NONE;
4026   tree postfix_expression = NULL_TREE;
4027
4028   /* Peek at the next token.  */
4029   token = cp_lexer_peek_token (parser->lexer);
4030   /* Some of the productions are determined by keywords.  */
4031   keyword = token->keyword;
4032   switch (keyword)
4033     {
4034     case RID_DYNCAST:
4035     case RID_STATCAST:
4036     case RID_REINTCAST:
4037     case RID_CONSTCAST:
4038       {
4039         tree type;
4040         tree expression;
4041         const char *saved_message;
4042
4043         /* All of these can be handled in the same way from the point
4044            of view of parsing.  Begin by consuming the token
4045            identifying the cast.  */
4046         cp_lexer_consume_token (parser->lexer);
4047
4048         /* New types cannot be defined in the cast.  */
4049         saved_message = parser->type_definition_forbidden_message;
4050         parser->type_definition_forbidden_message
4051           = "types may not be defined in casts";
4052
4053         /* Look for the opening `<'.  */
4054         cp_parser_require (parser, CPP_LESS, "`<'");
4055         /* Parse the type to which we are casting.  */
4056         type = cp_parser_type_id (parser);
4057         /* Look for the closing `>'.  */
4058         cp_parser_require (parser, CPP_GREATER, "`>'");
4059         /* Restore the old message.  */
4060         parser->type_definition_forbidden_message = saved_message;
4061
4062         /* And the expression which is being cast.  */
4063         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4064         expression = cp_parser_expression (parser, /*cast_p=*/true);
4065         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4066
4067         /* Only type conversions to integral or enumeration types
4068            can be used in constant-expressions.  */
4069         if (parser->integral_constant_expression_p
4070             && !dependent_type_p (type)
4071             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4072             && (cp_parser_non_integral_constant_expression
4073                 (parser,
4074                  "a cast to a type other than an integral or "
4075                  "enumeration type")))
4076           return error_mark_node;
4077
4078         switch (keyword)
4079           {
4080           case RID_DYNCAST:
4081             postfix_expression
4082               = build_dynamic_cast (type, expression);
4083             break;
4084           case RID_STATCAST:
4085             postfix_expression
4086               = build_static_cast (type, expression);
4087             break;
4088           case RID_REINTCAST:
4089             postfix_expression
4090               = build_reinterpret_cast (type, expression);
4091             break;
4092           case RID_CONSTCAST:
4093             postfix_expression
4094               = build_const_cast (type, expression);
4095             break;
4096           default:
4097             gcc_unreachable ();
4098           }
4099       }
4100       break;
4101
4102     case RID_TYPEID:
4103       {
4104         tree type;
4105         const char *saved_message;
4106         bool saved_in_type_id_in_expr_p;
4107
4108         /* Consume the `typeid' token.  */
4109         cp_lexer_consume_token (parser->lexer);
4110         /* Look for the `(' token.  */
4111         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4112         /* Types cannot be defined in a `typeid' expression.  */
4113         saved_message = parser->type_definition_forbidden_message;
4114         parser->type_definition_forbidden_message
4115           = "types may not be defined in a `typeid\' expression";
4116         /* We can't be sure yet whether we're looking at a type-id or an
4117            expression.  */
4118         cp_parser_parse_tentatively (parser);
4119         /* Try a type-id first.  */
4120         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4121         parser->in_type_id_in_expr_p = true;
4122         type = cp_parser_type_id (parser);
4123         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4124         /* Look for the `)' token.  Otherwise, we can't be sure that
4125            we're not looking at an expression: consider `typeid (int
4126            (3))', for example.  */
4127         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4128         /* If all went well, simply lookup the type-id.  */
4129         if (cp_parser_parse_definitely (parser))
4130           postfix_expression = get_typeid (type);
4131         /* Otherwise, fall back to the expression variant.  */
4132         else
4133           {
4134             tree expression;
4135
4136             /* Look for an expression.  */
4137             expression = cp_parser_expression (parser, /*cast_p=*/false);
4138             /* Compute its typeid.  */
4139             postfix_expression = build_typeid (expression);
4140             /* Look for the `)' token.  */
4141             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4142           }
4143         /* `typeid' may not appear in an integral constant expression.  */
4144         if (cp_parser_non_integral_constant_expression(parser,
4145                                                        "`typeid' operator"))
4146           return error_mark_node;
4147         /* Restore the saved message.  */
4148         parser->type_definition_forbidden_message = saved_message;
4149       }
4150       break;
4151
4152     case RID_TYPENAME:
4153       {
4154         tree type;
4155         /* The syntax permitted here is the same permitted for an
4156            elaborated-type-specifier.  */
4157         type = cp_parser_elaborated_type_specifier (parser,
4158                                                     /*is_friend=*/false,
4159                                                     /*is_declaration=*/false);
4160         postfix_expression = cp_parser_functional_cast (parser, type);
4161       }
4162       break;
4163
4164     default:
4165       {
4166         tree type;
4167
4168         /* If the next thing is a simple-type-specifier, we may be
4169            looking at a functional cast.  We could also be looking at
4170            an id-expression.  So, we try the functional cast, and if
4171            that doesn't work we fall back to the primary-expression.  */
4172         cp_parser_parse_tentatively (parser);
4173         /* Look for the simple-type-specifier.  */
4174         type = cp_parser_simple_type_specifier (parser,
4175                                                 /*decl_specs=*/NULL,
4176                                                 CP_PARSER_FLAGS_NONE);
4177         /* Parse the cast itself.  */
4178         if (!cp_parser_error_occurred (parser))
4179           postfix_expression
4180             = cp_parser_functional_cast (parser, type);
4181         /* If that worked, we're done.  */
4182         if (cp_parser_parse_definitely (parser))
4183           break;
4184
4185         /* If the functional-cast didn't work out, try a
4186            compound-literal.  */
4187         if (cp_parser_allow_gnu_extensions_p (parser)
4188             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4189           {
4190             VEC(constructor_elt,gc) *initializer_list = NULL;
4191             bool saved_in_type_id_in_expr_p;
4192
4193             cp_parser_parse_tentatively (parser);
4194             /* Consume the `('.  */
4195             cp_lexer_consume_token (parser->lexer);
4196             /* Parse the type.  */
4197             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4198             parser->in_type_id_in_expr_p = true;
4199             type = cp_parser_type_id (parser);
4200             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4201             /* Look for the `)'.  */
4202             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4203             /* Look for the `{'.  */
4204             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4205             /* If things aren't going well, there's no need to
4206                keep going.  */
4207             if (!cp_parser_error_occurred (parser))
4208               {
4209                 bool non_constant_p;
4210                 /* Parse the initializer-list.  */
4211                 initializer_list
4212                   = cp_parser_initializer_list (parser, &non_constant_p);
4213                 /* Allow a trailing `,'.  */
4214                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4215                   cp_lexer_consume_token (parser->lexer);
4216                 /* Look for the final `}'.  */
4217                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4218               }
4219             /* If that worked, we're definitely looking at a
4220                compound-literal expression.  */
4221             if (cp_parser_parse_definitely (parser))
4222               {
4223                 /* Warn the user that a compound literal is not
4224                    allowed in standard C++.  */
4225                 if (pedantic)
4226                   pedwarn ("ISO C++ forbids compound-literals");
4227                 /* Form the representation of the compound-literal.  */
4228                 postfix_expression
4229                   = finish_compound_literal (type, initializer_list);
4230                 break;
4231               }
4232           }
4233
4234         /* It must be a primary-expression.  */
4235         postfix_expression 
4236           = cp_parser_primary_expression (parser, address_p, cast_p, 
4237                                           /*template_arg_p=*/false,
4238                                           &idk);
4239       }
4240       break;
4241     }
4242
4243   /* Keep looping until the postfix-expression is complete.  */
4244   while (true)
4245     {
4246       if (idk == CP_ID_KIND_UNQUALIFIED
4247           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4248           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4249         /* It is not a Koenig lookup function call.  */
4250         postfix_expression
4251           = unqualified_name_lookup_error (postfix_expression);
4252
4253       /* Peek at the next token.  */
4254       token = cp_lexer_peek_token (parser->lexer);
4255
4256       switch (token->type)
4257         {
4258         case CPP_OPEN_SQUARE:
4259           postfix_expression
4260             = cp_parser_postfix_open_square_expression (parser,
4261                                                         postfix_expression,
4262                                                         false);
4263           idk = CP_ID_KIND_NONE;
4264           break;
4265
4266         case CPP_OPEN_PAREN:
4267           /* postfix-expression ( expression-list [opt] ) */
4268           {
4269             bool koenig_p;
4270             bool is_builtin_constant_p;
4271             bool saved_integral_constant_expression_p = false;
4272             bool saved_non_integral_constant_expression_p = false;
4273             tree args;
4274
4275             is_builtin_constant_p
4276               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4277             if (is_builtin_constant_p)
4278               {
4279                 /* The whole point of __builtin_constant_p is to allow
4280                    non-constant expressions to appear as arguments.  */
4281                 saved_integral_constant_expression_p
4282                   = parser->integral_constant_expression_p;
4283                 saved_non_integral_constant_expression_p
4284                   = parser->non_integral_constant_expression_p;
4285                 parser->integral_constant_expression_p = false;
4286               }
4287             args = (cp_parser_parenthesized_expression_list
4288                     (parser, /*is_attribute_list=*/false,
4289                      /*cast_p=*/false,
4290                      /*non_constant_p=*/NULL));
4291             if (is_builtin_constant_p)
4292               {
4293                 parser->integral_constant_expression_p
4294                   = saved_integral_constant_expression_p;
4295                 parser->non_integral_constant_expression_p
4296                   = saved_non_integral_constant_expression_p;
4297               }
4298
4299             if (args == error_mark_node)
4300               {
4301                 postfix_expression = error_mark_node;
4302                 break;
4303               }
4304
4305             /* Function calls are not permitted in
4306                constant-expressions.  */
4307             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4308                 && cp_parser_non_integral_constant_expression (parser,
4309                                                                "a function call"))
4310               {
4311                 postfix_expression = error_mark_node;
4312                 break;
4313               }
4314
4315             koenig_p = false;
4316             if (idk == CP_ID_KIND_UNQUALIFIED)
4317               {
4318                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4319                   {
4320                     if (args)
4321                       {
4322                         koenig_p = true;
4323                         postfix_expression
4324                           = perform_koenig_lookup (postfix_expression, args);
4325                       }
4326                     else
4327                       postfix_expression
4328                         = unqualified_fn_lookup_error (postfix_expression);
4329                   }
4330                 /* We do not perform argument-dependent lookup if
4331                    normal lookup finds a non-function, in accordance
4332                    with the expected resolution of DR 218.  */
4333                 else if (args && is_overloaded_fn (postfix_expression))
4334                   {
4335                     tree fn = get_first_fn (postfix_expression);
4336
4337                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4338                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4339
4340                     /* Only do argument dependent lookup if regular
4341                        lookup does not find a set of member functions.
4342                        [basic.lookup.koenig]/2a  */
4343                     if (!DECL_FUNCTION_MEMBER_P (fn))
4344                       {
4345                         koenig_p = true;
4346                         postfix_expression
4347                           = perform_koenig_lookup (postfix_expression, args);
4348                       }
4349                   }
4350               }
4351
4352             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4353               {
4354                 tree instance = TREE_OPERAND (postfix_expression, 0);
4355                 tree fn = TREE_OPERAND (postfix_expression, 1);
4356
4357                 if (processing_template_decl
4358                     && (type_dependent_expression_p (instance)
4359                         || (!BASELINK_P (fn)
4360                             && TREE_CODE (fn) != FIELD_DECL)
4361                         || type_dependent_expression_p (fn)
4362                         || any_type_dependent_arguments_p (args)))
4363                   {
4364                     postfix_expression
4365                       = build_min_nt (CALL_EXPR, postfix_expression,
4366                                       args, NULL_TREE);
4367                     break;
4368                   }
4369
4370                 if (BASELINK_P (fn))
4371                   postfix_expression
4372                     = (build_new_method_call
4373                        (instance, fn, args, NULL_TREE,
4374                         (idk == CP_ID_KIND_QUALIFIED
4375                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4376                 else
4377                   postfix_expression
4378                     = finish_call_expr (postfix_expression, args,
4379                                         /*disallow_virtual=*/false,
4380                                         /*koenig_p=*/false);
4381               }
4382             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4383                      || TREE_CODE (postfix_expression) == MEMBER_REF
4384                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4385               postfix_expression = (build_offset_ref_call_from_tree
4386                                     (postfix_expression, args));
4387             else if (idk == CP_ID_KIND_QUALIFIED)
4388               /* A call to a static class member, or a namespace-scope
4389                  function.  */
4390               postfix_expression
4391                 = finish_call_expr (postfix_expression, args,
4392                                     /*disallow_virtual=*/true,
4393                                     koenig_p);
4394             else
4395               /* All other function calls.  */
4396               postfix_expression
4397                 = finish_call_expr (postfix_expression, args,
4398                                     /*disallow_virtual=*/false,
4399                                     koenig_p);
4400
4401             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4402             idk = CP_ID_KIND_NONE;
4403           }
4404           break;
4405
4406         case CPP_DOT:
4407         case CPP_DEREF:
4408           /* postfix-expression . template [opt] id-expression
4409              postfix-expression . pseudo-destructor-name
4410              postfix-expression -> template [opt] id-expression
4411              postfix-expression -> pseudo-destructor-name */
4412
4413           /* Consume the `.' or `->' operator.  */
4414           cp_lexer_consume_token (parser->lexer);
4415
4416           postfix_expression
4417             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4418                                                       postfix_expression,
4419                                                       false, &idk);
4420           break;
4421
4422         case CPP_PLUS_PLUS:
4423           /* postfix-expression ++  */
4424           /* Consume the `++' token.  */
4425           cp_lexer_consume_token (parser->lexer);
4426           /* Generate a representation for the complete expression.  */
4427           postfix_expression
4428             = finish_increment_expr (postfix_expression,
4429                                      POSTINCREMENT_EXPR);
4430           /* Increments may not appear in constant-expressions.  */
4431           if (cp_parser_non_integral_constant_expression (parser,
4432                                                           "an increment"))
4433             postfix_expression = error_mark_node;
4434           idk = CP_ID_KIND_NONE;
4435           break;
4436
4437         case CPP_MINUS_MINUS:
4438           /* postfix-expression -- */
4439           /* Consume the `--' token.  */
4440           cp_lexer_consume_token (parser->lexer);
4441           /* Generate a representation for the complete expression.  */
4442           postfix_expression
4443             = finish_increment_expr (postfix_expression,
4444                                      POSTDECREMENT_EXPR);
4445           /* Decrements may not appear in constant-expressions.  */
4446           if (cp_parser_non_integral_constant_expression (parser,
4447                                                           "a decrement"))
4448             postfix_expression = error_mark_node;
4449           idk = CP_ID_KIND_NONE;
4450           break;
4451
4452         default:
4453           return postfix_expression;
4454         }
4455     }
4456
4457   /* We should never get here.  */
4458   gcc_unreachable ();
4459   return error_mark_node;
4460 }
4461
4462 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4463    by cp_parser_builtin_offsetof.  We're looking for
4464
4465      postfix-expression [ expression ]
4466
4467    FOR_OFFSETOF is set if we're being called in that context, which
4468    changes how we deal with integer constant expressions.  */
4469
4470 static tree
4471 cp_parser_postfix_open_square_expression (cp_parser *parser,
4472                                           tree postfix_expression,
4473                                           bool for_offsetof)
4474 {
4475   tree index;
4476
4477   /* Consume the `[' token.  */
4478   cp_lexer_consume_token (parser->lexer);
4479
4480   /* Parse the index expression.  */
4481   /* ??? For offsetof, there is a question of what to allow here.  If
4482      offsetof is not being used in an integral constant expression context,
4483      then we *could* get the right answer by computing the value at runtime.
4484      If we are in an integral constant expression context, then we might
4485      could accept any constant expression; hard to say without analysis.
4486      Rather than open the barn door too wide right away, allow only integer
4487      constant expressions here.  */
4488   if (for_offsetof)
4489     index = cp_parser_constant_expression (parser, false, NULL);
4490   else
4491     index = cp_parser_expression (parser, /*cast_p=*/false);
4492
4493   /* Look for the closing `]'.  */
4494   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4495
4496   /* Build the ARRAY_REF.  */
4497   postfix_expression = grok_array_decl (postfix_expression, index);
4498
4499   /* When not doing offsetof, array references are not permitted in
4500      constant-expressions.  */
4501   if (!for_offsetof
4502       && (cp_parser_non_integral_constant_expression
4503           (parser, "an array reference")))
4504     postfix_expression = error_mark_node;
4505
4506   return postfix_expression;
4507 }
4508
4509 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4510    by cp_parser_builtin_offsetof.  We're looking for
4511
4512      postfix-expression . template [opt] id-expression
4513      postfix-expression . pseudo-destructor-name
4514      postfix-expression -> template [opt] id-expression
4515      postfix-expression -> pseudo-destructor-name
4516
4517    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4518    limits what of the above we'll actually accept, but nevermind.
4519    TOKEN_TYPE is the "." or "->" token, which will already have been
4520    removed from the stream.  */
4521
4522 static tree
4523 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4524                                         enum cpp_ttype token_type,
4525                                         tree postfix_expression,
4526                                         bool for_offsetof, cp_id_kind *idk)
4527 {
4528   tree name;
4529   bool dependent_p;
4530   bool pseudo_destructor_p;
4531   tree scope = NULL_TREE;
4532
4533   /* If this is a `->' operator, dereference the pointer.  */
4534   if (token_type == CPP_DEREF)
4535     postfix_expression = build_x_arrow (postfix_expression);
4536   /* Check to see whether or not the expression is type-dependent.  */
4537   dependent_p = type_dependent_expression_p (postfix_expression);
4538   /* The identifier following the `->' or `.' is not qualified.  */
4539   parser->scope = NULL_TREE;
4540   parser->qualifying_scope = NULL_TREE;
4541   parser->object_scope = NULL_TREE;
4542   *idk = CP_ID_KIND_NONE;
4543   /* Enter the scope corresponding to the type of the object
4544      given by the POSTFIX_EXPRESSION.  */
4545   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4546     {
4547       scope = TREE_TYPE (postfix_expression);
4548       /* According to the standard, no expression should ever have
4549          reference type.  Unfortunately, we do not currently match
4550          the standard in this respect in that our internal representation
4551          of an expression may have reference type even when the standard
4552          says it does not.  Therefore, we have to manually obtain the
4553          underlying type here.  */
4554       scope = non_reference (scope);
4555       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4556       if (scope == unknown_type_node)
4557         {
4558           error ("%qE does not have class type", postfix_expression);
4559           scope = NULL_TREE;
4560         }
4561       else
4562         scope = complete_type_or_else (scope, NULL_TREE);
4563       /* Let the name lookup machinery know that we are processing a
4564          class member access expression.  */
4565       parser->context->object_type = scope;
4566       /* If something went wrong, we want to be able to discern that case,
4567          as opposed to the case where there was no SCOPE due to the type
4568          of expression being dependent.  */
4569       if (!scope)
4570         scope = error_mark_node;
4571       /* If the SCOPE was erroneous, make the various semantic analysis
4572          functions exit quickly -- and without issuing additional error
4573          messages.  */
4574       if (scope == error_mark_node)
4575         postfix_expression = error_mark_node;
4576     }
4577
4578   /* Assume this expression is not a pseudo-destructor access.  */
4579   pseudo_destructor_p = false;
4580
4581   /* If the SCOPE is a scalar type, then, if this is a valid program,
4582      we must be looking at a pseudo-destructor-name.  */
4583   if (scope && SCALAR_TYPE_P (scope))
4584     {
4585       tree s;
4586       tree type;
4587
4588       cp_parser_parse_tentatively (parser);
4589       /* Parse the pseudo-destructor-name.  */
4590       s = NULL_TREE;
4591       cp_parser_pseudo_destructor_name (parser, &s, &type);
4592       if (cp_parser_parse_definitely (parser))
4593         {
4594           pseudo_destructor_p = true;
4595           postfix_expression
4596             = finish_pseudo_destructor_expr (postfix_expression,
4597                                              s, TREE_TYPE (type));
4598         }
4599     }
4600
4601   if (!pseudo_destructor_p)
4602     {
4603       /* If the SCOPE is not a scalar type, we are looking at an
4604          ordinary class member access expression, rather than a
4605          pseudo-destructor-name.  */
4606       bool template_p;
4607       /* Parse the id-expression.  */
4608       name = (cp_parser_id_expression 
4609               (parser, 
4610                cp_parser_optional_template_keyword (parser),
4611                /*check_dependency_p=*/true,
4612                &template_p,
4613                /*declarator_p=*/false));
4614       /* In general, build a SCOPE_REF if the member name is qualified.
4615          However, if the name was not dependent and has already been
4616          resolved; there is no need to build the SCOPE_REF.  For example;
4617
4618              struct X { void f(); };
4619              template <typename T> void f(T* t) { t->X::f(); }
4620
4621          Even though "t" is dependent, "X::f" is not and has been resolved
4622          to a BASELINK; there is no need to include scope information.  */
4623
4624       /* But we do need to remember that there was an explicit scope for
4625          virtual function calls.  */
4626       if (parser->scope)
4627         *idk = CP_ID_KIND_QUALIFIED;
4628
4629       /* If the name is a template-id that names a type, we will get a
4630          TYPE_DECL here.  That is invalid code.  */
4631       if (TREE_CODE (name) == TYPE_DECL)
4632         {
4633           error ("invalid use of %qD", name);
4634           postfix_expression = error_mark_node;
4635         }
4636       else
4637         {
4638           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4639             {
4640               name = build_qualified_name (/*type=*/NULL_TREE,
4641                                            parser->scope,
4642                                            name,
4643                                            template_p);
4644               parser->scope = NULL_TREE;
4645               parser->qualifying_scope = NULL_TREE;
4646               parser->object_scope = NULL_TREE;
4647             }
4648           if (scope && name && BASELINK_P (name))
4649             adjust_result_of_qualified_name_lookup
4650               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4651           postfix_expression
4652             = finish_class_member_access_expr (postfix_expression, name,
4653                                                template_p);
4654         }
4655     }
4656
4657   /* We no longer need to look up names in the scope of the object on
4658      the left-hand side of the `.' or `->' operator.  */
4659   parser->context->object_type = NULL_TREE;
4660
4661   /* Outside of offsetof, these operators may not appear in
4662      constant-expressions.  */
4663   if (!for_offsetof
4664       && (cp_parser_non_integral_constant_expression
4665           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4666     postfix_expression = error_mark_node;
4667
4668   return postfix_expression;
4669 }
4670
4671 /* Parse a parenthesized expression-list.
4672
4673    expression-list:
4674      assignment-expression
4675      expression-list, assignment-expression
4676
4677    attribute-list:
4678      expression-list
4679      identifier
4680      identifier, expression-list
4681
4682    CAST_P is true if this expression is the target of a cast.
4683
4684    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4685    representation of an assignment-expression.  Note that a TREE_LIST
4686    is returned even if there is only a single expression in the list.
4687    error_mark_node is returned if the ( and or ) are
4688    missing. NULL_TREE is returned on no expressions. The parentheses
4689    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4690    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4691    indicates whether or not all of the expressions in the list were
4692    constant.  */
4693
4694 static tree
4695 cp_parser_parenthesized_expression_list (cp_parser* parser,
4696                                          bool is_attribute_list,
4697                                          bool cast_p,
4698                                          bool *non_constant_p)
4699 {
4700   tree expression_list = NULL_TREE;
4701   bool fold_expr_p = is_attribute_list;
4702   tree identifier = NULL_TREE;
4703
4704   /* Assume all the expressions will be constant.  */
4705   if (non_constant_p)
4706     *non_constant_p = false;
4707
4708   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4709     return error_mark_node;
4710
4711   /* Consume expressions until there are no more.  */
4712   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4713     while (true)
4714       {
4715         tree expr;
4716
4717         /* At the beginning of attribute lists, check to see if the
4718            next token is an identifier.  */
4719         if (is_attribute_list
4720             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4721           {
4722             cp_token *token;
4723
4724             /* Consume the identifier.  */
4725             token = cp_lexer_consume_token (parser->lexer);
4726             /* Save the identifier.  */
4727             identifier = token->value;
4728           }
4729         else
4730           {
4731             /* Parse the next assignment-expression.  */
4732             if (non_constant_p)
4733               {
4734                 bool expr_non_constant_p;
4735                 expr = (cp_parser_constant_expression
4736                         (parser, /*allow_non_constant_p=*/true,
4737                          &expr_non_constant_p));
4738                 if (expr_non_constant_p)
4739                   *non_constant_p = true;
4740               }
4741             else
4742               expr = cp_parser_assignment_expression (parser, cast_p);
4743
4744             if (fold_expr_p)
4745               expr = fold_non_dependent_expr (expr);
4746
4747              /* Add it to the list.  We add error_mark_node
4748                 expressions to the list, so that we can still tell if
4749                 the correct form for a parenthesized expression-list
4750                 is found. That gives better errors.  */
4751             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4752
4753             if (expr == error_mark_node)
4754               goto skip_comma;
4755           }
4756
4757         /* After the first item, attribute lists look the same as
4758            expression lists.  */
4759         is_attribute_list = false;
4760
4761       get_comma:;
4762         /* If the next token isn't a `,', then we are done.  */
4763         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4764           break;
4765
4766         /* Otherwise, consume the `,' and keep going.  */
4767         cp_lexer_consume_token (parser->lexer);
4768       }
4769
4770   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4771     {
4772       int ending;
4773
4774     skip_comma:;
4775       /* We try and resync to an unnested comma, as that will give the
4776          user better diagnostics.  */
4777       ending = cp_parser_skip_to_closing_parenthesis (parser,
4778                                                       /*recovering=*/true,
4779                                                       /*or_comma=*/true,
4780                                                       /*consume_paren=*/true);
4781       if (ending < 0)
4782         goto get_comma;
4783       if (!ending)
4784         return error_mark_node;
4785     }
4786
4787   /* We built up the list in reverse order so we must reverse it now.  */
4788   expression_list = nreverse (expression_list);
4789   if (identifier)
4790     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4791
4792   return expression_list;
4793 }
4794
4795 /* Parse a pseudo-destructor-name.
4796
4797    pseudo-destructor-name:
4798      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4799      :: [opt] nested-name-specifier template template-id :: ~ type-name
4800      :: [opt] nested-name-specifier [opt] ~ type-name
4801
4802    If either of the first two productions is used, sets *SCOPE to the
4803    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4804    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4805    or ERROR_MARK_NODE if the parse fails.  */
4806
4807 static void
4808 cp_parser_pseudo_destructor_name (cp_parser* parser,
4809                                   tree* scope,
4810                                   tree* type)
4811 {
4812   bool nested_name_specifier_p;
4813
4814   /* Assume that things will not work out.  */
4815   *type = error_mark_node;
4816
4817   /* Look for the optional `::' operator.  */
4818   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4819   /* Look for the optional nested-name-specifier.  */
4820   nested_name_specifier_p
4821     = (cp_parser_nested_name_specifier_opt (parser,
4822                                             /*typename_keyword_p=*/false,
4823                                             /*check_dependency_p=*/true,
4824                                             /*type_p=*/false,
4825                                             /*is_declaration=*/true)
4826        != NULL_TREE);
4827   /* Now, if we saw a nested-name-specifier, we might be doing the
4828      second production.  */
4829   if (nested_name_specifier_p
4830       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4831     {
4832       /* Consume the `template' keyword.  */
4833       cp_lexer_consume_token (parser->lexer);
4834       /* Parse the template-id.  */
4835       cp_parser_template_id (parser,
4836                              /*template_keyword_p=*/true,
4837                              /*check_dependency_p=*/false,
4838                              /*is_declaration=*/true);
4839       /* Look for the `::' token.  */
4840       cp_parser_require (parser, CPP_SCOPE, "`::'");
4841     }
4842   /* If the next token is not a `~', then there might be some
4843      additional qualification.  */
4844   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4845     {
4846       /* Look for the type-name.  */
4847       *scope = TREE_TYPE (cp_parser_type_name (parser));
4848
4849       if (*scope == error_mark_node)
4850         return;
4851
4852       /* If we don't have ::~, then something has gone wrong.  Since
4853          the only caller of this function is looking for something
4854          after `.' or `->' after a scalar type, most likely the
4855          program is trying to get a member of a non-aggregate
4856          type.  */
4857       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4858           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4859         {
4860           cp_parser_error (parser, "request for member of non-aggregate type");
4861           return;
4862         }
4863
4864       /* Look for the `::' token.  */
4865       cp_parser_require (parser, CPP_SCOPE, "`::'");
4866     }
4867   else
4868     *scope = NULL_TREE;
4869
4870   /* Look for the `~'.  */
4871   cp_parser_require (parser, CPP_COMPL, "`~'");
4872   /* Look for the type-name again.  We are not responsible for
4873      checking that it matches the first type-name.  */
4874   *type = cp_parser_type_name (parser);
4875 }
4876
4877 /* Parse a unary-expression.
4878
4879    unary-expression:
4880      postfix-expression
4881      ++ cast-expression
4882      -- cast-expression
4883      unary-operator cast-expression
4884      sizeof unary-expression
4885      sizeof ( type-id )
4886      new-expression
4887      delete-expression
4888
4889    GNU Extensions:
4890
4891    unary-expression:
4892      __extension__ cast-expression
4893      __alignof__ unary-expression
4894      __alignof__ ( type-id )
4895      __real__ cast-expression
4896      __imag__ cast-expression
4897      && identifier
4898
4899    ADDRESS_P is true iff the unary-expression is appearing as the
4900    operand of the `&' operator.   CAST_P is true if this expression is
4901    the target of a cast.
4902
4903    Returns a representation of the expression.  */
4904
4905 static tree
4906 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4907 {
4908   cp_token *token;
4909   enum tree_code unary_operator;
4910
4911   /* Peek at the next token.  */
4912   token = cp_lexer_peek_token (parser->lexer);
4913   /* Some keywords give away the kind of expression.  */
4914   if (token->type == CPP_KEYWORD)
4915     {
4916       enum rid keyword = token->keyword;
4917
4918       switch (keyword)
4919         {
4920         case RID_ALIGNOF:
4921         case RID_SIZEOF:
4922           {
4923             tree operand;
4924             enum tree_code op;
4925
4926             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4927             /* Consume the token.  */
4928             cp_lexer_consume_token (parser->lexer);
4929             /* Parse the operand.  */
4930             operand = cp_parser_sizeof_operand (parser, keyword);
4931
4932             if (TYPE_P (operand))
4933               return cxx_sizeof_or_alignof_type (operand, op, true);
4934             else
4935               return cxx_sizeof_or_alignof_expr (operand, op);
4936           }
4937
4938         case RID_NEW:
4939           return cp_parser_new_expression (parser);
4940
4941         case RID_DELETE:
4942           return cp_parser_delete_expression (parser);
4943
4944         case RID_EXTENSION:
4945           {
4946             /* The saved value of the PEDANTIC flag.  */
4947             int saved_pedantic;
4948             tree expr;
4949
4950             /* Save away the PEDANTIC flag.  */
4951             cp_parser_extension_opt (parser, &saved_pedantic);
4952             /* Parse the cast-expression.  */
4953             expr = cp_parser_simple_cast_expression (parser);
4954             /* Restore the PEDANTIC flag.  */
4955             pedantic = saved_pedantic;
4956
4957             return expr;
4958           }
4959
4960         case RID_REALPART:
4961         case RID_IMAGPART:
4962           {
4963             tree expression;
4964
4965             /* Consume the `__real__' or `__imag__' token.  */
4966             cp_lexer_consume_token (parser->lexer);
4967             /* Parse the cast-expression.  */
4968             expression = cp_parser_simple_cast_expression (parser);
4969             /* Create the complete representation.  */
4970             return build_x_unary_op ((keyword == RID_REALPART
4971                                       ? REALPART_EXPR : IMAGPART_EXPR),
4972                                      expression);
4973           }
4974           break;
4975
4976         default:
4977           break;
4978         }
4979     }
4980
4981   /* Look for the `:: new' and `:: delete', which also signal the
4982      beginning of a new-expression, or delete-expression,
4983      respectively.  If the next token is `::', then it might be one of
4984      these.  */
4985   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4986     {
4987       enum rid keyword;
4988
4989       /* See if the token after the `::' is one of the keywords in
4990          which we're interested.  */
4991       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4992       /* If it's `new', we have a new-expression.  */
4993       if (keyword == RID_NEW)
4994         return cp_parser_new_expression (parser);
4995       /* Similarly, for `delete'.  */
4996       else if (keyword == RID_DELETE)
4997         return cp_parser_delete_expression (parser);
4998     }
4999
5000   /* Look for a unary operator.  */
5001   unary_operator = cp_parser_unary_operator (token);
5002   /* The `++' and `--' operators can be handled similarly, even though
5003      they are not technically unary-operators in the grammar.  */
5004   if (unary_operator == ERROR_MARK)
5005     {
5006       if (token->type == CPP_PLUS_PLUS)
5007         unary_operator = PREINCREMENT_EXPR;
5008       else if (token->type == CPP_MINUS_MINUS)
5009         unary_operator = PREDECREMENT_EXPR;
5010       /* Handle the GNU address-of-label extension.  */
5011       else if (cp_parser_allow_gnu_extensions_p (parser)
5012                && token->type == CPP_AND_AND)
5013         {
5014           tree identifier;
5015
5016           /* Consume the '&&' token.  */
5017           cp_lexer_consume_token (parser->lexer);
5018           /* Look for the identifier.  */
5019           identifier = cp_parser_identifier (parser);
5020           /* Create an expression representing the address.  */
5021           return finish_label_address_expr (identifier);
5022         }
5023     }
5024   if (unary_operator != ERROR_MARK)
5025     {
5026       tree cast_expression;
5027       tree expression = error_mark_node;
5028       const char *non_constant_p = NULL;
5029
5030       /* Consume the operator token.  */
5031       token = cp_lexer_consume_token (parser->lexer);
5032       /* Parse the cast-expression.  */
5033       cast_expression
5034         = cp_parser_cast_expression (parser,
5035                                      unary_operator == ADDR_EXPR,
5036                                      /*cast_p=*/false);
5037       /* Now, build an appropriate representation.  */
5038       switch (unary_operator)
5039         {
5040         case INDIRECT_REF:
5041           non_constant_p = "`*'";
5042           expression = build_x_indirect_ref (cast_expression, "unary *");
5043           break;
5044
5045         case ADDR_EXPR:
5046           non_constant_p = "`&'";
5047           /* Fall through.  */
5048         case BIT_NOT_EXPR:
5049           expression = build_x_unary_op (unary_operator, cast_expression);
5050           break;
5051
5052         case PREINCREMENT_EXPR:
5053         case PREDECREMENT_EXPR:
5054           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5055                             ? "`++'" : "`--'");
5056           /* Fall through.  */
5057         case UNARY_PLUS_EXPR:
5058         case NEGATE_EXPR:
5059         case TRUTH_NOT_EXPR:
5060           expression = finish_unary_op_expr (unary_operator, cast_expression);
5061           break;
5062
5063         default:
5064           gcc_unreachable ();
5065         }
5066
5067       if (non_constant_p
5068           && cp_parser_non_integral_constant_expression (parser,
5069                                                          non_constant_p))
5070         expression = error_mark_node;
5071
5072       return expression;
5073     }
5074
5075   return cp_parser_postfix_expression (parser, address_p, cast_p);
5076 }
5077
5078 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5079    unary-operator, the corresponding tree code is returned.  */
5080
5081 static enum tree_code
5082 cp_parser_unary_operator (cp_token* token)
5083 {
5084   switch (token->type)
5085     {
5086     case CPP_MULT:
5087       return INDIRECT_REF;
5088
5089     case CPP_AND:
5090       return ADDR_EXPR;
5091
5092     case CPP_PLUS:
5093       return UNARY_PLUS_EXPR;
5094
5095     case CPP_MINUS:
5096       return NEGATE_EXPR;
5097
5098     case CPP_NOT:
5099       return TRUTH_NOT_EXPR;
5100
5101     case CPP_COMPL:
5102       return BIT_NOT_EXPR;
5103
5104     default:
5105       return ERROR_MARK;
5106     }
5107 }
5108
5109 /* Parse a new-expression.
5110
5111    new-expression:
5112      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5113      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5114
5115    Returns a representation of the expression.  */
5116
5117 static tree
5118 cp_parser_new_expression (cp_parser* parser)
5119 {
5120   bool global_scope_p;
5121   tree placement;
5122   tree type;
5123   tree initializer;
5124   tree nelts;
5125
5126   /* Look for the optional `::' operator.  */
5127   global_scope_p
5128     = (cp_parser_global_scope_opt (parser,
5129                                    /*current_scope_valid_p=*/false)
5130        != NULL_TREE);
5131   /* Look for the `new' operator.  */
5132   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5133   /* There's no easy way to tell a new-placement from the
5134      `( type-id )' construct.  */
5135   cp_parser_parse_tentatively (parser);
5136   /* Look for a new-placement.  */
5137   placement = cp_parser_new_placement (parser);
5138   /* If that didn't work out, there's no new-placement.  */
5139   if (!cp_parser_parse_definitely (parser))
5140     placement = NULL_TREE;
5141
5142   /* If the next token is a `(', then we have a parenthesized
5143      type-id.  */
5144   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5145     {
5146       /* Consume the `('.  */
5147       cp_lexer_consume_token (parser->lexer);
5148       /* Parse the type-id.  */
5149       type = cp_parser_type_id (parser);
5150       /* Look for the closing `)'.  */
5151       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5152       /* There should not be a direct-new-declarator in this production,
5153          but GCC used to allowed this, so we check and emit a sensible error
5154          message for this case.  */
5155       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5156         {
5157           error ("array bound forbidden after parenthesized type-id");
5158           inform ("try removing the parentheses around the type-id");
5159           cp_parser_direct_new_declarator (parser);
5160         }
5161       nelts = NULL_TREE;
5162     }
5163   /* Otherwise, there must be a new-type-id.  */
5164   else
5165     type = cp_parser_new_type_id (parser, &nelts);
5166
5167   /* If the next token is a `(', then we have a new-initializer.  */
5168   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5169     initializer = cp_parser_new_initializer (parser);
5170   else
5171     initializer = NULL_TREE;
5172
5173   /* A new-expression may not appear in an integral constant
5174      expression.  */
5175   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5176     return error_mark_node;
5177
5178   /* Create a representation of the new-expression.  */
5179   return build_new (placement, type, nelts, initializer, global_scope_p);
5180 }
5181
5182 /* Parse a new-placement.
5183
5184    new-placement:
5185      ( expression-list )
5186
5187    Returns the same representation as for an expression-list.  */
5188
5189 static tree
5190 cp_parser_new_placement (cp_parser* parser)
5191 {
5192   tree expression_list;
5193
5194   /* Parse the expression-list.  */
5195   expression_list = (cp_parser_parenthesized_expression_list
5196                      (parser, false, /*cast_p=*/false,
5197                       /*non_constant_p=*/NULL));
5198
5199   return expression_list;
5200 }
5201
5202 /* Parse a new-type-id.
5203
5204    new-type-id:
5205      type-specifier-seq new-declarator [opt]
5206
5207    Returns the TYPE allocated.  If the new-type-id indicates an array
5208    type, *NELTS is set to the number of elements in the last array
5209    bound; the TYPE will not include the last array bound.  */
5210
5211 static tree
5212 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5213 {
5214   cp_decl_specifier_seq type_specifier_seq;
5215   cp_declarator *new_declarator;
5216   cp_declarator *declarator;
5217   cp_declarator *outer_declarator;
5218   const char *saved_message;
5219   tree type;
5220
5221   /* The type-specifier sequence must not contain type definitions.
5222      (It cannot contain declarations of new types either, but if they
5223      are not definitions we will catch that because they are not
5224      complete.)  */
5225   saved_message = parser->type_definition_forbidden_message;
5226   parser->type_definition_forbidden_message
5227     = "types may not be defined in a new-type-id";
5228   /* Parse the type-specifier-seq.  */
5229   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5230                                 &type_specifier_seq);
5231   /* Restore the old message.  */
5232   parser->type_definition_forbidden_message = saved_message;
5233   /* Parse the new-declarator.  */
5234   new_declarator = cp_parser_new_declarator_opt (parser);
5235
5236   /* Determine the number of elements in the last array dimension, if
5237      any.  */
5238   *nelts = NULL_TREE;
5239   /* Skip down to the last array dimension.  */
5240   declarator = new_declarator;
5241   outer_declarator = NULL;
5242   while (declarator && (declarator->kind == cdk_pointer
5243                         || declarator->kind == cdk_ptrmem))
5244     {
5245       outer_declarator = declarator;
5246       declarator = declarator->declarator;
5247     }
5248   while (declarator
5249          && declarator->kind == cdk_array
5250          && declarator->declarator
5251          && declarator->declarator->kind == cdk_array)
5252     {
5253       outer_declarator = declarator;
5254       declarator = declarator->declarator;
5255     }
5256
5257   if (declarator && declarator->kind == cdk_array)
5258     {
5259       *nelts = declarator->u.array.bounds;
5260       if (*nelts == error_mark_node)
5261         *nelts = integer_one_node;
5262
5263       if (outer_declarator)
5264         outer_declarator->declarator = declarator->declarator;
5265       else
5266         new_declarator = NULL;
5267     }
5268
5269   type = groktypename (&type_specifier_seq, new_declarator);
5270   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5271     {
5272       *nelts = array_type_nelts_top (type);
5273       type = TREE_TYPE (type);
5274     }
5275   return type;
5276 }
5277
5278 /* Parse an (optional) new-declarator.
5279
5280    new-declarator:
5281      ptr-operator new-declarator [opt]
5282      direct-new-declarator
5283
5284    Returns the declarator.  */
5285
5286 static cp_declarator *
5287 cp_parser_new_declarator_opt (cp_parser* parser)
5288 {
5289   enum tree_code code;
5290   tree type;
5291   cp_cv_quals cv_quals;
5292
5293   /* We don't know if there's a ptr-operator next, or not.  */
5294   cp_parser_parse_tentatively (parser);
5295   /* Look for a ptr-operator.  */
5296   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5297   /* If that worked, look for more new-declarators.  */
5298   if (cp_parser_parse_definitely (parser))
5299     {
5300       cp_declarator *declarator;
5301
5302       /* Parse another optional declarator.  */
5303       declarator = cp_parser_new_declarator_opt (parser);
5304
5305       /* Create the representation of the declarator.  */
5306       if (type)
5307         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5308       else if (code == INDIRECT_REF)
5309         declarator = make_pointer_declarator (cv_quals, declarator);
5310       else
5311         declarator = make_reference_declarator (cv_quals, declarator);
5312
5313       return declarator;
5314     }
5315
5316   /* If the next token is a `[', there is a direct-new-declarator.  */
5317   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5318     return cp_parser_direct_new_declarator (parser);
5319
5320   return NULL;
5321 }
5322
5323 /* Parse a direct-new-declarator.
5324
5325    direct-new-declarator:
5326      [ expression ]
5327      direct-new-declarator [constant-expression]
5328
5329    */
5330
5331 static cp_declarator *
5332 cp_parser_direct_new_declarator (cp_parser* parser)
5333 {
5334   cp_declarator *declarator = NULL;
5335
5336   while (true)
5337     {
5338       tree expression;
5339
5340       /* Look for the opening `['.  */
5341       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5342       /* The first expression is not required to be constant.  */
5343       if (!declarator)
5344         {
5345           expression = cp_parser_expression (parser, /*cast_p=*/false);
5346           /* The standard requires that the expression have integral
5347              type.  DR 74 adds enumeration types.  We believe that the
5348              real intent is that these expressions be handled like the
5349              expression in a `switch' condition, which also allows
5350              classes with a single conversion to integral or
5351              enumeration type.  */
5352           if (!processing_template_decl)
5353             {
5354               expression
5355                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5356                                               expression,
5357                                               /*complain=*/true);
5358               if (!expression)
5359                 {
5360                   error ("expression in new-declarator must have integral "
5361                          "or enumeration type");
5362                   expression = error_mark_node;
5363                 }
5364             }
5365         }
5366       /* But all the other expressions must be.  */
5367       else
5368         expression
5369           = cp_parser_constant_expression (parser,
5370                                            /*allow_non_constant=*/false,
5371                                            NULL);
5372       /* Look for the closing `]'.  */
5373       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5374
5375       /* Add this bound to the declarator.  */
5376       declarator = make_array_declarator (declarator, expression);
5377
5378       /* If the next token is not a `[', then there are no more
5379          bounds.  */
5380       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5381         break;
5382     }
5383
5384   return declarator;
5385 }
5386
5387 /* Parse a new-initializer.
5388
5389    new-initializer:
5390      ( expression-list [opt] )
5391
5392    Returns a representation of the expression-list.  If there is no
5393    expression-list, VOID_ZERO_NODE is returned.  */
5394
5395 static tree
5396 cp_parser_new_initializer (cp_parser* parser)
5397 {
5398   tree expression_list;
5399
5400   expression_list = (cp_parser_parenthesized_expression_list
5401                      (parser, false, /*cast_p=*/false,
5402                       /*non_constant_p=*/NULL));
5403   if (!expression_list)
5404     expression_list = void_zero_node;
5405
5406   return expression_list;
5407 }
5408
5409 /* Parse a delete-expression.
5410
5411    delete-expression:
5412      :: [opt] delete cast-expression
5413      :: [opt] delete [ ] cast-expression
5414
5415    Returns a representation of the expression.  */
5416
5417 static tree
5418 cp_parser_delete_expression (cp_parser* parser)
5419 {
5420   bool global_scope_p;
5421   bool array_p;
5422   tree expression;
5423
5424   /* Look for the optional `::' operator.  */
5425   global_scope_p
5426     = (cp_parser_global_scope_opt (parser,
5427                                    /*current_scope_valid_p=*/false)
5428        != NULL_TREE);
5429   /* Look for the `delete' keyword.  */
5430   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5431   /* See if the array syntax is in use.  */
5432   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5433     {
5434       /* Consume the `[' token.  */
5435       cp_lexer_consume_token (parser->lexer);
5436       /* Look for the `]' token.  */
5437       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5438       /* Remember that this is the `[]' construct.  */
5439       array_p = true;
5440     }
5441   else
5442     array_p = false;
5443
5444   /* Parse the cast-expression.  */
5445   expression = cp_parser_simple_cast_expression (parser);
5446
5447   /* A delete-expression may not appear in an integral constant
5448      expression.  */
5449   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5450     return error_mark_node;
5451
5452   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5453 }
5454
5455 /* Parse a cast-expression.
5456
5457    cast-expression:
5458      unary-expression
5459      ( type-id ) cast-expression
5460
5461    ADDRESS_P is true iff the unary-expression is appearing as the
5462    operand of the `&' operator.   CAST_P is true if this expression is
5463    the target of a cast.
5464
5465    Returns a representation of the expression.  */
5466
5467 static tree
5468 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5469 {
5470   /* If it's a `(', then we might be looking at a cast.  */
5471   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5472     {
5473       tree type = NULL_TREE;
5474       tree expr = NULL_TREE;
5475       bool compound_literal_p;
5476       const char *saved_message;
5477
5478       /* There's no way to know yet whether or not this is a cast.
5479          For example, `(int (3))' is a unary-expression, while `(int)
5480          3' is a cast.  So, we resort to parsing tentatively.  */
5481       cp_parser_parse_tentatively (parser);
5482       /* Types may not be defined in a cast.  */
5483       saved_message = parser->type_definition_forbidden_message;
5484       parser->type_definition_forbidden_message
5485         = "types may not be defined in casts";
5486       /* Consume the `('.  */
5487       cp_lexer_consume_token (parser->lexer);
5488       /* A very tricky bit is that `(struct S) { 3 }' is a
5489          compound-literal (which we permit in C++ as an extension).
5490          But, that construct is not a cast-expression -- it is a
5491          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5492          is legal; if the compound-literal were a cast-expression,
5493          you'd need an extra set of parentheses.)  But, if we parse
5494          the type-id, and it happens to be a class-specifier, then we
5495          will commit to the parse at that point, because we cannot
5496          undo the action that is done when creating a new class.  So,
5497          then we cannot back up and do a postfix-expression.
5498
5499          Therefore, we scan ahead to the closing `)', and check to see
5500          if the token after the `)' is a `{'.  If so, we are not
5501          looking at a cast-expression.
5502
5503          Save tokens so that we can put them back.  */
5504       cp_lexer_save_tokens (parser->lexer);
5505       /* Skip tokens until the next token is a closing parenthesis.
5506          If we find the closing `)', and the next token is a `{', then
5507          we are looking at a compound-literal.  */
5508       compound_literal_p
5509         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5510                                                   /*consume_paren=*/true)
5511            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5512       /* Roll back the tokens we skipped.  */
5513       cp_lexer_rollback_tokens (parser->lexer);
5514       /* If we were looking at a compound-literal, simulate an error
5515          so that the call to cp_parser_parse_definitely below will
5516          fail.  */
5517       if (compound_literal_p)
5518         cp_parser_simulate_error (parser);
5519       else
5520         {
5521           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5522           parser->in_type_id_in_expr_p = true;
5523           /* Look for the type-id.  */
5524           type = cp_parser_type_id (parser);
5525           /* Look for the closing `)'.  */
5526           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5527           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5528         }
5529
5530       /* Restore the saved message.  */
5531       parser->type_definition_forbidden_message = saved_message;
5532
5533       /* If ok so far, parse the dependent expression. We cannot be
5534          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5535          ctor of T, but looks like a cast to function returning T
5536          without a dependent expression.  */
5537       if (!cp_parser_error_occurred (parser))
5538         expr = cp_parser_cast_expression (parser,
5539                                           /*address_p=*/false,
5540                                           /*cast_p=*/true);
5541
5542       if (cp_parser_parse_definitely (parser))
5543         {
5544           /* Warn about old-style casts, if so requested.  */
5545           if (warn_old_style_cast
5546               && !in_system_header
5547               && !VOID_TYPE_P (type)
5548               && current_lang_name != lang_name_c)
5549             warning (0, "use of old-style cast");
5550
5551           /* Only type conversions to integral or enumeration types
5552              can be used in constant-expressions.  */
5553           if (parser->integral_constant_expression_p
5554               && !dependent_type_p (type)
5555               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5556               && (cp_parser_non_integral_constant_expression
5557                   (parser,
5558                    "a cast to a type other than an integral or "
5559                    "enumeration type")))
5560             return error_mark_node;
5561
5562           /* Perform the cast.  */
5563           expr = build_c_cast (type, expr);
5564           return expr;
5565         }
5566     }
5567
5568   /* If we get here, then it's not a cast, so it must be a
5569      unary-expression.  */
5570   return cp_parser_unary_expression (parser, address_p, cast_p);
5571 }
5572
5573 /* Parse a binary expression of the general form:
5574
5575    pm-expression:
5576      cast-expression
5577      pm-expression .* cast-expression
5578      pm-expression ->* cast-expression
5579
5580    multiplicative-expression:
5581      pm-expression
5582      multiplicative-expression * pm-expression
5583      multiplicative-expression / pm-expression
5584      multiplicative-expression % pm-expression
5585
5586    additive-expression:
5587      multiplicative-expression
5588      additive-expression + multiplicative-expression
5589      additive-expression - multiplicative-expression
5590
5591    shift-expression:
5592      additive-expression
5593      shift-expression << additive-expression
5594      shift-expression >> additive-expression
5595
5596    relational-expression:
5597      shift-expression
5598      relational-expression < shift-expression
5599      relational-expression > shift-expression
5600      relational-expression <= shift-expression
5601      relational-expression >= shift-expression
5602
5603   GNU Extension:
5604
5605    relational-expression:
5606      relational-expression <? shift-expression
5607      relational-expression >? shift-expression
5608
5609    equality-expression:
5610      relational-expression
5611      equality-expression == relational-expression
5612      equality-expression != relational-expression
5613
5614    and-expression:
5615      equality-expression
5616      and-expression & equality-expression
5617
5618    exclusive-or-expression:
5619      and-expression
5620      exclusive-or-expression ^ and-expression
5621
5622    inclusive-or-expression:
5623      exclusive-or-expression
5624      inclusive-or-expression | exclusive-or-expression
5625
5626    logical-and-expression:
5627      inclusive-or-expression
5628      logical-and-expression && inclusive-or-expression
5629
5630    logical-or-expression:
5631      logical-and-expression
5632      logical-or-expression || logical-and-expression
5633
5634    All these are implemented with a single function like:
5635
5636    binary-expression:
5637      simple-cast-expression
5638      binary-expression <token> binary-expression
5639
5640    CAST_P is true if this expression is the target of a cast.
5641
5642    The binops_by_token map is used to get the tree codes for each <token> type.
5643    binary-expressions are associated according to a precedence table.  */
5644
5645 #define TOKEN_PRECEDENCE(token) \
5646   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5647    ? PREC_NOT_OPERATOR \
5648    : binops_by_token[token->type].prec)
5649
5650 static tree
5651 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5652 {
5653   cp_parser_expression_stack stack;
5654   cp_parser_expression_stack_entry *sp = &stack[0];
5655   tree lhs, rhs;
5656   cp_token *token;
5657   enum tree_code tree_type;
5658   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5659   bool overloaded_p;
5660
5661   /* Parse the first expression.  */
5662   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5663
5664   for (;;)
5665     {
5666       /* Get an operator token.  */
5667       token = cp_lexer_peek_token (parser->lexer);
5668       if (token->type == CPP_MIN || token->type == CPP_MAX)
5669         cp_parser_warn_min_max ();
5670
5671       new_prec = TOKEN_PRECEDENCE (token);
5672
5673       /* Popping an entry off the stack means we completed a subexpression:
5674          - either we found a token which is not an operator (`>' where it is not
5675            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5676            will happen repeatedly;
5677          - or, we found an operator which has lower priority.  This is the case
5678            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5679            parsing `3 * 4'.  */
5680       if (new_prec <= prec)
5681         {
5682           if (sp == stack)
5683             break;
5684           else
5685             goto pop;
5686         }
5687
5688      get_rhs:
5689       tree_type = binops_by_token[token->type].tree_type;
5690
5691       /* We used the operator token.  */
5692       cp_lexer_consume_token (parser->lexer);
5693
5694       /* Extract another operand.  It may be the RHS of this expression
5695          or the LHS of a new, higher priority expression.  */
5696       rhs = cp_parser_simple_cast_expression (parser);
5697
5698       /* Get another operator token.  Look up its precedence to avoid
5699          building a useless (immediately popped) stack entry for common
5700          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5701       token = cp_lexer_peek_token (parser->lexer);
5702       lookahead_prec = TOKEN_PRECEDENCE (token);
5703       if (lookahead_prec > new_prec)
5704         {
5705           /* ... and prepare to parse the RHS of the new, higher priority
5706              expression.  Since precedence levels on the stack are
5707              monotonically increasing, we do not have to care about
5708              stack overflows.  */
5709           sp->prec = prec;
5710           sp->tree_type = tree_type;
5711           sp->lhs = lhs;
5712           sp++;
5713           lhs = rhs;
5714           prec = new_prec;
5715           new_prec = lookahead_prec;
5716           goto get_rhs;
5717
5718          pop:
5719           /* If the stack is not empty, we have parsed into LHS the right side
5720              (`4' in the example above) of an expression we had suspended.
5721              We can use the information on the stack to recover the LHS (`3')
5722              from the stack together with the tree code (`MULT_EXPR'), and
5723              the precedence of the higher level subexpression
5724              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5725              which will be used to actually build the additive expression.  */
5726           --sp;
5727           prec = sp->prec;
5728           tree_type = sp->tree_type;
5729           rhs = lhs;
5730           lhs = sp->lhs;
5731         }
5732
5733       overloaded_p = false;
5734       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5735
5736       /* If the binary operator required the use of an overloaded operator,
5737          then this expression cannot be an integral constant-expression.
5738          An overloaded operator can be used even if both operands are
5739          otherwise permissible in an integral constant-expression if at
5740          least one of the operands is of enumeration type.  */
5741
5742       if (overloaded_p
5743           && (cp_parser_non_integral_constant_expression
5744               (parser, "calls to overloaded operators")))
5745         return error_mark_node;
5746     }
5747
5748   return lhs;
5749 }
5750
5751
5752 /* Parse the `? expression : assignment-expression' part of a
5753    conditional-expression.  The LOGICAL_OR_EXPR is the
5754    logical-or-expression that started the conditional-expression.
5755    Returns a representation of the entire conditional-expression.
5756
5757    This routine is used by cp_parser_assignment_expression.
5758
5759      ? expression : assignment-expression
5760
5761    GNU Extensions:
5762
5763      ? : assignment-expression */
5764
5765 static tree
5766 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5767 {
5768   tree expr;
5769   tree assignment_expr;
5770
5771   /* Consume the `?' token.  */
5772   cp_lexer_consume_token (parser->lexer);
5773   if (cp_parser_allow_gnu_extensions_p (parser)
5774       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5775     /* Implicit true clause.  */
5776     expr = NULL_TREE;
5777   else
5778     /* Parse the expression.  */
5779     expr = cp_parser_expression (parser, /*cast_p=*/false);
5780
5781   /* The next token should be a `:'.  */
5782   cp_parser_require (parser, CPP_COLON, "`:'");
5783   /* Parse the assignment-expression.  */
5784   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5785
5786   /* Build the conditional-expression.  */
5787   return build_x_conditional_expr (logical_or_expr,
5788                                    expr,
5789                                    assignment_expr);
5790 }
5791
5792 /* Parse an assignment-expression.
5793
5794    assignment-expression:
5795      conditional-expression
5796      logical-or-expression assignment-operator assignment_expression
5797      throw-expression
5798
5799    CAST_P is true if this expression is the target of a cast.
5800
5801    Returns a representation for the expression.  */
5802
5803 static tree
5804 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5805 {
5806   tree expr;
5807
5808   /* If the next token is the `throw' keyword, then we're looking at
5809      a throw-expression.  */
5810   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5811     expr = cp_parser_throw_expression (parser);
5812   /* Otherwise, it must be that we are looking at a
5813      logical-or-expression.  */
5814   else
5815     {
5816       /* Parse the binary expressions (logical-or-expression).  */
5817       expr = cp_parser_binary_expression (parser, cast_p);
5818       /* If the next token is a `?' then we're actually looking at a
5819          conditional-expression.  */
5820       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5821         return cp_parser_question_colon_clause (parser, expr);
5822       else
5823         {
5824           enum tree_code assignment_operator;
5825
5826           /* If it's an assignment-operator, we're using the second
5827              production.  */
5828           assignment_operator
5829             = cp_parser_assignment_operator_opt (parser);
5830           if (assignment_operator != ERROR_MARK)
5831             {
5832               tree rhs;
5833
5834               /* Parse the right-hand side of the assignment.  */
5835               rhs = cp_parser_assignment_expression (parser, cast_p);
5836               /* An assignment may not appear in a
5837                  constant-expression.  */
5838               if (cp_parser_non_integral_constant_expression (parser,
5839                                                               "an assignment"))
5840                 return error_mark_node;
5841               /* Build the assignment expression.  */
5842               expr = build_x_modify_expr (expr,
5843                                           assignment_operator,
5844                                           rhs);
5845             }
5846         }
5847     }
5848
5849   return expr;
5850 }
5851
5852 /* Parse an (optional) assignment-operator.
5853
5854    assignment-operator: one of
5855      = *= /= %= += -= >>= <<= &= ^= |=
5856
5857    GNU Extension:
5858
5859    assignment-operator: one of
5860      <?= >?=
5861
5862    If the next token is an assignment operator, the corresponding tree
5863    code is returned, and the token is consumed.  For example, for
5864    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5865    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5866    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5867    operator, ERROR_MARK is returned.  */
5868
5869 static enum tree_code
5870 cp_parser_assignment_operator_opt (cp_parser* parser)
5871 {
5872   enum tree_code op;
5873   cp_token *token;
5874
5875   /* Peek at the next toen.  */
5876   token = cp_lexer_peek_token (parser->lexer);
5877
5878   switch (token->type)
5879     {
5880     case CPP_EQ:
5881       op = NOP_EXPR;
5882       break;
5883
5884     case CPP_MULT_EQ:
5885       op = MULT_EXPR;
5886       break;
5887
5888     case CPP_DIV_EQ:
5889       op = TRUNC_DIV_EXPR;
5890       break;
5891
5892     case CPP_MOD_EQ:
5893       op = TRUNC_MOD_EXPR;
5894       break;
5895
5896     case CPP_PLUS_EQ:
5897       op = PLUS_EXPR;
5898       break;
5899
5900     case CPP_MINUS_EQ:
5901       op = MINUS_EXPR;
5902       break;
5903
5904     case CPP_RSHIFT_EQ:
5905       op = RSHIFT_EXPR;
5906       break;
5907
5908     case CPP_LSHIFT_EQ:
5909       op = LSHIFT_EXPR;
5910       break;
5911
5912     case CPP_AND_EQ:
5913       op = BIT_AND_EXPR;
5914       break;
5915
5916     case CPP_XOR_EQ:
5917       op = BIT_XOR_EXPR;
5918       break;
5919
5920     case CPP_OR_EQ:
5921       op = BIT_IOR_EXPR;
5922       break;
5923
5924     case CPP_MIN_EQ:
5925       op = MIN_EXPR;
5926       cp_parser_warn_min_max ();
5927       break;
5928
5929     case CPP_MAX_EQ:
5930       op = MAX_EXPR;
5931       cp_parser_warn_min_max ();
5932       break;
5933
5934     default:
5935       /* Nothing else is an assignment operator.  */
5936       op = ERROR_MARK;
5937     }
5938
5939   /* If it was an assignment operator, consume it.  */
5940   if (op != ERROR_MARK)
5941     cp_lexer_consume_token (parser->lexer);
5942
5943   return op;
5944 }
5945
5946 /* Parse an expression.
5947
5948    expression:
5949      assignment-expression
5950      expression , assignment-expression
5951
5952    CAST_P is true if this expression is the target of a cast.
5953
5954    Returns a representation of the expression.  */
5955
5956 static tree
5957 cp_parser_expression (cp_parser* parser, bool cast_p)
5958 {
5959   tree expression = NULL_TREE;
5960
5961   while (true)
5962     {
5963       tree assignment_expression;
5964
5965       /* Parse the next assignment-expression.  */
5966       assignment_expression
5967         = cp_parser_assignment_expression (parser, cast_p);
5968       /* If this is the first assignment-expression, we can just
5969          save it away.  */
5970       if (!expression)
5971         expression = assignment_expression;
5972       else
5973         expression = build_x_compound_expr (expression,
5974                                             assignment_expression);
5975       /* If the next token is not a comma, then we are done with the
5976          expression.  */
5977       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5978         break;
5979       /* Consume the `,'.  */
5980       cp_lexer_consume_token (parser->lexer);
5981       /* A comma operator cannot appear in a constant-expression.  */
5982       if (cp_parser_non_integral_constant_expression (parser,
5983                                                       "a comma operator"))
5984         expression = error_mark_node;
5985     }
5986
5987   return expression;
5988 }
5989
5990 /* Parse a constant-expression.
5991
5992    constant-expression:
5993      conditional-expression
5994
5995   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5996   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5997   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5998   is false, NON_CONSTANT_P should be NULL.  */
5999
6000 static tree
6001 cp_parser_constant_expression (cp_parser* parser,
6002                                bool allow_non_constant_p,
6003                                bool *non_constant_p)
6004 {
6005   bool saved_integral_constant_expression_p;
6006   bool saved_allow_non_integral_constant_expression_p;
6007   bool saved_non_integral_constant_expression_p;
6008   tree expression;
6009
6010   /* It might seem that we could simply parse the
6011      conditional-expression, and then check to see if it were
6012      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6013      one that the compiler can figure out is constant, possibly after
6014      doing some simplifications or optimizations.  The standard has a
6015      precise definition of constant-expression, and we must honor
6016      that, even though it is somewhat more restrictive.
6017
6018      For example:
6019
6020        int i[(2, 3)];
6021
6022      is not a legal declaration, because `(2, 3)' is not a
6023      constant-expression.  The `,' operator is forbidden in a
6024      constant-expression.  However, GCC's constant-folding machinery
6025      will fold this operation to an INTEGER_CST for `3'.  */
6026
6027   /* Save the old settings.  */
6028   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6029   saved_allow_non_integral_constant_expression_p
6030     = parser->allow_non_integral_constant_expression_p;
6031   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6032   /* We are now parsing a constant-expression.  */
6033   parser->integral_constant_expression_p = true;
6034   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6035   parser->non_integral_constant_expression_p = false;
6036   /* Although the grammar says "conditional-expression", we parse an
6037      "assignment-expression", which also permits "throw-expression"
6038      and the use of assignment operators.  In the case that
6039      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6040      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6041      actually essential that we look for an assignment-expression.
6042      For example, cp_parser_initializer_clauses uses this function to
6043      determine whether a particular assignment-expression is in fact
6044      constant.  */
6045   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6046   /* Restore the old settings.  */
6047   parser->integral_constant_expression_p
6048     = saved_integral_constant_expression_p;
6049   parser->allow_non_integral_constant_expression_p
6050     = saved_allow_non_integral_constant_expression_p;
6051   if (allow_non_constant_p)
6052     *non_constant_p = parser->non_integral_constant_expression_p;
6053   else if (parser->non_integral_constant_expression_p)
6054     expression = error_mark_node;
6055   parser->non_integral_constant_expression_p
6056     = saved_non_integral_constant_expression_p;
6057
6058   return expression;
6059 }
6060
6061 /* Parse __builtin_offsetof.
6062
6063    offsetof-expression:
6064      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6065
6066    offsetof-member-designator:
6067      id-expression
6068      | offsetof-member-designator "." id-expression
6069      | offsetof-member-designator "[" expression "]"
6070 */
6071
6072 static tree
6073 cp_parser_builtin_offsetof (cp_parser *parser)
6074 {
6075   int save_ice_p, save_non_ice_p;
6076   tree type, expr;
6077   cp_id_kind dummy;
6078
6079   /* We're about to accept non-integral-constant things, but will
6080      definitely yield an integral constant expression.  Save and
6081      restore these values around our local parsing.  */
6082   save_ice_p = parser->integral_constant_expression_p;
6083   save_non_ice_p = parser->non_integral_constant_expression_p;
6084
6085   /* Consume the "__builtin_offsetof" token.  */
6086   cp_lexer_consume_token (parser->lexer);
6087   /* Consume the opening `('.  */
6088   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6089   /* Parse the type-id.  */
6090   type = cp_parser_type_id (parser);
6091   /* Look for the `,'.  */
6092   cp_parser_require (parser, CPP_COMMA, "`,'");
6093
6094   /* Build the (type *)null that begins the traditional offsetof macro.  */
6095   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6096
6097   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6098   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6099                                                  true, &dummy);
6100   while (true)
6101     {
6102       cp_token *token = cp_lexer_peek_token (parser->lexer);
6103       switch (token->type)
6104         {
6105         case CPP_OPEN_SQUARE:
6106           /* offsetof-member-designator "[" expression "]" */
6107           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6108           break;
6109
6110         case CPP_DOT:
6111           /* offsetof-member-designator "." identifier */
6112           cp_lexer_consume_token (parser->lexer);
6113           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6114                                                          true, &dummy);
6115           break;
6116
6117         case CPP_CLOSE_PAREN:
6118           /* Consume the ")" token.  */
6119           cp_lexer_consume_token (parser->lexer);
6120           goto success;
6121
6122         default:
6123           /* Error.  We know the following require will fail, but
6124              that gives the proper error message.  */
6125           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6126           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6127           expr = error_mark_node;
6128           goto failure;
6129         }
6130     }
6131
6132  success:
6133   /* If we're processing a template, we can't finish the semantics yet.
6134      Otherwise we can fold the entire expression now.  */
6135   if (processing_template_decl)
6136     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6137   else
6138     expr = finish_offsetof (expr);
6139
6140  failure:
6141   parser->integral_constant_expression_p = save_ice_p;
6142   parser->non_integral_constant_expression_p = save_non_ice_p;
6143
6144   return expr;
6145 }
6146
6147 /* Statements [gram.stmt.stmt]  */
6148
6149 /* Parse a statement.
6150
6151    statement:
6152      labeled-statement
6153      expression-statement
6154      compound-statement
6155      selection-statement
6156      iteration-statement
6157      jump-statement
6158      declaration-statement
6159      try-block  */
6160
6161 static void
6162 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6163 {
6164   tree statement;
6165   cp_token *token;
6166   location_t statement_location;
6167
6168  restart:
6169   /* There is no statement yet.  */
6170   statement = NULL_TREE;
6171   /* Peek at the next token.  */
6172   token = cp_lexer_peek_token (parser->lexer);
6173   /* Remember the location of the first token in the statement.  */
6174   statement_location = token->location;
6175   /* If this is a keyword, then that will often determine what kind of
6176      statement we have.  */
6177   if (token->type == CPP_KEYWORD)
6178     {
6179       enum rid keyword = token->keyword;
6180
6181       switch (keyword)
6182         {
6183         case RID_CASE:
6184         case RID_DEFAULT:
6185           /* Looks like a labeled-statement with a case label.
6186              Parse the label, and then use tail recursion to parse
6187              the statement.  */
6188           cp_parser_label_for_labeled_statement (parser);
6189           goto restart;
6190
6191         case RID_IF:
6192         case RID_SWITCH:
6193           statement = cp_parser_selection_statement (parser);
6194           break;
6195
6196         case RID_WHILE:
6197         case RID_DO:
6198         case RID_FOR:
6199           statement = cp_parser_iteration_statement (parser);
6200           break;
6201
6202         case RID_BREAK:
6203         case RID_CONTINUE:
6204         case RID_RETURN:
6205         case RID_GOTO:
6206           statement = cp_parser_jump_statement (parser);
6207           break;
6208
6209           /* Objective-C++ exception-handling constructs.  */
6210         case RID_AT_TRY:
6211         case RID_AT_CATCH:
6212         case RID_AT_FINALLY:
6213         case RID_AT_SYNCHRONIZED:
6214         case RID_AT_THROW:
6215           statement = cp_parser_objc_statement (parser);
6216           break;
6217
6218         case RID_TRY:
6219           statement = cp_parser_try_block (parser);
6220           break;
6221
6222         default:
6223           /* It might be a keyword like `int' that can start a
6224              declaration-statement.  */
6225           break;
6226         }
6227     }
6228   else if (token->type == CPP_NAME)
6229     {
6230       /* If the next token is a `:', then we are looking at a
6231          labeled-statement.  */
6232       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6233       if (token->type == CPP_COLON)
6234         {
6235           /* Looks like a labeled-statement with an ordinary label.
6236              Parse the label, and then use tail recursion to parse
6237              the statement.  */
6238           cp_parser_label_for_labeled_statement (parser);
6239           goto restart;
6240         }
6241     }
6242   /* Anything that starts with a `{' must be a compound-statement.  */
6243   else if (token->type == CPP_OPEN_BRACE)
6244     statement = cp_parser_compound_statement (parser, NULL, false);
6245   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6246      a statement all its own.  */
6247   else if (token->type == CPP_PRAGMA)
6248     {
6249       cp_lexer_handle_pragma (parser->lexer);
6250       return;
6251     }
6252   else if (token->type == CPP_EOF)
6253     {
6254       cp_parser_error (parser, "expected statement");
6255       return;
6256     }
6257
6258   /* Everything else must be a declaration-statement or an
6259      expression-statement.  Try for the declaration-statement
6260      first, unless we are looking at a `;', in which case we know that
6261      we have an expression-statement.  */
6262   if (!statement)
6263     {
6264       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6265         {
6266           cp_parser_parse_tentatively (parser);
6267           /* Try to parse the declaration-statement.  */
6268           cp_parser_declaration_statement (parser);
6269           /* If that worked, we're done.  */
6270           if (cp_parser_parse_definitely (parser))
6271             return;
6272         }
6273       /* Look for an expression-statement instead.  */
6274       statement = cp_parser_expression_statement (parser, in_statement_expr);
6275     }
6276
6277   /* Set the line number for the statement.  */
6278   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6279     SET_EXPR_LOCATION (statement, statement_location);
6280 }
6281
6282 /* Parse the label for a labeled-statement, i.e.
6283
6284    identifier :
6285    case constant-expression :
6286    default :
6287
6288    GNU Extension:
6289    case constant-expression ... constant-expression : statement
6290
6291    When a label is parsed without errors, the label is added to the
6292    parse tree by the finish_* functions, so this function doesn't
6293    have to return the label.  */
6294   
6295 static void
6296 cp_parser_label_for_labeled_statement (cp_parser* parser)
6297 {
6298   cp_token *token;
6299
6300   /* The next token should be an identifier.  */
6301   token = cp_lexer_peek_token (parser->lexer);
6302   if (token->type != CPP_NAME
6303       && token->type != CPP_KEYWORD)
6304     {
6305       cp_parser_error (parser, "expected labeled-statement");
6306       return;
6307     }
6308
6309   switch (token->keyword)
6310     {
6311     case RID_CASE:
6312       {
6313         tree expr, expr_hi;
6314         cp_token *ellipsis;
6315
6316         /* Consume the `case' token.  */
6317         cp_lexer_consume_token (parser->lexer);
6318         /* Parse the constant-expression.  */
6319         expr = cp_parser_constant_expression (parser,
6320                                               /*allow_non_constant_p=*/false,
6321                                               NULL);
6322
6323         ellipsis = cp_lexer_peek_token (parser->lexer);
6324         if (ellipsis->type == CPP_ELLIPSIS)
6325           {
6326             /* Consume the `...' token.  */
6327             cp_lexer_consume_token (parser->lexer);
6328             expr_hi =
6329               cp_parser_constant_expression (parser,
6330                                              /*allow_non_constant_p=*/false,
6331                                              NULL);
6332             /* We don't need to emit warnings here, as the common code
6333                will do this for us.  */
6334           }
6335         else
6336           expr_hi = NULL_TREE;
6337
6338         if (!parser->in_switch_statement_p)
6339           error ("case label %qE not within a switch statement", expr);
6340         else
6341           finish_case_label (expr, expr_hi);
6342       }
6343       break;
6344
6345     case RID_DEFAULT:
6346       /* Consume the `default' token.  */
6347       cp_lexer_consume_token (parser->lexer);
6348       if (!parser->in_switch_statement_p)
6349         error ("case label not within a switch statement");
6350       else
6351         finish_case_label (NULL_TREE, NULL_TREE);
6352       break;
6353
6354     default:
6355       /* Anything else must be an ordinary label.  */
6356       finish_label_stmt (cp_parser_identifier (parser));
6357       break;
6358     }
6359
6360   /* Require the `:' token.  */
6361   cp_parser_require (parser, CPP_COLON, "`:'");
6362 }
6363
6364 /* Parse an expression-statement.
6365
6366    expression-statement:
6367      expression [opt] ;
6368
6369    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6370    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6371    indicates whether this expression-statement is part of an
6372    expression statement.  */
6373
6374 static tree
6375 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6376 {
6377   tree statement = NULL_TREE;
6378
6379   /* If the next token is a ';', then there is no expression
6380      statement.  */
6381   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6382     statement = cp_parser_expression (parser, /*cast_p=*/false);
6383
6384   /* Consume the final `;'.  */
6385   cp_parser_consume_semicolon_at_end_of_statement (parser);
6386
6387   if (in_statement_expr
6388       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6389     /* This is the final expression statement of a statement
6390        expression.  */
6391     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6392   else if (statement)
6393     statement = finish_expr_stmt (statement);
6394   else
6395     finish_stmt ();
6396
6397   return statement;
6398 }
6399
6400 /* Parse a compound-statement.
6401
6402    compound-statement:
6403      { statement-seq [opt] }
6404
6405    Returns a tree representing the statement.  */
6406
6407 static tree
6408 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6409                               bool in_try)
6410 {
6411   tree compound_stmt;
6412
6413   /* Consume the `{'.  */
6414   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6415     return error_mark_node;
6416   /* Begin the compound-statement.  */
6417   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6418   /* Parse an (optional) statement-seq.  */
6419   cp_parser_statement_seq_opt (parser, in_statement_expr);
6420   /* Finish the compound-statement.  */
6421   finish_compound_stmt (compound_stmt);
6422   /* Consume the `}'.  */
6423   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6424
6425   return compound_stmt;
6426 }
6427
6428 /* Parse an (optional) statement-seq.
6429
6430    statement-seq:
6431      statement
6432      statement-seq [opt] statement  */
6433
6434 static void
6435 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6436 {
6437   /* Scan statements until there aren't any more.  */
6438   while (true)
6439     {
6440       /* If we're looking at a `}', then we've run out of statements.  */
6441       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6442           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6443         break;
6444
6445       /* Parse the statement.  */
6446       cp_parser_statement (parser, in_statement_expr);
6447     }
6448 }
6449
6450 /* Parse a selection-statement.
6451
6452    selection-statement:
6453      if ( condition ) statement
6454      if ( condition ) statement else statement
6455      switch ( condition ) statement
6456
6457    Returns the new IF_STMT or SWITCH_STMT.  */
6458
6459 static tree
6460 cp_parser_selection_statement (cp_parser* parser)
6461 {
6462   cp_token *token;
6463   enum rid keyword;
6464
6465   /* Peek at the next token.  */
6466   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6467
6468   /* See what kind of keyword it is.  */
6469   keyword = token->keyword;
6470   switch (keyword)
6471     {
6472     case RID_IF:
6473     case RID_SWITCH:
6474       {
6475         tree statement;
6476         tree condition;
6477
6478         /* Look for the `('.  */
6479         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6480           {
6481             cp_parser_skip_to_end_of_statement (parser);
6482             return error_mark_node;
6483           }
6484
6485         /* Begin the selection-statement.  */
6486         if (keyword == RID_IF)
6487           statement = begin_if_stmt ();
6488         else
6489           statement = begin_switch_stmt ();
6490
6491         /* Parse the condition.  */
6492         condition = cp_parser_condition (parser);
6493         /* Look for the `)'.  */
6494         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6495           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6496                                                  /*consume_paren=*/true);
6497
6498         if (keyword == RID_IF)
6499           {
6500             /* Add the condition.  */
6501             finish_if_stmt_cond (condition, statement);
6502
6503             /* Parse the then-clause.  */
6504             cp_parser_implicitly_scoped_statement (parser);
6505             finish_then_clause (statement);
6506
6507             /* If the next token is `else', parse the else-clause.  */
6508             if (cp_lexer_next_token_is_keyword (parser->lexer,
6509                                                 RID_ELSE))
6510               {
6511                 /* Consume the `else' keyword.  */
6512                 cp_lexer_consume_token (parser->lexer);
6513                 begin_else_clause (statement);
6514                 /* Parse the else-clause.  */
6515                 cp_parser_implicitly_scoped_statement (parser);
6516                 finish_else_clause (statement);
6517               }
6518
6519             /* Now we're all done with the if-statement.  */
6520             finish_if_stmt (statement);
6521           }
6522         else
6523           {
6524             bool in_switch_statement_p;
6525
6526             /* Add the condition.  */
6527             finish_switch_cond (condition, statement);
6528
6529             /* Parse the body of the switch-statement.  */
6530             in_switch_statement_p = parser->in_switch_statement_p;
6531             parser->in_switch_statement_p = true;
6532             cp_parser_implicitly_scoped_statement (parser);
6533             parser->in_switch_statement_p = in_switch_statement_p;
6534
6535             /* Now we're all done with the switch-statement.  */
6536             finish_switch_stmt (statement);
6537           }
6538
6539         return statement;
6540       }
6541       break;
6542
6543     default:
6544       cp_parser_error (parser, "expected selection-statement");
6545       return error_mark_node;
6546     }
6547 }
6548
6549 /* Parse a condition.
6550
6551    condition:
6552      expression
6553      type-specifier-seq declarator = assignment-expression
6554
6555    GNU Extension:
6556
6557    condition:
6558      type-specifier-seq declarator asm-specification [opt]
6559        attributes [opt] = assignment-expression
6560
6561    Returns the expression that should be tested.  */
6562
6563 static tree
6564 cp_parser_condition (cp_parser* parser)
6565 {
6566   cp_decl_specifier_seq type_specifiers;
6567   const char *saved_message;
6568
6569   /* Try the declaration first.  */
6570   cp_parser_parse_tentatively (parser);
6571   /* New types are not allowed in the type-specifier-seq for a
6572      condition.  */
6573   saved_message = parser->type_definition_forbidden_message;
6574   parser->type_definition_forbidden_message
6575     = "types may not be defined in conditions";
6576   /* Parse the type-specifier-seq.  */
6577   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6578                                 &type_specifiers);
6579   /* Restore the saved message.  */
6580   parser->type_definition_forbidden_message = saved_message;
6581   /* If all is well, we might be looking at a declaration.  */
6582   if (!cp_parser_error_occurred (parser))
6583     {
6584       tree decl;
6585       tree asm_specification;
6586       tree attributes;
6587       cp_declarator *declarator;
6588       tree initializer = NULL_TREE;
6589
6590       /* Parse the declarator.  */
6591       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6592                                          /*ctor_dtor_or_conv_p=*/NULL,
6593                                          /*parenthesized_p=*/NULL,
6594                                          /*member_p=*/false);
6595       /* Parse the attributes.  */
6596       attributes = cp_parser_attributes_opt (parser);
6597       /* Parse the asm-specification.  */
6598       asm_specification = cp_parser_asm_specification_opt (parser);
6599       /* If the next token is not an `=', then we might still be
6600          looking at an expression.  For example:
6601
6602            if (A(a).x)
6603
6604          looks like a decl-specifier-seq and a declarator -- but then
6605          there is no `=', so this is an expression.  */
6606       cp_parser_require (parser, CPP_EQ, "`='");
6607       /* If we did see an `=', then we are looking at a declaration
6608          for sure.  */
6609       if (cp_parser_parse_definitely (parser))
6610         {
6611           tree pushed_scope;
6612           bool non_constant_p;
6613
6614           /* Create the declaration.  */
6615           decl = start_decl (declarator, &type_specifiers,
6616                              /*initialized_p=*/true,
6617                              attributes, /*prefix_attributes=*/NULL_TREE,
6618                              &pushed_scope);
6619           /* Parse the assignment-expression.  */
6620           initializer 
6621             = cp_parser_constant_expression (parser,
6622                                              /*allow_non_constant_p=*/true,
6623                                              &non_constant_p);
6624           if (!non_constant_p)
6625             initializer = fold_non_dependent_expr (initializer);
6626
6627           /* Process the initializer.  */
6628           cp_finish_decl (decl,
6629                           initializer, !non_constant_p, 
6630                           asm_specification,
6631                           LOOKUP_ONLYCONVERTING);
6632
6633           if (pushed_scope)
6634             pop_scope (pushed_scope);
6635
6636           return convert_from_reference (decl);
6637         }
6638     }
6639   /* If we didn't even get past the declarator successfully, we are
6640      definitely not looking at a declaration.  */
6641   else
6642     cp_parser_abort_tentative_parse (parser);
6643
6644   /* Otherwise, we are looking at an expression.  */
6645   return cp_parser_expression (parser, /*cast_p=*/false);
6646 }
6647
6648 /* Parse an iteration-statement.
6649
6650    iteration-statement:
6651      while ( condition ) statement
6652      do statement while ( expression ) ;
6653      for ( for-init-statement condition [opt] ; expression [opt] )
6654        statement
6655
6656    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6657
6658 static tree
6659 cp_parser_iteration_statement (cp_parser* parser)
6660 {
6661   cp_token *token;
6662   enum rid keyword;
6663   tree statement;
6664   bool in_iteration_statement_p;
6665
6666
6667   /* Peek at the next token.  */
6668   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6669   if (!token)
6670     return error_mark_node;
6671
6672   /* Remember whether or not we are already within an iteration
6673      statement.  */
6674   in_iteration_statement_p = parser->in_iteration_statement_p;
6675
6676   /* See what kind of keyword it is.  */
6677   keyword = token->keyword;
6678   switch (keyword)
6679     {
6680     case RID_WHILE:
6681       {
6682         tree condition;
6683
6684         /* Begin the while-statement.  */
6685         statement = begin_while_stmt ();
6686         /* Look for the `('.  */
6687         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6688         /* Parse the condition.  */
6689         condition = cp_parser_condition (parser);
6690         finish_while_stmt_cond (condition, statement);
6691         /* Look for the `)'.  */
6692         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6693         /* Parse the dependent statement.  */
6694         parser->in_iteration_statement_p = true;
6695         cp_parser_already_scoped_statement (parser);
6696         parser->in_iteration_statement_p = in_iteration_statement_p;
6697         /* We're done with the while-statement.  */
6698         finish_while_stmt (statement);
6699       }
6700       break;
6701
6702     case RID_DO:
6703       {
6704         tree expression;
6705
6706         /* Begin the do-statement.  */
6707         statement = begin_do_stmt ();
6708         /* Parse the body of the do-statement.  */
6709         parser->in_iteration_statement_p = true;
6710         cp_parser_implicitly_scoped_statement (parser);
6711         parser->in_iteration_statement_p = in_iteration_statement_p;
6712         finish_do_body (statement);
6713         /* Look for the `while' keyword.  */
6714         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6715         /* Look for the `('.  */
6716         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6717         /* Parse the expression.  */
6718         expression = cp_parser_expression (parser, /*cast_p=*/false);
6719         /* We're done with the do-statement.  */
6720         finish_do_stmt (expression, statement);
6721         /* Look for the `)'.  */
6722         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6723         /* Look for the `;'.  */
6724         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6725       }
6726       break;
6727
6728     case RID_FOR:
6729       {
6730         tree condition = NULL_TREE;
6731         tree expression = NULL_TREE;
6732
6733         /* Begin the for-statement.  */
6734         statement = begin_for_stmt ();
6735         /* Look for the `('.  */
6736         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6737         /* Parse the initialization.  */
6738         cp_parser_for_init_statement (parser);
6739         finish_for_init_stmt (statement);
6740
6741         /* If there's a condition, process it.  */
6742         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6743           condition = cp_parser_condition (parser);
6744         finish_for_cond (condition, statement);
6745         /* Look for the `;'.  */
6746         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6747
6748         /* If there's an expression, process it.  */
6749         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6750           expression = cp_parser_expression (parser, /*cast_p=*/false);
6751         finish_for_expr (expression, statement);
6752         /* Look for the `)'.  */
6753         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6754
6755         /* Parse the body of the for-statement.  */
6756         parser->in_iteration_statement_p = true;
6757         cp_parser_already_scoped_statement (parser);
6758         parser->in_iteration_statement_p = in_iteration_statement_p;
6759
6760         /* We're done with the for-statement.  */
6761         finish_for_stmt (statement);
6762       }
6763       break;
6764
6765     default:
6766       cp_parser_error (parser, "expected iteration-statement");
6767       statement = error_mark_node;
6768       break;
6769     }
6770
6771   return statement;
6772 }
6773
6774 /* Parse a for-init-statement.
6775
6776    for-init-statement:
6777      expression-statement
6778      simple-declaration  */
6779
6780 static void
6781 cp_parser_for_init_statement (cp_parser* parser)
6782 {
6783   /* If the next token is a `;', then we have an empty
6784      expression-statement.  Grammatically, this is also a
6785      simple-declaration, but an invalid one, because it does not
6786      declare anything.  Therefore, if we did not handle this case
6787      specially, we would issue an error message about an invalid
6788      declaration.  */
6789   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6790     {
6791       /* We're going to speculatively look for a declaration, falling back
6792          to an expression, if necessary.  */
6793       cp_parser_parse_tentatively (parser);
6794       /* Parse the declaration.  */
6795       cp_parser_simple_declaration (parser,
6796                                     /*function_definition_allowed_p=*/false);
6797       /* If the tentative parse failed, then we shall need to look for an
6798          expression-statement.  */
6799       if (cp_parser_parse_definitely (parser))
6800         return;
6801     }
6802
6803   cp_parser_expression_statement (parser, false);
6804 }
6805
6806 /* Parse a jump-statement.
6807
6808    jump-statement:
6809      break ;
6810      continue ;
6811      return expression [opt] ;
6812      goto identifier ;
6813
6814    GNU extension:
6815
6816    jump-statement:
6817      goto * expression ;
6818
6819    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6820
6821 static tree
6822 cp_parser_jump_statement (cp_parser* parser)
6823 {
6824   tree statement = error_mark_node;
6825   cp_token *token;
6826   enum rid keyword;
6827
6828   /* Peek at the next token.  */
6829   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6830   if (!token)
6831     return error_mark_node;
6832
6833   /* See what kind of keyword it is.  */
6834   keyword = token->keyword;
6835   switch (keyword)
6836     {
6837     case RID_BREAK:
6838       if (!parser->in_switch_statement_p
6839           && !parser->in_iteration_statement_p)
6840         {
6841           error ("break statement not within loop or switch");
6842           statement = error_mark_node;
6843         }
6844       else
6845         statement = finish_break_stmt ();
6846       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6847       break;
6848
6849     case RID_CONTINUE:
6850       if (!parser->in_iteration_statement_p)
6851         {
6852           error ("continue statement not within a loop");
6853           statement = error_mark_node;
6854         }
6855       else
6856         statement = finish_continue_stmt ();
6857       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6858       break;
6859
6860     case RID_RETURN:
6861       {
6862         tree expr;
6863
6864         /* If the next token is a `;', then there is no
6865            expression.  */
6866         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6867           expr = cp_parser_expression (parser, /*cast_p=*/false);
6868         else
6869           expr = NULL_TREE;
6870         /* Build the return-statement.  */
6871         statement = finish_return_stmt (expr);
6872         /* Look for the final `;'.  */
6873         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6874       }
6875       break;
6876
6877     case RID_GOTO:
6878       /* Create the goto-statement.  */
6879       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6880         {
6881           /* Issue a warning about this use of a GNU extension.  */
6882           if (pedantic)
6883             pedwarn ("ISO C++ forbids computed gotos");
6884           /* Consume the '*' token.  */
6885           cp_lexer_consume_token (parser->lexer);
6886           /* Parse the dependent expression.  */
6887           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6888         }
6889       else
6890         finish_goto_stmt (cp_parser_identifier (parser));
6891       /* Look for the final `;'.  */
6892       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6893       break;
6894
6895     default:
6896       cp_parser_error (parser, "expected jump-statement");
6897       break;
6898     }
6899
6900   return statement;
6901 }
6902
6903 /* Parse a declaration-statement.
6904
6905    declaration-statement:
6906      block-declaration  */
6907
6908 static void
6909 cp_parser_declaration_statement (cp_parser* parser)
6910 {
6911   void *p;
6912
6913   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6914   p = obstack_alloc (&declarator_obstack, 0);
6915
6916  /* Parse the block-declaration.  */
6917   cp_parser_block_declaration (parser, /*statement_p=*/true);
6918
6919   /* Free any declarators allocated.  */
6920   obstack_free (&declarator_obstack, p);
6921
6922   /* Finish off the statement.  */
6923   finish_stmt ();
6924 }
6925
6926 /* Some dependent statements (like `if (cond) statement'), are
6927    implicitly in their own scope.  In other words, if the statement is
6928    a single statement (as opposed to a compound-statement), it is
6929    none-the-less treated as if it were enclosed in braces.  Any
6930    declarations appearing in the dependent statement are out of scope
6931    after control passes that point.  This function parses a statement,
6932    but ensures that is in its own scope, even if it is not a
6933    compound-statement.
6934
6935    Returns the new statement.  */
6936
6937 static tree
6938 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6939 {
6940   tree statement;
6941
6942   /* If the token is not a `{', then we must take special action.  */
6943   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6944     {
6945       /* Create a compound-statement.  */
6946       statement = begin_compound_stmt (0);
6947       /* Parse the dependent-statement.  */
6948       cp_parser_statement (parser, false);
6949       /* Finish the dummy compound-statement.  */
6950       finish_compound_stmt (statement);
6951     }
6952   /* Otherwise, we simply parse the statement directly.  */
6953   else
6954     statement = cp_parser_compound_statement (parser, NULL, false);
6955
6956   /* Return the statement.  */
6957   return statement;
6958 }
6959
6960 /* For some dependent statements (like `while (cond) statement'), we
6961    have already created a scope.  Therefore, even if the dependent
6962    statement is a compound-statement, we do not want to create another
6963    scope.  */
6964
6965 static void
6966 cp_parser_already_scoped_statement (cp_parser* parser)
6967 {
6968   /* If the token is a `{', then we must take special action.  */
6969   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6970     cp_parser_statement (parser, false);
6971   else
6972     {
6973       /* Avoid calling cp_parser_compound_statement, so that we
6974          don't create a new scope.  Do everything else by hand.  */
6975       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6976       cp_parser_statement_seq_opt (parser, false);
6977       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6978     }
6979 }
6980
6981 /* Declarations [gram.dcl.dcl] */
6982
6983 /* Parse an optional declaration-sequence.
6984
6985    declaration-seq:
6986      declaration
6987      declaration-seq declaration  */
6988
6989 static void
6990 cp_parser_declaration_seq_opt (cp_parser* parser)
6991 {
6992   while (true)
6993     {
6994       cp_token *token;
6995
6996       token = cp_lexer_peek_token (parser->lexer);
6997
6998       if (token->type == CPP_CLOSE_BRACE
6999           || token->type == CPP_EOF)
7000         break;
7001
7002       if (token->type == CPP_SEMICOLON)
7003         {
7004           /* A declaration consisting of a single semicolon is
7005              invalid.  Allow it unless we're being pedantic.  */
7006           cp_lexer_consume_token (parser->lexer);
7007           if (pedantic && !in_system_header)
7008             pedwarn ("extra %<;%>");
7009           continue;
7010         }
7011
7012       /* If we're entering or exiting a region that's implicitly
7013          extern "C", modify the lang context appropriately.  */
7014       if (!parser->implicit_extern_c && token->implicit_extern_c)
7015         {
7016           push_lang_context (lang_name_c);
7017           parser->implicit_extern_c = true;
7018         }
7019       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7020         {
7021           pop_lang_context ();
7022           parser->implicit_extern_c = false;
7023         }
7024
7025       if (token->type == CPP_PRAGMA)
7026         {
7027           /* A top-level declaration can consist solely of a #pragma.
7028              A nested declaration cannot, so this is done here and not
7029              in cp_parser_declaration.  (A #pragma at block scope is
7030              handled in cp_parser_statement.)  */
7031           cp_lexer_handle_pragma (parser->lexer);
7032           continue;
7033         }
7034
7035       /* Parse the declaration itself.  */
7036       cp_parser_declaration (parser);
7037     }
7038 }
7039
7040 /* Parse a declaration.
7041
7042    declaration:
7043      block-declaration
7044      function-definition
7045      template-declaration
7046      explicit-instantiation
7047      explicit-specialization
7048      linkage-specification
7049      namespace-definition
7050
7051    GNU extension:
7052
7053    declaration:
7054       __extension__ declaration */
7055
7056 static void
7057 cp_parser_declaration (cp_parser* parser)
7058 {
7059   cp_token token1;
7060   cp_token token2;
7061   int saved_pedantic;
7062   void *p;
7063
7064   /* Check for the `__extension__' keyword.  */
7065   if (cp_parser_extension_opt (parser, &saved_pedantic))
7066     {
7067       /* Parse the qualified declaration.  */
7068       cp_parser_declaration (parser);
7069       /* Restore the PEDANTIC flag.  */
7070       pedantic = saved_pedantic;
7071
7072       return;
7073     }
7074
7075   /* Try to figure out what kind of declaration is present.  */
7076   token1 = *cp_lexer_peek_token (parser->lexer);
7077
7078   if (token1.type != CPP_EOF)
7079     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7080   else
7081     token2.type = token2.keyword = RID_MAX;
7082
7083   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7084   p = obstack_alloc (&declarator_obstack, 0);
7085
7086   /* If the next token is `extern' and the following token is a string
7087      literal, then we have a linkage specification.  */
7088   if (token1.keyword == RID_EXTERN
7089       && cp_parser_is_string_literal (&token2))
7090     cp_parser_linkage_specification (parser);
7091   /* If the next token is `template', then we have either a template
7092      declaration, an explicit instantiation, or an explicit
7093      specialization.  */
7094   else if (token1.keyword == RID_TEMPLATE)
7095     {
7096       /* `template <>' indicates a template specialization.  */
7097       if (token2.type == CPP_LESS
7098           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7099         cp_parser_explicit_specialization (parser);
7100       /* `template <' indicates a template declaration.  */
7101       else if (token2.type == CPP_LESS)
7102         cp_parser_template_declaration (parser, /*member_p=*/false);
7103       /* Anything else must be an explicit instantiation.  */
7104       else
7105         cp_parser_explicit_instantiation (parser);
7106     }
7107   /* If the next token is `export', then we have a template
7108      declaration.  */
7109   else if (token1.keyword == RID_EXPORT)
7110     cp_parser_template_declaration (parser, /*member_p=*/false);
7111   /* If the next token is `extern', 'static' or 'inline' and the one
7112      after that is `template', we have a GNU extended explicit
7113      instantiation directive.  */
7114   else if (cp_parser_allow_gnu_extensions_p (parser)
7115            && (token1.keyword == RID_EXTERN
7116                || token1.keyword == RID_STATIC
7117                || token1.keyword == RID_INLINE)
7118            && token2.keyword == RID_TEMPLATE)
7119     cp_parser_explicit_instantiation (parser);
7120   /* If the next token is `namespace', check for a named or unnamed
7121      namespace definition.  */
7122   else if (token1.keyword == RID_NAMESPACE
7123            && (/* A named namespace definition.  */
7124                (token2.type == CPP_NAME
7125                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7126                     == CPP_OPEN_BRACE))
7127                /* An unnamed namespace definition.  */
7128                || token2.type == CPP_OPEN_BRACE))
7129     cp_parser_namespace_definition (parser);
7130   /* Objective-C++ declaration/definition.  */
7131   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7132     cp_parser_objc_declaration (parser);
7133   /* We must have either a block declaration or a function
7134      definition.  */
7135   else
7136     /* Try to parse a block-declaration, or a function-definition.  */
7137     cp_parser_block_declaration (parser, /*statement_p=*/false);
7138
7139   /* Free any declarators allocated.  */
7140   obstack_free (&declarator_obstack, p);
7141 }
7142
7143 /* Parse a block-declaration.
7144
7145    block-declaration:
7146      simple-declaration
7147      asm-definition
7148      namespace-alias-definition
7149      using-declaration
7150      using-directive
7151
7152    GNU Extension:
7153
7154    block-declaration:
7155      __extension__ block-declaration
7156      label-declaration
7157
7158    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7159    part of a declaration-statement.  */
7160
7161 static void
7162 cp_parser_block_declaration (cp_parser *parser,
7163                              bool      statement_p)
7164 {
7165   cp_token *token1;
7166   int saved_pedantic;
7167
7168   /* Check for the `__extension__' keyword.  */
7169   if (cp_parser_extension_opt (parser, &saved_pedantic))
7170     {
7171       /* Parse the qualified declaration.  */
7172       cp_parser_block_declaration (parser, statement_p);
7173       /* Restore the PEDANTIC flag.  */
7174       pedantic = saved_pedantic;
7175
7176       return;
7177     }
7178
7179   /* Peek at the next token to figure out which kind of declaration is
7180      present.  */
7181   token1 = cp_lexer_peek_token (parser->lexer);
7182
7183   /* If the next keyword is `asm', we have an asm-definition.  */
7184   if (token1->keyword == RID_ASM)
7185     {
7186       if (statement_p)
7187         cp_parser_commit_to_tentative_parse (parser);
7188       cp_parser_asm_definition (parser);
7189     }
7190   /* If the next keyword is `namespace', we have a
7191      namespace-alias-definition.  */
7192   else if (token1->keyword == RID_NAMESPACE)
7193     cp_parser_namespace_alias_definition (parser);
7194   /* If the next keyword is `using', we have either a
7195      using-declaration or a using-directive.  */
7196   else if (token1->keyword == RID_USING)
7197     {
7198       cp_token *token2;
7199
7200       if (statement_p)
7201         cp_parser_commit_to_tentative_parse (parser);
7202       /* If the token after `using' is `namespace', then we have a
7203          using-directive.  */
7204       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7205       if (token2->keyword == RID_NAMESPACE)
7206         cp_parser_using_directive (parser);
7207       /* Otherwise, it's a using-declaration.  */
7208       else
7209         cp_parser_using_declaration (parser,
7210                                      /*access_declaration_p=*/false);
7211     }
7212   /* If the next keyword is `__label__' we have a label declaration.  */
7213   else if (token1->keyword == RID_LABEL)
7214     {
7215       if (statement_p)
7216         cp_parser_commit_to_tentative_parse (parser);
7217       cp_parser_label_declaration (parser);
7218     }
7219   /* Anything else must be a simple-declaration.  */
7220   else
7221     cp_parser_simple_declaration (parser, !statement_p);
7222 }
7223
7224 /* Parse a simple-declaration.
7225
7226    simple-declaration:
7227      decl-specifier-seq [opt] init-declarator-list [opt] ;
7228
7229    init-declarator-list:
7230      init-declarator
7231      init-declarator-list , init-declarator
7232
7233    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7234    function-definition as a simple-declaration.  */
7235
7236 static void
7237 cp_parser_simple_declaration (cp_parser* parser,
7238                               bool function_definition_allowed_p)
7239 {
7240   cp_decl_specifier_seq decl_specifiers;
7241   int declares_class_or_enum;
7242   bool saw_declarator;
7243
7244   /* Defer access checks until we know what is being declared; the
7245      checks for names appearing in the decl-specifier-seq should be
7246      done as if we were in the scope of the thing being declared.  */
7247   push_deferring_access_checks (dk_deferred);
7248
7249   /* Parse the decl-specifier-seq.  We have to keep track of whether
7250      or not the decl-specifier-seq declares a named class or
7251      enumeration type, since that is the only case in which the
7252      init-declarator-list is allowed to be empty.
7253
7254      [dcl.dcl]
7255
7256      In a simple-declaration, the optional init-declarator-list can be
7257      omitted only when declaring a class or enumeration, that is when
7258      the decl-specifier-seq contains either a class-specifier, an
7259      elaborated-type-specifier, or an enum-specifier.  */
7260   cp_parser_decl_specifier_seq (parser,
7261                                 CP_PARSER_FLAGS_OPTIONAL,
7262                                 &decl_specifiers,
7263                                 &declares_class_or_enum);
7264   /* We no longer need to defer access checks.  */
7265   stop_deferring_access_checks ();
7266
7267   /* In a block scope, a valid declaration must always have a
7268      decl-specifier-seq.  By not trying to parse declarators, we can
7269      resolve the declaration/expression ambiguity more quickly.  */
7270   if (!function_definition_allowed_p
7271       && !decl_specifiers.any_specifiers_p)
7272     {
7273       cp_parser_error (parser, "expected declaration");
7274       goto done;
7275     }
7276
7277   /* If the next two tokens are both identifiers, the code is
7278      erroneous. The usual cause of this situation is code like:
7279
7280        T t;
7281
7282      where "T" should name a type -- but does not.  */
7283   if (!decl_specifiers.type
7284       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7285     {
7286       /* If parsing tentatively, we should commit; we really are
7287          looking at a declaration.  */
7288       cp_parser_commit_to_tentative_parse (parser);
7289       /* Give up.  */
7290       goto done;
7291     }
7292
7293   /* If we have seen at least one decl-specifier, and the next token
7294      is not a parenthesis, then we must be looking at a declaration.
7295      (After "int (" we might be looking at a functional cast.)  */
7296   if (decl_specifiers.any_specifiers_p
7297       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7298     cp_parser_commit_to_tentative_parse (parser);
7299
7300   /* Keep going until we hit the `;' at the end of the simple
7301      declaration.  */
7302   saw_declarator = false;
7303   while (cp_lexer_next_token_is_not (parser->lexer,
7304                                      CPP_SEMICOLON))
7305     {
7306       cp_token *token;
7307       bool function_definition_p;
7308       tree decl;
7309
7310       if (saw_declarator)
7311         {
7312           /* If we are processing next declarator, coma is expected */
7313           token = cp_lexer_peek_token (parser->lexer);
7314           gcc_assert (token->type == CPP_COMMA);
7315           cp_lexer_consume_token (parser->lexer);
7316         }
7317       else
7318         saw_declarator = true;
7319
7320       /* Parse the init-declarator.  */
7321       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7322                                         /*checks=*/NULL_TREE,
7323                                         function_definition_allowed_p,
7324                                         /*member_p=*/false,
7325                                         declares_class_or_enum,
7326                                         &function_definition_p);
7327       /* If an error occurred while parsing tentatively, exit quickly.
7328          (That usually happens when in the body of a function; each
7329          statement is treated as a declaration-statement until proven
7330          otherwise.)  */
7331       if (cp_parser_error_occurred (parser))
7332         goto done;
7333       /* Handle function definitions specially.  */
7334       if (function_definition_p)
7335         {
7336           /* If the next token is a `,', then we are probably
7337              processing something like:
7338
7339                void f() {}, *p;
7340
7341              which is erroneous.  */
7342           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7343             error ("mixing declarations and function-definitions is forbidden");
7344           /* Otherwise, we're done with the list of declarators.  */
7345           else
7346             {
7347               pop_deferring_access_checks ();
7348               return;
7349             }
7350         }
7351       /* The next token should be either a `,' or a `;'.  */
7352       token = cp_lexer_peek_token (parser->lexer);
7353       /* If it's a `,', there are more declarators to come.  */
7354       if (token->type == CPP_COMMA)
7355         /* will be consumed next time around */;
7356       /* If it's a `;', we are done.  */
7357       else if (token->type == CPP_SEMICOLON)
7358         break;
7359       /* Anything else is an error.  */
7360       else
7361         {
7362           /* If we have already issued an error message we don't need
7363              to issue another one.  */
7364           if (decl != error_mark_node
7365               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7366             cp_parser_error (parser, "expected %<,%> or %<;%>");
7367           /* Skip tokens until we reach the end of the statement.  */
7368           cp_parser_skip_to_end_of_statement (parser);
7369           /* If the next token is now a `;', consume it.  */
7370           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7371             cp_lexer_consume_token (parser->lexer);
7372           goto done;
7373         }
7374       /* After the first time around, a function-definition is not
7375          allowed -- even if it was OK at first.  For example:
7376
7377            int i, f() {}
7378
7379          is not valid.  */
7380       function_definition_allowed_p = false;
7381     }
7382
7383   /* Issue an error message if no declarators are present, and the
7384      decl-specifier-seq does not itself declare a class or
7385      enumeration.  */
7386   if (!saw_declarator)
7387     {
7388       if (cp_parser_declares_only_class_p (parser))
7389         shadow_tag (&decl_specifiers);
7390       /* Perform any deferred access checks.  */
7391       perform_deferred_access_checks ();
7392     }
7393
7394   /* Consume the `;'.  */
7395   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7396
7397  done:
7398   pop_deferring_access_checks ();
7399 }
7400
7401 /* Parse a decl-specifier-seq.
7402
7403    decl-specifier-seq:
7404      decl-specifier-seq [opt] decl-specifier
7405
7406    decl-specifier:
7407      storage-class-specifier
7408      type-specifier
7409      function-specifier
7410      friend
7411      typedef
7412
7413    GNU Extension:
7414
7415    decl-specifier:
7416      attributes
7417
7418    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7419
7420    The parser flags FLAGS is used to control type-specifier parsing.
7421
7422    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7423    flags:
7424
7425      1: one of the decl-specifiers is an elaborated-type-specifier
7426         (i.e., a type declaration)
7427      2: one of the decl-specifiers is an enum-specifier or a
7428         class-specifier (i.e., a type definition)
7429
7430    */
7431
7432 static void
7433 cp_parser_decl_specifier_seq (cp_parser* parser,
7434                               cp_parser_flags flags,
7435                               cp_decl_specifier_seq *decl_specs,
7436                               int* declares_class_or_enum)
7437 {
7438   bool constructor_possible_p = !parser->in_declarator_p;
7439
7440   /* Clear DECL_SPECS.  */
7441   clear_decl_specs (decl_specs);
7442
7443   /* Assume no class or enumeration type is declared.  */
7444   *declares_class_or_enum = 0;
7445
7446   /* Keep reading specifiers until there are no more to read.  */
7447   while (true)
7448     {
7449       bool constructor_p;
7450       bool found_decl_spec;
7451       cp_token *token;
7452
7453       /* Peek at the next token.  */
7454       token = cp_lexer_peek_token (parser->lexer);
7455       /* Handle attributes.  */
7456       if (token->keyword == RID_ATTRIBUTE)
7457         {
7458           /* Parse the attributes.  */
7459           decl_specs->attributes
7460             = chainon (decl_specs->attributes,
7461                        cp_parser_attributes_opt (parser));
7462           continue;
7463         }
7464       /* Assume we will find a decl-specifier keyword.  */
7465       found_decl_spec = true;
7466       /* If the next token is an appropriate keyword, we can simply
7467          add it to the list.  */
7468       switch (token->keyword)
7469         {
7470           /* decl-specifier:
7471                friend  */
7472         case RID_FRIEND:
7473           ++decl_specs->specs[(int) ds_friend];
7474           /* Consume the token.  */
7475           cp_lexer_consume_token (parser->lexer);
7476           break;
7477
7478           /* function-specifier:
7479                inline
7480                virtual
7481                explicit  */
7482         case RID_INLINE:
7483         case RID_VIRTUAL:
7484         case RID_EXPLICIT:
7485           cp_parser_function_specifier_opt (parser, decl_specs);
7486           break;
7487
7488           /* decl-specifier:
7489                typedef  */
7490         case RID_TYPEDEF:
7491           ++decl_specs->specs[(int) ds_typedef];
7492           /* Consume the token.  */
7493           cp_lexer_consume_token (parser->lexer);
7494           /* A constructor declarator cannot appear in a typedef.  */
7495           constructor_possible_p = false;
7496           /* The "typedef" keyword can only occur in a declaration; we
7497              may as well commit at this point.  */
7498           cp_parser_commit_to_tentative_parse (parser);
7499           break;
7500
7501           /* storage-class-specifier:
7502                auto
7503                register
7504                static
7505                extern
7506                mutable
7507
7508              GNU Extension:
7509                thread  */
7510         case RID_AUTO:
7511         case RID_REGISTER:
7512         case RID_STATIC:
7513         case RID_EXTERN:
7514         case RID_MUTABLE:
7515           /* Consume the token.  */
7516           cp_lexer_consume_token (parser->lexer);
7517           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7518           break;
7519         case RID_THREAD:
7520           /* Consume the token.  */
7521           cp_lexer_consume_token (parser->lexer);
7522           ++decl_specs->specs[(int) ds_thread];
7523           break;
7524
7525         default:
7526           /* We did not yet find a decl-specifier yet.  */
7527           found_decl_spec = false;
7528           break;
7529         }
7530
7531       /* Constructors are a special case.  The `S' in `S()' is not a
7532          decl-specifier; it is the beginning of the declarator.  */
7533       constructor_p
7534         = (!found_decl_spec
7535            && constructor_possible_p
7536            && (cp_parser_constructor_declarator_p
7537                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7538
7539       /* If we don't have a DECL_SPEC yet, then we must be looking at
7540          a type-specifier.  */
7541       if (!found_decl_spec && !constructor_p)
7542         {
7543           int decl_spec_declares_class_or_enum;
7544           bool is_cv_qualifier;
7545           tree type_spec;
7546
7547           type_spec
7548             = cp_parser_type_specifier (parser, flags,
7549                                         decl_specs,
7550                                         /*is_declaration=*/true,
7551                                         &decl_spec_declares_class_or_enum,
7552                                         &is_cv_qualifier);
7553
7554           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7555
7556           /* If this type-specifier referenced a user-defined type
7557              (a typedef, class-name, etc.), then we can't allow any
7558              more such type-specifiers henceforth.
7559
7560              [dcl.spec]
7561
7562              The longest sequence of decl-specifiers that could
7563              possibly be a type name is taken as the
7564              decl-specifier-seq of a declaration.  The sequence shall
7565              be self-consistent as described below.
7566
7567              [dcl.type]
7568
7569              As a general rule, at most one type-specifier is allowed
7570              in the complete decl-specifier-seq of a declaration.  The
7571              only exceptions are the following:
7572
7573              -- const or volatile can be combined with any other
7574                 type-specifier.
7575
7576              -- signed or unsigned can be combined with char, long,
7577                 short, or int.
7578
7579              -- ..
7580
7581              Example:
7582
7583                typedef char* Pc;
7584                void g (const int Pc);
7585
7586              Here, Pc is *not* part of the decl-specifier seq; it's
7587              the declarator.  Therefore, once we see a type-specifier
7588              (other than a cv-qualifier), we forbid any additional
7589              user-defined types.  We *do* still allow things like `int
7590              int' to be considered a decl-specifier-seq, and issue the
7591              error message later.  */
7592           if (type_spec && !is_cv_qualifier)
7593             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7594           /* A constructor declarator cannot follow a type-specifier.  */
7595           if (type_spec)
7596             {
7597               constructor_possible_p = false;
7598               found_decl_spec = true;
7599             }
7600         }
7601
7602       /* If we still do not have a DECL_SPEC, then there are no more
7603          decl-specifiers.  */
7604       if (!found_decl_spec)
7605         break;
7606
7607       decl_specs->any_specifiers_p = true;
7608       /* After we see one decl-specifier, further decl-specifiers are
7609          always optional.  */
7610       flags |= CP_PARSER_FLAGS_OPTIONAL;
7611     }
7612
7613   cp_parser_check_decl_spec (decl_specs);
7614
7615   /* Don't allow a friend specifier with a class definition.  */
7616   if (decl_specs->specs[(int) ds_friend] != 0
7617       && (*declares_class_or_enum & 2))
7618     error ("class definition may not be declared a friend");
7619 }
7620
7621 /* Parse an (optional) storage-class-specifier.
7622
7623    storage-class-specifier:
7624      auto
7625      register
7626      static
7627      extern
7628      mutable
7629
7630    GNU Extension:
7631
7632    storage-class-specifier:
7633      thread
7634
7635    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7636
7637 static tree
7638 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7639 {
7640   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7641     {
7642     case RID_AUTO:
7643     case RID_REGISTER:
7644     case RID_STATIC:
7645     case RID_EXTERN:
7646     case RID_MUTABLE:
7647     case RID_THREAD:
7648       /* Consume the token.  */
7649       return cp_lexer_consume_token (parser->lexer)->value;
7650
7651     default:
7652       return NULL_TREE;
7653     }
7654 }
7655
7656 /* Parse an (optional) function-specifier.
7657
7658    function-specifier:
7659      inline
7660      virtual
7661      explicit
7662
7663    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7664    Updates DECL_SPECS, if it is non-NULL.  */
7665
7666 static tree
7667 cp_parser_function_specifier_opt (cp_parser* parser,
7668                                   cp_decl_specifier_seq *decl_specs)
7669 {
7670   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7671     {
7672     case RID_INLINE:
7673       if (decl_specs)
7674         ++decl_specs->specs[(int) ds_inline];
7675       break;
7676
7677     case RID_VIRTUAL:
7678       if (decl_specs)
7679         ++decl_specs->specs[(int) ds_virtual];
7680       break;
7681
7682     case RID_EXPLICIT:
7683       if (decl_specs)
7684         ++decl_specs->specs[(int) ds_explicit];
7685       break;
7686
7687     default:
7688       return NULL_TREE;
7689     }
7690
7691   /* Consume the token.  */
7692   return cp_lexer_consume_token (parser->lexer)->value;
7693 }
7694
7695 /* Parse a linkage-specification.
7696
7697    linkage-specification:
7698      extern string-literal { declaration-seq [opt] }
7699      extern string-literal declaration  */
7700
7701 static void
7702 cp_parser_linkage_specification (cp_parser* parser)
7703 {
7704   tree linkage;
7705
7706   /* Look for the `extern' keyword.  */
7707   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7708
7709   /* Look for the string-literal.  */
7710   linkage = cp_parser_string_literal (parser, false, false);
7711
7712   /* Transform the literal into an identifier.  If the literal is a
7713      wide-character string, or contains embedded NULs, then we can't
7714      handle it as the user wants.  */
7715   if (strlen (TREE_STRING_POINTER (linkage))
7716       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7717     {
7718       cp_parser_error (parser, "invalid linkage-specification");
7719       /* Assume C++ linkage.  */
7720       linkage = lang_name_cplusplus;
7721     }
7722   else
7723     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7724
7725   /* We're now using the new linkage.  */
7726   push_lang_context (linkage);
7727
7728   /* If the next token is a `{', then we're using the first
7729      production.  */
7730   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7731     {
7732       /* Consume the `{' token.  */
7733       cp_lexer_consume_token (parser->lexer);
7734       /* Parse the declarations.  */
7735       cp_parser_declaration_seq_opt (parser);
7736       /* Look for the closing `}'.  */
7737       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7738     }
7739   /* Otherwise, there's just one declaration.  */
7740   else
7741     {
7742       bool saved_in_unbraced_linkage_specification_p;
7743
7744       saved_in_unbraced_linkage_specification_p
7745         = parser->in_unbraced_linkage_specification_p;
7746       parser->in_unbraced_linkage_specification_p = true;
7747       cp_parser_declaration (parser);
7748       parser->in_unbraced_linkage_specification_p
7749         = saved_in_unbraced_linkage_specification_p;
7750     }
7751
7752   /* We're done with the linkage-specification.  */
7753   pop_lang_context ();
7754 }
7755
7756 /* Special member functions [gram.special] */
7757
7758 /* Parse a conversion-function-id.
7759
7760    conversion-function-id:
7761      operator conversion-type-id
7762
7763    Returns an IDENTIFIER_NODE representing the operator.  */
7764
7765 static tree
7766 cp_parser_conversion_function_id (cp_parser* parser)
7767 {
7768   tree type;
7769   tree saved_scope;
7770   tree saved_qualifying_scope;
7771   tree saved_object_scope;
7772   tree pushed_scope = NULL_TREE;
7773
7774   /* Look for the `operator' token.  */
7775   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7776     return error_mark_node;
7777   /* When we parse the conversion-type-id, the current scope will be
7778      reset.  However, we need that information in able to look up the
7779      conversion function later, so we save it here.  */
7780   saved_scope = parser->scope;
7781   saved_qualifying_scope = parser->qualifying_scope;
7782   saved_object_scope = parser->object_scope;
7783   /* We must enter the scope of the class so that the names of
7784      entities declared within the class are available in the
7785      conversion-type-id.  For example, consider:
7786
7787        struct S {
7788          typedef int I;
7789          operator I();
7790        };
7791
7792        S::operator I() { ... }
7793
7794      In order to see that `I' is a type-name in the definition, we
7795      must be in the scope of `S'.  */
7796   if (saved_scope)
7797     pushed_scope = push_scope (saved_scope);
7798   /* Parse the conversion-type-id.  */
7799   type = cp_parser_conversion_type_id (parser);
7800   /* Leave the scope of the class, if any.  */
7801   if (pushed_scope)
7802     pop_scope (pushed_scope);
7803   /* Restore the saved scope.  */
7804   parser->scope = saved_scope;
7805   parser->qualifying_scope = saved_qualifying_scope;
7806   parser->object_scope = saved_object_scope;
7807   /* If the TYPE is invalid, indicate failure.  */
7808   if (type == error_mark_node)
7809     return error_mark_node;
7810   return mangle_conv_op_name_for_type (type);
7811 }
7812
7813 /* Parse a conversion-type-id:
7814
7815    conversion-type-id:
7816      type-specifier-seq conversion-declarator [opt]
7817
7818    Returns the TYPE specified.  */
7819
7820 static tree
7821 cp_parser_conversion_type_id (cp_parser* parser)
7822 {
7823   tree attributes;
7824   cp_decl_specifier_seq type_specifiers;
7825   cp_declarator *declarator;
7826   tree type_specified;
7827
7828   /* Parse the attributes.  */
7829   attributes = cp_parser_attributes_opt (parser);
7830   /* Parse the type-specifiers.  */
7831   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7832                                 &type_specifiers);
7833   /* If that didn't work, stop.  */
7834   if (type_specifiers.type == error_mark_node)
7835     return error_mark_node;
7836   /* Parse the conversion-declarator.  */
7837   declarator = cp_parser_conversion_declarator_opt (parser);
7838
7839   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7840                                     /*initialized=*/0, &attributes);
7841   if (attributes)
7842     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7843   return type_specified;
7844 }
7845
7846 /* Parse an (optional) conversion-declarator.
7847
7848    conversion-declarator:
7849      ptr-operator conversion-declarator [opt]
7850
7851    */
7852
7853 static cp_declarator *
7854 cp_parser_conversion_declarator_opt (cp_parser* parser)
7855 {
7856   enum tree_code code;
7857   tree class_type;
7858   cp_cv_quals cv_quals;
7859
7860   /* We don't know if there's a ptr-operator next, or not.  */
7861   cp_parser_parse_tentatively (parser);
7862   /* Try the ptr-operator.  */
7863   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7864   /* If it worked, look for more conversion-declarators.  */
7865   if (cp_parser_parse_definitely (parser))
7866     {
7867       cp_declarator *declarator;
7868
7869       /* Parse another optional declarator.  */
7870       declarator = cp_parser_conversion_declarator_opt (parser);
7871
7872       /* Create the representation of the declarator.  */
7873       if (class_type)
7874         declarator = make_ptrmem_declarator (cv_quals, class_type,
7875                                              declarator);
7876       else if (code == INDIRECT_REF)
7877         declarator = make_pointer_declarator (cv_quals, declarator);
7878       else
7879         declarator = make_reference_declarator (cv_quals, declarator);
7880
7881       return declarator;
7882    }
7883
7884   return NULL;
7885 }
7886
7887 /* Parse an (optional) ctor-initializer.
7888
7889    ctor-initializer:
7890      : mem-initializer-list
7891
7892    Returns TRUE iff the ctor-initializer was actually present.  */
7893
7894 static bool
7895 cp_parser_ctor_initializer_opt (cp_parser* parser)
7896 {
7897   /* If the next token is not a `:', then there is no
7898      ctor-initializer.  */
7899   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7900     {
7901       /* Do default initialization of any bases and members.  */
7902       if (DECL_CONSTRUCTOR_P (current_function_decl))
7903         finish_mem_initializers (NULL_TREE);
7904
7905       return false;
7906     }
7907
7908   /* Consume the `:' token.  */
7909   cp_lexer_consume_token (parser->lexer);
7910   /* And the mem-initializer-list.  */
7911   cp_parser_mem_initializer_list (parser);
7912
7913   return true;
7914 }
7915
7916 /* Parse a mem-initializer-list.
7917
7918    mem-initializer-list:
7919      mem-initializer
7920      mem-initializer , mem-initializer-list  */
7921
7922 static void
7923 cp_parser_mem_initializer_list (cp_parser* parser)
7924 {
7925   tree mem_initializer_list = NULL_TREE;
7926
7927   /* Let the semantic analysis code know that we are starting the
7928      mem-initializer-list.  */
7929   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7930     error ("only constructors take base initializers");
7931
7932   /* Loop through the list.  */
7933   while (true)
7934     {
7935       tree mem_initializer;
7936
7937       /* Parse the mem-initializer.  */
7938       mem_initializer = cp_parser_mem_initializer (parser);
7939       /* Add it to the list, unless it was erroneous.  */
7940       if (mem_initializer != error_mark_node)
7941         {
7942           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7943           mem_initializer_list = mem_initializer;
7944         }
7945       /* If the next token is not a `,', we're done.  */
7946       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7947         break;
7948       /* Consume the `,' token.  */
7949       cp_lexer_consume_token (parser->lexer);
7950     }
7951
7952   /* Perform semantic analysis.  */
7953   if (DECL_CONSTRUCTOR_P (current_function_decl))
7954     finish_mem_initializers (mem_initializer_list);
7955 }
7956
7957 /* Parse a mem-initializer.
7958
7959    mem-initializer:
7960      mem-initializer-id ( expression-list [opt] )
7961
7962    GNU extension:
7963
7964    mem-initializer:
7965      ( expression-list [opt] )
7966
7967    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7968    class) or FIELD_DECL (for a non-static data member) to initialize;
7969    the TREE_VALUE is the expression-list.  An empty initialization
7970    list is represented by void_list_node.  */
7971
7972 static tree
7973 cp_parser_mem_initializer (cp_parser* parser)
7974 {
7975   tree mem_initializer_id;
7976   tree expression_list;
7977   tree member;
7978
7979   /* Find out what is being initialized.  */
7980   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7981     {
7982       pedwarn ("anachronistic old-style base class initializer");
7983       mem_initializer_id = NULL_TREE;
7984     }
7985   else
7986     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7987   member = expand_member_init (mem_initializer_id);
7988   if (member && !DECL_P (member))
7989     in_base_initializer = 1;
7990
7991   expression_list
7992     = cp_parser_parenthesized_expression_list (parser, false,
7993                                                /*cast_p=*/false,
7994                                                /*non_constant_p=*/NULL);
7995   if (expression_list == error_mark_node)
7996     return error_mark_node;
7997   if (!expression_list)
7998     expression_list = void_type_node;
7999
8000   in_base_initializer = 0;
8001
8002   return member ? build_tree_list (member, expression_list) : error_mark_node;
8003 }
8004
8005 /* Parse a mem-initializer-id.
8006
8007    mem-initializer-id:
8008      :: [opt] nested-name-specifier [opt] class-name
8009      identifier
8010
8011    Returns a TYPE indicating the class to be initializer for the first
8012    production.  Returns an IDENTIFIER_NODE indicating the data member
8013    to be initialized for the second production.  */
8014
8015 static tree
8016 cp_parser_mem_initializer_id (cp_parser* parser)
8017 {
8018   bool global_scope_p;
8019   bool nested_name_specifier_p;
8020   bool template_p = false;
8021   tree id;
8022
8023   /* `typename' is not allowed in this context ([temp.res]).  */
8024   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8025     {
8026       error ("keyword %<typename%> not allowed in this context (a qualified "
8027              "member initializer is implicitly a type)");
8028       cp_lexer_consume_token (parser->lexer);
8029     }
8030   /* Look for the optional `::' operator.  */
8031   global_scope_p
8032     = (cp_parser_global_scope_opt (parser,
8033                                    /*current_scope_valid_p=*/false)
8034        != NULL_TREE);
8035   /* Look for the optional nested-name-specifier.  The simplest way to
8036      implement:
8037
8038        [temp.res]
8039
8040        The keyword `typename' is not permitted in a base-specifier or
8041        mem-initializer; in these contexts a qualified name that
8042        depends on a template-parameter is implicitly assumed to be a
8043        type name.
8044
8045      is to assume that we have seen the `typename' keyword at this
8046      point.  */
8047   nested_name_specifier_p
8048     = (cp_parser_nested_name_specifier_opt (parser,
8049                                             /*typename_keyword_p=*/true,
8050                                             /*check_dependency_p=*/true,
8051                                             /*type_p=*/true,
8052                                             /*is_declaration=*/true)
8053        != NULL_TREE);
8054   if (nested_name_specifier_p)
8055     template_p = cp_parser_optional_template_keyword (parser);
8056   /* If there is a `::' operator or a nested-name-specifier, then we
8057      are definitely looking for a class-name.  */
8058   if (global_scope_p || nested_name_specifier_p)
8059     return cp_parser_class_name (parser,
8060                                  /*typename_keyword_p=*/true,
8061                                  /*template_keyword_p=*/template_p,
8062                                  none_type,
8063                                  /*check_dependency_p=*/true,
8064                                  /*class_head_p=*/false,
8065                                  /*is_declaration=*/true);
8066   /* Otherwise, we could also be looking for an ordinary identifier.  */
8067   cp_parser_parse_tentatively (parser);
8068   /* Try a class-name.  */
8069   id = cp_parser_class_name (parser,
8070                              /*typename_keyword_p=*/true,
8071                              /*template_keyword_p=*/false,
8072                              none_type,
8073                              /*check_dependency_p=*/true,
8074                              /*class_head_p=*/false,
8075                              /*is_declaration=*/true);
8076   /* If we found one, we're done.  */
8077   if (cp_parser_parse_definitely (parser))
8078     return id;
8079   /* Otherwise, look for an ordinary identifier.  */
8080   return cp_parser_identifier (parser);
8081 }
8082
8083 /* Overloading [gram.over] */
8084
8085 /* Parse an operator-function-id.
8086
8087    operator-function-id:
8088      operator operator
8089
8090    Returns an IDENTIFIER_NODE for the operator which is a
8091    human-readable spelling of the identifier, e.g., `operator +'.  */
8092
8093 static tree
8094 cp_parser_operator_function_id (cp_parser* parser)
8095 {
8096   /* Look for the `operator' keyword.  */
8097   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8098     return error_mark_node;
8099   /* And then the name of the operator itself.  */
8100   return cp_parser_operator (parser);
8101 }
8102
8103 /* Parse an operator.
8104
8105    operator:
8106      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8107      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8108      || ++ -- , ->* -> () []
8109
8110    GNU Extensions:
8111
8112    operator:
8113      <? >? <?= >?=
8114
8115    Returns an IDENTIFIER_NODE for the operator which is a
8116    human-readable spelling of the identifier, e.g., `operator +'.  */
8117
8118 static tree
8119 cp_parser_operator (cp_parser* parser)
8120 {
8121   tree id = NULL_TREE;
8122   cp_token *token;
8123
8124   /* Peek at the next token.  */
8125   token = cp_lexer_peek_token (parser->lexer);
8126   /* Figure out which operator we have.  */
8127   switch (token->type)
8128     {
8129     case CPP_KEYWORD:
8130       {
8131         enum tree_code op;
8132
8133         /* The keyword should be either `new' or `delete'.  */
8134         if (token->keyword == RID_NEW)
8135           op = NEW_EXPR;
8136         else if (token->keyword == RID_DELETE)
8137           op = DELETE_EXPR;
8138         else
8139           break;
8140
8141         /* Consume the `new' or `delete' token.  */
8142         cp_lexer_consume_token (parser->lexer);
8143
8144         /* Peek at the next token.  */
8145         token = cp_lexer_peek_token (parser->lexer);
8146         /* If it's a `[' token then this is the array variant of the
8147            operator.  */
8148         if (token->type == CPP_OPEN_SQUARE)
8149           {
8150             /* Consume the `[' token.  */
8151             cp_lexer_consume_token (parser->lexer);
8152             /* Look for the `]' token.  */
8153             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8154             id = ansi_opname (op == NEW_EXPR
8155                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8156           }
8157         /* Otherwise, we have the non-array variant.  */
8158         else
8159           id = ansi_opname (op);
8160
8161         return id;
8162       }
8163
8164     case CPP_PLUS:
8165       id = ansi_opname (PLUS_EXPR);
8166       break;
8167
8168     case CPP_MINUS:
8169       id = ansi_opname (MINUS_EXPR);
8170       break;
8171
8172     case CPP_MULT:
8173       id = ansi_opname (MULT_EXPR);
8174       break;
8175
8176     case CPP_DIV:
8177       id = ansi_opname (TRUNC_DIV_EXPR);
8178       break;
8179
8180     case CPP_MOD:
8181       id = ansi_opname (TRUNC_MOD_EXPR);
8182       break;
8183
8184     case CPP_XOR:
8185       id = ansi_opname (BIT_XOR_EXPR);
8186       break;
8187
8188     case CPP_AND:
8189       id = ansi_opname (BIT_AND_EXPR);
8190       break;
8191
8192     case CPP_OR:
8193       id = ansi_opname (BIT_IOR_EXPR);
8194       break;
8195
8196     case CPP_COMPL:
8197       id = ansi_opname (BIT_NOT_EXPR);
8198       break;
8199
8200     case CPP_NOT:
8201       id = ansi_opname (TRUTH_NOT_EXPR);
8202       break;
8203
8204     case CPP_EQ:
8205       id = ansi_assopname (NOP_EXPR);
8206       break;
8207
8208     case CPP_LESS:
8209       id = ansi_opname (LT_EXPR);
8210       break;
8211
8212     case CPP_GREATER:
8213       id = ansi_opname (GT_EXPR);
8214       break;
8215
8216     case CPP_PLUS_EQ:
8217       id = ansi_assopname (PLUS_EXPR);
8218       break;
8219
8220     case CPP_MINUS_EQ:
8221       id = ansi_assopname (MINUS_EXPR);
8222       break;
8223
8224     case CPP_MULT_EQ:
8225       id = ansi_assopname (MULT_EXPR);
8226       break;
8227
8228     case CPP_DIV_EQ:
8229       id = ansi_assopname (TRUNC_DIV_EXPR);
8230       break;
8231
8232     case CPP_MOD_EQ:
8233       id = ansi_assopname (TRUNC_MOD_EXPR);
8234       break;
8235
8236     case CPP_XOR_EQ:
8237       id = ansi_assopname (BIT_XOR_EXPR);
8238       break;
8239
8240     case CPP_AND_EQ:
8241       id = ansi_assopname (BIT_AND_EXPR);
8242       break;
8243
8244     case CPP_OR_EQ:
8245       id = ansi_assopname (BIT_IOR_EXPR);
8246       break;
8247
8248     case CPP_LSHIFT:
8249       id = ansi_opname (LSHIFT_EXPR);
8250       break;
8251
8252     case CPP_RSHIFT:
8253       id = ansi_opname (RSHIFT_EXPR);
8254       break;
8255
8256     case CPP_LSHIFT_EQ:
8257       id = ansi_assopname (LSHIFT_EXPR);
8258       break;
8259
8260     case CPP_RSHIFT_EQ:
8261       id = ansi_assopname (RSHIFT_EXPR);
8262       break;
8263
8264     case CPP_EQ_EQ:
8265       id = ansi_opname (EQ_EXPR);
8266       break;
8267
8268     case CPP_NOT_EQ:
8269       id = ansi_opname (NE_EXPR);
8270       break;
8271
8272     case CPP_LESS_EQ:
8273       id = ansi_opname (LE_EXPR);
8274       break;
8275
8276     case CPP_GREATER_EQ:
8277       id = ansi_opname (GE_EXPR);
8278       break;
8279
8280     case CPP_AND_AND:
8281       id = ansi_opname (TRUTH_ANDIF_EXPR);
8282       break;
8283
8284     case CPP_OR_OR:
8285       id = ansi_opname (TRUTH_ORIF_EXPR);
8286       break;
8287
8288     case CPP_PLUS_PLUS:
8289       id = ansi_opname (POSTINCREMENT_EXPR);
8290       break;
8291
8292     case CPP_MINUS_MINUS:
8293       id = ansi_opname (PREDECREMENT_EXPR);
8294       break;
8295
8296     case CPP_COMMA:
8297       id = ansi_opname (COMPOUND_EXPR);
8298       break;
8299
8300     case CPP_DEREF_STAR:
8301       id = ansi_opname (MEMBER_REF);
8302       break;
8303
8304     case CPP_DEREF:
8305       id = ansi_opname (COMPONENT_REF);
8306       break;
8307
8308     case CPP_OPEN_PAREN:
8309       /* Consume the `('.  */
8310       cp_lexer_consume_token (parser->lexer);
8311       /* Look for the matching `)'.  */
8312       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8313       return ansi_opname (CALL_EXPR);
8314
8315     case CPP_OPEN_SQUARE:
8316       /* Consume the `['.  */
8317       cp_lexer_consume_token (parser->lexer);
8318       /* Look for the matching `]'.  */
8319       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8320       return ansi_opname (ARRAY_REF);
8321
8322       /* Extensions.  */
8323     case CPP_MIN:
8324       id = ansi_opname (MIN_EXPR);
8325       cp_parser_warn_min_max ();
8326       break;
8327
8328     case CPP_MAX:
8329       id = ansi_opname (MAX_EXPR);
8330       cp_parser_warn_min_max ();
8331       break;
8332
8333     case CPP_MIN_EQ:
8334       id = ansi_assopname (MIN_EXPR);
8335       cp_parser_warn_min_max ();
8336       break;
8337
8338     case CPP_MAX_EQ:
8339       id = ansi_assopname (MAX_EXPR);
8340       cp_parser_warn_min_max ();
8341       break;
8342
8343     default:
8344       /* Anything else is an error.  */
8345       break;
8346     }
8347
8348   /* If we have selected an identifier, we need to consume the
8349      operator token.  */
8350   if (id)
8351     cp_lexer_consume_token (parser->lexer);
8352   /* Otherwise, no valid operator name was present.  */
8353   else
8354     {
8355       cp_parser_error (parser, "expected operator");
8356       id = error_mark_node;
8357     }
8358
8359   return id;
8360 }
8361
8362 /* Parse a template-declaration.
8363
8364    template-declaration:
8365      export [opt] template < template-parameter-list > declaration
8366
8367    If MEMBER_P is TRUE, this template-declaration occurs within a
8368    class-specifier.
8369
8370    The grammar rule given by the standard isn't correct.  What
8371    is really meant is:
8372
8373    template-declaration:
8374      export [opt] template-parameter-list-seq
8375        decl-specifier-seq [opt] init-declarator [opt] ;
8376      export [opt] template-parameter-list-seq
8377        function-definition
8378
8379    template-parameter-list-seq:
8380      template-parameter-list-seq [opt]
8381      template < template-parameter-list >  */
8382
8383 static void
8384 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8385 {
8386   /* Check for `export'.  */
8387   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8388     {
8389       /* Consume the `export' token.  */
8390       cp_lexer_consume_token (parser->lexer);
8391       /* Warn that we do not support `export'.  */
8392       warning (0, "keyword %<export%> not implemented, and will be ignored");
8393     }
8394
8395   cp_parser_template_declaration_after_export (parser, member_p);
8396 }
8397
8398 /* Parse a template-parameter-list.
8399
8400    template-parameter-list:
8401      template-parameter
8402      template-parameter-list , template-parameter
8403
8404    Returns a TREE_LIST.  Each node represents a template parameter.
8405    The nodes are connected via their TREE_CHAINs.  */
8406
8407 static tree
8408 cp_parser_template_parameter_list (cp_parser* parser)
8409 {
8410   tree parameter_list = NULL_TREE;
8411
8412   while (true)
8413     {
8414       tree parameter;
8415       cp_token *token;
8416       bool is_non_type;
8417
8418       /* Parse the template-parameter.  */
8419       parameter = cp_parser_template_parameter (parser, &is_non_type);
8420       /* Add it to the list.  */
8421       if (parameter != error_mark_node)
8422         parameter_list = process_template_parm (parameter_list,
8423                                                 parameter,
8424                                                 is_non_type);
8425       /* Peek at the next token.  */
8426       token = cp_lexer_peek_token (parser->lexer);
8427       /* If it's not a `,', we're done.  */
8428       if (token->type != CPP_COMMA)
8429         break;
8430       /* Otherwise, consume the `,' token.  */
8431       cp_lexer_consume_token (parser->lexer);
8432     }
8433
8434   return parameter_list;
8435 }
8436
8437 /* Parse a template-parameter.
8438
8439    template-parameter:
8440      type-parameter
8441      parameter-declaration
8442
8443    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8444    the parameter.  The TREE_PURPOSE is the default value, if any.
8445    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8446    iff this parameter is a non-type parameter.  */
8447
8448 static tree
8449 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8450 {
8451   cp_token *token;
8452   cp_parameter_declarator *parameter_declarator;
8453   tree parm;
8454
8455   /* Assume it is a type parameter or a template parameter.  */
8456   *is_non_type = false;
8457   /* Peek at the next token.  */
8458   token = cp_lexer_peek_token (parser->lexer);
8459   /* If it is `class' or `template', we have a type-parameter.  */
8460   if (token->keyword == RID_TEMPLATE)
8461     return cp_parser_type_parameter (parser);
8462   /* If it is `class' or `typename' we do not know yet whether it is a
8463      type parameter or a non-type parameter.  Consider:
8464
8465        template <typename T, typename T::X X> ...
8466
8467      or:
8468
8469        template <class C, class D*> ...
8470
8471      Here, the first parameter is a type parameter, and the second is
8472      a non-type parameter.  We can tell by looking at the token after
8473      the identifier -- if it is a `,', `=', or `>' then we have a type
8474      parameter.  */
8475   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8476     {
8477       /* Peek at the token after `class' or `typename'.  */
8478       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8479       /* If it's an identifier, skip it.  */
8480       if (token->type == CPP_NAME)
8481         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8482       /* Now, see if the token looks like the end of a template
8483          parameter.  */
8484       if (token->type == CPP_COMMA
8485           || token->type == CPP_EQ
8486           || token->type == CPP_GREATER)
8487         return cp_parser_type_parameter (parser);
8488     }
8489
8490   /* Otherwise, it is a non-type parameter.
8491
8492      [temp.param]
8493
8494      When parsing a default template-argument for a non-type
8495      template-parameter, the first non-nested `>' is taken as the end
8496      of the template parameter-list rather than a greater-than
8497      operator.  */
8498   *is_non_type = true;
8499   parameter_declarator
8500      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8501                                         /*parenthesized_p=*/NULL);
8502   parm = grokdeclarator (parameter_declarator->declarator,
8503                          &parameter_declarator->decl_specifiers,
8504                          PARM, /*initialized=*/0,
8505                          /*attrlist=*/NULL);
8506   if (parm == error_mark_node)
8507     return error_mark_node;
8508   return build_tree_list (parameter_declarator->default_argument, parm);
8509 }
8510
8511 /* Parse a type-parameter.
8512
8513    type-parameter:
8514      class identifier [opt]
8515      class identifier [opt] = type-id
8516      typename identifier [opt]
8517      typename identifier [opt] = type-id
8518      template < template-parameter-list > class identifier [opt]
8519      template < template-parameter-list > class identifier [opt]
8520        = id-expression
8521
8522    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8523    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8524    the declaration of the parameter.  */
8525
8526 static tree
8527 cp_parser_type_parameter (cp_parser* parser)
8528 {
8529   cp_token *token;
8530   tree parameter;
8531
8532   /* Look for a keyword to tell us what kind of parameter this is.  */
8533   token = cp_parser_require (parser, CPP_KEYWORD,
8534                              "`class', `typename', or `template'");
8535   if (!token)
8536     return error_mark_node;
8537
8538   switch (token->keyword)
8539     {
8540     case RID_CLASS:
8541     case RID_TYPENAME:
8542       {
8543         tree identifier;
8544         tree default_argument;
8545
8546         /* If the next token is an identifier, then it names the
8547            parameter.  */
8548         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8549           identifier = cp_parser_identifier (parser);
8550         else
8551           identifier = NULL_TREE;
8552
8553         /* Create the parameter.  */
8554         parameter = finish_template_type_parm (class_type_node, identifier);
8555
8556         /* If the next token is an `=', we have a default argument.  */
8557         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8558           {
8559             /* Consume the `=' token.  */
8560             cp_lexer_consume_token (parser->lexer);
8561             /* Parse the default-argument.  */
8562             push_deferring_access_checks (dk_no_deferred);
8563             default_argument = cp_parser_type_id (parser);
8564             pop_deferring_access_checks ();
8565           }
8566         else
8567           default_argument = NULL_TREE;
8568
8569         /* Create the combined representation of the parameter and the
8570            default argument.  */
8571         parameter = build_tree_list (default_argument, parameter);
8572       }
8573       break;
8574
8575     case RID_TEMPLATE:
8576       {
8577         tree parameter_list;
8578         tree identifier;
8579         tree default_argument;
8580
8581         /* Look for the `<'.  */
8582         cp_parser_require (parser, CPP_LESS, "`<'");
8583         /* Parse the template-parameter-list.  */
8584         begin_template_parm_list ();
8585         parameter_list
8586           = cp_parser_template_parameter_list (parser);
8587         parameter_list = end_template_parm_list (parameter_list);
8588         /* Look for the `>'.  */
8589         cp_parser_require (parser, CPP_GREATER, "`>'");
8590         /* Look for the `class' keyword.  */
8591         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8592         /* If the next token is an `=', then there is a
8593            default-argument.  If the next token is a `>', we are at
8594            the end of the parameter-list.  If the next token is a `,',
8595            then we are at the end of this parameter.  */
8596         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8597             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8598             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8599           {
8600             identifier = cp_parser_identifier (parser);
8601             /* Treat invalid names as if the parameter were nameless.  */
8602             if (identifier == error_mark_node)
8603               identifier = NULL_TREE;
8604           }
8605         else
8606           identifier = NULL_TREE;
8607
8608         /* Create the template parameter.  */
8609         parameter = finish_template_template_parm (class_type_node,
8610                                                    identifier);
8611
8612         /* If the next token is an `=', then there is a
8613            default-argument.  */
8614         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8615           {
8616             bool is_template;
8617
8618             /* Consume the `='.  */
8619             cp_lexer_consume_token (parser->lexer);
8620             /* Parse the id-expression.  */
8621             push_deferring_access_checks (dk_no_deferred);
8622             default_argument
8623               = cp_parser_id_expression (parser,
8624                                          /*template_keyword_p=*/false,
8625                                          /*check_dependency_p=*/true,
8626                                          /*template_p=*/&is_template,
8627                                          /*declarator_p=*/false);
8628             if (TREE_CODE (default_argument) == TYPE_DECL)
8629               /* If the id-expression was a template-id that refers to
8630                  a template-class, we already have the declaration here,
8631                  so no further lookup is needed.  */
8632                  ;
8633             else
8634               /* Look up the name.  */
8635               default_argument
8636                 = cp_parser_lookup_name (parser, default_argument,
8637                                          none_type,
8638                                          /*is_template=*/is_template,
8639                                          /*is_namespace=*/false,
8640                                          /*check_dependency=*/true,
8641                                          /*ambiguous_decls=*/NULL);
8642             /* See if the default argument is valid.  */
8643             default_argument
8644               = check_template_template_default_arg (default_argument);
8645             pop_deferring_access_checks ();
8646           }
8647         else
8648           default_argument = NULL_TREE;
8649
8650         /* Create the combined representation of the parameter and the
8651            default argument.  */
8652         parameter = build_tree_list (default_argument, parameter);
8653       }
8654       break;
8655
8656     default:
8657       gcc_unreachable ();
8658       break;
8659     }
8660
8661   return parameter;
8662 }
8663
8664 /* Parse a template-id.
8665
8666    template-id:
8667      template-name < template-argument-list [opt] >
8668
8669    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8670    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8671    returned.  Otherwise, if the template-name names a function, or set
8672    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8673    names a class, returns a TYPE_DECL for the specialization.
8674
8675    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8676    uninstantiated templates.  */
8677
8678 static tree
8679 cp_parser_template_id (cp_parser *parser,
8680                        bool template_keyword_p,
8681                        bool check_dependency_p,
8682                        bool is_declaration)
8683 {
8684   tree template;
8685   tree arguments;
8686   tree template_id;
8687   cp_token_position start_of_id = 0;
8688   tree access_check = NULL_TREE;
8689   cp_token *next_token, *next_token_2;
8690   bool is_identifier;
8691
8692   /* If the next token corresponds to a template-id, there is no need
8693      to reparse it.  */
8694   next_token = cp_lexer_peek_token (parser->lexer);
8695   if (next_token->type == CPP_TEMPLATE_ID)
8696     {
8697       tree value;
8698       tree check;
8699
8700       /* Get the stored value.  */
8701       value = cp_lexer_consume_token (parser->lexer)->value;
8702       /* Perform any access checks that were deferred.  */
8703       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8704         perform_or_defer_access_check (TREE_PURPOSE (check),
8705                                        TREE_VALUE (check));
8706       /* Return the stored value.  */
8707       return TREE_VALUE (value);
8708     }
8709
8710   /* Avoid performing name lookup if there is no possibility of
8711      finding a template-id.  */
8712   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8713       || (next_token->type == CPP_NAME
8714           && !cp_parser_nth_token_starts_template_argument_list_p
8715                (parser, 2)))
8716     {
8717       cp_parser_error (parser, "expected template-id");
8718       return error_mark_node;
8719     }
8720
8721   /* Remember where the template-id starts.  */
8722   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8723     start_of_id = cp_lexer_token_position (parser->lexer, false);
8724
8725   push_deferring_access_checks (dk_deferred);
8726
8727   /* Parse the template-name.  */
8728   is_identifier = false;
8729   template = cp_parser_template_name (parser, template_keyword_p,
8730                                       check_dependency_p,
8731                                       is_declaration,
8732                                       &is_identifier);
8733   if (template == error_mark_node || is_identifier)
8734     {
8735       pop_deferring_access_checks ();
8736       return template;
8737     }
8738
8739   /* If we find the sequence `[:' after a template-name, it's probably
8740      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8741      parse correctly the argument list.  */
8742   next_token = cp_lexer_peek_token (parser->lexer);
8743   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8744   if (next_token->type == CPP_OPEN_SQUARE
8745       && next_token->flags & DIGRAPH
8746       && next_token_2->type == CPP_COLON
8747       && !(next_token_2->flags & PREV_WHITE))
8748     {
8749       cp_parser_parse_tentatively (parser);
8750       /* Change `:' into `::'.  */
8751       next_token_2->type = CPP_SCOPE;
8752       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8753          CPP_LESS.  */
8754       cp_lexer_consume_token (parser->lexer);
8755       /* Parse the arguments.  */
8756       arguments = cp_parser_enclosed_template_argument_list (parser);
8757       if (!cp_parser_parse_definitely (parser))
8758         {
8759           /* If we couldn't parse an argument list, then we revert our changes
8760              and return simply an error. Maybe this is not a template-id
8761              after all.  */
8762           next_token_2->type = CPP_COLON;
8763           cp_parser_error (parser, "expected %<<%>");
8764           pop_deferring_access_checks ();
8765           return error_mark_node;
8766         }
8767       /* Otherwise, emit an error about the invalid digraph, but continue
8768          parsing because we got our argument list.  */
8769       pedwarn ("%<<::%> cannot begin a template-argument list");
8770       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8771               "between %<<%> and %<::%>");
8772       if (!flag_permissive)
8773         {
8774           static bool hint;
8775           if (!hint)
8776             {
8777               inform ("(if you use -fpermissive G++ will accept your code)");
8778               hint = true;
8779             }
8780         }
8781     }
8782   else
8783     {
8784       /* Look for the `<' that starts the template-argument-list.  */
8785       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8786         {
8787           pop_deferring_access_checks ();
8788           return error_mark_node;
8789         }
8790       /* Parse the arguments.  */
8791       arguments = cp_parser_enclosed_template_argument_list (parser);
8792     }
8793
8794   /* Build a representation of the specialization.  */
8795   if (TREE_CODE (template) == IDENTIFIER_NODE)
8796     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8797   else if (DECL_CLASS_TEMPLATE_P (template)
8798            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8799     {
8800       bool entering_scope;
8801       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8802          template (rather than some instantiation thereof) only if
8803          is not nested within some other construct.  For example, in
8804          "template <typename T> void f(T) { A<T>::", A<T> is just an
8805          instantiation of A.  */
8806       entering_scope = (template_parm_scope_p ()
8807                         && cp_lexer_next_token_is (parser->lexer,
8808                                                    CPP_SCOPE));
8809       template_id
8810         = finish_template_type (template, arguments, entering_scope);
8811     }
8812   else
8813     {
8814       /* If it's not a class-template or a template-template, it should be
8815          a function-template.  */
8816       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8817                    || TREE_CODE (template) == OVERLOAD
8818                    || BASELINK_P (template)));
8819
8820       template_id = lookup_template_function (template, arguments);
8821     }
8822
8823   /* Retrieve any deferred checks.  Do not pop this access checks yet
8824      so the memory will not be reclaimed during token replacing below.  */
8825   access_check = get_deferred_access_checks ();
8826
8827   /* If parsing tentatively, replace the sequence of tokens that makes
8828      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8829      should we re-parse the token stream, we will not have to repeat
8830      the effort required to do the parse, nor will we issue duplicate
8831      error messages about problems during instantiation of the
8832      template.  */
8833   if (start_of_id)
8834     {
8835       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8836
8837       /* Reset the contents of the START_OF_ID token.  */
8838       token->type = CPP_TEMPLATE_ID;
8839       token->value = build_tree_list (access_check, template_id);
8840       token->keyword = RID_MAX;
8841
8842       /* Purge all subsequent tokens.  */
8843       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8844
8845       /* ??? Can we actually assume that, if template_id ==
8846          error_mark_node, we will have issued a diagnostic to the
8847          user, as opposed to simply marking the tentative parse as
8848          failed?  */
8849       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8850         error ("parse error in template argument list");
8851     }
8852
8853   pop_deferring_access_checks ();
8854   return template_id;
8855 }
8856
8857 /* Parse a template-name.
8858
8859    template-name:
8860      identifier
8861
8862    The standard should actually say:
8863
8864    template-name:
8865      identifier
8866      operator-function-id
8867
8868    A defect report has been filed about this issue.
8869
8870    A conversion-function-id cannot be a template name because they cannot
8871    be part of a template-id. In fact, looking at this code:
8872
8873    a.operator K<int>()
8874
8875    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8876    It is impossible to call a templated conversion-function-id with an
8877    explicit argument list, since the only allowed template parameter is
8878    the type to which it is converting.
8879
8880    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8881    `template' keyword, in a construction like:
8882
8883      T::template f<3>()
8884
8885    In that case `f' is taken to be a template-name, even though there
8886    is no way of knowing for sure.
8887
8888    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8889    name refers to a set of overloaded functions, at least one of which
8890    is a template, or an IDENTIFIER_NODE with the name of the template,
8891    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8892    names are looked up inside uninstantiated templates.  */
8893
8894 static tree
8895 cp_parser_template_name (cp_parser* parser,
8896                          bool template_keyword_p,
8897                          bool check_dependency_p,
8898                          bool is_declaration,
8899                          bool *is_identifier)
8900 {
8901   tree identifier;
8902   tree decl;
8903   tree fns;
8904
8905   /* If the next token is `operator', then we have either an
8906      operator-function-id or a conversion-function-id.  */
8907   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8908     {
8909       /* We don't know whether we're looking at an
8910          operator-function-id or a conversion-function-id.  */
8911       cp_parser_parse_tentatively (parser);
8912       /* Try an operator-function-id.  */
8913       identifier = cp_parser_operator_function_id (parser);
8914       /* If that didn't work, try a conversion-function-id.  */
8915       if (!cp_parser_parse_definitely (parser))
8916         {
8917           cp_parser_error (parser, "expected template-name");
8918           return error_mark_node;
8919         }
8920     }
8921   /* Look for the identifier.  */
8922   else
8923     identifier = cp_parser_identifier (parser);
8924
8925   /* If we didn't find an identifier, we don't have a template-id.  */
8926   if (identifier == error_mark_node)
8927     return error_mark_node;
8928
8929   /* If the name immediately followed the `template' keyword, then it
8930      is a template-name.  However, if the next token is not `<', then
8931      we do not treat it as a template-name, since it is not being used
8932      as part of a template-id.  This enables us to handle constructs
8933      like:
8934
8935        template <typename T> struct S { S(); };
8936        template <typename T> S<T>::S();
8937
8938      correctly.  We would treat `S' as a template -- if it were `S<T>'
8939      -- but we do not if there is no `<'.  */
8940
8941   if (processing_template_decl
8942       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8943     {
8944       /* In a declaration, in a dependent context, we pretend that the
8945          "template" keyword was present in order to improve error
8946          recovery.  For example, given:
8947
8948            template <typename T> void f(T::X<int>);
8949
8950          we want to treat "X<int>" as a template-id.  */
8951       if (is_declaration
8952           && !template_keyword_p
8953           && parser->scope && TYPE_P (parser->scope)
8954           && check_dependency_p
8955           && dependent_type_p (parser->scope)
8956           /* Do not do this for dtors (or ctors), since they never
8957              need the template keyword before their name.  */
8958           && !constructor_name_p (identifier, parser->scope))
8959         {
8960           cp_token_position start = 0;
8961
8962           /* Explain what went wrong.  */
8963           error ("non-template %qD used as template", identifier);
8964           inform ("use %<%T::template %D%> to indicate that it is a template",
8965                   parser->scope, identifier);
8966           /* If parsing tentatively, find the location of the "<" token.  */
8967           if (cp_parser_simulate_error (parser))
8968             start = cp_lexer_token_position (parser->lexer, true);
8969           /* Parse the template arguments so that we can issue error
8970              messages about them.  */
8971           cp_lexer_consume_token (parser->lexer);
8972           cp_parser_enclosed_template_argument_list (parser);
8973           /* Skip tokens until we find a good place from which to
8974              continue parsing.  */
8975           cp_parser_skip_to_closing_parenthesis (parser,
8976                                                  /*recovering=*/true,
8977                                                  /*or_comma=*/true,
8978                                                  /*consume_paren=*/false);
8979           /* If parsing tentatively, permanently remove the
8980              template argument list.  That will prevent duplicate
8981              error messages from being issued about the missing
8982              "template" keyword.  */
8983           if (start)
8984             cp_lexer_purge_tokens_after (parser->lexer, start);
8985           if (is_identifier)
8986             *is_identifier = true;
8987           return identifier;
8988         }
8989
8990       /* If the "template" keyword is present, then there is generally
8991          no point in doing name-lookup, so we just return IDENTIFIER.
8992          But, if the qualifying scope is non-dependent then we can
8993          (and must) do name-lookup normally.  */
8994       if (template_keyword_p
8995           && (!parser->scope
8996               || (TYPE_P (parser->scope)
8997                   && dependent_type_p (parser->scope))))
8998         return identifier;
8999     }
9000
9001   /* Look up the name.  */
9002   decl = cp_parser_lookup_name (parser, identifier,
9003                                 none_type,
9004                                 /*is_template=*/false,
9005                                 /*is_namespace=*/false,
9006                                 check_dependency_p,
9007                                 /*ambiguous_decls=*/NULL);
9008   decl = maybe_get_template_decl_from_type_decl (decl);
9009
9010   /* If DECL is a template, then the name was a template-name.  */
9011   if (TREE_CODE (decl) == TEMPLATE_DECL)
9012     ;
9013   else
9014     {
9015       tree fn = NULL_TREE;
9016
9017       /* The standard does not explicitly indicate whether a name that
9018          names a set of overloaded declarations, some of which are
9019          templates, is a template-name.  However, such a name should
9020          be a template-name; otherwise, there is no way to form a
9021          template-id for the overloaded templates.  */
9022       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9023       if (TREE_CODE (fns) == OVERLOAD)
9024         for (fn = fns; fn; fn = OVL_NEXT (fn))
9025           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9026             break;
9027
9028       if (!fn)
9029         {
9030           /* The name does not name a template.  */
9031           cp_parser_error (parser, "expected template-name");
9032           return error_mark_node;
9033         }
9034     }
9035
9036   /* If DECL is dependent, and refers to a function, then just return
9037      its name; we will look it up again during template instantiation.  */
9038   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9039     {
9040       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9041       if (TYPE_P (scope) && dependent_type_p (scope))
9042         return identifier;
9043     }
9044
9045   return decl;
9046 }
9047
9048 /* Parse a template-argument-list.
9049
9050    template-argument-list:
9051      template-argument
9052      template-argument-list , template-argument
9053
9054    Returns a TREE_VEC containing the arguments.  */
9055
9056 static tree
9057 cp_parser_template_argument_list (cp_parser* parser)
9058 {
9059   tree fixed_args[10];
9060   unsigned n_args = 0;
9061   unsigned alloced = 10;
9062   tree *arg_ary = fixed_args;
9063   tree vec;
9064   bool saved_in_template_argument_list_p;
9065   bool saved_ice_p;
9066   bool saved_non_ice_p;
9067
9068   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9069   parser->in_template_argument_list_p = true;
9070   /* Even if the template-id appears in an integral
9071      constant-expression, the contents of the argument list do 
9072      not.  */ 
9073   saved_ice_p = parser->integral_constant_expression_p;
9074   parser->integral_constant_expression_p = false;
9075   saved_non_ice_p = parser->non_integral_constant_expression_p;
9076   parser->non_integral_constant_expression_p = false;
9077   /* Parse the arguments.  */
9078   do
9079     {
9080       tree argument;
9081
9082       if (n_args)
9083         /* Consume the comma.  */
9084         cp_lexer_consume_token (parser->lexer);
9085
9086       /* Parse the template-argument.  */
9087       argument = cp_parser_template_argument (parser);
9088       if (n_args == alloced)
9089         {
9090           alloced *= 2;
9091
9092           if (arg_ary == fixed_args)
9093             {
9094               arg_ary = xmalloc (sizeof (tree) * alloced);
9095               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9096             }
9097           else
9098             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
9099         }
9100       arg_ary[n_args++] = argument;
9101     }
9102   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9103
9104   vec = make_tree_vec (n_args);
9105
9106   while (n_args--)
9107     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9108
9109   if (arg_ary != fixed_args)
9110     free (arg_ary);
9111   parser->non_integral_constant_expression_p = saved_non_ice_p;
9112   parser->integral_constant_expression_p = saved_ice_p;
9113   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9114   return vec;
9115 }
9116
9117 /* Parse a template-argument.
9118
9119    template-argument:
9120      assignment-expression
9121      type-id
9122      id-expression
9123
9124    The representation is that of an assignment-expression, type-id, or
9125    id-expression -- except that the qualified id-expression is
9126    evaluated, so that the value returned is either a DECL or an
9127    OVERLOAD.
9128
9129    Although the standard says "assignment-expression", it forbids
9130    throw-expressions or assignments in the template argument.
9131    Therefore, we use "conditional-expression" instead.  */
9132
9133 static tree
9134 cp_parser_template_argument (cp_parser* parser)
9135 {
9136   tree argument;
9137   bool template_p;
9138   bool address_p;
9139   bool maybe_type_id = false;
9140   cp_token *token;
9141   cp_id_kind idk;
9142
9143   /* There's really no way to know what we're looking at, so we just
9144      try each alternative in order.
9145
9146        [temp.arg]
9147
9148        In a template-argument, an ambiguity between a type-id and an
9149        expression is resolved to a type-id, regardless of the form of
9150        the corresponding template-parameter.
9151
9152      Therefore, we try a type-id first.  */
9153   cp_parser_parse_tentatively (parser);
9154   argument = cp_parser_type_id (parser);
9155   /* If there was no error parsing the type-id but the next token is a '>>',
9156      we probably found a typo for '> >'. But there are type-id which are
9157      also valid expressions. For instance:
9158
9159      struct X { int operator >> (int); };
9160      template <int V> struct Foo {};
9161      Foo<X () >> 5> r;
9162
9163      Here 'X()' is a valid type-id of a function type, but the user just
9164      wanted to write the expression "X() >> 5". Thus, we remember that we
9165      found a valid type-id, but we still try to parse the argument as an
9166      expression to see what happens.  */
9167   if (!cp_parser_error_occurred (parser)
9168       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9169     {
9170       maybe_type_id = true;
9171       cp_parser_abort_tentative_parse (parser);
9172     }
9173   else
9174     {
9175       /* If the next token isn't a `,' or a `>', then this argument wasn't
9176       really finished. This means that the argument is not a valid
9177       type-id.  */
9178       if (!cp_parser_next_token_ends_template_argument_p (parser))
9179         cp_parser_error (parser, "expected template-argument");
9180       /* If that worked, we're done.  */
9181       if (cp_parser_parse_definitely (parser))
9182         return argument;
9183     }
9184   /* We're still not sure what the argument will be.  */
9185   cp_parser_parse_tentatively (parser);
9186   /* Try a template.  */
9187   argument = cp_parser_id_expression (parser,
9188                                       /*template_keyword_p=*/false,
9189                                       /*check_dependency_p=*/true,
9190                                       &template_p,
9191                                       /*declarator_p=*/false);
9192   /* If the next token isn't a `,' or a `>', then this argument wasn't
9193      really finished.  */
9194   if (!cp_parser_next_token_ends_template_argument_p (parser))
9195     cp_parser_error (parser, "expected template-argument");
9196   if (!cp_parser_error_occurred (parser))
9197     {
9198       /* Figure out what is being referred to.  If the id-expression
9199          was for a class template specialization, then we will have a
9200          TYPE_DECL at this point.  There is no need to do name lookup
9201          at this point in that case.  */
9202       if (TREE_CODE (argument) != TYPE_DECL)
9203         argument = cp_parser_lookup_name (parser, argument,
9204                                           none_type,
9205                                           /*is_template=*/template_p,
9206                                           /*is_namespace=*/false,
9207                                           /*check_dependency=*/true,
9208                                           /*ambiguous_decls=*/NULL);
9209       if (TREE_CODE (argument) != TEMPLATE_DECL
9210           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9211         cp_parser_error (parser, "expected template-name");
9212     }
9213   if (cp_parser_parse_definitely (parser))
9214     return argument;
9215   /* It must be a non-type argument.  There permitted cases are given
9216      in [temp.arg.nontype]:
9217
9218      -- an integral constant-expression of integral or enumeration
9219         type; or
9220
9221      -- the name of a non-type template-parameter; or
9222
9223      -- the name of an object or function with external linkage...
9224
9225      -- the address of an object or function with external linkage...
9226
9227      -- a pointer to member...  */
9228   /* Look for a non-type template parameter.  */
9229   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9230     {
9231       cp_parser_parse_tentatively (parser);
9232       argument = cp_parser_primary_expression (parser,
9233                                                /*adress_p=*/false,
9234                                                /*cast_p=*/false,
9235                                                /*template_arg_p=*/true,
9236                                                &idk);
9237       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9238           || !cp_parser_next_token_ends_template_argument_p (parser))
9239         cp_parser_simulate_error (parser);
9240       if (cp_parser_parse_definitely (parser))
9241         return argument;
9242     }
9243
9244   /* If the next token is "&", the argument must be the address of an
9245      object or function with external linkage.  */
9246   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9247   if (address_p)
9248     cp_lexer_consume_token (parser->lexer);
9249   /* See if we might have an id-expression.  */
9250   token = cp_lexer_peek_token (parser->lexer);
9251   if (token->type == CPP_NAME
9252       || token->keyword == RID_OPERATOR
9253       || token->type == CPP_SCOPE
9254       || token->type == CPP_TEMPLATE_ID
9255       || token->type == CPP_NESTED_NAME_SPECIFIER)
9256     {
9257       cp_parser_parse_tentatively (parser);
9258       argument = cp_parser_primary_expression (parser,
9259                                                address_p,
9260                                                /*cast_p=*/false,
9261                                                /*template_arg_p=*/true,
9262                                                &idk);
9263       if (cp_parser_error_occurred (parser)
9264           || !cp_parser_next_token_ends_template_argument_p (parser))
9265         cp_parser_abort_tentative_parse (parser);
9266       else
9267         {
9268           if (TREE_CODE (argument) == INDIRECT_REF)
9269             {
9270               gcc_assert (REFERENCE_REF_P (argument));
9271               argument = TREE_OPERAND (argument, 0);
9272             }
9273
9274           if (TREE_CODE (argument) == BASELINK)
9275             /* We don't need the information about what class was used
9276                to name the overloaded functions.  */  
9277             argument = BASELINK_FUNCTIONS (argument);
9278
9279           if (TREE_CODE (argument) == VAR_DECL)
9280             {
9281               /* A variable without external linkage might still be a
9282                  valid constant-expression, so no error is issued here
9283                  if the external-linkage check fails.  */
9284               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9285                 cp_parser_simulate_error (parser);
9286             }
9287           else if (is_overloaded_fn (argument))
9288             /* All overloaded functions are allowed; if the external
9289                linkage test does not pass, an error will be issued
9290                later.  */
9291             ;
9292           else if (address_p
9293                    && (TREE_CODE (argument) == OFFSET_REF
9294                        || TREE_CODE (argument) == SCOPE_REF))
9295             /* A pointer-to-member.  */
9296             ;
9297           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9298             ;
9299           else
9300             cp_parser_simulate_error (parser);
9301
9302           if (cp_parser_parse_definitely (parser))
9303             {
9304               if (address_p)
9305                 argument = build_x_unary_op (ADDR_EXPR, argument);
9306               return argument;
9307             }
9308         }
9309     }
9310   /* If the argument started with "&", there are no other valid
9311      alternatives at this point.  */
9312   if (address_p)
9313     {
9314       cp_parser_error (parser, "invalid non-type template argument");
9315       return error_mark_node;
9316     }
9317
9318   /* If the argument wasn't successfully parsed as a type-id followed
9319      by '>>', the argument can only be a constant expression now.
9320      Otherwise, we try parsing the constant-expression tentatively,
9321      because the argument could really be a type-id.  */
9322   if (maybe_type_id)
9323     cp_parser_parse_tentatively (parser);
9324   argument = cp_parser_constant_expression (parser,
9325                                             /*allow_non_constant_p=*/false,
9326                                             /*non_constant_p=*/NULL);
9327   argument = fold_non_dependent_expr (argument);
9328   if (!maybe_type_id)
9329     return argument;
9330   if (!cp_parser_next_token_ends_template_argument_p (parser))
9331     cp_parser_error (parser, "expected template-argument");
9332   if (cp_parser_parse_definitely (parser))
9333     return argument;
9334   /* We did our best to parse the argument as a non type-id, but that
9335      was the only alternative that matched (albeit with a '>' after
9336      it). We can assume it's just a typo from the user, and a
9337      diagnostic will then be issued.  */
9338   return cp_parser_type_id (parser);
9339 }
9340
9341 /* Parse an explicit-instantiation.
9342
9343    explicit-instantiation:
9344      template declaration
9345
9346    Although the standard says `declaration', what it really means is:
9347
9348    explicit-instantiation:
9349      template decl-specifier-seq [opt] declarator [opt] ;
9350
9351    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9352    supposed to be allowed.  A defect report has been filed about this
9353    issue.
9354
9355    GNU Extension:
9356
9357    explicit-instantiation:
9358      storage-class-specifier template
9359        decl-specifier-seq [opt] declarator [opt] ;
9360      function-specifier template
9361        decl-specifier-seq [opt] declarator [opt] ;  */
9362
9363 static void
9364 cp_parser_explicit_instantiation (cp_parser* parser)
9365 {
9366   int declares_class_or_enum;
9367   cp_decl_specifier_seq decl_specifiers;
9368   tree extension_specifier = NULL_TREE;
9369
9370   /* Look for an (optional) storage-class-specifier or
9371      function-specifier.  */
9372   if (cp_parser_allow_gnu_extensions_p (parser))
9373     {
9374       extension_specifier
9375         = cp_parser_storage_class_specifier_opt (parser);
9376       if (!extension_specifier)
9377         extension_specifier
9378           = cp_parser_function_specifier_opt (parser,
9379                                               /*decl_specs=*/NULL);
9380     }
9381
9382   /* Look for the `template' keyword.  */
9383   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9384   /* Let the front end know that we are processing an explicit
9385      instantiation.  */
9386   begin_explicit_instantiation ();
9387   /* [temp.explicit] says that we are supposed to ignore access
9388      control while processing explicit instantiation directives.  */
9389   push_deferring_access_checks (dk_no_check);
9390   /* Parse a decl-specifier-seq.  */
9391   cp_parser_decl_specifier_seq (parser,
9392                                 CP_PARSER_FLAGS_OPTIONAL,
9393                                 &decl_specifiers,
9394                                 &declares_class_or_enum);
9395   /* If there was exactly one decl-specifier, and it declared a class,
9396      and there's no declarator, then we have an explicit type
9397      instantiation.  */
9398   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9399     {
9400       tree type;
9401
9402       type = check_tag_decl (&decl_specifiers);
9403       /* Turn access control back on for names used during
9404          template instantiation.  */
9405       pop_deferring_access_checks ();
9406       if (type)
9407         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9408     }
9409   else
9410     {
9411       cp_declarator *declarator;
9412       tree decl;
9413
9414       /* Parse the declarator.  */
9415       declarator
9416         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9417                                 /*ctor_dtor_or_conv_p=*/NULL,
9418                                 /*parenthesized_p=*/NULL,
9419                                 /*member_p=*/false);
9420       if (declares_class_or_enum & 2)
9421         cp_parser_check_for_definition_in_return_type (declarator,
9422                                                        decl_specifiers.type);
9423       if (declarator != cp_error_declarator)
9424         {
9425           decl = grokdeclarator (declarator, &decl_specifiers,
9426                                  NORMAL, 0, NULL);
9427           /* Turn access control back on for names used during
9428              template instantiation.  */
9429           pop_deferring_access_checks ();
9430           /* Do the explicit instantiation.  */
9431           do_decl_instantiation (decl, extension_specifier);
9432         }
9433       else
9434         {
9435           pop_deferring_access_checks ();
9436           /* Skip the body of the explicit instantiation.  */
9437           cp_parser_skip_to_end_of_statement (parser);
9438         }
9439     }
9440   /* We're done with the instantiation.  */
9441   end_explicit_instantiation ();
9442
9443   cp_parser_consume_semicolon_at_end_of_statement (parser);
9444 }
9445
9446 /* Parse an explicit-specialization.
9447
9448    explicit-specialization:
9449      template < > declaration
9450
9451    Although the standard says `declaration', what it really means is:
9452
9453    explicit-specialization:
9454      template <> decl-specifier [opt] init-declarator [opt] ;
9455      template <> function-definition
9456      template <> explicit-specialization
9457      template <> template-declaration  */
9458
9459 static void
9460 cp_parser_explicit_specialization (cp_parser* parser)
9461 {
9462   bool need_lang_pop;
9463   /* Look for the `template' keyword.  */
9464   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9465   /* Look for the `<'.  */
9466   cp_parser_require (parser, CPP_LESS, "`<'");
9467   /* Look for the `>'.  */
9468   cp_parser_require (parser, CPP_GREATER, "`>'");
9469   /* We have processed another parameter list.  */
9470   ++parser->num_template_parameter_lists;
9471   /* [temp]
9472    
9473      A template ... explicit specialization ... shall not have C
9474      linkage.  */ 
9475   if (current_lang_name == lang_name_c)
9476     {
9477       error ("template specialization with C linkage");
9478       /* Give it C++ linkage to avoid confusing other parts of the
9479          front end.  */
9480       push_lang_context (lang_name_cplusplus);
9481       need_lang_pop = true;
9482     }
9483   else
9484     need_lang_pop = false;
9485   /* Let the front end know that we are beginning a specialization.  */
9486   begin_specialization ();
9487   /* If the next keyword is `template', we need to figure out whether
9488      or not we're looking a template-declaration.  */
9489   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9490     {
9491       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9492           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9493         cp_parser_template_declaration_after_export (parser,
9494                                                      /*member_p=*/false);
9495       else
9496         cp_parser_explicit_specialization (parser);
9497     }
9498   else
9499     /* Parse the dependent declaration.  */
9500     cp_parser_single_declaration (parser,
9501                                   /*checks=*/NULL_TREE,
9502                                   /*member_p=*/false,
9503                                   /*friend_p=*/NULL);
9504   /* We're done with the specialization.  */
9505   end_specialization ();
9506   /* For the erroneous case of a template with C linkage, we pushed an
9507      implicit C++ linkage scope; exit that scope now.  */
9508   if (need_lang_pop)
9509     pop_lang_context ();
9510   /* We're done with this parameter list.  */
9511   --parser->num_template_parameter_lists;
9512 }
9513
9514 /* Parse a type-specifier.
9515
9516    type-specifier:
9517      simple-type-specifier
9518      class-specifier
9519      enum-specifier
9520      elaborated-type-specifier
9521      cv-qualifier
9522
9523    GNU Extension:
9524
9525    type-specifier:
9526      __complex__
9527
9528    Returns a representation of the type-specifier.  For a
9529    class-specifier, enum-specifier, or elaborated-type-specifier, a
9530    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9531
9532    The parser flags FLAGS is used to control type-specifier parsing.
9533
9534    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9535    in a decl-specifier-seq.
9536
9537    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9538    class-specifier, enum-specifier, or elaborated-type-specifier, then
9539    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9540    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9541    zero.
9542
9543    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9544    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9545    is set to FALSE.  */
9546
9547 static tree
9548 cp_parser_type_specifier (cp_parser* parser,
9549                           cp_parser_flags flags,
9550                           cp_decl_specifier_seq *decl_specs,
9551                           bool is_declaration,
9552                           int* declares_class_or_enum,
9553                           bool* is_cv_qualifier)
9554 {
9555   tree type_spec = NULL_TREE;
9556   cp_token *token;
9557   enum rid keyword;
9558   cp_decl_spec ds = ds_last;
9559
9560   /* Assume this type-specifier does not declare a new type.  */
9561   if (declares_class_or_enum)
9562     *declares_class_or_enum = 0;
9563   /* And that it does not specify a cv-qualifier.  */
9564   if (is_cv_qualifier)
9565     *is_cv_qualifier = false;
9566   /* Peek at the next token.  */
9567   token = cp_lexer_peek_token (parser->lexer);
9568
9569   /* If we're looking at a keyword, we can use that to guide the
9570      production we choose.  */
9571   keyword = token->keyword;
9572   switch (keyword)
9573     {
9574     case RID_ENUM:
9575       /* 'enum' [identifier] '{' introduces an enum-specifier;
9576          'enum' <anything else> introduces an elaborated-type-specifier.  */
9577       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9578           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9579               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9580                  == CPP_OPEN_BRACE))
9581         {
9582           if (parser->num_template_parameter_lists)
9583             {
9584               error ("template declaration of %qs", "enum");
9585               cp_parser_skip_to_end_of_block_or_statement (parser);
9586               type_spec = error_mark_node;
9587             }
9588           else
9589             type_spec = cp_parser_enum_specifier (parser);
9590
9591           if (declares_class_or_enum)
9592             *declares_class_or_enum = 2;
9593           if (decl_specs)
9594             cp_parser_set_decl_spec_type (decl_specs,
9595                                           type_spec,
9596                                           /*user_defined_p=*/true);
9597           return type_spec;
9598         }
9599       else
9600         goto elaborated_type_specifier;
9601
9602       /* Any of these indicate either a class-specifier, or an
9603          elaborated-type-specifier.  */
9604     case RID_CLASS:
9605     case RID_STRUCT:
9606     case RID_UNION:
9607       /* Parse tentatively so that we can back up if we don't find a
9608          class-specifier.  */
9609       cp_parser_parse_tentatively (parser);
9610       /* Look for the class-specifier.  */
9611       type_spec = cp_parser_class_specifier (parser);
9612       /* If that worked, we're done.  */
9613       if (cp_parser_parse_definitely (parser))
9614         {
9615           if (declares_class_or_enum)
9616             *declares_class_or_enum = 2;
9617           if (decl_specs)
9618             cp_parser_set_decl_spec_type (decl_specs,
9619                                           type_spec,
9620                                           /*user_defined_p=*/true);
9621           return type_spec;
9622         }
9623
9624       /* Fall through.  */
9625     elaborated_type_specifier:
9626       /* We're declaring (not defining) a class or enum.  */
9627       if (declares_class_or_enum)
9628         *declares_class_or_enum = 1;
9629
9630       /* Fall through.  */
9631     case RID_TYPENAME:
9632       /* Look for an elaborated-type-specifier.  */
9633       type_spec
9634         = (cp_parser_elaborated_type_specifier
9635            (parser,
9636             decl_specs && decl_specs->specs[(int) ds_friend],
9637             is_declaration));
9638       if (decl_specs)
9639         cp_parser_set_decl_spec_type (decl_specs,
9640                                       type_spec,
9641                                       /*user_defined_p=*/true);
9642       return type_spec;
9643
9644     case RID_CONST:
9645       ds = ds_const;
9646       if (is_cv_qualifier)
9647         *is_cv_qualifier = true;
9648       break;
9649
9650     case RID_VOLATILE:
9651       ds = ds_volatile;
9652       if (is_cv_qualifier)
9653         *is_cv_qualifier = true;
9654       break;
9655
9656     case RID_RESTRICT:
9657       ds = ds_restrict;
9658       if (is_cv_qualifier)
9659         *is_cv_qualifier = true;
9660       break;
9661
9662     case RID_COMPLEX:
9663       /* The `__complex__' keyword is a GNU extension.  */
9664       ds = ds_complex;
9665       break;
9666
9667     default:
9668       break;
9669     }
9670
9671   /* Handle simple keywords.  */
9672   if (ds != ds_last)
9673     {
9674       if (decl_specs)
9675         {
9676           ++decl_specs->specs[(int)ds];
9677           decl_specs->any_specifiers_p = true;
9678         }
9679       return cp_lexer_consume_token (parser->lexer)->value;
9680     }
9681
9682   /* If we do not already have a type-specifier, assume we are looking
9683      at a simple-type-specifier.  */
9684   type_spec = cp_parser_simple_type_specifier (parser,
9685                                                decl_specs,
9686                                                flags);
9687
9688   /* If we didn't find a type-specifier, and a type-specifier was not
9689      optional in this context, issue an error message.  */
9690   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9691     {
9692       cp_parser_error (parser, "expected type specifier");
9693       return error_mark_node;
9694     }
9695
9696   return type_spec;
9697 }
9698
9699 /* Parse a simple-type-specifier.
9700
9701    simple-type-specifier:
9702      :: [opt] nested-name-specifier [opt] type-name
9703      :: [opt] nested-name-specifier template template-id
9704      char
9705      wchar_t
9706      bool
9707      short
9708      int
9709      long
9710      signed
9711      unsigned
9712      float
9713      double
9714      void
9715
9716    GNU Extension:
9717
9718    simple-type-specifier:
9719      __typeof__ unary-expression
9720      __typeof__ ( type-id )
9721
9722    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9723    appropriately updated.  */
9724
9725 static tree
9726 cp_parser_simple_type_specifier (cp_parser* parser,
9727                                  cp_decl_specifier_seq *decl_specs,
9728                                  cp_parser_flags flags)
9729 {
9730   tree type = NULL_TREE;
9731   cp_token *token;
9732
9733   /* Peek at the next token.  */
9734   token = cp_lexer_peek_token (parser->lexer);
9735
9736   /* If we're looking at a keyword, things are easy.  */
9737   switch (token->keyword)
9738     {
9739     case RID_CHAR:
9740       if (decl_specs)
9741         decl_specs->explicit_char_p = true;
9742       type = char_type_node;
9743       break;
9744     case RID_WCHAR:
9745       type = wchar_type_node;
9746       break;
9747     case RID_BOOL:
9748       type = boolean_type_node;
9749       break;
9750     case RID_SHORT:
9751       if (decl_specs)
9752         ++decl_specs->specs[(int) ds_short];
9753       type = short_integer_type_node;
9754       break;
9755     case RID_INT:
9756       if (decl_specs)
9757         decl_specs->explicit_int_p = true;
9758       type = integer_type_node;
9759       break;
9760     case RID_LONG:
9761       if (decl_specs)
9762         ++decl_specs->specs[(int) ds_long];
9763       type = long_integer_type_node;
9764       break;
9765     case RID_SIGNED:
9766       if (decl_specs)
9767         ++decl_specs->specs[(int) ds_signed];
9768       type = integer_type_node;
9769       break;
9770     case RID_UNSIGNED:
9771       if (decl_specs)
9772         ++decl_specs->specs[(int) ds_unsigned];
9773       type = unsigned_type_node;
9774       break;
9775     case RID_FLOAT:
9776       type = float_type_node;
9777       break;
9778     case RID_DOUBLE:
9779       type = double_type_node;
9780       break;
9781     case RID_VOID:
9782       type = void_type_node;
9783       break;
9784
9785     case RID_TYPEOF:
9786       /* Consume the `typeof' token.  */
9787       cp_lexer_consume_token (parser->lexer);
9788       /* Parse the operand to `typeof'.  */
9789       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9790       /* If it is not already a TYPE, take its type.  */
9791       if (!TYPE_P (type))
9792         type = finish_typeof (type);
9793
9794       if (decl_specs)
9795         cp_parser_set_decl_spec_type (decl_specs, type,
9796                                       /*user_defined_p=*/true);
9797
9798       return type;
9799
9800     default:
9801       break;
9802     }
9803
9804   /* If the type-specifier was for a built-in type, we're done.  */
9805   if (type)
9806     {
9807       tree id;
9808
9809       /* Record the type.  */
9810       if (decl_specs
9811           && (token->keyword != RID_SIGNED
9812               && token->keyword != RID_UNSIGNED
9813               && token->keyword != RID_SHORT
9814               && token->keyword != RID_LONG))
9815         cp_parser_set_decl_spec_type (decl_specs,
9816                                       type,
9817                                       /*user_defined=*/false);
9818       if (decl_specs)
9819         decl_specs->any_specifiers_p = true;
9820
9821       /* Consume the token.  */
9822       id = cp_lexer_consume_token (parser->lexer)->value;
9823
9824       /* There is no valid C++ program where a non-template type is
9825          followed by a "<".  That usually indicates that the user thought
9826          that the type was a template.  */
9827       cp_parser_check_for_invalid_template_id (parser, type);
9828
9829       return TYPE_NAME (type);
9830     }
9831
9832   /* The type-specifier must be a user-defined type.  */
9833   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9834     {
9835       bool qualified_p;
9836       bool global_p;
9837
9838       /* Don't gobble tokens or issue error messages if this is an
9839          optional type-specifier.  */
9840       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9841         cp_parser_parse_tentatively (parser);
9842
9843       /* Look for the optional `::' operator.  */
9844       global_p
9845         = (cp_parser_global_scope_opt (parser,
9846                                        /*current_scope_valid_p=*/false)
9847            != NULL_TREE);
9848       /* Look for the nested-name specifier.  */
9849       qualified_p
9850         = (cp_parser_nested_name_specifier_opt (parser,
9851                                                 /*typename_keyword_p=*/false,
9852                                                 /*check_dependency_p=*/true,
9853                                                 /*type_p=*/false,
9854                                                 /*is_declaration=*/false)
9855            != NULL_TREE);
9856       /* If we have seen a nested-name-specifier, and the next token
9857          is `template', then we are using the template-id production.  */
9858       if (parser->scope
9859           && cp_parser_optional_template_keyword (parser))
9860         {
9861           /* Look for the template-id.  */
9862           type = cp_parser_template_id (parser,
9863                                         /*template_keyword_p=*/true,
9864                                         /*check_dependency_p=*/true,
9865                                         /*is_declaration=*/false);
9866           /* If the template-id did not name a type, we are out of
9867              luck.  */
9868           if (TREE_CODE (type) != TYPE_DECL)
9869             {
9870               cp_parser_error (parser, "expected template-id for type");
9871               type = NULL_TREE;
9872             }
9873         }
9874       /* Otherwise, look for a type-name.  */
9875       else
9876         type = cp_parser_type_name (parser);
9877       /* Keep track of all name-lookups performed in class scopes.  */
9878       if (type
9879           && !global_p
9880           && !qualified_p
9881           && TREE_CODE (type) == TYPE_DECL
9882           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9883         maybe_note_name_used_in_class (DECL_NAME (type), type);
9884       /* If it didn't work out, we don't have a TYPE.  */
9885       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9886           && !cp_parser_parse_definitely (parser))
9887         type = NULL_TREE;
9888       if (type && decl_specs)
9889         cp_parser_set_decl_spec_type (decl_specs, type,
9890                                       /*user_defined=*/true);
9891     }
9892
9893   /* If we didn't get a type-name, issue an error message.  */
9894   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9895     {
9896       cp_parser_error (parser, "expected type-name");
9897       return error_mark_node;
9898     }
9899
9900   /* There is no valid C++ program where a non-template type is
9901      followed by a "<".  That usually indicates that the user thought
9902      that the type was a template.  */
9903   if (type && type != error_mark_node)
9904     {
9905       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9906          If it is, then the '<'...'>' enclose protocol names rather than
9907          template arguments, and so everything is fine.  */
9908       if (c_dialect_objc ()
9909           && (objc_is_id (type) || objc_is_class_name (type)))
9910         {
9911           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9912           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9913
9914           /* Clobber the "unqualified" type previously entered into
9915              DECL_SPECS with the new, improved protocol-qualified version.  */
9916           if (decl_specs)
9917             decl_specs->type = qual_type;
9918
9919           return qual_type;
9920         }
9921
9922       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9923     }
9924
9925   return type;
9926 }
9927
9928 /* Parse a type-name.
9929
9930    type-name:
9931      class-name
9932      enum-name
9933      typedef-name
9934
9935    enum-name:
9936      identifier
9937
9938    typedef-name:
9939      identifier
9940
9941    Returns a TYPE_DECL for the type.  */
9942
9943 static tree
9944 cp_parser_type_name (cp_parser* parser)
9945 {
9946   tree type_decl;
9947   tree identifier;
9948
9949   /* We can't know yet whether it is a class-name or not.  */
9950   cp_parser_parse_tentatively (parser);
9951   /* Try a class-name.  */
9952   type_decl = cp_parser_class_name (parser,
9953                                     /*typename_keyword_p=*/false,
9954                                     /*template_keyword_p=*/false,
9955                                     none_type,
9956                                     /*check_dependency_p=*/true,
9957                                     /*class_head_p=*/false,
9958                                     /*is_declaration=*/false);
9959   /* If it's not a class-name, keep looking.  */
9960   if (!cp_parser_parse_definitely (parser))
9961     {
9962       /* It must be a typedef-name or an enum-name.  */
9963       identifier = cp_parser_identifier (parser);
9964       if (identifier == error_mark_node)
9965         return error_mark_node;
9966
9967       /* Look up the type-name.  */
9968       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9969
9970       if (TREE_CODE (type_decl) != TYPE_DECL
9971           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9972         {
9973           /* See if this is an Objective-C type.  */
9974           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9975           tree type = objc_get_protocol_qualified_type (identifier, protos);
9976           if (type)
9977             type_decl = TYPE_NAME (type);
9978         }
9979
9980       /* Issue an error if we did not find a type-name.  */
9981       if (TREE_CODE (type_decl) != TYPE_DECL)
9982         {
9983           if (!cp_parser_simulate_error (parser))
9984             cp_parser_name_lookup_error (parser, identifier, type_decl,
9985                                          "is not a type");
9986           type_decl = error_mark_node;
9987         }
9988       /* Remember that the name was used in the definition of the
9989          current class so that we can check later to see if the
9990          meaning would have been different after the class was
9991          entirely defined.  */
9992       else if (type_decl != error_mark_node
9993                && !parser->scope)
9994         maybe_note_name_used_in_class (identifier, type_decl);
9995     }
9996
9997   return type_decl;
9998 }
9999
10000
10001 /* Parse an elaborated-type-specifier.  Note that the grammar given
10002    here incorporates the resolution to DR68.
10003
10004    elaborated-type-specifier:
10005      class-key :: [opt] nested-name-specifier [opt] identifier
10006      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10007      enum :: [opt] nested-name-specifier [opt] identifier
10008      typename :: [opt] nested-name-specifier identifier
10009      typename :: [opt] nested-name-specifier template [opt]
10010        template-id
10011
10012    GNU extension:
10013
10014    elaborated-type-specifier:
10015      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10016      class-key attributes :: [opt] nested-name-specifier [opt]
10017                template [opt] template-id
10018      enum attributes :: [opt] nested-name-specifier [opt] identifier
10019
10020    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10021    declared `friend'.  If IS_DECLARATION is TRUE, then this
10022    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10023    something is being declared.
10024
10025    Returns the TYPE specified.  */
10026
10027 static tree
10028 cp_parser_elaborated_type_specifier (cp_parser* parser,
10029                                      bool is_friend,
10030                                      bool is_declaration)
10031 {
10032   enum tag_types tag_type;
10033   tree identifier;
10034   tree type = NULL_TREE;
10035   tree attributes = NULL_TREE;
10036
10037   /* See if we're looking at the `enum' keyword.  */
10038   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10039     {
10040       /* Consume the `enum' token.  */
10041       cp_lexer_consume_token (parser->lexer);
10042       /* Remember that it's an enumeration type.  */
10043       tag_type = enum_type;
10044       /* Parse the attributes.  */
10045       attributes = cp_parser_attributes_opt (parser);
10046     }
10047   /* Or, it might be `typename'.  */
10048   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10049                                            RID_TYPENAME))
10050     {
10051       /* Consume the `typename' token.  */
10052       cp_lexer_consume_token (parser->lexer);
10053       /* Remember that it's a `typename' type.  */
10054       tag_type = typename_type;
10055       /* The `typename' keyword is only allowed in templates.  */
10056       if (!processing_template_decl)
10057         pedwarn ("using %<typename%> outside of template");
10058     }
10059   /* Otherwise it must be a class-key.  */
10060   else
10061     {
10062       tag_type = cp_parser_class_key (parser);
10063       if (tag_type == none_type)
10064         return error_mark_node;
10065       /* Parse the attributes.  */
10066       attributes = cp_parser_attributes_opt (parser);
10067     }
10068
10069   /* Look for the `::' operator.  */
10070   cp_parser_global_scope_opt (parser,
10071                               /*current_scope_valid_p=*/false);
10072   /* Look for the nested-name-specifier.  */
10073   if (tag_type == typename_type)
10074     {
10075       if (!cp_parser_nested_name_specifier (parser,
10076                                            /*typename_keyword_p=*/true,
10077                                            /*check_dependency_p=*/true,
10078                                            /*type_p=*/true,
10079                                             is_declaration))
10080         return error_mark_node;
10081     }
10082   else
10083     /* Even though `typename' is not present, the proposed resolution
10084        to Core Issue 180 says that in `class A<T>::B', `B' should be
10085        considered a type-name, even if `A<T>' is dependent.  */
10086     cp_parser_nested_name_specifier_opt (parser,
10087                                          /*typename_keyword_p=*/true,
10088                                          /*check_dependency_p=*/true,
10089                                          /*type_p=*/true,
10090                                          is_declaration);
10091   /* For everything but enumeration types, consider a template-id.  */
10092   if (tag_type != enum_type)
10093     {
10094       bool template_p = false;
10095       tree decl;
10096
10097       /* Allow the `template' keyword.  */
10098       template_p = cp_parser_optional_template_keyword (parser);
10099       /* If we didn't see `template', we don't know if there's a
10100          template-id or not.  */
10101       if (!template_p)
10102         cp_parser_parse_tentatively (parser);
10103       /* Parse the template-id.  */
10104       decl = cp_parser_template_id (parser, template_p,
10105                                     /*check_dependency_p=*/true,
10106                                     is_declaration);
10107       /* If we didn't find a template-id, look for an ordinary
10108          identifier.  */
10109       if (!template_p && !cp_parser_parse_definitely (parser))
10110         ;
10111       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10112          in effect, then we must assume that, upon instantiation, the
10113          template will correspond to a class.  */
10114       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10115                && tag_type == typename_type)
10116         type = make_typename_type (parser->scope, decl,
10117                                    typename_type,
10118                                    /*complain=*/1);
10119       else
10120         type = TREE_TYPE (decl);
10121     }
10122
10123   /* For an enumeration type, consider only a plain identifier.  */
10124   if (!type)
10125     {
10126       identifier = cp_parser_identifier (parser);
10127
10128       if (identifier == error_mark_node)
10129         {
10130           parser->scope = NULL_TREE;
10131           return error_mark_node;
10132         }
10133
10134       /* For a `typename', we needn't call xref_tag.  */
10135       if (tag_type == typename_type
10136           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10137         return cp_parser_make_typename_type (parser, parser->scope,
10138                                              identifier);
10139       /* Look up a qualified name in the usual way.  */
10140       if (parser->scope)
10141         {
10142           tree decl;
10143
10144           decl = cp_parser_lookup_name (parser, identifier,
10145                                         tag_type,
10146                                         /*is_template=*/false,
10147                                         /*is_namespace=*/false,
10148                                         /*check_dependency=*/true,
10149                                         /*ambiguous_decls=*/NULL);
10150
10151           /* If we are parsing friend declaration, DECL may be a
10152              TEMPLATE_DECL tree node here.  However, we need to check
10153              whether this TEMPLATE_DECL results in valid code.  Consider
10154              the following example:
10155
10156                namespace N {
10157                  template <class T> class C {};
10158                }
10159                class X {
10160                  template <class T> friend class N::C; // #1, valid code
10161                };
10162                template <class T> class Y {
10163                  friend class N::C;                    // #2, invalid code
10164                };
10165
10166              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10167              name lookup of `N::C'.  We see that friend declaration must
10168              be template for the code to be valid.  Note that
10169              processing_template_decl does not work here since it is
10170              always 1 for the above two cases.  */
10171
10172           decl = (cp_parser_maybe_treat_template_as_class
10173                   (decl, /*tag_name_p=*/is_friend
10174                          && parser->num_template_parameter_lists));
10175
10176           if (TREE_CODE (decl) != TYPE_DECL)
10177             {
10178               cp_parser_diagnose_invalid_type_name (parser,
10179                                                     parser->scope,
10180                                                     identifier);
10181               return error_mark_node;
10182             }
10183
10184           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10185             check_elaborated_type_specifier
10186               (tag_type, decl,
10187                (parser->num_template_parameter_lists
10188                 || DECL_SELF_REFERENCE_P (decl)));
10189
10190           type = TREE_TYPE (decl);
10191         }
10192       else
10193         {
10194           /* An elaborated-type-specifier sometimes introduces a new type and
10195              sometimes names an existing type.  Normally, the rule is that it
10196              introduces a new type only if there is not an existing type of
10197              the same name already in scope.  For example, given:
10198
10199                struct S {};
10200                void f() { struct S s; }
10201
10202              the `struct S' in the body of `f' is the same `struct S' as in
10203              the global scope; the existing definition is used.  However, if
10204              there were no global declaration, this would introduce a new
10205              local class named `S'.
10206
10207              An exception to this rule applies to the following code:
10208
10209                namespace N { struct S; }
10210
10211              Here, the elaborated-type-specifier names a new type
10212              unconditionally; even if there is already an `S' in the
10213              containing scope this declaration names a new type.
10214              This exception only applies if the elaborated-type-specifier
10215              forms the complete declaration:
10216
10217                [class.name]
10218
10219                A declaration consisting solely of `class-key identifier ;' is
10220                either a redeclaration of the name in the current scope or a
10221                forward declaration of the identifier as a class name.  It
10222                introduces the name into the current scope.
10223
10224              We are in this situation precisely when the next token is a `;'.
10225
10226              An exception to the exception is that a `friend' declaration does
10227              *not* name a new type; i.e., given:
10228
10229                struct S { friend struct T; };
10230
10231              `T' is not a new type in the scope of `S'.
10232
10233              Also, `new struct S' or `sizeof (struct S)' never results in the
10234              definition of a new type; a new type can only be declared in a
10235              declaration context.  */
10236
10237           tag_scope ts;
10238           bool template_p;
10239
10240           if (is_friend)
10241             /* Friends have special name lookup rules.  */
10242             ts = ts_within_enclosing_non_class;
10243           else if (is_declaration
10244                    && cp_lexer_next_token_is (parser->lexer,
10245                                               CPP_SEMICOLON))
10246             /* This is a `class-key identifier ;' */
10247             ts = ts_current;
10248           else
10249             ts = ts_global;
10250
10251           /* Warn about attributes. They are ignored.  */
10252           if (attributes)
10253             warning (OPT_Wattributes,
10254                      "type attributes are honored only at type definition");
10255
10256           template_p = 
10257             (parser->num_template_parameter_lists
10258              && (cp_parser_next_token_starts_class_definition_p (parser)
10259                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10260           /* An unqualified name was used to reference this type, so
10261              there were no qualifying templates.  */
10262           if (!cp_parser_check_template_parameters (parser, 
10263                                                     /*num_templates=*/0))
10264             return error_mark_node;
10265           type = xref_tag (tag_type, identifier, ts, template_p);
10266         }
10267     }
10268   if (tag_type != enum_type)
10269     cp_parser_check_class_key (tag_type, type);
10270
10271   /* A "<" cannot follow an elaborated type specifier.  If that
10272      happens, the user was probably trying to form a template-id.  */
10273   cp_parser_check_for_invalid_template_id (parser, type);
10274
10275   return type;
10276 }
10277
10278 /* Parse an enum-specifier.
10279
10280    enum-specifier:
10281      enum identifier [opt] { enumerator-list [opt] }
10282
10283    GNU Extensions:
10284      enum identifier [opt] { enumerator-list [opt] } attributes
10285
10286    Returns an ENUM_TYPE representing the enumeration.  */
10287
10288 static tree
10289 cp_parser_enum_specifier (cp_parser* parser)
10290 {
10291   tree identifier;
10292   tree type;
10293
10294   /* Caller guarantees that the current token is 'enum', an identifier
10295      possibly follows, and the token after that is an opening brace.
10296      If we don't have an identifier, fabricate an anonymous name for
10297      the enumeration being defined.  */
10298   cp_lexer_consume_token (parser->lexer);
10299
10300   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10301     identifier = cp_parser_identifier (parser);
10302   else
10303     identifier = make_anon_name ();
10304
10305   /* Issue an error message if type-definitions are forbidden here.  */
10306   if (!cp_parser_check_type_definition (parser))
10307     type = error_mark_node;
10308   else
10309     /* Create the new type.  We do this before consuming the opening
10310        brace so the enum will be recorded as being on the line of its
10311        tag (or the 'enum' keyword, if there is no tag).  */
10312     type = start_enum (identifier);
10313   
10314   /* Consume the opening brace.  */
10315   cp_lexer_consume_token (parser->lexer);
10316
10317   if (type == error_mark_node)
10318     {
10319       cp_parser_skip_to_end_of_block_or_statement (parser);
10320       return error_mark_node;
10321     }
10322
10323   /* If the next token is not '}', then there are some enumerators.  */
10324   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10325     cp_parser_enumerator_list (parser, type);
10326
10327   /* Consume the final '}'.  */
10328   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10329
10330   /* Look for trailing attributes to apply to this enumeration, and
10331      apply them if appropriate.  */
10332   if (cp_parser_allow_gnu_extensions_p (parser))
10333     {
10334       tree trailing_attr = cp_parser_attributes_opt (parser);
10335       cplus_decl_attributes (&type,
10336                              trailing_attr,
10337                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10338     }
10339
10340   /* Finish up the enumeration.  */
10341   finish_enum (type);
10342
10343   return type;
10344 }
10345
10346 /* Parse an enumerator-list.  The enumerators all have the indicated
10347    TYPE.
10348
10349    enumerator-list:
10350      enumerator-definition
10351      enumerator-list , enumerator-definition  */
10352
10353 static void
10354 cp_parser_enumerator_list (cp_parser* parser, tree type)
10355 {
10356   while (true)
10357     {
10358       /* Parse an enumerator-definition.  */
10359       cp_parser_enumerator_definition (parser, type);
10360
10361       /* If the next token is not a ',', we've reached the end of
10362          the list.  */
10363       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10364         break;
10365       /* Otherwise, consume the `,' and keep going.  */
10366       cp_lexer_consume_token (parser->lexer);
10367       /* If the next token is a `}', there is a trailing comma.  */
10368       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10369         {
10370           if (pedantic && !in_system_header)
10371             pedwarn ("comma at end of enumerator list");
10372           break;
10373         }
10374     }
10375 }
10376
10377 /* Parse an enumerator-definition.  The enumerator has the indicated
10378    TYPE.
10379
10380    enumerator-definition:
10381      enumerator
10382      enumerator = constant-expression
10383
10384    enumerator:
10385      identifier  */
10386
10387 static void
10388 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10389 {
10390   tree identifier;
10391   tree value;
10392
10393   /* Look for the identifier.  */
10394   identifier = cp_parser_identifier (parser);
10395   if (identifier == error_mark_node)
10396     return;
10397
10398   /* If the next token is an '=', then there is an explicit value.  */
10399   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10400     {
10401       /* Consume the `=' token.  */
10402       cp_lexer_consume_token (parser->lexer);
10403       /* Parse the value.  */
10404       value = cp_parser_constant_expression (parser,
10405                                              /*allow_non_constant_p=*/false,
10406                                              NULL);
10407     }
10408   else
10409     value = NULL_TREE;
10410
10411   /* Create the enumerator.  */
10412   build_enumerator (identifier, value, type);
10413 }
10414
10415 /* Parse a namespace-name.
10416
10417    namespace-name:
10418      original-namespace-name
10419      namespace-alias
10420
10421    Returns the NAMESPACE_DECL for the namespace.  */
10422
10423 static tree
10424 cp_parser_namespace_name (cp_parser* parser)
10425 {
10426   tree identifier;
10427   tree namespace_decl;
10428
10429   /* Get the name of the namespace.  */
10430   identifier = cp_parser_identifier (parser);
10431   if (identifier == error_mark_node)
10432     return error_mark_node;
10433
10434   /* Look up the identifier in the currently active scope.  Look only
10435      for namespaces, due to:
10436
10437        [basic.lookup.udir]
10438
10439        When looking up a namespace-name in a using-directive or alias
10440        definition, only namespace names are considered.
10441
10442      And:
10443
10444        [basic.lookup.qual]
10445
10446        During the lookup of a name preceding the :: scope resolution
10447        operator, object, function, and enumerator names are ignored.
10448
10449      (Note that cp_parser_class_or_namespace_name only calls this
10450      function if the token after the name is the scope resolution
10451      operator.)  */
10452   namespace_decl = cp_parser_lookup_name (parser, identifier,
10453                                           none_type,
10454                                           /*is_template=*/false,
10455                                           /*is_namespace=*/true,
10456                                           /*check_dependency=*/true,
10457                                           /*ambiguous_decls=*/NULL);
10458   /* If it's not a namespace, issue an error.  */
10459   if (namespace_decl == error_mark_node
10460       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10461     {
10462       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10463         error ("%qD is not a namespace-name", identifier);
10464       cp_parser_error (parser, "expected namespace-name");
10465       namespace_decl = error_mark_node;
10466     }
10467
10468   return namespace_decl;
10469 }
10470
10471 /* Parse a namespace-definition.
10472
10473    namespace-definition:
10474      named-namespace-definition
10475      unnamed-namespace-definition
10476
10477    named-namespace-definition:
10478      original-namespace-definition
10479      extension-namespace-definition
10480
10481    original-namespace-definition:
10482      namespace identifier { namespace-body }
10483
10484    extension-namespace-definition:
10485      namespace original-namespace-name { namespace-body }
10486
10487    unnamed-namespace-definition:
10488      namespace { namespace-body } */
10489
10490 static void
10491 cp_parser_namespace_definition (cp_parser* parser)
10492 {
10493   tree identifier;
10494
10495   /* Look for the `namespace' keyword.  */
10496   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10497
10498   /* Get the name of the namespace.  We do not attempt to distinguish
10499      between an original-namespace-definition and an
10500      extension-namespace-definition at this point.  The semantic
10501      analysis routines are responsible for that.  */
10502   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10503     identifier = cp_parser_identifier (parser);
10504   else
10505     identifier = NULL_TREE;
10506
10507   /* Look for the `{' to start the namespace.  */
10508   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10509   /* Start the namespace.  */
10510   push_namespace (identifier);
10511   /* Parse the body of the namespace.  */
10512   cp_parser_namespace_body (parser);
10513   /* Finish the namespace.  */
10514   pop_namespace ();
10515   /* Look for the final `}'.  */
10516   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10517 }
10518
10519 /* Parse a namespace-body.
10520
10521    namespace-body:
10522      declaration-seq [opt]  */
10523
10524 static void
10525 cp_parser_namespace_body (cp_parser* parser)
10526 {
10527   cp_parser_declaration_seq_opt (parser);
10528 }
10529
10530 /* Parse a namespace-alias-definition.
10531
10532    namespace-alias-definition:
10533      namespace identifier = qualified-namespace-specifier ;  */
10534
10535 static void
10536 cp_parser_namespace_alias_definition (cp_parser* parser)
10537 {
10538   tree identifier;
10539   tree namespace_specifier;
10540
10541   /* Look for the `namespace' keyword.  */
10542   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10543   /* Look for the identifier.  */
10544   identifier = cp_parser_identifier (parser);
10545   if (identifier == error_mark_node)
10546     return;
10547   /* Look for the `=' token.  */
10548   cp_parser_require (parser, CPP_EQ, "`='");
10549   /* Look for the qualified-namespace-specifier.  */
10550   namespace_specifier
10551     = cp_parser_qualified_namespace_specifier (parser);
10552   /* Look for the `;' token.  */
10553   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10554
10555   /* Register the alias in the symbol table.  */
10556   do_namespace_alias (identifier, namespace_specifier);
10557 }
10558
10559 /* Parse a qualified-namespace-specifier.
10560
10561    qualified-namespace-specifier:
10562      :: [opt] nested-name-specifier [opt] namespace-name
10563
10564    Returns a NAMESPACE_DECL corresponding to the specified
10565    namespace.  */
10566
10567 static tree
10568 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10569 {
10570   /* Look for the optional `::'.  */
10571   cp_parser_global_scope_opt (parser,
10572                               /*current_scope_valid_p=*/false);
10573
10574   /* Look for the optional nested-name-specifier.  */
10575   cp_parser_nested_name_specifier_opt (parser,
10576                                        /*typename_keyword_p=*/false,
10577                                        /*check_dependency_p=*/true,
10578                                        /*type_p=*/false,
10579                                        /*is_declaration=*/true);
10580
10581   return cp_parser_namespace_name (parser);
10582 }
10583
10584 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10585    access declaration.
10586
10587    using-declaration:
10588      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10589      using :: unqualified-id ;  
10590
10591    access-declaration:
10592      qualified-id ;  
10593
10594    */
10595
10596 static bool
10597 cp_parser_using_declaration (cp_parser* parser, 
10598                              bool access_declaration_p)
10599 {
10600   cp_token *token;
10601   bool typename_p = false;
10602   bool global_scope_p;
10603   tree decl;
10604   tree identifier;
10605   tree qscope;
10606
10607   if (access_declaration_p)
10608     cp_parser_parse_tentatively (parser);
10609   else
10610     {
10611       /* Look for the `using' keyword.  */
10612       cp_parser_require_keyword (parser, RID_USING, "`using'");
10613       
10614       /* Peek at the next token.  */
10615       token = cp_lexer_peek_token (parser->lexer);
10616       /* See if it's `typename'.  */
10617       if (token->keyword == RID_TYPENAME)
10618         {
10619           /* Remember that we've seen it.  */
10620           typename_p = true;
10621           /* Consume the `typename' token.  */
10622           cp_lexer_consume_token (parser->lexer);
10623         }
10624     }
10625
10626   /* Look for the optional global scope qualification.  */
10627   global_scope_p
10628     = (cp_parser_global_scope_opt (parser,
10629                                    /*current_scope_valid_p=*/false)
10630        != NULL_TREE);
10631
10632   /* If we saw `typename', or didn't see `::', then there must be a
10633      nested-name-specifier present.  */
10634   if (typename_p || !global_scope_p)
10635     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10636                                               /*check_dependency_p=*/true,
10637                                               /*type_p=*/false,
10638                                               /*is_declaration=*/true);
10639   /* Otherwise, we could be in either of the two productions.  In that
10640      case, treat the nested-name-specifier as optional.  */
10641   else
10642     qscope = cp_parser_nested_name_specifier_opt (parser,
10643                                                   /*typename_keyword_p=*/false,
10644                                                   /*check_dependency_p=*/true,
10645                                                   /*type_p=*/false,
10646                                                   /*is_declaration=*/true);
10647   if (!qscope)
10648     qscope = global_namespace;
10649
10650   if (access_declaration_p && cp_parser_error_occurred (parser))
10651     /* Something has already gone wrong; there's no need to parse
10652        further.  Since an error has occurred, the return value of
10653        cp_parser_parse_definitely will be false, as required.  */
10654     return cp_parser_parse_definitely (parser);
10655
10656   /* Parse the unqualified-id.  */
10657   identifier = cp_parser_unqualified_id (parser,
10658                                          /*template_keyword_p=*/false,
10659                                          /*check_dependency_p=*/true,
10660                                          /*declarator_p=*/true);
10661
10662   if (access_declaration_p)
10663     {
10664       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10665         cp_parser_simulate_error (parser);
10666       if (!cp_parser_parse_definitely (parser))
10667         return false;
10668     }
10669
10670   /* The function we call to handle a using-declaration is different
10671      depending on what scope we are in.  */
10672   if (qscope == error_mark_node || identifier == error_mark_node)
10673     ;
10674   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10675            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10676     /* [namespace.udecl]
10677
10678        A using declaration shall not name a template-id.  */
10679     error ("a template-id may not appear in a using-declaration");
10680   else
10681     {
10682       if (at_class_scope_p ())
10683         {
10684           /* Create the USING_DECL.  */
10685           decl = do_class_using_decl (parser->scope, identifier);
10686           /* Add it to the list of members in this class.  */
10687           finish_member_declaration (decl);
10688         }
10689       else
10690         {
10691           decl = cp_parser_lookup_name_simple (parser, identifier);
10692           if (decl == error_mark_node)
10693             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10694           else if (!at_namespace_scope_p ())
10695             do_local_using_decl (decl, qscope, identifier);
10696           else
10697             do_toplevel_using_decl (decl, qscope, identifier);
10698         }
10699     }
10700
10701   /* Look for the final `;'.  */
10702   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10703   
10704   return true;
10705 }
10706
10707 /* Parse a using-directive.
10708
10709    using-directive:
10710      using namespace :: [opt] nested-name-specifier [opt]
10711        namespace-name ;  */
10712
10713 static void
10714 cp_parser_using_directive (cp_parser* parser)
10715 {
10716   tree namespace_decl;
10717   tree attribs;
10718
10719   /* Look for the `using' keyword.  */
10720   cp_parser_require_keyword (parser, RID_USING, "`using'");
10721   /* And the `namespace' keyword.  */
10722   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10723   /* Look for the optional `::' operator.  */
10724   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10725   /* And the optional nested-name-specifier.  */
10726   cp_parser_nested_name_specifier_opt (parser,
10727                                        /*typename_keyword_p=*/false,
10728                                        /*check_dependency_p=*/true,
10729                                        /*type_p=*/false,
10730                                        /*is_declaration=*/true);
10731   /* Get the namespace being used.  */
10732   namespace_decl = cp_parser_namespace_name (parser);
10733   /* And any specified attributes.  */
10734   attribs = cp_parser_attributes_opt (parser);
10735   /* Update the symbol table.  */
10736   parse_using_directive (namespace_decl, attribs);
10737   /* Look for the final `;'.  */
10738   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10739 }
10740
10741 /* Parse an asm-definition.
10742
10743    asm-definition:
10744      asm ( string-literal ) ;
10745
10746    GNU Extension:
10747
10748    asm-definition:
10749      asm volatile [opt] ( string-literal ) ;
10750      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10751      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10752                           : asm-operand-list [opt] ) ;
10753      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10754                           : asm-operand-list [opt]
10755                           : asm-operand-list [opt] ) ;  */
10756
10757 static void
10758 cp_parser_asm_definition (cp_parser* parser)
10759 {
10760   tree string;
10761   tree outputs = NULL_TREE;
10762   tree inputs = NULL_TREE;
10763   tree clobbers = NULL_TREE;
10764   tree asm_stmt;
10765   bool volatile_p = false;
10766   bool extended_p = false;
10767
10768   /* Look for the `asm' keyword.  */
10769   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10770   /* See if the next token is `volatile'.  */
10771   if (cp_parser_allow_gnu_extensions_p (parser)
10772       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10773     {
10774       /* Remember that we saw the `volatile' keyword.  */
10775       volatile_p = true;
10776       /* Consume the token.  */
10777       cp_lexer_consume_token (parser->lexer);
10778     }
10779   /* Look for the opening `('.  */
10780   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10781     return;
10782   /* Look for the string.  */
10783   string = cp_parser_string_literal (parser, false, false);
10784   if (string == error_mark_node)
10785     {
10786       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10787                                              /*consume_paren=*/true);
10788       return;
10789     }
10790
10791   /* If we're allowing GNU extensions, check for the extended assembly
10792      syntax.  Unfortunately, the `:' tokens need not be separated by
10793      a space in C, and so, for compatibility, we tolerate that here
10794      too.  Doing that means that we have to treat the `::' operator as
10795      two `:' tokens.  */
10796   if (cp_parser_allow_gnu_extensions_p (parser)
10797       && parser->in_function_body
10798       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10799           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10800     {
10801       bool inputs_p = false;
10802       bool clobbers_p = false;
10803
10804       /* The extended syntax was used.  */
10805       extended_p = true;
10806
10807       /* Look for outputs.  */
10808       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10809         {
10810           /* Consume the `:'.  */
10811           cp_lexer_consume_token (parser->lexer);
10812           /* Parse the output-operands.  */
10813           if (cp_lexer_next_token_is_not (parser->lexer,
10814                                           CPP_COLON)
10815               && cp_lexer_next_token_is_not (parser->lexer,
10816                                              CPP_SCOPE)
10817               && cp_lexer_next_token_is_not (parser->lexer,
10818                                              CPP_CLOSE_PAREN))
10819             outputs = cp_parser_asm_operand_list (parser);
10820         }
10821       /* If the next token is `::', there are no outputs, and the
10822          next token is the beginning of the inputs.  */
10823       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10824         /* The inputs are coming next.  */
10825         inputs_p = true;
10826
10827       /* Look for inputs.  */
10828       if (inputs_p
10829           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10830         {
10831           /* Consume the `:' or `::'.  */
10832           cp_lexer_consume_token (parser->lexer);
10833           /* Parse the output-operands.  */
10834           if (cp_lexer_next_token_is_not (parser->lexer,
10835                                           CPP_COLON)
10836               && cp_lexer_next_token_is_not (parser->lexer,
10837                                              CPP_CLOSE_PAREN))
10838             inputs = cp_parser_asm_operand_list (parser);
10839         }
10840       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10841         /* The clobbers are coming next.  */
10842         clobbers_p = true;
10843
10844       /* Look for clobbers.  */
10845       if (clobbers_p
10846           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10847         {
10848           /* Consume the `:' or `::'.  */
10849           cp_lexer_consume_token (parser->lexer);
10850           /* Parse the clobbers.  */
10851           if (cp_lexer_next_token_is_not (parser->lexer,
10852                                           CPP_CLOSE_PAREN))
10853             clobbers = cp_parser_asm_clobber_list (parser);
10854         }
10855     }
10856   /* Look for the closing `)'.  */
10857   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10858     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10859                                            /*consume_paren=*/true);
10860   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10861
10862   /* Create the ASM_EXPR.  */
10863   if (parser->in_function_body)
10864     {
10865       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10866                                   inputs, clobbers);
10867       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10868       if (!extended_p)
10869         {
10870           tree temp = asm_stmt;
10871           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10872             temp = TREE_OPERAND (temp, 0);
10873
10874           ASM_INPUT_P (temp) = 1;
10875         }
10876     }
10877   else
10878     assemble_asm (string);
10879 }
10880
10881 /* Declarators [gram.dcl.decl] */
10882
10883 /* Parse an init-declarator.
10884
10885    init-declarator:
10886      declarator initializer [opt]
10887
10888    GNU Extension:
10889
10890    init-declarator:
10891      declarator asm-specification [opt] attributes [opt] initializer [opt]
10892
10893    function-definition:
10894      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10895        function-body
10896      decl-specifier-seq [opt] declarator function-try-block
10897
10898    GNU Extension:
10899
10900    function-definition:
10901      __extension__ function-definition
10902
10903    The DECL_SPECIFIERS apply to this declarator.  Returns a
10904    representation of the entity declared.  If MEMBER_P is TRUE, then
10905    this declarator appears in a class scope.  The new DECL created by
10906    this declarator is returned.
10907
10908    The CHECKS are access checks that should be performed once we know
10909    what entity is being declared (and, therefore, what classes have
10910    befriended it).
10911
10912    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10913    for a function-definition here as well.  If the declarator is a
10914    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10915    be TRUE upon return.  By that point, the function-definition will
10916    have been completely parsed.
10917
10918    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10919    is FALSE.  */
10920
10921 static tree
10922 cp_parser_init_declarator (cp_parser* parser,
10923                            cp_decl_specifier_seq *decl_specifiers,
10924                            tree checks,
10925                            bool function_definition_allowed_p,
10926                            bool member_p,
10927                            int declares_class_or_enum,
10928                            bool* function_definition_p)
10929 {
10930   cp_token *token;
10931   cp_declarator *declarator;
10932   tree prefix_attributes;
10933   tree attributes;
10934   tree asm_specification;
10935   tree initializer;
10936   tree decl = NULL_TREE;
10937   tree scope;
10938   bool is_initialized;
10939   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
10940      initialized with "= ..", CPP_OPEN_PAREN if initialized with
10941      "(...)".  */
10942   enum cpp_ttype initialization_kind;
10943   bool is_parenthesized_init;
10944   bool is_non_constant_init;
10945   int ctor_dtor_or_conv_p;
10946   bool friend_p;
10947   tree pushed_scope = NULL;
10948
10949   /* Gather the attributes that were provided with the
10950      decl-specifiers.  */
10951   prefix_attributes = decl_specifiers->attributes;
10952
10953   /* Assume that this is not the declarator for a function
10954      definition.  */
10955   if (function_definition_p)
10956     *function_definition_p = false;
10957
10958   /* Defer access checks while parsing the declarator; we cannot know
10959      what names are accessible until we know what is being
10960      declared.  */
10961   resume_deferring_access_checks ();
10962
10963   /* Parse the declarator.  */
10964   declarator
10965     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10966                             &ctor_dtor_or_conv_p,
10967                             /*parenthesized_p=*/NULL,
10968                             /*member_p=*/false);
10969   /* Gather up the deferred checks.  */
10970   stop_deferring_access_checks ();
10971
10972   /* If the DECLARATOR was erroneous, there's no need to go
10973      further.  */
10974   if (declarator == cp_error_declarator)
10975     return error_mark_node;
10976
10977   /* Check that the number of template-parameter-lists is OK.  */
10978   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10979     return error_mark_node;
10980
10981   if (declares_class_or_enum & 2)
10982     cp_parser_check_for_definition_in_return_type (declarator,
10983                                                    decl_specifiers->type);
10984
10985   /* Figure out what scope the entity declared by the DECLARATOR is
10986      located in.  `grokdeclarator' sometimes changes the scope, so
10987      we compute it now.  */
10988   scope = get_scope_of_declarator (declarator);
10989
10990   /* If we're allowing GNU extensions, look for an asm-specification
10991      and attributes.  */
10992   if (cp_parser_allow_gnu_extensions_p (parser))
10993     {
10994       /* Look for an asm-specification.  */
10995       asm_specification = cp_parser_asm_specification_opt (parser);
10996       /* And attributes.  */
10997       attributes = cp_parser_attributes_opt (parser);
10998     }
10999   else
11000     {
11001       asm_specification = NULL_TREE;
11002       attributes = NULL_TREE;
11003     }
11004
11005   /* Peek at the next token.  */
11006   token = cp_lexer_peek_token (parser->lexer);
11007   /* Check to see if the token indicates the start of a
11008      function-definition.  */
11009   if (cp_parser_token_starts_function_definition_p (token))
11010     {
11011       if (!function_definition_allowed_p)
11012         {
11013           /* If a function-definition should not appear here, issue an
11014              error message.  */
11015           cp_parser_error (parser,
11016                            "a function-definition is not allowed here");
11017           return error_mark_node;
11018         }
11019       else
11020         {
11021           /* Neither attributes nor an asm-specification are allowed
11022              on a function-definition.  */
11023           if (asm_specification)
11024             error ("an asm-specification is not allowed on a function-definition");
11025           if (attributes)
11026             error ("attributes are not allowed on a function-definition");
11027           /* This is a function-definition.  */
11028           *function_definition_p = true;
11029
11030           /* Parse the function definition.  */
11031           if (member_p)
11032             decl = cp_parser_save_member_function_body (parser,
11033                                                         decl_specifiers,
11034                                                         declarator,
11035                                                         prefix_attributes);
11036           else
11037             decl
11038               = (cp_parser_function_definition_from_specifiers_and_declarator
11039                  (parser, decl_specifiers, prefix_attributes, declarator));
11040
11041           return decl;
11042         }
11043     }
11044
11045   /* [dcl.dcl]
11046
11047      Only in function declarations for constructors, destructors, and
11048      type conversions can the decl-specifier-seq be omitted.
11049
11050      We explicitly postpone this check past the point where we handle
11051      function-definitions because we tolerate function-definitions
11052      that are missing their return types in some modes.  */
11053   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11054     {
11055       cp_parser_error (parser,
11056                        "expected constructor, destructor, or type conversion");
11057       return error_mark_node;
11058     }
11059
11060   /* An `=' or an `(' indicates an initializer.  */
11061   if (token->type == CPP_EQ
11062       || token->type == CPP_OPEN_PAREN)
11063     {
11064       is_initialized = true;
11065       initialization_kind = token->type;
11066     }
11067   else
11068     {
11069       /* If the init-declarator isn't initialized and isn't followed by a
11070          `,' or `;', it's not a valid init-declarator.  */
11071       if (token->type != CPP_COMMA
11072           && token->type != CPP_SEMICOLON)
11073         {
11074           cp_parser_error (parser, "expected initializer");
11075           return error_mark_node;
11076         }
11077       is_initialized = false;
11078       initialization_kind = CPP_EOF;
11079     }
11080
11081   /* Because start_decl has side-effects, we should only call it if we
11082      know we're going ahead.  By this point, we know that we cannot
11083      possibly be looking at any other construct.  */
11084   cp_parser_commit_to_tentative_parse (parser);
11085
11086   /* If the decl specifiers were bad, issue an error now that we're
11087      sure this was intended to be a declarator.  Then continue
11088      declaring the variable(s), as int, to try to cut down on further
11089      errors.  */
11090   if (decl_specifiers->any_specifiers_p
11091       && decl_specifiers->type == error_mark_node)
11092     {
11093       cp_parser_error (parser, "invalid type in declaration");
11094       decl_specifiers->type = integer_type_node;
11095     }
11096
11097   /* Check to see whether or not this declaration is a friend.  */
11098   friend_p = cp_parser_friend_p (decl_specifiers);
11099
11100   /* Enter the newly declared entry in the symbol table.  If we're
11101      processing a declaration in a class-specifier, we wait until
11102      after processing the initializer.  */
11103   if (!member_p)
11104     {
11105       if (parser->in_unbraced_linkage_specification_p)
11106         decl_specifiers->storage_class = sc_extern;
11107       decl = start_decl (declarator, decl_specifiers,
11108                          is_initialized, attributes, prefix_attributes,
11109                          &pushed_scope);
11110     }
11111   else if (scope)
11112     /* Enter the SCOPE.  That way unqualified names appearing in the
11113        initializer will be looked up in SCOPE.  */
11114     pushed_scope = push_scope (scope);
11115
11116   /* Perform deferred access control checks, now that we know in which
11117      SCOPE the declared entity resides.  */
11118   if (!member_p && decl)
11119     {
11120       tree saved_current_function_decl = NULL_TREE;
11121
11122       /* If the entity being declared is a function, pretend that we
11123          are in its scope.  If it is a `friend', it may have access to
11124          things that would not otherwise be accessible.  */
11125       if (TREE_CODE (decl) == FUNCTION_DECL)
11126         {
11127           saved_current_function_decl = current_function_decl;
11128           current_function_decl = decl;
11129         }
11130
11131       /* Perform access checks for template parameters.  */
11132       cp_parser_perform_template_parameter_access_checks (checks);
11133
11134       /* Perform the access control checks for the declarator and the
11135          the decl-specifiers.  */
11136       perform_deferred_access_checks ();
11137
11138       /* Restore the saved value.  */
11139       if (TREE_CODE (decl) == FUNCTION_DECL)
11140         current_function_decl = saved_current_function_decl;
11141     }
11142
11143   /* Parse the initializer.  */
11144   initializer = NULL_TREE;
11145   is_parenthesized_init = false;
11146   is_non_constant_init = true;
11147   if (is_initialized)
11148     {
11149       if (function_declarator_p (declarator))
11150         {
11151            if (initialization_kind == CPP_EQ)
11152              initializer = cp_parser_pure_specifier (parser);
11153            else
11154              {
11155                /* If the declaration was erroneous, we don't really
11156                   know what the user intended, so just silently
11157                   consume the initializer.  */
11158                if (decl != error_mark_node)
11159                  error ("initializer provided for function");
11160                cp_parser_skip_to_closing_parenthesis (parser,
11161                                                       /*recovering=*/true,
11162                                                       /*or_comma=*/false,
11163                                                       /*consume_paren=*/true);
11164              }
11165         }
11166       else
11167         initializer = cp_parser_initializer (parser,
11168                                              &is_parenthesized_init,
11169                                              &is_non_constant_init);
11170     }
11171
11172   /* The old parser allows attributes to appear after a parenthesized
11173      initializer.  Mark Mitchell proposed removing this functionality
11174      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11175      attributes -- but ignores them.  */
11176   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11177     if (cp_parser_attributes_opt (parser))
11178       warning (OPT_Wattributes,
11179                "attributes after parenthesized initializer ignored");
11180
11181   /* For an in-class declaration, use `grokfield' to create the
11182      declaration.  */
11183   if (member_p)
11184     {
11185       if (pushed_scope)
11186         {
11187           pop_scope (pushed_scope);
11188           pushed_scope = false;
11189         }
11190       decl = grokfield (declarator, decl_specifiers,
11191                         initializer, !is_non_constant_init,
11192                         /*asmspec=*/NULL_TREE,
11193                         prefix_attributes);
11194       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11195         cp_parser_save_default_args (parser, decl);
11196     }
11197
11198   /* Finish processing the declaration.  But, skip friend
11199      declarations.  */
11200   if (!friend_p && decl && decl != error_mark_node)
11201     {
11202       cp_finish_decl (decl,
11203                       initializer, !is_non_constant_init,
11204                       asm_specification,
11205                       /* If the initializer is in parentheses, then this is
11206                          a direct-initialization, which means that an
11207                          `explicit' constructor is OK.  Otherwise, an
11208                          `explicit' constructor cannot be used.  */
11209                       ((is_parenthesized_init || !is_initialized)
11210                      ? 0 : LOOKUP_ONLYCONVERTING));
11211     }
11212   if (!friend_p && pushed_scope)
11213     pop_scope (pushed_scope);
11214
11215   return decl;
11216 }
11217
11218 /* Parse a declarator.
11219
11220    declarator:
11221      direct-declarator
11222      ptr-operator declarator
11223
11224    abstract-declarator:
11225      ptr-operator abstract-declarator [opt]
11226      direct-abstract-declarator
11227
11228    GNU Extensions:
11229
11230    declarator:
11231      attributes [opt] direct-declarator
11232      attributes [opt] ptr-operator declarator
11233
11234    abstract-declarator:
11235      attributes [opt] ptr-operator abstract-declarator [opt]
11236      attributes [opt] direct-abstract-declarator
11237
11238    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11239    detect constructor, destructor or conversion operators. It is set
11240    to -1 if the declarator is a name, and +1 if it is a
11241    function. Otherwise it is set to zero. Usually you just want to
11242    test for >0, but internally the negative value is used.
11243
11244    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11245    a decl-specifier-seq unless it declares a constructor, destructor,
11246    or conversion.  It might seem that we could check this condition in
11247    semantic analysis, rather than parsing, but that makes it difficult
11248    to handle something like `f()'.  We want to notice that there are
11249    no decl-specifiers, and therefore realize that this is an
11250    expression, not a declaration.)
11251
11252    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11253    the declarator is a direct-declarator of the form "(...)".
11254
11255    MEMBER_P is true iff this declarator is a member-declarator.  */
11256
11257 static cp_declarator *
11258 cp_parser_declarator (cp_parser* parser,
11259                       cp_parser_declarator_kind dcl_kind,
11260                       int* ctor_dtor_or_conv_p,
11261                       bool* parenthesized_p,
11262                       bool member_p)
11263 {
11264   cp_token *token;
11265   cp_declarator *declarator;
11266   enum tree_code code;
11267   cp_cv_quals cv_quals;
11268   tree class_type;
11269   tree attributes = NULL_TREE;
11270
11271   /* Assume this is not a constructor, destructor, or type-conversion
11272      operator.  */
11273   if (ctor_dtor_or_conv_p)
11274     *ctor_dtor_or_conv_p = 0;
11275
11276   if (cp_parser_allow_gnu_extensions_p (parser))
11277     attributes = cp_parser_attributes_opt (parser);
11278
11279   /* Peek at the next token.  */
11280   token = cp_lexer_peek_token (parser->lexer);
11281
11282   /* Check for the ptr-operator production.  */
11283   cp_parser_parse_tentatively (parser);
11284   /* Parse the ptr-operator.  */
11285   code = cp_parser_ptr_operator (parser,
11286                                  &class_type,
11287                                  &cv_quals);
11288   /* If that worked, then we have a ptr-operator.  */
11289   if (cp_parser_parse_definitely (parser))
11290     {
11291       /* If a ptr-operator was found, then this declarator was not
11292          parenthesized.  */
11293       if (parenthesized_p)
11294         *parenthesized_p = true;
11295       /* The dependent declarator is optional if we are parsing an
11296          abstract-declarator.  */
11297       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11298         cp_parser_parse_tentatively (parser);
11299
11300       /* Parse the dependent declarator.  */
11301       declarator = cp_parser_declarator (parser, dcl_kind,
11302                                          /*ctor_dtor_or_conv_p=*/NULL,
11303                                          /*parenthesized_p=*/NULL,
11304                                          /*member_p=*/false);
11305
11306       /* If we are parsing an abstract-declarator, we must handle the
11307          case where the dependent declarator is absent.  */
11308       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11309           && !cp_parser_parse_definitely (parser))
11310         declarator = NULL;
11311
11312       /* Build the representation of the ptr-operator.  */
11313       if (class_type)
11314         declarator = make_ptrmem_declarator (cv_quals,
11315                                              class_type,
11316                                              declarator);
11317       else if (code == INDIRECT_REF)
11318         declarator = make_pointer_declarator (cv_quals, declarator);
11319       else
11320         declarator = make_reference_declarator (cv_quals, declarator);
11321     }
11322   /* Everything else is a direct-declarator.  */
11323   else
11324     {
11325       if (parenthesized_p)
11326         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11327                                                    CPP_OPEN_PAREN);
11328       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11329                                                 ctor_dtor_or_conv_p,
11330                                                 member_p);
11331     }
11332
11333   if (attributes && declarator && declarator != cp_error_declarator)
11334     declarator->attributes = attributes;
11335
11336   return declarator;
11337 }
11338
11339 /* Parse a direct-declarator or direct-abstract-declarator.
11340
11341    direct-declarator:
11342      declarator-id
11343      direct-declarator ( parameter-declaration-clause )
11344        cv-qualifier-seq [opt]
11345        exception-specification [opt]
11346      direct-declarator [ constant-expression [opt] ]
11347      ( declarator )
11348
11349    direct-abstract-declarator:
11350      direct-abstract-declarator [opt]
11351        ( parameter-declaration-clause )
11352        cv-qualifier-seq [opt]
11353        exception-specification [opt]
11354      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11355      ( abstract-declarator )
11356
11357    Returns a representation of the declarator.  DCL_KIND is
11358    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11359    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11360    we are parsing a direct-declarator.  It is
11361    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11362    of ambiguity we prefer an abstract declarator, as per
11363    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11364    cp_parser_declarator.  */
11365
11366 static cp_declarator *
11367 cp_parser_direct_declarator (cp_parser* parser,
11368                              cp_parser_declarator_kind dcl_kind,
11369                              int* ctor_dtor_or_conv_p,
11370                              bool member_p)
11371 {
11372   cp_token *token;
11373   cp_declarator *declarator = NULL;
11374   tree scope = NULL_TREE;
11375   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11376   bool saved_in_declarator_p = parser->in_declarator_p;
11377   bool first = true;
11378   tree pushed_scope = NULL_TREE;
11379
11380   while (true)
11381     {
11382       /* Peek at the next token.  */
11383       token = cp_lexer_peek_token (parser->lexer);
11384       if (token->type == CPP_OPEN_PAREN)
11385         {
11386           /* This is either a parameter-declaration-clause, or a
11387              parenthesized declarator. When we know we are parsing a
11388              named declarator, it must be a parenthesized declarator
11389              if FIRST is true. For instance, `(int)' is a
11390              parameter-declaration-clause, with an omitted
11391              direct-abstract-declarator. But `((*))', is a
11392              parenthesized abstract declarator. Finally, when T is a
11393              template parameter `(T)' is a
11394              parameter-declaration-clause, and not a parenthesized
11395              named declarator.
11396
11397              We first try and parse a parameter-declaration-clause,
11398              and then try a nested declarator (if FIRST is true).
11399
11400              It is not an error for it not to be a
11401              parameter-declaration-clause, even when FIRST is
11402              false. Consider,
11403
11404                int i (int);
11405                int i (3);
11406
11407              The first is the declaration of a function while the
11408              second is a the definition of a variable, including its
11409              initializer.
11410
11411              Having seen only the parenthesis, we cannot know which of
11412              these two alternatives should be selected.  Even more
11413              complex are examples like:
11414
11415                int i (int (a));
11416                int i (int (3));
11417
11418              The former is a function-declaration; the latter is a
11419              variable initialization.
11420
11421              Thus again, we try a parameter-declaration-clause, and if
11422              that fails, we back out and return.  */
11423
11424           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11425             {
11426               cp_parameter_declarator *params;
11427               unsigned saved_num_template_parameter_lists;
11428
11429               /* In a member-declarator, the only valid interpretation
11430                  of a parenthesis is the start of a
11431                  parameter-declaration-clause.  (It is invalid to
11432                  initialize a static data member with a parenthesized
11433                  initializer; only the "=" form of initialization is
11434                  permitted.)  */
11435               if (!member_p)
11436                 cp_parser_parse_tentatively (parser);
11437
11438               /* Consume the `('.  */
11439               cp_lexer_consume_token (parser->lexer);
11440               if (first)
11441                 {
11442                   /* If this is going to be an abstract declarator, we're
11443                      in a declarator and we can't have default args.  */
11444                   parser->default_arg_ok_p = false;
11445                   parser->in_declarator_p = true;
11446                 }
11447
11448               /* Inside the function parameter list, surrounding
11449                  template-parameter-lists do not apply.  */
11450               saved_num_template_parameter_lists
11451                 = parser->num_template_parameter_lists;
11452               parser->num_template_parameter_lists = 0;
11453
11454               /* Parse the parameter-declaration-clause.  */
11455               params = cp_parser_parameter_declaration_clause (parser);
11456
11457               parser->num_template_parameter_lists
11458                 = saved_num_template_parameter_lists;
11459
11460               /* If all went well, parse the cv-qualifier-seq and the
11461                  exception-specification.  */
11462               if (member_p || cp_parser_parse_definitely (parser))
11463                 {
11464                   cp_cv_quals cv_quals;
11465                   tree exception_specification;
11466
11467                   if (ctor_dtor_or_conv_p)
11468                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11469                   first = false;
11470                   /* Consume the `)'.  */
11471                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11472
11473                   /* Parse the cv-qualifier-seq.  */
11474                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11475                   /* And the exception-specification.  */
11476                   exception_specification
11477                     = cp_parser_exception_specification_opt (parser);
11478
11479                   /* Create the function-declarator.  */
11480                   declarator = make_call_declarator (declarator,
11481                                                      params,
11482                                                      cv_quals,
11483                                                      exception_specification);
11484                   /* Any subsequent parameter lists are to do with
11485                      return type, so are not those of the declared
11486                      function.  */
11487                   parser->default_arg_ok_p = false;
11488
11489                   /* Repeat the main loop.  */
11490                   continue;
11491                 }
11492             }
11493
11494           /* If this is the first, we can try a parenthesized
11495              declarator.  */
11496           if (first)
11497             {
11498               bool saved_in_type_id_in_expr_p;
11499
11500               parser->default_arg_ok_p = saved_default_arg_ok_p;
11501               parser->in_declarator_p = saved_in_declarator_p;
11502
11503               /* Consume the `('.  */
11504               cp_lexer_consume_token (parser->lexer);
11505               /* Parse the nested declarator.  */
11506               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11507               parser->in_type_id_in_expr_p = true;
11508               declarator
11509                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11510                                         /*parenthesized_p=*/NULL,
11511                                         member_p);
11512               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11513               first = false;
11514               /* Expect a `)'.  */
11515               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11516                 declarator = cp_error_declarator;
11517               if (declarator == cp_error_declarator)
11518                 break;
11519
11520               goto handle_declarator;
11521             }
11522           /* Otherwise, we must be done.  */
11523           else
11524             break;
11525         }
11526       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11527                && token->type == CPP_OPEN_SQUARE)
11528         {
11529           /* Parse an array-declarator.  */
11530           tree bounds;
11531
11532           if (ctor_dtor_or_conv_p)
11533             *ctor_dtor_or_conv_p = 0;
11534
11535           first = false;
11536           parser->default_arg_ok_p = false;
11537           parser->in_declarator_p = true;
11538           /* Consume the `['.  */
11539           cp_lexer_consume_token (parser->lexer);
11540           /* Peek at the next token.  */
11541           token = cp_lexer_peek_token (parser->lexer);
11542           /* If the next token is `]', then there is no
11543              constant-expression.  */
11544           if (token->type != CPP_CLOSE_SQUARE)
11545             {
11546               bool non_constant_p;
11547
11548               bounds
11549                 = cp_parser_constant_expression (parser,
11550                                                  /*allow_non_constant=*/true,
11551                                                  &non_constant_p);
11552               if (!non_constant_p)
11553                 bounds = fold_non_dependent_expr (bounds);
11554               /* Normally, the array bound must be an integral constant
11555                  expression.  However, as an extension, we allow VLAs
11556                  in function scopes.  */
11557               else if (!parser->in_function_body)
11558                 {
11559                   error ("array bound is not an integer constant");
11560                   bounds = error_mark_node;
11561                 }
11562             }
11563           else
11564             bounds = NULL_TREE;
11565           /* Look for the closing `]'.  */
11566           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11567             {
11568               declarator = cp_error_declarator;
11569               break;
11570             }
11571
11572           declarator = make_array_declarator (declarator, bounds);
11573         }
11574       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11575         {
11576           tree qualifying_scope;
11577           tree unqualified_name;
11578           special_function_kind sfk;
11579
11580           /* Parse a declarator-id */
11581           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11582             cp_parser_parse_tentatively (parser);
11583           unqualified_name = cp_parser_declarator_id (parser);
11584           qualifying_scope = parser->scope;
11585           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11586             {
11587               if (!cp_parser_parse_definitely (parser))
11588                 unqualified_name = error_mark_node;
11589               else if (qualifying_scope
11590                        || (TREE_CODE (unqualified_name)
11591                            != IDENTIFIER_NODE))
11592                 {
11593                   cp_parser_error (parser, "expected unqualified-id");
11594                   unqualified_name = error_mark_node;
11595                 }
11596             }
11597
11598           if (unqualified_name == error_mark_node)
11599             {
11600               declarator = cp_error_declarator;
11601               break;
11602             }
11603
11604           if (qualifying_scope && at_namespace_scope_p ()
11605               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11606             {
11607               /* In the declaration of a member of a template class
11608                  outside of the class itself, the SCOPE will sometimes
11609                  be a TYPENAME_TYPE.  For example, given:
11610
11611                  template <typename T>
11612                  int S<T>::R::i = 3;
11613
11614                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11615                  this context, we must resolve S<T>::R to an ordinary
11616                  type, rather than a typename type.
11617
11618                  The reason we normally avoid resolving TYPENAME_TYPEs
11619                  is that a specialization of `S' might render
11620                  `S<T>::R' not a type.  However, if `S' is
11621                  specialized, then this `i' will not be used, so there
11622                  is no harm in resolving the types here.  */
11623               tree type;
11624
11625               /* Resolve the TYPENAME_TYPE.  */
11626               type = resolve_typename_type (qualifying_scope,
11627                                             /*only_current_p=*/false);
11628               /* If that failed, the declarator is invalid.  */
11629               if (type == error_mark_node)
11630                 error ("%<%T::%D%> is not a type",
11631                        TYPE_CONTEXT (qualifying_scope),
11632                        TYPE_IDENTIFIER (qualifying_scope));
11633               qualifying_scope = type;
11634             }
11635
11636           sfk = sfk_none;
11637           if (unqualified_name)
11638             {
11639               tree class_type;
11640
11641               if (qualifying_scope
11642                   && CLASS_TYPE_P (qualifying_scope))
11643                 class_type = qualifying_scope;
11644               else
11645                 class_type = current_class_type;
11646
11647               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11648                 {
11649                   tree name_type = TREE_TYPE (unqualified_name);
11650                   if (class_type && same_type_p (name_type, class_type))
11651                     {
11652                       if (qualifying_scope
11653                           && CLASSTYPE_USE_TEMPLATE (name_type))
11654                         {
11655                           error ("invalid use of constructor as a template");
11656                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11657                                   "name the constructor in a qualified name",
11658                                   class_type,
11659                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11660                                   class_type, name_type);
11661                           declarator = cp_error_declarator;
11662                           break;
11663                         }
11664                       else
11665                         unqualified_name = constructor_name (class_type);
11666                     }
11667                   else
11668                     {
11669                       /* We do not attempt to print the declarator
11670                          here because we do not have enough
11671                          information about its original syntactic
11672                          form.  */
11673                       cp_parser_error (parser, "invalid declarator");
11674                       declarator = cp_error_declarator;
11675                       break;
11676                     }
11677                 }
11678
11679               if (class_type)
11680                 {
11681                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11682                     sfk = sfk_destructor;
11683                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11684                     sfk = sfk_conversion;
11685                   else if (/* There's no way to declare a constructor
11686                               for an anonymous type, even if the type
11687                               got a name for linkage purposes.  */
11688                            !TYPE_WAS_ANONYMOUS (class_type)
11689                            && constructor_name_p (unqualified_name,
11690                                                   class_type))
11691                     {
11692                       unqualified_name = constructor_name (class_type);
11693                       sfk = sfk_constructor;
11694                     }
11695
11696                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11697                     *ctor_dtor_or_conv_p = -1;
11698                 }
11699             }
11700           declarator = make_id_declarator (qualifying_scope, 
11701                                            unqualified_name,
11702                                            sfk);
11703           declarator->id_loc = token->location;
11704
11705         handle_declarator:;
11706           scope = get_scope_of_declarator (declarator);
11707           if (scope)
11708             /* Any names that appear after the declarator-id for a
11709                member are looked up in the containing scope.  */
11710             pushed_scope = push_scope (scope);
11711           parser->in_declarator_p = true;
11712           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11713               || (declarator && declarator->kind == cdk_id))
11714             /* Default args are only allowed on function
11715                declarations.  */
11716             parser->default_arg_ok_p = saved_default_arg_ok_p;
11717           else
11718             parser->default_arg_ok_p = false;
11719
11720           first = false;
11721         }
11722       /* We're done.  */
11723       else
11724         break;
11725     }
11726
11727   /* For an abstract declarator, we might wind up with nothing at this
11728      point.  That's an error; the declarator is not optional.  */
11729   if (!declarator)
11730     cp_parser_error (parser, "expected declarator");
11731
11732   /* If we entered a scope, we must exit it now.  */
11733   if (pushed_scope)
11734     pop_scope (pushed_scope);
11735
11736   parser->default_arg_ok_p = saved_default_arg_ok_p;
11737   parser->in_declarator_p = saved_in_declarator_p;
11738
11739   return declarator;
11740 }
11741
11742 /* Parse a ptr-operator.
11743
11744    ptr-operator:
11745      * cv-qualifier-seq [opt]
11746      &
11747      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11748
11749    GNU Extension:
11750
11751    ptr-operator:
11752      & cv-qualifier-seq [opt]
11753
11754    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11755    Returns ADDR_EXPR if a reference was used.  In the case of a
11756    pointer-to-member, *TYPE is filled in with the TYPE containing the
11757    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11758    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11759    ERROR_MARK if an error occurred.  */
11760
11761 static enum tree_code
11762 cp_parser_ptr_operator (cp_parser* parser,
11763                         tree* type,
11764                         cp_cv_quals *cv_quals)
11765 {
11766   enum tree_code code = ERROR_MARK;
11767   cp_token *token;
11768
11769   /* Assume that it's not a pointer-to-member.  */
11770   *type = NULL_TREE;
11771   /* And that there are no cv-qualifiers.  */
11772   *cv_quals = TYPE_UNQUALIFIED;
11773
11774   /* Peek at the next token.  */
11775   token = cp_lexer_peek_token (parser->lexer);
11776   /* If it's a `*' or `&' we have a pointer or reference.  */
11777   if (token->type == CPP_MULT || token->type == CPP_AND)
11778     {
11779       /* Remember which ptr-operator we were processing.  */
11780       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11781
11782       /* Consume the `*' or `&'.  */
11783       cp_lexer_consume_token (parser->lexer);
11784
11785       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11786          `&', if we are allowing GNU extensions.  (The only qualifier
11787          that can legally appear after `&' is `restrict', but that is
11788          enforced during semantic analysis.  */
11789       if (code == INDIRECT_REF
11790           || cp_parser_allow_gnu_extensions_p (parser))
11791         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11792     }
11793   else
11794     {
11795       /* Try the pointer-to-member case.  */
11796       cp_parser_parse_tentatively (parser);
11797       /* Look for the optional `::' operator.  */
11798       cp_parser_global_scope_opt (parser,
11799                                   /*current_scope_valid_p=*/false);
11800       /* Look for the nested-name specifier.  */
11801       cp_parser_nested_name_specifier (parser,
11802                                        /*typename_keyword_p=*/false,
11803                                        /*check_dependency_p=*/true,
11804                                        /*type_p=*/false,
11805                                        /*is_declaration=*/false);
11806       /* If we found it, and the next token is a `*', then we are
11807          indeed looking at a pointer-to-member operator.  */
11808       if (!cp_parser_error_occurred (parser)
11809           && cp_parser_require (parser, CPP_MULT, "`*'"))
11810         {
11811           /* Indicate that the `*' operator was used.  */
11812           code = INDIRECT_REF;
11813
11814           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11815             error ("%qD is a namespace", parser->scope);
11816           else
11817             {
11818               /* The type of which the member is a member is given by the
11819                  current SCOPE.  */
11820               *type = parser->scope;
11821               /* The next name will not be qualified.  */
11822               parser->scope = NULL_TREE;
11823               parser->qualifying_scope = NULL_TREE;
11824               parser->object_scope = NULL_TREE;
11825               /* Look for the optional cv-qualifier-seq.  */
11826               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11827             }
11828         }
11829       /* If that didn't work we don't have a ptr-operator.  */
11830       if (!cp_parser_parse_definitely (parser))
11831         cp_parser_error (parser, "expected ptr-operator");
11832     }
11833
11834   return code;
11835 }
11836
11837 /* Parse an (optional) cv-qualifier-seq.
11838
11839    cv-qualifier-seq:
11840      cv-qualifier cv-qualifier-seq [opt]
11841
11842    cv-qualifier:
11843      const
11844      volatile
11845
11846    GNU Extension:
11847
11848    cv-qualifier:
11849      __restrict__
11850
11851    Returns a bitmask representing the cv-qualifiers.  */
11852
11853 static cp_cv_quals
11854 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11855 {
11856   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11857
11858   while (true)
11859     {
11860       cp_token *token;
11861       cp_cv_quals cv_qualifier;
11862
11863       /* Peek at the next token.  */
11864       token = cp_lexer_peek_token (parser->lexer);
11865       /* See if it's a cv-qualifier.  */
11866       switch (token->keyword)
11867         {
11868         case RID_CONST:
11869           cv_qualifier = TYPE_QUAL_CONST;
11870           break;
11871
11872         case RID_VOLATILE:
11873           cv_qualifier = TYPE_QUAL_VOLATILE;
11874           break;
11875
11876         case RID_RESTRICT:
11877           cv_qualifier = TYPE_QUAL_RESTRICT;
11878           break;
11879
11880         default:
11881           cv_qualifier = TYPE_UNQUALIFIED;
11882           break;
11883         }
11884
11885       if (!cv_qualifier)
11886         break;
11887
11888       if (cv_quals & cv_qualifier)
11889         {
11890           error ("duplicate cv-qualifier");
11891           cp_lexer_purge_token (parser->lexer);
11892         }
11893       else
11894         {
11895           cp_lexer_consume_token (parser->lexer);
11896           cv_quals |= cv_qualifier;
11897         }
11898     }
11899
11900   return cv_quals;
11901 }
11902
11903 /* Parse a declarator-id.
11904
11905    declarator-id:
11906      id-expression
11907      :: [opt] nested-name-specifier [opt] type-name
11908
11909    In the `id-expression' case, the value returned is as for
11910    cp_parser_id_expression if the id-expression was an unqualified-id.
11911    If the id-expression was a qualified-id, then a SCOPE_REF is
11912    returned.  The first operand is the scope (either a NAMESPACE_DECL
11913    or TREE_TYPE), but the second is still just a representation of an
11914    unqualified-id.  */
11915
11916 static tree
11917 cp_parser_declarator_id (cp_parser* parser)
11918 {
11919   tree id;
11920   /* The expression must be an id-expression.  Assume that qualified
11921      names are the names of types so that:
11922
11923        template <class T>
11924        int S<T>::R::i = 3;
11925
11926      will work; we must treat `S<T>::R' as the name of a type.
11927      Similarly, assume that qualified names are templates, where
11928      required, so that:
11929
11930        template <class T>
11931        int S<T>::R<T>::i = 3;
11932
11933      will work, too.  */
11934   id = cp_parser_id_expression (parser,
11935                                 /*template_keyword_p=*/false,
11936                                 /*check_dependency_p=*/false,
11937                                 /*template_p=*/NULL,
11938                                 /*declarator_p=*/true);
11939   if (BASELINK_P (id))
11940     id = BASELINK_FUNCTIONS (id);
11941   return id;
11942 }
11943
11944 /* Parse a type-id.
11945
11946    type-id:
11947      type-specifier-seq abstract-declarator [opt]
11948
11949    Returns the TYPE specified.  */
11950
11951 static tree
11952 cp_parser_type_id (cp_parser* parser)
11953 {
11954   cp_decl_specifier_seq type_specifier_seq;
11955   cp_declarator *abstract_declarator;
11956
11957   /* Parse the type-specifier-seq.  */
11958   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11959                                 &type_specifier_seq);
11960   if (type_specifier_seq.type == error_mark_node)
11961     return error_mark_node;
11962
11963   /* There might or might not be an abstract declarator.  */
11964   cp_parser_parse_tentatively (parser);
11965   /* Look for the declarator.  */
11966   abstract_declarator
11967     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11968                             /*parenthesized_p=*/NULL,
11969                             /*member_p=*/false);
11970   /* Check to see if there really was a declarator.  */
11971   if (!cp_parser_parse_definitely (parser))
11972     abstract_declarator = NULL;
11973
11974   return groktypename (&type_specifier_seq, abstract_declarator);
11975 }
11976
11977 /* Parse a type-specifier-seq.
11978
11979    type-specifier-seq:
11980      type-specifier type-specifier-seq [opt]
11981
11982    GNU extension:
11983
11984    type-specifier-seq:
11985      attributes type-specifier-seq [opt]
11986
11987    If IS_CONDITION is true, we are at the start of a "condition",
11988    e.g., we've just seen "if (".
11989
11990    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11991
11992 static void
11993 cp_parser_type_specifier_seq (cp_parser* parser,
11994                               bool is_condition,
11995                               cp_decl_specifier_seq *type_specifier_seq)
11996 {
11997   bool seen_type_specifier = false;
11998   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11999
12000   /* Clear the TYPE_SPECIFIER_SEQ.  */
12001   clear_decl_specs (type_specifier_seq);
12002
12003   /* Parse the type-specifiers and attributes.  */
12004   while (true)
12005     {
12006       tree type_specifier;
12007       bool is_cv_qualifier;
12008
12009       /* Check for attributes first.  */
12010       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12011         {
12012           type_specifier_seq->attributes =
12013             chainon (type_specifier_seq->attributes,
12014                      cp_parser_attributes_opt (parser));
12015           continue;
12016         }
12017
12018       /* Look for the type-specifier.  */
12019       type_specifier = cp_parser_type_specifier (parser,
12020                                                  flags,
12021                                                  type_specifier_seq,
12022                                                  /*is_declaration=*/false,
12023                                                  NULL,
12024                                                  &is_cv_qualifier);
12025       if (!type_specifier)
12026         {
12027           /* If the first type-specifier could not be found, this is not a
12028              type-specifier-seq at all.  */
12029           if (!seen_type_specifier)
12030             {
12031               cp_parser_error (parser, "expected type-specifier");
12032               type_specifier_seq->type = error_mark_node;
12033               return;
12034             }
12035           /* If subsequent type-specifiers could not be found, the
12036              type-specifier-seq is complete.  */
12037           break;
12038         }
12039
12040       seen_type_specifier = true;
12041       /* The standard says that a condition can be:
12042
12043             type-specifier-seq declarator = assignment-expression
12044
12045          However, given:
12046
12047            struct S {};
12048            if (int S = ...)
12049
12050          we should treat the "S" as a declarator, not as a
12051          type-specifier.  The standard doesn't say that explicitly for
12052          type-specifier-seq, but it does say that for
12053          decl-specifier-seq in an ordinary declaration.  Perhaps it
12054          would be clearer just to allow a decl-specifier-seq here, and
12055          then add a semantic restriction that if any decl-specifiers
12056          that are not type-specifiers appear, the program is invalid.  */
12057       if (is_condition && !is_cv_qualifier)
12058         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12059     }
12060
12061   cp_parser_check_decl_spec (type_specifier_seq);
12062 }
12063
12064 /* Parse a parameter-declaration-clause.
12065
12066    parameter-declaration-clause:
12067      parameter-declaration-list [opt] ... [opt]
12068      parameter-declaration-list , ...
12069
12070    Returns a representation for the parameter declarations.  A return
12071    value of NULL indicates a parameter-declaration-clause consisting
12072    only of an ellipsis.  */
12073
12074 static cp_parameter_declarator *
12075 cp_parser_parameter_declaration_clause (cp_parser* parser)
12076 {
12077   cp_parameter_declarator *parameters;
12078   cp_token *token;
12079   bool ellipsis_p;
12080   bool is_error;
12081
12082   /* Peek at the next token.  */
12083   token = cp_lexer_peek_token (parser->lexer);
12084   /* Check for trivial parameter-declaration-clauses.  */
12085   if (token->type == CPP_ELLIPSIS)
12086     {
12087       /* Consume the `...' token.  */
12088       cp_lexer_consume_token (parser->lexer);
12089       return NULL;
12090     }
12091   else if (token->type == CPP_CLOSE_PAREN)
12092     /* There are no parameters.  */
12093     {
12094 #ifndef NO_IMPLICIT_EXTERN_C
12095       if (in_system_header && current_class_type == NULL
12096           && current_lang_name == lang_name_c)
12097         return NULL;
12098       else
12099 #endif
12100         return no_parameters;
12101     }
12102   /* Check for `(void)', too, which is a special case.  */
12103   else if (token->keyword == RID_VOID
12104            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12105                == CPP_CLOSE_PAREN))
12106     {
12107       /* Consume the `void' token.  */
12108       cp_lexer_consume_token (parser->lexer);
12109       /* There are no parameters.  */
12110       return no_parameters;
12111     }
12112
12113   /* Parse the parameter-declaration-list.  */
12114   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12115   /* If a parse error occurred while parsing the
12116      parameter-declaration-list, then the entire
12117      parameter-declaration-clause is erroneous.  */
12118   if (is_error)
12119     return NULL;
12120
12121   /* Peek at the next token.  */
12122   token = cp_lexer_peek_token (parser->lexer);
12123   /* If it's a `,', the clause should terminate with an ellipsis.  */
12124   if (token->type == CPP_COMMA)
12125     {
12126       /* Consume the `,'.  */
12127       cp_lexer_consume_token (parser->lexer);
12128       /* Expect an ellipsis.  */
12129       ellipsis_p
12130         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12131     }
12132   /* It might also be `...' if the optional trailing `,' was
12133      omitted.  */
12134   else if (token->type == CPP_ELLIPSIS)
12135     {
12136       /* Consume the `...' token.  */
12137       cp_lexer_consume_token (parser->lexer);
12138       /* And remember that we saw it.  */
12139       ellipsis_p = true;
12140     }
12141   else
12142     ellipsis_p = false;
12143
12144   /* Finish the parameter list.  */
12145   if (parameters && ellipsis_p)
12146     parameters->ellipsis_p = true;
12147
12148   return parameters;
12149 }
12150
12151 /* Parse a parameter-declaration-list.
12152
12153    parameter-declaration-list:
12154      parameter-declaration
12155      parameter-declaration-list , parameter-declaration
12156
12157    Returns a representation of the parameter-declaration-list, as for
12158    cp_parser_parameter_declaration_clause.  However, the
12159    `void_list_node' is never appended to the list.  Upon return,
12160    *IS_ERROR will be true iff an error occurred.  */
12161
12162 static cp_parameter_declarator *
12163 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12164 {
12165   cp_parameter_declarator *parameters = NULL;
12166   cp_parameter_declarator **tail = &parameters;
12167   bool saved_in_unbraced_linkage_specification_p;
12168
12169   /* Assume all will go well.  */
12170   *is_error = false;
12171   /* The special considerations that apply to a function within an
12172      unbraced linkage specifications do not apply to the parameters
12173      to the function.  */
12174   saved_in_unbraced_linkage_specification_p 
12175     = parser->in_unbraced_linkage_specification_p;
12176   parser->in_unbraced_linkage_specification_p = false;
12177
12178   /* Look for more parameters.  */
12179   while (true)
12180     {
12181       cp_parameter_declarator *parameter;
12182       bool parenthesized_p;
12183       /* Parse the parameter.  */
12184       parameter
12185         = cp_parser_parameter_declaration (parser,
12186                                            /*template_parm_p=*/false,
12187                                            &parenthesized_p);
12188
12189       /* If a parse error occurred parsing the parameter declaration,
12190          then the entire parameter-declaration-list is erroneous.  */
12191       if (!parameter)
12192         {
12193           *is_error = true;
12194           parameters = NULL;
12195           break;
12196         }
12197       /* Add the new parameter to the list.  */
12198       *tail = parameter;
12199       tail = &parameter->next;
12200
12201       /* Peek at the next token.  */
12202       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12203           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12204           /* These are for Objective-C++ */
12205           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12206           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12207         /* The parameter-declaration-list is complete.  */
12208         break;
12209       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12210         {
12211           cp_token *token;
12212
12213           /* Peek at the next token.  */
12214           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12215           /* If it's an ellipsis, then the list is complete.  */
12216           if (token->type == CPP_ELLIPSIS)
12217             break;
12218           /* Otherwise, there must be more parameters.  Consume the
12219              `,'.  */
12220           cp_lexer_consume_token (parser->lexer);
12221           /* When parsing something like:
12222
12223                 int i(float f, double d)
12224
12225              we can tell after seeing the declaration for "f" that we
12226              are not looking at an initialization of a variable "i",
12227              but rather at the declaration of a function "i".
12228
12229              Due to the fact that the parsing of template arguments
12230              (as specified to a template-id) requires backtracking we
12231              cannot use this technique when inside a template argument
12232              list.  */
12233           if (!parser->in_template_argument_list_p
12234               && !parser->in_type_id_in_expr_p
12235               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12236               /* However, a parameter-declaration of the form
12237                  "foat(f)" (which is a valid declaration of a
12238                  parameter "f") can also be interpreted as an
12239                  expression (the conversion of "f" to "float").  */
12240               && !parenthesized_p)
12241             cp_parser_commit_to_tentative_parse (parser);
12242         }
12243       else
12244         {
12245           cp_parser_error (parser, "expected %<,%> or %<...%>");
12246           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12247             cp_parser_skip_to_closing_parenthesis (parser,
12248                                                    /*recovering=*/true,
12249                                                    /*or_comma=*/false,
12250                                                    /*consume_paren=*/false);
12251           break;
12252         }
12253     }
12254
12255   parser->in_unbraced_linkage_specification_p
12256     = saved_in_unbraced_linkage_specification_p;
12257
12258   return parameters;
12259 }
12260
12261 /* Parse a parameter declaration.
12262
12263    parameter-declaration:
12264      decl-specifier-seq declarator
12265      decl-specifier-seq declarator = assignment-expression
12266      decl-specifier-seq abstract-declarator [opt]
12267      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12268
12269    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12270    declares a template parameter.  (In that case, a non-nested `>'
12271    token encountered during the parsing of the assignment-expression
12272    is not interpreted as a greater-than operator.)
12273
12274    Returns a representation of the parameter, or NULL if an error
12275    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12276    true iff the declarator is of the form "(p)".  */
12277
12278 static cp_parameter_declarator *
12279 cp_parser_parameter_declaration (cp_parser *parser,
12280                                  bool template_parm_p,
12281                                  bool *parenthesized_p)
12282 {
12283   int declares_class_or_enum;
12284   bool greater_than_is_operator_p;
12285   cp_decl_specifier_seq decl_specifiers;
12286   cp_declarator *declarator;
12287   tree default_argument;
12288   cp_token *token;
12289   const char *saved_message;
12290
12291   /* In a template parameter, `>' is not an operator.
12292
12293      [temp.param]
12294
12295      When parsing a default template-argument for a non-type
12296      template-parameter, the first non-nested `>' is taken as the end
12297      of the template parameter-list rather than a greater-than
12298      operator.  */
12299   greater_than_is_operator_p = !template_parm_p;
12300
12301   /* Type definitions may not appear in parameter types.  */
12302   saved_message = parser->type_definition_forbidden_message;
12303   parser->type_definition_forbidden_message
12304     = "types may not be defined in parameter types";
12305
12306   /* Parse the declaration-specifiers.  */
12307   cp_parser_decl_specifier_seq (parser,
12308                                 CP_PARSER_FLAGS_NONE,
12309                                 &decl_specifiers,
12310                                 &declares_class_or_enum);
12311   /* If an error occurred, there's no reason to attempt to parse the
12312      rest of the declaration.  */
12313   if (cp_parser_error_occurred (parser))
12314     {
12315       parser->type_definition_forbidden_message = saved_message;
12316       return NULL;
12317     }
12318
12319   /* Peek at the next token.  */
12320   token = cp_lexer_peek_token (parser->lexer);
12321   /* If the next token is a `)', `,', `=', `>', or `...', then there
12322      is no declarator.  */
12323   if (token->type == CPP_CLOSE_PAREN
12324       || token->type == CPP_COMMA
12325       || token->type == CPP_EQ
12326       || token->type == CPP_ELLIPSIS
12327       || token->type == CPP_GREATER)
12328     {
12329       declarator = NULL;
12330       if (parenthesized_p)
12331         *parenthesized_p = false;
12332     }
12333   /* Otherwise, there should be a declarator.  */
12334   else
12335     {
12336       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12337       parser->default_arg_ok_p = false;
12338
12339       /* After seeing a decl-specifier-seq, if the next token is not a
12340          "(", there is no possibility that the code is a valid
12341          expression.  Therefore, if parsing tentatively, we commit at
12342          this point.  */
12343       if (!parser->in_template_argument_list_p
12344           /* In an expression context, having seen:
12345
12346                (int((char ...
12347
12348              we cannot be sure whether we are looking at a
12349              function-type (taking a "char" as a parameter) or a cast
12350              of some object of type "char" to "int".  */
12351           && !parser->in_type_id_in_expr_p
12352           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12353           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12354         cp_parser_commit_to_tentative_parse (parser);
12355       /* Parse the declarator.  */
12356       declarator = cp_parser_declarator (parser,
12357                                          CP_PARSER_DECLARATOR_EITHER,
12358                                          /*ctor_dtor_or_conv_p=*/NULL,
12359                                          parenthesized_p,
12360                                          /*member_p=*/false);
12361       parser->default_arg_ok_p = saved_default_arg_ok_p;
12362       /* After the declarator, allow more attributes.  */
12363       decl_specifiers.attributes
12364         = chainon (decl_specifiers.attributes,
12365                    cp_parser_attributes_opt (parser));
12366     }
12367
12368   /* The restriction on defining new types applies only to the type
12369      of the parameter, not to the default argument.  */
12370   parser->type_definition_forbidden_message = saved_message;
12371
12372   /* If the next token is `=', then process a default argument.  */
12373   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12374     {
12375       bool saved_greater_than_is_operator_p;
12376       /* Consume the `='.  */
12377       cp_lexer_consume_token (parser->lexer);
12378
12379       /* If we are defining a class, then the tokens that make up the
12380          default argument must be saved and processed later.  */
12381       if (!template_parm_p && at_class_scope_p ()
12382           && TYPE_BEING_DEFINED (current_class_type))
12383         {
12384           unsigned depth = 0;
12385           cp_token *first_token;
12386           cp_token *token;
12387
12388           /* Add tokens until we have processed the entire default
12389              argument.  We add the range [first_token, token).  */
12390           first_token = cp_lexer_peek_token (parser->lexer);
12391           while (true)
12392             {
12393               bool done = false;
12394
12395               /* Peek at the next token.  */
12396               token = cp_lexer_peek_token (parser->lexer);
12397               /* What we do depends on what token we have.  */
12398               switch (token->type)
12399                 {
12400                   /* In valid code, a default argument must be
12401                      immediately followed by a `,' `)', or `...'.  */
12402                 case CPP_COMMA:
12403                 case CPP_CLOSE_PAREN:
12404                 case CPP_ELLIPSIS:
12405                   /* If we run into a non-nested `;', `}', or `]',
12406                      then the code is invalid -- but the default
12407                      argument is certainly over.  */
12408                 case CPP_SEMICOLON:
12409                 case CPP_CLOSE_BRACE:
12410                 case CPP_CLOSE_SQUARE:
12411                   if (depth == 0)
12412                     done = true;
12413                   /* Update DEPTH, if necessary.  */
12414                   else if (token->type == CPP_CLOSE_PAREN
12415                            || token->type == CPP_CLOSE_BRACE
12416                            || token->type == CPP_CLOSE_SQUARE)
12417                     --depth;
12418                   break;
12419
12420                 case CPP_OPEN_PAREN:
12421                 case CPP_OPEN_SQUARE:
12422                 case CPP_OPEN_BRACE:
12423                   ++depth;
12424                   break;
12425
12426                 case CPP_GREATER:
12427                   /* If we see a non-nested `>', and `>' is not an
12428                      operator, then it marks the end of the default
12429                      argument.  */
12430                   if (!depth && !greater_than_is_operator_p)
12431                     done = true;
12432                   break;
12433
12434                   /* If we run out of tokens, issue an error message.  */
12435                 case CPP_EOF:
12436                   error ("file ends in default argument");
12437                   done = true;
12438                   break;
12439
12440                 case CPP_NAME:
12441                 case CPP_SCOPE:
12442                   /* In these cases, we should look for template-ids.
12443                      For example, if the default argument is
12444                      `X<int, double>()', we need to do name lookup to
12445                      figure out whether or not `X' is a template; if
12446                      so, the `,' does not end the default argument.
12447
12448                      That is not yet done.  */
12449                   break;
12450
12451                 default:
12452                   break;
12453                 }
12454
12455               /* If we've reached the end, stop.  */
12456               if (done)
12457                 break;
12458
12459               /* Add the token to the token block.  */
12460               token = cp_lexer_consume_token (parser->lexer);
12461             }
12462
12463           /* Create a DEFAULT_ARG to represented the unparsed default
12464              argument.  */
12465           default_argument = make_node (DEFAULT_ARG);
12466           DEFARG_TOKENS (default_argument)
12467             = cp_token_cache_new (first_token, token);
12468           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12469         }
12470       /* Outside of a class definition, we can just parse the
12471          assignment-expression.  */
12472       else
12473         {
12474           bool saved_local_variables_forbidden_p;
12475
12476           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12477              set correctly.  */
12478           saved_greater_than_is_operator_p
12479             = parser->greater_than_is_operator_p;
12480           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12481           /* Local variable names (and the `this' keyword) may not
12482              appear in a default argument.  */
12483           saved_local_variables_forbidden_p
12484             = parser->local_variables_forbidden_p;
12485           parser->local_variables_forbidden_p = true;
12486           /* The default argument expression may cause implicitly
12487              defined member functions to be synthesized, which will
12488              result in garbage collection.  We must treat this
12489              situation as if we were within the body of function so as
12490              to avoid collecting live data on the stack.  */
12491           ++function_depth;
12492           /* Parse the assignment-expression.  */
12493           if (template_parm_p)
12494             push_deferring_access_checks (dk_no_deferred);
12495           default_argument
12496             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12497           if (template_parm_p)
12498             pop_deferring_access_checks ();
12499           /* Restore saved state.  */
12500           --function_depth;
12501           parser->greater_than_is_operator_p
12502             = saved_greater_than_is_operator_p;
12503           parser->local_variables_forbidden_p
12504             = saved_local_variables_forbidden_p;
12505         }
12506       if (!parser->default_arg_ok_p)
12507         {
12508           if (!flag_pedantic_errors)
12509             warning (0, "deprecated use of default argument for parameter of non-function");
12510           else
12511             {
12512               error ("default arguments are only permitted for function parameters");
12513               default_argument = NULL_TREE;
12514             }
12515         }
12516     }
12517   else
12518     default_argument = NULL_TREE;
12519
12520   return make_parameter_declarator (&decl_specifiers,
12521                                     declarator,
12522                                     default_argument);
12523 }
12524
12525 /* Parse a function-body.
12526
12527    function-body:
12528      compound_statement  */
12529
12530 static void
12531 cp_parser_function_body (cp_parser *parser)
12532 {
12533   cp_parser_compound_statement (parser, NULL, false);
12534 }
12535
12536 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12537    true if a ctor-initializer was present.  */
12538
12539 static bool
12540 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12541 {
12542   tree body;
12543   bool ctor_initializer_p;
12544
12545   /* Begin the function body.  */
12546   body = begin_function_body ();
12547   /* Parse the optional ctor-initializer.  */
12548   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12549   /* Parse the function-body.  */
12550   cp_parser_function_body (parser);
12551   /* Finish the function body.  */
12552   finish_function_body (body);
12553
12554   return ctor_initializer_p;
12555 }
12556
12557 /* Parse an initializer.
12558
12559    initializer:
12560      = initializer-clause
12561      ( expression-list )
12562
12563    Returns an expression representing the initializer.  If no
12564    initializer is present, NULL_TREE is returned.
12565
12566    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12567    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12568    set to FALSE if there is no initializer present.  If there is an
12569    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12570    is set to true; otherwise it is set to false.  */
12571
12572 static tree
12573 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12574                        bool* non_constant_p)
12575 {
12576   cp_token *token;
12577   tree init;
12578
12579   /* Peek at the next token.  */
12580   token = cp_lexer_peek_token (parser->lexer);
12581
12582   /* Let our caller know whether or not this initializer was
12583      parenthesized.  */
12584   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12585   /* Assume that the initializer is constant.  */
12586   *non_constant_p = false;
12587
12588   if (token->type == CPP_EQ)
12589     {
12590       /* Consume the `='.  */
12591       cp_lexer_consume_token (parser->lexer);
12592       /* Parse the initializer-clause.  */
12593       init = cp_parser_initializer_clause (parser, non_constant_p);
12594     }
12595   else if (token->type == CPP_OPEN_PAREN)
12596     init = cp_parser_parenthesized_expression_list (parser, false,
12597                                                     /*cast_p=*/false,
12598                                                     non_constant_p);
12599   else
12600     {
12601       /* Anything else is an error.  */
12602       cp_parser_error (parser, "expected initializer");
12603       init = error_mark_node;
12604     }
12605
12606   return init;
12607 }
12608
12609 /* Parse an initializer-clause.
12610
12611    initializer-clause:
12612      assignment-expression
12613      { initializer-list , [opt] }
12614      { }
12615
12616    Returns an expression representing the initializer.
12617
12618    If the `assignment-expression' production is used the value
12619    returned is simply a representation for the expression.
12620
12621    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12622    the elements of the initializer-list (or NULL, if the last
12623    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12624    NULL_TREE.  There is no way to detect whether or not the optional
12625    trailing `,' was provided.  NON_CONSTANT_P is as for
12626    cp_parser_initializer.  */
12627
12628 static tree
12629 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12630 {
12631   tree initializer;
12632
12633   /* Assume the expression is constant.  */
12634   *non_constant_p = false;
12635
12636   /* If it is not a `{', then we are looking at an
12637      assignment-expression.  */
12638   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12639     {
12640       initializer
12641         = cp_parser_constant_expression (parser,
12642                                         /*allow_non_constant_p=*/true,
12643                                         non_constant_p);
12644       if (!*non_constant_p)
12645         initializer = fold_non_dependent_expr (initializer);
12646     }
12647   else
12648     {
12649       /* Consume the `{' token.  */
12650       cp_lexer_consume_token (parser->lexer);
12651       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12652       initializer = make_node (CONSTRUCTOR);
12653       /* If it's not a `}', then there is a non-trivial initializer.  */
12654       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12655         {
12656           /* Parse the initializer list.  */
12657           CONSTRUCTOR_ELTS (initializer)
12658             = cp_parser_initializer_list (parser, non_constant_p);
12659           /* A trailing `,' token is allowed.  */
12660           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12661             cp_lexer_consume_token (parser->lexer);
12662         }
12663       /* Now, there should be a trailing `}'.  */
12664       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12665     }
12666
12667   return initializer;
12668 }
12669
12670 /* Parse an initializer-list.
12671
12672    initializer-list:
12673      initializer-clause
12674      initializer-list , initializer-clause
12675
12676    GNU Extension:
12677
12678    initializer-list:
12679      identifier : initializer-clause
12680      initializer-list, identifier : initializer-clause
12681
12682    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12683    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12684    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12685    as for cp_parser_initializer.  */
12686
12687 static VEC(constructor_elt,gc) *
12688 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12689 {
12690   VEC(constructor_elt,gc) *v = NULL;
12691
12692   /* Assume all of the expressions are constant.  */
12693   *non_constant_p = false;
12694
12695   /* Parse the rest of the list.  */
12696   while (true)
12697     {
12698       cp_token *token;
12699       tree identifier;
12700       tree initializer;
12701       bool clause_non_constant_p;
12702
12703       /* If the next token is an identifier and the following one is a
12704          colon, we are looking at the GNU designated-initializer
12705          syntax.  */
12706       if (cp_parser_allow_gnu_extensions_p (parser)
12707           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12708           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12709         {
12710           /* Consume the identifier.  */
12711           identifier = cp_lexer_consume_token (parser->lexer)->value;
12712           /* Consume the `:'.  */
12713           cp_lexer_consume_token (parser->lexer);
12714         }
12715       else
12716         identifier = NULL_TREE;
12717
12718       /* Parse the initializer.  */
12719       initializer = cp_parser_initializer_clause (parser,
12720                                                   &clause_non_constant_p);
12721       /* If any clause is non-constant, so is the entire initializer.  */
12722       if (clause_non_constant_p)
12723         *non_constant_p = true;
12724
12725       /* Add it to the vector.  */
12726       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12727
12728       /* If the next token is not a comma, we have reached the end of
12729          the list.  */
12730       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12731         break;
12732
12733       /* Peek at the next token.  */
12734       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12735       /* If the next token is a `}', then we're still done.  An
12736          initializer-clause can have a trailing `,' after the
12737          initializer-list and before the closing `}'.  */
12738       if (token->type == CPP_CLOSE_BRACE)
12739         break;
12740
12741       /* Consume the `,' token.  */
12742       cp_lexer_consume_token (parser->lexer);
12743     }
12744
12745   return v;
12746 }
12747
12748 /* Classes [gram.class] */
12749
12750 /* Parse a class-name.
12751
12752    class-name:
12753      identifier
12754      template-id
12755
12756    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12757    to indicate that names looked up in dependent types should be
12758    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12759    keyword has been used to indicate that the name that appears next
12760    is a template.  TAG_TYPE indicates the explicit tag given before
12761    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12762    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12763    is the class being defined in a class-head.
12764
12765    Returns the TYPE_DECL representing the class.  */
12766
12767 static tree
12768 cp_parser_class_name (cp_parser *parser,
12769                       bool typename_keyword_p,
12770                       bool template_keyword_p,
12771                       enum tag_types tag_type,
12772                       bool check_dependency_p,
12773                       bool class_head_p,
12774                       bool is_declaration)
12775 {
12776   tree decl;
12777   tree scope;
12778   bool typename_p;
12779   cp_token *token;
12780
12781   /* All class-names start with an identifier.  */
12782   token = cp_lexer_peek_token (parser->lexer);
12783   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12784     {
12785       cp_parser_error (parser, "expected class-name");
12786       return error_mark_node;
12787     }
12788
12789   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12790      to a template-id, so we save it here.  */
12791   scope = parser->scope;
12792   if (scope == error_mark_node)
12793     return error_mark_node;
12794
12795   /* Any name names a type if we're following the `typename' keyword
12796      in a qualified name where the enclosing scope is type-dependent.  */
12797   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12798                 && dependent_type_p (scope));
12799   /* Handle the common case (an identifier, but not a template-id)
12800      efficiently.  */
12801   if (token->type == CPP_NAME
12802       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12803     {
12804       cp_token *identifier_token;
12805       tree identifier;
12806       bool ambiguous_p;
12807
12808       /* Look for the identifier.  */
12809       identifier_token = cp_lexer_peek_token (parser->lexer);
12810       ambiguous_p = identifier_token->ambiguous_p;
12811       identifier = cp_parser_identifier (parser);
12812       /* If the next token isn't an identifier, we are certainly not
12813          looking at a class-name.  */
12814       if (identifier == error_mark_node)
12815         decl = error_mark_node;
12816       /* If we know this is a type-name, there's no need to look it
12817          up.  */
12818       else if (typename_p)
12819         decl = identifier;
12820       else
12821         {
12822           tree ambiguous_decls;
12823           /* If we already know that this lookup is ambiguous, then
12824              we've already issued an error message; there's no reason
12825              to check again.  */
12826           if (ambiguous_p)
12827             {
12828               cp_parser_simulate_error (parser);
12829               return error_mark_node;
12830             }
12831           /* If the next token is a `::', then the name must be a type
12832              name.
12833
12834              [basic.lookup.qual]
12835
12836              During the lookup for a name preceding the :: scope
12837              resolution operator, object, function, and enumerator
12838              names are ignored.  */
12839           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12840             tag_type = typename_type;
12841           /* Look up the name.  */
12842           decl = cp_parser_lookup_name (parser, identifier,
12843                                         tag_type,
12844                                         /*is_template=*/false,
12845                                         /*is_namespace=*/false,
12846                                         check_dependency_p,
12847                                         &ambiguous_decls);
12848           if (ambiguous_decls)
12849             {
12850               error ("reference to %qD is ambiguous", identifier);
12851               print_candidates (ambiguous_decls);
12852               if (cp_parser_parsing_tentatively (parser))
12853                 {
12854                   identifier_token->ambiguous_p = true;
12855                   cp_parser_simulate_error (parser);
12856                 }
12857               return error_mark_node;
12858             }
12859         }
12860     }
12861   else
12862     {
12863       /* Try a template-id.  */
12864       decl = cp_parser_template_id (parser, template_keyword_p,
12865                                     check_dependency_p,
12866                                     is_declaration);
12867       if (decl == error_mark_node)
12868         return error_mark_node;
12869     }
12870
12871   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12872
12873   /* If this is a typename, create a TYPENAME_TYPE.  */
12874   if (typename_p && decl != error_mark_node)
12875     {
12876       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12877       if (decl != error_mark_node)
12878         decl = TYPE_NAME (decl);
12879     }
12880
12881   /* Check to see that it is really the name of a class.  */
12882   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12883       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12884       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12885     /* Situations like this:
12886
12887          template <typename T> struct A {
12888            typename T::template X<int>::I i;
12889          };
12890
12891        are problematic.  Is `T::template X<int>' a class-name?  The
12892        standard does not seem to be definitive, but there is no other
12893        valid interpretation of the following `::'.  Therefore, those
12894        names are considered class-names.  */
12895     {
12896       decl = make_typename_type (scope, decl, tag_type, tf_error);
12897       if (decl != error_mark_node)
12898         decl = TYPE_NAME (decl);
12899     }
12900   else if (TREE_CODE (decl) != TYPE_DECL
12901            || TREE_TYPE (decl) == error_mark_node
12902            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12903     decl = error_mark_node;
12904
12905   if (decl == error_mark_node)
12906     cp_parser_error (parser, "expected class-name");
12907
12908   return decl;
12909 }
12910
12911 /* Parse a class-specifier.
12912
12913    class-specifier:
12914      class-head { member-specification [opt] }
12915
12916    Returns the TREE_TYPE representing the class.  */
12917
12918 static tree
12919 cp_parser_class_specifier (cp_parser* parser)
12920 {
12921   cp_token *token;
12922   tree type;
12923   tree attributes = NULL_TREE;
12924   int has_trailing_semicolon;
12925   bool nested_name_specifier_p;
12926   unsigned saved_num_template_parameter_lists;
12927   bool saved_in_function_body;
12928   tree old_scope = NULL_TREE;
12929   tree scope = NULL_TREE;
12930   tree bases = NULL_TREE;
12931
12932   push_deferring_access_checks (dk_no_deferred);
12933
12934   /* Parse the class-head.  */
12935   type = cp_parser_class_head (parser,
12936                                &nested_name_specifier_p,
12937                                &attributes,
12938                                &bases);
12939   /* If the class-head was a semantic disaster, skip the entire body
12940      of the class.  */
12941   if (!type)
12942     {
12943       cp_parser_skip_to_end_of_block_or_statement (parser);
12944       pop_deferring_access_checks ();
12945       return error_mark_node;
12946     }
12947
12948   /* Look for the `{'.  */
12949   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12950     {
12951       pop_deferring_access_checks ();
12952       return error_mark_node;
12953     }
12954
12955   /* Process the base classes.  */
12956   xref_basetypes (type, bases);
12957
12958   /* Issue an error message if type-definitions are forbidden here.  */
12959   cp_parser_check_type_definition (parser);
12960   /* Remember that we are defining one more class.  */
12961   ++parser->num_classes_being_defined;
12962   /* Inside the class, surrounding template-parameter-lists do not
12963      apply.  */
12964   saved_num_template_parameter_lists
12965     = parser->num_template_parameter_lists;
12966   parser->num_template_parameter_lists = 0;
12967   /* We are not in a function body.  */
12968   saved_in_function_body = parser->in_function_body;
12969   parser->in_function_body = false;
12970
12971   /* Start the class.  */
12972   if (nested_name_specifier_p)
12973     {
12974       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12975       old_scope = push_inner_scope (scope);
12976     }
12977   type = begin_class_definition (type);
12978
12979   if (type == error_mark_node)
12980     /* If the type is erroneous, skip the entire body of the class.  */
12981     cp_parser_skip_to_closing_brace (parser);
12982   else
12983     /* Parse the member-specification.  */
12984     cp_parser_member_specification_opt (parser);
12985
12986   /* Look for the trailing `}'.  */
12987   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12988   /* We get better error messages by noticing a common problem: a
12989      missing trailing `;'.  */
12990   token = cp_lexer_peek_token (parser->lexer);
12991   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12992   /* Look for trailing attributes to apply to this class.  */
12993   if (cp_parser_allow_gnu_extensions_p (parser))
12994     {
12995       tree sub_attr = cp_parser_attributes_opt (parser);
12996       attributes = chainon (attributes, sub_attr);
12997     }
12998   if (type != error_mark_node)
12999     type = finish_struct (type, attributes);
13000   if (nested_name_specifier_p)
13001     pop_inner_scope (old_scope, scope);
13002   /* If this class is not itself within the scope of another class,
13003      then we need to parse the bodies of all of the queued function
13004      definitions.  Note that the queued functions defined in a class
13005      are not always processed immediately following the
13006      class-specifier for that class.  Consider:
13007
13008        struct A {
13009          struct B { void f() { sizeof (A); } };
13010        };
13011
13012      If `f' were processed before the processing of `A' were
13013      completed, there would be no way to compute the size of `A'.
13014      Note that the nesting we are interested in here is lexical --
13015      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13016      for:
13017
13018        struct A { struct B; };
13019        struct A::B { void f() { } };
13020
13021      there is no need to delay the parsing of `A::B::f'.  */
13022   if (--parser->num_classes_being_defined == 0)
13023     {
13024       tree queue_entry;
13025       tree fn;
13026       tree class_type = NULL_TREE;
13027       tree pushed_scope = NULL_TREE;
13028  
13029       /* In a first pass, parse default arguments to the functions.
13030          Then, in a second pass, parse the bodies of the functions.
13031          This two-phased approach handles cases like:
13032
13033             struct S {
13034               void f() { g(); }
13035               void g(int i = 3);
13036             };
13037
13038          */
13039       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13040              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13041            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13042            TREE_PURPOSE (parser->unparsed_functions_queues)
13043              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13044         {
13045           fn = TREE_VALUE (queue_entry);
13046           /* If there are default arguments that have not yet been processed,
13047              take care of them now.  */
13048           if (class_type != TREE_PURPOSE (queue_entry))
13049             {
13050               if (pushed_scope)
13051                 pop_scope (pushed_scope);
13052               class_type = TREE_PURPOSE (queue_entry);
13053               pushed_scope = push_scope (class_type);
13054             }
13055           /* Make sure that any template parameters are in scope.  */
13056           maybe_begin_member_template_processing (fn);
13057           /* Parse the default argument expressions.  */
13058           cp_parser_late_parsing_default_args (parser, fn);
13059           /* Remove any template parameters from the symbol table.  */
13060           maybe_end_member_template_processing ();
13061         }
13062       if (pushed_scope)
13063         pop_scope (pushed_scope);
13064       /* Now parse the body of the functions.  */
13065       for (TREE_VALUE (parser->unparsed_functions_queues)
13066              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13067            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13068            TREE_VALUE (parser->unparsed_functions_queues)
13069              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13070         {
13071           /* Figure out which function we need to process.  */
13072           fn = TREE_VALUE (queue_entry);
13073           /* Parse the function.  */
13074           cp_parser_late_parsing_for_member (parser, fn);
13075         }
13076     }
13077
13078   /* Put back any saved access checks.  */
13079   pop_deferring_access_checks ();
13080
13081   /* Restore saved state.  */
13082   parser->in_function_body = saved_in_function_body;
13083   parser->num_template_parameter_lists
13084     = saved_num_template_parameter_lists;
13085
13086   return type;
13087 }
13088
13089 /* Parse a class-head.
13090
13091    class-head:
13092      class-key identifier [opt] base-clause [opt]
13093      class-key nested-name-specifier identifier base-clause [opt]
13094      class-key nested-name-specifier [opt] template-id
13095        base-clause [opt]
13096
13097    GNU Extensions:
13098      class-key attributes identifier [opt] base-clause [opt]
13099      class-key attributes nested-name-specifier identifier base-clause [opt]
13100      class-key attributes nested-name-specifier [opt] template-id
13101        base-clause [opt]
13102
13103    Returns the TYPE of the indicated class.  Sets
13104    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13105    involving a nested-name-specifier was used, and FALSE otherwise.
13106
13107    Returns error_mark_node if this is not a class-head.
13108
13109    Returns NULL_TREE if the class-head is syntactically valid, but
13110    semantically invalid in a way that means we should skip the entire
13111    body of the class.  */
13112
13113 static tree
13114 cp_parser_class_head (cp_parser* parser,
13115                       bool* nested_name_specifier_p,
13116                       tree *attributes_p,
13117                       tree *bases)
13118 {
13119   tree nested_name_specifier;
13120   enum tag_types class_key;
13121   tree id = NULL_TREE;
13122   tree type = NULL_TREE;
13123   tree attributes;
13124   bool template_id_p = false;
13125   bool qualified_p = false;
13126   bool invalid_nested_name_p = false;
13127   bool invalid_explicit_specialization_p = false;
13128   tree pushed_scope = NULL_TREE;
13129   unsigned num_templates;
13130
13131   /* Assume no nested-name-specifier will be present.  */
13132   *nested_name_specifier_p = false;
13133   /* Assume no template parameter lists will be used in defining the
13134      type.  */
13135   num_templates = 0;
13136
13137   /* Look for the class-key.  */
13138   class_key = cp_parser_class_key (parser);
13139   if (class_key == none_type)
13140     return error_mark_node;
13141
13142   /* Parse the attributes.  */
13143   attributes = cp_parser_attributes_opt (parser);
13144
13145   /* If the next token is `::', that is invalid -- but sometimes
13146      people do try to write:
13147
13148        struct ::S {};
13149
13150      Handle this gracefully by accepting the extra qualifier, and then
13151      issuing an error about it later if this really is a
13152      class-head.  If it turns out just to be an elaborated type
13153      specifier, remain silent.  */
13154   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13155     qualified_p = true;
13156
13157   push_deferring_access_checks (dk_no_check);
13158
13159   /* Determine the name of the class.  Begin by looking for an
13160      optional nested-name-specifier.  */
13161   nested_name_specifier
13162     = cp_parser_nested_name_specifier_opt (parser,
13163                                            /*typename_keyword_p=*/false,
13164                                            /*check_dependency_p=*/false,
13165                                            /*type_p=*/false,
13166                                            /*is_declaration=*/false);
13167   /* If there was a nested-name-specifier, then there *must* be an
13168      identifier.  */
13169   if (nested_name_specifier)
13170     {
13171       /* Although the grammar says `identifier', it really means
13172          `class-name' or `template-name'.  You are only allowed to
13173          define a class that has already been declared with this
13174          syntax.
13175
13176          The proposed resolution for Core Issue 180 says that whever
13177          you see `class T::X' you should treat `X' as a type-name.
13178
13179          It is OK to define an inaccessible class; for example:
13180
13181            class A { class B; };
13182            class A::B {};
13183
13184          We do not know if we will see a class-name, or a
13185          template-name.  We look for a class-name first, in case the
13186          class-name is a template-id; if we looked for the
13187          template-name first we would stop after the template-name.  */
13188       cp_parser_parse_tentatively (parser);
13189       type = cp_parser_class_name (parser,
13190                                    /*typename_keyword_p=*/false,
13191                                    /*template_keyword_p=*/false,
13192                                    class_type,
13193                                    /*check_dependency_p=*/false,
13194                                    /*class_head_p=*/true,
13195                                    /*is_declaration=*/false);
13196       /* If that didn't work, ignore the nested-name-specifier.  */
13197       if (!cp_parser_parse_definitely (parser))
13198         {
13199           invalid_nested_name_p = true;
13200           id = cp_parser_identifier (parser);
13201           if (id == error_mark_node)
13202             id = NULL_TREE;
13203         }
13204       /* If we could not find a corresponding TYPE, treat this
13205          declaration like an unqualified declaration.  */
13206       if (type == error_mark_node)
13207         nested_name_specifier = NULL_TREE;
13208       /* Otherwise, count the number of templates used in TYPE and its
13209          containing scopes.  */
13210       else
13211         {
13212           tree scope;
13213
13214           for (scope = TREE_TYPE (type);
13215                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13216                scope = (TYPE_P (scope)
13217                         ? TYPE_CONTEXT (scope)
13218                         : DECL_CONTEXT (scope)))
13219             if (TYPE_P (scope)
13220                 && CLASS_TYPE_P (scope)
13221                 && CLASSTYPE_TEMPLATE_INFO (scope)
13222                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13223                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13224               ++num_templates;
13225         }
13226     }
13227   /* Otherwise, the identifier is optional.  */
13228   else
13229     {
13230       /* We don't know whether what comes next is a template-id,
13231          an identifier, or nothing at all.  */
13232       cp_parser_parse_tentatively (parser);
13233       /* Check for a template-id.  */
13234       id = cp_parser_template_id (parser,
13235                                   /*template_keyword_p=*/false,
13236                                   /*check_dependency_p=*/true,
13237                                   /*is_declaration=*/true);
13238       /* If that didn't work, it could still be an identifier.  */
13239       if (!cp_parser_parse_definitely (parser))
13240         {
13241           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13242             id = cp_parser_identifier (parser);
13243           else
13244             id = NULL_TREE;
13245         }
13246       else
13247         {
13248           template_id_p = true;
13249           ++num_templates;
13250         }
13251     }
13252
13253   pop_deferring_access_checks ();
13254
13255   if (id)
13256     cp_parser_check_for_invalid_template_id (parser, id);
13257
13258   /* If it's not a `:' or a `{' then we can't really be looking at a
13259      class-head, since a class-head only appears as part of a
13260      class-specifier.  We have to detect this situation before calling
13261      xref_tag, since that has irreversible side-effects.  */
13262   if (!cp_parser_next_token_starts_class_definition_p (parser))
13263     {
13264       cp_parser_error (parser, "expected %<{%> or %<:%>");
13265       return error_mark_node;
13266     }
13267
13268   /* At this point, we're going ahead with the class-specifier, even
13269      if some other problem occurs.  */
13270   cp_parser_commit_to_tentative_parse (parser);
13271   /* Issue the error about the overly-qualified name now.  */
13272   if (qualified_p)
13273     cp_parser_error (parser,
13274                      "global qualification of class name is invalid");
13275   else if (invalid_nested_name_p)
13276     cp_parser_error (parser,
13277                      "qualified name does not name a class");
13278   else if (nested_name_specifier)
13279     {
13280       tree scope;
13281
13282       /* Reject typedef-names in class heads.  */
13283       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13284         {
13285           error ("invalid class name in declaration of %qD", type);
13286           type = NULL_TREE;
13287           goto done;
13288         }
13289
13290       /* Figure out in what scope the declaration is being placed.  */
13291       scope = current_scope ();
13292       /* If that scope does not contain the scope in which the
13293          class was originally declared, the program is invalid.  */
13294       if (scope && !is_ancestor (scope, nested_name_specifier))
13295         {
13296           error ("declaration of %qD in %qD which does not enclose %qD",
13297                  type, scope, nested_name_specifier);
13298           type = NULL_TREE;
13299           goto done;
13300         }
13301       /* [dcl.meaning]
13302
13303          A declarator-id shall not be qualified exception of the
13304          definition of a ... nested class outside of its class
13305          ... [or] a the definition or explicit instantiation of a
13306          class member of a namespace outside of its namespace.  */
13307       if (scope == nested_name_specifier)
13308         {
13309           pedwarn ("extra qualification ignored");
13310           nested_name_specifier = NULL_TREE;
13311           num_templates = 0;
13312         }
13313     }
13314   /* An explicit-specialization must be preceded by "template <>".  If
13315      it is not, try to recover gracefully.  */
13316   if (at_namespace_scope_p ()
13317       && parser->num_template_parameter_lists == 0
13318       && template_id_p)
13319     {
13320       error ("an explicit specialization must be preceded by %<template <>%>");
13321       invalid_explicit_specialization_p = true;
13322       /* Take the same action that would have been taken by
13323          cp_parser_explicit_specialization.  */
13324       ++parser->num_template_parameter_lists;
13325       begin_specialization ();
13326     }
13327   /* There must be no "return" statements between this point and the
13328      end of this function; set "type "to the correct return value and
13329      use "goto done;" to return.  */
13330   /* Make sure that the right number of template parameters were
13331      present.  */
13332   if (!cp_parser_check_template_parameters (parser, num_templates))
13333     {
13334       /* If something went wrong, there is no point in even trying to
13335          process the class-definition.  */
13336       type = NULL_TREE;
13337       goto done;
13338     }
13339
13340   /* Look up the type.  */
13341   if (template_id_p)
13342     {
13343       type = TREE_TYPE (id);
13344       type = maybe_process_partial_specialization (type);
13345       if (nested_name_specifier)
13346         pushed_scope = push_scope (nested_name_specifier);
13347     }
13348   else if (nested_name_specifier)
13349     {
13350       tree class_type;
13351
13352       /* Given:
13353
13354             template <typename T> struct S { struct T };
13355             template <typename T> struct S<T>::T { };
13356
13357          we will get a TYPENAME_TYPE when processing the definition of
13358          `S::T'.  We need to resolve it to the actual type before we
13359          try to define it.  */
13360       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13361         {
13362           class_type = resolve_typename_type (TREE_TYPE (type),
13363                                               /*only_current_p=*/false);
13364           if (class_type != error_mark_node)
13365             type = TYPE_NAME (class_type);
13366           else
13367             {
13368               cp_parser_error (parser, "could not resolve typename type");
13369               type = error_mark_node;
13370             }
13371         }
13372
13373       maybe_process_partial_specialization (TREE_TYPE (type));
13374       class_type = current_class_type;
13375       /* Enter the scope indicated by the nested-name-specifier.  */
13376       pushed_scope = push_scope (nested_name_specifier);
13377       /* Get the canonical version of this type.  */
13378       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13379       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13380           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13381         {
13382           type = push_template_decl (type);
13383           if (type == error_mark_node)
13384             {
13385               type = NULL_TREE;
13386               goto done;
13387             }
13388         }
13389
13390       type = TREE_TYPE (type);
13391       *nested_name_specifier_p = true;
13392     }
13393   else      /* The name is not a nested name.  */
13394     {
13395       /* If the class was unnamed, create a dummy name.  */
13396       if (!id)
13397         id = make_anon_name ();
13398       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13399                        parser->num_template_parameter_lists);
13400     }
13401
13402   /* Indicate whether this class was declared as a `class' or as a
13403      `struct'.  */
13404   if (TREE_CODE (type) == RECORD_TYPE)
13405     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13406   cp_parser_check_class_key (class_key, type);
13407
13408   /* If this type was already complete, and we see another definition,
13409      that's an error.  */
13410   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13411     {
13412       error ("redefinition of %q#T", type);
13413       error ("previous definition of %q+#T", type);
13414       type = NULL_TREE;
13415       goto done;
13416     }
13417
13418   /* We will have entered the scope containing the class; the names of
13419      base classes should be looked up in that context.  For example:
13420
13421        struct A { struct B {}; struct C; };
13422        struct A::C : B {};
13423
13424      is valid.  */
13425   *bases = NULL_TREE;
13426
13427   /* Get the list of base-classes, if there is one.  */
13428   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13429     *bases = cp_parser_base_clause (parser);
13430
13431  done:
13432   /* Leave the scope given by the nested-name-specifier.  We will
13433      enter the class scope itself while processing the members.  */
13434   if (pushed_scope)
13435     pop_scope (pushed_scope);
13436
13437   if (invalid_explicit_specialization_p)
13438     {
13439       end_specialization ();
13440       --parser->num_template_parameter_lists;
13441     }
13442   *attributes_p = attributes;
13443   return type;
13444 }
13445
13446 /* Parse a class-key.
13447
13448    class-key:
13449      class
13450      struct
13451      union
13452
13453    Returns the kind of class-key specified, or none_type to indicate
13454    error.  */
13455
13456 static enum tag_types
13457 cp_parser_class_key (cp_parser* parser)
13458 {
13459   cp_token *token;
13460   enum tag_types tag_type;
13461
13462   /* Look for the class-key.  */
13463   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13464   if (!token)
13465     return none_type;
13466
13467   /* Check to see if the TOKEN is a class-key.  */
13468   tag_type = cp_parser_token_is_class_key (token);
13469   if (!tag_type)
13470     cp_parser_error (parser, "expected class-key");
13471   return tag_type;
13472 }
13473
13474 /* Parse an (optional) member-specification.
13475
13476    member-specification:
13477      member-declaration member-specification [opt]
13478      access-specifier : member-specification [opt]  */
13479
13480 static void
13481 cp_parser_member_specification_opt (cp_parser* parser)
13482 {
13483   while (true)
13484     {
13485       cp_token *token;
13486       enum rid keyword;
13487
13488       /* Peek at the next token.  */
13489       token = cp_lexer_peek_token (parser->lexer);
13490       /* If it's a `}', or EOF then we've seen all the members.  */
13491       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13492         break;
13493
13494       /* See if this token is a keyword.  */
13495       keyword = token->keyword;
13496       switch (keyword)
13497         {
13498         case RID_PUBLIC:
13499         case RID_PROTECTED:
13500         case RID_PRIVATE:
13501           /* Consume the access-specifier.  */
13502           cp_lexer_consume_token (parser->lexer);
13503           /* Remember which access-specifier is active.  */
13504           current_access_specifier = token->value;
13505           /* Look for the `:'.  */
13506           cp_parser_require (parser, CPP_COLON, "`:'");
13507           break;
13508
13509         default:
13510           /* Accept #pragmas at class scope.  */
13511           if (token->type == CPP_PRAGMA)
13512             {
13513               cp_lexer_handle_pragma (parser->lexer);
13514               break;
13515             }
13516
13517           /* Otherwise, the next construction must be a
13518              member-declaration.  */
13519           cp_parser_member_declaration (parser);
13520         }
13521     }
13522 }
13523
13524 /* Parse a member-declaration.
13525
13526    member-declaration:
13527      decl-specifier-seq [opt] member-declarator-list [opt] ;
13528      function-definition ; [opt]
13529      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13530      using-declaration
13531      template-declaration
13532
13533    member-declarator-list:
13534      member-declarator
13535      member-declarator-list , member-declarator
13536
13537    member-declarator:
13538      declarator pure-specifier [opt]
13539      declarator constant-initializer [opt]
13540      identifier [opt] : constant-expression
13541
13542    GNU Extensions:
13543
13544    member-declaration:
13545      __extension__ member-declaration
13546
13547    member-declarator:
13548      declarator attributes [opt] pure-specifier [opt]
13549      declarator attributes [opt] constant-initializer [opt]
13550      identifier [opt] attributes [opt] : constant-expression  */
13551
13552 static void
13553 cp_parser_member_declaration (cp_parser* parser)
13554 {
13555   cp_decl_specifier_seq decl_specifiers;
13556   tree prefix_attributes;
13557   tree decl;
13558   int declares_class_or_enum;
13559   bool friend_p;
13560   cp_token *token;
13561   int saved_pedantic;
13562
13563   /* Check for the `__extension__' keyword.  */
13564   if (cp_parser_extension_opt (parser, &saved_pedantic))
13565     {
13566       /* Recurse.  */
13567       cp_parser_member_declaration (parser);
13568       /* Restore the old value of the PEDANTIC flag.  */
13569       pedantic = saved_pedantic;
13570
13571       return;
13572     }
13573
13574   /* Check for a template-declaration.  */
13575   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13576     {
13577       /* An explicit specialization here is an error condition, and we
13578          expect the specialization handler to detect and report this.  */
13579       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13580           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13581         cp_parser_explicit_specialization (parser);
13582       else
13583         cp_parser_template_declaration (parser, /*member_p=*/true);
13584
13585       return;
13586     }
13587
13588   /* Check for a using-declaration.  */
13589   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13590     {
13591       /* Parse the using-declaration.  */
13592       cp_parser_using_declaration (parser,
13593                                    /*access_declaration_p=*/false);
13594       return;
13595     }
13596
13597   /* Check for @defs.  */
13598   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13599     {
13600       tree ivar, member;
13601       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13602       ivar = ivar_chains;
13603       while (ivar)
13604         {
13605           member = ivar;
13606           ivar = TREE_CHAIN (member);
13607           TREE_CHAIN (member) = NULL_TREE;
13608           finish_member_declaration (member);
13609         }
13610       return;
13611     }
13612
13613   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13614     return;
13615
13616   /* Parse the decl-specifier-seq.  */
13617   cp_parser_decl_specifier_seq (parser,
13618                                 CP_PARSER_FLAGS_OPTIONAL,
13619                                 &decl_specifiers,
13620                                 &declares_class_or_enum);
13621   prefix_attributes = decl_specifiers.attributes;
13622   decl_specifiers.attributes = NULL_TREE;
13623   /* Check for an invalid type-name.  */
13624   if (!decl_specifiers.type
13625       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13626     return;
13627   /* If there is no declarator, then the decl-specifier-seq should
13628      specify a type.  */
13629   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13630     {
13631       /* If there was no decl-specifier-seq, and the next token is a
13632          `;', then we have something like:
13633
13634            struct S { ; };
13635
13636          [class.mem]
13637
13638          Each member-declaration shall declare at least one member
13639          name of the class.  */
13640       if (!decl_specifiers.any_specifiers_p)
13641         {
13642           cp_token *token = cp_lexer_peek_token (parser->lexer);
13643           if (pedantic && !token->in_system_header)
13644             pedwarn ("%Hextra %<;%>", &token->location);
13645         }
13646       else
13647         {
13648           tree type;
13649
13650           /* See if this declaration is a friend.  */
13651           friend_p = cp_parser_friend_p (&decl_specifiers);
13652           /* If there were decl-specifiers, check to see if there was
13653              a class-declaration.  */
13654           type = check_tag_decl (&decl_specifiers);
13655           /* Nested classes have already been added to the class, but
13656              a `friend' needs to be explicitly registered.  */
13657           if (friend_p)
13658             {
13659               /* If the `friend' keyword was present, the friend must
13660                  be introduced with a class-key.  */
13661                if (!declares_class_or_enum)
13662                  error ("a class-key must be used when declaring a friend");
13663                /* In this case:
13664
13665                     template <typename T> struct A {
13666                       friend struct A<T>::B;
13667                     };
13668
13669                   A<T>::B will be represented by a TYPENAME_TYPE, and
13670                   therefore not recognized by check_tag_decl.  */
13671                if (!type
13672                    && decl_specifiers.type
13673                    && TYPE_P (decl_specifiers.type))
13674                  type = decl_specifiers.type;
13675                if (!type || !TYPE_P (type))
13676                  error ("friend declaration does not name a class or "
13677                         "function");
13678                else
13679                  make_friend_class (current_class_type, type,
13680                                     /*complain=*/true);
13681             }
13682           /* If there is no TYPE, an error message will already have
13683              been issued.  */
13684           else if (!type || type == error_mark_node)
13685             ;
13686           /* An anonymous aggregate has to be handled specially; such
13687              a declaration really declares a data member (with a
13688              particular type), as opposed to a nested class.  */
13689           else if (ANON_AGGR_TYPE_P (type))
13690             {
13691               /* Remove constructors and such from TYPE, now that we
13692                  know it is an anonymous aggregate.  */
13693               fixup_anonymous_aggr (type);
13694               /* And make the corresponding data member.  */
13695               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13696               /* Add it to the class.  */
13697               finish_member_declaration (decl);
13698             }
13699           else
13700             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13701         }
13702     }
13703   else
13704     {
13705       /* See if these declarations will be friends.  */
13706       friend_p = cp_parser_friend_p (&decl_specifiers);
13707
13708       /* Keep going until we hit the `;' at the end of the
13709          declaration.  */
13710       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13711         {
13712           tree attributes = NULL_TREE;
13713           tree first_attribute;
13714
13715           /* Peek at the next token.  */
13716           token = cp_lexer_peek_token (parser->lexer);
13717
13718           /* Check for a bitfield declaration.  */
13719           if (token->type == CPP_COLON
13720               || (token->type == CPP_NAME
13721                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13722                   == CPP_COLON))
13723             {
13724               tree identifier;
13725               tree width;
13726
13727               /* Get the name of the bitfield.  Note that we cannot just
13728                  check TOKEN here because it may have been invalidated by
13729                  the call to cp_lexer_peek_nth_token above.  */
13730               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13731                 identifier = cp_parser_identifier (parser);
13732               else
13733                 identifier = NULL_TREE;
13734
13735               /* Consume the `:' token.  */
13736               cp_lexer_consume_token (parser->lexer);
13737               /* Get the width of the bitfield.  */
13738               width
13739                 = cp_parser_constant_expression (parser,
13740                                                  /*allow_non_constant=*/false,
13741                                                  NULL);
13742
13743               /* Look for attributes that apply to the bitfield.  */
13744               attributes = cp_parser_attributes_opt (parser);
13745               /* Remember which attributes are prefix attributes and
13746                  which are not.  */
13747               first_attribute = attributes;
13748               /* Combine the attributes.  */
13749               attributes = chainon (prefix_attributes, attributes);
13750
13751               /* Create the bitfield declaration.  */
13752               decl = grokbitfield (identifier
13753                                    ? make_id_declarator (NULL_TREE,
13754                                                          identifier,
13755                                                          sfk_none)
13756                                    : NULL,
13757                                    &decl_specifiers,
13758                                    width);
13759               /* Apply the attributes.  */
13760               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13761             }
13762           else
13763             {
13764               cp_declarator *declarator;
13765               tree initializer;
13766               tree asm_specification;
13767               int ctor_dtor_or_conv_p;
13768
13769               /* Parse the declarator.  */
13770               declarator
13771                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13772                                         &ctor_dtor_or_conv_p,
13773                                         /*parenthesized_p=*/NULL,
13774                                         /*member_p=*/true);
13775
13776               /* If something went wrong parsing the declarator, make sure
13777                  that we at least consume some tokens.  */
13778               if (declarator == cp_error_declarator)
13779                 {
13780                   /* Skip to the end of the statement.  */
13781                   cp_parser_skip_to_end_of_statement (parser);
13782                   /* If the next token is not a semicolon, that is
13783                      probably because we just skipped over the body of
13784                      a function.  So, we consume a semicolon if
13785                      present, but do not issue an error message if it
13786                      is not present.  */
13787                   if (cp_lexer_next_token_is (parser->lexer,
13788                                               CPP_SEMICOLON))
13789                     cp_lexer_consume_token (parser->lexer);
13790                   return;
13791                 }
13792
13793               if (declares_class_or_enum & 2)
13794                 cp_parser_check_for_definition_in_return_type
13795                   (declarator, decl_specifiers.type);
13796
13797               /* Look for an asm-specification.  */
13798               asm_specification = cp_parser_asm_specification_opt (parser);
13799               /* Look for attributes that apply to the declaration.  */
13800               attributes = cp_parser_attributes_opt (parser);
13801               /* Remember which attributes are prefix attributes and
13802                  which are not.  */
13803               first_attribute = attributes;
13804               /* Combine the attributes.  */
13805               attributes = chainon (prefix_attributes, attributes);
13806
13807               /* If it's an `=', then we have a constant-initializer or a
13808                  pure-specifier.  It is not correct to parse the
13809                  initializer before registering the member declaration
13810                  since the member declaration should be in scope while
13811                  its initializer is processed.  However, the rest of the
13812                  front end does not yet provide an interface that allows
13813                  us to handle this correctly.  */
13814               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13815                 {
13816                   /* In [class.mem]:
13817
13818                      A pure-specifier shall be used only in the declaration of
13819                      a virtual function.
13820
13821                      A member-declarator can contain a constant-initializer
13822                      only if it declares a static member of integral or
13823                      enumeration type.
13824
13825                      Therefore, if the DECLARATOR is for a function, we look
13826                      for a pure-specifier; otherwise, we look for a
13827                      constant-initializer.  When we call `grokfield', it will
13828                      perform more stringent semantics checks.  */
13829                   if (function_declarator_p (declarator))
13830                     initializer = cp_parser_pure_specifier (parser);
13831                   else
13832                     /* Parse the initializer.  */
13833                     initializer = cp_parser_constant_initializer (parser);
13834                 }
13835               /* Otherwise, there is no initializer.  */
13836               else
13837                 initializer = NULL_TREE;
13838
13839               /* See if we are probably looking at a function
13840                  definition.  We are certainly not looking at a
13841                  member-declarator.  Calling `grokfield' has
13842                  side-effects, so we must not do it unless we are sure
13843                  that we are looking at a member-declarator.  */
13844               if (cp_parser_token_starts_function_definition_p
13845                   (cp_lexer_peek_token (parser->lexer)))
13846                 {
13847                   /* The grammar does not allow a pure-specifier to be
13848                      used when a member function is defined.  (It is
13849                      possible that this fact is an oversight in the
13850                      standard, since a pure function may be defined
13851                      outside of the class-specifier.  */
13852                   if (initializer)
13853                     error ("pure-specifier on function-definition");
13854                   decl = cp_parser_save_member_function_body (parser,
13855                                                               &decl_specifiers,
13856                                                               declarator,
13857                                                               attributes);
13858                   /* If the member was not a friend, declare it here.  */
13859                   if (!friend_p)
13860                     finish_member_declaration (decl);
13861                   /* Peek at the next token.  */
13862                   token = cp_lexer_peek_token (parser->lexer);
13863                   /* If the next token is a semicolon, consume it.  */
13864                   if (token->type == CPP_SEMICOLON)
13865                     cp_lexer_consume_token (parser->lexer);
13866                   return;
13867                 }
13868               else
13869                 /* Create the declaration.  */
13870                 decl = grokfield (declarator, &decl_specifiers,
13871                                   initializer, /*init_const_expr_p=*/true,
13872                                   asm_specification,
13873                                   attributes);
13874             }
13875
13876           /* Reset PREFIX_ATTRIBUTES.  */
13877           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13878             attributes = TREE_CHAIN (attributes);
13879           if (attributes)
13880             TREE_CHAIN (attributes) = NULL_TREE;
13881
13882           /* If there is any qualification still in effect, clear it
13883              now; we will be starting fresh with the next declarator.  */
13884           parser->scope = NULL_TREE;
13885           parser->qualifying_scope = NULL_TREE;
13886           parser->object_scope = NULL_TREE;
13887           /* If it's a `,', then there are more declarators.  */
13888           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13889             cp_lexer_consume_token (parser->lexer);
13890           /* If the next token isn't a `;', then we have a parse error.  */
13891           else if (cp_lexer_next_token_is_not (parser->lexer,
13892                                                CPP_SEMICOLON))
13893             {
13894               cp_parser_error (parser, "expected %<;%>");
13895               /* Skip tokens until we find a `;'.  */
13896               cp_parser_skip_to_end_of_statement (parser);
13897
13898               break;
13899             }
13900
13901           if (decl)
13902             {
13903               /* Add DECL to the list of members.  */
13904               if (!friend_p)
13905                 finish_member_declaration (decl);
13906
13907               if (TREE_CODE (decl) == FUNCTION_DECL)
13908                 cp_parser_save_default_args (parser, decl);
13909             }
13910         }
13911     }
13912
13913   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13914 }
13915
13916 /* Parse a pure-specifier.
13917
13918    pure-specifier:
13919      = 0
13920
13921    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13922    Otherwise, ERROR_MARK_NODE is returned.  */
13923
13924 static tree
13925 cp_parser_pure_specifier (cp_parser* parser)
13926 {
13927   cp_token *token;
13928
13929   /* Look for the `=' token.  */
13930   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13931     return error_mark_node;
13932   /* Look for the `0' token.  */
13933   token = cp_lexer_consume_token (parser->lexer);
13934   if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13935     {
13936       cp_parser_error (parser,
13937                        "invalid pure specifier (only `= 0' is allowed)");
13938       cp_parser_skip_to_end_of_statement (parser);
13939       return error_mark_node;
13940     }
13941
13942   /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13943      We need to get information from the lexer about how the number
13944      was spelled in order to fix this problem.  */
13945   return integer_zero_node;
13946 }
13947
13948 /* Parse a constant-initializer.
13949
13950    constant-initializer:
13951      = constant-expression
13952
13953    Returns a representation of the constant-expression.  */
13954
13955 static tree
13956 cp_parser_constant_initializer (cp_parser* parser)
13957 {
13958   /* Look for the `=' token.  */
13959   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13960     return error_mark_node;
13961
13962   /* It is invalid to write:
13963
13964        struct S { static const int i = { 7 }; };
13965
13966      */
13967   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13968     {
13969       cp_parser_error (parser,
13970                        "a brace-enclosed initializer is not allowed here");
13971       /* Consume the opening brace.  */
13972       cp_lexer_consume_token (parser->lexer);
13973       /* Skip the initializer.  */
13974       cp_parser_skip_to_closing_brace (parser);
13975       /* Look for the trailing `}'.  */
13976       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13977
13978       return error_mark_node;
13979     }
13980
13981   return cp_parser_constant_expression (parser,
13982                                         /*allow_non_constant=*/false,
13983                                         NULL);
13984 }
13985
13986 /* Derived classes [gram.class.derived] */
13987
13988 /* Parse a base-clause.
13989
13990    base-clause:
13991      : base-specifier-list
13992
13993    base-specifier-list:
13994      base-specifier
13995      base-specifier-list , base-specifier
13996
13997    Returns a TREE_LIST representing the base-classes, in the order in
13998    which they were declared.  The representation of each node is as
13999    described by cp_parser_base_specifier.
14000
14001    In the case that no bases are specified, this function will return
14002    NULL_TREE, not ERROR_MARK_NODE.  */
14003
14004 static tree
14005 cp_parser_base_clause (cp_parser* parser)
14006 {
14007   tree bases = NULL_TREE;
14008
14009   /* Look for the `:' that begins the list.  */
14010   cp_parser_require (parser, CPP_COLON, "`:'");
14011
14012   /* Scan the base-specifier-list.  */
14013   while (true)
14014     {
14015       cp_token *token;
14016       tree base;
14017
14018       /* Look for the base-specifier.  */
14019       base = cp_parser_base_specifier (parser);
14020       /* Add BASE to the front of the list.  */
14021       if (base != error_mark_node)
14022         {
14023           TREE_CHAIN (base) = bases;
14024           bases = base;
14025         }
14026       /* Peek at the next token.  */
14027       token = cp_lexer_peek_token (parser->lexer);
14028       /* If it's not a comma, then the list is complete.  */
14029       if (token->type != CPP_COMMA)
14030         break;
14031       /* Consume the `,'.  */
14032       cp_lexer_consume_token (parser->lexer);
14033     }
14034
14035   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14036      base class had a qualified name.  However, the next name that
14037      appears is certainly not qualified.  */
14038   parser->scope = NULL_TREE;
14039   parser->qualifying_scope = NULL_TREE;
14040   parser->object_scope = NULL_TREE;
14041
14042   return nreverse (bases);
14043 }
14044
14045 /* Parse a base-specifier.
14046
14047    base-specifier:
14048      :: [opt] nested-name-specifier [opt] class-name
14049      virtual access-specifier [opt] :: [opt] nested-name-specifier
14050        [opt] class-name
14051      access-specifier virtual [opt] :: [opt] nested-name-specifier
14052        [opt] class-name
14053
14054    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14055    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14056    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14057    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14058
14059 static tree
14060 cp_parser_base_specifier (cp_parser* parser)
14061 {
14062   cp_token *token;
14063   bool done = false;
14064   bool virtual_p = false;
14065   bool duplicate_virtual_error_issued_p = false;
14066   bool duplicate_access_error_issued_p = false;
14067   bool class_scope_p, template_p;
14068   tree access = access_default_node;
14069   tree type;
14070
14071   /* Process the optional `virtual' and `access-specifier'.  */
14072   while (!done)
14073     {
14074       /* Peek at the next token.  */
14075       token = cp_lexer_peek_token (parser->lexer);
14076       /* Process `virtual'.  */
14077       switch (token->keyword)
14078         {
14079         case RID_VIRTUAL:
14080           /* If `virtual' appears more than once, issue an error.  */
14081           if (virtual_p && !duplicate_virtual_error_issued_p)
14082             {
14083               cp_parser_error (parser,
14084                                "%<virtual%> specified more than once in base-specified");
14085               duplicate_virtual_error_issued_p = true;
14086             }
14087
14088           virtual_p = true;
14089
14090           /* Consume the `virtual' token.  */
14091           cp_lexer_consume_token (parser->lexer);
14092
14093           break;
14094
14095         case RID_PUBLIC:
14096         case RID_PROTECTED:
14097         case RID_PRIVATE:
14098           /* If more than one access specifier appears, issue an
14099              error.  */
14100           if (access != access_default_node
14101               && !duplicate_access_error_issued_p)
14102             {
14103               cp_parser_error (parser,
14104                                "more than one access specifier in base-specified");
14105               duplicate_access_error_issued_p = true;
14106             }
14107
14108           access = ridpointers[(int) token->keyword];
14109
14110           /* Consume the access-specifier.  */
14111           cp_lexer_consume_token (parser->lexer);
14112
14113           break;
14114
14115         default:
14116           done = true;
14117           break;
14118         }
14119     }
14120   /* It is not uncommon to see programs mechanically, erroneously, use
14121      the 'typename' keyword to denote (dependent) qualified types
14122      as base classes.  */
14123   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14124     {
14125       if (!processing_template_decl)
14126         error ("keyword %<typename%> not allowed outside of templates");
14127       else
14128         error ("keyword %<typename%> not allowed in this context "
14129                "(the base class is implicitly a type)");
14130       cp_lexer_consume_token (parser->lexer);
14131     }
14132
14133   /* Look for the optional `::' operator.  */
14134   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14135   /* Look for the nested-name-specifier.  The simplest way to
14136      implement:
14137
14138        [temp.res]
14139
14140        The keyword `typename' is not permitted in a base-specifier or
14141        mem-initializer; in these contexts a qualified name that
14142        depends on a template-parameter is implicitly assumed to be a
14143        type name.
14144
14145      is to pretend that we have seen the `typename' keyword at this
14146      point.  */
14147   cp_parser_nested_name_specifier_opt (parser,
14148                                        /*typename_keyword_p=*/true,
14149                                        /*check_dependency_p=*/true,
14150                                        typename_type,
14151                                        /*is_declaration=*/true);
14152   /* If the base class is given by a qualified name, assume that names
14153      we see are type names or templates, as appropriate.  */
14154   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14155   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14156
14157   /* Finally, look for the class-name.  */
14158   type = cp_parser_class_name (parser,
14159                                class_scope_p,
14160                                template_p,
14161                                typename_type,
14162                                /*check_dependency_p=*/true,
14163                                /*class_head_p=*/false,
14164                                /*is_declaration=*/true);
14165
14166   if (type == error_mark_node)
14167     return error_mark_node;
14168
14169   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14170 }
14171
14172 /* Exception handling [gram.exception] */
14173
14174 /* Parse an (optional) exception-specification.
14175
14176    exception-specification:
14177      throw ( type-id-list [opt] )
14178
14179    Returns a TREE_LIST representing the exception-specification.  The
14180    TREE_VALUE of each node is a type.  */
14181
14182 static tree
14183 cp_parser_exception_specification_opt (cp_parser* parser)
14184 {
14185   cp_token *token;
14186   tree type_id_list;
14187
14188   /* Peek at the next token.  */
14189   token = cp_lexer_peek_token (parser->lexer);
14190   /* If it's not `throw', then there's no exception-specification.  */
14191   if (!cp_parser_is_keyword (token, RID_THROW))
14192     return NULL_TREE;
14193
14194   /* Consume the `throw'.  */
14195   cp_lexer_consume_token (parser->lexer);
14196
14197   /* Look for the `('.  */
14198   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14199
14200   /* Peek at the next token.  */
14201   token = cp_lexer_peek_token (parser->lexer);
14202   /* If it's not a `)', then there is a type-id-list.  */
14203   if (token->type != CPP_CLOSE_PAREN)
14204     {
14205       const char *saved_message;
14206
14207       /* Types may not be defined in an exception-specification.  */
14208       saved_message = parser->type_definition_forbidden_message;
14209       parser->type_definition_forbidden_message
14210         = "types may not be defined in an exception-specification";
14211       /* Parse the type-id-list.  */
14212       type_id_list = cp_parser_type_id_list (parser);
14213       /* Restore the saved message.  */
14214       parser->type_definition_forbidden_message = saved_message;
14215     }
14216   else
14217     type_id_list = empty_except_spec;
14218
14219   /* Look for the `)'.  */
14220   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14221
14222   return type_id_list;
14223 }
14224
14225 /* Parse an (optional) type-id-list.
14226
14227    type-id-list:
14228      type-id
14229      type-id-list , type-id
14230
14231    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14232    in the order that the types were presented.  */
14233
14234 static tree
14235 cp_parser_type_id_list (cp_parser* parser)
14236 {
14237   tree types = NULL_TREE;
14238
14239   while (true)
14240     {
14241       cp_token *token;
14242       tree type;
14243
14244       /* Get the next type-id.  */
14245       type = cp_parser_type_id (parser);
14246       /* Add it to the list.  */
14247       types = add_exception_specifier (types, type, /*complain=*/1);
14248       /* Peek at the next token.  */
14249       token = cp_lexer_peek_token (parser->lexer);
14250       /* If it is not a `,', we are done.  */
14251       if (token->type != CPP_COMMA)
14252         break;
14253       /* Consume the `,'.  */
14254       cp_lexer_consume_token (parser->lexer);
14255     }
14256
14257   return nreverse (types);
14258 }
14259
14260 /* Parse a try-block.
14261
14262    try-block:
14263      try compound-statement handler-seq  */
14264
14265 static tree
14266 cp_parser_try_block (cp_parser* parser)
14267 {
14268   tree try_block;
14269
14270   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14271   try_block = begin_try_block ();
14272   cp_parser_compound_statement (parser, NULL, true);
14273   finish_try_block (try_block);
14274   cp_parser_handler_seq (parser);
14275   finish_handler_sequence (try_block);
14276
14277   return try_block;
14278 }
14279
14280 /* Parse a function-try-block.
14281
14282    function-try-block:
14283      try ctor-initializer [opt] function-body handler-seq  */
14284
14285 static bool
14286 cp_parser_function_try_block (cp_parser* parser)
14287 {
14288   tree compound_stmt;
14289   tree try_block;
14290   bool ctor_initializer_p;
14291
14292   /* Look for the `try' keyword.  */
14293   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14294     return false;
14295   /* Let the rest of the front-end know where we are.  */
14296   try_block = begin_function_try_block (&compound_stmt);
14297   /* Parse the function-body.  */
14298   ctor_initializer_p
14299     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14300   /* We're done with the `try' part.  */
14301   finish_function_try_block (try_block);
14302   /* Parse the handlers.  */
14303   cp_parser_handler_seq (parser);
14304   /* We're done with the handlers.  */
14305   finish_function_handler_sequence (try_block, compound_stmt);
14306
14307   return ctor_initializer_p;
14308 }
14309
14310 /* Parse a handler-seq.
14311
14312    handler-seq:
14313      handler handler-seq [opt]  */
14314
14315 static void
14316 cp_parser_handler_seq (cp_parser* parser)
14317 {
14318   while (true)
14319     {
14320       cp_token *token;
14321
14322       /* Parse the handler.  */
14323       cp_parser_handler (parser);
14324       /* Peek at the next token.  */
14325       token = cp_lexer_peek_token (parser->lexer);
14326       /* If it's not `catch' then there are no more handlers.  */
14327       if (!cp_parser_is_keyword (token, RID_CATCH))
14328         break;
14329     }
14330 }
14331
14332 /* Parse a handler.
14333
14334    handler:
14335      catch ( exception-declaration ) compound-statement  */
14336
14337 static void
14338 cp_parser_handler (cp_parser* parser)
14339 {
14340   tree handler;
14341   tree declaration;
14342
14343   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14344   handler = begin_handler ();
14345   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14346   declaration = cp_parser_exception_declaration (parser);
14347   finish_handler_parms (declaration, handler);
14348   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14349   cp_parser_compound_statement (parser, NULL, false);
14350   finish_handler (handler);
14351 }
14352
14353 /* Parse an exception-declaration.
14354
14355    exception-declaration:
14356      type-specifier-seq declarator
14357      type-specifier-seq abstract-declarator
14358      type-specifier-seq
14359      ...
14360
14361    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14362    ellipsis variant is used.  */
14363
14364 static tree
14365 cp_parser_exception_declaration (cp_parser* parser)
14366 {
14367   tree decl;
14368   cp_decl_specifier_seq type_specifiers;
14369   cp_declarator *declarator;
14370   const char *saved_message;
14371
14372   /* If it's an ellipsis, it's easy to handle.  */
14373   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14374     {
14375       /* Consume the `...' token.  */
14376       cp_lexer_consume_token (parser->lexer);
14377       return NULL_TREE;
14378     }
14379
14380   /* Types may not be defined in exception-declarations.  */
14381   saved_message = parser->type_definition_forbidden_message;
14382   parser->type_definition_forbidden_message
14383     = "types may not be defined in exception-declarations";
14384
14385   /* Parse the type-specifier-seq.  */
14386   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14387                                 &type_specifiers);
14388   /* If it's a `)', then there is no declarator.  */
14389   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14390     declarator = NULL;
14391   else
14392     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14393                                        /*ctor_dtor_or_conv_p=*/NULL,
14394                                        /*parenthesized_p=*/NULL,
14395                                        /*member_p=*/false);
14396
14397   /* Restore the saved message.  */
14398   parser->type_definition_forbidden_message = saved_message;
14399
14400   if (type_specifiers.any_specifiers_p)
14401     {
14402       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14403       if (decl == NULL_TREE)
14404         error ("invalid catch parameter");
14405     }
14406   else
14407     decl = NULL_TREE;
14408
14409   return decl;
14410 }
14411
14412 /* Parse a throw-expression.
14413
14414    throw-expression:
14415      throw assignment-expression [opt]
14416
14417    Returns a THROW_EXPR representing the throw-expression.  */
14418
14419 static tree
14420 cp_parser_throw_expression (cp_parser* parser)
14421 {
14422   tree expression;
14423   cp_token* token;
14424
14425   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14426   token = cp_lexer_peek_token (parser->lexer);
14427   /* Figure out whether or not there is an assignment-expression
14428      following the "throw" keyword.  */
14429   if (token->type == CPP_COMMA
14430       || token->type == CPP_SEMICOLON
14431       || token->type == CPP_CLOSE_PAREN
14432       || token->type == CPP_CLOSE_SQUARE
14433       || token->type == CPP_CLOSE_BRACE
14434       || token->type == CPP_COLON)
14435     expression = NULL_TREE;
14436   else
14437     expression = cp_parser_assignment_expression (parser,
14438                                                   /*cast_p=*/false);
14439
14440   return build_throw (expression);
14441 }
14442
14443 /* GNU Extensions */
14444
14445 /* Parse an (optional) asm-specification.
14446
14447    asm-specification:
14448      asm ( string-literal )
14449
14450    If the asm-specification is present, returns a STRING_CST
14451    corresponding to the string-literal.  Otherwise, returns
14452    NULL_TREE.  */
14453
14454 static tree
14455 cp_parser_asm_specification_opt (cp_parser* parser)
14456 {
14457   cp_token *token;
14458   tree asm_specification;
14459
14460   /* Peek at the next token.  */
14461   token = cp_lexer_peek_token (parser->lexer);
14462   /* If the next token isn't the `asm' keyword, then there's no
14463      asm-specification.  */
14464   if (!cp_parser_is_keyword (token, RID_ASM))
14465     return NULL_TREE;
14466
14467   /* Consume the `asm' token.  */
14468   cp_lexer_consume_token (parser->lexer);
14469   /* Look for the `('.  */
14470   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14471
14472   /* Look for the string-literal.  */
14473   asm_specification = cp_parser_string_literal (parser, false, false);
14474
14475   /* Look for the `)'.  */
14476   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14477
14478   return asm_specification;
14479 }
14480
14481 /* Parse an asm-operand-list.
14482
14483    asm-operand-list:
14484      asm-operand
14485      asm-operand-list , asm-operand
14486
14487    asm-operand:
14488      string-literal ( expression )
14489      [ string-literal ] string-literal ( expression )
14490
14491    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14492    each node is the expression.  The TREE_PURPOSE is itself a
14493    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14494    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14495    is a STRING_CST for the string literal before the parenthesis.  */
14496
14497 static tree
14498 cp_parser_asm_operand_list (cp_parser* parser)
14499 {
14500   tree asm_operands = NULL_TREE;
14501
14502   while (true)
14503     {
14504       tree string_literal;
14505       tree expression;
14506       tree name;
14507
14508       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14509         {
14510           /* Consume the `[' token.  */
14511           cp_lexer_consume_token (parser->lexer);
14512           /* Read the operand name.  */
14513           name = cp_parser_identifier (parser);
14514           if (name != error_mark_node)
14515             name = build_string (IDENTIFIER_LENGTH (name),
14516                                  IDENTIFIER_POINTER (name));
14517           /* Look for the closing `]'.  */
14518           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14519         }
14520       else
14521         name = NULL_TREE;
14522       /* Look for the string-literal.  */
14523       string_literal = cp_parser_string_literal (parser, false, false);
14524
14525       /* Look for the `('.  */
14526       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14527       /* Parse the expression.  */
14528       expression = cp_parser_expression (parser, /*cast_p=*/false);
14529       /* Look for the `)'.  */
14530       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14531
14532       /* Add this operand to the list.  */
14533       asm_operands = tree_cons (build_tree_list (name, string_literal),
14534                                 expression,
14535                                 asm_operands);
14536       /* If the next token is not a `,', there are no more
14537          operands.  */
14538       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14539         break;
14540       /* Consume the `,'.  */
14541       cp_lexer_consume_token (parser->lexer);
14542     }
14543
14544   return nreverse (asm_operands);
14545 }
14546
14547 /* Parse an asm-clobber-list.
14548
14549    asm-clobber-list:
14550      string-literal
14551      asm-clobber-list , string-literal
14552
14553    Returns a TREE_LIST, indicating the clobbers in the order that they
14554    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14555
14556 static tree
14557 cp_parser_asm_clobber_list (cp_parser* parser)
14558 {
14559   tree clobbers = NULL_TREE;
14560
14561   while (true)
14562     {
14563       tree string_literal;
14564
14565       /* Look for the string literal.  */
14566       string_literal = cp_parser_string_literal (parser, false, false);
14567       /* Add it to the list.  */
14568       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14569       /* If the next token is not a `,', then the list is
14570          complete.  */
14571       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14572         break;
14573       /* Consume the `,' token.  */
14574       cp_lexer_consume_token (parser->lexer);
14575     }
14576
14577   return clobbers;
14578 }
14579
14580 /* Parse an (optional) series of attributes.
14581
14582    attributes:
14583      attributes attribute
14584
14585    attribute:
14586      __attribute__ (( attribute-list [opt] ))
14587
14588    The return value is as for cp_parser_attribute_list.  */
14589
14590 static tree
14591 cp_parser_attributes_opt (cp_parser* parser)
14592 {
14593   tree attributes = NULL_TREE;
14594
14595   while (true)
14596     {
14597       cp_token *token;
14598       tree attribute_list;
14599
14600       /* Peek at the next token.  */
14601       token = cp_lexer_peek_token (parser->lexer);
14602       /* If it's not `__attribute__', then we're done.  */
14603       if (token->keyword != RID_ATTRIBUTE)
14604         break;
14605
14606       /* Consume the `__attribute__' keyword.  */
14607       cp_lexer_consume_token (parser->lexer);
14608       /* Look for the two `(' tokens.  */
14609       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14610       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14611
14612       /* Peek at the next token.  */
14613       token = cp_lexer_peek_token (parser->lexer);
14614       if (token->type != CPP_CLOSE_PAREN)
14615         /* Parse the attribute-list.  */
14616         attribute_list = cp_parser_attribute_list (parser);
14617       else
14618         /* If the next token is a `)', then there is no attribute
14619            list.  */
14620         attribute_list = NULL;
14621
14622       /* Look for the two `)' tokens.  */
14623       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14624       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14625
14626       /* Add these new attributes to the list.  */
14627       attributes = chainon (attributes, attribute_list);
14628     }
14629
14630   return attributes;
14631 }
14632
14633 /* Parse an attribute-list.
14634
14635    attribute-list:
14636      attribute
14637      attribute-list , attribute
14638
14639    attribute:
14640      identifier
14641      identifier ( identifier )
14642      identifier ( identifier , expression-list )
14643      identifier ( expression-list )
14644
14645    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14646    to an attribute.  The TREE_PURPOSE of each node is the identifier
14647    indicating which attribute is in use.  The TREE_VALUE represents
14648    the arguments, if any.  */
14649
14650 static tree
14651 cp_parser_attribute_list (cp_parser* parser)
14652 {
14653   tree attribute_list = NULL_TREE;
14654   bool save_translate_strings_p = parser->translate_strings_p;
14655
14656   parser->translate_strings_p = false;
14657   while (true)
14658     {
14659       cp_token *token;
14660       tree identifier;
14661       tree attribute;
14662
14663       /* Look for the identifier.  We also allow keywords here; for
14664          example `__attribute__ ((const))' is legal.  */
14665       token = cp_lexer_peek_token (parser->lexer);
14666       if (token->type == CPP_NAME
14667           || token->type == CPP_KEYWORD)
14668         {
14669           tree arguments = NULL_TREE;
14670
14671           /* Consume the token.  */
14672           token = cp_lexer_consume_token (parser->lexer);
14673
14674           /* Save away the identifier that indicates which attribute
14675              this is.  */
14676           identifier = token->value;
14677           attribute = build_tree_list (identifier, NULL_TREE);
14678
14679           /* Peek at the next token.  */
14680           token = cp_lexer_peek_token (parser->lexer);
14681           /* If it's an `(', then parse the attribute arguments.  */
14682           if (token->type == CPP_OPEN_PAREN)
14683             {
14684               arguments = cp_parser_parenthesized_expression_list
14685                           (parser, true, /*cast_p=*/false,
14686                            /*non_constant_p=*/NULL);
14687               /* Save the arguments away.  */
14688               TREE_VALUE (attribute) = arguments;
14689             }
14690
14691           if (arguments != error_mark_node)
14692             {
14693               /* Add this attribute to the list.  */
14694               TREE_CHAIN (attribute) = attribute_list;
14695               attribute_list = attribute;
14696             }
14697
14698           token = cp_lexer_peek_token (parser->lexer);
14699         }
14700       /* Now, look for more attributes.  If the next token isn't a
14701          `,', we're done.  */
14702       if (token->type != CPP_COMMA)
14703         break;
14704
14705       /* Consume the comma and keep going.  */
14706       cp_lexer_consume_token (parser->lexer);
14707     }
14708   parser->translate_strings_p = save_translate_strings_p;
14709
14710   /* We built up the list in reverse order.  */
14711   return nreverse (attribute_list);
14712 }
14713
14714 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14715    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14716    current value of the PEDANTIC flag, regardless of whether or not
14717    the `__extension__' keyword is present.  The caller is responsible
14718    for restoring the value of the PEDANTIC flag.  */
14719
14720 static bool
14721 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14722 {
14723   /* Save the old value of the PEDANTIC flag.  */
14724   *saved_pedantic = pedantic;
14725
14726   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14727     {
14728       /* Consume the `__extension__' token.  */
14729       cp_lexer_consume_token (parser->lexer);
14730       /* We're not being pedantic while the `__extension__' keyword is
14731          in effect.  */
14732       pedantic = 0;
14733
14734       return true;
14735     }
14736
14737   return false;
14738 }
14739
14740 /* Parse a label declaration.
14741
14742    label-declaration:
14743      __label__ label-declarator-seq ;
14744
14745    label-declarator-seq:
14746      identifier , label-declarator-seq
14747      identifier  */
14748
14749 static void
14750 cp_parser_label_declaration (cp_parser* parser)
14751 {
14752   /* Look for the `__label__' keyword.  */
14753   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14754
14755   while (true)
14756     {
14757       tree identifier;
14758
14759       /* Look for an identifier.  */
14760       identifier = cp_parser_identifier (parser);
14761       /* If we failed, stop.  */
14762       if (identifier == error_mark_node)
14763         break;
14764       /* Declare it as a label.  */
14765       finish_label_decl (identifier);
14766       /* If the next token is a `;', stop.  */
14767       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14768         break;
14769       /* Look for the `,' separating the label declarations.  */
14770       cp_parser_require (parser, CPP_COMMA, "`,'");
14771     }
14772
14773   /* Look for the final `;'.  */
14774   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14775 }
14776
14777 /* Support Functions */
14778
14779 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14780    NAME should have one of the representations used for an
14781    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14782    is returned.  If PARSER->SCOPE is a dependent type, then a
14783    SCOPE_REF is returned.
14784
14785    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14786    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14787    was formed.  Abstractly, such entities should not be passed to this
14788    function, because they do not need to be looked up, but it is
14789    simpler to check for this special case here, rather than at the
14790    call-sites.
14791
14792    In cases not explicitly covered above, this function returns a
14793    DECL, OVERLOAD, or baselink representing the result of the lookup.
14794    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14795    is returned.
14796
14797    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14798    (e.g., "struct") that was used.  In that case bindings that do not
14799    refer to types are ignored.
14800
14801    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14802    ignored.
14803
14804    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14805    are ignored.
14806
14807    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14808    types.
14809
14810    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14811    TREE_LIST of candiates if name-lookup results in an ambiguity, and
14812    NULL_TREE otherwise.  */ 
14813
14814 static tree
14815 cp_parser_lookup_name (cp_parser *parser, tree name,
14816                        enum tag_types tag_type,
14817                        bool is_template, 
14818                        bool is_namespace,
14819                        bool check_dependency,
14820                        tree *ambiguous_decls)
14821 {
14822   int flags = 0;
14823   tree decl;
14824   tree object_type = parser->context->object_type;
14825
14826   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14827     flags |= LOOKUP_COMPLAIN;
14828
14829   /* Assume that the lookup will be unambiguous.  */
14830   if (ambiguous_decls)
14831     *ambiguous_decls = NULL_TREE;
14832
14833   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14834      no longer valid.  Note that if we are parsing tentatively, and
14835      the parse fails, OBJECT_TYPE will be automatically restored.  */
14836   parser->context->object_type = NULL_TREE;
14837
14838   if (name == error_mark_node)
14839     return error_mark_node;
14840
14841   /* A template-id has already been resolved; there is no lookup to
14842      do.  */
14843   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14844     return name;
14845   if (BASELINK_P (name))
14846     {
14847       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14848                   == TEMPLATE_ID_EXPR);
14849       return name;
14850     }
14851
14852   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14853      it should already have been checked to make sure that the name
14854      used matches the type being destroyed.  */
14855   if (TREE_CODE (name) == BIT_NOT_EXPR)
14856     {
14857       tree type;
14858
14859       /* Figure out to which type this destructor applies.  */
14860       if (parser->scope)
14861         type = parser->scope;
14862       else if (object_type)
14863         type = object_type;
14864       else
14865         type = current_class_type;
14866       /* If that's not a class type, there is no destructor.  */
14867       if (!type || !CLASS_TYPE_P (type))
14868         return error_mark_node;
14869       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14870         lazily_declare_fn (sfk_destructor, type);
14871       if (!CLASSTYPE_DESTRUCTORS (type))
14872           return error_mark_node;
14873       /* If it was a class type, return the destructor.  */
14874       return CLASSTYPE_DESTRUCTORS (type);
14875     }
14876
14877   /* By this point, the NAME should be an ordinary identifier.  If
14878      the id-expression was a qualified name, the qualifying scope is
14879      stored in PARSER->SCOPE at this point.  */
14880   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14881
14882   /* Perform the lookup.  */
14883   if (parser->scope)
14884     {
14885       bool dependent_p;
14886
14887       if (parser->scope == error_mark_node)
14888         return error_mark_node;
14889
14890       /* If the SCOPE is dependent, the lookup must be deferred until
14891          the template is instantiated -- unless we are explicitly
14892          looking up names in uninstantiated templates.  Even then, we
14893          cannot look up the name if the scope is not a class type; it
14894          might, for example, be a template type parameter.  */
14895       dependent_p = (TYPE_P (parser->scope)
14896                      && !(parser->in_declarator_p
14897                           && currently_open_class (parser->scope))
14898                      && dependent_type_p (parser->scope));
14899       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14900            && dependent_p)
14901         {
14902           if (tag_type)
14903             {
14904               tree type;
14905
14906               /* The resolution to Core Issue 180 says that `struct
14907                  A::B' should be considered a type-name, even if `A'
14908                  is dependent.  */
14909               type = make_typename_type (parser->scope, name, tag_type,
14910                                          /*complain=*/1);
14911               decl = TYPE_NAME (type);
14912             }
14913           else if (is_template
14914                    && (cp_parser_next_token_ends_template_argument_p (parser)
14915                        || cp_lexer_next_token_is (parser->lexer,
14916                                                   CPP_CLOSE_PAREN)))
14917             decl = make_unbound_class_template (parser->scope,
14918                                                 name, NULL_TREE,
14919                                                 /*complain=*/1);
14920           else
14921             decl = build_qualified_name (/*type=*/NULL_TREE,
14922                                          parser->scope, name,
14923                                          is_template);
14924         }
14925       else
14926         {
14927           tree pushed_scope = NULL_TREE;
14928
14929           /* If PARSER->SCOPE is a dependent type, then it must be a
14930              class type, and we must not be checking dependencies;
14931              otherwise, we would have processed this lookup above.  So
14932              that PARSER->SCOPE is not considered a dependent base by
14933              lookup_member, we must enter the scope here.  */
14934           if (dependent_p)
14935             pushed_scope = push_scope (parser->scope);
14936           /* If the PARSER->SCOPE is a template specialization, it
14937              may be instantiated during name lookup.  In that case,
14938              errors may be issued.  Even if we rollback the current
14939              tentative parse, those errors are valid.  */
14940           decl = lookup_qualified_name (parser->scope, name,
14941                                         tag_type != none_type,
14942                                         /*complain=*/true);
14943           if (pushed_scope)
14944             pop_scope (pushed_scope);
14945         }
14946       parser->qualifying_scope = parser->scope;
14947       parser->object_scope = NULL_TREE;
14948     }
14949   else if (object_type)
14950     {
14951       tree object_decl = NULL_TREE;
14952       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14953          OBJECT_TYPE is not a class.  */
14954       if (CLASS_TYPE_P (object_type))
14955         /* If the OBJECT_TYPE is a template specialization, it may
14956            be instantiated during name lookup.  In that case, errors
14957            may be issued.  Even if we rollback the current tentative
14958            parse, those errors are valid.  */
14959         object_decl = lookup_member (object_type,
14960                                      name,
14961                                      /*protect=*/0,
14962                                      tag_type != none_type);
14963       /* Look it up in the enclosing context, too.  */
14964       decl = lookup_name_real (name, tag_type != none_type,
14965                                /*nonclass=*/0,
14966                                /*block_p=*/true, is_namespace, flags);
14967       parser->object_scope = object_type;
14968       parser->qualifying_scope = NULL_TREE;
14969       if (object_decl)
14970         decl = object_decl;
14971     }
14972   else
14973     {
14974       decl = lookup_name_real (name, tag_type != none_type,
14975                                /*nonclass=*/0,
14976                                /*block_p=*/true, is_namespace, flags);
14977       parser->qualifying_scope = NULL_TREE;
14978       parser->object_scope = NULL_TREE;
14979     }
14980
14981   /* If the lookup failed, let our caller know.  */
14982   if (!decl || decl == error_mark_node)
14983     return error_mark_node;
14984
14985   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14986   if (TREE_CODE (decl) == TREE_LIST)
14987     {
14988       if (ambiguous_decls)
14989         *ambiguous_decls = decl;
14990       /* The error message we have to print is too complicated for
14991          cp_parser_error, so we incorporate its actions directly.  */
14992       if (!cp_parser_simulate_error (parser))
14993         {
14994           error ("reference to %qD is ambiguous", name);
14995           print_candidates (decl);
14996         }
14997       return error_mark_node;
14998     }
14999
15000   gcc_assert (DECL_P (decl)
15001               || TREE_CODE (decl) == OVERLOAD
15002               || TREE_CODE (decl) == SCOPE_REF
15003               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15004               || BASELINK_P (decl));
15005
15006   /* If we have resolved the name of a member declaration, check to
15007      see if the declaration is accessible.  When the name resolves to
15008      set of overloaded functions, accessibility is checked when
15009      overload resolution is done.
15010
15011      During an explicit instantiation, access is not checked at all,
15012      as per [temp.explicit].  */
15013   if (DECL_P (decl))
15014     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15015
15016   return decl;
15017 }
15018
15019 /* Like cp_parser_lookup_name, but for use in the typical case where
15020    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15021    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15022
15023 static tree
15024 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15025 {
15026   return cp_parser_lookup_name (parser, name,
15027                                 none_type,
15028                                 /*is_template=*/false,
15029                                 /*is_namespace=*/false,
15030                                 /*check_dependency=*/true,
15031                                 /*ambiguous_decls=*/NULL);
15032 }
15033
15034 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15035    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15036    true, the DECL indicates the class being defined in a class-head,
15037    or declared in an elaborated-type-specifier.
15038
15039    Otherwise, return DECL.  */
15040
15041 static tree
15042 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15043 {
15044   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15045      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15046
15047        struct A {
15048          template <typename T> struct B;
15049        };
15050
15051        template <typename T> struct A::B {};
15052
15053      Similarly, in an elaborated-type-specifier:
15054
15055        namespace N { struct X{}; }
15056
15057        struct A {
15058          template <typename T> friend struct N::X;
15059        };
15060
15061      However, if the DECL refers to a class type, and we are in
15062      the scope of the class, then the name lookup automatically
15063      finds the TYPE_DECL created by build_self_reference rather
15064      than a TEMPLATE_DECL.  For example, in:
15065
15066        template <class T> struct S {
15067          S s;
15068        };
15069
15070      there is no need to handle such case.  */
15071
15072   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15073     return DECL_TEMPLATE_RESULT (decl);
15074
15075   return decl;
15076 }
15077
15078 /* If too many, or too few, template-parameter lists apply to the
15079    declarator, issue an error message.  Returns TRUE if all went well,
15080    and FALSE otherwise.  */
15081
15082 static bool
15083 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15084                                                 cp_declarator *declarator)
15085 {
15086   unsigned num_templates;
15087
15088   /* We haven't seen any classes that involve template parameters yet.  */
15089   num_templates = 0;
15090
15091   switch (declarator->kind)
15092     {
15093     case cdk_id:
15094       if (declarator->u.id.qualifying_scope)
15095         {
15096           tree scope;
15097           tree member;
15098
15099           scope = declarator->u.id.qualifying_scope;
15100           member = declarator->u.id.unqualified_name;
15101
15102           while (scope && CLASS_TYPE_P (scope))
15103             {
15104               /* You're supposed to have one `template <...>'
15105                  for every template class, but you don't need one
15106                  for a full specialization.  For example:
15107
15108                  template <class T> struct S{};
15109                  template <> struct S<int> { void f(); };
15110                  void S<int>::f () {}
15111
15112                  is correct; there shouldn't be a `template <>' for
15113                  the definition of `S<int>::f'.  */
15114               if (!CLASSTYPE_TEMPLATE_INFO (scope))
15115                 /* If SCOPE does not have template information of any
15116                    kind, then it is not a template, nor is it nested
15117                    within a template.  */
15118                 break;
15119               if (explicit_class_specialization_p (scope))
15120                 break;
15121               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15122                 ++num_templates;
15123
15124               scope = TYPE_CONTEXT (scope);
15125             }
15126         }
15127       else if (TREE_CODE (declarator->u.id.unqualified_name)
15128                == TEMPLATE_ID_EXPR)
15129         /* If the DECLARATOR has the form `X<y>' then it uses one
15130            additional level of template parameters.  */
15131         ++num_templates;
15132
15133       return cp_parser_check_template_parameters (parser,
15134                                                   num_templates);
15135
15136     case cdk_function:
15137     case cdk_array:
15138     case cdk_pointer:
15139     case cdk_reference:
15140     case cdk_ptrmem:
15141       return (cp_parser_check_declarator_template_parameters
15142               (parser, declarator->declarator));
15143
15144     case cdk_error:
15145       return true;
15146
15147     default:
15148       gcc_unreachable ();
15149     }
15150   return false;
15151 }
15152
15153 /* NUM_TEMPLATES were used in the current declaration.  If that is
15154    invalid, return FALSE and issue an error messages.  Otherwise,
15155    return TRUE.  */
15156
15157 static bool
15158 cp_parser_check_template_parameters (cp_parser* parser,
15159                                      unsigned num_templates)
15160 {
15161   /* If there are more template classes than parameter lists, we have
15162      something like:
15163
15164        template <class T> void S<T>::R<T>::f ();  */
15165   if (parser->num_template_parameter_lists < num_templates)
15166     {
15167       error ("too few template-parameter-lists");
15168       return false;
15169     }
15170   /* If there are the same number of template classes and parameter
15171      lists, that's OK.  */
15172   if (parser->num_template_parameter_lists == num_templates)
15173     return true;
15174   /* If there are more, but only one more, then we are referring to a
15175      member template.  That's OK too.  */
15176   if (parser->num_template_parameter_lists == num_templates + 1)
15177       return true;
15178   /* Otherwise, there are too many template parameter lists.  We have
15179      something like:
15180
15181      template <class T> template <class U> void S::f();  */
15182   error ("too many template-parameter-lists");
15183   return false;
15184 }
15185
15186 /* Parse an optional `::' token indicating that the following name is
15187    from the global namespace.  If so, PARSER->SCOPE is set to the
15188    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15189    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15190    Returns the new value of PARSER->SCOPE, if the `::' token is
15191    present, and NULL_TREE otherwise.  */
15192
15193 static tree
15194 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15195 {
15196   cp_token *token;
15197
15198   /* Peek at the next token.  */
15199   token = cp_lexer_peek_token (parser->lexer);
15200   /* If we're looking at a `::' token then we're starting from the
15201      global namespace, not our current location.  */
15202   if (token->type == CPP_SCOPE)
15203     {
15204       /* Consume the `::' token.  */
15205       cp_lexer_consume_token (parser->lexer);
15206       /* Set the SCOPE so that we know where to start the lookup.  */
15207       parser->scope = global_namespace;
15208       parser->qualifying_scope = global_namespace;
15209       parser->object_scope = NULL_TREE;
15210
15211       return parser->scope;
15212     }
15213   else if (!current_scope_valid_p)
15214     {
15215       parser->scope = NULL_TREE;
15216       parser->qualifying_scope = NULL_TREE;
15217       parser->object_scope = NULL_TREE;
15218     }
15219
15220   return NULL_TREE;
15221 }
15222
15223 /* Returns TRUE if the upcoming token sequence is the start of a
15224    constructor declarator.  If FRIEND_P is true, the declarator is
15225    preceded by the `friend' specifier.  */
15226
15227 static bool
15228 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15229 {
15230   bool constructor_p;
15231   tree type_decl = NULL_TREE;
15232   bool nested_name_p;
15233   cp_token *next_token;
15234
15235   /* The common case is that this is not a constructor declarator, so
15236      try to avoid doing lots of work if at all possible.  It's not
15237      valid declare a constructor at function scope.  */
15238   if (parser->in_function_body)
15239     return false;
15240   /* And only certain tokens can begin a constructor declarator.  */
15241   next_token = cp_lexer_peek_token (parser->lexer);
15242   if (next_token->type != CPP_NAME
15243       && next_token->type != CPP_SCOPE
15244       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15245       && next_token->type != CPP_TEMPLATE_ID)
15246     return false;
15247
15248   /* Parse tentatively; we are going to roll back all of the tokens
15249      consumed here.  */
15250   cp_parser_parse_tentatively (parser);
15251   /* Assume that we are looking at a constructor declarator.  */
15252   constructor_p = true;
15253
15254   /* Look for the optional `::' operator.  */
15255   cp_parser_global_scope_opt (parser,
15256                               /*current_scope_valid_p=*/false);
15257   /* Look for the nested-name-specifier.  */
15258   nested_name_p
15259     = (cp_parser_nested_name_specifier_opt (parser,
15260                                             /*typename_keyword_p=*/false,
15261                                             /*check_dependency_p=*/false,
15262                                             /*type_p=*/false,
15263                                             /*is_declaration=*/false)
15264        != NULL_TREE);
15265   /* Outside of a class-specifier, there must be a
15266      nested-name-specifier.  */
15267   if (!nested_name_p &&
15268       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15269        || friend_p))
15270     constructor_p = false;
15271   /* If we still think that this might be a constructor-declarator,
15272      look for a class-name.  */
15273   if (constructor_p)
15274     {
15275       /* If we have:
15276
15277            template <typename T> struct S { S(); };
15278            template <typename T> S<T>::S ();
15279
15280          we must recognize that the nested `S' names a class.
15281          Similarly, for:
15282
15283            template <typename T> S<T>::S<T> ();
15284
15285          we must recognize that the nested `S' names a template.  */
15286       type_decl = cp_parser_class_name (parser,
15287                                         /*typename_keyword_p=*/false,
15288                                         /*template_keyword_p=*/false,
15289                                         none_type,
15290                                         /*check_dependency_p=*/false,
15291                                         /*class_head_p=*/false,
15292                                         /*is_declaration=*/false);
15293       /* If there was no class-name, then this is not a constructor.  */
15294       constructor_p = !cp_parser_error_occurred (parser);
15295     }
15296
15297   /* If we're still considering a constructor, we have to see a `(',
15298      to begin the parameter-declaration-clause, followed by either a
15299      `)', an `...', or a decl-specifier.  We need to check for a
15300      type-specifier to avoid being fooled into thinking that:
15301
15302        S::S (f) (int);
15303
15304      is a constructor.  (It is actually a function named `f' that
15305      takes one parameter (of type `int') and returns a value of type
15306      `S::S'.  */
15307   if (constructor_p
15308       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15309     {
15310       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15311           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15312           /* A parameter declaration begins with a decl-specifier,
15313              which is either the "attribute" keyword, a storage class
15314              specifier, or (usually) a type-specifier.  */
15315           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15316         {
15317           tree type;
15318           tree pushed_scope = NULL_TREE;
15319           unsigned saved_num_template_parameter_lists;
15320
15321           /* Names appearing in the type-specifier should be looked up
15322              in the scope of the class.  */
15323           if (current_class_type)
15324             type = NULL_TREE;
15325           else
15326             {
15327               type = TREE_TYPE (type_decl);
15328               if (TREE_CODE (type) == TYPENAME_TYPE)
15329                 {
15330                   type = resolve_typename_type (type,
15331                                                 /*only_current_p=*/false);
15332                   if (type == error_mark_node)
15333                     {
15334                       cp_parser_abort_tentative_parse (parser);
15335                       return false;
15336                     }
15337                 }
15338               pushed_scope = push_scope (type);
15339             }
15340
15341           /* Inside the constructor parameter list, surrounding
15342              template-parameter-lists do not apply.  */
15343           saved_num_template_parameter_lists
15344             = parser->num_template_parameter_lists;
15345           parser->num_template_parameter_lists = 0;
15346
15347           /* Look for the type-specifier.  */
15348           cp_parser_type_specifier (parser,
15349                                     CP_PARSER_FLAGS_NONE,
15350                                     /*decl_specs=*/NULL,
15351                                     /*is_declarator=*/true,
15352                                     /*declares_class_or_enum=*/NULL,
15353                                     /*is_cv_qualifier=*/NULL);
15354
15355           parser->num_template_parameter_lists
15356             = saved_num_template_parameter_lists;
15357
15358           /* Leave the scope of the class.  */
15359           if (pushed_scope)
15360             pop_scope (pushed_scope);
15361
15362           constructor_p = !cp_parser_error_occurred (parser);
15363         }
15364     }
15365   else
15366     constructor_p = false;
15367   /* We did not really want to consume any tokens.  */
15368   cp_parser_abort_tentative_parse (parser);
15369
15370   return constructor_p;
15371 }
15372
15373 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15374    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15375    they must be performed once we are in the scope of the function.
15376
15377    Returns the function defined.  */
15378
15379 static tree
15380 cp_parser_function_definition_from_specifiers_and_declarator
15381   (cp_parser* parser,
15382    cp_decl_specifier_seq *decl_specifiers,
15383    tree attributes,
15384    const cp_declarator *declarator)
15385 {
15386   tree fn;
15387   bool success_p;
15388
15389   /* Begin the function-definition.  */
15390   success_p = start_function (decl_specifiers, declarator, attributes);
15391
15392   /* The things we're about to see are not directly qualified by any
15393      template headers we've seen thus far.  */
15394   reset_specialization ();
15395
15396   /* If there were names looked up in the decl-specifier-seq that we
15397      did not check, check them now.  We must wait until we are in the
15398      scope of the function to perform the checks, since the function
15399      might be a friend.  */
15400   perform_deferred_access_checks ();
15401
15402   if (!success_p)
15403     {
15404       /* Skip the entire function.  */
15405       error ("invalid function declaration");
15406       cp_parser_skip_to_end_of_block_or_statement (parser);
15407       fn = error_mark_node;
15408     }
15409   else
15410     fn = cp_parser_function_definition_after_declarator (parser,
15411                                                          /*inline_p=*/false);
15412
15413   return fn;
15414 }
15415
15416 /* Parse the part of a function-definition that follows the
15417    declarator.  INLINE_P is TRUE iff this function is an inline
15418    function defined with a class-specifier.
15419
15420    Returns the function defined.  */
15421
15422 static tree
15423 cp_parser_function_definition_after_declarator (cp_parser* parser,
15424                                                 bool inline_p)
15425 {
15426   tree fn;
15427   bool ctor_initializer_p = false;
15428   bool saved_in_unbraced_linkage_specification_p;
15429   bool saved_in_function_body;
15430   unsigned saved_num_template_parameter_lists;
15431
15432   saved_in_function_body = parser->in_function_body;
15433   parser->in_function_body = true;
15434   /* If the next token is `return', then the code may be trying to
15435      make use of the "named return value" extension that G++ used to
15436      support.  */
15437   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15438     {
15439       /* Consume the `return' keyword.  */
15440       cp_lexer_consume_token (parser->lexer);
15441       /* Look for the identifier that indicates what value is to be
15442          returned.  */
15443       cp_parser_identifier (parser);
15444       /* Issue an error message.  */
15445       error ("named return values are no longer supported");
15446       /* Skip tokens until we reach the start of the function body.  */
15447       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15448              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15449         cp_lexer_consume_token (parser->lexer);
15450     }
15451   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15452      anything declared inside `f'.  */
15453   saved_in_unbraced_linkage_specification_p
15454     = parser->in_unbraced_linkage_specification_p;
15455   parser->in_unbraced_linkage_specification_p = false;
15456   /* Inside the function, surrounding template-parameter-lists do not
15457      apply.  */
15458   saved_num_template_parameter_lists
15459     = parser->num_template_parameter_lists;
15460   parser->num_template_parameter_lists = 0;
15461   /* If the next token is `try', then we are looking at a
15462      function-try-block.  */
15463   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15464     ctor_initializer_p = cp_parser_function_try_block (parser);
15465   /* A function-try-block includes the function-body, so we only do
15466      this next part if we're not processing a function-try-block.  */
15467   else
15468     ctor_initializer_p
15469       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15470
15471   /* Finish the function.  */
15472   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15473                         (inline_p ? 2 : 0));
15474   /* Generate code for it, if necessary.  */
15475   expand_or_defer_fn (fn);
15476   /* Restore the saved values.  */
15477   parser->in_unbraced_linkage_specification_p
15478     = saved_in_unbraced_linkage_specification_p;
15479   parser->num_template_parameter_lists
15480     = saved_num_template_parameter_lists;
15481   parser->in_function_body = saved_in_function_body;
15482
15483   return fn;
15484 }
15485
15486 /* Parse a template-declaration, assuming that the `export' (and
15487    `extern') keywords, if present, has already been scanned.  MEMBER_P
15488    is as for cp_parser_template_declaration.  */
15489
15490 static void
15491 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15492 {
15493   tree decl = NULL_TREE;
15494   tree checks;
15495   tree parameter_list;
15496   bool friend_p = false;
15497   bool need_lang_pop;
15498
15499   /* Look for the `template' keyword.  */
15500   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15501     return;
15502
15503   /* And the `<'.  */
15504   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15505     return;
15506   if (at_class_scope_p () && current_function_decl)
15507     {
15508       /* 14.5.2.2 [temp.mem]
15509
15510          A local class shall not have member templates.  */
15511       error ("invalid declaration of member template in local class");
15512       cp_parser_skip_to_end_of_block_or_statement (parser);
15513       return;
15514     }
15515   /* [temp]
15516    
15517      A template ... shall not have C linkage.  */
15518   if (current_lang_name == lang_name_c)
15519     {
15520       error ("template with C linkage");
15521       /* Give it C++ linkage to avoid confusing other parts of the
15522          front end.  */
15523       push_lang_context (lang_name_cplusplus);
15524       need_lang_pop = true;
15525     }
15526   else
15527     need_lang_pop = false;
15528
15529   /* We cannot perform access checks on the template parameter
15530      declarations until we know what is being declared, just as we
15531      cannot check the decl-specifier list.  */
15532   push_deferring_access_checks (dk_deferred);
15533
15534   /* If the next token is `>', then we have an invalid
15535      specialization.  Rather than complain about an invalid template
15536      parameter, issue an error message here.  */
15537   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15538     {
15539       cp_parser_error (parser, "invalid explicit specialization");
15540       begin_specialization ();
15541       parameter_list = NULL_TREE;
15542     }
15543   else
15544     {
15545       /* Parse the template parameters.  */
15546       begin_template_parm_list ();
15547       parameter_list = cp_parser_template_parameter_list (parser);
15548       parameter_list = end_template_parm_list (parameter_list);
15549     }
15550
15551   /* Get the deferred access checks from the parameter list.  These
15552      will be checked once we know what is being declared, as for a
15553      member template the checks must be performed in the scope of the
15554      class containing the member.  */
15555   checks = get_deferred_access_checks ();
15556
15557   /* Look for the `>'.  */
15558   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15559   /* We just processed one more parameter list.  */
15560   ++parser->num_template_parameter_lists;
15561   /* If the next token is `template', there are more template
15562      parameters.  */
15563   if (cp_lexer_next_token_is_keyword (parser->lexer,
15564                                       RID_TEMPLATE))
15565     cp_parser_template_declaration_after_export (parser, member_p);
15566   else
15567     {
15568       /* There are no access checks when parsing a template, as we do not
15569          know if a specialization will be a friend.  */
15570       push_deferring_access_checks (dk_no_check);
15571       decl = cp_parser_single_declaration (parser,
15572                                            checks,
15573                                            member_p,
15574                                            &friend_p);
15575       pop_deferring_access_checks ();
15576
15577       /* If this is a member template declaration, let the front
15578          end know.  */
15579       if (member_p && !friend_p && decl)
15580         {
15581           if (TREE_CODE (decl) == TYPE_DECL)
15582             cp_parser_check_access_in_redeclaration (decl);
15583
15584           decl = finish_member_template_decl (decl);
15585         }
15586       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15587         make_friend_class (current_class_type, TREE_TYPE (decl),
15588                            /*complain=*/true);
15589     }
15590   /* We are done with the current parameter list.  */
15591   --parser->num_template_parameter_lists;
15592
15593   pop_deferring_access_checks ();
15594
15595   /* Finish up.  */
15596   finish_template_decl (parameter_list);
15597
15598   /* Register member declarations.  */
15599   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15600     finish_member_declaration (decl);
15601   /* For the erroneous case of a template with C linkage, we pushed an
15602      implicit C++ linkage scope; exit that scope now.  */
15603   if (need_lang_pop)
15604     pop_lang_context ();
15605   /* If DECL is a function template, we must return to parse it later.
15606      (Even though there is no definition, there might be default
15607      arguments that need handling.)  */
15608   if (member_p && decl
15609       && (TREE_CODE (decl) == FUNCTION_DECL
15610           || DECL_FUNCTION_TEMPLATE_P (decl)))
15611     TREE_VALUE (parser->unparsed_functions_queues)
15612       = tree_cons (NULL_TREE, decl,
15613                    TREE_VALUE (parser->unparsed_functions_queues));
15614 }
15615
15616 /* Perform the deferred access checks from a template-parameter-list.
15617    CHECKS is a TREE_LIST of access checks, as returned by
15618    get_deferred_access_checks.  */
15619
15620 static void
15621 cp_parser_perform_template_parameter_access_checks (tree checks)
15622 {
15623   ++processing_template_parmlist;
15624   perform_access_checks (checks);
15625   --processing_template_parmlist;
15626 }
15627
15628 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15629    `function-definition' sequence.  MEMBER_P is true, this declaration
15630    appears in a class scope.
15631
15632    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15633    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15634
15635 static tree
15636 cp_parser_single_declaration (cp_parser* parser,
15637                               tree checks,
15638                               bool member_p,
15639                               bool* friend_p)
15640 {
15641   int declares_class_or_enum;
15642   tree decl = NULL_TREE;
15643   cp_decl_specifier_seq decl_specifiers;
15644   bool function_definition_p = false;
15645
15646   /* This function is only used when processing a template
15647      declaration.  */
15648   gcc_assert (innermost_scope_kind () == sk_template_parms
15649               || innermost_scope_kind () == sk_template_spec);
15650
15651   /* Defer access checks until we know what is being declared.  */
15652   push_deferring_access_checks (dk_deferred);
15653
15654   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15655      alternative.  */
15656   cp_parser_decl_specifier_seq (parser,
15657                                 CP_PARSER_FLAGS_OPTIONAL,
15658                                 &decl_specifiers,
15659                                 &declares_class_or_enum);
15660   if (friend_p)
15661     *friend_p = cp_parser_friend_p (&decl_specifiers);
15662
15663   /* There are no template typedefs.  */
15664   if (decl_specifiers.specs[(int) ds_typedef])
15665     {
15666       error ("template declaration of %qs", "typedef");
15667       decl = error_mark_node;
15668     }
15669
15670   /* Gather up the access checks that occurred the
15671      decl-specifier-seq.  */
15672   stop_deferring_access_checks ();
15673
15674   /* Check for the declaration of a template class.  */
15675   if (declares_class_or_enum)
15676     {
15677       if (cp_parser_declares_only_class_p (parser))
15678         {
15679           decl = shadow_tag (&decl_specifiers);
15680
15681           /* In this case:
15682
15683                struct C {
15684                  friend template <typename T> struct A<T>::B;
15685                };
15686
15687              A<T>::B will be represented by a TYPENAME_TYPE, and
15688              therefore not recognized by shadow_tag.  */
15689           if (friend_p && *friend_p
15690               && !decl
15691               && decl_specifiers.type
15692               && TYPE_P (decl_specifiers.type))
15693             decl = decl_specifiers.type;
15694
15695           if (decl && decl != error_mark_node)
15696             decl = TYPE_NAME (decl);
15697           else
15698             decl = error_mark_node;
15699
15700           /* Perform access checks for template parameters.  */
15701           cp_parser_perform_template_parameter_access_checks (checks);
15702         }
15703     }
15704   /* If it's not a template class, try for a template function.  If
15705      the next token is a `;', then this declaration does not declare
15706      anything.  But, if there were errors in the decl-specifiers, then
15707      the error might well have come from an attempted class-specifier.
15708      In that case, there's no need to warn about a missing declarator.  */
15709   if (!decl
15710       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15711           || decl_specifiers.type != error_mark_node))
15712     decl = cp_parser_init_declarator (parser,
15713                                       &decl_specifiers,
15714                                       checks,
15715                                       /*function_definition_allowed_p=*/true,
15716                                       member_p,
15717                                       declares_class_or_enum,
15718                                       &function_definition_p);
15719
15720   pop_deferring_access_checks ();
15721
15722   /* Clear any current qualification; whatever comes next is the start
15723      of something new.  */
15724   parser->scope = NULL_TREE;
15725   parser->qualifying_scope = NULL_TREE;
15726   parser->object_scope = NULL_TREE;
15727   /* Look for a trailing `;' after the declaration.  */
15728   if (!function_definition_p
15729       && (decl == error_mark_node
15730           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15731     cp_parser_skip_to_end_of_block_or_statement (parser);
15732
15733   return decl;
15734 }
15735
15736 /* Parse a cast-expression that is not the operand of a unary "&".  */
15737
15738 static tree
15739 cp_parser_simple_cast_expression (cp_parser *parser)
15740 {
15741   return cp_parser_cast_expression (parser, /*address_p=*/false,
15742                                     /*cast_p=*/false);
15743 }
15744
15745 /* Parse a functional cast to TYPE.  Returns an expression
15746    representing the cast.  */
15747
15748 static tree
15749 cp_parser_functional_cast (cp_parser* parser, tree type)
15750 {
15751   tree expression_list;
15752   tree cast;
15753
15754   expression_list
15755     = cp_parser_parenthesized_expression_list (parser, false,
15756                                                /*cast_p=*/true,
15757                                                /*non_constant_p=*/NULL);
15758
15759   cast = build_functional_cast (type, expression_list);
15760   /* [expr.const]/1: In an integral constant expression "only type
15761      conversions to integral or enumeration type can be used".  */
15762   if (TREE_CODE (type) == TYPE_DECL)
15763     type = TREE_TYPE (type);
15764   if (cast != error_mark_node && !dependent_type_p (type)
15765       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15766     {
15767       if (cp_parser_non_integral_constant_expression
15768           (parser, "a call to a constructor"))
15769         return error_mark_node;
15770     }
15771   return cast;
15772 }
15773
15774 /* Save the tokens that make up the body of a member function defined
15775    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15776    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15777    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15778    for the member function.  */
15779
15780 static tree
15781 cp_parser_save_member_function_body (cp_parser* parser,
15782                                      cp_decl_specifier_seq *decl_specifiers,
15783                                      cp_declarator *declarator,
15784                                      tree attributes)
15785 {
15786   cp_token *first;
15787   cp_token *last;
15788   tree fn;
15789
15790   /* Create the function-declaration.  */
15791   fn = start_method (decl_specifiers, declarator, attributes);
15792   /* If something went badly wrong, bail out now.  */
15793   if (fn == error_mark_node)
15794     {
15795       /* If there's a function-body, skip it.  */
15796       if (cp_parser_token_starts_function_definition_p
15797           (cp_lexer_peek_token (parser->lexer)))
15798         cp_parser_skip_to_end_of_block_or_statement (parser);
15799       return error_mark_node;
15800     }
15801
15802   /* Remember it, if there default args to post process.  */
15803   cp_parser_save_default_args (parser, fn);
15804
15805   /* Save away the tokens that make up the body of the
15806      function.  */
15807   first = parser->lexer->next_token;
15808   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15809   /* Handle function try blocks.  */
15810   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15811     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15812   last = parser->lexer->next_token;
15813
15814   /* Save away the inline definition; we will process it when the
15815      class is complete.  */
15816   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15817   DECL_PENDING_INLINE_P (fn) = 1;
15818
15819   /* We need to know that this was defined in the class, so that
15820      friend templates are handled correctly.  */
15821   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15822
15823   /* We're done with the inline definition.  */
15824   finish_method (fn);
15825
15826   /* Add FN to the queue of functions to be parsed later.  */
15827   TREE_VALUE (parser->unparsed_functions_queues)
15828     = tree_cons (NULL_TREE, fn,
15829                  TREE_VALUE (parser->unparsed_functions_queues));
15830
15831   return fn;
15832 }
15833
15834 /* Parse a template-argument-list, as well as the trailing ">" (but
15835    not the opening ">").  See cp_parser_template_argument_list for the
15836    return value.  */
15837
15838 static tree
15839 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15840 {
15841   tree arguments;
15842   tree saved_scope;
15843   tree saved_qualifying_scope;
15844   tree saved_object_scope;
15845   bool saved_greater_than_is_operator_p;
15846   bool saved_skip_evaluation;
15847
15848   /* [temp.names]
15849
15850      When parsing a template-id, the first non-nested `>' is taken as
15851      the end of the template-argument-list rather than a greater-than
15852      operator.  */
15853   saved_greater_than_is_operator_p
15854     = parser->greater_than_is_operator_p;
15855   parser->greater_than_is_operator_p = false;
15856   /* Parsing the argument list may modify SCOPE, so we save it
15857      here.  */
15858   saved_scope = parser->scope;
15859   saved_qualifying_scope = parser->qualifying_scope;
15860   saved_object_scope = parser->object_scope;
15861   /* We need to evaluate the template arguments, even though this
15862      template-id may be nested within a "sizeof".  */
15863   saved_skip_evaluation = skip_evaluation;
15864   skip_evaluation = false;
15865   /* Parse the template-argument-list itself.  */
15866   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15867     arguments = NULL_TREE;
15868   else
15869     arguments = cp_parser_template_argument_list (parser);
15870   /* Look for the `>' that ends the template-argument-list. If we find
15871      a '>>' instead, it's probably just a typo.  */
15872   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15873     {
15874       if (!saved_greater_than_is_operator_p)
15875         {
15876           /* If we're in a nested template argument list, the '>>' has
15877             to be a typo for '> >'. We emit the error message, but we
15878             continue parsing and we push a '>' as next token, so that
15879             the argument list will be parsed correctly.  Note that the
15880             global source location is still on the token before the
15881             '>>', so we need to say explicitly where we want it.  */
15882           cp_token *token = cp_lexer_peek_token (parser->lexer);
15883           error ("%H%<>>%> should be %<> >%> "
15884                  "within a nested template argument list",
15885                  &token->location);
15886
15887           /* ??? Proper recovery should terminate two levels of
15888              template argument list here.  */
15889           token->type = CPP_GREATER;
15890         }
15891       else
15892         {
15893           /* If this is not a nested template argument list, the '>>'
15894             is a typo for '>'. Emit an error message and continue.
15895             Same deal about the token location, but here we can get it
15896             right by consuming the '>>' before issuing the diagnostic.  */
15897           cp_lexer_consume_token (parser->lexer);
15898           error ("spurious %<>>%>, use %<>%> to terminate "
15899                  "a template argument list");
15900         }
15901     }
15902   else
15903     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15904   /* The `>' token might be a greater-than operator again now.  */
15905   parser->greater_than_is_operator_p
15906     = saved_greater_than_is_operator_p;
15907   /* Restore the SAVED_SCOPE.  */
15908   parser->scope = saved_scope;
15909   parser->qualifying_scope = saved_qualifying_scope;
15910   parser->object_scope = saved_object_scope;
15911   skip_evaluation = saved_skip_evaluation;
15912
15913   return arguments;
15914 }
15915
15916 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15917    arguments, or the body of the function have not yet been parsed,
15918    parse them now.  */
15919
15920 static void
15921 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15922 {
15923   /* If this member is a template, get the underlying
15924      FUNCTION_DECL.  */
15925   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15926     member_function = DECL_TEMPLATE_RESULT (member_function);
15927
15928   /* There should not be any class definitions in progress at this
15929      point; the bodies of members are only parsed outside of all class
15930      definitions.  */
15931   gcc_assert (parser->num_classes_being_defined == 0);
15932   /* While we're parsing the member functions we might encounter more
15933      classes.  We want to handle them right away, but we don't want
15934      them getting mixed up with functions that are currently in the
15935      queue.  */
15936   parser->unparsed_functions_queues
15937     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15938
15939   /* Make sure that any template parameters are in scope.  */
15940   maybe_begin_member_template_processing (member_function);
15941
15942   /* If the body of the function has not yet been parsed, parse it
15943      now.  */
15944   if (DECL_PENDING_INLINE_P (member_function))
15945     {
15946       tree function_scope;
15947       cp_token_cache *tokens;
15948
15949       /* The function is no longer pending; we are processing it.  */
15950       tokens = DECL_PENDING_INLINE_INFO (member_function);
15951       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15952       DECL_PENDING_INLINE_P (member_function) = 0;
15953
15954       /* If this is a local class, enter the scope of the containing
15955          function.  */
15956       function_scope = current_function_decl;
15957       if (function_scope)
15958         push_function_context_to (function_scope);
15959
15960
15961       /* Push the body of the function onto the lexer stack.  */
15962       cp_parser_push_lexer_for_tokens (parser, tokens);
15963
15964       /* Let the front end know that we going to be defining this
15965          function.  */
15966       start_preparsed_function (member_function, NULL_TREE,
15967                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15968
15969       /* Don't do access checking if it is a templated function.  */
15970       if (processing_template_decl)
15971         push_deferring_access_checks (dk_no_check);
15972
15973       /* Now, parse the body of the function.  */
15974       cp_parser_function_definition_after_declarator (parser,
15975                                                       /*inline_p=*/true);
15976
15977       if (processing_template_decl)
15978         pop_deferring_access_checks ();
15979
15980       /* Leave the scope of the containing function.  */
15981       if (function_scope)
15982         pop_function_context_from (function_scope);
15983       cp_parser_pop_lexer (parser);
15984     }
15985
15986   /* Remove any template parameters from the symbol table.  */
15987   maybe_end_member_template_processing ();
15988
15989   /* Restore the queue.  */
15990   parser->unparsed_functions_queues
15991     = TREE_CHAIN (parser->unparsed_functions_queues);
15992 }
15993
15994 /* If DECL contains any default args, remember it on the unparsed
15995    functions queue.  */
15996
15997 static void
15998 cp_parser_save_default_args (cp_parser* parser, tree decl)
15999 {
16000   tree probe;
16001
16002   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16003        probe;
16004        probe = TREE_CHAIN (probe))
16005     if (TREE_PURPOSE (probe))
16006       {
16007         TREE_PURPOSE (parser->unparsed_functions_queues)
16008           = tree_cons (current_class_type, decl,
16009                        TREE_PURPOSE (parser->unparsed_functions_queues));
16010         break;
16011       }
16012   return;
16013 }
16014
16015 /* FN is a FUNCTION_DECL which may contains a parameter with an
16016    unparsed DEFAULT_ARG.  Parse the default args now.  This function
16017    assumes that the current scope is the scope in which the default
16018    argument should be processed.  */
16019
16020 static void
16021 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16022 {
16023   bool saved_local_variables_forbidden_p;
16024   tree parm;
16025
16026   /* While we're parsing the default args, we might (due to the
16027      statement expression extension) encounter more classes.  We want
16028      to handle them right away, but we don't want them getting mixed
16029      up with default args that are currently in the queue.  */
16030   parser->unparsed_functions_queues
16031     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16032
16033   /* Local variable names (and the `this' keyword) may not appear
16034      in a default argument.  */
16035   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16036   parser->local_variables_forbidden_p = true;
16037
16038   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16039        parm;
16040        parm = TREE_CHAIN (parm))
16041     {
16042       cp_token_cache *tokens;
16043       tree default_arg = TREE_PURPOSE (parm);
16044       tree parsed_arg;
16045       VEC(tree,gc) *insts;
16046       tree copy;
16047       unsigned ix;
16048
16049       if (!default_arg)
16050         continue;
16051
16052       if (TREE_CODE (default_arg) != DEFAULT_ARG)
16053         /* This can happen for a friend declaration for a function
16054            already declared with default arguments.  */
16055         continue;
16056
16057        /* Push the saved tokens for the default argument onto the parser's
16058           lexer stack.  */
16059       tokens = DEFARG_TOKENS (default_arg);
16060       cp_parser_push_lexer_for_tokens (parser, tokens);
16061
16062       /* Parse the assignment-expression.  */
16063       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16064
16065       if (!processing_template_decl)
16066         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16067       
16068       TREE_PURPOSE (parm) = parsed_arg;
16069
16070       /* Update any instantiations we've already created.  */
16071       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16072            VEC_iterate (tree, insts, ix, copy); ix++)
16073         TREE_PURPOSE (copy) = parsed_arg;
16074
16075       /* If the token stream has not been completely used up, then
16076          there was extra junk after the end of the default
16077          argument.  */
16078       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16079         cp_parser_error (parser, "expected %<,%>");
16080
16081       /* Revert to the main lexer.  */
16082       cp_parser_pop_lexer (parser);
16083     }
16084
16085   /* Make sure no default arg is missing.  */
16086   check_default_args (fn);
16087
16088   /* Restore the state of local_variables_forbidden_p.  */
16089   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16090
16091   /* Restore the queue.  */
16092   parser->unparsed_functions_queues
16093     = TREE_CHAIN (parser->unparsed_functions_queues);
16094 }
16095
16096 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16097    either a TYPE or an expression, depending on the form of the
16098    input.  The KEYWORD indicates which kind of expression we have
16099    encountered.  */
16100
16101 static tree
16102 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16103 {
16104   static const char *format;
16105   tree expr = NULL_TREE;
16106   const char *saved_message;
16107   bool saved_integral_constant_expression_p;
16108   bool saved_non_integral_constant_expression_p;
16109
16110   /* Initialize FORMAT the first time we get here.  */
16111   if (!format)
16112     format = "types may not be defined in '%s' expressions";
16113
16114   /* Types cannot be defined in a `sizeof' expression.  Save away the
16115      old message.  */
16116   saved_message = parser->type_definition_forbidden_message;
16117   /* And create the new one.  */
16118   parser->type_definition_forbidden_message
16119     = xmalloc (strlen (format)
16120                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16121                + 1 /* `\0' */);
16122   sprintf ((char *) parser->type_definition_forbidden_message,
16123            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16124
16125   /* The restrictions on constant-expressions do not apply inside
16126      sizeof expressions.  */
16127   saved_integral_constant_expression_p
16128     = parser->integral_constant_expression_p;
16129   saved_non_integral_constant_expression_p
16130     = parser->non_integral_constant_expression_p;
16131   parser->integral_constant_expression_p = false;
16132
16133   /* Do not actually evaluate the expression.  */
16134   ++skip_evaluation;
16135   /* If it's a `(', then we might be looking at the type-id
16136      construction.  */
16137   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16138     {
16139       tree type;
16140       bool saved_in_type_id_in_expr_p;
16141
16142       /* We can't be sure yet whether we're looking at a type-id or an
16143          expression.  */
16144       cp_parser_parse_tentatively (parser);
16145       /* Consume the `('.  */
16146       cp_lexer_consume_token (parser->lexer);
16147       /* Parse the type-id.  */
16148       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16149       parser->in_type_id_in_expr_p = true;
16150       type = cp_parser_type_id (parser);
16151       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16152       /* Now, look for the trailing `)'.  */
16153       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16154       /* If all went well, then we're done.  */
16155       if (cp_parser_parse_definitely (parser))
16156         {
16157           cp_decl_specifier_seq decl_specs;
16158
16159           /* Build a trivial decl-specifier-seq.  */
16160           clear_decl_specs (&decl_specs);
16161           decl_specs.type = type;
16162
16163           /* Call grokdeclarator to figure out what type this is.  */
16164           expr = grokdeclarator (NULL,
16165                                  &decl_specs,
16166                                  TYPENAME,
16167                                  /*initialized=*/0,
16168                                  /*attrlist=*/NULL);
16169         }
16170     }
16171
16172   /* If the type-id production did not work out, then we must be
16173      looking at the unary-expression production.  */
16174   if (!expr)
16175     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16176                                        /*cast_p=*/false);
16177   /* Go back to evaluating expressions.  */
16178   --skip_evaluation;
16179
16180   /* Free the message we created.  */
16181   free ((char *) parser->type_definition_forbidden_message);
16182   /* And restore the old one.  */
16183   parser->type_definition_forbidden_message = saved_message;
16184   parser->integral_constant_expression_p
16185     = saved_integral_constant_expression_p;
16186   parser->non_integral_constant_expression_p
16187     = saved_non_integral_constant_expression_p;
16188
16189   return expr;
16190 }
16191
16192 /* If the current declaration has no declarator, return true.  */
16193
16194 static bool
16195 cp_parser_declares_only_class_p (cp_parser *parser)
16196 {
16197   /* If the next token is a `;' or a `,' then there is no
16198      declarator.  */
16199   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16200           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16201 }
16202
16203 /* Update the DECL_SPECS to reflect the storage class indicated by
16204    KEYWORD.  */
16205
16206 static void
16207 cp_parser_set_storage_class (cp_parser *parser,
16208                              cp_decl_specifier_seq *decl_specs,
16209                              enum rid keyword)
16210 {
16211   cp_storage_class storage_class;
16212
16213   if (parser->in_unbraced_linkage_specification_p)
16214     {
16215       error ("invalid use of %qD in linkage specification",
16216              ridpointers[keyword]);
16217       return;
16218     }
16219   else if (decl_specs->storage_class != sc_none)
16220     {
16221       decl_specs->multiple_storage_classes_p = true;
16222       return;
16223     }
16224
16225   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16226       && decl_specs->specs[(int) ds_thread])
16227     {
16228       error ("%<__thread%> before %qD", ridpointers[keyword]);
16229       decl_specs->specs[(int) ds_thread] = 0;
16230     }
16231
16232   switch (keyword) 
16233     {
16234     case RID_AUTO:
16235       storage_class = sc_auto;
16236       break;
16237     case RID_REGISTER:
16238       storage_class = sc_register;
16239       break;
16240     case RID_STATIC:
16241       storage_class = sc_static;
16242       break;
16243     case RID_EXTERN:
16244       storage_class = sc_extern;
16245       break;
16246     case RID_MUTABLE:
16247       storage_class = sc_mutable;
16248       break;
16249     default:
16250       gcc_unreachable ();
16251     }
16252   decl_specs->storage_class = storage_class;
16253 }
16254
16255 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16256    is true, the type is a user-defined type; otherwise it is a
16257    built-in type specified by a keyword.  */
16258
16259 static void
16260 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16261                               tree type_spec,
16262                               bool user_defined_p)
16263 {
16264   decl_specs->any_specifiers_p = true;
16265
16266   /* If the user tries to redeclare bool or wchar_t (with, for
16267      example, in "typedef int wchar_t;") we remember that this is what
16268      happened.  In system headers, we ignore these declarations so
16269      that G++ can work with system headers that are not C++-safe.  */
16270   if (decl_specs->specs[(int) ds_typedef]
16271       && !user_defined_p
16272       && (type_spec == boolean_type_node
16273           || type_spec == wchar_type_node)
16274       && (decl_specs->type
16275           || decl_specs->specs[(int) ds_long]
16276           || decl_specs->specs[(int) ds_short]
16277           || decl_specs->specs[(int) ds_unsigned]
16278           || decl_specs->specs[(int) ds_signed]))
16279     {
16280       decl_specs->redefined_builtin_type = type_spec;
16281       if (!decl_specs->type)
16282         {
16283           decl_specs->type = type_spec;
16284           decl_specs->user_defined_type_p = false;
16285         }
16286     }
16287   else if (decl_specs->type)
16288     decl_specs->multiple_types_p = true;
16289   else
16290     {
16291       decl_specs->type = type_spec;
16292       decl_specs->user_defined_type_p = user_defined_p;
16293       decl_specs->redefined_builtin_type = NULL_TREE;
16294     }
16295 }
16296
16297 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16298    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16299
16300 static bool
16301 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16302 {
16303   return decl_specifiers->specs[(int) ds_friend] != 0;
16304 }
16305
16306 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16307    issue an error message indicating that TOKEN_DESC was expected.
16308
16309    Returns the token consumed, if the token had the appropriate type.
16310    Otherwise, returns NULL.  */
16311
16312 static cp_token *
16313 cp_parser_require (cp_parser* parser,
16314                    enum cpp_ttype type,
16315                    const char* token_desc)
16316 {
16317   if (cp_lexer_next_token_is (parser->lexer, type))
16318     return cp_lexer_consume_token (parser->lexer);
16319   else
16320     {
16321       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16322       if (!cp_parser_simulate_error (parser))
16323         {
16324           char *message = concat ("expected ", token_desc, NULL);
16325           cp_parser_error (parser, message);
16326           free (message);
16327         }
16328       return NULL;
16329     }
16330 }
16331
16332 /* Like cp_parser_require, except that tokens will be skipped until
16333    the desired token is found.  An error message is still produced if
16334    the next token is not as expected.  */
16335
16336 static void
16337 cp_parser_skip_until_found (cp_parser* parser,
16338                             enum cpp_ttype type,
16339                             const char* token_desc)
16340 {
16341   cp_token *token;
16342   unsigned nesting_depth = 0;
16343
16344   if (cp_parser_require (parser, type, token_desc))
16345     return;
16346
16347   /* Skip tokens until the desired token is found.  */
16348   while (true)
16349     {
16350       /* Peek at the next token.  */
16351       token = cp_lexer_peek_token (parser->lexer);
16352       /* If we've reached the token we want, consume it and
16353          stop.  */
16354       if (token->type == type && !nesting_depth)
16355         {
16356           cp_lexer_consume_token (parser->lexer);
16357           return;
16358         }
16359       /* If we've run out of tokens, stop.  */
16360       if (token->type == CPP_EOF)
16361         return;
16362       if (token->type == CPP_OPEN_BRACE
16363           || token->type == CPP_OPEN_PAREN
16364           || token->type == CPP_OPEN_SQUARE)
16365         ++nesting_depth;
16366       else if (token->type == CPP_CLOSE_BRACE
16367                || token->type == CPP_CLOSE_PAREN
16368                || token->type == CPP_CLOSE_SQUARE)
16369         {
16370           if (nesting_depth-- == 0)
16371             return;
16372         }
16373       /* Consume this token.  */
16374       cp_lexer_consume_token (parser->lexer);
16375     }
16376 }
16377
16378 /* If the next token is the indicated keyword, consume it.  Otherwise,
16379    issue an error message indicating that TOKEN_DESC was expected.
16380
16381    Returns the token consumed, if the token had the appropriate type.
16382    Otherwise, returns NULL.  */
16383
16384 static cp_token *
16385 cp_parser_require_keyword (cp_parser* parser,
16386                            enum rid keyword,
16387                            const char* token_desc)
16388 {
16389   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16390
16391   if (token && token->keyword != keyword)
16392     {
16393       dyn_string_t error_msg;
16394
16395       /* Format the error message.  */
16396       error_msg = dyn_string_new (0);
16397       dyn_string_append_cstr (error_msg, "expected ");
16398       dyn_string_append_cstr (error_msg, token_desc);
16399       cp_parser_error (parser, error_msg->s);
16400       dyn_string_delete (error_msg);
16401       return NULL;
16402     }
16403
16404   return token;
16405 }
16406
16407 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16408    function-definition.  */
16409
16410 static bool
16411 cp_parser_token_starts_function_definition_p (cp_token* token)
16412 {
16413   return (/* An ordinary function-body begins with an `{'.  */
16414           token->type == CPP_OPEN_BRACE
16415           /* A ctor-initializer begins with a `:'.  */
16416           || token->type == CPP_COLON
16417           /* A function-try-block begins with `try'.  */
16418           || token->keyword == RID_TRY
16419           /* The named return value extension begins with `return'.  */
16420           || token->keyword == RID_RETURN);
16421 }
16422
16423 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16424    definition.  */
16425
16426 static bool
16427 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16428 {
16429   cp_token *token;
16430
16431   token = cp_lexer_peek_token (parser->lexer);
16432   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16433 }
16434
16435 /* Returns TRUE iff the next token is the "," or ">" ending a
16436    template-argument.  */
16437
16438 static bool
16439 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16440 {
16441   cp_token *token;
16442
16443   token = cp_lexer_peek_token (parser->lexer);
16444   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16445 }
16446
16447 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16448    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16449
16450 static bool
16451 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16452                                                      size_t n)
16453 {
16454   cp_token *token;
16455
16456   token = cp_lexer_peek_nth_token (parser->lexer, n);
16457   if (token->type == CPP_LESS)
16458     return true;
16459   /* Check for the sequence `<::' in the original code. It would be lexed as
16460      `[:', where `[' is a digraph, and there is no whitespace before
16461      `:'.  */
16462   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16463     {
16464       cp_token *token2;
16465       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16466       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16467         return true;
16468     }
16469   return false;
16470 }
16471
16472 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16473    or none_type otherwise.  */
16474
16475 static enum tag_types
16476 cp_parser_token_is_class_key (cp_token* token)
16477 {
16478   switch (token->keyword)
16479     {
16480     case RID_CLASS:
16481       return class_type;
16482     case RID_STRUCT:
16483       return record_type;
16484     case RID_UNION:
16485       return union_type;
16486
16487     default:
16488       return none_type;
16489     }
16490 }
16491
16492 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16493
16494 static void
16495 cp_parser_check_class_key (enum tag_types class_key, tree type)
16496 {
16497   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16498     pedwarn ("%qs tag used in naming %q#T",
16499             class_key == union_type ? "union"
16500              : class_key == record_type ? "struct" : "class",
16501              type);
16502 }
16503
16504 /* Issue an error message if DECL is redeclared with different
16505    access than its original declaration [class.access.spec/3].
16506    This applies to nested classes and nested class templates.
16507    [class.mem/1].  */
16508
16509 static void
16510 cp_parser_check_access_in_redeclaration (tree decl)
16511 {
16512   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16513     return;
16514
16515   if ((TREE_PRIVATE (decl)
16516        != (current_access_specifier == access_private_node))
16517       || (TREE_PROTECTED (decl)
16518           != (current_access_specifier == access_protected_node)))
16519     error ("%qD redeclared with different access", decl);
16520 }
16521
16522 /* Look for the `template' keyword, as a syntactic disambiguator.
16523    Return TRUE iff it is present, in which case it will be
16524    consumed.  */
16525
16526 static bool
16527 cp_parser_optional_template_keyword (cp_parser *parser)
16528 {
16529   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16530     {
16531       /* The `template' keyword can only be used within templates;
16532          outside templates the parser can always figure out what is a
16533          template and what is not.  */
16534       if (!processing_template_decl)
16535         {
16536           error ("%<template%> (as a disambiguator) is only allowed "
16537                  "within templates");
16538           /* If this part of the token stream is rescanned, the same
16539              error message would be generated.  So, we purge the token
16540              from the stream.  */
16541           cp_lexer_purge_token (parser->lexer);
16542           return false;
16543         }
16544       else
16545         {
16546           /* Consume the `template' keyword.  */
16547           cp_lexer_consume_token (parser->lexer);
16548           return true;
16549         }
16550     }
16551
16552   return false;
16553 }
16554
16555 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16556    set PARSER->SCOPE, and perform other related actions.  */
16557
16558 static void
16559 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16560 {
16561   tree value;
16562   tree check;
16563
16564   /* Get the stored value.  */
16565   value = cp_lexer_consume_token (parser->lexer)->value;
16566   /* Perform any access checks that were deferred.  */
16567   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16568     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16569   /* Set the scope from the stored value.  */
16570   parser->scope = TREE_VALUE (value);
16571   parser->qualifying_scope = TREE_TYPE (value);
16572   parser->object_scope = NULL_TREE;
16573 }
16574
16575 /* Consume tokens up through a non-nested END token.  */
16576
16577 static void
16578 cp_parser_cache_group (cp_parser *parser,
16579                        enum cpp_ttype end,
16580                        unsigned depth)
16581 {
16582   while (true)
16583     {
16584       cp_token *token;
16585
16586       /* Abort a parenthesized expression if we encounter a brace.  */
16587       if ((end == CPP_CLOSE_PAREN || depth == 0)
16588           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16589         return;
16590       /* If we've reached the end of the file, stop.  */
16591       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16592         return;
16593       /* Consume the next token.  */
16594       token = cp_lexer_consume_token (parser->lexer);
16595       /* See if it starts a new group.  */
16596       if (token->type == CPP_OPEN_BRACE)
16597         {
16598           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16599           if (depth == 0)
16600             return;
16601         }
16602       else if (token->type == CPP_OPEN_PAREN)
16603         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16604       else if (token->type == end)
16605         return;
16606     }
16607 }
16608
16609 /* Begin parsing tentatively.  We always save tokens while parsing
16610    tentatively so that if the tentative parsing fails we can restore the
16611    tokens.  */
16612
16613 static void
16614 cp_parser_parse_tentatively (cp_parser* parser)
16615 {
16616   /* Enter a new parsing context.  */
16617   parser->context = cp_parser_context_new (parser->context);
16618   /* Begin saving tokens.  */
16619   cp_lexer_save_tokens (parser->lexer);
16620   /* In order to avoid repetitive access control error messages,
16621      access checks are queued up until we are no longer parsing
16622      tentatively.  */
16623   push_deferring_access_checks (dk_deferred);
16624 }
16625
16626 /* Commit to the currently active tentative parse.  */
16627
16628 static void
16629 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16630 {
16631   cp_parser_context *context;
16632   cp_lexer *lexer;
16633
16634   /* Mark all of the levels as committed.  */
16635   lexer = parser->lexer;
16636   for (context = parser->context; context->next; context = context->next)
16637     {
16638       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16639         break;
16640       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16641       while (!cp_lexer_saving_tokens (lexer))
16642         lexer = lexer->next;
16643       cp_lexer_commit_tokens (lexer);
16644     }
16645 }
16646
16647 /* Abort the currently active tentative parse.  All consumed tokens
16648    will be rolled back, and no diagnostics will be issued.  */
16649
16650 static void
16651 cp_parser_abort_tentative_parse (cp_parser* parser)
16652 {
16653   cp_parser_simulate_error (parser);
16654   /* Now, pretend that we want to see if the construct was
16655      successfully parsed.  */
16656   cp_parser_parse_definitely (parser);
16657 }
16658
16659 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16660    token stream.  Otherwise, commit to the tokens we have consumed.
16661    Returns true if no error occurred; false otherwise.  */
16662
16663 static bool
16664 cp_parser_parse_definitely (cp_parser* parser)
16665 {
16666   bool error_occurred;
16667   cp_parser_context *context;
16668
16669   /* Remember whether or not an error occurred, since we are about to
16670      destroy that information.  */
16671   error_occurred = cp_parser_error_occurred (parser);
16672   /* Remove the topmost context from the stack.  */
16673   context = parser->context;
16674   parser->context = context->next;
16675   /* If no parse errors occurred, commit to the tentative parse.  */
16676   if (!error_occurred)
16677     {
16678       /* Commit to the tokens read tentatively, unless that was
16679          already done.  */
16680       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16681         cp_lexer_commit_tokens (parser->lexer);
16682
16683       pop_to_parent_deferring_access_checks ();
16684     }
16685   /* Otherwise, if errors occurred, roll back our state so that things
16686      are just as they were before we began the tentative parse.  */
16687   else
16688     {
16689       cp_lexer_rollback_tokens (parser->lexer);
16690       pop_deferring_access_checks ();
16691     }
16692   /* Add the context to the front of the free list.  */
16693   context->next = cp_parser_context_free_list;
16694   cp_parser_context_free_list = context;
16695
16696   return !error_occurred;
16697 }
16698
16699 /* Returns true if we are parsing tentatively and are not committed to
16700    this tentative parse.  */
16701
16702 static bool
16703 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16704 {
16705   return (cp_parser_parsing_tentatively (parser)
16706           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16707 }
16708
16709 /* Returns nonzero iff an error has occurred during the most recent
16710    tentative parse.  */
16711
16712 static bool
16713 cp_parser_error_occurred (cp_parser* parser)
16714 {
16715   return (cp_parser_parsing_tentatively (parser)
16716           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16717 }
16718
16719 /* Returns nonzero if GNU extensions are allowed.  */
16720
16721 static bool
16722 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16723 {
16724   return parser->allow_gnu_extensions_p;
16725 }
16726 \f
16727 /* Objective-C++ Productions */
16728
16729
16730 /* Parse an Objective-C expression, which feeds into a primary-expression
16731    above.
16732
16733    objc-expression:
16734      objc-message-expression
16735      objc-string-literal
16736      objc-encode-expression
16737      objc-protocol-expression
16738      objc-selector-expression
16739
16740   Returns a tree representation of the expression.  */
16741
16742 static tree
16743 cp_parser_objc_expression (cp_parser* parser)
16744 {
16745   /* Try to figure out what kind of declaration is present.  */
16746   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16747
16748   switch (kwd->type)
16749     {
16750     case CPP_OPEN_SQUARE:
16751       return cp_parser_objc_message_expression (parser);
16752
16753     case CPP_OBJC_STRING:
16754       kwd = cp_lexer_consume_token (parser->lexer);
16755       return objc_build_string_object (kwd->value);
16756
16757     case CPP_KEYWORD:
16758       switch (kwd->keyword)
16759         {
16760         case RID_AT_ENCODE:
16761           return cp_parser_objc_encode_expression (parser);
16762
16763         case RID_AT_PROTOCOL:
16764           return cp_parser_objc_protocol_expression (parser);
16765
16766         case RID_AT_SELECTOR:
16767           return cp_parser_objc_selector_expression (parser);
16768
16769         default:
16770           break;
16771         }
16772     default:
16773       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16774       cp_parser_skip_to_end_of_block_or_statement (parser);
16775     }
16776
16777   return error_mark_node;
16778 }
16779
16780 /* Parse an Objective-C message expression.
16781
16782    objc-message-expression:
16783      [ objc-message-receiver objc-message-args ]
16784
16785    Returns a representation of an Objective-C message.  */
16786
16787 static tree
16788 cp_parser_objc_message_expression (cp_parser* parser)
16789 {
16790   tree receiver, messageargs;
16791
16792   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16793   receiver = cp_parser_objc_message_receiver (parser);
16794   messageargs = cp_parser_objc_message_args (parser);
16795   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16796
16797   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16798 }
16799
16800 /* Parse an objc-message-receiver.
16801
16802    objc-message-receiver:
16803      expression
16804      simple-type-specifier
16805
16806   Returns a representation of the type or expression.  */
16807
16808 static tree
16809 cp_parser_objc_message_receiver (cp_parser* parser)
16810 {
16811   tree rcv;
16812
16813   /* An Objective-C message receiver may be either (1) a type
16814      or (2) an expression.  */
16815   cp_parser_parse_tentatively (parser);
16816   rcv = cp_parser_expression (parser, false);
16817
16818   if (cp_parser_parse_definitely (parser))
16819     return rcv;
16820
16821   rcv = cp_parser_simple_type_specifier (parser,
16822                                          /*decl_specs=*/NULL,
16823                                          CP_PARSER_FLAGS_NONE);
16824
16825   return objc_get_class_reference (rcv);
16826 }
16827
16828 /* Parse the arguments and selectors comprising an Objective-C message.
16829
16830    objc-message-args:
16831      objc-selector
16832      objc-selector-args
16833      objc-selector-args , objc-comma-args
16834
16835    objc-selector-args:
16836      objc-selector [opt] : assignment-expression
16837      objc-selector-args objc-selector [opt] : assignment-expression
16838
16839    objc-comma-args:
16840      assignment-expression
16841      objc-comma-args , assignment-expression
16842
16843    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16844    selector arguments and TREE_VALUE containing a list of comma
16845    arguments.  */
16846
16847 static tree
16848 cp_parser_objc_message_args (cp_parser* parser)
16849 {
16850   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16851   bool maybe_unary_selector_p = true;
16852   cp_token *token = cp_lexer_peek_token (parser->lexer);
16853
16854   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16855     {
16856       tree selector = NULL_TREE, arg;
16857
16858       if (token->type != CPP_COLON)
16859         selector = cp_parser_objc_selector (parser);
16860
16861       /* Detect if we have a unary selector.  */
16862       if (maybe_unary_selector_p
16863           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16864         return build_tree_list (selector, NULL_TREE);
16865
16866       maybe_unary_selector_p = false;
16867       cp_parser_require (parser, CPP_COLON, "`:'");
16868       arg = cp_parser_assignment_expression (parser, false);
16869
16870       sel_args
16871         = chainon (sel_args,
16872                    build_tree_list (selector, arg));
16873
16874       token = cp_lexer_peek_token (parser->lexer);
16875     }
16876
16877   /* Handle non-selector arguments, if any. */
16878   while (token->type == CPP_COMMA)
16879     {
16880       tree arg;
16881
16882       cp_lexer_consume_token (parser->lexer);
16883       arg = cp_parser_assignment_expression (parser, false);
16884
16885       addl_args
16886         = chainon (addl_args,
16887                    build_tree_list (NULL_TREE, arg));
16888
16889       token = cp_lexer_peek_token (parser->lexer);
16890     }
16891
16892   return build_tree_list (sel_args, addl_args);
16893 }
16894
16895 /* Parse an Objective-C encode expression.
16896
16897    objc-encode-expression:
16898      @encode objc-typename
16899
16900    Returns an encoded representation of the type argument.  */
16901
16902 static tree
16903 cp_parser_objc_encode_expression (cp_parser* parser)
16904 {
16905   tree type;
16906
16907   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16908   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16909   type = complete_type (cp_parser_type_id (parser));
16910   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16911
16912   if (!type)
16913     {
16914       error ("%<@encode%> must specify a type as an argument");
16915       return error_mark_node;
16916     }
16917
16918   return objc_build_encode_expr (type);
16919 }
16920
16921 /* Parse an Objective-C @defs expression.  */
16922
16923 static tree
16924 cp_parser_objc_defs_expression (cp_parser *parser)
16925 {
16926   tree name;
16927
16928   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16929   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16930   name = cp_parser_identifier (parser);
16931   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16932
16933   return objc_get_class_ivars (name);
16934 }
16935
16936 /* Parse an Objective-C protocol expression.
16937
16938   objc-protocol-expression:
16939     @protocol ( identifier )
16940
16941   Returns a representation of the protocol expression.  */
16942
16943 static tree
16944 cp_parser_objc_protocol_expression (cp_parser* parser)
16945 {
16946   tree proto;
16947
16948   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16949   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16950   proto = cp_parser_identifier (parser);
16951   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16952
16953   return objc_build_protocol_expr (proto);
16954 }
16955
16956 /* Parse an Objective-C selector expression.
16957
16958    objc-selector-expression:
16959      @selector ( objc-method-signature )
16960
16961    objc-method-signature:
16962      objc-selector
16963      objc-selector-seq
16964
16965    objc-selector-seq:
16966      objc-selector :
16967      objc-selector-seq objc-selector :
16968
16969   Returns a representation of the method selector.  */
16970
16971 static tree
16972 cp_parser_objc_selector_expression (cp_parser* parser)
16973 {
16974   tree sel_seq = NULL_TREE;
16975   bool maybe_unary_selector_p = true;
16976   cp_token *token;
16977
16978   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16979   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16980   token = cp_lexer_peek_token (parser->lexer);
16981
16982   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16983          || token->type == CPP_SCOPE)
16984     {
16985       tree selector = NULL_TREE;
16986
16987       if (token->type != CPP_COLON
16988           || token->type == CPP_SCOPE)
16989         selector = cp_parser_objc_selector (parser);
16990
16991       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16992           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16993         {
16994           /* Detect if we have a unary selector.  */
16995           if (maybe_unary_selector_p)
16996             {
16997               sel_seq = selector;
16998               goto finish_selector;
16999             }
17000           else
17001             {
17002               cp_parser_error (parser, "expected %<:%>");
17003             }
17004         }
17005       maybe_unary_selector_p = false;
17006       token = cp_lexer_consume_token (parser->lexer);
17007       
17008       if (token->type == CPP_SCOPE)
17009         {
17010           sel_seq
17011             = chainon (sel_seq,
17012                        build_tree_list (selector, NULL_TREE));
17013           sel_seq
17014             = chainon (sel_seq,
17015                        build_tree_list (NULL_TREE, NULL_TREE));
17016         }
17017       else
17018         sel_seq
17019           = chainon (sel_seq,
17020                      build_tree_list (selector, NULL_TREE));
17021
17022       token = cp_lexer_peek_token (parser->lexer);
17023     }
17024
17025  finish_selector:
17026   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17027
17028   return objc_build_selector_expr (sel_seq);
17029 }
17030
17031 /* Parse a list of identifiers.
17032
17033    objc-identifier-list:
17034      identifier
17035      objc-identifier-list , identifier
17036
17037    Returns a TREE_LIST of identifier nodes.  */
17038
17039 static tree
17040 cp_parser_objc_identifier_list (cp_parser* parser)
17041 {
17042   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17043   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17044
17045   while (sep->type == CPP_COMMA)
17046     {
17047       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17048       list = chainon (list,
17049                       build_tree_list (NULL_TREE,
17050                                        cp_parser_identifier (parser)));
17051       sep = cp_lexer_peek_token (parser->lexer);
17052     }
17053
17054   return list;
17055 }
17056
17057 /* Parse an Objective-C alias declaration.
17058
17059    objc-alias-declaration:
17060      @compatibility_alias identifier identifier ;
17061
17062    This function registers the alias mapping with the Objective-C front-end.
17063    It returns nothing.  */
17064
17065 static void
17066 cp_parser_objc_alias_declaration (cp_parser* parser)
17067 {
17068   tree alias, orig;
17069
17070   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17071   alias = cp_parser_identifier (parser);
17072   orig = cp_parser_identifier (parser);
17073   objc_declare_alias (alias, orig);
17074   cp_parser_consume_semicolon_at_end_of_statement (parser);
17075 }
17076
17077 /* Parse an Objective-C class forward-declaration.
17078
17079    objc-class-declaration:
17080      @class objc-identifier-list ;
17081
17082    The function registers the forward declarations with the Objective-C
17083    front-end.  It returns nothing.  */
17084
17085 static void
17086 cp_parser_objc_class_declaration (cp_parser* parser)
17087 {
17088   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17089   objc_declare_class (cp_parser_objc_identifier_list (parser));
17090   cp_parser_consume_semicolon_at_end_of_statement (parser);
17091 }
17092
17093 /* Parse a list of Objective-C protocol references.
17094
17095    objc-protocol-refs-opt:
17096      objc-protocol-refs [opt]
17097
17098    objc-protocol-refs:
17099      < objc-identifier-list >
17100
17101    Returns a TREE_LIST of identifiers, if any.  */
17102
17103 static tree
17104 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17105 {
17106   tree protorefs = NULL_TREE;
17107
17108   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17109     {
17110       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17111       protorefs = cp_parser_objc_identifier_list (parser);
17112       cp_parser_require (parser, CPP_GREATER, "`>'");
17113     }
17114
17115   return protorefs;
17116 }
17117
17118 /* Parse a Objective-C visibility specification.  */
17119
17120 static void
17121 cp_parser_objc_visibility_spec (cp_parser* parser)
17122 {
17123   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17124
17125   switch (vis->keyword)
17126     {
17127     case RID_AT_PRIVATE:
17128       objc_set_visibility (2);
17129       break;
17130     case RID_AT_PROTECTED:
17131       objc_set_visibility (0);
17132       break;
17133     case RID_AT_PUBLIC:
17134       objc_set_visibility (1);
17135       break;
17136     default:
17137       return;
17138     }
17139
17140   /* Eat '@private'/'@protected'/'@public'.  */
17141   cp_lexer_consume_token (parser->lexer);
17142 }
17143
17144 /* Parse an Objective-C method type.  */
17145
17146 static void
17147 cp_parser_objc_method_type (cp_parser* parser)
17148 {
17149   objc_set_method_type
17150    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17151     ? PLUS_EXPR
17152     : MINUS_EXPR);
17153 }
17154
17155 /* Parse an Objective-C protocol qualifier.  */
17156
17157 static tree
17158 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17159 {
17160   tree quals = NULL_TREE, node;
17161   cp_token *token = cp_lexer_peek_token (parser->lexer);
17162
17163   node = token->value;
17164
17165   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17166          && (node == ridpointers [(int) RID_IN]
17167              || node == ridpointers [(int) RID_OUT]
17168              || node == ridpointers [(int) RID_INOUT]
17169              || node == ridpointers [(int) RID_BYCOPY]
17170              || node == ridpointers [(int) RID_BYREF]
17171              || node == ridpointers [(int) RID_ONEWAY]))
17172     {
17173       quals = tree_cons (NULL_TREE, node, quals);
17174       cp_lexer_consume_token (parser->lexer);
17175       token = cp_lexer_peek_token (parser->lexer);
17176       node = token->value;
17177     }
17178
17179   return quals;
17180 }
17181
17182 /* Parse an Objective-C typename.  */
17183
17184 static tree
17185 cp_parser_objc_typename (cp_parser* parser)
17186 {
17187   tree typename = NULL_TREE;
17188
17189   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17190     {
17191       tree proto_quals, cp_type = NULL_TREE;
17192
17193       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17194       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17195
17196       /* An ObjC type name may consist of just protocol qualifiers, in which
17197          case the type shall default to 'id'.  */
17198       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17199         cp_type = cp_parser_type_id (parser);
17200
17201       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17202       typename = build_tree_list (proto_quals, cp_type);
17203     }
17204
17205   return typename;
17206 }
17207
17208 /* Check to see if TYPE refers to an Objective-C selector name.  */
17209
17210 static bool
17211 cp_parser_objc_selector_p (enum cpp_ttype type)
17212 {
17213   return (type == CPP_NAME || type == CPP_KEYWORD
17214           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17215           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17216           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17217           || type == CPP_XOR || type == CPP_XOR_EQ);
17218 }
17219
17220 /* Parse an Objective-C selector.  */
17221
17222 static tree
17223 cp_parser_objc_selector (cp_parser* parser)
17224 {
17225   cp_token *token = cp_lexer_consume_token (parser->lexer);
17226
17227   if (!cp_parser_objc_selector_p (token->type))
17228     {
17229       error ("invalid Objective-C++ selector name");
17230       return error_mark_node;
17231     }
17232
17233   /* C++ operator names are allowed to appear in ObjC selectors.  */
17234   switch (token->type)
17235     {
17236     case CPP_AND_AND: return get_identifier ("and");
17237     case CPP_AND_EQ: return get_identifier ("and_eq");
17238     case CPP_AND: return get_identifier ("bitand");
17239     case CPP_OR: return get_identifier ("bitor");
17240     case CPP_COMPL: return get_identifier ("compl");
17241     case CPP_NOT: return get_identifier ("not");
17242     case CPP_NOT_EQ: return get_identifier ("not_eq");
17243     case CPP_OR_OR: return get_identifier ("or");
17244     case CPP_OR_EQ: return get_identifier ("or_eq");
17245     case CPP_XOR: return get_identifier ("xor");
17246     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17247     default: return token->value;
17248     }
17249 }
17250
17251 /* Parse an Objective-C params list.  */
17252
17253 static tree
17254 cp_parser_objc_method_keyword_params (cp_parser* parser)
17255 {
17256   tree params = NULL_TREE;
17257   bool maybe_unary_selector_p = true;
17258   cp_token *token = cp_lexer_peek_token (parser->lexer);
17259
17260   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17261     {
17262       tree selector = NULL_TREE, typename, identifier;
17263
17264       if (token->type != CPP_COLON)
17265         selector = cp_parser_objc_selector (parser);
17266
17267       /* Detect if we have a unary selector.  */
17268       if (maybe_unary_selector_p
17269           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17270         return selector;
17271
17272       maybe_unary_selector_p = false;
17273       cp_parser_require (parser, CPP_COLON, "`:'");
17274       typename = cp_parser_objc_typename (parser);
17275       identifier = cp_parser_identifier (parser);
17276
17277       params
17278         = chainon (params,
17279                    objc_build_keyword_decl (selector,
17280                                             typename,
17281                                             identifier));
17282
17283       token = cp_lexer_peek_token (parser->lexer);
17284     }
17285
17286   return params;
17287 }
17288
17289 /* Parse the non-keyword Objective-C params.  */
17290
17291 static tree
17292 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17293 {
17294   tree params = make_node (TREE_LIST);
17295   cp_token *token = cp_lexer_peek_token (parser->lexer);
17296   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17297
17298   while (token->type == CPP_COMMA)
17299     {
17300       cp_parameter_declarator *parmdecl;
17301       tree parm;
17302
17303       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17304       token = cp_lexer_peek_token (parser->lexer);
17305
17306       if (token->type == CPP_ELLIPSIS)
17307         {
17308           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17309           *ellipsisp = true;
17310           break;
17311         }
17312
17313       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17314       parm = grokdeclarator (parmdecl->declarator,
17315                              &parmdecl->decl_specifiers,
17316                              PARM, /*initialized=*/0,
17317                              /*attrlist=*/NULL);
17318
17319       chainon (params, build_tree_list (NULL_TREE, parm));
17320       token = cp_lexer_peek_token (parser->lexer);
17321     }
17322
17323   return params;
17324 }
17325
17326 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17327
17328 static void
17329 cp_parser_objc_interstitial_code (cp_parser* parser)
17330 {
17331   cp_token *token = cp_lexer_peek_token (parser->lexer);
17332
17333   /* If the next token is `extern' and the following token is a string
17334      literal, then we have a linkage specification.  */
17335   if (token->keyword == RID_EXTERN
17336       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17337     cp_parser_linkage_specification (parser);
17338   /* Handle #pragma, if any.  */
17339   else if (token->type == CPP_PRAGMA)
17340     cp_lexer_handle_pragma (parser->lexer);
17341   /* Allow stray semicolons.  */
17342   else if (token->type == CPP_SEMICOLON)
17343     cp_lexer_consume_token (parser->lexer);
17344   /* Finally, try to parse a block-declaration, or a function-definition.  */
17345   else
17346     cp_parser_block_declaration (parser, /*statement_p=*/false);
17347 }
17348
17349 /* Parse a method signature.  */
17350
17351 static tree
17352 cp_parser_objc_method_signature (cp_parser* parser)
17353 {
17354   tree rettype, kwdparms, optparms;
17355   bool ellipsis = false;
17356
17357   cp_parser_objc_method_type (parser);
17358   rettype = cp_parser_objc_typename (parser);
17359   kwdparms = cp_parser_objc_method_keyword_params (parser);
17360   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17361
17362   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17363 }
17364
17365 /* Pars an Objective-C method prototype list.  */
17366
17367 static void
17368 cp_parser_objc_method_prototype_list (cp_parser* parser)
17369 {
17370   cp_token *token = cp_lexer_peek_token (parser->lexer);
17371
17372   while (token->keyword != RID_AT_END)
17373     {
17374       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17375         {
17376           objc_add_method_declaration
17377            (cp_parser_objc_method_signature (parser));
17378           cp_parser_consume_semicolon_at_end_of_statement (parser);
17379         }
17380       else
17381         /* Allow for interspersed non-ObjC++ code.  */
17382         cp_parser_objc_interstitial_code (parser);
17383
17384       token = cp_lexer_peek_token (parser->lexer);
17385     }
17386
17387   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17388   objc_finish_interface ();
17389 }
17390
17391 /* Parse an Objective-C method definition list.  */
17392
17393 static void
17394 cp_parser_objc_method_definition_list (cp_parser* parser)
17395 {
17396   cp_token *token = cp_lexer_peek_token (parser->lexer);
17397
17398   while (token->keyword != RID_AT_END)
17399     {
17400       tree meth;
17401
17402       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17403         {
17404           push_deferring_access_checks (dk_deferred);
17405           objc_start_method_definition
17406            (cp_parser_objc_method_signature (parser));
17407
17408           /* For historical reasons, we accept an optional semicolon.  */
17409           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17410             cp_lexer_consume_token (parser->lexer);
17411
17412           perform_deferred_access_checks ();
17413           stop_deferring_access_checks ();
17414           meth = cp_parser_function_definition_after_declarator (parser,
17415                                                                  false);
17416           pop_deferring_access_checks ();
17417           objc_finish_method_definition (meth);
17418         }
17419       else
17420         /* Allow for interspersed non-ObjC++ code.  */
17421         cp_parser_objc_interstitial_code (parser);
17422
17423       token = cp_lexer_peek_token (parser->lexer);
17424     }
17425
17426   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17427   objc_finish_implementation ();
17428 }
17429
17430 /* Parse Objective-C ivars.  */
17431
17432 static void
17433 cp_parser_objc_class_ivars (cp_parser* parser)
17434 {
17435   cp_token *token = cp_lexer_peek_token (parser->lexer);
17436
17437   if (token->type != CPP_OPEN_BRACE)
17438     return;     /* No ivars specified.  */
17439
17440   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17441   token = cp_lexer_peek_token (parser->lexer);
17442
17443   while (token->type != CPP_CLOSE_BRACE)
17444     {
17445       cp_decl_specifier_seq declspecs;
17446       int decl_class_or_enum_p;
17447       tree prefix_attributes;
17448
17449       cp_parser_objc_visibility_spec (parser);
17450
17451       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17452         break;
17453
17454       cp_parser_decl_specifier_seq (parser,
17455                                     CP_PARSER_FLAGS_OPTIONAL,
17456                                     &declspecs,
17457                                     &decl_class_or_enum_p);
17458       prefix_attributes = declspecs.attributes;
17459       declspecs.attributes = NULL_TREE;
17460
17461       /* Keep going until we hit the `;' at the end of the
17462          declaration.  */
17463       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17464         {
17465           tree width = NULL_TREE, attributes, first_attribute, decl;
17466           cp_declarator *declarator = NULL;
17467           int ctor_dtor_or_conv_p;
17468
17469           /* Check for a (possibly unnamed) bitfield declaration.  */
17470           token = cp_lexer_peek_token (parser->lexer);
17471           if (token->type == CPP_COLON)
17472             goto eat_colon;
17473
17474           if (token->type == CPP_NAME
17475               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17476                   == CPP_COLON))
17477             {
17478               /* Get the name of the bitfield.  */
17479               declarator = make_id_declarator (NULL_TREE,
17480                                                cp_parser_identifier (parser),
17481                                                sfk_none);
17482
17483              eat_colon:
17484               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17485               /* Get the width of the bitfield.  */
17486               width
17487                 = cp_parser_constant_expression (parser,
17488                                                  /*allow_non_constant=*/false,
17489                                                  NULL);
17490             }
17491           else
17492             {
17493               /* Parse the declarator.  */
17494               declarator
17495                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17496                                         &ctor_dtor_or_conv_p,
17497                                         /*parenthesized_p=*/NULL,
17498                                         /*member_p=*/false);
17499             }
17500
17501           /* Look for attributes that apply to the ivar.  */
17502           attributes = cp_parser_attributes_opt (parser);
17503           /* Remember which attributes are prefix attributes and
17504              which are not.  */
17505           first_attribute = attributes;
17506           /* Combine the attributes.  */
17507           attributes = chainon (prefix_attributes, attributes);
17508
17509           if (width)
17510             {
17511               /* Create the bitfield declaration.  */
17512               decl = grokbitfield (declarator, &declspecs, width);
17513               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17514             }
17515           else
17516             decl = grokfield (declarator, &declspecs, 
17517                               NULL_TREE, /*init_const_expr_p=*/false,
17518                               NULL_TREE, attributes);
17519
17520           /* Add the instance variable.  */
17521           objc_add_instance_variable (decl);
17522
17523           /* Reset PREFIX_ATTRIBUTES.  */
17524           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17525             attributes = TREE_CHAIN (attributes);
17526           if (attributes)
17527             TREE_CHAIN (attributes) = NULL_TREE;
17528
17529           token = cp_lexer_peek_token (parser->lexer);
17530
17531           if (token->type == CPP_COMMA)
17532             {
17533               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17534               continue;
17535             }
17536           break;
17537         }
17538
17539       cp_parser_consume_semicolon_at_end_of_statement (parser);
17540       token = cp_lexer_peek_token (parser->lexer);
17541     }
17542
17543   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17544   /* For historical reasons, we accept an optional semicolon.  */
17545   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17546     cp_lexer_consume_token (parser->lexer);
17547 }
17548
17549 /* Parse an Objective-C protocol declaration.  */
17550
17551 static void
17552 cp_parser_objc_protocol_declaration (cp_parser* parser)
17553 {
17554   tree proto, protorefs;
17555   cp_token *tok;
17556
17557   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17558   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17559     {
17560       error ("identifier expected after %<@protocol%>");
17561       goto finish;
17562     }
17563
17564   /* See if we have a forward declaration or a definition.  */
17565   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17566
17567   /* Try a forward declaration first.  */
17568   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17569     {
17570       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17571      finish:
17572       cp_parser_consume_semicolon_at_end_of_statement (parser);
17573     }
17574
17575   /* Ok, we got a full-fledged definition (or at least should).  */
17576   else
17577     {
17578       proto = cp_parser_identifier (parser);
17579       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17580       objc_start_protocol (proto, protorefs);
17581       cp_parser_objc_method_prototype_list (parser);
17582     }
17583 }
17584
17585 /* Parse an Objective-C superclass or category.  */
17586
17587 static void
17588 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17589                                                           tree *categ)
17590 {
17591   cp_token *next = cp_lexer_peek_token (parser->lexer);
17592
17593   *super = *categ = NULL_TREE;
17594   if (next->type == CPP_COLON)
17595     {
17596       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17597       *super = cp_parser_identifier (parser);
17598     }
17599   else if (next->type == CPP_OPEN_PAREN)
17600     {
17601       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17602       *categ = cp_parser_identifier (parser);
17603       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17604     }
17605 }
17606
17607 /* Parse an Objective-C class interface.  */
17608
17609 static void
17610 cp_parser_objc_class_interface (cp_parser* parser)
17611 {
17612   tree name, super, categ, protos;
17613
17614   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17615   name = cp_parser_identifier (parser);
17616   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17617   protos = cp_parser_objc_protocol_refs_opt (parser);
17618
17619   /* We have either a class or a category on our hands.  */
17620   if (categ)
17621     objc_start_category_interface (name, categ, protos);
17622   else
17623     {
17624       objc_start_class_interface (name, super, protos);
17625       /* Handle instance variable declarations, if any.  */
17626       cp_parser_objc_class_ivars (parser);
17627       objc_continue_interface ();
17628     }
17629
17630   cp_parser_objc_method_prototype_list (parser);
17631 }
17632
17633 /* Parse an Objective-C class implementation.  */
17634
17635 static void
17636 cp_parser_objc_class_implementation (cp_parser* parser)
17637 {
17638   tree name, super, categ;
17639
17640   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17641   name = cp_parser_identifier (parser);
17642   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17643
17644   /* We have either a class or a category on our hands.  */
17645   if (categ)
17646     objc_start_category_implementation (name, categ);
17647   else
17648     {
17649       objc_start_class_implementation (name, super);
17650       /* Handle instance variable declarations, if any.  */
17651       cp_parser_objc_class_ivars (parser);
17652       objc_continue_implementation ();
17653     }
17654
17655   cp_parser_objc_method_definition_list (parser);
17656 }
17657
17658 /* Consume the @end token and finish off the implementation.  */
17659
17660 static void
17661 cp_parser_objc_end_implementation (cp_parser* parser)
17662 {
17663   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17664   objc_finish_implementation ();
17665 }
17666
17667 /* Parse an Objective-C declaration.  */
17668
17669 static void
17670 cp_parser_objc_declaration (cp_parser* parser)
17671 {
17672   /* Try to figure out what kind of declaration is present.  */
17673   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17674
17675   switch (kwd->keyword)
17676     {
17677     case RID_AT_ALIAS:
17678       cp_parser_objc_alias_declaration (parser);
17679       break;
17680     case RID_AT_CLASS:
17681       cp_parser_objc_class_declaration (parser);
17682       break;
17683     case RID_AT_PROTOCOL:
17684       cp_parser_objc_protocol_declaration (parser);
17685       break;
17686     case RID_AT_INTERFACE:
17687       cp_parser_objc_class_interface (parser);
17688       break;
17689     case RID_AT_IMPLEMENTATION:
17690       cp_parser_objc_class_implementation (parser);
17691       break;
17692     case RID_AT_END:
17693       cp_parser_objc_end_implementation (parser);
17694       break;
17695     default:
17696       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17697       cp_parser_skip_to_end_of_block_or_statement (parser);
17698     }
17699 }
17700
17701 /* Parse an Objective-C try-catch-finally statement.
17702
17703    objc-try-catch-finally-stmt:
17704      @try compound-statement objc-catch-clause-seq [opt]
17705        objc-finally-clause [opt]
17706
17707    objc-catch-clause-seq:
17708      objc-catch-clause objc-catch-clause-seq [opt]
17709
17710    objc-catch-clause:
17711      @catch ( exception-declaration ) compound-statement
17712
17713    objc-finally-clause
17714      @finally compound-statement
17715
17716    Returns NULL_TREE.  */
17717
17718 static tree
17719 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17720   location_t location;
17721   tree stmt;
17722
17723   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17724   location = cp_lexer_peek_token (parser->lexer)->location;
17725   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17726      node, lest it get absorbed into the surrounding block.  */
17727   stmt = push_stmt_list ();
17728   cp_parser_compound_statement (parser, NULL, false);
17729   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17730
17731   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17732     {
17733       cp_parameter_declarator *parmdecl;
17734       tree parm;
17735
17736       cp_lexer_consume_token (parser->lexer);
17737       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17738       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17739       parm = grokdeclarator (parmdecl->declarator,
17740                              &parmdecl->decl_specifiers,
17741                              PARM, /*initialized=*/0,
17742                              /*attrlist=*/NULL);
17743       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17744       objc_begin_catch_clause (parm);
17745       cp_parser_compound_statement (parser, NULL, false);
17746       objc_finish_catch_clause ();
17747     }
17748
17749   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17750     {
17751       cp_lexer_consume_token (parser->lexer);
17752       location = cp_lexer_peek_token (parser->lexer)->location;
17753       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17754          node, lest it get absorbed into the surrounding block.  */
17755       stmt = push_stmt_list ();
17756       cp_parser_compound_statement (parser, NULL, false);
17757       objc_build_finally_clause (location, pop_stmt_list (stmt));
17758     }
17759
17760   return objc_finish_try_stmt ();
17761 }
17762
17763 /* Parse an Objective-C synchronized statement.
17764
17765    objc-synchronized-stmt:
17766      @synchronized ( expression ) compound-statement
17767
17768    Returns NULL_TREE.  */
17769
17770 static tree
17771 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17772   location_t location;
17773   tree lock, stmt;
17774
17775   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17776
17777   location = cp_lexer_peek_token (parser->lexer)->location;
17778   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17779   lock = cp_parser_expression (parser, false);
17780   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17781
17782   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17783      node, lest it get absorbed into the surrounding block.  */
17784   stmt = push_stmt_list ();
17785   cp_parser_compound_statement (parser, NULL, false);
17786
17787   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17788 }
17789
17790 /* Parse an Objective-C throw statement.
17791
17792    objc-throw-stmt:
17793      @throw assignment-expression [opt] ;
17794
17795    Returns a constructed '@throw' statement.  */
17796
17797 static tree
17798 cp_parser_objc_throw_statement (cp_parser *parser) {
17799   tree expr = NULL_TREE;
17800
17801   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17802
17803   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17804     expr = cp_parser_assignment_expression (parser, false);
17805
17806   cp_parser_consume_semicolon_at_end_of_statement (parser);
17807
17808   return objc_build_throw_stmt (expr);
17809 }
17810
17811 /* Parse an Objective-C statement.  */
17812
17813 static tree
17814 cp_parser_objc_statement (cp_parser * parser) {
17815   /* Try to figure out what kind of declaration is present.  */
17816   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17817
17818   switch (kwd->keyword)
17819     {
17820     case RID_AT_TRY:
17821       return cp_parser_objc_try_catch_finally_statement (parser);
17822     case RID_AT_SYNCHRONIZED:
17823       return cp_parser_objc_synchronized_statement (parser);
17824     case RID_AT_THROW:
17825       return cp_parser_objc_throw_statement (parser);
17826     default:
17827       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17828       cp_parser_skip_to_end_of_block_or_statement (parser);
17829     }
17830
17831   return error_mark_node;
17832 }
17833 \f
17834 /* The parser.  */
17835
17836 static GTY (()) cp_parser *the_parser;
17837
17838 /* External interface.  */
17839
17840 /* Parse one entire translation unit.  */
17841
17842 void
17843 c_parse_file (void)
17844 {
17845   bool error_occurred;
17846   static bool already_called = false;
17847
17848   if (already_called)
17849     {
17850       sorry ("inter-module optimizations not implemented for C++");
17851       return;
17852     }
17853   already_called = true;
17854
17855   the_parser = cp_parser_new ();
17856   push_deferring_access_checks (flag_access_control
17857                                 ? dk_no_deferred : dk_no_check);
17858   error_occurred = cp_parser_translation_unit (the_parser);
17859   the_parser = NULL;
17860 }
17861
17862 /* This variable must be provided by every front end.  */
17863
17864 int yydebug;
17865
17866 #include "gt-cp-parser.h"