OSDN Git Service

PR c++/24782
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A C++ token.  */
48
49 typedef struct cp_token GTY (())
50 {
51   /* The kind of token.  */
52   ENUM_BITFIELD (cpp_ttype) type : 8;
53   /* If this token is a keyword, this value indicates which keyword.
54      Otherwise, this value is RID_MAX.  */
55   ENUM_BITFIELD (rid) keyword : 8;
56   /* Token flags.  */
57   unsigned char flags;
58   /* Identifier for the pragma.  */
59   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
60   /* True if this token is from a system header.  */
61   BOOL_BITFIELD in_system_header : 1;
62   /* True if this token is from a context where it is implicitly extern "C" */
63   BOOL_BITFIELD implicit_extern_c : 1;
64   /* True for a CPP_NAME token that is not a keyword (i.e., for which
65      KEYWORD is RID_MAX) iff this name was looked up and found to be
66      ambiguous.  An error has already been reported.  */
67   BOOL_BITFIELD ambiguous_p : 1;
68   /* The value associated with this token, if any.  */
69   tree value;
70   /* The location at which this token was found.  */
71   location_t location;
72 } cp_token;
73
74 /* We use a stack of token pointer for saving token sets.  */
75 typedef struct cp_token *cp_token_position;
76 DEF_VEC_P (cp_token_position);
77 DEF_VEC_ALLOC_P (cp_token_position,heap);
78
79 static const cp_token eof_token =
80 {
81   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
82 #if USE_MAPPED_LOCATION
83   0
84 #else
85   {0, 0}
86 #endif
87 };
88
89 /* The cp_lexer structure represents the C++ lexer.  It is responsible
90    for managing the token stream from the preprocessor and supplying
91    it to the parser.  Tokens are never added to the cp_lexer after
92    it is created.  */
93
94 typedef struct cp_lexer GTY (())
95 {
96   /* The memory allocated for the buffer.  NULL if this lexer does not
97      own the token buffer.  */
98   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
99   /* If the lexer owns the buffer, this is the number of tokens in the
100      buffer.  */
101   size_t buffer_length;
102
103   /* A pointer just past the last available token.  The tokens
104      in this lexer are [buffer, last_token).  */
105   cp_token_position GTY ((skip)) last_token;
106
107   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
108      no more available tokens.  */
109   cp_token_position GTY ((skip)) next_token;
110
111   /* A stack indicating positions at which cp_lexer_save_tokens was
112      called.  The top entry is the most recent position at which we
113      began saving tokens.  If the stack is non-empty, we are saving
114      tokens.  */
115   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
116
117   /* The next lexer in a linked list of lexers.  */
118   struct cp_lexer *next;
119
120   /* True if we should output debugging information.  */
121   bool debugging_p;
122
123   /* True if we're in the context of parsing a pragma, and should not
124      increment past the end-of-line marker.  */
125   bool in_pragma;
126 } cp_lexer;
127
128 /* cp_token_cache is a range of tokens.  There is no need to represent
129    allocate heap memory for it, since tokens are never removed from the
130    lexer's array.  There is also no need for the GC to walk through
131    a cp_token_cache, since everything in here is referenced through
132    a lexer.  */
133
134 typedef struct cp_token_cache GTY(())
135 {
136   /* The beginning of the token range.  */
137   cp_token * GTY((skip)) first;
138
139   /* Points immediately after the last token in the range.  */
140   cp_token * GTY ((skip)) last;
141 } cp_token_cache;
142
143 /* Prototypes.  */
144
145 static cp_lexer *cp_lexer_new_main
146   (void);
147 static cp_lexer *cp_lexer_new_from_tokens
148   (cp_token_cache *tokens);
149 static void cp_lexer_destroy
150   (cp_lexer *);
151 static int cp_lexer_saving_tokens
152   (const cp_lexer *);
153 static cp_token_position cp_lexer_token_position
154   (cp_lexer *, bool);
155 static cp_token *cp_lexer_token_at
156   (cp_lexer *, cp_token_position);
157 static void cp_lexer_get_preprocessor_token
158   (cp_lexer *, cp_token *);
159 static inline cp_token *cp_lexer_peek_token
160   (cp_lexer *);
161 static cp_token *cp_lexer_peek_nth_token
162   (cp_lexer *, size_t);
163 static inline bool cp_lexer_next_token_is
164   (cp_lexer *, enum cpp_ttype);
165 static bool cp_lexer_next_token_is_not
166   (cp_lexer *, enum cpp_ttype);
167 static bool cp_lexer_next_token_is_keyword
168   (cp_lexer *, enum rid);
169 static cp_token *cp_lexer_consume_token
170   (cp_lexer *);
171 static void cp_lexer_purge_token
172   (cp_lexer *);
173 static void cp_lexer_purge_tokens_after
174   (cp_lexer *, cp_token_position);
175 static void cp_lexer_save_tokens
176   (cp_lexer *);
177 static void cp_lexer_commit_tokens
178   (cp_lexer *);
179 static void cp_lexer_rollback_tokens
180   (cp_lexer *);
181 #ifdef ENABLE_CHECKING
182 static void cp_lexer_print_token
183   (FILE *, cp_token *);
184 static inline bool cp_lexer_debugging_p
185   (cp_lexer *);
186 static void cp_lexer_start_debugging
187   (cp_lexer *) ATTRIBUTE_UNUSED;
188 static void cp_lexer_stop_debugging
189   (cp_lexer *) ATTRIBUTE_UNUSED;
190 #else
191 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
192    about passing NULL to functions that require non-NULL arguments
193    (fputs, fprintf).  It will never be used, so all we need is a value
194    of the right type that's guaranteed not to be NULL.  */
195 #define cp_lexer_debug_stream stdout
196 #define cp_lexer_print_token(str, tok) (void) 0
197 #define cp_lexer_debugging_p(lexer) 0
198 #endif /* ENABLE_CHECKING */
199
200 static cp_token_cache *cp_token_cache_new
201   (cp_token *, cp_token *);
202
203 static void cp_parser_initial_pragma
204   (cp_token *);
205
206 /* Manifest constants.  */
207 #define CP_LEXER_BUFFER_SIZE 10000
208 #define CP_SAVED_TOKEN_STACK 5
209
210 /* A token type for keywords, as opposed to ordinary identifiers.  */
211 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
212
213 /* A token type for template-ids.  If a template-id is processed while
214    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
215    the value of the CPP_TEMPLATE_ID is whatever was returned by
216    cp_parser_template_id.  */
217 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
218
219 /* A token type for nested-name-specifiers.  If a
220    nested-name-specifier is processed while parsing tentatively, it is
221    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
222    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
223    cp_parser_nested_name_specifier_opt.  */
224 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
225
226 /* A token type for tokens that are not tokens at all; these are used
227    to represent slots in the array where there used to be a token
228    that has now been deleted.  */
229 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
230
231 /* The number of token types, including C++-specific ones.  */
232 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
233
234 /* Variables.  */
235
236 #ifdef ENABLE_CHECKING
237 /* The stream to which debugging output should be written.  */
238 static FILE *cp_lexer_debug_stream;
239 #endif /* ENABLE_CHECKING */
240
241 /* Create a new main C++ lexer, the lexer that gets tokens from the
242    preprocessor.  */
243
244 static cp_lexer *
245 cp_lexer_new_main (void)
246 {
247   cp_token first_token;
248   cp_lexer *lexer;
249   cp_token *pos;
250   size_t alloc;
251   size_t space;
252   cp_token *buffer;
253
254   /* It's possible that parsing the first pragma will load a PCH file,
255      which is a GC collection point.  So we have to do that before
256      allocating any memory.  */
257   cp_parser_initial_pragma (&first_token);
258
259   /* Tell c_lex_with_flags not to merge string constants.  */
260   c_lex_return_raw_strings = true;
261
262   c_common_no_more_pch ();
263
264   /* Allocate the memory.  */
265   lexer = GGC_CNEW (cp_lexer);
266
267 #ifdef ENABLE_CHECKING
268   /* Initially we are not debugging.  */
269   lexer->debugging_p = false;
270 #endif /* ENABLE_CHECKING */
271   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
272                                    CP_SAVED_TOKEN_STACK);
273
274   /* Create the buffer.  */
275   alloc = CP_LEXER_BUFFER_SIZE;
276   buffer = GGC_NEWVEC (cp_token, alloc);
277
278   /* Put the first token in the buffer.  */
279   space = alloc;
280   pos = buffer;
281   *pos = first_token;
282
283   /* Get the remaining tokens from the preprocessor.  */
284   while (pos->type != CPP_EOF)
285     {
286       pos++;
287       if (!--space)
288         {
289           space = alloc;
290           alloc *= 2;
291           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
292           pos = buffer + space;
293         }
294       cp_lexer_get_preprocessor_token (lexer, pos);
295     }
296   lexer->buffer = buffer;
297   lexer->buffer_length = alloc - space;
298   lexer->last_token = pos;
299   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
300
301   /* Subsequent preprocessor diagnostics should use compiler
302      diagnostic functions to get the compiler source location.  */
303   cpp_get_options (parse_in)->client_diagnostic = true;
304   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
305
306   gcc_assert (lexer->next_token->type != CPP_PURGED);
307   return lexer;
308 }
309
310 /* Create a new lexer whose token stream is primed with the tokens in
311    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
312
313 static cp_lexer *
314 cp_lexer_new_from_tokens (cp_token_cache *cache)
315 {
316   cp_token *first = cache->first;
317   cp_token *last = cache->last;
318   cp_lexer *lexer = GGC_CNEW (cp_lexer);
319
320   /* We do not own the buffer.  */
321   lexer->buffer = NULL;
322   lexer->buffer_length = 0;
323   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
324   lexer->last_token = last;
325
326   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
327                                    CP_SAVED_TOKEN_STACK);
328
329 #ifdef ENABLE_CHECKING
330   /* Initially we are not debugging.  */
331   lexer->debugging_p = false;
332 #endif
333
334   gcc_assert (lexer->next_token->type != CPP_PURGED);
335   return lexer;
336 }
337
338 /* Frees all resources associated with LEXER.  */
339
340 static void
341 cp_lexer_destroy (cp_lexer *lexer)
342 {
343   if (lexer->buffer)
344     ggc_free (lexer->buffer);
345   VEC_free (cp_token_position, heap, lexer->saved_tokens);
346   ggc_free (lexer);
347 }
348
349 /* Returns nonzero if debugging information should be output.  */
350
351 #ifdef ENABLE_CHECKING
352
353 static inline bool
354 cp_lexer_debugging_p (cp_lexer *lexer)
355 {
356   return lexer->debugging_p;
357 }
358
359 #endif /* ENABLE_CHECKING */
360
361 static inline cp_token_position
362 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
363 {
364   gcc_assert (!previous_p || lexer->next_token != &eof_token);
365
366   return lexer->next_token - previous_p;
367 }
368
369 static inline cp_token *
370 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
371 {
372   return pos;
373 }
374
375 /* nonzero if we are presently saving tokens.  */
376
377 static inline int
378 cp_lexer_saving_tokens (const cp_lexer* lexer)
379 {
380   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
381 }
382
383 /* Store the next token from the preprocessor in *TOKEN.  Return true
384    if we reach EOF.  */
385
386 static void
387 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
388                                  cp_token *token)
389 {
390   static int is_extern_c = 0;
391
392    /* Get a new token from the preprocessor.  */
393   token->type
394     = c_lex_with_flags (&token->value, &token->location, &token->flags);
395   token->keyword = RID_MAX;
396   token->pragma_kind = PRAGMA_NONE;
397   token->in_system_header = in_system_header;
398
399   /* On some systems, some header files are surrounded by an
400      implicit extern "C" block.  Set a flag in the token if it
401      comes from such a header.  */
402   is_extern_c += pending_lang_change;
403   pending_lang_change = 0;
404   token->implicit_extern_c = is_extern_c > 0;
405
406   /* Check to see if this token is a keyword.  */
407   if (token->type == CPP_NAME)
408     {
409       if (C_IS_RESERVED_WORD (token->value))
410         {
411           /* Mark this token as a keyword.  */
412           token->type = CPP_KEYWORD;
413           /* Record which keyword.  */
414           token->keyword = C_RID_CODE (token->value);
415           /* Update the value.  Some keywords are mapped to particular
416              entities, rather than simply having the value of the
417              corresponding IDENTIFIER_NODE.  For example, `__const' is
418              mapped to `const'.  */
419           token->value = ridpointers[token->keyword];
420         }
421       else
422         {
423           token->ambiguous_p = false;
424           token->keyword = RID_MAX;
425         }
426     }
427   /* Handle Objective-C++ keywords.  */
428   else if (token->type == CPP_AT_NAME)
429     {
430       token->type = CPP_KEYWORD;
431       switch (C_RID_CODE (token->value))
432         {
433         /* Map 'class' to '@class', 'private' to '@private', etc.  */
434         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
435         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
436         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
437         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
438         case RID_THROW: token->keyword = RID_AT_THROW; break;
439         case RID_TRY: token->keyword = RID_AT_TRY; break;
440         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
441         default: token->keyword = C_RID_CODE (token->value);
442         }
443     }
444   else if (token->type == CPP_PRAGMA)
445     {
446       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
447       token->pragma_kind = TREE_INT_CST_LOW (token->value);
448       token->value = NULL;
449     }
450 }
451
452 /* Update the globals input_location and in_system_header from TOKEN.  */
453 static inline void
454 cp_lexer_set_source_position_from_token (cp_token *token)
455 {
456   if (token->type != CPP_EOF)
457     {
458       input_location = token->location;
459       in_system_header = token->in_system_header;
460     }
461 }
462
463 /* Return a pointer to the next token in the token stream, but do not
464    consume it.  */
465
466 static inline cp_token *
467 cp_lexer_peek_token (cp_lexer *lexer)
468 {
469   if (cp_lexer_debugging_p (lexer))
470     {
471       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
472       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
473       putc ('\n', cp_lexer_debug_stream);
474     }
475   return lexer->next_token;
476 }
477
478 /* Return true if the next token has the indicated TYPE.  */
479
480 static inline bool
481 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
482 {
483   return cp_lexer_peek_token (lexer)->type == type;
484 }
485
486 /* Return true if the next token does not have the indicated TYPE.  */
487
488 static inline bool
489 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
490 {
491   return !cp_lexer_next_token_is (lexer, type);
492 }
493
494 /* Return true if the next token is the indicated KEYWORD.  */
495
496 static inline bool
497 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
498 {
499   cp_token *token;
500
501   /* Peek at the next token.  */
502   token = cp_lexer_peek_token (lexer);
503   /* Check to see if it is the indicated keyword.  */
504   return token->keyword == keyword;
505 }
506
507 /* Return a pointer to the Nth token in the token stream.  If N is 1,
508    then this is precisely equivalent to cp_lexer_peek_token (except
509    that it is not inline).  One would like to disallow that case, but
510    there is one case (cp_parser_nth_token_starts_template_id) where
511    the caller passes a variable for N and it might be 1.  */
512
513 static cp_token *
514 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
515 {
516   cp_token *token;
517
518   /* N is 1-based, not zero-based.  */
519   gcc_assert (n > 0);
520   
521   if (cp_lexer_debugging_p (lexer))
522     fprintf (cp_lexer_debug_stream,
523              "cp_lexer: peeking ahead %ld at token: ", (long)n);
524
525   --n;
526   token = lexer->next_token;
527   gcc_assert (!n || token != &eof_token);
528   while (n != 0)
529     {
530       ++token;
531       if (token == lexer->last_token)
532         {
533           token = (cp_token *)&eof_token;
534           break;
535         }
536
537       if (token->type != CPP_PURGED)
538         --n;
539     }
540
541   if (cp_lexer_debugging_p (lexer))
542     {
543       cp_lexer_print_token (cp_lexer_debug_stream, token);
544       putc ('\n', cp_lexer_debug_stream);
545     }
546
547   return token;
548 }
549
550 /* Return the next token, and advance the lexer's next_token pointer
551    to point to the next non-purged token.  */
552
553 static cp_token *
554 cp_lexer_consume_token (cp_lexer* lexer)
555 {
556   cp_token *token = lexer->next_token;
557
558   gcc_assert (token != &eof_token);
559   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
560
561   do
562     {
563       lexer->next_token++;
564       if (lexer->next_token == lexer->last_token)
565         {
566           lexer->next_token = (cp_token *)&eof_token;
567           break;
568         }
569
570     }
571   while (lexer->next_token->type == CPP_PURGED);
572
573   cp_lexer_set_source_position_from_token (token);
574
575   /* Provide debugging output.  */
576   if (cp_lexer_debugging_p (lexer))
577     {
578       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
579       cp_lexer_print_token (cp_lexer_debug_stream, token);
580       putc ('\n', cp_lexer_debug_stream);
581     }
582
583   return token;
584 }
585
586 /* Permanently remove the next token from the token stream, and
587    advance the next_token pointer to refer to the next non-purged
588    token.  */
589
590 static void
591 cp_lexer_purge_token (cp_lexer *lexer)
592 {
593   cp_token *tok = lexer->next_token;
594
595   gcc_assert (tok != &eof_token);
596   tok->type = CPP_PURGED;
597   tok->location = UNKNOWN_LOCATION;
598   tok->value = NULL_TREE;
599   tok->keyword = RID_MAX;
600
601   do
602     {
603       tok++;
604       if (tok == lexer->last_token)
605         {
606           tok = (cp_token *)&eof_token;
607           break;
608         }
609     }
610   while (tok->type == CPP_PURGED);
611   lexer->next_token = tok;
612 }
613
614 /* Permanently remove all tokens after TOK, up to, but not
615    including, the token that will be returned next by
616    cp_lexer_peek_token.  */
617
618 static void
619 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
620 {
621   cp_token *peek = lexer->next_token;
622
623   if (peek == &eof_token)
624     peek = lexer->last_token;
625
626   gcc_assert (tok < peek);
627
628   for ( tok += 1; tok != peek; tok += 1)
629     {
630       tok->type = CPP_PURGED;
631       tok->location = UNKNOWN_LOCATION;
632       tok->value = NULL_TREE;
633       tok->keyword = RID_MAX;
634     }
635 }
636
637 /* Begin saving tokens.  All tokens consumed after this point will be
638    preserved.  */
639
640 static void
641 cp_lexer_save_tokens (cp_lexer* lexer)
642 {
643   /* Provide debugging output.  */
644   if (cp_lexer_debugging_p (lexer))
645     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
646
647   VEC_safe_push (cp_token_position, heap,
648                  lexer->saved_tokens, lexer->next_token);
649 }
650
651 /* Commit to the portion of the token stream most recently saved.  */
652
653 static void
654 cp_lexer_commit_tokens (cp_lexer* lexer)
655 {
656   /* Provide debugging output.  */
657   if (cp_lexer_debugging_p (lexer))
658     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
659
660   VEC_pop (cp_token_position, lexer->saved_tokens);
661 }
662
663 /* Return all tokens saved since the last call to cp_lexer_save_tokens
664    to the token stream.  Stop saving tokens.  */
665
666 static void
667 cp_lexer_rollback_tokens (cp_lexer* lexer)
668 {
669   /* Provide debugging output.  */
670   if (cp_lexer_debugging_p (lexer))
671     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
672
673   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
674 }
675
676 /* Print a representation of the TOKEN on the STREAM.  */
677
678 #ifdef ENABLE_CHECKING
679
680 static void
681 cp_lexer_print_token (FILE * stream, cp_token *token)
682 {
683   /* We don't use cpp_type2name here because the parser defines
684      a few tokens of its own.  */
685   static const char *const token_names[] = {
686     /* cpplib-defined token types */
687 #define OP(e, s) #e,
688 #define TK(e, s) #e,
689     TTYPE_TABLE
690 #undef OP
691 #undef TK
692     /* C++ parser token types - see "Manifest constants", above.  */
693     "KEYWORD",
694     "TEMPLATE_ID",
695     "NESTED_NAME_SPECIFIER",
696     "PURGED"
697   };
698
699   /* If we have a name for the token, print it out.  Otherwise, we
700      simply give the numeric code.  */
701   gcc_assert (token->type < ARRAY_SIZE(token_names));
702   fputs (token_names[token->type], stream);
703
704   /* For some tokens, print the associated data.  */
705   switch (token->type)
706     {
707     case CPP_KEYWORD:
708       /* Some keywords have a value that is not an IDENTIFIER_NODE.
709          For example, `struct' is mapped to an INTEGER_CST.  */
710       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
711         break;
712       /* else fall through */
713     case CPP_NAME:
714       fputs (IDENTIFIER_POINTER (token->value), stream);
715       break;
716
717     case CPP_STRING:
718     case CPP_WSTRING:
719       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
720       break;
721
722     default:
723       break;
724     }
725 }
726
727 /* Start emitting debugging information.  */
728
729 static void
730 cp_lexer_start_debugging (cp_lexer* lexer)
731 {
732   lexer->debugging_p = true;
733 }
734
735 /* Stop emitting debugging information.  */
736
737 static void
738 cp_lexer_stop_debugging (cp_lexer* lexer)
739 {
740   lexer->debugging_p = false;
741 }
742
743 #endif /* ENABLE_CHECKING */
744
745 /* Create a new cp_token_cache, representing a range of tokens.  */
746
747 static cp_token_cache *
748 cp_token_cache_new (cp_token *first, cp_token *last)
749 {
750   cp_token_cache *cache = GGC_NEW (cp_token_cache);
751   cache->first = first;
752   cache->last = last;
753   return cache;
754 }
755
756 \f
757 /* Decl-specifiers.  */
758
759 static void clear_decl_specs
760   (cp_decl_specifier_seq *);
761
762 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
763
764 static void
765 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
766 {
767   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
768 }
769
770 /* Declarators.  */
771
772 /* Nothing other than the parser should be creating declarators;
773    declarators are a semi-syntactic representation of C++ entities.
774    Other parts of the front end that need to create entities (like
775    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
776
777 static cp_declarator *make_call_declarator
778   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
779 static cp_declarator *make_array_declarator
780   (cp_declarator *, tree);
781 static cp_declarator *make_pointer_declarator
782   (cp_cv_quals, cp_declarator *);
783 static cp_declarator *make_reference_declarator
784   (cp_cv_quals, cp_declarator *);
785 static cp_parameter_declarator *make_parameter_declarator
786   (cp_decl_specifier_seq *, cp_declarator *, tree);
787 static cp_declarator *make_ptrmem_declarator
788   (cp_cv_quals, tree, cp_declarator *);
789
790 cp_declarator *cp_error_declarator;
791
792 /* The obstack on which declarators and related data structures are
793    allocated.  */
794 static struct obstack declarator_obstack;
795
796 /* Alloc BYTES from the declarator memory pool.  */
797
798 static inline void *
799 alloc_declarator (size_t bytes)
800 {
801   return obstack_alloc (&declarator_obstack, bytes);
802 }
803
804 /* Allocate a declarator of the indicated KIND.  Clear fields that are
805    common to all declarators.  */
806
807 static cp_declarator *
808 make_declarator (cp_declarator_kind kind)
809 {
810   cp_declarator *declarator;
811
812   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
813   declarator->kind = kind;
814   declarator->attributes = NULL_TREE;
815   declarator->declarator = NULL;
816
817   return declarator;
818 }
819
820 /* Make a declarator for a generalized identifier.  If
821    QUALIFYING_SCOPE is non-NULL, the identifier is
822    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
823    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
824    is, if any.   */
825
826 static cp_declarator *
827 make_id_declarator (tree qualifying_scope, tree unqualified_name,
828                     special_function_kind sfk)
829 {
830   cp_declarator *declarator;
831
832   /* It is valid to write:
833
834        class C { void f(); };
835        typedef C D;
836        void D::f();
837
838      The standard is not clear about whether `typedef const C D' is
839      legal; as of 2002-09-15 the committee is considering that
840      question.  EDG 3.0 allows that syntax.  Therefore, we do as
841      well.  */
842   if (qualifying_scope && TYPE_P (qualifying_scope))
843     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
844
845   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
846               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
847               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
848
849   declarator = make_declarator (cdk_id);
850   declarator->u.id.qualifying_scope = qualifying_scope;
851   declarator->u.id.unqualified_name = unqualified_name;
852   declarator->u.id.sfk = sfk;
853
854   return declarator;
855 }
856
857 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
858    of modifiers such as const or volatile to apply to the pointer
859    type, represented as identifiers.  */
860
861 cp_declarator *
862 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
863 {
864   cp_declarator *declarator;
865
866   declarator = make_declarator (cdk_pointer);
867   declarator->declarator = target;
868   declarator->u.pointer.qualifiers = cv_qualifiers;
869   declarator->u.pointer.class_type = NULL_TREE;
870
871   return declarator;
872 }
873
874 /* Like make_pointer_declarator -- but for references.  */
875
876 cp_declarator *
877 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
878 {
879   cp_declarator *declarator;
880
881   declarator = make_declarator (cdk_reference);
882   declarator->declarator = target;
883   declarator->u.pointer.qualifiers = cv_qualifiers;
884   declarator->u.pointer.class_type = NULL_TREE;
885
886   return declarator;
887 }
888
889 /* Like make_pointer_declarator -- but for a pointer to a non-static
890    member of CLASS_TYPE.  */
891
892 cp_declarator *
893 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
894                         cp_declarator *pointee)
895 {
896   cp_declarator *declarator;
897
898   declarator = make_declarator (cdk_ptrmem);
899   declarator->declarator = pointee;
900   declarator->u.pointer.qualifiers = cv_qualifiers;
901   declarator->u.pointer.class_type = class_type;
902
903   return declarator;
904 }
905
906 /* Make a declarator for the function given by TARGET, with the
907    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
908    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
909    indicates what exceptions can be thrown.  */
910
911 cp_declarator *
912 make_call_declarator (cp_declarator *target,
913                       cp_parameter_declarator *parms,
914                       cp_cv_quals cv_qualifiers,
915                       tree exception_specification)
916 {
917   cp_declarator *declarator;
918
919   declarator = make_declarator (cdk_function);
920   declarator->declarator = target;
921   declarator->u.function.parameters = parms;
922   declarator->u.function.qualifiers = cv_qualifiers;
923   declarator->u.function.exception_specification = exception_specification;
924
925   return declarator;
926 }
927
928 /* Make a declarator for an array of BOUNDS elements, each of which is
929    defined by ELEMENT.  */
930
931 cp_declarator *
932 make_array_declarator (cp_declarator *element, tree bounds)
933 {
934   cp_declarator *declarator;
935
936   declarator = make_declarator (cdk_array);
937   declarator->declarator = element;
938   declarator->u.array.bounds = bounds;
939
940   return declarator;
941 }
942
943 cp_parameter_declarator *no_parameters;
944
945 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
946    DECLARATOR and DEFAULT_ARGUMENT.  */
947
948 cp_parameter_declarator *
949 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
950                            cp_declarator *declarator,
951                            tree default_argument)
952 {
953   cp_parameter_declarator *parameter;
954
955   parameter = ((cp_parameter_declarator *)
956                alloc_declarator (sizeof (cp_parameter_declarator)));
957   parameter->next = NULL;
958   if (decl_specifiers)
959     parameter->decl_specifiers = *decl_specifiers;
960   else
961     clear_decl_specs (&parameter->decl_specifiers);
962   parameter->declarator = declarator;
963   parameter->default_argument = default_argument;
964   parameter->ellipsis_p = false;
965
966   return parameter;
967 }
968
969 /* The parser.  */
970
971 /* Overview
972    --------
973
974    A cp_parser parses the token stream as specified by the C++
975    grammar.  Its job is purely parsing, not semantic analysis.  For
976    example, the parser breaks the token stream into declarators,
977    expressions, statements, and other similar syntactic constructs.
978    It does not check that the types of the expressions on either side
979    of an assignment-statement are compatible, or that a function is
980    not declared with a parameter of type `void'.
981
982    The parser invokes routines elsewhere in the compiler to perform
983    semantic analysis and to build up the abstract syntax tree for the
984    code processed.
985
986    The parser (and the template instantiation code, which is, in a
987    way, a close relative of parsing) are the only parts of the
988    compiler that should be calling push_scope and pop_scope, or
989    related functions.  The parser (and template instantiation code)
990    keeps track of what scope is presently active; everything else
991    should simply honor that.  (The code that generates static
992    initializers may also need to set the scope, in order to check
993    access control correctly when emitting the initializers.)
994
995    Methodology
996    -----------
997
998    The parser is of the standard recursive-descent variety.  Upcoming
999    tokens in the token stream are examined in order to determine which
1000    production to use when parsing a non-terminal.  Some C++ constructs
1001    require arbitrary look ahead to disambiguate.  For example, it is
1002    impossible, in the general case, to tell whether a statement is an
1003    expression or declaration without scanning the entire statement.
1004    Therefore, the parser is capable of "parsing tentatively."  When the
1005    parser is not sure what construct comes next, it enters this mode.
1006    Then, while we attempt to parse the construct, the parser queues up
1007    error messages, rather than issuing them immediately, and saves the
1008    tokens it consumes.  If the construct is parsed successfully, the
1009    parser "commits", i.e., it issues any queued error messages and
1010    the tokens that were being preserved are permanently discarded.
1011    If, however, the construct is not parsed successfully, the parser
1012    rolls back its state completely so that it can resume parsing using
1013    a different alternative.
1014
1015    Future Improvements
1016    -------------------
1017
1018    The performance of the parser could probably be improved substantially.
1019    We could often eliminate the need to parse tentatively by looking ahead
1020    a little bit.  In some places, this approach might not entirely eliminate
1021    the need to parse tentatively, but it might still speed up the average
1022    case.  */
1023
1024 /* Flags that are passed to some parsing functions.  These values can
1025    be bitwise-ored together.  */
1026
1027 typedef enum cp_parser_flags
1028 {
1029   /* No flags.  */
1030   CP_PARSER_FLAGS_NONE = 0x0,
1031   /* The construct is optional.  If it is not present, then no error
1032      should be issued.  */
1033   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1034   /* When parsing a type-specifier, do not allow user-defined types.  */
1035   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1036 } cp_parser_flags;
1037
1038 /* The different kinds of declarators we want to parse.  */
1039
1040 typedef enum cp_parser_declarator_kind
1041 {
1042   /* We want an abstract declarator.  */
1043   CP_PARSER_DECLARATOR_ABSTRACT,
1044   /* We want a named declarator.  */
1045   CP_PARSER_DECLARATOR_NAMED,
1046   /* We don't mind, but the name must be an unqualified-id.  */
1047   CP_PARSER_DECLARATOR_EITHER
1048 } cp_parser_declarator_kind;
1049
1050 /* The precedence values used to parse binary expressions.  The minimum value
1051    of PREC must be 1, because zero is reserved to quickly discriminate
1052    binary operators from other tokens.  */
1053
1054 enum cp_parser_prec
1055 {
1056   PREC_NOT_OPERATOR,
1057   PREC_LOGICAL_OR_EXPRESSION,
1058   PREC_LOGICAL_AND_EXPRESSION,
1059   PREC_INCLUSIVE_OR_EXPRESSION,
1060   PREC_EXCLUSIVE_OR_EXPRESSION,
1061   PREC_AND_EXPRESSION,
1062   PREC_EQUALITY_EXPRESSION,
1063   PREC_RELATIONAL_EXPRESSION,
1064   PREC_SHIFT_EXPRESSION,
1065   PREC_ADDITIVE_EXPRESSION,
1066   PREC_MULTIPLICATIVE_EXPRESSION,
1067   PREC_PM_EXPRESSION,
1068   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1069 };
1070
1071 /* A mapping from a token type to a corresponding tree node type, with a
1072    precedence value.  */
1073
1074 typedef struct cp_parser_binary_operations_map_node
1075 {
1076   /* The token type.  */
1077   enum cpp_ttype token_type;
1078   /* The corresponding tree code.  */
1079   enum tree_code tree_type;
1080   /* The precedence of this operator.  */
1081   enum cp_parser_prec prec;
1082 } cp_parser_binary_operations_map_node;
1083
1084 /* The status of a tentative parse.  */
1085
1086 typedef enum cp_parser_status_kind
1087 {
1088   /* No errors have occurred.  */
1089   CP_PARSER_STATUS_KIND_NO_ERROR,
1090   /* An error has occurred.  */
1091   CP_PARSER_STATUS_KIND_ERROR,
1092   /* We are committed to this tentative parse, whether or not an error
1093      has occurred.  */
1094   CP_PARSER_STATUS_KIND_COMMITTED
1095 } cp_parser_status_kind;
1096
1097 typedef struct cp_parser_expression_stack_entry
1098 {
1099   tree lhs;
1100   enum tree_code tree_type;
1101   int prec;
1102 } cp_parser_expression_stack_entry;
1103
1104 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1105    entries because precedence levels on the stack are monotonically
1106    increasing.  */
1107 typedef struct cp_parser_expression_stack_entry
1108   cp_parser_expression_stack[NUM_PREC_VALUES];
1109
1110 /* Context that is saved and restored when parsing tentatively.  */
1111 typedef struct cp_parser_context GTY (())
1112 {
1113   /* If this is a tentative parsing context, the status of the
1114      tentative parse.  */
1115   enum cp_parser_status_kind status;
1116   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1117      that are looked up in this context must be looked up both in the
1118      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1119      the context of the containing expression.  */
1120   tree object_type;
1121
1122   /* The next parsing context in the stack.  */
1123   struct cp_parser_context *next;
1124 } cp_parser_context;
1125
1126 /* Prototypes.  */
1127
1128 /* Constructors and destructors.  */
1129
1130 static cp_parser_context *cp_parser_context_new
1131   (cp_parser_context *);
1132
1133 /* Class variables.  */
1134
1135 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1136
1137 /* The operator-precedence table used by cp_parser_binary_expression.
1138    Transformed into an associative array (binops_by_token) by
1139    cp_parser_new.  */
1140
1141 static const cp_parser_binary_operations_map_node binops[] = {
1142   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1143   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1144
1145   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1146   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1147   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1148
1149   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1150   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1151
1152   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1153   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1154
1155   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1156   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1157   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1158   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1159   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1160   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1161
1162   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1163   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1164
1165   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1166
1167   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1168
1169   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1170
1171   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1172
1173   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1174 };
1175
1176 /* The same as binops, but initialized by cp_parser_new so that
1177    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1178    for speed.  */
1179 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1180
1181 /* Constructors and destructors.  */
1182
1183 /* Construct a new context.  The context below this one on the stack
1184    is given by NEXT.  */
1185
1186 static cp_parser_context *
1187 cp_parser_context_new (cp_parser_context* next)
1188 {
1189   cp_parser_context *context;
1190
1191   /* Allocate the storage.  */
1192   if (cp_parser_context_free_list != NULL)
1193     {
1194       /* Pull the first entry from the free list.  */
1195       context = cp_parser_context_free_list;
1196       cp_parser_context_free_list = context->next;
1197       memset (context, 0, sizeof (*context));
1198     }
1199   else
1200     context = GGC_CNEW (cp_parser_context);
1201
1202   /* No errors have occurred yet in this context.  */
1203   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1204   /* If this is not the bottomost context, copy information that we
1205      need from the previous context.  */
1206   if (next)
1207     {
1208       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1209          expression, then we are parsing one in this context, too.  */
1210       context->object_type = next->object_type;
1211       /* Thread the stack.  */
1212       context->next = next;
1213     }
1214
1215   return context;
1216 }
1217
1218 /* The cp_parser structure represents the C++ parser.  */
1219
1220 typedef struct cp_parser GTY(())
1221 {
1222   /* The lexer from which we are obtaining tokens.  */
1223   cp_lexer *lexer;
1224
1225   /* The scope in which names should be looked up.  If NULL_TREE, then
1226      we look up names in the scope that is currently open in the
1227      source program.  If non-NULL, this is either a TYPE or
1228      NAMESPACE_DECL for the scope in which we should look.  It can
1229      also be ERROR_MARK, when we've parsed a bogus scope.
1230
1231      This value is not cleared automatically after a name is looked
1232      up, so we must be careful to clear it before starting a new look
1233      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1234      will look up `Z' in the scope of `X', rather than the current
1235      scope.)  Unfortunately, it is difficult to tell when name lookup
1236      is complete, because we sometimes peek at a token, look it up,
1237      and then decide not to consume it.   */
1238   tree scope;
1239
1240   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1241      last lookup took place.  OBJECT_SCOPE is used if an expression
1242      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1243      respectively.  QUALIFYING_SCOPE is used for an expression of the
1244      form "X::Y"; it refers to X.  */
1245   tree object_scope;
1246   tree qualifying_scope;
1247
1248   /* A stack of parsing contexts.  All but the bottom entry on the
1249      stack will be tentative contexts.
1250
1251      We parse tentatively in order to determine which construct is in
1252      use in some situations.  For example, in order to determine
1253      whether a statement is an expression-statement or a
1254      declaration-statement we parse it tentatively as a
1255      declaration-statement.  If that fails, we then reparse the same
1256      token stream as an expression-statement.  */
1257   cp_parser_context *context;
1258
1259   /* True if we are parsing GNU C++.  If this flag is not set, then
1260      GNU extensions are not recognized.  */
1261   bool allow_gnu_extensions_p;
1262
1263   /* TRUE if the `>' token should be interpreted as the greater-than
1264      operator.  FALSE if it is the end of a template-id or
1265      template-parameter-list.  */
1266   bool greater_than_is_operator_p;
1267
1268   /* TRUE if default arguments are allowed within a parameter list
1269      that starts at this point. FALSE if only a gnu extension makes
1270      them permissible.  */
1271   bool default_arg_ok_p;
1272
1273   /* TRUE if we are parsing an integral constant-expression.  See
1274      [expr.const] for a precise definition.  */
1275   bool integral_constant_expression_p;
1276
1277   /* TRUE if we are parsing an integral constant-expression -- but a
1278      non-constant expression should be permitted as well.  This flag
1279      is used when parsing an array bound so that GNU variable-length
1280      arrays are tolerated.  */
1281   bool allow_non_integral_constant_expression_p;
1282
1283   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1284      been seen that makes the expression non-constant.  */
1285   bool non_integral_constant_expression_p;
1286
1287   /* TRUE if local variable names and `this' are forbidden in the
1288      current context.  */
1289   bool local_variables_forbidden_p;
1290
1291   /* TRUE if the declaration we are parsing is part of a
1292      linkage-specification of the form `extern string-literal
1293      declaration'.  */
1294   bool in_unbraced_linkage_specification_p;
1295
1296   /* TRUE if we are presently parsing a declarator, after the
1297      direct-declarator.  */
1298   bool in_declarator_p;
1299
1300   /* TRUE if we are presently parsing a template-argument-list.  */
1301   bool in_template_argument_list_p;
1302
1303   /* TRUE if we are presently parsing the body of an
1304      iteration-statement.  */
1305   bool in_iteration_statement_p;
1306
1307   /* TRUE if we are presently parsing the body of a switch
1308      statement.  */
1309   bool in_switch_statement_p;
1310
1311   /* TRUE if we are parsing a type-id in an expression context.  In
1312      such a situation, both "type (expr)" and "type (type)" are valid
1313      alternatives.  */
1314   bool in_type_id_in_expr_p;
1315
1316   /* TRUE if we are currently in a header file where declarations are
1317      implicitly extern "C".  */
1318   bool implicit_extern_c;
1319
1320   /* TRUE if strings in expressions should be translated to the execution
1321      character set.  */
1322   bool translate_strings_p;
1323
1324   /* If non-NULL, then we are parsing a construct where new type
1325      definitions are not permitted.  The string stored here will be
1326      issued as an error message if a type is defined.  */
1327   const char *type_definition_forbidden_message;
1328
1329   /* A list of lists. The outer list is a stack, used for member
1330      functions of local classes. At each level there are two sub-list,
1331      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1332      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1333      TREE_VALUE's. The functions are chained in reverse declaration
1334      order.
1335
1336      The TREE_PURPOSE sublist contains those functions with default
1337      arguments that need post processing, and the TREE_VALUE sublist
1338      contains those functions with definitions that need post
1339      processing.
1340
1341      These lists can only be processed once the outermost class being
1342      defined is complete.  */
1343   tree unparsed_functions_queues;
1344
1345   /* The number of classes whose definitions are currently in
1346      progress.  */
1347   unsigned num_classes_being_defined;
1348
1349   /* The number of template parameter lists that apply directly to the
1350      current declaration.  */
1351   unsigned num_template_parameter_lists;
1352 } cp_parser;
1353
1354 /* The type of a function that parses some kind of expression.  */
1355 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1356
1357 /* Prototypes.  */
1358
1359 /* Constructors and destructors.  */
1360
1361 static cp_parser *cp_parser_new
1362   (void);
1363
1364 /* Routines to parse various constructs.
1365
1366    Those that return `tree' will return the error_mark_node (rather
1367    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1368    Sometimes, they will return an ordinary node if error-recovery was
1369    attempted, even though a parse error occurred.  So, to check
1370    whether or not a parse error occurred, you should always use
1371    cp_parser_error_occurred.  If the construct is optional (indicated
1372    either by an `_opt' in the name of the function that does the
1373    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1374    the construct is not present.  */
1375
1376 /* Lexical conventions [gram.lex]  */
1377
1378 static tree cp_parser_identifier
1379   (cp_parser *);
1380 static tree cp_parser_string_literal
1381   (cp_parser *, bool, bool);
1382
1383 /* Basic concepts [gram.basic]  */
1384
1385 static bool cp_parser_translation_unit
1386   (cp_parser *);
1387
1388 /* Expressions [gram.expr]  */
1389
1390 static tree cp_parser_primary_expression
1391   (cp_parser *, bool, bool, bool, cp_id_kind *);
1392 static tree cp_parser_id_expression
1393   (cp_parser *, bool, bool, bool *, bool);
1394 static tree cp_parser_unqualified_id
1395   (cp_parser *, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier_opt
1397   (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_nested_name_specifier
1399   (cp_parser *, bool, bool, bool, bool);
1400 static tree cp_parser_class_or_namespace_name
1401   (cp_parser *, bool, bool, bool, bool, bool);
1402 static tree cp_parser_postfix_expression
1403   (cp_parser *, bool, bool);
1404 static tree cp_parser_postfix_open_square_expression
1405   (cp_parser *, tree, bool);
1406 static tree cp_parser_postfix_dot_deref_expression
1407   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1408 static tree cp_parser_parenthesized_expression_list
1409   (cp_parser *, bool, bool, bool *);
1410 static void cp_parser_pseudo_destructor_name
1411   (cp_parser *, tree *, tree *);
1412 static tree cp_parser_unary_expression
1413   (cp_parser *, bool, bool);
1414 static enum tree_code cp_parser_unary_operator
1415   (cp_token *);
1416 static tree cp_parser_new_expression
1417   (cp_parser *);
1418 static tree cp_parser_new_placement
1419   (cp_parser *);
1420 static tree cp_parser_new_type_id
1421   (cp_parser *, tree *);
1422 static cp_declarator *cp_parser_new_declarator_opt
1423   (cp_parser *);
1424 static cp_declarator *cp_parser_direct_new_declarator
1425   (cp_parser *);
1426 static tree cp_parser_new_initializer
1427   (cp_parser *);
1428 static tree cp_parser_delete_expression
1429   (cp_parser *);
1430 static tree cp_parser_cast_expression
1431   (cp_parser *, bool, bool);
1432 static tree cp_parser_binary_expression
1433   (cp_parser *, bool);
1434 static tree cp_parser_question_colon_clause
1435   (cp_parser *, tree);
1436 static tree cp_parser_assignment_expression
1437   (cp_parser *, bool);
1438 static enum tree_code cp_parser_assignment_operator_opt
1439   (cp_parser *);
1440 static tree cp_parser_expression
1441   (cp_parser *, bool);
1442 static tree cp_parser_constant_expression
1443   (cp_parser *, bool, bool *);
1444 static tree cp_parser_builtin_offsetof
1445   (cp_parser *);
1446
1447 /* Statements [gram.stmt.stmt]  */
1448
1449 static void cp_parser_statement
1450   (cp_parser *, tree, bool);
1451 static tree cp_parser_labeled_statement
1452   (cp_parser *, tree, bool);
1453 static tree cp_parser_expression_statement
1454   (cp_parser *, tree);
1455 static tree cp_parser_compound_statement
1456   (cp_parser *, tree, bool);
1457 static void cp_parser_statement_seq_opt
1458   (cp_parser *, tree);
1459 static tree cp_parser_selection_statement
1460   (cp_parser *);
1461 static tree cp_parser_condition
1462   (cp_parser *);
1463 static tree cp_parser_iteration_statement
1464   (cp_parser *);
1465 static void cp_parser_for_init_statement
1466   (cp_parser *);
1467 static tree cp_parser_jump_statement
1468   (cp_parser *);
1469 static void cp_parser_declaration_statement
1470   (cp_parser *);
1471
1472 static tree cp_parser_implicitly_scoped_statement
1473   (cp_parser *);
1474 static void cp_parser_already_scoped_statement
1475   (cp_parser *);
1476
1477 /* Declarations [gram.dcl.dcl] */
1478
1479 static void cp_parser_declaration_seq_opt
1480   (cp_parser *);
1481 static void cp_parser_declaration
1482   (cp_parser *);
1483 static void cp_parser_block_declaration
1484   (cp_parser *, bool);
1485 static void cp_parser_simple_declaration
1486   (cp_parser *, bool);
1487 static void cp_parser_decl_specifier_seq
1488   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1489 static tree cp_parser_storage_class_specifier_opt
1490   (cp_parser *);
1491 static tree cp_parser_function_specifier_opt
1492   (cp_parser *, cp_decl_specifier_seq *);
1493 static tree cp_parser_type_specifier
1494   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1495    int *, bool *);
1496 static tree cp_parser_simple_type_specifier
1497   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1498 static tree cp_parser_type_name
1499   (cp_parser *);
1500 static tree cp_parser_elaborated_type_specifier
1501   (cp_parser *, bool, bool);
1502 static tree cp_parser_enum_specifier
1503   (cp_parser *);
1504 static void cp_parser_enumerator_list
1505   (cp_parser *, tree);
1506 static void cp_parser_enumerator_definition
1507   (cp_parser *, tree);
1508 static tree cp_parser_namespace_name
1509   (cp_parser *);
1510 static void cp_parser_namespace_definition
1511   (cp_parser *);
1512 static void cp_parser_namespace_body
1513   (cp_parser *);
1514 static tree cp_parser_qualified_namespace_specifier
1515   (cp_parser *);
1516 static void cp_parser_namespace_alias_definition
1517   (cp_parser *);
1518 static void cp_parser_using_declaration
1519   (cp_parser *);
1520 static void cp_parser_using_directive
1521   (cp_parser *);
1522 static void cp_parser_asm_definition
1523   (cp_parser *);
1524 static void cp_parser_linkage_specification
1525   (cp_parser *);
1526
1527 /* Declarators [gram.dcl.decl] */
1528
1529 static tree cp_parser_init_declarator
1530   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1531 static cp_declarator *cp_parser_declarator
1532   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1533 static cp_declarator *cp_parser_direct_declarator
1534   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1535 static enum tree_code cp_parser_ptr_operator
1536   (cp_parser *, tree *, cp_cv_quals *);
1537 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1538   (cp_parser *);
1539 static tree cp_parser_declarator_id
1540   (cp_parser *);
1541 static tree cp_parser_type_id
1542   (cp_parser *);
1543 static void cp_parser_type_specifier_seq
1544   (cp_parser *, bool, cp_decl_specifier_seq *);
1545 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1546   (cp_parser *);
1547 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1548   (cp_parser *, bool *);
1549 static cp_parameter_declarator *cp_parser_parameter_declaration
1550   (cp_parser *, bool, bool *);
1551 static void cp_parser_function_body
1552   (cp_parser *);
1553 static tree cp_parser_initializer
1554   (cp_parser *, bool *, bool *);
1555 static tree cp_parser_initializer_clause
1556   (cp_parser *, bool *);
1557 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1558   (cp_parser *, bool *);
1559
1560 static bool cp_parser_ctor_initializer_opt_and_function_body
1561   (cp_parser *);
1562
1563 /* Classes [gram.class] */
1564
1565 static tree cp_parser_class_name
1566   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1567 static tree cp_parser_class_specifier
1568   (cp_parser *);
1569 static tree cp_parser_class_head
1570   (cp_parser *, bool *, tree *);
1571 static enum tag_types cp_parser_class_key
1572   (cp_parser *);
1573 static void cp_parser_member_specification_opt
1574   (cp_parser *);
1575 static void cp_parser_member_declaration
1576   (cp_parser *);
1577 static tree cp_parser_pure_specifier
1578   (cp_parser *);
1579 static tree cp_parser_constant_initializer
1580   (cp_parser *);
1581
1582 /* Derived classes [gram.class.derived] */
1583
1584 static tree cp_parser_base_clause
1585   (cp_parser *);
1586 static tree cp_parser_base_specifier
1587   (cp_parser *);
1588
1589 /* Special member functions [gram.special] */
1590
1591 static tree cp_parser_conversion_function_id
1592   (cp_parser *);
1593 static tree cp_parser_conversion_type_id
1594   (cp_parser *);
1595 static cp_declarator *cp_parser_conversion_declarator_opt
1596   (cp_parser *);
1597 static bool cp_parser_ctor_initializer_opt
1598   (cp_parser *);
1599 static void cp_parser_mem_initializer_list
1600   (cp_parser *);
1601 static tree cp_parser_mem_initializer
1602   (cp_parser *);
1603 static tree cp_parser_mem_initializer_id
1604   (cp_parser *);
1605
1606 /* Overloading [gram.over] */
1607
1608 static tree cp_parser_operator_function_id
1609   (cp_parser *);
1610 static tree cp_parser_operator
1611   (cp_parser *);
1612
1613 /* Templates [gram.temp] */
1614
1615 static void cp_parser_template_declaration
1616   (cp_parser *, bool);
1617 static tree cp_parser_template_parameter_list
1618   (cp_parser *);
1619 static tree cp_parser_template_parameter
1620   (cp_parser *, bool *);
1621 static tree cp_parser_type_parameter
1622   (cp_parser *);
1623 static tree cp_parser_template_id
1624   (cp_parser *, bool, bool, bool);
1625 static tree cp_parser_template_name
1626   (cp_parser *, bool, bool, bool, bool *);
1627 static tree cp_parser_template_argument_list
1628   (cp_parser *);
1629 static tree cp_parser_template_argument
1630   (cp_parser *);
1631 static void cp_parser_explicit_instantiation
1632   (cp_parser *);
1633 static void cp_parser_explicit_specialization
1634   (cp_parser *);
1635
1636 /* Exception handling [gram.exception] */
1637
1638 static tree cp_parser_try_block
1639   (cp_parser *);
1640 static bool cp_parser_function_try_block
1641   (cp_parser *);
1642 static void cp_parser_handler_seq
1643   (cp_parser *);
1644 static void cp_parser_handler
1645   (cp_parser *);
1646 static tree cp_parser_exception_declaration
1647   (cp_parser *);
1648 static tree cp_parser_throw_expression
1649   (cp_parser *);
1650 static tree cp_parser_exception_specification_opt
1651   (cp_parser *);
1652 static tree cp_parser_type_id_list
1653   (cp_parser *);
1654
1655 /* GNU Extensions */
1656
1657 static tree cp_parser_asm_specification_opt
1658   (cp_parser *);
1659 static tree cp_parser_asm_operand_list
1660   (cp_parser *);
1661 static tree cp_parser_asm_clobber_list
1662   (cp_parser *);
1663 static tree cp_parser_attributes_opt
1664   (cp_parser *);
1665 static tree cp_parser_attribute_list
1666   (cp_parser *);
1667 static bool cp_parser_extension_opt
1668   (cp_parser *, int *);
1669 static void cp_parser_label_declaration
1670   (cp_parser *);
1671
1672 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1673 static bool cp_parser_pragma
1674   (cp_parser *, enum pragma_context);
1675
1676 /* Objective-C++ Productions */
1677
1678 static tree cp_parser_objc_message_receiver
1679   (cp_parser *);
1680 static tree cp_parser_objc_message_args
1681   (cp_parser *);
1682 static tree cp_parser_objc_message_expression
1683   (cp_parser *);
1684 static tree cp_parser_objc_encode_expression
1685   (cp_parser *);
1686 static tree cp_parser_objc_defs_expression
1687   (cp_parser *);
1688 static tree cp_parser_objc_protocol_expression
1689   (cp_parser *);
1690 static tree cp_parser_objc_selector_expression
1691   (cp_parser *);
1692 static tree cp_parser_objc_expression
1693   (cp_parser *);
1694 static bool cp_parser_objc_selector_p
1695   (enum cpp_ttype);
1696 static tree cp_parser_objc_selector
1697   (cp_parser *);
1698 static tree cp_parser_objc_protocol_refs_opt
1699   (cp_parser *);
1700 static void cp_parser_objc_declaration
1701   (cp_parser *);
1702 static tree cp_parser_objc_statement
1703   (cp_parser *);
1704
1705 /* Utility Routines */
1706
1707 static tree cp_parser_lookup_name
1708   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1709 static tree cp_parser_lookup_name_simple
1710   (cp_parser *, tree);
1711 static tree cp_parser_maybe_treat_template_as_class
1712   (tree, bool);
1713 static bool cp_parser_check_declarator_template_parameters
1714   (cp_parser *, cp_declarator *);
1715 static bool cp_parser_check_template_parameters
1716   (cp_parser *, unsigned);
1717 static tree cp_parser_simple_cast_expression
1718   (cp_parser *);
1719 static tree cp_parser_global_scope_opt
1720   (cp_parser *, bool);
1721 static bool cp_parser_constructor_declarator_p
1722   (cp_parser *, bool);
1723 static tree cp_parser_function_definition_from_specifiers_and_declarator
1724   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1725 static tree cp_parser_function_definition_after_declarator
1726   (cp_parser *, bool);
1727 static void cp_parser_template_declaration_after_export
1728   (cp_parser *, bool);
1729 static tree cp_parser_single_declaration
1730   (cp_parser *, bool, bool *);
1731 static tree cp_parser_functional_cast
1732   (cp_parser *, tree);
1733 static tree cp_parser_save_member_function_body
1734   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1735 static tree cp_parser_enclosed_template_argument_list
1736   (cp_parser *);
1737 static void cp_parser_save_default_args
1738   (cp_parser *, tree);
1739 static void cp_parser_late_parsing_for_member
1740   (cp_parser *, tree);
1741 static void cp_parser_late_parsing_default_args
1742   (cp_parser *, tree);
1743 static tree cp_parser_sizeof_operand
1744   (cp_parser *, enum rid);
1745 static bool cp_parser_declares_only_class_p
1746   (cp_parser *);
1747 static void cp_parser_set_storage_class
1748   (cp_decl_specifier_seq *, cp_storage_class);
1749 static void cp_parser_set_decl_spec_type
1750   (cp_decl_specifier_seq *, tree, bool);
1751 static bool cp_parser_friend_p
1752   (const cp_decl_specifier_seq *);
1753 static cp_token *cp_parser_require
1754   (cp_parser *, enum cpp_ttype, const char *);
1755 static cp_token *cp_parser_require_keyword
1756   (cp_parser *, enum rid, const char *);
1757 static bool cp_parser_token_starts_function_definition_p
1758   (cp_token *);
1759 static bool cp_parser_next_token_starts_class_definition_p
1760   (cp_parser *);
1761 static bool cp_parser_next_token_ends_template_argument_p
1762   (cp_parser *);
1763 static bool cp_parser_nth_token_starts_template_argument_list_p
1764   (cp_parser *, size_t);
1765 static enum tag_types cp_parser_token_is_class_key
1766   (cp_token *);
1767 static void cp_parser_check_class_key
1768   (enum tag_types, tree type);
1769 static void cp_parser_check_access_in_redeclaration
1770   (tree type);
1771 static bool cp_parser_optional_template_keyword
1772   (cp_parser *);
1773 static void cp_parser_pre_parsed_nested_name_specifier
1774   (cp_parser *);
1775 static void cp_parser_cache_group
1776   (cp_parser *, enum cpp_ttype, unsigned);
1777 static void cp_parser_parse_tentatively
1778   (cp_parser *);
1779 static void cp_parser_commit_to_tentative_parse
1780   (cp_parser *);
1781 static void cp_parser_abort_tentative_parse
1782   (cp_parser *);
1783 static bool cp_parser_parse_definitely
1784   (cp_parser *);
1785 static inline bool cp_parser_parsing_tentatively
1786   (cp_parser *);
1787 static bool cp_parser_uncommitted_to_tentative_parse_p
1788   (cp_parser *);
1789 static void cp_parser_error
1790   (cp_parser *, const char *);
1791 static void cp_parser_name_lookup_error
1792   (cp_parser *, tree, tree, const char *);
1793 static bool cp_parser_simulate_error
1794   (cp_parser *);
1795 static void cp_parser_check_type_definition
1796   (cp_parser *);
1797 static void cp_parser_check_for_definition_in_return_type
1798   (cp_declarator *, tree);
1799 static void cp_parser_check_for_invalid_template_id
1800   (cp_parser *, tree);
1801 static bool cp_parser_non_integral_constant_expression
1802   (cp_parser *, const char *);
1803 static void cp_parser_diagnose_invalid_type_name
1804   (cp_parser *, tree, tree);
1805 static bool cp_parser_parse_and_diagnose_invalid_type_name
1806   (cp_parser *);
1807 static int cp_parser_skip_to_closing_parenthesis
1808   (cp_parser *, bool, bool, bool);
1809 static void cp_parser_skip_to_end_of_statement
1810   (cp_parser *);
1811 static void cp_parser_consume_semicolon_at_end_of_statement
1812   (cp_parser *);
1813 static void cp_parser_skip_to_end_of_block_or_statement
1814   (cp_parser *);
1815 static void cp_parser_skip_to_closing_brace
1816   (cp_parser *);
1817 static void cp_parser_skip_until_found
1818   (cp_parser *, enum cpp_ttype, const char *);
1819 static void cp_parser_skip_to_pragma_eol
1820   (cp_parser*, cp_token *);
1821 static bool cp_parser_error_occurred
1822   (cp_parser *);
1823 static bool cp_parser_allow_gnu_extensions_p
1824   (cp_parser *);
1825 static bool cp_parser_is_string_literal
1826   (cp_token *);
1827 static bool cp_parser_is_keyword
1828   (cp_token *, enum rid);
1829 static tree cp_parser_make_typename_type
1830   (cp_parser *, tree, tree);
1831
1832 /* Returns nonzero if we are parsing tentatively.  */
1833
1834 static inline bool
1835 cp_parser_parsing_tentatively (cp_parser* parser)
1836 {
1837   return parser->context->next != NULL;
1838 }
1839
1840 /* Returns nonzero if TOKEN is a string literal.  */
1841
1842 static bool
1843 cp_parser_is_string_literal (cp_token* token)
1844 {
1845   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1846 }
1847
1848 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1849
1850 static bool
1851 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1852 {
1853   return token->keyword == keyword;
1854 }
1855
1856 /* A minimum or maximum operator has been seen.  As these are
1857    deprecated, issue a warning.  */
1858
1859 static inline void
1860 cp_parser_warn_min_max (void)
1861 {
1862   if (warn_deprecated && !in_system_header)
1863     warning (0, "minimum/maximum operators are deprecated");
1864 }
1865
1866 /* If not parsing tentatively, issue a diagnostic of the form
1867       FILE:LINE: MESSAGE before TOKEN
1868    where TOKEN is the next token in the input stream.  MESSAGE
1869    (specified by the caller) is usually of the form "expected
1870    OTHER-TOKEN".  */
1871
1872 static void
1873 cp_parser_error (cp_parser* parser, const char* message)
1874 {
1875   if (!cp_parser_simulate_error (parser))
1876     {
1877       cp_token *token = cp_lexer_peek_token (parser->lexer);
1878       /* This diagnostic makes more sense if it is tagged to the line
1879          of the token we just peeked at.  */
1880       cp_lexer_set_source_position_from_token (token);
1881
1882       if (token->type == CPP_PRAGMA)
1883         {
1884           error ("%<#pragma%> is not allowed here");
1885           cp_parser_skip_to_pragma_eol (parser, token);
1886           return;
1887         }
1888
1889       c_parse_error (message,
1890                      /* Because c_parser_error does not understand
1891                         CPP_KEYWORD, keywords are treated like
1892                         identifiers.  */
1893                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1894                      token->value);
1895     }
1896 }
1897
1898 /* Issue an error about name-lookup failing.  NAME is the
1899    IDENTIFIER_NODE DECL is the result of
1900    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1901    the thing that we hoped to find.  */
1902
1903 static void
1904 cp_parser_name_lookup_error (cp_parser* parser,
1905                              tree name,
1906                              tree decl,
1907                              const char* desired)
1908 {
1909   /* If name lookup completely failed, tell the user that NAME was not
1910      declared.  */
1911   if (decl == error_mark_node)
1912     {
1913       if (parser->scope && parser->scope != global_namespace)
1914         error ("%<%D::%D%> has not been declared",
1915                parser->scope, name);
1916       else if (parser->scope == global_namespace)
1917         error ("%<::%D%> has not been declared", name);
1918       else if (parser->object_scope
1919                && !CLASS_TYPE_P (parser->object_scope))
1920         error ("request for member %qD in non-class type %qT",
1921                name, parser->object_scope);
1922       else if (parser->object_scope)
1923         error ("%<%T::%D%> has not been declared",
1924                parser->object_scope, name);
1925       else
1926         error ("%qD has not been declared", name);
1927     }
1928   else if (parser->scope && parser->scope != global_namespace)
1929     error ("%<%D::%D%> %s", parser->scope, name, desired);
1930   else if (parser->scope == global_namespace)
1931     error ("%<::%D%> %s", name, desired);
1932   else
1933     error ("%qD %s", name, desired);
1934 }
1935
1936 /* If we are parsing tentatively, remember that an error has occurred
1937    during this tentative parse.  Returns true if the error was
1938    simulated; false if a message should be issued by the caller.  */
1939
1940 static bool
1941 cp_parser_simulate_error (cp_parser* parser)
1942 {
1943   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1944     {
1945       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1946       return true;
1947     }
1948   return false;
1949 }
1950
1951 /* This function is called when a type is defined.  If type
1952    definitions are forbidden at this point, an error message is
1953    issued.  */
1954
1955 static void
1956 cp_parser_check_type_definition (cp_parser* parser)
1957 {
1958   /* If types are forbidden here, issue a message.  */
1959   if (parser->type_definition_forbidden_message)
1960     /* Use `%s' to print the string in case there are any escape
1961        characters in the message.  */
1962     error ("%s", parser->type_definition_forbidden_message);
1963 }
1964
1965 /* This function is called when the DECLARATOR is processed.  The TYPE
1966    was a type defined in the decl-specifiers.  If it is invalid to
1967    define a type in the decl-specifiers for DECLARATOR, an error is
1968    issued.  */
1969
1970 static void
1971 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1972                                                tree type)
1973 {
1974   /* [dcl.fct] forbids type definitions in return types.
1975      Unfortunately, it's not easy to know whether or not we are
1976      processing a return type until after the fact.  */
1977   while (declarator
1978          && (declarator->kind == cdk_pointer
1979              || declarator->kind == cdk_reference
1980              || declarator->kind == cdk_ptrmem))
1981     declarator = declarator->declarator;
1982   if (declarator
1983       && declarator->kind == cdk_function)
1984     {
1985       error ("new types may not be defined in a return type");
1986       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1987               type);
1988     }
1989 }
1990
1991 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1992    "<" in any valid C++ program.  If the next token is indeed "<",
1993    issue a message warning the user about what appears to be an
1994    invalid attempt to form a template-id.  */
1995
1996 static void
1997 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1998                                          tree type)
1999 {
2000   cp_token_position start = 0;
2001
2002   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2003     {
2004       if (TYPE_P (type))
2005         error ("%qT is not a template", type);
2006       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2007         error ("%qE is not a template", type);
2008       else
2009         error ("invalid template-id");
2010       /* Remember the location of the invalid "<".  */
2011       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2012         start = cp_lexer_token_position (parser->lexer, true);
2013       /* Consume the "<".  */
2014       cp_lexer_consume_token (parser->lexer);
2015       /* Parse the template arguments.  */
2016       cp_parser_enclosed_template_argument_list (parser);
2017       /* Permanently remove the invalid template arguments so that
2018          this error message is not issued again.  */
2019       if (start)
2020         cp_lexer_purge_tokens_after (parser->lexer, start);
2021     }
2022 }
2023
2024 /* If parsing an integral constant-expression, issue an error message
2025    about the fact that THING appeared and return true.  Otherwise,
2026    return false.  In either case, set
2027    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2028
2029 static bool
2030 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2031                                             const char *thing)
2032 {
2033   parser->non_integral_constant_expression_p = true;
2034   if (parser->integral_constant_expression_p)
2035     {
2036       if (!parser->allow_non_integral_constant_expression_p)
2037         {
2038           error ("%s cannot appear in a constant-expression", thing);
2039           return true;
2040         }
2041     }
2042   return false;
2043 }
2044
2045 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2046    qualifying scope (or NULL, if none) for ID.  This function commits
2047    to the current active tentative parse, if any.  (Otherwise, the
2048    problematic construct might be encountered again later, resulting
2049    in duplicate error messages.)  */
2050
2051 static void
2052 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2053 {
2054   tree decl, old_scope;
2055   /* Try to lookup the identifier.  */
2056   old_scope = parser->scope;
2057   parser->scope = scope;
2058   decl = cp_parser_lookup_name_simple (parser, id);
2059   parser->scope = old_scope;
2060   /* If the lookup found a template-name, it means that the user forgot
2061   to specify an argument list. Emit a useful error message.  */
2062   if (TREE_CODE (decl) == TEMPLATE_DECL)
2063     error ("invalid use of template-name %qE without an argument list",
2064       decl);
2065   else if (!parser->scope)
2066     {
2067       /* Issue an error message.  */
2068       error ("%qE does not name a type", id);
2069       /* If we're in a template class, it's possible that the user was
2070          referring to a type from a base class.  For example:
2071
2072            template <typename T> struct A { typedef T X; };
2073            template <typename T> struct B : public A<T> { X x; };
2074
2075          The user should have said "typename A<T>::X".  */
2076       if (processing_template_decl && current_class_type
2077           && TYPE_BINFO (current_class_type))
2078         {
2079           tree b;
2080
2081           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2082                b;
2083                b = TREE_CHAIN (b))
2084             {
2085               tree base_type = BINFO_TYPE (b);
2086               if (CLASS_TYPE_P (base_type)
2087                   && dependent_type_p (base_type))
2088                 {
2089                   tree field;
2090                   /* Go from a particular instantiation of the
2091                      template (which will have an empty TYPE_FIELDs),
2092                      to the main version.  */
2093                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2094                   for (field = TYPE_FIELDS (base_type);
2095                        field;
2096                        field = TREE_CHAIN (field))
2097                     if (TREE_CODE (field) == TYPE_DECL
2098                         && DECL_NAME (field) == id)
2099                       {
2100                         inform ("(perhaps %<typename %T::%E%> was intended)",
2101                                 BINFO_TYPE (b), id);
2102                         break;
2103                       }
2104                   if (field)
2105                     break;
2106                 }
2107             }
2108         }
2109     }
2110   /* Here we diagnose qualified-ids where the scope is actually correct,
2111      but the identifier does not resolve to a valid type name.  */
2112   else if (parser->scope != error_mark_node)
2113     {
2114       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2115         error ("%qE in namespace %qE does not name a type",
2116                id, parser->scope);
2117       else if (TYPE_P (parser->scope))
2118         error ("%qE in class %qT does not name a type", id, parser->scope);
2119       else
2120         gcc_unreachable ();
2121     }
2122   cp_parser_commit_to_tentative_parse (parser);
2123 }
2124
2125 /* Check for a common situation where a type-name should be present,
2126    but is not, and issue a sensible error message.  Returns true if an
2127    invalid type-name was detected.
2128
2129    The situation handled by this function are variable declarations of the
2130    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2131    Usually, `ID' should name a type, but if we got here it means that it
2132    does not. We try to emit the best possible error message depending on
2133    how exactly the id-expression looks like.
2134 */
2135
2136 static bool
2137 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2138 {
2139   tree id;
2140
2141   cp_parser_parse_tentatively (parser);
2142   id = cp_parser_id_expression (parser,
2143                                 /*template_keyword_p=*/false,
2144                                 /*check_dependency_p=*/true,
2145                                 /*template_p=*/NULL,
2146                                 /*declarator_p=*/true);
2147   /* After the id-expression, there should be a plain identifier,
2148      otherwise this is not a simple variable declaration. Also, if
2149      the scope is dependent, we cannot do much.  */
2150   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2151       || (parser->scope && TYPE_P (parser->scope)
2152           && dependent_type_p (parser->scope)))
2153     {
2154       cp_parser_abort_tentative_parse (parser);
2155       return false;
2156     }
2157   if (!cp_parser_parse_definitely (parser)
2158       || TREE_CODE (id) != IDENTIFIER_NODE)
2159     return false;
2160
2161   /* Emit a diagnostic for the invalid type.  */
2162   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2163   /* Skip to the end of the declaration; there's no point in
2164      trying to process it.  */
2165   cp_parser_skip_to_end_of_block_or_statement (parser);
2166   return true;
2167 }
2168
2169 /* Consume tokens up to, and including, the next non-nested closing `)'.
2170    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2171    are doing error recovery. Returns -1 if OR_COMMA is true and we
2172    found an unnested comma.  */
2173
2174 static int
2175 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2176                                        bool recovering,
2177                                        bool or_comma,
2178                                        bool consume_paren)
2179 {
2180   unsigned paren_depth = 0;
2181   unsigned brace_depth = 0;
2182
2183   if (recovering && !or_comma
2184       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2185     return 0;
2186
2187   while (true)
2188     {
2189       cp_token * token = cp_lexer_peek_token (parser->lexer);
2190
2191       switch (token->type)
2192         {
2193         case CPP_EOF:
2194         case CPP_PRAGMA_EOL:
2195           /* If we've run out of tokens, then there is no closing `)'.  */
2196           return 0;
2197
2198         case CPP_SEMICOLON:
2199           /* This matches the processing in skip_to_end_of_statement.  */
2200           if (!brace_depth)
2201             return 0;
2202           break;
2203
2204         case CPP_OPEN_BRACE:
2205           ++brace_depth;
2206           break;
2207         case CPP_CLOSE_BRACE:
2208           if (!brace_depth--)
2209             return 0;
2210           break;
2211
2212         case CPP_COMMA:
2213           if (recovering && or_comma && !brace_depth && !paren_depth)
2214             return -1;
2215           break;
2216
2217         case CPP_OPEN_PAREN:
2218           if (!brace_depth)
2219             ++paren_depth;
2220           break;
2221
2222         case CPP_CLOSE_PAREN:
2223           if (!brace_depth && !paren_depth--)
2224             {
2225               if (consume_paren)
2226                 cp_lexer_consume_token (parser->lexer);
2227               return 1;
2228             }
2229           break;
2230
2231         default:
2232           break;
2233         }
2234
2235       /* Consume the token.  */
2236       cp_lexer_consume_token (parser->lexer);
2237     }
2238 }
2239
2240 /* Consume tokens until we reach the end of the current statement.
2241    Normally, that will be just before consuming a `;'.  However, if a
2242    non-nested `}' comes first, then we stop before consuming that.  */
2243
2244 static void
2245 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2246 {
2247   unsigned nesting_depth = 0;
2248
2249   while (true)
2250     {
2251       cp_token *token = cp_lexer_peek_token (parser->lexer);
2252
2253       switch (token->type)
2254         {
2255         case CPP_EOF:
2256         case CPP_PRAGMA_EOL:
2257           /* If we've run out of tokens, stop.  */
2258           return;
2259
2260         case CPP_SEMICOLON:
2261           /* If the next token is a `;', we have reached the end of the
2262              statement.  */
2263           if (!nesting_depth)
2264             return;
2265           break;
2266
2267         case CPP_CLOSE_BRACE:
2268           /* If this is a non-nested '}', stop before consuming it.
2269              That way, when confronted with something like:
2270
2271                { 3 + }
2272
2273              we stop before consuming the closing '}', even though we
2274              have not yet reached a `;'.  */
2275           if (nesting_depth == 0)
2276             return;
2277
2278           /* If it is the closing '}' for a block that we have
2279              scanned, stop -- but only after consuming the token.
2280              That way given:
2281
2282                 void f g () { ... }
2283                 typedef int I;
2284
2285              we will stop after the body of the erroneously declared
2286              function, but before consuming the following `typedef'
2287              declaration.  */
2288           if (--nesting_depth == 0)
2289             {
2290               cp_lexer_consume_token (parser->lexer);
2291               return;
2292             }
2293
2294         case CPP_OPEN_BRACE:
2295           ++nesting_depth;
2296           break;
2297
2298         default:
2299           break;
2300         }
2301
2302       /* Consume the token.  */
2303       cp_lexer_consume_token (parser->lexer);
2304     }
2305 }
2306
2307 /* This function is called at the end of a statement or declaration.
2308    If the next token is a semicolon, it is consumed; otherwise, error
2309    recovery is attempted.  */
2310
2311 static void
2312 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2313 {
2314   /* Look for the trailing `;'.  */
2315   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2316     {
2317       /* If there is additional (erroneous) input, skip to the end of
2318          the statement.  */
2319       cp_parser_skip_to_end_of_statement (parser);
2320       /* If the next token is now a `;', consume it.  */
2321       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2322         cp_lexer_consume_token (parser->lexer);
2323     }
2324 }
2325
2326 /* Skip tokens until we have consumed an entire block, or until we
2327    have consumed a non-nested `;'.  */
2328
2329 static void
2330 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2331 {
2332   int nesting_depth = 0;
2333
2334   while (nesting_depth >= 0)
2335     {
2336       cp_token *token = cp_lexer_peek_token (parser->lexer);
2337
2338       switch (token->type)
2339         {
2340         case CPP_EOF:
2341         case CPP_PRAGMA_EOL:
2342           /* If we've run out of tokens, stop.  */
2343           return;
2344
2345         case CPP_SEMICOLON:
2346           /* Stop if this is an unnested ';'. */
2347           if (!nesting_depth)
2348             nesting_depth = -1;
2349           break;
2350
2351         case CPP_CLOSE_BRACE:
2352           /* Stop if this is an unnested '}', or closes the outermost
2353              nesting level.  */
2354           nesting_depth--;
2355           if (!nesting_depth)
2356             nesting_depth = -1;
2357           break;
2358
2359         case CPP_OPEN_BRACE:
2360           /* Nest. */
2361           nesting_depth++;
2362           break;
2363
2364         default:
2365           break;
2366         }
2367
2368       /* Consume the token.  */
2369       cp_lexer_consume_token (parser->lexer);
2370     }
2371 }
2372
2373 /* Skip tokens until a non-nested closing curly brace is the next
2374    token.  */
2375
2376 static void
2377 cp_parser_skip_to_closing_brace (cp_parser *parser)
2378 {
2379   unsigned nesting_depth = 0;
2380
2381   while (true)
2382     {
2383       cp_token *token = cp_lexer_peek_token (parser->lexer);
2384
2385       switch (token->type)
2386         {
2387         case CPP_EOF:
2388         case CPP_PRAGMA_EOL:
2389           /* If we've run out of tokens, stop.  */
2390           return;
2391
2392         case CPP_CLOSE_BRACE:
2393           /* If the next token is a non-nested `}', then we have reached
2394              the end of the current block.  */
2395           if (nesting_depth-- == 0)
2396             return;
2397           break;
2398
2399         case CPP_OPEN_BRACE:
2400           /* If it the next token is a `{', then we are entering a new
2401              block.  Consume the entire block.  */
2402           ++nesting_depth;
2403           break;
2404
2405         default:
2406           break;
2407         }
2408
2409       /* Consume the token.  */
2410       cp_lexer_consume_token (parser->lexer);
2411     }
2412 }
2413
2414 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2415    parameter is the PRAGMA token, allowing us to purge the entire pragma
2416    sequence.  */
2417
2418 static void
2419 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2420 {
2421   cp_token *token;
2422
2423   parser->lexer->in_pragma = false;
2424
2425   do
2426     token = cp_lexer_consume_token (parser->lexer);
2427   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2428
2429   /* Ensure that the pragma is not parsed again.  */
2430   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2431 }
2432
2433 /* This is a simple wrapper around make_typename_type. When the id is
2434    an unresolved identifier node, we can provide a superior diagnostic
2435    using cp_parser_diagnose_invalid_type_name.  */
2436
2437 static tree
2438 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2439 {
2440   tree result;
2441   if (TREE_CODE (id) == IDENTIFIER_NODE)
2442     {
2443       result = make_typename_type (scope, id, typename_type,
2444                                    /*complain=*/tf_none);
2445       if (result == error_mark_node)
2446         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2447       return result;
2448     }
2449   return make_typename_type (scope, id, typename_type, tf_error);
2450 }
2451
2452
2453 /* Create a new C++ parser.  */
2454
2455 static cp_parser *
2456 cp_parser_new (void)
2457 {
2458   cp_parser *parser;
2459   cp_lexer *lexer;
2460   unsigned i;
2461
2462   /* cp_lexer_new_main is called before calling ggc_alloc because
2463      cp_lexer_new_main might load a PCH file.  */
2464   lexer = cp_lexer_new_main ();
2465
2466   /* Initialize the binops_by_token so that we can get the tree
2467      directly from the token.  */
2468   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2469     binops_by_token[binops[i].token_type] = binops[i];
2470
2471   parser = GGC_CNEW (cp_parser);
2472   parser->lexer = lexer;
2473   parser->context = cp_parser_context_new (NULL);
2474
2475   /* For now, we always accept GNU extensions.  */
2476   parser->allow_gnu_extensions_p = 1;
2477
2478   /* The `>' token is a greater-than operator, not the end of a
2479      template-id.  */
2480   parser->greater_than_is_operator_p = true;
2481
2482   parser->default_arg_ok_p = true;
2483
2484   /* We are not parsing a constant-expression.  */
2485   parser->integral_constant_expression_p = false;
2486   parser->allow_non_integral_constant_expression_p = false;
2487   parser->non_integral_constant_expression_p = false;
2488
2489   /* Local variable names are not forbidden.  */
2490   parser->local_variables_forbidden_p = false;
2491
2492   /* We are not processing an `extern "C"' declaration.  */
2493   parser->in_unbraced_linkage_specification_p = false;
2494
2495   /* We are not processing a declarator.  */
2496   parser->in_declarator_p = false;
2497
2498   /* We are not processing a template-argument-list.  */
2499   parser->in_template_argument_list_p = false;
2500
2501   /* We are not in an iteration statement.  */
2502   parser->in_iteration_statement_p = false;
2503
2504   /* We are not in a switch statement.  */
2505   parser->in_switch_statement_p = false;
2506
2507   /* We are not parsing a type-id inside an expression.  */
2508   parser->in_type_id_in_expr_p = false;
2509
2510   /* Declarations aren't implicitly extern "C".  */
2511   parser->implicit_extern_c = false;
2512
2513   /* String literals should be translated to the execution character set.  */
2514   parser->translate_strings_p = true;
2515
2516   /* The unparsed function queue is empty.  */
2517   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2518
2519   /* There are no classes being defined.  */
2520   parser->num_classes_being_defined = 0;
2521
2522   /* No template parameters apply.  */
2523   parser->num_template_parameter_lists = 0;
2524
2525   return parser;
2526 }
2527
2528 /* Create a cp_lexer structure which will emit the tokens in CACHE
2529    and push it onto the parser's lexer stack.  This is used for delayed
2530    parsing of in-class method bodies and default arguments, and should
2531    not be confused with tentative parsing.  */
2532 static void
2533 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2534 {
2535   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2536   lexer->next = parser->lexer;
2537   parser->lexer = lexer;
2538
2539   /* Move the current source position to that of the first token in the
2540      new lexer.  */
2541   cp_lexer_set_source_position_from_token (lexer->next_token);
2542 }
2543
2544 /* Pop the top lexer off the parser stack.  This is never used for the
2545    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2546 static void
2547 cp_parser_pop_lexer (cp_parser *parser)
2548 {
2549   cp_lexer *lexer = parser->lexer;
2550   parser->lexer = lexer->next;
2551   cp_lexer_destroy (lexer);
2552
2553   /* Put the current source position back where it was before this
2554      lexer was pushed.  */
2555   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2556 }
2557
2558 /* Lexical conventions [gram.lex]  */
2559
2560 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2561    identifier.  */
2562
2563 static tree
2564 cp_parser_identifier (cp_parser* parser)
2565 {
2566   cp_token *token;
2567
2568   /* Look for the identifier.  */
2569   token = cp_parser_require (parser, CPP_NAME, "identifier");
2570   /* Return the value.  */
2571   return token ? token->value : error_mark_node;
2572 }
2573
2574 /* Parse a sequence of adjacent string constants.  Returns a
2575    TREE_STRING representing the combined, nul-terminated string
2576    constant.  If TRANSLATE is true, translate the string to the
2577    execution character set.  If WIDE_OK is true, a wide string is
2578    invalid here.
2579
2580    C++98 [lex.string] says that if a narrow string literal token is
2581    adjacent to a wide string literal token, the behavior is undefined.
2582    However, C99 6.4.5p4 says that this results in a wide string literal.
2583    We follow C99 here, for consistency with the C front end.
2584
2585    This code is largely lifted from lex_string() in c-lex.c.
2586
2587    FUTURE: ObjC++ will need to handle @-strings here.  */
2588 static tree
2589 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2590 {
2591   tree value;
2592   bool wide = false;
2593   size_t count;
2594   struct obstack str_ob;
2595   cpp_string str, istr, *strs;
2596   cp_token *tok;
2597
2598   tok = cp_lexer_peek_token (parser->lexer);
2599   if (!cp_parser_is_string_literal (tok))
2600     {
2601       cp_parser_error (parser, "expected string-literal");
2602       return error_mark_node;
2603     }
2604
2605   /* Try to avoid the overhead of creating and destroying an obstack
2606      for the common case of just one string.  */
2607   if (!cp_parser_is_string_literal
2608       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2609     {
2610       cp_lexer_consume_token (parser->lexer);
2611
2612       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2613       str.len = TREE_STRING_LENGTH (tok->value);
2614       count = 1;
2615       if (tok->type == CPP_WSTRING)
2616         wide = true;
2617
2618       strs = &str;
2619     }
2620   else
2621     {
2622       gcc_obstack_init (&str_ob);
2623       count = 0;
2624
2625       do
2626         {
2627           cp_lexer_consume_token (parser->lexer);
2628           count++;
2629           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2630           str.len = TREE_STRING_LENGTH (tok->value);
2631           if (tok->type == CPP_WSTRING)
2632             wide = true;
2633
2634           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2635
2636           tok = cp_lexer_peek_token (parser->lexer);
2637         }
2638       while (cp_parser_is_string_literal (tok));
2639
2640       strs = (cpp_string *) obstack_finish (&str_ob);
2641     }
2642
2643   if (wide && !wide_ok)
2644     {
2645       cp_parser_error (parser, "a wide string is invalid in this context");
2646       wide = false;
2647     }
2648
2649   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2650       (parse_in, strs, count, &istr, wide))
2651     {
2652       value = build_string (istr.len, (char *)istr.text);
2653       free ((void *)istr.text);
2654
2655       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2656       value = fix_string_type (value);
2657     }
2658   else
2659     /* cpp_interpret_string has issued an error.  */
2660     value = error_mark_node;
2661
2662   if (count > 1)
2663     obstack_free (&str_ob, 0);
2664
2665   return value;
2666 }
2667
2668
2669 /* Basic concepts [gram.basic]  */
2670
2671 /* Parse a translation-unit.
2672
2673    translation-unit:
2674      declaration-seq [opt]
2675
2676    Returns TRUE if all went well.  */
2677
2678 static bool
2679 cp_parser_translation_unit (cp_parser* parser)
2680 {
2681   /* The address of the first non-permanent object on the declarator
2682      obstack.  */
2683   static void *declarator_obstack_base;
2684
2685   bool success;
2686
2687   /* Create the declarator obstack, if necessary.  */
2688   if (!cp_error_declarator)
2689     {
2690       gcc_obstack_init (&declarator_obstack);
2691       /* Create the error declarator.  */
2692       cp_error_declarator = make_declarator (cdk_error);
2693       /* Create the empty parameter list.  */
2694       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2695       /* Remember where the base of the declarator obstack lies.  */
2696       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2697     }
2698
2699   cp_parser_declaration_seq_opt (parser);
2700   
2701   /* If there are no tokens left then all went well.  */
2702   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2703     {
2704       /* Get rid of the token array; we don't need it any more.  */
2705       cp_lexer_destroy (parser->lexer);
2706       parser->lexer = NULL;
2707       
2708       /* This file might have been a context that's implicitly extern
2709          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2710       if (parser->implicit_extern_c)
2711         {
2712           pop_lang_context ();
2713           parser->implicit_extern_c = false;
2714         }
2715       
2716       /* Finish up.  */
2717       finish_translation_unit ();
2718       
2719       success = true;
2720     }
2721   else
2722     {
2723       cp_parser_error (parser, "expected declaration");
2724       success = false;
2725     }
2726   
2727   /* Make sure the declarator obstack was fully cleaned up.  */
2728   gcc_assert (obstack_next_free (&declarator_obstack)
2729               == declarator_obstack_base);
2730
2731   /* All went well.  */
2732   return success;
2733 }
2734
2735 /* Expressions [gram.expr] */
2736
2737 /* Parse a primary-expression.
2738
2739    primary-expression:
2740      literal
2741      this
2742      ( expression )
2743      id-expression
2744
2745    GNU Extensions:
2746
2747    primary-expression:
2748      ( compound-statement )
2749      __builtin_va_arg ( assignment-expression , type-id )
2750
2751    Objective-C++ Extension:
2752
2753    primary-expression:
2754      objc-expression
2755
2756    literal:
2757      __null
2758
2759    ADDRESS_P is true iff this expression was immediately preceded by
2760    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2761    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2762    true iff this expression is a template argument.
2763
2764    Returns a representation of the expression.  Upon return, *IDK
2765    indicates what kind of id-expression (if any) was present.  */
2766
2767 static tree
2768 cp_parser_primary_expression (cp_parser *parser,
2769                               bool address_p,
2770                               bool cast_p,
2771                               bool template_arg_p,
2772                               cp_id_kind *idk)
2773 {
2774   cp_token *token;
2775
2776   /* Assume the primary expression is not an id-expression.  */
2777   *idk = CP_ID_KIND_NONE;
2778
2779   /* Peek at the next token.  */
2780   token = cp_lexer_peek_token (parser->lexer);
2781   switch (token->type)
2782     {
2783       /* literal:
2784            integer-literal
2785            character-literal
2786            floating-literal
2787            string-literal
2788            boolean-literal  */
2789     case CPP_CHAR:
2790     case CPP_WCHAR:
2791     case CPP_NUMBER:
2792       token = cp_lexer_consume_token (parser->lexer);
2793       /* Floating-point literals are only allowed in an integral
2794          constant expression if they are cast to an integral or
2795          enumeration type.  */
2796       if (TREE_CODE (token->value) == REAL_CST
2797           && parser->integral_constant_expression_p
2798           && pedantic)
2799         {
2800           /* CAST_P will be set even in invalid code like "int(2.7 +
2801              ...)".   Therefore, we have to check that the next token
2802              is sure to end the cast.  */
2803           if (cast_p)
2804             {
2805               cp_token *next_token;
2806
2807               next_token = cp_lexer_peek_token (parser->lexer);
2808               if (/* The comma at the end of an
2809                      enumerator-definition.  */
2810                   next_token->type != CPP_COMMA
2811                   /* The curly brace at the end of an enum-specifier.  */
2812                   && next_token->type != CPP_CLOSE_BRACE
2813                   /* The end of a statement.  */
2814                   && next_token->type != CPP_SEMICOLON
2815                   /* The end of the cast-expression.  */
2816                   && next_token->type != CPP_CLOSE_PAREN
2817                   /* The end of an array bound.  */
2818                   && next_token->type != CPP_CLOSE_SQUARE
2819                   /* The closing ">" in a template-argument-list.  */
2820                   && (next_token->type != CPP_GREATER
2821                       || parser->greater_than_is_operator_p))
2822                 cast_p = false;
2823             }
2824
2825           /* If we are within a cast, then the constraint that the
2826              cast is to an integral or enumeration type will be
2827              checked at that point.  If we are not within a cast, then
2828              this code is invalid.  */
2829           if (!cast_p)
2830             cp_parser_non_integral_constant_expression
2831               (parser, "floating-point literal");
2832         }
2833       return token->value;
2834
2835     case CPP_STRING:
2836     case CPP_WSTRING:
2837       /* ??? Should wide strings be allowed when parser->translate_strings_p
2838          is false (i.e. in attributes)?  If not, we can kill the third
2839          argument to cp_parser_string_literal.  */
2840       return cp_parser_string_literal (parser,
2841                                        parser->translate_strings_p,
2842                                        true);
2843
2844     case CPP_OPEN_PAREN:
2845       {
2846         tree expr;
2847         bool saved_greater_than_is_operator_p;
2848
2849         /* Consume the `('.  */
2850         cp_lexer_consume_token (parser->lexer);
2851         /* Within a parenthesized expression, a `>' token is always
2852            the greater-than operator.  */
2853         saved_greater_than_is_operator_p
2854           = parser->greater_than_is_operator_p;
2855         parser->greater_than_is_operator_p = true;
2856         /* If we see `( { ' then we are looking at the beginning of
2857            a GNU statement-expression.  */
2858         if (cp_parser_allow_gnu_extensions_p (parser)
2859             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2860           {
2861             /* Statement-expressions are not allowed by the standard.  */
2862             if (pedantic)
2863               pedwarn ("ISO C++ forbids braced-groups within expressions");
2864
2865             /* And they're not allowed outside of a function-body; you
2866                cannot, for example, write:
2867
2868                  int i = ({ int j = 3; j + 1; });
2869
2870                at class or namespace scope.  */
2871             if (!at_function_scope_p ())
2872               error ("statement-expressions are allowed only inside functions");
2873             /* Start the statement-expression.  */
2874             expr = begin_stmt_expr ();
2875             /* Parse the compound-statement.  */
2876             cp_parser_compound_statement (parser, expr, false);
2877             /* Finish up.  */
2878             expr = finish_stmt_expr (expr, false);
2879           }
2880         else
2881           {
2882             /* Parse the parenthesized expression.  */
2883             expr = cp_parser_expression (parser, cast_p);
2884             /* Let the front end know that this expression was
2885                enclosed in parentheses. This matters in case, for
2886                example, the expression is of the form `A::B', since
2887                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2888                not.  */
2889             finish_parenthesized_expr (expr);
2890           }
2891         /* The `>' token might be the end of a template-id or
2892            template-parameter-list now.  */
2893         parser->greater_than_is_operator_p
2894           = saved_greater_than_is_operator_p;
2895         /* Consume the `)'.  */
2896         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2897           cp_parser_skip_to_end_of_statement (parser);
2898
2899         return expr;
2900       }
2901
2902     case CPP_KEYWORD:
2903       switch (token->keyword)
2904         {
2905           /* These two are the boolean literals.  */
2906         case RID_TRUE:
2907           cp_lexer_consume_token (parser->lexer);
2908           return boolean_true_node;
2909         case RID_FALSE:
2910           cp_lexer_consume_token (parser->lexer);
2911           return boolean_false_node;
2912
2913           /* The `__null' literal.  */
2914         case RID_NULL:
2915           cp_lexer_consume_token (parser->lexer);
2916           return null_node;
2917
2918           /* Recognize the `this' keyword.  */
2919         case RID_THIS:
2920           cp_lexer_consume_token (parser->lexer);
2921           if (parser->local_variables_forbidden_p)
2922             {
2923               error ("%<this%> may not be used in this context");
2924               return error_mark_node;
2925             }
2926           /* Pointers cannot appear in constant-expressions.  */
2927           if (cp_parser_non_integral_constant_expression (parser,
2928                                                           "`this'"))
2929             return error_mark_node;
2930           return finish_this_expr ();
2931
2932           /* The `operator' keyword can be the beginning of an
2933              id-expression.  */
2934         case RID_OPERATOR:
2935           goto id_expression;
2936
2937         case RID_FUNCTION_NAME:
2938         case RID_PRETTY_FUNCTION_NAME:
2939         case RID_C99_FUNCTION_NAME:
2940           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2941              __func__ are the names of variables -- but they are
2942              treated specially.  Therefore, they are handled here,
2943              rather than relying on the generic id-expression logic
2944              below.  Grammatically, these names are id-expressions.
2945
2946              Consume the token.  */
2947           token = cp_lexer_consume_token (parser->lexer);
2948           /* Look up the name.  */
2949           return finish_fname (token->value);
2950
2951         case RID_VA_ARG:
2952           {
2953             tree expression;
2954             tree type;
2955
2956             /* The `__builtin_va_arg' construct is used to handle
2957                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2958             cp_lexer_consume_token (parser->lexer);
2959             /* Look for the opening `('.  */
2960             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2961             /* Now, parse the assignment-expression.  */
2962             expression = cp_parser_assignment_expression (parser,
2963                                                           /*cast_p=*/false);
2964             /* Look for the `,'.  */
2965             cp_parser_require (parser, CPP_COMMA, "`,'");
2966             /* Parse the type-id.  */
2967             type = cp_parser_type_id (parser);
2968             /* Look for the closing `)'.  */
2969             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2970             /* Using `va_arg' in a constant-expression is not
2971                allowed.  */
2972             if (cp_parser_non_integral_constant_expression (parser,
2973                                                             "`va_arg'"))
2974               return error_mark_node;
2975             return build_x_va_arg (expression, type);
2976           }
2977
2978         case RID_OFFSETOF:
2979           return cp_parser_builtin_offsetof (parser);
2980
2981           /* Objective-C++ expressions.  */
2982         case RID_AT_ENCODE:
2983         case RID_AT_PROTOCOL:
2984         case RID_AT_SELECTOR:
2985           return cp_parser_objc_expression (parser);
2986
2987         default:
2988           cp_parser_error (parser, "expected primary-expression");
2989           return error_mark_node;
2990         }
2991
2992       /* An id-expression can start with either an identifier, a
2993          `::' as the beginning of a qualified-id, or the "operator"
2994          keyword.  */
2995     case CPP_NAME:
2996     case CPP_SCOPE:
2997     case CPP_TEMPLATE_ID:
2998     case CPP_NESTED_NAME_SPECIFIER:
2999       {
3000         tree id_expression;
3001         tree decl;
3002         const char *error_msg;
3003         bool template_p;
3004         bool done;
3005
3006       id_expression:
3007         /* Parse the id-expression.  */
3008         id_expression
3009           = cp_parser_id_expression (parser,
3010                                      /*template_keyword_p=*/false,
3011                                      /*check_dependency_p=*/true,
3012                                      &template_p,
3013                                      /*declarator_p=*/false);
3014         if (id_expression == error_mark_node)
3015           return error_mark_node;
3016         token = cp_lexer_peek_token (parser->lexer);
3017         done = (token->type != CPP_OPEN_SQUARE
3018                 && token->type != CPP_OPEN_PAREN
3019                 && token->type != CPP_DOT
3020                 && token->type != CPP_DEREF
3021                 && token->type != CPP_PLUS_PLUS
3022                 && token->type != CPP_MINUS_MINUS);
3023         /* If we have a template-id, then no further lookup is
3024            required.  If the template-id was for a template-class, we
3025            will sometimes have a TYPE_DECL at this point.  */
3026         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3027                  || TREE_CODE (id_expression) == TYPE_DECL)
3028           decl = id_expression;
3029         /* Look up the name.  */
3030         else
3031           {
3032             tree ambiguous_decls;
3033
3034             decl = cp_parser_lookup_name (parser, id_expression,
3035                                           none_type,
3036                                           template_p,
3037                                           /*is_namespace=*/false,
3038                                           /*check_dependency=*/true,
3039                                           &ambiguous_decls);
3040             /* If the lookup was ambiguous, an error will already have
3041                been issued.  */
3042             if (ambiguous_decls)
3043               return error_mark_node;
3044
3045             /* In Objective-C++, an instance variable (ivar) may be preferred
3046                to whatever cp_parser_lookup_name() found.  */
3047             decl = objc_lookup_ivar (decl, id_expression);
3048
3049             /* If name lookup gives us a SCOPE_REF, then the
3050                qualifying scope was dependent.  */
3051             if (TREE_CODE (decl) == SCOPE_REF)
3052               return decl;
3053             /* Check to see if DECL is a local variable in a context
3054                where that is forbidden.  */
3055             if (parser->local_variables_forbidden_p
3056                 && local_variable_p (decl))
3057               {
3058                 /* It might be that we only found DECL because we are
3059                    trying to be generous with pre-ISO scoping rules.
3060                    For example, consider:
3061
3062                      int i;
3063                      void g() {
3064                        for (int i = 0; i < 10; ++i) {}
3065                        extern void f(int j = i);
3066                      }
3067
3068                    Here, name look up will originally find the out
3069                    of scope `i'.  We need to issue a warning message,
3070                    but then use the global `i'.  */
3071                 decl = check_for_out_of_scope_variable (decl);
3072                 if (local_variable_p (decl))
3073                   {
3074                     error ("local variable %qD may not appear in this context",
3075                            decl);
3076                     return error_mark_node;
3077                   }
3078               }
3079           }
3080
3081         decl = (finish_id_expression 
3082                 (id_expression, decl, parser->scope,
3083                  idk,
3084                  parser->integral_constant_expression_p,
3085                  parser->allow_non_integral_constant_expression_p,
3086                  &parser->non_integral_constant_expression_p,
3087                  template_p, done, address_p,
3088                  template_arg_p,
3089                  &error_msg));
3090         if (error_msg)
3091           cp_parser_error (parser, error_msg);
3092         return decl;
3093       }
3094
3095       /* Anything else is an error.  */
3096     default:
3097       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3098       if (c_dialect_objc ()
3099           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3100         return cp_parser_objc_expression (parser);
3101
3102       cp_parser_error (parser, "expected primary-expression");
3103       return error_mark_node;
3104     }
3105 }
3106
3107 /* Parse an id-expression.
3108
3109    id-expression:
3110      unqualified-id
3111      qualified-id
3112
3113    qualified-id:
3114      :: [opt] nested-name-specifier template [opt] unqualified-id
3115      :: identifier
3116      :: operator-function-id
3117      :: template-id
3118
3119    Return a representation of the unqualified portion of the
3120    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3121    a `::' or nested-name-specifier.
3122
3123    Often, if the id-expression was a qualified-id, the caller will
3124    want to make a SCOPE_REF to represent the qualified-id.  This
3125    function does not do this in order to avoid wastefully creating
3126    SCOPE_REFs when they are not required.
3127
3128    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3129    `template' keyword.
3130
3131    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3132    uninstantiated templates.
3133
3134    If *TEMPLATE_P is non-NULL, it is set to true iff the
3135    `template' keyword is used to explicitly indicate that the entity
3136    named is a template.
3137
3138    If DECLARATOR_P is true, the id-expression is appearing as part of
3139    a declarator, rather than as part of an expression.  */
3140
3141 static tree
3142 cp_parser_id_expression (cp_parser *parser,
3143                          bool template_keyword_p,
3144                          bool check_dependency_p,
3145                          bool *template_p,
3146                          bool declarator_p)
3147 {
3148   bool global_scope_p;
3149   bool nested_name_specifier_p;
3150
3151   /* Assume the `template' keyword was not used.  */
3152   if (template_p)
3153     *template_p = template_keyword_p;
3154
3155   /* Look for the optional `::' operator.  */
3156   global_scope_p
3157     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3158        != NULL_TREE);
3159   /* Look for the optional nested-name-specifier.  */
3160   nested_name_specifier_p
3161     = (cp_parser_nested_name_specifier_opt (parser,
3162                                             /*typename_keyword_p=*/false,
3163                                             check_dependency_p,
3164                                             /*type_p=*/false,
3165                                             declarator_p)
3166        != NULL_TREE);
3167   /* If there is a nested-name-specifier, then we are looking at
3168      the first qualified-id production.  */
3169   if (nested_name_specifier_p)
3170     {
3171       tree saved_scope;
3172       tree saved_object_scope;
3173       tree saved_qualifying_scope;
3174       tree unqualified_id;
3175       bool is_template;
3176
3177       /* See if the next token is the `template' keyword.  */
3178       if (!template_p)
3179         template_p = &is_template;
3180       *template_p = cp_parser_optional_template_keyword (parser);
3181       /* Name lookup we do during the processing of the
3182          unqualified-id might obliterate SCOPE.  */
3183       saved_scope = parser->scope;
3184       saved_object_scope = parser->object_scope;
3185       saved_qualifying_scope = parser->qualifying_scope;
3186       /* Process the final unqualified-id.  */
3187       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3188                                                  check_dependency_p,
3189                                                  declarator_p);
3190       /* Restore the SAVED_SCOPE for our caller.  */
3191       parser->scope = saved_scope;
3192       parser->object_scope = saved_object_scope;
3193       parser->qualifying_scope = saved_qualifying_scope;
3194
3195       return unqualified_id;
3196     }
3197   /* Otherwise, if we are in global scope, then we are looking at one
3198      of the other qualified-id productions.  */
3199   else if (global_scope_p)
3200     {
3201       cp_token *token;
3202       tree id;
3203
3204       /* Peek at the next token.  */
3205       token = cp_lexer_peek_token (parser->lexer);
3206
3207       /* If it's an identifier, and the next token is not a "<", then
3208          we can avoid the template-id case.  This is an optimization
3209          for this common case.  */
3210       if (token->type == CPP_NAME
3211           && !cp_parser_nth_token_starts_template_argument_list_p
3212                (parser, 2))
3213         return cp_parser_identifier (parser);
3214
3215       cp_parser_parse_tentatively (parser);
3216       /* Try a template-id.  */
3217       id = cp_parser_template_id (parser,
3218                                   /*template_keyword_p=*/false,
3219                                   /*check_dependency_p=*/true,
3220                                   declarator_p);
3221       /* If that worked, we're done.  */
3222       if (cp_parser_parse_definitely (parser))
3223         return id;
3224
3225       /* Peek at the next token.  (Changes in the token buffer may
3226          have invalidated the pointer obtained above.)  */
3227       token = cp_lexer_peek_token (parser->lexer);
3228
3229       switch (token->type)
3230         {
3231         case CPP_NAME:
3232           return cp_parser_identifier (parser);
3233
3234         case CPP_KEYWORD:
3235           if (token->keyword == RID_OPERATOR)
3236             return cp_parser_operator_function_id (parser);
3237           /* Fall through.  */
3238
3239         default:
3240           cp_parser_error (parser, "expected id-expression");
3241           return error_mark_node;
3242         }
3243     }
3244   else
3245     return cp_parser_unqualified_id (parser, template_keyword_p,
3246                                      /*check_dependency_p=*/true,
3247                                      declarator_p);
3248 }
3249
3250 /* Parse an unqualified-id.
3251
3252    unqualified-id:
3253      identifier
3254      operator-function-id
3255      conversion-function-id
3256      ~ class-name
3257      template-id
3258
3259    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3260    keyword, in a construct like `A::template ...'.
3261
3262    Returns a representation of unqualified-id.  For the `identifier'
3263    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3264    production a BIT_NOT_EXPR is returned; the operand of the
3265    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3266    other productions, see the documentation accompanying the
3267    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3268    names are looked up in uninstantiated templates.  If DECLARATOR_P
3269    is true, the unqualified-id is appearing as part of a declarator,
3270    rather than as part of an expression.  */
3271
3272 static tree
3273 cp_parser_unqualified_id (cp_parser* parser,
3274                           bool template_keyword_p,
3275                           bool check_dependency_p,
3276                           bool declarator_p)
3277 {
3278   cp_token *token;
3279
3280   /* Peek at the next token.  */
3281   token = cp_lexer_peek_token (parser->lexer);
3282
3283   switch (token->type)
3284     {
3285     case CPP_NAME:
3286       {
3287         tree id;
3288
3289         /* We don't know yet whether or not this will be a
3290            template-id.  */
3291         cp_parser_parse_tentatively (parser);
3292         /* Try a template-id.  */
3293         id = cp_parser_template_id (parser, template_keyword_p,
3294                                     check_dependency_p,
3295                                     declarator_p);
3296         /* If it worked, we're done.  */
3297         if (cp_parser_parse_definitely (parser))
3298           return id;
3299         /* Otherwise, it's an ordinary identifier.  */
3300         return cp_parser_identifier (parser);
3301       }
3302
3303     case CPP_TEMPLATE_ID:
3304       return cp_parser_template_id (parser, template_keyword_p,
3305                                     check_dependency_p,
3306                                     declarator_p);
3307
3308     case CPP_COMPL:
3309       {
3310         tree type_decl;
3311         tree qualifying_scope;
3312         tree object_scope;
3313         tree scope;
3314         bool done;
3315
3316         /* Consume the `~' token.  */
3317         cp_lexer_consume_token (parser->lexer);
3318         /* Parse the class-name.  The standard, as written, seems to
3319            say that:
3320
3321              template <typename T> struct S { ~S (); };
3322              template <typename T> S<T>::~S() {}
3323
3324            is invalid, since `~' must be followed by a class-name, but
3325            `S<T>' is dependent, and so not known to be a class.
3326            That's not right; we need to look in uninstantiated
3327            templates.  A further complication arises from:
3328
3329              template <typename T> void f(T t) {
3330                t.T::~T();
3331              }
3332
3333            Here, it is not possible to look up `T' in the scope of `T'
3334            itself.  We must look in both the current scope, and the
3335            scope of the containing complete expression.
3336
3337            Yet another issue is:
3338
3339              struct S {
3340                int S;
3341                ~S();
3342              };
3343
3344              S::~S() {}
3345
3346            The standard does not seem to say that the `S' in `~S'
3347            should refer to the type `S' and not the data member
3348            `S::S'.  */
3349
3350         /* DR 244 says that we look up the name after the "~" in the
3351            same scope as we looked up the qualifying name.  That idea
3352            isn't fully worked out; it's more complicated than that.  */
3353         scope = parser->scope;
3354         object_scope = parser->object_scope;
3355         qualifying_scope = parser->qualifying_scope;
3356
3357         /* If the name is of the form "X::~X" it's OK.  */
3358         if (scope && TYPE_P (scope)
3359             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3360             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3361                 == CPP_OPEN_PAREN)
3362             && (cp_lexer_peek_token (parser->lexer)->value
3363                 == TYPE_IDENTIFIER (scope)))
3364           {
3365             cp_lexer_consume_token (parser->lexer);
3366             return build_nt (BIT_NOT_EXPR, scope);
3367           }
3368
3369         /* If there was an explicit qualification (S::~T), first look
3370            in the scope given by the qualification (i.e., S).  */
3371         done = false;
3372         type_decl = NULL_TREE;
3373         if (scope)
3374           {
3375             cp_parser_parse_tentatively (parser);
3376             type_decl = cp_parser_class_name (parser,
3377                                               /*typename_keyword_p=*/false,
3378                                               /*template_keyword_p=*/false,
3379                                               none_type,
3380                                               /*check_dependency=*/false,
3381                                               /*class_head_p=*/false,
3382                                               declarator_p);
3383             if (cp_parser_parse_definitely (parser))
3384               done = true;
3385           }
3386         /* In "N::S::~S", look in "N" as well.  */
3387         if (!done && scope && qualifying_scope)
3388           {
3389             cp_parser_parse_tentatively (parser);
3390             parser->scope = qualifying_scope;
3391             parser->object_scope = NULL_TREE;
3392             parser->qualifying_scope = NULL_TREE;
3393             type_decl
3394               = cp_parser_class_name (parser,
3395                                       /*typename_keyword_p=*/false,
3396                                       /*template_keyword_p=*/false,
3397                                       none_type,
3398                                       /*check_dependency=*/false,
3399                                       /*class_head_p=*/false,
3400                                       declarator_p);
3401             if (cp_parser_parse_definitely (parser))
3402               done = true;
3403           }
3404         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3405         else if (!done && object_scope)
3406           {
3407             cp_parser_parse_tentatively (parser);
3408             parser->scope = object_scope;
3409             parser->object_scope = NULL_TREE;
3410             parser->qualifying_scope = NULL_TREE;
3411             type_decl
3412               = cp_parser_class_name (parser,
3413                                       /*typename_keyword_p=*/false,
3414                                       /*template_keyword_p=*/false,
3415                                       none_type,
3416                                       /*check_dependency=*/false,
3417                                       /*class_head_p=*/false,
3418                                       declarator_p);
3419             if (cp_parser_parse_definitely (parser))
3420               done = true;
3421           }
3422         /* Look in the surrounding context.  */
3423         if (!done)
3424           {
3425             parser->scope = NULL_TREE;
3426             parser->object_scope = NULL_TREE;
3427             parser->qualifying_scope = NULL_TREE;
3428             type_decl
3429               = cp_parser_class_name (parser,
3430                                       /*typename_keyword_p=*/false,
3431                                       /*template_keyword_p=*/false,
3432                                       none_type,
3433                                       /*check_dependency=*/false,
3434                                       /*class_head_p=*/false,
3435                                       declarator_p);
3436           }
3437         /* If an error occurred, assume that the name of the
3438            destructor is the same as the name of the qualifying
3439            class.  That allows us to keep parsing after running
3440            into ill-formed destructor names.  */
3441         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3442           return build_nt (BIT_NOT_EXPR, scope);
3443         else if (type_decl == error_mark_node)
3444           return error_mark_node;
3445
3446         /* [class.dtor]
3447
3448            A typedef-name that names a class shall not be used as the
3449            identifier in the declarator for a destructor declaration.  */
3450         if (declarator_p
3451             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3452             && !DECL_SELF_REFERENCE_P (type_decl)
3453             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3454           error ("typedef-name %qD used as destructor declarator",
3455                  type_decl);
3456
3457         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3458       }
3459
3460     case CPP_KEYWORD:
3461       if (token->keyword == RID_OPERATOR)
3462         {
3463           tree id;
3464
3465           /* This could be a template-id, so we try that first.  */
3466           cp_parser_parse_tentatively (parser);
3467           /* Try a template-id.  */
3468           id = cp_parser_template_id (parser, template_keyword_p,
3469                                       /*check_dependency_p=*/true,
3470                                       declarator_p);
3471           /* If that worked, we're done.  */
3472           if (cp_parser_parse_definitely (parser))
3473             return id;
3474           /* We still don't know whether we're looking at an
3475              operator-function-id or a conversion-function-id.  */
3476           cp_parser_parse_tentatively (parser);
3477           /* Try an operator-function-id.  */
3478           id = cp_parser_operator_function_id (parser);
3479           /* If that didn't work, try a conversion-function-id.  */
3480           if (!cp_parser_parse_definitely (parser))
3481             id = cp_parser_conversion_function_id (parser);
3482
3483           return id;
3484         }
3485       /* Fall through.  */
3486
3487     default:
3488       cp_parser_error (parser, "expected unqualified-id");
3489       return error_mark_node;
3490     }
3491 }
3492
3493 /* Parse an (optional) nested-name-specifier.
3494
3495    nested-name-specifier:
3496      class-or-namespace-name :: nested-name-specifier [opt]
3497      class-or-namespace-name :: template nested-name-specifier [opt]
3498
3499    PARSER->SCOPE should be set appropriately before this function is
3500    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3501    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3502    in name lookups.
3503
3504    Sets PARSER->SCOPE to the class (TYPE) or namespace
3505    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3506    it unchanged if there is no nested-name-specifier.  Returns the new
3507    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3508
3509    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3510    part of a declaration and/or decl-specifier.  */
3511
3512 static tree
3513 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3514                                      bool typename_keyword_p,
3515                                      bool check_dependency_p,
3516                                      bool type_p,
3517                                      bool is_declaration)
3518 {
3519   bool success = false;
3520   cp_token_position start = 0;
3521   cp_token *token;
3522
3523   /* If the next token corresponds to a nested name specifier, there
3524      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3525      false, it may have been true before, in which case something
3526      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3527      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3528      CHECK_DEPENDENCY_P is false, we have to fall through into the
3529      main loop.  */
3530   if (check_dependency_p
3531       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3532     {
3533       cp_parser_pre_parsed_nested_name_specifier (parser);
3534       return parser->scope;
3535     }
3536
3537   /* Remember where the nested-name-specifier starts.  */
3538   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3539     {
3540       start = cp_lexer_token_position (parser->lexer, false);
3541       push_deferring_access_checks (dk_deferred);
3542     }
3543
3544   while (true)
3545     {
3546       tree new_scope;
3547       tree old_scope;
3548       tree saved_qualifying_scope;
3549       bool template_keyword_p;
3550
3551       /* Spot cases that cannot be the beginning of a
3552          nested-name-specifier.  */
3553       token = cp_lexer_peek_token (parser->lexer);
3554
3555       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3556          the already parsed nested-name-specifier.  */
3557       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3558         {
3559           /* Grab the nested-name-specifier and continue the loop.  */
3560           cp_parser_pre_parsed_nested_name_specifier (parser);
3561           success = true;
3562           continue;
3563         }
3564
3565       /* Spot cases that cannot be the beginning of a
3566          nested-name-specifier.  On the second and subsequent times
3567          through the loop, we look for the `template' keyword.  */
3568       if (success && token->keyword == RID_TEMPLATE)
3569         ;
3570       /* A template-id can start a nested-name-specifier.  */
3571       else if (token->type == CPP_TEMPLATE_ID)
3572         ;
3573       else
3574         {
3575           /* If the next token is not an identifier, then it is
3576              definitely not a class-or-namespace-name.  */
3577           if (token->type != CPP_NAME)
3578             break;
3579           /* If the following token is neither a `<' (to begin a
3580              template-id), nor a `::', then we are not looking at a
3581              nested-name-specifier.  */
3582           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3583           if (token->type != CPP_SCOPE
3584               && !cp_parser_nth_token_starts_template_argument_list_p
3585                   (parser, 2))
3586             break;
3587         }
3588
3589       /* The nested-name-specifier is optional, so we parse
3590          tentatively.  */
3591       cp_parser_parse_tentatively (parser);
3592
3593       /* Look for the optional `template' keyword, if this isn't the
3594          first time through the loop.  */
3595       if (success)
3596         template_keyword_p = cp_parser_optional_template_keyword (parser);
3597       else
3598         template_keyword_p = false;
3599
3600       /* Save the old scope since the name lookup we are about to do
3601          might destroy it.  */
3602       old_scope = parser->scope;
3603       saved_qualifying_scope = parser->qualifying_scope;
3604       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3605          look up names in "X<T>::I" in order to determine that "Y" is
3606          a template.  So, if we have a typename at this point, we make
3607          an effort to look through it.  */
3608       if (is_declaration
3609           && !typename_keyword_p
3610           && parser->scope
3611           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3612         parser->scope = resolve_typename_type (parser->scope,
3613                                                /*only_current_p=*/false);
3614       /* Parse the qualifying entity.  */
3615       new_scope
3616         = cp_parser_class_or_namespace_name (parser,
3617                                              typename_keyword_p,
3618                                              template_keyword_p,
3619                                              check_dependency_p,
3620                                              type_p,
3621                                              is_declaration);
3622       /* Look for the `::' token.  */
3623       cp_parser_require (parser, CPP_SCOPE, "`::'");
3624
3625       /* If we found what we wanted, we keep going; otherwise, we're
3626          done.  */
3627       if (!cp_parser_parse_definitely (parser))
3628         {
3629           bool error_p = false;
3630
3631           /* Restore the OLD_SCOPE since it was valid before the
3632              failed attempt at finding the last
3633              class-or-namespace-name.  */
3634           parser->scope = old_scope;
3635           parser->qualifying_scope = saved_qualifying_scope;
3636           /* If the next token is an identifier, and the one after
3637              that is a `::', then any valid interpretation would have
3638              found a class-or-namespace-name.  */
3639           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3640                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3641                      == CPP_SCOPE)
3642                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3643                      != CPP_COMPL))
3644             {
3645               token = cp_lexer_consume_token (parser->lexer);
3646               if (!error_p)
3647                 {
3648                   if (!token->ambiguous_p)
3649                     {
3650                       tree decl;
3651                       tree ambiguous_decls;
3652
3653                       decl = cp_parser_lookup_name (parser, token->value,
3654                                                     none_type,
3655                                                     /*is_template=*/false,
3656                                                     /*is_namespace=*/false,
3657                                                     /*check_dependency=*/true,
3658                                                     &ambiguous_decls);
3659                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3660                         error ("%qD used without template parameters", decl);
3661                       else if (ambiguous_decls)
3662                         {
3663                           error ("reference to %qD is ambiguous", 
3664                                  token->value);
3665                           print_candidates (ambiguous_decls);
3666                           decl = error_mark_node;
3667                         }
3668                       else
3669                         cp_parser_name_lookup_error
3670                           (parser, token->value, decl,
3671                            "is not a class or namespace");
3672                     }
3673                   parser->scope = error_mark_node;
3674                   error_p = true;
3675                   /* Treat this as a successful nested-name-specifier
3676                      due to:
3677
3678                      [basic.lookup.qual]
3679
3680                      If the name found is not a class-name (clause
3681                      _class_) or namespace-name (_namespace.def_), the
3682                      program is ill-formed.  */
3683                   success = true;
3684                 }
3685               cp_lexer_consume_token (parser->lexer);
3686             }
3687           break;
3688         }
3689       /* We've found one valid nested-name-specifier.  */
3690       success = true;
3691       /* Name lookup always gives us a DECL.  */
3692       if (TREE_CODE (new_scope) == TYPE_DECL)
3693         new_scope = TREE_TYPE (new_scope);
3694       /* Uses of "template" must be followed by actual templates.  */
3695       if (template_keyword_p
3696           && !(CLASS_TYPE_P (new_scope)
3697                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3698                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3699                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3700           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3701                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3702                    == TEMPLATE_ID_EXPR)))
3703         pedwarn (TYPE_P (new_scope)
3704                  ? "%qT is not a template"
3705                  : "%qD is not a template",
3706                  new_scope);
3707       /* If it is a class scope, try to complete it; we are about to
3708          be looking up names inside the class.  */
3709       if (TYPE_P (new_scope)
3710           /* Since checking types for dependency can be expensive,
3711              avoid doing it if the type is already complete.  */
3712           && !COMPLETE_TYPE_P (new_scope)
3713           /* Do not try to complete dependent types.  */
3714           && !dependent_type_p (new_scope))
3715         new_scope = complete_type (new_scope);
3716       /* Make sure we look in the right scope the next time through
3717          the loop.  */
3718       parser->scope = new_scope;
3719     }
3720
3721   /* If parsing tentatively, replace the sequence of tokens that makes
3722      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3723      token.  That way, should we re-parse the token stream, we will
3724      not have to repeat the effort required to do the parse, nor will
3725      we issue duplicate error messages.  */
3726   if (success && start)
3727     {
3728       cp_token *token;
3729       tree access_checks;
3730
3731       token = cp_lexer_token_at (parser->lexer, start);
3732       /* Reset the contents of the START token.  */
3733       token->type = CPP_NESTED_NAME_SPECIFIER;
3734       /* Retrieve any deferred checks.  Do not pop this access checks yet
3735          so the memory will not be reclaimed during token replacing below.  */
3736       access_checks = get_deferred_access_checks ();
3737       token->value = build_tree_list (copy_list (access_checks),
3738                                       parser->scope);
3739       TREE_TYPE (token->value) = parser->qualifying_scope;
3740       token->keyword = RID_MAX;
3741
3742       /* Purge all subsequent tokens.  */
3743       cp_lexer_purge_tokens_after (parser->lexer, start);
3744     }
3745   
3746   if (start)
3747     pop_to_parent_deferring_access_checks ();
3748
3749   return success ? parser->scope : NULL_TREE;
3750 }
3751
3752 /* Parse a nested-name-specifier.  See
3753    cp_parser_nested_name_specifier_opt for details.  This function
3754    behaves identically, except that it will an issue an error if no
3755    nested-name-specifier is present.  */
3756
3757 static tree
3758 cp_parser_nested_name_specifier (cp_parser *parser,
3759                                  bool typename_keyword_p,
3760                                  bool check_dependency_p,
3761                                  bool type_p,
3762                                  bool is_declaration)
3763 {
3764   tree scope;
3765
3766   /* Look for the nested-name-specifier.  */
3767   scope = cp_parser_nested_name_specifier_opt (parser,
3768                                                typename_keyword_p,
3769                                                check_dependency_p,
3770                                                type_p,
3771                                                is_declaration);
3772   /* If it was not present, issue an error message.  */
3773   if (!scope)
3774     {
3775       cp_parser_error (parser, "expected nested-name-specifier");
3776       parser->scope = NULL_TREE;
3777     }
3778
3779   return scope;
3780 }
3781
3782 /* Parse a class-or-namespace-name.
3783
3784    class-or-namespace-name:
3785      class-name
3786      namespace-name
3787
3788    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3789    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3790    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3791    TYPE_P is TRUE iff the next name should be taken as a class-name,
3792    even the same name is declared to be another entity in the same
3793    scope.
3794
3795    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3796    specified by the class-or-namespace-name.  If neither is found the
3797    ERROR_MARK_NODE is returned.  */
3798
3799 static tree
3800 cp_parser_class_or_namespace_name (cp_parser *parser,
3801                                    bool typename_keyword_p,
3802                                    bool template_keyword_p,
3803                                    bool check_dependency_p,
3804                                    bool type_p,
3805                                    bool is_declaration)
3806 {
3807   tree saved_scope;
3808   tree saved_qualifying_scope;
3809   tree saved_object_scope;
3810   tree scope;
3811   bool only_class_p;
3812
3813   /* Before we try to parse the class-name, we must save away the
3814      current PARSER->SCOPE since cp_parser_class_name will destroy
3815      it.  */
3816   saved_scope = parser->scope;
3817   saved_qualifying_scope = parser->qualifying_scope;
3818   saved_object_scope = parser->object_scope;
3819   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3820      there is no need to look for a namespace-name.  */
3821   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3822   if (!only_class_p)
3823     cp_parser_parse_tentatively (parser);
3824   scope = cp_parser_class_name (parser,
3825                                 typename_keyword_p,
3826                                 template_keyword_p,
3827                                 type_p ? class_type : none_type,
3828                                 check_dependency_p,
3829                                 /*class_head_p=*/false,
3830                                 is_declaration);
3831   /* If that didn't work, try for a namespace-name.  */
3832   if (!only_class_p && !cp_parser_parse_definitely (parser))
3833     {
3834       /* Restore the saved scope.  */
3835       parser->scope = saved_scope;
3836       parser->qualifying_scope = saved_qualifying_scope;
3837       parser->object_scope = saved_object_scope;
3838       /* If we are not looking at an identifier followed by the scope
3839          resolution operator, then this is not part of a
3840          nested-name-specifier.  (Note that this function is only used
3841          to parse the components of a nested-name-specifier.)  */
3842       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3843           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3844         return error_mark_node;
3845       scope = cp_parser_namespace_name (parser);
3846     }
3847
3848   return scope;
3849 }
3850
3851 /* Parse a postfix-expression.
3852
3853    postfix-expression:
3854      primary-expression
3855      postfix-expression [ expression ]
3856      postfix-expression ( expression-list [opt] )
3857      simple-type-specifier ( expression-list [opt] )
3858      typename :: [opt] nested-name-specifier identifier
3859        ( expression-list [opt] )
3860      typename :: [opt] nested-name-specifier template [opt] template-id
3861        ( expression-list [opt] )
3862      postfix-expression . template [opt] id-expression
3863      postfix-expression -> template [opt] id-expression
3864      postfix-expression . pseudo-destructor-name
3865      postfix-expression -> pseudo-destructor-name
3866      postfix-expression ++
3867      postfix-expression --
3868      dynamic_cast < type-id > ( expression )
3869      static_cast < type-id > ( expression )
3870      reinterpret_cast < type-id > ( expression )
3871      const_cast < type-id > ( expression )
3872      typeid ( expression )
3873      typeid ( type-id )
3874
3875    GNU Extension:
3876
3877    postfix-expression:
3878      ( type-id ) { initializer-list , [opt] }
3879
3880    This extension is a GNU version of the C99 compound-literal
3881    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3882    but they are essentially the same concept.)
3883
3884    If ADDRESS_P is true, the postfix expression is the operand of the
3885    `&' operator.  CAST_P is true if this expression is the target of a
3886    cast.
3887
3888    Returns a representation of the expression.  */
3889
3890 static tree
3891 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3892 {
3893   cp_token *token;
3894   enum rid keyword;
3895   cp_id_kind idk = CP_ID_KIND_NONE;
3896   tree postfix_expression = NULL_TREE;
3897
3898   /* Peek at the next token.  */
3899   token = cp_lexer_peek_token (parser->lexer);
3900   /* Some of the productions are determined by keywords.  */
3901   keyword = token->keyword;
3902   switch (keyword)
3903     {
3904     case RID_DYNCAST:
3905     case RID_STATCAST:
3906     case RID_REINTCAST:
3907     case RID_CONSTCAST:
3908       {
3909         tree type;
3910         tree expression;
3911         const char *saved_message;
3912
3913         /* All of these can be handled in the same way from the point
3914            of view of parsing.  Begin by consuming the token
3915            identifying the cast.  */
3916         cp_lexer_consume_token (parser->lexer);
3917
3918         /* New types cannot be defined in the cast.  */
3919         saved_message = parser->type_definition_forbidden_message;
3920         parser->type_definition_forbidden_message
3921           = "types may not be defined in casts";
3922
3923         /* Look for the opening `<'.  */
3924         cp_parser_require (parser, CPP_LESS, "`<'");
3925         /* Parse the type to which we are casting.  */
3926         type = cp_parser_type_id (parser);
3927         /* Look for the closing `>'.  */
3928         cp_parser_require (parser, CPP_GREATER, "`>'");
3929         /* Restore the old message.  */
3930         parser->type_definition_forbidden_message = saved_message;
3931
3932         /* And the expression which is being cast.  */
3933         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3934         expression = cp_parser_expression (parser, /*cast_p=*/true);
3935         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3936
3937         /* Only type conversions to integral or enumeration types
3938            can be used in constant-expressions.  */
3939         if (parser->integral_constant_expression_p
3940             && !dependent_type_p (type)
3941             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3942             && (cp_parser_non_integral_constant_expression
3943                 (parser,
3944                  "a cast to a type other than an integral or "
3945                  "enumeration type")))
3946           return error_mark_node;
3947
3948         switch (keyword)
3949           {
3950           case RID_DYNCAST:
3951             postfix_expression
3952               = build_dynamic_cast (type, expression);
3953             break;
3954           case RID_STATCAST:
3955             postfix_expression
3956               = build_static_cast (type, expression);
3957             break;
3958           case RID_REINTCAST:
3959             postfix_expression
3960               = build_reinterpret_cast (type, expression);
3961             break;
3962           case RID_CONSTCAST:
3963             postfix_expression
3964               = build_const_cast (type, expression);
3965             break;
3966           default:
3967             gcc_unreachable ();
3968           }
3969       }
3970       break;
3971
3972     case RID_TYPEID:
3973       {
3974         tree type;
3975         const char *saved_message;
3976         bool saved_in_type_id_in_expr_p;
3977
3978         /* Consume the `typeid' token.  */
3979         cp_lexer_consume_token (parser->lexer);
3980         /* Look for the `(' token.  */
3981         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3982         /* Types cannot be defined in a `typeid' expression.  */
3983         saved_message = parser->type_definition_forbidden_message;
3984         parser->type_definition_forbidden_message
3985           = "types may not be defined in a `typeid\' expression";
3986         /* We can't be sure yet whether we're looking at a type-id or an
3987            expression.  */
3988         cp_parser_parse_tentatively (parser);
3989         /* Try a type-id first.  */
3990         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3991         parser->in_type_id_in_expr_p = true;
3992         type = cp_parser_type_id (parser);
3993         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3994         /* Look for the `)' token.  Otherwise, we can't be sure that
3995            we're not looking at an expression: consider `typeid (int
3996            (3))', for example.  */
3997         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3998         /* If all went well, simply lookup the type-id.  */
3999         if (cp_parser_parse_definitely (parser))
4000           postfix_expression = get_typeid (type);
4001         /* Otherwise, fall back to the expression variant.  */
4002         else
4003           {
4004             tree expression;
4005
4006             /* Look for an expression.  */
4007             expression = cp_parser_expression (parser, /*cast_p=*/false);
4008             /* Compute its typeid.  */
4009             postfix_expression = build_typeid (expression);
4010             /* Look for the `)' token.  */
4011             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4012           }
4013         /* `typeid' may not appear in an integral constant expression.  */
4014         if (cp_parser_non_integral_constant_expression(parser,
4015                                                        "`typeid' operator"))
4016           return error_mark_node;
4017         /* Restore the saved message.  */
4018         parser->type_definition_forbidden_message = saved_message;
4019       }
4020       break;
4021
4022     case RID_TYPENAME:
4023       {
4024         tree type;
4025         /* The syntax permitted here is the same permitted for an
4026            elaborated-type-specifier.  */
4027         type = cp_parser_elaborated_type_specifier (parser,
4028                                                     /*is_friend=*/false,
4029                                                     /*is_declaration=*/false);
4030         postfix_expression = cp_parser_functional_cast (parser, type);
4031       }
4032       break;
4033
4034     default:
4035       {
4036         tree type;
4037
4038         /* If the next thing is a simple-type-specifier, we may be
4039            looking at a functional cast.  We could also be looking at
4040            an id-expression.  So, we try the functional cast, and if
4041            that doesn't work we fall back to the primary-expression.  */
4042         cp_parser_parse_tentatively (parser);
4043         /* Look for the simple-type-specifier.  */
4044         type = cp_parser_simple_type_specifier (parser,
4045                                                 /*decl_specs=*/NULL,
4046                                                 CP_PARSER_FLAGS_NONE);
4047         /* Parse the cast itself.  */
4048         if (!cp_parser_error_occurred (parser))
4049           postfix_expression
4050             = cp_parser_functional_cast (parser, type);
4051         /* If that worked, we're done.  */
4052         if (cp_parser_parse_definitely (parser))
4053           break;
4054
4055         /* If the functional-cast didn't work out, try a
4056            compound-literal.  */
4057         if (cp_parser_allow_gnu_extensions_p (parser)
4058             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4059           {
4060             VEC(constructor_elt,gc) *initializer_list = NULL;
4061             bool saved_in_type_id_in_expr_p;
4062
4063             cp_parser_parse_tentatively (parser);
4064             /* Consume the `('.  */
4065             cp_lexer_consume_token (parser->lexer);
4066             /* Parse the type.  */
4067             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4068             parser->in_type_id_in_expr_p = true;
4069             type = cp_parser_type_id (parser);
4070             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4071             /* Look for the `)'.  */
4072             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4073             /* Look for the `{'.  */
4074             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4075             /* If things aren't going well, there's no need to
4076                keep going.  */
4077             if (!cp_parser_error_occurred (parser))
4078               {
4079                 bool non_constant_p;
4080                 /* Parse the initializer-list.  */
4081                 initializer_list
4082                   = cp_parser_initializer_list (parser, &non_constant_p);
4083                 /* Allow a trailing `,'.  */
4084                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4085                   cp_lexer_consume_token (parser->lexer);
4086                 /* Look for the final `}'.  */
4087                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4088               }
4089             /* If that worked, we're definitely looking at a
4090                compound-literal expression.  */
4091             if (cp_parser_parse_definitely (parser))
4092               {
4093                 /* Warn the user that a compound literal is not
4094                    allowed in standard C++.  */
4095                 if (pedantic)
4096                   pedwarn ("ISO C++ forbids compound-literals");
4097                 /* Form the representation of the compound-literal.  */
4098                 postfix_expression
4099                   = finish_compound_literal (type, initializer_list);
4100                 break;
4101               }
4102           }
4103
4104         /* It must be a primary-expression.  */
4105         postfix_expression 
4106           = cp_parser_primary_expression (parser, address_p, cast_p, 
4107                                           /*template_arg_p=*/false,
4108                                           &idk);
4109       }
4110       break;
4111     }
4112
4113   /* Keep looping until the postfix-expression is complete.  */
4114   while (true)
4115     {
4116       if (idk == CP_ID_KIND_UNQUALIFIED
4117           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4118           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4119         /* It is not a Koenig lookup function call.  */
4120         postfix_expression
4121           = unqualified_name_lookup_error (postfix_expression);
4122
4123       /* Peek at the next token.  */
4124       token = cp_lexer_peek_token (parser->lexer);
4125
4126       switch (token->type)
4127         {
4128         case CPP_OPEN_SQUARE:
4129           postfix_expression
4130             = cp_parser_postfix_open_square_expression (parser,
4131                                                         postfix_expression,
4132                                                         false);
4133           idk = CP_ID_KIND_NONE;
4134           break;
4135
4136         case CPP_OPEN_PAREN:
4137           /* postfix-expression ( expression-list [opt] ) */
4138           {
4139             bool koenig_p;
4140             bool is_builtin_constant_p;
4141             bool saved_integral_constant_expression_p = false;
4142             bool saved_non_integral_constant_expression_p = false;
4143             tree args;
4144
4145             is_builtin_constant_p
4146               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4147             if (is_builtin_constant_p)
4148               {
4149                 /* The whole point of __builtin_constant_p is to allow
4150                    non-constant expressions to appear as arguments.  */
4151                 saved_integral_constant_expression_p
4152                   = parser->integral_constant_expression_p;
4153                 saved_non_integral_constant_expression_p
4154                   = parser->non_integral_constant_expression_p;
4155                 parser->integral_constant_expression_p = false;
4156               }
4157             args = (cp_parser_parenthesized_expression_list
4158                     (parser, /*is_attribute_list=*/false,
4159                      /*cast_p=*/false,
4160                      /*non_constant_p=*/NULL));
4161             if (is_builtin_constant_p)
4162               {
4163                 parser->integral_constant_expression_p
4164                   = saved_integral_constant_expression_p;
4165                 parser->non_integral_constant_expression_p
4166                   = saved_non_integral_constant_expression_p;
4167               }
4168
4169             if (args == error_mark_node)
4170               {
4171                 postfix_expression = error_mark_node;
4172                 break;
4173               }
4174
4175             /* Function calls are not permitted in
4176                constant-expressions.  */
4177             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4178                 && cp_parser_non_integral_constant_expression (parser,
4179                                                                "a function call"))
4180               {
4181                 postfix_expression = error_mark_node;
4182                 break;
4183               }
4184
4185             koenig_p = false;
4186             if (idk == CP_ID_KIND_UNQUALIFIED)
4187               {
4188                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4189                   {
4190                     if (args)
4191                       {
4192                         koenig_p = true;
4193                         postfix_expression
4194                           = perform_koenig_lookup (postfix_expression, args);
4195                       }
4196                     else
4197                       postfix_expression
4198                         = unqualified_fn_lookup_error (postfix_expression);
4199                   }
4200                 /* We do not perform argument-dependent lookup if
4201                    normal lookup finds a non-function, in accordance
4202                    with the expected resolution of DR 218.  */
4203                 else if (args && is_overloaded_fn (postfix_expression))
4204                   {
4205                     tree fn = get_first_fn (postfix_expression);
4206
4207                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4208                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4209
4210                     /* Only do argument dependent lookup if regular
4211                        lookup does not find a set of member functions.
4212                        [basic.lookup.koenig]/2a  */
4213                     if (!DECL_FUNCTION_MEMBER_P (fn))
4214                       {
4215                         koenig_p = true;
4216                         postfix_expression
4217                           = perform_koenig_lookup (postfix_expression, args);
4218                       }
4219                   }
4220               }
4221
4222             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4223               {
4224                 tree instance = TREE_OPERAND (postfix_expression, 0);
4225                 tree fn = TREE_OPERAND (postfix_expression, 1);
4226
4227                 if (processing_template_decl
4228                     && (type_dependent_expression_p (instance)
4229                         || (!BASELINK_P (fn)
4230                             && TREE_CODE (fn) != FIELD_DECL)
4231                         || type_dependent_expression_p (fn)
4232                         || any_type_dependent_arguments_p (args)))
4233                   {
4234                     postfix_expression
4235                       = build_min_nt (CALL_EXPR, postfix_expression,
4236                                       args, NULL_TREE);
4237                     break;
4238                   }
4239
4240                 if (BASELINK_P (fn))
4241                   postfix_expression
4242                     = (build_new_method_call
4243                        (instance, fn, args, NULL_TREE,
4244                         (idk == CP_ID_KIND_QUALIFIED
4245                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4246                 else
4247                   postfix_expression
4248                     = finish_call_expr (postfix_expression, args,
4249                                         /*disallow_virtual=*/false,
4250                                         /*koenig_p=*/false);
4251               }
4252             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4253                      || TREE_CODE (postfix_expression) == MEMBER_REF
4254                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4255               postfix_expression = (build_offset_ref_call_from_tree
4256                                     (postfix_expression, args));
4257             else if (idk == CP_ID_KIND_QUALIFIED)
4258               /* A call to a static class member, or a namespace-scope
4259                  function.  */
4260               postfix_expression
4261                 = finish_call_expr (postfix_expression, args,
4262                                     /*disallow_virtual=*/true,
4263                                     koenig_p);
4264             else
4265               /* All other function calls.  */
4266               postfix_expression
4267                 = finish_call_expr (postfix_expression, args,
4268                                     /*disallow_virtual=*/false,
4269                                     koenig_p);
4270
4271             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4272             idk = CP_ID_KIND_NONE;
4273           }
4274           break;
4275
4276         case CPP_DOT:
4277         case CPP_DEREF:
4278           /* postfix-expression . template [opt] id-expression
4279              postfix-expression . pseudo-destructor-name
4280              postfix-expression -> template [opt] id-expression
4281              postfix-expression -> pseudo-destructor-name */
4282
4283           /* Consume the `.' or `->' operator.  */
4284           cp_lexer_consume_token (parser->lexer);
4285
4286           postfix_expression
4287             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4288                                                       postfix_expression,
4289                                                       false, &idk);
4290           break;
4291
4292         case CPP_PLUS_PLUS:
4293           /* postfix-expression ++  */
4294           /* Consume the `++' token.  */
4295           cp_lexer_consume_token (parser->lexer);
4296           /* Generate a representation for the complete expression.  */
4297           postfix_expression
4298             = finish_increment_expr (postfix_expression,
4299                                      POSTINCREMENT_EXPR);
4300           /* Increments may not appear in constant-expressions.  */
4301           if (cp_parser_non_integral_constant_expression (parser,
4302                                                           "an increment"))
4303             postfix_expression = error_mark_node;
4304           idk = CP_ID_KIND_NONE;
4305           break;
4306
4307         case CPP_MINUS_MINUS:
4308           /* postfix-expression -- */
4309           /* Consume the `--' token.  */
4310           cp_lexer_consume_token (parser->lexer);
4311           /* Generate a representation for the complete expression.  */
4312           postfix_expression
4313             = finish_increment_expr (postfix_expression,
4314                                      POSTDECREMENT_EXPR);
4315           /* Decrements may not appear in constant-expressions.  */
4316           if (cp_parser_non_integral_constant_expression (parser,
4317                                                           "a decrement"))
4318             postfix_expression = error_mark_node;
4319           idk = CP_ID_KIND_NONE;
4320           break;
4321
4322         default:
4323           return postfix_expression;
4324         }
4325     }
4326
4327   /* We should never get here.  */
4328   gcc_unreachable ();
4329   return error_mark_node;
4330 }
4331
4332 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4333    by cp_parser_builtin_offsetof.  We're looking for
4334
4335      postfix-expression [ expression ]
4336
4337    FOR_OFFSETOF is set if we're being called in that context, which
4338    changes how we deal with integer constant expressions.  */
4339
4340 static tree
4341 cp_parser_postfix_open_square_expression (cp_parser *parser,
4342                                           tree postfix_expression,
4343                                           bool for_offsetof)
4344 {
4345   tree index;
4346
4347   /* Consume the `[' token.  */
4348   cp_lexer_consume_token (parser->lexer);
4349
4350   /* Parse the index expression.  */
4351   /* ??? For offsetof, there is a question of what to allow here.  If
4352      offsetof is not being used in an integral constant expression context,
4353      then we *could* get the right answer by computing the value at runtime.
4354      If we are in an integral constant expression context, then we might
4355      could accept any constant expression; hard to say without analysis.
4356      Rather than open the barn door too wide right away, allow only integer
4357      constant expressions here.  */
4358   if (for_offsetof)
4359     index = cp_parser_constant_expression (parser, false, NULL);
4360   else
4361     index = cp_parser_expression (parser, /*cast_p=*/false);
4362
4363   /* Look for the closing `]'.  */
4364   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4365
4366   /* Build the ARRAY_REF.  */
4367   postfix_expression = grok_array_decl (postfix_expression, index);
4368
4369   /* When not doing offsetof, array references are not permitted in
4370      constant-expressions.  */
4371   if (!for_offsetof
4372       && (cp_parser_non_integral_constant_expression
4373           (parser, "an array reference")))
4374     postfix_expression = error_mark_node;
4375
4376   return postfix_expression;
4377 }
4378
4379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4380    by cp_parser_builtin_offsetof.  We're looking for
4381
4382      postfix-expression . template [opt] id-expression
4383      postfix-expression . pseudo-destructor-name
4384      postfix-expression -> template [opt] id-expression
4385      postfix-expression -> pseudo-destructor-name
4386
4387    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4388    limits what of the above we'll actually accept, but nevermind.
4389    TOKEN_TYPE is the "." or "->" token, which will already have been
4390    removed from the stream.  */
4391
4392 static tree
4393 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4394                                         enum cpp_ttype token_type,
4395                                         tree postfix_expression,
4396                                         bool for_offsetof, cp_id_kind *idk)
4397 {
4398   tree name;
4399   bool dependent_p;
4400   bool pseudo_destructor_p;
4401   tree scope = NULL_TREE;
4402
4403   /* If this is a `->' operator, dereference the pointer.  */
4404   if (token_type == CPP_DEREF)
4405     postfix_expression = build_x_arrow (postfix_expression);
4406   /* Check to see whether or not the expression is type-dependent.  */
4407   dependent_p = type_dependent_expression_p (postfix_expression);
4408   /* The identifier following the `->' or `.' is not qualified.  */
4409   parser->scope = NULL_TREE;
4410   parser->qualifying_scope = NULL_TREE;
4411   parser->object_scope = NULL_TREE;
4412   *idk = CP_ID_KIND_NONE;
4413   /* Enter the scope corresponding to the type of the object
4414      given by the POSTFIX_EXPRESSION.  */
4415   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4416     {
4417       scope = TREE_TYPE (postfix_expression);
4418       /* According to the standard, no expression should ever have
4419          reference type.  Unfortunately, we do not currently match
4420          the standard in this respect in that our internal representation
4421          of an expression may have reference type even when the standard
4422          says it does not.  Therefore, we have to manually obtain the
4423          underlying type here.  */
4424       scope = non_reference (scope);
4425       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4426       if (scope == unknown_type_node)
4427         {
4428           error ("%qE does not have class type", postfix_expression);
4429           scope = NULL_TREE;
4430         }
4431       else
4432         scope = complete_type_or_else (scope, NULL_TREE);
4433       /* Let the name lookup machinery know that we are processing a
4434          class member access expression.  */
4435       parser->context->object_type = scope;
4436       /* If something went wrong, we want to be able to discern that case,
4437          as opposed to the case where there was no SCOPE due to the type
4438          of expression being dependent.  */
4439       if (!scope)
4440         scope = error_mark_node;
4441       /* If the SCOPE was erroneous, make the various semantic analysis
4442          functions exit quickly -- and without issuing additional error
4443          messages.  */
4444       if (scope == error_mark_node)
4445         postfix_expression = error_mark_node;
4446     }
4447
4448   /* Assume this expression is not a pseudo-destructor access.  */
4449   pseudo_destructor_p = false;
4450
4451   /* If the SCOPE is a scalar type, then, if this is a valid program,
4452      we must be looking at a pseudo-destructor-name.  */
4453   if (scope && SCALAR_TYPE_P (scope))
4454     {
4455       tree s;
4456       tree type;
4457
4458       cp_parser_parse_tentatively (parser);
4459       /* Parse the pseudo-destructor-name.  */
4460       s = NULL_TREE;
4461       cp_parser_pseudo_destructor_name (parser, &s, &type);
4462       if (cp_parser_parse_definitely (parser))
4463         {
4464           pseudo_destructor_p = true;
4465           postfix_expression
4466             = finish_pseudo_destructor_expr (postfix_expression,
4467                                              s, TREE_TYPE (type));
4468         }
4469     }
4470
4471   if (!pseudo_destructor_p)
4472     {
4473       /* If the SCOPE is not a scalar type, we are looking at an
4474          ordinary class member access expression, rather than a
4475          pseudo-destructor-name.  */
4476       bool template_p;
4477       /* Parse the id-expression.  */
4478       name = (cp_parser_id_expression 
4479               (parser, 
4480                cp_parser_optional_template_keyword (parser),
4481                /*check_dependency_p=*/true,
4482                &template_p,
4483                /*declarator_p=*/false));
4484       /* In general, build a SCOPE_REF if the member name is qualified.
4485          However, if the name was not dependent and has already been
4486          resolved; there is no need to build the SCOPE_REF.  For example;
4487
4488              struct X { void f(); };
4489              template <typename T> void f(T* t) { t->X::f(); }
4490
4491          Even though "t" is dependent, "X::f" is not and has been resolved
4492          to a BASELINK; there is no need to include scope information.  */
4493
4494       /* But we do need to remember that there was an explicit scope for
4495          virtual function calls.  */
4496       if (parser->scope)
4497         *idk = CP_ID_KIND_QUALIFIED;
4498
4499       /* If the name is a template-id that names a type, we will get a
4500          TYPE_DECL here.  That is invalid code.  */
4501       if (TREE_CODE (name) == TYPE_DECL)
4502         {
4503           error ("invalid use of %qD", name);
4504           postfix_expression = error_mark_node;
4505         }
4506       else
4507         {
4508           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4509             {
4510               name = build_qualified_name (/*type=*/NULL_TREE,
4511                                            parser->scope,
4512                                            name,
4513                                            template_p);
4514               parser->scope = NULL_TREE;
4515               parser->qualifying_scope = NULL_TREE;
4516               parser->object_scope = NULL_TREE;
4517             }
4518           if (scope && name && BASELINK_P (name))
4519             adjust_result_of_qualified_name_lookup
4520               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4521           postfix_expression
4522             = finish_class_member_access_expr (postfix_expression, name,
4523                                                template_p);
4524         }
4525     }
4526
4527   /* We no longer need to look up names in the scope of the object on
4528      the left-hand side of the `.' or `->' operator.  */
4529   parser->context->object_type = NULL_TREE;
4530
4531   /* Outside of offsetof, these operators may not appear in
4532      constant-expressions.  */
4533   if (!for_offsetof
4534       && (cp_parser_non_integral_constant_expression
4535           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4536     postfix_expression = error_mark_node;
4537
4538   return postfix_expression;
4539 }
4540
4541 /* Parse a parenthesized expression-list.
4542
4543    expression-list:
4544      assignment-expression
4545      expression-list, assignment-expression
4546
4547    attribute-list:
4548      expression-list
4549      identifier
4550      identifier, expression-list
4551
4552    CAST_P is true if this expression is the target of a cast.
4553
4554    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4555    representation of an assignment-expression.  Note that a TREE_LIST
4556    is returned even if there is only a single expression in the list.
4557    error_mark_node is returned if the ( and or ) are
4558    missing. NULL_TREE is returned on no expressions. The parentheses
4559    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4560    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4561    indicates whether or not all of the expressions in the list were
4562    constant.  */
4563
4564 static tree
4565 cp_parser_parenthesized_expression_list (cp_parser* parser,
4566                                          bool is_attribute_list,
4567                                          bool cast_p,
4568                                          bool *non_constant_p)
4569 {
4570   tree expression_list = NULL_TREE;
4571   bool fold_expr_p = is_attribute_list;
4572   tree identifier = NULL_TREE;
4573
4574   /* Assume all the expressions will be constant.  */
4575   if (non_constant_p)
4576     *non_constant_p = false;
4577
4578   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4579     return error_mark_node;
4580
4581   /* Consume expressions until there are no more.  */
4582   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4583     while (true)
4584       {
4585         tree expr;
4586
4587         /* At the beginning of attribute lists, check to see if the
4588            next token is an identifier.  */
4589         if (is_attribute_list
4590             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4591           {
4592             cp_token *token;
4593
4594             /* Consume the identifier.  */
4595             token = cp_lexer_consume_token (parser->lexer);
4596             /* Save the identifier.  */
4597             identifier = token->value;
4598           }
4599         else
4600           {
4601             /* Parse the next assignment-expression.  */
4602             if (non_constant_p)
4603               {
4604                 bool expr_non_constant_p;
4605                 expr = (cp_parser_constant_expression
4606                         (parser, /*allow_non_constant_p=*/true,
4607                          &expr_non_constant_p));
4608                 if (expr_non_constant_p)
4609                   *non_constant_p = true;
4610               }
4611             else
4612               expr = cp_parser_assignment_expression (parser, cast_p);
4613
4614             if (fold_expr_p)
4615               expr = fold_non_dependent_expr (expr);
4616
4617              /* Add it to the list.  We add error_mark_node
4618                 expressions to the list, so that we can still tell if
4619                 the correct form for a parenthesized expression-list
4620                 is found. That gives better errors.  */
4621             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4622
4623             if (expr == error_mark_node)
4624               goto skip_comma;
4625           }
4626
4627         /* After the first item, attribute lists look the same as
4628            expression lists.  */
4629         is_attribute_list = false;
4630
4631       get_comma:;
4632         /* If the next token isn't a `,', then we are done.  */
4633         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4634           break;
4635
4636         /* Otherwise, consume the `,' and keep going.  */
4637         cp_lexer_consume_token (parser->lexer);
4638       }
4639
4640   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4641     {
4642       int ending;
4643
4644     skip_comma:;
4645       /* We try and resync to an unnested comma, as that will give the
4646          user better diagnostics.  */
4647       ending = cp_parser_skip_to_closing_parenthesis (parser,
4648                                                       /*recovering=*/true,
4649                                                       /*or_comma=*/true,
4650                                                       /*consume_paren=*/true);
4651       if (ending < 0)
4652         goto get_comma;
4653       if (!ending)
4654         return error_mark_node;
4655     }
4656
4657   /* We built up the list in reverse order so we must reverse it now.  */
4658   expression_list = nreverse (expression_list);
4659   if (identifier)
4660     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4661
4662   return expression_list;
4663 }
4664
4665 /* Parse a pseudo-destructor-name.
4666
4667    pseudo-destructor-name:
4668      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4669      :: [opt] nested-name-specifier template template-id :: ~ type-name
4670      :: [opt] nested-name-specifier [opt] ~ type-name
4671
4672    If either of the first two productions is used, sets *SCOPE to the
4673    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4674    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4675    or ERROR_MARK_NODE if the parse fails.  */
4676
4677 static void
4678 cp_parser_pseudo_destructor_name (cp_parser* parser,
4679                                   tree* scope,
4680                                   tree* type)
4681 {
4682   bool nested_name_specifier_p;
4683
4684   /* Assume that things will not work out.  */
4685   *type = error_mark_node;
4686
4687   /* Look for the optional `::' operator.  */
4688   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4689   /* Look for the optional nested-name-specifier.  */
4690   nested_name_specifier_p
4691     = (cp_parser_nested_name_specifier_opt (parser,
4692                                             /*typename_keyword_p=*/false,
4693                                             /*check_dependency_p=*/true,
4694                                             /*type_p=*/false,
4695                                             /*is_declaration=*/true)
4696        != NULL_TREE);
4697   /* Now, if we saw a nested-name-specifier, we might be doing the
4698      second production.  */
4699   if (nested_name_specifier_p
4700       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4701     {
4702       /* Consume the `template' keyword.  */
4703       cp_lexer_consume_token (parser->lexer);
4704       /* Parse the template-id.  */
4705       cp_parser_template_id (parser,
4706                              /*template_keyword_p=*/true,
4707                              /*check_dependency_p=*/false,
4708                              /*is_declaration=*/true);
4709       /* Look for the `::' token.  */
4710       cp_parser_require (parser, CPP_SCOPE, "`::'");
4711     }
4712   /* If the next token is not a `~', then there might be some
4713      additional qualification.  */
4714   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4715     {
4716       /* Look for the type-name.  */
4717       *scope = TREE_TYPE (cp_parser_type_name (parser));
4718
4719       if (*scope == error_mark_node)
4720         return;
4721
4722       /* If we don't have ::~, then something has gone wrong.  Since
4723          the only caller of this function is looking for something
4724          after `.' or `->' after a scalar type, most likely the
4725          program is trying to get a member of a non-aggregate
4726          type.  */
4727       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4728           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4729         {
4730           cp_parser_error (parser, "request for member of non-aggregate type");
4731           return;
4732         }
4733
4734       /* Look for the `::' token.  */
4735       cp_parser_require (parser, CPP_SCOPE, "`::'");
4736     }
4737   else
4738     *scope = NULL_TREE;
4739
4740   /* Look for the `~'.  */
4741   cp_parser_require (parser, CPP_COMPL, "`~'");
4742   /* Look for the type-name again.  We are not responsible for
4743      checking that it matches the first type-name.  */
4744   *type = cp_parser_type_name (parser);
4745 }
4746
4747 /* Parse a unary-expression.
4748
4749    unary-expression:
4750      postfix-expression
4751      ++ cast-expression
4752      -- cast-expression
4753      unary-operator cast-expression
4754      sizeof unary-expression
4755      sizeof ( type-id )
4756      new-expression
4757      delete-expression
4758
4759    GNU Extensions:
4760
4761    unary-expression:
4762      __extension__ cast-expression
4763      __alignof__ unary-expression
4764      __alignof__ ( type-id )
4765      __real__ cast-expression
4766      __imag__ cast-expression
4767      && identifier
4768
4769    ADDRESS_P is true iff the unary-expression is appearing as the
4770    operand of the `&' operator.   CAST_P is true if this expression is
4771    the target of a cast.
4772
4773    Returns a representation of the expression.  */
4774
4775 static tree
4776 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4777 {
4778   cp_token *token;
4779   enum tree_code unary_operator;
4780
4781   /* Peek at the next token.  */
4782   token = cp_lexer_peek_token (parser->lexer);
4783   /* Some keywords give away the kind of expression.  */
4784   if (token->type == CPP_KEYWORD)
4785     {
4786       enum rid keyword = token->keyword;
4787
4788       switch (keyword)
4789         {
4790         case RID_ALIGNOF:
4791         case RID_SIZEOF:
4792           {
4793             tree operand;
4794             enum tree_code op;
4795
4796             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4797             /* Consume the token.  */
4798             cp_lexer_consume_token (parser->lexer);
4799             /* Parse the operand.  */
4800             operand = cp_parser_sizeof_operand (parser, keyword);
4801
4802             if (TYPE_P (operand))
4803               return cxx_sizeof_or_alignof_type (operand, op, true);
4804             else
4805               return cxx_sizeof_or_alignof_expr (operand, op);
4806           }
4807
4808         case RID_NEW:
4809           return cp_parser_new_expression (parser);
4810
4811         case RID_DELETE:
4812           return cp_parser_delete_expression (parser);
4813
4814         case RID_EXTENSION:
4815           {
4816             /* The saved value of the PEDANTIC flag.  */
4817             int saved_pedantic;
4818             tree expr;
4819
4820             /* Save away the PEDANTIC flag.  */
4821             cp_parser_extension_opt (parser, &saved_pedantic);
4822             /* Parse the cast-expression.  */
4823             expr = cp_parser_simple_cast_expression (parser);
4824             /* Restore the PEDANTIC flag.  */
4825             pedantic = saved_pedantic;
4826
4827             return expr;
4828           }
4829
4830         case RID_REALPART:
4831         case RID_IMAGPART:
4832           {
4833             tree expression;
4834
4835             /* Consume the `__real__' or `__imag__' token.  */
4836             cp_lexer_consume_token (parser->lexer);
4837             /* Parse the cast-expression.  */
4838             expression = cp_parser_simple_cast_expression (parser);
4839             /* Create the complete representation.  */
4840             return build_x_unary_op ((keyword == RID_REALPART
4841                                       ? REALPART_EXPR : IMAGPART_EXPR),
4842                                      expression);
4843           }
4844           break;
4845
4846         default:
4847           break;
4848         }
4849     }
4850
4851   /* Look for the `:: new' and `:: delete', which also signal the
4852      beginning of a new-expression, or delete-expression,
4853      respectively.  If the next token is `::', then it might be one of
4854      these.  */
4855   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4856     {
4857       enum rid keyword;
4858
4859       /* See if the token after the `::' is one of the keywords in
4860          which we're interested.  */
4861       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4862       /* If it's `new', we have a new-expression.  */
4863       if (keyword == RID_NEW)
4864         return cp_parser_new_expression (parser);
4865       /* Similarly, for `delete'.  */
4866       else if (keyword == RID_DELETE)
4867         return cp_parser_delete_expression (parser);
4868     }
4869
4870   /* Look for a unary operator.  */
4871   unary_operator = cp_parser_unary_operator (token);
4872   /* The `++' and `--' operators can be handled similarly, even though
4873      they are not technically unary-operators in the grammar.  */
4874   if (unary_operator == ERROR_MARK)
4875     {
4876       if (token->type == CPP_PLUS_PLUS)
4877         unary_operator = PREINCREMENT_EXPR;
4878       else if (token->type == CPP_MINUS_MINUS)
4879         unary_operator = PREDECREMENT_EXPR;
4880       /* Handle the GNU address-of-label extension.  */
4881       else if (cp_parser_allow_gnu_extensions_p (parser)
4882                && token->type == CPP_AND_AND)
4883         {
4884           tree identifier;
4885
4886           /* Consume the '&&' token.  */
4887           cp_lexer_consume_token (parser->lexer);
4888           /* Look for the identifier.  */
4889           identifier = cp_parser_identifier (parser);
4890           /* Create an expression representing the address.  */
4891           return finish_label_address_expr (identifier);
4892         }
4893     }
4894   if (unary_operator != ERROR_MARK)
4895     {
4896       tree cast_expression;
4897       tree expression = error_mark_node;
4898       const char *non_constant_p = NULL;
4899
4900       /* Consume the operator token.  */
4901       token = cp_lexer_consume_token (parser->lexer);
4902       /* Parse the cast-expression.  */
4903       cast_expression
4904         = cp_parser_cast_expression (parser,
4905                                      unary_operator == ADDR_EXPR,
4906                                      /*cast_p=*/false);
4907       /* Now, build an appropriate representation.  */
4908       switch (unary_operator)
4909         {
4910         case INDIRECT_REF:
4911           non_constant_p = "`*'";
4912           expression = build_x_indirect_ref (cast_expression, "unary *");
4913           break;
4914
4915         case ADDR_EXPR:
4916           non_constant_p = "`&'";
4917           /* Fall through.  */
4918         case BIT_NOT_EXPR:
4919           expression = build_x_unary_op (unary_operator, cast_expression);
4920           break;
4921
4922         case PREINCREMENT_EXPR:
4923         case PREDECREMENT_EXPR:
4924           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4925                             ? "`++'" : "`--'");
4926           /* Fall through.  */
4927         case UNARY_PLUS_EXPR:
4928         case NEGATE_EXPR:
4929         case TRUTH_NOT_EXPR:
4930           expression = finish_unary_op_expr (unary_operator, cast_expression);
4931           break;
4932
4933         default:
4934           gcc_unreachable ();
4935         }
4936
4937       if (non_constant_p
4938           && cp_parser_non_integral_constant_expression (parser,
4939                                                          non_constant_p))
4940         expression = error_mark_node;
4941
4942       return expression;
4943     }
4944
4945   return cp_parser_postfix_expression (parser, address_p, cast_p);
4946 }
4947
4948 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4949    unary-operator, the corresponding tree code is returned.  */
4950
4951 static enum tree_code
4952 cp_parser_unary_operator (cp_token* token)
4953 {
4954   switch (token->type)
4955     {
4956     case CPP_MULT:
4957       return INDIRECT_REF;
4958
4959     case CPP_AND:
4960       return ADDR_EXPR;
4961
4962     case CPP_PLUS:
4963       return UNARY_PLUS_EXPR;
4964
4965     case CPP_MINUS:
4966       return NEGATE_EXPR;
4967
4968     case CPP_NOT:
4969       return TRUTH_NOT_EXPR;
4970
4971     case CPP_COMPL:
4972       return BIT_NOT_EXPR;
4973
4974     default:
4975       return ERROR_MARK;
4976     }
4977 }
4978
4979 /* Parse a new-expression.
4980
4981    new-expression:
4982      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4983      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4984
4985    Returns a representation of the expression.  */
4986
4987 static tree
4988 cp_parser_new_expression (cp_parser* parser)
4989 {
4990   bool global_scope_p;
4991   tree placement;
4992   tree type;
4993   tree initializer;
4994   tree nelts;
4995
4996   /* Look for the optional `::' operator.  */
4997   global_scope_p
4998     = (cp_parser_global_scope_opt (parser,
4999                                    /*current_scope_valid_p=*/false)
5000        != NULL_TREE);
5001   /* Look for the `new' operator.  */
5002   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5003   /* There's no easy way to tell a new-placement from the
5004      `( type-id )' construct.  */
5005   cp_parser_parse_tentatively (parser);
5006   /* Look for a new-placement.  */
5007   placement = cp_parser_new_placement (parser);
5008   /* If that didn't work out, there's no new-placement.  */
5009   if (!cp_parser_parse_definitely (parser))
5010     placement = NULL_TREE;
5011
5012   /* If the next token is a `(', then we have a parenthesized
5013      type-id.  */
5014   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5015     {
5016       /* Consume the `('.  */
5017       cp_lexer_consume_token (parser->lexer);
5018       /* Parse the type-id.  */
5019       type = cp_parser_type_id (parser);
5020       /* Look for the closing `)'.  */
5021       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5022       /* There should not be a direct-new-declarator in this production,
5023          but GCC used to allowed this, so we check and emit a sensible error
5024          message for this case.  */
5025       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5026         {
5027           error ("array bound forbidden after parenthesized type-id");
5028           inform ("try removing the parentheses around the type-id");
5029           cp_parser_direct_new_declarator (parser);
5030         }
5031       nelts = NULL_TREE;
5032     }
5033   /* Otherwise, there must be a new-type-id.  */
5034   else
5035     type = cp_parser_new_type_id (parser, &nelts);
5036
5037   /* If the next token is a `(', then we have a new-initializer.  */
5038   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5039     initializer = cp_parser_new_initializer (parser);
5040   else
5041     initializer = NULL_TREE;
5042
5043   /* A new-expression may not appear in an integral constant
5044      expression.  */
5045   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5046     return error_mark_node;
5047
5048   /* Create a representation of the new-expression.  */
5049   return build_new (placement, type, nelts, initializer, global_scope_p);
5050 }
5051
5052 /* Parse a new-placement.
5053
5054    new-placement:
5055      ( expression-list )
5056
5057    Returns the same representation as for an expression-list.  */
5058
5059 static tree
5060 cp_parser_new_placement (cp_parser* parser)
5061 {
5062   tree expression_list;
5063
5064   /* Parse the expression-list.  */
5065   expression_list = (cp_parser_parenthesized_expression_list
5066                      (parser, false, /*cast_p=*/false,
5067                       /*non_constant_p=*/NULL));
5068
5069   return expression_list;
5070 }
5071
5072 /* Parse a new-type-id.
5073
5074    new-type-id:
5075      type-specifier-seq new-declarator [opt]
5076
5077    Returns the TYPE allocated.  If the new-type-id indicates an array
5078    type, *NELTS is set to the number of elements in the last array
5079    bound; the TYPE will not include the last array bound.  */
5080
5081 static tree
5082 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5083 {
5084   cp_decl_specifier_seq type_specifier_seq;
5085   cp_declarator *new_declarator;
5086   cp_declarator *declarator;
5087   cp_declarator *outer_declarator;
5088   const char *saved_message;
5089   tree type;
5090
5091   /* The type-specifier sequence must not contain type definitions.
5092      (It cannot contain declarations of new types either, but if they
5093      are not definitions we will catch that because they are not
5094      complete.)  */
5095   saved_message = parser->type_definition_forbidden_message;
5096   parser->type_definition_forbidden_message
5097     = "types may not be defined in a new-type-id";
5098   /* Parse the type-specifier-seq.  */
5099   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5100                                 &type_specifier_seq);
5101   /* Restore the old message.  */
5102   parser->type_definition_forbidden_message = saved_message;
5103   /* Parse the new-declarator.  */
5104   new_declarator = cp_parser_new_declarator_opt (parser);
5105
5106   /* Determine the number of elements in the last array dimension, if
5107      any.  */
5108   *nelts = NULL_TREE;
5109   /* Skip down to the last array dimension.  */
5110   declarator = new_declarator;
5111   outer_declarator = NULL;
5112   while (declarator && (declarator->kind == cdk_pointer
5113                         || declarator->kind == cdk_ptrmem))
5114     {
5115       outer_declarator = declarator;
5116       declarator = declarator->declarator;
5117     }
5118   while (declarator
5119          && declarator->kind == cdk_array
5120          && declarator->declarator
5121          && declarator->declarator->kind == cdk_array)
5122     {
5123       outer_declarator = declarator;
5124       declarator = declarator->declarator;
5125     }
5126
5127   if (declarator && declarator->kind == cdk_array)
5128     {
5129       *nelts = declarator->u.array.bounds;
5130       if (*nelts == error_mark_node)
5131         *nelts = integer_one_node;
5132
5133       if (outer_declarator)
5134         outer_declarator->declarator = declarator->declarator;
5135       else
5136         new_declarator = NULL;
5137     }
5138
5139   type = groktypename (&type_specifier_seq, new_declarator);
5140   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5141     {
5142       *nelts = array_type_nelts_top (type);
5143       type = TREE_TYPE (type);
5144     }
5145   return type;
5146 }
5147
5148 /* Parse an (optional) new-declarator.
5149
5150    new-declarator:
5151      ptr-operator new-declarator [opt]
5152      direct-new-declarator
5153
5154    Returns the declarator.  */
5155
5156 static cp_declarator *
5157 cp_parser_new_declarator_opt (cp_parser* parser)
5158 {
5159   enum tree_code code;
5160   tree type;
5161   cp_cv_quals cv_quals;
5162
5163   /* We don't know if there's a ptr-operator next, or not.  */
5164   cp_parser_parse_tentatively (parser);
5165   /* Look for a ptr-operator.  */
5166   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5167   /* If that worked, look for more new-declarators.  */
5168   if (cp_parser_parse_definitely (parser))
5169     {
5170       cp_declarator *declarator;
5171
5172       /* Parse another optional declarator.  */
5173       declarator = cp_parser_new_declarator_opt (parser);
5174
5175       /* Create the representation of the declarator.  */
5176       if (type)
5177         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5178       else if (code == INDIRECT_REF)
5179         declarator = make_pointer_declarator (cv_quals, declarator);
5180       else
5181         declarator = make_reference_declarator (cv_quals, declarator);
5182
5183       return declarator;
5184     }
5185
5186   /* If the next token is a `[', there is a direct-new-declarator.  */
5187   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5188     return cp_parser_direct_new_declarator (parser);
5189
5190   return NULL;
5191 }
5192
5193 /* Parse a direct-new-declarator.
5194
5195    direct-new-declarator:
5196      [ expression ]
5197      direct-new-declarator [constant-expression]
5198
5199    */
5200
5201 static cp_declarator *
5202 cp_parser_direct_new_declarator (cp_parser* parser)
5203 {
5204   cp_declarator *declarator = NULL;
5205
5206   while (true)
5207     {
5208       tree expression;
5209
5210       /* Look for the opening `['.  */
5211       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5212       /* The first expression is not required to be constant.  */
5213       if (!declarator)
5214         {
5215           expression = cp_parser_expression (parser, /*cast_p=*/false);
5216           /* The standard requires that the expression have integral
5217              type.  DR 74 adds enumeration types.  We believe that the
5218              real intent is that these expressions be handled like the
5219              expression in a `switch' condition, which also allows
5220              classes with a single conversion to integral or
5221              enumeration type.  */
5222           if (!processing_template_decl)
5223             {
5224               expression
5225                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5226                                               expression,
5227                                               /*complain=*/true);
5228               if (!expression)
5229                 {
5230                   error ("expression in new-declarator must have integral "
5231                          "or enumeration type");
5232                   expression = error_mark_node;
5233                 }
5234             }
5235         }
5236       /* But all the other expressions must be.  */
5237       else
5238         expression
5239           = cp_parser_constant_expression (parser,
5240                                            /*allow_non_constant=*/false,
5241                                            NULL);
5242       /* Look for the closing `]'.  */
5243       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5244
5245       /* Add this bound to the declarator.  */
5246       declarator = make_array_declarator (declarator, expression);
5247
5248       /* If the next token is not a `[', then there are no more
5249          bounds.  */
5250       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5251         break;
5252     }
5253
5254   return declarator;
5255 }
5256
5257 /* Parse a new-initializer.
5258
5259    new-initializer:
5260      ( expression-list [opt] )
5261
5262    Returns a representation of the expression-list.  If there is no
5263    expression-list, VOID_ZERO_NODE is returned.  */
5264
5265 static tree
5266 cp_parser_new_initializer (cp_parser* parser)
5267 {
5268   tree expression_list;
5269
5270   expression_list = (cp_parser_parenthesized_expression_list
5271                      (parser, false, /*cast_p=*/false,
5272                       /*non_constant_p=*/NULL));
5273   if (!expression_list)
5274     expression_list = void_zero_node;
5275
5276   return expression_list;
5277 }
5278
5279 /* Parse a delete-expression.
5280
5281    delete-expression:
5282      :: [opt] delete cast-expression
5283      :: [opt] delete [ ] cast-expression
5284
5285    Returns a representation of the expression.  */
5286
5287 static tree
5288 cp_parser_delete_expression (cp_parser* parser)
5289 {
5290   bool global_scope_p;
5291   bool array_p;
5292   tree expression;
5293
5294   /* Look for the optional `::' operator.  */
5295   global_scope_p
5296     = (cp_parser_global_scope_opt (parser,
5297                                    /*current_scope_valid_p=*/false)
5298        != NULL_TREE);
5299   /* Look for the `delete' keyword.  */
5300   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5301   /* See if the array syntax is in use.  */
5302   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5303     {
5304       /* Consume the `[' token.  */
5305       cp_lexer_consume_token (parser->lexer);
5306       /* Look for the `]' token.  */
5307       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5308       /* Remember that this is the `[]' construct.  */
5309       array_p = true;
5310     }
5311   else
5312     array_p = false;
5313
5314   /* Parse the cast-expression.  */
5315   expression = cp_parser_simple_cast_expression (parser);
5316
5317   /* A delete-expression may not appear in an integral constant
5318      expression.  */
5319   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5320     return error_mark_node;
5321
5322   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5323 }
5324
5325 /* Parse a cast-expression.
5326
5327    cast-expression:
5328      unary-expression
5329      ( type-id ) cast-expression
5330
5331    ADDRESS_P is true iff the unary-expression is appearing as the
5332    operand of the `&' operator.   CAST_P is true if this expression is
5333    the target of a cast.
5334
5335    Returns a representation of the expression.  */
5336
5337 static tree
5338 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5339 {
5340   /* If it's a `(', then we might be looking at a cast.  */
5341   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5342     {
5343       tree type = NULL_TREE;
5344       tree expr = NULL_TREE;
5345       bool compound_literal_p;
5346       const char *saved_message;
5347
5348       /* There's no way to know yet whether or not this is a cast.
5349          For example, `(int (3))' is a unary-expression, while `(int)
5350          3' is a cast.  So, we resort to parsing tentatively.  */
5351       cp_parser_parse_tentatively (parser);
5352       /* Types may not be defined in a cast.  */
5353       saved_message = parser->type_definition_forbidden_message;
5354       parser->type_definition_forbidden_message
5355         = "types may not be defined in casts";
5356       /* Consume the `('.  */
5357       cp_lexer_consume_token (parser->lexer);
5358       /* A very tricky bit is that `(struct S) { 3 }' is a
5359          compound-literal (which we permit in C++ as an extension).
5360          But, that construct is not a cast-expression -- it is a
5361          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5362          is legal; if the compound-literal were a cast-expression,
5363          you'd need an extra set of parentheses.)  But, if we parse
5364          the type-id, and it happens to be a class-specifier, then we
5365          will commit to the parse at that point, because we cannot
5366          undo the action that is done when creating a new class.  So,
5367          then we cannot back up and do a postfix-expression.
5368
5369          Therefore, we scan ahead to the closing `)', and check to see
5370          if the token after the `)' is a `{'.  If so, we are not
5371          looking at a cast-expression.
5372
5373          Save tokens so that we can put them back.  */
5374       cp_lexer_save_tokens (parser->lexer);
5375       /* Skip tokens until the next token is a closing parenthesis.
5376          If we find the closing `)', and the next token is a `{', then
5377          we are looking at a compound-literal.  */
5378       compound_literal_p
5379         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5380                                                   /*consume_paren=*/true)
5381            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5382       /* Roll back the tokens we skipped.  */
5383       cp_lexer_rollback_tokens (parser->lexer);
5384       /* If we were looking at a compound-literal, simulate an error
5385          so that the call to cp_parser_parse_definitely below will
5386          fail.  */
5387       if (compound_literal_p)
5388         cp_parser_simulate_error (parser);
5389       else
5390         {
5391           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5392           parser->in_type_id_in_expr_p = true;
5393           /* Look for the type-id.  */
5394           type = cp_parser_type_id (parser);
5395           /* Look for the closing `)'.  */
5396           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5397           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5398         }
5399
5400       /* Restore the saved message.  */
5401       parser->type_definition_forbidden_message = saved_message;
5402
5403       /* If ok so far, parse the dependent expression. We cannot be
5404          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5405          ctor of T, but looks like a cast to function returning T
5406          without a dependent expression.  */
5407       if (!cp_parser_error_occurred (parser))
5408         expr = cp_parser_cast_expression (parser,
5409                                           /*address_p=*/false,
5410                                           /*cast_p=*/true);
5411
5412       if (cp_parser_parse_definitely (parser))
5413         {
5414           /* Warn about old-style casts, if so requested.  */
5415           if (warn_old_style_cast
5416               && !in_system_header
5417               && !VOID_TYPE_P (type)
5418               && current_lang_name != lang_name_c)
5419             warning (0, "use of old-style cast");
5420
5421           /* Only type conversions to integral or enumeration types
5422              can be used in constant-expressions.  */
5423           if (parser->integral_constant_expression_p
5424               && !dependent_type_p (type)
5425               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5426               && (cp_parser_non_integral_constant_expression
5427                   (parser,
5428                    "a cast to a type other than an integral or "
5429                    "enumeration type")))
5430             return error_mark_node;
5431
5432           /* Perform the cast.  */
5433           expr = build_c_cast (type, expr);
5434           return expr;
5435         }
5436     }
5437
5438   /* If we get here, then it's not a cast, so it must be a
5439      unary-expression.  */
5440   return cp_parser_unary_expression (parser, address_p, cast_p);
5441 }
5442
5443 /* Parse a binary expression of the general form:
5444
5445    pm-expression:
5446      cast-expression
5447      pm-expression .* cast-expression
5448      pm-expression ->* cast-expression
5449
5450    multiplicative-expression:
5451      pm-expression
5452      multiplicative-expression * pm-expression
5453      multiplicative-expression / pm-expression
5454      multiplicative-expression % pm-expression
5455
5456    additive-expression:
5457      multiplicative-expression
5458      additive-expression + multiplicative-expression
5459      additive-expression - multiplicative-expression
5460
5461    shift-expression:
5462      additive-expression
5463      shift-expression << additive-expression
5464      shift-expression >> additive-expression
5465
5466    relational-expression:
5467      shift-expression
5468      relational-expression < shift-expression
5469      relational-expression > shift-expression
5470      relational-expression <= shift-expression
5471      relational-expression >= shift-expression
5472
5473   GNU Extension:
5474
5475    relational-expression:
5476      relational-expression <? shift-expression
5477      relational-expression >? shift-expression
5478
5479    equality-expression:
5480      relational-expression
5481      equality-expression == relational-expression
5482      equality-expression != relational-expression
5483
5484    and-expression:
5485      equality-expression
5486      and-expression & equality-expression
5487
5488    exclusive-or-expression:
5489      and-expression
5490      exclusive-or-expression ^ and-expression
5491
5492    inclusive-or-expression:
5493      exclusive-or-expression
5494      inclusive-or-expression | exclusive-or-expression
5495
5496    logical-and-expression:
5497      inclusive-or-expression
5498      logical-and-expression && inclusive-or-expression
5499
5500    logical-or-expression:
5501      logical-and-expression
5502      logical-or-expression || logical-and-expression
5503
5504    All these are implemented with a single function like:
5505
5506    binary-expression:
5507      simple-cast-expression
5508      binary-expression <token> binary-expression
5509
5510    CAST_P is true if this expression is the target of a cast.
5511
5512    The binops_by_token map is used to get the tree codes for each <token> type.
5513    binary-expressions are associated according to a precedence table.  */
5514
5515 #define TOKEN_PRECEDENCE(token) \
5516   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5517    ? PREC_NOT_OPERATOR \
5518    : binops_by_token[token->type].prec)
5519
5520 static tree
5521 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5522 {
5523   cp_parser_expression_stack stack;
5524   cp_parser_expression_stack_entry *sp = &stack[0];
5525   tree lhs, rhs;
5526   cp_token *token;
5527   enum tree_code tree_type;
5528   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5529   bool overloaded_p;
5530
5531   /* Parse the first expression.  */
5532   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5533
5534   for (;;)
5535     {
5536       /* Get an operator token.  */
5537       token = cp_lexer_peek_token (parser->lexer);
5538       if (token->type == CPP_MIN || token->type == CPP_MAX)
5539         cp_parser_warn_min_max ();
5540
5541       new_prec = TOKEN_PRECEDENCE (token);
5542
5543       /* Popping an entry off the stack means we completed a subexpression:
5544          - either we found a token which is not an operator (`>' where it is not
5545            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5546            will happen repeatedly;
5547          - or, we found an operator which has lower priority.  This is the case
5548            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5549            parsing `3 * 4'.  */
5550       if (new_prec <= prec)
5551         {
5552           if (sp == stack)
5553             break;
5554           else
5555             goto pop;
5556         }
5557
5558      get_rhs:
5559       tree_type = binops_by_token[token->type].tree_type;
5560
5561       /* We used the operator token.  */
5562       cp_lexer_consume_token (parser->lexer);
5563
5564       /* Extract another operand.  It may be the RHS of this expression
5565          or the LHS of a new, higher priority expression.  */
5566       rhs = cp_parser_simple_cast_expression (parser);
5567
5568       /* Get another operator token.  Look up its precedence to avoid
5569          building a useless (immediately popped) stack entry for common
5570          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5571       token = cp_lexer_peek_token (parser->lexer);
5572       lookahead_prec = TOKEN_PRECEDENCE (token);
5573       if (lookahead_prec > new_prec)
5574         {
5575           /* ... and prepare to parse the RHS of the new, higher priority
5576              expression.  Since precedence levels on the stack are
5577              monotonically increasing, we do not have to care about
5578              stack overflows.  */
5579           sp->prec = prec;
5580           sp->tree_type = tree_type;
5581           sp->lhs = lhs;
5582           sp++;
5583           lhs = rhs;
5584           prec = new_prec;
5585           new_prec = lookahead_prec;
5586           goto get_rhs;
5587
5588          pop:
5589           /* If the stack is not empty, we have parsed into LHS the right side
5590              (`4' in the example above) of an expression we had suspended.
5591              We can use the information on the stack to recover the LHS (`3')
5592              from the stack together with the tree code (`MULT_EXPR'), and
5593              the precedence of the higher level subexpression
5594              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5595              which will be used to actually build the additive expression.  */
5596           --sp;
5597           prec = sp->prec;
5598           tree_type = sp->tree_type;
5599           rhs = lhs;
5600           lhs = sp->lhs;
5601         }
5602
5603       overloaded_p = false;
5604       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5605
5606       /* If the binary operator required the use of an overloaded operator,
5607          then this expression cannot be an integral constant-expression.
5608          An overloaded operator can be used even if both operands are
5609          otherwise permissible in an integral constant-expression if at
5610          least one of the operands is of enumeration type.  */
5611
5612       if (overloaded_p
5613           && (cp_parser_non_integral_constant_expression
5614               (parser, "calls to overloaded operators")))
5615         return error_mark_node;
5616     }
5617
5618   return lhs;
5619 }
5620
5621
5622 /* Parse the `? expression : assignment-expression' part of a
5623    conditional-expression.  The LOGICAL_OR_EXPR is the
5624    logical-or-expression that started the conditional-expression.
5625    Returns a representation of the entire conditional-expression.
5626
5627    This routine is used by cp_parser_assignment_expression.
5628
5629      ? expression : assignment-expression
5630
5631    GNU Extensions:
5632
5633      ? : assignment-expression */
5634
5635 static tree
5636 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5637 {
5638   tree expr;
5639   tree assignment_expr;
5640
5641   /* Consume the `?' token.  */
5642   cp_lexer_consume_token (parser->lexer);
5643   if (cp_parser_allow_gnu_extensions_p (parser)
5644       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5645     /* Implicit true clause.  */
5646     expr = NULL_TREE;
5647   else
5648     /* Parse the expression.  */
5649     expr = cp_parser_expression (parser, /*cast_p=*/false);
5650
5651   /* The next token should be a `:'.  */
5652   cp_parser_require (parser, CPP_COLON, "`:'");
5653   /* Parse the assignment-expression.  */
5654   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5655
5656   /* Build the conditional-expression.  */
5657   return build_x_conditional_expr (logical_or_expr,
5658                                    expr,
5659                                    assignment_expr);
5660 }
5661
5662 /* Parse an assignment-expression.
5663
5664    assignment-expression:
5665      conditional-expression
5666      logical-or-expression assignment-operator assignment_expression
5667      throw-expression
5668
5669    CAST_P is true if this expression is the target of a cast.
5670
5671    Returns a representation for the expression.  */
5672
5673 static tree
5674 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5675 {
5676   tree expr;
5677
5678   /* If the next token is the `throw' keyword, then we're looking at
5679      a throw-expression.  */
5680   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5681     expr = cp_parser_throw_expression (parser);
5682   /* Otherwise, it must be that we are looking at a
5683      logical-or-expression.  */
5684   else
5685     {
5686       /* Parse the binary expressions (logical-or-expression).  */
5687       expr = cp_parser_binary_expression (parser, cast_p);
5688       /* If the next token is a `?' then we're actually looking at a
5689          conditional-expression.  */
5690       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5691         return cp_parser_question_colon_clause (parser, expr);
5692       else
5693         {
5694           enum tree_code assignment_operator;
5695
5696           /* If it's an assignment-operator, we're using the second
5697              production.  */
5698           assignment_operator
5699             = cp_parser_assignment_operator_opt (parser);
5700           if (assignment_operator != ERROR_MARK)
5701             {
5702               tree rhs;
5703
5704               /* Parse the right-hand side of the assignment.  */
5705               rhs = cp_parser_assignment_expression (parser, cast_p);
5706               /* An assignment may not appear in a
5707                  constant-expression.  */
5708               if (cp_parser_non_integral_constant_expression (parser,
5709                                                               "an assignment"))
5710                 return error_mark_node;
5711               /* Build the assignment expression.  */
5712               expr = build_x_modify_expr (expr,
5713                                           assignment_operator,
5714                                           rhs);
5715             }
5716         }
5717     }
5718
5719   return expr;
5720 }
5721
5722 /* Parse an (optional) assignment-operator.
5723
5724    assignment-operator: one of
5725      = *= /= %= += -= >>= <<= &= ^= |=
5726
5727    GNU Extension:
5728
5729    assignment-operator: one of
5730      <?= >?=
5731
5732    If the next token is an assignment operator, the corresponding tree
5733    code is returned, and the token is consumed.  For example, for
5734    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5735    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5736    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5737    operator, ERROR_MARK is returned.  */
5738
5739 static enum tree_code
5740 cp_parser_assignment_operator_opt (cp_parser* parser)
5741 {
5742   enum tree_code op;
5743   cp_token *token;
5744
5745   /* Peek at the next toen.  */
5746   token = cp_lexer_peek_token (parser->lexer);
5747
5748   switch (token->type)
5749     {
5750     case CPP_EQ:
5751       op = NOP_EXPR;
5752       break;
5753
5754     case CPP_MULT_EQ:
5755       op = MULT_EXPR;
5756       break;
5757
5758     case CPP_DIV_EQ:
5759       op = TRUNC_DIV_EXPR;
5760       break;
5761
5762     case CPP_MOD_EQ:
5763       op = TRUNC_MOD_EXPR;
5764       break;
5765
5766     case CPP_PLUS_EQ:
5767       op = PLUS_EXPR;
5768       break;
5769
5770     case CPP_MINUS_EQ:
5771       op = MINUS_EXPR;
5772       break;
5773
5774     case CPP_RSHIFT_EQ:
5775       op = RSHIFT_EXPR;
5776       break;
5777
5778     case CPP_LSHIFT_EQ:
5779       op = LSHIFT_EXPR;
5780       break;
5781
5782     case CPP_AND_EQ:
5783       op = BIT_AND_EXPR;
5784       break;
5785
5786     case CPP_XOR_EQ:
5787       op = BIT_XOR_EXPR;
5788       break;
5789
5790     case CPP_OR_EQ:
5791       op = BIT_IOR_EXPR;
5792       break;
5793
5794     case CPP_MIN_EQ:
5795       op = MIN_EXPR;
5796       cp_parser_warn_min_max ();
5797       break;
5798
5799     case CPP_MAX_EQ:
5800       op = MAX_EXPR;
5801       cp_parser_warn_min_max ();
5802       break;
5803
5804     default:
5805       /* Nothing else is an assignment operator.  */
5806       op = ERROR_MARK;
5807     }
5808
5809   /* If it was an assignment operator, consume it.  */
5810   if (op != ERROR_MARK)
5811     cp_lexer_consume_token (parser->lexer);
5812
5813   return op;
5814 }
5815
5816 /* Parse an expression.
5817
5818    expression:
5819      assignment-expression
5820      expression , assignment-expression
5821
5822    CAST_P is true if this expression is the target of a cast.
5823
5824    Returns a representation of the expression.  */
5825
5826 static tree
5827 cp_parser_expression (cp_parser* parser, bool cast_p)
5828 {
5829   tree expression = NULL_TREE;
5830
5831   while (true)
5832     {
5833       tree assignment_expression;
5834
5835       /* Parse the next assignment-expression.  */
5836       assignment_expression
5837         = cp_parser_assignment_expression (parser, cast_p);
5838       /* If this is the first assignment-expression, we can just
5839          save it away.  */
5840       if (!expression)
5841         expression = assignment_expression;
5842       else
5843         expression = build_x_compound_expr (expression,
5844                                             assignment_expression);
5845       /* If the next token is not a comma, then we are done with the
5846          expression.  */
5847       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5848         break;
5849       /* Consume the `,'.  */
5850       cp_lexer_consume_token (parser->lexer);
5851       /* A comma operator cannot appear in a constant-expression.  */
5852       if (cp_parser_non_integral_constant_expression (parser,
5853                                                       "a comma operator"))
5854         expression = error_mark_node;
5855     }
5856
5857   return expression;
5858 }
5859
5860 /* Parse a constant-expression.
5861
5862    constant-expression:
5863      conditional-expression
5864
5865   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5866   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5867   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5868   is false, NON_CONSTANT_P should be NULL.  */
5869
5870 static tree
5871 cp_parser_constant_expression (cp_parser* parser,
5872                                bool allow_non_constant_p,
5873                                bool *non_constant_p)
5874 {
5875   bool saved_integral_constant_expression_p;
5876   bool saved_allow_non_integral_constant_expression_p;
5877   bool saved_non_integral_constant_expression_p;
5878   tree expression;
5879
5880   /* It might seem that we could simply parse the
5881      conditional-expression, and then check to see if it were
5882      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5883      one that the compiler can figure out is constant, possibly after
5884      doing some simplifications or optimizations.  The standard has a
5885      precise definition of constant-expression, and we must honor
5886      that, even though it is somewhat more restrictive.
5887
5888      For example:
5889
5890        int i[(2, 3)];
5891
5892      is not a legal declaration, because `(2, 3)' is not a
5893      constant-expression.  The `,' operator is forbidden in a
5894      constant-expression.  However, GCC's constant-folding machinery
5895      will fold this operation to an INTEGER_CST for `3'.  */
5896
5897   /* Save the old settings.  */
5898   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5899   saved_allow_non_integral_constant_expression_p
5900     = parser->allow_non_integral_constant_expression_p;
5901   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5902   /* We are now parsing a constant-expression.  */
5903   parser->integral_constant_expression_p = true;
5904   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5905   parser->non_integral_constant_expression_p = false;
5906   /* Although the grammar says "conditional-expression", we parse an
5907      "assignment-expression", which also permits "throw-expression"
5908      and the use of assignment operators.  In the case that
5909      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5910      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5911      actually essential that we look for an assignment-expression.
5912      For example, cp_parser_initializer_clauses uses this function to
5913      determine whether a particular assignment-expression is in fact
5914      constant.  */
5915   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5916   /* Restore the old settings.  */
5917   parser->integral_constant_expression_p
5918     = saved_integral_constant_expression_p;
5919   parser->allow_non_integral_constant_expression_p
5920     = saved_allow_non_integral_constant_expression_p;
5921   if (allow_non_constant_p)
5922     *non_constant_p = parser->non_integral_constant_expression_p;
5923   else if (parser->non_integral_constant_expression_p)
5924     expression = error_mark_node;
5925   parser->non_integral_constant_expression_p
5926     = saved_non_integral_constant_expression_p;
5927
5928   return expression;
5929 }
5930
5931 /* Parse __builtin_offsetof.
5932
5933    offsetof-expression:
5934      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5935
5936    offsetof-member-designator:
5937      id-expression
5938      | offsetof-member-designator "." id-expression
5939      | offsetof-member-designator "[" expression "]"
5940 */
5941
5942 static tree
5943 cp_parser_builtin_offsetof (cp_parser *parser)
5944 {
5945   int save_ice_p, save_non_ice_p;
5946   tree type, expr;
5947   cp_id_kind dummy;
5948
5949   /* We're about to accept non-integral-constant things, but will
5950      definitely yield an integral constant expression.  Save and
5951      restore these values around our local parsing.  */
5952   save_ice_p = parser->integral_constant_expression_p;
5953   save_non_ice_p = parser->non_integral_constant_expression_p;
5954
5955   /* Consume the "__builtin_offsetof" token.  */
5956   cp_lexer_consume_token (parser->lexer);
5957   /* Consume the opening `('.  */
5958   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5959   /* Parse the type-id.  */
5960   type = cp_parser_type_id (parser);
5961   /* Look for the `,'.  */
5962   cp_parser_require (parser, CPP_COMMA, "`,'");
5963
5964   /* Build the (type *)null that begins the traditional offsetof macro.  */
5965   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5966
5967   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5968   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5969                                                  true, &dummy);
5970   while (true)
5971     {
5972       cp_token *token = cp_lexer_peek_token (parser->lexer);
5973       switch (token->type)
5974         {
5975         case CPP_OPEN_SQUARE:
5976           /* offsetof-member-designator "[" expression "]" */
5977           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5978           break;
5979
5980         case CPP_DOT:
5981           /* offsetof-member-designator "." identifier */
5982           cp_lexer_consume_token (parser->lexer);
5983           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5984                                                          true, &dummy);
5985           break;
5986
5987         case CPP_CLOSE_PAREN:
5988           /* Consume the ")" token.  */
5989           cp_lexer_consume_token (parser->lexer);
5990           goto success;
5991
5992         default:
5993           /* Error.  We know the following require will fail, but
5994              that gives the proper error message.  */
5995           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5996           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5997           expr = error_mark_node;
5998           goto failure;
5999         }
6000     }
6001
6002  success:
6003   /* If we're processing a template, we can't finish the semantics yet.
6004      Otherwise we can fold the entire expression now.  */
6005   if (processing_template_decl)
6006     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6007   else
6008     expr = fold_offsetof (expr);
6009
6010  failure:
6011   parser->integral_constant_expression_p = save_ice_p;
6012   parser->non_integral_constant_expression_p = save_non_ice_p;
6013
6014   return expr;
6015 }
6016
6017 /* Statements [gram.stmt.stmt]  */
6018
6019 /* Parse a statement.
6020
6021    statement:
6022      labeled-statement
6023      expression-statement
6024      compound-statement
6025      selection-statement
6026      iteration-statement
6027      jump-statement
6028      declaration-statement
6029      try-block
6030
6031   IN_COMPOUND is true when the statement is nested inside a 
6032   cp_parser_compound_statement; this matters for certain pragmas.  */
6033
6034 static void
6035 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6036                      bool in_compound)
6037 {
6038   tree statement;
6039   cp_token *token;
6040   location_t statement_location;
6041
6042  restart:
6043   /* There is no statement yet.  */
6044   statement = NULL_TREE;
6045   /* Peek at the next token.  */
6046   token = cp_lexer_peek_token (parser->lexer);
6047   /* Remember the location of the first token in the statement.  */
6048   statement_location = token->location;
6049   /* If this is a keyword, then that will often determine what kind of
6050      statement we have.  */
6051   if (token->type == CPP_KEYWORD)
6052     {
6053       enum rid keyword = token->keyword;
6054
6055       switch (keyword)
6056         {
6057         case RID_CASE:
6058         case RID_DEFAULT:
6059           statement = cp_parser_labeled_statement (parser, in_statement_expr,
6060                                                    in_compound);
6061           break;
6062
6063         case RID_IF:
6064         case RID_SWITCH:
6065           statement = cp_parser_selection_statement (parser);
6066           break;
6067
6068         case RID_WHILE:
6069         case RID_DO:
6070         case RID_FOR:
6071           statement = cp_parser_iteration_statement (parser);
6072           break;
6073
6074         case RID_BREAK:
6075         case RID_CONTINUE:
6076         case RID_RETURN:
6077         case RID_GOTO:
6078           statement = cp_parser_jump_statement (parser);
6079           break;
6080
6081           /* Objective-C++ exception-handling constructs.  */
6082         case RID_AT_TRY:
6083         case RID_AT_CATCH:
6084         case RID_AT_FINALLY:
6085         case RID_AT_SYNCHRONIZED:
6086         case RID_AT_THROW:
6087           statement = cp_parser_objc_statement (parser);
6088           break;
6089
6090         case RID_TRY:
6091           statement = cp_parser_try_block (parser);
6092           break;
6093
6094         default:
6095           /* It might be a keyword like `int' that can start a
6096              declaration-statement.  */
6097           break;
6098         }
6099     }
6100   else if (token->type == CPP_NAME)
6101     {
6102       /* If the next token is a `:', then we are looking at a
6103          labeled-statement.  */
6104       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6105       if (token->type == CPP_COLON)
6106         statement = cp_parser_labeled_statement (parser, in_statement_expr,
6107                                                  in_compound);
6108     }
6109   /* Anything that starts with a `{' must be a compound-statement.  */
6110   else if (token->type == CPP_OPEN_BRACE)
6111     statement = cp_parser_compound_statement (parser, NULL, false);
6112   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6113      a statement all its own.  */
6114   else if (token->type == CPP_PRAGMA)
6115     {
6116       /* Only certain OpenMP pragmas are attached to statements, and thus
6117          are considered statements themselves.  All others are not.  In
6118          the context of a compound, accept the pragma as a "statement" and
6119          return so that we can check for a close brace.  Otherwise we 
6120          require a real statement and must go back and read one.  */
6121       if (in_compound)
6122         cp_parser_pragma (parser, pragma_compound);
6123       else if (!cp_parser_pragma (parser, pragma_stmt))
6124         goto restart;
6125       return;
6126     }
6127   else if (token->type == CPP_EOF)
6128     {
6129       cp_parser_error (parser, "expected statement");
6130       return;
6131     }
6132
6133   /* Everything else must be a declaration-statement or an
6134      expression-statement.  Try for the declaration-statement
6135      first, unless we are looking at a `;', in which case we know that
6136      we have an expression-statement.  */
6137   if (!statement)
6138     {
6139       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6140         {
6141           cp_parser_parse_tentatively (parser);
6142           /* Try to parse the declaration-statement.  */
6143           cp_parser_declaration_statement (parser);
6144           /* If that worked, we're done.  */
6145           if (cp_parser_parse_definitely (parser))
6146             return;
6147         }
6148       /* Look for an expression-statement instead.  */
6149       statement = cp_parser_expression_statement (parser, in_statement_expr);
6150     }
6151
6152   /* Set the line number for the statement.  */
6153   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6154     SET_EXPR_LOCATION (statement, statement_location);
6155 }
6156
6157 /* Parse a labeled-statement.
6158
6159    labeled-statement:
6160      identifier : statement
6161      case constant-expression : statement
6162      default : statement
6163
6164    GNU Extension:
6165
6166    labeled-statement:
6167      case constant-expression ... constant-expression : statement
6168
6169    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6170    For an ordinary label, returns a LABEL_EXPR.
6171
6172    IN_COMPOUND is as for cp_parser_statement: true when we're nested
6173    inside a compound.  */
6174
6175 static tree
6176 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6177                              bool in_compound)
6178 {
6179   cp_token *token;
6180   tree statement = error_mark_node;
6181
6182   /* The next token should be an identifier.  */
6183   token = cp_lexer_peek_token (parser->lexer);
6184   if (token->type != CPP_NAME
6185       && token->type != CPP_KEYWORD)
6186     {
6187       cp_parser_error (parser, "expected labeled-statement");
6188       return error_mark_node;
6189     }
6190
6191   switch (token->keyword)
6192     {
6193     case RID_CASE:
6194       {
6195         tree expr, expr_hi;
6196         cp_token *ellipsis;
6197
6198         /* Consume the `case' token.  */
6199         cp_lexer_consume_token (parser->lexer);
6200         /* Parse the constant-expression.  */
6201         expr = cp_parser_constant_expression (parser,
6202                                               /*allow_non_constant_p=*/false,
6203                                               NULL);
6204
6205         ellipsis = cp_lexer_peek_token (parser->lexer);
6206         if (ellipsis->type == CPP_ELLIPSIS)
6207           {
6208             /* Consume the `...' token.  */
6209             cp_lexer_consume_token (parser->lexer);
6210             expr_hi =
6211               cp_parser_constant_expression (parser,
6212                                              /*allow_non_constant_p=*/false,
6213                                              NULL);
6214             /* We don't need to emit warnings here, as the common code
6215                will do this for us.  */
6216           }
6217         else
6218           expr_hi = NULL_TREE;
6219
6220         if (parser->in_switch_statement_p)
6221           statement = finish_case_label (expr, expr_hi);
6222         else
6223           error ("case label %qE not within a switch statement", expr);
6224       }
6225       break;
6226
6227     case RID_DEFAULT:
6228       /* Consume the `default' token.  */
6229       cp_lexer_consume_token (parser->lexer);
6230
6231       if (parser->in_switch_statement_p)
6232         statement = finish_case_label (NULL_TREE, NULL_TREE);
6233       else
6234         error ("case label not within a switch statement");
6235       break;
6236
6237     default:
6238       /* Anything else must be an ordinary label.  */
6239       statement = finish_label_stmt (cp_parser_identifier (parser));
6240       break;
6241     }
6242
6243   /* Require the `:' token.  */
6244   cp_parser_require (parser, CPP_COLON, "`:'");
6245   /* Parse the labeled statement.  */
6246   cp_parser_statement (parser, in_statement_expr, in_compound);
6247
6248   /* Return the label, in the case of a `case' or `default' label.  */
6249   return statement;
6250 }
6251
6252 /* Parse an expression-statement.
6253
6254    expression-statement:
6255      expression [opt] ;
6256
6257    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6258    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6259    indicates whether this expression-statement is part of an
6260    expression statement.  */
6261
6262 static tree
6263 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6264 {
6265   tree statement = NULL_TREE;
6266
6267   /* If the next token is a ';', then there is no expression
6268      statement.  */
6269   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6270     statement = cp_parser_expression (parser, /*cast_p=*/false);
6271
6272   /* Consume the final `;'.  */
6273   cp_parser_consume_semicolon_at_end_of_statement (parser);
6274
6275   if (in_statement_expr
6276       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6277     /* This is the final expression statement of a statement
6278        expression.  */
6279     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6280   else if (statement)
6281     statement = finish_expr_stmt (statement);
6282   else
6283     finish_stmt ();
6284
6285   return statement;
6286 }
6287
6288 /* Parse a compound-statement.
6289
6290    compound-statement:
6291      { statement-seq [opt] }
6292
6293    Returns a tree representing the statement.  */
6294
6295 static tree
6296 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6297                               bool in_try)
6298 {
6299   tree compound_stmt;
6300
6301   /* Consume the `{'.  */
6302   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6303     return error_mark_node;
6304   /* Begin the compound-statement.  */
6305   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6306   /* Parse an (optional) statement-seq.  */
6307   cp_parser_statement_seq_opt (parser, in_statement_expr);
6308   /* Finish the compound-statement.  */
6309   finish_compound_stmt (compound_stmt);
6310   /* Consume the `}'.  */
6311   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6312
6313   return compound_stmt;
6314 }
6315
6316 /* Parse an (optional) statement-seq.
6317
6318    statement-seq:
6319      statement
6320      statement-seq [opt] statement  */
6321
6322 static void
6323 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6324 {
6325   /* Scan statements until there aren't any more.  */
6326   while (true)
6327     {
6328       cp_token *token = cp_lexer_peek_token (parser->lexer);
6329
6330       /* If we're looking at a `}', then we've run out of statements.  */
6331       if (token->type == CPP_CLOSE_BRACE
6332           || token->type == CPP_EOF
6333           || token->type == CPP_PRAGMA_EOL)
6334         break;
6335
6336       /* Parse the statement.  */
6337       cp_parser_statement (parser, in_statement_expr, true);
6338     }
6339 }
6340
6341 /* Parse a selection-statement.
6342
6343    selection-statement:
6344      if ( condition ) statement
6345      if ( condition ) statement else statement
6346      switch ( condition ) statement
6347
6348    Returns the new IF_STMT or SWITCH_STMT.  */
6349
6350 static tree
6351 cp_parser_selection_statement (cp_parser* parser)
6352 {
6353   cp_token *token;
6354   enum rid keyword;
6355
6356   /* Peek at the next token.  */
6357   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6358
6359   /* See what kind of keyword it is.  */
6360   keyword = token->keyword;
6361   switch (keyword)
6362     {
6363     case RID_IF:
6364     case RID_SWITCH:
6365       {
6366         tree statement;
6367         tree condition;
6368
6369         /* Look for the `('.  */
6370         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6371           {
6372             cp_parser_skip_to_end_of_statement (parser);
6373             return error_mark_node;
6374           }
6375
6376         /* Begin the selection-statement.  */
6377         if (keyword == RID_IF)
6378           statement = begin_if_stmt ();
6379         else
6380           statement = begin_switch_stmt ();
6381
6382         /* Parse the condition.  */
6383         condition = cp_parser_condition (parser);
6384         /* Look for the `)'.  */
6385         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6386           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6387                                                  /*consume_paren=*/true);
6388
6389         if (keyword == RID_IF)
6390           {
6391             /* Add the condition.  */
6392             finish_if_stmt_cond (condition, statement);
6393
6394             /* Parse the then-clause.  */
6395             cp_parser_implicitly_scoped_statement (parser);
6396             finish_then_clause (statement);
6397
6398             /* If the next token is `else', parse the else-clause.  */
6399             if (cp_lexer_next_token_is_keyword (parser->lexer,
6400                                                 RID_ELSE))
6401               {
6402                 /* Consume the `else' keyword.  */
6403                 cp_lexer_consume_token (parser->lexer);
6404                 begin_else_clause (statement);
6405                 /* Parse the else-clause.  */
6406                 cp_parser_implicitly_scoped_statement (parser);
6407                 finish_else_clause (statement);
6408               }
6409
6410             /* Now we're all done with the if-statement.  */
6411             finish_if_stmt (statement);
6412           }
6413         else
6414           {
6415             bool in_switch_statement_p;
6416
6417             /* Add the condition.  */
6418             finish_switch_cond (condition, statement);
6419
6420             /* Parse the body of the switch-statement.  */
6421             in_switch_statement_p = parser->in_switch_statement_p;
6422             parser->in_switch_statement_p = true;
6423             cp_parser_implicitly_scoped_statement (parser);
6424             parser->in_switch_statement_p = in_switch_statement_p;
6425
6426             /* Now we're all done with the switch-statement.  */
6427             finish_switch_stmt (statement);
6428           }
6429
6430         return statement;
6431       }
6432       break;
6433
6434     default:
6435       cp_parser_error (parser, "expected selection-statement");
6436       return error_mark_node;
6437     }
6438 }
6439
6440 /* Parse a condition.
6441
6442    condition:
6443      expression
6444      type-specifier-seq declarator = assignment-expression
6445
6446    GNU Extension:
6447
6448    condition:
6449      type-specifier-seq declarator asm-specification [opt]
6450        attributes [opt] = assignment-expression
6451
6452    Returns the expression that should be tested.  */
6453
6454 static tree
6455 cp_parser_condition (cp_parser* parser)
6456 {
6457   cp_decl_specifier_seq type_specifiers;
6458   const char *saved_message;
6459
6460   /* Try the declaration first.  */
6461   cp_parser_parse_tentatively (parser);
6462   /* New types are not allowed in the type-specifier-seq for a
6463      condition.  */
6464   saved_message = parser->type_definition_forbidden_message;
6465   parser->type_definition_forbidden_message
6466     = "types may not be defined in conditions";
6467   /* Parse the type-specifier-seq.  */
6468   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6469                                 &type_specifiers);
6470   /* Restore the saved message.  */
6471   parser->type_definition_forbidden_message = saved_message;
6472   /* If all is well, we might be looking at a declaration.  */
6473   if (!cp_parser_error_occurred (parser))
6474     {
6475       tree decl;
6476       tree asm_specification;
6477       tree attributes;
6478       cp_declarator *declarator;
6479       tree initializer = NULL_TREE;
6480
6481       /* Parse the declarator.  */
6482       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6483                                          /*ctor_dtor_or_conv_p=*/NULL,
6484                                          /*parenthesized_p=*/NULL,
6485                                          /*member_p=*/false);
6486       /* Parse the attributes.  */
6487       attributes = cp_parser_attributes_opt (parser);
6488       /* Parse the asm-specification.  */
6489       asm_specification = cp_parser_asm_specification_opt (parser);
6490       /* If the next token is not an `=', then we might still be
6491          looking at an expression.  For example:
6492
6493            if (A(a).x)
6494
6495          looks like a decl-specifier-seq and a declarator -- but then
6496          there is no `=', so this is an expression.  */
6497       cp_parser_require (parser, CPP_EQ, "`='");
6498       /* If we did see an `=', then we are looking at a declaration
6499          for sure.  */
6500       if (cp_parser_parse_definitely (parser))
6501         {
6502           tree pushed_scope;
6503
6504           /* Create the declaration.  */
6505           decl = start_decl (declarator, &type_specifiers,
6506                              /*initialized_p=*/true,
6507                              attributes, /*prefix_attributes=*/NULL_TREE,
6508                              &pushed_scope);
6509           /* Parse the assignment-expression.  */
6510           initializer = cp_parser_assignment_expression (parser,
6511                                                          /*cast_p=*/false);
6512
6513           /* Process the initializer.  */
6514           cp_finish_decl (decl,
6515                           initializer,
6516                           asm_specification,
6517                           LOOKUP_ONLYCONVERTING);
6518
6519           if (pushed_scope)
6520             pop_scope (pushed_scope);
6521
6522           return convert_from_reference (decl);
6523         }
6524     }
6525   /* If we didn't even get past the declarator successfully, we are
6526      definitely not looking at a declaration.  */
6527   else
6528     cp_parser_abort_tentative_parse (parser);
6529
6530   /* Otherwise, we are looking at an expression.  */
6531   return cp_parser_expression (parser, /*cast_p=*/false);
6532 }
6533
6534 /* Parse an iteration-statement.
6535
6536    iteration-statement:
6537      while ( condition ) statement
6538      do statement while ( expression ) ;
6539      for ( for-init-statement condition [opt] ; expression [opt] )
6540        statement
6541
6542    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6543
6544 static tree
6545 cp_parser_iteration_statement (cp_parser* parser)
6546 {
6547   cp_token *token;
6548   enum rid keyword;
6549   tree statement;
6550   bool in_iteration_statement_p;
6551
6552
6553   /* Peek at the next token.  */
6554   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6555   if (!token)
6556     return error_mark_node;
6557
6558   /* Remember whether or not we are already within an iteration
6559      statement.  */
6560   in_iteration_statement_p = parser->in_iteration_statement_p;
6561
6562   /* See what kind of keyword it is.  */
6563   keyword = token->keyword;
6564   switch (keyword)
6565     {
6566     case RID_WHILE:
6567       {
6568         tree condition;
6569
6570         /* Begin the while-statement.  */
6571         statement = begin_while_stmt ();
6572         /* Look for the `('.  */
6573         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6574         /* Parse the condition.  */
6575         condition = cp_parser_condition (parser);
6576         finish_while_stmt_cond (condition, statement);
6577         /* Look for the `)'.  */
6578         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6579         /* Parse the dependent statement.  */
6580         parser->in_iteration_statement_p = true;
6581         cp_parser_already_scoped_statement (parser);
6582         parser->in_iteration_statement_p = in_iteration_statement_p;
6583         /* We're done with the while-statement.  */
6584         finish_while_stmt (statement);
6585       }
6586       break;
6587
6588     case RID_DO:
6589       {
6590         tree expression;
6591
6592         /* Begin the do-statement.  */
6593         statement = begin_do_stmt ();
6594         /* Parse the body of the do-statement.  */
6595         parser->in_iteration_statement_p = true;
6596         cp_parser_implicitly_scoped_statement (parser);
6597         parser->in_iteration_statement_p = in_iteration_statement_p;
6598         finish_do_body (statement);
6599         /* Look for the `while' keyword.  */
6600         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6601         /* Look for the `('.  */
6602         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6603         /* Parse the expression.  */
6604         expression = cp_parser_expression (parser, /*cast_p=*/false);
6605         /* We're done with the do-statement.  */
6606         finish_do_stmt (expression, statement);
6607         /* Look for the `)'.  */
6608         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6609         /* Look for the `;'.  */
6610         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6611       }
6612       break;
6613
6614     case RID_FOR:
6615       {
6616         tree condition = NULL_TREE;
6617         tree expression = NULL_TREE;
6618
6619         /* Begin the for-statement.  */
6620         statement = begin_for_stmt ();
6621         /* Look for the `('.  */
6622         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6623         /* Parse the initialization.  */
6624         cp_parser_for_init_statement (parser);
6625         finish_for_init_stmt (statement);
6626
6627         /* If there's a condition, process it.  */
6628         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6629           condition = cp_parser_condition (parser);
6630         finish_for_cond (condition, statement);
6631         /* Look for the `;'.  */
6632         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6633
6634         /* If there's an expression, process it.  */
6635         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6636           expression = cp_parser_expression (parser, /*cast_p=*/false);
6637         finish_for_expr (expression, statement);
6638         /* Look for the `)'.  */
6639         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6640
6641         /* Parse the body of the for-statement.  */
6642         parser->in_iteration_statement_p = true;
6643         cp_parser_already_scoped_statement (parser);
6644         parser->in_iteration_statement_p = in_iteration_statement_p;
6645
6646         /* We're done with the for-statement.  */
6647         finish_for_stmt (statement);
6648       }
6649       break;
6650
6651     default:
6652       cp_parser_error (parser, "expected iteration-statement");
6653       statement = error_mark_node;
6654       break;
6655     }
6656
6657   return statement;
6658 }
6659
6660 /* Parse a for-init-statement.
6661
6662    for-init-statement:
6663      expression-statement
6664      simple-declaration  */
6665
6666 static void
6667 cp_parser_for_init_statement (cp_parser* parser)
6668 {
6669   /* If the next token is a `;', then we have an empty
6670      expression-statement.  Grammatically, this is also a
6671      simple-declaration, but an invalid one, because it does not
6672      declare anything.  Therefore, if we did not handle this case
6673      specially, we would issue an error message about an invalid
6674      declaration.  */
6675   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6676     {
6677       /* We're going to speculatively look for a declaration, falling back
6678          to an expression, if necessary.  */
6679       cp_parser_parse_tentatively (parser);
6680       /* Parse the declaration.  */
6681       cp_parser_simple_declaration (parser,
6682                                     /*function_definition_allowed_p=*/false);
6683       /* If the tentative parse failed, then we shall need to look for an
6684          expression-statement.  */
6685       if (cp_parser_parse_definitely (parser))
6686         return;
6687     }
6688
6689   cp_parser_expression_statement (parser, false);
6690 }
6691
6692 /* Parse a jump-statement.
6693
6694    jump-statement:
6695      break ;
6696      continue ;
6697      return expression [opt] ;
6698      goto identifier ;
6699
6700    GNU extension:
6701
6702    jump-statement:
6703      goto * expression ;
6704
6705    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6706
6707 static tree
6708 cp_parser_jump_statement (cp_parser* parser)
6709 {
6710   tree statement = error_mark_node;
6711   cp_token *token;
6712   enum rid keyword;
6713
6714   /* Peek at the next token.  */
6715   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6716   if (!token)
6717     return error_mark_node;
6718
6719   /* See what kind of keyword it is.  */
6720   keyword = token->keyword;
6721   switch (keyword)
6722     {
6723     case RID_BREAK:
6724       if (!parser->in_switch_statement_p
6725           && !parser->in_iteration_statement_p)
6726         {
6727           error ("break statement not within loop or switch");
6728           statement = error_mark_node;
6729         }
6730       else
6731         statement = finish_break_stmt ();
6732       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6733       break;
6734
6735     case RID_CONTINUE:
6736       if (!parser->in_iteration_statement_p)
6737         {
6738           error ("continue statement not within a loop");
6739           statement = error_mark_node;
6740         }
6741       else
6742         statement = finish_continue_stmt ();
6743       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6744       break;
6745
6746     case RID_RETURN:
6747       {
6748         tree expr;
6749
6750         /* If the next token is a `;', then there is no
6751            expression.  */
6752         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6753           expr = cp_parser_expression (parser, /*cast_p=*/false);
6754         else
6755           expr = NULL_TREE;
6756         /* Build the return-statement.  */
6757         statement = finish_return_stmt (expr);
6758         /* Look for the final `;'.  */
6759         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6760       }
6761       break;
6762
6763     case RID_GOTO:
6764       /* Create the goto-statement.  */
6765       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6766         {
6767           /* Issue a warning about this use of a GNU extension.  */
6768           if (pedantic)
6769             pedwarn ("ISO C++ forbids computed gotos");
6770           /* Consume the '*' token.  */
6771           cp_lexer_consume_token (parser->lexer);
6772           /* Parse the dependent expression.  */
6773           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6774         }
6775       else
6776         finish_goto_stmt (cp_parser_identifier (parser));
6777       /* Look for the final `;'.  */
6778       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6779       break;
6780
6781     default:
6782       cp_parser_error (parser, "expected jump-statement");
6783       break;
6784     }
6785
6786   return statement;
6787 }
6788
6789 /* Parse a declaration-statement.
6790
6791    declaration-statement:
6792      block-declaration  */
6793
6794 static void
6795 cp_parser_declaration_statement (cp_parser* parser)
6796 {
6797   void *p;
6798
6799   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6800   p = obstack_alloc (&declarator_obstack, 0);
6801
6802  /* Parse the block-declaration.  */
6803   cp_parser_block_declaration (parser, /*statement_p=*/true);
6804
6805   /* Free any declarators allocated.  */
6806   obstack_free (&declarator_obstack, p);
6807
6808   /* Finish off the statement.  */
6809   finish_stmt ();
6810 }
6811
6812 /* Some dependent statements (like `if (cond) statement'), are
6813    implicitly in their own scope.  In other words, if the statement is
6814    a single statement (as opposed to a compound-statement), it is
6815    none-the-less treated as if it were enclosed in braces.  Any
6816    declarations appearing in the dependent statement are out of scope
6817    after control passes that point.  This function parses a statement,
6818    but ensures that is in its own scope, even if it is not a
6819    compound-statement.
6820
6821    Returns the new statement.  */
6822
6823 static tree
6824 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6825 {
6826   tree statement;
6827
6828   /* If the token is not a `{', then we must take special action.  */
6829   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6830     {
6831       /* Create a compound-statement.  */
6832       statement = begin_compound_stmt (0);
6833       /* Parse the dependent-statement.  */
6834       cp_parser_statement (parser, NULL_TREE, false);
6835       /* Finish the dummy compound-statement.  */
6836       finish_compound_stmt (statement);
6837     }
6838   /* Otherwise, we simply parse the statement directly.  */
6839   else
6840     statement = cp_parser_compound_statement (parser, NULL, false);
6841
6842   /* Return the statement.  */
6843   return statement;
6844 }
6845
6846 /* For some dependent statements (like `while (cond) statement'), we
6847    have already created a scope.  Therefore, even if the dependent
6848    statement is a compound-statement, we do not want to create another
6849    scope.  */
6850
6851 static void
6852 cp_parser_already_scoped_statement (cp_parser* parser)
6853 {
6854   /* If the token is a `{', then we must take special action.  */
6855   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6856     cp_parser_statement (parser, NULL_TREE, false);
6857   else
6858     {
6859       /* Avoid calling cp_parser_compound_statement, so that we
6860          don't create a new scope.  Do everything else by hand.  */
6861       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6862       cp_parser_statement_seq_opt (parser, NULL_TREE);
6863       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6864     }
6865 }
6866
6867 /* Declarations [gram.dcl.dcl] */
6868
6869 /* Parse an optional declaration-sequence.
6870
6871    declaration-seq:
6872      declaration
6873      declaration-seq declaration  */
6874
6875 static void
6876 cp_parser_declaration_seq_opt (cp_parser* parser)
6877 {
6878   while (true)
6879     {
6880       cp_token *token;
6881
6882       token = cp_lexer_peek_token (parser->lexer);
6883
6884       if (token->type == CPP_CLOSE_BRACE
6885           || token->type == CPP_EOF
6886           || token->type == CPP_PRAGMA_EOL)
6887         break;
6888
6889       if (token->type == CPP_SEMICOLON)
6890         {
6891           /* A declaration consisting of a single semicolon is
6892              invalid.  Allow it unless we're being pedantic.  */
6893           cp_lexer_consume_token (parser->lexer);
6894           if (pedantic && !in_system_header)
6895             pedwarn ("extra %<;%>");
6896           continue;
6897         }
6898
6899       /* If we're entering or exiting a region that's implicitly
6900          extern "C", modify the lang context appropriately.  */
6901       if (!parser->implicit_extern_c && token->implicit_extern_c)
6902         {
6903           push_lang_context (lang_name_c);
6904           parser->implicit_extern_c = true;
6905         }
6906       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6907         {
6908           pop_lang_context ();
6909           parser->implicit_extern_c = false;
6910         }
6911
6912       if (token->type == CPP_PRAGMA)
6913         {
6914           /* A top-level declaration can consist solely of a #pragma.
6915              A nested declaration cannot, so this is done here and not
6916              in cp_parser_declaration.  (A #pragma at block scope is
6917              handled in cp_parser_statement.)  */
6918           cp_parser_pragma (parser, pragma_external);
6919           continue;
6920         }
6921
6922       /* Parse the declaration itself.  */
6923       cp_parser_declaration (parser);
6924     }
6925 }
6926
6927 /* Parse a declaration.
6928
6929    declaration:
6930      block-declaration
6931      function-definition
6932      template-declaration
6933      explicit-instantiation
6934      explicit-specialization
6935      linkage-specification
6936      namespace-definition
6937
6938    GNU extension:
6939
6940    declaration:
6941       __extension__ declaration */
6942
6943 static void
6944 cp_parser_declaration (cp_parser* parser)
6945 {
6946   cp_token token1;
6947   cp_token token2;
6948   int saved_pedantic;
6949   void *p;
6950
6951   /* Check for the `__extension__' keyword.  */
6952   if (cp_parser_extension_opt (parser, &saved_pedantic))
6953     {
6954       /* Parse the qualified declaration.  */
6955       cp_parser_declaration (parser);
6956       /* Restore the PEDANTIC flag.  */
6957       pedantic = saved_pedantic;
6958
6959       return;
6960     }
6961
6962   /* Try to figure out what kind of declaration is present.  */
6963   token1 = *cp_lexer_peek_token (parser->lexer);
6964
6965   if (token1.type != CPP_EOF)
6966     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6967   else
6968     {
6969       token2.type = CPP_EOF;
6970       token2.keyword = RID_MAX;
6971     }
6972
6973   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6974   p = obstack_alloc (&declarator_obstack, 0);
6975
6976   /* If the next token is `extern' and the following token is a string
6977      literal, then we have a linkage specification.  */
6978   if (token1.keyword == RID_EXTERN
6979       && cp_parser_is_string_literal (&token2))
6980     cp_parser_linkage_specification (parser);
6981   /* If the next token is `template', then we have either a template
6982      declaration, an explicit instantiation, or an explicit
6983      specialization.  */
6984   else if (token1.keyword == RID_TEMPLATE)
6985     {
6986       /* `template <>' indicates a template specialization.  */
6987       if (token2.type == CPP_LESS
6988           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6989         cp_parser_explicit_specialization (parser);
6990       /* `template <' indicates a template declaration.  */
6991       else if (token2.type == CPP_LESS)
6992         cp_parser_template_declaration (parser, /*member_p=*/false);
6993       /* Anything else must be an explicit instantiation.  */
6994       else
6995         cp_parser_explicit_instantiation (parser);
6996     }
6997   /* If the next token is `export', then we have a template
6998      declaration.  */
6999   else if (token1.keyword == RID_EXPORT)
7000     cp_parser_template_declaration (parser, /*member_p=*/false);
7001   /* If the next token is `extern', 'static' or 'inline' and the one
7002      after that is `template', we have a GNU extended explicit
7003      instantiation directive.  */
7004   else if (cp_parser_allow_gnu_extensions_p (parser)
7005            && (token1.keyword == RID_EXTERN
7006                || token1.keyword == RID_STATIC
7007                || token1.keyword == RID_INLINE)
7008            && token2.keyword == RID_TEMPLATE)
7009     cp_parser_explicit_instantiation (parser);
7010   /* If the next token is `namespace', check for a named or unnamed
7011      namespace definition.  */
7012   else if (token1.keyword == RID_NAMESPACE
7013            && (/* A named namespace definition.  */
7014                (token2.type == CPP_NAME
7015                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7016                     == CPP_OPEN_BRACE))
7017                /* An unnamed namespace definition.  */
7018                || token2.type == CPP_OPEN_BRACE))
7019     cp_parser_namespace_definition (parser);
7020   /* Objective-C++ declaration/definition.  */
7021   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7022     cp_parser_objc_declaration (parser);
7023   /* We must have either a block declaration or a function
7024      definition.  */
7025   else
7026     /* Try to parse a block-declaration, or a function-definition.  */
7027     cp_parser_block_declaration (parser, /*statement_p=*/false);
7028
7029   /* Free any declarators allocated.  */
7030   obstack_free (&declarator_obstack, p);
7031 }
7032
7033 /* Parse a block-declaration.
7034
7035    block-declaration:
7036      simple-declaration
7037      asm-definition
7038      namespace-alias-definition
7039      using-declaration
7040      using-directive
7041
7042    GNU Extension:
7043
7044    block-declaration:
7045      __extension__ block-declaration
7046      label-declaration
7047
7048    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7049    part of a declaration-statement.  */
7050
7051 static void
7052 cp_parser_block_declaration (cp_parser *parser,
7053                              bool      statement_p)
7054 {
7055   cp_token *token1;
7056   int saved_pedantic;
7057
7058   /* Check for the `__extension__' keyword.  */
7059   if (cp_parser_extension_opt (parser, &saved_pedantic))
7060     {
7061       /* Parse the qualified declaration.  */
7062       cp_parser_block_declaration (parser, statement_p);
7063       /* Restore the PEDANTIC flag.  */
7064       pedantic = saved_pedantic;
7065
7066       return;
7067     }
7068
7069   /* Peek at the next token to figure out which kind of declaration is
7070      present.  */
7071   token1 = cp_lexer_peek_token (parser->lexer);
7072
7073   /* If the next keyword is `asm', we have an asm-definition.  */
7074   if (token1->keyword == RID_ASM)
7075     {
7076       if (statement_p)
7077         cp_parser_commit_to_tentative_parse (parser);
7078       cp_parser_asm_definition (parser);
7079     }
7080   /* If the next keyword is `namespace', we have a
7081      namespace-alias-definition.  */
7082   else if (token1->keyword == RID_NAMESPACE)
7083     cp_parser_namespace_alias_definition (parser);
7084   /* If the next keyword is `using', we have either a
7085      using-declaration or a using-directive.  */
7086   else if (token1->keyword == RID_USING)
7087     {
7088       cp_token *token2;
7089
7090       if (statement_p)
7091         cp_parser_commit_to_tentative_parse (parser);
7092       /* If the token after `using' is `namespace', then we have a
7093          using-directive.  */
7094       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7095       if (token2->keyword == RID_NAMESPACE)
7096         cp_parser_using_directive (parser);
7097       /* Otherwise, it's a using-declaration.  */
7098       else
7099         cp_parser_using_declaration (parser);
7100     }
7101   /* If the next keyword is `__label__' we have a label declaration.  */
7102   else if (token1->keyword == RID_LABEL)
7103     {
7104       if (statement_p)
7105         cp_parser_commit_to_tentative_parse (parser);
7106       cp_parser_label_declaration (parser);
7107     }
7108   /* Anything else must be a simple-declaration.  */
7109   else
7110     cp_parser_simple_declaration (parser, !statement_p);
7111 }
7112
7113 /* Parse a simple-declaration.
7114
7115    simple-declaration:
7116      decl-specifier-seq [opt] init-declarator-list [opt] ;
7117
7118    init-declarator-list:
7119      init-declarator
7120      init-declarator-list , init-declarator
7121
7122    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7123    function-definition as a simple-declaration.  */
7124
7125 static void
7126 cp_parser_simple_declaration (cp_parser* parser,
7127                               bool function_definition_allowed_p)
7128 {
7129   cp_decl_specifier_seq decl_specifiers;
7130   int declares_class_or_enum;
7131   bool saw_declarator;
7132
7133   /* Defer access checks until we know what is being declared; the
7134      checks for names appearing in the decl-specifier-seq should be
7135      done as if we were in the scope of the thing being declared.  */
7136   push_deferring_access_checks (dk_deferred);
7137
7138   /* Parse the decl-specifier-seq.  We have to keep track of whether
7139      or not the decl-specifier-seq declares a named class or
7140      enumeration type, since that is the only case in which the
7141      init-declarator-list is allowed to be empty.
7142
7143      [dcl.dcl]
7144
7145      In a simple-declaration, the optional init-declarator-list can be
7146      omitted only when declaring a class or enumeration, that is when
7147      the decl-specifier-seq contains either a class-specifier, an
7148      elaborated-type-specifier, or an enum-specifier.  */
7149   cp_parser_decl_specifier_seq (parser,
7150                                 CP_PARSER_FLAGS_OPTIONAL,
7151                                 &decl_specifiers,
7152                                 &declares_class_or_enum);
7153   /* We no longer need to defer access checks.  */
7154   stop_deferring_access_checks ();
7155
7156   /* In a block scope, a valid declaration must always have a
7157      decl-specifier-seq.  By not trying to parse declarators, we can
7158      resolve the declaration/expression ambiguity more quickly.  */
7159   if (!function_definition_allowed_p
7160       && !decl_specifiers.any_specifiers_p)
7161     {
7162       cp_parser_error (parser, "expected declaration");
7163       goto done;
7164     }
7165
7166   /* If the next two tokens are both identifiers, the code is
7167      erroneous. The usual cause of this situation is code like:
7168
7169        T t;
7170
7171      where "T" should name a type -- but does not.  */
7172   if (!decl_specifiers.type
7173       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7174     {
7175       /* If parsing tentatively, we should commit; we really are
7176          looking at a declaration.  */
7177       cp_parser_commit_to_tentative_parse (parser);
7178       /* Give up.  */
7179       goto done;
7180     }
7181
7182   /* If we have seen at least one decl-specifier, and the next token
7183      is not a parenthesis, then we must be looking at a declaration.
7184      (After "int (" we might be looking at a functional cast.)  */
7185   if (decl_specifiers.any_specifiers_p
7186       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7187     cp_parser_commit_to_tentative_parse (parser);
7188
7189   /* Keep going until we hit the `;' at the end of the simple
7190      declaration.  */
7191   saw_declarator = false;
7192   while (cp_lexer_next_token_is_not (parser->lexer,
7193                                      CPP_SEMICOLON))
7194     {
7195       cp_token *token;
7196       bool function_definition_p;
7197       tree decl;
7198
7199       if (saw_declarator)
7200         {
7201           /* If we are processing next declarator, coma is expected */
7202           token = cp_lexer_peek_token (parser->lexer);
7203           gcc_assert (token->type == CPP_COMMA);
7204           cp_lexer_consume_token (parser->lexer);
7205         }
7206       else
7207         saw_declarator = true;
7208
7209       /* Parse the init-declarator.  */
7210       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7211                                         function_definition_allowed_p,
7212                                         /*member_p=*/false,
7213                                         declares_class_or_enum,
7214                                         &function_definition_p);
7215       /* If an error occurred while parsing tentatively, exit quickly.
7216          (That usually happens when in the body of a function; each
7217          statement is treated as a declaration-statement until proven
7218          otherwise.)  */
7219       if (cp_parser_error_occurred (parser))
7220         goto done;
7221       /* Handle function definitions specially.  */
7222       if (function_definition_p)
7223         {
7224           /* If the next token is a `,', then we are probably
7225              processing something like:
7226
7227                void f() {}, *p;
7228
7229              which is erroneous.  */
7230           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7231             error ("mixing declarations and function-definitions is forbidden");
7232           /* Otherwise, we're done with the list of declarators.  */
7233           else
7234             {
7235               pop_deferring_access_checks ();
7236               return;
7237             }
7238         }
7239       /* The next token should be either a `,' or a `;'.  */
7240       token = cp_lexer_peek_token (parser->lexer);
7241       /* If it's a `,', there are more declarators to come.  */
7242       if (token->type == CPP_COMMA)
7243         /* will be consumed next time around */;
7244       /* If it's a `;', we are done.  */
7245       else if (token->type == CPP_SEMICOLON)
7246         break;
7247       /* Anything else is an error.  */
7248       else
7249         {
7250           /* If we have already issued an error message we don't need
7251              to issue another one.  */
7252           if (decl != error_mark_node
7253               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7254             cp_parser_error (parser, "expected %<,%> or %<;%>");
7255           /* Skip tokens until we reach the end of the statement.  */
7256           cp_parser_skip_to_end_of_statement (parser);
7257           /* If the next token is now a `;', consume it.  */
7258           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7259             cp_lexer_consume_token (parser->lexer);
7260           goto done;
7261         }
7262       /* After the first time around, a function-definition is not
7263          allowed -- even if it was OK at first.  For example:
7264
7265            int i, f() {}
7266
7267          is not valid.  */
7268       function_definition_allowed_p = false;
7269     }
7270
7271   /* Issue an error message if no declarators are present, and the
7272      decl-specifier-seq does not itself declare a class or
7273      enumeration.  */
7274   if (!saw_declarator)
7275     {
7276       if (cp_parser_declares_only_class_p (parser))
7277         shadow_tag (&decl_specifiers);
7278       /* Perform any deferred access checks.  */
7279       perform_deferred_access_checks ();
7280     }
7281
7282   /* Consume the `;'.  */
7283   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7284
7285  done:
7286   pop_deferring_access_checks ();
7287 }
7288
7289 /* Parse a decl-specifier-seq.
7290
7291    decl-specifier-seq:
7292      decl-specifier-seq [opt] decl-specifier
7293
7294    decl-specifier:
7295      storage-class-specifier
7296      type-specifier
7297      function-specifier
7298      friend
7299      typedef
7300
7301    GNU Extension:
7302
7303    decl-specifier:
7304      attributes
7305
7306    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7307
7308    The parser flags FLAGS is used to control type-specifier parsing.
7309
7310    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7311    flags:
7312
7313      1: one of the decl-specifiers is an elaborated-type-specifier
7314         (i.e., a type declaration)
7315      2: one of the decl-specifiers is an enum-specifier or a
7316         class-specifier (i.e., a type definition)
7317
7318    */
7319
7320 static void
7321 cp_parser_decl_specifier_seq (cp_parser* parser,
7322                               cp_parser_flags flags,
7323                               cp_decl_specifier_seq *decl_specs,
7324                               int* declares_class_or_enum)
7325 {
7326   bool constructor_possible_p = !parser->in_declarator_p;
7327
7328   /* Clear DECL_SPECS.  */
7329   clear_decl_specs (decl_specs);
7330
7331   /* Assume no class or enumeration type is declared.  */
7332   *declares_class_or_enum = 0;
7333
7334   /* Keep reading specifiers until there are no more to read.  */
7335   while (true)
7336     {
7337       bool constructor_p;
7338       bool found_decl_spec;
7339       cp_token *token;
7340
7341       /* Peek at the next token.  */
7342       token = cp_lexer_peek_token (parser->lexer);
7343       /* Handle attributes.  */
7344       if (token->keyword == RID_ATTRIBUTE)
7345         {
7346           /* Parse the attributes.  */
7347           decl_specs->attributes
7348             = chainon (decl_specs->attributes,
7349                        cp_parser_attributes_opt (parser));
7350           continue;
7351         }
7352       /* Assume we will find a decl-specifier keyword.  */
7353       found_decl_spec = true;
7354       /* If the next token is an appropriate keyword, we can simply
7355          add it to the list.  */
7356       switch (token->keyword)
7357         {
7358           /* decl-specifier:
7359                friend  */
7360         case RID_FRIEND:
7361           if (decl_specs->specs[(int) ds_friend]++)
7362             error ("duplicate %<friend%>");
7363           /* Consume the token.  */
7364           cp_lexer_consume_token (parser->lexer);
7365           break;
7366
7367           /* function-specifier:
7368                inline
7369                virtual
7370                explicit  */
7371         case RID_INLINE:
7372         case RID_VIRTUAL:
7373         case RID_EXPLICIT:
7374           cp_parser_function_specifier_opt (parser, decl_specs);
7375           break;
7376
7377           /* decl-specifier:
7378                typedef  */
7379         case RID_TYPEDEF:
7380           ++decl_specs->specs[(int) ds_typedef];
7381           /* Consume the token.  */
7382           cp_lexer_consume_token (parser->lexer);
7383           /* A constructor declarator cannot appear in a typedef.  */
7384           constructor_possible_p = false;
7385           /* The "typedef" keyword can only occur in a declaration; we
7386              may as well commit at this point.  */
7387           cp_parser_commit_to_tentative_parse (parser);
7388           break;
7389
7390           /* storage-class-specifier:
7391                auto
7392                register
7393                static
7394                extern
7395                mutable
7396
7397              GNU Extension:
7398                thread  */
7399         case RID_AUTO:
7400           /* Consume the token.  */
7401           cp_lexer_consume_token (parser->lexer);
7402           cp_parser_set_storage_class (decl_specs, sc_auto);
7403           break;
7404         case RID_REGISTER:
7405           /* Consume the token.  */
7406           cp_lexer_consume_token (parser->lexer);
7407           cp_parser_set_storage_class (decl_specs, sc_register);
7408           break;
7409         case RID_STATIC:
7410           /* Consume the token.  */
7411           cp_lexer_consume_token (parser->lexer);
7412           if (decl_specs->specs[(int) ds_thread])
7413             {
7414               error ("%<__thread%> before %<static%>");
7415               decl_specs->specs[(int) ds_thread] = 0;
7416             }
7417           cp_parser_set_storage_class (decl_specs, sc_static);
7418           break;
7419         case RID_EXTERN:
7420           /* Consume the token.  */
7421           cp_lexer_consume_token (parser->lexer);
7422           if (decl_specs->specs[(int) ds_thread])
7423             {
7424               error ("%<__thread%> before %<extern%>");
7425               decl_specs->specs[(int) ds_thread] = 0;
7426             }
7427           cp_parser_set_storage_class (decl_specs, sc_extern);
7428           break;
7429         case RID_MUTABLE:
7430           /* Consume the token.  */
7431           cp_lexer_consume_token (parser->lexer);
7432           cp_parser_set_storage_class (decl_specs, sc_mutable);
7433           break;
7434         case RID_THREAD:
7435           /* Consume the token.  */
7436           cp_lexer_consume_token (parser->lexer);
7437           ++decl_specs->specs[(int) ds_thread];
7438           break;
7439
7440         default:
7441           /* We did not yet find a decl-specifier yet.  */
7442           found_decl_spec = false;
7443           break;
7444         }
7445
7446       /* Constructors are a special case.  The `S' in `S()' is not a
7447          decl-specifier; it is the beginning of the declarator.  */
7448       constructor_p
7449         = (!found_decl_spec
7450            && constructor_possible_p
7451            && (cp_parser_constructor_declarator_p
7452                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7453
7454       /* If we don't have a DECL_SPEC yet, then we must be looking at
7455          a type-specifier.  */
7456       if (!found_decl_spec && !constructor_p)
7457         {
7458           int decl_spec_declares_class_or_enum;
7459           bool is_cv_qualifier;
7460           tree type_spec;
7461
7462           type_spec
7463             = cp_parser_type_specifier (parser, flags,
7464                                         decl_specs,
7465                                         /*is_declaration=*/true,
7466                                         &decl_spec_declares_class_or_enum,
7467                                         &is_cv_qualifier);
7468
7469           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7470
7471           /* If this type-specifier referenced a user-defined type
7472              (a typedef, class-name, etc.), then we can't allow any
7473              more such type-specifiers henceforth.
7474
7475              [dcl.spec]
7476
7477              The longest sequence of decl-specifiers that could
7478              possibly be a type name is taken as the
7479              decl-specifier-seq of a declaration.  The sequence shall
7480              be self-consistent as described below.
7481
7482              [dcl.type]
7483
7484              As a general rule, at most one type-specifier is allowed
7485              in the complete decl-specifier-seq of a declaration.  The
7486              only exceptions are the following:
7487
7488              -- const or volatile can be combined with any other
7489                 type-specifier.
7490
7491              -- signed or unsigned can be combined with char, long,
7492                 short, or int.
7493
7494              -- ..
7495
7496              Example:
7497
7498                typedef char* Pc;
7499                void g (const int Pc);
7500
7501              Here, Pc is *not* part of the decl-specifier seq; it's
7502              the declarator.  Therefore, once we see a type-specifier
7503              (other than a cv-qualifier), we forbid any additional
7504              user-defined types.  We *do* still allow things like `int
7505              int' to be considered a decl-specifier-seq, and issue the
7506              error message later.  */
7507           if (type_spec && !is_cv_qualifier)
7508             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7509           /* A constructor declarator cannot follow a type-specifier.  */
7510           if (type_spec)
7511             {
7512               constructor_possible_p = false;
7513               found_decl_spec = true;
7514             }
7515         }
7516
7517       /* If we still do not have a DECL_SPEC, then there are no more
7518          decl-specifiers.  */
7519       if (!found_decl_spec)
7520         break;
7521
7522       decl_specs->any_specifiers_p = true;
7523       /* After we see one decl-specifier, further decl-specifiers are
7524          always optional.  */
7525       flags |= CP_PARSER_FLAGS_OPTIONAL;
7526     }
7527
7528   /* Don't allow a friend specifier with a class definition.  */
7529   if (decl_specs->specs[(int) ds_friend] != 0
7530       && (*declares_class_or_enum & 2))
7531     error ("class definition may not be declared a friend");
7532 }
7533
7534 /* Parse an (optional) storage-class-specifier.
7535
7536    storage-class-specifier:
7537      auto
7538      register
7539      static
7540      extern
7541      mutable
7542
7543    GNU Extension:
7544
7545    storage-class-specifier:
7546      thread
7547
7548    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7549
7550 static tree
7551 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7552 {
7553   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7554     {
7555     case RID_AUTO:
7556     case RID_REGISTER:
7557     case RID_STATIC:
7558     case RID_EXTERN:
7559     case RID_MUTABLE:
7560     case RID_THREAD:
7561       /* Consume the token.  */
7562       return cp_lexer_consume_token (parser->lexer)->value;
7563
7564     default:
7565       return NULL_TREE;
7566     }
7567 }
7568
7569 /* Parse an (optional) function-specifier.
7570
7571    function-specifier:
7572      inline
7573      virtual
7574      explicit
7575
7576    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7577    Updates DECL_SPECS, if it is non-NULL.  */
7578
7579 static tree
7580 cp_parser_function_specifier_opt (cp_parser* parser,
7581                                   cp_decl_specifier_seq *decl_specs)
7582 {
7583   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7584     {
7585     case RID_INLINE:
7586       if (decl_specs)
7587         ++decl_specs->specs[(int) ds_inline];
7588       break;
7589
7590     case RID_VIRTUAL:
7591       if (decl_specs)
7592         ++decl_specs->specs[(int) ds_virtual];
7593       break;
7594
7595     case RID_EXPLICIT:
7596       if (decl_specs)
7597         ++decl_specs->specs[(int) ds_explicit];
7598       break;
7599
7600     default:
7601       return NULL_TREE;
7602     }
7603
7604   /* Consume the token.  */
7605   return cp_lexer_consume_token (parser->lexer)->value;
7606 }
7607
7608 /* Parse a linkage-specification.
7609
7610    linkage-specification:
7611      extern string-literal { declaration-seq [opt] }
7612      extern string-literal declaration  */
7613
7614 static void
7615 cp_parser_linkage_specification (cp_parser* parser)
7616 {
7617   tree linkage;
7618
7619   /* Look for the `extern' keyword.  */
7620   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7621
7622   /* Look for the string-literal.  */
7623   linkage = cp_parser_string_literal (parser, false, false);
7624
7625   /* Transform the literal into an identifier.  If the literal is a
7626      wide-character string, or contains embedded NULs, then we can't
7627      handle it as the user wants.  */
7628   if (strlen (TREE_STRING_POINTER (linkage))
7629       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7630     {
7631       cp_parser_error (parser, "invalid linkage-specification");
7632       /* Assume C++ linkage.  */
7633       linkage = lang_name_cplusplus;
7634     }
7635   else
7636     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7637
7638   /* We're now using the new linkage.  */
7639   push_lang_context (linkage);
7640
7641   /* If the next token is a `{', then we're using the first
7642      production.  */
7643   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7644     {
7645       /* Consume the `{' token.  */
7646       cp_lexer_consume_token (parser->lexer);
7647       /* Parse the declarations.  */
7648       cp_parser_declaration_seq_opt (parser);
7649       /* Look for the closing `}'.  */
7650       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7651     }
7652   /* Otherwise, there's just one declaration.  */
7653   else
7654     {
7655       bool saved_in_unbraced_linkage_specification_p;
7656
7657       saved_in_unbraced_linkage_specification_p
7658         = parser->in_unbraced_linkage_specification_p;
7659       parser->in_unbraced_linkage_specification_p = true;
7660       have_extern_spec = true;
7661       cp_parser_declaration (parser);
7662       have_extern_spec = false;
7663       parser->in_unbraced_linkage_specification_p
7664         = saved_in_unbraced_linkage_specification_p;
7665     }
7666
7667   /* We're done with the linkage-specification.  */
7668   pop_lang_context ();
7669 }
7670
7671 /* Special member functions [gram.special] */
7672
7673 /* Parse a conversion-function-id.
7674
7675    conversion-function-id:
7676      operator conversion-type-id
7677
7678    Returns an IDENTIFIER_NODE representing the operator.  */
7679
7680 static tree
7681 cp_parser_conversion_function_id (cp_parser* parser)
7682 {
7683   tree type;
7684   tree saved_scope;
7685   tree saved_qualifying_scope;
7686   tree saved_object_scope;
7687   tree pushed_scope = NULL_TREE;
7688
7689   /* Look for the `operator' token.  */
7690   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7691     return error_mark_node;
7692   /* When we parse the conversion-type-id, the current scope will be
7693      reset.  However, we need that information in able to look up the
7694      conversion function later, so we save it here.  */
7695   saved_scope = parser->scope;
7696   saved_qualifying_scope = parser->qualifying_scope;
7697   saved_object_scope = parser->object_scope;
7698   /* We must enter the scope of the class so that the names of
7699      entities declared within the class are available in the
7700      conversion-type-id.  For example, consider:
7701
7702        struct S {
7703          typedef int I;
7704          operator I();
7705        };
7706
7707        S::operator I() { ... }
7708
7709      In order to see that `I' is a type-name in the definition, we
7710      must be in the scope of `S'.  */
7711   if (saved_scope)
7712     pushed_scope = push_scope (saved_scope);
7713   /* Parse the conversion-type-id.  */
7714   type = cp_parser_conversion_type_id (parser);
7715   /* Leave the scope of the class, if any.  */
7716   if (pushed_scope)
7717     pop_scope (pushed_scope);
7718   /* Restore the saved scope.  */
7719   parser->scope = saved_scope;
7720   parser->qualifying_scope = saved_qualifying_scope;
7721   parser->object_scope = saved_object_scope;
7722   /* If the TYPE is invalid, indicate failure.  */
7723   if (type == error_mark_node)
7724     return error_mark_node;
7725   return mangle_conv_op_name_for_type (type);
7726 }
7727
7728 /* Parse a conversion-type-id:
7729
7730    conversion-type-id:
7731      type-specifier-seq conversion-declarator [opt]
7732
7733    Returns the TYPE specified.  */
7734
7735 static tree
7736 cp_parser_conversion_type_id (cp_parser* parser)
7737 {
7738   tree attributes;
7739   cp_decl_specifier_seq type_specifiers;
7740   cp_declarator *declarator;
7741   tree type_specified;
7742
7743   /* Parse the attributes.  */
7744   attributes = cp_parser_attributes_opt (parser);
7745   /* Parse the type-specifiers.  */
7746   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7747                                 &type_specifiers);
7748   /* If that didn't work, stop.  */
7749   if (type_specifiers.type == error_mark_node)
7750     return error_mark_node;
7751   /* Parse the conversion-declarator.  */
7752   declarator = cp_parser_conversion_declarator_opt (parser);
7753
7754   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7755                                     /*initialized=*/0, &attributes);
7756   if (attributes)
7757     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7758   return type_specified;
7759 }
7760
7761 /* Parse an (optional) conversion-declarator.
7762
7763    conversion-declarator:
7764      ptr-operator conversion-declarator [opt]
7765
7766    */
7767
7768 static cp_declarator *
7769 cp_parser_conversion_declarator_opt (cp_parser* parser)
7770 {
7771   enum tree_code code;
7772   tree class_type;
7773   cp_cv_quals cv_quals;
7774
7775   /* We don't know if there's a ptr-operator next, or not.  */
7776   cp_parser_parse_tentatively (parser);
7777   /* Try the ptr-operator.  */
7778   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7779   /* If it worked, look for more conversion-declarators.  */
7780   if (cp_parser_parse_definitely (parser))
7781     {
7782       cp_declarator *declarator;
7783
7784       /* Parse another optional declarator.  */
7785       declarator = cp_parser_conversion_declarator_opt (parser);
7786
7787       /* Create the representation of the declarator.  */
7788       if (class_type)
7789         declarator = make_ptrmem_declarator (cv_quals, class_type,
7790                                              declarator);
7791       else if (code == INDIRECT_REF)
7792         declarator = make_pointer_declarator (cv_quals, declarator);
7793       else
7794         declarator = make_reference_declarator (cv_quals, declarator);
7795
7796       return declarator;
7797    }
7798
7799   return NULL;
7800 }
7801
7802 /* Parse an (optional) ctor-initializer.
7803
7804    ctor-initializer:
7805      : mem-initializer-list
7806
7807    Returns TRUE iff the ctor-initializer was actually present.  */
7808
7809 static bool
7810 cp_parser_ctor_initializer_opt (cp_parser* parser)
7811 {
7812   /* If the next token is not a `:', then there is no
7813      ctor-initializer.  */
7814   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7815     {
7816       /* Do default initialization of any bases and members.  */
7817       if (DECL_CONSTRUCTOR_P (current_function_decl))
7818         finish_mem_initializers (NULL_TREE);
7819
7820       return false;
7821     }
7822
7823   /* Consume the `:' token.  */
7824   cp_lexer_consume_token (parser->lexer);
7825   /* And the mem-initializer-list.  */
7826   cp_parser_mem_initializer_list (parser);
7827
7828   return true;
7829 }
7830
7831 /* Parse a mem-initializer-list.
7832
7833    mem-initializer-list:
7834      mem-initializer
7835      mem-initializer , mem-initializer-list  */
7836
7837 static void
7838 cp_parser_mem_initializer_list (cp_parser* parser)
7839 {
7840   tree mem_initializer_list = NULL_TREE;
7841
7842   /* Let the semantic analysis code know that we are starting the
7843      mem-initializer-list.  */
7844   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7845     error ("only constructors take base initializers");
7846
7847   /* Loop through the list.  */
7848   while (true)
7849     {
7850       tree mem_initializer;
7851
7852       /* Parse the mem-initializer.  */
7853       mem_initializer = cp_parser_mem_initializer (parser);
7854       /* Add it to the list, unless it was erroneous.  */
7855       if (mem_initializer != error_mark_node)
7856         {
7857           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7858           mem_initializer_list = mem_initializer;
7859         }
7860       /* If the next token is not a `,', we're done.  */
7861       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7862         break;
7863       /* Consume the `,' token.  */
7864       cp_lexer_consume_token (parser->lexer);
7865     }
7866
7867   /* Perform semantic analysis.  */
7868   if (DECL_CONSTRUCTOR_P (current_function_decl))
7869     finish_mem_initializers (mem_initializer_list);
7870 }
7871
7872 /* Parse a mem-initializer.
7873
7874    mem-initializer:
7875      mem-initializer-id ( expression-list [opt] )
7876
7877    GNU extension:
7878
7879    mem-initializer:
7880      ( expression-list [opt] )
7881
7882    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7883    class) or FIELD_DECL (for a non-static data member) to initialize;
7884    the TREE_VALUE is the expression-list.  An empty initialization
7885    list is represented by void_list_node.  */
7886
7887 static tree
7888 cp_parser_mem_initializer (cp_parser* parser)
7889 {
7890   tree mem_initializer_id;
7891   tree expression_list;
7892   tree member;
7893
7894   /* Find out what is being initialized.  */
7895   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7896     {
7897       pedwarn ("anachronistic old-style base class initializer");
7898       mem_initializer_id = NULL_TREE;
7899     }
7900   else
7901     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7902   member = expand_member_init (mem_initializer_id);
7903   if (member && !DECL_P (member))
7904     in_base_initializer = 1;
7905
7906   expression_list
7907     = cp_parser_parenthesized_expression_list (parser, false,
7908                                                /*cast_p=*/false,
7909                                                /*non_constant_p=*/NULL);
7910   if (expression_list == error_mark_node)
7911     return error_mark_node;
7912   if (!expression_list)
7913     expression_list = void_type_node;
7914
7915   in_base_initializer = 0;
7916
7917   return member ? build_tree_list (member, expression_list) : error_mark_node;
7918 }
7919
7920 /* Parse a mem-initializer-id.
7921
7922    mem-initializer-id:
7923      :: [opt] nested-name-specifier [opt] class-name
7924      identifier
7925
7926    Returns a TYPE indicating the class to be initializer for the first
7927    production.  Returns an IDENTIFIER_NODE indicating the data member
7928    to be initialized for the second production.  */
7929
7930 static tree
7931 cp_parser_mem_initializer_id (cp_parser* parser)
7932 {
7933   bool global_scope_p;
7934   bool nested_name_specifier_p;
7935   bool template_p = false;
7936   tree id;
7937
7938   /* `typename' is not allowed in this context ([temp.res]).  */
7939   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7940     {
7941       error ("keyword %<typename%> not allowed in this context (a qualified "
7942              "member initializer is implicitly a type)");
7943       cp_lexer_consume_token (parser->lexer);
7944     }
7945   /* Look for the optional `::' operator.  */
7946   global_scope_p
7947     = (cp_parser_global_scope_opt (parser,
7948                                    /*current_scope_valid_p=*/false)
7949        != NULL_TREE);
7950   /* Look for the optional nested-name-specifier.  The simplest way to
7951      implement:
7952
7953        [temp.res]
7954
7955        The keyword `typename' is not permitted in a base-specifier or
7956        mem-initializer; in these contexts a qualified name that
7957        depends on a template-parameter is implicitly assumed to be a
7958        type name.
7959
7960      is to assume that we have seen the `typename' keyword at this
7961      point.  */
7962   nested_name_specifier_p
7963     = (cp_parser_nested_name_specifier_opt (parser,
7964                                             /*typename_keyword_p=*/true,
7965                                             /*check_dependency_p=*/true,
7966                                             /*type_p=*/true,
7967                                             /*is_declaration=*/true)
7968        != NULL_TREE);
7969   if (nested_name_specifier_p)
7970     template_p = cp_parser_optional_template_keyword (parser);
7971   /* If there is a `::' operator or a nested-name-specifier, then we
7972      are definitely looking for a class-name.  */
7973   if (global_scope_p || nested_name_specifier_p)
7974     return cp_parser_class_name (parser,
7975                                  /*typename_keyword_p=*/true,
7976                                  /*template_keyword_p=*/template_p,
7977                                  none_type,
7978                                  /*check_dependency_p=*/true,
7979                                  /*class_head_p=*/false,
7980                                  /*is_declaration=*/true);
7981   /* Otherwise, we could also be looking for an ordinary identifier.  */
7982   cp_parser_parse_tentatively (parser);
7983   /* Try a class-name.  */
7984   id = cp_parser_class_name (parser,
7985                              /*typename_keyword_p=*/true,
7986                              /*template_keyword_p=*/false,
7987                              none_type,
7988                              /*check_dependency_p=*/true,
7989                              /*class_head_p=*/false,
7990                              /*is_declaration=*/true);
7991   /* If we found one, we're done.  */
7992   if (cp_parser_parse_definitely (parser))
7993     return id;
7994   /* Otherwise, look for an ordinary identifier.  */
7995   return cp_parser_identifier (parser);
7996 }
7997
7998 /* Overloading [gram.over] */
7999
8000 /* Parse an operator-function-id.
8001
8002    operator-function-id:
8003      operator operator
8004
8005    Returns an IDENTIFIER_NODE for the operator which is a
8006    human-readable spelling of the identifier, e.g., `operator +'.  */
8007
8008 static tree
8009 cp_parser_operator_function_id (cp_parser* parser)
8010 {
8011   /* Look for the `operator' keyword.  */
8012   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8013     return error_mark_node;
8014   /* And then the name of the operator itself.  */
8015   return cp_parser_operator (parser);
8016 }
8017
8018 /* Parse an operator.
8019
8020    operator:
8021      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8022      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8023      || ++ -- , ->* -> () []
8024
8025    GNU Extensions:
8026
8027    operator:
8028      <? >? <?= >?=
8029
8030    Returns an IDENTIFIER_NODE for the operator which is a
8031    human-readable spelling of the identifier, e.g., `operator +'.  */
8032
8033 static tree
8034 cp_parser_operator (cp_parser* parser)
8035 {
8036   tree id = NULL_TREE;
8037   cp_token *token;
8038
8039   /* Peek at the next token.  */
8040   token = cp_lexer_peek_token (parser->lexer);
8041   /* Figure out which operator we have.  */
8042   switch (token->type)
8043     {
8044     case CPP_KEYWORD:
8045       {
8046         enum tree_code op;
8047
8048         /* The keyword should be either `new' or `delete'.  */
8049         if (token->keyword == RID_NEW)
8050           op = NEW_EXPR;
8051         else if (token->keyword == RID_DELETE)
8052           op = DELETE_EXPR;
8053         else
8054           break;
8055
8056         /* Consume the `new' or `delete' token.  */
8057         cp_lexer_consume_token (parser->lexer);
8058
8059         /* Peek at the next token.  */
8060         token = cp_lexer_peek_token (parser->lexer);
8061         /* If it's a `[' token then this is the array variant of the
8062            operator.  */
8063         if (token->type == CPP_OPEN_SQUARE)
8064           {
8065             /* Consume the `[' token.  */
8066             cp_lexer_consume_token (parser->lexer);
8067             /* Look for the `]' token.  */
8068             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8069             id = ansi_opname (op == NEW_EXPR
8070                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8071           }
8072         /* Otherwise, we have the non-array variant.  */
8073         else
8074           id = ansi_opname (op);
8075
8076         return id;
8077       }
8078
8079     case CPP_PLUS:
8080       id = ansi_opname (PLUS_EXPR);
8081       break;
8082
8083     case CPP_MINUS:
8084       id = ansi_opname (MINUS_EXPR);
8085       break;
8086
8087     case CPP_MULT:
8088       id = ansi_opname (MULT_EXPR);
8089       break;
8090
8091     case CPP_DIV:
8092       id = ansi_opname (TRUNC_DIV_EXPR);
8093       break;
8094
8095     case CPP_MOD:
8096       id = ansi_opname (TRUNC_MOD_EXPR);
8097       break;
8098
8099     case CPP_XOR:
8100       id = ansi_opname (BIT_XOR_EXPR);
8101       break;
8102
8103     case CPP_AND:
8104       id = ansi_opname (BIT_AND_EXPR);
8105       break;
8106
8107     case CPP_OR:
8108       id = ansi_opname (BIT_IOR_EXPR);
8109       break;
8110
8111     case CPP_COMPL:
8112       id = ansi_opname (BIT_NOT_EXPR);
8113       break;
8114
8115     case CPP_NOT:
8116       id = ansi_opname (TRUTH_NOT_EXPR);
8117       break;
8118
8119     case CPP_EQ:
8120       id = ansi_assopname (NOP_EXPR);
8121       break;
8122
8123     case CPP_LESS:
8124       id = ansi_opname (LT_EXPR);
8125       break;
8126
8127     case CPP_GREATER:
8128       id = ansi_opname (GT_EXPR);
8129       break;
8130
8131     case CPP_PLUS_EQ:
8132       id = ansi_assopname (PLUS_EXPR);
8133       break;
8134
8135     case CPP_MINUS_EQ:
8136       id = ansi_assopname (MINUS_EXPR);
8137       break;
8138
8139     case CPP_MULT_EQ:
8140       id = ansi_assopname (MULT_EXPR);
8141       break;
8142
8143     case CPP_DIV_EQ:
8144       id = ansi_assopname (TRUNC_DIV_EXPR);
8145       break;
8146
8147     case CPP_MOD_EQ:
8148       id = ansi_assopname (TRUNC_MOD_EXPR);
8149       break;
8150
8151     case CPP_XOR_EQ:
8152       id = ansi_assopname (BIT_XOR_EXPR);
8153       break;
8154
8155     case CPP_AND_EQ:
8156       id = ansi_assopname (BIT_AND_EXPR);
8157       break;
8158
8159     case CPP_OR_EQ:
8160       id = ansi_assopname (BIT_IOR_EXPR);
8161       break;
8162
8163     case CPP_LSHIFT:
8164       id = ansi_opname (LSHIFT_EXPR);
8165       break;
8166
8167     case CPP_RSHIFT:
8168       id = ansi_opname (RSHIFT_EXPR);
8169       break;
8170
8171     case CPP_LSHIFT_EQ:
8172       id = ansi_assopname (LSHIFT_EXPR);
8173       break;
8174
8175     case CPP_RSHIFT_EQ:
8176       id = ansi_assopname (RSHIFT_EXPR);
8177       break;
8178
8179     case CPP_EQ_EQ:
8180       id = ansi_opname (EQ_EXPR);
8181       break;
8182
8183     case CPP_NOT_EQ:
8184       id = ansi_opname (NE_EXPR);
8185       break;
8186
8187     case CPP_LESS_EQ:
8188       id = ansi_opname (LE_EXPR);
8189       break;
8190
8191     case CPP_GREATER_EQ:
8192       id = ansi_opname (GE_EXPR);
8193       break;
8194
8195     case CPP_AND_AND:
8196       id = ansi_opname (TRUTH_ANDIF_EXPR);
8197       break;
8198
8199     case CPP_OR_OR:
8200       id = ansi_opname (TRUTH_ORIF_EXPR);
8201       break;
8202
8203     case CPP_PLUS_PLUS:
8204       id = ansi_opname (POSTINCREMENT_EXPR);
8205       break;
8206
8207     case CPP_MINUS_MINUS:
8208       id = ansi_opname (PREDECREMENT_EXPR);
8209       break;
8210
8211     case CPP_COMMA:
8212       id = ansi_opname (COMPOUND_EXPR);
8213       break;
8214
8215     case CPP_DEREF_STAR:
8216       id = ansi_opname (MEMBER_REF);
8217       break;
8218
8219     case CPP_DEREF:
8220       id = ansi_opname (COMPONENT_REF);
8221       break;
8222
8223     case CPP_OPEN_PAREN:
8224       /* Consume the `('.  */
8225       cp_lexer_consume_token (parser->lexer);
8226       /* Look for the matching `)'.  */
8227       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8228       return ansi_opname (CALL_EXPR);
8229
8230     case CPP_OPEN_SQUARE:
8231       /* Consume the `['.  */
8232       cp_lexer_consume_token (parser->lexer);
8233       /* Look for the matching `]'.  */
8234       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8235       return ansi_opname (ARRAY_REF);
8236
8237       /* Extensions.  */
8238     case CPP_MIN:
8239       id = ansi_opname (MIN_EXPR);
8240       cp_parser_warn_min_max ();
8241       break;
8242
8243     case CPP_MAX:
8244       id = ansi_opname (MAX_EXPR);
8245       cp_parser_warn_min_max ();
8246       break;
8247
8248     case CPP_MIN_EQ:
8249       id = ansi_assopname (MIN_EXPR);
8250       cp_parser_warn_min_max ();
8251       break;
8252
8253     case CPP_MAX_EQ:
8254       id = ansi_assopname (MAX_EXPR);
8255       cp_parser_warn_min_max ();
8256       break;
8257
8258     default:
8259       /* Anything else is an error.  */
8260       break;
8261     }
8262
8263   /* If we have selected an identifier, we need to consume the
8264      operator token.  */
8265   if (id)
8266     cp_lexer_consume_token (parser->lexer);
8267   /* Otherwise, no valid operator name was present.  */
8268   else
8269     {
8270       cp_parser_error (parser, "expected operator");
8271       id = error_mark_node;
8272     }
8273
8274   return id;
8275 }
8276
8277 /* Parse a template-declaration.
8278
8279    template-declaration:
8280      export [opt] template < template-parameter-list > declaration
8281
8282    If MEMBER_P is TRUE, this template-declaration occurs within a
8283    class-specifier.
8284
8285    The grammar rule given by the standard isn't correct.  What
8286    is really meant is:
8287
8288    template-declaration:
8289      export [opt] template-parameter-list-seq
8290        decl-specifier-seq [opt] init-declarator [opt] ;
8291      export [opt] template-parameter-list-seq
8292        function-definition
8293
8294    template-parameter-list-seq:
8295      template-parameter-list-seq [opt]
8296      template < template-parameter-list >  */
8297
8298 static void
8299 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8300 {
8301   /* Check for `export'.  */
8302   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8303     {
8304       /* Consume the `export' token.  */
8305       cp_lexer_consume_token (parser->lexer);
8306       /* Warn that we do not support `export'.  */
8307       warning (0, "keyword %<export%> not implemented, and will be ignored");
8308     }
8309
8310   cp_parser_template_declaration_after_export (parser, member_p);
8311 }
8312
8313 /* Parse a template-parameter-list.
8314
8315    template-parameter-list:
8316      template-parameter
8317      template-parameter-list , template-parameter
8318
8319    Returns a TREE_LIST.  Each node represents a template parameter.
8320    The nodes are connected via their TREE_CHAINs.  */
8321
8322 static tree
8323 cp_parser_template_parameter_list (cp_parser* parser)
8324 {
8325   tree parameter_list = NULL_TREE;
8326
8327   begin_template_parm_list ();
8328   while (true)
8329     {
8330       tree parameter;
8331       cp_token *token;
8332       bool is_non_type;
8333
8334       /* Parse the template-parameter.  */
8335       parameter = cp_parser_template_parameter (parser, &is_non_type);
8336       /* Add it to the list.  */
8337       if (parameter != error_mark_node)
8338         parameter_list = process_template_parm (parameter_list,
8339                                                 parameter,
8340                                                 is_non_type);
8341       /* Peek at the next token.  */
8342       token = cp_lexer_peek_token (parser->lexer);
8343       /* If it's not a `,', we're done.  */
8344       if (token->type != CPP_COMMA)
8345         break;
8346       /* Otherwise, consume the `,' token.  */
8347       cp_lexer_consume_token (parser->lexer);
8348     }
8349
8350   return end_template_parm_list (parameter_list);
8351 }
8352
8353 /* Parse a template-parameter.
8354
8355    template-parameter:
8356      type-parameter
8357      parameter-declaration
8358
8359    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8360    the parameter.  The TREE_PURPOSE is the default value, if any.
8361    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8362    iff this parameter is a non-type parameter.  */
8363
8364 static tree
8365 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8366 {
8367   cp_token *token;
8368   cp_parameter_declarator *parameter_declarator;
8369   tree parm;
8370
8371   /* Assume it is a type parameter or a template parameter.  */
8372   *is_non_type = false;
8373   /* Peek at the next token.  */
8374   token = cp_lexer_peek_token (parser->lexer);
8375   /* If it is `class' or `template', we have a type-parameter.  */
8376   if (token->keyword == RID_TEMPLATE)
8377     return cp_parser_type_parameter (parser);
8378   /* If it is `class' or `typename' we do not know yet whether it is a
8379      type parameter or a non-type parameter.  Consider:
8380
8381        template <typename T, typename T::X X> ...
8382
8383      or:
8384
8385        template <class C, class D*> ...
8386
8387      Here, the first parameter is a type parameter, and the second is
8388      a non-type parameter.  We can tell by looking at the token after
8389      the identifier -- if it is a `,', `=', or `>' then we have a type
8390      parameter.  */
8391   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8392     {
8393       /* Peek at the token after `class' or `typename'.  */
8394       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8395       /* If it's an identifier, skip it.  */
8396       if (token->type == CPP_NAME)
8397         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8398       /* Now, see if the token looks like the end of a template
8399          parameter.  */
8400       if (token->type == CPP_COMMA
8401           || token->type == CPP_EQ
8402           || token->type == CPP_GREATER)
8403         return cp_parser_type_parameter (parser);
8404     }
8405
8406   /* Otherwise, it is a non-type parameter.
8407
8408      [temp.param]
8409
8410      When parsing a default template-argument for a non-type
8411      template-parameter, the first non-nested `>' is taken as the end
8412      of the template parameter-list rather than a greater-than
8413      operator.  */
8414   *is_non_type = true;
8415   parameter_declarator
8416      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8417                                         /*parenthesized_p=*/NULL);
8418   parm = grokdeclarator (parameter_declarator->declarator,
8419                          &parameter_declarator->decl_specifiers,
8420                          PARM, /*initialized=*/0,
8421                          /*attrlist=*/NULL);
8422   if (parm == error_mark_node)
8423     return error_mark_node;
8424   return build_tree_list (parameter_declarator->default_argument, parm);
8425 }
8426
8427 /* Parse a type-parameter.
8428
8429    type-parameter:
8430      class identifier [opt]
8431      class identifier [opt] = type-id
8432      typename identifier [opt]
8433      typename identifier [opt] = type-id
8434      template < template-parameter-list > class identifier [opt]
8435      template < template-parameter-list > class identifier [opt]
8436        = id-expression
8437
8438    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8439    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8440    the declaration of the parameter.  */
8441
8442 static tree
8443 cp_parser_type_parameter (cp_parser* parser)
8444 {
8445   cp_token *token;
8446   tree parameter;
8447
8448   /* Look for a keyword to tell us what kind of parameter this is.  */
8449   token = cp_parser_require (parser, CPP_KEYWORD,
8450                              "`class', `typename', or `template'");
8451   if (!token)
8452     return error_mark_node;
8453
8454   switch (token->keyword)
8455     {
8456     case RID_CLASS:
8457     case RID_TYPENAME:
8458       {
8459         tree identifier;
8460         tree default_argument;
8461
8462         /* If the next token is an identifier, then it names the
8463            parameter.  */
8464         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8465           identifier = cp_parser_identifier (parser);
8466         else
8467           identifier = NULL_TREE;
8468
8469         /* Create the parameter.  */
8470         parameter = finish_template_type_parm (class_type_node, identifier);
8471
8472         /* If the next token is an `=', we have a default argument.  */
8473         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8474           {
8475             /* Consume the `=' token.  */
8476             cp_lexer_consume_token (parser->lexer);
8477             /* Parse the default-argument.  */
8478             default_argument = cp_parser_type_id (parser);
8479           }
8480         else
8481           default_argument = NULL_TREE;
8482
8483         /* Create the combined representation of the parameter and the
8484            default argument.  */
8485         parameter = build_tree_list (default_argument, parameter);
8486       }
8487       break;
8488
8489     case RID_TEMPLATE:
8490       {
8491         tree parameter_list;
8492         tree identifier;
8493         tree default_argument;
8494
8495         /* Look for the `<'.  */
8496         cp_parser_require (parser, CPP_LESS, "`<'");
8497         /* Parse the template-parameter-list.  */
8498         parameter_list = cp_parser_template_parameter_list (parser);
8499         /* Look for the `>'.  */
8500         cp_parser_require (parser, CPP_GREATER, "`>'");
8501         /* Look for the `class' keyword.  */
8502         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8503         /* If the next token is an `=', then there is a
8504            default-argument.  If the next token is a `>', we are at
8505            the end of the parameter-list.  If the next token is a `,',
8506            then we are at the end of this parameter.  */
8507         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8508             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8509             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8510           {
8511             identifier = cp_parser_identifier (parser);
8512             /* Treat invalid names as if the parameter were nameless.  */
8513             if (identifier == error_mark_node)
8514               identifier = NULL_TREE;
8515           }
8516         else
8517           identifier = NULL_TREE;
8518
8519         /* Create the template parameter.  */
8520         parameter = finish_template_template_parm (class_type_node,
8521                                                    identifier);
8522
8523         /* If the next token is an `=', then there is a
8524            default-argument.  */
8525         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8526           {
8527             bool is_template;
8528
8529             /* Consume the `='.  */
8530             cp_lexer_consume_token (parser->lexer);
8531             /* Parse the id-expression.  */
8532             default_argument
8533               = cp_parser_id_expression (parser,
8534                                          /*template_keyword_p=*/false,
8535                                          /*check_dependency_p=*/true,
8536                                          /*template_p=*/&is_template,
8537                                          /*declarator_p=*/false);
8538             if (TREE_CODE (default_argument) == TYPE_DECL)
8539               /* If the id-expression was a template-id that refers to
8540                  a template-class, we already have the declaration here,
8541                  so no further lookup is needed.  */
8542                  ;
8543             else
8544               /* Look up the name.  */
8545               default_argument
8546                 = cp_parser_lookup_name (parser, default_argument,
8547                                          none_type,
8548                                          /*is_template=*/is_template,
8549                                          /*is_namespace=*/false,
8550                                          /*check_dependency=*/true,
8551                                          /*ambiguous_decls=*/NULL);
8552             /* See if the default argument is valid.  */
8553             default_argument
8554               = check_template_template_default_arg (default_argument);
8555           }
8556         else
8557           default_argument = NULL_TREE;
8558
8559         /* Create the combined representation of the parameter and the
8560            default argument.  */
8561         parameter = build_tree_list (default_argument, parameter);
8562       }
8563       break;
8564
8565     default:
8566       gcc_unreachable ();
8567       break;
8568     }
8569
8570   return parameter;
8571 }
8572
8573 /* Parse a template-id.
8574
8575    template-id:
8576      template-name < template-argument-list [opt] >
8577
8578    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8579    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8580    returned.  Otherwise, if the template-name names a function, or set
8581    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8582    names a class, returns a TYPE_DECL for the specialization.
8583
8584    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8585    uninstantiated templates.  */
8586
8587 static tree
8588 cp_parser_template_id (cp_parser *parser,
8589                        bool template_keyword_p,
8590                        bool check_dependency_p,
8591                        bool is_declaration)
8592 {
8593   tree template;
8594   tree arguments;
8595   tree template_id;
8596   cp_token_position start_of_id = 0;
8597   tree access_check = NULL_TREE;
8598   cp_token *next_token, *next_token_2;
8599   bool is_identifier;
8600
8601   /* If the next token corresponds to a template-id, there is no need
8602      to reparse it.  */
8603   next_token = cp_lexer_peek_token (parser->lexer);
8604   if (next_token->type == CPP_TEMPLATE_ID)
8605     {
8606       tree value;
8607       tree check;
8608
8609       /* Get the stored value.  */
8610       value = cp_lexer_consume_token (parser->lexer)->value;
8611       /* Perform any access checks that were deferred.  */
8612       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8613         perform_or_defer_access_check (TREE_PURPOSE (check),
8614                                        TREE_VALUE (check));
8615       /* Return the stored value.  */
8616       return TREE_VALUE (value);
8617     }
8618
8619   /* Avoid performing name lookup if there is no possibility of
8620      finding a template-id.  */
8621   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8622       || (next_token->type == CPP_NAME
8623           && !cp_parser_nth_token_starts_template_argument_list_p
8624                (parser, 2)))
8625     {
8626       cp_parser_error (parser, "expected template-id");
8627       return error_mark_node;
8628     }
8629
8630   /* Remember where the template-id starts.  */
8631   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8632     start_of_id = cp_lexer_token_position (parser->lexer, false);
8633
8634   push_deferring_access_checks (dk_deferred);
8635
8636   /* Parse the template-name.  */
8637   is_identifier = false;
8638   template = cp_parser_template_name (parser, template_keyword_p,
8639                                       check_dependency_p,
8640                                       is_declaration,
8641                                       &is_identifier);
8642   if (template == error_mark_node || is_identifier)
8643     {
8644       pop_deferring_access_checks ();
8645       return template;
8646     }
8647
8648   /* If we find the sequence `[:' after a template-name, it's probably
8649      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8650      parse correctly the argument list.  */
8651   next_token = cp_lexer_peek_token (parser->lexer);
8652   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8653   if (next_token->type == CPP_OPEN_SQUARE
8654       && next_token->flags & DIGRAPH
8655       && next_token_2->type == CPP_COLON
8656       && !(next_token_2->flags & PREV_WHITE))
8657     {
8658       cp_parser_parse_tentatively (parser);
8659       /* Change `:' into `::'.  */
8660       next_token_2->type = CPP_SCOPE;
8661       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8662          CPP_LESS.  */
8663       cp_lexer_consume_token (parser->lexer);
8664       /* Parse the arguments.  */
8665       arguments = cp_parser_enclosed_template_argument_list (parser);
8666       if (!cp_parser_parse_definitely (parser))
8667         {
8668           /* If we couldn't parse an argument list, then we revert our changes
8669              and return simply an error. Maybe this is not a template-id
8670              after all.  */
8671           next_token_2->type = CPP_COLON;
8672           cp_parser_error (parser, "expected %<<%>");
8673           pop_deferring_access_checks ();
8674           return error_mark_node;
8675         }
8676       /* Otherwise, emit an error about the invalid digraph, but continue
8677          parsing because we got our argument list.  */
8678       pedwarn ("%<<::%> cannot begin a template-argument list");
8679       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8680               "between %<<%> and %<::%>");
8681       if (!flag_permissive)
8682         {
8683           static bool hint;
8684           if (!hint)
8685             {
8686               inform ("(if you use -fpermissive G++ will accept your code)");
8687               hint = true;
8688             }
8689         }
8690     }
8691   else
8692     {
8693       /* Look for the `<' that starts the template-argument-list.  */
8694       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8695         {
8696           pop_deferring_access_checks ();
8697           return error_mark_node;
8698         }
8699       /* Parse the arguments.  */
8700       arguments = cp_parser_enclosed_template_argument_list (parser);
8701     }
8702
8703   /* Build a representation of the specialization.  */
8704   if (TREE_CODE (template) == IDENTIFIER_NODE)
8705     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8706   else if (DECL_CLASS_TEMPLATE_P (template)
8707            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8708     template_id
8709       = finish_template_type (template, arguments,
8710                               cp_lexer_next_token_is (parser->lexer,
8711                                                       CPP_SCOPE));
8712   else
8713     {
8714       /* If it's not a class-template or a template-template, it should be
8715          a function-template.  */
8716       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8717                    || TREE_CODE (template) == OVERLOAD
8718                    || BASELINK_P (template)));
8719
8720       template_id = lookup_template_function (template, arguments);
8721     }
8722
8723   /* Retrieve any deferred checks.  Do not pop this access checks yet
8724      so the memory will not be reclaimed during token replacing below.  */
8725   access_check = get_deferred_access_checks ();
8726
8727   /* If parsing tentatively, replace the sequence of tokens that makes
8728      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8729      should we re-parse the token stream, we will not have to repeat
8730      the effort required to do the parse, nor will we issue duplicate
8731      error messages about problems during instantiation of the
8732      template.  */
8733   if (start_of_id)
8734     {
8735       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8736
8737       /* Reset the contents of the START_OF_ID token.  */
8738       token->type = CPP_TEMPLATE_ID;
8739       token->value = build_tree_list (access_check, template_id);
8740       token->keyword = RID_MAX;
8741
8742       /* Purge all subsequent tokens.  */
8743       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8744
8745       /* ??? Can we actually assume that, if template_id ==
8746          error_mark_node, we will have issued a diagnostic to the
8747          user, as opposed to simply marking the tentative parse as
8748          failed?  */
8749       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8750         error ("parse error in template argument list");
8751     }
8752
8753   pop_deferring_access_checks ();
8754   return template_id;
8755 }
8756
8757 /* Parse a template-name.
8758
8759    template-name:
8760      identifier
8761
8762    The standard should actually say:
8763
8764    template-name:
8765      identifier
8766      operator-function-id
8767
8768    A defect report has been filed about this issue.
8769
8770    A conversion-function-id cannot be a template name because they cannot
8771    be part of a template-id. In fact, looking at this code:
8772
8773    a.operator K<int>()
8774
8775    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8776    It is impossible to call a templated conversion-function-id with an
8777    explicit argument list, since the only allowed template parameter is
8778    the type to which it is converting.
8779
8780    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8781    `template' keyword, in a construction like:
8782
8783      T::template f<3>()
8784
8785    In that case `f' is taken to be a template-name, even though there
8786    is no way of knowing for sure.
8787
8788    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8789    name refers to a set of overloaded functions, at least one of which
8790    is a template, or an IDENTIFIER_NODE with the name of the template,
8791    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8792    names are looked up inside uninstantiated templates.  */
8793
8794 static tree
8795 cp_parser_template_name (cp_parser* parser,
8796                          bool template_keyword_p,
8797                          bool check_dependency_p,
8798                          bool is_declaration,
8799                          bool *is_identifier)
8800 {
8801   tree identifier;
8802   tree decl;
8803   tree fns;
8804
8805   /* If the next token is `operator', then we have either an
8806      operator-function-id or a conversion-function-id.  */
8807   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8808     {
8809       /* We don't know whether we're looking at an
8810          operator-function-id or a conversion-function-id.  */
8811       cp_parser_parse_tentatively (parser);
8812       /* Try an operator-function-id.  */
8813       identifier = cp_parser_operator_function_id (parser);
8814       /* If that didn't work, try a conversion-function-id.  */
8815       if (!cp_parser_parse_definitely (parser))
8816         {
8817           cp_parser_error (parser, "expected template-name");
8818           return error_mark_node;
8819         }
8820     }
8821   /* Look for the identifier.  */
8822   else
8823     identifier = cp_parser_identifier (parser);
8824
8825   /* If we didn't find an identifier, we don't have a template-id.  */
8826   if (identifier == error_mark_node)
8827     return error_mark_node;
8828
8829   /* If the name immediately followed the `template' keyword, then it
8830      is a template-name.  However, if the next token is not `<', then
8831      we do not treat it as a template-name, since it is not being used
8832      as part of a template-id.  This enables us to handle constructs
8833      like:
8834
8835        template <typename T> struct S { S(); };
8836        template <typename T> S<T>::S();
8837
8838      correctly.  We would treat `S' as a template -- if it were `S<T>'
8839      -- but we do not if there is no `<'.  */
8840
8841   if (processing_template_decl
8842       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8843     {
8844       /* In a declaration, in a dependent context, we pretend that the
8845          "template" keyword was present in order to improve error
8846          recovery.  For example, given:
8847
8848            template <typename T> void f(T::X<int>);
8849
8850          we want to treat "X<int>" as a template-id.  */
8851       if (is_declaration
8852           && !template_keyword_p
8853           && parser->scope && TYPE_P (parser->scope)
8854           && check_dependency_p
8855           && dependent_type_p (parser->scope)
8856           /* Do not do this for dtors (or ctors), since they never
8857              need the template keyword before their name.  */
8858           && !constructor_name_p (identifier, parser->scope))
8859         {
8860           cp_token_position start = 0;
8861
8862           /* Explain what went wrong.  */
8863           error ("non-template %qD used as template", identifier);
8864           inform ("use %<%T::template %D%> to indicate that it is a template",
8865                   parser->scope, identifier);
8866           /* If parsing tentatively, find the location of the "<" token.  */
8867           if (cp_parser_simulate_error (parser))
8868             start = cp_lexer_token_position (parser->lexer, true);
8869           /* Parse the template arguments so that we can issue error
8870              messages about them.  */
8871           cp_lexer_consume_token (parser->lexer);
8872           cp_parser_enclosed_template_argument_list (parser);
8873           /* Skip tokens until we find a good place from which to
8874              continue parsing.  */
8875           cp_parser_skip_to_closing_parenthesis (parser,
8876                                                  /*recovering=*/true,
8877                                                  /*or_comma=*/true,
8878                                                  /*consume_paren=*/false);
8879           /* If parsing tentatively, permanently remove the
8880              template argument list.  That will prevent duplicate
8881              error messages from being issued about the missing
8882              "template" keyword.  */
8883           if (start)
8884             cp_lexer_purge_tokens_after (parser->lexer, start);
8885           if (is_identifier)
8886             *is_identifier = true;
8887           return identifier;
8888         }
8889
8890       /* If the "template" keyword is present, then there is generally
8891          no point in doing name-lookup, so we just return IDENTIFIER.
8892          But, if the qualifying scope is non-dependent then we can
8893          (and must) do name-lookup normally.  */
8894       if (template_keyword_p
8895           && (!parser->scope
8896               || (TYPE_P (parser->scope)
8897                   && dependent_type_p (parser->scope))))
8898         return identifier;
8899     }
8900
8901   /* Look up the name.  */
8902   decl = cp_parser_lookup_name (parser, identifier,
8903                                 none_type,
8904                                 /*is_template=*/false,
8905                                 /*is_namespace=*/false,
8906                                 check_dependency_p,
8907                                 /*ambiguous_decls=*/NULL);
8908   decl = maybe_get_template_decl_from_type_decl (decl);
8909
8910   /* If DECL is a template, then the name was a template-name.  */
8911   if (TREE_CODE (decl) == TEMPLATE_DECL)
8912     ;
8913   else
8914     {
8915       tree fn = NULL_TREE;
8916
8917       /* The standard does not explicitly indicate whether a name that
8918          names a set of overloaded declarations, some of which are
8919          templates, is a template-name.  However, such a name should
8920          be a template-name; otherwise, there is no way to form a
8921          template-id for the overloaded templates.  */
8922       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8923       if (TREE_CODE (fns) == OVERLOAD)
8924         for (fn = fns; fn; fn = OVL_NEXT (fn))
8925           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8926             break;
8927
8928       if (!fn)
8929         {
8930           /* The name does not name a template.  */
8931           cp_parser_error (parser, "expected template-name");
8932           return error_mark_node;
8933         }
8934     }
8935
8936   /* If DECL is dependent, and refers to a function, then just return
8937      its name; we will look it up again during template instantiation.  */
8938   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8939     {
8940       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8941       if (TYPE_P (scope) && dependent_type_p (scope))
8942         return identifier;
8943     }
8944
8945   return decl;
8946 }
8947
8948 /* Parse a template-argument-list.
8949
8950    template-argument-list:
8951      template-argument
8952      template-argument-list , template-argument
8953
8954    Returns a TREE_VEC containing the arguments.  */
8955
8956 static tree
8957 cp_parser_template_argument_list (cp_parser* parser)
8958 {
8959   tree fixed_args[10];
8960   unsigned n_args = 0;
8961   unsigned alloced = 10;
8962   tree *arg_ary = fixed_args;
8963   tree vec;
8964   bool saved_in_template_argument_list_p;
8965   bool saved_ice_p;
8966   bool saved_non_ice_p;
8967
8968   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8969   parser->in_template_argument_list_p = true;
8970   /* Even if the template-id appears in an integral
8971      constant-expression, the contents of the argument list do 
8972      not.  */ 
8973   saved_ice_p = parser->integral_constant_expression_p;
8974   parser->integral_constant_expression_p = false;
8975   saved_non_ice_p = parser->non_integral_constant_expression_p;
8976   parser->non_integral_constant_expression_p = false;
8977   /* Parse the arguments.  */
8978   do
8979     {
8980       tree argument;
8981
8982       if (n_args)
8983         /* Consume the comma.  */
8984         cp_lexer_consume_token (parser->lexer);
8985
8986       /* Parse the template-argument.  */
8987       argument = cp_parser_template_argument (parser);
8988       if (n_args == alloced)
8989         {
8990           alloced *= 2;
8991
8992           if (arg_ary == fixed_args)
8993             {
8994               arg_ary = XNEWVEC (tree, alloced);
8995               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8996             }
8997           else
8998             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
8999         }
9000       arg_ary[n_args++] = argument;
9001     }
9002   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9003
9004   vec = make_tree_vec (n_args);
9005
9006   while (n_args--)
9007     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9008
9009   if (arg_ary != fixed_args)
9010     free (arg_ary);
9011   parser->non_integral_constant_expression_p = saved_non_ice_p;
9012   parser->integral_constant_expression_p = saved_ice_p;
9013   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9014   return vec;
9015 }
9016
9017 /* Parse a template-argument.
9018
9019    template-argument:
9020      assignment-expression
9021      type-id
9022      id-expression
9023
9024    The representation is that of an assignment-expression, type-id, or
9025    id-expression -- except that the qualified id-expression is
9026    evaluated, so that the value returned is either a DECL or an
9027    OVERLOAD.
9028
9029    Although the standard says "assignment-expression", it forbids
9030    throw-expressions or assignments in the template argument.
9031    Therefore, we use "conditional-expression" instead.  */
9032
9033 static tree
9034 cp_parser_template_argument (cp_parser* parser)
9035 {
9036   tree argument;
9037   bool template_p;
9038   bool address_p;
9039   bool maybe_type_id = false;
9040   cp_token *token;
9041   cp_id_kind idk;
9042
9043   /* There's really no way to know what we're looking at, so we just
9044      try each alternative in order.
9045
9046        [temp.arg]
9047
9048        In a template-argument, an ambiguity between a type-id and an
9049        expression is resolved to a type-id, regardless of the form of
9050        the corresponding template-parameter.
9051
9052      Therefore, we try a type-id first.  */
9053   cp_parser_parse_tentatively (parser);
9054   argument = cp_parser_type_id (parser);
9055   /* If there was no error parsing the type-id but the next token is a '>>',
9056      we probably found a typo for '> >'. But there are type-id which are
9057      also valid expressions. For instance:
9058
9059      struct X { int operator >> (int); };
9060      template <int V> struct Foo {};
9061      Foo<X () >> 5> r;
9062
9063      Here 'X()' is a valid type-id of a function type, but the user just
9064      wanted to write the expression "X() >> 5". Thus, we remember that we
9065      found a valid type-id, but we still try to parse the argument as an
9066      expression to see what happens.  */
9067   if (!cp_parser_error_occurred (parser)
9068       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9069     {
9070       maybe_type_id = true;
9071       cp_parser_abort_tentative_parse (parser);
9072     }
9073   else
9074     {
9075       /* If the next token isn't a `,' or a `>', then this argument wasn't
9076       really finished. This means that the argument is not a valid
9077       type-id.  */
9078       if (!cp_parser_next_token_ends_template_argument_p (parser))
9079         cp_parser_error (parser, "expected template-argument");
9080       /* If that worked, we're done.  */
9081       if (cp_parser_parse_definitely (parser))
9082         return argument;
9083     }
9084   /* We're still not sure what the argument will be.  */
9085   cp_parser_parse_tentatively (parser);
9086   /* Try a template.  */
9087   argument = cp_parser_id_expression (parser,
9088                                       /*template_keyword_p=*/false,
9089                                       /*check_dependency_p=*/true,
9090                                       &template_p,
9091                                       /*declarator_p=*/false);
9092   /* If the next token isn't a `,' or a `>', then this argument wasn't
9093      really finished.  */
9094   if (!cp_parser_next_token_ends_template_argument_p (parser))
9095     cp_parser_error (parser, "expected template-argument");
9096   if (!cp_parser_error_occurred (parser))
9097     {
9098       /* Figure out what is being referred to.  If the id-expression
9099          was for a class template specialization, then we will have a
9100          TYPE_DECL at this point.  There is no need to do name lookup
9101          at this point in that case.  */
9102       if (TREE_CODE (argument) != TYPE_DECL)
9103         argument = cp_parser_lookup_name (parser, argument,
9104                                           none_type,
9105                                           /*is_template=*/template_p,
9106                                           /*is_namespace=*/false,
9107                                           /*check_dependency=*/true,
9108                                           /*ambiguous_decls=*/NULL);
9109       if (TREE_CODE (argument) != TEMPLATE_DECL
9110           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9111         cp_parser_error (parser, "expected template-name");
9112     }
9113   if (cp_parser_parse_definitely (parser))
9114     return argument;
9115   /* It must be a non-type argument.  There permitted cases are given
9116      in [temp.arg.nontype]:
9117
9118      -- an integral constant-expression of integral or enumeration
9119         type; or
9120
9121      -- the name of a non-type template-parameter; or
9122
9123      -- the name of an object or function with external linkage...
9124
9125      -- the address of an object or function with external linkage...
9126
9127      -- a pointer to member...  */
9128   /* Look for a non-type template parameter.  */
9129   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9130     {
9131       cp_parser_parse_tentatively (parser);
9132       argument = cp_parser_primary_expression (parser,
9133                                                /*adress_p=*/false,
9134                                                /*cast_p=*/false,
9135                                                /*template_arg_p=*/true,
9136                                                &idk);
9137       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9138           || !cp_parser_next_token_ends_template_argument_p (parser))
9139         cp_parser_simulate_error (parser);
9140       if (cp_parser_parse_definitely (parser))
9141         return argument;
9142     }
9143
9144   /* If the next token is "&", the argument must be the address of an
9145      object or function with external linkage.  */
9146   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9147   if (address_p)
9148     cp_lexer_consume_token (parser->lexer);
9149   /* See if we might have an id-expression.  */
9150   token = cp_lexer_peek_token (parser->lexer);
9151   if (token->type == CPP_NAME
9152       || token->keyword == RID_OPERATOR
9153       || token->type == CPP_SCOPE
9154       || token->type == CPP_TEMPLATE_ID
9155       || token->type == CPP_NESTED_NAME_SPECIFIER)
9156     {
9157       cp_parser_parse_tentatively (parser);
9158       argument = cp_parser_primary_expression (parser,
9159                                                address_p,
9160                                                /*cast_p=*/false,
9161                                                /*template_arg_p=*/true,
9162                                                &idk);
9163       if (cp_parser_error_occurred (parser)
9164           || !cp_parser_next_token_ends_template_argument_p (parser))
9165         cp_parser_abort_tentative_parse (parser);
9166       else
9167         {
9168           if (TREE_CODE (argument) == INDIRECT_REF)
9169             {
9170               gcc_assert (REFERENCE_REF_P (argument));
9171               argument = TREE_OPERAND (argument, 0);
9172             }
9173
9174           if (TREE_CODE (argument) == BASELINK)
9175             /* We don't need the information about what class was used
9176                to name the overloaded functions.  */  
9177             argument = BASELINK_FUNCTIONS (argument);
9178
9179           if (TREE_CODE (argument) == VAR_DECL)
9180             {
9181               /* A variable without external linkage might still be a
9182                  valid constant-expression, so no error is issued here
9183                  if the external-linkage check fails.  */
9184               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9185                 cp_parser_simulate_error (parser);
9186             }
9187           else if (is_overloaded_fn (argument))
9188             /* All overloaded functions are allowed; if the external
9189                linkage test does not pass, an error will be issued
9190                later.  */
9191             ;
9192           else if (address_p
9193                    && (TREE_CODE (argument) == OFFSET_REF
9194                        || TREE_CODE (argument) == SCOPE_REF))
9195             /* A pointer-to-member.  */
9196             ;
9197           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9198             ;
9199           else
9200             cp_parser_simulate_error (parser);
9201
9202           if (cp_parser_parse_definitely (parser))
9203             {
9204               if (address_p)
9205                 argument = build_x_unary_op (ADDR_EXPR, argument);
9206               return argument;
9207             }
9208         }
9209     }
9210   /* If the argument started with "&", there are no other valid
9211      alternatives at this point.  */
9212   if (address_p)
9213     {
9214       cp_parser_error (parser, "invalid non-type template argument");
9215       return error_mark_node;
9216     }
9217
9218   /* If the argument wasn't successfully parsed as a type-id followed
9219      by '>>', the argument can only be a constant expression now.
9220      Otherwise, we try parsing the constant-expression tentatively,
9221      because the argument could really be a type-id.  */
9222   if (maybe_type_id)
9223     cp_parser_parse_tentatively (parser);
9224   argument = cp_parser_constant_expression (parser,
9225                                             /*allow_non_constant_p=*/false,
9226                                             /*non_constant_p=*/NULL);
9227   argument = fold_non_dependent_expr (argument);
9228   if (!maybe_type_id)
9229     return argument;
9230   if (!cp_parser_next_token_ends_template_argument_p (parser))
9231     cp_parser_error (parser, "expected template-argument");
9232   if (cp_parser_parse_definitely (parser))
9233     return argument;
9234   /* We did our best to parse the argument as a non type-id, but that
9235      was the only alternative that matched (albeit with a '>' after
9236      it). We can assume it's just a typo from the user, and a
9237      diagnostic will then be issued.  */
9238   return cp_parser_type_id (parser);
9239 }
9240
9241 /* Parse an explicit-instantiation.
9242
9243    explicit-instantiation:
9244      template declaration
9245
9246    Although the standard says `declaration', what it really means is:
9247
9248    explicit-instantiation:
9249      template decl-specifier-seq [opt] declarator [opt] ;
9250
9251    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9252    supposed to be allowed.  A defect report has been filed about this
9253    issue.
9254
9255    GNU Extension:
9256
9257    explicit-instantiation:
9258      storage-class-specifier template
9259        decl-specifier-seq [opt] declarator [opt] ;
9260      function-specifier template
9261        decl-specifier-seq [opt] declarator [opt] ;  */
9262
9263 static void
9264 cp_parser_explicit_instantiation (cp_parser* parser)
9265 {
9266   int declares_class_or_enum;
9267   cp_decl_specifier_seq decl_specifiers;
9268   tree extension_specifier = NULL_TREE;
9269
9270   /* Look for an (optional) storage-class-specifier or
9271      function-specifier.  */
9272   if (cp_parser_allow_gnu_extensions_p (parser))
9273     {
9274       extension_specifier
9275         = cp_parser_storage_class_specifier_opt (parser);
9276       if (!extension_specifier)
9277         extension_specifier
9278           = cp_parser_function_specifier_opt (parser,
9279                                               /*decl_specs=*/NULL);
9280     }
9281
9282   /* Look for the `template' keyword.  */
9283   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9284   /* Let the front end know that we are processing an explicit
9285      instantiation.  */
9286   begin_explicit_instantiation ();
9287   /* [temp.explicit] says that we are supposed to ignore access
9288      control while processing explicit instantiation directives.  */
9289   push_deferring_access_checks (dk_no_check);
9290   /* Parse a decl-specifier-seq.  */
9291   cp_parser_decl_specifier_seq (parser,
9292                                 CP_PARSER_FLAGS_OPTIONAL,
9293                                 &decl_specifiers,
9294                                 &declares_class_or_enum);
9295   /* If there was exactly one decl-specifier, and it declared a class,
9296      and there's no declarator, then we have an explicit type
9297      instantiation.  */
9298   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9299     {
9300       tree type;
9301
9302       type = check_tag_decl (&decl_specifiers);
9303       /* Turn access control back on for names used during
9304          template instantiation.  */
9305       pop_deferring_access_checks ();
9306       if (type)
9307         do_type_instantiation (type, extension_specifier,
9308                                /*complain=*/tf_error);
9309     }
9310   else
9311     {
9312       cp_declarator *declarator;
9313       tree decl;
9314
9315       /* Parse the declarator.  */
9316       declarator
9317         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9318                                 /*ctor_dtor_or_conv_p=*/NULL,
9319                                 /*parenthesized_p=*/NULL,
9320                                 /*member_p=*/false);
9321       if (declares_class_or_enum & 2)
9322         cp_parser_check_for_definition_in_return_type (declarator,
9323                                                        decl_specifiers.type);
9324       if (declarator != cp_error_declarator)
9325         {
9326           decl = grokdeclarator (declarator, &decl_specifiers,
9327                                  NORMAL, 0, NULL);
9328           /* Turn access control back on for names used during
9329              template instantiation.  */
9330           pop_deferring_access_checks ();
9331           /* Do the explicit instantiation.  */
9332           do_decl_instantiation (decl, extension_specifier);
9333         }
9334       else
9335         {
9336           pop_deferring_access_checks ();
9337           /* Skip the body of the explicit instantiation.  */
9338           cp_parser_skip_to_end_of_statement (parser);
9339         }
9340     }
9341   /* We're done with the instantiation.  */
9342   end_explicit_instantiation ();
9343
9344   cp_parser_consume_semicolon_at_end_of_statement (parser);
9345 }
9346
9347 /* Parse an explicit-specialization.
9348
9349    explicit-specialization:
9350      template < > declaration
9351
9352    Although the standard says `declaration', what it really means is:
9353
9354    explicit-specialization:
9355      template <> decl-specifier [opt] init-declarator [opt] ;
9356      template <> function-definition
9357      template <> explicit-specialization
9358      template <> template-declaration  */
9359
9360 static void
9361 cp_parser_explicit_specialization (cp_parser* parser)
9362 {
9363   bool need_lang_pop;
9364   /* Look for the `template' keyword.  */
9365   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9366   /* Look for the `<'.  */
9367   cp_parser_require (parser, CPP_LESS, "`<'");
9368   /* Look for the `>'.  */
9369   cp_parser_require (parser, CPP_GREATER, "`>'");
9370   /* We have processed another parameter list.  */
9371   ++parser->num_template_parameter_lists;
9372   /* [temp]
9373    
9374      A template ... explicit specialization ... shall not have C
9375      linkage.  */ 
9376   if (current_lang_name == lang_name_c)
9377     {
9378       error ("template specialization with C linkage");
9379       /* Give it C++ linkage to avoid confusing other parts of the
9380          front end.  */
9381       push_lang_context (lang_name_cplusplus);
9382       need_lang_pop = true;
9383     }
9384   else
9385     need_lang_pop = false;
9386   /* Let the front end know that we are beginning a specialization.  */
9387   begin_specialization ();
9388   /* If the next keyword is `template', we need to figure out whether
9389      or not we're looking a template-declaration.  */
9390   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9391     {
9392       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9393           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9394         cp_parser_template_declaration_after_export (parser,
9395                                                      /*member_p=*/false);
9396       else
9397         cp_parser_explicit_specialization (parser);
9398     }
9399   else
9400     /* Parse the dependent declaration.  */
9401     cp_parser_single_declaration (parser,
9402                                   /*member_p=*/false,
9403                                   /*friend_p=*/NULL);
9404   /* We're done with the specialization.  */
9405   end_specialization ();
9406   /* For the erroneous case of a template with C linkage, we pushed an
9407      implicit C++ linkage scope; exit that scope now.  */
9408   if (need_lang_pop)
9409     pop_lang_context ();
9410   /* We're done with this parameter list.  */
9411   --parser->num_template_parameter_lists;
9412 }
9413
9414 /* Parse a type-specifier.
9415
9416    type-specifier:
9417      simple-type-specifier
9418      class-specifier
9419      enum-specifier
9420      elaborated-type-specifier
9421      cv-qualifier
9422
9423    GNU Extension:
9424
9425    type-specifier:
9426      __complex__
9427
9428    Returns a representation of the type-specifier.  For a
9429    class-specifier, enum-specifier, or elaborated-type-specifier, a
9430    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9431
9432    The parser flags FLAGS is used to control type-specifier parsing.
9433
9434    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9435    in a decl-specifier-seq.
9436
9437    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9438    class-specifier, enum-specifier, or elaborated-type-specifier, then
9439    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9440    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9441    zero.
9442
9443    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9444    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9445    is set to FALSE.  */
9446
9447 static tree
9448 cp_parser_type_specifier (cp_parser* parser,
9449                           cp_parser_flags flags,
9450                           cp_decl_specifier_seq *decl_specs,
9451                           bool is_declaration,
9452                           int* declares_class_or_enum,
9453                           bool* is_cv_qualifier)
9454 {
9455   tree type_spec = NULL_TREE;
9456   cp_token *token;
9457   enum rid keyword;
9458   cp_decl_spec ds = ds_last;
9459
9460   /* Assume this type-specifier does not declare a new type.  */
9461   if (declares_class_or_enum)
9462     *declares_class_or_enum = 0;
9463   /* And that it does not specify a cv-qualifier.  */
9464   if (is_cv_qualifier)
9465     *is_cv_qualifier = false;
9466   /* Peek at the next token.  */
9467   token = cp_lexer_peek_token (parser->lexer);
9468
9469   /* If we're looking at a keyword, we can use that to guide the
9470      production we choose.  */
9471   keyword = token->keyword;
9472   switch (keyword)
9473     {
9474     case RID_ENUM:
9475       /* 'enum' [identifier] '{' introduces an enum-specifier;
9476          'enum' <anything else> introduces an elaborated-type-specifier.  */
9477       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9478           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9479               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9480                  == CPP_OPEN_BRACE))
9481         {
9482           if (parser->num_template_parameter_lists)
9483             {
9484               error ("template declaration of %qs", "enum");
9485               cp_parser_skip_to_end_of_block_or_statement (parser);
9486               type_spec = error_mark_node;
9487             }
9488           else
9489             type_spec = cp_parser_enum_specifier (parser);
9490
9491           if (declares_class_or_enum)
9492             *declares_class_or_enum = 2;
9493           if (decl_specs)
9494             cp_parser_set_decl_spec_type (decl_specs,
9495                                           type_spec,
9496                                           /*user_defined_p=*/true);
9497           return type_spec;
9498         }
9499       else
9500         goto elaborated_type_specifier;
9501
9502       /* Any of these indicate either a class-specifier, or an
9503          elaborated-type-specifier.  */
9504     case RID_CLASS:
9505     case RID_STRUCT:
9506     case RID_UNION:
9507       /* Parse tentatively so that we can back up if we don't find a
9508          class-specifier.  */
9509       cp_parser_parse_tentatively (parser);
9510       /* Look for the class-specifier.  */
9511       type_spec = cp_parser_class_specifier (parser);
9512       /* If that worked, we're done.  */
9513       if (cp_parser_parse_definitely (parser))
9514         {
9515           if (declares_class_or_enum)
9516             *declares_class_or_enum = 2;
9517           if (decl_specs)
9518             cp_parser_set_decl_spec_type (decl_specs,
9519                                           type_spec,
9520                                           /*user_defined_p=*/true);
9521           return type_spec;
9522         }
9523
9524       /* Fall through.  */
9525     elaborated_type_specifier:
9526       /* We're declaring (not defining) a class or enum.  */
9527       if (declares_class_or_enum)
9528         *declares_class_or_enum = 1;
9529
9530       /* Fall through.  */
9531     case RID_TYPENAME:
9532       /* Look for an elaborated-type-specifier.  */
9533       type_spec
9534         = (cp_parser_elaborated_type_specifier
9535            (parser,
9536             decl_specs && decl_specs->specs[(int) ds_friend],
9537             is_declaration));
9538       if (decl_specs)
9539         cp_parser_set_decl_spec_type (decl_specs,
9540                                       type_spec,
9541                                       /*user_defined_p=*/true);
9542       return type_spec;
9543
9544     case RID_CONST:
9545       ds = ds_const;
9546       if (is_cv_qualifier)
9547         *is_cv_qualifier = true;
9548       break;
9549
9550     case RID_VOLATILE:
9551       ds = ds_volatile;
9552       if (is_cv_qualifier)
9553         *is_cv_qualifier = true;
9554       break;
9555
9556     case RID_RESTRICT:
9557       ds = ds_restrict;
9558       if (is_cv_qualifier)
9559         *is_cv_qualifier = true;
9560       break;
9561
9562     case RID_COMPLEX:
9563       /* The `__complex__' keyword is a GNU extension.  */
9564       ds = ds_complex;
9565       break;
9566
9567     default:
9568       break;
9569     }
9570
9571   /* Handle simple keywords.  */
9572   if (ds != ds_last)
9573     {
9574       if (decl_specs)
9575         {
9576           ++decl_specs->specs[(int)ds];
9577           decl_specs->any_specifiers_p = true;
9578         }
9579       return cp_lexer_consume_token (parser->lexer)->value;
9580     }
9581
9582   /* If we do not already have a type-specifier, assume we are looking
9583      at a simple-type-specifier.  */
9584   type_spec = cp_parser_simple_type_specifier (parser,
9585                                                decl_specs,
9586                                                flags);
9587
9588   /* If we didn't find a type-specifier, and a type-specifier was not
9589      optional in this context, issue an error message.  */
9590   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9591     {
9592       cp_parser_error (parser, "expected type specifier");
9593       return error_mark_node;
9594     }
9595
9596   return type_spec;
9597 }
9598
9599 /* Parse a simple-type-specifier.
9600
9601    simple-type-specifier:
9602      :: [opt] nested-name-specifier [opt] type-name
9603      :: [opt] nested-name-specifier template template-id
9604      char
9605      wchar_t
9606      bool
9607      short
9608      int
9609      long
9610      signed
9611      unsigned
9612      float
9613      double
9614      void
9615
9616    GNU Extension:
9617
9618    simple-type-specifier:
9619      __typeof__ unary-expression
9620      __typeof__ ( type-id )
9621
9622    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9623    appropriately updated.  */
9624
9625 static tree
9626 cp_parser_simple_type_specifier (cp_parser* parser,
9627                                  cp_decl_specifier_seq *decl_specs,
9628                                  cp_parser_flags flags)
9629 {
9630   tree type = NULL_TREE;
9631   cp_token *token;
9632
9633   /* Peek at the next token.  */
9634   token = cp_lexer_peek_token (parser->lexer);
9635
9636   /* If we're looking at a keyword, things are easy.  */
9637   switch (token->keyword)
9638     {
9639     case RID_CHAR:
9640       if (decl_specs)
9641         decl_specs->explicit_char_p = true;
9642       type = char_type_node;
9643       break;
9644     case RID_WCHAR:
9645       type = wchar_type_node;
9646       break;
9647     case RID_BOOL:
9648       type = boolean_type_node;
9649       break;
9650     case RID_SHORT:
9651       if (decl_specs)
9652         ++decl_specs->specs[(int) ds_short];
9653       type = short_integer_type_node;
9654       break;
9655     case RID_INT:
9656       if (decl_specs)
9657         decl_specs->explicit_int_p = true;
9658       type = integer_type_node;
9659       break;
9660     case RID_LONG:
9661       if (decl_specs)
9662         ++decl_specs->specs[(int) ds_long];
9663       type = long_integer_type_node;
9664       break;
9665     case RID_SIGNED:
9666       if (decl_specs)
9667         ++decl_specs->specs[(int) ds_signed];
9668       type = integer_type_node;
9669       break;
9670     case RID_UNSIGNED:
9671       if (decl_specs)
9672         ++decl_specs->specs[(int) ds_unsigned];
9673       type = unsigned_type_node;
9674       break;
9675     case RID_FLOAT:
9676       type = float_type_node;
9677       break;
9678     case RID_DOUBLE:
9679       type = double_type_node;
9680       break;
9681     case RID_VOID:
9682       type = void_type_node;
9683       break;
9684
9685     case RID_TYPEOF:
9686       /* Consume the `typeof' token.  */
9687       cp_lexer_consume_token (parser->lexer);
9688       /* Parse the operand to `typeof'.  */
9689       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9690       /* If it is not already a TYPE, take its type.  */
9691       if (!TYPE_P (type))
9692         type = finish_typeof (type);
9693
9694       if (decl_specs)
9695         cp_parser_set_decl_spec_type (decl_specs, type,
9696                                       /*user_defined_p=*/true);
9697
9698       return type;
9699
9700     default:
9701       break;
9702     }
9703
9704   /* If the type-specifier was for a built-in type, we're done.  */
9705   if (type)
9706     {
9707       tree id;
9708
9709       /* Record the type.  */
9710       if (decl_specs
9711           && (token->keyword != RID_SIGNED
9712               && token->keyword != RID_UNSIGNED
9713               && token->keyword != RID_SHORT
9714               && token->keyword != RID_LONG))
9715         cp_parser_set_decl_spec_type (decl_specs,
9716                                       type,
9717                                       /*user_defined=*/false);
9718       if (decl_specs)
9719         decl_specs->any_specifiers_p = true;
9720
9721       /* Consume the token.  */
9722       id = cp_lexer_consume_token (parser->lexer)->value;
9723
9724       /* There is no valid C++ program where a non-template type is
9725          followed by a "<".  That usually indicates that the user thought
9726          that the type was a template.  */
9727       cp_parser_check_for_invalid_template_id (parser, type);
9728
9729       return TYPE_NAME (type);
9730     }
9731
9732   /* The type-specifier must be a user-defined type.  */
9733   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9734     {
9735       bool qualified_p;
9736       bool global_p;
9737
9738       /* Don't gobble tokens or issue error messages if this is an
9739          optional type-specifier.  */
9740       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9741         cp_parser_parse_tentatively (parser);
9742
9743       /* Look for the optional `::' operator.  */
9744       global_p
9745         = (cp_parser_global_scope_opt (parser,
9746                                        /*current_scope_valid_p=*/false)
9747            != NULL_TREE);
9748       /* Look for the nested-name specifier.  */
9749       qualified_p
9750         = (cp_parser_nested_name_specifier_opt (parser,
9751                                                 /*typename_keyword_p=*/false,
9752                                                 /*check_dependency_p=*/true,
9753                                                 /*type_p=*/false,
9754                                                 /*is_declaration=*/false)
9755            != NULL_TREE);
9756       /* If we have seen a nested-name-specifier, and the next token
9757          is `template', then we are using the template-id production.  */
9758       if (parser->scope
9759           && cp_parser_optional_template_keyword (parser))
9760         {
9761           /* Look for the template-id.  */
9762           type = cp_parser_template_id (parser,
9763                                         /*template_keyword_p=*/true,
9764                                         /*check_dependency_p=*/true,
9765                                         /*is_declaration=*/false);
9766           /* If the template-id did not name a type, we are out of
9767              luck.  */
9768           if (TREE_CODE (type) != TYPE_DECL)
9769             {
9770               cp_parser_error (parser, "expected template-id for type");
9771               type = NULL_TREE;
9772             }
9773         }
9774       /* Otherwise, look for a type-name.  */
9775       else
9776         type = cp_parser_type_name (parser);
9777       /* Keep track of all name-lookups performed in class scopes.  */
9778       if (type
9779           && !global_p
9780           && !qualified_p
9781           && TREE_CODE (type) == TYPE_DECL
9782           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9783         maybe_note_name_used_in_class (DECL_NAME (type), type);
9784       /* If it didn't work out, we don't have a TYPE.  */
9785       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9786           && !cp_parser_parse_definitely (parser))
9787         type = NULL_TREE;
9788       if (type && decl_specs)
9789         cp_parser_set_decl_spec_type (decl_specs, type,
9790                                       /*user_defined=*/true);
9791     }
9792
9793   /* If we didn't get a type-name, issue an error message.  */
9794   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9795     {
9796       cp_parser_error (parser, "expected type-name");
9797       return error_mark_node;
9798     }
9799
9800   /* There is no valid C++ program where a non-template type is
9801      followed by a "<".  That usually indicates that the user thought
9802      that the type was a template.  */
9803   if (type && type != error_mark_node)
9804     {
9805       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9806          If it is, then the '<'...'>' enclose protocol names rather than
9807          template arguments, and so everything is fine.  */
9808       if (c_dialect_objc ()
9809           && (objc_is_id (type) || objc_is_class_name (type)))
9810         {
9811           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9812           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9813
9814           /* Clobber the "unqualified" type previously entered into
9815              DECL_SPECS with the new, improved protocol-qualified version.  */
9816           if (decl_specs)
9817             decl_specs->type = qual_type;
9818
9819           return qual_type;
9820         }
9821
9822       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9823     }
9824
9825   return type;
9826 }
9827
9828 /* Parse a type-name.
9829
9830    type-name:
9831      class-name
9832      enum-name
9833      typedef-name
9834
9835    enum-name:
9836      identifier
9837
9838    typedef-name:
9839      identifier
9840
9841    Returns a TYPE_DECL for the type.  */
9842
9843 static tree
9844 cp_parser_type_name (cp_parser* parser)
9845 {
9846   tree type_decl;
9847   tree identifier;
9848
9849   /* We can't know yet whether it is a class-name or not.  */
9850   cp_parser_parse_tentatively (parser);
9851   /* Try a class-name.  */
9852   type_decl = cp_parser_class_name (parser,
9853                                     /*typename_keyword_p=*/false,
9854                                     /*template_keyword_p=*/false,
9855                                     none_type,
9856                                     /*check_dependency_p=*/true,
9857                                     /*class_head_p=*/false,
9858                                     /*is_declaration=*/false);
9859   /* If it's not a class-name, keep looking.  */
9860   if (!cp_parser_parse_definitely (parser))
9861     {
9862       /* It must be a typedef-name or an enum-name.  */
9863       identifier = cp_parser_identifier (parser);
9864       if (identifier == error_mark_node)
9865         return error_mark_node;
9866
9867       /* Look up the type-name.  */
9868       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9869
9870       if (TREE_CODE (type_decl) != TYPE_DECL
9871           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9872         {
9873           /* See if this is an Objective-C type.  */
9874           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9875           tree type = objc_get_protocol_qualified_type (identifier, protos);
9876           if (type)
9877             type_decl = TYPE_NAME (type);
9878         }
9879
9880       /* Issue an error if we did not find a type-name.  */
9881       if (TREE_CODE (type_decl) != TYPE_DECL)
9882         {
9883           if (!cp_parser_simulate_error (parser))
9884             cp_parser_name_lookup_error (parser, identifier, type_decl,
9885                                          "is not a type");
9886           type_decl = error_mark_node;
9887         }
9888       /* Remember that the name was used in the definition of the
9889          current class so that we can check later to see if the
9890          meaning would have been different after the class was
9891          entirely defined.  */
9892       else if (type_decl != error_mark_node
9893                && !parser->scope)
9894         maybe_note_name_used_in_class (identifier, type_decl);
9895     }
9896
9897   return type_decl;
9898 }
9899
9900
9901 /* Parse an elaborated-type-specifier.  Note that the grammar given
9902    here incorporates the resolution to DR68.
9903
9904    elaborated-type-specifier:
9905      class-key :: [opt] nested-name-specifier [opt] identifier
9906      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9907      enum :: [opt] nested-name-specifier [opt] identifier
9908      typename :: [opt] nested-name-specifier identifier
9909      typename :: [opt] nested-name-specifier template [opt]
9910        template-id
9911
9912    GNU extension:
9913
9914    elaborated-type-specifier:
9915      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9916      class-key attributes :: [opt] nested-name-specifier [opt]
9917                template [opt] template-id
9918      enum attributes :: [opt] nested-name-specifier [opt] identifier
9919
9920    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9921    declared `friend'.  If IS_DECLARATION is TRUE, then this
9922    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9923    something is being declared.
9924
9925    Returns the TYPE specified.  */
9926
9927 static tree
9928 cp_parser_elaborated_type_specifier (cp_parser* parser,
9929                                      bool is_friend,
9930                                      bool is_declaration)
9931 {
9932   enum tag_types tag_type;
9933   tree identifier;
9934   tree type = NULL_TREE;
9935   tree attributes = NULL_TREE;
9936
9937   /* See if we're looking at the `enum' keyword.  */
9938   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9939     {
9940       /* Consume the `enum' token.  */
9941       cp_lexer_consume_token (parser->lexer);
9942       /* Remember that it's an enumeration type.  */
9943       tag_type = enum_type;
9944       /* Parse the attributes.  */
9945       attributes = cp_parser_attributes_opt (parser);
9946     }
9947   /* Or, it might be `typename'.  */
9948   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9949                                            RID_TYPENAME))
9950     {
9951       /* Consume the `typename' token.  */
9952       cp_lexer_consume_token (parser->lexer);
9953       /* Remember that it's a `typename' type.  */
9954       tag_type = typename_type;
9955       /* The `typename' keyword is only allowed in templates.  */
9956       if (!processing_template_decl)
9957         pedwarn ("using %<typename%> outside of template");
9958     }
9959   /* Otherwise it must be a class-key.  */
9960   else
9961     {
9962       tag_type = cp_parser_class_key (parser);
9963       if (tag_type == none_type)
9964         return error_mark_node;
9965       /* Parse the attributes.  */
9966       attributes = cp_parser_attributes_opt (parser);
9967     }
9968
9969   /* Look for the `::' operator.  */
9970   cp_parser_global_scope_opt (parser,
9971                               /*current_scope_valid_p=*/false);
9972   /* Look for the nested-name-specifier.  */
9973   if (tag_type == typename_type)
9974     {
9975       if (!cp_parser_nested_name_specifier (parser,
9976                                            /*typename_keyword_p=*/true,
9977                                            /*check_dependency_p=*/true,
9978                                            /*type_p=*/true,
9979                                             is_declaration))
9980         return error_mark_node;
9981     }
9982   else
9983     /* Even though `typename' is not present, the proposed resolution
9984        to Core Issue 180 says that in `class A<T>::B', `B' should be
9985        considered a type-name, even if `A<T>' is dependent.  */
9986     cp_parser_nested_name_specifier_opt (parser,
9987                                          /*typename_keyword_p=*/true,
9988                                          /*check_dependency_p=*/true,
9989                                          /*type_p=*/true,
9990                                          is_declaration);
9991   /* For everything but enumeration types, consider a template-id.  */
9992   if (tag_type != enum_type)
9993     {
9994       bool template_p = false;
9995       tree decl;
9996
9997       /* Allow the `template' keyword.  */
9998       template_p = cp_parser_optional_template_keyword (parser);
9999       /* If we didn't see `template', we don't know if there's a
10000          template-id or not.  */
10001       if (!template_p)
10002         cp_parser_parse_tentatively (parser);
10003       /* Parse the template-id.  */
10004       decl = cp_parser_template_id (parser, template_p,
10005                                     /*check_dependency_p=*/true,
10006                                     is_declaration);
10007       /* If we didn't find a template-id, look for an ordinary
10008          identifier.  */
10009       if (!template_p && !cp_parser_parse_definitely (parser))
10010         ;
10011       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10012          in effect, then we must assume that, upon instantiation, the
10013          template will correspond to a class.  */
10014       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10015                && tag_type == typename_type)
10016         type = make_typename_type (parser->scope, decl,
10017                                    typename_type,
10018                                    /*complain=*/tf_error);
10019       else
10020         type = TREE_TYPE (decl);
10021     }
10022
10023   /* For an enumeration type, consider only a plain identifier.  */
10024   if (!type)
10025     {
10026       identifier = cp_parser_identifier (parser);
10027
10028       if (identifier == error_mark_node)
10029         {
10030           parser->scope = NULL_TREE;
10031           return error_mark_node;
10032         }
10033
10034       /* For a `typename', we needn't call xref_tag.  */
10035       if (tag_type == typename_type
10036           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10037         return cp_parser_make_typename_type (parser, parser->scope,
10038                                              identifier);
10039       /* Look up a qualified name in the usual way.  */
10040       if (parser->scope)
10041         {
10042           tree decl;
10043
10044           decl = cp_parser_lookup_name (parser, identifier,
10045                                         tag_type,
10046                                         /*is_template=*/false,
10047                                         /*is_namespace=*/false,
10048                                         /*check_dependency=*/true,
10049                                         /*ambiguous_decls=*/NULL);
10050
10051           /* If we are parsing friend declaration, DECL may be a
10052              TEMPLATE_DECL tree node here.  However, we need to check
10053              whether this TEMPLATE_DECL results in valid code.  Consider
10054              the following example:
10055
10056                namespace N {
10057                  template <class T> class C {};
10058                }
10059                class X {
10060                  template <class T> friend class N::C; // #1, valid code
10061                };
10062                template <class T> class Y {
10063                  friend class N::C;                    // #2, invalid code
10064                };
10065
10066              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10067              name lookup of `N::C'.  We see that friend declaration must
10068              be template for the code to be valid.  Note that
10069              processing_template_decl does not work here since it is
10070              always 1 for the above two cases.  */
10071
10072           decl = (cp_parser_maybe_treat_template_as_class
10073                   (decl, /*tag_name_p=*/is_friend
10074                          && parser->num_template_parameter_lists));
10075
10076           if (TREE_CODE (decl) != TYPE_DECL)
10077             {
10078               cp_parser_diagnose_invalid_type_name (parser,
10079                                                     parser->scope,
10080                                                     identifier);
10081               return error_mark_node;
10082             }
10083
10084           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10085             check_elaborated_type_specifier
10086               (tag_type, decl,
10087                (parser->num_template_parameter_lists
10088                 || DECL_SELF_REFERENCE_P (decl)));
10089
10090           type = TREE_TYPE (decl);
10091         }
10092       else
10093         {
10094           /* An elaborated-type-specifier sometimes introduces a new type and
10095              sometimes names an existing type.  Normally, the rule is that it
10096              introduces a new type only if there is not an existing type of
10097              the same name already in scope.  For example, given:
10098
10099                struct S {};
10100                void f() { struct S s; }
10101
10102              the `struct S' in the body of `f' is the same `struct S' as in
10103              the global scope; the existing definition is used.  However, if
10104              there were no global declaration, this would introduce a new
10105              local class named `S'.
10106
10107              An exception to this rule applies to the following code:
10108
10109                namespace N { struct S; }
10110
10111              Here, the elaborated-type-specifier names a new type
10112              unconditionally; even if there is already an `S' in the
10113              containing scope this declaration names a new type.
10114              This exception only applies if the elaborated-type-specifier
10115              forms the complete declaration:
10116
10117                [class.name]
10118
10119                A declaration consisting solely of `class-key identifier ;' is
10120                either a redeclaration of the name in the current scope or a
10121                forward declaration of the identifier as a class name.  It
10122                introduces the name into the current scope.
10123
10124              We are in this situation precisely when the next token is a `;'.
10125
10126              An exception to the exception is that a `friend' declaration does
10127              *not* name a new type; i.e., given:
10128
10129                struct S { friend struct T; };
10130
10131              `T' is not a new type in the scope of `S'.
10132
10133              Also, `new struct S' or `sizeof (struct S)' never results in the
10134              definition of a new type; a new type can only be declared in a
10135              declaration context.  */
10136
10137           tag_scope ts;
10138           bool template_p;
10139
10140           if (is_friend)
10141             /* Friends have special name lookup rules.  */
10142             ts = ts_within_enclosing_non_class;
10143           else if (is_declaration
10144                    && cp_lexer_next_token_is (parser->lexer,
10145                                               CPP_SEMICOLON))
10146             /* This is a `class-key identifier ;' */
10147             ts = ts_current;
10148           else
10149             ts = ts_global;
10150
10151           /* Warn about attributes. They are ignored.  */
10152           if (attributes)
10153             warning (OPT_Wattributes,
10154                      "type attributes are honored only at type definition");
10155
10156           template_p = 
10157             (parser->num_template_parameter_lists
10158              && (cp_parser_next_token_starts_class_definition_p (parser)
10159                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10160           /* An unqualified name was used to reference this type, so
10161              there were no qualifying templates.  */
10162           if (!cp_parser_check_template_parameters (parser, 
10163                                                     /*num_templates=*/0))
10164             return error_mark_node;
10165           type = xref_tag (tag_type, identifier, ts, template_p);
10166         }
10167     }
10168   if (tag_type != enum_type)
10169     cp_parser_check_class_key (tag_type, type);
10170
10171   /* A "<" cannot follow an elaborated type specifier.  If that
10172      happens, the user was probably trying to form a template-id.  */
10173   cp_parser_check_for_invalid_template_id (parser, type);
10174
10175   return type;
10176 }
10177
10178 /* Parse an enum-specifier.
10179
10180    enum-specifier:
10181      enum identifier [opt] { enumerator-list [opt] }
10182
10183    GNU Extensions:
10184      enum identifier [opt] { enumerator-list [opt] } attributes
10185
10186    Returns an ENUM_TYPE representing the enumeration.  */
10187
10188 static tree
10189 cp_parser_enum_specifier (cp_parser* parser)
10190 {
10191   tree identifier;
10192   tree type;
10193
10194   /* Caller guarantees that the current token is 'enum', an identifier
10195      possibly follows, and the token after that is an opening brace.
10196      If we don't have an identifier, fabricate an anonymous name for
10197      the enumeration being defined.  */
10198   cp_lexer_consume_token (parser->lexer);
10199
10200   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10201     identifier = cp_parser_identifier (parser);
10202   else
10203     identifier = make_anon_name ();
10204
10205   /* Issue an error message if type-definitions are forbidden here.  */
10206   cp_parser_check_type_definition (parser);
10207
10208   /* Create the new type.  We do this before consuming the opening brace
10209      so the enum will be recorded as being on the line of its tag (or the
10210      'enum' keyword, if there is no tag).  */
10211   type = start_enum (identifier);
10212
10213   /* Consume the opening brace.  */
10214   cp_lexer_consume_token (parser->lexer);
10215
10216   /* If the next token is not '}', then there are some enumerators.  */
10217   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10218     cp_parser_enumerator_list (parser, type);
10219
10220   /* Consume the final '}'.  */
10221   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10222
10223   /* Look for trailing attributes to apply to this enumeration, and
10224      apply them if appropriate.  */
10225   if (cp_parser_allow_gnu_extensions_p (parser))
10226     {
10227       tree trailing_attr = cp_parser_attributes_opt (parser);
10228       cplus_decl_attributes (&type,
10229                              trailing_attr,
10230                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10231     }
10232
10233   /* Finish up the enumeration.  */
10234   finish_enum (type);
10235
10236   return type;
10237 }
10238
10239 /* Parse an enumerator-list.  The enumerators all have the indicated
10240    TYPE.
10241
10242    enumerator-list:
10243      enumerator-definition
10244      enumerator-list , enumerator-definition  */
10245
10246 static void
10247 cp_parser_enumerator_list (cp_parser* parser, tree type)
10248 {
10249   while (true)
10250     {
10251       /* Parse an enumerator-definition.  */
10252       cp_parser_enumerator_definition (parser, type);
10253
10254       /* If the next token is not a ',', we've reached the end of
10255          the list.  */
10256       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10257         break;
10258       /* Otherwise, consume the `,' and keep going.  */
10259       cp_lexer_consume_token (parser->lexer);
10260       /* If the next token is a `}', there is a trailing comma.  */
10261       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10262         {
10263           if (pedantic && !in_system_header)
10264             pedwarn ("comma at end of enumerator list");
10265           break;
10266         }
10267     }
10268 }
10269
10270 /* Parse an enumerator-definition.  The enumerator has the indicated
10271    TYPE.
10272
10273    enumerator-definition:
10274      enumerator
10275      enumerator = constant-expression
10276
10277    enumerator:
10278      identifier  */
10279
10280 static void
10281 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10282 {
10283   tree identifier;
10284   tree value;
10285
10286   /* Look for the identifier.  */
10287   identifier = cp_parser_identifier (parser);
10288   if (identifier == error_mark_node)
10289     return;
10290
10291   /* If the next token is an '=', then there is an explicit value.  */
10292   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10293     {
10294       /* Consume the `=' token.  */
10295       cp_lexer_consume_token (parser->lexer);
10296       /* Parse the value.  */
10297       value = cp_parser_constant_expression (parser,
10298                                              /*allow_non_constant_p=*/false,
10299                                              NULL);
10300     }
10301   else
10302     value = NULL_TREE;
10303
10304   /* Create the enumerator.  */
10305   build_enumerator (identifier, value, type);
10306 }
10307
10308 /* Parse a namespace-name.
10309
10310    namespace-name:
10311      original-namespace-name
10312      namespace-alias
10313
10314    Returns the NAMESPACE_DECL for the namespace.  */
10315
10316 static tree
10317 cp_parser_namespace_name (cp_parser* parser)
10318 {
10319   tree identifier;
10320   tree namespace_decl;
10321
10322   /* Get the name of the namespace.  */
10323   identifier = cp_parser_identifier (parser);
10324   if (identifier == error_mark_node)
10325     return error_mark_node;
10326
10327   /* Look up the identifier in the currently active scope.  Look only
10328      for namespaces, due to:
10329
10330        [basic.lookup.udir]
10331
10332        When looking up a namespace-name in a using-directive or alias
10333        definition, only namespace names are considered.
10334
10335      And:
10336
10337        [basic.lookup.qual]
10338
10339        During the lookup of a name preceding the :: scope resolution
10340        operator, object, function, and enumerator names are ignored.
10341
10342      (Note that cp_parser_class_or_namespace_name only calls this
10343      function if the token after the name is the scope resolution
10344      operator.)  */
10345   namespace_decl = cp_parser_lookup_name (parser, identifier,
10346                                           none_type,
10347                                           /*is_template=*/false,
10348                                           /*is_namespace=*/true,
10349                                           /*check_dependency=*/true,
10350                                           /*ambiguous_decls=*/NULL);
10351   /* If it's not a namespace, issue an error.  */
10352   if (namespace_decl == error_mark_node
10353       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10354     {
10355       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10356         error ("%qD is not a namespace-name", identifier);
10357       cp_parser_error (parser, "expected namespace-name");
10358       namespace_decl = error_mark_node;
10359     }
10360
10361   return namespace_decl;
10362 }
10363
10364 /* Parse a namespace-definition.
10365
10366    namespace-definition:
10367      named-namespace-definition
10368      unnamed-namespace-definition
10369
10370    named-namespace-definition:
10371      original-namespace-definition
10372      extension-namespace-definition
10373
10374    original-namespace-definition:
10375      namespace identifier { namespace-body }
10376
10377    extension-namespace-definition:
10378      namespace original-namespace-name { namespace-body }
10379
10380    unnamed-namespace-definition:
10381      namespace { namespace-body } */
10382
10383 static void
10384 cp_parser_namespace_definition (cp_parser* parser)
10385 {
10386   tree identifier;
10387
10388   /* Look for the `namespace' keyword.  */
10389   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10390
10391   /* Get the name of the namespace.  We do not attempt to distinguish
10392      between an original-namespace-definition and an
10393      extension-namespace-definition at this point.  The semantic
10394      analysis routines are responsible for that.  */
10395   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10396     identifier = cp_parser_identifier (parser);
10397   else
10398     identifier = NULL_TREE;
10399
10400   /* Look for the `{' to start the namespace.  */
10401   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10402   /* Start the namespace.  */
10403   push_namespace (identifier);
10404   /* Parse the body of the namespace.  */
10405   cp_parser_namespace_body (parser);
10406   /* Finish the namespace.  */
10407   pop_namespace ();
10408   /* Look for the final `}'.  */
10409   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10410 }
10411
10412 /* Parse a namespace-body.
10413
10414    namespace-body:
10415      declaration-seq [opt]  */
10416
10417 static void
10418 cp_parser_namespace_body (cp_parser* parser)
10419 {
10420   cp_parser_declaration_seq_opt (parser);
10421 }
10422
10423 /* Parse a namespace-alias-definition.
10424
10425    namespace-alias-definition:
10426      namespace identifier = qualified-namespace-specifier ;  */
10427
10428 static void
10429 cp_parser_namespace_alias_definition (cp_parser* parser)
10430 {
10431   tree identifier;
10432   tree namespace_specifier;
10433
10434   /* Look for the `namespace' keyword.  */
10435   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10436   /* Look for the identifier.  */
10437   identifier = cp_parser_identifier (parser);
10438   if (identifier == error_mark_node)
10439     return;
10440   /* Look for the `=' token.  */
10441   cp_parser_require (parser, CPP_EQ, "`='");
10442   /* Look for the qualified-namespace-specifier.  */
10443   namespace_specifier
10444     = cp_parser_qualified_namespace_specifier (parser);
10445   /* Look for the `;' token.  */
10446   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10447
10448   /* Register the alias in the symbol table.  */
10449   do_namespace_alias (identifier, namespace_specifier);
10450 }
10451
10452 /* Parse a qualified-namespace-specifier.
10453
10454    qualified-namespace-specifier:
10455      :: [opt] nested-name-specifier [opt] namespace-name
10456
10457    Returns a NAMESPACE_DECL corresponding to the specified
10458    namespace.  */
10459
10460 static tree
10461 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10462 {
10463   /* Look for the optional `::'.  */
10464   cp_parser_global_scope_opt (parser,
10465                               /*current_scope_valid_p=*/false);
10466
10467   /* Look for the optional nested-name-specifier.  */
10468   cp_parser_nested_name_specifier_opt (parser,
10469                                        /*typename_keyword_p=*/false,
10470                                        /*check_dependency_p=*/true,
10471                                        /*type_p=*/false,
10472                                        /*is_declaration=*/true);
10473
10474   return cp_parser_namespace_name (parser);
10475 }
10476
10477 /* Parse a using-declaration.
10478
10479    using-declaration:
10480      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10481      using :: unqualified-id ;  */
10482
10483 static void
10484 cp_parser_using_declaration (cp_parser* parser)
10485 {
10486   cp_token *token;
10487   bool typename_p = false;
10488   bool global_scope_p;
10489   tree decl;
10490   tree identifier;
10491   tree qscope;
10492
10493   /* Look for the `using' keyword.  */
10494   cp_parser_require_keyword (parser, RID_USING, "`using'");
10495
10496   /* Peek at the next token.  */
10497   token = cp_lexer_peek_token (parser->lexer);
10498   /* See if it's `typename'.  */
10499   if (token->keyword == RID_TYPENAME)
10500     {
10501       /* Remember that we've seen it.  */
10502       typename_p = true;
10503       /* Consume the `typename' token.  */
10504       cp_lexer_consume_token (parser->lexer);
10505     }
10506
10507   /* Look for the optional global scope qualification.  */
10508   global_scope_p
10509     = (cp_parser_global_scope_opt (parser,
10510                                    /*current_scope_valid_p=*/false)
10511        != NULL_TREE);
10512
10513   /* If we saw `typename', or didn't see `::', then there must be a
10514      nested-name-specifier present.  */
10515   if (typename_p || !global_scope_p)
10516     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10517                                               /*check_dependency_p=*/true,
10518                                               /*type_p=*/false,
10519                                               /*is_declaration=*/true);
10520   /* Otherwise, we could be in either of the two productions.  In that
10521      case, treat the nested-name-specifier as optional.  */
10522   else
10523     qscope = cp_parser_nested_name_specifier_opt (parser,
10524                                                   /*typename_keyword_p=*/false,
10525                                                   /*check_dependency_p=*/true,
10526                                                   /*type_p=*/false,
10527                                                   /*is_declaration=*/true);
10528   if (!qscope)
10529     qscope = global_namespace;
10530
10531   /* Parse the unqualified-id.  */
10532   identifier = cp_parser_unqualified_id (parser,
10533                                          /*template_keyword_p=*/false,
10534                                          /*check_dependency_p=*/true,
10535                                          /*declarator_p=*/true);
10536
10537   /* The function we call to handle a using-declaration is different
10538      depending on what scope we are in.  */
10539   if (qscope == error_mark_node || identifier == error_mark_node)
10540     ;
10541   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10542            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10543     /* [namespace.udecl]
10544
10545        A using declaration shall not name a template-id.  */
10546     error ("a template-id may not appear in a using-declaration");
10547   else
10548     {
10549       if (at_class_scope_p ())
10550         {
10551           /* Create the USING_DECL.  */
10552           decl = do_class_using_decl (parser->scope, identifier);
10553           /* Add it to the list of members in this class.  */
10554           finish_member_declaration (decl);
10555         }
10556       else
10557         {
10558           decl = cp_parser_lookup_name_simple (parser, identifier);
10559           if (decl == error_mark_node)
10560             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10561           else if (!at_namespace_scope_p ())
10562             do_local_using_decl (decl, qscope, identifier);
10563           else
10564             do_toplevel_using_decl (decl, qscope, identifier);
10565         }
10566     }
10567
10568   /* Look for the final `;'.  */
10569   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10570 }
10571
10572 /* Parse a using-directive.
10573
10574    using-directive:
10575      using namespace :: [opt] nested-name-specifier [opt]
10576        namespace-name ;  */
10577
10578 static void
10579 cp_parser_using_directive (cp_parser* parser)
10580 {
10581   tree namespace_decl;
10582   tree attribs;
10583
10584   /* Look for the `using' keyword.  */
10585   cp_parser_require_keyword (parser, RID_USING, "`using'");
10586   /* And the `namespace' keyword.  */
10587   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10588   /* Look for the optional `::' operator.  */
10589   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10590   /* And the optional nested-name-specifier.  */
10591   cp_parser_nested_name_specifier_opt (parser,
10592                                        /*typename_keyword_p=*/false,
10593                                        /*check_dependency_p=*/true,
10594                                        /*type_p=*/false,
10595                                        /*is_declaration=*/true);
10596   /* Get the namespace being used.  */
10597   namespace_decl = cp_parser_namespace_name (parser);
10598   /* And any specified attributes.  */
10599   attribs = cp_parser_attributes_opt (parser);
10600   /* Update the symbol table.  */
10601   parse_using_directive (namespace_decl, attribs);
10602   /* Look for the final `;'.  */
10603   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10604 }
10605
10606 /* Parse an asm-definition.
10607
10608    asm-definition:
10609      asm ( string-literal ) ;
10610
10611    GNU Extension:
10612
10613    asm-definition:
10614      asm volatile [opt] ( string-literal ) ;
10615      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10616      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10617                           : asm-operand-list [opt] ) ;
10618      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10619                           : asm-operand-list [opt]
10620                           : asm-operand-list [opt] ) ;  */
10621
10622 static void
10623 cp_parser_asm_definition (cp_parser* parser)
10624 {
10625   tree string;
10626   tree outputs = NULL_TREE;
10627   tree inputs = NULL_TREE;
10628   tree clobbers = NULL_TREE;
10629   tree asm_stmt;
10630   bool volatile_p = false;
10631   bool extended_p = false;
10632
10633   /* Look for the `asm' keyword.  */
10634   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10635   /* See if the next token is `volatile'.  */
10636   if (cp_parser_allow_gnu_extensions_p (parser)
10637       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10638     {
10639       /* Remember that we saw the `volatile' keyword.  */
10640       volatile_p = true;
10641       /* Consume the token.  */
10642       cp_lexer_consume_token (parser->lexer);
10643     }
10644   /* Look for the opening `('.  */
10645   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10646     return;
10647   /* Look for the string.  */
10648   string = cp_parser_string_literal (parser, false, false);
10649   if (string == error_mark_node)
10650     {
10651       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10652                                              /*consume_paren=*/true);
10653       return;
10654     }
10655
10656   /* If we're allowing GNU extensions, check for the extended assembly
10657      syntax.  Unfortunately, the `:' tokens need not be separated by
10658      a space in C, and so, for compatibility, we tolerate that here
10659      too.  Doing that means that we have to treat the `::' operator as
10660      two `:' tokens.  */
10661   if (cp_parser_allow_gnu_extensions_p (parser)
10662       && at_function_scope_p ()
10663       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10664           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10665     {
10666       bool inputs_p = false;
10667       bool clobbers_p = false;
10668
10669       /* The extended syntax was used.  */
10670       extended_p = true;
10671
10672       /* Look for outputs.  */
10673       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10674         {
10675           /* Consume the `:'.  */
10676           cp_lexer_consume_token (parser->lexer);
10677           /* Parse the output-operands.  */
10678           if (cp_lexer_next_token_is_not (parser->lexer,
10679                                           CPP_COLON)
10680               && cp_lexer_next_token_is_not (parser->lexer,
10681                                              CPP_SCOPE)
10682               && cp_lexer_next_token_is_not (parser->lexer,
10683                                              CPP_CLOSE_PAREN))
10684             outputs = cp_parser_asm_operand_list (parser);
10685         }
10686       /* If the next token is `::', there are no outputs, and the
10687          next token is the beginning of the inputs.  */
10688       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10689         /* The inputs are coming next.  */
10690         inputs_p = true;
10691
10692       /* Look for inputs.  */
10693       if (inputs_p
10694           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10695         {
10696           /* Consume the `:' or `::'.  */
10697           cp_lexer_consume_token (parser->lexer);
10698           /* Parse the output-operands.  */
10699           if (cp_lexer_next_token_is_not (parser->lexer,
10700                                           CPP_COLON)
10701               && cp_lexer_next_token_is_not (parser->lexer,
10702                                              CPP_CLOSE_PAREN))
10703             inputs = cp_parser_asm_operand_list (parser);
10704         }
10705       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10706         /* The clobbers are coming next.  */
10707         clobbers_p = true;
10708
10709       /* Look for clobbers.  */
10710       if (clobbers_p
10711           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10712         {
10713           /* Consume the `:' or `::'.  */
10714           cp_lexer_consume_token (parser->lexer);
10715           /* Parse the clobbers.  */
10716           if (cp_lexer_next_token_is_not (parser->lexer,
10717                                           CPP_CLOSE_PAREN))
10718             clobbers = cp_parser_asm_clobber_list (parser);
10719         }
10720     }
10721   /* Look for the closing `)'.  */
10722   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10723     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10724                                            /*consume_paren=*/true);
10725   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10726
10727   /* Create the ASM_EXPR.  */
10728   if (at_function_scope_p ())
10729     {
10730       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10731                                   inputs, clobbers);
10732       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10733       if (!extended_p)
10734         {
10735           tree temp = asm_stmt;
10736           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10737             temp = TREE_OPERAND (temp, 0);
10738
10739           ASM_INPUT_P (temp) = 1;
10740         }
10741     }
10742   else
10743     assemble_asm (string);
10744 }
10745
10746 /* Declarators [gram.dcl.decl] */
10747
10748 /* Parse an init-declarator.
10749
10750    init-declarator:
10751      declarator initializer [opt]
10752
10753    GNU Extension:
10754
10755    init-declarator:
10756      declarator asm-specification [opt] attributes [opt] initializer [opt]
10757
10758    function-definition:
10759      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10760        function-body
10761      decl-specifier-seq [opt] declarator function-try-block
10762
10763    GNU Extension:
10764
10765    function-definition:
10766      __extension__ function-definition
10767
10768    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10769    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10770    then this declarator appears in a class scope.  The new DECL created
10771    by this declarator is returned.
10772
10773    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10774    for a function-definition here as well.  If the declarator is a
10775    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10776    be TRUE upon return.  By that point, the function-definition will
10777    have been completely parsed.
10778
10779    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10780    is FALSE.  */
10781
10782 static tree
10783 cp_parser_init_declarator (cp_parser* parser,
10784                            cp_decl_specifier_seq *decl_specifiers,
10785                            bool function_definition_allowed_p,
10786                            bool member_p,
10787                            int declares_class_or_enum,
10788                            bool* function_definition_p)
10789 {
10790   cp_token *token;
10791   cp_declarator *declarator;
10792   tree prefix_attributes;
10793   tree attributes;
10794   tree asm_specification;
10795   tree initializer;
10796   tree decl = NULL_TREE;
10797   tree scope;
10798   bool is_initialized;
10799   bool is_parenthesized_init;
10800   bool is_non_constant_init;
10801   int ctor_dtor_or_conv_p;
10802   bool friend_p;
10803   tree pushed_scope = NULL;
10804
10805   /* Gather the attributes that were provided with the
10806      decl-specifiers.  */
10807   prefix_attributes = decl_specifiers->attributes;
10808
10809   /* Assume that this is not the declarator for a function
10810      definition.  */
10811   if (function_definition_p)
10812     *function_definition_p = false;
10813
10814   /* Defer access checks while parsing the declarator; we cannot know
10815      what names are accessible until we know what is being
10816      declared.  */
10817   resume_deferring_access_checks ();
10818
10819   /* Parse the declarator.  */
10820   declarator
10821     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10822                             &ctor_dtor_or_conv_p,
10823                             /*parenthesized_p=*/NULL,
10824                             /*member_p=*/false);
10825   /* Gather up the deferred checks.  */
10826   stop_deferring_access_checks ();
10827
10828   /* If the DECLARATOR was erroneous, there's no need to go
10829      further.  */
10830   if (declarator == cp_error_declarator)
10831     return error_mark_node;
10832
10833   if (declares_class_or_enum & 2)
10834     cp_parser_check_for_definition_in_return_type (declarator,
10835                                                    decl_specifiers->type);
10836
10837   /* Figure out what scope the entity declared by the DECLARATOR is
10838      located in.  `grokdeclarator' sometimes changes the scope, so
10839      we compute it now.  */
10840   scope = get_scope_of_declarator (declarator);
10841
10842   /* If we're allowing GNU extensions, look for an asm-specification
10843      and attributes.  */
10844   if (cp_parser_allow_gnu_extensions_p (parser))
10845     {
10846       /* Look for an asm-specification.  */
10847       asm_specification = cp_parser_asm_specification_opt (parser);
10848       /* And attributes.  */
10849       attributes = cp_parser_attributes_opt (parser);
10850     }
10851   else
10852     {
10853       asm_specification = NULL_TREE;
10854       attributes = NULL_TREE;
10855     }
10856
10857   /* Peek at the next token.  */
10858   token = cp_lexer_peek_token (parser->lexer);
10859   /* Check to see if the token indicates the start of a
10860      function-definition.  */
10861   if (cp_parser_token_starts_function_definition_p (token))
10862     {
10863       if (!function_definition_allowed_p)
10864         {
10865           /* If a function-definition should not appear here, issue an
10866              error message.  */
10867           cp_parser_error (parser,
10868                            "a function-definition is not allowed here");
10869           return error_mark_node;
10870         }
10871       else
10872         {
10873           /* Neither attributes nor an asm-specification are allowed
10874              on a function-definition.  */
10875           if (asm_specification)
10876             error ("an asm-specification is not allowed on a function-definition");
10877           if (attributes)
10878             error ("attributes are not allowed on a function-definition");
10879           /* This is a function-definition.  */
10880           *function_definition_p = true;
10881
10882           /* Parse the function definition.  */
10883           if (member_p)
10884             decl = cp_parser_save_member_function_body (parser,
10885                                                         decl_specifiers,
10886                                                         declarator,
10887                                                         prefix_attributes);
10888           else
10889             decl
10890               = (cp_parser_function_definition_from_specifiers_and_declarator
10891                  (parser, decl_specifiers, prefix_attributes, declarator));
10892
10893           return decl;
10894         }
10895     }
10896
10897   /* [dcl.dcl]
10898
10899      Only in function declarations for constructors, destructors, and
10900      type conversions can the decl-specifier-seq be omitted.
10901
10902      We explicitly postpone this check past the point where we handle
10903      function-definitions because we tolerate function-definitions
10904      that are missing their return types in some modes.  */
10905   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10906     {
10907       cp_parser_error (parser,
10908                        "expected constructor, destructor, or type conversion");
10909       return error_mark_node;
10910     }
10911
10912   /* An `=' or an `(' indicates an initializer.  */
10913   is_initialized = (token->type == CPP_EQ
10914                      || token->type == CPP_OPEN_PAREN);
10915   /* If the init-declarator isn't initialized and isn't followed by a
10916      `,' or `;', it's not a valid init-declarator.  */
10917   if (!is_initialized
10918       && token->type != CPP_COMMA
10919       && token->type != CPP_SEMICOLON)
10920     {
10921       cp_parser_error (parser, "expected initializer");
10922       return error_mark_node;
10923     }
10924
10925   /* Because start_decl has side-effects, we should only call it if we
10926      know we're going ahead.  By this point, we know that we cannot
10927      possibly be looking at any other construct.  */
10928   cp_parser_commit_to_tentative_parse (parser);
10929
10930   /* If the decl specifiers were bad, issue an error now that we're
10931      sure this was intended to be a declarator.  Then continue
10932      declaring the variable(s), as int, to try to cut down on further
10933      errors.  */
10934   if (decl_specifiers->any_specifiers_p
10935       && decl_specifiers->type == error_mark_node)
10936     {
10937       cp_parser_error (parser, "invalid type in declaration");
10938       decl_specifiers->type = integer_type_node;
10939     }
10940
10941   /* Check to see whether or not this declaration is a friend.  */
10942   friend_p = cp_parser_friend_p (decl_specifiers);
10943
10944   /* Check that the number of template-parameter-lists is OK.  */
10945   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10946     return error_mark_node;
10947
10948   /* Enter the newly declared entry in the symbol table.  If we're
10949      processing a declaration in a class-specifier, we wait until
10950      after processing the initializer.  */
10951   if (!member_p)
10952     {
10953       if (parser->in_unbraced_linkage_specification_p)
10954         {
10955           decl_specifiers->storage_class = sc_extern;
10956           have_extern_spec = false;
10957         }
10958       decl = start_decl (declarator, decl_specifiers,
10959                          is_initialized, attributes, prefix_attributes,
10960                          &pushed_scope);
10961     }
10962   else if (scope)
10963     /* Enter the SCOPE.  That way unqualified names appearing in the
10964        initializer will be looked up in SCOPE.  */
10965     pushed_scope = push_scope (scope);
10966
10967   /* Perform deferred access control checks, now that we know in which
10968      SCOPE the declared entity resides.  */
10969   if (!member_p && decl)
10970     {
10971       tree saved_current_function_decl = NULL_TREE;
10972
10973       /* If the entity being declared is a function, pretend that we
10974          are in its scope.  If it is a `friend', it may have access to
10975          things that would not otherwise be accessible.  */
10976       if (TREE_CODE (decl) == FUNCTION_DECL)
10977         {
10978           saved_current_function_decl = current_function_decl;
10979           current_function_decl = decl;
10980         }
10981
10982       /* Perform the access control checks for the declarator and the
10983          the decl-specifiers.  */
10984       perform_deferred_access_checks ();
10985
10986       /* Restore the saved value.  */
10987       if (TREE_CODE (decl) == FUNCTION_DECL)
10988         current_function_decl = saved_current_function_decl;
10989     }
10990
10991   /* Parse the initializer.  */
10992   if (is_initialized)
10993     initializer = cp_parser_initializer (parser,
10994                                          &is_parenthesized_init,
10995                                          &is_non_constant_init);
10996   else
10997     {
10998       initializer = NULL_TREE;
10999       is_parenthesized_init = false;
11000       is_non_constant_init = true;
11001     }
11002
11003   /* The old parser allows attributes to appear after a parenthesized
11004      initializer.  Mark Mitchell proposed removing this functionality
11005      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11006      attributes -- but ignores them.  */
11007   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11008     if (cp_parser_attributes_opt (parser))
11009       warning (OPT_Wattributes,
11010                "attributes after parenthesized initializer ignored");
11011
11012   /* For an in-class declaration, use `grokfield' to create the
11013      declaration.  */
11014   if (member_p)
11015     {
11016       if (pushed_scope)
11017         {
11018           pop_scope (pushed_scope);
11019           pushed_scope = false;
11020         }
11021       decl = grokfield (declarator, decl_specifiers,
11022                         initializer, /*asmspec=*/NULL_TREE,
11023                         prefix_attributes);
11024       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11025         cp_parser_save_default_args (parser, decl);
11026     }
11027
11028   /* Finish processing the declaration.  But, skip friend
11029      declarations.  */
11030   if (!friend_p && decl && decl != error_mark_node)
11031     {
11032       cp_finish_decl (decl,
11033                       initializer,
11034                       asm_specification,
11035                       /* If the initializer is in parentheses, then this is
11036                          a direct-initialization, which means that an
11037                          `explicit' constructor is OK.  Otherwise, an
11038                          `explicit' constructor cannot be used.  */
11039                       ((is_parenthesized_init || !is_initialized)
11040                      ? 0 : LOOKUP_ONLYCONVERTING));
11041     }
11042   if (!friend_p && pushed_scope)
11043     pop_scope (pushed_scope);
11044
11045   /* Remember whether or not variables were initialized by
11046      constant-expressions.  */
11047   if (decl && TREE_CODE (decl) == VAR_DECL
11048       && is_initialized && !is_non_constant_init)
11049     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
11050
11051   return decl;
11052 }
11053
11054 /* Parse a declarator.
11055
11056    declarator:
11057      direct-declarator
11058      ptr-operator declarator
11059
11060    abstract-declarator:
11061      ptr-operator abstract-declarator [opt]
11062      direct-abstract-declarator
11063
11064    GNU Extensions:
11065
11066    declarator:
11067      attributes [opt] direct-declarator
11068      attributes [opt] ptr-operator declarator
11069
11070    abstract-declarator:
11071      attributes [opt] ptr-operator abstract-declarator [opt]
11072      attributes [opt] direct-abstract-declarator
11073
11074    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11075    detect constructor, destructor or conversion operators. It is set
11076    to -1 if the declarator is a name, and +1 if it is a
11077    function. Otherwise it is set to zero. Usually you just want to
11078    test for >0, but internally the negative value is used.
11079
11080    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11081    a decl-specifier-seq unless it declares a constructor, destructor,
11082    or conversion.  It might seem that we could check this condition in
11083    semantic analysis, rather than parsing, but that makes it difficult
11084    to handle something like `f()'.  We want to notice that there are
11085    no decl-specifiers, and therefore realize that this is an
11086    expression, not a declaration.)
11087
11088    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11089    the declarator is a direct-declarator of the form "(...)".
11090
11091    MEMBER_P is true iff this declarator is a member-declarator.  */
11092
11093 static cp_declarator *
11094 cp_parser_declarator (cp_parser* parser,
11095                       cp_parser_declarator_kind dcl_kind,
11096                       int* ctor_dtor_or_conv_p,
11097                       bool* parenthesized_p,
11098                       bool member_p)
11099 {
11100   cp_token *token;
11101   cp_declarator *declarator;
11102   enum tree_code code;
11103   cp_cv_quals cv_quals;
11104   tree class_type;
11105   tree attributes = NULL_TREE;
11106
11107   /* Assume this is not a constructor, destructor, or type-conversion
11108      operator.  */
11109   if (ctor_dtor_or_conv_p)
11110     *ctor_dtor_or_conv_p = 0;
11111
11112   if (cp_parser_allow_gnu_extensions_p (parser))
11113     attributes = cp_parser_attributes_opt (parser);
11114
11115   /* Peek at the next token.  */
11116   token = cp_lexer_peek_token (parser->lexer);
11117
11118   /* Check for the ptr-operator production.  */
11119   cp_parser_parse_tentatively (parser);
11120   /* Parse the ptr-operator.  */
11121   code = cp_parser_ptr_operator (parser,
11122                                  &class_type,
11123                                  &cv_quals);
11124   /* If that worked, then we have a ptr-operator.  */
11125   if (cp_parser_parse_definitely (parser))
11126     {
11127       /* If a ptr-operator was found, then this declarator was not
11128          parenthesized.  */
11129       if (parenthesized_p)
11130         *parenthesized_p = true;
11131       /* The dependent declarator is optional if we are parsing an
11132          abstract-declarator.  */
11133       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11134         cp_parser_parse_tentatively (parser);
11135
11136       /* Parse the dependent declarator.  */
11137       declarator = cp_parser_declarator (parser, dcl_kind,
11138                                          /*ctor_dtor_or_conv_p=*/NULL,
11139                                          /*parenthesized_p=*/NULL,
11140                                          /*member_p=*/false);
11141
11142       /* If we are parsing an abstract-declarator, we must handle the
11143          case where the dependent declarator is absent.  */
11144       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11145           && !cp_parser_parse_definitely (parser))
11146         declarator = NULL;
11147
11148       /* Build the representation of the ptr-operator.  */
11149       if (class_type)
11150         declarator = make_ptrmem_declarator (cv_quals,
11151                                              class_type,
11152                                              declarator);
11153       else if (code == INDIRECT_REF)
11154         declarator = make_pointer_declarator (cv_quals, declarator);
11155       else
11156         declarator = make_reference_declarator (cv_quals, declarator);
11157     }
11158   /* Everything else is a direct-declarator.  */
11159   else
11160     {
11161       if (parenthesized_p)
11162         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11163                                                    CPP_OPEN_PAREN);
11164       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11165                                                 ctor_dtor_or_conv_p,
11166                                                 member_p);
11167     }
11168
11169   if (attributes && declarator != cp_error_declarator)
11170     declarator->attributes = attributes;
11171
11172   return declarator;
11173 }
11174
11175 /* Parse a direct-declarator or direct-abstract-declarator.
11176
11177    direct-declarator:
11178      declarator-id
11179      direct-declarator ( parameter-declaration-clause )
11180        cv-qualifier-seq [opt]
11181        exception-specification [opt]
11182      direct-declarator [ constant-expression [opt] ]
11183      ( declarator )
11184
11185    direct-abstract-declarator:
11186      direct-abstract-declarator [opt]
11187        ( parameter-declaration-clause )
11188        cv-qualifier-seq [opt]
11189        exception-specification [opt]
11190      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11191      ( abstract-declarator )
11192
11193    Returns a representation of the declarator.  DCL_KIND is
11194    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11195    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11196    we are parsing a direct-declarator.  It is
11197    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11198    of ambiguity we prefer an abstract declarator, as per
11199    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11200    cp_parser_declarator.  */
11201
11202 static cp_declarator *
11203 cp_parser_direct_declarator (cp_parser* parser,
11204                              cp_parser_declarator_kind dcl_kind,
11205                              int* ctor_dtor_or_conv_p,
11206                              bool member_p)
11207 {
11208   cp_token *token;
11209   cp_declarator *declarator = NULL;
11210   tree scope = NULL_TREE;
11211   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11212   bool saved_in_declarator_p = parser->in_declarator_p;
11213   bool first = true;
11214   tree pushed_scope = NULL_TREE;
11215
11216   while (true)
11217     {
11218       /* Peek at the next token.  */
11219       token = cp_lexer_peek_token (parser->lexer);
11220       if (token->type == CPP_OPEN_PAREN)
11221         {
11222           /* This is either a parameter-declaration-clause, or a
11223              parenthesized declarator. When we know we are parsing a
11224              named declarator, it must be a parenthesized declarator
11225              if FIRST is true. For instance, `(int)' is a
11226              parameter-declaration-clause, with an omitted
11227              direct-abstract-declarator. But `((*))', is a
11228              parenthesized abstract declarator. Finally, when T is a
11229              template parameter `(T)' is a
11230              parameter-declaration-clause, and not a parenthesized
11231              named declarator.
11232
11233              We first try and parse a parameter-declaration-clause,
11234              and then try a nested declarator (if FIRST is true).
11235
11236              It is not an error for it not to be a
11237              parameter-declaration-clause, even when FIRST is
11238              false. Consider,
11239
11240                int i (int);
11241                int i (3);
11242
11243              The first is the declaration of a function while the
11244              second is a the definition of a variable, including its
11245              initializer.
11246
11247              Having seen only the parenthesis, we cannot know which of
11248              these two alternatives should be selected.  Even more
11249              complex are examples like:
11250
11251                int i (int (a));
11252                int i (int (3));
11253
11254              The former is a function-declaration; the latter is a
11255              variable initialization.
11256
11257              Thus again, we try a parameter-declaration-clause, and if
11258              that fails, we back out and return.  */
11259
11260           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11261             {
11262               cp_parameter_declarator *params;
11263               unsigned saved_num_template_parameter_lists;
11264
11265               /* In a member-declarator, the only valid interpretation
11266                  of a parenthesis is the start of a
11267                  parameter-declaration-clause.  (It is invalid to
11268                  initialize a static data member with a parenthesized
11269                  initializer; only the "=" form of initialization is
11270                  permitted.)  */
11271               if (!member_p)
11272                 cp_parser_parse_tentatively (parser);
11273
11274               /* Consume the `('.  */
11275               cp_lexer_consume_token (parser->lexer);
11276               if (first)
11277                 {
11278                   /* If this is going to be an abstract declarator, we're
11279                      in a declarator and we can't have default args.  */
11280                   parser->default_arg_ok_p = false;
11281                   parser->in_declarator_p = true;
11282                 }
11283
11284               /* Inside the function parameter list, surrounding
11285                  template-parameter-lists do not apply.  */
11286               saved_num_template_parameter_lists
11287                 = parser->num_template_parameter_lists;
11288               parser->num_template_parameter_lists = 0;
11289
11290               /* Parse the parameter-declaration-clause.  */
11291               params = cp_parser_parameter_declaration_clause (parser);
11292
11293               parser->num_template_parameter_lists
11294                 = saved_num_template_parameter_lists;
11295
11296               /* If all went well, parse the cv-qualifier-seq and the
11297                  exception-specification.  */
11298               if (member_p || cp_parser_parse_definitely (parser))
11299                 {
11300                   cp_cv_quals cv_quals;
11301                   tree exception_specification;
11302
11303                   if (ctor_dtor_or_conv_p)
11304                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11305                   first = false;
11306                   /* Consume the `)'.  */
11307                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11308
11309                   /* Parse the cv-qualifier-seq.  */
11310                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11311                   /* And the exception-specification.  */
11312                   exception_specification
11313                     = cp_parser_exception_specification_opt (parser);
11314
11315                   /* Create the function-declarator.  */
11316                   declarator = make_call_declarator (declarator,
11317                                                      params,
11318                                                      cv_quals,
11319                                                      exception_specification);
11320                   /* Any subsequent parameter lists are to do with
11321                      return type, so are not those of the declared
11322                      function.  */
11323                   parser->default_arg_ok_p = false;
11324
11325                   /* Repeat the main loop.  */
11326                   continue;
11327                 }
11328             }
11329
11330           /* If this is the first, we can try a parenthesized
11331              declarator.  */
11332           if (first)
11333             {
11334               bool saved_in_type_id_in_expr_p;
11335
11336               parser->default_arg_ok_p = saved_default_arg_ok_p;
11337               parser->in_declarator_p = saved_in_declarator_p;
11338
11339               /* Consume the `('.  */
11340               cp_lexer_consume_token (parser->lexer);
11341               /* Parse the nested declarator.  */
11342               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11343               parser->in_type_id_in_expr_p = true;
11344               declarator
11345                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11346                                         /*parenthesized_p=*/NULL,
11347                                         member_p);
11348               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11349               first = false;
11350               /* Expect a `)'.  */
11351               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11352                 declarator = cp_error_declarator;
11353               if (declarator == cp_error_declarator)
11354                 break;
11355
11356               goto handle_declarator;
11357             }
11358           /* Otherwise, we must be done.  */
11359           else
11360             break;
11361         }
11362       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11363                && token->type == CPP_OPEN_SQUARE)
11364         {
11365           /* Parse an array-declarator.  */
11366           tree bounds;
11367
11368           if (ctor_dtor_or_conv_p)
11369             *ctor_dtor_or_conv_p = 0;
11370
11371           first = false;
11372           parser->default_arg_ok_p = false;
11373           parser->in_declarator_p = true;
11374           /* Consume the `['.  */
11375           cp_lexer_consume_token (parser->lexer);
11376           /* Peek at the next token.  */
11377           token = cp_lexer_peek_token (parser->lexer);
11378           /* If the next token is `]', then there is no
11379              constant-expression.  */
11380           if (token->type != CPP_CLOSE_SQUARE)
11381             {
11382               bool non_constant_p;
11383
11384               bounds
11385                 = cp_parser_constant_expression (parser,
11386                                                  /*allow_non_constant=*/true,
11387                                                  &non_constant_p);
11388               if (!non_constant_p)
11389                 bounds = fold_non_dependent_expr (bounds);
11390               /* Normally, the array bound must be an integral constant
11391                  expression.  However, as an extension, we allow VLAs
11392                  in function scopes.  */
11393               else if (!at_function_scope_p ())
11394                 {
11395                   error ("array bound is not an integer constant");
11396                   bounds = error_mark_node;
11397                 }
11398             }
11399           else
11400             bounds = NULL_TREE;
11401           /* Look for the closing `]'.  */
11402           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11403             {
11404               declarator = cp_error_declarator;
11405               break;
11406             }
11407
11408           declarator = make_array_declarator (declarator, bounds);
11409         }
11410       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11411         {
11412           tree qualifying_scope;
11413           tree unqualified_name;
11414           special_function_kind sfk;
11415
11416           /* Parse a declarator-id */
11417           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11418             cp_parser_parse_tentatively (parser);
11419           unqualified_name = cp_parser_declarator_id (parser);
11420           qualifying_scope = parser->scope;
11421           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11422             {
11423               if (!cp_parser_parse_definitely (parser))
11424                 unqualified_name = error_mark_node;
11425               else if (qualifying_scope
11426                        || (TREE_CODE (unqualified_name)
11427                            != IDENTIFIER_NODE))
11428                 {
11429                   cp_parser_error (parser, "expected unqualified-id");
11430                   unqualified_name = error_mark_node;
11431                 }
11432             }
11433
11434           if (unqualified_name == error_mark_node)
11435             {
11436               declarator = cp_error_declarator;
11437               break;
11438             }
11439
11440           if (qualifying_scope && at_namespace_scope_p ()
11441               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11442             {
11443               /* In the declaration of a member of a template class
11444                  outside of the class itself, the SCOPE will sometimes
11445                  be a TYPENAME_TYPE.  For example, given:
11446
11447                  template <typename T>
11448                  int S<T>::R::i = 3;
11449
11450                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11451                  this context, we must resolve S<T>::R to an ordinary
11452                  type, rather than a typename type.
11453
11454                  The reason we normally avoid resolving TYPENAME_TYPEs
11455                  is that a specialization of `S' might render
11456                  `S<T>::R' not a type.  However, if `S' is
11457                  specialized, then this `i' will not be used, so there
11458                  is no harm in resolving the types here.  */
11459               tree type;
11460
11461               /* Resolve the TYPENAME_TYPE.  */
11462               type = resolve_typename_type (qualifying_scope,
11463                                             /*only_current_p=*/false);
11464               /* If that failed, the declarator is invalid.  */
11465               if (type == error_mark_node)
11466                 error ("%<%T::%D%> is not a type",
11467                        TYPE_CONTEXT (qualifying_scope),
11468                        TYPE_IDENTIFIER (qualifying_scope));
11469               qualifying_scope = type;
11470             }
11471
11472           sfk = sfk_none;
11473           if (unqualified_name)
11474             {
11475               tree class_type;
11476
11477               if (qualifying_scope
11478                   && CLASS_TYPE_P (qualifying_scope))
11479                 class_type = qualifying_scope;
11480               else
11481                 class_type = current_class_type;
11482
11483               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11484                 {
11485                   if (qualifying_scope 
11486                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11487                     {
11488                       error ("invalid use of constructor as a template");
11489                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11490                               "the constructor in a qualified name",
11491                               class_type,
11492                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11493                               class_type, class_type);
11494                       declarator = cp_error_declarator;
11495                       break;
11496                     }
11497                   else if (class_type
11498                            && same_type_p (TREE_TYPE (unqualified_name),
11499                                            class_type))
11500                     unqualified_name = constructor_name (class_type);
11501                   else
11502                     {
11503                       /* We do not attempt to print the declarator
11504                          here because we do not have enough
11505                          information about its original syntactic
11506                          form.  */
11507                       error ("invalid declarator");
11508                       declarator = cp_error_declarator;
11509                       break;
11510                     }
11511                 }
11512
11513               if (class_type)
11514                 {
11515                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11516                     sfk = sfk_destructor;
11517                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11518                     sfk = sfk_conversion;
11519                   else if (/* There's no way to declare a constructor
11520                               for an anonymous type, even if the type
11521                               got a name for linkage purposes.  */
11522                            !TYPE_WAS_ANONYMOUS (class_type)
11523                            && constructor_name_p (unqualified_name,
11524                                                   class_type))
11525                     {
11526                       unqualified_name = constructor_name (class_type);
11527                       sfk = sfk_constructor;
11528                     }
11529
11530                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11531                     *ctor_dtor_or_conv_p = -1;
11532                 }
11533             }
11534           declarator = make_id_declarator (qualifying_scope, 
11535                                            unqualified_name,
11536                                            sfk);
11537           declarator->id_loc = token->location;
11538
11539         handle_declarator:;
11540           scope = get_scope_of_declarator (declarator);
11541           if (scope)
11542             /* Any names that appear after the declarator-id for a
11543                member are looked up in the containing scope.  */
11544             pushed_scope = push_scope (scope);
11545           parser->in_declarator_p = true;
11546           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11547               || (declarator && declarator->kind == cdk_id))
11548             /* Default args are only allowed on function
11549                declarations.  */
11550             parser->default_arg_ok_p = saved_default_arg_ok_p;
11551           else
11552             parser->default_arg_ok_p = false;
11553
11554           first = false;
11555         }
11556       /* We're done.  */
11557       else
11558         break;
11559     }
11560
11561   /* For an abstract declarator, we might wind up with nothing at this
11562      point.  That's an error; the declarator is not optional.  */
11563   if (!declarator)
11564     cp_parser_error (parser, "expected declarator");
11565
11566   /* If we entered a scope, we must exit it now.  */
11567   if (pushed_scope)
11568     pop_scope (pushed_scope);
11569
11570   parser->default_arg_ok_p = saved_default_arg_ok_p;
11571   parser->in_declarator_p = saved_in_declarator_p;
11572
11573   return declarator;
11574 }
11575
11576 /* Parse a ptr-operator.
11577
11578    ptr-operator:
11579      * cv-qualifier-seq [opt]
11580      &
11581      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11582
11583    GNU Extension:
11584
11585    ptr-operator:
11586      & cv-qualifier-seq [opt]
11587
11588    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11589    Returns ADDR_EXPR if a reference was used.  In the case of a
11590    pointer-to-member, *TYPE is filled in with the TYPE containing the
11591    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11592    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11593    ERROR_MARK if an error occurred.  */
11594
11595 static enum tree_code
11596 cp_parser_ptr_operator (cp_parser* parser,
11597                         tree* type,
11598                         cp_cv_quals *cv_quals)
11599 {
11600   enum tree_code code = ERROR_MARK;
11601   cp_token *token;
11602
11603   /* Assume that it's not a pointer-to-member.  */
11604   *type = NULL_TREE;
11605   /* And that there are no cv-qualifiers.  */
11606   *cv_quals = TYPE_UNQUALIFIED;
11607
11608   /* Peek at the next token.  */
11609   token = cp_lexer_peek_token (parser->lexer);
11610   /* If it's a `*' or `&' we have a pointer or reference.  */
11611   if (token->type == CPP_MULT || token->type == CPP_AND)
11612     {
11613       /* Remember which ptr-operator we were processing.  */
11614       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11615
11616       /* Consume the `*' or `&'.  */
11617       cp_lexer_consume_token (parser->lexer);
11618
11619       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11620          `&', if we are allowing GNU extensions.  (The only qualifier
11621          that can legally appear after `&' is `restrict', but that is
11622          enforced during semantic analysis.  */
11623       if (code == INDIRECT_REF
11624           || cp_parser_allow_gnu_extensions_p (parser))
11625         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11626     }
11627   else
11628     {
11629       /* Try the pointer-to-member case.  */
11630       cp_parser_parse_tentatively (parser);
11631       /* Look for the optional `::' operator.  */
11632       cp_parser_global_scope_opt (parser,
11633                                   /*current_scope_valid_p=*/false);
11634       /* Look for the nested-name specifier.  */
11635       cp_parser_nested_name_specifier (parser,
11636                                        /*typename_keyword_p=*/false,
11637                                        /*check_dependency_p=*/true,
11638                                        /*type_p=*/false,
11639                                        /*is_declaration=*/false);
11640       /* If we found it, and the next token is a `*', then we are
11641          indeed looking at a pointer-to-member operator.  */
11642       if (!cp_parser_error_occurred (parser)
11643           && cp_parser_require (parser, CPP_MULT, "`*'"))
11644         {
11645           /* The type of which the member is a member is given by the
11646              current SCOPE.  */
11647           *type = parser->scope;
11648           /* The next name will not be qualified.  */
11649           parser->scope = NULL_TREE;
11650           parser->qualifying_scope = NULL_TREE;
11651           parser->object_scope = NULL_TREE;
11652           /* Indicate that the `*' operator was used.  */
11653           code = INDIRECT_REF;
11654           /* Look for the optional cv-qualifier-seq.  */
11655           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11656         }
11657       /* If that didn't work we don't have a ptr-operator.  */
11658       if (!cp_parser_parse_definitely (parser))
11659         cp_parser_error (parser, "expected ptr-operator");
11660     }
11661
11662   return code;
11663 }
11664
11665 /* Parse an (optional) cv-qualifier-seq.
11666
11667    cv-qualifier-seq:
11668      cv-qualifier cv-qualifier-seq [opt]
11669
11670    cv-qualifier:
11671      const
11672      volatile
11673
11674    GNU Extension:
11675
11676    cv-qualifier:
11677      __restrict__
11678
11679    Returns a bitmask representing the cv-qualifiers.  */
11680
11681 static cp_cv_quals
11682 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11683 {
11684   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11685
11686   while (true)
11687     {
11688       cp_token *token;
11689       cp_cv_quals cv_qualifier;
11690
11691       /* Peek at the next token.  */
11692       token = cp_lexer_peek_token (parser->lexer);
11693       /* See if it's a cv-qualifier.  */
11694       switch (token->keyword)
11695         {
11696         case RID_CONST:
11697           cv_qualifier = TYPE_QUAL_CONST;
11698           break;
11699
11700         case RID_VOLATILE:
11701           cv_qualifier = TYPE_QUAL_VOLATILE;
11702           break;
11703
11704         case RID_RESTRICT:
11705           cv_qualifier = TYPE_QUAL_RESTRICT;
11706           break;
11707
11708         default:
11709           cv_qualifier = TYPE_UNQUALIFIED;
11710           break;
11711         }
11712
11713       if (!cv_qualifier)
11714         break;
11715
11716       if (cv_quals & cv_qualifier)
11717         {
11718           error ("duplicate cv-qualifier");
11719           cp_lexer_purge_token (parser->lexer);
11720         }
11721       else
11722         {
11723           cp_lexer_consume_token (parser->lexer);
11724           cv_quals |= cv_qualifier;
11725         }
11726     }
11727
11728   return cv_quals;
11729 }
11730
11731 /* Parse a declarator-id.
11732
11733    declarator-id:
11734      id-expression
11735      :: [opt] nested-name-specifier [opt] type-name
11736
11737    In the `id-expression' case, the value returned is as for
11738    cp_parser_id_expression if the id-expression was an unqualified-id.
11739    If the id-expression was a qualified-id, then a SCOPE_REF is
11740    returned.  The first operand is the scope (either a NAMESPACE_DECL
11741    or TREE_TYPE), but the second is still just a representation of an
11742    unqualified-id.  */
11743
11744 static tree
11745 cp_parser_declarator_id (cp_parser* parser)
11746 {
11747   tree id;
11748   /* The expression must be an id-expression.  Assume that qualified
11749      names are the names of types so that:
11750
11751        template <class T>
11752        int S<T>::R::i = 3;
11753
11754      will work; we must treat `S<T>::R' as the name of a type.
11755      Similarly, assume that qualified names are templates, where
11756      required, so that:
11757
11758        template <class T>
11759        int S<T>::R<T>::i = 3;
11760
11761      will work, too.  */
11762   id = cp_parser_id_expression (parser,
11763                                 /*template_keyword_p=*/false,
11764                                 /*check_dependency_p=*/false,
11765                                 /*template_p=*/NULL,
11766                                 /*declarator_p=*/true);
11767   if (BASELINK_P (id))
11768     id = BASELINK_FUNCTIONS (id);
11769   return id;
11770 }
11771
11772 /* Parse a type-id.
11773
11774    type-id:
11775      type-specifier-seq abstract-declarator [opt]
11776
11777    Returns the TYPE specified.  */
11778
11779 static tree
11780 cp_parser_type_id (cp_parser* parser)
11781 {
11782   cp_decl_specifier_seq type_specifier_seq;
11783   cp_declarator *abstract_declarator;
11784
11785   /* Parse the type-specifier-seq.  */
11786   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11787                                 &type_specifier_seq);
11788   if (type_specifier_seq.type == error_mark_node)
11789     return error_mark_node;
11790
11791   /* There might or might not be an abstract declarator.  */
11792   cp_parser_parse_tentatively (parser);
11793   /* Look for the declarator.  */
11794   abstract_declarator
11795     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11796                             /*parenthesized_p=*/NULL,
11797                             /*member_p=*/false);
11798   /* Check to see if there really was a declarator.  */
11799   if (!cp_parser_parse_definitely (parser))
11800     abstract_declarator = NULL;
11801
11802   return groktypename (&type_specifier_seq, abstract_declarator);
11803 }
11804
11805 /* Parse a type-specifier-seq.
11806
11807    type-specifier-seq:
11808      type-specifier type-specifier-seq [opt]
11809
11810    GNU extension:
11811
11812    type-specifier-seq:
11813      attributes type-specifier-seq [opt]
11814
11815    If IS_CONDITION is true, we are at the start of a "condition",
11816    e.g., we've just seen "if (".
11817
11818    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11819
11820 static void
11821 cp_parser_type_specifier_seq (cp_parser* parser,
11822                               bool is_condition,
11823                               cp_decl_specifier_seq *type_specifier_seq)
11824 {
11825   bool seen_type_specifier = false;
11826   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11827
11828   /* Clear the TYPE_SPECIFIER_SEQ.  */
11829   clear_decl_specs (type_specifier_seq);
11830
11831   /* Parse the type-specifiers and attributes.  */
11832   while (true)
11833     {
11834       tree type_specifier;
11835       bool is_cv_qualifier;
11836
11837       /* Check for attributes first.  */
11838       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11839         {
11840           type_specifier_seq->attributes =
11841             chainon (type_specifier_seq->attributes,
11842                      cp_parser_attributes_opt (parser));
11843           continue;
11844         }
11845
11846       /* Look for the type-specifier.  */
11847       type_specifier = cp_parser_type_specifier (parser,
11848                                                  flags,
11849                                                  type_specifier_seq,
11850                                                  /*is_declaration=*/false,
11851                                                  NULL,
11852                                                  &is_cv_qualifier);
11853       if (!type_specifier)
11854         {
11855           /* If the first type-specifier could not be found, this is not a
11856              type-specifier-seq at all.  */
11857           if (!seen_type_specifier)
11858             {
11859               cp_parser_error (parser, "expected type-specifier");
11860               type_specifier_seq->type = error_mark_node;
11861               return;
11862             }
11863           /* If subsequent type-specifiers could not be found, the
11864              type-specifier-seq is complete.  */
11865           break;
11866         }
11867
11868       seen_type_specifier = true;
11869       /* The standard says that a condition can be:
11870
11871             type-specifier-seq declarator = assignment-expression
11872
11873          However, given:
11874
11875            struct S {};
11876            if (int S = ...)
11877
11878          we should treat the "S" as a declarator, not as a
11879          type-specifier.  The standard doesn't say that explicitly for
11880          type-specifier-seq, but it does say that for
11881          decl-specifier-seq in an ordinary declaration.  Perhaps it
11882          would be clearer just to allow a decl-specifier-seq here, and
11883          then add a semantic restriction that if any decl-specifiers
11884          that are not type-specifiers appear, the program is invalid.  */
11885       if (is_condition && !is_cv_qualifier)
11886         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11887     }
11888
11889   return;
11890 }
11891
11892 /* Parse a parameter-declaration-clause.
11893
11894    parameter-declaration-clause:
11895      parameter-declaration-list [opt] ... [opt]
11896      parameter-declaration-list , ...
11897
11898    Returns a representation for the parameter declarations.  A return
11899    value of NULL indicates a parameter-declaration-clause consisting
11900    only of an ellipsis.  */
11901
11902 static cp_parameter_declarator *
11903 cp_parser_parameter_declaration_clause (cp_parser* parser)
11904 {
11905   cp_parameter_declarator *parameters;
11906   cp_token *token;
11907   bool ellipsis_p;
11908   bool is_error;
11909
11910   /* Peek at the next token.  */
11911   token = cp_lexer_peek_token (parser->lexer);
11912   /* Check for trivial parameter-declaration-clauses.  */
11913   if (token->type == CPP_ELLIPSIS)
11914     {
11915       /* Consume the `...' token.  */
11916       cp_lexer_consume_token (parser->lexer);
11917       return NULL;
11918     }
11919   else if (token->type == CPP_CLOSE_PAREN)
11920     /* There are no parameters.  */
11921     {
11922 #ifndef NO_IMPLICIT_EXTERN_C
11923       if (in_system_header && current_class_type == NULL
11924           && current_lang_name == lang_name_c)
11925         return NULL;
11926       else
11927 #endif
11928         return no_parameters;
11929     }
11930   /* Check for `(void)', too, which is a special case.  */
11931   else if (token->keyword == RID_VOID
11932            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11933                == CPP_CLOSE_PAREN))
11934     {
11935       /* Consume the `void' token.  */
11936       cp_lexer_consume_token (parser->lexer);
11937       /* There are no parameters.  */
11938       return no_parameters;
11939     }
11940
11941   /* Parse the parameter-declaration-list.  */
11942   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11943   /* If a parse error occurred while parsing the
11944      parameter-declaration-list, then the entire
11945      parameter-declaration-clause is erroneous.  */
11946   if (is_error)
11947     return NULL;
11948
11949   /* Peek at the next token.  */
11950   token = cp_lexer_peek_token (parser->lexer);
11951   /* If it's a `,', the clause should terminate with an ellipsis.  */
11952   if (token->type == CPP_COMMA)
11953     {
11954       /* Consume the `,'.  */
11955       cp_lexer_consume_token (parser->lexer);
11956       /* Expect an ellipsis.  */
11957       ellipsis_p
11958         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11959     }
11960   /* It might also be `...' if the optional trailing `,' was
11961      omitted.  */
11962   else if (token->type == CPP_ELLIPSIS)
11963     {
11964       /* Consume the `...' token.  */
11965       cp_lexer_consume_token (parser->lexer);
11966       /* And remember that we saw it.  */
11967       ellipsis_p = true;
11968     }
11969   else
11970     ellipsis_p = false;
11971
11972   /* Finish the parameter list.  */
11973   if (parameters && ellipsis_p)
11974     parameters->ellipsis_p = true;
11975
11976   return parameters;
11977 }
11978
11979 /* Parse a parameter-declaration-list.
11980
11981    parameter-declaration-list:
11982      parameter-declaration
11983      parameter-declaration-list , parameter-declaration
11984
11985    Returns a representation of the parameter-declaration-list, as for
11986    cp_parser_parameter_declaration_clause.  However, the
11987    `void_list_node' is never appended to the list.  Upon return,
11988    *IS_ERROR will be true iff an error occurred.  */
11989
11990 static cp_parameter_declarator *
11991 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11992 {
11993   cp_parameter_declarator *parameters = NULL;
11994   cp_parameter_declarator **tail = &parameters;
11995
11996   /* Assume all will go well.  */
11997   *is_error = false;
11998
11999   /* Look for more parameters.  */
12000   while (true)
12001     {
12002       cp_parameter_declarator *parameter;
12003       bool parenthesized_p;
12004       /* Parse the parameter.  */
12005       parameter
12006         = cp_parser_parameter_declaration (parser,
12007                                            /*template_parm_p=*/false,
12008                                            &parenthesized_p);
12009
12010       /* If a parse error occurred parsing the parameter declaration,
12011          then the entire parameter-declaration-list is erroneous.  */
12012       if (!parameter)
12013         {
12014           *is_error = true;
12015           parameters = NULL;
12016           break;
12017         }
12018       /* Add the new parameter to the list.  */
12019       *tail = parameter;
12020       tail = &parameter->next;
12021
12022       /* Peek at the next token.  */
12023       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12024           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12025           /* These are for Objective-C++ */
12026           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12027           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12028         /* The parameter-declaration-list is complete.  */
12029         break;
12030       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12031         {
12032           cp_token *token;
12033
12034           /* Peek at the next token.  */
12035           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12036           /* If it's an ellipsis, then the list is complete.  */
12037           if (token->type == CPP_ELLIPSIS)
12038             break;
12039           /* Otherwise, there must be more parameters.  Consume the
12040              `,'.  */
12041           cp_lexer_consume_token (parser->lexer);
12042           /* When parsing something like:
12043
12044                 int i(float f, double d)
12045
12046              we can tell after seeing the declaration for "f" that we
12047              are not looking at an initialization of a variable "i",
12048              but rather at the declaration of a function "i".
12049
12050              Due to the fact that the parsing of template arguments
12051              (as specified to a template-id) requires backtracking we
12052              cannot use this technique when inside a template argument
12053              list.  */
12054           if (!parser->in_template_argument_list_p
12055               && !parser->in_type_id_in_expr_p
12056               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12057               /* However, a parameter-declaration of the form
12058                  "foat(f)" (which is a valid declaration of a
12059                  parameter "f") can also be interpreted as an
12060                  expression (the conversion of "f" to "float").  */
12061               && !parenthesized_p)
12062             cp_parser_commit_to_tentative_parse (parser);
12063         }
12064       else
12065         {
12066           cp_parser_error (parser, "expected %<,%> or %<...%>");
12067           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12068             cp_parser_skip_to_closing_parenthesis (parser,
12069                                                    /*recovering=*/true,
12070                                                    /*or_comma=*/false,
12071                                                    /*consume_paren=*/false);
12072           break;
12073         }
12074     }
12075
12076   return parameters;
12077 }
12078
12079 /* Parse a parameter declaration.
12080
12081    parameter-declaration:
12082      decl-specifier-seq declarator
12083      decl-specifier-seq declarator = assignment-expression
12084      decl-specifier-seq abstract-declarator [opt]
12085      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12086
12087    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12088    declares a template parameter.  (In that case, a non-nested `>'
12089    token encountered during the parsing of the assignment-expression
12090    is not interpreted as a greater-than operator.)
12091
12092    Returns a representation of the parameter, or NULL if an error
12093    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12094    true iff the declarator is of the form "(p)".  */
12095
12096 static cp_parameter_declarator *
12097 cp_parser_parameter_declaration (cp_parser *parser,
12098                                  bool template_parm_p,
12099                                  bool *parenthesized_p)
12100 {
12101   int declares_class_or_enum;
12102   bool greater_than_is_operator_p;
12103   cp_decl_specifier_seq decl_specifiers;
12104   cp_declarator *declarator;
12105   tree default_argument;
12106   cp_token *token;
12107   const char *saved_message;
12108
12109   /* In a template parameter, `>' is not an operator.
12110
12111      [temp.param]
12112
12113      When parsing a default template-argument for a non-type
12114      template-parameter, the first non-nested `>' is taken as the end
12115      of the template parameter-list rather than a greater-than
12116      operator.  */
12117   greater_than_is_operator_p = !template_parm_p;
12118
12119   /* Type definitions may not appear in parameter types.  */
12120   saved_message = parser->type_definition_forbidden_message;
12121   parser->type_definition_forbidden_message
12122     = "types may not be defined in parameter types";
12123
12124   /* Parse the declaration-specifiers.  */
12125   cp_parser_decl_specifier_seq (parser,
12126                                 CP_PARSER_FLAGS_NONE,
12127                                 &decl_specifiers,
12128                                 &declares_class_or_enum);
12129   /* If an error occurred, there's no reason to attempt to parse the
12130      rest of the declaration.  */
12131   if (cp_parser_error_occurred (parser))
12132     {
12133       parser->type_definition_forbidden_message = saved_message;
12134       return NULL;
12135     }
12136
12137   /* Peek at the next token.  */
12138   token = cp_lexer_peek_token (parser->lexer);
12139   /* If the next token is a `)', `,', `=', `>', or `...', then there
12140      is no declarator.  */
12141   if (token->type == CPP_CLOSE_PAREN
12142       || token->type == CPP_COMMA
12143       || token->type == CPP_EQ
12144       || token->type == CPP_ELLIPSIS
12145       || token->type == CPP_GREATER)
12146     {
12147       declarator = NULL;
12148       if (parenthesized_p)
12149         *parenthesized_p = false;
12150     }
12151   /* Otherwise, there should be a declarator.  */
12152   else
12153     {
12154       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12155       parser->default_arg_ok_p = false;
12156
12157       /* After seeing a decl-specifier-seq, if the next token is not a
12158          "(", there is no possibility that the code is a valid
12159          expression.  Therefore, if parsing tentatively, we commit at
12160          this point.  */
12161       if (!parser->in_template_argument_list_p
12162           /* In an expression context, having seen:
12163
12164                (int((char ...
12165
12166              we cannot be sure whether we are looking at a
12167              function-type (taking a "char" as a parameter) or a cast
12168              of some object of type "char" to "int".  */
12169           && !parser->in_type_id_in_expr_p
12170           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12171           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12172         cp_parser_commit_to_tentative_parse (parser);
12173       /* Parse the declarator.  */
12174       declarator = cp_parser_declarator (parser,
12175                                          CP_PARSER_DECLARATOR_EITHER,
12176                                          /*ctor_dtor_or_conv_p=*/NULL,
12177                                          parenthesized_p,
12178                                          /*member_p=*/false);
12179       parser->default_arg_ok_p = saved_default_arg_ok_p;
12180       /* After the declarator, allow more attributes.  */
12181       decl_specifiers.attributes
12182         = chainon (decl_specifiers.attributes,
12183                    cp_parser_attributes_opt (parser));
12184     }
12185
12186   /* The restriction on defining new types applies only to the type
12187      of the parameter, not to the default argument.  */
12188   parser->type_definition_forbidden_message = saved_message;
12189
12190   /* If the next token is `=', then process a default argument.  */
12191   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12192     {
12193       bool saved_greater_than_is_operator_p;
12194       /* Consume the `='.  */
12195       cp_lexer_consume_token (parser->lexer);
12196
12197       /* If we are defining a class, then the tokens that make up the
12198          default argument must be saved and processed later.  */
12199       if (!template_parm_p && at_class_scope_p ()
12200           && TYPE_BEING_DEFINED (current_class_type))
12201         {
12202           unsigned depth = 0;
12203           cp_token *first_token;
12204           cp_token *token;
12205
12206           /* Add tokens until we have processed the entire default
12207              argument.  We add the range [first_token, token).  */
12208           first_token = cp_lexer_peek_token (parser->lexer);
12209           while (true)
12210             {
12211               bool done = false;
12212
12213               /* Peek at the next token.  */
12214               token = cp_lexer_peek_token (parser->lexer);
12215               /* What we do depends on what token we have.  */
12216               switch (token->type)
12217                 {
12218                   /* In valid code, a default argument must be
12219                      immediately followed by a `,' `)', or `...'.  */
12220                 case CPP_COMMA:
12221                 case CPP_CLOSE_PAREN:
12222                 case CPP_ELLIPSIS:
12223                   /* If we run into a non-nested `;', `}', or `]',
12224                      then the code is invalid -- but the default
12225                      argument is certainly over.  */
12226                 case CPP_SEMICOLON:
12227                 case CPP_CLOSE_BRACE:
12228                 case CPP_CLOSE_SQUARE:
12229                   if (depth == 0)
12230                     done = true;
12231                   /* Update DEPTH, if necessary.  */
12232                   else if (token->type == CPP_CLOSE_PAREN
12233                            || token->type == CPP_CLOSE_BRACE
12234                            || token->type == CPP_CLOSE_SQUARE)
12235                     --depth;
12236                   break;
12237
12238                 case CPP_OPEN_PAREN:
12239                 case CPP_OPEN_SQUARE:
12240                 case CPP_OPEN_BRACE:
12241                   ++depth;
12242                   break;
12243
12244                 case CPP_GREATER:
12245                   /* If we see a non-nested `>', and `>' is not an
12246                      operator, then it marks the end of the default
12247                      argument.  */
12248                   if (!depth && !greater_than_is_operator_p)
12249                     done = true;
12250                   break;
12251
12252                   /* If we run out of tokens, issue an error message.  */
12253                 case CPP_EOF:
12254                 case CPP_PRAGMA_EOL:
12255                   error ("file ends in default argument");
12256                   done = true;
12257                   break;
12258
12259                 case CPP_NAME:
12260                 case CPP_SCOPE:
12261                   /* In these cases, we should look for template-ids.
12262                      For example, if the default argument is
12263                      `X<int, double>()', we need to do name lookup to
12264                      figure out whether or not `X' is a template; if
12265                      so, the `,' does not end the default argument.
12266
12267                      That is not yet done.  */
12268                   break;
12269
12270                 default:
12271                   break;
12272                 }
12273
12274               /* If we've reached the end, stop.  */
12275               if (done)
12276                 break;
12277
12278               /* Add the token to the token block.  */
12279               token = cp_lexer_consume_token (parser->lexer);
12280             }
12281
12282           /* Create a DEFAULT_ARG to represented the unparsed default
12283              argument.  */
12284           default_argument = make_node (DEFAULT_ARG);
12285           DEFARG_TOKENS (default_argument)
12286             = cp_token_cache_new (first_token, token);
12287           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12288         }
12289       /* Outside of a class definition, we can just parse the
12290          assignment-expression.  */
12291       else
12292         {
12293           bool saved_local_variables_forbidden_p;
12294
12295           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12296              set correctly.  */
12297           saved_greater_than_is_operator_p
12298             = parser->greater_than_is_operator_p;
12299           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12300           /* Local variable names (and the `this' keyword) may not
12301              appear in a default argument.  */
12302           saved_local_variables_forbidden_p
12303             = parser->local_variables_forbidden_p;
12304           parser->local_variables_forbidden_p = true;
12305           /* Parse the assignment-expression.  */
12306           default_argument
12307             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12308           /* Restore saved state.  */
12309           parser->greater_than_is_operator_p
12310             = saved_greater_than_is_operator_p;
12311           parser->local_variables_forbidden_p
12312             = saved_local_variables_forbidden_p;
12313         }
12314       if (!parser->default_arg_ok_p)
12315         {
12316           if (!flag_pedantic_errors)
12317             warning (0, "deprecated use of default argument for parameter of non-function");
12318           else
12319             {
12320               error ("default arguments are only permitted for function parameters");
12321               default_argument = NULL_TREE;
12322             }
12323         }
12324     }
12325   else
12326     default_argument = NULL_TREE;
12327
12328   return make_parameter_declarator (&decl_specifiers,
12329                                     declarator,
12330                                     default_argument);
12331 }
12332
12333 /* Parse a function-body.
12334
12335    function-body:
12336      compound_statement  */
12337
12338 static void
12339 cp_parser_function_body (cp_parser *parser)
12340 {
12341   cp_parser_compound_statement (parser, NULL, false);
12342 }
12343
12344 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12345    true if a ctor-initializer was present.  */
12346
12347 static bool
12348 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12349 {
12350   tree body;
12351   bool ctor_initializer_p;
12352
12353   /* Begin the function body.  */
12354   body = begin_function_body ();
12355   /* Parse the optional ctor-initializer.  */
12356   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12357   /* Parse the function-body.  */
12358   cp_parser_function_body (parser);
12359   /* Finish the function body.  */
12360   finish_function_body (body);
12361
12362   return ctor_initializer_p;
12363 }
12364
12365 /* Parse an initializer.
12366
12367    initializer:
12368      = initializer-clause
12369      ( expression-list )
12370
12371    Returns an expression representing the initializer.  If no
12372    initializer is present, NULL_TREE is returned.
12373
12374    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12375    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12376    set to FALSE if there is no initializer present.  If there is an
12377    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12378    is set to true; otherwise it is set to false.  */
12379
12380 static tree
12381 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12382                        bool* non_constant_p)
12383 {
12384   cp_token *token;
12385   tree init;
12386
12387   /* Peek at the next token.  */
12388   token = cp_lexer_peek_token (parser->lexer);
12389
12390   /* Let our caller know whether or not this initializer was
12391      parenthesized.  */
12392   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12393   /* Assume that the initializer is constant.  */
12394   *non_constant_p = false;
12395
12396   if (token->type == CPP_EQ)
12397     {
12398       /* Consume the `='.  */
12399       cp_lexer_consume_token (parser->lexer);
12400       /* Parse the initializer-clause.  */
12401       init = cp_parser_initializer_clause (parser, non_constant_p);
12402     }
12403   else if (token->type == CPP_OPEN_PAREN)
12404     init = cp_parser_parenthesized_expression_list (parser, false,
12405                                                     /*cast_p=*/false,
12406                                                     non_constant_p);
12407   else
12408     {
12409       /* Anything else is an error.  */
12410       cp_parser_error (parser, "expected initializer");
12411       init = error_mark_node;
12412     }
12413
12414   return init;
12415 }
12416
12417 /* Parse an initializer-clause.
12418
12419    initializer-clause:
12420      assignment-expression
12421      { initializer-list , [opt] }
12422      { }
12423
12424    Returns an expression representing the initializer.
12425
12426    If the `assignment-expression' production is used the value
12427    returned is simply a representation for the expression.
12428
12429    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12430    the elements of the initializer-list (or NULL, if the last
12431    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12432    NULL_TREE.  There is no way to detect whether or not the optional
12433    trailing `,' was provided.  NON_CONSTANT_P is as for
12434    cp_parser_initializer.  */
12435
12436 static tree
12437 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12438 {
12439   tree initializer;
12440
12441   /* Assume the expression is constant.  */
12442   *non_constant_p = false;
12443
12444   /* If it is not a `{', then we are looking at an
12445      assignment-expression.  */
12446   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12447     {
12448       initializer
12449         = cp_parser_constant_expression (parser,
12450                                         /*allow_non_constant_p=*/true,
12451                                         non_constant_p);
12452       if (!*non_constant_p)
12453         initializer = fold_non_dependent_expr (initializer);
12454     }
12455   else
12456     {
12457       /* Consume the `{' token.  */
12458       cp_lexer_consume_token (parser->lexer);
12459       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12460       initializer = make_node (CONSTRUCTOR);
12461       /* If it's not a `}', then there is a non-trivial initializer.  */
12462       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12463         {
12464           /* Parse the initializer list.  */
12465           CONSTRUCTOR_ELTS (initializer)
12466             = cp_parser_initializer_list (parser, non_constant_p);
12467           /* A trailing `,' token is allowed.  */
12468           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12469             cp_lexer_consume_token (parser->lexer);
12470         }
12471       /* Now, there should be a trailing `}'.  */
12472       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12473     }
12474
12475   return initializer;
12476 }
12477
12478 /* Parse an initializer-list.
12479
12480    initializer-list:
12481      initializer-clause
12482      initializer-list , initializer-clause
12483
12484    GNU Extension:
12485
12486    initializer-list:
12487      identifier : initializer-clause
12488      initializer-list, identifier : initializer-clause
12489
12490    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12491    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12492    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12493    as for cp_parser_initializer.  */
12494
12495 static VEC(constructor_elt,gc) *
12496 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12497 {
12498   VEC(constructor_elt,gc) *v = NULL;
12499
12500   /* Assume all of the expressions are constant.  */
12501   *non_constant_p = false;
12502
12503   /* Parse the rest of the list.  */
12504   while (true)
12505     {
12506       cp_token *token;
12507       tree identifier;
12508       tree initializer;
12509       bool clause_non_constant_p;
12510
12511       /* If the next token is an identifier and the following one is a
12512          colon, we are looking at the GNU designated-initializer
12513          syntax.  */
12514       if (cp_parser_allow_gnu_extensions_p (parser)
12515           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12516           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12517         {
12518           /* Consume the identifier.  */
12519           identifier = cp_lexer_consume_token (parser->lexer)->value;
12520           /* Consume the `:'.  */
12521           cp_lexer_consume_token (parser->lexer);
12522         }
12523       else
12524         identifier = NULL_TREE;
12525
12526       /* Parse the initializer.  */
12527       initializer = cp_parser_initializer_clause (parser,
12528                                                   &clause_non_constant_p);
12529       /* If any clause is non-constant, so is the entire initializer.  */
12530       if (clause_non_constant_p)
12531         *non_constant_p = true;
12532
12533       /* Add it to the vector.  */
12534       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12535
12536       /* If the next token is not a comma, we have reached the end of
12537          the list.  */
12538       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12539         break;
12540
12541       /* Peek at the next token.  */
12542       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12543       /* If the next token is a `}', then we're still done.  An
12544          initializer-clause can have a trailing `,' after the
12545          initializer-list and before the closing `}'.  */
12546       if (token->type == CPP_CLOSE_BRACE)
12547         break;
12548
12549       /* Consume the `,' token.  */
12550       cp_lexer_consume_token (parser->lexer);
12551     }
12552
12553   return v;
12554 }
12555
12556 /* Classes [gram.class] */
12557
12558 /* Parse a class-name.
12559
12560    class-name:
12561      identifier
12562      template-id
12563
12564    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12565    to indicate that names looked up in dependent types should be
12566    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12567    keyword has been used to indicate that the name that appears next
12568    is a template.  TAG_TYPE indicates the explicit tag given before
12569    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12570    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12571    is the class being defined in a class-head.
12572
12573    Returns the TYPE_DECL representing the class.  */
12574
12575 static tree
12576 cp_parser_class_name (cp_parser *parser,
12577                       bool typename_keyword_p,
12578                       bool template_keyword_p,
12579                       enum tag_types tag_type,
12580                       bool check_dependency_p,
12581                       bool class_head_p,
12582                       bool is_declaration)
12583 {
12584   tree decl;
12585   tree scope;
12586   bool typename_p;
12587   cp_token *token;
12588
12589   /* All class-names start with an identifier.  */
12590   token = cp_lexer_peek_token (parser->lexer);
12591   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12592     {
12593       cp_parser_error (parser, "expected class-name");
12594       return error_mark_node;
12595     }
12596
12597   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12598      to a template-id, so we save it here.  */
12599   scope = parser->scope;
12600   if (scope == error_mark_node)
12601     return error_mark_node;
12602
12603   /* Any name names a type if we're following the `typename' keyword
12604      in a qualified name where the enclosing scope is type-dependent.  */
12605   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12606                 && dependent_type_p (scope));
12607   /* Handle the common case (an identifier, but not a template-id)
12608      efficiently.  */
12609   if (token->type == CPP_NAME
12610       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12611     {
12612       cp_token *identifier_token;
12613       tree identifier;
12614       bool ambiguous_p;
12615
12616       /* Look for the identifier.  */
12617       identifier_token = cp_lexer_peek_token (parser->lexer);
12618       ambiguous_p = identifier_token->ambiguous_p;
12619       identifier = cp_parser_identifier (parser);
12620       /* If the next token isn't an identifier, we are certainly not
12621          looking at a class-name.  */
12622       if (identifier == error_mark_node)
12623         decl = error_mark_node;
12624       /* If we know this is a type-name, there's no need to look it
12625          up.  */
12626       else if (typename_p)
12627         decl = identifier;
12628       else
12629         {
12630           tree ambiguous_decls;
12631           /* If we already know that this lookup is ambiguous, then
12632              we've already issued an error message; there's no reason
12633              to check again.  */
12634           if (ambiguous_p)
12635             {
12636               cp_parser_simulate_error (parser);
12637               return error_mark_node;
12638             }
12639           /* If the next token is a `::', then the name must be a type
12640              name.
12641
12642              [basic.lookup.qual]
12643
12644              During the lookup for a name preceding the :: scope
12645              resolution operator, object, function, and enumerator
12646              names are ignored.  */
12647           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12648             tag_type = typename_type;
12649           /* Look up the name.  */
12650           decl = cp_parser_lookup_name (parser, identifier,
12651                                         tag_type,
12652                                         /*is_template=*/false,
12653                                         /*is_namespace=*/false,
12654                                         check_dependency_p,
12655                                         &ambiguous_decls);
12656           if (ambiguous_decls)
12657             {
12658               error ("reference to %qD is ambiguous", identifier);
12659               print_candidates (ambiguous_decls);
12660               if (cp_parser_parsing_tentatively (parser))
12661                 {
12662                   identifier_token->ambiguous_p = true;
12663                   cp_parser_simulate_error (parser);
12664                 }
12665               return error_mark_node;
12666             }
12667         }
12668     }
12669   else
12670     {
12671       /* Try a template-id.  */
12672       decl = cp_parser_template_id (parser, template_keyword_p,
12673                                     check_dependency_p,
12674                                     is_declaration);
12675       if (decl == error_mark_node)
12676         return error_mark_node;
12677     }
12678
12679   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12680
12681   /* If this is a typename, create a TYPENAME_TYPE.  */
12682   if (typename_p && decl != error_mark_node)
12683     {
12684       decl = make_typename_type (scope, decl, typename_type,
12685                                  /*complain=*/tf_error);
12686       if (decl != error_mark_node)
12687         decl = TYPE_NAME (decl);
12688     }
12689
12690   /* Check to see that it is really the name of a class.  */
12691   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12692       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12693       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12694     /* Situations like this:
12695
12696          template <typename T> struct A {
12697            typename T::template X<int>::I i;
12698          };
12699
12700        are problematic.  Is `T::template X<int>' a class-name?  The
12701        standard does not seem to be definitive, but there is no other
12702        valid interpretation of the following `::'.  Therefore, those
12703        names are considered class-names.  */
12704     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12705   else if (decl == error_mark_node
12706            || TREE_CODE (decl) != TYPE_DECL
12707            || TREE_TYPE (decl) == error_mark_node
12708            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12709     {
12710       cp_parser_error (parser, "expected class-name");
12711       return error_mark_node;
12712     }
12713
12714   return decl;
12715 }
12716
12717 /* Parse a class-specifier.
12718
12719    class-specifier:
12720      class-head { member-specification [opt] }
12721
12722    Returns the TREE_TYPE representing the class.  */
12723
12724 static tree
12725 cp_parser_class_specifier (cp_parser* parser)
12726 {
12727   cp_token *token;
12728   tree type;
12729   tree attributes = NULL_TREE;
12730   int has_trailing_semicolon;
12731   bool nested_name_specifier_p;
12732   unsigned saved_num_template_parameter_lists;
12733   tree old_scope = NULL_TREE;
12734   tree scope = NULL_TREE;
12735
12736   push_deferring_access_checks (dk_no_deferred);
12737
12738   /* Parse the class-head.  */
12739   type = cp_parser_class_head (parser,
12740                                &nested_name_specifier_p,
12741                                &attributes);
12742   /* If the class-head was a semantic disaster, skip the entire body
12743      of the class.  */
12744   if (!type)
12745     {
12746       cp_parser_skip_to_end_of_block_or_statement (parser);
12747       pop_deferring_access_checks ();
12748       return error_mark_node;
12749     }
12750
12751   /* Look for the `{'.  */
12752   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12753     {
12754       pop_deferring_access_checks ();
12755       return error_mark_node;
12756     }
12757
12758   /* Issue an error message if type-definitions are forbidden here.  */
12759   cp_parser_check_type_definition (parser);
12760   /* Remember that we are defining one more class.  */
12761   ++parser->num_classes_being_defined;
12762   /* Inside the class, surrounding template-parameter-lists do not
12763      apply.  */
12764   saved_num_template_parameter_lists
12765     = parser->num_template_parameter_lists;
12766   parser->num_template_parameter_lists = 0;
12767
12768   /* Start the class.  */
12769   if (nested_name_specifier_p)
12770     {
12771       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12772       old_scope = push_inner_scope (scope);
12773     }
12774   type = begin_class_definition (type);
12775
12776   if (type == error_mark_node)
12777     /* If the type is erroneous, skip the entire body of the class.  */
12778     cp_parser_skip_to_closing_brace (parser);
12779   else
12780     /* Parse the member-specification.  */
12781     cp_parser_member_specification_opt (parser);
12782
12783   /* Look for the trailing `}'.  */
12784   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12785   /* We get better error messages by noticing a common problem: a
12786      missing trailing `;'.  */
12787   token = cp_lexer_peek_token (parser->lexer);
12788   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12789   /* Look for trailing attributes to apply to this class.  */
12790   if (cp_parser_allow_gnu_extensions_p (parser))
12791     {
12792       tree sub_attr = cp_parser_attributes_opt (parser);
12793       attributes = chainon (attributes, sub_attr);
12794     }
12795   if (type != error_mark_node)
12796     type = finish_struct (type, attributes);
12797   if (nested_name_specifier_p)
12798     pop_inner_scope (old_scope, scope);
12799   /* If this class is not itself within the scope of another class,
12800      then we need to parse the bodies of all of the queued function
12801      definitions.  Note that the queued functions defined in a class
12802      are not always processed immediately following the
12803      class-specifier for that class.  Consider:
12804
12805        struct A {
12806          struct B { void f() { sizeof (A); } };
12807        };
12808
12809      If `f' were processed before the processing of `A' were
12810      completed, there would be no way to compute the size of `A'.
12811      Note that the nesting we are interested in here is lexical --
12812      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12813      for:
12814
12815        struct A { struct B; };
12816        struct A::B { void f() { } };
12817
12818      there is no need to delay the parsing of `A::B::f'.  */
12819   if (--parser->num_classes_being_defined == 0)
12820     {
12821       tree queue_entry;
12822       tree fn;
12823       tree class_type = NULL_TREE;
12824       tree pushed_scope = NULL_TREE;
12825  
12826       /* In a first pass, parse default arguments to the functions.
12827          Then, in a second pass, parse the bodies of the functions.
12828          This two-phased approach handles cases like:
12829
12830             struct S {
12831               void f() { g(); }
12832               void g(int i = 3);
12833             };
12834
12835          */
12836       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12837              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12838            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12839            TREE_PURPOSE (parser->unparsed_functions_queues)
12840              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12841         {
12842           fn = TREE_VALUE (queue_entry);
12843           /* If there are default arguments that have not yet been processed,
12844              take care of them now.  */
12845           if (class_type != TREE_PURPOSE (queue_entry))
12846             {
12847               if (pushed_scope)
12848                 pop_scope (pushed_scope);
12849               class_type = TREE_PURPOSE (queue_entry);
12850               pushed_scope = push_scope (class_type);
12851             }
12852           /* Make sure that any template parameters are in scope.  */
12853           maybe_begin_member_template_processing (fn);
12854           /* Parse the default argument expressions.  */
12855           cp_parser_late_parsing_default_args (parser, fn);
12856           /* Remove any template parameters from the symbol table.  */
12857           maybe_end_member_template_processing ();
12858         }
12859       if (pushed_scope)
12860         pop_scope (pushed_scope);
12861       /* Now parse the body of the functions.  */
12862       for (TREE_VALUE (parser->unparsed_functions_queues)
12863              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12864            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12865            TREE_VALUE (parser->unparsed_functions_queues)
12866              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12867         {
12868           /* Figure out which function we need to process.  */
12869           fn = TREE_VALUE (queue_entry);
12870           /* Parse the function.  */
12871           cp_parser_late_parsing_for_member (parser, fn);
12872         }
12873     }
12874
12875   /* Put back any saved access checks.  */
12876   pop_deferring_access_checks ();
12877
12878   /* Restore the count of active template-parameter-lists.  */
12879   parser->num_template_parameter_lists
12880     = saved_num_template_parameter_lists;
12881
12882   return type;
12883 }
12884
12885 /* Parse a class-head.
12886
12887    class-head:
12888      class-key identifier [opt] base-clause [opt]
12889      class-key nested-name-specifier identifier base-clause [opt]
12890      class-key nested-name-specifier [opt] template-id
12891        base-clause [opt]
12892
12893    GNU Extensions:
12894      class-key attributes identifier [opt] base-clause [opt]
12895      class-key attributes nested-name-specifier identifier base-clause [opt]
12896      class-key attributes nested-name-specifier [opt] template-id
12897        base-clause [opt]
12898
12899    Returns the TYPE of the indicated class.  Sets
12900    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12901    involving a nested-name-specifier was used, and FALSE otherwise.
12902
12903    Returns error_mark_node if this is not a class-head.
12904
12905    Returns NULL_TREE if the class-head is syntactically valid, but
12906    semantically invalid in a way that means we should skip the entire
12907    body of the class.  */
12908
12909 static tree
12910 cp_parser_class_head (cp_parser* parser,
12911                       bool* nested_name_specifier_p,
12912                       tree *attributes_p)
12913 {
12914   tree nested_name_specifier;
12915   enum tag_types class_key;
12916   tree id = NULL_TREE;
12917   tree type = NULL_TREE;
12918   tree attributes;
12919   bool template_id_p = false;
12920   bool qualified_p = false;
12921   bool invalid_nested_name_p = false;
12922   bool invalid_explicit_specialization_p = false;
12923   tree pushed_scope = NULL_TREE;
12924   unsigned num_templates;
12925   tree bases;
12926
12927   /* Assume no nested-name-specifier will be present.  */
12928   *nested_name_specifier_p = false;
12929   /* Assume no template parameter lists will be used in defining the
12930      type.  */
12931   num_templates = 0;
12932
12933   /* Look for the class-key.  */
12934   class_key = cp_parser_class_key (parser);
12935   if (class_key == none_type)
12936     return error_mark_node;
12937
12938   /* Parse the attributes.  */
12939   attributes = cp_parser_attributes_opt (parser);
12940
12941   /* If the next token is `::', that is invalid -- but sometimes
12942      people do try to write:
12943
12944        struct ::S {};
12945
12946      Handle this gracefully by accepting the extra qualifier, and then
12947      issuing an error about it later if this really is a
12948      class-head.  If it turns out just to be an elaborated type
12949      specifier, remain silent.  */
12950   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12951     qualified_p = true;
12952
12953   push_deferring_access_checks (dk_no_check);
12954
12955   /* Determine the name of the class.  Begin by looking for an
12956      optional nested-name-specifier.  */
12957   nested_name_specifier
12958     = cp_parser_nested_name_specifier_opt (parser,
12959                                            /*typename_keyword_p=*/false,
12960                                            /*check_dependency_p=*/false,
12961                                            /*type_p=*/false,
12962                                            /*is_declaration=*/false);
12963   /* If there was a nested-name-specifier, then there *must* be an
12964      identifier.  */
12965   if (nested_name_specifier)
12966     {
12967       /* Although the grammar says `identifier', it really means
12968          `class-name' or `template-name'.  You are only allowed to
12969          define a class that has already been declared with this
12970          syntax.
12971
12972          The proposed resolution for Core Issue 180 says that whever
12973          you see `class T::X' you should treat `X' as a type-name.
12974
12975          It is OK to define an inaccessible class; for example:
12976
12977            class A { class B; };
12978            class A::B {};
12979
12980          We do not know if we will see a class-name, or a
12981          template-name.  We look for a class-name first, in case the
12982          class-name is a template-id; if we looked for the
12983          template-name first we would stop after the template-name.  */
12984       cp_parser_parse_tentatively (parser);
12985       type = cp_parser_class_name (parser,
12986                                    /*typename_keyword_p=*/false,
12987                                    /*template_keyword_p=*/false,
12988                                    class_type,
12989                                    /*check_dependency_p=*/false,
12990                                    /*class_head_p=*/true,
12991                                    /*is_declaration=*/false);
12992       /* If that didn't work, ignore the nested-name-specifier.  */
12993       if (!cp_parser_parse_definitely (parser))
12994         {
12995           invalid_nested_name_p = true;
12996           id = cp_parser_identifier (parser);
12997           if (id == error_mark_node)
12998             id = NULL_TREE;
12999         }
13000       /* If we could not find a corresponding TYPE, treat this
13001          declaration like an unqualified declaration.  */
13002       if (type == error_mark_node)
13003         nested_name_specifier = NULL_TREE;
13004       /* Otherwise, count the number of templates used in TYPE and its
13005          containing scopes.  */
13006       else
13007         {
13008           tree scope;
13009
13010           for (scope = TREE_TYPE (type);
13011                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13012                scope = (TYPE_P (scope)
13013                         ? TYPE_CONTEXT (scope)
13014                         : DECL_CONTEXT (scope)))
13015             if (TYPE_P (scope)
13016                 && CLASS_TYPE_P (scope)
13017                 && CLASSTYPE_TEMPLATE_INFO (scope)
13018                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13019                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13020               ++num_templates;
13021         }
13022     }
13023   /* Otherwise, the identifier is optional.  */
13024   else
13025     {
13026       /* We don't know whether what comes next is a template-id,
13027          an identifier, or nothing at all.  */
13028       cp_parser_parse_tentatively (parser);
13029       /* Check for a template-id.  */
13030       id = cp_parser_template_id (parser,
13031                                   /*template_keyword_p=*/false,
13032                                   /*check_dependency_p=*/true,
13033                                   /*is_declaration=*/true);
13034       /* If that didn't work, it could still be an identifier.  */
13035       if (!cp_parser_parse_definitely (parser))
13036         {
13037           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13038             id = cp_parser_identifier (parser);
13039           else
13040             id = NULL_TREE;
13041         }
13042       else
13043         {
13044           template_id_p = true;
13045           ++num_templates;
13046         }
13047     }
13048
13049   pop_deferring_access_checks ();
13050
13051   if (id)
13052     cp_parser_check_for_invalid_template_id (parser, id);
13053
13054   /* If it's not a `:' or a `{' then we can't really be looking at a
13055      class-head, since a class-head only appears as part of a
13056      class-specifier.  We have to detect this situation before calling
13057      xref_tag, since that has irreversible side-effects.  */
13058   if (!cp_parser_next_token_starts_class_definition_p (parser))
13059     {
13060       cp_parser_error (parser, "expected %<{%> or %<:%>");
13061       return error_mark_node;
13062     }
13063
13064   /* At this point, we're going ahead with the class-specifier, even
13065      if some other problem occurs.  */
13066   cp_parser_commit_to_tentative_parse (parser);
13067   /* Issue the error about the overly-qualified name now.  */
13068   if (qualified_p)
13069     cp_parser_error (parser,
13070                      "global qualification of class name is invalid");
13071   else if (invalid_nested_name_p)
13072     cp_parser_error (parser,
13073                      "qualified name does not name a class");
13074   else if (nested_name_specifier)
13075     {
13076       tree scope;
13077
13078       /* Reject typedef-names in class heads.  */
13079       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13080         {
13081           error ("invalid class name in declaration of %qD", type);
13082           type = NULL_TREE;
13083           goto done;
13084         }
13085
13086       /* Figure out in what scope the declaration is being placed.  */
13087       scope = current_scope ();
13088       /* If that scope does not contain the scope in which the
13089          class was originally declared, the program is invalid.  */
13090       if (scope && !is_ancestor (scope, nested_name_specifier))
13091         {
13092           error ("declaration of %qD in %qD which does not enclose %qD",
13093                  type, scope, nested_name_specifier);
13094           type = NULL_TREE;
13095           goto done;
13096         }
13097       /* [dcl.meaning]
13098
13099          A declarator-id shall not be qualified exception of the
13100          definition of a ... nested class outside of its class
13101          ... [or] a the definition or explicit instantiation of a
13102          class member of a namespace outside of its namespace.  */
13103       if (scope == nested_name_specifier)
13104         {
13105           pedwarn ("extra qualification ignored");
13106           nested_name_specifier = NULL_TREE;
13107           num_templates = 0;
13108         }
13109     }
13110   /* An explicit-specialization must be preceded by "template <>".  If
13111      it is not, try to recover gracefully.  */
13112   if (at_namespace_scope_p ()
13113       && parser->num_template_parameter_lists == 0
13114       && template_id_p)
13115     {
13116       error ("an explicit specialization must be preceded by %<template <>%>");
13117       invalid_explicit_specialization_p = true;
13118       /* Take the same action that would have been taken by
13119          cp_parser_explicit_specialization.  */
13120       ++parser->num_template_parameter_lists;
13121       begin_specialization ();
13122     }
13123   /* There must be no "return" statements between this point and the
13124      end of this function; set "type "to the correct return value and
13125      use "goto done;" to return.  */
13126   /* Make sure that the right number of template parameters were
13127      present.  */
13128   if (!cp_parser_check_template_parameters (parser, num_templates))
13129     {
13130       /* If something went wrong, there is no point in even trying to
13131          process the class-definition.  */
13132       type = NULL_TREE;
13133       goto done;
13134     }
13135
13136   /* Look up the type.  */
13137   if (template_id_p)
13138     {
13139       type = TREE_TYPE (id);
13140       maybe_process_partial_specialization (type);
13141       if (nested_name_specifier)
13142         pushed_scope = push_scope (nested_name_specifier);
13143     }
13144   else if (nested_name_specifier)
13145     {
13146       tree class_type;
13147
13148       /* Given:
13149
13150             template <typename T> struct S { struct T };
13151             template <typename T> struct S<T>::T { };
13152
13153          we will get a TYPENAME_TYPE when processing the definition of
13154          `S::T'.  We need to resolve it to the actual type before we
13155          try to define it.  */
13156       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13157         {
13158           class_type = resolve_typename_type (TREE_TYPE (type),
13159                                               /*only_current_p=*/false);
13160           if (class_type != error_mark_node)
13161             type = TYPE_NAME (class_type);
13162           else
13163             {
13164               cp_parser_error (parser, "could not resolve typename type");
13165               type = error_mark_node;
13166             }
13167         }
13168
13169       maybe_process_partial_specialization (TREE_TYPE (type));
13170       class_type = current_class_type;
13171       /* Enter the scope indicated by the nested-name-specifier.  */
13172       pushed_scope = push_scope (nested_name_specifier);
13173       /* Get the canonical version of this type.  */
13174       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13175       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13176           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13177         {
13178           type = push_template_decl (type);
13179           if (type == error_mark_node)
13180             {
13181               type = NULL_TREE;
13182               goto done;
13183             }
13184         }
13185
13186       type = TREE_TYPE (type);
13187       *nested_name_specifier_p = true;
13188     }
13189   else      /* The name is not a nested name.  */
13190     {
13191       /* If the class was unnamed, create a dummy name.  */
13192       if (!id)
13193         id = make_anon_name ();
13194       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13195                        parser->num_template_parameter_lists);
13196     }
13197
13198   /* Indicate whether this class was declared as a `class' or as a
13199      `struct'.  */
13200   if (TREE_CODE (type) == RECORD_TYPE)
13201     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13202   cp_parser_check_class_key (class_key, type);
13203
13204   /* If this type was already complete, and we see another definition,
13205      that's an error.  */
13206   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13207     {
13208       error ("redefinition of %q#T", type);
13209       error ("previous definition of %q+#T", type);
13210       type = NULL_TREE;
13211       goto done;
13212     }
13213
13214   /* We will have entered the scope containing the class; the names of
13215      base classes should be looked up in that context.  For example:
13216
13217        struct A { struct B {}; struct C; };
13218        struct A::C : B {};
13219
13220      is valid.  */
13221   bases = NULL_TREE;
13222
13223   /* Get the list of base-classes, if there is one.  */
13224   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13225     bases = cp_parser_base_clause (parser);
13226
13227   /* Process the base classes.  */
13228   xref_basetypes (type, bases);
13229
13230  done:
13231   /* Leave the scope given by the nested-name-specifier.  We will
13232      enter the class scope itself while processing the members.  */
13233   if (pushed_scope)
13234     pop_scope (pushed_scope);
13235
13236   if (invalid_explicit_specialization_p)
13237     {
13238       end_specialization ();
13239       --parser->num_template_parameter_lists;
13240     }
13241   *attributes_p = attributes;
13242   return type;
13243 }
13244
13245 /* Parse a class-key.
13246
13247    class-key:
13248      class
13249      struct
13250      union
13251
13252    Returns the kind of class-key specified, or none_type to indicate
13253    error.  */
13254
13255 static enum tag_types
13256 cp_parser_class_key (cp_parser* parser)
13257 {
13258   cp_token *token;
13259   enum tag_types tag_type;
13260
13261   /* Look for the class-key.  */
13262   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13263   if (!token)
13264     return none_type;
13265
13266   /* Check to see if the TOKEN is a class-key.  */
13267   tag_type = cp_parser_token_is_class_key (token);
13268   if (!tag_type)
13269     cp_parser_error (parser, "expected class-key");
13270   return tag_type;
13271 }
13272
13273 /* Parse an (optional) member-specification.
13274
13275    member-specification:
13276      member-declaration member-specification [opt]
13277      access-specifier : member-specification [opt]  */
13278
13279 static void
13280 cp_parser_member_specification_opt (cp_parser* parser)
13281 {
13282   while (true)
13283     {
13284       cp_token *token;
13285       enum rid keyword;
13286
13287       /* Peek at the next token.  */
13288       token = cp_lexer_peek_token (parser->lexer);
13289       /* If it's a `}', or EOF then we've seen all the members.  */
13290       if (token->type == CPP_CLOSE_BRACE
13291           || token->type == CPP_EOF
13292           || token->type == CPP_PRAGMA_EOL)
13293         break;
13294
13295       /* See if this token is a keyword.  */
13296       keyword = token->keyword;
13297       switch (keyword)
13298         {
13299         case RID_PUBLIC:
13300         case RID_PROTECTED:
13301         case RID_PRIVATE:
13302           /* Consume the access-specifier.  */
13303           cp_lexer_consume_token (parser->lexer);
13304           /* Remember which access-specifier is active.  */
13305           current_access_specifier = token->value;
13306           /* Look for the `:'.  */
13307           cp_parser_require (parser, CPP_COLON, "`:'");
13308           break;
13309
13310         default:
13311           /* Accept #pragmas at class scope.  */
13312           if (token->type == CPP_PRAGMA)
13313             {
13314               cp_parser_pragma (parser, pragma_external);
13315               break;
13316             }
13317
13318           /* Otherwise, the next construction must be a
13319              member-declaration.  */
13320           cp_parser_member_declaration (parser);
13321         }
13322     }
13323 }
13324
13325 /* Parse a member-declaration.
13326
13327    member-declaration:
13328      decl-specifier-seq [opt] member-declarator-list [opt] ;
13329      function-definition ; [opt]
13330      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13331      using-declaration
13332      template-declaration
13333
13334    member-declarator-list:
13335      member-declarator
13336      member-declarator-list , member-declarator
13337
13338    member-declarator:
13339      declarator pure-specifier [opt]
13340      declarator constant-initializer [opt]
13341      identifier [opt] : constant-expression
13342
13343    GNU Extensions:
13344
13345    member-declaration:
13346      __extension__ member-declaration
13347
13348    member-declarator:
13349      declarator attributes [opt] pure-specifier [opt]
13350      declarator attributes [opt] constant-initializer [opt]
13351      identifier [opt] attributes [opt] : constant-expression  */
13352
13353 static void
13354 cp_parser_member_declaration (cp_parser* parser)
13355 {
13356   cp_decl_specifier_seq decl_specifiers;
13357   tree prefix_attributes;
13358   tree decl;
13359   int declares_class_or_enum;
13360   bool friend_p;
13361   cp_token *token;
13362   int saved_pedantic;
13363
13364   /* Check for the `__extension__' keyword.  */
13365   if (cp_parser_extension_opt (parser, &saved_pedantic))
13366     {
13367       /* Recurse.  */
13368       cp_parser_member_declaration (parser);
13369       /* Restore the old value of the PEDANTIC flag.  */
13370       pedantic = saved_pedantic;
13371
13372       return;
13373     }
13374
13375   /* Check for a template-declaration.  */
13376   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13377     {
13378       /* An explicit specialization here is an error condition, and we
13379          expect the specialization handler to detect and report this.  */
13380       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13381           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13382         cp_parser_explicit_specialization (parser);
13383       else
13384         cp_parser_template_declaration (parser, /*member_p=*/true);
13385
13386       return;
13387     }
13388
13389   /* Check for a using-declaration.  */
13390   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13391     {
13392       /* Parse the using-declaration.  */
13393       cp_parser_using_declaration (parser);
13394
13395       return;
13396     }
13397
13398   /* Check for @defs.  */
13399   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13400     {
13401       tree ivar, member;
13402       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13403       ivar = ivar_chains;
13404       while (ivar)
13405         {
13406           member = ivar;
13407           ivar = TREE_CHAIN (member);
13408           TREE_CHAIN (member) = NULL_TREE;
13409           finish_member_declaration (member);
13410         }
13411       return;
13412     }
13413
13414   /* Parse the decl-specifier-seq.  */
13415   cp_parser_decl_specifier_seq (parser,
13416                                 CP_PARSER_FLAGS_OPTIONAL,
13417                                 &decl_specifiers,
13418                                 &declares_class_or_enum);
13419   prefix_attributes = decl_specifiers.attributes;
13420   decl_specifiers.attributes = NULL_TREE;
13421   /* Check for an invalid type-name.  */
13422   if (!decl_specifiers.type
13423       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13424     return;
13425   /* If there is no declarator, then the decl-specifier-seq should
13426      specify a type.  */
13427   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13428     {
13429       /* If there was no decl-specifier-seq, and the next token is a
13430          `;', then we have something like:
13431
13432            struct S { ; };
13433
13434          [class.mem]
13435
13436          Each member-declaration shall declare at least one member
13437          name of the class.  */
13438       if (!decl_specifiers.any_specifiers_p)
13439         {
13440           cp_token *token = cp_lexer_peek_token (parser->lexer);
13441           if (pedantic && !token->in_system_header)
13442             pedwarn ("%Hextra %<;%>", &token->location);
13443         }
13444       else
13445         {
13446           tree type;
13447
13448           /* See if this declaration is a friend.  */
13449           friend_p = cp_parser_friend_p (&decl_specifiers);
13450           /* If there were decl-specifiers, check to see if there was
13451              a class-declaration.  */
13452           type = check_tag_decl (&decl_specifiers);
13453           /* Nested classes have already been added to the class, but
13454              a `friend' needs to be explicitly registered.  */
13455           if (friend_p)
13456             {
13457               /* If the `friend' keyword was present, the friend must
13458                  be introduced with a class-key.  */
13459                if (!declares_class_or_enum)
13460                  error ("a class-key must be used when declaring a friend");
13461                /* In this case:
13462
13463                     template <typename T> struct A {
13464                       friend struct A<T>::B;
13465                     };
13466
13467                   A<T>::B will be represented by a TYPENAME_TYPE, and
13468                   therefore not recognized by check_tag_decl.  */
13469                if (!type
13470                    && decl_specifiers.type
13471                    && TYPE_P (decl_specifiers.type))
13472                  type = decl_specifiers.type;
13473                if (!type || !TYPE_P (type))
13474                  error ("friend declaration does not name a class or "
13475                         "function");
13476                else
13477                  make_friend_class (current_class_type, type,
13478                                     /*complain=*/true);
13479             }
13480           /* If there is no TYPE, an error message will already have
13481              been issued.  */
13482           else if (!type || type == error_mark_node)
13483             ;
13484           /* An anonymous aggregate has to be handled specially; such
13485              a declaration really declares a data member (with a
13486              particular type), as opposed to a nested class.  */
13487           else if (ANON_AGGR_TYPE_P (type))
13488             {
13489               /* Remove constructors and such from TYPE, now that we
13490                  know it is an anonymous aggregate.  */
13491               fixup_anonymous_aggr (type);
13492               /* And make the corresponding data member.  */
13493               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13494               /* Add it to the class.  */
13495               finish_member_declaration (decl);
13496             }
13497           else
13498             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13499         }
13500     }
13501   else
13502     {
13503       /* See if these declarations will be friends.  */
13504       friend_p = cp_parser_friend_p (&decl_specifiers);
13505
13506       /* Keep going until we hit the `;' at the end of the
13507          declaration.  */
13508       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13509         {
13510           tree attributes = NULL_TREE;
13511           tree first_attribute;
13512
13513           /* Peek at the next token.  */
13514           token = cp_lexer_peek_token (parser->lexer);
13515
13516           /* Check for a bitfield declaration.  */
13517           if (token->type == CPP_COLON
13518               || (token->type == CPP_NAME
13519                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13520                   == CPP_COLON))
13521             {
13522               tree identifier;
13523               tree width;
13524
13525               /* Get the name of the bitfield.  Note that we cannot just
13526                  check TOKEN here because it may have been invalidated by
13527                  the call to cp_lexer_peek_nth_token above.  */
13528               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13529                 identifier = cp_parser_identifier (parser);
13530               else
13531                 identifier = NULL_TREE;
13532
13533               /* Consume the `:' token.  */
13534               cp_lexer_consume_token (parser->lexer);
13535               /* Get the width of the bitfield.  */
13536               width
13537                 = cp_parser_constant_expression (parser,
13538                                                  /*allow_non_constant=*/false,
13539                                                  NULL);
13540
13541               /* Look for attributes that apply to the bitfield.  */
13542               attributes = cp_parser_attributes_opt (parser);
13543               /* Remember which attributes are prefix attributes and
13544                  which are not.  */
13545               first_attribute = attributes;
13546               /* Combine the attributes.  */
13547               attributes = chainon (prefix_attributes, attributes);
13548
13549               /* Create the bitfield declaration.  */
13550               decl = grokbitfield (identifier
13551                                    ? make_id_declarator (NULL_TREE,
13552                                                          identifier,
13553                                                          sfk_none)
13554                                    : NULL,
13555                                    &decl_specifiers,
13556                                    width);
13557               /* Apply the attributes.  */
13558               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13559             }
13560           else
13561             {
13562               cp_declarator *declarator;
13563               tree initializer;
13564               tree asm_specification;
13565               int ctor_dtor_or_conv_p;
13566
13567               /* Parse the declarator.  */
13568               declarator
13569                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13570                                         &ctor_dtor_or_conv_p,
13571                                         /*parenthesized_p=*/NULL,
13572                                         /*member_p=*/true);
13573
13574               /* If something went wrong parsing the declarator, make sure
13575                  that we at least consume some tokens.  */
13576               if (declarator == cp_error_declarator)
13577                 {
13578                   /* Skip to the end of the statement.  */
13579                   cp_parser_skip_to_end_of_statement (parser);
13580                   /* If the next token is not a semicolon, that is
13581                      probably because we just skipped over the body of
13582                      a function.  So, we consume a semicolon if
13583                      present, but do not issue an error message if it
13584                      is not present.  */
13585                   if (cp_lexer_next_token_is (parser->lexer,
13586                                               CPP_SEMICOLON))
13587                     cp_lexer_consume_token (parser->lexer);
13588                   return;
13589                 }
13590
13591               if (declares_class_or_enum & 2)
13592                 cp_parser_check_for_definition_in_return_type
13593                   (declarator, decl_specifiers.type);
13594
13595               /* Look for an asm-specification.  */
13596               asm_specification = cp_parser_asm_specification_opt (parser);
13597               /* Look for attributes that apply to the declaration.  */
13598               attributes = cp_parser_attributes_opt (parser);
13599               /* Remember which attributes are prefix attributes and
13600                  which are not.  */
13601               first_attribute = attributes;
13602               /* Combine the attributes.  */
13603               attributes = chainon (prefix_attributes, attributes);
13604
13605               /* If it's an `=', then we have a constant-initializer or a
13606                  pure-specifier.  It is not correct to parse the
13607                  initializer before registering the member declaration
13608                  since the member declaration should be in scope while
13609                  its initializer is processed.  However, the rest of the
13610                  front end does not yet provide an interface that allows
13611                  us to handle this correctly.  */
13612               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13613                 {
13614                   /* In [class.mem]:
13615
13616                      A pure-specifier shall be used only in the declaration of
13617                      a virtual function.
13618
13619                      A member-declarator can contain a constant-initializer
13620                      only if it declares a static member of integral or
13621                      enumeration type.
13622
13623                      Therefore, if the DECLARATOR is for a function, we look
13624                      for a pure-specifier; otherwise, we look for a
13625                      constant-initializer.  When we call `grokfield', it will
13626                      perform more stringent semantics checks.  */
13627                   if (declarator->kind == cdk_function)
13628                     initializer = cp_parser_pure_specifier (parser);
13629                   else
13630                     /* Parse the initializer.  */
13631                     initializer = cp_parser_constant_initializer (parser);
13632                 }
13633               /* Otherwise, there is no initializer.  */
13634               else
13635                 initializer = NULL_TREE;
13636
13637               /* See if we are probably looking at a function
13638                  definition.  We are certainly not looking at a
13639                  member-declarator.  Calling `grokfield' has
13640                  side-effects, so we must not do it unless we are sure
13641                  that we are looking at a member-declarator.  */
13642               if (cp_parser_token_starts_function_definition_p
13643                   (cp_lexer_peek_token (parser->lexer)))
13644                 {
13645                   /* The grammar does not allow a pure-specifier to be
13646                      used when a member function is defined.  (It is
13647                      possible that this fact is an oversight in the
13648                      standard, since a pure function may be defined
13649                      outside of the class-specifier.  */
13650                   if (initializer)
13651                     error ("pure-specifier on function-definition");
13652                   decl = cp_parser_save_member_function_body (parser,
13653                                                               &decl_specifiers,
13654                                                               declarator,
13655                                                               attributes);
13656                   /* If the member was not a friend, declare it here.  */
13657                   if (!friend_p)
13658                     finish_member_declaration (decl);
13659                   /* Peek at the next token.  */
13660                   token = cp_lexer_peek_token (parser->lexer);
13661                   /* If the next token is a semicolon, consume it.  */
13662                   if (token->type == CPP_SEMICOLON)
13663                     cp_lexer_consume_token (parser->lexer);
13664                   return;
13665                 }
13666               else
13667                 {
13668                   /* Create the declaration.  */
13669                   decl = grokfield (declarator, &decl_specifiers,
13670                                     initializer, asm_specification,
13671                                     attributes);
13672                   /* Any initialization must have been from a
13673                      constant-expression.  */
13674                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13675                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13676                 }
13677             }
13678
13679           /* Reset PREFIX_ATTRIBUTES.  */
13680           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13681             attributes = TREE_CHAIN (attributes);
13682           if (attributes)
13683             TREE_CHAIN (attributes) = NULL_TREE;
13684
13685           /* If there is any qualification still in effect, clear it
13686              now; we will be starting fresh with the next declarator.  */
13687           parser->scope = NULL_TREE;
13688           parser->qualifying_scope = NULL_TREE;
13689           parser->object_scope = NULL_TREE;
13690           /* If it's a `,', then there are more declarators.  */
13691           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13692             cp_lexer_consume_token (parser->lexer);
13693           /* If the next token isn't a `;', then we have a parse error.  */
13694           else if (cp_lexer_next_token_is_not (parser->lexer,
13695                                                CPP_SEMICOLON))
13696             {
13697               cp_parser_error (parser, "expected %<;%>");
13698               /* Skip tokens until we find a `;'.  */
13699               cp_parser_skip_to_end_of_statement (parser);
13700
13701               break;
13702             }
13703
13704           if (decl)
13705             {
13706               /* Add DECL to the list of members.  */
13707               if (!friend_p)
13708                 finish_member_declaration (decl);
13709
13710               if (TREE_CODE (decl) == FUNCTION_DECL)
13711                 cp_parser_save_default_args (parser, decl);
13712             }
13713         }
13714     }
13715
13716   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13717 }
13718
13719 /* Parse a pure-specifier.
13720
13721    pure-specifier:
13722      = 0
13723
13724    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13725    Otherwise, ERROR_MARK_NODE is returned.  */
13726
13727 static tree
13728 cp_parser_pure_specifier (cp_parser* parser)
13729 {
13730   cp_token *token;
13731
13732   /* Look for the `=' token.  */
13733   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13734     return error_mark_node;
13735   /* Look for the `0' token.  */
13736   token = cp_lexer_consume_token (parser->lexer);
13737   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13738   if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13739     return integer_zero_node;
13740
13741   cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13742   cp_parser_skip_to_end_of_statement (parser);
13743   return error_mark_node;
13744 }
13745
13746 /* Parse a constant-initializer.
13747
13748    constant-initializer:
13749      = constant-expression
13750
13751    Returns a representation of the constant-expression.  */
13752
13753 static tree
13754 cp_parser_constant_initializer (cp_parser* parser)
13755 {
13756   /* Look for the `=' token.  */
13757   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13758     return error_mark_node;
13759
13760   /* It is invalid to write:
13761
13762        struct S { static const int i = { 7 }; };
13763
13764      */
13765   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13766     {
13767       cp_parser_error (parser,
13768                        "a brace-enclosed initializer is not allowed here");
13769       /* Consume the opening brace.  */
13770       cp_lexer_consume_token (parser->lexer);
13771       /* Skip the initializer.  */
13772       cp_parser_skip_to_closing_brace (parser);
13773       /* Look for the trailing `}'.  */
13774       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13775
13776       return error_mark_node;
13777     }
13778
13779   return cp_parser_constant_expression (parser,
13780                                         /*allow_non_constant=*/false,
13781                                         NULL);
13782 }
13783
13784 /* Derived classes [gram.class.derived] */
13785
13786 /* Parse a base-clause.
13787
13788    base-clause:
13789      : base-specifier-list
13790
13791    base-specifier-list:
13792      base-specifier
13793      base-specifier-list , base-specifier
13794
13795    Returns a TREE_LIST representing the base-classes, in the order in
13796    which they were declared.  The representation of each node is as
13797    described by cp_parser_base_specifier.
13798
13799    In the case that no bases are specified, this function will return
13800    NULL_TREE, not ERROR_MARK_NODE.  */
13801
13802 static tree
13803 cp_parser_base_clause (cp_parser* parser)
13804 {
13805   tree bases = NULL_TREE;
13806
13807   /* Look for the `:' that begins the list.  */
13808   cp_parser_require (parser, CPP_COLON, "`:'");
13809
13810   /* Scan the base-specifier-list.  */
13811   while (true)
13812     {
13813       cp_token *token;
13814       tree base;
13815
13816       /* Look for the base-specifier.  */
13817       base = cp_parser_base_specifier (parser);
13818       /* Add BASE to the front of the list.  */
13819       if (base != error_mark_node)
13820         {
13821           TREE_CHAIN (base) = bases;
13822           bases = base;
13823         }
13824       /* Peek at the next token.  */
13825       token = cp_lexer_peek_token (parser->lexer);
13826       /* If it's not a comma, then the list is complete.  */
13827       if (token->type != CPP_COMMA)
13828         break;
13829       /* Consume the `,'.  */
13830       cp_lexer_consume_token (parser->lexer);
13831     }
13832
13833   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13834      base class had a qualified name.  However, the next name that
13835      appears is certainly not qualified.  */
13836   parser->scope = NULL_TREE;
13837   parser->qualifying_scope = NULL_TREE;
13838   parser->object_scope = NULL_TREE;
13839
13840   return nreverse (bases);
13841 }
13842
13843 /* Parse a base-specifier.
13844
13845    base-specifier:
13846      :: [opt] nested-name-specifier [opt] class-name
13847      virtual access-specifier [opt] :: [opt] nested-name-specifier
13848        [opt] class-name
13849      access-specifier virtual [opt] :: [opt] nested-name-specifier
13850        [opt] class-name
13851
13852    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13853    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13854    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13855    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13856
13857 static tree
13858 cp_parser_base_specifier (cp_parser* parser)
13859 {
13860   cp_token *token;
13861   bool done = false;
13862   bool virtual_p = false;
13863   bool duplicate_virtual_error_issued_p = false;
13864   bool duplicate_access_error_issued_p = false;
13865   bool class_scope_p, template_p;
13866   tree access = access_default_node;
13867   tree type;
13868
13869   /* Process the optional `virtual' and `access-specifier'.  */
13870   while (!done)
13871     {
13872       /* Peek at the next token.  */
13873       token = cp_lexer_peek_token (parser->lexer);
13874       /* Process `virtual'.  */
13875       switch (token->keyword)
13876         {
13877         case RID_VIRTUAL:
13878           /* If `virtual' appears more than once, issue an error.  */
13879           if (virtual_p && !duplicate_virtual_error_issued_p)
13880             {
13881               cp_parser_error (parser,
13882                                "%<virtual%> specified more than once in base-specified");
13883               duplicate_virtual_error_issued_p = true;
13884             }
13885
13886           virtual_p = true;
13887
13888           /* Consume the `virtual' token.  */
13889           cp_lexer_consume_token (parser->lexer);
13890
13891           break;
13892
13893         case RID_PUBLIC:
13894         case RID_PROTECTED:
13895         case RID_PRIVATE:
13896           /* If more than one access specifier appears, issue an
13897              error.  */
13898           if (access != access_default_node
13899               && !duplicate_access_error_issued_p)
13900             {
13901               cp_parser_error (parser,
13902                                "more than one access specifier in base-specified");
13903               duplicate_access_error_issued_p = true;
13904             }
13905
13906           access = ridpointers[(int) token->keyword];
13907
13908           /* Consume the access-specifier.  */
13909           cp_lexer_consume_token (parser->lexer);
13910
13911           break;
13912
13913         default:
13914           done = true;
13915           break;
13916         }
13917     }
13918   /* It is not uncommon to see programs mechanically, erroneously, use
13919      the 'typename' keyword to denote (dependent) qualified types
13920      as base classes.  */
13921   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13922     {
13923       if (!processing_template_decl)
13924         error ("keyword %<typename%> not allowed outside of templates");
13925       else
13926         error ("keyword %<typename%> not allowed in this context "
13927                "(the base class is implicitly a type)");
13928       cp_lexer_consume_token (parser->lexer);
13929     }
13930
13931   /* Look for the optional `::' operator.  */
13932   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13933   /* Look for the nested-name-specifier.  The simplest way to
13934      implement:
13935
13936        [temp.res]
13937
13938        The keyword `typename' is not permitted in a base-specifier or
13939        mem-initializer; in these contexts a qualified name that
13940        depends on a template-parameter is implicitly assumed to be a
13941        type name.
13942
13943      is to pretend that we have seen the `typename' keyword at this
13944      point.  */
13945   cp_parser_nested_name_specifier_opt (parser,
13946                                        /*typename_keyword_p=*/true,
13947                                        /*check_dependency_p=*/true,
13948                                        typename_type,
13949                                        /*is_declaration=*/true);
13950   /* If the base class is given by a qualified name, assume that names
13951      we see are type names or templates, as appropriate.  */
13952   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13953   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13954
13955   /* Finally, look for the class-name.  */
13956   type = cp_parser_class_name (parser,
13957                                class_scope_p,
13958                                template_p,
13959                                typename_type,
13960                                /*check_dependency_p=*/true,
13961                                /*class_head_p=*/false,
13962                                /*is_declaration=*/true);
13963
13964   if (type == error_mark_node)
13965     return error_mark_node;
13966
13967   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13968 }
13969
13970 /* Exception handling [gram.exception] */
13971
13972 /* Parse an (optional) exception-specification.
13973
13974    exception-specification:
13975      throw ( type-id-list [opt] )
13976
13977    Returns a TREE_LIST representing the exception-specification.  The
13978    TREE_VALUE of each node is a type.  */
13979
13980 static tree
13981 cp_parser_exception_specification_opt (cp_parser* parser)
13982 {
13983   cp_token *token;
13984   tree type_id_list;
13985
13986   /* Peek at the next token.  */
13987   token = cp_lexer_peek_token (parser->lexer);
13988   /* If it's not `throw', then there's no exception-specification.  */
13989   if (!cp_parser_is_keyword (token, RID_THROW))
13990     return NULL_TREE;
13991
13992   /* Consume the `throw'.  */
13993   cp_lexer_consume_token (parser->lexer);
13994
13995   /* Look for the `('.  */
13996   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13997
13998   /* Peek at the next token.  */
13999   token = cp_lexer_peek_token (parser->lexer);
14000   /* If it's not a `)', then there is a type-id-list.  */
14001   if (token->type != CPP_CLOSE_PAREN)
14002     {
14003       const char *saved_message;
14004
14005       /* Types may not be defined in an exception-specification.  */
14006       saved_message = parser->type_definition_forbidden_message;
14007       parser->type_definition_forbidden_message
14008         = "types may not be defined in an exception-specification";
14009       /* Parse the type-id-list.  */
14010       type_id_list = cp_parser_type_id_list (parser);
14011       /* Restore the saved message.  */
14012       parser->type_definition_forbidden_message = saved_message;
14013     }
14014   else
14015     type_id_list = empty_except_spec;
14016
14017   /* Look for the `)'.  */
14018   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14019
14020   return type_id_list;
14021 }
14022
14023 /* Parse an (optional) type-id-list.
14024
14025    type-id-list:
14026      type-id
14027      type-id-list , type-id
14028
14029    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14030    in the order that the types were presented.  */
14031
14032 static tree
14033 cp_parser_type_id_list (cp_parser* parser)
14034 {
14035   tree types = NULL_TREE;
14036
14037   while (true)
14038     {
14039       cp_token *token;
14040       tree type;
14041
14042       /* Get the next type-id.  */
14043       type = cp_parser_type_id (parser);
14044       /* Add it to the list.  */
14045       types = add_exception_specifier (types, type, /*complain=*/1);
14046       /* Peek at the next token.  */
14047       token = cp_lexer_peek_token (parser->lexer);
14048       /* If it is not a `,', we are done.  */
14049       if (token->type != CPP_COMMA)
14050         break;
14051       /* Consume the `,'.  */
14052       cp_lexer_consume_token (parser->lexer);
14053     }
14054
14055   return nreverse (types);
14056 }
14057
14058 /* Parse a try-block.
14059
14060    try-block:
14061      try compound-statement handler-seq  */
14062
14063 static tree
14064 cp_parser_try_block (cp_parser* parser)
14065 {
14066   tree try_block;
14067
14068   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14069   try_block = begin_try_block ();
14070   cp_parser_compound_statement (parser, NULL, true);
14071   finish_try_block (try_block);
14072   cp_parser_handler_seq (parser);
14073   finish_handler_sequence (try_block);
14074
14075   return try_block;
14076 }
14077
14078 /* Parse a function-try-block.
14079
14080    function-try-block:
14081      try ctor-initializer [opt] function-body handler-seq  */
14082
14083 static bool
14084 cp_parser_function_try_block (cp_parser* parser)
14085 {
14086   tree try_block;
14087   bool ctor_initializer_p;
14088
14089   /* Look for the `try' keyword.  */
14090   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14091     return false;
14092   /* Let the rest of the front-end know where we are.  */
14093   try_block = begin_function_try_block ();
14094   /* Parse the function-body.  */
14095   ctor_initializer_p
14096     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14097   /* We're done with the `try' part.  */
14098   finish_function_try_block (try_block);
14099   /* Parse the handlers.  */
14100   cp_parser_handler_seq (parser);
14101   /* We're done with the handlers.  */
14102   finish_function_handler_sequence (try_block);
14103
14104   return ctor_initializer_p;
14105 }
14106
14107 /* Parse a handler-seq.
14108
14109    handler-seq:
14110      handler handler-seq [opt]  */
14111
14112 static void
14113 cp_parser_handler_seq (cp_parser* parser)
14114 {
14115   while (true)
14116     {
14117       cp_token *token;
14118
14119       /* Parse the handler.  */
14120       cp_parser_handler (parser);
14121       /* Peek at the next token.  */
14122       token = cp_lexer_peek_token (parser->lexer);
14123       /* If it's not `catch' then there are no more handlers.  */
14124       if (!cp_parser_is_keyword (token, RID_CATCH))
14125         break;
14126     }
14127 }
14128
14129 /* Parse a handler.
14130
14131    handler:
14132      catch ( exception-declaration ) compound-statement  */
14133
14134 static void
14135 cp_parser_handler (cp_parser* parser)
14136 {
14137   tree handler;
14138   tree declaration;
14139
14140   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14141   handler = begin_handler ();
14142   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14143   declaration = cp_parser_exception_declaration (parser);
14144   finish_handler_parms (declaration, handler);
14145   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14146   cp_parser_compound_statement (parser, NULL, false);
14147   finish_handler (handler);
14148 }
14149
14150 /* Parse an exception-declaration.
14151
14152    exception-declaration:
14153      type-specifier-seq declarator
14154      type-specifier-seq abstract-declarator
14155      type-specifier-seq
14156      ...
14157
14158    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14159    ellipsis variant is used.  */
14160
14161 static tree
14162 cp_parser_exception_declaration (cp_parser* parser)
14163 {
14164   tree decl;
14165   cp_decl_specifier_seq type_specifiers;
14166   cp_declarator *declarator;
14167   const char *saved_message;
14168
14169   /* If it's an ellipsis, it's easy to handle.  */
14170   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14171     {
14172       /* Consume the `...' token.  */
14173       cp_lexer_consume_token (parser->lexer);
14174       return NULL_TREE;
14175     }
14176
14177   /* Types may not be defined in exception-declarations.  */
14178   saved_message = parser->type_definition_forbidden_message;
14179   parser->type_definition_forbidden_message
14180     = "types may not be defined in exception-declarations";
14181
14182   /* Parse the type-specifier-seq.  */
14183   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14184                                 &type_specifiers);
14185   /* If it's a `)', then there is no declarator.  */
14186   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14187     declarator = NULL;
14188   else
14189     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14190                                        /*ctor_dtor_or_conv_p=*/NULL,
14191                                        /*parenthesized_p=*/NULL,
14192                                        /*member_p=*/false);
14193
14194   /* Restore the saved message.  */
14195   parser->type_definition_forbidden_message = saved_message;
14196
14197   if (type_specifiers.any_specifiers_p)
14198     {
14199       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14200       if (decl == NULL_TREE)
14201         error ("invalid catch parameter");
14202     }
14203   else
14204     decl = NULL_TREE;
14205
14206   return decl;
14207 }
14208
14209 /* Parse a throw-expression.
14210
14211    throw-expression:
14212      throw assignment-expression [opt]
14213
14214    Returns a THROW_EXPR representing the throw-expression.  */
14215
14216 static tree
14217 cp_parser_throw_expression (cp_parser* parser)
14218 {
14219   tree expression;
14220   cp_token* token;
14221
14222   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14223   token = cp_lexer_peek_token (parser->lexer);
14224   /* Figure out whether or not there is an assignment-expression
14225      following the "throw" keyword.  */
14226   if (token->type == CPP_COMMA
14227       || token->type == CPP_SEMICOLON
14228       || token->type == CPP_CLOSE_PAREN
14229       || token->type == CPP_CLOSE_SQUARE
14230       || token->type == CPP_CLOSE_BRACE
14231       || token->type == CPP_COLON)
14232     expression = NULL_TREE;
14233   else
14234     expression = cp_parser_assignment_expression (parser,
14235                                                   /*cast_p=*/false);
14236
14237   return build_throw (expression);
14238 }
14239
14240 /* GNU Extensions */
14241
14242 /* Parse an (optional) asm-specification.
14243
14244    asm-specification:
14245      asm ( string-literal )
14246
14247    If the asm-specification is present, returns a STRING_CST
14248    corresponding to the string-literal.  Otherwise, returns
14249    NULL_TREE.  */
14250
14251 static tree
14252 cp_parser_asm_specification_opt (cp_parser* parser)
14253 {
14254   cp_token *token;
14255   tree asm_specification;
14256
14257   /* Peek at the next token.  */
14258   token = cp_lexer_peek_token (parser->lexer);
14259   /* If the next token isn't the `asm' keyword, then there's no
14260      asm-specification.  */
14261   if (!cp_parser_is_keyword (token, RID_ASM))
14262     return NULL_TREE;
14263
14264   /* Consume the `asm' token.  */
14265   cp_lexer_consume_token (parser->lexer);
14266   /* Look for the `('.  */
14267   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14268
14269   /* Look for the string-literal.  */
14270   asm_specification = cp_parser_string_literal (parser, false, false);
14271
14272   /* Look for the `)'.  */
14273   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14274
14275   return asm_specification;
14276 }
14277
14278 /* Parse an asm-operand-list.
14279
14280    asm-operand-list:
14281      asm-operand
14282      asm-operand-list , asm-operand
14283
14284    asm-operand:
14285      string-literal ( expression )
14286      [ string-literal ] string-literal ( expression )
14287
14288    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14289    each node is the expression.  The TREE_PURPOSE is itself a
14290    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14291    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14292    is a STRING_CST for the string literal before the parenthesis.  */
14293
14294 static tree
14295 cp_parser_asm_operand_list (cp_parser* parser)
14296 {
14297   tree asm_operands = NULL_TREE;
14298
14299   while (true)
14300     {
14301       tree string_literal;
14302       tree expression;
14303       tree name;
14304
14305       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14306         {
14307           /* Consume the `[' token.  */
14308           cp_lexer_consume_token (parser->lexer);
14309           /* Read the operand name.  */
14310           name = cp_parser_identifier (parser);
14311           if (name != error_mark_node)
14312             name = build_string (IDENTIFIER_LENGTH (name),
14313                                  IDENTIFIER_POINTER (name));
14314           /* Look for the closing `]'.  */
14315           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14316         }
14317       else
14318         name = NULL_TREE;
14319       /* Look for the string-literal.  */
14320       string_literal = cp_parser_string_literal (parser, false, false);
14321
14322       /* Look for the `('.  */
14323       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14324       /* Parse the expression.  */
14325       expression = cp_parser_expression (parser, /*cast_p=*/false);
14326       /* Look for the `)'.  */
14327       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14328
14329       /* Add this operand to the list.  */
14330       asm_operands = tree_cons (build_tree_list (name, string_literal),
14331                                 expression,
14332                                 asm_operands);
14333       /* If the next token is not a `,', there are no more
14334          operands.  */
14335       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14336         break;
14337       /* Consume the `,'.  */
14338       cp_lexer_consume_token (parser->lexer);
14339     }
14340
14341   return nreverse (asm_operands);
14342 }
14343
14344 /* Parse an asm-clobber-list.
14345
14346    asm-clobber-list:
14347      string-literal
14348      asm-clobber-list , string-literal
14349
14350    Returns a TREE_LIST, indicating the clobbers in the order that they
14351    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14352
14353 static tree
14354 cp_parser_asm_clobber_list (cp_parser* parser)
14355 {
14356   tree clobbers = NULL_TREE;
14357
14358   while (true)
14359     {
14360       tree string_literal;
14361
14362       /* Look for the string literal.  */
14363       string_literal = cp_parser_string_literal (parser, false, false);
14364       /* Add it to the list.  */
14365       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14366       /* If the next token is not a `,', then the list is
14367          complete.  */
14368       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14369         break;
14370       /* Consume the `,' token.  */
14371       cp_lexer_consume_token (parser->lexer);
14372     }
14373
14374   return clobbers;
14375 }
14376
14377 /* Parse an (optional) series of attributes.
14378
14379    attributes:
14380      attributes attribute
14381
14382    attribute:
14383      __attribute__ (( attribute-list [opt] ))
14384
14385    The return value is as for cp_parser_attribute_list.  */
14386
14387 static tree
14388 cp_parser_attributes_opt (cp_parser* parser)
14389 {
14390   tree attributes = NULL_TREE;
14391
14392   while (true)
14393     {
14394       cp_token *token;
14395       tree attribute_list;
14396
14397       /* Peek at the next token.  */
14398       token = cp_lexer_peek_token (parser->lexer);
14399       /* If it's not `__attribute__', then we're done.  */
14400       if (token->keyword != RID_ATTRIBUTE)
14401         break;
14402
14403       /* Consume the `__attribute__' keyword.  */
14404       cp_lexer_consume_token (parser->lexer);
14405       /* Look for the two `(' tokens.  */
14406       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14407       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14408
14409       /* Peek at the next token.  */
14410       token = cp_lexer_peek_token (parser->lexer);
14411       if (token->type != CPP_CLOSE_PAREN)
14412         /* Parse the attribute-list.  */
14413         attribute_list = cp_parser_attribute_list (parser);
14414       else
14415         /* If the next token is a `)', then there is no attribute
14416            list.  */
14417         attribute_list = NULL;
14418
14419       /* Look for the two `)' tokens.  */
14420       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14421       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14422
14423       /* Add these new attributes to the list.  */
14424       attributes = chainon (attributes, attribute_list);
14425     }
14426
14427   return attributes;
14428 }
14429
14430 /* Parse an attribute-list.
14431
14432    attribute-list:
14433      attribute
14434      attribute-list , attribute
14435
14436    attribute:
14437      identifier
14438      identifier ( identifier )
14439      identifier ( identifier , expression-list )
14440      identifier ( expression-list )
14441
14442    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14443    to an attribute.  The TREE_PURPOSE of each node is the identifier
14444    indicating which attribute is in use.  The TREE_VALUE represents
14445    the arguments, if any.  */
14446
14447 static tree
14448 cp_parser_attribute_list (cp_parser* parser)
14449 {
14450   tree attribute_list = NULL_TREE;
14451   bool save_translate_strings_p = parser->translate_strings_p;
14452
14453   parser->translate_strings_p = false;
14454   while (true)
14455     {
14456       cp_token *token;
14457       tree identifier;
14458       tree attribute;
14459
14460       /* Look for the identifier.  We also allow keywords here; for
14461          example `__attribute__ ((const))' is legal.  */
14462       token = cp_lexer_peek_token (parser->lexer);
14463       if (token->type == CPP_NAME
14464           || token->type == CPP_KEYWORD)
14465         {
14466           /* Consume the token.  */
14467           token = cp_lexer_consume_token (parser->lexer);
14468
14469           /* Save away the identifier that indicates which attribute
14470              this is.  */
14471           identifier = token->value;
14472           attribute = build_tree_list (identifier, NULL_TREE);
14473
14474           /* Peek at the next token.  */
14475           token = cp_lexer_peek_token (parser->lexer);
14476           /* If it's an `(', then parse the attribute arguments.  */
14477           if (token->type == CPP_OPEN_PAREN)
14478             {
14479               tree arguments;
14480
14481               arguments = (cp_parser_parenthesized_expression_list
14482                            (parser, true, /*cast_p=*/false,
14483                             /*non_constant_p=*/NULL));
14484               /* Save the identifier and arguments away.  */
14485               TREE_VALUE (attribute) = arguments;
14486             }
14487
14488           /* Add this attribute to the list.  */
14489           TREE_CHAIN (attribute) = attribute_list;
14490           attribute_list = attribute;
14491
14492           token = cp_lexer_peek_token (parser->lexer);
14493         }
14494       /* Now, look for more attributes.  If the next token isn't a
14495          `,', we're done.  */
14496       if (token->type != CPP_COMMA)
14497         break;
14498
14499       /* Consume the comma and keep going.  */
14500       cp_lexer_consume_token (parser->lexer);
14501     }
14502   parser->translate_strings_p = save_translate_strings_p;
14503
14504   /* We built up the list in reverse order.  */
14505   return nreverse (attribute_list);
14506 }
14507
14508 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14509    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14510    current value of the PEDANTIC flag, regardless of whether or not
14511    the `__extension__' keyword is present.  The caller is responsible
14512    for restoring the value of the PEDANTIC flag.  */
14513
14514 static bool
14515 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14516 {
14517   /* Save the old value of the PEDANTIC flag.  */
14518   *saved_pedantic = pedantic;
14519
14520   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14521     {
14522       /* Consume the `__extension__' token.  */
14523       cp_lexer_consume_token (parser->lexer);
14524       /* We're not being pedantic while the `__extension__' keyword is
14525          in effect.  */
14526       pedantic = 0;
14527
14528       return true;
14529     }
14530
14531   return false;
14532 }
14533
14534 /* Parse a label declaration.
14535
14536    label-declaration:
14537      __label__ label-declarator-seq ;
14538
14539    label-declarator-seq:
14540      identifier , label-declarator-seq
14541      identifier  */
14542
14543 static void
14544 cp_parser_label_declaration (cp_parser* parser)
14545 {
14546   /* Look for the `__label__' keyword.  */
14547   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14548
14549   while (true)
14550     {
14551       tree identifier;
14552
14553       /* Look for an identifier.  */
14554       identifier = cp_parser_identifier (parser);
14555       /* If we failed, stop.  */
14556       if (identifier == error_mark_node)
14557         break;
14558       /* Declare it as a label.  */
14559       finish_label_decl (identifier);
14560       /* If the next token is a `;', stop.  */
14561       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14562         break;
14563       /* Look for the `,' separating the label declarations.  */
14564       cp_parser_require (parser, CPP_COMMA, "`,'");
14565     }
14566
14567   /* Look for the final `;'.  */
14568   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14569 }
14570
14571 /* Support Functions */
14572
14573 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14574    NAME should have one of the representations used for an
14575    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14576    is returned.  If PARSER->SCOPE is a dependent type, then a
14577    SCOPE_REF is returned.
14578
14579    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14580    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14581    was formed.  Abstractly, such entities should not be passed to this
14582    function, because they do not need to be looked up, but it is
14583    simpler to check for this special case here, rather than at the
14584    call-sites.
14585
14586    In cases not explicitly covered above, this function returns a
14587    DECL, OVERLOAD, or baselink representing the result of the lookup.
14588    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14589    is returned.
14590
14591    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14592    (e.g., "struct") that was used.  In that case bindings that do not
14593    refer to types are ignored.
14594
14595    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14596    ignored.
14597
14598    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14599    are ignored.
14600
14601    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14602    types.
14603
14604    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14605    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14606    NULL_TREE otherwise.  */ 
14607
14608 static tree
14609 cp_parser_lookup_name (cp_parser *parser, tree name,
14610                        enum tag_types tag_type,
14611                        bool is_template, 
14612                        bool is_namespace,
14613                        bool check_dependency,
14614                        tree *ambiguous_decls)
14615 {
14616   int flags = 0;
14617   tree decl;
14618   tree object_type = parser->context->object_type;
14619
14620   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14621     flags |= LOOKUP_COMPLAIN;
14622
14623   /* Assume that the lookup will be unambiguous.  */
14624   if (ambiguous_decls)
14625     *ambiguous_decls = NULL_TREE;
14626
14627   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14628      no longer valid.  Note that if we are parsing tentatively, and
14629      the parse fails, OBJECT_TYPE will be automatically restored.  */
14630   parser->context->object_type = NULL_TREE;
14631
14632   if (name == error_mark_node)
14633     return error_mark_node;
14634
14635   /* A template-id has already been resolved; there is no lookup to
14636      do.  */
14637   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14638     return name;
14639   if (BASELINK_P (name))
14640     {
14641       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14642                   == TEMPLATE_ID_EXPR);
14643       return name;
14644     }
14645
14646   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14647      it should already have been checked to make sure that the name
14648      used matches the type being destroyed.  */
14649   if (TREE_CODE (name) == BIT_NOT_EXPR)
14650     {
14651       tree type;
14652
14653       /* Figure out to which type this destructor applies.  */
14654       if (parser->scope)
14655         type = parser->scope;
14656       else if (object_type)
14657         type = object_type;
14658       else
14659         type = current_class_type;
14660       /* If that's not a class type, there is no destructor.  */
14661       if (!type || !CLASS_TYPE_P (type))
14662         return error_mark_node;
14663       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14664         lazily_declare_fn (sfk_destructor, type);
14665       if (!CLASSTYPE_DESTRUCTORS (type))
14666           return error_mark_node;
14667       /* If it was a class type, return the destructor.  */
14668       return CLASSTYPE_DESTRUCTORS (type);
14669     }
14670
14671   /* By this point, the NAME should be an ordinary identifier.  If
14672      the id-expression was a qualified name, the qualifying scope is
14673      stored in PARSER->SCOPE at this point.  */
14674   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14675
14676   /* Perform the lookup.  */
14677   if (parser->scope)
14678     {
14679       bool dependent_p;
14680
14681       if (parser->scope == error_mark_node)
14682         return error_mark_node;
14683
14684       /* If the SCOPE is dependent, the lookup must be deferred until
14685          the template is instantiated -- unless we are explicitly
14686          looking up names in uninstantiated templates.  Even then, we
14687          cannot look up the name if the scope is not a class type; it
14688          might, for example, be a template type parameter.  */
14689       dependent_p = (TYPE_P (parser->scope)
14690                      && !(parser->in_declarator_p
14691                           && currently_open_class (parser->scope))
14692                      && dependent_type_p (parser->scope));
14693       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14694            && dependent_p)
14695         {
14696           if (tag_type)
14697             {
14698               tree type;
14699
14700               /* The resolution to Core Issue 180 says that `struct
14701                  A::B' should be considered a type-name, even if `A'
14702                  is dependent.  */
14703               type = make_typename_type (parser->scope, name, tag_type,
14704                                          /*complain=*/tf_error);
14705               decl = TYPE_NAME (type);
14706             }
14707           else if (is_template
14708                    && (cp_parser_next_token_ends_template_argument_p (parser)
14709                        || cp_lexer_next_token_is (parser->lexer,
14710                                                   CPP_CLOSE_PAREN)))
14711             decl = make_unbound_class_template (parser->scope,
14712                                                 name, NULL_TREE,
14713                                                 /*complain=*/tf_error);
14714           else
14715             decl = build_qualified_name (/*type=*/NULL_TREE,
14716                                          parser->scope, name,
14717                                          is_template);
14718         }
14719       else
14720         {
14721           tree pushed_scope = NULL_TREE;
14722
14723           /* If PARSER->SCOPE is a dependent type, then it must be a
14724              class type, and we must not be checking dependencies;
14725              otherwise, we would have processed this lookup above.  So
14726              that PARSER->SCOPE is not considered a dependent base by
14727              lookup_member, we must enter the scope here.  */
14728           if (dependent_p)
14729             pushed_scope = push_scope (parser->scope);
14730           /* If the PARSER->SCOPE is a template specialization, it
14731              may be instantiated during name lookup.  In that case,
14732              errors may be issued.  Even if we rollback the current
14733              tentative parse, those errors are valid.  */
14734           decl = lookup_qualified_name (parser->scope, name,
14735                                         tag_type != none_type,
14736                                         /*complain=*/true);
14737           if (pushed_scope)
14738             pop_scope (pushed_scope);
14739         }
14740       parser->qualifying_scope = parser->scope;
14741       parser->object_scope = NULL_TREE;
14742     }
14743   else if (object_type)
14744     {
14745       tree object_decl = NULL_TREE;
14746       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14747          OBJECT_TYPE is not a class.  */
14748       if (CLASS_TYPE_P (object_type))
14749         /* If the OBJECT_TYPE is a template specialization, it may
14750            be instantiated during name lookup.  In that case, errors
14751            may be issued.  Even if we rollback the current tentative
14752            parse, those errors are valid.  */
14753         object_decl = lookup_member (object_type,
14754                                      name,
14755                                      /*protect=*/0,
14756                                      tag_type != none_type);
14757       /* Look it up in the enclosing context, too.  */
14758       decl = lookup_name_real (name, tag_type != none_type,
14759                                /*nonclass=*/0,
14760                                /*block_p=*/true, is_namespace, flags);
14761       parser->object_scope = object_type;
14762       parser->qualifying_scope = NULL_TREE;
14763       if (object_decl)
14764         decl = object_decl;
14765     }
14766   else
14767     {
14768       decl = lookup_name_real (name, tag_type != none_type,
14769                                /*nonclass=*/0,
14770                                /*block_p=*/true, is_namespace, flags);
14771       parser->qualifying_scope = NULL_TREE;
14772       parser->object_scope = NULL_TREE;
14773     }
14774
14775   /* If the lookup failed, let our caller know.  */
14776   if (!decl || decl == error_mark_node)
14777     return error_mark_node;
14778
14779   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14780   if (TREE_CODE (decl) == TREE_LIST)
14781     {
14782       if (ambiguous_decls)
14783         *ambiguous_decls = decl;
14784       /* The error message we have to print is too complicated for
14785          cp_parser_error, so we incorporate its actions directly.  */
14786       if (!cp_parser_simulate_error (parser))
14787         {
14788           error ("reference to %qD is ambiguous", name);
14789           print_candidates (decl);
14790         }
14791       return error_mark_node;
14792     }
14793
14794   gcc_assert (DECL_P (decl)
14795               || TREE_CODE (decl) == OVERLOAD
14796               || TREE_CODE (decl) == SCOPE_REF
14797               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14798               || BASELINK_P (decl));
14799
14800   /* If we have resolved the name of a member declaration, check to
14801      see if the declaration is accessible.  When the name resolves to
14802      set of overloaded functions, accessibility is checked when
14803      overload resolution is done.
14804
14805      During an explicit instantiation, access is not checked at all,
14806      as per [temp.explicit].  */
14807   if (DECL_P (decl))
14808     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14809
14810   return decl;
14811 }
14812
14813 /* Like cp_parser_lookup_name, but for use in the typical case where
14814    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14815    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14816
14817 static tree
14818 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14819 {
14820   return cp_parser_lookup_name (parser, name,
14821                                 none_type,
14822                                 /*is_template=*/false,
14823                                 /*is_namespace=*/false,
14824                                 /*check_dependency=*/true,
14825                                 /*ambiguous_decls=*/NULL);
14826 }
14827
14828 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14829    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14830    true, the DECL indicates the class being defined in a class-head,
14831    or declared in an elaborated-type-specifier.
14832
14833    Otherwise, return DECL.  */
14834
14835 static tree
14836 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14837 {
14838   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14839      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14840
14841        struct A {
14842          template <typename T> struct B;
14843        };
14844
14845        template <typename T> struct A::B {};
14846
14847      Similarly, in an elaborated-type-specifier:
14848
14849        namespace N { struct X{}; }
14850
14851        struct A {
14852          template <typename T> friend struct N::X;
14853        };
14854
14855      However, if the DECL refers to a class type, and we are in
14856      the scope of the class, then the name lookup automatically
14857      finds the TYPE_DECL created by build_self_reference rather
14858      than a TEMPLATE_DECL.  For example, in:
14859
14860        template <class T> struct S {
14861          S s;
14862        };
14863
14864      there is no need to handle such case.  */
14865
14866   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14867     return DECL_TEMPLATE_RESULT (decl);
14868
14869   return decl;
14870 }
14871
14872 /* If too many, or too few, template-parameter lists apply to the
14873    declarator, issue an error message.  Returns TRUE if all went well,
14874    and FALSE otherwise.  */
14875
14876 static bool
14877 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14878                                                 cp_declarator *declarator)
14879 {
14880   unsigned num_templates;
14881
14882   /* We haven't seen any classes that involve template parameters yet.  */
14883   num_templates = 0;
14884
14885   switch (declarator->kind)
14886     {
14887     case cdk_id:
14888       if (declarator->u.id.qualifying_scope)
14889         {
14890           tree scope;
14891           tree member;
14892
14893           scope = declarator->u.id.qualifying_scope;
14894           member = declarator->u.id.unqualified_name;
14895
14896           while (scope && CLASS_TYPE_P (scope))
14897             {
14898               /* You're supposed to have one `template <...>'
14899                  for every template class, but you don't need one
14900                  for a full specialization.  For example:
14901
14902                  template <class T> struct S{};
14903                  template <> struct S<int> { void f(); };
14904                  void S<int>::f () {}
14905
14906                  is correct; there shouldn't be a `template <>' for
14907                  the definition of `S<int>::f'.  */
14908               if (CLASSTYPE_TEMPLATE_INFO (scope)
14909                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14910                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14911                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14912                 ++num_templates;
14913
14914               scope = TYPE_CONTEXT (scope);
14915             }
14916         }
14917       else if (TREE_CODE (declarator->u.id.unqualified_name)
14918                == TEMPLATE_ID_EXPR)
14919         /* If the DECLARATOR has the form `X<y>' then it uses one
14920            additional level of template parameters.  */
14921         ++num_templates;
14922
14923       return cp_parser_check_template_parameters (parser,
14924                                                   num_templates);
14925
14926     case cdk_function:
14927     case cdk_array:
14928     case cdk_pointer:
14929     case cdk_reference:
14930     case cdk_ptrmem:
14931       return (cp_parser_check_declarator_template_parameters
14932               (parser, declarator->declarator));
14933
14934     case cdk_error:
14935       return true;
14936
14937     default:
14938       gcc_unreachable ();
14939     }
14940   return false;
14941 }
14942
14943 /* NUM_TEMPLATES were used in the current declaration.  If that is
14944    invalid, return FALSE and issue an error messages.  Otherwise,
14945    return TRUE.  */
14946
14947 static bool
14948 cp_parser_check_template_parameters (cp_parser* parser,
14949                                      unsigned num_templates)
14950 {
14951   /* If there are more template classes than parameter lists, we have
14952      something like:
14953
14954        template <class T> void S<T>::R<T>::f ();  */
14955   if (parser->num_template_parameter_lists < num_templates)
14956     {
14957       error ("too few template-parameter-lists");
14958       return false;
14959     }
14960   /* If there are the same number of template classes and parameter
14961      lists, that's OK.  */
14962   if (parser->num_template_parameter_lists == num_templates)
14963     return true;
14964   /* If there are more, but only one more, then we are referring to a
14965      member template.  That's OK too.  */
14966   if (parser->num_template_parameter_lists == num_templates + 1)
14967       return true;
14968   /* Otherwise, there are too many template parameter lists.  We have
14969      something like:
14970
14971      template <class T> template <class U> void S::f();  */
14972   error ("too many template-parameter-lists");
14973   return false;
14974 }
14975
14976 /* Parse an optional `::' token indicating that the following name is
14977    from the global namespace.  If so, PARSER->SCOPE is set to the
14978    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14979    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14980    Returns the new value of PARSER->SCOPE, if the `::' token is
14981    present, and NULL_TREE otherwise.  */
14982
14983 static tree
14984 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14985 {
14986   cp_token *token;
14987
14988   /* Peek at the next token.  */
14989   token = cp_lexer_peek_token (parser->lexer);
14990   /* If we're looking at a `::' token then we're starting from the
14991      global namespace, not our current location.  */
14992   if (token->type == CPP_SCOPE)
14993     {
14994       /* Consume the `::' token.  */
14995       cp_lexer_consume_token (parser->lexer);
14996       /* Set the SCOPE so that we know where to start the lookup.  */
14997       parser->scope = global_namespace;
14998       parser->qualifying_scope = global_namespace;
14999       parser->object_scope = NULL_TREE;
15000
15001       return parser->scope;
15002     }
15003   else if (!current_scope_valid_p)
15004     {
15005       parser->scope = NULL_TREE;
15006       parser->qualifying_scope = NULL_TREE;
15007       parser->object_scope = NULL_TREE;
15008     }
15009
15010   return NULL_TREE;
15011 }
15012
15013 /* Returns TRUE if the upcoming token sequence is the start of a
15014    constructor declarator.  If FRIEND_P is true, the declarator is
15015    preceded by the `friend' specifier.  */
15016
15017 static bool
15018 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15019 {
15020   bool constructor_p;
15021   tree type_decl = NULL_TREE;
15022   bool nested_name_p;
15023   cp_token *next_token;
15024
15025   /* The common case is that this is not a constructor declarator, so
15026      try to avoid doing lots of work if at all possible.  It's not
15027      valid declare a constructor at function scope.  */
15028   if (at_function_scope_p ())
15029     return false;
15030   /* And only certain tokens can begin a constructor declarator.  */
15031   next_token = cp_lexer_peek_token (parser->lexer);
15032   if (next_token->type != CPP_NAME
15033       && next_token->type != CPP_SCOPE
15034       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15035       && next_token->type != CPP_TEMPLATE_ID)
15036     return false;
15037
15038   /* Parse tentatively; we are going to roll back all of the tokens
15039      consumed here.  */
15040   cp_parser_parse_tentatively (parser);
15041   /* Assume that we are looking at a constructor declarator.  */
15042   constructor_p = true;
15043
15044   /* Look for the optional `::' operator.  */
15045   cp_parser_global_scope_opt (parser,
15046                               /*current_scope_valid_p=*/false);
15047   /* Look for the nested-name-specifier.  */
15048   nested_name_p
15049     = (cp_parser_nested_name_specifier_opt (parser,
15050                                             /*typename_keyword_p=*/false,
15051                                             /*check_dependency_p=*/false,
15052                                             /*type_p=*/false,
15053                                             /*is_declaration=*/false)
15054        != NULL_TREE);
15055   /* Outside of a class-specifier, there must be a
15056      nested-name-specifier.  */
15057   if (!nested_name_p &&
15058       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15059        || friend_p))
15060     constructor_p = false;
15061   /* If we still think that this might be a constructor-declarator,
15062      look for a class-name.  */
15063   if (constructor_p)
15064     {
15065       /* If we have:
15066
15067            template <typename T> struct S { S(); };
15068            template <typename T> S<T>::S ();
15069
15070          we must recognize that the nested `S' names a class.
15071          Similarly, for:
15072
15073            template <typename T> S<T>::S<T> ();
15074
15075          we must recognize that the nested `S' names a template.  */
15076       type_decl = cp_parser_class_name (parser,
15077                                         /*typename_keyword_p=*/false,
15078                                         /*template_keyword_p=*/false,
15079                                         none_type,
15080                                         /*check_dependency_p=*/false,
15081                                         /*class_head_p=*/false,
15082                                         /*is_declaration=*/false);
15083       /* If there was no class-name, then this is not a constructor.  */
15084       constructor_p = !cp_parser_error_occurred (parser);
15085     }
15086
15087   /* If we're still considering a constructor, we have to see a `(',
15088      to begin the parameter-declaration-clause, followed by either a
15089      `)', an `...', or a decl-specifier.  We need to check for a
15090      type-specifier to avoid being fooled into thinking that:
15091
15092        S::S (f) (int);
15093
15094      is a constructor.  (It is actually a function named `f' that
15095      takes one parameter (of type `int') and returns a value of type
15096      `S::S'.  */
15097   if (constructor_p
15098       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15099     {
15100       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15101           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15102           /* A parameter declaration begins with a decl-specifier,
15103              which is either the "attribute" keyword, a storage class
15104              specifier, or (usually) a type-specifier.  */
15105           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15106           && !cp_parser_storage_class_specifier_opt (parser))
15107         {
15108           tree type;
15109           tree pushed_scope = NULL_TREE;
15110           unsigned saved_num_template_parameter_lists;
15111
15112           /* Names appearing in the type-specifier should be looked up
15113              in the scope of the class.  */
15114           if (current_class_type)
15115             type = NULL_TREE;
15116           else
15117             {
15118               type = TREE_TYPE (type_decl);
15119               if (TREE_CODE (type) == TYPENAME_TYPE)
15120                 {
15121                   type = resolve_typename_type (type,
15122                                                 /*only_current_p=*/false);
15123                   if (type == error_mark_node)
15124                     {
15125                       cp_parser_abort_tentative_parse (parser);
15126                       return false;
15127                     }
15128                 }
15129               pushed_scope = push_scope (type);
15130             }
15131
15132           /* Inside the constructor parameter list, surrounding
15133              template-parameter-lists do not apply.  */
15134           saved_num_template_parameter_lists
15135             = parser->num_template_parameter_lists;
15136           parser->num_template_parameter_lists = 0;
15137
15138           /* Look for the type-specifier.  */
15139           cp_parser_type_specifier (parser,
15140                                     CP_PARSER_FLAGS_NONE,
15141                                     /*decl_specs=*/NULL,
15142                                     /*is_declarator=*/true,
15143                                     /*declares_class_or_enum=*/NULL,
15144                                     /*is_cv_qualifier=*/NULL);
15145
15146           parser->num_template_parameter_lists
15147             = saved_num_template_parameter_lists;
15148
15149           /* Leave the scope of the class.  */
15150           if (pushed_scope)
15151             pop_scope (pushed_scope);
15152
15153           constructor_p = !cp_parser_error_occurred (parser);
15154         }
15155     }
15156   else
15157     constructor_p = false;
15158   /* We did not really want to consume any tokens.  */
15159   cp_parser_abort_tentative_parse (parser);
15160
15161   return constructor_p;
15162 }
15163
15164 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15165    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15166    they must be performed once we are in the scope of the function.
15167
15168    Returns the function defined.  */
15169
15170 static tree
15171 cp_parser_function_definition_from_specifiers_and_declarator
15172   (cp_parser* parser,
15173    cp_decl_specifier_seq *decl_specifiers,
15174    tree attributes,
15175    const cp_declarator *declarator)
15176 {
15177   tree fn;
15178   bool success_p;
15179
15180   /* Begin the function-definition.  */
15181   success_p = start_function (decl_specifiers, declarator, attributes);
15182
15183   /* The things we're about to see are not directly qualified by any
15184      template headers we've seen thus far.  */
15185   reset_specialization ();
15186
15187   /* If there were names looked up in the decl-specifier-seq that we
15188      did not check, check them now.  We must wait until we are in the
15189      scope of the function to perform the checks, since the function
15190      might be a friend.  */
15191   perform_deferred_access_checks ();
15192
15193   if (!success_p)
15194     {
15195       /* Skip the entire function.  */
15196       error ("invalid function declaration");
15197       cp_parser_skip_to_end_of_block_or_statement (parser);
15198       fn = error_mark_node;
15199     }
15200   else
15201     fn = cp_parser_function_definition_after_declarator (parser,
15202                                                          /*inline_p=*/false);
15203
15204   return fn;
15205 }
15206
15207 /* Parse the part of a function-definition that follows the
15208    declarator.  INLINE_P is TRUE iff this function is an inline
15209    function defined with a class-specifier.
15210
15211    Returns the function defined.  */
15212
15213 static tree
15214 cp_parser_function_definition_after_declarator (cp_parser* parser,
15215                                                 bool inline_p)
15216 {
15217   tree fn;
15218   bool ctor_initializer_p = false;
15219   bool saved_in_unbraced_linkage_specification_p;
15220   unsigned saved_num_template_parameter_lists;
15221
15222   /* If the next token is `return', then the code may be trying to
15223      make use of the "named return value" extension that G++ used to
15224      support.  */
15225   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15226     {
15227       /* Consume the `return' keyword.  */
15228       cp_lexer_consume_token (parser->lexer);
15229       /* Look for the identifier that indicates what value is to be
15230          returned.  */
15231       cp_parser_identifier (parser);
15232       /* Issue an error message.  */
15233       error ("named return values are no longer supported");
15234       /* Skip tokens until we reach the start of the function body.  */
15235       while (true)
15236         {
15237           cp_token *token = cp_lexer_peek_token (parser->lexer);
15238           if (token->type == CPP_OPEN_BRACE
15239               || token->type == CPP_EOF
15240               || token->type == CPP_PRAGMA_EOL)
15241             break;
15242           cp_lexer_consume_token (parser->lexer);
15243         }
15244     }
15245   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15246      anything declared inside `f'.  */
15247   saved_in_unbraced_linkage_specification_p
15248     = parser->in_unbraced_linkage_specification_p;
15249   parser->in_unbraced_linkage_specification_p = false;
15250   /* Inside the function, surrounding template-parameter-lists do not
15251      apply.  */
15252   saved_num_template_parameter_lists
15253     = parser->num_template_parameter_lists;
15254   parser->num_template_parameter_lists = 0;
15255   /* If the next token is `try', then we are looking at a
15256      function-try-block.  */
15257   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15258     ctor_initializer_p = cp_parser_function_try_block (parser);
15259   /* A function-try-block includes the function-body, so we only do
15260      this next part if we're not processing a function-try-block.  */
15261   else
15262     ctor_initializer_p
15263       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15264
15265   /* Finish the function.  */
15266   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15267                         (inline_p ? 2 : 0));
15268   /* Generate code for it, if necessary.  */
15269   expand_or_defer_fn (fn);
15270   /* Restore the saved values.  */
15271   parser->in_unbraced_linkage_specification_p
15272     = saved_in_unbraced_linkage_specification_p;
15273   parser->num_template_parameter_lists
15274     = saved_num_template_parameter_lists;
15275
15276   return fn;
15277 }
15278
15279 /* Parse a template-declaration, assuming that the `export' (and
15280    `extern') keywords, if present, has already been scanned.  MEMBER_P
15281    is as for cp_parser_template_declaration.  */
15282
15283 static void
15284 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15285 {
15286   tree decl = NULL_TREE;
15287   tree parameter_list;
15288   bool friend_p = false;
15289   bool need_lang_pop;
15290
15291   /* Look for the `template' keyword.  */
15292   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15293     return;
15294
15295   /* And the `<'.  */
15296   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15297     return;
15298   /* [temp]
15299    
15300      A template ... shall not have C linkage.  */
15301   if (current_lang_name == lang_name_c)
15302     {
15303       error ("template with C linkage");
15304       /* Give it C++ linkage to avoid confusing other parts of the
15305          front end.  */
15306       push_lang_context (lang_name_cplusplus);
15307       need_lang_pop = true;
15308     }
15309   else
15310     need_lang_pop = false;
15311   /* If the next token is `>', then we have an invalid
15312      specialization.  Rather than complain about an invalid template
15313      parameter, issue an error message here.  */
15314   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15315     {
15316       cp_parser_error (parser, "invalid explicit specialization");
15317       begin_specialization ();
15318       parameter_list = NULL_TREE;
15319     }
15320   else
15321     /* Parse the template parameters.  */
15322     parameter_list = cp_parser_template_parameter_list (parser);
15323
15324   /* Look for the `>'.  */
15325   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15326   /* We just processed one more parameter list.  */
15327   ++parser->num_template_parameter_lists;
15328   /* If the next token is `template', there are more template
15329      parameters.  */
15330   if (cp_lexer_next_token_is_keyword (parser->lexer,
15331                                       RID_TEMPLATE))
15332     cp_parser_template_declaration_after_export (parser, member_p);
15333   else
15334     {
15335       /* There are no access checks when parsing a template, as we do not
15336          know if a specialization will be a friend.  */
15337       push_deferring_access_checks (dk_no_check);
15338
15339       decl = cp_parser_single_declaration (parser,
15340                                            member_p,
15341                                            &friend_p);
15342
15343       pop_deferring_access_checks ();
15344
15345       /* If this is a member template declaration, let the front
15346          end know.  */
15347       if (member_p && !friend_p && decl)
15348         {
15349           if (TREE_CODE (decl) == TYPE_DECL)
15350             cp_parser_check_access_in_redeclaration (decl);
15351
15352           decl = finish_member_template_decl (decl);
15353         }
15354       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15355         make_friend_class (current_class_type, TREE_TYPE (decl),
15356                            /*complain=*/true);
15357     }
15358   /* We are done with the current parameter list.  */
15359   --parser->num_template_parameter_lists;
15360
15361   /* Finish up.  */
15362   finish_template_decl (parameter_list);
15363
15364   /* Register member declarations.  */
15365   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15366     finish_member_declaration (decl);
15367   /* For the erroneous case of a template with C linkage, we pushed an
15368      implicit C++ linkage scope; exit that scope now.  */
15369   if (need_lang_pop)
15370     pop_lang_context ();
15371   /* If DECL is a function template, we must return to parse it later.
15372      (Even though there is no definition, there might be default
15373      arguments that need handling.)  */
15374   if (member_p && decl
15375       && (TREE_CODE (decl) == FUNCTION_DECL
15376           || DECL_FUNCTION_TEMPLATE_P (decl)))
15377     TREE_VALUE (parser->unparsed_functions_queues)
15378       = tree_cons (NULL_TREE, decl,
15379                    TREE_VALUE (parser->unparsed_functions_queues));
15380 }
15381
15382 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15383    `function-definition' sequence.  MEMBER_P is true, this declaration
15384    appears in a class scope.
15385
15386    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15387    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15388
15389 static tree
15390 cp_parser_single_declaration (cp_parser* parser,
15391                               bool member_p,
15392                               bool* friend_p)
15393 {
15394   int declares_class_or_enum;
15395   tree decl = NULL_TREE;
15396   cp_decl_specifier_seq decl_specifiers;
15397   bool function_definition_p = false;
15398
15399   /* This function is only used when processing a template
15400      declaration.  */
15401   gcc_assert (innermost_scope_kind () == sk_template_parms
15402               || innermost_scope_kind () == sk_template_spec);
15403
15404   /* Defer access checks until we know what is being declared.  */
15405   push_deferring_access_checks (dk_deferred);
15406
15407   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15408      alternative.  */
15409   cp_parser_decl_specifier_seq (parser,
15410                                 CP_PARSER_FLAGS_OPTIONAL,
15411                                 &decl_specifiers,
15412                                 &declares_class_or_enum);
15413   if (friend_p)
15414     *friend_p = cp_parser_friend_p (&decl_specifiers);
15415
15416   /* There are no template typedefs.  */
15417   if (decl_specifiers.specs[(int) ds_typedef])
15418     {
15419       error ("template declaration of %qs", "typedef");
15420       decl = error_mark_node;
15421     }
15422
15423   /* Gather up the access checks that occurred the
15424      decl-specifier-seq.  */
15425   stop_deferring_access_checks ();
15426
15427   /* Check for the declaration of a template class.  */
15428   if (declares_class_or_enum)
15429     {
15430       if (cp_parser_declares_only_class_p (parser))
15431         {
15432           decl = shadow_tag (&decl_specifiers);
15433
15434           /* In this case:
15435
15436                struct C {
15437                  friend template <typename T> struct A<T>::B;
15438                };
15439
15440              A<T>::B will be represented by a TYPENAME_TYPE, and
15441              therefore not recognized by shadow_tag.  */
15442           if (friend_p && *friend_p
15443               && !decl
15444               && decl_specifiers.type
15445               && TYPE_P (decl_specifiers.type))
15446             decl = decl_specifiers.type;
15447
15448           if (decl && decl != error_mark_node)
15449             decl = TYPE_NAME (decl);
15450           else
15451             decl = error_mark_node;
15452         }
15453     }
15454   /* If it's not a template class, try for a template function.  If
15455      the next token is a `;', then this declaration does not declare
15456      anything.  But, if there were errors in the decl-specifiers, then
15457      the error might well have come from an attempted class-specifier.
15458      In that case, there's no need to warn about a missing declarator.  */
15459   if (!decl
15460       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15461           || decl_specifiers.type != error_mark_node))
15462     decl = cp_parser_init_declarator (parser,
15463                                       &decl_specifiers,
15464                                       /*function_definition_allowed_p=*/true,
15465                                       member_p,
15466                                       declares_class_or_enum,
15467                                       &function_definition_p);
15468
15469   pop_deferring_access_checks ();
15470
15471   /* Clear any current qualification; whatever comes next is the start
15472      of something new.  */
15473   parser->scope = NULL_TREE;
15474   parser->qualifying_scope = NULL_TREE;
15475   parser->object_scope = NULL_TREE;
15476   /* Look for a trailing `;' after the declaration.  */
15477   if (!function_definition_p
15478       && (decl == error_mark_node
15479           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15480     cp_parser_skip_to_end_of_block_or_statement (parser);
15481
15482   return decl;
15483 }
15484
15485 /* Parse a cast-expression that is not the operand of a unary "&".  */
15486
15487 static tree
15488 cp_parser_simple_cast_expression (cp_parser *parser)
15489 {
15490   return cp_parser_cast_expression (parser, /*address_p=*/false,
15491                                     /*cast_p=*/false);
15492 }
15493
15494 /* Parse a functional cast to TYPE.  Returns an expression
15495    representing the cast.  */
15496
15497 static tree
15498 cp_parser_functional_cast (cp_parser* parser, tree type)
15499 {
15500   tree expression_list;
15501   tree cast;
15502
15503   expression_list
15504     = cp_parser_parenthesized_expression_list (parser, false,
15505                                                /*cast_p=*/true,
15506                                                /*non_constant_p=*/NULL);
15507
15508   cast = build_functional_cast (type, expression_list);
15509   /* [expr.const]/1: In an integral constant expression "only type
15510      conversions to integral or enumeration type can be used".  */
15511   if (TREE_CODE (type) == TYPE_DECL)
15512     type = TREE_TYPE (type);
15513   if (cast != error_mark_node && !dependent_type_p (type)
15514       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15515     {
15516       if (cp_parser_non_integral_constant_expression
15517           (parser, "a call to a constructor"))
15518         return error_mark_node;
15519     }
15520   return cast;
15521 }
15522
15523 /* Save the tokens that make up the body of a member function defined
15524    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15525    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15526    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15527    for the member function.  */
15528
15529 static tree
15530 cp_parser_save_member_function_body (cp_parser* parser,
15531                                      cp_decl_specifier_seq *decl_specifiers,
15532                                      cp_declarator *declarator,
15533                                      tree attributes)
15534 {
15535   cp_token *first;
15536   cp_token *last;
15537   tree fn;
15538
15539   /* Create the function-declaration.  */
15540   fn = start_method (decl_specifiers, declarator, attributes);
15541   /* If something went badly wrong, bail out now.  */
15542   if (fn == error_mark_node)
15543     {
15544       /* If there's a function-body, skip it.  */
15545       if (cp_parser_token_starts_function_definition_p
15546           (cp_lexer_peek_token (parser->lexer)))
15547         cp_parser_skip_to_end_of_block_or_statement (parser);
15548       return error_mark_node;
15549     }
15550
15551   /* Remember it, if there default args to post process.  */
15552   cp_parser_save_default_args (parser, fn);
15553
15554   /* Save away the tokens that make up the body of the
15555      function.  */
15556   first = parser->lexer->next_token;
15557   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15558   /* Handle function try blocks.  */
15559   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15560     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15561   last = parser->lexer->next_token;
15562
15563   /* Save away the inline definition; we will process it when the
15564      class is complete.  */
15565   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15566   DECL_PENDING_INLINE_P (fn) = 1;
15567
15568   /* We need to know that this was defined in the class, so that
15569      friend templates are handled correctly.  */
15570   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15571
15572   /* We're done with the inline definition.  */
15573   finish_method (fn);
15574
15575   /* Add FN to the queue of functions to be parsed later.  */
15576   TREE_VALUE (parser->unparsed_functions_queues)
15577     = tree_cons (NULL_TREE, fn,
15578                  TREE_VALUE (parser->unparsed_functions_queues));
15579
15580   return fn;
15581 }
15582
15583 /* Parse a template-argument-list, as well as the trailing ">" (but
15584    not the opening ">").  See cp_parser_template_argument_list for the
15585    return value.  */
15586
15587 static tree
15588 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15589 {
15590   tree arguments;
15591   tree saved_scope;
15592   tree saved_qualifying_scope;
15593   tree saved_object_scope;
15594   bool saved_greater_than_is_operator_p;
15595   bool saved_skip_evaluation;
15596
15597   /* [temp.names]
15598
15599      When parsing a template-id, the first non-nested `>' is taken as
15600      the end of the template-argument-list rather than a greater-than
15601      operator.  */
15602   saved_greater_than_is_operator_p
15603     = parser->greater_than_is_operator_p;
15604   parser->greater_than_is_operator_p = false;
15605   /* Parsing the argument list may modify SCOPE, so we save it
15606      here.  */
15607   saved_scope = parser->scope;
15608   saved_qualifying_scope = parser->qualifying_scope;
15609   saved_object_scope = parser->object_scope;
15610   /* We need to evaluate the template arguments, even though this
15611      template-id may be nested within a "sizeof".  */
15612   saved_skip_evaluation = skip_evaluation;
15613   skip_evaluation = false;
15614   /* Parse the template-argument-list itself.  */
15615   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15616     arguments = NULL_TREE;
15617   else
15618     arguments = cp_parser_template_argument_list (parser);
15619   /* Look for the `>' that ends the template-argument-list. If we find
15620      a '>>' instead, it's probably just a typo.  */
15621   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15622     {
15623       if (!saved_greater_than_is_operator_p)
15624         {
15625           /* If we're in a nested template argument list, the '>>' has
15626             to be a typo for '> >'. We emit the error message, but we
15627             continue parsing and we push a '>' as next token, so that
15628             the argument list will be parsed correctly.  Note that the
15629             global source location is still on the token before the
15630             '>>', so we need to say explicitly where we want it.  */
15631           cp_token *token = cp_lexer_peek_token (parser->lexer);
15632           error ("%H%<>>%> should be %<> >%> "
15633                  "within a nested template argument list",
15634                  &token->location);
15635
15636           /* ??? Proper recovery should terminate two levels of
15637              template argument list here.  */
15638           token->type = CPP_GREATER;
15639         }
15640       else
15641         {
15642           /* If this is not a nested template argument list, the '>>'
15643             is a typo for '>'. Emit an error message and continue.
15644             Same deal about the token location, but here we can get it
15645             right by consuming the '>>' before issuing the diagnostic.  */
15646           cp_lexer_consume_token (parser->lexer);
15647           error ("spurious %<>>%>, use %<>%> to terminate "
15648                  "a template argument list");
15649         }
15650     }
15651   else
15652     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15653   /* The `>' token might be a greater-than operator again now.  */
15654   parser->greater_than_is_operator_p
15655     = saved_greater_than_is_operator_p;
15656   /* Restore the SAVED_SCOPE.  */
15657   parser->scope = saved_scope;
15658   parser->qualifying_scope = saved_qualifying_scope;
15659   parser->object_scope = saved_object_scope;
15660   skip_evaluation = saved_skip_evaluation;
15661
15662   return arguments;
15663 }
15664
15665 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15666    arguments, or the body of the function have not yet been parsed,
15667    parse them now.  */
15668
15669 static void
15670 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15671 {
15672   /* If this member is a template, get the underlying
15673      FUNCTION_DECL.  */
15674   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15675     member_function = DECL_TEMPLATE_RESULT (member_function);
15676
15677   /* There should not be any class definitions in progress at this
15678      point; the bodies of members are only parsed outside of all class
15679      definitions.  */
15680   gcc_assert (parser->num_classes_being_defined == 0);
15681   /* While we're parsing the member functions we might encounter more
15682      classes.  We want to handle them right away, but we don't want
15683      them getting mixed up with functions that are currently in the
15684      queue.  */
15685   parser->unparsed_functions_queues
15686     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15687
15688   /* Make sure that any template parameters are in scope.  */
15689   maybe_begin_member_template_processing (member_function);
15690
15691   /* If the body of the function has not yet been parsed, parse it
15692      now.  */
15693   if (DECL_PENDING_INLINE_P (member_function))
15694     {
15695       tree function_scope;
15696       cp_token_cache *tokens;
15697
15698       /* The function is no longer pending; we are processing it.  */
15699       tokens = DECL_PENDING_INLINE_INFO (member_function);
15700       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15701       DECL_PENDING_INLINE_P (member_function) = 0;
15702
15703       /* If this is a local class, enter the scope of the containing
15704          function.  */
15705       function_scope = current_function_decl;
15706       if (function_scope)
15707         push_function_context_to (function_scope);
15708
15709
15710       /* Push the body of the function onto the lexer stack.  */
15711       cp_parser_push_lexer_for_tokens (parser, tokens);
15712
15713       /* Let the front end know that we going to be defining this
15714          function.  */
15715       start_preparsed_function (member_function, NULL_TREE,
15716                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15717
15718       /* Don't do access checking if it is a templated function.  */
15719       if (processing_template_decl)
15720         push_deferring_access_checks (dk_no_check);
15721
15722       /* Now, parse the body of the function.  */
15723       cp_parser_function_definition_after_declarator (parser,
15724                                                       /*inline_p=*/true);
15725
15726       if (processing_template_decl)
15727         pop_deferring_access_checks ();
15728
15729       /* Leave the scope of the containing function.  */
15730       if (function_scope)
15731         pop_function_context_from (function_scope);
15732       cp_parser_pop_lexer (parser);
15733     }
15734
15735   /* Remove any template parameters from the symbol table.  */
15736   maybe_end_member_template_processing ();
15737
15738   /* Restore the queue.  */
15739   parser->unparsed_functions_queues
15740     = TREE_CHAIN (parser->unparsed_functions_queues);
15741 }
15742
15743 /* If DECL contains any default args, remember it on the unparsed
15744    functions queue.  */
15745
15746 static void
15747 cp_parser_save_default_args (cp_parser* parser, tree decl)
15748 {
15749   tree probe;
15750
15751   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15752        probe;
15753        probe = TREE_CHAIN (probe))
15754     if (TREE_PURPOSE (probe))
15755       {
15756         TREE_PURPOSE (parser->unparsed_functions_queues)
15757           = tree_cons (current_class_type, decl,
15758                        TREE_PURPOSE (parser->unparsed_functions_queues));
15759         break;
15760       }
15761   return;
15762 }
15763
15764 /* FN is a FUNCTION_DECL which may contains a parameter with an
15765    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15766    assumes that the current scope is the scope in which the default
15767    argument should be processed.  */
15768
15769 static void
15770 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15771 {
15772   bool saved_local_variables_forbidden_p;
15773   tree parm;
15774
15775   /* While we're parsing the default args, we might (due to the
15776      statement expression extension) encounter more classes.  We want
15777      to handle them right away, but we don't want them getting mixed
15778      up with default args that are currently in the queue.  */
15779   parser->unparsed_functions_queues
15780     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15781
15782   /* Local variable names (and the `this' keyword) may not appear
15783      in a default argument.  */
15784   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15785   parser->local_variables_forbidden_p = true;
15786
15787   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15788        parm;
15789        parm = TREE_CHAIN (parm))
15790     {
15791       cp_token_cache *tokens;
15792       tree default_arg = TREE_PURPOSE (parm);
15793       tree parsed_arg;
15794       VEC(tree,gc) *insts;
15795       tree copy;
15796       unsigned ix;
15797
15798       if (!default_arg)
15799         continue;
15800
15801       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15802         /* This can happen for a friend declaration for a function
15803            already declared with default arguments.  */
15804         continue;
15805
15806        /* Push the saved tokens for the default argument onto the parser's
15807           lexer stack.  */
15808       tokens = DEFARG_TOKENS (default_arg);
15809       cp_parser_push_lexer_for_tokens (parser, tokens);
15810
15811       /* Parse the assignment-expression.  */
15812       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15813
15814       if (!processing_template_decl)
15815         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15816       
15817       TREE_PURPOSE (parm) = parsed_arg;
15818
15819       /* Update any instantiations we've already created.  */
15820       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15821            VEC_iterate (tree, insts, ix, copy); ix++)
15822         TREE_PURPOSE (copy) = parsed_arg;
15823
15824       /* If the token stream has not been completely used up, then
15825          there was extra junk after the end of the default
15826          argument.  */
15827       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15828         cp_parser_error (parser, "expected %<,%>");
15829
15830       /* Revert to the main lexer.  */
15831       cp_parser_pop_lexer (parser);
15832     }
15833
15834   /* Restore the state of local_variables_forbidden_p.  */
15835   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15836
15837   /* Restore the queue.  */
15838   parser->unparsed_functions_queues
15839     = TREE_CHAIN (parser->unparsed_functions_queues);
15840 }
15841
15842 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15843    either a TYPE or an expression, depending on the form of the
15844    input.  The KEYWORD indicates which kind of expression we have
15845    encountered.  */
15846
15847 static tree
15848 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15849 {
15850   static const char *format;
15851   tree expr = NULL_TREE;
15852   const char *saved_message;
15853   bool saved_integral_constant_expression_p;
15854   bool saved_non_integral_constant_expression_p;
15855
15856   /* Initialize FORMAT the first time we get here.  */
15857   if (!format)
15858     format = "types may not be defined in '%s' expressions";
15859
15860   /* Types cannot be defined in a `sizeof' expression.  Save away the
15861      old message.  */
15862   saved_message = parser->type_definition_forbidden_message;
15863   /* And create the new one.  */
15864   parser->type_definition_forbidden_message
15865     = XNEWVEC (const char, strlen (format)
15866                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15867                + 1 /* `\0' */);
15868   sprintf ((char *) parser->type_definition_forbidden_message,
15869            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15870
15871   /* The restrictions on constant-expressions do not apply inside
15872      sizeof expressions.  */
15873   saved_integral_constant_expression_p
15874     = parser->integral_constant_expression_p;
15875   saved_non_integral_constant_expression_p
15876     = parser->non_integral_constant_expression_p;
15877   parser->integral_constant_expression_p = false;
15878
15879   /* Do not actually evaluate the expression.  */
15880   ++skip_evaluation;
15881   /* If it's a `(', then we might be looking at the type-id
15882      construction.  */
15883   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15884     {
15885       tree type;
15886       bool saved_in_type_id_in_expr_p;
15887
15888       /* We can't be sure yet whether we're looking at a type-id or an
15889          expression.  */
15890       cp_parser_parse_tentatively (parser);
15891       /* Consume the `('.  */
15892       cp_lexer_consume_token (parser->lexer);
15893       /* Parse the type-id.  */
15894       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15895       parser->in_type_id_in_expr_p = true;
15896       type = cp_parser_type_id (parser);
15897       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15898       /* Now, look for the trailing `)'.  */
15899       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15900       /* If all went well, then we're done.  */
15901       if (cp_parser_parse_definitely (parser))
15902         {
15903           cp_decl_specifier_seq decl_specs;
15904
15905           /* Build a trivial decl-specifier-seq.  */
15906           clear_decl_specs (&decl_specs);
15907           decl_specs.type = type;
15908
15909           /* Call grokdeclarator to figure out what type this is.  */
15910           expr = grokdeclarator (NULL,
15911                                  &decl_specs,
15912                                  TYPENAME,
15913                                  /*initialized=*/0,
15914                                  /*attrlist=*/NULL);
15915         }
15916     }
15917
15918   /* If the type-id production did not work out, then we must be
15919      looking at the unary-expression production.  */
15920   if (!expr)
15921     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15922                                        /*cast_p=*/false);
15923   /* Go back to evaluating expressions.  */
15924   --skip_evaluation;
15925
15926   /* Free the message we created.  */
15927   free ((char *) parser->type_definition_forbidden_message);
15928   /* And restore the old one.  */
15929   parser->type_definition_forbidden_message = saved_message;
15930   parser->integral_constant_expression_p
15931     = saved_integral_constant_expression_p;
15932   parser->non_integral_constant_expression_p
15933     = saved_non_integral_constant_expression_p;
15934
15935   return expr;
15936 }
15937
15938 /* If the current declaration has no declarator, return true.  */
15939
15940 static bool
15941 cp_parser_declares_only_class_p (cp_parser *parser)
15942 {
15943   /* If the next token is a `;' or a `,' then there is no
15944      declarator.  */
15945   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15946           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15947 }
15948
15949 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15950
15951 static void
15952 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15953                              cp_storage_class storage_class)
15954 {
15955   if (decl_specs->storage_class != sc_none)
15956     decl_specs->multiple_storage_classes_p = true;
15957   else
15958     decl_specs->storage_class = storage_class;
15959 }
15960
15961 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15962    is true, the type is a user-defined type; otherwise it is a
15963    built-in type specified by a keyword.  */
15964
15965 static void
15966 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15967                               tree type_spec,
15968                               bool user_defined_p)
15969 {
15970   decl_specs->any_specifiers_p = true;
15971
15972   /* If the user tries to redeclare bool or wchar_t (with, for
15973      example, in "typedef int wchar_t;") we remember that this is what
15974      happened.  In system headers, we ignore these declarations so
15975      that G++ can work with system headers that are not C++-safe.  */
15976   if (decl_specs->specs[(int) ds_typedef]
15977       && !user_defined_p
15978       && (type_spec == boolean_type_node
15979           || type_spec == wchar_type_node)
15980       && (decl_specs->type
15981           || decl_specs->specs[(int) ds_long]
15982           || decl_specs->specs[(int) ds_short]
15983           || decl_specs->specs[(int) ds_unsigned]
15984           || decl_specs->specs[(int) ds_signed]))
15985     {
15986       decl_specs->redefined_builtin_type = type_spec;
15987       if (!decl_specs->type)
15988         {
15989           decl_specs->type = type_spec;
15990           decl_specs->user_defined_type_p = false;
15991         }
15992     }
15993   else if (decl_specs->type)
15994     decl_specs->multiple_types_p = true;
15995   else
15996     {
15997       decl_specs->type = type_spec;
15998       decl_specs->user_defined_type_p = user_defined_p;
15999       decl_specs->redefined_builtin_type = NULL_TREE;
16000     }
16001 }
16002
16003 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16004    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16005
16006 static bool
16007 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16008 {
16009   return decl_specifiers->specs[(int) ds_friend] != 0;
16010 }
16011
16012 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16013    issue an error message indicating that TOKEN_DESC was expected.
16014
16015    Returns the token consumed, if the token had the appropriate type.
16016    Otherwise, returns NULL.  */
16017
16018 static cp_token *
16019 cp_parser_require (cp_parser* parser,
16020                    enum cpp_ttype type,
16021                    const char* token_desc)
16022 {
16023   if (cp_lexer_next_token_is (parser->lexer, type))
16024     return cp_lexer_consume_token (parser->lexer);
16025   else
16026     {
16027       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16028       if (!cp_parser_simulate_error (parser))
16029         {
16030           char *message = concat ("expected ", token_desc, NULL);
16031           cp_parser_error (parser, message);
16032           free (message);
16033         }
16034       return NULL;
16035     }
16036 }
16037
16038 /* Like cp_parser_require, except that tokens will be skipped until
16039    the desired token is found.  An error message is still produced if
16040    the next token is not as expected.  */
16041
16042 static void
16043 cp_parser_skip_until_found (cp_parser* parser,
16044                             enum cpp_ttype type,
16045                             const char* token_desc)
16046 {
16047   cp_token *token;
16048   unsigned nesting_depth = 0;
16049
16050   if (cp_parser_require (parser, type, token_desc))
16051     return;
16052
16053   /* Skip tokens until the desired token is found.  */
16054   while (true)
16055     {
16056       /* Peek at the next token.  */
16057       token = cp_lexer_peek_token (parser->lexer);
16058
16059       /* If we've reached the token we want, consume it and stop.  */
16060       if (token->type == type && !nesting_depth)
16061         {
16062           cp_lexer_consume_token (parser->lexer);
16063           return;
16064         }
16065
16066       switch (token->type)
16067         {
16068         case CPP_EOF:
16069         case CPP_PRAGMA_EOL:
16070           /* If we've run out of tokens, stop.  */
16071           return;
16072
16073         case CPP_OPEN_BRACE:
16074         case CPP_OPEN_PAREN:
16075         case CPP_OPEN_SQUARE:
16076           ++nesting_depth;
16077           break;
16078
16079         case CPP_CLOSE_BRACE:
16080         case CPP_CLOSE_PAREN:
16081         case CPP_CLOSE_SQUARE:
16082           if (nesting_depth-- == 0)
16083             return;
16084           break;
16085
16086         default:
16087           break;
16088         }
16089
16090       /* Consume this token.  */
16091       cp_lexer_consume_token (parser->lexer);
16092     }
16093 }
16094
16095 /* If the next token is the indicated keyword, consume it.  Otherwise,
16096    issue an error message indicating that TOKEN_DESC was expected.
16097
16098    Returns the token consumed, if the token had the appropriate type.
16099    Otherwise, returns NULL.  */
16100
16101 static cp_token *
16102 cp_parser_require_keyword (cp_parser* parser,
16103                            enum rid keyword,
16104                            const char* token_desc)
16105 {
16106   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16107
16108   if (token && token->keyword != keyword)
16109     {
16110       dyn_string_t error_msg;
16111
16112       /* Format the error message.  */
16113       error_msg = dyn_string_new (0);
16114       dyn_string_append_cstr (error_msg, "expected ");
16115       dyn_string_append_cstr (error_msg, token_desc);
16116       cp_parser_error (parser, error_msg->s);
16117       dyn_string_delete (error_msg);
16118       return NULL;
16119     }
16120
16121   return token;
16122 }
16123
16124 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16125    function-definition.  */
16126
16127 static bool
16128 cp_parser_token_starts_function_definition_p (cp_token* token)
16129 {
16130   return (/* An ordinary function-body begins with an `{'.  */
16131           token->type == CPP_OPEN_BRACE
16132           /* A ctor-initializer begins with a `:'.  */
16133           || token->type == CPP_COLON
16134           /* A function-try-block begins with `try'.  */
16135           || token->keyword == RID_TRY
16136           /* The named return value extension begins with `return'.  */
16137           || token->keyword == RID_RETURN);
16138 }
16139
16140 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16141    definition.  */
16142
16143 static bool
16144 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16145 {
16146   cp_token *token;
16147
16148   token = cp_lexer_peek_token (parser->lexer);
16149   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16150 }
16151
16152 /* Returns TRUE iff the next token is the "," or ">" ending a
16153    template-argument.  */
16154
16155 static bool
16156 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16157 {
16158   cp_token *token;
16159
16160   token = cp_lexer_peek_token (parser->lexer);
16161   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16162 }
16163
16164 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16165    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16166
16167 static bool
16168 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16169                                                      size_t n)
16170 {
16171   cp_token *token;
16172
16173   token = cp_lexer_peek_nth_token (parser->lexer, n);
16174   if (token->type == CPP_LESS)
16175     return true;
16176   /* Check for the sequence `<::' in the original code. It would be lexed as
16177      `[:', where `[' is a digraph, and there is no whitespace before
16178      `:'.  */
16179   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16180     {
16181       cp_token *token2;
16182       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16183       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16184         return true;
16185     }
16186   return false;
16187 }
16188
16189 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16190    or none_type otherwise.  */
16191
16192 static enum tag_types
16193 cp_parser_token_is_class_key (cp_token* token)
16194 {
16195   switch (token->keyword)
16196     {
16197     case RID_CLASS:
16198       return class_type;
16199     case RID_STRUCT:
16200       return record_type;
16201     case RID_UNION:
16202       return union_type;
16203
16204     default:
16205       return none_type;
16206     }
16207 }
16208
16209 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16210
16211 static void
16212 cp_parser_check_class_key (enum tag_types class_key, tree type)
16213 {
16214   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16215     pedwarn ("%qs tag used in naming %q#T",
16216             class_key == union_type ? "union"
16217              : class_key == record_type ? "struct" : "class",
16218              type);
16219 }
16220
16221 /* Issue an error message if DECL is redeclared with different
16222    access than its original declaration [class.access.spec/3].
16223    This applies to nested classes and nested class templates.
16224    [class.mem/1].  */
16225
16226 static void
16227 cp_parser_check_access_in_redeclaration (tree decl)
16228 {
16229   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16230     return;
16231
16232   if ((TREE_PRIVATE (decl)
16233        != (current_access_specifier == access_private_node))
16234       || (TREE_PROTECTED (decl)
16235           != (current_access_specifier == access_protected_node)))
16236     error ("%qD redeclared with different access", decl);
16237 }
16238
16239 /* Look for the `template' keyword, as a syntactic disambiguator.
16240    Return TRUE iff it is present, in which case it will be
16241    consumed.  */
16242
16243 static bool
16244 cp_parser_optional_template_keyword (cp_parser *parser)
16245 {
16246   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16247     {
16248       /* The `template' keyword can only be used within templates;
16249          outside templates the parser can always figure out what is a
16250          template and what is not.  */
16251       if (!processing_template_decl)
16252         {
16253           error ("%<template%> (as a disambiguator) is only allowed "
16254                  "within templates");
16255           /* If this part of the token stream is rescanned, the same
16256              error message would be generated.  So, we purge the token
16257              from the stream.  */
16258           cp_lexer_purge_token (parser->lexer);
16259           return false;
16260         }
16261       else
16262         {
16263           /* Consume the `template' keyword.  */
16264           cp_lexer_consume_token (parser->lexer);
16265           return true;
16266         }
16267     }
16268
16269   return false;
16270 }
16271
16272 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16273    set PARSER->SCOPE, and perform other related actions.  */
16274
16275 static void
16276 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16277 {
16278   tree value;
16279   tree check;
16280
16281   /* Get the stored value.  */
16282   value = cp_lexer_consume_token (parser->lexer)->value;
16283   /* Perform any access checks that were deferred.  */
16284   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16285     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16286   /* Set the scope from the stored value.  */
16287   parser->scope = TREE_VALUE (value);
16288   parser->qualifying_scope = TREE_TYPE (value);
16289   parser->object_scope = NULL_TREE;
16290 }
16291
16292 /* Consume tokens up through a non-nested END token.  */
16293
16294 static void
16295 cp_parser_cache_group (cp_parser *parser,
16296                        enum cpp_ttype end,
16297                        unsigned depth)
16298 {
16299   while (true)
16300     {
16301       cp_token *token;
16302
16303       /* Abort a parenthesized expression if we encounter a brace.  */
16304       if ((end == CPP_CLOSE_PAREN || depth == 0)
16305           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16306         return;
16307       /* If we've reached the end of the file, stop.  */
16308       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16309           || (end != CPP_PRAGMA_EOL
16310               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16311         return;
16312       /* Consume the next token.  */
16313       token = cp_lexer_consume_token (parser->lexer);
16314       /* See if it starts a new group.  */
16315       if (token->type == CPP_OPEN_BRACE)
16316         {
16317           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16318           if (depth == 0)
16319             return;
16320         }
16321       else if (token->type == CPP_OPEN_PAREN)
16322         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16323       else if (token->type == CPP_PRAGMA)
16324         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16325       else if (token->type == end)
16326         return;
16327     }
16328 }
16329
16330 /* Begin parsing tentatively.  We always save tokens while parsing
16331    tentatively so that if the tentative parsing fails we can restore the
16332    tokens.  */
16333
16334 static void
16335 cp_parser_parse_tentatively (cp_parser* parser)
16336 {
16337   /* Enter a new parsing context.  */
16338   parser->context = cp_parser_context_new (parser->context);
16339   /* Begin saving tokens.  */
16340   cp_lexer_save_tokens (parser->lexer);
16341   /* In order to avoid repetitive access control error messages,
16342      access checks are queued up until we are no longer parsing
16343      tentatively.  */
16344   push_deferring_access_checks (dk_deferred);
16345 }
16346
16347 /* Commit to the currently active tentative parse.  */
16348
16349 static void
16350 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16351 {
16352   cp_parser_context *context;
16353   cp_lexer *lexer;
16354
16355   /* Mark all of the levels as committed.  */
16356   lexer = parser->lexer;
16357   for (context = parser->context; context->next; context = context->next)
16358     {
16359       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16360         break;
16361       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16362       while (!cp_lexer_saving_tokens (lexer))
16363         lexer = lexer->next;
16364       cp_lexer_commit_tokens (lexer);
16365     }
16366 }
16367
16368 /* Abort the currently active tentative parse.  All consumed tokens
16369    will be rolled back, and no diagnostics will be issued.  */
16370
16371 static void
16372 cp_parser_abort_tentative_parse (cp_parser* parser)
16373 {
16374   cp_parser_simulate_error (parser);
16375   /* Now, pretend that we want to see if the construct was
16376      successfully parsed.  */
16377   cp_parser_parse_definitely (parser);
16378 }
16379
16380 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16381    token stream.  Otherwise, commit to the tokens we have consumed.
16382    Returns true if no error occurred; false otherwise.  */
16383
16384 static bool
16385 cp_parser_parse_definitely (cp_parser* parser)
16386 {
16387   bool error_occurred;
16388   cp_parser_context *context;
16389
16390   /* Remember whether or not an error occurred, since we are about to
16391      destroy that information.  */
16392   error_occurred = cp_parser_error_occurred (parser);
16393   /* Remove the topmost context from the stack.  */
16394   context = parser->context;
16395   parser->context = context->next;
16396   /* If no parse errors occurred, commit to the tentative parse.  */
16397   if (!error_occurred)
16398     {
16399       /* Commit to the tokens read tentatively, unless that was
16400          already done.  */
16401       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16402         cp_lexer_commit_tokens (parser->lexer);
16403
16404       pop_to_parent_deferring_access_checks ();
16405     }
16406   /* Otherwise, if errors occurred, roll back our state so that things
16407      are just as they were before we began the tentative parse.  */
16408   else
16409     {
16410       cp_lexer_rollback_tokens (parser->lexer);
16411       pop_deferring_access_checks ();
16412     }
16413   /* Add the context to the front of the free list.  */
16414   context->next = cp_parser_context_free_list;
16415   cp_parser_context_free_list = context;
16416
16417   return !error_occurred;
16418 }
16419
16420 /* Returns true if we are parsing tentatively and are not committed to
16421    this tentative parse.  */
16422
16423 static bool
16424 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16425 {
16426   return (cp_parser_parsing_tentatively (parser)
16427           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16428 }
16429
16430 /* Returns nonzero iff an error has occurred during the most recent
16431    tentative parse.  */
16432
16433 static bool
16434 cp_parser_error_occurred (cp_parser* parser)
16435 {
16436   return (cp_parser_parsing_tentatively (parser)
16437           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16438 }
16439
16440 /* Returns nonzero if GNU extensions are allowed.  */
16441
16442 static bool
16443 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16444 {
16445   return parser->allow_gnu_extensions_p;
16446 }
16447 \f
16448 /* Objective-C++ Productions */
16449
16450
16451 /* Parse an Objective-C expression, which feeds into a primary-expression
16452    above.
16453
16454    objc-expression:
16455      objc-message-expression
16456      objc-string-literal
16457      objc-encode-expression
16458      objc-protocol-expression
16459      objc-selector-expression
16460
16461   Returns a tree representation of the expression.  */
16462
16463 static tree
16464 cp_parser_objc_expression (cp_parser* parser)
16465 {
16466   /* Try to figure out what kind of declaration is present.  */
16467   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16468
16469   switch (kwd->type)
16470     {
16471     case CPP_OPEN_SQUARE:
16472       return cp_parser_objc_message_expression (parser);
16473
16474     case CPP_OBJC_STRING:
16475       kwd = cp_lexer_consume_token (parser->lexer);
16476       return objc_build_string_object (kwd->value);
16477
16478     case CPP_KEYWORD:
16479       switch (kwd->keyword)
16480         {
16481         case RID_AT_ENCODE:
16482           return cp_parser_objc_encode_expression (parser);
16483
16484         case RID_AT_PROTOCOL:
16485           return cp_parser_objc_protocol_expression (parser);
16486
16487         case RID_AT_SELECTOR:
16488           return cp_parser_objc_selector_expression (parser);
16489
16490         default:
16491           break;
16492         }
16493     default:
16494       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16495       cp_parser_skip_to_end_of_block_or_statement (parser);
16496     }
16497
16498   return error_mark_node;
16499 }
16500
16501 /* Parse an Objective-C message expression.
16502
16503    objc-message-expression:
16504      [ objc-message-receiver objc-message-args ]
16505
16506    Returns a representation of an Objective-C message.  */
16507
16508 static tree
16509 cp_parser_objc_message_expression (cp_parser* parser)
16510 {
16511   tree receiver, messageargs;
16512
16513   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16514   receiver = cp_parser_objc_message_receiver (parser);
16515   messageargs = cp_parser_objc_message_args (parser);
16516   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16517
16518   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16519 }
16520
16521 /* Parse an objc-message-receiver.
16522
16523    objc-message-receiver:
16524      expression
16525      simple-type-specifier
16526
16527   Returns a representation of the type or expression.  */
16528
16529 static tree
16530 cp_parser_objc_message_receiver (cp_parser* parser)
16531 {
16532   tree rcv;
16533
16534   /* An Objective-C message receiver may be either (1) a type
16535      or (2) an expression.  */
16536   cp_parser_parse_tentatively (parser);
16537   rcv = cp_parser_expression (parser, false);
16538
16539   if (cp_parser_parse_definitely (parser))
16540     return rcv;
16541
16542   rcv = cp_parser_simple_type_specifier (parser,
16543                                          /*decl_specs=*/NULL,
16544                                          CP_PARSER_FLAGS_NONE);
16545
16546   return objc_get_class_reference (rcv);
16547 }
16548
16549 /* Parse the arguments and selectors comprising an Objective-C message.
16550
16551    objc-message-args:
16552      objc-selector
16553      objc-selector-args
16554      objc-selector-args , objc-comma-args
16555
16556    objc-selector-args:
16557      objc-selector [opt] : assignment-expression
16558      objc-selector-args objc-selector [opt] : assignment-expression
16559
16560    objc-comma-args:
16561      assignment-expression
16562      objc-comma-args , assignment-expression
16563
16564    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16565    selector arguments and TREE_VALUE containing a list of comma
16566    arguments.  */
16567
16568 static tree
16569 cp_parser_objc_message_args (cp_parser* parser)
16570 {
16571   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16572   bool maybe_unary_selector_p = true;
16573   cp_token *token = cp_lexer_peek_token (parser->lexer);
16574
16575   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16576     {
16577       tree selector = NULL_TREE, arg;
16578
16579       if (token->type != CPP_COLON)
16580         selector = cp_parser_objc_selector (parser);
16581
16582       /* Detect if we have a unary selector.  */
16583       if (maybe_unary_selector_p
16584           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16585         return build_tree_list (selector, NULL_TREE);
16586
16587       maybe_unary_selector_p = false;
16588       cp_parser_require (parser, CPP_COLON, "`:'");
16589       arg = cp_parser_assignment_expression (parser, false);
16590
16591       sel_args
16592         = chainon (sel_args,
16593                    build_tree_list (selector, arg));
16594
16595       token = cp_lexer_peek_token (parser->lexer);
16596     }
16597
16598   /* Handle non-selector arguments, if any. */
16599   while (token->type == CPP_COMMA)
16600     {
16601       tree arg;
16602
16603       cp_lexer_consume_token (parser->lexer);
16604       arg = cp_parser_assignment_expression (parser, false);
16605
16606       addl_args
16607         = chainon (addl_args,
16608                    build_tree_list (NULL_TREE, arg));
16609
16610       token = cp_lexer_peek_token (parser->lexer);
16611     }
16612
16613   return build_tree_list (sel_args, addl_args);
16614 }
16615
16616 /* Parse an Objective-C encode expression.
16617
16618    objc-encode-expression:
16619      @encode objc-typename
16620
16621    Returns an encoded representation of the type argument.  */
16622
16623 static tree
16624 cp_parser_objc_encode_expression (cp_parser* parser)
16625 {
16626   tree type;
16627
16628   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16629   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16630   type = complete_type (cp_parser_type_id (parser));
16631   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16632
16633   if (!type)
16634     {
16635       error ("%<@encode%> must specify a type as an argument");
16636       return error_mark_node;
16637     }
16638
16639   return objc_build_encode_expr (type);
16640 }
16641
16642 /* Parse an Objective-C @defs expression.  */
16643
16644 static tree
16645 cp_parser_objc_defs_expression (cp_parser *parser)
16646 {
16647   tree name;
16648
16649   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16650   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16651   name = cp_parser_identifier (parser);
16652   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16653
16654   return objc_get_class_ivars (name);
16655 }
16656
16657 /* Parse an Objective-C protocol expression.
16658
16659   objc-protocol-expression:
16660     @protocol ( identifier )
16661
16662   Returns a representation of the protocol expression.  */
16663
16664 static tree
16665 cp_parser_objc_protocol_expression (cp_parser* parser)
16666 {
16667   tree proto;
16668
16669   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16670   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16671   proto = cp_parser_identifier (parser);
16672   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16673
16674   return objc_build_protocol_expr (proto);
16675 }
16676
16677 /* Parse an Objective-C selector expression.
16678
16679    objc-selector-expression:
16680      @selector ( objc-method-signature )
16681
16682    objc-method-signature:
16683      objc-selector
16684      objc-selector-seq
16685
16686    objc-selector-seq:
16687      objc-selector :
16688      objc-selector-seq objc-selector :
16689
16690   Returns a representation of the method selector.  */
16691
16692 static tree
16693 cp_parser_objc_selector_expression (cp_parser* parser)
16694 {
16695   tree sel_seq = NULL_TREE;
16696   bool maybe_unary_selector_p = true;
16697   cp_token *token;
16698
16699   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16700   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16701   token = cp_lexer_peek_token (parser->lexer);
16702
16703   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16704          || token->type == CPP_SCOPE)
16705     {
16706       tree selector = NULL_TREE;
16707
16708       if (token->type != CPP_COLON
16709           || token->type == CPP_SCOPE)
16710         selector = cp_parser_objc_selector (parser);
16711
16712       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16713           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16714         {
16715           /* Detect if we have a unary selector.  */
16716           if (maybe_unary_selector_p)
16717             {
16718               sel_seq = selector;
16719               goto finish_selector;
16720             }
16721           else
16722             {
16723               cp_parser_error (parser, "expected %<:%>");
16724             }
16725         }
16726       maybe_unary_selector_p = false;
16727       token = cp_lexer_consume_token (parser->lexer);
16728       
16729       if (token->type == CPP_SCOPE)
16730         {
16731           sel_seq
16732             = chainon (sel_seq,
16733                        build_tree_list (selector, NULL_TREE));
16734           sel_seq
16735             = chainon (sel_seq,
16736                        build_tree_list (NULL_TREE, NULL_TREE));
16737         }
16738       else
16739         sel_seq
16740           = chainon (sel_seq,
16741                      build_tree_list (selector, NULL_TREE));
16742
16743       token = cp_lexer_peek_token (parser->lexer);
16744     }
16745
16746  finish_selector:
16747   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16748
16749   return objc_build_selector_expr (sel_seq);
16750 }
16751
16752 /* Parse a list of identifiers.
16753
16754    objc-identifier-list:
16755      identifier
16756      objc-identifier-list , identifier
16757
16758    Returns a TREE_LIST of identifier nodes.  */
16759
16760 static tree
16761 cp_parser_objc_identifier_list (cp_parser* parser)
16762 {
16763   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16764   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16765
16766   while (sep->type == CPP_COMMA)
16767     {
16768       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16769       list = chainon (list,
16770                       build_tree_list (NULL_TREE,
16771                                        cp_parser_identifier (parser)));
16772       sep = cp_lexer_peek_token (parser->lexer);
16773     }
16774
16775   return list;
16776 }
16777
16778 /* Parse an Objective-C alias declaration.
16779
16780    objc-alias-declaration:
16781      @compatibility_alias identifier identifier ;
16782
16783    This function registers the alias mapping with the Objective-C front-end.
16784    It returns nothing.  */
16785
16786 static void
16787 cp_parser_objc_alias_declaration (cp_parser* parser)
16788 {
16789   tree alias, orig;
16790
16791   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16792   alias = cp_parser_identifier (parser);
16793   orig = cp_parser_identifier (parser);
16794   objc_declare_alias (alias, orig);
16795   cp_parser_consume_semicolon_at_end_of_statement (parser);
16796 }
16797
16798 /* Parse an Objective-C class forward-declaration.
16799
16800    objc-class-declaration:
16801      @class objc-identifier-list ;
16802
16803    The function registers the forward declarations with the Objective-C
16804    front-end.  It returns nothing.  */
16805
16806 static void
16807 cp_parser_objc_class_declaration (cp_parser* parser)
16808 {
16809   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16810   objc_declare_class (cp_parser_objc_identifier_list (parser));
16811   cp_parser_consume_semicolon_at_end_of_statement (parser);
16812 }
16813
16814 /* Parse a list of Objective-C protocol references.
16815
16816    objc-protocol-refs-opt:
16817      objc-protocol-refs [opt]
16818
16819    objc-protocol-refs:
16820      < objc-identifier-list >
16821
16822    Returns a TREE_LIST of identifiers, if any.  */
16823
16824 static tree
16825 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16826 {
16827   tree protorefs = NULL_TREE;
16828
16829   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16830     {
16831       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16832       protorefs = cp_parser_objc_identifier_list (parser);
16833       cp_parser_require (parser, CPP_GREATER, "`>'");
16834     }
16835
16836   return protorefs;
16837 }
16838
16839 /* Parse a Objective-C visibility specification.  */
16840
16841 static void
16842 cp_parser_objc_visibility_spec (cp_parser* parser)
16843 {
16844   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16845
16846   switch (vis->keyword)
16847     {
16848     case RID_AT_PRIVATE:
16849       objc_set_visibility (2);
16850       break;
16851     case RID_AT_PROTECTED:
16852       objc_set_visibility (0);
16853       break;
16854     case RID_AT_PUBLIC:
16855       objc_set_visibility (1);
16856       break;
16857     default:
16858       return;
16859     }
16860
16861   /* Eat '@private'/'@protected'/'@public'.  */
16862   cp_lexer_consume_token (parser->lexer);
16863 }
16864
16865 /* Parse an Objective-C method type.  */
16866
16867 static void
16868 cp_parser_objc_method_type (cp_parser* parser)
16869 {
16870   objc_set_method_type
16871    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16872     ? PLUS_EXPR
16873     : MINUS_EXPR);
16874 }
16875
16876 /* Parse an Objective-C protocol qualifier.  */
16877
16878 static tree
16879 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16880 {
16881   tree quals = NULL_TREE, node;
16882   cp_token *token = cp_lexer_peek_token (parser->lexer);
16883
16884   node = token->value;
16885
16886   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16887          && (node == ridpointers [(int) RID_IN]
16888              || node == ridpointers [(int) RID_OUT]
16889              || node == ridpointers [(int) RID_INOUT]
16890              || node == ridpointers [(int) RID_BYCOPY]
16891              || node == ridpointers [(int) RID_BYREF]
16892              || node == ridpointers [(int) RID_ONEWAY]))
16893     {
16894       quals = tree_cons (NULL_TREE, node, quals);
16895       cp_lexer_consume_token (parser->lexer);
16896       token = cp_lexer_peek_token (parser->lexer);
16897       node = token->value;
16898     }
16899
16900   return quals;
16901 }
16902
16903 /* Parse an Objective-C typename.  */
16904
16905 static tree
16906 cp_parser_objc_typename (cp_parser* parser)
16907 {
16908   tree typename = NULL_TREE;
16909
16910   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16911     {
16912       tree proto_quals, cp_type = NULL_TREE;
16913
16914       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16915       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16916
16917       /* An ObjC type name may consist of just protocol qualifiers, in which
16918          case the type shall default to 'id'.  */
16919       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16920         cp_type = cp_parser_type_id (parser);
16921
16922       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16923       typename = build_tree_list (proto_quals, cp_type);
16924     }
16925
16926   return typename;
16927 }
16928
16929 /* Check to see if TYPE refers to an Objective-C selector name.  */
16930
16931 static bool
16932 cp_parser_objc_selector_p (enum cpp_ttype type)
16933 {
16934   return (type == CPP_NAME || type == CPP_KEYWORD
16935           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16936           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16937           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16938           || type == CPP_XOR || type == CPP_XOR_EQ);
16939 }
16940
16941 /* Parse an Objective-C selector.  */
16942
16943 static tree
16944 cp_parser_objc_selector (cp_parser* parser)
16945 {
16946   cp_token *token = cp_lexer_consume_token (parser->lexer);
16947
16948   if (!cp_parser_objc_selector_p (token->type))
16949     {
16950       error ("invalid Objective-C++ selector name");
16951       return error_mark_node;
16952     }
16953
16954   /* C++ operator names are allowed to appear in ObjC selectors.  */
16955   switch (token->type)
16956     {
16957     case CPP_AND_AND: return get_identifier ("and");
16958     case CPP_AND_EQ: return get_identifier ("and_eq");
16959     case CPP_AND: return get_identifier ("bitand");
16960     case CPP_OR: return get_identifier ("bitor");
16961     case CPP_COMPL: return get_identifier ("compl");
16962     case CPP_NOT: return get_identifier ("not");
16963     case CPP_NOT_EQ: return get_identifier ("not_eq");
16964     case CPP_OR_OR: return get_identifier ("or");
16965     case CPP_OR_EQ: return get_identifier ("or_eq");
16966     case CPP_XOR: return get_identifier ("xor");
16967     case CPP_XOR_EQ: return get_identifier ("xor_eq");
16968     default: return token->value;
16969     }
16970 }
16971
16972 /* Parse an Objective-C params list.  */
16973
16974 static tree
16975 cp_parser_objc_method_keyword_params (cp_parser* parser)
16976 {
16977   tree params = NULL_TREE;
16978   bool maybe_unary_selector_p = true;
16979   cp_token *token = cp_lexer_peek_token (parser->lexer);
16980
16981   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16982     {
16983       tree selector = NULL_TREE, typename, identifier;
16984
16985       if (token->type != CPP_COLON)
16986         selector = cp_parser_objc_selector (parser);
16987
16988       /* Detect if we have a unary selector.  */
16989       if (maybe_unary_selector_p
16990           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16991         return selector;
16992
16993       maybe_unary_selector_p = false;
16994       cp_parser_require (parser, CPP_COLON, "`:'");
16995       typename = cp_parser_objc_typename (parser);
16996       identifier = cp_parser_identifier (parser);
16997
16998       params
16999         = chainon (params,
17000                    objc_build_keyword_decl (selector,
17001                                             typename,
17002                                             identifier));
17003
17004       token = cp_lexer_peek_token (parser->lexer);
17005     }
17006
17007   return params;
17008 }
17009
17010 /* Parse the non-keyword Objective-C params.  */
17011
17012 static tree
17013 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17014 {
17015   tree params = make_node (TREE_LIST);
17016   cp_token *token = cp_lexer_peek_token (parser->lexer);
17017   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17018
17019   while (token->type == CPP_COMMA)
17020     {
17021       cp_parameter_declarator *parmdecl;
17022       tree parm;
17023
17024       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17025       token = cp_lexer_peek_token (parser->lexer);
17026
17027       if (token->type == CPP_ELLIPSIS)
17028         {
17029           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17030           *ellipsisp = true;
17031           break;
17032         }
17033
17034       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17035       parm = grokdeclarator (parmdecl->declarator,
17036                              &parmdecl->decl_specifiers,
17037                              PARM, /*initialized=*/0,
17038                              /*attrlist=*/NULL);
17039
17040       chainon (params, build_tree_list (NULL_TREE, parm));
17041       token = cp_lexer_peek_token (parser->lexer);
17042     }
17043
17044   return params;
17045 }
17046
17047 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17048
17049 static void
17050 cp_parser_objc_interstitial_code (cp_parser* parser)
17051 {
17052   cp_token *token = cp_lexer_peek_token (parser->lexer);
17053
17054   /* If the next token is `extern' and the following token is a string
17055      literal, then we have a linkage specification.  */
17056   if (token->keyword == RID_EXTERN
17057       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17058     cp_parser_linkage_specification (parser);
17059   /* Handle #pragma, if any.  */
17060   else if (token->type == CPP_PRAGMA)
17061     cp_parser_pragma (parser, pragma_external);
17062   /* Allow stray semicolons.  */
17063   else if (token->type == CPP_SEMICOLON)
17064     cp_lexer_consume_token (parser->lexer);
17065   /* Finally, try to parse a block-declaration, or a function-definition.  */
17066   else
17067     cp_parser_block_declaration (parser, /*statement_p=*/false);
17068 }
17069
17070 /* Parse a method signature.  */
17071
17072 static tree
17073 cp_parser_objc_method_signature (cp_parser* parser)
17074 {
17075   tree rettype, kwdparms, optparms;
17076   bool ellipsis = false;
17077
17078   cp_parser_objc_method_type (parser);
17079   rettype = cp_parser_objc_typename (parser);
17080   kwdparms = cp_parser_objc_method_keyword_params (parser);
17081   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17082
17083   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17084 }
17085
17086 /* Pars an Objective-C method prototype list.  */
17087
17088 static void
17089 cp_parser_objc_method_prototype_list (cp_parser* parser)
17090 {
17091   cp_token *token = cp_lexer_peek_token (parser->lexer);
17092
17093   while (token->keyword != RID_AT_END)
17094     {
17095       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17096         {
17097           objc_add_method_declaration
17098            (cp_parser_objc_method_signature (parser));
17099           cp_parser_consume_semicolon_at_end_of_statement (parser);
17100         }
17101       else
17102         /* Allow for interspersed non-ObjC++ code.  */
17103         cp_parser_objc_interstitial_code (parser);
17104
17105       token = cp_lexer_peek_token (parser->lexer);
17106     }
17107
17108   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17109   objc_finish_interface ();
17110 }
17111
17112 /* Parse an Objective-C method definition list.  */
17113
17114 static void
17115 cp_parser_objc_method_definition_list (cp_parser* parser)
17116 {
17117   cp_token *token = cp_lexer_peek_token (parser->lexer);
17118
17119   while (token->keyword != RID_AT_END)
17120     {
17121       tree meth;
17122
17123       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17124         {
17125           push_deferring_access_checks (dk_deferred);
17126           objc_start_method_definition
17127            (cp_parser_objc_method_signature (parser));
17128
17129           /* For historical reasons, we accept an optional semicolon.  */
17130           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17131             cp_lexer_consume_token (parser->lexer);
17132
17133           perform_deferred_access_checks ();
17134           stop_deferring_access_checks ();
17135           meth = cp_parser_function_definition_after_declarator (parser,
17136                                                                  false);
17137           pop_deferring_access_checks ();
17138           objc_finish_method_definition (meth);
17139         }
17140       else
17141         /* Allow for interspersed non-ObjC++ code.  */
17142         cp_parser_objc_interstitial_code (parser);
17143
17144       token = cp_lexer_peek_token (parser->lexer);
17145     }
17146
17147   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17148   objc_finish_implementation ();
17149 }
17150
17151 /* Parse Objective-C ivars.  */
17152
17153 static void
17154 cp_parser_objc_class_ivars (cp_parser* parser)
17155 {
17156   cp_token *token = cp_lexer_peek_token (parser->lexer);
17157
17158   if (token->type != CPP_OPEN_BRACE)
17159     return;     /* No ivars specified.  */
17160
17161   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17162   token = cp_lexer_peek_token (parser->lexer);
17163
17164   while (token->type != CPP_CLOSE_BRACE)
17165     {
17166       cp_decl_specifier_seq declspecs;
17167       int decl_class_or_enum_p;
17168       tree prefix_attributes;
17169
17170       cp_parser_objc_visibility_spec (parser);
17171
17172       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17173         break;
17174
17175       cp_parser_decl_specifier_seq (parser,
17176                                     CP_PARSER_FLAGS_OPTIONAL,
17177                                     &declspecs,
17178                                     &decl_class_or_enum_p);
17179       prefix_attributes = declspecs.attributes;
17180       declspecs.attributes = NULL_TREE;
17181
17182       /* Keep going until we hit the `;' at the end of the
17183          declaration.  */
17184       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17185         {
17186           tree width = NULL_TREE, attributes, first_attribute, decl;
17187           cp_declarator *declarator = NULL;
17188           int ctor_dtor_or_conv_p;
17189
17190           /* Check for a (possibly unnamed) bitfield declaration.  */
17191           token = cp_lexer_peek_token (parser->lexer);
17192           if (token->type == CPP_COLON)
17193             goto eat_colon;
17194
17195           if (token->type == CPP_NAME
17196               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17197                   == CPP_COLON))
17198             {
17199               /* Get the name of the bitfield.  */
17200               declarator = make_id_declarator (NULL_TREE,
17201                                                cp_parser_identifier (parser),
17202                                                sfk_none);
17203
17204              eat_colon:
17205               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17206               /* Get the width of the bitfield.  */
17207               width
17208                 = cp_parser_constant_expression (parser,
17209                                                  /*allow_non_constant=*/false,
17210                                                  NULL);
17211             }
17212           else
17213             {
17214               /* Parse the declarator.  */
17215               declarator
17216                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17217                                         &ctor_dtor_or_conv_p,
17218                                         /*parenthesized_p=*/NULL,
17219                                         /*member_p=*/false);
17220             }
17221
17222           /* Look for attributes that apply to the ivar.  */
17223           attributes = cp_parser_attributes_opt (parser);
17224           /* Remember which attributes are prefix attributes and
17225              which are not.  */
17226           first_attribute = attributes;
17227           /* Combine the attributes.  */
17228           attributes = chainon (prefix_attributes, attributes);
17229
17230           if (width)
17231             {
17232               /* Create the bitfield declaration.  */
17233               decl = grokbitfield (declarator, &declspecs, width);
17234               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17235             }
17236           else
17237             decl = grokfield (declarator, &declspecs, NULL_TREE,
17238                               NULL_TREE, attributes);
17239
17240           /* Add the instance variable.  */
17241           objc_add_instance_variable (decl);
17242
17243           /* Reset PREFIX_ATTRIBUTES.  */
17244           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17245             attributes = TREE_CHAIN (attributes);
17246           if (attributes)
17247             TREE_CHAIN (attributes) = NULL_TREE;
17248
17249           token = cp_lexer_peek_token (parser->lexer);
17250
17251           if (token->type == CPP_COMMA)
17252             {
17253               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17254               continue;
17255             }
17256           break;
17257         }
17258
17259       cp_parser_consume_semicolon_at_end_of_statement (parser);
17260       token = cp_lexer_peek_token (parser->lexer);
17261     }
17262
17263   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17264   /* For historical reasons, we accept an optional semicolon.  */
17265   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17266     cp_lexer_consume_token (parser->lexer);
17267 }
17268
17269 /* Parse an Objective-C protocol declaration.  */
17270
17271 static void
17272 cp_parser_objc_protocol_declaration (cp_parser* parser)
17273 {
17274   tree proto, protorefs;
17275   cp_token *tok;
17276
17277   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17278   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17279     {
17280       error ("identifier expected after %<@protocol%>");
17281       goto finish;
17282     }
17283
17284   /* See if we have a forward declaration or a definition.  */
17285   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17286
17287   /* Try a forward declaration first.  */
17288   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17289     {
17290       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17291      finish:
17292       cp_parser_consume_semicolon_at_end_of_statement (parser);
17293     }
17294
17295   /* Ok, we got a full-fledged definition (or at least should).  */
17296   else
17297     {
17298       proto = cp_parser_identifier (parser);
17299       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17300       objc_start_protocol (proto, protorefs);
17301       cp_parser_objc_method_prototype_list (parser);
17302     }
17303 }
17304
17305 /* Parse an Objective-C superclass or category.  */
17306
17307 static void
17308 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17309                                                           tree *categ)
17310 {
17311   cp_token *next = cp_lexer_peek_token (parser->lexer);
17312
17313   *super = *categ = NULL_TREE;
17314   if (next->type == CPP_COLON)
17315     {
17316       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17317       *super = cp_parser_identifier (parser);
17318     }
17319   else if (next->type == CPP_OPEN_PAREN)
17320     {
17321       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17322       *categ = cp_parser_identifier (parser);
17323       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17324     }
17325 }
17326
17327 /* Parse an Objective-C class interface.  */
17328
17329 static void
17330 cp_parser_objc_class_interface (cp_parser* parser)
17331 {
17332   tree name, super, categ, protos;
17333
17334   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17335   name = cp_parser_identifier (parser);
17336   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17337   protos = cp_parser_objc_protocol_refs_opt (parser);
17338
17339   /* We have either a class or a category on our hands.  */
17340   if (categ)
17341     objc_start_category_interface (name, categ, protos);
17342   else
17343     {
17344       objc_start_class_interface (name, super, protos);
17345       /* Handle instance variable declarations, if any.  */
17346       cp_parser_objc_class_ivars (parser);
17347       objc_continue_interface ();
17348     }
17349
17350   cp_parser_objc_method_prototype_list (parser);
17351 }
17352
17353 /* Parse an Objective-C class implementation.  */
17354
17355 static void
17356 cp_parser_objc_class_implementation (cp_parser* parser)
17357 {
17358   tree name, super, categ;
17359
17360   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17361   name = cp_parser_identifier (parser);
17362   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17363
17364   /* We have either a class or a category on our hands.  */
17365   if (categ)
17366     objc_start_category_implementation (name, categ);
17367   else
17368     {
17369       objc_start_class_implementation (name, super);
17370       /* Handle instance variable declarations, if any.  */
17371       cp_parser_objc_class_ivars (parser);
17372       objc_continue_implementation ();
17373     }
17374
17375   cp_parser_objc_method_definition_list (parser);
17376 }
17377
17378 /* Consume the @end token and finish off the implementation.  */
17379
17380 static void
17381 cp_parser_objc_end_implementation (cp_parser* parser)
17382 {
17383   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17384   objc_finish_implementation ();
17385 }
17386
17387 /* Parse an Objective-C declaration.  */
17388
17389 static void
17390 cp_parser_objc_declaration (cp_parser* parser)
17391 {
17392   /* Try to figure out what kind of declaration is present.  */
17393   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17394
17395   switch (kwd->keyword)
17396     {
17397     case RID_AT_ALIAS:
17398       cp_parser_objc_alias_declaration (parser);
17399       break;
17400     case RID_AT_CLASS:
17401       cp_parser_objc_class_declaration (parser);
17402       break;
17403     case RID_AT_PROTOCOL:
17404       cp_parser_objc_protocol_declaration (parser);
17405       break;
17406     case RID_AT_INTERFACE:
17407       cp_parser_objc_class_interface (parser);
17408       break;
17409     case RID_AT_IMPLEMENTATION:
17410       cp_parser_objc_class_implementation (parser);
17411       break;
17412     case RID_AT_END:
17413       cp_parser_objc_end_implementation (parser);
17414       break;
17415     default:
17416       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17417       cp_parser_skip_to_end_of_block_or_statement (parser);
17418     }
17419 }
17420
17421 /* Parse an Objective-C try-catch-finally statement.
17422
17423    objc-try-catch-finally-stmt:
17424      @try compound-statement objc-catch-clause-seq [opt]
17425        objc-finally-clause [opt]
17426
17427    objc-catch-clause-seq:
17428      objc-catch-clause objc-catch-clause-seq [opt]
17429
17430    objc-catch-clause:
17431      @catch ( exception-declaration ) compound-statement
17432
17433    objc-finally-clause
17434      @finally compound-statement
17435
17436    Returns NULL_TREE.  */
17437
17438 static tree
17439 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17440   location_t location;
17441   tree stmt;
17442
17443   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17444   location = cp_lexer_peek_token (parser->lexer)->location;
17445   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17446      node, lest it get absorbed into the surrounding block.  */
17447   stmt = push_stmt_list ();
17448   cp_parser_compound_statement (parser, NULL, false);
17449   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17450
17451   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17452     {
17453       cp_parameter_declarator *parmdecl;
17454       tree parm;
17455
17456       cp_lexer_consume_token (parser->lexer);
17457       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17458       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17459       parm = grokdeclarator (parmdecl->declarator,
17460                              &parmdecl->decl_specifiers,
17461                              PARM, /*initialized=*/0,
17462                              /*attrlist=*/NULL);
17463       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17464       objc_begin_catch_clause (parm);
17465       cp_parser_compound_statement (parser, NULL, false);
17466       objc_finish_catch_clause ();
17467     }
17468
17469   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17470     {
17471       cp_lexer_consume_token (parser->lexer);
17472       location = cp_lexer_peek_token (parser->lexer)->location;
17473       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17474          node, lest it get absorbed into the surrounding block.  */
17475       stmt = push_stmt_list ();
17476       cp_parser_compound_statement (parser, NULL, false);
17477       objc_build_finally_clause (location, pop_stmt_list (stmt));
17478     }
17479
17480   return objc_finish_try_stmt ();
17481 }
17482
17483 /* Parse an Objective-C synchronized statement.
17484
17485    objc-synchronized-stmt:
17486      @synchronized ( expression ) compound-statement
17487
17488    Returns NULL_TREE.  */
17489
17490 static tree
17491 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17492   location_t location;
17493   tree lock, stmt;
17494
17495   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17496
17497   location = cp_lexer_peek_token (parser->lexer)->location;
17498   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17499   lock = cp_parser_expression (parser, false);
17500   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17501
17502   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17503      node, lest it get absorbed into the surrounding block.  */
17504   stmt = push_stmt_list ();
17505   cp_parser_compound_statement (parser, NULL, false);
17506
17507   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17508 }
17509
17510 /* Parse an Objective-C throw statement.
17511
17512    objc-throw-stmt:
17513      @throw assignment-expression [opt] ;
17514
17515    Returns a constructed '@throw' statement.  */
17516
17517 static tree
17518 cp_parser_objc_throw_statement (cp_parser *parser) {
17519   tree expr = NULL_TREE;
17520
17521   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17522
17523   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17524     expr = cp_parser_assignment_expression (parser, false);
17525
17526   cp_parser_consume_semicolon_at_end_of_statement (parser);
17527
17528   return objc_build_throw_stmt (expr);
17529 }
17530
17531 /* Parse an Objective-C statement.  */
17532
17533 static tree
17534 cp_parser_objc_statement (cp_parser * parser) {
17535   /* Try to figure out what kind of declaration is present.  */
17536   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17537
17538   switch (kwd->keyword)
17539     {
17540     case RID_AT_TRY:
17541       return cp_parser_objc_try_catch_finally_statement (parser);
17542     case RID_AT_SYNCHRONIZED:
17543       return cp_parser_objc_synchronized_statement (parser);
17544     case RID_AT_THROW:
17545       return cp_parser_objc_throw_statement (parser);
17546     default:
17547       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17548       cp_parser_skip_to_end_of_block_or_statement (parser);
17549     }
17550
17551   return error_mark_node;
17552 }
17553 /* The parser.  */
17554
17555 static GTY (()) cp_parser *the_parser;
17556
17557 \f
17558 /* Special handling for the first token or line in the file.  The first
17559    thing in the file might be #pragma GCC pch_preprocess, which loads a
17560    PCH file, which is a GC collection point.  So we need to handle this
17561    first pragma without benefit of an existing lexer structure.
17562
17563    Always returns one token to the caller in *FIRST_TOKEN.  This is 
17564    either the true first token of the file, or the first token after
17565    the initial pragma.  */
17566
17567 static void
17568 cp_parser_initial_pragma (cp_token *first_token)
17569 {
17570   tree name = NULL;
17571
17572   cp_lexer_get_preprocessor_token (NULL, first_token);
17573   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
17574     return;
17575
17576   cp_lexer_get_preprocessor_token (NULL, first_token);
17577   if (first_token->type == CPP_STRING)
17578     {
17579       name = first_token->value;
17580
17581       cp_lexer_get_preprocessor_token (NULL, first_token);
17582       if (first_token->type != CPP_PRAGMA_EOL)
17583         error ("junk at end of %<#pragma GCC pch_preprocess%>");
17584     }
17585   else
17586     error ("expected string literal");
17587
17588   /* Skip to the end of the pragma.  */
17589   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
17590     cp_lexer_get_preprocessor_token (NULL, first_token);
17591
17592   /* Read one more token to return to our caller.  */
17593   cp_lexer_get_preprocessor_token (NULL, first_token);
17594
17595   /* Now actually load the PCH file.  */
17596   if (name)
17597     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
17598 }
17599
17600 /* Normal parsing of a pragma token.  Here we can (and must) use the
17601    regular lexer.  */
17602
17603 static bool
17604 cp_parser_pragma (cp_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
17605 {
17606   cp_token *pragma_tok;
17607   unsigned int id;
17608
17609   pragma_tok = cp_lexer_consume_token (parser->lexer);
17610   gcc_assert (pragma_tok->type == CPP_PRAGMA);
17611   parser->lexer->in_pragma = true;
17612
17613   id = pragma_tok->pragma_kind;
17614   switch (id)
17615     {
17616     case PRAGMA_GCC_PCH_PREPROCESS:
17617       error ("%<#pragma GCC pch_preprocess%> must be first");
17618       break;
17619
17620     default:
17621       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
17622       c_invoke_pragma_handler (id);
17623       break;
17624     }
17625
17626   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
17627   return false;
17628 }
17629
17630 /* The interface the pragma parsers have to the lexer.  */
17631
17632 enum cpp_ttype
17633 pragma_lex (tree *value)
17634 {
17635   cp_token *tok;
17636   enum cpp_ttype ret;
17637
17638   tok = cp_lexer_peek_token (the_parser->lexer);
17639
17640   ret = tok->type;
17641   *value = tok->value;
17642
17643   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
17644     ret = CPP_EOF;
17645   else if (ret == CPP_STRING)
17646     *value = cp_parser_string_literal (the_parser, false, false);
17647   else
17648     {
17649       cp_lexer_consume_token (the_parser->lexer);
17650       if (ret == CPP_KEYWORD)
17651         ret = CPP_NAME;
17652     }
17653
17654   return ret;
17655 }
17656
17657 \f
17658 /* External interface.  */
17659
17660 /* Parse one entire translation unit.  */
17661
17662 void
17663 c_parse_file (void)
17664 {
17665   bool error_occurred;
17666   static bool already_called = false;
17667
17668   if (already_called)
17669     {
17670       sorry ("inter-module optimizations not implemented for C++");
17671       return;
17672     }
17673   already_called = true;
17674
17675   the_parser = cp_parser_new ();
17676   push_deferring_access_checks (flag_access_control
17677                                 ? dk_no_deferred : dk_no_check);
17678   error_occurred = cp_parser_translation_unit (the_parser);
17679   the_parser = NULL;
17680 }
17681
17682 /* This variable must be provided by every front end.  */
17683
17684 int yydebug;
17685
17686 #include "gt-cp-parser.h"