OSDN Git Service

e80b8ee09d957790e48c0b45cd29d8b88815f9e8
[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 "cgraph.h"
40 #include "c-common.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A C++ token.  */
49
50 typedef struct cp_token GTY (())
51 {
52   /* The kind of token.  */
53   ENUM_BITFIELD (cpp_ttype) type : 8;
54   /* If this token is a keyword, this value indicates which keyword.
55      Otherwise, this value is RID_MAX.  */
56   ENUM_BITFIELD (rid) keyword : 8;
57   /* Token flags.  */
58   unsigned char flags;
59   /* Identifier for the pragma.  */
60   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61   /* True if this token is from a system header.  */
62   BOOL_BITFIELD in_system_header : 1;
63   /* True if this token is from a context where it is implicitly extern "C" */
64   BOOL_BITFIELD implicit_extern_c : 1;
65   /* True for a CPP_NAME token that is not a keyword (i.e., for which
66      KEYWORD is RID_MAX) iff this name was looked up and found to be
67      ambiguous.  An error has already been reported.  */
68   BOOL_BITFIELD ambiguous_p : 1;
69   /* The value associated with this token, if any.  */
70   tree value;
71   /* The location at which this token was found.  */
72   location_t location;
73 } cp_token;
74
75 /* We use a stack of token pointer for saving token sets.  */
76 typedef struct cp_token *cp_token_position;
77 DEF_VEC_P (cp_token_position);
78 DEF_VEC_ALLOC_P (cp_token_position,heap);
79
80 static const cp_token eof_token =
81 {
82   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
83 #if USE_MAPPED_LOCATION
84   0
85 #else
86   {0, 0}
87 #endif
88 };
89
90 /* The cp_lexer structure represents the C++ lexer.  It is responsible
91    for managing the token stream from the preprocessor and supplying
92    it to the parser.  Tokens are never added to the cp_lexer after
93    it is created.  */
94
95 typedef struct cp_lexer GTY (())
96 {
97   /* The memory allocated for the buffer.  NULL if this lexer does not
98      own the token buffer.  */
99   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
100   /* If the lexer owns the buffer, this is the number of tokens in the
101      buffer.  */
102   size_t buffer_length;
103
104   /* A pointer just past the last available token.  The tokens
105      in this lexer are [buffer, last_token).  */
106   cp_token_position GTY ((skip)) last_token;
107
108   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
109      no more available tokens.  */
110   cp_token_position GTY ((skip)) next_token;
111
112   /* A stack indicating positions at which cp_lexer_save_tokens was
113      called.  The top entry is the most recent position at which we
114      began saving tokens.  If the stack is non-empty, we are saving
115      tokens.  */
116   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
117
118   /* The next lexer in a linked list of lexers.  */
119   struct cp_lexer *next;
120
121   /* True if we should output debugging information.  */
122   bool debugging_p;
123
124   /* True if we're in the context of parsing a pragma, and should not
125      increment past the end-of-line marker.  */
126   bool in_pragma;
127 } cp_lexer;
128
129 /* cp_token_cache is a range of tokens.  There is no need to represent
130    allocate heap memory for it, since tokens are never removed from the
131    lexer's array.  There is also no need for the GC to walk through
132    a cp_token_cache, since everything in here is referenced through
133    a lexer.  */
134
135 typedef struct cp_token_cache GTY(())
136 {
137   /* The beginning of the token range.  */
138   cp_token * GTY((skip)) first;
139
140   /* Points immediately after the last token in the range.  */
141   cp_token * GTY ((skip)) last;
142 } cp_token_cache;
143
144 /* Prototypes.  */
145
146 static cp_lexer *cp_lexer_new_main
147   (void);
148 static cp_lexer *cp_lexer_new_from_tokens
149   (cp_token_cache *tokens);
150 static void cp_lexer_destroy
151   (cp_lexer *);
152 static int cp_lexer_saving_tokens
153   (const cp_lexer *);
154 static cp_token_position cp_lexer_token_position
155   (cp_lexer *, bool);
156 static cp_token *cp_lexer_token_at
157   (cp_lexer *, cp_token_position);
158 static void cp_lexer_get_preprocessor_token
159   (cp_lexer *, cp_token *);
160 static inline cp_token *cp_lexer_peek_token
161   (cp_lexer *);
162 static cp_token *cp_lexer_peek_nth_token
163   (cp_lexer *, size_t);
164 static inline bool cp_lexer_next_token_is
165   (cp_lexer *, enum cpp_ttype);
166 static bool cp_lexer_next_token_is_not
167   (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_keyword
169   (cp_lexer *, enum rid);
170 static cp_token *cp_lexer_consume_token
171   (cp_lexer *);
172 static void cp_lexer_purge_token
173   (cp_lexer *);
174 static void cp_lexer_purge_tokens_after
175   (cp_lexer *, cp_token_position);
176 static void cp_lexer_save_tokens
177   (cp_lexer *);
178 static void cp_lexer_commit_tokens
179   (cp_lexer *);
180 static void cp_lexer_rollback_tokens
181   (cp_lexer *);
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184   (FILE *, cp_token *);
185 static inline bool cp_lexer_debugging_p
186   (cp_lexer *);
187 static void cp_lexer_start_debugging
188   (cp_lexer *) ATTRIBUTE_UNUSED;
189 static void cp_lexer_stop_debugging
190   (cp_lexer *) ATTRIBUTE_UNUSED;
191 #else
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193    about passing NULL to functions that require non-NULL arguments
194    (fputs, fprintf).  It will never be used, so all we need is a value
195    of the right type that's guaranteed not to be NULL.  */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
200
201 static cp_token_cache *cp_token_cache_new
202   (cp_token *, cp_token *);
203
204 static void cp_parser_initial_pragma
205   (cp_token *);
206
207 /* Manifest constants.  */
208 #define CP_LEXER_BUFFER_SIZE 10000
209 #define CP_SAVED_TOKEN_STACK 5
210
211 /* A token type for keywords, as opposed to ordinary identifiers.  */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
213
214 /* A token type for template-ids.  If a template-id is processed while
215    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216    the value of the CPP_TEMPLATE_ID is whatever was returned by
217    cp_parser_template_id.  */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
219
220 /* A token type for nested-name-specifiers.  If a
221    nested-name-specifier is processed while parsing tentatively, it is
222    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224    cp_parser_nested_name_specifier_opt.  */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
226
227 /* A token type for tokens that are not tokens at all; these are used
228    to represent slots in the array where there used to be a token
229    that has now been deleted.  */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
231
232 /* The number of token types, including C++-specific ones.  */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
234
235 /* Variables.  */
236
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written.  */
239 static FILE *cp_lexer_debug_stream;
240 #endif /* ENABLE_CHECKING */
241
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
243    preprocessor.  */
244
245 static cp_lexer *
246 cp_lexer_new_main (void)
247 {
248   cp_token first_token;
249   cp_lexer *lexer;
250   cp_token *pos;
251   size_t alloc;
252   size_t space;
253   cp_token *buffer;
254
255   /* It's possible that parsing the first pragma will load a PCH file,
256      which is a GC collection point.  So we have to do that before
257      allocating any memory.  */
258   cp_parser_initial_pragma (&first_token);
259
260   /* Tell c_lex_with_flags not to merge string constants.  */
261   c_lex_return_raw_strings = true;
262
263   c_common_no_more_pch ();
264
265   /* Allocate the memory.  */
266   lexer = GGC_CNEW (cp_lexer);
267
268 #ifdef ENABLE_CHECKING
269   /* Initially we are not debugging.  */
270   lexer->debugging_p = false;
271 #endif /* ENABLE_CHECKING */
272   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
273                                    CP_SAVED_TOKEN_STACK);
274
275   /* Create the buffer.  */
276   alloc = CP_LEXER_BUFFER_SIZE;
277   buffer = GGC_NEWVEC (cp_token, alloc);
278
279   /* Put the first token in the buffer.  */
280   space = alloc;
281   pos = buffer;
282   *pos = first_token;
283
284   /* Get the remaining tokens from the preprocessor.  */
285   while (pos->type != CPP_EOF)
286     {
287       pos++;
288       if (!--space)
289         {
290           space = alloc;
291           alloc *= 2;
292           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
293           pos = buffer + space;
294         }
295       cp_lexer_get_preprocessor_token (lexer, pos);
296     }
297   lexer->buffer = buffer;
298   lexer->buffer_length = alloc - space;
299   lexer->last_token = pos;
300   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
301
302   /* Subsequent preprocessor diagnostics should use compiler
303      diagnostic functions to get the compiler source location.  */
304   cpp_get_options (parse_in)->client_diagnostic = true;
305   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
306
307   gcc_assert (lexer->next_token->type != CPP_PURGED);
308   return lexer;
309 }
310
311 /* Create a new lexer whose token stream is primed with the tokens in
312    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
313
314 static cp_lexer *
315 cp_lexer_new_from_tokens (cp_token_cache *cache)
316 {
317   cp_token *first = cache->first;
318   cp_token *last = cache->last;
319   cp_lexer *lexer = GGC_CNEW (cp_lexer);
320
321   /* We do not own the buffer.  */
322   lexer->buffer = NULL;
323   lexer->buffer_length = 0;
324   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
325   lexer->last_token = last;
326
327   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
328                                    CP_SAVED_TOKEN_STACK);
329
330 #ifdef ENABLE_CHECKING
331   /* Initially we are not debugging.  */
332   lexer->debugging_p = false;
333 #endif
334
335   gcc_assert (lexer->next_token->type != CPP_PURGED);
336   return lexer;
337 }
338
339 /* Frees all resources associated with LEXER.  */
340
341 static void
342 cp_lexer_destroy (cp_lexer *lexer)
343 {
344   if (lexer->buffer)
345     ggc_free (lexer->buffer);
346   VEC_free (cp_token_position, heap, lexer->saved_tokens);
347   ggc_free (lexer);
348 }
349
350 /* Returns nonzero if debugging information should be output.  */
351
352 #ifdef ENABLE_CHECKING
353
354 static inline bool
355 cp_lexer_debugging_p (cp_lexer *lexer)
356 {
357   return lexer->debugging_p;
358 }
359
360 #endif /* ENABLE_CHECKING */
361
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
364 {
365   gcc_assert (!previous_p || lexer->next_token != &eof_token);
366
367   return lexer->next_token - previous_p;
368 }
369
370 static inline cp_token *
371 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
372 {
373   return pos;
374 }
375
376 /* nonzero if we are presently saving tokens.  */
377
378 static inline int
379 cp_lexer_saving_tokens (const cp_lexer* lexer)
380 {
381   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
382 }
383
384 /* Store the next token from the preprocessor in *TOKEN.  Return true
385    if we reach EOF.  */
386
387 static void
388 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
389                                  cp_token *token)
390 {
391   static int is_extern_c = 0;
392
393    /* Get a new token from the preprocessor.  */
394   token->type
395     = c_lex_with_flags (&token->value, &token->location, &token->flags);
396   token->keyword = RID_MAX;
397   token->pragma_kind = PRAGMA_NONE;
398   token->in_system_header = in_system_header;
399
400   /* On some systems, some header files are surrounded by an
401      implicit extern "C" block.  Set a flag in the token if it
402      comes from such a header.  */
403   is_extern_c += pending_lang_change;
404   pending_lang_change = 0;
405   token->implicit_extern_c = is_extern_c > 0;
406
407   /* Check to see if this token is a keyword.  */
408   if (token->type == CPP_NAME)
409     {
410       if (C_IS_RESERVED_WORD (token->value))
411         {
412           /* Mark this token as a keyword.  */
413           token->type = CPP_KEYWORD;
414           /* Record which keyword.  */
415           token->keyword = C_RID_CODE (token->value);
416           /* Update the value.  Some keywords are mapped to particular
417              entities, rather than simply having the value of the
418              corresponding IDENTIFIER_NODE.  For example, `__const' is
419              mapped to `const'.  */
420           token->value = ridpointers[token->keyword];
421         }
422       else
423         {
424           token->ambiguous_p = false;
425           token->keyword = RID_MAX;
426         }
427     }
428   /* Handle Objective-C++ keywords.  */
429   else if (token->type == CPP_AT_NAME)
430     {
431       token->type = CPP_KEYWORD;
432       switch (C_RID_CODE (token->value))
433         {
434         /* Map 'class' to '@class', 'private' to '@private', etc.  */
435         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439         case RID_THROW: token->keyword = RID_AT_THROW; break;
440         case RID_TRY: token->keyword = RID_AT_TRY; break;
441         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442         default: token->keyword = C_RID_CODE (token->value);
443         }
444     }
445   else if (token->type == CPP_PRAGMA)
446     {
447       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
448       token->pragma_kind = TREE_INT_CST_LOW (token->value);
449       token->value = NULL;
450     }
451 }
452
453 /* Update the globals input_location and in_system_header from TOKEN.  */
454 static inline void
455 cp_lexer_set_source_position_from_token (cp_token *token)
456 {
457   if (token->type != CPP_EOF)
458     {
459       input_location = token->location;
460       in_system_header = token->in_system_header;
461     }
462 }
463
464 /* Return a pointer to the next token in the token stream, but do not
465    consume it.  */
466
467 static inline cp_token *
468 cp_lexer_peek_token (cp_lexer *lexer)
469 {
470   if (cp_lexer_debugging_p (lexer))
471     {
472       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
473       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
474       putc ('\n', cp_lexer_debug_stream);
475     }
476   return lexer->next_token;
477 }
478
479 /* Return true if the next token has the indicated TYPE.  */
480
481 static inline bool
482 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
483 {
484   return cp_lexer_peek_token (lexer)->type == type;
485 }
486
487 /* Return true if the next token does not have the indicated TYPE.  */
488
489 static inline bool
490 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
491 {
492   return !cp_lexer_next_token_is (lexer, type);
493 }
494
495 /* Return true if the next token is the indicated KEYWORD.  */
496
497 static inline bool
498 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
499 {
500   return cp_lexer_peek_token (lexer)->keyword == keyword;
501 }
502
503 /* Return a pointer to the Nth token in the token stream.  If N is 1,
504    then this is precisely equivalent to cp_lexer_peek_token (except
505    that it is not inline).  One would like to disallow that case, but
506    there is one case (cp_parser_nth_token_starts_template_id) where
507    the caller passes a variable for N and it might be 1.  */
508
509 static cp_token *
510 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
511 {
512   cp_token *token;
513
514   /* N is 1-based, not zero-based.  */
515   gcc_assert (n > 0);
516   
517   if (cp_lexer_debugging_p (lexer))
518     fprintf (cp_lexer_debug_stream,
519              "cp_lexer: peeking ahead %ld at token: ", (long)n);
520
521   --n;
522   token = lexer->next_token;
523   gcc_assert (!n || token != &eof_token);
524   while (n != 0)
525     {
526       ++token;
527       if (token == lexer->last_token)
528         {
529           token = (cp_token *)&eof_token;
530           break;
531         }
532
533       if (token->type != CPP_PURGED)
534         --n;
535     }
536
537   if (cp_lexer_debugging_p (lexer))
538     {
539       cp_lexer_print_token (cp_lexer_debug_stream, token);
540       putc ('\n', cp_lexer_debug_stream);
541     }
542
543   return token;
544 }
545
546 /* Return the next token, and advance the lexer's next_token pointer
547    to point to the next non-purged token.  */
548
549 static cp_token *
550 cp_lexer_consume_token (cp_lexer* lexer)
551 {
552   cp_token *token = lexer->next_token;
553
554   gcc_assert (token != &eof_token);
555   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
556
557   do
558     {
559       lexer->next_token++;
560       if (lexer->next_token == lexer->last_token)
561         {
562           lexer->next_token = (cp_token *)&eof_token;
563           break;
564         }
565
566     }
567   while (lexer->next_token->type == CPP_PURGED);
568
569   cp_lexer_set_source_position_from_token (token);
570
571   /* Provide debugging output.  */
572   if (cp_lexer_debugging_p (lexer))
573     {
574       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575       cp_lexer_print_token (cp_lexer_debug_stream, token);
576       putc ('\n', cp_lexer_debug_stream);
577     }
578
579   return token;
580 }
581
582 /* Permanently remove the next token from the token stream, and
583    advance the next_token pointer to refer to the next non-purged
584    token.  */
585
586 static void
587 cp_lexer_purge_token (cp_lexer *lexer)
588 {
589   cp_token *tok = lexer->next_token;
590
591   gcc_assert (tok != &eof_token);
592   tok->type = CPP_PURGED;
593   tok->location = UNKNOWN_LOCATION;
594   tok->value = NULL_TREE;
595   tok->keyword = RID_MAX;
596
597   do
598     {
599       tok++;
600       if (tok == lexer->last_token)
601         {
602           tok = (cp_token *)&eof_token;
603           break;
604         }
605     }
606   while (tok->type == CPP_PURGED);
607   lexer->next_token = tok;
608 }
609
610 /* Permanently remove all tokens after TOK, up to, but not
611    including, the token that will be returned next by
612    cp_lexer_peek_token.  */
613
614 static void
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
616 {
617   cp_token *peek = lexer->next_token;
618
619   if (peek == &eof_token)
620     peek = lexer->last_token;
621
622   gcc_assert (tok < peek);
623
624   for ( tok += 1; tok != peek; tok += 1)
625     {
626       tok->type = CPP_PURGED;
627       tok->location = UNKNOWN_LOCATION;
628       tok->value = NULL_TREE;
629       tok->keyword = RID_MAX;
630     }
631 }
632
633 /* Begin saving tokens.  All tokens consumed after this point will be
634    preserved.  */
635
636 static void
637 cp_lexer_save_tokens (cp_lexer* lexer)
638 {
639   /* Provide debugging output.  */
640   if (cp_lexer_debugging_p (lexer))
641     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
642
643   VEC_safe_push (cp_token_position, heap,
644                  lexer->saved_tokens, lexer->next_token);
645 }
646
647 /* Commit to the portion of the token stream most recently saved.  */
648
649 static void
650 cp_lexer_commit_tokens (cp_lexer* lexer)
651 {
652   /* Provide debugging output.  */
653   if (cp_lexer_debugging_p (lexer))
654     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
655
656   VEC_pop (cp_token_position, lexer->saved_tokens);
657 }
658
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660    to the token stream.  Stop saving tokens.  */
661
662 static void
663 cp_lexer_rollback_tokens (cp_lexer* lexer)
664 {
665   /* Provide debugging output.  */
666   if (cp_lexer_debugging_p (lexer))
667     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
668
669   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
670 }
671
672 /* Print a representation of the TOKEN on the STREAM.  */
673
674 #ifdef ENABLE_CHECKING
675
676 static void
677 cp_lexer_print_token (FILE * stream, cp_token *token)
678 {
679   /* We don't use cpp_type2name here because the parser defines
680      a few tokens of its own.  */
681   static const char *const token_names[] = {
682     /* cpplib-defined token types */
683 #define OP(e, s) #e,
684 #define TK(e, s) #e,
685     TTYPE_TABLE
686 #undef OP
687 #undef TK
688     /* C++ parser token types - see "Manifest constants", above.  */
689     "KEYWORD",
690     "TEMPLATE_ID",
691     "NESTED_NAME_SPECIFIER",
692     "PURGED"
693   };
694
695   /* If we have a name for the token, print it out.  Otherwise, we
696      simply give the numeric code.  */
697   gcc_assert (token->type < ARRAY_SIZE(token_names));
698   fputs (token_names[token->type], stream);
699
700   /* For some tokens, print the associated data.  */
701   switch (token->type)
702     {
703     case CPP_KEYWORD:
704       /* Some keywords have a value that is not an IDENTIFIER_NODE.
705          For example, `struct' is mapped to an INTEGER_CST.  */
706       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
707         break;
708       /* else fall through */
709     case CPP_NAME:
710       fputs (IDENTIFIER_POINTER (token->value), stream);
711       break;
712
713     case CPP_STRING:
714     case CPP_WSTRING:
715       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
716       break;
717
718     default:
719       break;
720     }
721 }
722
723 /* Start emitting debugging information.  */
724
725 static void
726 cp_lexer_start_debugging (cp_lexer* lexer)
727 {
728   lexer->debugging_p = true;
729 }
730
731 /* Stop emitting debugging information.  */
732
733 static void
734 cp_lexer_stop_debugging (cp_lexer* lexer)
735 {
736   lexer->debugging_p = false;
737 }
738
739 #endif /* ENABLE_CHECKING */
740
741 /* Create a new cp_token_cache, representing a range of tokens.  */
742
743 static cp_token_cache *
744 cp_token_cache_new (cp_token *first, cp_token *last)
745 {
746   cp_token_cache *cache = GGC_NEW (cp_token_cache);
747   cache->first = first;
748   cache->last = last;
749   return cache;
750 }
751
752 \f
753 /* Decl-specifiers.  */
754
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
756
757 static void
758 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
759 {
760   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
761 }
762
763 /* Declarators.  */
764
765 /* Nothing other than the parser should be creating declarators;
766    declarators are a semi-syntactic representation of C++ entities.
767    Other parts of the front end that need to create entities (like
768    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
769
770 static cp_declarator *make_call_declarator
771   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
772 static cp_declarator *make_array_declarator
773   (cp_declarator *, tree);
774 static cp_declarator *make_pointer_declarator
775   (cp_cv_quals, cp_declarator *);
776 static cp_declarator *make_reference_declarator
777   (cp_cv_quals, cp_declarator *);
778 static cp_parameter_declarator *make_parameter_declarator
779   (cp_decl_specifier_seq *, cp_declarator *, tree);
780 static cp_declarator *make_ptrmem_declarator
781   (cp_cv_quals, tree, cp_declarator *);
782
783 /* An erroneous declarator.  */
784 static cp_declarator *cp_error_declarator;
785
786 /* The obstack on which declarators and related data structures are
787    allocated.  */
788 static struct obstack declarator_obstack;
789
790 /* Alloc BYTES from the declarator memory pool.  */
791
792 static inline void *
793 alloc_declarator (size_t bytes)
794 {
795   return obstack_alloc (&declarator_obstack, bytes);
796 }
797
798 /* Allocate a declarator of the indicated KIND.  Clear fields that are
799    common to all declarators.  */
800
801 static cp_declarator *
802 make_declarator (cp_declarator_kind kind)
803 {
804   cp_declarator *declarator;
805
806   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
807   declarator->kind = kind;
808   declarator->attributes = NULL_TREE;
809   declarator->declarator = NULL;
810
811   return declarator;
812 }
813
814 /* Make a declarator for a generalized identifier.  If
815    QUALIFYING_SCOPE is non-NULL, the identifier is
816    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
817    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
818    is, if any.   */
819
820 static cp_declarator *
821 make_id_declarator (tree qualifying_scope, tree unqualified_name,
822                     special_function_kind sfk)
823 {
824   cp_declarator *declarator;
825
826   /* It is valid to write:
827
828        class C { void f(); };
829        typedef C D;
830        void D::f();
831
832      The standard is not clear about whether `typedef const C D' is
833      legal; as of 2002-09-15 the committee is considering that
834      question.  EDG 3.0 allows that syntax.  Therefore, we do as
835      well.  */
836   if (qualifying_scope && TYPE_P (qualifying_scope))
837     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
838
839   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
840               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
841               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
842
843   declarator = make_declarator (cdk_id);
844   declarator->u.id.qualifying_scope = qualifying_scope;
845   declarator->u.id.unqualified_name = unqualified_name;
846   declarator->u.id.sfk = sfk;
847
848   return declarator;
849 }
850
851 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
852    of modifiers such as const or volatile to apply to the pointer
853    type, represented as identifiers.  */
854
855 cp_declarator *
856 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
857 {
858   cp_declarator *declarator;
859
860   declarator = make_declarator (cdk_pointer);
861   declarator->declarator = target;
862   declarator->u.pointer.qualifiers = cv_qualifiers;
863   declarator->u.pointer.class_type = NULL_TREE;
864
865   return declarator;
866 }
867
868 /* Like make_pointer_declarator -- but for references.  */
869
870 cp_declarator *
871 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
872 {
873   cp_declarator *declarator;
874
875   declarator = make_declarator (cdk_reference);
876   declarator->declarator = target;
877   declarator->u.pointer.qualifiers = cv_qualifiers;
878   declarator->u.pointer.class_type = NULL_TREE;
879
880   return declarator;
881 }
882
883 /* Like make_pointer_declarator -- but for a pointer to a non-static
884    member of CLASS_TYPE.  */
885
886 cp_declarator *
887 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
888                         cp_declarator *pointee)
889 {
890   cp_declarator *declarator;
891
892   declarator = make_declarator (cdk_ptrmem);
893   declarator->declarator = pointee;
894   declarator->u.pointer.qualifiers = cv_qualifiers;
895   declarator->u.pointer.class_type = class_type;
896
897   return declarator;
898 }
899
900 /* Make a declarator for the function given by TARGET, with the
901    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
902    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
903    indicates what exceptions can be thrown.  */
904
905 cp_declarator *
906 make_call_declarator (cp_declarator *target,
907                       cp_parameter_declarator *parms,
908                       cp_cv_quals cv_qualifiers,
909                       tree exception_specification)
910 {
911   cp_declarator *declarator;
912
913   declarator = make_declarator (cdk_function);
914   declarator->declarator = target;
915   declarator->u.function.parameters = parms;
916   declarator->u.function.qualifiers = cv_qualifiers;
917   declarator->u.function.exception_specification = exception_specification;
918
919   return declarator;
920 }
921
922 /* Make a declarator for an array of BOUNDS elements, each of which is
923    defined by ELEMENT.  */
924
925 cp_declarator *
926 make_array_declarator (cp_declarator *element, tree bounds)
927 {
928   cp_declarator *declarator;
929
930   declarator = make_declarator (cdk_array);
931   declarator->declarator = element;
932   declarator->u.array.bounds = bounds;
933
934   return declarator;
935 }
936
937 cp_parameter_declarator *no_parameters;
938
939 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
940    DECLARATOR and DEFAULT_ARGUMENT.  */
941
942 cp_parameter_declarator *
943 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
944                            cp_declarator *declarator,
945                            tree default_argument)
946 {
947   cp_parameter_declarator *parameter;
948
949   parameter = ((cp_parameter_declarator *)
950                alloc_declarator (sizeof (cp_parameter_declarator)));
951   parameter->next = NULL;
952   if (decl_specifiers)
953     parameter->decl_specifiers = *decl_specifiers;
954   else
955     clear_decl_specs (&parameter->decl_specifiers);
956   parameter->declarator = declarator;
957   parameter->default_argument = default_argument;
958   parameter->ellipsis_p = false;
959
960   return parameter;
961 }
962
963 /* The parser.  */
964
965 /* Overview
966    --------
967
968    A cp_parser parses the token stream as specified by the C++
969    grammar.  Its job is purely parsing, not semantic analysis.  For
970    example, the parser breaks the token stream into declarators,
971    expressions, statements, and other similar syntactic constructs.
972    It does not check that the types of the expressions on either side
973    of an assignment-statement are compatible, or that a function is
974    not declared with a parameter of type `void'.
975
976    The parser invokes routines elsewhere in the compiler to perform
977    semantic analysis and to build up the abstract syntax tree for the
978    code processed.
979
980    The parser (and the template instantiation code, which is, in a
981    way, a close relative of parsing) are the only parts of the
982    compiler that should be calling push_scope and pop_scope, or
983    related functions.  The parser (and template instantiation code)
984    keeps track of what scope is presently active; everything else
985    should simply honor that.  (The code that generates static
986    initializers may also need to set the scope, in order to check
987    access control correctly when emitting the initializers.)
988
989    Methodology
990    -----------
991
992    The parser is of the standard recursive-descent variety.  Upcoming
993    tokens in the token stream are examined in order to determine which
994    production to use when parsing a non-terminal.  Some C++ constructs
995    require arbitrary look ahead to disambiguate.  For example, it is
996    impossible, in the general case, to tell whether a statement is an
997    expression or declaration without scanning the entire statement.
998    Therefore, the parser is capable of "parsing tentatively."  When the
999    parser is not sure what construct comes next, it enters this mode.
1000    Then, while we attempt to parse the construct, the parser queues up
1001    error messages, rather than issuing them immediately, and saves the
1002    tokens it consumes.  If the construct is parsed successfully, the
1003    parser "commits", i.e., it issues any queued error messages and
1004    the tokens that were being preserved are permanently discarded.
1005    If, however, the construct is not parsed successfully, the parser
1006    rolls back its state completely so that it can resume parsing using
1007    a different alternative.
1008
1009    Future Improvements
1010    -------------------
1011
1012    The performance of the parser could probably be improved substantially.
1013    We could often eliminate the need to parse tentatively by looking ahead
1014    a little bit.  In some places, this approach might not entirely eliminate
1015    the need to parse tentatively, but it might still speed up the average
1016    case.  */
1017
1018 /* Flags that are passed to some parsing functions.  These values can
1019    be bitwise-ored together.  */
1020
1021 typedef enum cp_parser_flags
1022 {
1023   /* No flags.  */
1024   CP_PARSER_FLAGS_NONE = 0x0,
1025   /* The construct is optional.  If it is not present, then no error
1026      should be issued.  */
1027   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1028   /* When parsing a type-specifier, do not allow user-defined types.  */
1029   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1030 } cp_parser_flags;
1031
1032 /* The different kinds of declarators we want to parse.  */
1033
1034 typedef enum cp_parser_declarator_kind
1035 {
1036   /* We want an abstract declarator.  */
1037   CP_PARSER_DECLARATOR_ABSTRACT,
1038   /* We want a named declarator.  */
1039   CP_PARSER_DECLARATOR_NAMED,
1040   /* We don't mind, but the name must be an unqualified-id.  */
1041   CP_PARSER_DECLARATOR_EITHER
1042 } cp_parser_declarator_kind;
1043
1044 /* The precedence values used to parse binary expressions.  The minimum value
1045    of PREC must be 1, because zero is reserved to quickly discriminate
1046    binary operators from other tokens.  */
1047
1048 enum cp_parser_prec
1049 {
1050   PREC_NOT_OPERATOR,
1051   PREC_LOGICAL_OR_EXPRESSION,
1052   PREC_LOGICAL_AND_EXPRESSION,
1053   PREC_INCLUSIVE_OR_EXPRESSION,
1054   PREC_EXCLUSIVE_OR_EXPRESSION,
1055   PREC_AND_EXPRESSION,
1056   PREC_EQUALITY_EXPRESSION,
1057   PREC_RELATIONAL_EXPRESSION,
1058   PREC_SHIFT_EXPRESSION,
1059   PREC_ADDITIVE_EXPRESSION,
1060   PREC_MULTIPLICATIVE_EXPRESSION,
1061   PREC_PM_EXPRESSION,
1062   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1063 };
1064
1065 /* A mapping from a token type to a corresponding tree node type, with a
1066    precedence value.  */
1067
1068 typedef struct cp_parser_binary_operations_map_node
1069 {
1070   /* The token type.  */
1071   enum cpp_ttype token_type;
1072   /* The corresponding tree code.  */
1073   enum tree_code tree_type;
1074   /* The precedence of this operator.  */
1075   enum cp_parser_prec prec;
1076 } cp_parser_binary_operations_map_node;
1077
1078 /* The status of a tentative parse.  */
1079
1080 typedef enum cp_parser_status_kind
1081 {
1082   /* No errors have occurred.  */
1083   CP_PARSER_STATUS_KIND_NO_ERROR,
1084   /* An error has occurred.  */
1085   CP_PARSER_STATUS_KIND_ERROR,
1086   /* We are committed to this tentative parse, whether or not an error
1087      has occurred.  */
1088   CP_PARSER_STATUS_KIND_COMMITTED
1089 } cp_parser_status_kind;
1090
1091 typedef struct cp_parser_expression_stack_entry
1092 {
1093   tree lhs;
1094   enum tree_code tree_type;
1095   int prec;
1096 } cp_parser_expression_stack_entry;
1097
1098 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1099    entries because precedence levels on the stack are monotonically
1100    increasing.  */
1101 typedef struct cp_parser_expression_stack_entry
1102   cp_parser_expression_stack[NUM_PREC_VALUES];
1103
1104 /* Context that is saved and restored when parsing tentatively.  */
1105 typedef struct cp_parser_context GTY (())
1106 {
1107   /* If this is a tentative parsing context, the status of the
1108      tentative parse.  */
1109   enum cp_parser_status_kind status;
1110   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1111      that are looked up in this context must be looked up both in the
1112      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1113      the context of the containing expression.  */
1114   tree object_type;
1115
1116   /* The next parsing context in the stack.  */
1117   struct cp_parser_context *next;
1118 } cp_parser_context;
1119
1120 /* Prototypes.  */
1121
1122 /* Constructors and destructors.  */
1123
1124 static cp_parser_context *cp_parser_context_new
1125   (cp_parser_context *);
1126
1127 /* Class variables.  */
1128
1129 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1130
1131 /* The operator-precedence table used by cp_parser_binary_expression.
1132    Transformed into an associative array (binops_by_token) by
1133    cp_parser_new.  */
1134
1135 static const cp_parser_binary_operations_map_node binops[] = {
1136   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1137   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1138
1139   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1142
1143   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1145
1146   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1148
1149   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1151   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1153   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1154   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1155
1156   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1157   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1158
1159   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1160
1161   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1162
1163   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1164
1165   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1166
1167   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1168 };
1169
1170 /* The same as binops, but initialized by cp_parser_new so that
1171    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1172    for speed.  */
1173 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1174
1175 /* Constructors and destructors.  */
1176
1177 /* Construct a new context.  The context below this one on the stack
1178    is given by NEXT.  */
1179
1180 static cp_parser_context *
1181 cp_parser_context_new (cp_parser_context* next)
1182 {
1183   cp_parser_context *context;
1184
1185   /* Allocate the storage.  */
1186   if (cp_parser_context_free_list != NULL)
1187     {
1188       /* Pull the first entry from the free list.  */
1189       context = cp_parser_context_free_list;
1190       cp_parser_context_free_list = context->next;
1191       memset (context, 0, sizeof (*context));
1192     }
1193   else
1194     context = GGC_CNEW (cp_parser_context);
1195
1196   /* No errors have occurred yet in this context.  */
1197   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1198   /* If this is not the bottomost context, copy information that we
1199      need from the previous context.  */
1200   if (next)
1201     {
1202       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1203          expression, then we are parsing one in this context, too.  */
1204       context->object_type = next->object_type;
1205       /* Thread the stack.  */
1206       context->next = next;
1207     }
1208
1209   return context;
1210 }
1211
1212 /* The cp_parser structure represents the C++ parser.  */
1213
1214 typedef struct cp_parser GTY(())
1215 {
1216   /* The lexer from which we are obtaining tokens.  */
1217   cp_lexer *lexer;
1218
1219   /* The scope in which names should be looked up.  If NULL_TREE, then
1220      we look up names in the scope that is currently open in the
1221      source program.  If non-NULL, this is either a TYPE or
1222      NAMESPACE_DECL for the scope in which we should look.  It can
1223      also be ERROR_MARK, when we've parsed a bogus scope.
1224
1225      This value is not cleared automatically after a name is looked
1226      up, so we must be careful to clear it before starting a new look
1227      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1228      will look up `Z' in the scope of `X', rather than the current
1229      scope.)  Unfortunately, it is difficult to tell when name lookup
1230      is complete, because we sometimes peek at a token, look it up,
1231      and then decide not to consume it.   */
1232   tree scope;
1233
1234   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1235      last lookup took place.  OBJECT_SCOPE is used if an expression
1236      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1237      respectively.  QUALIFYING_SCOPE is used for an expression of the
1238      form "X::Y"; it refers to X.  */
1239   tree object_scope;
1240   tree qualifying_scope;
1241
1242   /* A stack of parsing contexts.  All but the bottom entry on the
1243      stack will be tentative contexts.
1244
1245      We parse tentatively in order to determine which construct is in
1246      use in some situations.  For example, in order to determine
1247      whether a statement is an expression-statement or a
1248      declaration-statement we parse it tentatively as a
1249      declaration-statement.  If that fails, we then reparse the same
1250      token stream as an expression-statement.  */
1251   cp_parser_context *context;
1252
1253   /* True if we are parsing GNU C++.  If this flag is not set, then
1254      GNU extensions are not recognized.  */
1255   bool allow_gnu_extensions_p;
1256
1257   /* TRUE if the `>' token should be interpreted as the greater-than
1258      operator.  FALSE if it is the end of a template-id or
1259      template-parameter-list.  */
1260   bool greater_than_is_operator_p;
1261
1262   /* TRUE if default arguments are allowed within a parameter list
1263      that starts at this point. FALSE if only a gnu extension makes
1264      them permissible.  */
1265   bool default_arg_ok_p;
1266
1267   /* TRUE if we are parsing an integral constant-expression.  See
1268      [expr.const] for a precise definition.  */
1269   bool integral_constant_expression_p;
1270
1271   /* TRUE if we are parsing an integral constant-expression -- but a
1272      non-constant expression should be permitted as well.  This flag
1273      is used when parsing an array bound so that GNU variable-length
1274      arrays are tolerated.  */
1275   bool allow_non_integral_constant_expression_p;
1276
1277   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1278      been seen that makes the expression non-constant.  */
1279   bool non_integral_constant_expression_p;
1280
1281   /* TRUE if local variable names and `this' are forbidden in the
1282      current context.  */
1283   bool local_variables_forbidden_p;
1284
1285   /* TRUE if the declaration we are parsing is part of a
1286      linkage-specification of the form `extern string-literal
1287      declaration'.  */
1288   bool in_unbraced_linkage_specification_p;
1289
1290   /* TRUE if we are presently parsing a declarator, after the
1291      direct-declarator.  */
1292   bool in_declarator_p;
1293
1294   /* TRUE if we are presently parsing a template-argument-list.  */
1295   bool in_template_argument_list_p;
1296
1297   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1298      to IN_OMP_BLOCK if parsing OpenMP structured block and
1299      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1300      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1301      iteration-statement, OpenMP block or loop within that switch.  */
1302 #define IN_SWITCH_STMT          1
1303 #define IN_ITERATION_STMT       2
1304 #define IN_OMP_BLOCK            4
1305 #define IN_OMP_FOR              8
1306   unsigned char in_statement;
1307
1308   /* TRUE if we are presently parsing the body of a switch statement.
1309      Note that this doesn't quite overlap with in_statement above.
1310      The difference relates to giving the right sets of error messages:
1311      "case not in switch" vs "break statement used with OpenMP...".  */
1312   bool in_switch_statement_p;
1313
1314   /* TRUE if we are parsing a type-id in an expression context.  In
1315      such a situation, both "type (expr)" and "type (type)" are valid
1316      alternatives.  */
1317   bool in_type_id_in_expr_p;
1318
1319   /* TRUE if we are currently in a header file where declarations are
1320      implicitly extern "C".  */
1321   bool implicit_extern_c;
1322
1323   /* TRUE if strings in expressions should be translated to the execution
1324      character set.  */
1325   bool translate_strings_p;
1326
1327   /* If non-NULL, then we are parsing a construct where new type
1328      definitions are not permitted.  The string stored here will be
1329      issued as an error message if a type is defined.  */
1330   const char *type_definition_forbidden_message;
1331
1332   /* A list of lists. The outer list is a stack, used for member
1333      functions of local classes. At each level there are two sub-list,
1334      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1335      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1336      TREE_VALUE's. The functions are chained in reverse declaration
1337      order.
1338
1339      The TREE_PURPOSE sublist contains those functions with default
1340      arguments that need post processing, and the TREE_VALUE sublist
1341      contains those functions with definitions that need post
1342      processing.
1343
1344      These lists can only be processed once the outermost class being
1345      defined is complete.  */
1346   tree unparsed_functions_queues;
1347
1348   /* The number of classes whose definitions are currently in
1349      progress.  */
1350   unsigned num_classes_being_defined;
1351
1352   /* The number of template parameter lists that apply directly to the
1353      current declaration.  */
1354   unsigned num_template_parameter_lists;
1355 } 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, bool);
1394 static tree cp_parser_unqualified_id
1395   (cp_parser *, bool, 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 *, tree, 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 *, bool);
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 void cp_parser_perform_template_parameter_access_checks
1730   (tree);
1731 static tree cp_parser_single_declaration
1732   (cp_parser *, tree, bool, bool *);
1733 static tree cp_parser_functional_cast
1734   (cp_parser *, tree);
1735 static tree cp_parser_save_member_function_body
1736   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1737 static tree cp_parser_enclosed_template_argument_list
1738   (cp_parser *);
1739 static void cp_parser_save_default_args
1740   (cp_parser *, tree);
1741 static void cp_parser_late_parsing_for_member
1742   (cp_parser *, tree);
1743 static void cp_parser_late_parsing_default_args
1744   (cp_parser *, tree);
1745 static tree cp_parser_sizeof_operand
1746   (cp_parser *, enum rid);
1747 static bool cp_parser_declares_only_class_p
1748   (cp_parser *);
1749 static void cp_parser_set_storage_class
1750   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1751 static void cp_parser_set_decl_spec_type
1752   (cp_decl_specifier_seq *, tree, bool);
1753 static bool cp_parser_friend_p
1754   (const cp_decl_specifier_seq *);
1755 static cp_token *cp_parser_require
1756   (cp_parser *, enum cpp_ttype, const char *);
1757 static cp_token *cp_parser_require_keyword
1758   (cp_parser *, enum rid, const char *);
1759 static bool cp_parser_token_starts_function_definition_p
1760   (cp_token *);
1761 static bool cp_parser_next_token_starts_class_definition_p
1762   (cp_parser *);
1763 static bool cp_parser_next_token_ends_template_argument_p
1764   (cp_parser *);
1765 static bool cp_parser_nth_token_starts_template_argument_list_p
1766   (cp_parser *, size_t);
1767 static enum tag_types cp_parser_token_is_class_key
1768   (cp_token *);
1769 static void cp_parser_check_class_key
1770   (enum tag_types, tree type);
1771 static void cp_parser_check_access_in_redeclaration
1772   (tree type);
1773 static bool cp_parser_optional_template_keyword
1774   (cp_parser *);
1775 static void cp_parser_pre_parsed_nested_name_specifier
1776   (cp_parser *);
1777 static void cp_parser_cache_group
1778   (cp_parser *, enum cpp_ttype, unsigned);
1779 static void cp_parser_parse_tentatively
1780   (cp_parser *);
1781 static void cp_parser_commit_to_tentative_parse
1782   (cp_parser *);
1783 static void cp_parser_abort_tentative_parse
1784   (cp_parser *);
1785 static bool cp_parser_parse_definitely
1786   (cp_parser *);
1787 static inline bool cp_parser_parsing_tentatively
1788   (cp_parser *);
1789 static bool cp_parser_uncommitted_to_tentative_parse_p
1790   (cp_parser *);
1791 static void cp_parser_error
1792   (cp_parser *, const char *);
1793 static void cp_parser_name_lookup_error
1794   (cp_parser *, tree, tree, const char *);
1795 static bool cp_parser_simulate_error
1796   (cp_parser *);
1797 static void cp_parser_check_type_definition
1798   (cp_parser *);
1799 static void cp_parser_check_for_definition_in_return_type
1800   (cp_declarator *, tree);
1801 static void cp_parser_check_for_invalid_template_id
1802   (cp_parser *, tree);
1803 static bool cp_parser_non_integral_constant_expression
1804   (cp_parser *, const char *);
1805 static void cp_parser_diagnose_invalid_type_name
1806   (cp_parser *, tree, tree);
1807 static bool cp_parser_parse_and_diagnose_invalid_type_name
1808   (cp_parser *);
1809 static int cp_parser_skip_to_closing_parenthesis
1810   (cp_parser *, bool, bool, bool);
1811 static void cp_parser_skip_to_end_of_statement
1812   (cp_parser *);
1813 static void cp_parser_consume_semicolon_at_end_of_statement
1814   (cp_parser *);
1815 static void cp_parser_skip_to_end_of_block_or_statement
1816   (cp_parser *);
1817 static void cp_parser_skip_to_closing_brace
1818   (cp_parser *);
1819 static void cp_parser_skip_until_found
1820   (cp_parser *, enum cpp_ttype, const char *);
1821 static void cp_parser_skip_to_pragma_eol
1822   (cp_parser*, cp_token *);
1823 static bool cp_parser_error_occurred
1824   (cp_parser *);
1825 static bool cp_parser_allow_gnu_extensions_p
1826   (cp_parser *);
1827 static bool cp_parser_is_string_literal
1828   (cp_token *);
1829 static bool cp_parser_is_keyword
1830   (cp_token *, enum rid);
1831 static tree cp_parser_make_typename_type
1832   (cp_parser *, tree, tree);
1833
1834 /* Returns nonzero if we are parsing tentatively.  */
1835
1836 static inline bool
1837 cp_parser_parsing_tentatively (cp_parser* parser)
1838 {
1839   return parser->context->next != NULL;
1840 }
1841
1842 /* Returns nonzero if TOKEN is a string literal.  */
1843
1844 static bool
1845 cp_parser_is_string_literal (cp_token* token)
1846 {
1847   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1848 }
1849
1850 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1851
1852 static bool
1853 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1854 {
1855   return token->keyword == keyword;
1856 }
1857
1858 /* A minimum or maximum operator has been seen.  As these are
1859    deprecated, issue a warning.  */
1860
1861 static inline void
1862 cp_parser_warn_min_max (void)
1863 {
1864   if (warn_deprecated && !in_system_header)
1865     warning (OPT_Wdeprecated, "minimum/maximum operators are deprecated");
1866 }
1867
1868 /* If not parsing tentatively, issue a diagnostic of the form
1869       FILE:LINE: MESSAGE before TOKEN
1870    where TOKEN is the next token in the input stream.  MESSAGE
1871    (specified by the caller) is usually of the form "expected
1872    OTHER-TOKEN".  */
1873
1874 static void
1875 cp_parser_error (cp_parser* parser, const char* message)
1876 {
1877   if (!cp_parser_simulate_error (parser))
1878     {
1879       cp_token *token = cp_lexer_peek_token (parser->lexer);
1880       /* This diagnostic makes more sense if it is tagged to the line
1881          of the token we just peeked at.  */
1882       cp_lexer_set_source_position_from_token (token);
1883
1884       if (token->type == CPP_PRAGMA)
1885         {
1886           error ("%<#pragma%> is not allowed here");
1887           cp_parser_skip_to_pragma_eol (parser, token);
1888           return;
1889         }
1890
1891       c_parse_error (message,
1892                      /* Because c_parser_error does not understand
1893                         CPP_KEYWORD, keywords are treated like
1894                         identifiers.  */
1895                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1896                      token->value);
1897     }
1898 }
1899
1900 /* Issue an error about name-lookup failing.  NAME is the
1901    IDENTIFIER_NODE DECL is the result of
1902    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1903    the thing that we hoped to find.  */
1904
1905 static void
1906 cp_parser_name_lookup_error (cp_parser* parser,
1907                              tree name,
1908                              tree decl,
1909                              const char* desired)
1910 {
1911   /* If name lookup completely failed, tell the user that NAME was not
1912      declared.  */
1913   if (decl == error_mark_node)
1914     {
1915       if (parser->scope && parser->scope != global_namespace)
1916         error ("%<%D::%D%> has not been declared",
1917                parser->scope, name);
1918       else if (parser->scope == global_namespace)
1919         error ("%<::%D%> has not been declared", name);
1920       else if (parser->object_scope
1921                && !CLASS_TYPE_P (parser->object_scope))
1922         error ("request for member %qD in non-class type %qT",
1923                name, parser->object_scope);
1924       else if (parser->object_scope)
1925         error ("%<%T::%D%> has not been declared",
1926                parser->object_scope, name);
1927       else
1928         error ("%qD has not been declared", name);
1929     }
1930   else if (parser->scope && parser->scope != global_namespace)
1931     error ("%<%D::%D%> %s", parser->scope, name, desired);
1932   else if (parser->scope == global_namespace)
1933     error ("%<::%D%> %s", name, desired);
1934   else
1935     error ("%qD %s", name, desired);
1936 }
1937
1938 /* If we are parsing tentatively, remember that an error has occurred
1939    during this tentative parse.  Returns true if the error was
1940    simulated; false if a message should be issued by the caller.  */
1941
1942 static bool
1943 cp_parser_simulate_error (cp_parser* parser)
1944 {
1945   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1946     {
1947       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1948       return true;
1949     }
1950   return false;
1951 }
1952
1953 /* This function is called when a type is defined.  If type
1954    definitions are forbidden at this point, an error message is
1955    issued.  */
1956
1957 static void
1958 cp_parser_check_type_definition (cp_parser* parser)
1959 {
1960   /* If types are forbidden here, issue a message.  */
1961   if (parser->type_definition_forbidden_message)
1962     /* Use `%s' to print the string in case there are any escape
1963        characters in the message.  */
1964     error ("%s", parser->type_definition_forbidden_message);
1965 }
1966
1967 /* This function is called when the DECLARATOR is processed.  The TYPE
1968    was a type defined in the decl-specifiers.  If it is invalid to
1969    define a type in the decl-specifiers for DECLARATOR, an error is
1970    issued.  */
1971
1972 static void
1973 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1974                                                tree type)
1975 {
1976   /* [dcl.fct] forbids type definitions in return types.
1977      Unfortunately, it's not easy to know whether or not we are
1978      processing a return type until after the fact.  */
1979   while (declarator
1980          && (declarator->kind == cdk_pointer
1981              || declarator->kind == cdk_reference
1982              || declarator->kind == cdk_ptrmem))
1983     declarator = declarator->declarator;
1984   if (declarator
1985       && declarator->kind == cdk_function)
1986     {
1987       error ("new types may not be defined in a return type");
1988       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1989               type);
1990     }
1991 }
1992
1993 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1994    "<" in any valid C++ program.  If the next token is indeed "<",
1995    issue a message warning the user about what appears to be an
1996    invalid attempt to form a template-id.  */
1997
1998 static void
1999 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2000                                          tree type)
2001 {
2002   cp_token_position start = 0;
2003
2004   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2005     {
2006       if (TYPE_P (type))
2007         error ("%qT is not a template", type);
2008       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2009         error ("%qE is not a template", type);
2010       else
2011         error ("invalid template-id");
2012       /* Remember the location of the invalid "<".  */
2013       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2014         start = cp_lexer_token_position (parser->lexer, true);
2015       /* Consume the "<".  */
2016       cp_lexer_consume_token (parser->lexer);
2017       /* Parse the template arguments.  */
2018       cp_parser_enclosed_template_argument_list (parser);
2019       /* Permanently remove the invalid template arguments so that
2020          this error message is not issued again.  */
2021       if (start)
2022         cp_lexer_purge_tokens_after (parser->lexer, start);
2023     }
2024 }
2025
2026 /* If parsing an integral constant-expression, issue an error message
2027    about the fact that THING appeared and return true.  Otherwise,
2028    return false.  In either case, set
2029    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2030
2031 static bool
2032 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2033                                             const char *thing)
2034 {
2035   parser->non_integral_constant_expression_p = true;
2036   if (parser->integral_constant_expression_p)
2037     {
2038       if (!parser->allow_non_integral_constant_expression_p)
2039         {
2040           error ("%s cannot appear in a constant-expression", thing);
2041           return true;
2042         }
2043     }
2044   return false;
2045 }
2046
2047 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2048    qualifying scope (or NULL, if none) for ID.  This function commits
2049    to the current active tentative parse, if any.  (Otherwise, the
2050    problematic construct might be encountered again later, resulting
2051    in duplicate error messages.)  */
2052
2053 static void
2054 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2055 {
2056   tree decl, old_scope;
2057   /* Try to lookup the identifier.  */
2058   old_scope = parser->scope;
2059   parser->scope = scope;
2060   decl = cp_parser_lookup_name_simple (parser, id);
2061   parser->scope = old_scope;
2062   /* If the lookup found a template-name, it means that the user forgot
2063   to specify an argument list. Emit a useful error message.  */
2064   if (TREE_CODE (decl) == TEMPLATE_DECL)
2065     error ("invalid use of template-name %qE without an argument list",
2066       decl);
2067   else if (!parser->scope)
2068     {
2069       /* Issue an error message.  */
2070       error ("%qE does not name a type", id);
2071       /* If we're in a template class, it's possible that the user was
2072          referring to a type from a base class.  For example:
2073
2074            template <typename T> struct A { typedef T X; };
2075            template <typename T> struct B : public A<T> { X x; };
2076
2077          The user should have said "typename A<T>::X".  */
2078       if (processing_template_decl && current_class_type
2079           && TYPE_BINFO (current_class_type))
2080         {
2081           tree b;
2082
2083           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2084                b;
2085                b = TREE_CHAIN (b))
2086             {
2087               tree base_type = BINFO_TYPE (b);
2088               if (CLASS_TYPE_P (base_type)
2089                   && dependent_type_p (base_type))
2090                 {
2091                   tree field;
2092                   /* Go from a particular instantiation of the
2093                      template (which will have an empty TYPE_FIELDs),
2094                      to the main version.  */
2095                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2096                   for (field = TYPE_FIELDS (base_type);
2097                        field;
2098                        field = TREE_CHAIN (field))
2099                     if (TREE_CODE (field) == TYPE_DECL
2100                         && DECL_NAME (field) == id)
2101                       {
2102                         inform ("(perhaps %<typename %T::%E%> was intended)",
2103                                 BINFO_TYPE (b), id);
2104                         break;
2105                       }
2106                   if (field)
2107                     break;
2108                 }
2109             }
2110         }
2111     }
2112   /* Here we diagnose qualified-ids where the scope is actually correct,
2113      but the identifier does not resolve to a valid type name.  */
2114   else if (parser->scope != error_mark_node)
2115     {
2116       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2117         error ("%qE in namespace %qE does not name a type",
2118                id, parser->scope);
2119       else if (TYPE_P (parser->scope))
2120         error ("%qE in class %qT does not name a type", id, parser->scope);
2121       else
2122         gcc_unreachable ();
2123     }
2124   cp_parser_commit_to_tentative_parse (parser);
2125 }
2126
2127 /* Check for a common situation where a type-name should be present,
2128    but is not, and issue a sensible error message.  Returns true if an
2129    invalid type-name was detected.
2130
2131    The situation handled by this function are variable declarations of the
2132    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2133    Usually, `ID' should name a type, but if we got here it means that it
2134    does not. We try to emit the best possible error message depending on
2135    how exactly the id-expression looks like.
2136 */
2137
2138 static bool
2139 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2140 {
2141   tree id;
2142
2143   cp_parser_parse_tentatively (parser);
2144   id = cp_parser_id_expression (parser,
2145                                 /*template_keyword_p=*/false,
2146                                 /*check_dependency_p=*/true,
2147                                 /*template_p=*/NULL,
2148                                 /*declarator_p=*/true,
2149                                 /*optional_p=*/false);
2150   /* After the id-expression, there should be a plain identifier,
2151      otherwise this is not a simple variable declaration. Also, if
2152      the scope is dependent, we cannot do much.  */
2153   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2154       || (parser->scope && TYPE_P (parser->scope)
2155           && dependent_type_p (parser->scope)))
2156     {
2157       cp_parser_abort_tentative_parse (parser);
2158       return false;
2159     }
2160   if (!cp_parser_parse_definitely (parser)
2161       || TREE_CODE (id) != IDENTIFIER_NODE)
2162     return false;
2163
2164   /* Emit a diagnostic for the invalid type.  */
2165   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2166   /* Skip to the end of the declaration; there's no point in
2167      trying to process it.  */
2168   cp_parser_skip_to_end_of_block_or_statement (parser);
2169   return true;
2170 }
2171
2172 /* Consume tokens up to, and including, the next non-nested closing `)'.
2173    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2174    are doing error recovery. Returns -1 if OR_COMMA is true and we
2175    found an unnested comma.  */
2176
2177 static int
2178 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2179                                        bool recovering,
2180                                        bool or_comma,
2181                                        bool consume_paren)
2182 {
2183   unsigned paren_depth = 0;
2184   unsigned brace_depth = 0;
2185
2186   if (recovering && !or_comma
2187       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2188     return 0;
2189
2190   while (true)
2191     {
2192       cp_token * token = cp_lexer_peek_token (parser->lexer);
2193
2194       switch (token->type)
2195         {
2196         case CPP_EOF:
2197         case CPP_PRAGMA_EOL:
2198           /* If we've run out of tokens, then there is no closing `)'.  */
2199           return 0;
2200
2201         case CPP_SEMICOLON:
2202           /* This matches the processing in skip_to_end_of_statement.  */
2203           if (!brace_depth)
2204             return 0;
2205           break;
2206
2207         case CPP_OPEN_BRACE:
2208           ++brace_depth;
2209           break;
2210         case CPP_CLOSE_BRACE:
2211           if (!brace_depth--)
2212             return 0;
2213           break;
2214
2215         case CPP_COMMA:
2216           if (recovering && or_comma && !brace_depth && !paren_depth)
2217             return -1;
2218           break;
2219
2220         case CPP_OPEN_PAREN:
2221           if (!brace_depth)
2222             ++paren_depth;
2223           break;
2224
2225         case CPP_CLOSE_PAREN:
2226           if (!brace_depth && !paren_depth--)
2227             {
2228               if (consume_paren)
2229                 cp_lexer_consume_token (parser->lexer);
2230               return 1;
2231             }
2232           break;
2233
2234         default:
2235           break;
2236         }
2237
2238       /* Consume the token.  */
2239       cp_lexer_consume_token (parser->lexer);
2240     }
2241 }
2242
2243 /* Consume tokens until we reach the end of the current statement.
2244    Normally, that will be just before consuming a `;'.  However, if a
2245    non-nested `}' comes first, then we stop before consuming that.  */
2246
2247 static void
2248 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2249 {
2250   unsigned nesting_depth = 0;
2251
2252   while (true)
2253     {
2254       cp_token *token = cp_lexer_peek_token (parser->lexer);
2255
2256       switch (token->type)
2257         {
2258         case CPP_EOF:
2259         case CPP_PRAGMA_EOL:
2260           /* If we've run out of tokens, stop.  */
2261           return;
2262
2263         case CPP_SEMICOLON:
2264           /* If the next token is a `;', we have reached the end of the
2265              statement.  */
2266           if (!nesting_depth)
2267             return;
2268           break;
2269
2270         case CPP_CLOSE_BRACE:
2271           /* If this is a non-nested '}', stop before consuming it.
2272              That way, when confronted with something like:
2273
2274                { 3 + }
2275
2276              we stop before consuming the closing '}', even though we
2277              have not yet reached a `;'.  */
2278           if (nesting_depth == 0)
2279             return;
2280
2281           /* If it is the closing '}' for a block that we have
2282              scanned, stop -- but only after consuming the token.
2283              That way given:
2284
2285                 void f g () { ... }
2286                 typedef int I;
2287
2288              we will stop after the body of the erroneously declared
2289              function, but before consuming the following `typedef'
2290              declaration.  */
2291           if (--nesting_depth == 0)
2292             {
2293               cp_lexer_consume_token (parser->lexer);
2294               return;
2295             }
2296
2297         case CPP_OPEN_BRACE:
2298           ++nesting_depth;
2299           break;
2300
2301         default:
2302           break;
2303         }
2304
2305       /* Consume the token.  */
2306       cp_lexer_consume_token (parser->lexer);
2307     }
2308 }
2309
2310 /* This function is called at the end of a statement or declaration.
2311    If the next token is a semicolon, it is consumed; otherwise, error
2312    recovery is attempted.  */
2313
2314 static void
2315 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2316 {
2317   /* Look for the trailing `;'.  */
2318   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2319     {
2320       /* If there is additional (erroneous) input, skip to the end of
2321          the statement.  */
2322       cp_parser_skip_to_end_of_statement (parser);
2323       /* If the next token is now a `;', consume it.  */
2324       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2325         cp_lexer_consume_token (parser->lexer);
2326     }
2327 }
2328
2329 /* Skip tokens until we have consumed an entire block, or until we
2330    have consumed a non-nested `;'.  */
2331
2332 static void
2333 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2334 {
2335   int nesting_depth = 0;
2336
2337   while (nesting_depth >= 0)
2338     {
2339       cp_token *token = cp_lexer_peek_token (parser->lexer);
2340
2341       switch (token->type)
2342         {
2343         case CPP_EOF:
2344         case CPP_PRAGMA_EOL:
2345           /* If we've run out of tokens, stop.  */
2346           return;
2347
2348         case CPP_SEMICOLON:
2349           /* Stop if this is an unnested ';'. */
2350           if (!nesting_depth)
2351             nesting_depth = -1;
2352           break;
2353
2354         case CPP_CLOSE_BRACE:
2355           /* Stop if this is an unnested '}', or closes the outermost
2356              nesting level.  */
2357           nesting_depth--;
2358           if (!nesting_depth)
2359             nesting_depth = -1;
2360           break;
2361
2362         case CPP_OPEN_BRACE:
2363           /* Nest. */
2364           nesting_depth++;
2365           break;
2366
2367         default:
2368           break;
2369         }
2370
2371       /* Consume the token.  */
2372       cp_lexer_consume_token (parser->lexer);
2373     }
2374 }
2375
2376 /* Skip tokens until a non-nested closing curly brace is the next
2377    token.  */
2378
2379 static void
2380 cp_parser_skip_to_closing_brace (cp_parser *parser)
2381 {
2382   unsigned nesting_depth = 0;
2383
2384   while (true)
2385     {
2386       cp_token *token = cp_lexer_peek_token (parser->lexer);
2387
2388       switch (token->type)
2389         {
2390         case CPP_EOF:
2391         case CPP_PRAGMA_EOL:
2392           /* If we've run out of tokens, stop.  */
2393           return;
2394
2395         case CPP_CLOSE_BRACE:
2396           /* If the next token is a non-nested `}', then we have reached
2397              the end of the current block.  */
2398           if (nesting_depth-- == 0)
2399             return;
2400           break;
2401
2402         case CPP_OPEN_BRACE:
2403           /* If it the next token is a `{', then we are entering a new
2404              block.  Consume the entire block.  */
2405           ++nesting_depth;
2406           break;
2407
2408         default:
2409           break;
2410         }
2411
2412       /* Consume the token.  */
2413       cp_lexer_consume_token (parser->lexer);
2414     }
2415 }
2416
2417 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2418    parameter is the PRAGMA token, allowing us to purge the entire pragma
2419    sequence.  */
2420
2421 static void
2422 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2423 {
2424   cp_token *token;
2425
2426   parser->lexer->in_pragma = false;
2427
2428   do
2429     token = cp_lexer_consume_token (parser->lexer);
2430   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2431
2432   /* Ensure that the pragma is not parsed again.  */
2433   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2434 }
2435
2436 /* Require pragma end of line, resyncing with it as necessary.  The
2437    arguments are as for cp_parser_skip_to_pragma_eol.  */
2438
2439 static void
2440 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2441 {
2442   parser->lexer->in_pragma = false;
2443   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2444     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2445 }
2446
2447 /* This is a simple wrapper around make_typename_type. When the id is
2448    an unresolved identifier node, we can provide a superior diagnostic
2449    using cp_parser_diagnose_invalid_type_name.  */
2450
2451 static tree
2452 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2453 {
2454   tree result;
2455   if (TREE_CODE (id) == IDENTIFIER_NODE)
2456     {
2457       result = make_typename_type (scope, id, typename_type,
2458                                    /*complain=*/tf_none);
2459       if (result == error_mark_node)
2460         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2461       return result;
2462     }
2463   return make_typename_type (scope, id, typename_type, tf_error);
2464 }
2465
2466
2467 /* Create a new C++ parser.  */
2468
2469 static cp_parser *
2470 cp_parser_new (void)
2471 {
2472   cp_parser *parser;
2473   cp_lexer *lexer;
2474   unsigned i;
2475
2476   /* cp_lexer_new_main is called before calling ggc_alloc because
2477      cp_lexer_new_main might load a PCH file.  */
2478   lexer = cp_lexer_new_main ();
2479
2480   /* Initialize the binops_by_token so that we can get the tree
2481      directly from the token.  */
2482   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2483     binops_by_token[binops[i].token_type] = binops[i];
2484
2485   parser = GGC_CNEW (cp_parser);
2486   parser->lexer = lexer;
2487   parser->context = cp_parser_context_new (NULL);
2488
2489   /* For now, we always accept GNU extensions.  */
2490   parser->allow_gnu_extensions_p = 1;
2491
2492   /* The `>' token is a greater-than operator, not the end of a
2493      template-id.  */
2494   parser->greater_than_is_operator_p = true;
2495
2496   parser->default_arg_ok_p = true;
2497
2498   /* We are not parsing a constant-expression.  */
2499   parser->integral_constant_expression_p = false;
2500   parser->allow_non_integral_constant_expression_p = false;
2501   parser->non_integral_constant_expression_p = false;
2502
2503   /* Local variable names are not forbidden.  */
2504   parser->local_variables_forbidden_p = false;
2505
2506   /* We are not processing an `extern "C"' declaration.  */
2507   parser->in_unbraced_linkage_specification_p = false;
2508
2509   /* We are not processing a declarator.  */
2510   parser->in_declarator_p = false;
2511
2512   /* We are not processing a template-argument-list.  */
2513   parser->in_template_argument_list_p = false;
2514
2515   /* We are not in an iteration statement.  */
2516   parser->in_statement = 0;
2517
2518   /* We are not in a switch statement.  */
2519   parser->in_switch_statement_p = false;
2520
2521   /* We are not parsing a type-id inside an expression.  */
2522   parser->in_type_id_in_expr_p = false;
2523
2524   /* Declarations aren't implicitly extern "C".  */
2525   parser->implicit_extern_c = false;
2526
2527   /* String literals should be translated to the execution character set.  */
2528   parser->translate_strings_p = true;
2529
2530   /* The unparsed function queue is empty.  */
2531   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2532
2533   /* There are no classes being defined.  */
2534   parser->num_classes_being_defined = 0;
2535
2536   /* No template parameters apply.  */
2537   parser->num_template_parameter_lists = 0;
2538
2539   return parser;
2540 }
2541
2542 /* Create a cp_lexer structure which will emit the tokens in CACHE
2543    and push it onto the parser's lexer stack.  This is used for delayed
2544    parsing of in-class method bodies and default arguments, and should
2545    not be confused with tentative parsing.  */
2546 static void
2547 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2548 {
2549   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2550   lexer->next = parser->lexer;
2551   parser->lexer = lexer;
2552
2553   /* Move the current source position to that of the first token in the
2554      new lexer.  */
2555   cp_lexer_set_source_position_from_token (lexer->next_token);
2556 }
2557
2558 /* Pop the top lexer off the parser stack.  This is never used for the
2559    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2560 static void
2561 cp_parser_pop_lexer (cp_parser *parser)
2562 {
2563   cp_lexer *lexer = parser->lexer;
2564   parser->lexer = lexer->next;
2565   cp_lexer_destroy (lexer);
2566
2567   /* Put the current source position back where it was before this
2568      lexer was pushed.  */
2569   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2570 }
2571
2572 /* Lexical conventions [gram.lex]  */
2573
2574 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2575    identifier.  */
2576
2577 static tree
2578 cp_parser_identifier (cp_parser* parser)
2579 {
2580   cp_token *token;
2581
2582   /* Look for the identifier.  */
2583   token = cp_parser_require (parser, CPP_NAME, "identifier");
2584   /* Return the value.  */
2585   return token ? token->value : error_mark_node;
2586 }
2587
2588 /* Parse a sequence of adjacent string constants.  Returns a
2589    TREE_STRING representing the combined, nul-terminated string
2590    constant.  If TRANSLATE is true, translate the string to the
2591    execution character set.  If WIDE_OK is true, a wide string is
2592    invalid here.
2593
2594    C++98 [lex.string] says that if a narrow string literal token is
2595    adjacent to a wide string literal token, the behavior is undefined.
2596    However, C99 6.4.5p4 says that this results in a wide string literal.
2597    We follow C99 here, for consistency with the C front end.
2598
2599    This code is largely lifted from lex_string() in c-lex.c.
2600
2601    FUTURE: ObjC++ will need to handle @-strings here.  */
2602 static tree
2603 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2604 {
2605   tree value;
2606   bool wide = false;
2607   size_t count;
2608   struct obstack str_ob;
2609   cpp_string str, istr, *strs;
2610   cp_token *tok;
2611
2612   tok = cp_lexer_peek_token (parser->lexer);
2613   if (!cp_parser_is_string_literal (tok))
2614     {
2615       cp_parser_error (parser, "expected string-literal");
2616       return error_mark_node;
2617     }
2618
2619   /* Try to avoid the overhead of creating and destroying an obstack
2620      for the common case of just one string.  */
2621   if (!cp_parser_is_string_literal
2622       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2623     {
2624       cp_lexer_consume_token (parser->lexer);
2625
2626       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2627       str.len = TREE_STRING_LENGTH (tok->value);
2628       count = 1;
2629       if (tok->type == CPP_WSTRING)
2630         wide = true;
2631
2632       strs = &str;
2633     }
2634   else
2635     {
2636       gcc_obstack_init (&str_ob);
2637       count = 0;
2638
2639       do
2640         {
2641           cp_lexer_consume_token (parser->lexer);
2642           count++;
2643           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2644           str.len = TREE_STRING_LENGTH (tok->value);
2645           if (tok->type == CPP_WSTRING)
2646             wide = true;
2647
2648           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2649
2650           tok = cp_lexer_peek_token (parser->lexer);
2651         }
2652       while (cp_parser_is_string_literal (tok));
2653
2654       strs = (cpp_string *) obstack_finish (&str_ob);
2655     }
2656
2657   if (wide && !wide_ok)
2658     {
2659       cp_parser_error (parser, "a wide string is invalid in this context");
2660       wide = false;
2661     }
2662
2663   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2664       (parse_in, strs, count, &istr, wide))
2665     {
2666       value = build_string (istr.len, (char *)istr.text);
2667       free ((void *)istr.text);
2668
2669       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2670       value = fix_string_type (value);
2671     }
2672   else
2673     /* cpp_interpret_string has issued an error.  */
2674     value = error_mark_node;
2675
2676   if (count > 1)
2677     obstack_free (&str_ob, 0);
2678
2679   return value;
2680 }
2681
2682
2683 /* Basic concepts [gram.basic]  */
2684
2685 /* Parse a translation-unit.
2686
2687    translation-unit:
2688      declaration-seq [opt]
2689
2690    Returns TRUE if all went well.  */
2691
2692 static bool
2693 cp_parser_translation_unit (cp_parser* parser)
2694 {
2695   /* The address of the first non-permanent object on the declarator
2696      obstack.  */
2697   static void *declarator_obstack_base;
2698
2699   bool success;
2700
2701   /* Create the declarator obstack, if necessary.  */
2702   if (!cp_error_declarator)
2703     {
2704       gcc_obstack_init (&declarator_obstack);
2705       /* Create the error declarator.  */
2706       cp_error_declarator = make_declarator (cdk_error);
2707       /* Create the empty parameter list.  */
2708       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2709       /* Remember where the base of the declarator obstack lies.  */
2710       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2711     }
2712
2713   cp_parser_declaration_seq_opt (parser);
2714   
2715   /* If there are no tokens left then all went well.  */
2716   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2717     {
2718       /* Get rid of the token array; we don't need it any more.  */
2719       cp_lexer_destroy (parser->lexer);
2720       parser->lexer = NULL;
2721       
2722       /* This file might have been a context that's implicitly extern
2723          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2724       if (parser->implicit_extern_c)
2725         {
2726           pop_lang_context ();
2727           parser->implicit_extern_c = false;
2728         }
2729       
2730       /* Finish up.  */
2731       finish_translation_unit ();
2732       
2733       success = true;
2734     }
2735   else
2736     {
2737       cp_parser_error (parser, "expected declaration");
2738       success = false;
2739     }
2740   
2741   /* Make sure the declarator obstack was fully cleaned up.  */
2742   gcc_assert (obstack_next_free (&declarator_obstack)
2743               == declarator_obstack_base);
2744
2745   /* All went well.  */
2746   return success;
2747 }
2748
2749 /* Expressions [gram.expr] */
2750
2751 /* Parse a primary-expression.
2752
2753    primary-expression:
2754      literal
2755      this
2756      ( expression )
2757      id-expression
2758
2759    GNU Extensions:
2760
2761    primary-expression:
2762      ( compound-statement )
2763      __builtin_va_arg ( assignment-expression , type-id )
2764      __builtin_offsetof ( type-id , offsetof-expression )
2765
2766    Objective-C++ Extension:
2767
2768    primary-expression:
2769      objc-expression
2770
2771    literal:
2772      __null
2773
2774    ADDRESS_P is true iff this expression was immediately preceded by
2775    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2776    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2777    true iff this expression is a template argument.
2778
2779    Returns a representation of the expression.  Upon return, *IDK
2780    indicates what kind of id-expression (if any) was present.  */
2781
2782 static tree
2783 cp_parser_primary_expression (cp_parser *parser,
2784                               bool address_p,
2785                               bool cast_p,
2786                               bool template_arg_p,
2787                               cp_id_kind *idk)
2788 {
2789   cp_token *token;
2790
2791   /* Assume the primary expression is not an id-expression.  */
2792   *idk = CP_ID_KIND_NONE;
2793
2794   /* Peek at the next token.  */
2795   token = cp_lexer_peek_token (parser->lexer);
2796   switch (token->type)
2797     {
2798       /* literal:
2799            integer-literal
2800            character-literal
2801            floating-literal
2802            string-literal
2803            boolean-literal  */
2804     case CPP_CHAR:
2805     case CPP_WCHAR:
2806     case CPP_NUMBER:
2807       token = cp_lexer_consume_token (parser->lexer);
2808       /* Floating-point literals are only allowed in an integral
2809          constant expression if they are cast to an integral or
2810          enumeration type.  */
2811       if (TREE_CODE (token->value) == REAL_CST
2812           && parser->integral_constant_expression_p
2813           && pedantic)
2814         {
2815           /* CAST_P will be set even in invalid code like "int(2.7 +
2816              ...)".   Therefore, we have to check that the next token
2817              is sure to end the cast.  */
2818           if (cast_p)
2819             {
2820               cp_token *next_token;
2821
2822               next_token = cp_lexer_peek_token (parser->lexer);
2823               if (/* The comma at the end of an
2824                      enumerator-definition.  */
2825                   next_token->type != CPP_COMMA
2826                   /* The curly brace at the end of an enum-specifier.  */
2827                   && next_token->type != CPP_CLOSE_BRACE
2828                   /* The end of a statement.  */
2829                   && next_token->type != CPP_SEMICOLON
2830                   /* The end of the cast-expression.  */
2831                   && next_token->type != CPP_CLOSE_PAREN
2832                   /* The end of an array bound.  */
2833                   && next_token->type != CPP_CLOSE_SQUARE
2834                   /* The closing ">" in a template-argument-list.  */
2835                   && (next_token->type != CPP_GREATER
2836                       || parser->greater_than_is_operator_p))
2837                 cast_p = false;
2838             }
2839
2840           /* If we are within a cast, then the constraint that the
2841              cast is to an integral or enumeration type will be
2842              checked at that point.  If we are not within a cast, then
2843              this code is invalid.  */
2844           if (!cast_p)
2845             cp_parser_non_integral_constant_expression
2846               (parser, "floating-point literal");
2847         }
2848       return token->value;
2849
2850     case CPP_STRING:
2851     case CPP_WSTRING:
2852       /* ??? Should wide strings be allowed when parser->translate_strings_p
2853          is false (i.e. in attributes)?  If not, we can kill the third
2854          argument to cp_parser_string_literal.  */
2855       return cp_parser_string_literal (parser,
2856                                        parser->translate_strings_p,
2857                                        true);
2858
2859     case CPP_OPEN_PAREN:
2860       {
2861         tree expr;
2862         bool saved_greater_than_is_operator_p;
2863
2864         /* Consume the `('.  */
2865         cp_lexer_consume_token (parser->lexer);
2866         /* Within a parenthesized expression, a `>' token is always
2867            the greater-than operator.  */
2868         saved_greater_than_is_operator_p
2869           = parser->greater_than_is_operator_p;
2870         parser->greater_than_is_operator_p = true;
2871         /* If we see `( { ' then we are looking at the beginning of
2872            a GNU statement-expression.  */
2873         if (cp_parser_allow_gnu_extensions_p (parser)
2874             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2875           {
2876             /* Statement-expressions are not allowed by the standard.  */
2877             if (pedantic)
2878               pedwarn ("ISO C++ forbids braced-groups within expressions");
2879
2880             /* And they're not allowed outside of a function-body; you
2881                cannot, for example, write:
2882
2883                  int i = ({ int j = 3; j + 1; });
2884
2885                at class or namespace scope.  */
2886             if (!at_function_scope_p ())
2887               error ("statement-expressions are allowed only inside functions");
2888             /* Start the statement-expression.  */
2889             expr = begin_stmt_expr ();
2890             /* Parse the compound-statement.  */
2891             cp_parser_compound_statement (parser, expr, false);
2892             /* Finish up.  */
2893             expr = finish_stmt_expr (expr, false);
2894           }
2895         else
2896           {
2897             /* Parse the parenthesized expression.  */
2898             expr = cp_parser_expression (parser, cast_p);
2899             /* Let the front end know that this expression was
2900                enclosed in parentheses. This matters in case, for
2901                example, the expression is of the form `A::B', since
2902                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2903                not.  */
2904             finish_parenthesized_expr (expr);
2905           }
2906         /* The `>' token might be the end of a template-id or
2907            template-parameter-list now.  */
2908         parser->greater_than_is_operator_p
2909           = saved_greater_than_is_operator_p;
2910         /* Consume the `)'.  */
2911         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2912           cp_parser_skip_to_end_of_statement (parser);
2913
2914         return expr;
2915       }
2916
2917     case CPP_KEYWORD:
2918       switch (token->keyword)
2919         {
2920           /* These two are the boolean literals.  */
2921         case RID_TRUE:
2922           cp_lexer_consume_token (parser->lexer);
2923           return boolean_true_node;
2924         case RID_FALSE:
2925           cp_lexer_consume_token (parser->lexer);
2926           return boolean_false_node;
2927
2928           /* The `__null' literal.  */
2929         case RID_NULL:
2930           cp_lexer_consume_token (parser->lexer);
2931           return null_node;
2932
2933           /* Recognize the `this' keyword.  */
2934         case RID_THIS:
2935           cp_lexer_consume_token (parser->lexer);
2936           if (parser->local_variables_forbidden_p)
2937             {
2938               error ("%<this%> may not be used in this context");
2939               return error_mark_node;
2940             }
2941           /* Pointers cannot appear in constant-expressions.  */
2942           if (cp_parser_non_integral_constant_expression (parser,
2943                                                           "`this'"))
2944             return error_mark_node;
2945           return finish_this_expr ();
2946
2947           /* The `operator' keyword can be the beginning of an
2948              id-expression.  */
2949         case RID_OPERATOR:
2950           goto id_expression;
2951
2952         case RID_FUNCTION_NAME:
2953         case RID_PRETTY_FUNCTION_NAME:
2954         case RID_C99_FUNCTION_NAME:
2955           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2956              __func__ are the names of variables -- but they are
2957              treated specially.  Therefore, they are handled here,
2958              rather than relying on the generic id-expression logic
2959              below.  Grammatically, these names are id-expressions.
2960
2961              Consume the token.  */
2962           token = cp_lexer_consume_token (parser->lexer);
2963           /* Look up the name.  */
2964           return finish_fname (token->value);
2965
2966         case RID_VA_ARG:
2967           {
2968             tree expression;
2969             tree type;
2970
2971             /* The `__builtin_va_arg' construct is used to handle
2972                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2973             cp_lexer_consume_token (parser->lexer);
2974             /* Look for the opening `('.  */
2975             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2976             /* Now, parse the assignment-expression.  */
2977             expression = cp_parser_assignment_expression (parser,
2978                                                           /*cast_p=*/false);
2979             /* Look for the `,'.  */
2980             cp_parser_require (parser, CPP_COMMA, "`,'");
2981             /* Parse the type-id.  */
2982             type = cp_parser_type_id (parser);
2983             /* Look for the closing `)'.  */
2984             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2985             /* Using `va_arg' in a constant-expression is not
2986                allowed.  */
2987             if (cp_parser_non_integral_constant_expression (parser,
2988                                                             "`va_arg'"))
2989               return error_mark_node;
2990             return build_x_va_arg (expression, type);
2991           }
2992
2993         case RID_OFFSETOF:
2994           return cp_parser_builtin_offsetof (parser);
2995
2996           /* Objective-C++ expressions.  */
2997         case RID_AT_ENCODE:
2998         case RID_AT_PROTOCOL:
2999         case RID_AT_SELECTOR:
3000           return cp_parser_objc_expression (parser);
3001
3002         default:
3003           cp_parser_error (parser, "expected primary-expression");
3004           return error_mark_node;
3005         }
3006
3007       /* An id-expression can start with either an identifier, a
3008          `::' as the beginning of a qualified-id, or the "operator"
3009          keyword.  */
3010     case CPP_NAME:
3011     case CPP_SCOPE:
3012     case CPP_TEMPLATE_ID:
3013     case CPP_NESTED_NAME_SPECIFIER:
3014       {
3015         tree id_expression;
3016         tree decl;
3017         const char *error_msg;
3018         bool template_p;
3019         bool done;
3020
3021       id_expression:
3022         /* Parse the id-expression.  */
3023         id_expression
3024           = cp_parser_id_expression (parser,
3025                                      /*template_keyword_p=*/false,
3026                                      /*check_dependency_p=*/true,
3027                                      &template_p,
3028                                      /*declarator_p=*/false,
3029                                      /*optional_p=*/false);
3030         if (id_expression == error_mark_node)
3031           return error_mark_node;
3032         token = cp_lexer_peek_token (parser->lexer);
3033         done = (token->type != CPP_OPEN_SQUARE
3034                 && token->type != CPP_OPEN_PAREN
3035                 && token->type != CPP_DOT
3036                 && token->type != CPP_DEREF
3037                 && token->type != CPP_PLUS_PLUS
3038                 && token->type != CPP_MINUS_MINUS);
3039         /* If we have a template-id, then no further lookup is
3040            required.  If the template-id was for a template-class, we
3041            will sometimes have a TYPE_DECL at this point.  */
3042         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3043                  || TREE_CODE (id_expression) == TYPE_DECL)
3044           decl = id_expression;
3045         /* Look up the name.  */
3046         else
3047           {
3048             tree ambiguous_decls;
3049
3050             decl = cp_parser_lookup_name (parser, id_expression,
3051                                           none_type,
3052                                           template_p,
3053                                           /*is_namespace=*/false,
3054                                           /*check_dependency=*/true,
3055                                           &ambiguous_decls);
3056             /* If the lookup was ambiguous, an error will already have
3057                been issued.  */
3058             if (ambiguous_decls)
3059               return error_mark_node;
3060
3061             /* In Objective-C++, an instance variable (ivar) may be preferred
3062                to whatever cp_parser_lookup_name() found.  */
3063             decl = objc_lookup_ivar (decl, id_expression);
3064
3065             /* If name lookup gives us a SCOPE_REF, then the
3066                qualifying scope was dependent.  */
3067             if (TREE_CODE (decl) == SCOPE_REF)
3068               return decl;
3069             /* Check to see if DECL is a local variable in a context
3070                where that is forbidden.  */
3071             if (parser->local_variables_forbidden_p
3072                 && local_variable_p (decl))
3073               {
3074                 /* It might be that we only found DECL because we are
3075                    trying to be generous with pre-ISO scoping rules.
3076                    For example, consider:
3077
3078                      int i;
3079                      void g() {
3080                        for (int i = 0; i < 10; ++i) {}
3081                        extern void f(int j = i);
3082                      }
3083
3084                    Here, name look up will originally find the out
3085                    of scope `i'.  We need to issue a warning message,
3086                    but then use the global `i'.  */
3087                 decl = check_for_out_of_scope_variable (decl);
3088                 if (local_variable_p (decl))
3089                   {
3090                     error ("local variable %qD may not appear in this context",
3091                            decl);
3092                     return error_mark_node;
3093                   }
3094               }
3095           }
3096
3097         decl = (finish_id_expression 
3098                 (id_expression, decl, parser->scope,
3099                  idk,
3100                  parser->integral_constant_expression_p,
3101                  parser->allow_non_integral_constant_expression_p,
3102                  &parser->non_integral_constant_expression_p,
3103                  template_p, done, address_p,
3104                  template_arg_p,
3105                  &error_msg));
3106         if (error_msg)
3107           cp_parser_error (parser, error_msg);
3108         return decl;
3109       }
3110
3111       /* Anything else is an error.  */
3112     default:
3113       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3114       if (c_dialect_objc ()
3115           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3116         return cp_parser_objc_expression (parser);
3117
3118       cp_parser_error (parser, "expected primary-expression");
3119       return error_mark_node;
3120     }
3121 }
3122
3123 /* Parse an id-expression.
3124
3125    id-expression:
3126      unqualified-id
3127      qualified-id
3128
3129    qualified-id:
3130      :: [opt] nested-name-specifier template [opt] unqualified-id
3131      :: identifier
3132      :: operator-function-id
3133      :: template-id
3134
3135    Return a representation of the unqualified portion of the
3136    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3137    a `::' or nested-name-specifier.
3138
3139    Often, if the id-expression was a qualified-id, the caller will
3140    want to make a SCOPE_REF to represent the qualified-id.  This
3141    function does not do this in order to avoid wastefully creating
3142    SCOPE_REFs when they are not required.
3143
3144    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3145    `template' keyword.
3146
3147    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3148    uninstantiated templates.
3149
3150    If *TEMPLATE_P is non-NULL, it is set to true iff the
3151    `template' keyword is used to explicitly indicate that the entity
3152    named is a template.
3153
3154    If DECLARATOR_P is true, the id-expression is appearing as part of
3155    a declarator, rather than as part of an expression.  */
3156
3157 static tree
3158 cp_parser_id_expression (cp_parser *parser,
3159                          bool template_keyword_p,
3160                          bool check_dependency_p,
3161                          bool *template_p,
3162                          bool declarator_p,
3163                          bool optional_p)
3164 {
3165   bool global_scope_p;
3166   bool nested_name_specifier_p;
3167
3168   /* Assume the `template' keyword was not used.  */
3169   if (template_p)
3170     *template_p = template_keyword_p;
3171
3172   /* Look for the optional `::' operator.  */
3173   global_scope_p
3174     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3175        != NULL_TREE);
3176   /* Look for the optional nested-name-specifier.  */
3177   nested_name_specifier_p
3178     = (cp_parser_nested_name_specifier_opt (parser,
3179                                             /*typename_keyword_p=*/false,
3180                                             check_dependency_p,
3181                                             /*type_p=*/false,
3182                                             declarator_p)
3183        != NULL_TREE);
3184   /* If there is a nested-name-specifier, then we are looking at
3185      the first qualified-id production.  */
3186   if (nested_name_specifier_p)
3187     {
3188       tree saved_scope;
3189       tree saved_object_scope;
3190       tree saved_qualifying_scope;
3191       tree unqualified_id;
3192       bool is_template;
3193
3194       /* See if the next token is the `template' keyword.  */
3195       if (!template_p)
3196         template_p = &is_template;
3197       *template_p = cp_parser_optional_template_keyword (parser);
3198       /* Name lookup we do during the processing of the
3199          unqualified-id might obliterate SCOPE.  */
3200       saved_scope = parser->scope;
3201       saved_object_scope = parser->object_scope;
3202       saved_qualifying_scope = parser->qualifying_scope;
3203       /* Process the final unqualified-id.  */
3204       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3205                                                  check_dependency_p,
3206                                                  declarator_p,
3207                                                  /*optional_p=*/false);
3208       /* Restore the SAVED_SCOPE for our caller.  */
3209       parser->scope = saved_scope;
3210       parser->object_scope = saved_object_scope;
3211       parser->qualifying_scope = saved_qualifying_scope;
3212
3213       return unqualified_id;
3214     }
3215   /* Otherwise, if we are in global scope, then we are looking at one
3216      of the other qualified-id productions.  */
3217   else if (global_scope_p)
3218     {
3219       cp_token *token;
3220       tree id;
3221
3222       /* Peek at the next token.  */
3223       token = cp_lexer_peek_token (parser->lexer);
3224
3225       /* If it's an identifier, and the next token is not a "<", then
3226          we can avoid the template-id case.  This is an optimization
3227          for this common case.  */
3228       if (token->type == CPP_NAME
3229           && !cp_parser_nth_token_starts_template_argument_list_p
3230                (parser, 2))
3231         return cp_parser_identifier (parser);
3232
3233       cp_parser_parse_tentatively (parser);
3234       /* Try a template-id.  */
3235       id = cp_parser_template_id (parser,
3236                                   /*template_keyword_p=*/false,
3237                                   /*check_dependency_p=*/true,
3238                                   declarator_p);
3239       /* If that worked, we're done.  */
3240       if (cp_parser_parse_definitely (parser))
3241         return id;
3242
3243       /* Peek at the next token.  (Changes in the token buffer may
3244          have invalidated the pointer obtained above.)  */
3245       token = cp_lexer_peek_token (parser->lexer);
3246
3247       switch (token->type)
3248         {
3249         case CPP_NAME:
3250           return cp_parser_identifier (parser);
3251
3252         case CPP_KEYWORD:
3253           if (token->keyword == RID_OPERATOR)
3254             return cp_parser_operator_function_id (parser);
3255           /* Fall through.  */
3256
3257         default:
3258           cp_parser_error (parser, "expected id-expression");
3259           return error_mark_node;
3260         }
3261     }
3262   else
3263     return cp_parser_unqualified_id (parser, template_keyword_p,
3264                                      /*check_dependency_p=*/true,
3265                                      declarator_p,
3266                                      optional_p);
3267 }
3268
3269 /* Parse an unqualified-id.
3270
3271    unqualified-id:
3272      identifier
3273      operator-function-id
3274      conversion-function-id
3275      ~ class-name
3276      template-id
3277
3278    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3279    keyword, in a construct like `A::template ...'.
3280
3281    Returns a representation of unqualified-id.  For the `identifier'
3282    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3283    production a BIT_NOT_EXPR is returned; the operand of the
3284    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3285    other productions, see the documentation accompanying the
3286    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3287    names are looked up in uninstantiated templates.  If DECLARATOR_P
3288    is true, the unqualified-id is appearing as part of a declarator,
3289    rather than as part of an expression.  */
3290
3291 static tree
3292 cp_parser_unqualified_id (cp_parser* parser,
3293                           bool template_keyword_p,
3294                           bool check_dependency_p,
3295                           bool declarator_p, 
3296                           bool optional_p)
3297 {
3298   cp_token *token;
3299
3300   /* Peek at the next token.  */
3301   token = cp_lexer_peek_token (parser->lexer);
3302
3303   switch (token->type)
3304     {
3305     case CPP_NAME:
3306       {
3307         tree id;
3308
3309         /* We don't know yet whether or not this will be a
3310            template-id.  */
3311         cp_parser_parse_tentatively (parser);
3312         /* Try a template-id.  */
3313         id = cp_parser_template_id (parser, template_keyword_p,
3314                                     check_dependency_p,
3315                                     declarator_p);
3316         /* If it worked, we're done.  */
3317         if (cp_parser_parse_definitely (parser))
3318           return id;
3319         /* Otherwise, it's an ordinary identifier.  */
3320         return cp_parser_identifier (parser);
3321       }
3322
3323     case CPP_TEMPLATE_ID:
3324       return cp_parser_template_id (parser, template_keyword_p,
3325                                     check_dependency_p,
3326                                     declarator_p);
3327
3328     case CPP_COMPL:
3329       {
3330         tree type_decl;
3331         tree qualifying_scope;
3332         tree object_scope;
3333         tree scope;
3334         bool done;
3335
3336         /* Consume the `~' token.  */
3337         cp_lexer_consume_token (parser->lexer);
3338         /* Parse the class-name.  The standard, as written, seems to
3339            say that:
3340
3341              template <typename T> struct S { ~S (); };
3342              template <typename T> S<T>::~S() {}
3343
3344            is invalid, since `~' must be followed by a class-name, but
3345            `S<T>' is dependent, and so not known to be a class.
3346            That's not right; we need to look in uninstantiated
3347            templates.  A further complication arises from:
3348
3349              template <typename T> void f(T t) {
3350                t.T::~T();
3351              }
3352
3353            Here, it is not possible to look up `T' in the scope of `T'
3354            itself.  We must look in both the current scope, and the
3355            scope of the containing complete expression.
3356
3357            Yet another issue is:
3358
3359              struct S {
3360                int S;
3361                ~S();
3362              };
3363
3364              S::~S() {}
3365
3366            The standard does not seem to say that the `S' in `~S'
3367            should refer to the type `S' and not the data member
3368            `S::S'.  */
3369
3370         /* DR 244 says that we look up the name after the "~" in the
3371            same scope as we looked up the qualifying name.  That idea
3372            isn't fully worked out; it's more complicated than that.  */
3373         scope = parser->scope;
3374         object_scope = parser->object_scope;
3375         qualifying_scope = parser->qualifying_scope;
3376
3377         /* If the name is of the form "X::~X" it's OK.  */
3378         if (scope && TYPE_P (scope)
3379             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3380             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3381                 == CPP_OPEN_PAREN)
3382             && (cp_lexer_peek_token (parser->lexer)->value
3383                 == TYPE_IDENTIFIER (scope)))
3384           {
3385             cp_lexer_consume_token (parser->lexer);
3386             return build_nt (BIT_NOT_EXPR, scope);
3387           }
3388
3389         /* If there was an explicit qualification (S::~T), first look
3390            in the scope given by the qualification (i.e., S).  */
3391         done = false;
3392         type_decl = NULL_TREE;
3393         if (scope)
3394           {
3395             cp_parser_parse_tentatively (parser);
3396             type_decl = cp_parser_class_name (parser,
3397                                               /*typename_keyword_p=*/false,
3398                                               /*template_keyword_p=*/false,
3399                                               none_type,
3400                                               /*check_dependency=*/false,
3401                                               /*class_head_p=*/false,
3402                                               declarator_p);
3403             if (cp_parser_parse_definitely (parser))
3404               done = true;
3405           }
3406         /* In "N::S::~S", look in "N" as well.  */
3407         if (!done && scope && qualifying_scope)
3408           {
3409             cp_parser_parse_tentatively (parser);
3410             parser->scope = qualifying_scope;
3411             parser->object_scope = NULL_TREE;
3412             parser->qualifying_scope = NULL_TREE;
3413             type_decl
3414               = cp_parser_class_name (parser,
3415                                       /*typename_keyword_p=*/false,
3416                                       /*template_keyword_p=*/false,
3417                                       none_type,
3418                                       /*check_dependency=*/false,
3419                                       /*class_head_p=*/false,
3420                                       declarator_p);
3421             if (cp_parser_parse_definitely (parser))
3422               done = true;
3423           }
3424         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3425         else if (!done && object_scope)
3426           {
3427             cp_parser_parse_tentatively (parser);
3428             parser->scope = object_scope;
3429             parser->object_scope = NULL_TREE;
3430             parser->qualifying_scope = NULL_TREE;
3431             type_decl
3432               = cp_parser_class_name (parser,
3433                                       /*typename_keyword_p=*/false,
3434                                       /*template_keyword_p=*/false,
3435                                       none_type,
3436                                       /*check_dependency=*/false,
3437                                       /*class_head_p=*/false,
3438                                       declarator_p);
3439             if (cp_parser_parse_definitely (parser))
3440               done = true;
3441           }
3442         /* Look in the surrounding context.  */
3443         if (!done)
3444           {
3445             parser->scope = NULL_TREE;
3446             parser->object_scope = NULL_TREE;
3447             parser->qualifying_scope = NULL_TREE;
3448             type_decl
3449               = cp_parser_class_name (parser,
3450                                       /*typename_keyword_p=*/false,
3451                                       /*template_keyword_p=*/false,
3452                                       none_type,
3453                                       /*check_dependency=*/false,
3454                                       /*class_head_p=*/false,
3455                                       declarator_p);
3456           }
3457         /* If an error occurred, assume that the name of the
3458            destructor is the same as the name of the qualifying
3459            class.  That allows us to keep parsing after running
3460            into ill-formed destructor names.  */
3461         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3462           return build_nt (BIT_NOT_EXPR, scope);
3463         else if (type_decl == error_mark_node)
3464           return error_mark_node;
3465
3466         /* Check that destructor name and scope match.  */
3467         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3468           {
3469             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3470               error ("declaration of %<~%T%> as member of %qT",
3471                      type_decl, scope);
3472             return error_mark_node;
3473           }
3474
3475         /* [class.dtor]
3476
3477            A typedef-name that names a class shall not be used as the
3478            identifier in the declarator for a destructor declaration.  */
3479         if (declarator_p
3480             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3481             && !DECL_SELF_REFERENCE_P (type_decl)
3482             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3483           error ("typedef-name %qD used as destructor declarator",
3484                  type_decl);
3485
3486         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3487       }
3488
3489     case CPP_KEYWORD:
3490       if (token->keyword == RID_OPERATOR)
3491         {
3492           tree id;
3493
3494           /* This could be a template-id, so we try that first.  */
3495           cp_parser_parse_tentatively (parser);
3496           /* Try a template-id.  */
3497           id = cp_parser_template_id (parser, template_keyword_p,
3498                                       /*check_dependency_p=*/true,
3499                                       declarator_p);
3500           /* If that worked, we're done.  */
3501           if (cp_parser_parse_definitely (parser))
3502             return id;
3503           /* We still don't know whether we're looking at an
3504              operator-function-id or a conversion-function-id.  */
3505           cp_parser_parse_tentatively (parser);
3506           /* Try an operator-function-id.  */
3507           id = cp_parser_operator_function_id (parser);
3508           /* If that didn't work, try a conversion-function-id.  */
3509           if (!cp_parser_parse_definitely (parser))
3510             id = cp_parser_conversion_function_id (parser);
3511
3512           return id;
3513         }
3514       /* Fall through.  */
3515
3516     default:
3517       if (optional_p)
3518         return NULL_TREE;
3519       cp_parser_error (parser, "expected unqualified-id");
3520       return error_mark_node;
3521     }
3522 }
3523
3524 /* Parse an (optional) nested-name-specifier.
3525
3526    nested-name-specifier:
3527      class-or-namespace-name :: nested-name-specifier [opt]
3528      class-or-namespace-name :: template nested-name-specifier [opt]
3529
3530    PARSER->SCOPE should be set appropriately before this function is
3531    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3532    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3533    in name lookups.
3534
3535    Sets PARSER->SCOPE to the class (TYPE) or namespace
3536    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3537    it unchanged if there is no nested-name-specifier.  Returns the new
3538    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3539
3540    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3541    part of a declaration and/or decl-specifier.  */
3542
3543 static tree
3544 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3545                                      bool typename_keyword_p,
3546                                      bool check_dependency_p,
3547                                      bool type_p,
3548                                      bool is_declaration)
3549 {
3550   bool success = false;
3551   cp_token_position start = 0;
3552   cp_token *token;
3553
3554   /* If the next token corresponds to a nested name specifier, there
3555      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3556      false, it may have been true before, in which case something
3557      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3558      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3559      CHECK_DEPENDENCY_P is false, we have to fall through into the
3560      main loop.  */
3561   if (check_dependency_p
3562       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3563     {
3564       cp_parser_pre_parsed_nested_name_specifier (parser);
3565       return parser->scope;
3566     }
3567
3568   /* Remember where the nested-name-specifier starts.  */
3569   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3570     {
3571       start = cp_lexer_token_position (parser->lexer, false);
3572       push_deferring_access_checks (dk_deferred);
3573     }
3574
3575   while (true)
3576     {
3577       tree new_scope;
3578       tree old_scope;
3579       tree saved_qualifying_scope;
3580       bool template_keyword_p;
3581
3582       /* Spot cases that cannot be the beginning of a
3583          nested-name-specifier.  */
3584       token = cp_lexer_peek_token (parser->lexer);
3585
3586       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3587          the already parsed nested-name-specifier.  */
3588       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3589         {
3590           /* Grab the nested-name-specifier and continue the loop.  */
3591           cp_parser_pre_parsed_nested_name_specifier (parser);
3592           success = true;
3593           continue;
3594         }
3595
3596       /* Spot cases that cannot be the beginning of a
3597          nested-name-specifier.  On the second and subsequent times
3598          through the loop, we look for the `template' keyword.  */
3599       if (success && token->keyword == RID_TEMPLATE)
3600         ;
3601       /* A template-id can start a nested-name-specifier.  */
3602       else if (token->type == CPP_TEMPLATE_ID)
3603         ;
3604       else
3605         {
3606           /* If the next token is not an identifier, then it is
3607              definitely not a class-or-namespace-name.  */
3608           if (token->type != CPP_NAME)
3609             break;
3610           /* If the following token is neither a `<' (to begin a
3611              template-id), nor a `::', then we are not looking at a
3612              nested-name-specifier.  */
3613           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3614           if (token->type != CPP_SCOPE
3615               && !cp_parser_nth_token_starts_template_argument_list_p
3616                   (parser, 2))
3617             break;
3618         }
3619
3620       /* The nested-name-specifier is optional, so we parse
3621          tentatively.  */
3622       cp_parser_parse_tentatively (parser);
3623
3624       /* Look for the optional `template' keyword, if this isn't the
3625          first time through the loop.  */
3626       if (success)
3627         template_keyword_p = cp_parser_optional_template_keyword (parser);
3628       else
3629         template_keyword_p = false;
3630
3631       /* Save the old scope since the name lookup we are about to do
3632          might destroy it.  */
3633       old_scope = parser->scope;
3634       saved_qualifying_scope = parser->qualifying_scope;
3635       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3636          look up names in "X<T>::I" in order to determine that "Y" is
3637          a template.  So, if we have a typename at this point, we make
3638          an effort to look through it.  */
3639       if (is_declaration
3640           && !typename_keyword_p
3641           && parser->scope
3642           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3643         parser->scope = resolve_typename_type (parser->scope,
3644                                                /*only_current_p=*/false);
3645       /* Parse the qualifying entity.  */
3646       new_scope
3647         = cp_parser_class_or_namespace_name (parser,
3648                                              typename_keyword_p,
3649                                              template_keyword_p,
3650                                              check_dependency_p,
3651                                              type_p,
3652                                              is_declaration);
3653       /* Look for the `::' token.  */
3654       cp_parser_require (parser, CPP_SCOPE, "`::'");
3655
3656       /* If we found what we wanted, we keep going; otherwise, we're
3657          done.  */
3658       if (!cp_parser_parse_definitely (parser))
3659         {
3660           bool error_p = false;
3661
3662           /* Restore the OLD_SCOPE since it was valid before the
3663              failed attempt at finding the last
3664              class-or-namespace-name.  */
3665           parser->scope = old_scope;
3666           parser->qualifying_scope = saved_qualifying_scope;
3667           /* If the next token is an identifier, and the one after
3668              that is a `::', then any valid interpretation would have
3669              found a class-or-namespace-name.  */
3670           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3671                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3672                      == CPP_SCOPE)
3673                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3674                      != CPP_COMPL))
3675             {
3676               token = cp_lexer_consume_token (parser->lexer);
3677               if (!error_p)
3678                 {
3679                   if (!token->ambiguous_p)
3680                     {
3681                       tree decl;
3682                       tree ambiguous_decls;
3683
3684                       decl = cp_parser_lookup_name (parser, token->value,
3685                                                     none_type,
3686                                                     /*is_template=*/false,
3687                                                     /*is_namespace=*/false,
3688                                                     /*check_dependency=*/true,
3689                                                     &ambiguous_decls);
3690                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3691                         error ("%qD used without template parameters", decl);
3692                       else if (ambiguous_decls)
3693                         {
3694                           error ("reference to %qD is ambiguous", 
3695                                  token->value);
3696                           print_candidates (ambiguous_decls);
3697                           decl = error_mark_node;
3698                         }
3699                       else
3700                         cp_parser_name_lookup_error
3701                           (parser, token->value, decl,
3702                            "is not a class or namespace");
3703                     }
3704                   parser->scope = error_mark_node;
3705                   error_p = true;
3706                   /* Treat this as a successful nested-name-specifier
3707                      due to:
3708
3709                      [basic.lookup.qual]
3710
3711                      If the name found is not a class-name (clause
3712                      _class_) or namespace-name (_namespace.def_), the
3713                      program is ill-formed.  */
3714                   success = true;
3715                 }
3716               cp_lexer_consume_token (parser->lexer);
3717             }
3718           break;
3719         }
3720       /* We've found one valid nested-name-specifier.  */
3721       success = true;
3722       /* Name lookup always gives us a DECL.  */
3723       if (TREE_CODE (new_scope) == TYPE_DECL)
3724         new_scope = TREE_TYPE (new_scope);
3725       /* Uses of "template" must be followed by actual templates.  */
3726       if (template_keyword_p
3727           && !(CLASS_TYPE_P (new_scope)
3728                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3729                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3730                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3731           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3732                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3733                    == TEMPLATE_ID_EXPR)))
3734         pedwarn (TYPE_P (new_scope)
3735                  ? "%qT is not a template"
3736                  : "%qD is not a template",
3737                  new_scope);
3738       /* If it is a class scope, try to complete it; we are about to
3739          be looking up names inside the class.  */
3740       if (TYPE_P (new_scope)
3741           /* Since checking types for dependency can be expensive,
3742              avoid doing it if the type is already complete.  */
3743           && !COMPLETE_TYPE_P (new_scope)
3744           /* Do not try to complete dependent types.  */
3745           && !dependent_type_p (new_scope))
3746         new_scope = complete_type (new_scope);
3747       /* Make sure we look in the right scope the next time through
3748          the loop.  */
3749       parser->scope = new_scope;
3750     }
3751
3752   /* If parsing tentatively, replace the sequence of tokens that makes
3753      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3754      token.  That way, should we re-parse the token stream, we will
3755      not have to repeat the effort required to do the parse, nor will
3756      we issue duplicate error messages.  */
3757   if (success && start)
3758     {
3759       cp_token *token;
3760       tree access_checks;
3761
3762       token = cp_lexer_token_at (parser->lexer, start);
3763       /* Reset the contents of the START token.  */
3764       token->type = CPP_NESTED_NAME_SPECIFIER;
3765       /* Retrieve any deferred checks.  Do not pop this access checks yet
3766          so the memory will not be reclaimed during token replacing below.  */
3767       access_checks = get_deferred_access_checks ();
3768       token->value = build_tree_list (copy_list (access_checks),
3769                                       parser->scope);
3770       TREE_TYPE (token->value) = parser->qualifying_scope;
3771       token->keyword = RID_MAX;
3772
3773       /* Purge all subsequent tokens.  */
3774       cp_lexer_purge_tokens_after (parser->lexer, start);
3775     }
3776   
3777   if (start)
3778     pop_to_parent_deferring_access_checks ();
3779
3780   return success ? parser->scope : NULL_TREE;
3781 }
3782
3783 /* Parse a nested-name-specifier.  See
3784    cp_parser_nested_name_specifier_opt for details.  This function
3785    behaves identically, except that it will an issue an error if no
3786    nested-name-specifier is present.  */
3787
3788 static tree
3789 cp_parser_nested_name_specifier (cp_parser *parser,
3790                                  bool typename_keyword_p,
3791                                  bool check_dependency_p,
3792                                  bool type_p,
3793                                  bool is_declaration)
3794 {
3795   tree scope;
3796
3797   /* Look for the nested-name-specifier.  */
3798   scope = cp_parser_nested_name_specifier_opt (parser,
3799                                                typename_keyword_p,
3800                                                check_dependency_p,
3801                                                type_p,
3802                                                is_declaration);
3803   /* If it was not present, issue an error message.  */
3804   if (!scope)
3805     {
3806       cp_parser_error (parser, "expected nested-name-specifier");
3807       parser->scope = NULL_TREE;
3808     }
3809
3810   return scope;
3811 }
3812
3813 /* Parse a class-or-namespace-name.
3814
3815    class-or-namespace-name:
3816      class-name
3817      namespace-name
3818
3819    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3820    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3821    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3822    TYPE_P is TRUE iff the next name should be taken as a class-name,
3823    even the same name is declared to be another entity in the same
3824    scope.
3825
3826    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3827    specified by the class-or-namespace-name.  If neither is found the
3828    ERROR_MARK_NODE is returned.  */
3829
3830 static tree
3831 cp_parser_class_or_namespace_name (cp_parser *parser,
3832                                    bool typename_keyword_p,
3833                                    bool template_keyword_p,
3834                                    bool check_dependency_p,
3835                                    bool type_p,
3836                                    bool is_declaration)
3837 {
3838   tree saved_scope;
3839   tree saved_qualifying_scope;
3840   tree saved_object_scope;
3841   tree scope;
3842   bool only_class_p;
3843
3844   /* Before we try to parse the class-name, we must save away the
3845      current PARSER->SCOPE since cp_parser_class_name will destroy
3846      it.  */
3847   saved_scope = parser->scope;
3848   saved_qualifying_scope = parser->qualifying_scope;
3849   saved_object_scope = parser->object_scope;
3850   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3851      there is no need to look for a namespace-name.  */
3852   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3853   if (!only_class_p)
3854     cp_parser_parse_tentatively (parser);
3855   scope = cp_parser_class_name (parser,
3856                                 typename_keyword_p,
3857                                 template_keyword_p,
3858                                 type_p ? class_type : none_type,
3859                                 check_dependency_p,
3860                                 /*class_head_p=*/false,
3861                                 is_declaration);
3862   /* If that didn't work, try for a namespace-name.  */
3863   if (!only_class_p && !cp_parser_parse_definitely (parser))
3864     {
3865       /* Restore the saved scope.  */
3866       parser->scope = saved_scope;
3867       parser->qualifying_scope = saved_qualifying_scope;
3868       parser->object_scope = saved_object_scope;
3869       /* If we are not looking at an identifier followed by the scope
3870          resolution operator, then this is not part of a
3871          nested-name-specifier.  (Note that this function is only used
3872          to parse the components of a nested-name-specifier.)  */
3873       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3874           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3875         return error_mark_node;
3876       scope = cp_parser_namespace_name (parser);
3877     }
3878
3879   return scope;
3880 }
3881
3882 /* Parse a postfix-expression.
3883
3884    postfix-expression:
3885      primary-expression
3886      postfix-expression [ expression ]
3887      postfix-expression ( expression-list [opt] )
3888      simple-type-specifier ( expression-list [opt] )
3889      typename :: [opt] nested-name-specifier identifier
3890        ( expression-list [opt] )
3891      typename :: [opt] nested-name-specifier template [opt] template-id
3892        ( expression-list [opt] )
3893      postfix-expression . template [opt] id-expression
3894      postfix-expression -> template [opt] id-expression
3895      postfix-expression . pseudo-destructor-name
3896      postfix-expression -> pseudo-destructor-name
3897      postfix-expression ++
3898      postfix-expression --
3899      dynamic_cast < type-id > ( expression )
3900      static_cast < type-id > ( expression )
3901      reinterpret_cast < type-id > ( expression )
3902      const_cast < type-id > ( expression )
3903      typeid ( expression )
3904      typeid ( type-id )
3905
3906    GNU Extension:
3907
3908    postfix-expression:
3909      ( type-id ) { initializer-list , [opt] }
3910
3911    This extension is a GNU version of the C99 compound-literal
3912    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3913    but they are essentially the same concept.)
3914
3915    If ADDRESS_P is true, the postfix expression is the operand of the
3916    `&' operator.  CAST_P is true if this expression is the target of a
3917    cast.
3918
3919    Returns a representation of the expression.  */
3920
3921 static tree
3922 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3923 {
3924   cp_token *token;
3925   enum rid keyword;
3926   cp_id_kind idk = CP_ID_KIND_NONE;
3927   tree postfix_expression = NULL_TREE;
3928
3929   /* Peek at the next token.  */
3930   token = cp_lexer_peek_token (parser->lexer);
3931   /* Some of the productions are determined by keywords.  */
3932   keyword = token->keyword;
3933   switch (keyword)
3934     {
3935     case RID_DYNCAST:
3936     case RID_STATCAST:
3937     case RID_REINTCAST:
3938     case RID_CONSTCAST:
3939       {
3940         tree type;
3941         tree expression;
3942         const char *saved_message;
3943
3944         /* All of these can be handled in the same way from the point
3945            of view of parsing.  Begin by consuming the token
3946            identifying the cast.  */
3947         cp_lexer_consume_token (parser->lexer);
3948
3949         /* New types cannot be defined in the cast.  */
3950         saved_message = parser->type_definition_forbidden_message;
3951         parser->type_definition_forbidden_message
3952           = "types may not be defined in casts";
3953
3954         /* Look for the opening `<'.  */
3955         cp_parser_require (parser, CPP_LESS, "`<'");
3956         /* Parse the type to which we are casting.  */
3957         type = cp_parser_type_id (parser);
3958         /* Look for the closing `>'.  */
3959         cp_parser_require (parser, CPP_GREATER, "`>'");
3960         /* Restore the old message.  */
3961         parser->type_definition_forbidden_message = saved_message;
3962
3963         /* And the expression which is being cast.  */
3964         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3965         expression = cp_parser_expression (parser, /*cast_p=*/true);
3966         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3967
3968         /* Only type conversions to integral or enumeration types
3969            can be used in constant-expressions.  */
3970         if (parser->integral_constant_expression_p
3971             && !dependent_type_p (type)
3972             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3973             && (cp_parser_non_integral_constant_expression
3974                 (parser,
3975                  "a cast to a type other than an integral or "
3976                  "enumeration type")))
3977           return error_mark_node;
3978
3979         switch (keyword)
3980           {
3981           case RID_DYNCAST:
3982             postfix_expression
3983               = build_dynamic_cast (type, expression);
3984             break;
3985           case RID_STATCAST:
3986             postfix_expression
3987               = build_static_cast (type, expression);
3988             break;
3989           case RID_REINTCAST:
3990             postfix_expression
3991               = build_reinterpret_cast (type, expression);
3992             break;
3993           case RID_CONSTCAST:
3994             postfix_expression
3995               = build_const_cast (type, expression);
3996             break;
3997           default:
3998             gcc_unreachable ();
3999           }
4000       }
4001       break;
4002
4003     case RID_TYPEID:
4004       {
4005         tree type;
4006         const char *saved_message;
4007         bool saved_in_type_id_in_expr_p;
4008
4009         /* Consume the `typeid' token.  */
4010         cp_lexer_consume_token (parser->lexer);
4011         /* Look for the `(' token.  */
4012         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4013         /* Types cannot be defined in a `typeid' expression.  */
4014         saved_message = parser->type_definition_forbidden_message;
4015         parser->type_definition_forbidden_message
4016           = "types may not be defined in a `typeid\' expression";
4017         /* We can't be sure yet whether we're looking at a type-id or an
4018            expression.  */
4019         cp_parser_parse_tentatively (parser);
4020         /* Try a type-id first.  */
4021         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4022         parser->in_type_id_in_expr_p = true;
4023         type = cp_parser_type_id (parser);
4024         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4025         /* Look for the `)' token.  Otherwise, we can't be sure that
4026            we're not looking at an expression: consider `typeid (int
4027            (3))', for example.  */
4028         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4029         /* If all went well, simply lookup the type-id.  */
4030         if (cp_parser_parse_definitely (parser))
4031           postfix_expression = get_typeid (type);
4032         /* Otherwise, fall back to the expression variant.  */
4033         else
4034           {
4035             tree expression;
4036
4037             /* Look for an expression.  */
4038             expression = cp_parser_expression (parser, /*cast_p=*/false);
4039             /* Compute its typeid.  */
4040             postfix_expression = build_typeid (expression);
4041             /* Look for the `)' token.  */
4042             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4043           }
4044         /* `typeid' may not appear in an integral constant expression.  */
4045         if (cp_parser_non_integral_constant_expression(parser,
4046                                                        "`typeid' operator"))
4047           return error_mark_node;
4048         /* Restore the saved message.  */
4049         parser->type_definition_forbidden_message = saved_message;
4050       }
4051       break;
4052
4053     case RID_TYPENAME:
4054       {
4055         tree type;
4056         /* The syntax permitted here is the same permitted for an
4057            elaborated-type-specifier.  */
4058         type = cp_parser_elaborated_type_specifier (parser,
4059                                                     /*is_friend=*/false,
4060                                                     /*is_declaration=*/false);
4061         postfix_expression = cp_parser_functional_cast (parser, type);
4062       }
4063       break;
4064
4065     default:
4066       {
4067         tree type;
4068
4069         /* If the next thing is a simple-type-specifier, we may be
4070            looking at a functional cast.  We could also be looking at
4071            an id-expression.  So, we try the functional cast, and if
4072            that doesn't work we fall back to the primary-expression.  */
4073         cp_parser_parse_tentatively (parser);
4074         /* Look for the simple-type-specifier.  */
4075         type = cp_parser_simple_type_specifier (parser,
4076                                                 /*decl_specs=*/NULL,
4077                                                 CP_PARSER_FLAGS_NONE);
4078         /* Parse the cast itself.  */
4079         if (!cp_parser_error_occurred (parser))
4080           postfix_expression
4081             = cp_parser_functional_cast (parser, type);
4082         /* If that worked, we're done.  */
4083         if (cp_parser_parse_definitely (parser))
4084           break;
4085
4086         /* If the functional-cast didn't work out, try a
4087            compound-literal.  */
4088         if (cp_parser_allow_gnu_extensions_p (parser)
4089             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4090           {
4091             VEC(constructor_elt,gc) *initializer_list = NULL;
4092             bool saved_in_type_id_in_expr_p;
4093
4094             cp_parser_parse_tentatively (parser);
4095             /* Consume the `('.  */
4096             cp_lexer_consume_token (parser->lexer);
4097             /* Parse the type.  */
4098             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4099             parser->in_type_id_in_expr_p = true;
4100             type = cp_parser_type_id (parser);
4101             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4102             /* Look for the `)'.  */
4103             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4104             /* Look for the `{'.  */
4105             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4106             /* If things aren't going well, there's no need to
4107                keep going.  */
4108             if (!cp_parser_error_occurred (parser))
4109               {
4110                 bool non_constant_p;
4111                 /* Parse the initializer-list.  */
4112                 initializer_list
4113                   = cp_parser_initializer_list (parser, &non_constant_p);
4114                 /* Allow a trailing `,'.  */
4115                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4116                   cp_lexer_consume_token (parser->lexer);
4117                 /* Look for the final `}'.  */
4118                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4119               }
4120             /* If that worked, we're definitely looking at a
4121                compound-literal expression.  */
4122             if (cp_parser_parse_definitely (parser))
4123               {
4124                 /* Warn the user that a compound literal is not
4125                    allowed in standard C++.  */
4126                 if (pedantic)
4127                   pedwarn ("ISO C++ forbids compound-literals");
4128                 /* Form the representation of the compound-literal.  */
4129                 postfix_expression
4130                   = finish_compound_literal (type, initializer_list);
4131                 break;
4132               }
4133           }
4134
4135         /* It must be a primary-expression.  */
4136         postfix_expression 
4137           = cp_parser_primary_expression (parser, address_p, cast_p, 
4138                                           /*template_arg_p=*/false,
4139                                           &idk);
4140       }
4141       break;
4142     }
4143
4144   /* Keep looping until the postfix-expression is complete.  */
4145   while (true)
4146     {
4147       if (idk == CP_ID_KIND_UNQUALIFIED
4148           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4149           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4150         /* It is not a Koenig lookup function call.  */
4151         postfix_expression
4152           = unqualified_name_lookup_error (postfix_expression);
4153
4154       /* Peek at the next token.  */
4155       token = cp_lexer_peek_token (parser->lexer);
4156
4157       switch (token->type)
4158         {
4159         case CPP_OPEN_SQUARE:
4160           postfix_expression
4161             = cp_parser_postfix_open_square_expression (parser,
4162                                                         postfix_expression,
4163                                                         false);
4164           idk = CP_ID_KIND_NONE;
4165           break;
4166
4167         case CPP_OPEN_PAREN:
4168           /* postfix-expression ( expression-list [opt] ) */
4169           {
4170             bool koenig_p;
4171             bool is_builtin_constant_p;
4172             bool saved_integral_constant_expression_p = false;
4173             bool saved_non_integral_constant_expression_p = false;
4174             tree args;
4175
4176             is_builtin_constant_p
4177               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4178             if (is_builtin_constant_p)
4179               {
4180                 /* The whole point of __builtin_constant_p is to allow
4181                    non-constant expressions to appear as arguments.  */
4182                 saved_integral_constant_expression_p
4183                   = parser->integral_constant_expression_p;
4184                 saved_non_integral_constant_expression_p
4185                   = parser->non_integral_constant_expression_p;
4186                 parser->integral_constant_expression_p = false;
4187               }
4188             args = (cp_parser_parenthesized_expression_list
4189                     (parser, /*is_attribute_list=*/false,
4190                      /*cast_p=*/false,
4191                      /*non_constant_p=*/NULL));
4192             if (is_builtin_constant_p)
4193               {
4194                 parser->integral_constant_expression_p
4195                   = saved_integral_constant_expression_p;
4196                 parser->non_integral_constant_expression_p
4197                   = saved_non_integral_constant_expression_p;
4198               }
4199
4200             if (args == error_mark_node)
4201               {
4202                 postfix_expression = error_mark_node;
4203                 break;
4204               }
4205
4206             /* Function calls are not permitted in
4207                constant-expressions.  */
4208             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4209                 && cp_parser_non_integral_constant_expression (parser,
4210                                                                "a function call"))
4211               {
4212                 postfix_expression = error_mark_node;
4213                 break;
4214               }
4215
4216             koenig_p = false;
4217             if (idk == CP_ID_KIND_UNQUALIFIED)
4218               {
4219                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4220                   {
4221                     if (args)
4222                       {
4223                         koenig_p = true;
4224                         postfix_expression
4225                           = perform_koenig_lookup (postfix_expression, args);
4226                       }
4227                     else
4228                       postfix_expression
4229                         = unqualified_fn_lookup_error (postfix_expression);
4230                   }
4231                 /* We do not perform argument-dependent lookup if
4232                    normal lookup finds a non-function, in accordance
4233                    with the expected resolution of DR 218.  */
4234                 else if (args && is_overloaded_fn (postfix_expression))
4235                   {
4236                     tree fn = get_first_fn (postfix_expression);
4237
4238                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4239                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4240
4241                     /* Only do argument dependent lookup if regular
4242                        lookup does not find a set of member functions.
4243                        [basic.lookup.koenig]/2a  */
4244                     if (!DECL_FUNCTION_MEMBER_P (fn))
4245                       {
4246                         koenig_p = true;
4247                         postfix_expression
4248                           = perform_koenig_lookup (postfix_expression, args);
4249                       }
4250                   }
4251               }
4252
4253             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4254               {
4255                 tree instance = TREE_OPERAND (postfix_expression, 0);
4256                 tree fn = TREE_OPERAND (postfix_expression, 1);
4257
4258                 if (processing_template_decl
4259                     && (type_dependent_expression_p (instance)
4260                         || (!BASELINK_P (fn)
4261                             && TREE_CODE (fn) != FIELD_DECL)
4262                         || type_dependent_expression_p (fn)
4263                         || any_type_dependent_arguments_p (args)))
4264                   {
4265                     postfix_expression
4266                       = build_min_nt (CALL_EXPR, postfix_expression,
4267                                       args, NULL_TREE);
4268                     break;
4269                   }
4270
4271                 if (BASELINK_P (fn))
4272                   postfix_expression
4273                     = (build_new_method_call
4274                        (instance, fn, args, NULL_TREE,
4275                         (idk == CP_ID_KIND_QUALIFIED
4276                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4277                         /*fn_p=*/NULL));
4278                 else
4279                   postfix_expression
4280                     = finish_call_expr (postfix_expression, args,
4281                                         /*disallow_virtual=*/false,
4282                                         /*koenig_p=*/false);
4283               }
4284             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4285                      || TREE_CODE (postfix_expression) == MEMBER_REF
4286                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4287               postfix_expression = (build_offset_ref_call_from_tree
4288                                     (postfix_expression, args));
4289             else if (idk == CP_ID_KIND_QUALIFIED)
4290               /* A call to a static class member, or a namespace-scope
4291                  function.  */
4292               postfix_expression
4293                 = finish_call_expr (postfix_expression, args,
4294                                     /*disallow_virtual=*/true,
4295                                     koenig_p);
4296             else
4297               /* All other function calls.  */
4298               postfix_expression
4299                 = finish_call_expr (postfix_expression, args,
4300                                     /*disallow_virtual=*/false,
4301                                     koenig_p);
4302
4303             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4304             idk = CP_ID_KIND_NONE;
4305           }
4306           break;
4307
4308         case CPP_DOT:
4309         case CPP_DEREF:
4310           /* postfix-expression . template [opt] id-expression
4311              postfix-expression . pseudo-destructor-name
4312              postfix-expression -> template [opt] id-expression
4313              postfix-expression -> pseudo-destructor-name */
4314
4315           /* Consume the `.' or `->' operator.  */
4316           cp_lexer_consume_token (parser->lexer);
4317
4318           postfix_expression
4319             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4320                                                       postfix_expression,
4321                                                       false, &idk);
4322           break;
4323
4324         case CPP_PLUS_PLUS:
4325           /* postfix-expression ++  */
4326           /* Consume the `++' token.  */
4327           cp_lexer_consume_token (parser->lexer);
4328           /* Generate a representation for the complete expression.  */
4329           postfix_expression
4330             = finish_increment_expr (postfix_expression,
4331                                      POSTINCREMENT_EXPR);
4332           /* Increments may not appear in constant-expressions.  */
4333           if (cp_parser_non_integral_constant_expression (parser,
4334                                                           "an increment"))
4335             postfix_expression = error_mark_node;
4336           idk = CP_ID_KIND_NONE;
4337           break;
4338
4339         case CPP_MINUS_MINUS:
4340           /* postfix-expression -- */
4341           /* Consume the `--' token.  */
4342           cp_lexer_consume_token (parser->lexer);
4343           /* Generate a representation for the complete expression.  */
4344           postfix_expression
4345             = finish_increment_expr (postfix_expression,
4346                                      POSTDECREMENT_EXPR);
4347           /* Decrements may not appear in constant-expressions.  */
4348           if (cp_parser_non_integral_constant_expression (parser,
4349                                                           "a decrement"))
4350             postfix_expression = error_mark_node;
4351           idk = CP_ID_KIND_NONE;
4352           break;
4353
4354         default:
4355           return postfix_expression;
4356         }
4357     }
4358
4359   /* We should never get here.  */
4360   gcc_unreachable ();
4361   return error_mark_node;
4362 }
4363
4364 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4365    by cp_parser_builtin_offsetof.  We're looking for
4366
4367      postfix-expression [ expression ]
4368
4369    FOR_OFFSETOF is set if we're being called in that context, which
4370    changes how we deal with integer constant expressions.  */
4371
4372 static tree
4373 cp_parser_postfix_open_square_expression (cp_parser *parser,
4374                                           tree postfix_expression,
4375                                           bool for_offsetof)
4376 {
4377   tree index;
4378
4379   /* Consume the `[' token.  */
4380   cp_lexer_consume_token (parser->lexer);
4381
4382   /* Parse the index expression.  */
4383   /* ??? For offsetof, there is a question of what to allow here.  If
4384      offsetof is not being used in an integral constant expression context,
4385      then we *could* get the right answer by computing the value at runtime.
4386      If we are in an integral constant expression context, then we might
4387      could accept any constant expression; hard to say without analysis.
4388      Rather than open the barn door too wide right away, allow only integer
4389      constant expressions here.  */
4390   if (for_offsetof)
4391     index = cp_parser_constant_expression (parser, false, NULL);
4392   else
4393     index = cp_parser_expression (parser, /*cast_p=*/false);
4394
4395   /* Look for the closing `]'.  */
4396   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4397
4398   /* Build the ARRAY_REF.  */
4399   postfix_expression = grok_array_decl (postfix_expression, index);
4400
4401   /* When not doing offsetof, array references are not permitted in
4402      constant-expressions.  */
4403   if (!for_offsetof
4404       && (cp_parser_non_integral_constant_expression
4405           (parser, "an array reference")))
4406     postfix_expression = error_mark_node;
4407
4408   return postfix_expression;
4409 }
4410
4411 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4412    by cp_parser_builtin_offsetof.  We're looking for
4413
4414      postfix-expression . template [opt] id-expression
4415      postfix-expression . pseudo-destructor-name
4416      postfix-expression -> template [opt] id-expression
4417      postfix-expression -> pseudo-destructor-name
4418
4419    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4420    limits what of the above we'll actually accept, but nevermind.
4421    TOKEN_TYPE is the "." or "->" token, which will already have been
4422    removed from the stream.  */
4423
4424 static tree
4425 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4426                                         enum cpp_ttype token_type,
4427                                         tree postfix_expression,
4428                                         bool for_offsetof, cp_id_kind *idk)
4429 {
4430   tree name;
4431   bool dependent_p;
4432   bool pseudo_destructor_p;
4433   tree scope = NULL_TREE;
4434
4435   /* If this is a `->' operator, dereference the pointer.  */
4436   if (token_type == CPP_DEREF)
4437     postfix_expression = build_x_arrow (postfix_expression);
4438   /* Check to see whether or not the expression is type-dependent.  */
4439   dependent_p = type_dependent_expression_p (postfix_expression);
4440   /* The identifier following the `->' or `.' is not qualified.  */
4441   parser->scope = NULL_TREE;
4442   parser->qualifying_scope = NULL_TREE;
4443   parser->object_scope = NULL_TREE;
4444   *idk = CP_ID_KIND_NONE;
4445   /* Enter the scope corresponding to the type of the object
4446      given by the POSTFIX_EXPRESSION.  */
4447   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4448     {
4449       scope = TREE_TYPE (postfix_expression);
4450       /* According to the standard, no expression should ever have
4451          reference type.  Unfortunately, we do not currently match
4452          the standard in this respect in that our internal representation
4453          of an expression may have reference type even when the standard
4454          says it does not.  Therefore, we have to manually obtain the
4455          underlying type here.  */
4456       scope = non_reference (scope);
4457       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4458       if (scope == unknown_type_node)
4459         {
4460           error ("%qE does not have class type", postfix_expression);
4461           scope = NULL_TREE;
4462         }
4463       else
4464         scope = complete_type_or_else (scope, NULL_TREE);
4465       /* Let the name lookup machinery know that we are processing a
4466          class member access expression.  */
4467       parser->context->object_type = scope;
4468       /* If something went wrong, we want to be able to discern that case,
4469          as opposed to the case where there was no SCOPE due to the type
4470          of expression being dependent.  */
4471       if (!scope)
4472         scope = error_mark_node;
4473       /* If the SCOPE was erroneous, make the various semantic analysis
4474          functions exit quickly -- and without issuing additional error
4475          messages.  */
4476       if (scope == error_mark_node)
4477         postfix_expression = error_mark_node;
4478     }
4479
4480   /* Assume this expression is not a pseudo-destructor access.  */
4481   pseudo_destructor_p = false;
4482
4483   /* If the SCOPE is a scalar type, then, if this is a valid program,
4484      we must be looking at a pseudo-destructor-name.  */
4485   if (scope && SCALAR_TYPE_P (scope))
4486     {
4487       tree s;
4488       tree type;
4489
4490       cp_parser_parse_tentatively (parser);
4491       /* Parse the pseudo-destructor-name.  */
4492       s = NULL_TREE;
4493       cp_parser_pseudo_destructor_name (parser, &s, &type);
4494       if (cp_parser_parse_definitely (parser))
4495         {
4496           pseudo_destructor_p = true;
4497           postfix_expression
4498             = finish_pseudo_destructor_expr (postfix_expression,
4499                                              s, TREE_TYPE (type));
4500         }
4501     }
4502
4503   if (!pseudo_destructor_p)
4504     {
4505       /* If the SCOPE is not a scalar type, we are looking at an
4506          ordinary class member access expression, rather than a
4507          pseudo-destructor-name.  */
4508       bool template_p;
4509       /* Parse the id-expression.  */
4510       name = (cp_parser_id_expression 
4511               (parser, 
4512                cp_parser_optional_template_keyword (parser),
4513                /*check_dependency_p=*/true,
4514                &template_p,
4515                /*declarator_p=*/false,
4516                /*optional_p=*/false));
4517       /* In general, build a SCOPE_REF if the member name is qualified.
4518          However, if the name was not dependent and has already been
4519          resolved; there is no need to build the SCOPE_REF.  For example;
4520
4521              struct X { void f(); };
4522              template <typename T> void f(T* t) { t->X::f(); }
4523
4524          Even though "t" is dependent, "X::f" is not and has been resolved
4525          to a BASELINK; there is no need to include scope information.  */
4526
4527       /* But we do need to remember that there was an explicit scope for
4528          virtual function calls.  */
4529       if (parser->scope)
4530         *idk = CP_ID_KIND_QUALIFIED;
4531
4532       /* If the name is a template-id that names a type, we will get a
4533          TYPE_DECL here.  That is invalid code.  */
4534       if (TREE_CODE (name) == TYPE_DECL)
4535         {
4536           error ("invalid use of %qD", name);
4537           postfix_expression = error_mark_node;
4538         }
4539       else
4540         {
4541           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4542             {
4543               name = build_qualified_name (/*type=*/NULL_TREE,
4544                                            parser->scope,
4545                                            name,
4546                                            template_p);
4547               parser->scope = NULL_TREE;
4548               parser->qualifying_scope = NULL_TREE;
4549               parser->object_scope = NULL_TREE;
4550             }
4551           if (scope && name && BASELINK_P (name))
4552             adjust_result_of_qualified_name_lookup
4553               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4554           postfix_expression
4555             = finish_class_member_access_expr (postfix_expression, name,
4556                                                template_p);
4557         }
4558     }
4559
4560   /* We no longer need to look up names in the scope of the object on
4561      the left-hand side of the `.' or `->' operator.  */
4562   parser->context->object_type = NULL_TREE;
4563
4564   /* Outside of offsetof, these operators may not appear in
4565      constant-expressions.  */
4566   if (!for_offsetof
4567       && (cp_parser_non_integral_constant_expression
4568           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4569     postfix_expression = error_mark_node;
4570
4571   return postfix_expression;
4572 }
4573
4574 /* Parse a parenthesized expression-list.
4575
4576    expression-list:
4577      assignment-expression
4578      expression-list, assignment-expression
4579
4580    attribute-list:
4581      expression-list
4582      identifier
4583      identifier, expression-list
4584
4585    CAST_P is true if this expression is the target of a cast.
4586
4587    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4588    representation of an assignment-expression.  Note that a TREE_LIST
4589    is returned even if there is only a single expression in the list.
4590    error_mark_node is returned if the ( and or ) are
4591    missing. NULL_TREE is returned on no expressions. The parentheses
4592    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4593    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4594    indicates whether or not all of the expressions in the list were
4595    constant.  */
4596
4597 static tree
4598 cp_parser_parenthesized_expression_list (cp_parser* parser,
4599                                          bool is_attribute_list,
4600                                          bool cast_p,
4601                                          bool *non_constant_p)
4602 {
4603   tree expression_list = NULL_TREE;
4604   bool fold_expr_p = is_attribute_list;
4605   tree identifier = NULL_TREE;
4606
4607   /* Assume all the expressions will be constant.  */
4608   if (non_constant_p)
4609     *non_constant_p = false;
4610
4611   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4612     return error_mark_node;
4613
4614   /* Consume expressions until there are no more.  */
4615   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4616     while (true)
4617       {
4618         tree expr;
4619
4620         /* At the beginning of attribute lists, check to see if the
4621            next token is an identifier.  */
4622         if (is_attribute_list
4623             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4624           {
4625             cp_token *token;
4626
4627             /* Consume the identifier.  */
4628             token = cp_lexer_consume_token (parser->lexer);
4629             /* Save the identifier.  */
4630             identifier = token->value;
4631           }
4632         else
4633           {
4634             /* Parse the next assignment-expression.  */
4635             if (non_constant_p)
4636               {
4637                 bool expr_non_constant_p;
4638                 expr = (cp_parser_constant_expression
4639                         (parser, /*allow_non_constant_p=*/true,
4640                          &expr_non_constant_p));
4641                 if (expr_non_constant_p)
4642                   *non_constant_p = true;
4643               }
4644             else
4645               expr = cp_parser_assignment_expression (parser, cast_p);
4646
4647             if (fold_expr_p)
4648               expr = fold_non_dependent_expr (expr);
4649
4650              /* Add it to the list.  We add error_mark_node
4651                 expressions to the list, so that we can still tell if
4652                 the correct form for a parenthesized expression-list
4653                 is found. That gives better errors.  */
4654             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4655
4656             if (expr == error_mark_node)
4657               goto skip_comma;
4658           }
4659
4660         /* After the first item, attribute lists look the same as
4661            expression lists.  */
4662         is_attribute_list = false;
4663
4664       get_comma:;
4665         /* If the next token isn't a `,', then we are done.  */
4666         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4667           break;
4668
4669         /* Otherwise, consume the `,' and keep going.  */
4670         cp_lexer_consume_token (parser->lexer);
4671       }
4672
4673   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4674     {
4675       int ending;
4676
4677     skip_comma:;
4678       /* We try and resync to an unnested comma, as that will give the
4679          user better diagnostics.  */
4680       ending = cp_parser_skip_to_closing_parenthesis (parser,
4681                                                       /*recovering=*/true,
4682                                                       /*or_comma=*/true,
4683                                                       /*consume_paren=*/true);
4684       if (ending < 0)
4685         goto get_comma;
4686       if (!ending)
4687         return error_mark_node;
4688     }
4689
4690   /* We built up the list in reverse order so we must reverse it now.  */
4691   expression_list = nreverse (expression_list);
4692   if (identifier)
4693     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4694
4695   return expression_list;
4696 }
4697
4698 /* Parse a pseudo-destructor-name.
4699
4700    pseudo-destructor-name:
4701      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4702      :: [opt] nested-name-specifier template template-id :: ~ type-name
4703      :: [opt] nested-name-specifier [opt] ~ type-name
4704
4705    If either of the first two productions is used, sets *SCOPE to the
4706    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4707    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4708    or ERROR_MARK_NODE if the parse fails.  */
4709
4710 static void
4711 cp_parser_pseudo_destructor_name (cp_parser* parser,
4712                                   tree* scope,
4713                                   tree* type)
4714 {
4715   bool nested_name_specifier_p;
4716
4717   /* Assume that things will not work out.  */
4718   *type = error_mark_node;
4719
4720   /* Look for the optional `::' operator.  */
4721   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4722   /* Look for the optional nested-name-specifier.  */
4723   nested_name_specifier_p
4724     = (cp_parser_nested_name_specifier_opt (parser,
4725                                             /*typename_keyword_p=*/false,
4726                                             /*check_dependency_p=*/true,
4727                                             /*type_p=*/false,
4728                                             /*is_declaration=*/true)
4729        != NULL_TREE);
4730   /* Now, if we saw a nested-name-specifier, we might be doing the
4731      second production.  */
4732   if (nested_name_specifier_p
4733       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4734     {
4735       /* Consume the `template' keyword.  */
4736       cp_lexer_consume_token (parser->lexer);
4737       /* Parse the template-id.  */
4738       cp_parser_template_id (parser,
4739                              /*template_keyword_p=*/true,
4740                              /*check_dependency_p=*/false,
4741                              /*is_declaration=*/true);
4742       /* Look for the `::' token.  */
4743       cp_parser_require (parser, CPP_SCOPE, "`::'");
4744     }
4745   /* If the next token is not a `~', then there might be some
4746      additional qualification.  */
4747   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4748     {
4749       /* Look for the type-name.  */
4750       *scope = TREE_TYPE (cp_parser_type_name (parser));
4751
4752       if (*scope == error_mark_node)
4753         return;
4754
4755       /* If we don't have ::~, then something has gone wrong.  Since
4756          the only caller of this function is looking for something
4757          after `.' or `->' after a scalar type, most likely the
4758          program is trying to get a member of a non-aggregate
4759          type.  */
4760       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4761           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4762         {
4763           cp_parser_error (parser, "request for member of non-aggregate type");
4764           return;
4765         }
4766
4767       /* Look for the `::' token.  */
4768       cp_parser_require (parser, CPP_SCOPE, "`::'");
4769     }
4770   else
4771     *scope = NULL_TREE;
4772
4773   /* Look for the `~'.  */
4774   cp_parser_require (parser, CPP_COMPL, "`~'");
4775   /* Look for the type-name again.  We are not responsible for
4776      checking that it matches the first type-name.  */
4777   *type = cp_parser_type_name (parser);
4778 }
4779
4780 /* Parse a unary-expression.
4781
4782    unary-expression:
4783      postfix-expression
4784      ++ cast-expression
4785      -- cast-expression
4786      unary-operator cast-expression
4787      sizeof unary-expression
4788      sizeof ( type-id )
4789      new-expression
4790      delete-expression
4791
4792    GNU Extensions:
4793
4794    unary-expression:
4795      __extension__ cast-expression
4796      __alignof__ unary-expression
4797      __alignof__ ( type-id )
4798      __real__ cast-expression
4799      __imag__ cast-expression
4800      && identifier
4801
4802    ADDRESS_P is true iff the unary-expression is appearing as the
4803    operand of the `&' operator.   CAST_P is true if this expression is
4804    the target of a cast.
4805
4806    Returns a representation of the expression.  */
4807
4808 static tree
4809 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4810 {
4811   cp_token *token;
4812   enum tree_code unary_operator;
4813
4814   /* Peek at the next token.  */
4815   token = cp_lexer_peek_token (parser->lexer);
4816   /* Some keywords give away the kind of expression.  */
4817   if (token->type == CPP_KEYWORD)
4818     {
4819       enum rid keyword = token->keyword;
4820
4821       switch (keyword)
4822         {
4823         case RID_ALIGNOF:
4824         case RID_SIZEOF:
4825           {
4826             tree operand;
4827             enum tree_code op;
4828
4829             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4830             /* Consume the token.  */
4831             cp_lexer_consume_token (parser->lexer);
4832             /* Parse the operand.  */
4833             operand = cp_parser_sizeof_operand (parser, keyword);
4834
4835             if (TYPE_P (operand))
4836               return cxx_sizeof_or_alignof_type (operand, op, true);
4837             else
4838               return cxx_sizeof_or_alignof_expr (operand, op);
4839           }
4840
4841         case RID_NEW:
4842           return cp_parser_new_expression (parser);
4843
4844         case RID_DELETE:
4845           return cp_parser_delete_expression (parser);
4846
4847         case RID_EXTENSION:
4848           {
4849             /* The saved value of the PEDANTIC flag.  */
4850             int saved_pedantic;
4851             tree expr;
4852
4853             /* Save away the PEDANTIC flag.  */
4854             cp_parser_extension_opt (parser, &saved_pedantic);
4855             /* Parse the cast-expression.  */
4856             expr = cp_parser_simple_cast_expression (parser);
4857             /* Restore the PEDANTIC flag.  */
4858             pedantic = saved_pedantic;
4859
4860             return expr;
4861           }
4862
4863         case RID_REALPART:
4864         case RID_IMAGPART:
4865           {
4866             tree expression;
4867
4868             /* Consume the `__real__' or `__imag__' token.  */
4869             cp_lexer_consume_token (parser->lexer);
4870             /* Parse the cast-expression.  */
4871             expression = cp_parser_simple_cast_expression (parser);
4872             /* Create the complete representation.  */
4873             return build_x_unary_op ((keyword == RID_REALPART
4874                                       ? REALPART_EXPR : IMAGPART_EXPR),
4875                                      expression);
4876           }
4877           break;
4878
4879         default:
4880           break;
4881         }
4882     }
4883
4884   /* Look for the `:: new' and `:: delete', which also signal the
4885      beginning of a new-expression, or delete-expression,
4886      respectively.  If the next token is `::', then it might be one of
4887      these.  */
4888   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4889     {
4890       enum rid keyword;
4891
4892       /* See if the token after the `::' is one of the keywords in
4893          which we're interested.  */
4894       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4895       /* If it's `new', we have a new-expression.  */
4896       if (keyword == RID_NEW)
4897         return cp_parser_new_expression (parser);
4898       /* Similarly, for `delete'.  */
4899       else if (keyword == RID_DELETE)
4900         return cp_parser_delete_expression (parser);
4901     }
4902
4903   /* Look for a unary operator.  */
4904   unary_operator = cp_parser_unary_operator (token);
4905   /* The `++' and `--' operators can be handled similarly, even though
4906      they are not technically unary-operators in the grammar.  */
4907   if (unary_operator == ERROR_MARK)
4908     {
4909       if (token->type == CPP_PLUS_PLUS)
4910         unary_operator = PREINCREMENT_EXPR;
4911       else if (token->type == CPP_MINUS_MINUS)
4912         unary_operator = PREDECREMENT_EXPR;
4913       /* Handle the GNU address-of-label extension.  */
4914       else if (cp_parser_allow_gnu_extensions_p (parser)
4915                && token->type == CPP_AND_AND)
4916         {
4917           tree identifier;
4918
4919           /* Consume the '&&' token.  */
4920           cp_lexer_consume_token (parser->lexer);
4921           /* Look for the identifier.  */
4922           identifier = cp_parser_identifier (parser);
4923           /* Create an expression representing the address.  */
4924           return finish_label_address_expr (identifier);
4925         }
4926     }
4927   if (unary_operator != ERROR_MARK)
4928     {
4929       tree cast_expression;
4930       tree expression = error_mark_node;
4931       const char *non_constant_p = NULL;
4932
4933       /* Consume the operator token.  */
4934       token = cp_lexer_consume_token (parser->lexer);
4935       /* Parse the cast-expression.  */
4936       cast_expression
4937         = cp_parser_cast_expression (parser,
4938                                      unary_operator == ADDR_EXPR,
4939                                      /*cast_p=*/false);
4940       /* Now, build an appropriate representation.  */
4941       switch (unary_operator)
4942         {
4943         case INDIRECT_REF:
4944           non_constant_p = "`*'";
4945           expression = build_x_indirect_ref (cast_expression, "unary *");
4946           break;
4947
4948         case ADDR_EXPR:
4949           non_constant_p = "`&'";
4950           /* Fall through.  */
4951         case BIT_NOT_EXPR:
4952           expression = build_x_unary_op (unary_operator, cast_expression);
4953           break;
4954
4955         case PREINCREMENT_EXPR:
4956         case PREDECREMENT_EXPR:
4957           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4958                             ? "`++'" : "`--'");
4959           /* Fall through.  */
4960         case UNARY_PLUS_EXPR:
4961         case NEGATE_EXPR:
4962         case TRUTH_NOT_EXPR:
4963           expression = finish_unary_op_expr (unary_operator, cast_expression);
4964           break;
4965
4966         default:
4967           gcc_unreachable ();
4968         }
4969
4970       if (non_constant_p
4971           && cp_parser_non_integral_constant_expression (parser,
4972                                                          non_constant_p))
4973         expression = error_mark_node;
4974
4975       return expression;
4976     }
4977
4978   return cp_parser_postfix_expression (parser, address_p, cast_p);
4979 }
4980
4981 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4982    unary-operator, the corresponding tree code is returned.  */
4983
4984 static enum tree_code
4985 cp_parser_unary_operator (cp_token* token)
4986 {
4987   switch (token->type)
4988     {
4989     case CPP_MULT:
4990       return INDIRECT_REF;
4991
4992     case CPP_AND:
4993       return ADDR_EXPR;
4994
4995     case CPP_PLUS:
4996       return UNARY_PLUS_EXPR;
4997
4998     case CPP_MINUS:
4999       return NEGATE_EXPR;
5000
5001     case CPP_NOT:
5002       return TRUTH_NOT_EXPR;
5003
5004     case CPP_COMPL:
5005       return BIT_NOT_EXPR;
5006
5007     default:
5008       return ERROR_MARK;
5009     }
5010 }
5011
5012 /* Parse a new-expression.
5013
5014    new-expression:
5015      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5016      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5017
5018    Returns a representation of the expression.  */
5019
5020 static tree
5021 cp_parser_new_expression (cp_parser* parser)
5022 {
5023   bool global_scope_p;
5024   tree placement;
5025   tree type;
5026   tree initializer;
5027   tree nelts;
5028
5029   /* Look for the optional `::' operator.  */
5030   global_scope_p
5031     = (cp_parser_global_scope_opt (parser,
5032                                    /*current_scope_valid_p=*/false)
5033        != NULL_TREE);
5034   /* Look for the `new' operator.  */
5035   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5036   /* There's no easy way to tell a new-placement from the
5037      `( type-id )' construct.  */
5038   cp_parser_parse_tentatively (parser);
5039   /* Look for a new-placement.  */
5040   placement = cp_parser_new_placement (parser);
5041   /* If that didn't work out, there's no new-placement.  */
5042   if (!cp_parser_parse_definitely (parser))
5043     placement = NULL_TREE;
5044
5045   /* If the next token is a `(', then we have a parenthesized
5046      type-id.  */
5047   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5048     {
5049       /* Consume the `('.  */
5050       cp_lexer_consume_token (parser->lexer);
5051       /* Parse the type-id.  */
5052       type = cp_parser_type_id (parser);
5053       /* Look for the closing `)'.  */
5054       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5055       /* There should not be a direct-new-declarator in this production,
5056          but GCC used to allowed this, so we check and emit a sensible error
5057          message for this case.  */
5058       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5059         {
5060           error ("array bound forbidden after parenthesized type-id");
5061           inform ("try removing the parentheses around the type-id");
5062           cp_parser_direct_new_declarator (parser);
5063         }
5064       nelts = NULL_TREE;
5065     }
5066   /* Otherwise, there must be a new-type-id.  */
5067   else
5068     type = cp_parser_new_type_id (parser, &nelts);
5069
5070   /* If the next token is a `(', then we have a new-initializer.  */
5071   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5072     initializer = cp_parser_new_initializer (parser);
5073   else
5074     initializer = NULL_TREE;
5075
5076   /* A new-expression may not appear in an integral constant
5077      expression.  */
5078   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5079     return error_mark_node;
5080
5081   /* Create a representation of the new-expression.  */
5082   return build_new (placement, type, nelts, initializer, global_scope_p);
5083 }
5084
5085 /* Parse a new-placement.
5086
5087    new-placement:
5088      ( expression-list )
5089
5090    Returns the same representation as for an expression-list.  */
5091
5092 static tree
5093 cp_parser_new_placement (cp_parser* parser)
5094 {
5095   tree expression_list;
5096
5097   /* Parse the expression-list.  */
5098   expression_list = (cp_parser_parenthesized_expression_list
5099                      (parser, false, /*cast_p=*/false,
5100                       /*non_constant_p=*/NULL));
5101
5102   return expression_list;
5103 }
5104
5105 /* Parse a new-type-id.
5106
5107    new-type-id:
5108      type-specifier-seq new-declarator [opt]
5109
5110    Returns the TYPE allocated.  If the new-type-id indicates an array
5111    type, *NELTS is set to the number of elements in the last array
5112    bound; the TYPE will not include the last array bound.  */
5113
5114 static tree
5115 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5116 {
5117   cp_decl_specifier_seq type_specifier_seq;
5118   cp_declarator *new_declarator;
5119   cp_declarator *declarator;
5120   cp_declarator *outer_declarator;
5121   const char *saved_message;
5122   tree type;
5123
5124   /* The type-specifier sequence must not contain type definitions.
5125      (It cannot contain declarations of new types either, but if they
5126      are not definitions we will catch that because they are not
5127      complete.)  */
5128   saved_message = parser->type_definition_forbidden_message;
5129   parser->type_definition_forbidden_message
5130     = "types may not be defined in a new-type-id";
5131   /* Parse the type-specifier-seq.  */
5132   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5133                                 &type_specifier_seq);
5134   /* Restore the old message.  */
5135   parser->type_definition_forbidden_message = saved_message;
5136   /* Parse the new-declarator.  */
5137   new_declarator = cp_parser_new_declarator_opt (parser);
5138
5139   /* Determine the number of elements in the last array dimension, if
5140      any.  */
5141   *nelts = NULL_TREE;
5142   /* Skip down to the last array dimension.  */
5143   declarator = new_declarator;
5144   outer_declarator = NULL;
5145   while (declarator && (declarator->kind == cdk_pointer
5146                         || declarator->kind == cdk_ptrmem))
5147     {
5148       outer_declarator = declarator;
5149       declarator = declarator->declarator;
5150     }
5151   while (declarator
5152          && declarator->kind == cdk_array
5153          && declarator->declarator
5154          && declarator->declarator->kind == cdk_array)
5155     {
5156       outer_declarator = declarator;
5157       declarator = declarator->declarator;
5158     }
5159
5160   if (declarator && declarator->kind == cdk_array)
5161     {
5162       *nelts = declarator->u.array.bounds;
5163       if (*nelts == error_mark_node)
5164         *nelts = integer_one_node;
5165
5166       if (outer_declarator)
5167         outer_declarator->declarator = declarator->declarator;
5168       else
5169         new_declarator = NULL;
5170     }
5171
5172   type = groktypename (&type_specifier_seq, new_declarator);
5173   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5174     {
5175       *nelts = array_type_nelts_top (type);
5176       type = TREE_TYPE (type);
5177     }
5178   return type;
5179 }
5180
5181 /* Parse an (optional) new-declarator.
5182
5183    new-declarator:
5184      ptr-operator new-declarator [opt]
5185      direct-new-declarator
5186
5187    Returns the declarator.  */
5188
5189 static cp_declarator *
5190 cp_parser_new_declarator_opt (cp_parser* parser)
5191 {
5192   enum tree_code code;
5193   tree type;
5194   cp_cv_quals cv_quals;
5195
5196   /* We don't know if there's a ptr-operator next, or not.  */
5197   cp_parser_parse_tentatively (parser);
5198   /* Look for a ptr-operator.  */
5199   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5200   /* If that worked, look for more new-declarators.  */
5201   if (cp_parser_parse_definitely (parser))
5202     {
5203       cp_declarator *declarator;
5204
5205       /* Parse another optional declarator.  */
5206       declarator = cp_parser_new_declarator_opt (parser);
5207
5208       /* Create the representation of the declarator.  */
5209       if (type)
5210         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5211       else if (code == INDIRECT_REF)
5212         declarator = make_pointer_declarator (cv_quals, declarator);
5213       else
5214         declarator = make_reference_declarator (cv_quals, declarator);
5215
5216       return declarator;
5217     }
5218
5219   /* If the next token is a `[', there is a direct-new-declarator.  */
5220   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5221     return cp_parser_direct_new_declarator (parser);
5222
5223   return NULL;
5224 }
5225
5226 /* Parse a direct-new-declarator.
5227
5228    direct-new-declarator:
5229      [ expression ]
5230      direct-new-declarator [constant-expression]
5231
5232    */
5233
5234 static cp_declarator *
5235 cp_parser_direct_new_declarator (cp_parser* parser)
5236 {
5237   cp_declarator *declarator = NULL;
5238
5239   while (true)
5240     {
5241       tree expression;
5242
5243       /* Look for the opening `['.  */
5244       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5245       /* The first expression is not required to be constant.  */
5246       if (!declarator)
5247         {
5248           expression = cp_parser_expression (parser, /*cast_p=*/false);
5249           /* The standard requires that the expression have integral
5250              type.  DR 74 adds enumeration types.  We believe that the
5251              real intent is that these expressions be handled like the
5252              expression in a `switch' condition, which also allows
5253              classes with a single conversion to integral or
5254              enumeration type.  */
5255           if (!processing_template_decl)
5256             {
5257               expression
5258                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5259                                               expression,
5260                                               /*complain=*/true);
5261               if (!expression)
5262                 {
5263                   error ("expression in new-declarator must have integral "
5264                          "or enumeration type");
5265                   expression = error_mark_node;
5266                 }
5267             }
5268         }
5269       /* But all the other expressions must be.  */
5270       else
5271         expression
5272           = cp_parser_constant_expression (parser,
5273                                            /*allow_non_constant=*/false,
5274                                            NULL);
5275       /* Look for the closing `]'.  */
5276       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5277
5278       /* Add this bound to the declarator.  */
5279       declarator = make_array_declarator (declarator, expression);
5280
5281       /* If the next token is not a `[', then there are no more
5282          bounds.  */
5283       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5284         break;
5285     }
5286
5287   return declarator;
5288 }
5289
5290 /* Parse a new-initializer.
5291
5292    new-initializer:
5293      ( expression-list [opt] )
5294
5295    Returns a representation of the expression-list.  If there is no
5296    expression-list, VOID_ZERO_NODE is returned.  */
5297
5298 static tree
5299 cp_parser_new_initializer (cp_parser* parser)
5300 {
5301   tree expression_list;
5302
5303   expression_list = (cp_parser_parenthesized_expression_list
5304                      (parser, false, /*cast_p=*/false,
5305                       /*non_constant_p=*/NULL));
5306   if (!expression_list)
5307     expression_list = void_zero_node;
5308
5309   return expression_list;
5310 }
5311
5312 /* Parse a delete-expression.
5313
5314    delete-expression:
5315      :: [opt] delete cast-expression
5316      :: [opt] delete [ ] cast-expression
5317
5318    Returns a representation of the expression.  */
5319
5320 static tree
5321 cp_parser_delete_expression (cp_parser* parser)
5322 {
5323   bool global_scope_p;
5324   bool array_p;
5325   tree expression;
5326
5327   /* Look for the optional `::' operator.  */
5328   global_scope_p
5329     = (cp_parser_global_scope_opt (parser,
5330                                    /*current_scope_valid_p=*/false)
5331        != NULL_TREE);
5332   /* Look for the `delete' keyword.  */
5333   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5334   /* See if the array syntax is in use.  */
5335   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5336     {
5337       /* Consume the `[' token.  */
5338       cp_lexer_consume_token (parser->lexer);
5339       /* Look for the `]' token.  */
5340       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5341       /* Remember that this is the `[]' construct.  */
5342       array_p = true;
5343     }
5344   else
5345     array_p = false;
5346
5347   /* Parse the cast-expression.  */
5348   expression = cp_parser_simple_cast_expression (parser);
5349
5350   /* A delete-expression may not appear in an integral constant
5351      expression.  */
5352   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5353     return error_mark_node;
5354
5355   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5356 }
5357
5358 /* Parse a cast-expression.
5359
5360    cast-expression:
5361      unary-expression
5362      ( type-id ) cast-expression
5363
5364    ADDRESS_P is true iff the unary-expression is appearing as the
5365    operand of the `&' operator.   CAST_P is true if this expression is
5366    the target of a cast.
5367
5368    Returns a representation of the expression.  */
5369
5370 static tree
5371 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5372 {
5373   /* If it's a `(', then we might be looking at a cast.  */
5374   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5375     {
5376       tree type = NULL_TREE;
5377       tree expr = NULL_TREE;
5378       bool compound_literal_p;
5379       const char *saved_message;
5380
5381       /* There's no way to know yet whether or not this is a cast.
5382          For example, `(int (3))' is a unary-expression, while `(int)
5383          3' is a cast.  So, we resort to parsing tentatively.  */
5384       cp_parser_parse_tentatively (parser);
5385       /* Types may not be defined in a cast.  */
5386       saved_message = parser->type_definition_forbidden_message;
5387       parser->type_definition_forbidden_message
5388         = "types may not be defined in casts";
5389       /* Consume the `('.  */
5390       cp_lexer_consume_token (parser->lexer);
5391       /* A very tricky bit is that `(struct S) { 3 }' is a
5392          compound-literal (which we permit in C++ as an extension).
5393          But, that construct is not a cast-expression -- it is a
5394          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5395          is legal; if the compound-literal were a cast-expression,
5396          you'd need an extra set of parentheses.)  But, if we parse
5397          the type-id, and it happens to be a class-specifier, then we
5398          will commit to the parse at that point, because we cannot
5399          undo the action that is done when creating a new class.  So,
5400          then we cannot back up and do a postfix-expression.
5401
5402          Therefore, we scan ahead to the closing `)', and check to see
5403          if the token after the `)' is a `{'.  If so, we are not
5404          looking at a cast-expression.
5405
5406          Save tokens so that we can put them back.  */
5407       cp_lexer_save_tokens (parser->lexer);
5408       /* Skip tokens until the next token is a closing parenthesis.
5409          If we find the closing `)', and the next token is a `{', then
5410          we are looking at a compound-literal.  */
5411       compound_literal_p
5412         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5413                                                   /*consume_paren=*/true)
5414            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5415       /* Roll back the tokens we skipped.  */
5416       cp_lexer_rollback_tokens (parser->lexer);
5417       /* If we were looking at a compound-literal, simulate an error
5418          so that the call to cp_parser_parse_definitely below will
5419          fail.  */
5420       if (compound_literal_p)
5421         cp_parser_simulate_error (parser);
5422       else
5423         {
5424           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5425           parser->in_type_id_in_expr_p = true;
5426           /* Look for the type-id.  */
5427           type = cp_parser_type_id (parser);
5428           /* Look for the closing `)'.  */
5429           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5430           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5431         }
5432
5433       /* Restore the saved message.  */
5434       parser->type_definition_forbidden_message = saved_message;
5435
5436       /* If ok so far, parse the dependent expression. We cannot be
5437          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5438          ctor of T, but looks like a cast to function returning T
5439          without a dependent expression.  */
5440       if (!cp_parser_error_occurred (parser))
5441         expr = cp_parser_cast_expression (parser,
5442                                           /*address_p=*/false,
5443                                           /*cast_p=*/true);
5444
5445       if (cp_parser_parse_definitely (parser))
5446         {
5447           /* Warn about old-style casts, if so requested.  */
5448           if (warn_old_style_cast
5449               && !in_system_header
5450               && !VOID_TYPE_P (type)
5451               && current_lang_name != lang_name_c)
5452             warning (OPT_Wold_style_cast, "use of old-style cast");
5453
5454           /* Only type conversions to integral or enumeration types
5455              can be used in constant-expressions.  */
5456           if (parser->integral_constant_expression_p
5457               && !dependent_type_p (type)
5458               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5459               && (cp_parser_non_integral_constant_expression
5460                   (parser,
5461                    "a cast to a type other than an integral or "
5462                    "enumeration type")))
5463             return error_mark_node;
5464
5465           /* Perform the cast.  */
5466           expr = build_c_cast (type, expr);
5467           return expr;
5468         }
5469     }
5470
5471   /* If we get here, then it's not a cast, so it must be a
5472      unary-expression.  */
5473   return cp_parser_unary_expression (parser, address_p, cast_p);
5474 }
5475
5476 /* Parse a binary expression of the general form:
5477
5478    pm-expression:
5479      cast-expression
5480      pm-expression .* cast-expression
5481      pm-expression ->* cast-expression
5482
5483    multiplicative-expression:
5484      pm-expression
5485      multiplicative-expression * pm-expression
5486      multiplicative-expression / pm-expression
5487      multiplicative-expression % pm-expression
5488
5489    additive-expression:
5490      multiplicative-expression
5491      additive-expression + multiplicative-expression
5492      additive-expression - multiplicative-expression
5493
5494    shift-expression:
5495      additive-expression
5496      shift-expression << additive-expression
5497      shift-expression >> additive-expression
5498
5499    relational-expression:
5500      shift-expression
5501      relational-expression < shift-expression
5502      relational-expression > shift-expression
5503      relational-expression <= shift-expression
5504      relational-expression >= shift-expression
5505
5506   GNU Extension:
5507
5508    relational-expression:
5509      relational-expression <? shift-expression
5510      relational-expression >? shift-expression
5511
5512    equality-expression:
5513      relational-expression
5514      equality-expression == relational-expression
5515      equality-expression != relational-expression
5516
5517    and-expression:
5518      equality-expression
5519      and-expression & equality-expression
5520
5521    exclusive-or-expression:
5522      and-expression
5523      exclusive-or-expression ^ and-expression
5524
5525    inclusive-or-expression:
5526      exclusive-or-expression
5527      inclusive-or-expression | exclusive-or-expression
5528
5529    logical-and-expression:
5530      inclusive-or-expression
5531      logical-and-expression && inclusive-or-expression
5532
5533    logical-or-expression:
5534      logical-and-expression
5535      logical-or-expression || logical-and-expression
5536
5537    All these are implemented with a single function like:
5538
5539    binary-expression:
5540      simple-cast-expression
5541      binary-expression <token> binary-expression
5542
5543    CAST_P is true if this expression is the target of a cast.
5544
5545    The binops_by_token map is used to get the tree codes for each <token> type.
5546    binary-expressions are associated according to a precedence table.  */
5547
5548 #define TOKEN_PRECEDENCE(token) \
5549   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5550    ? PREC_NOT_OPERATOR \
5551    : binops_by_token[token->type].prec)
5552
5553 static tree
5554 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5555 {
5556   cp_parser_expression_stack stack;
5557   cp_parser_expression_stack_entry *sp = &stack[0];
5558   tree lhs, rhs;
5559   cp_token *token;
5560   enum tree_code tree_type;
5561   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5562   bool overloaded_p;
5563
5564   /* Parse the first expression.  */
5565   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5566
5567   for (;;)
5568     {
5569       /* Get an operator token.  */
5570       token = cp_lexer_peek_token (parser->lexer);
5571       if (token->type == CPP_MIN || token->type == CPP_MAX)
5572         cp_parser_warn_min_max ();
5573
5574       new_prec = TOKEN_PRECEDENCE (token);
5575
5576       /* Popping an entry off the stack means we completed a subexpression:
5577          - either we found a token which is not an operator (`>' where it is not
5578            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5579            will happen repeatedly;
5580          - or, we found an operator which has lower priority.  This is the case
5581            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5582            parsing `3 * 4'.  */
5583       if (new_prec <= prec)
5584         {
5585           if (sp == stack)
5586             break;
5587           else
5588             goto pop;
5589         }
5590
5591      get_rhs:
5592       tree_type = binops_by_token[token->type].tree_type;
5593
5594       /* We used the operator token.  */
5595       cp_lexer_consume_token (parser->lexer);
5596
5597       /* Extract another operand.  It may be the RHS of this expression
5598          or the LHS of a new, higher priority expression.  */
5599       rhs = cp_parser_simple_cast_expression (parser);
5600
5601       /* Get another operator token.  Look up its precedence to avoid
5602          building a useless (immediately popped) stack entry for common
5603          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5604       token = cp_lexer_peek_token (parser->lexer);
5605       lookahead_prec = TOKEN_PRECEDENCE (token);
5606       if (lookahead_prec > new_prec)
5607         {
5608           /* ... and prepare to parse the RHS of the new, higher priority
5609              expression.  Since precedence levels on the stack are
5610              monotonically increasing, we do not have to care about
5611              stack overflows.  */
5612           sp->prec = prec;
5613           sp->tree_type = tree_type;
5614           sp->lhs = lhs;
5615           sp++;
5616           lhs = rhs;
5617           prec = new_prec;
5618           new_prec = lookahead_prec;
5619           goto get_rhs;
5620
5621          pop:
5622           /* If the stack is not empty, we have parsed into LHS the right side
5623              (`4' in the example above) of an expression we had suspended.
5624              We can use the information on the stack to recover the LHS (`3')
5625              from the stack together with the tree code (`MULT_EXPR'), and
5626              the precedence of the higher level subexpression
5627              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5628              which will be used to actually build the additive expression.  */
5629           --sp;
5630           prec = sp->prec;
5631           tree_type = sp->tree_type;
5632           rhs = lhs;
5633           lhs = sp->lhs;
5634         }
5635
5636       overloaded_p = false;
5637       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5638
5639       /* If the binary operator required the use of an overloaded operator,
5640          then this expression cannot be an integral constant-expression.
5641          An overloaded operator can be used even if both operands are
5642          otherwise permissible in an integral constant-expression if at
5643          least one of the operands is of enumeration type.  */
5644
5645       if (overloaded_p
5646           && (cp_parser_non_integral_constant_expression
5647               (parser, "calls to overloaded operators")))
5648         return error_mark_node;
5649     }
5650
5651   return lhs;
5652 }
5653
5654
5655 /* Parse the `? expression : assignment-expression' part of a
5656    conditional-expression.  The LOGICAL_OR_EXPR is the
5657    logical-or-expression that started the conditional-expression.
5658    Returns a representation of the entire conditional-expression.
5659
5660    This routine is used by cp_parser_assignment_expression.
5661
5662      ? expression : assignment-expression
5663
5664    GNU Extensions:
5665
5666      ? : assignment-expression */
5667
5668 static tree
5669 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5670 {
5671   tree expr;
5672   tree assignment_expr;
5673
5674   /* Consume the `?' token.  */
5675   cp_lexer_consume_token (parser->lexer);
5676   if (cp_parser_allow_gnu_extensions_p (parser)
5677       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5678     /* Implicit true clause.  */
5679     expr = NULL_TREE;
5680   else
5681     /* Parse the expression.  */
5682     expr = cp_parser_expression (parser, /*cast_p=*/false);
5683
5684   /* The next token should be a `:'.  */
5685   cp_parser_require (parser, CPP_COLON, "`:'");
5686   /* Parse the assignment-expression.  */
5687   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5688
5689   /* Build the conditional-expression.  */
5690   return build_x_conditional_expr (logical_or_expr,
5691                                    expr,
5692                                    assignment_expr);
5693 }
5694
5695 /* Parse an assignment-expression.
5696
5697    assignment-expression:
5698      conditional-expression
5699      logical-or-expression assignment-operator assignment_expression
5700      throw-expression
5701
5702    CAST_P is true if this expression is the target of a cast.
5703
5704    Returns a representation for the expression.  */
5705
5706 static tree
5707 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5708 {
5709   tree expr;
5710
5711   /* If the next token is the `throw' keyword, then we're looking at
5712      a throw-expression.  */
5713   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5714     expr = cp_parser_throw_expression (parser);
5715   /* Otherwise, it must be that we are looking at a
5716      logical-or-expression.  */
5717   else
5718     {
5719       /* Parse the binary expressions (logical-or-expression).  */
5720       expr = cp_parser_binary_expression (parser, cast_p);
5721       /* If the next token is a `?' then we're actually looking at a
5722          conditional-expression.  */
5723       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5724         return cp_parser_question_colon_clause (parser, expr);
5725       else
5726         {
5727           enum tree_code assignment_operator;
5728
5729           /* If it's an assignment-operator, we're using the second
5730              production.  */
5731           assignment_operator
5732             = cp_parser_assignment_operator_opt (parser);
5733           if (assignment_operator != ERROR_MARK)
5734             {
5735               tree rhs;
5736
5737               /* Parse the right-hand side of the assignment.  */
5738               rhs = cp_parser_assignment_expression (parser, cast_p);
5739               /* An assignment may not appear in a
5740                  constant-expression.  */
5741               if (cp_parser_non_integral_constant_expression (parser,
5742                                                               "an assignment"))
5743                 return error_mark_node;
5744               /* Build the assignment expression.  */
5745               expr = build_x_modify_expr (expr,
5746                                           assignment_operator,
5747                                           rhs);
5748             }
5749         }
5750     }
5751
5752   return expr;
5753 }
5754
5755 /* Parse an (optional) assignment-operator.
5756
5757    assignment-operator: one of
5758      = *= /= %= += -= >>= <<= &= ^= |=
5759
5760    GNU Extension:
5761
5762    assignment-operator: one of
5763      <?= >?=
5764
5765    If the next token is an assignment operator, the corresponding tree
5766    code is returned, and the token is consumed.  For example, for
5767    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5768    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5769    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5770    operator, ERROR_MARK is returned.  */
5771
5772 static enum tree_code
5773 cp_parser_assignment_operator_opt (cp_parser* parser)
5774 {
5775   enum tree_code op;
5776   cp_token *token;
5777
5778   /* Peek at the next toen.  */
5779   token = cp_lexer_peek_token (parser->lexer);
5780
5781   switch (token->type)
5782     {
5783     case CPP_EQ:
5784       op = NOP_EXPR;
5785       break;
5786
5787     case CPP_MULT_EQ:
5788       op = MULT_EXPR;
5789       break;
5790
5791     case CPP_DIV_EQ:
5792       op = TRUNC_DIV_EXPR;
5793       break;
5794
5795     case CPP_MOD_EQ:
5796       op = TRUNC_MOD_EXPR;
5797       break;
5798
5799     case CPP_PLUS_EQ:
5800       op = PLUS_EXPR;
5801       break;
5802
5803     case CPP_MINUS_EQ:
5804       op = MINUS_EXPR;
5805       break;
5806
5807     case CPP_RSHIFT_EQ:
5808       op = RSHIFT_EXPR;
5809       break;
5810
5811     case CPP_LSHIFT_EQ:
5812       op = LSHIFT_EXPR;
5813       break;
5814
5815     case CPP_AND_EQ:
5816       op = BIT_AND_EXPR;
5817       break;
5818
5819     case CPP_XOR_EQ:
5820       op = BIT_XOR_EXPR;
5821       break;
5822
5823     case CPP_OR_EQ:
5824       op = BIT_IOR_EXPR;
5825       break;
5826
5827     case CPP_MIN_EQ:
5828       op = MIN_EXPR;
5829       cp_parser_warn_min_max ();
5830       break;
5831
5832     case CPP_MAX_EQ:
5833       op = MAX_EXPR;
5834       cp_parser_warn_min_max ();
5835       break;
5836
5837     default:
5838       /* Nothing else is an assignment operator.  */
5839       op = ERROR_MARK;
5840     }
5841
5842   /* If it was an assignment operator, consume it.  */
5843   if (op != ERROR_MARK)
5844     cp_lexer_consume_token (parser->lexer);
5845
5846   return op;
5847 }
5848
5849 /* Parse an expression.
5850
5851    expression:
5852      assignment-expression
5853      expression , assignment-expression
5854
5855    CAST_P is true if this expression is the target of a cast.
5856
5857    Returns a representation of the expression.  */
5858
5859 static tree
5860 cp_parser_expression (cp_parser* parser, bool cast_p)
5861 {
5862   tree expression = NULL_TREE;
5863
5864   while (true)
5865     {
5866       tree assignment_expression;
5867
5868       /* Parse the next assignment-expression.  */
5869       assignment_expression
5870         = cp_parser_assignment_expression (parser, cast_p);
5871       /* If this is the first assignment-expression, we can just
5872          save it away.  */
5873       if (!expression)
5874         expression = assignment_expression;
5875       else
5876         expression = build_x_compound_expr (expression,
5877                                             assignment_expression);
5878       /* If the next token is not a comma, then we are done with the
5879          expression.  */
5880       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5881         break;
5882       /* Consume the `,'.  */
5883       cp_lexer_consume_token (parser->lexer);
5884       /* A comma operator cannot appear in a constant-expression.  */
5885       if (cp_parser_non_integral_constant_expression (parser,
5886                                                       "a comma operator"))
5887         expression = error_mark_node;
5888     }
5889
5890   return expression;
5891 }
5892
5893 /* Parse a constant-expression.
5894
5895    constant-expression:
5896      conditional-expression
5897
5898   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5899   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5900   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5901   is false, NON_CONSTANT_P should be NULL.  */
5902
5903 static tree
5904 cp_parser_constant_expression (cp_parser* parser,
5905                                bool allow_non_constant_p,
5906                                bool *non_constant_p)
5907 {
5908   bool saved_integral_constant_expression_p;
5909   bool saved_allow_non_integral_constant_expression_p;
5910   bool saved_non_integral_constant_expression_p;
5911   tree expression;
5912
5913   /* It might seem that we could simply parse the
5914      conditional-expression, and then check to see if it were
5915      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5916      one that the compiler can figure out is constant, possibly after
5917      doing some simplifications or optimizations.  The standard has a
5918      precise definition of constant-expression, and we must honor
5919      that, even though it is somewhat more restrictive.
5920
5921      For example:
5922
5923        int i[(2, 3)];
5924
5925      is not a legal declaration, because `(2, 3)' is not a
5926      constant-expression.  The `,' operator is forbidden in a
5927      constant-expression.  However, GCC's constant-folding machinery
5928      will fold this operation to an INTEGER_CST for `3'.  */
5929
5930   /* Save the old settings.  */
5931   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5932   saved_allow_non_integral_constant_expression_p
5933     = parser->allow_non_integral_constant_expression_p;
5934   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5935   /* We are now parsing a constant-expression.  */
5936   parser->integral_constant_expression_p = true;
5937   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5938   parser->non_integral_constant_expression_p = false;
5939   /* Although the grammar says "conditional-expression", we parse an
5940      "assignment-expression", which also permits "throw-expression"
5941      and the use of assignment operators.  In the case that
5942      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5943      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5944      actually essential that we look for an assignment-expression.
5945      For example, cp_parser_initializer_clauses uses this function to
5946      determine whether a particular assignment-expression is in fact
5947      constant.  */
5948   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5949   /* Restore the old settings.  */
5950   parser->integral_constant_expression_p
5951     = saved_integral_constant_expression_p;
5952   parser->allow_non_integral_constant_expression_p
5953     = saved_allow_non_integral_constant_expression_p;
5954   if (allow_non_constant_p)
5955     *non_constant_p = parser->non_integral_constant_expression_p;
5956   else if (parser->non_integral_constant_expression_p)
5957     expression = error_mark_node;
5958   parser->non_integral_constant_expression_p
5959     = saved_non_integral_constant_expression_p;
5960
5961   return expression;
5962 }
5963
5964 /* Parse __builtin_offsetof.
5965
5966    offsetof-expression:
5967      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5968
5969    offsetof-member-designator:
5970      id-expression
5971      | offsetof-member-designator "." id-expression
5972      | offsetof-member-designator "[" expression "]"
5973 */
5974
5975 static tree
5976 cp_parser_builtin_offsetof (cp_parser *parser)
5977 {
5978   int save_ice_p, save_non_ice_p;
5979   tree type, expr;
5980   cp_id_kind dummy;
5981
5982   /* We're about to accept non-integral-constant things, but will
5983      definitely yield an integral constant expression.  Save and
5984      restore these values around our local parsing.  */
5985   save_ice_p = parser->integral_constant_expression_p;
5986   save_non_ice_p = parser->non_integral_constant_expression_p;
5987
5988   /* Consume the "__builtin_offsetof" token.  */
5989   cp_lexer_consume_token (parser->lexer);
5990   /* Consume the opening `('.  */
5991   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5992   /* Parse the type-id.  */
5993   type = cp_parser_type_id (parser);
5994   /* Look for the `,'.  */
5995   cp_parser_require (parser, CPP_COMMA, "`,'");
5996
5997   /* Build the (type *)null that begins the traditional offsetof macro.  */
5998   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5999
6000   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6001   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6002                                                  true, &dummy);
6003   while (true)
6004     {
6005       cp_token *token = cp_lexer_peek_token (parser->lexer);
6006       switch (token->type)
6007         {
6008         case CPP_OPEN_SQUARE:
6009           /* offsetof-member-designator "[" expression "]" */
6010           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6011           break;
6012
6013         case CPP_DOT:
6014           /* offsetof-member-designator "." identifier */
6015           cp_lexer_consume_token (parser->lexer);
6016           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6017                                                          true, &dummy);
6018           break;
6019
6020         case CPP_CLOSE_PAREN:
6021           /* Consume the ")" token.  */
6022           cp_lexer_consume_token (parser->lexer);
6023           goto success;
6024
6025         default:
6026           /* Error.  We know the following require will fail, but
6027              that gives the proper error message.  */
6028           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6029           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6030           expr = error_mark_node;
6031           goto failure;
6032         }
6033     }
6034
6035  success:
6036   /* If we're processing a template, we can't finish the semantics yet.
6037      Otherwise we can fold the entire expression now.  */
6038   if (processing_template_decl)
6039     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6040   else
6041     expr = fold_offsetof (expr);
6042
6043  failure:
6044   parser->integral_constant_expression_p = save_ice_p;
6045   parser->non_integral_constant_expression_p = save_non_ice_p;
6046
6047   return expr;
6048 }
6049
6050 /* Statements [gram.stmt.stmt]  */
6051
6052 /* Parse a statement.
6053
6054    statement:
6055      labeled-statement
6056      expression-statement
6057      compound-statement
6058      selection-statement
6059      iteration-statement
6060      jump-statement
6061      declaration-statement
6062      try-block
6063
6064   IN_COMPOUND is true when the statement is nested inside a 
6065   cp_parser_compound_statement; this matters for certain pragmas.  */
6066
6067 static void
6068 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6069                      bool in_compound)
6070 {
6071   tree statement;
6072   cp_token *token;
6073   location_t statement_location;
6074
6075  restart:
6076   /* There is no statement yet.  */
6077   statement = NULL_TREE;
6078   /* Peek at the next token.  */
6079   token = cp_lexer_peek_token (parser->lexer);
6080   /* Remember the location of the first token in the statement.  */
6081   statement_location = token->location;
6082   /* If this is a keyword, then that will often determine what kind of
6083      statement we have.  */
6084   if (token->type == CPP_KEYWORD)
6085     {
6086       enum rid keyword = token->keyword;
6087
6088       switch (keyword)
6089         {
6090         case RID_CASE:
6091         case RID_DEFAULT:
6092           statement = cp_parser_labeled_statement (parser, in_statement_expr,
6093                                                    in_compound);
6094           break;
6095
6096         case RID_IF:
6097         case RID_SWITCH:
6098           statement = cp_parser_selection_statement (parser);
6099           break;
6100
6101         case RID_WHILE:
6102         case RID_DO:
6103         case RID_FOR:
6104           statement = cp_parser_iteration_statement (parser);
6105           break;
6106
6107         case RID_BREAK:
6108         case RID_CONTINUE:
6109         case RID_RETURN:
6110         case RID_GOTO:
6111           statement = cp_parser_jump_statement (parser);
6112           break;
6113
6114           /* Objective-C++ exception-handling constructs.  */
6115         case RID_AT_TRY:
6116         case RID_AT_CATCH:
6117         case RID_AT_FINALLY:
6118         case RID_AT_SYNCHRONIZED:
6119         case RID_AT_THROW:
6120           statement = cp_parser_objc_statement (parser);
6121           break;
6122
6123         case RID_TRY:
6124           statement = cp_parser_try_block (parser);
6125           break;
6126
6127         default:
6128           /* It might be a keyword like `int' that can start a
6129              declaration-statement.  */
6130           break;
6131         }
6132     }
6133   else if (token->type == CPP_NAME)
6134     {
6135       /* If the next token is a `:', then we are looking at a
6136          labeled-statement.  */
6137       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6138       if (token->type == CPP_COLON)
6139         statement = cp_parser_labeled_statement (parser, in_statement_expr,
6140                                                  in_compound);
6141     }
6142   /* Anything that starts with a `{' must be a compound-statement.  */
6143   else if (token->type == CPP_OPEN_BRACE)
6144     statement = cp_parser_compound_statement (parser, NULL, false);
6145   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6146      a statement all its own.  */
6147   else if (token->type == CPP_PRAGMA)
6148     {
6149       /* Only certain OpenMP pragmas are attached to statements, and thus
6150          are considered statements themselves.  All others are not.  In
6151          the context of a compound, accept the pragma as a "statement" and
6152          return so that we can check for a close brace.  Otherwise we 
6153          require a real statement and must go back and read one.  */
6154       if (in_compound)
6155         cp_parser_pragma (parser, pragma_compound);
6156       else if (!cp_parser_pragma (parser, pragma_stmt))
6157         goto restart;
6158       return;
6159     }
6160   else if (token->type == CPP_EOF)
6161     {
6162       cp_parser_error (parser, "expected statement");
6163       return;
6164     }
6165
6166   /* Everything else must be a declaration-statement or an
6167      expression-statement.  Try for the declaration-statement
6168      first, unless we are looking at a `;', in which case we know that
6169      we have an expression-statement.  */
6170   if (!statement)
6171     {
6172       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6173         {
6174           cp_parser_parse_tentatively (parser);
6175           /* Try to parse the declaration-statement.  */
6176           cp_parser_declaration_statement (parser);
6177           /* If that worked, we're done.  */
6178           if (cp_parser_parse_definitely (parser))
6179             return;
6180         }
6181       /* Look for an expression-statement instead.  */
6182       statement = cp_parser_expression_statement (parser, in_statement_expr);
6183     }
6184
6185   /* Set the line number for the statement.  */
6186   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6187     SET_EXPR_LOCATION (statement, statement_location);
6188 }
6189
6190 /* Parse a labeled-statement.
6191
6192    labeled-statement:
6193      identifier : statement
6194      case constant-expression : statement
6195      default : statement
6196
6197    GNU Extension:
6198
6199    labeled-statement:
6200      case constant-expression ... constant-expression : statement
6201
6202    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6203    For an ordinary label, returns a LABEL_EXPR.
6204
6205    IN_COMPOUND is as for cp_parser_statement: true when we're nested
6206    inside a compound.  */
6207
6208 static tree
6209 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6210                              bool in_compound)
6211 {
6212   cp_token *token;
6213   tree statement = error_mark_node;
6214
6215   /* The next token should be an identifier.  */
6216   token = cp_lexer_peek_token (parser->lexer);
6217   if (token->type != CPP_NAME
6218       && token->type != CPP_KEYWORD)
6219     {
6220       cp_parser_error (parser, "expected labeled-statement");
6221       return error_mark_node;
6222     }
6223
6224   switch (token->keyword)
6225     {
6226     case RID_CASE:
6227       {
6228         tree expr, expr_hi;
6229         cp_token *ellipsis;
6230
6231         /* Consume the `case' token.  */
6232         cp_lexer_consume_token (parser->lexer);
6233         /* Parse the constant-expression.  */
6234         expr = cp_parser_constant_expression (parser,
6235                                               /*allow_non_constant_p=*/false,
6236                                               NULL);
6237
6238         ellipsis = cp_lexer_peek_token (parser->lexer);
6239         if (ellipsis->type == CPP_ELLIPSIS)
6240           {
6241             /* Consume the `...' token.  */
6242             cp_lexer_consume_token (parser->lexer);
6243             expr_hi =
6244               cp_parser_constant_expression (parser,
6245                                              /*allow_non_constant_p=*/false,
6246                                              NULL);
6247             /* We don't need to emit warnings here, as the common code
6248                will do this for us.  */
6249           }
6250         else
6251           expr_hi = NULL_TREE;
6252
6253         if (parser->in_switch_statement_p)
6254           statement = finish_case_label (expr, expr_hi);
6255         else
6256           error ("case label %qE not within a switch statement", expr);
6257       }
6258       break;
6259
6260     case RID_DEFAULT:
6261       /* Consume the `default' token.  */
6262       cp_lexer_consume_token (parser->lexer);
6263
6264       if (parser->in_switch_statement_p)
6265         statement = finish_case_label (NULL_TREE, NULL_TREE);
6266       else
6267         error ("case label not within a switch statement");
6268       break;
6269
6270     default:
6271       /* Anything else must be an ordinary label.  */
6272       statement = finish_label_stmt (cp_parser_identifier (parser));
6273       break;
6274     }
6275
6276   /* Require the `:' token.  */
6277   cp_parser_require (parser, CPP_COLON, "`:'");
6278   /* Parse the labeled statement.  */
6279   cp_parser_statement (parser, in_statement_expr, in_compound);
6280
6281   /* Return the label, in the case of a `case' or `default' label.  */
6282   return statement;
6283 }
6284
6285 /* Parse an expression-statement.
6286
6287    expression-statement:
6288      expression [opt] ;
6289
6290    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6291    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6292    indicates whether this expression-statement is part of an
6293    expression statement.  */
6294
6295 static tree
6296 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6297 {
6298   tree statement = NULL_TREE;
6299
6300   /* If the next token is a ';', then there is no expression
6301      statement.  */
6302   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6303     statement = cp_parser_expression (parser, /*cast_p=*/false);
6304
6305   /* Consume the final `;'.  */
6306   cp_parser_consume_semicolon_at_end_of_statement (parser);
6307
6308   if (in_statement_expr
6309       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6310     /* This is the final expression statement of a statement
6311        expression.  */
6312     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6313   else if (statement)
6314     statement = finish_expr_stmt (statement);
6315   else
6316     finish_stmt ();
6317
6318   return statement;
6319 }
6320
6321 /* Parse a compound-statement.
6322
6323    compound-statement:
6324      { statement-seq [opt] }
6325
6326    Returns a tree representing the statement.  */
6327
6328 static tree
6329 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6330                               bool in_try)
6331 {
6332   tree compound_stmt;
6333
6334   /* Consume the `{'.  */
6335   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6336     return error_mark_node;
6337   /* Begin the compound-statement.  */
6338   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6339   /* Parse an (optional) statement-seq.  */
6340   cp_parser_statement_seq_opt (parser, in_statement_expr);
6341   /* Finish the compound-statement.  */
6342   finish_compound_stmt (compound_stmt);
6343   /* Consume the `}'.  */
6344   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6345
6346   return compound_stmt;
6347 }
6348
6349 /* Parse an (optional) statement-seq.
6350
6351    statement-seq:
6352      statement
6353      statement-seq [opt] statement  */
6354
6355 static void
6356 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6357 {
6358   /* Scan statements until there aren't any more.  */
6359   while (true)
6360     {
6361       cp_token *token = cp_lexer_peek_token (parser->lexer);
6362
6363       /* If we're looking at a `}', then we've run out of statements.  */
6364       if (token->type == CPP_CLOSE_BRACE
6365           || token->type == CPP_EOF
6366           || token->type == CPP_PRAGMA_EOL)
6367         break;
6368
6369       /* Parse the statement.  */
6370       cp_parser_statement (parser, in_statement_expr, true);
6371     }
6372 }
6373
6374 /* Parse a selection-statement.
6375
6376    selection-statement:
6377      if ( condition ) statement
6378      if ( condition ) statement else statement
6379      switch ( condition ) statement
6380
6381    Returns the new IF_STMT or SWITCH_STMT.  */
6382
6383 static tree
6384 cp_parser_selection_statement (cp_parser* parser)
6385 {
6386   cp_token *token;
6387   enum rid keyword;
6388
6389   /* Peek at the next token.  */
6390   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6391
6392   /* See what kind of keyword it is.  */
6393   keyword = token->keyword;
6394   switch (keyword)
6395     {
6396     case RID_IF:
6397     case RID_SWITCH:
6398       {
6399         tree statement;
6400         tree condition;
6401
6402         /* Look for the `('.  */
6403         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6404           {
6405             cp_parser_skip_to_end_of_statement (parser);
6406             return error_mark_node;
6407           }
6408
6409         /* Begin the selection-statement.  */
6410         if (keyword == RID_IF)
6411           statement = begin_if_stmt ();
6412         else
6413           statement = begin_switch_stmt ();
6414
6415         /* Parse the condition.  */
6416         condition = cp_parser_condition (parser);
6417         /* Look for the `)'.  */
6418         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6419           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6420                                                  /*consume_paren=*/true);
6421
6422         if (keyword == RID_IF)
6423           {
6424             /* Add the condition.  */
6425             finish_if_stmt_cond (condition, statement);
6426
6427             /* Parse the then-clause.  */
6428             cp_parser_implicitly_scoped_statement (parser);
6429             finish_then_clause (statement);
6430
6431             /* If the next token is `else', parse the else-clause.  */
6432             if (cp_lexer_next_token_is_keyword (parser->lexer,
6433                                                 RID_ELSE))
6434               {
6435                 /* Consume the `else' keyword.  */
6436                 cp_lexer_consume_token (parser->lexer);
6437                 begin_else_clause (statement);
6438                 /* Parse the else-clause.  */
6439                 cp_parser_implicitly_scoped_statement (parser);
6440                 finish_else_clause (statement);
6441               }
6442
6443             /* Now we're all done with the if-statement.  */
6444             finish_if_stmt (statement);
6445           }
6446         else
6447           {
6448             bool in_switch_statement_p;
6449             unsigned char in_statement;
6450
6451             /* Add the condition.  */
6452             finish_switch_cond (condition, statement);
6453
6454             /* Parse the body of the switch-statement.  */
6455             in_switch_statement_p = parser->in_switch_statement_p;
6456             in_statement = parser->in_statement;
6457             parser->in_switch_statement_p = true;
6458             parser->in_statement |= IN_SWITCH_STMT;
6459             cp_parser_implicitly_scoped_statement (parser);
6460             parser->in_switch_statement_p = in_switch_statement_p;
6461             parser->in_statement = in_statement;
6462
6463             /* Now we're all done with the switch-statement.  */
6464             finish_switch_stmt (statement);
6465           }
6466
6467         return statement;
6468       }
6469       break;
6470
6471     default:
6472       cp_parser_error (parser, "expected selection-statement");
6473       return error_mark_node;
6474     }
6475 }
6476
6477 /* Parse a condition.
6478
6479    condition:
6480      expression
6481      type-specifier-seq declarator = assignment-expression
6482
6483    GNU Extension:
6484
6485    condition:
6486      type-specifier-seq declarator asm-specification [opt]
6487        attributes [opt] = assignment-expression
6488
6489    Returns the expression that should be tested.  */
6490
6491 static tree
6492 cp_parser_condition (cp_parser* parser)
6493 {
6494   cp_decl_specifier_seq type_specifiers;
6495   const char *saved_message;
6496
6497   /* Try the declaration first.  */
6498   cp_parser_parse_tentatively (parser);
6499   /* New types are not allowed in the type-specifier-seq for a
6500      condition.  */
6501   saved_message = parser->type_definition_forbidden_message;
6502   parser->type_definition_forbidden_message
6503     = "types may not be defined in conditions";
6504   /* Parse the type-specifier-seq.  */
6505   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6506                                 &type_specifiers);
6507   /* Restore the saved message.  */
6508   parser->type_definition_forbidden_message = saved_message;
6509   /* If all is well, we might be looking at a declaration.  */
6510   if (!cp_parser_error_occurred (parser))
6511     {
6512       tree decl;
6513       tree asm_specification;
6514       tree attributes;
6515       cp_declarator *declarator;
6516       tree initializer = NULL_TREE;
6517
6518       /* Parse the declarator.  */
6519       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6520                                          /*ctor_dtor_or_conv_p=*/NULL,
6521                                          /*parenthesized_p=*/NULL,
6522                                          /*member_p=*/false);
6523       /* Parse the attributes.  */
6524       attributes = cp_parser_attributes_opt (parser);
6525       /* Parse the asm-specification.  */
6526       asm_specification = cp_parser_asm_specification_opt (parser);
6527       /* If the next token is not an `=', then we might still be
6528          looking at an expression.  For example:
6529
6530            if (A(a).x)
6531
6532          looks like a decl-specifier-seq and a declarator -- but then
6533          there is no `=', so this is an expression.  */
6534       cp_parser_require (parser, CPP_EQ, "`='");
6535       /* If we did see an `=', then we are looking at a declaration
6536          for sure.  */
6537       if (cp_parser_parse_definitely (parser))
6538         {
6539           tree pushed_scope;
6540           bool non_constant_p;
6541
6542           /* Create the declaration.  */
6543           decl = start_decl (declarator, &type_specifiers,
6544                              /*initialized_p=*/true,
6545                              attributes, /*prefix_attributes=*/NULL_TREE,
6546                              &pushed_scope);
6547           /* Parse the assignment-expression.  */
6548           initializer 
6549             = cp_parser_constant_expression (parser,
6550                                              /*allow_non_constant_p=*/true,
6551                                              &non_constant_p);
6552           if (!non_constant_p)
6553             initializer = fold_non_dependent_expr (initializer);
6554
6555           /* Process the initializer.  */
6556           cp_finish_decl (decl,
6557                           initializer, !non_constant_p, 
6558                           asm_specification,
6559                           LOOKUP_ONLYCONVERTING);
6560
6561           if (pushed_scope)
6562             pop_scope (pushed_scope);
6563
6564           return convert_from_reference (decl);
6565         }
6566     }
6567   /* If we didn't even get past the declarator successfully, we are
6568      definitely not looking at a declaration.  */
6569   else
6570     cp_parser_abort_tentative_parse (parser);
6571
6572   /* Otherwise, we are looking at an expression.  */
6573   return cp_parser_expression (parser, /*cast_p=*/false);
6574 }
6575
6576 /* Parse an iteration-statement.
6577
6578    iteration-statement:
6579      while ( condition ) statement
6580      do statement while ( expression ) ;
6581      for ( for-init-statement condition [opt] ; expression [opt] )
6582        statement
6583
6584    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6585
6586 static tree
6587 cp_parser_iteration_statement (cp_parser* parser)
6588 {
6589   cp_token *token;
6590   enum rid keyword;
6591   tree statement;
6592   unsigned char in_statement;
6593
6594   /* Peek at the next token.  */
6595   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6596   if (!token)
6597     return error_mark_node;
6598
6599   /* Remember whether or not we are already within an iteration
6600      statement.  */
6601   in_statement = parser->in_statement;
6602
6603   /* See what kind of keyword it is.  */
6604   keyword = token->keyword;
6605   switch (keyword)
6606     {
6607     case RID_WHILE:
6608       {
6609         tree condition;
6610
6611         /* Begin the while-statement.  */
6612         statement = begin_while_stmt ();
6613         /* Look for the `('.  */
6614         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6615         /* Parse the condition.  */
6616         condition = cp_parser_condition (parser);
6617         finish_while_stmt_cond (condition, statement);
6618         /* Look for the `)'.  */
6619         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6620         /* Parse the dependent statement.  */
6621         parser->in_statement = IN_ITERATION_STMT;
6622         cp_parser_already_scoped_statement (parser);
6623         parser->in_statement = in_statement;
6624         /* We're done with the while-statement.  */
6625         finish_while_stmt (statement);
6626       }
6627       break;
6628
6629     case RID_DO:
6630       {
6631         tree expression;
6632
6633         /* Begin the do-statement.  */
6634         statement = begin_do_stmt ();
6635         /* Parse the body of the do-statement.  */
6636         parser->in_statement = IN_ITERATION_STMT;
6637         cp_parser_implicitly_scoped_statement (parser);
6638         parser->in_statement = in_statement;
6639         finish_do_body (statement);
6640         /* Look for the `while' keyword.  */
6641         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6642         /* Look for the `('.  */
6643         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6644         /* Parse the expression.  */
6645         expression = cp_parser_expression (parser, /*cast_p=*/false);
6646         /* We're done with the do-statement.  */
6647         finish_do_stmt (expression, statement);
6648         /* Look for the `)'.  */
6649         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6650         /* Look for the `;'.  */
6651         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6652       }
6653       break;
6654
6655     case RID_FOR:
6656       {
6657         tree condition = NULL_TREE;
6658         tree expression = NULL_TREE;
6659
6660         /* Begin the for-statement.  */
6661         statement = begin_for_stmt ();
6662         /* Look for the `('.  */
6663         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6664         /* Parse the initialization.  */
6665         cp_parser_for_init_statement (parser);
6666         finish_for_init_stmt (statement);
6667
6668         /* If there's a condition, process it.  */
6669         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6670           condition = cp_parser_condition (parser);
6671         finish_for_cond (condition, statement);
6672         /* Look for the `;'.  */
6673         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6674
6675         /* If there's an expression, process it.  */
6676         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6677           expression = cp_parser_expression (parser, /*cast_p=*/false);
6678         finish_for_expr (expression, statement);
6679         /* Look for the `)'.  */
6680         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6681
6682         /* Parse the body of the for-statement.  */
6683         parser->in_statement = IN_ITERATION_STMT;
6684         cp_parser_already_scoped_statement (parser);
6685         parser->in_statement = in_statement;
6686
6687         /* We're done with the for-statement.  */
6688         finish_for_stmt (statement);
6689       }
6690       break;
6691
6692     default:
6693       cp_parser_error (parser, "expected iteration-statement");
6694       statement = error_mark_node;
6695       break;
6696     }
6697
6698   return statement;
6699 }
6700
6701 /* Parse a for-init-statement.
6702
6703    for-init-statement:
6704      expression-statement
6705      simple-declaration  */
6706
6707 static void
6708 cp_parser_for_init_statement (cp_parser* parser)
6709 {
6710   /* If the next token is a `;', then we have an empty
6711      expression-statement.  Grammatically, this is also a
6712      simple-declaration, but an invalid one, because it does not
6713      declare anything.  Therefore, if we did not handle this case
6714      specially, we would issue an error message about an invalid
6715      declaration.  */
6716   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6717     {
6718       /* We're going to speculatively look for a declaration, falling back
6719          to an expression, if necessary.  */
6720       cp_parser_parse_tentatively (parser);
6721       /* Parse the declaration.  */
6722       cp_parser_simple_declaration (parser,
6723                                     /*function_definition_allowed_p=*/false);
6724       /* If the tentative parse failed, then we shall need to look for an
6725          expression-statement.  */
6726       if (cp_parser_parse_definitely (parser))
6727         return;
6728     }
6729
6730   cp_parser_expression_statement (parser, false);
6731 }
6732
6733 /* Parse a jump-statement.
6734
6735    jump-statement:
6736      break ;
6737      continue ;
6738      return expression [opt] ;
6739      goto identifier ;
6740
6741    GNU extension:
6742
6743    jump-statement:
6744      goto * expression ;
6745
6746    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6747
6748 static tree
6749 cp_parser_jump_statement (cp_parser* parser)
6750 {
6751   tree statement = error_mark_node;
6752   cp_token *token;
6753   enum rid keyword;
6754
6755   /* Peek at the next token.  */
6756   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6757   if (!token)
6758     return error_mark_node;
6759
6760   /* See what kind of keyword it is.  */
6761   keyword = token->keyword;
6762   switch (keyword)
6763     {
6764     case RID_BREAK:
6765       switch (parser->in_statement)
6766         {
6767         case 0:
6768           error ("break statement not within loop or switch");
6769           break;
6770         default:
6771           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6772                       || parser->in_statement == IN_ITERATION_STMT);
6773           statement = finish_break_stmt ();
6774           break;
6775         case IN_OMP_BLOCK:
6776           error ("invalid exit from OpenMP structured block");
6777           break;
6778         case IN_OMP_FOR:
6779           error ("break statement used with OpenMP for loop");
6780           break;
6781         }
6782       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6783       break;
6784
6785     case RID_CONTINUE:
6786       switch (parser->in_statement & ~IN_SWITCH_STMT)
6787         {
6788         case 0:
6789           error ("continue statement not within a loop");
6790           break;
6791         case IN_ITERATION_STMT:
6792         case IN_OMP_FOR:
6793           statement = finish_continue_stmt ();
6794           break;
6795         case IN_OMP_BLOCK:
6796           error ("invalid exit from OpenMP structured block");
6797           break;
6798         default:
6799           gcc_unreachable ();
6800         }
6801       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6802       break;
6803
6804     case RID_RETURN:
6805       {
6806         tree expr;
6807
6808         /* If the next token is a `;', then there is no
6809            expression.  */
6810         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6811           expr = cp_parser_expression (parser, /*cast_p=*/false);
6812         else
6813           expr = NULL_TREE;
6814         /* Build the return-statement.  */
6815         statement = finish_return_stmt (expr);
6816         /* Look for the final `;'.  */
6817         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6818       }
6819       break;
6820
6821     case RID_GOTO:
6822       /* Create the goto-statement.  */
6823       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6824         {
6825           /* Issue a warning about this use of a GNU extension.  */
6826           if (pedantic)
6827             pedwarn ("ISO C++ forbids computed gotos");
6828           /* Consume the '*' token.  */
6829           cp_lexer_consume_token (parser->lexer);
6830           /* Parse the dependent expression.  */
6831           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6832         }
6833       else
6834         finish_goto_stmt (cp_parser_identifier (parser));
6835       /* Look for the final `;'.  */
6836       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6837       break;
6838
6839     default:
6840       cp_parser_error (parser, "expected jump-statement");
6841       break;
6842     }
6843
6844   return statement;
6845 }
6846
6847 /* Parse a declaration-statement.
6848
6849    declaration-statement:
6850      block-declaration  */
6851
6852 static void
6853 cp_parser_declaration_statement (cp_parser* parser)
6854 {
6855   void *p;
6856
6857   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6858   p = obstack_alloc (&declarator_obstack, 0);
6859
6860  /* Parse the block-declaration.  */
6861   cp_parser_block_declaration (parser, /*statement_p=*/true);
6862
6863   /* Free any declarators allocated.  */
6864   obstack_free (&declarator_obstack, p);
6865
6866   /* Finish off the statement.  */
6867   finish_stmt ();
6868 }
6869
6870 /* Some dependent statements (like `if (cond) statement'), are
6871    implicitly in their own scope.  In other words, if the statement is
6872    a single statement (as opposed to a compound-statement), it is
6873    none-the-less treated as if it were enclosed in braces.  Any
6874    declarations appearing in the dependent statement are out of scope
6875    after control passes that point.  This function parses a statement,
6876    but ensures that is in its own scope, even if it is not a
6877    compound-statement.
6878
6879    Returns the new statement.  */
6880
6881 static tree
6882 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6883 {
6884   tree statement;
6885
6886   /* Mark if () ; with a special NOP_EXPR.  */
6887   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6888     {
6889       cp_lexer_consume_token (parser->lexer);
6890       statement = add_stmt (build_empty_stmt ());
6891     }
6892   /* if a compound is opened, we simply parse the statement directly.  */
6893   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6894     statement = cp_parser_compound_statement (parser, NULL, false);
6895   /* If the token is not a `{', then we must take special action.  */
6896   else
6897     {
6898       /* Create a compound-statement.  */
6899       statement = begin_compound_stmt (0);
6900       /* Parse the dependent-statement.  */
6901       cp_parser_statement (parser, NULL_TREE, false);
6902       /* Finish the dummy compound-statement.  */
6903       finish_compound_stmt (statement);
6904     }
6905
6906   /* Return the statement.  */
6907   return statement;
6908 }
6909
6910 /* For some dependent statements (like `while (cond) statement'), we
6911    have already created a scope.  Therefore, even if the dependent
6912    statement is a compound-statement, we do not want to create another
6913    scope.  */
6914
6915 static void
6916 cp_parser_already_scoped_statement (cp_parser* parser)
6917 {
6918   /* If the token is a `{', then we must take special action.  */
6919   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6920     cp_parser_statement (parser, NULL_TREE, false);
6921   else
6922     {
6923       /* Avoid calling cp_parser_compound_statement, so that we
6924          don't create a new scope.  Do everything else by hand.  */
6925       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6926       cp_parser_statement_seq_opt (parser, NULL_TREE);
6927       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6928     }
6929 }
6930
6931 /* Declarations [gram.dcl.dcl] */
6932
6933 /* Parse an optional declaration-sequence.
6934
6935    declaration-seq:
6936      declaration
6937      declaration-seq declaration  */
6938
6939 static void
6940 cp_parser_declaration_seq_opt (cp_parser* parser)
6941 {
6942   while (true)
6943     {
6944       cp_token *token;
6945
6946       token = cp_lexer_peek_token (parser->lexer);
6947
6948       if (token->type == CPP_CLOSE_BRACE
6949           || token->type == CPP_EOF
6950           || token->type == CPP_PRAGMA_EOL)
6951         break;
6952
6953       if (token->type == CPP_SEMICOLON)
6954         {
6955           /* A declaration consisting of a single semicolon is
6956              invalid.  Allow it unless we're being pedantic.  */
6957           cp_lexer_consume_token (parser->lexer);
6958           if (pedantic && !in_system_header)
6959             pedwarn ("extra %<;%>");
6960           continue;
6961         }
6962
6963       /* If we're entering or exiting a region that's implicitly
6964          extern "C", modify the lang context appropriately.  */
6965       if (!parser->implicit_extern_c && token->implicit_extern_c)
6966         {
6967           push_lang_context (lang_name_c);
6968           parser->implicit_extern_c = true;
6969         }
6970       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6971         {
6972           pop_lang_context ();
6973           parser->implicit_extern_c = false;
6974         }
6975
6976       if (token->type == CPP_PRAGMA)
6977         {
6978           /* A top-level declaration can consist solely of a #pragma.
6979              A nested declaration cannot, so this is done here and not
6980              in cp_parser_declaration.  (A #pragma at block scope is
6981              handled in cp_parser_statement.)  */
6982           cp_parser_pragma (parser, pragma_external);
6983           continue;
6984         }
6985
6986       /* Parse the declaration itself.  */
6987       cp_parser_declaration (parser);
6988     }
6989 }
6990
6991 /* Parse a declaration.
6992
6993    declaration:
6994      block-declaration
6995      function-definition
6996      template-declaration
6997      explicit-instantiation
6998      explicit-specialization
6999      linkage-specification
7000      namespace-definition
7001
7002    GNU extension:
7003
7004    declaration:
7005       __extension__ declaration */
7006
7007 static void
7008 cp_parser_declaration (cp_parser* parser)
7009 {
7010   cp_token token1;
7011   cp_token token2;
7012   int saved_pedantic;
7013   void *p;
7014
7015   /* Check for the `__extension__' keyword.  */
7016   if (cp_parser_extension_opt (parser, &saved_pedantic))
7017     {
7018       /* Parse the qualified declaration.  */
7019       cp_parser_declaration (parser);
7020       /* Restore the PEDANTIC flag.  */
7021       pedantic = saved_pedantic;
7022
7023       return;
7024     }
7025
7026   /* Try to figure out what kind of declaration is present.  */
7027   token1 = *cp_lexer_peek_token (parser->lexer);
7028
7029   if (token1.type != CPP_EOF)
7030     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7031   else
7032     {
7033       token2.type = CPP_EOF;
7034       token2.keyword = RID_MAX;
7035     }
7036
7037   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7038   p = obstack_alloc (&declarator_obstack, 0);
7039
7040   /* If the next token is `extern' and the following token is a string
7041      literal, then we have a linkage specification.  */
7042   if (token1.keyword == RID_EXTERN
7043       && cp_parser_is_string_literal (&token2))
7044     cp_parser_linkage_specification (parser);
7045   /* If the next token is `template', then we have either a template
7046      declaration, an explicit instantiation, or an explicit
7047      specialization.  */
7048   else if (token1.keyword == RID_TEMPLATE)
7049     {
7050       /* `template <>' indicates a template specialization.  */
7051       if (token2.type == CPP_LESS
7052           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7053         cp_parser_explicit_specialization (parser);
7054       /* `template <' indicates a template declaration.  */
7055       else if (token2.type == CPP_LESS)
7056         cp_parser_template_declaration (parser, /*member_p=*/false);
7057       /* Anything else must be an explicit instantiation.  */
7058       else
7059         cp_parser_explicit_instantiation (parser);
7060     }
7061   /* If the next token is `export', then we have a template
7062      declaration.  */
7063   else if (token1.keyword == RID_EXPORT)
7064     cp_parser_template_declaration (parser, /*member_p=*/false);
7065   /* If the next token is `extern', 'static' or 'inline' and the one
7066      after that is `template', we have a GNU extended explicit
7067      instantiation directive.  */
7068   else if (cp_parser_allow_gnu_extensions_p (parser)
7069            && (token1.keyword == RID_EXTERN
7070                || token1.keyword == RID_STATIC
7071                || token1.keyword == RID_INLINE)
7072            && token2.keyword == RID_TEMPLATE)
7073     cp_parser_explicit_instantiation (parser);
7074   /* If the next token is `namespace', check for a named or unnamed
7075      namespace definition.  */
7076   else if (token1.keyword == RID_NAMESPACE
7077            && (/* A named namespace definition.  */
7078                (token2.type == CPP_NAME
7079                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7080                     != CPP_EQ))
7081                /* An unnamed namespace definition.  */
7082                || token2.type == CPP_OPEN_BRACE
7083                || token2.keyword == RID_ATTRIBUTE))
7084     cp_parser_namespace_definition (parser);
7085   /* Objective-C++ declaration/definition.  */
7086   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7087     cp_parser_objc_declaration (parser);
7088   /* We must have either a block declaration or a function
7089      definition.  */
7090   else
7091     /* Try to parse a block-declaration, or a function-definition.  */
7092     cp_parser_block_declaration (parser, /*statement_p=*/false);
7093
7094   /* Free any declarators allocated.  */
7095   obstack_free (&declarator_obstack, p);
7096 }
7097
7098 /* Parse a block-declaration.
7099
7100    block-declaration:
7101      simple-declaration
7102      asm-definition
7103      namespace-alias-definition
7104      using-declaration
7105      using-directive
7106
7107    GNU Extension:
7108
7109    block-declaration:
7110      __extension__ block-declaration
7111      label-declaration
7112
7113    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7114    part of a declaration-statement.  */
7115
7116 static void
7117 cp_parser_block_declaration (cp_parser *parser,
7118                              bool      statement_p)
7119 {
7120   cp_token *token1;
7121   int saved_pedantic;
7122
7123   /* Check for the `__extension__' keyword.  */
7124   if (cp_parser_extension_opt (parser, &saved_pedantic))
7125     {
7126       /* Parse the qualified declaration.  */
7127       cp_parser_block_declaration (parser, statement_p);
7128       /* Restore the PEDANTIC flag.  */
7129       pedantic = saved_pedantic;
7130
7131       return;
7132     }
7133
7134   /* Peek at the next token to figure out which kind of declaration is
7135      present.  */
7136   token1 = cp_lexer_peek_token (parser->lexer);
7137
7138   /* If the next keyword is `asm', we have an asm-definition.  */
7139   if (token1->keyword == RID_ASM)
7140     {
7141       if (statement_p)
7142         cp_parser_commit_to_tentative_parse (parser);
7143       cp_parser_asm_definition (parser);
7144     }
7145   /* If the next keyword is `namespace', we have a
7146      namespace-alias-definition.  */
7147   else if (token1->keyword == RID_NAMESPACE)
7148     cp_parser_namespace_alias_definition (parser);
7149   /* If the next keyword is `using', we have either a
7150      using-declaration or a using-directive.  */
7151   else if (token1->keyword == RID_USING)
7152     {
7153       cp_token *token2;
7154
7155       if (statement_p)
7156         cp_parser_commit_to_tentative_parse (parser);
7157       /* If the token after `using' is `namespace', then we have a
7158          using-directive.  */
7159       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7160       if (token2->keyword == RID_NAMESPACE)
7161         cp_parser_using_directive (parser);
7162       /* Otherwise, it's a using-declaration.  */
7163       else
7164         cp_parser_using_declaration (parser);
7165     }
7166   /* If the next keyword is `__label__' we have a label declaration.  */
7167   else if (token1->keyword == RID_LABEL)
7168     {
7169       if (statement_p)
7170         cp_parser_commit_to_tentative_parse (parser);
7171       cp_parser_label_declaration (parser);
7172     }
7173   /* Anything else must be a simple-declaration.  */
7174   else
7175     cp_parser_simple_declaration (parser, !statement_p);
7176 }
7177
7178 /* Parse a simple-declaration.
7179
7180    simple-declaration:
7181      decl-specifier-seq [opt] init-declarator-list [opt] ;
7182
7183    init-declarator-list:
7184      init-declarator
7185      init-declarator-list , init-declarator
7186
7187    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7188    function-definition as a simple-declaration.  */
7189
7190 static void
7191 cp_parser_simple_declaration (cp_parser* parser,
7192                               bool function_definition_allowed_p)
7193 {
7194   cp_decl_specifier_seq decl_specifiers;
7195   int declares_class_or_enum;
7196   bool saw_declarator;
7197
7198   /* Defer access checks until we know what is being declared; the
7199      checks for names appearing in the decl-specifier-seq should be
7200      done as if we were in the scope of the thing being declared.  */
7201   push_deferring_access_checks (dk_deferred);
7202
7203   /* Parse the decl-specifier-seq.  We have to keep track of whether
7204      or not the decl-specifier-seq declares a named class or
7205      enumeration type, since that is the only case in which the
7206      init-declarator-list is allowed to be empty.
7207
7208      [dcl.dcl]
7209
7210      In a simple-declaration, the optional init-declarator-list can be
7211      omitted only when declaring a class or enumeration, that is when
7212      the decl-specifier-seq contains either a class-specifier, an
7213      elaborated-type-specifier, or an enum-specifier.  */
7214   cp_parser_decl_specifier_seq (parser,
7215                                 CP_PARSER_FLAGS_OPTIONAL,
7216                                 &decl_specifiers,
7217                                 &declares_class_or_enum);
7218   /* We no longer need to defer access checks.  */
7219   stop_deferring_access_checks ();
7220
7221   /* In a block scope, a valid declaration must always have a
7222      decl-specifier-seq.  By not trying to parse declarators, we can
7223      resolve the declaration/expression ambiguity more quickly.  */
7224   if (!function_definition_allowed_p
7225       && !decl_specifiers.any_specifiers_p)
7226     {
7227       cp_parser_error (parser, "expected declaration");
7228       goto done;
7229     }
7230
7231   /* If the next two tokens are both identifiers, the code is
7232      erroneous. The usual cause of this situation is code like:
7233
7234        T t;
7235
7236      where "T" should name a type -- but does not.  */
7237   if (!decl_specifiers.type
7238       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7239     {
7240       /* If parsing tentatively, we should commit; we really are
7241          looking at a declaration.  */
7242       cp_parser_commit_to_tentative_parse (parser);
7243       /* Give up.  */
7244       goto done;
7245     }
7246
7247   /* If we have seen at least one decl-specifier, and the next token
7248      is not a parenthesis, then we must be looking at a declaration.
7249      (After "int (" we might be looking at a functional cast.)  */
7250   if (decl_specifiers.any_specifiers_p
7251       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7252     cp_parser_commit_to_tentative_parse (parser);
7253
7254   /* Keep going until we hit the `;' at the end of the simple
7255      declaration.  */
7256   saw_declarator = false;
7257   while (cp_lexer_next_token_is_not (parser->lexer,
7258                                      CPP_SEMICOLON))
7259     {
7260       cp_token *token;
7261       bool function_definition_p;
7262       tree decl;
7263
7264       if (saw_declarator)
7265         {
7266           /* If we are processing next declarator, coma is expected */
7267           token = cp_lexer_peek_token (parser->lexer);
7268           gcc_assert (token->type == CPP_COMMA);
7269           cp_lexer_consume_token (parser->lexer);
7270         }
7271       else
7272         saw_declarator = true;
7273
7274       /* Parse the init-declarator.  */
7275       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7276                                         /*checks=*/NULL_TREE,
7277                                         function_definition_allowed_p,
7278                                         /*member_p=*/false,
7279                                         declares_class_or_enum,
7280                                         &function_definition_p);
7281       /* If an error occurred while parsing tentatively, exit quickly.
7282          (That usually happens when in the body of a function; each
7283          statement is treated as a declaration-statement until proven
7284          otherwise.)  */
7285       if (cp_parser_error_occurred (parser))
7286         goto done;
7287       /* Handle function definitions specially.  */
7288       if (function_definition_p)
7289         {
7290           /* If the next token is a `,', then we are probably
7291              processing something like:
7292
7293                void f() {}, *p;
7294
7295              which is erroneous.  */
7296           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7297             error ("mixing declarations and function-definitions is forbidden");
7298           /* Otherwise, we're done with the list of declarators.  */
7299           else
7300             {
7301               pop_deferring_access_checks ();
7302               return;
7303             }
7304         }
7305       /* The next token should be either a `,' or a `;'.  */
7306       token = cp_lexer_peek_token (parser->lexer);
7307       /* If it's a `,', there are more declarators to come.  */
7308       if (token->type == CPP_COMMA)
7309         /* will be consumed next time around */;
7310       /* If it's a `;', we are done.  */
7311       else if (token->type == CPP_SEMICOLON)
7312         break;
7313       /* Anything else is an error.  */
7314       else
7315         {
7316           /* If we have already issued an error message we don't need
7317              to issue another one.  */
7318           if (decl != error_mark_node
7319               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7320             cp_parser_error (parser, "expected %<,%> or %<;%>");
7321           /* Skip tokens until we reach the end of the statement.  */
7322           cp_parser_skip_to_end_of_statement (parser);
7323           /* If the next token is now a `;', consume it.  */
7324           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7325             cp_lexer_consume_token (parser->lexer);
7326           goto done;
7327         }
7328       /* After the first time around, a function-definition is not
7329          allowed -- even if it was OK at first.  For example:
7330
7331            int i, f() {}
7332
7333          is not valid.  */
7334       function_definition_allowed_p = false;
7335     }
7336
7337   /* Issue an error message if no declarators are present, and the
7338      decl-specifier-seq does not itself declare a class or
7339      enumeration.  */
7340   if (!saw_declarator)
7341     {
7342       if (cp_parser_declares_only_class_p (parser))
7343         shadow_tag (&decl_specifiers);
7344       /* Perform any deferred access checks.  */
7345       perform_deferred_access_checks ();
7346     }
7347
7348   /* Consume the `;'.  */
7349   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7350
7351  done:
7352   pop_deferring_access_checks ();
7353 }
7354
7355 /* Parse a decl-specifier-seq.
7356
7357    decl-specifier-seq:
7358      decl-specifier-seq [opt] decl-specifier
7359
7360    decl-specifier:
7361      storage-class-specifier
7362      type-specifier
7363      function-specifier
7364      friend
7365      typedef
7366
7367    GNU Extension:
7368
7369    decl-specifier:
7370      attributes
7371
7372    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7373
7374    The parser flags FLAGS is used to control type-specifier parsing.
7375
7376    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7377    flags:
7378
7379      1: one of the decl-specifiers is an elaborated-type-specifier
7380         (i.e., a type declaration)
7381      2: one of the decl-specifiers is an enum-specifier or a
7382         class-specifier (i.e., a type definition)
7383
7384    */
7385
7386 static void
7387 cp_parser_decl_specifier_seq (cp_parser* parser,
7388                               cp_parser_flags flags,
7389                               cp_decl_specifier_seq *decl_specs,
7390                               int* declares_class_or_enum)
7391 {
7392   bool constructor_possible_p = !parser->in_declarator_p;
7393   cp_decl_spec ds;
7394
7395   /* Clear DECL_SPECS.  */
7396   clear_decl_specs (decl_specs);
7397
7398   /* Assume no class or enumeration type is declared.  */
7399   *declares_class_or_enum = 0;
7400
7401   /* Keep reading specifiers until there are no more to read.  */
7402   while (true)
7403     {
7404       bool constructor_p;
7405       bool found_decl_spec;
7406       cp_token *token;
7407
7408       /* Peek at the next token.  */
7409       token = cp_lexer_peek_token (parser->lexer);
7410       /* Handle attributes.  */
7411       if (token->keyword == RID_ATTRIBUTE)
7412         {
7413           /* Parse the attributes.  */
7414           decl_specs->attributes
7415             = chainon (decl_specs->attributes,
7416                        cp_parser_attributes_opt (parser));
7417           continue;
7418         }
7419       /* Assume we will find a decl-specifier keyword.  */
7420       found_decl_spec = true;
7421       /* If the next token is an appropriate keyword, we can simply
7422          add it to the list.  */
7423       switch (token->keyword)
7424         {
7425           /* decl-specifier:
7426                friend  */
7427         case RID_FRIEND:
7428           ++decl_specs->specs[(int) ds_friend];
7429           /* Consume the token.  */
7430           cp_lexer_consume_token (parser->lexer);
7431           break;
7432
7433           /* function-specifier:
7434                inline
7435                virtual
7436                explicit  */
7437         case RID_INLINE:
7438         case RID_VIRTUAL:
7439         case RID_EXPLICIT:
7440           cp_parser_function_specifier_opt (parser, decl_specs);
7441           break;
7442
7443           /* decl-specifier:
7444                typedef  */
7445         case RID_TYPEDEF:
7446           ++decl_specs->specs[(int) ds_typedef];
7447           /* Consume the token.  */
7448           cp_lexer_consume_token (parser->lexer);
7449           /* A constructor declarator cannot appear in a typedef.  */
7450           constructor_possible_p = false;
7451           /* The "typedef" keyword can only occur in a declaration; we
7452              may as well commit at this point.  */
7453           cp_parser_commit_to_tentative_parse (parser);
7454           break;
7455
7456           /* storage-class-specifier:
7457                auto
7458                register
7459                static
7460                extern
7461                mutable
7462
7463              GNU Extension:
7464                thread  */
7465         case RID_AUTO:
7466         case RID_REGISTER:
7467         case RID_STATIC:
7468         case RID_EXTERN:
7469         case RID_MUTABLE:
7470           /* Consume the token.  */
7471           cp_lexer_consume_token (parser->lexer);
7472           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7473           break;
7474         case RID_THREAD:
7475           /* Consume the token.  */
7476           cp_lexer_consume_token (parser->lexer);
7477           ++decl_specs->specs[(int) ds_thread];
7478           break;
7479
7480         default:
7481           /* We did not yet find a decl-specifier yet.  */
7482           found_decl_spec = false;
7483           break;
7484         }
7485
7486       /* Constructors are a special case.  The `S' in `S()' is not a
7487          decl-specifier; it is the beginning of the declarator.  */
7488       constructor_p
7489         = (!found_decl_spec
7490            && constructor_possible_p
7491            && (cp_parser_constructor_declarator_p
7492                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7493
7494       /* If we don't have a DECL_SPEC yet, then we must be looking at
7495          a type-specifier.  */
7496       if (!found_decl_spec && !constructor_p)
7497         {
7498           int decl_spec_declares_class_or_enum;
7499           bool is_cv_qualifier;
7500           tree type_spec;
7501
7502           type_spec
7503             = cp_parser_type_specifier (parser, flags,
7504                                         decl_specs,
7505                                         /*is_declaration=*/true,
7506                                         &decl_spec_declares_class_or_enum,
7507                                         &is_cv_qualifier);
7508
7509           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7510
7511           /* If this type-specifier referenced a user-defined type
7512              (a typedef, class-name, etc.), then we can't allow any
7513              more such type-specifiers henceforth.
7514
7515              [dcl.spec]
7516
7517              The longest sequence of decl-specifiers that could
7518              possibly be a type name is taken as the
7519              decl-specifier-seq of a declaration.  The sequence shall
7520              be self-consistent as described below.
7521
7522              [dcl.type]
7523
7524              As a general rule, at most one type-specifier is allowed
7525              in the complete decl-specifier-seq of a declaration.  The
7526              only exceptions are the following:
7527
7528              -- const or volatile can be combined with any other
7529                 type-specifier.
7530
7531              -- signed or unsigned can be combined with char, long,
7532                 short, or int.
7533
7534              -- ..
7535
7536              Example:
7537
7538                typedef char* Pc;
7539                void g (const int Pc);
7540
7541              Here, Pc is *not* part of the decl-specifier seq; it's
7542              the declarator.  Therefore, once we see a type-specifier
7543              (other than a cv-qualifier), we forbid any additional
7544              user-defined types.  We *do* still allow things like `int
7545              int' to be considered a decl-specifier-seq, and issue the
7546              error message later.  */
7547           if (type_spec && !is_cv_qualifier)
7548             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7549           /* A constructor declarator cannot follow a type-specifier.  */
7550           if (type_spec)
7551             {
7552               constructor_possible_p = false;
7553               found_decl_spec = true;
7554             }
7555         }
7556
7557       /* If we still do not have a DECL_SPEC, then there are no more
7558          decl-specifiers.  */
7559       if (!found_decl_spec)
7560         break;
7561
7562       decl_specs->any_specifiers_p = true;
7563       /* After we see one decl-specifier, further decl-specifiers are
7564          always optional.  */
7565       flags |= CP_PARSER_FLAGS_OPTIONAL;
7566     }
7567
7568   /* Check for repeated decl-specifiers.  */
7569   for (ds = ds_first; ds != ds_last; ++ds)
7570     {
7571       unsigned count = decl_specs->specs[(int)ds];
7572       if (count < 2)
7573         continue;
7574       /* The "long" specifier is a special case because of "long long".  */
7575       if (ds == ds_long)
7576         {
7577           if (count > 2)
7578             error ("%<long long long%> is too long for GCC");
7579           else if (pedantic && !in_system_header && warn_long_long)
7580             pedwarn ("ISO C++ does not support %<long long%>");
7581         }
7582       else if (count > 1)
7583         {
7584           static const char *const decl_spec_names[] = {
7585             "signed",
7586             "unsigned",
7587             "short",
7588             "long",
7589             "const",
7590             "volatile",
7591             "restrict",
7592             "inline",
7593             "virtual",
7594             "explicit",
7595             "friend",
7596             "typedef",
7597             "__complex",
7598             "__thread"
7599           };
7600           error ("duplicate %qs", decl_spec_names[(int)ds]);
7601         }
7602     }
7603
7604   /* Don't allow a friend specifier with a class definition.  */
7605   if (decl_specs->specs[(int) ds_friend] != 0
7606       && (*declares_class_or_enum & 2))
7607     error ("class definition may not be declared a friend");
7608 }
7609
7610 /* Parse an (optional) storage-class-specifier.
7611
7612    storage-class-specifier:
7613      auto
7614      register
7615      static
7616      extern
7617      mutable
7618
7619    GNU Extension:
7620
7621    storage-class-specifier:
7622      thread
7623
7624    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7625
7626 static tree
7627 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7628 {
7629   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7630     {
7631     case RID_AUTO:
7632     case RID_REGISTER:
7633     case RID_STATIC:
7634     case RID_EXTERN:
7635     case RID_MUTABLE:
7636     case RID_THREAD:
7637       /* Consume the token.  */
7638       return cp_lexer_consume_token (parser->lexer)->value;
7639
7640     default:
7641       return NULL_TREE;
7642     }
7643 }
7644
7645 /* Parse an (optional) function-specifier.
7646
7647    function-specifier:
7648      inline
7649      virtual
7650      explicit
7651
7652    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7653    Updates DECL_SPECS, if it is non-NULL.  */
7654
7655 static tree
7656 cp_parser_function_specifier_opt (cp_parser* parser,
7657                                   cp_decl_specifier_seq *decl_specs)
7658 {
7659   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7660     {
7661     case RID_INLINE:
7662       if (decl_specs)
7663         ++decl_specs->specs[(int) ds_inline];
7664       break;
7665
7666     case RID_VIRTUAL:
7667       /* 14.5.2.3 [temp.mem]
7668          
7669          A member function template shall not be virtual.  */
7670       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7671         error ("templates may not be %<virtual%>");
7672       else if (decl_specs)
7673         ++decl_specs->specs[(int) ds_virtual];
7674       break;
7675
7676     case RID_EXPLICIT:
7677       if (decl_specs)
7678         ++decl_specs->specs[(int) ds_explicit];
7679       break;
7680
7681     default:
7682       return NULL_TREE;
7683     }
7684
7685   /* Consume the token.  */
7686   return cp_lexer_consume_token (parser->lexer)->value;
7687 }
7688
7689 /* Parse a linkage-specification.
7690
7691    linkage-specification:
7692      extern string-literal { declaration-seq [opt] }
7693      extern string-literal declaration  */
7694
7695 static void
7696 cp_parser_linkage_specification (cp_parser* parser)
7697 {
7698   tree linkage;
7699
7700   /* Look for the `extern' keyword.  */
7701   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7702
7703   /* Look for the string-literal.  */
7704   linkage = cp_parser_string_literal (parser, false, false);
7705
7706   /* Transform the literal into an identifier.  If the literal is a
7707      wide-character string, or contains embedded NULs, then we can't
7708      handle it as the user wants.  */
7709   if (strlen (TREE_STRING_POINTER (linkage))
7710       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7711     {
7712       cp_parser_error (parser, "invalid linkage-specification");
7713       /* Assume C++ linkage.  */
7714       linkage = lang_name_cplusplus;
7715     }
7716   else
7717     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7718
7719   /* We're now using the new linkage.  */
7720   push_lang_context (linkage);
7721
7722   /* If the next token is a `{', then we're using the first
7723      production.  */
7724   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7725     {
7726       /* Consume the `{' token.  */
7727       cp_lexer_consume_token (parser->lexer);
7728       /* Parse the declarations.  */
7729       cp_parser_declaration_seq_opt (parser);
7730       /* Look for the closing `}'.  */
7731       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7732     }
7733   /* Otherwise, there's just one declaration.  */
7734   else
7735     {
7736       bool saved_in_unbraced_linkage_specification_p;
7737
7738       saved_in_unbraced_linkage_specification_p
7739         = parser->in_unbraced_linkage_specification_p;
7740       parser->in_unbraced_linkage_specification_p = true;
7741       have_extern_spec = true;
7742       cp_parser_declaration (parser);
7743       have_extern_spec = false;
7744       parser->in_unbraced_linkage_specification_p
7745         = saved_in_unbraced_linkage_specification_p;
7746     }
7747
7748   /* We're done with the linkage-specification.  */
7749   pop_lang_context ();
7750 }
7751
7752 /* Special member functions [gram.special] */
7753
7754 /* Parse a conversion-function-id.
7755
7756    conversion-function-id:
7757      operator conversion-type-id
7758
7759    Returns an IDENTIFIER_NODE representing the operator.  */
7760
7761 static tree
7762 cp_parser_conversion_function_id (cp_parser* parser)
7763 {
7764   tree type;
7765   tree saved_scope;
7766   tree saved_qualifying_scope;
7767   tree saved_object_scope;
7768   tree pushed_scope = NULL_TREE;
7769
7770   /* Look for the `operator' token.  */
7771   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7772     return error_mark_node;
7773   /* When we parse the conversion-type-id, the current scope will be
7774      reset.  However, we need that information in able to look up the
7775      conversion function later, so we save it here.  */
7776   saved_scope = parser->scope;
7777   saved_qualifying_scope = parser->qualifying_scope;
7778   saved_object_scope = parser->object_scope;
7779   /* We must enter the scope of the class so that the names of
7780      entities declared within the class are available in the
7781      conversion-type-id.  For example, consider:
7782
7783        struct S {
7784          typedef int I;
7785          operator I();
7786        };
7787
7788        S::operator I() { ... }
7789
7790      In order to see that `I' is a type-name in the definition, we
7791      must be in the scope of `S'.  */
7792   if (saved_scope)
7793     pushed_scope = push_scope (saved_scope);
7794   /* Parse the conversion-type-id.  */
7795   type = cp_parser_conversion_type_id (parser);
7796   /* Leave the scope of the class, if any.  */
7797   if (pushed_scope)
7798     pop_scope (pushed_scope);
7799   /* Restore the saved scope.  */
7800   parser->scope = saved_scope;
7801   parser->qualifying_scope = saved_qualifying_scope;
7802   parser->object_scope = saved_object_scope;
7803   /* If the TYPE is invalid, indicate failure.  */
7804   if (type == error_mark_node)
7805     return error_mark_node;
7806   return mangle_conv_op_name_for_type (type);
7807 }
7808
7809 /* Parse a conversion-type-id:
7810
7811    conversion-type-id:
7812      type-specifier-seq conversion-declarator [opt]
7813
7814    Returns the TYPE specified.  */
7815
7816 static tree
7817 cp_parser_conversion_type_id (cp_parser* parser)
7818 {
7819   tree attributes;
7820   cp_decl_specifier_seq type_specifiers;
7821   cp_declarator *declarator;
7822   tree type_specified;
7823
7824   /* Parse the attributes.  */
7825   attributes = cp_parser_attributes_opt (parser);
7826   /* Parse the type-specifiers.  */
7827   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7828                                 &type_specifiers);
7829   /* If that didn't work, stop.  */
7830   if (type_specifiers.type == error_mark_node)
7831     return error_mark_node;
7832   /* Parse the conversion-declarator.  */
7833   declarator = cp_parser_conversion_declarator_opt (parser);
7834
7835   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7836                                     /*initialized=*/0, &attributes);
7837   if (attributes)
7838     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7839   return type_specified;
7840 }
7841
7842 /* Parse an (optional) conversion-declarator.
7843
7844    conversion-declarator:
7845      ptr-operator conversion-declarator [opt]
7846
7847    */
7848
7849 static cp_declarator *
7850 cp_parser_conversion_declarator_opt (cp_parser* parser)
7851 {
7852   enum tree_code code;
7853   tree class_type;
7854   cp_cv_quals cv_quals;
7855
7856   /* We don't know if there's a ptr-operator next, or not.  */
7857   cp_parser_parse_tentatively (parser);
7858   /* Try the ptr-operator.  */
7859   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7860   /* If it worked, look for more conversion-declarators.  */
7861   if (cp_parser_parse_definitely (parser))
7862     {
7863       cp_declarator *declarator;
7864
7865       /* Parse another optional declarator.  */
7866       declarator = cp_parser_conversion_declarator_opt (parser);
7867
7868       /* Create the representation of the declarator.  */
7869       if (class_type)
7870         declarator = make_ptrmem_declarator (cv_quals, class_type,
7871                                              declarator);
7872       else if (code == INDIRECT_REF)
7873         declarator = make_pointer_declarator (cv_quals, declarator);
7874       else
7875         declarator = make_reference_declarator (cv_quals, declarator);
7876
7877       return declarator;
7878    }
7879
7880   return NULL;
7881 }
7882
7883 /* Parse an (optional) ctor-initializer.
7884
7885    ctor-initializer:
7886      : mem-initializer-list
7887
7888    Returns TRUE iff the ctor-initializer was actually present.  */
7889
7890 static bool
7891 cp_parser_ctor_initializer_opt (cp_parser* parser)
7892 {
7893   /* If the next token is not a `:', then there is no
7894      ctor-initializer.  */
7895   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7896     {
7897       /* Do default initialization of any bases and members.  */
7898       if (DECL_CONSTRUCTOR_P (current_function_decl))
7899         finish_mem_initializers (NULL_TREE);
7900
7901       return false;
7902     }
7903
7904   /* Consume the `:' token.  */
7905   cp_lexer_consume_token (parser->lexer);
7906   /* And the mem-initializer-list.  */
7907   cp_parser_mem_initializer_list (parser);
7908
7909   return true;
7910 }
7911
7912 /* Parse a mem-initializer-list.
7913
7914    mem-initializer-list:
7915      mem-initializer
7916      mem-initializer , mem-initializer-list  */
7917
7918 static void
7919 cp_parser_mem_initializer_list (cp_parser* parser)
7920 {
7921   tree mem_initializer_list = NULL_TREE;
7922
7923   /* Let the semantic analysis code know that we are starting the
7924      mem-initializer-list.  */
7925   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7926     error ("only constructors take base initializers");
7927
7928   /* Loop through the list.  */
7929   while (true)
7930     {
7931       tree mem_initializer;
7932
7933       /* Parse the mem-initializer.  */
7934       mem_initializer = cp_parser_mem_initializer (parser);
7935       /* Add it to the list, unless it was erroneous.  */
7936       if (mem_initializer != error_mark_node)
7937         {
7938           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7939           mem_initializer_list = mem_initializer;
7940         }
7941       /* If the next token is not a `,', we're done.  */
7942       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7943         break;
7944       /* Consume the `,' token.  */
7945       cp_lexer_consume_token (parser->lexer);
7946     }
7947
7948   /* Perform semantic analysis.  */
7949   if (DECL_CONSTRUCTOR_P (current_function_decl))
7950     finish_mem_initializers (mem_initializer_list);
7951 }
7952
7953 /* Parse a mem-initializer.
7954
7955    mem-initializer:
7956      mem-initializer-id ( expression-list [opt] )
7957
7958    GNU extension:
7959
7960    mem-initializer:
7961      ( expression-list [opt] )
7962
7963    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7964    class) or FIELD_DECL (for a non-static data member) to initialize;
7965    the TREE_VALUE is the expression-list.  An empty initialization
7966    list is represented by void_list_node.  */
7967
7968 static tree
7969 cp_parser_mem_initializer (cp_parser* parser)
7970 {
7971   tree mem_initializer_id;
7972   tree expression_list;
7973   tree member;
7974
7975   /* Find out what is being initialized.  */
7976   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7977     {
7978       pedwarn ("anachronistic old-style base class initializer");
7979       mem_initializer_id = NULL_TREE;
7980     }
7981   else
7982     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7983   member = expand_member_init (mem_initializer_id);
7984   if (member && !DECL_P (member))
7985     in_base_initializer = 1;
7986
7987   expression_list
7988     = cp_parser_parenthesized_expression_list (parser, false,
7989                                                /*cast_p=*/false,
7990                                                /*non_constant_p=*/NULL);
7991   if (expression_list == error_mark_node)
7992     return error_mark_node;
7993   if (!expression_list)
7994     expression_list = void_type_node;
7995
7996   in_base_initializer = 0;
7997
7998   return member ? build_tree_list (member, expression_list) : error_mark_node;
7999 }
8000
8001 /* Parse a mem-initializer-id.
8002
8003    mem-initializer-id:
8004      :: [opt] nested-name-specifier [opt] class-name
8005      identifier
8006
8007    Returns a TYPE indicating the class to be initializer for the first
8008    production.  Returns an IDENTIFIER_NODE indicating the data member
8009    to be initialized for the second production.  */
8010
8011 static tree
8012 cp_parser_mem_initializer_id (cp_parser* parser)
8013 {
8014   bool global_scope_p;
8015   bool nested_name_specifier_p;
8016   bool template_p = false;
8017   tree id;
8018
8019   /* `typename' is not allowed in this context ([temp.res]).  */
8020   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8021     {
8022       error ("keyword %<typename%> not allowed in this context (a qualified "
8023              "member initializer is implicitly a type)");
8024       cp_lexer_consume_token (parser->lexer);
8025     }
8026   /* Look for the optional `::' operator.  */
8027   global_scope_p
8028     = (cp_parser_global_scope_opt (parser,
8029                                    /*current_scope_valid_p=*/false)
8030        != NULL_TREE);
8031   /* Look for the optional nested-name-specifier.  The simplest way to
8032      implement:
8033
8034        [temp.res]
8035
8036        The keyword `typename' is not permitted in a base-specifier or
8037        mem-initializer; in these contexts a qualified name that
8038        depends on a template-parameter is implicitly assumed to be a
8039        type name.
8040
8041      is to assume that we have seen the `typename' keyword at this
8042      point.  */
8043   nested_name_specifier_p
8044     = (cp_parser_nested_name_specifier_opt (parser,
8045                                             /*typename_keyword_p=*/true,
8046                                             /*check_dependency_p=*/true,
8047                                             /*type_p=*/true,
8048                                             /*is_declaration=*/true)
8049        != NULL_TREE);
8050   if (nested_name_specifier_p)
8051     template_p = cp_parser_optional_template_keyword (parser);
8052   /* If there is a `::' operator or a nested-name-specifier, then we
8053      are definitely looking for a class-name.  */
8054   if (global_scope_p || nested_name_specifier_p)
8055     return cp_parser_class_name (parser,
8056                                  /*typename_keyword_p=*/true,
8057                                  /*template_keyword_p=*/template_p,
8058                                  none_type,
8059                                  /*check_dependency_p=*/true,
8060                                  /*class_head_p=*/false,
8061                                  /*is_declaration=*/true);
8062   /* Otherwise, we could also be looking for an ordinary identifier.  */
8063   cp_parser_parse_tentatively (parser);
8064   /* Try a class-name.  */
8065   id = cp_parser_class_name (parser,
8066                              /*typename_keyword_p=*/true,
8067                              /*template_keyword_p=*/false,
8068                              none_type,
8069                              /*check_dependency_p=*/true,
8070                              /*class_head_p=*/false,
8071                              /*is_declaration=*/true);
8072   /* If we found one, we're done.  */
8073   if (cp_parser_parse_definitely (parser))
8074     return id;
8075   /* Otherwise, look for an ordinary identifier.  */
8076   return cp_parser_identifier (parser);
8077 }
8078
8079 /* Overloading [gram.over] */
8080
8081 /* Parse an operator-function-id.
8082
8083    operator-function-id:
8084      operator operator
8085
8086    Returns an IDENTIFIER_NODE for the operator which is a
8087    human-readable spelling of the identifier, e.g., `operator +'.  */
8088
8089 static tree
8090 cp_parser_operator_function_id (cp_parser* parser)
8091 {
8092   /* Look for the `operator' keyword.  */
8093   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8094     return error_mark_node;
8095   /* And then the name of the operator itself.  */
8096   return cp_parser_operator (parser);
8097 }
8098
8099 /* Parse an operator.
8100
8101    operator:
8102      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8103      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8104      || ++ -- , ->* -> () []
8105
8106    GNU Extensions:
8107
8108    operator:
8109      <? >? <?= >?=
8110
8111    Returns an IDENTIFIER_NODE for the operator which is a
8112    human-readable spelling of the identifier, e.g., `operator +'.  */
8113
8114 static tree
8115 cp_parser_operator (cp_parser* parser)
8116 {
8117   tree id = NULL_TREE;
8118   cp_token *token;
8119
8120   /* Peek at the next token.  */
8121   token = cp_lexer_peek_token (parser->lexer);
8122   /* Figure out which operator we have.  */
8123   switch (token->type)
8124     {
8125     case CPP_KEYWORD:
8126       {
8127         enum tree_code op;
8128
8129         /* The keyword should be either `new' or `delete'.  */
8130         if (token->keyword == RID_NEW)
8131           op = NEW_EXPR;
8132         else if (token->keyword == RID_DELETE)
8133           op = DELETE_EXPR;
8134         else
8135           break;
8136
8137         /* Consume the `new' or `delete' token.  */
8138         cp_lexer_consume_token (parser->lexer);
8139
8140         /* Peek at the next token.  */
8141         token = cp_lexer_peek_token (parser->lexer);
8142         /* If it's a `[' token then this is the array variant of the
8143            operator.  */
8144         if (token->type == CPP_OPEN_SQUARE)
8145           {
8146             /* Consume the `[' token.  */
8147             cp_lexer_consume_token (parser->lexer);
8148             /* Look for the `]' token.  */
8149             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8150             id = ansi_opname (op == NEW_EXPR
8151                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8152           }
8153         /* Otherwise, we have the non-array variant.  */
8154         else
8155           id = ansi_opname (op);
8156
8157         return id;
8158       }
8159
8160     case CPP_PLUS:
8161       id = ansi_opname (PLUS_EXPR);
8162       break;
8163
8164     case CPP_MINUS:
8165       id = ansi_opname (MINUS_EXPR);
8166       break;
8167
8168     case CPP_MULT:
8169       id = ansi_opname (MULT_EXPR);
8170       break;
8171
8172     case CPP_DIV:
8173       id = ansi_opname (TRUNC_DIV_EXPR);
8174       break;
8175
8176     case CPP_MOD:
8177       id = ansi_opname (TRUNC_MOD_EXPR);
8178       break;
8179
8180     case CPP_XOR:
8181       id = ansi_opname (BIT_XOR_EXPR);
8182       break;
8183
8184     case CPP_AND:
8185       id = ansi_opname (BIT_AND_EXPR);
8186       break;
8187
8188     case CPP_OR:
8189       id = ansi_opname (BIT_IOR_EXPR);
8190       break;
8191
8192     case CPP_COMPL:
8193       id = ansi_opname (BIT_NOT_EXPR);
8194       break;
8195
8196     case CPP_NOT:
8197       id = ansi_opname (TRUTH_NOT_EXPR);
8198       break;
8199
8200     case CPP_EQ:
8201       id = ansi_assopname (NOP_EXPR);
8202       break;
8203
8204     case CPP_LESS:
8205       id = ansi_opname (LT_EXPR);
8206       break;
8207
8208     case CPP_GREATER:
8209       id = ansi_opname (GT_EXPR);
8210       break;
8211
8212     case CPP_PLUS_EQ:
8213       id = ansi_assopname (PLUS_EXPR);
8214       break;
8215
8216     case CPP_MINUS_EQ:
8217       id = ansi_assopname (MINUS_EXPR);
8218       break;
8219
8220     case CPP_MULT_EQ:
8221       id = ansi_assopname (MULT_EXPR);
8222       break;
8223
8224     case CPP_DIV_EQ:
8225       id = ansi_assopname (TRUNC_DIV_EXPR);
8226       break;
8227
8228     case CPP_MOD_EQ:
8229       id = ansi_assopname (TRUNC_MOD_EXPR);
8230       break;
8231
8232     case CPP_XOR_EQ:
8233       id = ansi_assopname (BIT_XOR_EXPR);
8234       break;
8235
8236     case CPP_AND_EQ:
8237       id = ansi_assopname (BIT_AND_EXPR);
8238       break;
8239
8240     case CPP_OR_EQ:
8241       id = ansi_assopname (BIT_IOR_EXPR);
8242       break;
8243
8244     case CPP_LSHIFT:
8245       id = ansi_opname (LSHIFT_EXPR);
8246       break;
8247
8248     case CPP_RSHIFT:
8249       id = ansi_opname (RSHIFT_EXPR);
8250       break;
8251
8252     case CPP_LSHIFT_EQ:
8253       id = ansi_assopname (LSHIFT_EXPR);
8254       break;
8255
8256     case CPP_RSHIFT_EQ:
8257       id = ansi_assopname (RSHIFT_EXPR);
8258       break;
8259
8260     case CPP_EQ_EQ:
8261       id = ansi_opname (EQ_EXPR);
8262       break;
8263
8264     case CPP_NOT_EQ:
8265       id = ansi_opname (NE_EXPR);
8266       break;
8267
8268     case CPP_LESS_EQ:
8269       id = ansi_opname (LE_EXPR);
8270       break;
8271
8272     case CPP_GREATER_EQ:
8273       id = ansi_opname (GE_EXPR);
8274       break;
8275
8276     case CPP_AND_AND:
8277       id = ansi_opname (TRUTH_ANDIF_EXPR);
8278       break;
8279
8280     case CPP_OR_OR:
8281       id = ansi_opname (TRUTH_ORIF_EXPR);
8282       break;
8283
8284     case CPP_PLUS_PLUS:
8285       id = ansi_opname (POSTINCREMENT_EXPR);
8286       break;
8287
8288     case CPP_MINUS_MINUS:
8289       id = ansi_opname (PREDECREMENT_EXPR);
8290       break;
8291
8292     case CPP_COMMA:
8293       id = ansi_opname (COMPOUND_EXPR);
8294       break;
8295
8296     case CPP_DEREF_STAR:
8297       id = ansi_opname (MEMBER_REF);
8298       break;
8299
8300     case CPP_DEREF:
8301       id = ansi_opname (COMPONENT_REF);
8302       break;
8303
8304     case CPP_OPEN_PAREN:
8305       /* Consume the `('.  */
8306       cp_lexer_consume_token (parser->lexer);
8307       /* Look for the matching `)'.  */
8308       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8309       return ansi_opname (CALL_EXPR);
8310
8311     case CPP_OPEN_SQUARE:
8312       /* Consume the `['.  */
8313       cp_lexer_consume_token (parser->lexer);
8314       /* Look for the matching `]'.  */
8315       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8316       return ansi_opname (ARRAY_REF);
8317
8318       /* Extensions.  */
8319     case CPP_MIN:
8320       id = ansi_opname (MIN_EXPR);
8321       cp_parser_warn_min_max ();
8322       break;
8323
8324     case CPP_MAX:
8325       id = ansi_opname (MAX_EXPR);
8326       cp_parser_warn_min_max ();
8327       break;
8328
8329     case CPP_MIN_EQ:
8330       id = ansi_assopname (MIN_EXPR);
8331       cp_parser_warn_min_max ();
8332       break;
8333
8334     case CPP_MAX_EQ:
8335       id = ansi_assopname (MAX_EXPR);
8336       cp_parser_warn_min_max ();
8337       break;
8338
8339     default:
8340       /* Anything else is an error.  */
8341       break;
8342     }
8343
8344   /* If we have selected an identifier, we need to consume the
8345      operator token.  */
8346   if (id)
8347     cp_lexer_consume_token (parser->lexer);
8348   /* Otherwise, no valid operator name was present.  */
8349   else
8350     {
8351       cp_parser_error (parser, "expected operator");
8352       id = error_mark_node;
8353     }
8354
8355   return id;
8356 }
8357
8358 /* Parse a template-declaration.
8359
8360    template-declaration:
8361      export [opt] template < template-parameter-list > declaration
8362
8363    If MEMBER_P is TRUE, this template-declaration occurs within a
8364    class-specifier.
8365
8366    The grammar rule given by the standard isn't correct.  What
8367    is really meant is:
8368
8369    template-declaration:
8370      export [opt] template-parameter-list-seq
8371        decl-specifier-seq [opt] init-declarator [opt] ;
8372      export [opt] template-parameter-list-seq
8373        function-definition
8374
8375    template-parameter-list-seq:
8376      template-parameter-list-seq [opt]
8377      template < template-parameter-list >  */
8378
8379 static void
8380 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8381 {
8382   /* Check for `export'.  */
8383   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8384     {
8385       /* Consume the `export' token.  */
8386       cp_lexer_consume_token (parser->lexer);
8387       /* Warn that we do not support `export'.  */
8388       warning (0, "keyword %<export%> not implemented, and will be ignored");
8389     }
8390
8391   cp_parser_template_declaration_after_export (parser, member_p);
8392 }
8393
8394 /* Parse a template-parameter-list.
8395
8396    template-parameter-list:
8397      template-parameter
8398      template-parameter-list , template-parameter
8399
8400    Returns a TREE_LIST.  Each node represents a template parameter.
8401    The nodes are connected via their TREE_CHAINs.  */
8402
8403 static tree
8404 cp_parser_template_parameter_list (cp_parser* parser)
8405 {
8406   tree parameter_list = NULL_TREE;
8407
8408   begin_template_parm_list ();
8409   while (true)
8410     {
8411       tree parameter;
8412       cp_token *token;
8413       bool is_non_type;
8414
8415       /* Parse the template-parameter.  */
8416       parameter = cp_parser_template_parameter (parser, &is_non_type);
8417       /* Add it to the list.  */
8418       if (parameter != error_mark_node)
8419         parameter_list = process_template_parm (parameter_list,
8420                                                 parameter,
8421                                                 is_non_type);
8422       /* Peek at the next token.  */
8423       token = cp_lexer_peek_token (parser->lexer);
8424       /* If it's not a `,', we're done.  */
8425       if (token->type != CPP_COMMA)
8426         break;
8427       /* Otherwise, consume the `,' token.  */
8428       cp_lexer_consume_token (parser->lexer);
8429     }
8430
8431   return end_template_parm_list (parameter_list);
8432 }
8433
8434 /* Parse a template-parameter.
8435
8436    template-parameter:
8437      type-parameter
8438      parameter-declaration
8439
8440    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8441    the parameter.  The TREE_PURPOSE is the default value, if any.
8442    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8443    iff this parameter is a non-type parameter.  */
8444
8445 static tree
8446 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8447 {
8448   cp_token *token;
8449   cp_parameter_declarator *parameter_declarator;
8450   tree parm;
8451
8452   /* Assume it is a type parameter or a template parameter.  */
8453   *is_non_type = false;
8454   /* Peek at the next token.  */
8455   token = cp_lexer_peek_token (parser->lexer);
8456   /* If it is `class' or `template', we have a type-parameter.  */
8457   if (token->keyword == RID_TEMPLATE)
8458     return cp_parser_type_parameter (parser);
8459   /* If it is `class' or `typename' we do not know yet whether it is a
8460      type parameter or a non-type parameter.  Consider:
8461
8462        template <typename T, typename T::X X> ...
8463
8464      or:
8465
8466        template <class C, class D*> ...
8467
8468      Here, the first parameter is a type parameter, and the second is
8469      a non-type parameter.  We can tell by looking at the token after
8470      the identifier -- if it is a `,', `=', or `>' then we have a type
8471      parameter.  */
8472   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8473     {
8474       /* Peek at the token after `class' or `typename'.  */
8475       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8476       /* If it's an identifier, skip it.  */
8477       if (token->type == CPP_NAME)
8478         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8479       /* Now, see if the token looks like the end of a template
8480          parameter.  */
8481       if (token->type == CPP_COMMA
8482           || token->type == CPP_EQ
8483           || token->type == CPP_GREATER)
8484         return cp_parser_type_parameter (parser);
8485     }
8486
8487   /* Otherwise, it is a non-type parameter.
8488
8489      [temp.param]
8490
8491      When parsing a default template-argument for a non-type
8492      template-parameter, the first non-nested `>' is taken as the end
8493      of the template parameter-list rather than a greater-than
8494      operator.  */
8495   *is_non_type = true;
8496   parameter_declarator
8497      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8498                                         /*parenthesized_p=*/NULL);
8499   parm = grokdeclarator (parameter_declarator->declarator,
8500                          &parameter_declarator->decl_specifiers,
8501                          PARM, /*initialized=*/0,
8502                          /*attrlist=*/NULL);
8503   if (parm == error_mark_node)
8504     return error_mark_node;
8505   return build_tree_list (parameter_declarator->default_argument, parm);
8506 }
8507
8508 /* Parse a type-parameter.
8509
8510    type-parameter:
8511      class identifier [opt]
8512      class identifier [opt] = type-id
8513      typename identifier [opt]
8514      typename identifier [opt] = type-id
8515      template < template-parameter-list > class identifier [opt]
8516      template < template-parameter-list > class identifier [opt]
8517        = id-expression
8518
8519    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8520    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8521    the declaration of the parameter.  */
8522
8523 static tree
8524 cp_parser_type_parameter (cp_parser* parser)
8525 {
8526   cp_token *token;
8527   tree parameter;
8528
8529   /* Look for a keyword to tell us what kind of parameter this is.  */
8530   token = cp_parser_require (parser, CPP_KEYWORD,
8531                              "`class', `typename', or `template'");
8532   if (!token)
8533     return error_mark_node;
8534
8535   switch (token->keyword)
8536     {
8537     case RID_CLASS:
8538     case RID_TYPENAME:
8539       {
8540         tree identifier;
8541         tree default_argument;
8542
8543         /* If the next token is an identifier, then it names the
8544            parameter.  */
8545         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8546           identifier = cp_parser_identifier (parser);
8547         else
8548           identifier = NULL_TREE;
8549
8550         /* Create the parameter.  */
8551         parameter = finish_template_type_parm (class_type_node, identifier);
8552
8553         /* If the next token is an `=', we have a default argument.  */
8554         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8555           {
8556             /* Consume the `=' token.  */
8557             cp_lexer_consume_token (parser->lexer);
8558             /* Parse the default-argument.  */
8559             push_deferring_access_checks (dk_no_deferred);
8560             default_argument = cp_parser_type_id (parser);
8561             pop_deferring_access_checks ();
8562           }
8563         else
8564           default_argument = NULL_TREE;
8565
8566         /* Create the combined representation of the parameter and the
8567            default argument.  */
8568         parameter = build_tree_list (default_argument, parameter);
8569       }
8570       break;
8571
8572     case RID_TEMPLATE:
8573       {
8574         tree parameter_list;
8575         tree identifier;
8576         tree default_argument;
8577
8578         /* Look for the `<'.  */
8579         cp_parser_require (parser, CPP_LESS, "`<'");
8580         /* Parse the template-parameter-list.  */
8581         parameter_list = cp_parser_template_parameter_list (parser);
8582         /* Look for the `>'.  */
8583         cp_parser_require (parser, CPP_GREATER, "`>'");
8584         /* Look for the `class' keyword.  */
8585         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8586         /* If the next token is an `=', then there is a
8587            default-argument.  If the next token is a `>', we are at
8588            the end of the parameter-list.  If the next token is a `,',
8589            then we are at the end of this parameter.  */
8590         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8591             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8592             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8593           {
8594             identifier = cp_parser_identifier (parser);
8595             /* Treat invalid names as if the parameter were nameless.  */
8596             if (identifier == error_mark_node)
8597               identifier = NULL_TREE;
8598           }
8599         else
8600           identifier = NULL_TREE;
8601
8602         /* Create the template parameter.  */
8603         parameter = finish_template_template_parm (class_type_node,
8604                                                    identifier);
8605
8606         /* If the next token is an `=', then there is a
8607            default-argument.  */
8608         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8609           {
8610             bool is_template;
8611
8612             /* Consume the `='.  */
8613             cp_lexer_consume_token (parser->lexer);
8614             /* Parse the id-expression.  */
8615             push_deferring_access_checks (dk_no_deferred);
8616             default_argument
8617               = cp_parser_id_expression (parser,
8618                                          /*template_keyword_p=*/false,
8619                                          /*check_dependency_p=*/true,
8620                                          /*template_p=*/&is_template,
8621                                          /*declarator_p=*/false,
8622                                          /*optional_p=*/false);
8623             if (TREE_CODE (default_argument) == TYPE_DECL)
8624               /* If the id-expression was a template-id that refers to
8625                  a template-class, we already have the declaration here,
8626                  so no further lookup is needed.  */
8627                  ;
8628             else
8629               /* Look up the name.  */
8630               default_argument
8631                 = cp_parser_lookup_name (parser, default_argument,
8632                                          none_type,
8633                                          /*is_template=*/is_template,
8634                                          /*is_namespace=*/false,
8635                                          /*check_dependency=*/true,
8636                                          /*ambiguous_decls=*/NULL);
8637             /* See if the default argument is valid.  */
8638             default_argument
8639               = check_template_template_default_arg (default_argument);
8640             pop_deferring_access_checks ();
8641           }
8642         else
8643           default_argument = NULL_TREE;
8644
8645         /* Create the combined representation of the parameter and the
8646            default argument.  */
8647         parameter = build_tree_list (default_argument, parameter);
8648       }
8649       break;
8650
8651     default:
8652       gcc_unreachable ();
8653       break;
8654     }
8655
8656   return parameter;
8657 }
8658
8659 /* Parse a template-id.
8660
8661    template-id:
8662      template-name < template-argument-list [opt] >
8663
8664    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8665    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8666    returned.  Otherwise, if the template-name names a function, or set
8667    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8668    names a class, returns a TYPE_DECL for the specialization.
8669
8670    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8671    uninstantiated templates.  */
8672
8673 static tree
8674 cp_parser_template_id (cp_parser *parser,
8675                        bool template_keyword_p,
8676                        bool check_dependency_p,
8677                        bool is_declaration)
8678 {
8679   tree template;
8680   tree arguments;
8681   tree template_id;
8682   cp_token_position start_of_id = 0;
8683   tree access_check = NULL_TREE;
8684   cp_token *next_token, *next_token_2;
8685   bool is_identifier;
8686
8687   /* If the next token corresponds to a template-id, there is no need
8688      to reparse it.  */
8689   next_token = cp_lexer_peek_token (parser->lexer);
8690   if (next_token->type == CPP_TEMPLATE_ID)
8691     {
8692       tree value;
8693       tree check;
8694
8695       /* Get the stored value.  */
8696       value = cp_lexer_consume_token (parser->lexer)->value;
8697       /* Perform any access checks that were deferred.  */
8698       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8699         perform_or_defer_access_check (TREE_PURPOSE (check),
8700                                        TREE_VALUE (check));
8701       /* Return the stored value.  */
8702       return TREE_VALUE (value);
8703     }
8704
8705   /* Avoid performing name lookup if there is no possibility of
8706      finding a template-id.  */
8707   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8708       || (next_token->type == CPP_NAME
8709           && !cp_parser_nth_token_starts_template_argument_list_p
8710                (parser, 2)))
8711     {
8712       cp_parser_error (parser, "expected template-id");
8713       return error_mark_node;
8714     }
8715
8716   /* Remember where the template-id starts.  */
8717   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8718     start_of_id = cp_lexer_token_position (parser->lexer, false);
8719
8720   push_deferring_access_checks (dk_deferred);
8721
8722   /* Parse the template-name.  */
8723   is_identifier = false;
8724   template = cp_parser_template_name (parser, template_keyword_p,
8725                                       check_dependency_p,
8726                                       is_declaration,
8727                                       &is_identifier);
8728   if (template == error_mark_node || is_identifier)
8729     {
8730       pop_deferring_access_checks ();
8731       return template;
8732     }
8733
8734   /* If we find the sequence `[:' after a template-name, it's probably
8735      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8736      parse correctly the argument list.  */
8737   next_token = cp_lexer_peek_token (parser->lexer);
8738   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8739   if (next_token->type == CPP_OPEN_SQUARE
8740       && next_token->flags & DIGRAPH
8741       && next_token_2->type == CPP_COLON
8742       && !(next_token_2->flags & PREV_WHITE))
8743     {
8744       cp_parser_parse_tentatively (parser);
8745       /* Change `:' into `::'.  */
8746       next_token_2->type = CPP_SCOPE;
8747       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8748          CPP_LESS.  */
8749       cp_lexer_consume_token (parser->lexer);
8750       /* Parse the arguments.  */
8751       arguments = cp_parser_enclosed_template_argument_list (parser);
8752       if (!cp_parser_parse_definitely (parser))
8753         {
8754           /* If we couldn't parse an argument list, then we revert our changes
8755              and return simply an error. Maybe this is not a template-id
8756              after all.  */
8757           next_token_2->type = CPP_COLON;
8758           cp_parser_error (parser, "expected %<<%>");
8759           pop_deferring_access_checks ();
8760           return error_mark_node;
8761         }
8762       /* Otherwise, emit an error about the invalid digraph, but continue
8763          parsing because we got our argument list.  */
8764       pedwarn ("%<<::%> cannot begin a template-argument list");
8765       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8766               "between %<<%> and %<::%>");
8767       if (!flag_permissive)
8768         {
8769           static bool hint;
8770           if (!hint)
8771             {
8772               inform ("(if you use -fpermissive G++ will accept your code)");
8773               hint = true;
8774             }
8775         }
8776     }
8777   else
8778     {
8779       /* Look for the `<' that starts the template-argument-list.  */
8780       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8781         {
8782           pop_deferring_access_checks ();
8783           return error_mark_node;
8784         }
8785       /* Parse the arguments.  */
8786       arguments = cp_parser_enclosed_template_argument_list (parser);
8787     }
8788
8789   /* Build a representation of the specialization.  */
8790   if (TREE_CODE (template) == IDENTIFIER_NODE)
8791     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8792   else if (DECL_CLASS_TEMPLATE_P (template)
8793            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8794     template_id
8795       = finish_template_type (template, arguments,
8796                               cp_lexer_next_token_is (parser->lexer,
8797                                                       CPP_SCOPE));
8798   else
8799     {
8800       /* If it's not a class-template or a template-template, it should be
8801          a function-template.  */
8802       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8803                    || TREE_CODE (template) == OVERLOAD
8804                    || BASELINK_P (template)));
8805
8806       template_id = lookup_template_function (template, arguments);
8807     }
8808
8809   /* Retrieve any deferred checks.  Do not pop this access checks yet
8810      so the memory will not be reclaimed during token replacing below.  */
8811   access_check = get_deferred_access_checks ();
8812
8813   /* If parsing tentatively, replace the sequence of tokens that makes
8814      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8815      should we re-parse the token stream, we will not have to repeat
8816      the effort required to do the parse, nor will we issue duplicate
8817      error messages about problems during instantiation of the
8818      template.  */
8819   if (start_of_id)
8820     {
8821       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8822
8823       /* Reset the contents of the START_OF_ID token.  */
8824       token->type = CPP_TEMPLATE_ID;
8825       token->value = build_tree_list (access_check, template_id);
8826       token->keyword = RID_MAX;
8827
8828       /* Purge all subsequent tokens.  */
8829       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8830
8831       /* ??? Can we actually assume that, if template_id ==
8832          error_mark_node, we will have issued a diagnostic to the
8833          user, as opposed to simply marking the tentative parse as
8834          failed?  */
8835       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8836         error ("parse error in template argument list");
8837     }
8838
8839   pop_deferring_access_checks ();
8840   return template_id;
8841 }
8842
8843 /* Parse a template-name.
8844
8845    template-name:
8846      identifier
8847
8848    The standard should actually say:
8849
8850    template-name:
8851      identifier
8852      operator-function-id
8853
8854    A defect report has been filed about this issue.
8855
8856    A conversion-function-id cannot be a template name because they cannot
8857    be part of a template-id. In fact, looking at this code:
8858
8859    a.operator K<int>()
8860
8861    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8862    It is impossible to call a templated conversion-function-id with an
8863    explicit argument list, since the only allowed template parameter is
8864    the type to which it is converting.
8865
8866    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8867    `template' keyword, in a construction like:
8868
8869      T::template f<3>()
8870
8871    In that case `f' is taken to be a template-name, even though there
8872    is no way of knowing for sure.
8873
8874    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8875    name refers to a set of overloaded functions, at least one of which
8876    is a template, or an IDENTIFIER_NODE with the name of the template,
8877    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8878    names are looked up inside uninstantiated templates.  */
8879
8880 static tree
8881 cp_parser_template_name (cp_parser* parser,
8882                          bool template_keyword_p,
8883                          bool check_dependency_p,
8884                          bool is_declaration,
8885                          bool *is_identifier)
8886 {
8887   tree identifier;
8888   tree decl;
8889   tree fns;
8890
8891   /* If the next token is `operator', then we have either an
8892      operator-function-id or a conversion-function-id.  */
8893   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8894     {
8895       /* We don't know whether we're looking at an
8896          operator-function-id or a conversion-function-id.  */
8897       cp_parser_parse_tentatively (parser);
8898       /* Try an operator-function-id.  */
8899       identifier = cp_parser_operator_function_id (parser);
8900       /* If that didn't work, try a conversion-function-id.  */
8901       if (!cp_parser_parse_definitely (parser))
8902         {
8903           cp_parser_error (parser, "expected template-name");
8904           return error_mark_node;
8905         }
8906     }
8907   /* Look for the identifier.  */
8908   else
8909     identifier = cp_parser_identifier (parser);
8910
8911   /* If we didn't find an identifier, we don't have a template-id.  */
8912   if (identifier == error_mark_node)
8913     return error_mark_node;
8914
8915   /* If the name immediately followed the `template' keyword, then it
8916      is a template-name.  However, if the next token is not `<', then
8917      we do not treat it as a template-name, since it is not being used
8918      as part of a template-id.  This enables us to handle constructs
8919      like:
8920
8921        template <typename T> struct S { S(); };
8922        template <typename T> S<T>::S();
8923
8924      correctly.  We would treat `S' as a template -- if it were `S<T>'
8925      -- but we do not if there is no `<'.  */
8926
8927   if (processing_template_decl
8928       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8929     {
8930       /* In a declaration, in a dependent context, we pretend that the
8931          "template" keyword was present in order to improve error
8932          recovery.  For example, given:
8933
8934            template <typename T> void f(T::X<int>);
8935
8936          we want to treat "X<int>" as a template-id.  */
8937       if (is_declaration
8938           && !template_keyword_p
8939           && parser->scope && TYPE_P (parser->scope)
8940           && check_dependency_p
8941           && dependent_type_p (parser->scope)
8942           /* Do not do this for dtors (or ctors), since they never
8943              need the template keyword before their name.  */
8944           && !constructor_name_p (identifier, parser->scope))
8945         {
8946           cp_token_position start = 0;
8947
8948           /* Explain what went wrong.  */
8949           error ("non-template %qD used as template", identifier);
8950           inform ("use %<%T::template %D%> to indicate that it is a template",
8951                   parser->scope, identifier);
8952           /* If parsing tentatively, find the location of the "<" token.  */
8953           if (cp_parser_simulate_error (parser))
8954             start = cp_lexer_token_position (parser->lexer, true);
8955           /* Parse the template arguments so that we can issue error
8956              messages about them.  */
8957           cp_lexer_consume_token (parser->lexer);
8958           cp_parser_enclosed_template_argument_list (parser);
8959           /* Skip tokens until we find a good place from which to
8960              continue parsing.  */
8961           cp_parser_skip_to_closing_parenthesis (parser,
8962                                                  /*recovering=*/true,
8963                                                  /*or_comma=*/true,
8964                                                  /*consume_paren=*/false);
8965           /* If parsing tentatively, permanently remove the
8966              template argument list.  That will prevent duplicate
8967              error messages from being issued about the missing
8968              "template" keyword.  */
8969           if (start)
8970             cp_lexer_purge_tokens_after (parser->lexer, start);
8971           if (is_identifier)
8972             *is_identifier = true;
8973           return identifier;
8974         }
8975
8976       /* If the "template" keyword is present, then there is generally
8977          no point in doing name-lookup, so we just return IDENTIFIER.
8978          But, if the qualifying scope is non-dependent then we can
8979          (and must) do name-lookup normally.  */
8980       if (template_keyword_p
8981           && (!parser->scope
8982               || (TYPE_P (parser->scope)
8983                   && dependent_type_p (parser->scope))))
8984         return identifier;
8985     }
8986
8987   /* Look up the name.  */
8988   decl = cp_parser_lookup_name (parser, identifier,
8989                                 none_type,
8990                                 /*is_template=*/false,
8991                                 /*is_namespace=*/false,
8992                                 check_dependency_p,
8993                                 /*ambiguous_decls=*/NULL);
8994   decl = maybe_get_template_decl_from_type_decl (decl);
8995
8996   /* If DECL is a template, then the name was a template-name.  */
8997   if (TREE_CODE (decl) == TEMPLATE_DECL)
8998     ;
8999   else
9000     {
9001       tree fn = NULL_TREE;
9002
9003       /* The standard does not explicitly indicate whether a name that
9004          names a set of overloaded declarations, some of which are
9005          templates, is a template-name.  However, such a name should
9006          be a template-name; otherwise, there is no way to form a
9007          template-id for the overloaded templates.  */
9008       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9009       if (TREE_CODE (fns) == OVERLOAD)
9010         for (fn = fns; fn; fn = OVL_NEXT (fn))
9011           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9012             break;
9013
9014       if (!fn)
9015         {
9016           /* The name does not name a template.  */
9017           cp_parser_error (parser, "expected template-name");
9018           return error_mark_node;
9019         }
9020     }
9021
9022   /* If DECL is dependent, and refers to a function, then just return
9023      its name; we will look it up again during template instantiation.  */
9024   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9025     {
9026       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9027       if (TYPE_P (scope) && dependent_type_p (scope))
9028         return identifier;
9029     }
9030
9031   return decl;
9032 }
9033
9034 /* Parse a template-argument-list.
9035
9036    template-argument-list:
9037      template-argument
9038      template-argument-list , template-argument
9039
9040    Returns a TREE_VEC containing the arguments.  */
9041
9042 static tree
9043 cp_parser_template_argument_list (cp_parser* parser)
9044 {
9045   tree fixed_args[10];
9046   unsigned n_args = 0;
9047   unsigned alloced = 10;
9048   tree *arg_ary = fixed_args;
9049   tree vec;
9050   bool saved_in_template_argument_list_p;
9051   bool saved_ice_p;
9052   bool saved_non_ice_p;
9053
9054   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9055   parser->in_template_argument_list_p = true;
9056   /* Even if the template-id appears in an integral
9057      constant-expression, the contents of the argument list do 
9058      not.  */ 
9059   saved_ice_p = parser->integral_constant_expression_p;
9060   parser->integral_constant_expression_p = false;
9061   saved_non_ice_p = parser->non_integral_constant_expression_p;
9062   parser->non_integral_constant_expression_p = false;
9063   /* Parse the arguments.  */
9064   do
9065     {
9066       tree argument;
9067
9068       if (n_args)
9069         /* Consume the comma.  */
9070         cp_lexer_consume_token (parser->lexer);
9071
9072       /* Parse the template-argument.  */
9073       argument = cp_parser_template_argument (parser);
9074       if (n_args == alloced)
9075         {
9076           alloced *= 2;
9077
9078           if (arg_ary == fixed_args)
9079             {
9080               arg_ary = XNEWVEC (tree, alloced);
9081               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9082             }
9083           else
9084             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9085         }
9086       arg_ary[n_args++] = argument;
9087     }
9088   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9089
9090   vec = make_tree_vec (n_args);
9091
9092   while (n_args--)
9093     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9094
9095   if (arg_ary != fixed_args)
9096     free (arg_ary);
9097   parser->non_integral_constant_expression_p = saved_non_ice_p;
9098   parser->integral_constant_expression_p = saved_ice_p;
9099   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9100   return vec;
9101 }
9102
9103 /* Parse a template-argument.
9104
9105    template-argument:
9106      assignment-expression
9107      type-id
9108      id-expression
9109
9110    The representation is that of an assignment-expression, type-id, or
9111    id-expression -- except that the qualified id-expression is
9112    evaluated, so that the value returned is either a DECL or an
9113    OVERLOAD.
9114
9115    Although the standard says "assignment-expression", it forbids
9116    throw-expressions or assignments in the template argument.
9117    Therefore, we use "conditional-expression" instead.  */
9118
9119 static tree
9120 cp_parser_template_argument (cp_parser* parser)
9121 {
9122   tree argument;
9123   bool template_p;
9124   bool address_p;
9125   bool maybe_type_id = false;
9126   cp_token *token;
9127   cp_id_kind idk;
9128
9129   /* There's really no way to know what we're looking at, so we just
9130      try each alternative in order.
9131
9132        [temp.arg]
9133
9134        In a template-argument, an ambiguity between a type-id and an
9135        expression is resolved to a type-id, regardless of the form of
9136        the corresponding template-parameter.
9137
9138      Therefore, we try a type-id first.  */
9139   cp_parser_parse_tentatively (parser);
9140   argument = cp_parser_type_id (parser);
9141   /* If there was no error parsing the type-id but the next token is a '>>',
9142      we probably found a typo for '> >'. But there are type-id which are
9143      also valid expressions. For instance:
9144
9145      struct X { int operator >> (int); };
9146      template <int V> struct Foo {};
9147      Foo<X () >> 5> r;
9148
9149      Here 'X()' is a valid type-id of a function type, but the user just
9150      wanted to write the expression "X() >> 5". Thus, we remember that we
9151      found a valid type-id, but we still try to parse the argument as an
9152      expression to see what happens.  */
9153   if (!cp_parser_error_occurred (parser)
9154       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9155     {
9156       maybe_type_id = true;
9157       cp_parser_abort_tentative_parse (parser);
9158     }
9159   else
9160     {
9161       /* If the next token isn't a `,' or a `>', then this argument wasn't
9162       really finished. This means that the argument is not a valid
9163       type-id.  */
9164       if (!cp_parser_next_token_ends_template_argument_p (parser))
9165         cp_parser_error (parser, "expected template-argument");
9166       /* If that worked, we're done.  */
9167       if (cp_parser_parse_definitely (parser))
9168         return argument;
9169     }
9170   /* We're still not sure what the argument will be.  */
9171   cp_parser_parse_tentatively (parser);
9172   /* Try a template.  */
9173   argument = cp_parser_id_expression (parser,
9174                                       /*template_keyword_p=*/false,
9175                                       /*check_dependency_p=*/true,
9176                                       &template_p,
9177                                       /*declarator_p=*/false,
9178                                       /*optional_p=*/false);
9179   /* If the next token isn't a `,' or a `>', then this argument wasn't
9180      really finished.  */
9181   if (!cp_parser_next_token_ends_template_argument_p (parser))
9182     cp_parser_error (parser, "expected template-argument");
9183   if (!cp_parser_error_occurred (parser))
9184     {
9185       /* Figure out what is being referred to.  If the id-expression
9186          was for a class template specialization, then we will have a
9187          TYPE_DECL at this point.  There is no need to do name lookup
9188          at this point in that case.  */
9189       if (TREE_CODE (argument) != TYPE_DECL)
9190         argument = cp_parser_lookup_name (parser, argument,
9191                                           none_type,
9192                                           /*is_template=*/template_p,
9193                                           /*is_namespace=*/false,
9194                                           /*check_dependency=*/true,
9195                                           /*ambiguous_decls=*/NULL);
9196       if (TREE_CODE (argument) != TEMPLATE_DECL
9197           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9198         cp_parser_error (parser, "expected template-name");
9199     }
9200   if (cp_parser_parse_definitely (parser))
9201     return argument;
9202   /* It must be a non-type argument.  There permitted cases are given
9203      in [temp.arg.nontype]:
9204
9205      -- an integral constant-expression of integral or enumeration
9206         type; or
9207
9208      -- the name of a non-type template-parameter; or
9209
9210      -- the name of an object or function with external linkage...
9211
9212      -- the address of an object or function with external linkage...
9213
9214      -- a pointer to member...  */
9215   /* Look for a non-type template parameter.  */
9216   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9217     {
9218       cp_parser_parse_tentatively (parser);
9219       argument = cp_parser_primary_expression (parser,
9220                                                /*adress_p=*/false,
9221                                                /*cast_p=*/false,
9222                                                /*template_arg_p=*/true,
9223                                                &idk);
9224       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9225           || !cp_parser_next_token_ends_template_argument_p (parser))
9226         cp_parser_simulate_error (parser);
9227       if (cp_parser_parse_definitely (parser))
9228         return argument;
9229     }
9230
9231   /* If the next token is "&", the argument must be the address of an
9232      object or function with external linkage.  */
9233   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9234   if (address_p)
9235     cp_lexer_consume_token (parser->lexer);
9236   /* See if we might have an id-expression.  */
9237   token = cp_lexer_peek_token (parser->lexer);
9238   if (token->type == CPP_NAME
9239       || token->keyword == RID_OPERATOR
9240       || token->type == CPP_SCOPE
9241       || token->type == CPP_TEMPLATE_ID
9242       || token->type == CPP_NESTED_NAME_SPECIFIER)
9243     {
9244       cp_parser_parse_tentatively (parser);
9245       argument = cp_parser_primary_expression (parser,
9246                                                address_p,
9247                                                /*cast_p=*/false,
9248                                                /*template_arg_p=*/true,
9249                                                &idk);
9250       if (cp_parser_error_occurred (parser)
9251           || !cp_parser_next_token_ends_template_argument_p (parser))
9252         cp_parser_abort_tentative_parse (parser);
9253       else
9254         {
9255           if (TREE_CODE (argument) == INDIRECT_REF)
9256             {
9257               gcc_assert (REFERENCE_REF_P (argument));
9258               argument = TREE_OPERAND (argument, 0);
9259             }
9260
9261           if (TREE_CODE (argument) == BASELINK)
9262             /* We don't need the information about what class was used
9263                to name the overloaded functions.  */  
9264             argument = BASELINK_FUNCTIONS (argument);
9265
9266           if (TREE_CODE (argument) == VAR_DECL)
9267             {
9268               /* A variable without external linkage might still be a
9269                  valid constant-expression, so no error is issued here
9270                  if the external-linkage check fails.  */
9271               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9272                 cp_parser_simulate_error (parser);
9273             }
9274           else if (is_overloaded_fn (argument))
9275             /* All overloaded functions are allowed; if the external
9276                linkage test does not pass, an error will be issued
9277                later.  */
9278             ;
9279           else if (address_p
9280                    && (TREE_CODE (argument) == OFFSET_REF
9281                        || TREE_CODE (argument) == SCOPE_REF))
9282             /* A pointer-to-member.  */
9283             ;
9284           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9285             ;
9286           else
9287             cp_parser_simulate_error (parser);
9288
9289           if (cp_parser_parse_definitely (parser))
9290             {
9291               if (address_p)
9292                 argument = build_x_unary_op (ADDR_EXPR, argument);
9293               return argument;
9294             }
9295         }
9296     }
9297   /* If the argument started with "&", there are no other valid
9298      alternatives at this point.  */
9299   if (address_p)
9300     {
9301       cp_parser_error (parser, "invalid non-type template argument");
9302       return error_mark_node;
9303     }
9304
9305   /* If the argument wasn't successfully parsed as a type-id followed
9306      by '>>', the argument can only be a constant expression now.
9307      Otherwise, we try parsing the constant-expression tentatively,
9308      because the argument could really be a type-id.  */
9309   if (maybe_type_id)
9310     cp_parser_parse_tentatively (parser);
9311   argument = cp_parser_constant_expression (parser,
9312                                             /*allow_non_constant_p=*/false,
9313                                             /*non_constant_p=*/NULL);
9314   argument = fold_non_dependent_expr (argument);
9315   if (!maybe_type_id)
9316     return argument;
9317   if (!cp_parser_next_token_ends_template_argument_p (parser))
9318     cp_parser_error (parser, "expected template-argument");
9319   if (cp_parser_parse_definitely (parser))
9320     return argument;
9321   /* We did our best to parse the argument as a non type-id, but that
9322      was the only alternative that matched (albeit with a '>' after
9323      it). We can assume it's just a typo from the user, and a
9324      diagnostic will then be issued.  */
9325   return cp_parser_type_id (parser);
9326 }
9327
9328 /* Parse an explicit-instantiation.
9329
9330    explicit-instantiation:
9331      template declaration
9332
9333    Although the standard says `declaration', what it really means is:
9334
9335    explicit-instantiation:
9336      template decl-specifier-seq [opt] declarator [opt] ;
9337
9338    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9339    supposed to be allowed.  A defect report has been filed about this
9340    issue.
9341
9342    GNU Extension:
9343
9344    explicit-instantiation:
9345      storage-class-specifier template
9346        decl-specifier-seq [opt] declarator [opt] ;
9347      function-specifier template
9348        decl-specifier-seq [opt] declarator [opt] ;  */
9349
9350 static void
9351 cp_parser_explicit_instantiation (cp_parser* parser)
9352 {
9353   int declares_class_or_enum;
9354   cp_decl_specifier_seq decl_specifiers;
9355   tree extension_specifier = NULL_TREE;
9356
9357   /* Look for an (optional) storage-class-specifier or
9358      function-specifier.  */
9359   if (cp_parser_allow_gnu_extensions_p (parser))
9360     {
9361       extension_specifier
9362         = cp_parser_storage_class_specifier_opt (parser);
9363       if (!extension_specifier)
9364         extension_specifier
9365           = cp_parser_function_specifier_opt (parser,
9366                                               /*decl_specs=*/NULL);
9367     }
9368
9369   /* Look for the `template' keyword.  */
9370   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9371   /* Let the front end know that we are processing an explicit
9372      instantiation.  */
9373   begin_explicit_instantiation ();
9374   /* [temp.explicit] says that we are supposed to ignore access
9375      control while processing explicit instantiation directives.  */
9376   push_deferring_access_checks (dk_no_check);
9377   /* Parse a decl-specifier-seq.  */
9378   cp_parser_decl_specifier_seq (parser,
9379                                 CP_PARSER_FLAGS_OPTIONAL,
9380                                 &decl_specifiers,
9381                                 &declares_class_or_enum);
9382   /* If there was exactly one decl-specifier, and it declared a class,
9383      and there's no declarator, then we have an explicit type
9384      instantiation.  */
9385   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9386     {
9387       tree type;
9388
9389       type = check_tag_decl (&decl_specifiers);
9390       /* Turn access control back on for names used during
9391          template instantiation.  */
9392       pop_deferring_access_checks ();
9393       if (type)
9394         do_type_instantiation (type, extension_specifier,
9395                                /*complain=*/tf_error);
9396     }
9397   else
9398     {
9399       cp_declarator *declarator;
9400       tree decl;
9401
9402       /* Parse the declarator.  */
9403       declarator
9404         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9405                                 /*ctor_dtor_or_conv_p=*/NULL,
9406                                 /*parenthesized_p=*/NULL,
9407                                 /*member_p=*/false);
9408       if (declares_class_or_enum & 2)
9409         cp_parser_check_for_definition_in_return_type (declarator,
9410                                                        decl_specifiers.type);
9411       if (declarator != cp_error_declarator)
9412         {
9413           decl = grokdeclarator (declarator, &decl_specifiers,
9414                                  NORMAL, 0, NULL);
9415           /* Turn access control back on for names used during
9416              template instantiation.  */
9417           pop_deferring_access_checks ();
9418           /* Do the explicit instantiation.  */
9419           do_decl_instantiation (decl, extension_specifier);
9420         }
9421       else
9422         {
9423           pop_deferring_access_checks ();
9424           /* Skip the body of the explicit instantiation.  */
9425           cp_parser_skip_to_end_of_statement (parser);
9426         }
9427     }
9428   /* We're done with the instantiation.  */
9429   end_explicit_instantiation ();
9430
9431   cp_parser_consume_semicolon_at_end_of_statement (parser);
9432 }
9433
9434 /* Parse an explicit-specialization.
9435
9436    explicit-specialization:
9437      template < > declaration
9438
9439    Although the standard says `declaration', what it really means is:
9440
9441    explicit-specialization:
9442      template <> decl-specifier [opt] init-declarator [opt] ;
9443      template <> function-definition
9444      template <> explicit-specialization
9445      template <> template-declaration  */
9446
9447 static void
9448 cp_parser_explicit_specialization (cp_parser* parser)
9449 {
9450   bool need_lang_pop;
9451   /* Look for the `template' keyword.  */
9452   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9453   /* Look for the `<'.  */
9454   cp_parser_require (parser, CPP_LESS, "`<'");
9455   /* Look for the `>'.  */
9456   cp_parser_require (parser, CPP_GREATER, "`>'");
9457   /* We have processed another parameter list.  */
9458   ++parser->num_template_parameter_lists;
9459   /* [temp]
9460    
9461      A template ... explicit specialization ... shall not have C
9462      linkage.  */ 
9463   if (current_lang_name == lang_name_c)
9464     {
9465       error ("template specialization with C linkage");
9466       /* Give it C++ linkage to avoid confusing other parts of the
9467          front end.  */
9468       push_lang_context (lang_name_cplusplus);
9469       need_lang_pop = true;
9470     }
9471   else
9472     need_lang_pop = false;
9473   /* Let the front end know that we are beginning a specialization.  */
9474   begin_specialization ();
9475   /* If the next keyword is `template', we need to figure out whether
9476      or not we're looking a template-declaration.  */
9477   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9478     {
9479       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9480           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9481         cp_parser_template_declaration_after_export (parser,
9482                                                      /*member_p=*/false);
9483       else
9484         cp_parser_explicit_specialization (parser);
9485     }
9486   else
9487     /* Parse the dependent declaration.  */
9488     cp_parser_single_declaration (parser,
9489                                   /*checks=*/NULL_TREE,
9490                                   /*member_p=*/false,
9491                                   /*friend_p=*/NULL);
9492   /* We're done with the specialization.  */
9493   end_specialization ();
9494   /* For the erroneous case of a template with C linkage, we pushed an
9495      implicit C++ linkage scope; exit that scope now.  */
9496   if (need_lang_pop)
9497     pop_lang_context ();
9498   /* We're done with this parameter list.  */
9499   --parser->num_template_parameter_lists;
9500 }
9501
9502 /* Parse a type-specifier.
9503
9504    type-specifier:
9505      simple-type-specifier
9506      class-specifier
9507      enum-specifier
9508      elaborated-type-specifier
9509      cv-qualifier
9510
9511    GNU Extension:
9512
9513    type-specifier:
9514      __complex__
9515
9516    Returns a representation of the type-specifier.  For a
9517    class-specifier, enum-specifier, or elaborated-type-specifier, a
9518    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9519
9520    The parser flags FLAGS is used to control type-specifier parsing.
9521
9522    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9523    in a decl-specifier-seq.
9524
9525    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9526    class-specifier, enum-specifier, or elaborated-type-specifier, then
9527    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9528    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9529    zero.
9530
9531    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9532    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9533    is set to FALSE.  */
9534
9535 static tree
9536 cp_parser_type_specifier (cp_parser* parser,
9537                           cp_parser_flags flags,
9538                           cp_decl_specifier_seq *decl_specs,
9539                           bool is_declaration,
9540                           int* declares_class_or_enum,
9541                           bool* is_cv_qualifier)
9542 {
9543   tree type_spec = NULL_TREE;
9544   cp_token *token;
9545   enum rid keyword;
9546   cp_decl_spec ds = ds_last;
9547
9548   /* Assume this type-specifier does not declare a new type.  */
9549   if (declares_class_or_enum)
9550     *declares_class_or_enum = 0;
9551   /* And that it does not specify a cv-qualifier.  */
9552   if (is_cv_qualifier)
9553     *is_cv_qualifier = false;
9554   /* Peek at the next token.  */
9555   token = cp_lexer_peek_token (parser->lexer);
9556
9557   /* If we're looking at a keyword, we can use that to guide the
9558      production we choose.  */
9559   keyword = token->keyword;
9560   switch (keyword)
9561     {
9562     case RID_ENUM:
9563       /* 'enum' [identifier] '{' introduces an enum-specifier;
9564          'enum' <anything else> introduces an elaborated-type-specifier.  */
9565       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9566           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9567               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9568                  == CPP_OPEN_BRACE))
9569         {
9570           if (parser->num_template_parameter_lists)
9571             {
9572               error ("template declaration of %qs", "enum");
9573               cp_parser_skip_to_end_of_block_or_statement (parser);
9574               type_spec = error_mark_node;
9575             }
9576           else
9577             type_spec = cp_parser_enum_specifier (parser);
9578
9579           if (declares_class_or_enum)
9580             *declares_class_or_enum = 2;
9581           if (decl_specs)
9582             cp_parser_set_decl_spec_type (decl_specs,
9583                                           type_spec,
9584                                           /*user_defined_p=*/true);
9585           return type_spec;
9586         }
9587       else
9588         goto elaborated_type_specifier;
9589
9590       /* Any of these indicate either a class-specifier, or an
9591          elaborated-type-specifier.  */
9592     case RID_CLASS:
9593     case RID_STRUCT:
9594     case RID_UNION:
9595       /* Parse tentatively so that we can back up if we don't find a
9596          class-specifier.  */
9597       cp_parser_parse_tentatively (parser);
9598       /* Look for the class-specifier.  */
9599       type_spec = cp_parser_class_specifier (parser);
9600       /* If that worked, we're done.  */
9601       if (cp_parser_parse_definitely (parser))
9602         {
9603           if (declares_class_or_enum)
9604             *declares_class_or_enum = 2;
9605           if (decl_specs)
9606             cp_parser_set_decl_spec_type (decl_specs,
9607                                           type_spec,
9608                                           /*user_defined_p=*/true);
9609           return type_spec;
9610         }
9611
9612       /* Fall through.  */
9613     elaborated_type_specifier:
9614       /* We're declaring (not defining) a class or enum.  */
9615       if (declares_class_or_enum)
9616         *declares_class_or_enum = 1;
9617
9618       /* Fall through.  */
9619     case RID_TYPENAME:
9620       /* Look for an elaborated-type-specifier.  */
9621       type_spec
9622         = (cp_parser_elaborated_type_specifier
9623            (parser,
9624             decl_specs && decl_specs->specs[(int) ds_friend],
9625             is_declaration));
9626       if (decl_specs)
9627         cp_parser_set_decl_spec_type (decl_specs,
9628                                       type_spec,
9629                                       /*user_defined_p=*/true);
9630       return type_spec;
9631
9632     case RID_CONST:
9633       ds = ds_const;
9634       if (is_cv_qualifier)
9635         *is_cv_qualifier = true;
9636       break;
9637
9638     case RID_VOLATILE:
9639       ds = ds_volatile;
9640       if (is_cv_qualifier)
9641         *is_cv_qualifier = true;
9642       break;
9643
9644     case RID_RESTRICT:
9645       ds = ds_restrict;
9646       if (is_cv_qualifier)
9647         *is_cv_qualifier = true;
9648       break;
9649
9650     case RID_COMPLEX:
9651       /* The `__complex__' keyword is a GNU extension.  */
9652       ds = ds_complex;
9653       break;
9654
9655     default:
9656       break;
9657     }
9658
9659   /* Handle simple keywords.  */
9660   if (ds != ds_last)
9661     {
9662       if (decl_specs)
9663         {
9664           ++decl_specs->specs[(int)ds];
9665           decl_specs->any_specifiers_p = true;
9666         }
9667       return cp_lexer_consume_token (parser->lexer)->value;
9668     }
9669
9670   /* If we do not already have a type-specifier, assume we are looking
9671      at a simple-type-specifier.  */
9672   type_spec = cp_parser_simple_type_specifier (parser,
9673                                                decl_specs,
9674                                                flags);
9675
9676   /* If we didn't find a type-specifier, and a type-specifier was not
9677      optional in this context, issue an error message.  */
9678   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9679     {
9680       cp_parser_error (parser, "expected type specifier");
9681       return error_mark_node;
9682     }
9683
9684   return type_spec;
9685 }
9686
9687 /* Parse a simple-type-specifier.
9688
9689    simple-type-specifier:
9690      :: [opt] nested-name-specifier [opt] type-name
9691      :: [opt] nested-name-specifier template template-id
9692      char
9693      wchar_t
9694      bool
9695      short
9696      int
9697      long
9698      signed
9699      unsigned
9700      float
9701      double
9702      void
9703
9704    GNU Extension:
9705
9706    simple-type-specifier:
9707      __typeof__ unary-expression
9708      __typeof__ ( type-id )
9709
9710    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9711    appropriately updated.  */
9712
9713 static tree
9714 cp_parser_simple_type_specifier (cp_parser* parser,
9715                                  cp_decl_specifier_seq *decl_specs,
9716                                  cp_parser_flags flags)
9717 {
9718   tree type = NULL_TREE;
9719   cp_token *token;
9720
9721   /* Peek at the next token.  */
9722   token = cp_lexer_peek_token (parser->lexer);
9723
9724   /* If we're looking at a keyword, things are easy.  */
9725   switch (token->keyword)
9726     {
9727     case RID_CHAR:
9728       if (decl_specs)
9729         decl_specs->explicit_char_p = true;
9730       type = char_type_node;
9731       break;
9732     case RID_WCHAR:
9733       type = wchar_type_node;
9734       break;
9735     case RID_BOOL:
9736       type = boolean_type_node;
9737       break;
9738     case RID_SHORT:
9739       if (decl_specs)
9740         ++decl_specs->specs[(int) ds_short];
9741       type = short_integer_type_node;
9742       break;
9743     case RID_INT:
9744       if (decl_specs)
9745         decl_specs->explicit_int_p = true;
9746       type = integer_type_node;
9747       break;
9748     case RID_LONG:
9749       if (decl_specs)
9750         ++decl_specs->specs[(int) ds_long];
9751       type = long_integer_type_node;
9752       break;
9753     case RID_SIGNED:
9754       if (decl_specs)
9755         ++decl_specs->specs[(int) ds_signed];
9756       type = integer_type_node;
9757       break;
9758     case RID_UNSIGNED:
9759       if (decl_specs)
9760         ++decl_specs->specs[(int) ds_unsigned];
9761       type = unsigned_type_node;
9762       break;
9763     case RID_FLOAT:
9764       type = float_type_node;
9765       break;
9766     case RID_DOUBLE:
9767       type = double_type_node;
9768       break;
9769     case RID_VOID:
9770       type = void_type_node;
9771       break;
9772
9773     case RID_TYPEOF:
9774       /* Consume the `typeof' token.  */
9775       cp_lexer_consume_token (parser->lexer);
9776       /* Parse the operand to `typeof'.  */
9777       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9778       /* If it is not already a TYPE, take its type.  */
9779       if (!TYPE_P (type))
9780         type = finish_typeof (type);
9781
9782       if (decl_specs)
9783         cp_parser_set_decl_spec_type (decl_specs, type,
9784                                       /*user_defined_p=*/true);
9785
9786       return type;
9787
9788     default:
9789       break;
9790     }
9791
9792   /* If the type-specifier was for a built-in type, we're done.  */
9793   if (type)
9794     {
9795       tree id;
9796
9797       /* Record the type.  */
9798       if (decl_specs
9799           && (token->keyword != RID_SIGNED
9800               && token->keyword != RID_UNSIGNED
9801               && token->keyword != RID_SHORT
9802               && token->keyword != RID_LONG))
9803         cp_parser_set_decl_spec_type (decl_specs,
9804                                       type,
9805                                       /*user_defined=*/false);
9806       if (decl_specs)
9807         decl_specs->any_specifiers_p = true;
9808
9809       /* Consume the token.  */
9810       id = cp_lexer_consume_token (parser->lexer)->value;
9811
9812       /* There is no valid C++ program where a non-template type is
9813          followed by a "<".  That usually indicates that the user thought
9814          that the type was a template.  */
9815       cp_parser_check_for_invalid_template_id (parser, type);
9816
9817       return TYPE_NAME (type);
9818     }
9819
9820   /* The type-specifier must be a user-defined type.  */
9821   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9822     {
9823       bool qualified_p;
9824       bool global_p;
9825
9826       /* Don't gobble tokens or issue error messages if this is an
9827          optional type-specifier.  */
9828       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9829         cp_parser_parse_tentatively (parser);
9830
9831       /* Look for the optional `::' operator.  */
9832       global_p
9833         = (cp_parser_global_scope_opt (parser,
9834                                        /*current_scope_valid_p=*/false)
9835            != NULL_TREE);
9836       /* Look for the nested-name specifier.  */
9837       qualified_p
9838         = (cp_parser_nested_name_specifier_opt (parser,
9839                                                 /*typename_keyword_p=*/false,
9840                                                 /*check_dependency_p=*/true,
9841                                                 /*type_p=*/false,
9842                                                 /*is_declaration=*/false)
9843            != NULL_TREE);
9844       /* If we have seen a nested-name-specifier, and the next token
9845          is `template', then we are using the template-id production.  */
9846       if (parser->scope
9847           && cp_parser_optional_template_keyword (parser))
9848         {
9849           /* Look for the template-id.  */
9850           type = cp_parser_template_id (parser,
9851                                         /*template_keyword_p=*/true,
9852                                         /*check_dependency_p=*/true,
9853                                         /*is_declaration=*/false);
9854           /* If the template-id did not name a type, we are out of
9855              luck.  */
9856           if (TREE_CODE (type) != TYPE_DECL)
9857             {
9858               cp_parser_error (parser, "expected template-id for type");
9859               type = NULL_TREE;
9860             }
9861         }
9862       /* Otherwise, look for a type-name.  */
9863       else
9864         type = cp_parser_type_name (parser);
9865       /* Keep track of all name-lookups performed in class scopes.  */
9866       if (type
9867           && !global_p
9868           && !qualified_p
9869           && TREE_CODE (type) == TYPE_DECL
9870           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9871         maybe_note_name_used_in_class (DECL_NAME (type), type);
9872       /* If it didn't work out, we don't have a TYPE.  */
9873       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9874           && !cp_parser_parse_definitely (parser))
9875         type = NULL_TREE;
9876       if (type && decl_specs)
9877         cp_parser_set_decl_spec_type (decl_specs, type,
9878                                       /*user_defined=*/true);
9879     }
9880
9881   /* If we didn't get a type-name, issue an error message.  */
9882   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9883     {
9884       cp_parser_error (parser, "expected type-name");
9885       return error_mark_node;
9886     }
9887
9888   /* There is no valid C++ program where a non-template type is
9889      followed by a "<".  That usually indicates that the user thought
9890      that the type was a template.  */
9891   if (type && type != error_mark_node)
9892     {
9893       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9894          If it is, then the '<'...'>' enclose protocol names rather than
9895          template arguments, and so everything is fine.  */
9896       if (c_dialect_objc ()
9897           && (objc_is_id (type) || objc_is_class_name (type)))
9898         {
9899           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9900           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9901
9902           /* Clobber the "unqualified" type previously entered into
9903              DECL_SPECS with the new, improved protocol-qualified version.  */
9904           if (decl_specs)
9905             decl_specs->type = qual_type;
9906
9907           return qual_type;
9908         }
9909
9910       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9911     }
9912
9913   return type;
9914 }
9915
9916 /* Parse a type-name.
9917
9918    type-name:
9919      class-name
9920      enum-name
9921      typedef-name
9922
9923    enum-name:
9924      identifier
9925
9926    typedef-name:
9927      identifier
9928
9929    Returns a TYPE_DECL for the type.  */
9930
9931 static tree
9932 cp_parser_type_name (cp_parser* parser)
9933 {
9934   tree type_decl;
9935   tree identifier;
9936
9937   /* We can't know yet whether it is a class-name or not.  */
9938   cp_parser_parse_tentatively (parser);
9939   /* Try a class-name.  */
9940   type_decl = cp_parser_class_name (parser,
9941                                     /*typename_keyword_p=*/false,
9942                                     /*template_keyword_p=*/false,
9943                                     none_type,
9944                                     /*check_dependency_p=*/true,
9945                                     /*class_head_p=*/false,
9946                                     /*is_declaration=*/false);
9947   /* If it's not a class-name, keep looking.  */
9948   if (!cp_parser_parse_definitely (parser))
9949     {
9950       /* It must be a typedef-name or an enum-name.  */
9951       identifier = cp_parser_identifier (parser);
9952       if (identifier == error_mark_node)
9953         return error_mark_node;
9954
9955       /* Look up the type-name.  */
9956       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9957
9958       if (TREE_CODE (type_decl) != TYPE_DECL
9959           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9960         {
9961           /* See if this is an Objective-C type.  */
9962           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9963           tree type = objc_get_protocol_qualified_type (identifier, protos);
9964           if (type)
9965             type_decl = TYPE_NAME (type);
9966         }
9967
9968       /* Issue an error if we did not find a type-name.  */
9969       if (TREE_CODE (type_decl) != TYPE_DECL)
9970         {
9971           if (!cp_parser_simulate_error (parser))
9972             cp_parser_name_lookup_error (parser, identifier, type_decl,
9973                                          "is not a type");
9974           type_decl = error_mark_node;
9975         }
9976       /* Remember that the name was used in the definition of the
9977          current class so that we can check later to see if the
9978          meaning would have been different after the class was
9979          entirely defined.  */
9980       else if (type_decl != error_mark_node
9981                && !parser->scope)
9982         maybe_note_name_used_in_class (identifier, type_decl);
9983     }
9984
9985   return type_decl;
9986 }
9987
9988
9989 /* Parse an elaborated-type-specifier.  Note that the grammar given
9990    here incorporates the resolution to DR68.
9991
9992    elaborated-type-specifier:
9993      class-key :: [opt] nested-name-specifier [opt] identifier
9994      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9995      enum :: [opt] nested-name-specifier [opt] identifier
9996      typename :: [opt] nested-name-specifier identifier
9997      typename :: [opt] nested-name-specifier template [opt]
9998        template-id
9999
10000    GNU extension:
10001
10002    elaborated-type-specifier:
10003      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10004      class-key attributes :: [opt] nested-name-specifier [opt]
10005                template [opt] template-id
10006      enum attributes :: [opt] nested-name-specifier [opt] identifier
10007
10008    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10009    declared `friend'.  If IS_DECLARATION is TRUE, then this
10010    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10011    something is being declared.
10012
10013    Returns the TYPE specified.  */
10014
10015 static tree
10016 cp_parser_elaborated_type_specifier (cp_parser* parser,
10017                                      bool is_friend,
10018                                      bool is_declaration)
10019 {
10020   enum tag_types tag_type;
10021   tree identifier;
10022   tree type = NULL_TREE;
10023   tree attributes = NULL_TREE;
10024
10025   /* See if we're looking at the `enum' keyword.  */
10026   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10027     {
10028       /* Consume the `enum' token.  */
10029       cp_lexer_consume_token (parser->lexer);
10030       /* Remember that it's an enumeration type.  */
10031       tag_type = enum_type;
10032       /* Parse the attributes.  */
10033       attributes = cp_parser_attributes_opt (parser);
10034     }
10035   /* Or, it might be `typename'.  */
10036   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10037                                            RID_TYPENAME))
10038     {
10039       /* Consume the `typename' token.  */
10040       cp_lexer_consume_token (parser->lexer);
10041       /* Remember that it's a `typename' type.  */
10042       tag_type = typename_type;
10043       /* The `typename' keyword is only allowed in templates.  */
10044       if (!processing_template_decl)
10045         pedwarn ("using %<typename%> outside of template");
10046     }
10047   /* Otherwise it must be a class-key.  */
10048   else
10049     {
10050       tag_type = cp_parser_class_key (parser);
10051       if (tag_type == none_type)
10052         return error_mark_node;
10053       /* Parse the attributes.  */
10054       attributes = cp_parser_attributes_opt (parser);
10055     }
10056
10057   /* Look for the `::' operator.  */
10058   cp_parser_global_scope_opt (parser,
10059                               /*current_scope_valid_p=*/false);
10060   /* Look for the nested-name-specifier.  */
10061   if (tag_type == typename_type)
10062     {
10063       if (!cp_parser_nested_name_specifier (parser,
10064                                            /*typename_keyword_p=*/true,
10065                                            /*check_dependency_p=*/true,
10066                                            /*type_p=*/true,
10067                                             is_declaration))
10068         return error_mark_node;
10069     }
10070   else
10071     /* Even though `typename' is not present, the proposed resolution
10072        to Core Issue 180 says that in `class A<T>::B', `B' should be
10073        considered a type-name, even if `A<T>' is dependent.  */
10074     cp_parser_nested_name_specifier_opt (parser,
10075                                          /*typename_keyword_p=*/true,
10076                                          /*check_dependency_p=*/true,
10077                                          /*type_p=*/true,
10078                                          is_declaration);
10079   /* For everything but enumeration types, consider a template-id.  */
10080   if (tag_type != enum_type)
10081     {
10082       bool template_p = false;
10083       tree decl;
10084
10085       /* Allow the `template' keyword.  */
10086       template_p = cp_parser_optional_template_keyword (parser);
10087       /* If we didn't see `template', we don't know if there's a
10088          template-id or not.  */
10089       if (!template_p)
10090         cp_parser_parse_tentatively (parser);
10091       /* Parse the template-id.  */
10092       decl = cp_parser_template_id (parser, template_p,
10093                                     /*check_dependency_p=*/true,
10094                                     is_declaration);
10095       /* If we didn't find a template-id, look for an ordinary
10096          identifier.  */
10097       if (!template_p && !cp_parser_parse_definitely (parser))
10098         ;
10099       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10100          in effect, then we must assume that, upon instantiation, the
10101          template will correspond to a class.  */
10102       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10103                && tag_type == typename_type)
10104         type = make_typename_type (parser->scope, decl,
10105                                    typename_type,
10106                                    /*complain=*/tf_error);
10107       else
10108         type = TREE_TYPE (decl);
10109     }
10110
10111   /* For an enumeration type, consider only a plain identifier.  */
10112   if (!type)
10113     {
10114       identifier = cp_parser_identifier (parser);
10115
10116       if (identifier == error_mark_node)
10117         {
10118           parser->scope = NULL_TREE;
10119           return error_mark_node;
10120         }
10121
10122       /* For a `typename', we needn't call xref_tag.  */
10123       if (tag_type == typename_type
10124           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10125         return cp_parser_make_typename_type (parser, parser->scope,
10126                                              identifier);
10127       /* Look up a qualified name in the usual way.  */
10128       if (parser->scope)
10129         {
10130           tree decl;
10131
10132           decl = cp_parser_lookup_name (parser, identifier,
10133                                         tag_type,
10134                                         /*is_template=*/false,
10135                                         /*is_namespace=*/false,
10136                                         /*check_dependency=*/true,
10137                                         /*ambiguous_decls=*/NULL);
10138
10139           /* If we are parsing friend declaration, DECL may be a
10140              TEMPLATE_DECL tree node here.  However, we need to check
10141              whether this TEMPLATE_DECL results in valid code.  Consider
10142              the following example:
10143
10144                namespace N {
10145                  template <class T> class C {};
10146                }
10147                class X {
10148                  template <class T> friend class N::C; // #1, valid code
10149                };
10150                template <class T> class Y {
10151                  friend class N::C;                    // #2, invalid code
10152                };
10153
10154              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10155              name lookup of `N::C'.  We see that friend declaration must
10156              be template for the code to be valid.  Note that
10157              processing_template_decl does not work here since it is
10158              always 1 for the above two cases.  */
10159
10160           decl = (cp_parser_maybe_treat_template_as_class
10161                   (decl, /*tag_name_p=*/is_friend
10162                          && parser->num_template_parameter_lists));
10163
10164           if (TREE_CODE (decl) != TYPE_DECL)
10165             {
10166               cp_parser_diagnose_invalid_type_name (parser,
10167                                                     parser->scope,
10168                                                     identifier);
10169               return error_mark_node;
10170             }
10171
10172           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10173             check_elaborated_type_specifier
10174               (tag_type, decl,
10175                (parser->num_template_parameter_lists
10176                 || DECL_SELF_REFERENCE_P (decl)));
10177
10178           type = TREE_TYPE (decl);
10179         }
10180       else
10181         {
10182           /* An elaborated-type-specifier sometimes introduces a new type and
10183              sometimes names an existing type.  Normally, the rule is that it
10184              introduces a new type only if there is not an existing type of
10185              the same name already in scope.  For example, given:
10186
10187                struct S {};
10188                void f() { struct S s; }
10189
10190              the `struct S' in the body of `f' is the same `struct S' as in
10191              the global scope; the existing definition is used.  However, if
10192              there were no global declaration, this would introduce a new
10193              local class named `S'.
10194
10195              An exception to this rule applies to the following code:
10196
10197                namespace N { struct S; }
10198
10199              Here, the elaborated-type-specifier names a new type
10200              unconditionally; even if there is already an `S' in the
10201              containing scope this declaration names a new type.
10202              This exception only applies if the elaborated-type-specifier
10203              forms the complete declaration:
10204
10205                [class.name]
10206
10207                A declaration consisting solely of `class-key identifier ;' is
10208                either a redeclaration of the name in the current scope or a
10209                forward declaration of the identifier as a class name.  It
10210                introduces the name into the current scope.
10211
10212              We are in this situation precisely when the next token is a `;'.
10213
10214              An exception to the exception is that a `friend' declaration does
10215              *not* name a new type; i.e., given:
10216
10217                struct S { friend struct T; };
10218
10219              `T' is not a new type in the scope of `S'.
10220
10221              Also, `new struct S' or `sizeof (struct S)' never results in the
10222              definition of a new type; a new type can only be declared in a
10223              declaration context.  */
10224
10225           tag_scope ts;
10226           bool template_p;
10227
10228           if (is_friend)
10229             /* Friends have special name lookup rules.  */
10230             ts = ts_within_enclosing_non_class;
10231           else if (is_declaration
10232                    && cp_lexer_next_token_is (parser->lexer,
10233                                               CPP_SEMICOLON))
10234             /* This is a `class-key identifier ;' */
10235             ts = ts_current;
10236           else
10237             ts = ts_global;
10238
10239           /* Warn about attributes. They are ignored.  */
10240           if (attributes)
10241             warning (OPT_Wattributes,
10242                      "type attributes are honored only at type definition");
10243
10244           template_p = 
10245             (parser->num_template_parameter_lists
10246              && (cp_parser_next_token_starts_class_definition_p (parser)
10247                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10248           /* An unqualified name was used to reference this type, so
10249              there were no qualifying templates.  */
10250           if (!cp_parser_check_template_parameters (parser, 
10251                                                     /*num_templates=*/0))
10252             return error_mark_node;
10253           type = xref_tag (tag_type, identifier, ts, template_p);
10254         }
10255     }
10256   if (tag_type != enum_type)
10257     cp_parser_check_class_key (tag_type, type);
10258
10259   /* A "<" cannot follow an elaborated type specifier.  If that
10260      happens, the user was probably trying to form a template-id.  */
10261   cp_parser_check_for_invalid_template_id (parser, type);
10262
10263   return type;
10264 }
10265
10266 /* Parse an enum-specifier.
10267
10268    enum-specifier:
10269      enum identifier [opt] { enumerator-list [opt] }
10270
10271    GNU Extensions:
10272      enum identifier [opt] { enumerator-list [opt] } attributes
10273
10274    Returns an ENUM_TYPE representing the enumeration.  */
10275
10276 static tree
10277 cp_parser_enum_specifier (cp_parser* parser)
10278 {
10279   tree identifier;
10280   tree type;
10281
10282   /* Caller guarantees that the current token is 'enum', an identifier
10283      possibly follows, and the token after that is an opening brace.
10284      If we don't have an identifier, fabricate an anonymous name for
10285      the enumeration being defined.  */
10286   cp_lexer_consume_token (parser->lexer);
10287
10288   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10289     identifier = cp_parser_identifier (parser);
10290   else
10291     identifier = make_anon_name ();
10292
10293   /* Issue an error message if type-definitions are forbidden here.  */
10294   cp_parser_check_type_definition (parser);
10295
10296   /* Create the new type.  We do this before consuming the opening brace
10297      so the enum will be recorded as being on the line of its tag (or the
10298      'enum' keyword, if there is no tag).  */
10299   type = start_enum (identifier);
10300
10301   /* Consume the opening brace.  */
10302   cp_lexer_consume_token (parser->lexer);
10303
10304   /* If the next token is not '}', then there are some enumerators.  */
10305   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10306     cp_parser_enumerator_list (parser, type);
10307
10308   /* Consume the final '}'.  */
10309   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10310
10311   /* Look for trailing attributes to apply to this enumeration, and
10312      apply them if appropriate.  */
10313   if (cp_parser_allow_gnu_extensions_p (parser))
10314     {
10315       tree trailing_attr = cp_parser_attributes_opt (parser);
10316       cplus_decl_attributes (&type,
10317                              trailing_attr,
10318                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10319     }
10320
10321   /* Finish up the enumeration.  */
10322   finish_enum (type);
10323
10324   return type;
10325 }
10326
10327 /* Parse an enumerator-list.  The enumerators all have the indicated
10328    TYPE.
10329
10330    enumerator-list:
10331      enumerator-definition
10332      enumerator-list , enumerator-definition  */
10333
10334 static void
10335 cp_parser_enumerator_list (cp_parser* parser, tree type)
10336 {
10337   while (true)
10338     {
10339       /* Parse an enumerator-definition.  */
10340       cp_parser_enumerator_definition (parser, type);
10341
10342       /* If the next token is not a ',', we've reached the end of
10343          the list.  */
10344       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10345         break;
10346       /* Otherwise, consume the `,' and keep going.  */
10347       cp_lexer_consume_token (parser->lexer);
10348       /* If the next token is a `}', there is a trailing comma.  */
10349       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10350         {
10351           if (pedantic && !in_system_header)
10352             pedwarn ("comma at end of enumerator list");
10353           break;
10354         }
10355     }
10356 }
10357
10358 /* Parse an enumerator-definition.  The enumerator has the indicated
10359    TYPE.
10360
10361    enumerator-definition:
10362      enumerator
10363      enumerator = constant-expression
10364
10365    enumerator:
10366      identifier  */
10367
10368 static void
10369 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10370 {
10371   tree identifier;
10372   tree value;
10373
10374   /* Look for the identifier.  */
10375   identifier = cp_parser_identifier (parser);
10376   if (identifier == error_mark_node)
10377     return;
10378
10379   /* If the next token is an '=', then there is an explicit value.  */
10380   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10381     {
10382       /* Consume the `=' token.  */
10383       cp_lexer_consume_token (parser->lexer);
10384       /* Parse the value.  */
10385       value = cp_parser_constant_expression (parser,
10386                                              /*allow_non_constant_p=*/false,
10387                                              NULL);
10388     }
10389   else
10390     value = NULL_TREE;
10391
10392   /* Create the enumerator.  */
10393   build_enumerator (identifier, value, type);
10394 }
10395
10396 /* Parse a namespace-name.
10397
10398    namespace-name:
10399      original-namespace-name
10400      namespace-alias
10401
10402    Returns the NAMESPACE_DECL for the namespace.  */
10403
10404 static tree
10405 cp_parser_namespace_name (cp_parser* parser)
10406 {
10407   tree identifier;
10408   tree namespace_decl;
10409
10410   /* Get the name of the namespace.  */
10411   identifier = cp_parser_identifier (parser);
10412   if (identifier == error_mark_node)
10413     return error_mark_node;
10414
10415   /* Look up the identifier in the currently active scope.  Look only
10416      for namespaces, due to:
10417
10418        [basic.lookup.udir]
10419
10420        When looking up a namespace-name in a using-directive or alias
10421        definition, only namespace names are considered.
10422
10423      And:
10424
10425        [basic.lookup.qual]
10426
10427        During the lookup of a name preceding the :: scope resolution
10428        operator, object, function, and enumerator names are ignored.
10429
10430      (Note that cp_parser_class_or_namespace_name only calls this
10431      function if the token after the name is the scope resolution
10432      operator.)  */
10433   namespace_decl = cp_parser_lookup_name (parser, identifier,
10434                                           none_type,
10435                                           /*is_template=*/false,
10436                                           /*is_namespace=*/true,
10437                                           /*check_dependency=*/true,
10438                                           /*ambiguous_decls=*/NULL);
10439   /* If it's not a namespace, issue an error.  */
10440   if (namespace_decl == error_mark_node
10441       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10442     {
10443       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10444         error ("%qD is not a namespace-name", identifier);
10445       cp_parser_error (parser, "expected namespace-name");
10446       namespace_decl = error_mark_node;
10447     }
10448
10449   return namespace_decl;
10450 }
10451
10452 /* Parse a namespace-definition.
10453
10454    namespace-definition:
10455      named-namespace-definition
10456      unnamed-namespace-definition
10457
10458    named-namespace-definition:
10459      original-namespace-definition
10460      extension-namespace-definition
10461
10462    original-namespace-definition:
10463      namespace identifier { namespace-body }
10464
10465    extension-namespace-definition:
10466      namespace original-namespace-name { namespace-body }
10467
10468    unnamed-namespace-definition:
10469      namespace { namespace-body } */
10470
10471 static void
10472 cp_parser_namespace_definition (cp_parser* parser)
10473 {
10474   tree identifier, attribs;
10475
10476   /* Look for the `namespace' keyword.  */
10477   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10478
10479   /* Get the name of the namespace.  We do not attempt to distinguish
10480      between an original-namespace-definition and an
10481      extension-namespace-definition at this point.  The semantic
10482      analysis routines are responsible for that.  */
10483   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10484     identifier = cp_parser_identifier (parser);
10485   else
10486     identifier = NULL_TREE;
10487
10488   /* Parse any specified attributes.  */
10489   attribs = cp_parser_attributes_opt (parser);
10490
10491   /* Look for the `{' to start the namespace.  */
10492   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10493   /* Start the namespace.  */
10494   push_namespace_with_attribs (identifier, attribs);
10495   /* Parse the body of the namespace.  */
10496   cp_parser_namespace_body (parser);
10497   /* Finish the namespace.  */
10498   pop_namespace ();
10499   /* Look for the final `}'.  */
10500   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10501 }
10502
10503 /* Parse a namespace-body.
10504
10505    namespace-body:
10506      declaration-seq [opt]  */
10507
10508 static void
10509 cp_parser_namespace_body (cp_parser* parser)
10510 {
10511   cp_parser_declaration_seq_opt (parser);
10512 }
10513
10514 /* Parse a namespace-alias-definition.
10515
10516    namespace-alias-definition:
10517      namespace identifier = qualified-namespace-specifier ;  */
10518
10519 static void
10520 cp_parser_namespace_alias_definition (cp_parser* parser)
10521 {
10522   tree identifier;
10523   tree namespace_specifier;
10524
10525   /* Look for the `namespace' keyword.  */
10526   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10527   /* Look for the identifier.  */
10528   identifier = cp_parser_identifier (parser);
10529   if (identifier == error_mark_node)
10530     return;
10531   /* Look for the `=' token.  */
10532   cp_parser_require (parser, CPP_EQ, "`='");
10533   /* Look for the qualified-namespace-specifier.  */
10534   namespace_specifier
10535     = cp_parser_qualified_namespace_specifier (parser);
10536   /* Look for the `;' token.  */
10537   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10538
10539   /* Register the alias in the symbol table.  */
10540   do_namespace_alias (identifier, namespace_specifier);
10541 }
10542
10543 /* Parse a qualified-namespace-specifier.
10544
10545    qualified-namespace-specifier:
10546      :: [opt] nested-name-specifier [opt] namespace-name
10547
10548    Returns a NAMESPACE_DECL corresponding to the specified
10549    namespace.  */
10550
10551 static tree
10552 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10553 {
10554   /* Look for the optional `::'.  */
10555   cp_parser_global_scope_opt (parser,
10556                               /*current_scope_valid_p=*/false);
10557
10558   /* Look for the optional nested-name-specifier.  */
10559   cp_parser_nested_name_specifier_opt (parser,
10560                                        /*typename_keyword_p=*/false,
10561                                        /*check_dependency_p=*/true,
10562                                        /*type_p=*/false,
10563                                        /*is_declaration=*/true);
10564
10565   return cp_parser_namespace_name (parser);
10566 }
10567
10568 /* Parse a using-declaration.
10569
10570    using-declaration:
10571      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10572      using :: unqualified-id ;  */
10573
10574 static void
10575 cp_parser_using_declaration (cp_parser* parser)
10576 {
10577   cp_token *token;
10578   bool typename_p = false;
10579   bool global_scope_p;
10580   tree decl;
10581   tree identifier;
10582   tree qscope;
10583
10584   /* Look for the `using' keyword.  */
10585   cp_parser_require_keyword (parser, RID_USING, "`using'");
10586
10587   /* Peek at the next token.  */
10588   token = cp_lexer_peek_token (parser->lexer);
10589   /* See if it's `typename'.  */
10590   if (token->keyword == RID_TYPENAME)
10591     {
10592       /* Remember that we've seen it.  */
10593       typename_p = true;
10594       /* Consume the `typename' token.  */
10595       cp_lexer_consume_token (parser->lexer);
10596     }
10597
10598   /* Look for the optional global scope qualification.  */
10599   global_scope_p
10600     = (cp_parser_global_scope_opt (parser,
10601                                    /*current_scope_valid_p=*/false)
10602        != NULL_TREE);
10603
10604   /* If we saw `typename', or didn't see `::', then there must be a
10605      nested-name-specifier present.  */
10606   if (typename_p || !global_scope_p)
10607     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10608                                               /*check_dependency_p=*/true,
10609                                               /*type_p=*/false,
10610                                               /*is_declaration=*/true);
10611   /* Otherwise, we could be in either of the two productions.  In that
10612      case, treat the nested-name-specifier as optional.  */
10613   else
10614     qscope = cp_parser_nested_name_specifier_opt (parser,
10615                                                   /*typename_keyword_p=*/false,
10616                                                   /*check_dependency_p=*/true,
10617                                                   /*type_p=*/false,
10618                                                   /*is_declaration=*/true);
10619   if (!qscope)
10620     qscope = global_namespace;
10621
10622   /* Parse the unqualified-id.  */
10623   identifier = cp_parser_unqualified_id (parser,
10624                                          /*template_keyword_p=*/false,
10625                                          /*check_dependency_p=*/true,
10626                                          /*declarator_p=*/true,
10627                                          /*optional_p=*/false);
10628
10629   /* The function we call to handle a using-declaration is different
10630      depending on what scope we are in.  */
10631   if (qscope == error_mark_node || identifier == error_mark_node)
10632     ;
10633   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10634            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10635     /* [namespace.udecl]
10636
10637        A using declaration shall not name a template-id.  */
10638     error ("a template-id may not appear in a using-declaration");
10639   else
10640     {
10641       if (at_class_scope_p ())
10642         {
10643           /* Create the USING_DECL.  */
10644           decl = do_class_using_decl (parser->scope, identifier);
10645           /* Add it to the list of members in this class.  */
10646           finish_member_declaration (decl);
10647         }
10648       else
10649         {
10650           decl = cp_parser_lookup_name_simple (parser, identifier);
10651           if (decl == error_mark_node)
10652             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10653           else if (!at_namespace_scope_p ())
10654             do_local_using_decl (decl, qscope, identifier);
10655           else
10656             do_toplevel_using_decl (decl, qscope, identifier);
10657         }
10658     }
10659
10660   /* Look for the final `;'.  */
10661   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10662 }
10663
10664 /* Parse a using-directive.
10665
10666    using-directive:
10667      using namespace :: [opt] nested-name-specifier [opt]
10668        namespace-name ;  */
10669
10670 static void
10671 cp_parser_using_directive (cp_parser* parser)
10672 {
10673   tree namespace_decl;
10674   tree attribs;
10675
10676   /* Look for the `using' keyword.  */
10677   cp_parser_require_keyword (parser, RID_USING, "`using'");
10678   /* And the `namespace' keyword.  */
10679   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10680   /* Look for the optional `::' operator.  */
10681   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10682   /* And the optional nested-name-specifier.  */
10683   cp_parser_nested_name_specifier_opt (parser,
10684                                        /*typename_keyword_p=*/false,
10685                                        /*check_dependency_p=*/true,
10686                                        /*type_p=*/false,
10687                                        /*is_declaration=*/true);
10688   /* Get the namespace being used.  */
10689   namespace_decl = cp_parser_namespace_name (parser);
10690   /* And any specified attributes.  */
10691   attribs = cp_parser_attributes_opt (parser);
10692   /* Update the symbol table.  */
10693   parse_using_directive (namespace_decl, attribs);
10694   /* Look for the final `;'.  */
10695   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10696 }
10697
10698 /* Parse an asm-definition.
10699
10700    asm-definition:
10701      asm ( string-literal ) ;
10702
10703    GNU Extension:
10704
10705    asm-definition:
10706      asm volatile [opt] ( string-literal ) ;
10707      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10708      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10709                           : asm-operand-list [opt] ) ;
10710      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10711                           : asm-operand-list [opt]
10712                           : asm-operand-list [opt] ) ;  */
10713
10714 static void
10715 cp_parser_asm_definition (cp_parser* parser)
10716 {
10717   tree string;
10718   tree outputs = NULL_TREE;
10719   tree inputs = NULL_TREE;
10720   tree clobbers = NULL_TREE;
10721   tree asm_stmt;
10722   bool volatile_p = false;
10723   bool extended_p = false;
10724
10725   /* Look for the `asm' keyword.  */
10726   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10727   /* See if the next token is `volatile'.  */
10728   if (cp_parser_allow_gnu_extensions_p (parser)
10729       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10730     {
10731       /* Remember that we saw the `volatile' keyword.  */
10732       volatile_p = true;
10733       /* Consume the token.  */
10734       cp_lexer_consume_token (parser->lexer);
10735     }
10736   /* Look for the opening `('.  */
10737   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10738     return;
10739   /* Look for the string.  */
10740   string = cp_parser_string_literal (parser, false, false);
10741   if (string == error_mark_node)
10742     {
10743       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10744                                              /*consume_paren=*/true);
10745       return;
10746     }
10747
10748   /* If we're allowing GNU extensions, check for the extended assembly
10749      syntax.  Unfortunately, the `:' tokens need not be separated by
10750      a space in C, and so, for compatibility, we tolerate that here
10751      too.  Doing that means that we have to treat the `::' operator as
10752      two `:' tokens.  */
10753   if (cp_parser_allow_gnu_extensions_p (parser)
10754       && at_function_scope_p ()
10755       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10756           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10757     {
10758       bool inputs_p = false;
10759       bool clobbers_p = false;
10760
10761       /* The extended syntax was used.  */
10762       extended_p = true;
10763
10764       /* Look for outputs.  */
10765       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10766         {
10767           /* Consume the `:'.  */
10768           cp_lexer_consume_token (parser->lexer);
10769           /* Parse the output-operands.  */
10770           if (cp_lexer_next_token_is_not (parser->lexer,
10771                                           CPP_COLON)
10772               && cp_lexer_next_token_is_not (parser->lexer,
10773                                              CPP_SCOPE)
10774               && cp_lexer_next_token_is_not (parser->lexer,
10775                                              CPP_CLOSE_PAREN))
10776             outputs = cp_parser_asm_operand_list (parser);
10777         }
10778       /* If the next token is `::', there are no outputs, and the
10779          next token is the beginning of the inputs.  */
10780       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10781         /* The inputs are coming next.  */
10782         inputs_p = true;
10783
10784       /* Look for inputs.  */
10785       if (inputs_p
10786           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10787         {
10788           /* Consume the `:' or `::'.  */
10789           cp_lexer_consume_token (parser->lexer);
10790           /* Parse the output-operands.  */
10791           if (cp_lexer_next_token_is_not (parser->lexer,
10792                                           CPP_COLON)
10793               && cp_lexer_next_token_is_not (parser->lexer,
10794                                              CPP_CLOSE_PAREN))
10795             inputs = cp_parser_asm_operand_list (parser);
10796         }
10797       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10798         /* The clobbers are coming next.  */
10799         clobbers_p = true;
10800
10801       /* Look for clobbers.  */
10802       if (clobbers_p
10803           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10804         {
10805           /* Consume the `:' or `::'.  */
10806           cp_lexer_consume_token (parser->lexer);
10807           /* Parse the clobbers.  */
10808           if (cp_lexer_next_token_is_not (parser->lexer,
10809                                           CPP_CLOSE_PAREN))
10810             clobbers = cp_parser_asm_clobber_list (parser);
10811         }
10812     }
10813   /* Look for the closing `)'.  */
10814   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10815     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10816                                            /*consume_paren=*/true);
10817   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10818
10819   /* Create the ASM_EXPR.  */
10820   if (at_function_scope_p ())
10821     {
10822       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10823                                   inputs, clobbers);
10824       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10825       if (!extended_p)
10826         {
10827           tree temp = asm_stmt;
10828           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10829             temp = TREE_OPERAND (temp, 0);
10830
10831           ASM_INPUT_P (temp) = 1;
10832         }
10833     }
10834   else
10835     cgraph_add_asm_node (string);
10836 }
10837
10838 /* Declarators [gram.dcl.decl] */
10839
10840 /* Parse an init-declarator.
10841
10842    init-declarator:
10843      declarator initializer [opt]
10844
10845    GNU Extension:
10846
10847    init-declarator:
10848      declarator asm-specification [opt] attributes [opt] initializer [opt]
10849
10850    function-definition:
10851      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10852        function-body
10853      decl-specifier-seq [opt] declarator function-try-block
10854
10855    GNU Extension:
10856
10857    function-definition:
10858      __extension__ function-definition
10859
10860    The DECL_SPECIFIERS apply to this declarator.  Returns a
10861    representation of the entity declared.  If MEMBER_P is TRUE, then
10862    this declarator appears in a class scope.  The new DECL created by
10863    this declarator is returned.
10864
10865    The CHECKS are access checks that should be performed once we know
10866    what entity is being declared (and, therefore, what classes have
10867    befriended it).
10868
10869    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10870    for a function-definition here as well.  If the declarator is a
10871    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10872    be TRUE upon return.  By that point, the function-definition will
10873    have been completely parsed.
10874
10875    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10876    is FALSE.  */
10877
10878 static tree
10879 cp_parser_init_declarator (cp_parser* parser,
10880                            cp_decl_specifier_seq *decl_specifiers,
10881                            tree checks,
10882                            bool function_definition_allowed_p,
10883                            bool member_p,
10884                            int declares_class_or_enum,
10885                            bool* function_definition_p)
10886 {
10887   cp_token *token;
10888   cp_declarator *declarator;
10889   tree prefix_attributes;
10890   tree attributes;
10891   tree asm_specification;
10892   tree initializer;
10893   tree decl = NULL_TREE;
10894   tree scope;
10895   bool is_initialized;
10896   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
10897      initialized with "= ..", CPP_OPEN_PAREN if initialized with
10898      "(...)".  */
10899   enum cpp_ttype initialization_kind;
10900   bool is_parenthesized_init = false;
10901   bool is_non_constant_init;
10902   int ctor_dtor_or_conv_p;
10903   bool friend_p;
10904   tree pushed_scope = NULL;
10905
10906   /* Gather the attributes that were provided with the
10907      decl-specifiers.  */
10908   prefix_attributes = decl_specifiers->attributes;
10909
10910   /* Assume that this is not the declarator for a function
10911      definition.  */
10912   if (function_definition_p)
10913     *function_definition_p = false;
10914
10915   /* Defer access checks while parsing the declarator; we cannot know
10916      what names are accessible until we know what is being
10917      declared.  */
10918   resume_deferring_access_checks ();
10919
10920   /* Parse the declarator.  */
10921   declarator
10922     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10923                             &ctor_dtor_or_conv_p,
10924                             /*parenthesized_p=*/NULL,
10925                             /*member_p=*/false);
10926   /* Gather up the deferred checks.  */
10927   stop_deferring_access_checks ();
10928
10929   /* If the DECLARATOR was erroneous, there's no need to go
10930      further.  */
10931   if (declarator == cp_error_declarator)
10932     return error_mark_node;
10933
10934   if (declares_class_or_enum & 2)
10935     cp_parser_check_for_definition_in_return_type (declarator,
10936                                                    decl_specifiers->type);
10937
10938   /* Figure out what scope the entity declared by the DECLARATOR is
10939      located in.  `grokdeclarator' sometimes changes the scope, so
10940      we compute it now.  */
10941   scope = get_scope_of_declarator (declarator);
10942
10943   /* If we're allowing GNU extensions, look for an asm-specification
10944      and attributes.  */
10945   if (cp_parser_allow_gnu_extensions_p (parser))
10946     {
10947       /* Look for an asm-specification.  */
10948       asm_specification = cp_parser_asm_specification_opt (parser);
10949       /* And attributes.  */
10950       attributes = cp_parser_attributes_opt (parser);
10951     }
10952   else
10953     {
10954       asm_specification = NULL_TREE;
10955       attributes = NULL_TREE;
10956     }
10957
10958   /* Peek at the next token.  */
10959   token = cp_lexer_peek_token (parser->lexer);
10960   /* Check to see if the token indicates the start of a
10961      function-definition.  */
10962   if (cp_parser_token_starts_function_definition_p (token))
10963     {
10964       if (!function_definition_allowed_p)
10965         {
10966           /* If a function-definition should not appear here, issue an
10967              error message.  */
10968           cp_parser_error (parser,
10969                            "a function-definition is not allowed here");
10970           return error_mark_node;
10971         }
10972       else
10973         {
10974           /* Neither attributes nor an asm-specification are allowed
10975              on a function-definition.  */
10976           if (asm_specification)
10977             error ("an asm-specification is not allowed on a function-definition");
10978           if (attributes)
10979             error ("attributes are not allowed on a function-definition");
10980           /* This is a function-definition.  */
10981           *function_definition_p = true;
10982
10983           /* Parse the function definition.  */
10984           if (member_p)
10985             decl = cp_parser_save_member_function_body (parser,
10986                                                         decl_specifiers,
10987                                                         declarator,
10988                                                         prefix_attributes);
10989           else
10990             decl
10991               = (cp_parser_function_definition_from_specifiers_and_declarator
10992                  (parser, decl_specifiers, prefix_attributes, declarator));
10993
10994           return decl;
10995         }
10996     }
10997
10998   /* [dcl.dcl]
10999
11000      Only in function declarations for constructors, destructors, and
11001      type conversions can the decl-specifier-seq be omitted.
11002
11003      We explicitly postpone this check past the point where we handle
11004      function-definitions because we tolerate function-definitions
11005      that are missing their return types in some modes.  */
11006   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11007     {
11008       cp_parser_error (parser,
11009                        "expected constructor, destructor, or type conversion");
11010       return error_mark_node;
11011     }
11012
11013   /* An `=' or an `(' indicates an initializer.  */
11014   if (token->type == CPP_EQ
11015       || token->type == CPP_OPEN_PAREN)
11016     {
11017       is_initialized = true;
11018       initialization_kind = token->type;
11019     }
11020   else
11021     {
11022       /* If the init-declarator isn't initialized and isn't followed by a
11023          `,' or `;', it's not a valid init-declarator.  */
11024       if (token->type != CPP_COMMA
11025           && token->type != CPP_SEMICOLON)
11026         {
11027           cp_parser_error (parser, "expected initializer");
11028           return error_mark_node;
11029         }
11030       is_initialized = false;
11031       initialization_kind = CPP_EOF;
11032     }
11033
11034   /* Because start_decl has side-effects, we should only call it if we
11035      know we're going ahead.  By this point, we know that we cannot
11036      possibly be looking at any other construct.  */
11037   cp_parser_commit_to_tentative_parse (parser);
11038
11039   /* If the decl specifiers were bad, issue an error now that we're
11040      sure this was intended to be a declarator.  Then continue
11041      declaring the variable(s), as int, to try to cut down on further
11042      errors.  */
11043   if (decl_specifiers->any_specifiers_p
11044       && decl_specifiers->type == error_mark_node)
11045     {
11046       cp_parser_error (parser, "invalid type in declaration");
11047       decl_specifiers->type = integer_type_node;
11048     }
11049
11050   /* Check to see whether or not this declaration is a friend.  */
11051   friend_p = cp_parser_friend_p (decl_specifiers);
11052
11053   /* Check that the number of template-parameter-lists is OK.  */
11054   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11055     return error_mark_node;
11056
11057   /* Enter the newly declared entry in the symbol table.  If we're
11058      processing a declaration in a class-specifier, we wait until
11059      after processing the initializer.  */
11060   if (!member_p)
11061     {
11062       if (parser->in_unbraced_linkage_specification_p)
11063         {
11064           decl_specifiers->storage_class = sc_extern;
11065           have_extern_spec = false;
11066         }
11067       decl = start_decl (declarator, decl_specifiers,
11068                          is_initialized, attributes, prefix_attributes,
11069                          &pushed_scope);
11070     }
11071   else if (scope)
11072     /* Enter the SCOPE.  That way unqualified names appearing in the
11073        initializer will be looked up in SCOPE.  */
11074     pushed_scope = push_scope (scope);
11075
11076   /* Perform deferred access control checks, now that we know in which
11077      SCOPE the declared entity resides.  */
11078   if (!member_p && decl)
11079     {
11080       tree saved_current_function_decl = NULL_TREE;
11081
11082       /* If the entity being declared is a function, pretend that we
11083          are in its scope.  If it is a `friend', it may have access to
11084          things that would not otherwise be accessible.  */
11085       if (TREE_CODE (decl) == FUNCTION_DECL)
11086         {
11087           saved_current_function_decl = current_function_decl;
11088           current_function_decl = decl;
11089         }
11090
11091       /* Perform access checks for template parameters.  */
11092       cp_parser_perform_template_parameter_access_checks (checks);
11093
11094       /* Perform the access control checks for the declarator and the
11095          the decl-specifiers.  */
11096       perform_deferred_access_checks ();
11097
11098       /* Restore the saved value.  */
11099       if (TREE_CODE (decl) == FUNCTION_DECL)
11100         current_function_decl = saved_current_function_decl;
11101     }
11102
11103   /* Parse the initializer.  */
11104   initializer = NULL_TREE;
11105   is_parenthesized_init = false;
11106   is_non_constant_init = true;
11107   if (is_initialized)
11108     {
11109       if (declarator->kind == cdk_function
11110           && declarator->declarator->kind == cdk_id
11111           && initialization_kind == CPP_EQ)
11112         initializer = cp_parser_pure_specifier (parser);
11113       else
11114         initializer = cp_parser_initializer (parser,
11115                                              &is_parenthesized_init,
11116                                              &is_non_constant_init);
11117     }
11118
11119   /* The old parser allows attributes to appear after a parenthesized
11120      initializer.  Mark Mitchell proposed removing this functionality
11121      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11122      attributes -- but ignores them.  */
11123   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11124     if (cp_parser_attributes_opt (parser))
11125       warning (OPT_Wattributes,
11126                "attributes after parenthesized initializer ignored");
11127
11128   /* For an in-class declaration, use `grokfield' to create the
11129      declaration.  */
11130   if (member_p)
11131     {
11132       if (pushed_scope)
11133         {
11134           pop_scope (pushed_scope);
11135           pushed_scope = false;
11136         }
11137       decl = grokfield (declarator, decl_specifiers,
11138                         initializer, !is_non_constant_init,
11139                         /*asmspec=*/NULL_TREE,
11140                         prefix_attributes);
11141       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11142         cp_parser_save_default_args (parser, decl);
11143     }
11144
11145   /* Finish processing the declaration.  But, skip friend
11146      declarations.  */
11147   if (!friend_p && decl && decl != error_mark_node)
11148     {
11149       cp_finish_decl (decl,
11150                       initializer, !is_non_constant_init,
11151                       asm_specification,
11152                       /* If the initializer is in parentheses, then this is
11153                          a direct-initialization, which means that an
11154                          `explicit' constructor is OK.  Otherwise, an
11155                          `explicit' constructor cannot be used.  */
11156                       ((is_parenthesized_init || !is_initialized)
11157                      ? 0 : LOOKUP_ONLYCONVERTING));
11158     }
11159   if (!friend_p && pushed_scope)
11160     pop_scope (pushed_scope);
11161
11162   return decl;
11163 }
11164
11165 /* Parse a declarator.
11166
11167    declarator:
11168      direct-declarator
11169      ptr-operator declarator
11170
11171    abstract-declarator:
11172      ptr-operator abstract-declarator [opt]
11173      direct-abstract-declarator
11174
11175    GNU Extensions:
11176
11177    declarator:
11178      attributes [opt] direct-declarator
11179      attributes [opt] ptr-operator declarator
11180
11181    abstract-declarator:
11182      attributes [opt] ptr-operator abstract-declarator [opt]
11183      attributes [opt] direct-abstract-declarator
11184
11185    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11186    detect constructor, destructor or conversion operators. It is set
11187    to -1 if the declarator is a name, and +1 if it is a
11188    function. Otherwise it is set to zero. Usually you just want to
11189    test for >0, but internally the negative value is used.
11190
11191    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11192    a decl-specifier-seq unless it declares a constructor, destructor,
11193    or conversion.  It might seem that we could check this condition in
11194    semantic analysis, rather than parsing, but that makes it difficult
11195    to handle something like `f()'.  We want to notice that there are
11196    no decl-specifiers, and therefore realize that this is an
11197    expression, not a declaration.)
11198
11199    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11200    the declarator is a direct-declarator of the form "(...)".
11201
11202    MEMBER_P is true iff this declarator is a member-declarator.  */
11203
11204 static cp_declarator *
11205 cp_parser_declarator (cp_parser* parser,
11206                       cp_parser_declarator_kind dcl_kind,
11207                       int* ctor_dtor_or_conv_p,
11208                       bool* parenthesized_p,
11209                       bool member_p)
11210 {
11211   cp_token *token;
11212   cp_declarator *declarator;
11213   enum tree_code code;
11214   cp_cv_quals cv_quals;
11215   tree class_type;
11216   tree attributes = NULL_TREE;
11217
11218   /* Assume this is not a constructor, destructor, or type-conversion
11219      operator.  */
11220   if (ctor_dtor_or_conv_p)
11221     *ctor_dtor_or_conv_p = 0;
11222
11223   if (cp_parser_allow_gnu_extensions_p (parser))
11224     attributes = cp_parser_attributes_opt (parser);
11225
11226   /* Peek at the next token.  */
11227   token = cp_lexer_peek_token (parser->lexer);
11228
11229   /* Check for the ptr-operator production.  */
11230   cp_parser_parse_tentatively (parser);
11231   /* Parse the ptr-operator.  */
11232   code = cp_parser_ptr_operator (parser,
11233                                  &class_type,
11234                                  &cv_quals);
11235   /* If that worked, then we have a ptr-operator.  */
11236   if (cp_parser_parse_definitely (parser))
11237     {
11238       /* If a ptr-operator was found, then this declarator was not
11239          parenthesized.  */
11240       if (parenthesized_p)
11241         *parenthesized_p = true;
11242       /* The dependent declarator is optional if we are parsing an
11243          abstract-declarator.  */
11244       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11245         cp_parser_parse_tentatively (parser);
11246
11247       /* Parse the dependent declarator.  */
11248       declarator = cp_parser_declarator (parser, dcl_kind,
11249                                          /*ctor_dtor_or_conv_p=*/NULL,
11250                                          /*parenthesized_p=*/NULL,
11251                                          /*member_p=*/false);
11252
11253       /* If we are parsing an abstract-declarator, we must handle the
11254          case where the dependent declarator is absent.  */
11255       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11256           && !cp_parser_parse_definitely (parser))
11257         declarator = NULL;
11258
11259       /* Build the representation of the ptr-operator.  */
11260       if (class_type)
11261         declarator = make_ptrmem_declarator (cv_quals,
11262                                              class_type,
11263                                              declarator);
11264       else if (code == INDIRECT_REF)
11265         declarator = make_pointer_declarator (cv_quals, declarator);
11266       else
11267         declarator = make_reference_declarator (cv_quals, declarator);
11268     }
11269   /* Everything else is a direct-declarator.  */
11270   else
11271     {
11272       if (parenthesized_p)
11273         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11274                                                    CPP_OPEN_PAREN);
11275       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11276                                                 ctor_dtor_or_conv_p,
11277                                                 member_p);
11278     }
11279
11280   if (attributes && declarator != cp_error_declarator)
11281     declarator->attributes = attributes;
11282
11283   return declarator;
11284 }
11285
11286 /* Parse a direct-declarator or direct-abstract-declarator.
11287
11288    direct-declarator:
11289      declarator-id
11290      direct-declarator ( parameter-declaration-clause )
11291        cv-qualifier-seq [opt]
11292        exception-specification [opt]
11293      direct-declarator [ constant-expression [opt] ]
11294      ( declarator )
11295
11296    direct-abstract-declarator:
11297      direct-abstract-declarator [opt]
11298        ( parameter-declaration-clause )
11299        cv-qualifier-seq [opt]
11300        exception-specification [opt]
11301      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11302      ( abstract-declarator )
11303
11304    Returns a representation of the declarator.  DCL_KIND is
11305    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11306    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11307    we are parsing a direct-declarator.  It is
11308    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11309    of ambiguity we prefer an abstract declarator, as per
11310    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11311    cp_parser_declarator.  */
11312
11313 static cp_declarator *
11314 cp_parser_direct_declarator (cp_parser* parser,
11315                              cp_parser_declarator_kind dcl_kind,
11316                              int* ctor_dtor_or_conv_p,
11317                              bool member_p)
11318 {
11319   cp_token *token;
11320   cp_declarator *declarator = NULL;
11321   tree scope = NULL_TREE;
11322   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11323   bool saved_in_declarator_p = parser->in_declarator_p;
11324   bool first = true;
11325   tree pushed_scope = NULL_TREE;
11326
11327   while (true)
11328     {
11329       /* Peek at the next token.  */
11330       token = cp_lexer_peek_token (parser->lexer);
11331       if (token->type == CPP_OPEN_PAREN)
11332         {
11333           /* This is either a parameter-declaration-clause, or a
11334              parenthesized declarator. When we know we are parsing a
11335              named declarator, it must be a parenthesized declarator
11336              if FIRST is true. For instance, `(int)' is a
11337              parameter-declaration-clause, with an omitted
11338              direct-abstract-declarator. But `((*))', is a
11339              parenthesized abstract declarator. Finally, when T is a
11340              template parameter `(T)' is a
11341              parameter-declaration-clause, and not a parenthesized
11342              named declarator.
11343
11344              We first try and parse a parameter-declaration-clause,
11345              and then try a nested declarator (if FIRST is true).
11346
11347              It is not an error for it not to be a
11348              parameter-declaration-clause, even when FIRST is
11349              false. Consider,
11350
11351                int i (int);
11352                int i (3);
11353
11354              The first is the declaration of a function while the
11355              second is a the definition of a variable, including its
11356              initializer.
11357
11358              Having seen only the parenthesis, we cannot know which of
11359              these two alternatives should be selected.  Even more
11360              complex are examples like:
11361
11362                int i (int (a));
11363                int i (int (3));
11364
11365              The former is a function-declaration; the latter is a
11366              variable initialization.
11367
11368              Thus again, we try a parameter-declaration-clause, and if
11369              that fails, we back out and return.  */
11370
11371           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11372             {
11373               cp_parameter_declarator *params;
11374               unsigned saved_num_template_parameter_lists;
11375
11376               /* In a member-declarator, the only valid interpretation
11377                  of a parenthesis is the start of a
11378                  parameter-declaration-clause.  (It is invalid to
11379                  initialize a static data member with a parenthesized
11380                  initializer; only the "=" form of initialization is
11381                  permitted.)  */
11382               if (!member_p)
11383                 cp_parser_parse_tentatively (parser);
11384
11385               /* Consume the `('.  */
11386               cp_lexer_consume_token (parser->lexer);
11387               if (first)
11388                 {
11389                   /* If this is going to be an abstract declarator, we're
11390                      in a declarator and we can't have default args.  */
11391                   parser->default_arg_ok_p = false;
11392                   parser->in_declarator_p = true;
11393                 }
11394
11395               /* Inside the function parameter list, surrounding
11396                  template-parameter-lists do not apply.  */
11397               saved_num_template_parameter_lists
11398                 = parser->num_template_parameter_lists;
11399               parser->num_template_parameter_lists = 0;
11400
11401               /* Parse the parameter-declaration-clause.  */
11402               params = cp_parser_parameter_declaration_clause (parser);
11403
11404               parser->num_template_parameter_lists
11405                 = saved_num_template_parameter_lists;
11406
11407               /* If all went well, parse the cv-qualifier-seq and the
11408                  exception-specification.  */
11409               if (member_p || cp_parser_parse_definitely (parser))
11410                 {
11411                   cp_cv_quals cv_quals;
11412                   tree exception_specification;
11413
11414                   if (ctor_dtor_or_conv_p)
11415                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11416                   first = false;
11417                   /* Consume the `)'.  */
11418                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11419
11420                   /* Parse the cv-qualifier-seq.  */
11421                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11422                   /* And the exception-specification.  */
11423                   exception_specification
11424                     = cp_parser_exception_specification_opt (parser);
11425
11426                   /* Create the function-declarator.  */
11427                   declarator = make_call_declarator (declarator,
11428                                                      params,
11429                                                      cv_quals,
11430                                                      exception_specification);
11431                   /* Any subsequent parameter lists are to do with
11432                      return type, so are not those of the declared
11433                      function.  */
11434                   parser->default_arg_ok_p = false;
11435
11436                   /* Repeat the main loop.  */
11437                   continue;
11438                 }
11439             }
11440
11441           /* If this is the first, we can try a parenthesized
11442              declarator.  */
11443           if (first)
11444             {
11445               bool saved_in_type_id_in_expr_p;
11446
11447               parser->default_arg_ok_p = saved_default_arg_ok_p;
11448               parser->in_declarator_p = saved_in_declarator_p;
11449
11450               /* Consume the `('.  */
11451               cp_lexer_consume_token (parser->lexer);
11452               /* Parse the nested declarator.  */
11453               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11454               parser->in_type_id_in_expr_p = true;
11455               declarator
11456                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11457                                         /*parenthesized_p=*/NULL,
11458                                         member_p);
11459               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11460               first = false;
11461               /* Expect a `)'.  */
11462               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11463                 declarator = cp_error_declarator;
11464               if (declarator == cp_error_declarator)
11465                 break;
11466
11467               goto handle_declarator;
11468             }
11469           /* Otherwise, we must be done.  */
11470           else
11471             break;
11472         }
11473       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11474                && token->type == CPP_OPEN_SQUARE)
11475         {
11476           /* Parse an array-declarator.  */
11477           tree bounds;
11478
11479           if (ctor_dtor_or_conv_p)
11480             *ctor_dtor_or_conv_p = 0;
11481
11482           first = false;
11483           parser->default_arg_ok_p = false;
11484           parser->in_declarator_p = true;
11485           /* Consume the `['.  */
11486           cp_lexer_consume_token (parser->lexer);
11487           /* Peek at the next token.  */
11488           token = cp_lexer_peek_token (parser->lexer);
11489           /* If the next token is `]', then there is no
11490              constant-expression.  */
11491           if (token->type != CPP_CLOSE_SQUARE)
11492             {
11493               bool non_constant_p;
11494
11495               bounds
11496                 = cp_parser_constant_expression (parser,
11497                                                  /*allow_non_constant=*/true,
11498                                                  &non_constant_p);
11499               if (!non_constant_p)
11500                 bounds = fold_non_dependent_expr (bounds);
11501               /* Normally, the array bound must be an integral constant
11502                  expression.  However, as an extension, we allow VLAs
11503                  in function scopes.  */
11504               else if (!at_function_scope_p ())
11505                 {
11506                   error ("array bound is not an integer constant");
11507                   bounds = error_mark_node;
11508                 }
11509             }
11510           else
11511             bounds = NULL_TREE;
11512           /* Look for the closing `]'.  */
11513           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11514             {
11515               declarator = cp_error_declarator;
11516               break;
11517             }
11518
11519           declarator = make_array_declarator (declarator, bounds);
11520         }
11521       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11522         {
11523           tree qualifying_scope;
11524           tree unqualified_name;
11525           special_function_kind sfk;
11526           bool abstract_ok;
11527
11528           /* Parse a declarator-id */
11529           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11530           if (abstract_ok)
11531             cp_parser_parse_tentatively (parser);
11532           unqualified_name 
11533             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11534           qualifying_scope = parser->scope;
11535           if (abstract_ok)
11536             {
11537               if (!cp_parser_parse_definitely (parser))
11538                 unqualified_name = error_mark_node;
11539               else if (unqualified_name
11540                        && (qualifying_scope
11541                            || (TREE_CODE (unqualified_name)
11542                                != IDENTIFIER_NODE)))
11543                 {
11544                   cp_parser_error (parser, "expected unqualified-id");
11545                   unqualified_name = error_mark_node;
11546                 }
11547             }
11548
11549           if (!unqualified_name)
11550             return NULL;
11551           if (unqualified_name == error_mark_node)
11552             {
11553               declarator = cp_error_declarator;
11554               break;
11555             }
11556
11557           if (qualifying_scope && at_namespace_scope_p ()
11558               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11559             {
11560               /* In the declaration of a member of a template class
11561                  outside of the class itself, the SCOPE will sometimes
11562                  be a TYPENAME_TYPE.  For example, given:
11563
11564                  template <typename T>
11565                  int S<T>::R::i = 3;
11566
11567                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11568                  this context, we must resolve S<T>::R to an ordinary
11569                  type, rather than a typename type.
11570
11571                  The reason we normally avoid resolving TYPENAME_TYPEs
11572                  is that a specialization of `S' might render
11573                  `S<T>::R' not a type.  However, if `S' is
11574                  specialized, then this `i' will not be used, so there
11575                  is no harm in resolving the types here.  */
11576               tree type;
11577
11578               /* Resolve the TYPENAME_TYPE.  */
11579               type = resolve_typename_type (qualifying_scope,
11580                                             /*only_current_p=*/false);
11581               /* If that failed, the declarator is invalid.  */
11582               if (type == error_mark_node)
11583                 error ("%<%T::%D%> is not a type",
11584                        TYPE_CONTEXT (qualifying_scope),
11585                        TYPE_IDENTIFIER (qualifying_scope));
11586               qualifying_scope = type;
11587             }
11588
11589           sfk = sfk_none;
11590           if (unqualified_name)
11591             {
11592               tree class_type;
11593
11594               if (qualifying_scope
11595                   && CLASS_TYPE_P (qualifying_scope))
11596                 class_type = qualifying_scope;
11597               else
11598                 class_type = current_class_type;
11599
11600               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11601                 {
11602                   tree name_type = TREE_TYPE (unqualified_name);
11603                   if (class_type && same_type_p (name_type, class_type))
11604                     {
11605                       if (qualifying_scope
11606                           && CLASSTYPE_USE_TEMPLATE (name_type))
11607                         {
11608                           error ("invalid use of constructor as a template");
11609                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11610                                   "name the constructor in a qualified name",
11611                                   class_type,
11612                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11613                                   class_type, name_type);
11614                           declarator = cp_error_declarator;
11615                           break;
11616                         }
11617                       else
11618                         unqualified_name = constructor_name (class_type);
11619                     }
11620                   else
11621                     {
11622                       /* We do not attempt to print the declarator
11623                          here because we do not have enough
11624                          information about its original syntactic
11625                          form.  */
11626                       cp_parser_error (parser, "invalid declarator");
11627                       declarator = cp_error_declarator;
11628                       break;
11629                     }
11630                 }
11631
11632               if (class_type)
11633                 {
11634                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11635                     sfk = sfk_destructor;
11636                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11637                     sfk = sfk_conversion;
11638                   else if (/* There's no way to declare a constructor
11639                               for an anonymous type, even if the type
11640                               got a name for linkage purposes.  */
11641                            !TYPE_WAS_ANONYMOUS (class_type)
11642                            && constructor_name_p (unqualified_name,
11643                                                   class_type))
11644                     {
11645                       unqualified_name = constructor_name (class_type);
11646                       sfk = sfk_constructor;
11647                     }
11648
11649                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11650                     *ctor_dtor_or_conv_p = -1;
11651                 }
11652             }
11653           declarator = make_id_declarator (qualifying_scope, 
11654                                            unqualified_name,
11655                                            sfk);
11656           declarator->id_loc = token->location;
11657
11658         handle_declarator:;
11659           scope = get_scope_of_declarator (declarator);
11660           if (scope)
11661             /* Any names that appear after the declarator-id for a
11662                member are looked up in the containing scope.  */
11663             pushed_scope = push_scope (scope);
11664           parser->in_declarator_p = true;
11665           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11666               || (declarator && declarator->kind == cdk_id))
11667             /* Default args are only allowed on function
11668                declarations.  */
11669             parser->default_arg_ok_p = saved_default_arg_ok_p;
11670           else
11671             parser->default_arg_ok_p = false;
11672
11673           first = false;
11674         }
11675       /* We're done.  */
11676       else
11677         break;
11678     }
11679
11680   /* For an abstract declarator, we might wind up with nothing at this
11681      point.  That's an error; the declarator is not optional.  */
11682   if (!declarator)
11683     cp_parser_error (parser, "expected declarator");
11684
11685   /* If we entered a scope, we must exit it now.  */
11686   if (pushed_scope)
11687     pop_scope (pushed_scope);
11688
11689   parser->default_arg_ok_p = saved_default_arg_ok_p;
11690   parser->in_declarator_p = saved_in_declarator_p;
11691
11692   return declarator;
11693 }
11694
11695 /* Parse a ptr-operator.
11696
11697    ptr-operator:
11698      * cv-qualifier-seq [opt]
11699      &
11700      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11701
11702    GNU Extension:
11703
11704    ptr-operator:
11705      & cv-qualifier-seq [opt]
11706
11707    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11708    Returns ADDR_EXPR if a reference was used.  In the case of a
11709    pointer-to-member, *TYPE is filled in with the TYPE containing the
11710    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11711    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11712    ERROR_MARK if an error occurred.  */
11713
11714 static enum tree_code
11715 cp_parser_ptr_operator (cp_parser* parser,
11716                         tree* type,
11717                         cp_cv_quals *cv_quals)
11718 {
11719   enum tree_code code = ERROR_MARK;
11720   cp_token *token;
11721
11722   /* Assume that it's not a pointer-to-member.  */
11723   *type = NULL_TREE;
11724   /* And that there are no cv-qualifiers.  */
11725   *cv_quals = TYPE_UNQUALIFIED;
11726
11727   /* Peek at the next token.  */
11728   token = cp_lexer_peek_token (parser->lexer);
11729   /* If it's a `*' or `&' we have a pointer or reference.  */
11730   if (token->type == CPP_MULT || token->type == CPP_AND)
11731     {
11732       /* Remember which ptr-operator we were processing.  */
11733       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11734
11735       /* Consume the `*' or `&'.  */
11736       cp_lexer_consume_token (parser->lexer);
11737
11738       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11739          `&', if we are allowing GNU extensions.  (The only qualifier
11740          that can legally appear after `&' is `restrict', but that is
11741          enforced during semantic analysis.  */
11742       if (code == INDIRECT_REF
11743           || cp_parser_allow_gnu_extensions_p (parser))
11744         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11745     }
11746   else
11747     {
11748       /* Try the pointer-to-member case.  */
11749       cp_parser_parse_tentatively (parser);
11750       /* Look for the optional `::' operator.  */
11751       cp_parser_global_scope_opt (parser,
11752                                   /*current_scope_valid_p=*/false);
11753       /* Look for the nested-name specifier.  */
11754       cp_parser_nested_name_specifier (parser,
11755                                        /*typename_keyword_p=*/false,
11756                                        /*check_dependency_p=*/true,
11757                                        /*type_p=*/false,
11758                                        /*is_declaration=*/false);
11759       /* If we found it, and the next token is a `*', then we are
11760          indeed looking at a pointer-to-member operator.  */
11761       if (!cp_parser_error_occurred (parser)
11762           && cp_parser_require (parser, CPP_MULT, "`*'"))
11763         {
11764           /* Indicate that the `*' operator was used.  */
11765           code = INDIRECT_REF;
11766
11767           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11768             error ("%qD is a namespace", parser->scope);
11769           else
11770             {
11771               /* The type of which the member is a member is given by the
11772                  current SCOPE.  */
11773               *type = parser->scope;
11774               /* The next name will not be qualified.  */
11775               parser->scope = NULL_TREE;
11776               parser->qualifying_scope = NULL_TREE;
11777               parser->object_scope = NULL_TREE;
11778               /* Look for the optional cv-qualifier-seq.  */
11779               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11780             }
11781         }
11782       /* If that didn't work we don't have a ptr-operator.  */
11783       if (!cp_parser_parse_definitely (parser))
11784         cp_parser_error (parser, "expected ptr-operator");
11785     }
11786
11787   return code;
11788 }
11789
11790 /* Parse an (optional) cv-qualifier-seq.
11791
11792    cv-qualifier-seq:
11793      cv-qualifier cv-qualifier-seq [opt]
11794
11795    cv-qualifier:
11796      const
11797      volatile
11798
11799    GNU Extension:
11800
11801    cv-qualifier:
11802      __restrict__
11803
11804    Returns a bitmask representing the cv-qualifiers.  */
11805
11806 static cp_cv_quals
11807 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11808 {
11809   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11810
11811   while (true)
11812     {
11813       cp_token *token;
11814       cp_cv_quals cv_qualifier;
11815
11816       /* Peek at the next token.  */
11817       token = cp_lexer_peek_token (parser->lexer);
11818       /* See if it's a cv-qualifier.  */
11819       switch (token->keyword)
11820         {
11821         case RID_CONST:
11822           cv_qualifier = TYPE_QUAL_CONST;
11823           break;
11824
11825         case RID_VOLATILE:
11826           cv_qualifier = TYPE_QUAL_VOLATILE;
11827           break;
11828
11829         case RID_RESTRICT:
11830           cv_qualifier = TYPE_QUAL_RESTRICT;
11831           break;
11832
11833         default:
11834           cv_qualifier = TYPE_UNQUALIFIED;
11835           break;
11836         }
11837
11838       if (!cv_qualifier)
11839         break;
11840
11841       if (cv_quals & cv_qualifier)
11842         {
11843           error ("duplicate cv-qualifier");
11844           cp_lexer_purge_token (parser->lexer);
11845         }
11846       else
11847         {
11848           cp_lexer_consume_token (parser->lexer);
11849           cv_quals |= cv_qualifier;
11850         }
11851     }
11852
11853   return cv_quals;
11854 }
11855
11856 /* Parse a declarator-id.
11857
11858    declarator-id:
11859      id-expression
11860      :: [opt] nested-name-specifier [opt] type-name
11861
11862    In the `id-expression' case, the value returned is as for
11863    cp_parser_id_expression if the id-expression was an unqualified-id.
11864    If the id-expression was a qualified-id, then a SCOPE_REF is
11865    returned.  The first operand is the scope (either a NAMESPACE_DECL
11866    or TREE_TYPE), but the second is still just a representation of an
11867    unqualified-id.  */
11868
11869 static tree
11870 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11871 {
11872   tree id;
11873   /* The expression must be an id-expression.  Assume that qualified
11874      names are the names of types so that:
11875
11876        template <class T>
11877        int S<T>::R::i = 3;
11878
11879      will work; we must treat `S<T>::R' as the name of a type.
11880      Similarly, assume that qualified names are templates, where
11881      required, so that:
11882
11883        template <class T>
11884        int S<T>::R<T>::i = 3;
11885
11886      will work, too.  */
11887   id = cp_parser_id_expression (parser,
11888                                 /*template_keyword_p=*/false,
11889                                 /*check_dependency_p=*/false,
11890                                 /*template_p=*/NULL,
11891                                 /*declarator_p=*/true,
11892                                 optional_p);
11893   if (id && BASELINK_P (id))
11894     id = BASELINK_FUNCTIONS (id);
11895   return id;
11896 }
11897
11898 /* Parse a type-id.
11899
11900    type-id:
11901      type-specifier-seq abstract-declarator [opt]
11902
11903    Returns the TYPE specified.  */
11904
11905 static tree
11906 cp_parser_type_id (cp_parser* parser)
11907 {
11908   cp_decl_specifier_seq type_specifier_seq;
11909   cp_declarator *abstract_declarator;
11910
11911   /* Parse the type-specifier-seq.  */
11912   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11913                                 &type_specifier_seq);
11914   if (type_specifier_seq.type == error_mark_node)
11915     return error_mark_node;
11916
11917   /* There might or might not be an abstract declarator.  */
11918   cp_parser_parse_tentatively (parser);
11919   /* Look for the declarator.  */
11920   abstract_declarator
11921     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11922                             /*parenthesized_p=*/NULL,
11923                             /*member_p=*/false);
11924   /* Check to see if there really was a declarator.  */
11925   if (!cp_parser_parse_definitely (parser))
11926     abstract_declarator = NULL;
11927
11928   return groktypename (&type_specifier_seq, abstract_declarator);
11929 }
11930
11931 /* Parse a type-specifier-seq.
11932
11933    type-specifier-seq:
11934      type-specifier type-specifier-seq [opt]
11935
11936    GNU extension:
11937
11938    type-specifier-seq:
11939      attributes type-specifier-seq [opt]
11940
11941    If IS_CONDITION is true, we are at the start of a "condition",
11942    e.g., we've just seen "if (".
11943
11944    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11945
11946 static void
11947 cp_parser_type_specifier_seq (cp_parser* parser,
11948                               bool is_condition,
11949                               cp_decl_specifier_seq *type_specifier_seq)
11950 {
11951   bool seen_type_specifier = false;
11952   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11953
11954   /* Clear the TYPE_SPECIFIER_SEQ.  */
11955   clear_decl_specs (type_specifier_seq);
11956
11957   /* Parse the type-specifiers and attributes.  */
11958   while (true)
11959     {
11960       tree type_specifier;
11961       bool is_cv_qualifier;
11962
11963       /* Check for attributes first.  */
11964       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11965         {
11966           type_specifier_seq->attributes =
11967             chainon (type_specifier_seq->attributes,
11968                      cp_parser_attributes_opt (parser));
11969           continue;
11970         }
11971
11972       /* Look for the type-specifier.  */
11973       type_specifier = cp_parser_type_specifier (parser,
11974                                                  flags,
11975                                                  type_specifier_seq,
11976                                                  /*is_declaration=*/false,
11977                                                  NULL,
11978                                                  &is_cv_qualifier);
11979       if (!type_specifier)
11980         {
11981           /* If the first type-specifier could not be found, this is not a
11982              type-specifier-seq at all.  */
11983           if (!seen_type_specifier)
11984             {
11985               cp_parser_error (parser, "expected type-specifier");
11986               type_specifier_seq->type = error_mark_node;
11987               return;
11988             }
11989           /* If subsequent type-specifiers could not be found, the
11990              type-specifier-seq is complete.  */
11991           break;
11992         }
11993
11994       seen_type_specifier = true;
11995       /* The standard says that a condition can be:
11996
11997             type-specifier-seq declarator = assignment-expression
11998
11999          However, given:
12000
12001            struct S {};
12002            if (int S = ...)
12003
12004          we should treat the "S" as a declarator, not as a
12005          type-specifier.  The standard doesn't say that explicitly for
12006          type-specifier-seq, but it does say that for
12007          decl-specifier-seq in an ordinary declaration.  Perhaps it
12008          would be clearer just to allow a decl-specifier-seq here, and
12009          then add a semantic restriction that if any decl-specifiers
12010          that are not type-specifiers appear, the program is invalid.  */
12011       if (is_condition && !is_cv_qualifier)
12012         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12013     }
12014 }
12015
12016 /* Parse a parameter-declaration-clause.
12017
12018    parameter-declaration-clause:
12019      parameter-declaration-list [opt] ... [opt]
12020      parameter-declaration-list , ...
12021
12022    Returns a representation for the parameter declarations.  A return
12023    value of NULL indicates a parameter-declaration-clause consisting
12024    only of an ellipsis.  */
12025
12026 static cp_parameter_declarator *
12027 cp_parser_parameter_declaration_clause (cp_parser* parser)
12028 {
12029   cp_parameter_declarator *parameters;
12030   cp_token *token;
12031   bool ellipsis_p;
12032   bool is_error;
12033
12034   /* Peek at the next token.  */
12035   token = cp_lexer_peek_token (parser->lexer);
12036   /* Check for trivial parameter-declaration-clauses.  */
12037   if (token->type == CPP_ELLIPSIS)
12038     {
12039       /* Consume the `...' token.  */
12040       cp_lexer_consume_token (parser->lexer);
12041       return NULL;
12042     }
12043   else if (token->type == CPP_CLOSE_PAREN)
12044     /* There are no parameters.  */
12045     {
12046 #ifndef NO_IMPLICIT_EXTERN_C
12047       if (in_system_header && current_class_type == NULL
12048           && current_lang_name == lang_name_c)
12049         return NULL;
12050       else
12051 #endif
12052         return no_parameters;
12053     }
12054   /* Check for `(void)', too, which is a special case.  */
12055   else if (token->keyword == RID_VOID
12056            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12057                == CPP_CLOSE_PAREN))
12058     {
12059       /* Consume the `void' token.  */
12060       cp_lexer_consume_token (parser->lexer);
12061       /* There are no parameters.  */
12062       return no_parameters;
12063     }
12064
12065   /* Parse the parameter-declaration-list.  */
12066   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12067   /* If a parse error occurred while parsing the
12068      parameter-declaration-list, then the entire
12069      parameter-declaration-clause is erroneous.  */
12070   if (is_error)
12071     return NULL;
12072
12073   /* Peek at the next token.  */
12074   token = cp_lexer_peek_token (parser->lexer);
12075   /* If it's a `,', the clause should terminate with an ellipsis.  */
12076   if (token->type == CPP_COMMA)
12077     {
12078       /* Consume the `,'.  */
12079       cp_lexer_consume_token (parser->lexer);
12080       /* Expect an ellipsis.  */
12081       ellipsis_p
12082         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12083     }
12084   /* It might also be `...' if the optional trailing `,' was
12085      omitted.  */
12086   else if (token->type == CPP_ELLIPSIS)
12087     {
12088       /* Consume the `...' token.  */
12089       cp_lexer_consume_token (parser->lexer);
12090       /* And remember that we saw it.  */
12091       ellipsis_p = true;
12092     }
12093   else
12094     ellipsis_p = false;
12095
12096   /* Finish the parameter list.  */
12097   if (parameters && ellipsis_p)
12098     parameters->ellipsis_p = true;
12099
12100   return parameters;
12101 }
12102
12103 /* Parse a parameter-declaration-list.
12104
12105    parameter-declaration-list:
12106      parameter-declaration
12107      parameter-declaration-list , parameter-declaration
12108
12109    Returns a representation of the parameter-declaration-list, as for
12110    cp_parser_parameter_declaration_clause.  However, the
12111    `void_list_node' is never appended to the list.  Upon return,
12112    *IS_ERROR will be true iff an error occurred.  */
12113
12114 static cp_parameter_declarator *
12115 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12116 {
12117   cp_parameter_declarator *parameters = NULL;
12118   cp_parameter_declarator **tail = &parameters;
12119
12120   /* Assume all will go well.  */
12121   *is_error = false;
12122
12123   /* Look for more parameters.  */
12124   while (true)
12125     {
12126       cp_parameter_declarator *parameter;
12127       bool parenthesized_p;
12128       /* Parse the parameter.  */
12129       parameter
12130         = cp_parser_parameter_declaration (parser,
12131                                            /*template_parm_p=*/false,
12132                                            &parenthesized_p);
12133
12134       /* If a parse error occurred parsing the parameter declaration,
12135          then the entire parameter-declaration-list is erroneous.  */
12136       if (!parameter)
12137         {
12138           *is_error = true;
12139           parameters = NULL;
12140           break;
12141         }
12142       /* Add the new parameter to the list.  */
12143       *tail = parameter;
12144       tail = &parameter->next;
12145
12146       /* Peek at the next token.  */
12147       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12148           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12149           /* These are for Objective-C++ */
12150           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12151           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12152         /* The parameter-declaration-list is complete.  */
12153         break;
12154       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12155         {
12156           cp_token *token;
12157
12158           /* Peek at the next token.  */
12159           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12160           /* If it's an ellipsis, then the list is complete.  */
12161           if (token->type == CPP_ELLIPSIS)
12162             break;
12163           /* Otherwise, there must be more parameters.  Consume the
12164              `,'.  */
12165           cp_lexer_consume_token (parser->lexer);
12166           /* When parsing something like:
12167
12168                 int i(float f, double d)
12169
12170              we can tell after seeing the declaration for "f" that we
12171              are not looking at an initialization of a variable "i",
12172              but rather at the declaration of a function "i".
12173
12174              Due to the fact that the parsing of template arguments
12175              (as specified to a template-id) requires backtracking we
12176              cannot use this technique when inside a template argument
12177              list.  */
12178           if (!parser->in_template_argument_list_p
12179               && !parser->in_type_id_in_expr_p
12180               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12181               /* However, a parameter-declaration of the form
12182                  "foat(f)" (which is a valid declaration of a
12183                  parameter "f") can also be interpreted as an
12184                  expression (the conversion of "f" to "float").  */
12185               && !parenthesized_p)
12186             cp_parser_commit_to_tentative_parse (parser);
12187         }
12188       else
12189         {
12190           cp_parser_error (parser, "expected %<,%> or %<...%>");
12191           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12192             cp_parser_skip_to_closing_parenthesis (parser,
12193                                                    /*recovering=*/true,
12194                                                    /*or_comma=*/false,
12195                                                    /*consume_paren=*/false);
12196           break;
12197         }
12198     }
12199
12200   return parameters;
12201 }
12202
12203 /* Parse a parameter declaration.
12204
12205    parameter-declaration:
12206      decl-specifier-seq declarator
12207      decl-specifier-seq declarator = assignment-expression
12208      decl-specifier-seq abstract-declarator [opt]
12209      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12210
12211    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12212    declares a template parameter.  (In that case, a non-nested `>'
12213    token encountered during the parsing of the assignment-expression
12214    is not interpreted as a greater-than operator.)
12215
12216    Returns a representation of the parameter, or NULL if an error
12217    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12218    true iff the declarator is of the form "(p)".  */
12219
12220 static cp_parameter_declarator *
12221 cp_parser_parameter_declaration (cp_parser *parser,
12222                                  bool template_parm_p,
12223                                  bool *parenthesized_p)
12224 {
12225   int declares_class_or_enum;
12226   bool greater_than_is_operator_p;
12227   cp_decl_specifier_seq decl_specifiers;
12228   cp_declarator *declarator;
12229   tree default_argument;
12230   cp_token *token;
12231   const char *saved_message;
12232
12233   /* In a template parameter, `>' is not an operator.
12234
12235      [temp.param]
12236
12237      When parsing a default template-argument for a non-type
12238      template-parameter, the first non-nested `>' is taken as the end
12239      of the template parameter-list rather than a greater-than
12240      operator.  */
12241   greater_than_is_operator_p = !template_parm_p;
12242
12243   /* Type definitions may not appear in parameter types.  */
12244   saved_message = parser->type_definition_forbidden_message;
12245   parser->type_definition_forbidden_message
12246     = "types may not be defined in parameter types";
12247
12248   /* Parse the declaration-specifiers.  */
12249   cp_parser_decl_specifier_seq (parser,
12250                                 CP_PARSER_FLAGS_NONE,
12251                                 &decl_specifiers,
12252                                 &declares_class_or_enum);
12253   /* If an error occurred, there's no reason to attempt to parse the
12254      rest of the declaration.  */
12255   if (cp_parser_error_occurred (parser))
12256     {
12257       parser->type_definition_forbidden_message = saved_message;
12258       return NULL;
12259     }
12260
12261   /* Peek at the next token.  */
12262   token = cp_lexer_peek_token (parser->lexer);
12263   /* If the next token is a `)', `,', `=', `>', or `...', then there
12264      is no declarator.  */
12265   if (token->type == CPP_CLOSE_PAREN
12266       || token->type == CPP_COMMA
12267       || token->type == CPP_EQ
12268       || token->type == CPP_ELLIPSIS
12269       || token->type == CPP_GREATER)
12270     {
12271       declarator = NULL;
12272       if (parenthesized_p)
12273         *parenthesized_p = false;
12274     }
12275   /* Otherwise, there should be a declarator.  */
12276   else
12277     {
12278       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12279       parser->default_arg_ok_p = false;
12280
12281       /* After seeing a decl-specifier-seq, if the next token is not a
12282          "(", there is no possibility that the code is a valid
12283          expression.  Therefore, if parsing tentatively, we commit at
12284          this point.  */
12285       if (!parser->in_template_argument_list_p
12286           /* In an expression context, having seen:
12287
12288                (int((char ...
12289
12290              we cannot be sure whether we are looking at a
12291              function-type (taking a "char" as a parameter) or a cast
12292              of some object of type "char" to "int".  */
12293           && !parser->in_type_id_in_expr_p
12294           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12295           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12296         cp_parser_commit_to_tentative_parse (parser);
12297       /* Parse the declarator.  */
12298       declarator = cp_parser_declarator (parser,
12299                                          CP_PARSER_DECLARATOR_EITHER,
12300                                          /*ctor_dtor_or_conv_p=*/NULL,
12301                                          parenthesized_p,
12302                                          /*member_p=*/false);
12303       parser->default_arg_ok_p = saved_default_arg_ok_p;
12304       /* After the declarator, allow more attributes.  */
12305       decl_specifiers.attributes
12306         = chainon (decl_specifiers.attributes,
12307                    cp_parser_attributes_opt (parser));
12308     }
12309
12310   /* The restriction on defining new types applies only to the type
12311      of the parameter, not to the default argument.  */
12312   parser->type_definition_forbidden_message = saved_message;
12313
12314   /* If the next token is `=', then process a default argument.  */
12315   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12316     {
12317       bool saved_greater_than_is_operator_p;
12318       /* Consume the `='.  */
12319       cp_lexer_consume_token (parser->lexer);
12320
12321       /* If we are defining a class, then the tokens that make up the
12322          default argument must be saved and processed later.  */
12323       if (!template_parm_p && at_class_scope_p ()
12324           && TYPE_BEING_DEFINED (current_class_type))
12325         {
12326           unsigned depth = 0;
12327           cp_token *first_token;
12328           cp_token *token;
12329
12330           /* Add tokens until we have processed the entire default
12331              argument.  We add the range [first_token, token).  */
12332           first_token = cp_lexer_peek_token (parser->lexer);
12333           while (true)
12334             {
12335               bool done = false;
12336
12337               /* Peek at the next token.  */
12338               token = cp_lexer_peek_token (parser->lexer);
12339               /* What we do depends on what token we have.  */
12340               switch (token->type)
12341                 {
12342                   /* In valid code, a default argument must be
12343                      immediately followed by a `,' `)', or `...'.  */
12344                 case CPP_COMMA:
12345                 case CPP_CLOSE_PAREN:
12346                 case CPP_ELLIPSIS:
12347                   /* If we run into a non-nested `;', `}', or `]',
12348                      then the code is invalid -- but the default
12349                      argument is certainly over.  */
12350                 case CPP_SEMICOLON:
12351                 case CPP_CLOSE_BRACE:
12352                 case CPP_CLOSE_SQUARE:
12353                   if (depth == 0)
12354                     done = true;
12355                   /* Update DEPTH, if necessary.  */
12356                   else if (token->type == CPP_CLOSE_PAREN
12357                            || token->type == CPP_CLOSE_BRACE
12358                            || token->type == CPP_CLOSE_SQUARE)
12359                     --depth;
12360                   break;
12361
12362                 case CPP_OPEN_PAREN:
12363                 case CPP_OPEN_SQUARE:
12364                 case CPP_OPEN_BRACE:
12365                   ++depth;
12366                   break;
12367
12368                 case CPP_GREATER:
12369                   /* If we see a non-nested `>', and `>' is not an
12370                      operator, then it marks the end of the default
12371                      argument.  */
12372                   if (!depth && !greater_than_is_operator_p)
12373                     done = true;
12374                   break;
12375
12376                   /* If we run out of tokens, issue an error message.  */
12377                 case CPP_EOF:
12378                 case CPP_PRAGMA_EOL:
12379                   error ("file ends in default argument");
12380                   done = true;
12381                   break;
12382
12383                 case CPP_NAME:
12384                 case CPP_SCOPE:
12385                   /* In these cases, we should look for template-ids.
12386                      For example, if the default argument is
12387                      `X<int, double>()', we need to do name lookup to
12388                      figure out whether or not `X' is a template; if
12389                      so, the `,' does not end the default argument.
12390
12391                      That is not yet done.  */
12392                   break;
12393
12394                 default:
12395                   break;
12396                 }
12397
12398               /* If we've reached the end, stop.  */
12399               if (done)
12400                 break;
12401
12402               /* Add the token to the token block.  */
12403               token = cp_lexer_consume_token (parser->lexer);
12404             }
12405
12406           /* Create a DEFAULT_ARG to represented the unparsed default
12407              argument.  */
12408           default_argument = make_node (DEFAULT_ARG);
12409           DEFARG_TOKENS (default_argument)
12410             = cp_token_cache_new (first_token, token);
12411           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12412         }
12413       /* Outside of a class definition, we can just parse the
12414          assignment-expression.  */
12415       else
12416         {
12417           bool saved_local_variables_forbidden_p;
12418
12419           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12420              set correctly.  */
12421           saved_greater_than_is_operator_p
12422             = parser->greater_than_is_operator_p;
12423           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12424           /* Local variable names (and the `this' keyword) may not
12425              appear in a default argument.  */
12426           saved_local_variables_forbidden_p
12427             = parser->local_variables_forbidden_p;
12428           parser->local_variables_forbidden_p = true;
12429           /* The default argument expression may cause implicitly
12430              defined member functions to be synthesized, which will
12431              result in garbage collection.  We must treat this
12432              situation as if we were within the body of function so as
12433              to avoid collecting live data on the stack.  */
12434           ++function_depth;
12435           /* Parse the assignment-expression.  */
12436           if (template_parm_p)
12437             push_deferring_access_checks (dk_no_deferred);
12438           default_argument
12439             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12440           if (template_parm_p)
12441             pop_deferring_access_checks ();
12442           /* Restore saved state.  */
12443           --function_depth;
12444           parser->greater_than_is_operator_p
12445             = saved_greater_than_is_operator_p;
12446           parser->local_variables_forbidden_p
12447             = saved_local_variables_forbidden_p;
12448         }
12449       if (!parser->default_arg_ok_p)
12450         {
12451           if (!flag_pedantic_errors)
12452             warning (0, "deprecated use of default argument for parameter of non-function");
12453           else
12454             {
12455               error ("default arguments are only permitted for function parameters");
12456               default_argument = NULL_TREE;
12457             }
12458         }
12459     }
12460   else
12461     default_argument = NULL_TREE;
12462
12463   return make_parameter_declarator (&decl_specifiers,
12464                                     declarator,
12465                                     default_argument);
12466 }
12467
12468 /* Parse a function-body.
12469
12470    function-body:
12471      compound_statement  */
12472
12473 static void
12474 cp_parser_function_body (cp_parser *parser)
12475 {
12476   cp_parser_compound_statement (parser, NULL, false);
12477 }
12478
12479 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12480    true if a ctor-initializer was present.  */
12481
12482 static bool
12483 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12484 {
12485   tree body;
12486   bool ctor_initializer_p;
12487
12488   /* Begin the function body.  */
12489   body = begin_function_body ();
12490   /* Parse the optional ctor-initializer.  */
12491   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12492   /* Parse the function-body.  */
12493   cp_parser_function_body (parser);
12494   /* Finish the function body.  */
12495   finish_function_body (body);
12496
12497   return ctor_initializer_p;
12498 }
12499
12500 /* Parse an initializer.
12501
12502    initializer:
12503      = initializer-clause
12504      ( expression-list )
12505
12506    Returns an expression representing the initializer.  If no
12507    initializer is present, NULL_TREE is returned.
12508
12509    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12510    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12511    set to FALSE if there is no initializer present.  If there is an
12512    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12513    is set to true; otherwise it is set to false.  */
12514
12515 static tree
12516 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12517                        bool* non_constant_p)
12518 {
12519   cp_token *token;
12520   tree init;
12521
12522   /* Peek at the next token.  */
12523   token = cp_lexer_peek_token (parser->lexer);
12524
12525   /* Let our caller know whether or not this initializer was
12526      parenthesized.  */
12527   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12528   /* Assume that the initializer is constant.  */
12529   *non_constant_p = false;
12530
12531   if (token->type == CPP_EQ)
12532     {
12533       /* Consume the `='.  */
12534       cp_lexer_consume_token (parser->lexer);
12535       /* Parse the initializer-clause.  */
12536       init = cp_parser_initializer_clause (parser, non_constant_p);
12537     }
12538   else if (token->type == CPP_OPEN_PAREN)
12539     init = cp_parser_parenthesized_expression_list (parser, false,
12540                                                     /*cast_p=*/false,
12541                                                     non_constant_p);
12542   else
12543     {
12544       /* Anything else is an error.  */
12545       cp_parser_error (parser, "expected initializer");
12546       init = error_mark_node;
12547     }
12548
12549   return init;
12550 }
12551
12552 /* Parse an initializer-clause.
12553
12554    initializer-clause:
12555      assignment-expression
12556      { initializer-list , [opt] }
12557      { }
12558
12559    Returns an expression representing the initializer.
12560
12561    If the `assignment-expression' production is used the value
12562    returned is simply a representation for the expression.
12563
12564    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12565    the elements of the initializer-list (or NULL, if the last
12566    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12567    NULL_TREE.  There is no way to detect whether or not the optional
12568    trailing `,' was provided.  NON_CONSTANT_P is as for
12569    cp_parser_initializer.  */
12570
12571 static tree
12572 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12573 {
12574   tree initializer;
12575
12576   /* Assume the expression is constant.  */
12577   *non_constant_p = false;
12578
12579   /* If it is not a `{', then we are looking at an
12580      assignment-expression.  */
12581   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12582     {
12583       initializer
12584         = cp_parser_constant_expression (parser,
12585                                         /*allow_non_constant_p=*/true,
12586                                         non_constant_p);
12587       if (!*non_constant_p)
12588         initializer = fold_non_dependent_expr (initializer);
12589     }
12590   else
12591     {
12592       /* Consume the `{' token.  */
12593       cp_lexer_consume_token (parser->lexer);
12594       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12595       initializer = make_node (CONSTRUCTOR);
12596       /* If it's not a `}', then there is a non-trivial initializer.  */
12597       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12598         {
12599           /* Parse the initializer list.  */
12600           CONSTRUCTOR_ELTS (initializer)
12601             = cp_parser_initializer_list (parser, non_constant_p);
12602           /* A trailing `,' token is allowed.  */
12603           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12604             cp_lexer_consume_token (parser->lexer);
12605         }
12606       /* Now, there should be a trailing `}'.  */
12607       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12608     }
12609
12610   return initializer;
12611 }
12612
12613 /* Parse an initializer-list.
12614
12615    initializer-list:
12616      initializer-clause
12617      initializer-list , initializer-clause
12618
12619    GNU Extension:
12620
12621    initializer-list:
12622      identifier : initializer-clause
12623      initializer-list, identifier : initializer-clause
12624
12625    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12626    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12627    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12628    as for cp_parser_initializer.  */
12629
12630 static VEC(constructor_elt,gc) *
12631 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12632 {
12633   VEC(constructor_elt,gc) *v = NULL;
12634
12635   /* Assume all of the expressions are constant.  */
12636   *non_constant_p = false;
12637
12638   /* Parse the rest of the list.  */
12639   while (true)
12640     {
12641       cp_token *token;
12642       tree identifier;
12643       tree initializer;
12644       bool clause_non_constant_p;
12645
12646       /* If the next token is an identifier and the following one is a
12647          colon, we are looking at the GNU designated-initializer
12648          syntax.  */
12649       if (cp_parser_allow_gnu_extensions_p (parser)
12650           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12651           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12652         {
12653           /* Consume the identifier.  */
12654           identifier = cp_lexer_consume_token (parser->lexer)->value;
12655           /* Consume the `:'.  */
12656           cp_lexer_consume_token (parser->lexer);
12657         }
12658       else
12659         identifier = NULL_TREE;
12660
12661       /* Parse the initializer.  */
12662       initializer = cp_parser_initializer_clause (parser,
12663                                                   &clause_non_constant_p);
12664       /* If any clause is non-constant, so is the entire initializer.  */
12665       if (clause_non_constant_p)
12666         *non_constant_p = true;
12667
12668       /* Add it to the vector.  */
12669       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12670
12671       /* If the next token is not a comma, we have reached the end of
12672          the list.  */
12673       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12674         break;
12675
12676       /* Peek at the next token.  */
12677       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12678       /* If the next token is a `}', then we're still done.  An
12679          initializer-clause can have a trailing `,' after the
12680          initializer-list and before the closing `}'.  */
12681       if (token->type == CPP_CLOSE_BRACE)
12682         break;
12683
12684       /* Consume the `,' token.  */
12685       cp_lexer_consume_token (parser->lexer);
12686     }
12687
12688   return v;
12689 }
12690
12691 /* Classes [gram.class] */
12692
12693 /* Parse a class-name.
12694
12695    class-name:
12696      identifier
12697      template-id
12698
12699    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12700    to indicate that names looked up in dependent types should be
12701    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12702    keyword has been used to indicate that the name that appears next
12703    is a template.  TAG_TYPE indicates the explicit tag given before
12704    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12705    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12706    is the class being defined in a class-head.
12707
12708    Returns the TYPE_DECL representing the class.  */
12709
12710 static tree
12711 cp_parser_class_name (cp_parser *parser,
12712                       bool typename_keyword_p,
12713                       bool template_keyword_p,
12714                       enum tag_types tag_type,
12715                       bool check_dependency_p,
12716                       bool class_head_p,
12717                       bool is_declaration)
12718 {
12719   tree decl;
12720   tree scope;
12721   bool typename_p;
12722   cp_token *token;
12723
12724   /* All class-names start with an identifier.  */
12725   token = cp_lexer_peek_token (parser->lexer);
12726   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12727     {
12728       cp_parser_error (parser, "expected class-name");
12729       return error_mark_node;
12730     }
12731
12732   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12733      to a template-id, so we save it here.  */
12734   scope = parser->scope;
12735   if (scope == error_mark_node)
12736     return error_mark_node;
12737
12738   /* Any name names a type if we're following the `typename' keyword
12739      in a qualified name where the enclosing scope is type-dependent.  */
12740   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12741                 && dependent_type_p (scope));
12742   /* Handle the common case (an identifier, but not a template-id)
12743      efficiently.  */
12744   if (token->type == CPP_NAME
12745       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12746     {
12747       cp_token *identifier_token;
12748       tree identifier;
12749       bool ambiguous_p;
12750
12751       /* Look for the identifier.  */
12752       identifier_token = cp_lexer_peek_token (parser->lexer);
12753       ambiguous_p = identifier_token->ambiguous_p;
12754       identifier = cp_parser_identifier (parser);
12755       /* If the next token isn't an identifier, we are certainly not
12756          looking at a class-name.  */
12757       if (identifier == error_mark_node)
12758         decl = error_mark_node;
12759       /* If we know this is a type-name, there's no need to look it
12760          up.  */
12761       else if (typename_p)
12762         decl = identifier;
12763       else
12764         {
12765           tree ambiguous_decls;
12766           /* If we already know that this lookup is ambiguous, then
12767              we've already issued an error message; there's no reason
12768              to check again.  */
12769           if (ambiguous_p)
12770             {
12771               cp_parser_simulate_error (parser);
12772               return error_mark_node;
12773             }
12774           /* If the next token is a `::', then the name must be a type
12775              name.
12776
12777              [basic.lookup.qual]
12778
12779              During the lookup for a name preceding the :: scope
12780              resolution operator, object, function, and enumerator
12781              names are ignored.  */
12782           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12783             tag_type = typename_type;
12784           /* Look up the name.  */
12785           decl = cp_parser_lookup_name (parser, identifier,
12786                                         tag_type,
12787                                         /*is_template=*/false,
12788                                         /*is_namespace=*/false,
12789                                         check_dependency_p,
12790                                         &ambiguous_decls);
12791           if (ambiguous_decls)
12792             {
12793               error ("reference to %qD is ambiguous", identifier);
12794               print_candidates (ambiguous_decls);
12795               if (cp_parser_parsing_tentatively (parser))
12796                 {
12797                   identifier_token->ambiguous_p = true;
12798                   cp_parser_simulate_error (parser);
12799                 }
12800               return error_mark_node;
12801             }
12802         }
12803     }
12804   else
12805     {
12806       /* Try a template-id.  */
12807       decl = cp_parser_template_id (parser, template_keyword_p,
12808                                     check_dependency_p,
12809                                     is_declaration);
12810       if (decl == error_mark_node)
12811         return error_mark_node;
12812     }
12813
12814   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12815
12816   /* If this is a typename, create a TYPENAME_TYPE.  */
12817   if (typename_p && decl != error_mark_node)
12818     {
12819       decl = make_typename_type (scope, decl, typename_type,
12820                                  /*complain=*/tf_error);
12821       if (decl != error_mark_node)
12822         decl = TYPE_NAME (decl);
12823     }
12824
12825   /* Check to see that it is really the name of a class.  */
12826   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12827       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12828       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12829     /* Situations like this:
12830
12831          template <typename T> struct A {
12832            typename T::template X<int>::I i;
12833          };
12834
12835        are problematic.  Is `T::template X<int>' a class-name?  The
12836        standard does not seem to be definitive, but there is no other
12837        valid interpretation of the following `::'.  Therefore, those
12838        names are considered class-names.  */
12839     {
12840       decl = make_typename_type (scope, decl, tag_type, tf_error);
12841       if (decl != error_mark_node)
12842         decl = TYPE_NAME (decl);
12843     }
12844   else if (TREE_CODE (decl) != TYPE_DECL
12845            || TREE_TYPE (decl) == error_mark_node
12846            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12847     decl = error_mark_node;
12848
12849   if (decl == error_mark_node)
12850     cp_parser_error (parser, "expected class-name");
12851
12852   return decl;
12853 }
12854
12855 /* Parse a class-specifier.
12856
12857    class-specifier:
12858      class-head { member-specification [opt] }
12859
12860    Returns the TREE_TYPE representing the class.  */
12861
12862 static tree
12863 cp_parser_class_specifier (cp_parser* parser)
12864 {
12865   cp_token *token;
12866   tree type;
12867   tree attributes = NULL_TREE;
12868   int has_trailing_semicolon;
12869   bool nested_name_specifier_p;
12870   unsigned saved_num_template_parameter_lists;
12871   tree old_scope = NULL_TREE;
12872   tree scope = NULL_TREE;
12873
12874   push_deferring_access_checks (dk_no_deferred);
12875
12876   /* Parse the class-head.  */
12877   type = cp_parser_class_head (parser,
12878                                &nested_name_specifier_p,
12879                                &attributes);
12880   /* If the class-head was a semantic disaster, skip the entire body
12881      of the class.  */
12882   if (!type)
12883     {
12884       cp_parser_skip_to_end_of_block_or_statement (parser);
12885       pop_deferring_access_checks ();
12886       return error_mark_node;
12887     }
12888
12889   /* Look for the `{'.  */
12890   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12891     {
12892       pop_deferring_access_checks ();
12893       return error_mark_node;
12894     }
12895
12896   /* Issue an error message if type-definitions are forbidden here.  */
12897   cp_parser_check_type_definition (parser);
12898   /* Remember that we are defining one more class.  */
12899   ++parser->num_classes_being_defined;
12900   /* Inside the class, surrounding template-parameter-lists do not
12901      apply.  */
12902   saved_num_template_parameter_lists
12903     = parser->num_template_parameter_lists;
12904   parser->num_template_parameter_lists = 0;
12905
12906   /* Start the class.  */
12907   if (nested_name_specifier_p)
12908     {
12909       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12910       old_scope = push_inner_scope (scope);
12911     }
12912   type = begin_class_definition (type);
12913
12914   if (type == error_mark_node)
12915     /* If the type is erroneous, skip the entire body of the class.  */
12916     cp_parser_skip_to_closing_brace (parser);
12917   else
12918     /* Parse the member-specification.  */
12919     cp_parser_member_specification_opt (parser);
12920
12921   /* Look for the trailing `}'.  */
12922   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12923   /* We get better error messages by noticing a common problem: a
12924      missing trailing `;'.  */
12925   token = cp_lexer_peek_token (parser->lexer);
12926   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12927   /* Look for trailing attributes to apply to this class.  */
12928   if (cp_parser_allow_gnu_extensions_p (parser))
12929     {
12930       tree sub_attr = cp_parser_attributes_opt (parser);
12931       attributes = chainon (attributes, sub_attr);
12932     }
12933   if (type != error_mark_node)
12934     type = finish_struct (type, attributes);
12935   if (nested_name_specifier_p)
12936     pop_inner_scope (old_scope, scope);
12937   /* If this class is not itself within the scope of another class,
12938      then we need to parse the bodies of all of the queued function
12939      definitions.  Note that the queued functions defined in a class
12940      are not always processed immediately following the
12941      class-specifier for that class.  Consider:
12942
12943        struct A {
12944          struct B { void f() { sizeof (A); } };
12945        };
12946
12947      If `f' were processed before the processing of `A' were
12948      completed, there would be no way to compute the size of `A'.
12949      Note that the nesting we are interested in here is lexical --
12950      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12951      for:
12952
12953        struct A { struct B; };
12954        struct A::B { void f() { } };
12955
12956      there is no need to delay the parsing of `A::B::f'.  */
12957   if (--parser->num_classes_being_defined == 0)
12958     {
12959       tree queue_entry;
12960       tree fn;
12961       tree class_type = NULL_TREE;
12962       tree pushed_scope = NULL_TREE;
12963  
12964       /* In a first pass, parse default arguments to the functions.
12965          Then, in a second pass, parse the bodies of the functions.
12966          This two-phased approach handles cases like:
12967
12968             struct S {
12969               void f() { g(); }
12970               void g(int i = 3);
12971             };
12972
12973          */
12974       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12975              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12976            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12977            TREE_PURPOSE (parser->unparsed_functions_queues)
12978              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12979         {
12980           fn = TREE_VALUE (queue_entry);
12981           /* If there are default arguments that have not yet been processed,
12982              take care of them now.  */
12983           if (class_type != TREE_PURPOSE (queue_entry))
12984             {
12985               if (pushed_scope)
12986                 pop_scope (pushed_scope);
12987               class_type = TREE_PURPOSE (queue_entry);
12988               pushed_scope = push_scope (class_type);
12989             }
12990           /* Make sure that any template parameters are in scope.  */
12991           maybe_begin_member_template_processing (fn);
12992           /* Parse the default argument expressions.  */
12993           cp_parser_late_parsing_default_args (parser, fn);
12994           /* Remove any template parameters from the symbol table.  */
12995           maybe_end_member_template_processing ();
12996         }
12997       if (pushed_scope)
12998         pop_scope (pushed_scope);
12999       /* Now parse the body of the functions.  */
13000       for (TREE_VALUE (parser->unparsed_functions_queues)
13001              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13002            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13003            TREE_VALUE (parser->unparsed_functions_queues)
13004              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13005         {
13006           /* Figure out which function we need to process.  */
13007           fn = TREE_VALUE (queue_entry);
13008           /* Parse the function.  */
13009           cp_parser_late_parsing_for_member (parser, fn);
13010         }
13011     }
13012
13013   /* Put back any saved access checks.  */
13014   pop_deferring_access_checks ();
13015
13016   /* Restore the count of active template-parameter-lists.  */
13017   parser->num_template_parameter_lists
13018     = saved_num_template_parameter_lists;
13019
13020   return type;
13021 }
13022
13023 /* Parse a class-head.
13024
13025    class-head:
13026      class-key identifier [opt] base-clause [opt]
13027      class-key nested-name-specifier identifier base-clause [opt]
13028      class-key nested-name-specifier [opt] template-id
13029        base-clause [opt]
13030
13031    GNU Extensions:
13032      class-key attributes identifier [opt] base-clause [opt]
13033      class-key attributes nested-name-specifier identifier base-clause [opt]
13034      class-key attributes nested-name-specifier [opt] template-id
13035        base-clause [opt]
13036
13037    Returns the TYPE of the indicated class.  Sets
13038    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13039    involving a nested-name-specifier was used, and FALSE otherwise.
13040
13041    Returns error_mark_node if this is not a class-head.
13042
13043    Returns NULL_TREE if the class-head is syntactically valid, but
13044    semantically invalid in a way that means we should skip the entire
13045    body of the class.  */
13046
13047 static tree
13048 cp_parser_class_head (cp_parser* parser,
13049                       bool* nested_name_specifier_p,
13050                       tree *attributes_p)
13051 {
13052   tree nested_name_specifier;
13053   enum tag_types class_key;
13054   tree id = NULL_TREE;
13055   tree type = NULL_TREE;
13056   tree attributes;
13057   bool template_id_p = false;
13058   bool qualified_p = false;
13059   bool invalid_nested_name_p = false;
13060   bool invalid_explicit_specialization_p = false;
13061   tree pushed_scope = NULL_TREE;
13062   unsigned num_templates;
13063   tree bases;
13064
13065   /* Assume no nested-name-specifier will be present.  */
13066   *nested_name_specifier_p = false;
13067   /* Assume no template parameter lists will be used in defining the
13068      type.  */
13069   num_templates = 0;
13070
13071   /* Look for the class-key.  */
13072   class_key = cp_parser_class_key (parser);
13073   if (class_key == none_type)
13074     return error_mark_node;
13075
13076   /* Parse the attributes.  */
13077   attributes = cp_parser_attributes_opt (parser);
13078
13079   /* If the next token is `::', that is invalid -- but sometimes
13080      people do try to write:
13081
13082        struct ::S {};
13083
13084      Handle this gracefully by accepting the extra qualifier, and then
13085      issuing an error about it later if this really is a
13086      class-head.  If it turns out just to be an elaborated type
13087      specifier, remain silent.  */
13088   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13089     qualified_p = true;
13090
13091   push_deferring_access_checks (dk_no_check);
13092
13093   /* Determine the name of the class.  Begin by looking for an
13094      optional nested-name-specifier.  */
13095   nested_name_specifier
13096     = cp_parser_nested_name_specifier_opt (parser,
13097                                            /*typename_keyword_p=*/false,
13098                                            /*check_dependency_p=*/false,
13099                                            /*type_p=*/false,
13100                                            /*is_declaration=*/false);
13101   /* If there was a nested-name-specifier, then there *must* be an
13102      identifier.  */
13103   if (nested_name_specifier)
13104     {
13105       /* Although the grammar says `identifier', it really means
13106          `class-name' or `template-name'.  You are only allowed to
13107          define a class that has already been declared with this
13108          syntax.
13109
13110          The proposed resolution for Core Issue 180 says that whever
13111          you see `class T::X' you should treat `X' as a type-name.
13112
13113          It is OK to define an inaccessible class; for example:
13114
13115            class A { class B; };
13116            class A::B {};
13117
13118          We do not know if we will see a class-name, or a
13119          template-name.  We look for a class-name first, in case the
13120          class-name is a template-id; if we looked for the
13121          template-name first we would stop after the template-name.  */
13122       cp_parser_parse_tentatively (parser);
13123       type = cp_parser_class_name (parser,
13124                                    /*typename_keyword_p=*/false,
13125                                    /*template_keyword_p=*/false,
13126                                    class_type,
13127                                    /*check_dependency_p=*/false,
13128                                    /*class_head_p=*/true,
13129                                    /*is_declaration=*/false);
13130       /* If that didn't work, ignore the nested-name-specifier.  */
13131       if (!cp_parser_parse_definitely (parser))
13132         {
13133           invalid_nested_name_p = true;
13134           id = cp_parser_identifier (parser);
13135           if (id == error_mark_node)
13136             id = NULL_TREE;
13137         }
13138       /* If we could not find a corresponding TYPE, treat this
13139          declaration like an unqualified declaration.  */
13140       if (type == error_mark_node)
13141         nested_name_specifier = NULL_TREE;
13142       /* Otherwise, count the number of templates used in TYPE and its
13143          containing scopes.  */
13144       else
13145         {
13146           tree scope;
13147
13148           for (scope = TREE_TYPE (type);
13149                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13150                scope = (TYPE_P (scope)
13151                         ? TYPE_CONTEXT (scope)
13152                         : DECL_CONTEXT (scope)))
13153             if (TYPE_P (scope)
13154                 && CLASS_TYPE_P (scope)
13155                 && CLASSTYPE_TEMPLATE_INFO (scope)
13156                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13157                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13158               ++num_templates;
13159         }
13160     }
13161   /* Otherwise, the identifier is optional.  */
13162   else
13163     {
13164       /* We don't know whether what comes next is a template-id,
13165          an identifier, or nothing at all.  */
13166       cp_parser_parse_tentatively (parser);
13167       /* Check for a template-id.  */
13168       id = cp_parser_template_id (parser,
13169                                   /*template_keyword_p=*/false,
13170                                   /*check_dependency_p=*/true,
13171                                   /*is_declaration=*/true);
13172       /* If that didn't work, it could still be an identifier.  */
13173       if (!cp_parser_parse_definitely (parser))
13174         {
13175           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13176             id = cp_parser_identifier (parser);
13177           else
13178             id = NULL_TREE;
13179         }
13180       else
13181         {
13182           template_id_p = true;
13183           ++num_templates;
13184         }
13185     }
13186
13187   pop_deferring_access_checks ();
13188
13189   if (id)
13190     cp_parser_check_for_invalid_template_id (parser, id);
13191
13192   /* If it's not a `:' or a `{' then we can't really be looking at a
13193      class-head, since a class-head only appears as part of a
13194      class-specifier.  We have to detect this situation before calling
13195      xref_tag, since that has irreversible side-effects.  */
13196   if (!cp_parser_next_token_starts_class_definition_p (parser))
13197     {
13198       cp_parser_error (parser, "expected %<{%> or %<:%>");
13199       return error_mark_node;
13200     }
13201
13202   /* At this point, we're going ahead with the class-specifier, even
13203      if some other problem occurs.  */
13204   cp_parser_commit_to_tentative_parse (parser);
13205   /* Issue the error about the overly-qualified name now.  */
13206   if (qualified_p)
13207     cp_parser_error (parser,
13208                      "global qualification of class name is invalid");
13209   else if (invalid_nested_name_p)
13210     cp_parser_error (parser,
13211                      "qualified name does not name a class");
13212   else if (nested_name_specifier)
13213     {
13214       tree scope;
13215
13216       /* Reject typedef-names in class heads.  */
13217       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13218         {
13219           error ("invalid class name in declaration of %qD", type);
13220           type = NULL_TREE;
13221           goto done;
13222         }
13223
13224       /* Figure out in what scope the declaration is being placed.  */
13225       scope = current_scope ();
13226       /* If that scope does not contain the scope in which the
13227          class was originally declared, the program is invalid.  */
13228       if (scope && !is_ancestor (scope, nested_name_specifier))
13229         {
13230           error ("declaration of %qD in %qD which does not enclose %qD",
13231                  type, scope, nested_name_specifier);
13232           type = NULL_TREE;
13233           goto done;
13234         }
13235       /* [dcl.meaning]
13236
13237          A declarator-id shall not be qualified exception of the
13238          definition of a ... nested class outside of its class
13239          ... [or] a the definition or explicit instantiation of a
13240          class member of a namespace outside of its namespace.  */
13241       if (scope == nested_name_specifier)
13242         {
13243           pedwarn ("extra qualification ignored");
13244           nested_name_specifier = NULL_TREE;
13245           num_templates = 0;
13246         }
13247     }
13248   /* An explicit-specialization must be preceded by "template <>".  If
13249      it is not, try to recover gracefully.  */
13250   if (at_namespace_scope_p ()
13251       && parser->num_template_parameter_lists == 0
13252       && template_id_p)
13253     {
13254       error ("an explicit specialization must be preceded by %<template <>%>");
13255       invalid_explicit_specialization_p = true;
13256       /* Take the same action that would have been taken by
13257          cp_parser_explicit_specialization.  */
13258       ++parser->num_template_parameter_lists;
13259       begin_specialization ();
13260     }
13261   /* There must be no "return" statements between this point and the
13262      end of this function; set "type "to the correct return value and
13263      use "goto done;" to return.  */
13264   /* Make sure that the right number of template parameters were
13265      present.  */
13266   if (!cp_parser_check_template_parameters (parser, num_templates))
13267     {
13268       /* If something went wrong, there is no point in even trying to
13269          process the class-definition.  */
13270       type = NULL_TREE;
13271       goto done;
13272     }
13273
13274   /* Look up the type.  */
13275   if (template_id_p)
13276     {
13277       type = TREE_TYPE (id);
13278       maybe_process_partial_specialization (type);
13279       if (nested_name_specifier)
13280         pushed_scope = push_scope (nested_name_specifier);
13281     }
13282   else if (nested_name_specifier)
13283     {
13284       tree class_type;
13285
13286       /* Given:
13287
13288             template <typename T> struct S { struct T };
13289             template <typename T> struct S<T>::T { };
13290
13291          we will get a TYPENAME_TYPE when processing the definition of
13292          `S::T'.  We need to resolve it to the actual type before we
13293          try to define it.  */
13294       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13295         {
13296           class_type = resolve_typename_type (TREE_TYPE (type),
13297                                               /*only_current_p=*/false);
13298           if (class_type != error_mark_node)
13299             type = TYPE_NAME (class_type);
13300           else
13301             {
13302               cp_parser_error (parser, "could not resolve typename type");
13303               type = error_mark_node;
13304             }
13305         }
13306
13307       maybe_process_partial_specialization (TREE_TYPE (type));
13308       class_type = current_class_type;
13309       /* Enter the scope indicated by the nested-name-specifier.  */
13310       pushed_scope = push_scope (nested_name_specifier);
13311       /* Get the canonical version of this type.  */
13312       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13313       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13314           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13315         {
13316           type = push_template_decl (type);
13317           if (type == error_mark_node)
13318             {
13319               type = NULL_TREE;
13320               goto done;
13321             }
13322         }
13323
13324       type = TREE_TYPE (type);
13325       *nested_name_specifier_p = true;
13326     }
13327   else      /* The name is not a nested name.  */
13328     {
13329       /* If the class was unnamed, create a dummy name.  */
13330       if (!id)
13331         id = make_anon_name ();
13332       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13333                        parser->num_template_parameter_lists);
13334     }
13335
13336   /* Indicate whether this class was declared as a `class' or as a
13337      `struct'.  */
13338   if (TREE_CODE (type) == RECORD_TYPE)
13339     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13340   cp_parser_check_class_key (class_key, type);
13341
13342   /* If this type was already complete, and we see another definition,
13343      that's an error.  */
13344   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13345     {
13346       error ("redefinition of %q#T", type);
13347       error ("previous definition of %q+#T", type);
13348       type = NULL_TREE;
13349       goto done;
13350     }
13351
13352   /* We will have entered the scope containing the class; the names of
13353      base classes should be looked up in that context.  For example:
13354
13355        struct A { struct B {}; struct C; };
13356        struct A::C : B {};
13357
13358      is valid.  */
13359   bases = NULL_TREE;
13360
13361   /* Get the list of base-classes, if there is one.  */
13362   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13363     bases = cp_parser_base_clause (parser);
13364
13365   /* Process the base classes.  */
13366   xref_basetypes (type, bases);
13367
13368  done:
13369   /* Leave the scope given by the nested-name-specifier.  We will
13370      enter the class scope itself while processing the members.  */
13371   if (pushed_scope)
13372     pop_scope (pushed_scope);
13373
13374   if (invalid_explicit_specialization_p)
13375     {
13376       end_specialization ();
13377       --parser->num_template_parameter_lists;
13378     }
13379   *attributes_p = attributes;
13380   return type;
13381 }
13382
13383 /* Parse a class-key.
13384
13385    class-key:
13386      class
13387      struct
13388      union
13389
13390    Returns the kind of class-key specified, or none_type to indicate
13391    error.  */
13392
13393 static enum tag_types
13394 cp_parser_class_key (cp_parser* parser)
13395 {
13396   cp_token *token;
13397   enum tag_types tag_type;
13398
13399   /* Look for the class-key.  */
13400   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13401   if (!token)
13402     return none_type;
13403
13404   /* Check to see if the TOKEN is a class-key.  */
13405   tag_type = cp_parser_token_is_class_key (token);
13406   if (!tag_type)
13407     cp_parser_error (parser, "expected class-key");
13408   return tag_type;
13409 }
13410
13411 /* Parse an (optional) member-specification.
13412
13413    member-specification:
13414      member-declaration member-specification [opt]
13415      access-specifier : member-specification [opt]  */
13416
13417 static void
13418 cp_parser_member_specification_opt (cp_parser* parser)
13419 {
13420   while (true)
13421     {
13422       cp_token *token;
13423       enum rid keyword;
13424
13425       /* Peek at the next token.  */
13426       token = cp_lexer_peek_token (parser->lexer);
13427       /* If it's a `}', or EOF then we've seen all the members.  */
13428       if (token->type == CPP_CLOSE_BRACE
13429           || token->type == CPP_EOF
13430           || token->type == CPP_PRAGMA_EOL)
13431         break;
13432
13433       /* See if this token is a keyword.  */
13434       keyword = token->keyword;
13435       switch (keyword)
13436         {
13437         case RID_PUBLIC:
13438         case RID_PROTECTED:
13439         case RID_PRIVATE:
13440           /* Consume the access-specifier.  */
13441           cp_lexer_consume_token (parser->lexer);
13442           /* Remember which access-specifier is active.  */
13443           current_access_specifier = token->value;
13444           /* Look for the `:'.  */
13445           cp_parser_require (parser, CPP_COLON, "`:'");
13446           break;
13447
13448         default:
13449           /* Accept #pragmas at class scope.  */
13450           if (token->type == CPP_PRAGMA)
13451             {
13452               cp_parser_pragma (parser, pragma_external);
13453               break;
13454             }
13455
13456           /* Otherwise, the next construction must be a
13457              member-declaration.  */
13458           cp_parser_member_declaration (parser);
13459         }
13460     }
13461 }
13462
13463 /* Parse a member-declaration.
13464
13465    member-declaration:
13466      decl-specifier-seq [opt] member-declarator-list [opt] ;
13467      function-definition ; [opt]
13468      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13469      using-declaration
13470      template-declaration
13471
13472    member-declarator-list:
13473      member-declarator
13474      member-declarator-list , member-declarator
13475
13476    member-declarator:
13477      declarator pure-specifier [opt]
13478      declarator constant-initializer [opt]
13479      identifier [opt] : constant-expression
13480
13481    GNU Extensions:
13482
13483    member-declaration:
13484      __extension__ member-declaration
13485
13486    member-declarator:
13487      declarator attributes [opt] pure-specifier [opt]
13488      declarator attributes [opt] constant-initializer [opt]
13489      identifier [opt] attributes [opt] : constant-expression  */
13490
13491 static void
13492 cp_parser_member_declaration (cp_parser* parser)
13493 {
13494   cp_decl_specifier_seq decl_specifiers;
13495   tree prefix_attributes;
13496   tree decl;
13497   int declares_class_or_enum;
13498   bool friend_p;
13499   cp_token *token;
13500   int saved_pedantic;
13501
13502   /* Check for the `__extension__' keyword.  */
13503   if (cp_parser_extension_opt (parser, &saved_pedantic))
13504     {
13505       /* Recurse.  */
13506       cp_parser_member_declaration (parser);
13507       /* Restore the old value of the PEDANTIC flag.  */
13508       pedantic = saved_pedantic;
13509
13510       return;
13511     }
13512
13513   /* Check for a template-declaration.  */
13514   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13515     {
13516       /* An explicit specialization here is an error condition, and we
13517          expect the specialization handler to detect and report this.  */
13518       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13519           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13520         cp_parser_explicit_specialization (parser);
13521       else
13522         cp_parser_template_declaration (parser, /*member_p=*/true);
13523
13524       return;
13525     }
13526
13527   /* Check for a using-declaration.  */
13528   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13529     {
13530       /* Parse the using-declaration.  */
13531       cp_parser_using_declaration (parser);
13532
13533       return;
13534     }
13535
13536   /* Check for @defs.  */
13537   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13538     {
13539       tree ivar, member;
13540       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13541       ivar = ivar_chains;
13542       while (ivar)
13543         {
13544           member = ivar;
13545           ivar = TREE_CHAIN (member);
13546           TREE_CHAIN (member) = NULL_TREE;
13547           finish_member_declaration (member);
13548         }
13549       return;
13550     }
13551
13552   /* Parse the decl-specifier-seq.  */
13553   cp_parser_decl_specifier_seq (parser,
13554                                 CP_PARSER_FLAGS_OPTIONAL,
13555                                 &decl_specifiers,
13556                                 &declares_class_or_enum);
13557   prefix_attributes = decl_specifiers.attributes;
13558   decl_specifiers.attributes = NULL_TREE;
13559   /* Check for an invalid type-name.  */
13560   if (!decl_specifiers.type
13561       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13562     return;
13563   /* If there is no declarator, then the decl-specifier-seq should
13564      specify a type.  */
13565   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13566     {
13567       /* If there was no decl-specifier-seq, and the next token is a
13568          `;', then we have something like:
13569
13570            struct S { ; };
13571
13572          [class.mem]
13573
13574          Each member-declaration shall declare at least one member
13575          name of the class.  */
13576       if (!decl_specifiers.any_specifiers_p)
13577         {
13578           cp_token *token = cp_lexer_peek_token (parser->lexer);
13579           if (pedantic && !token->in_system_header)
13580             pedwarn ("%Hextra %<;%>", &token->location);
13581         }
13582       else
13583         {
13584           tree type;
13585
13586           /* See if this declaration is a friend.  */
13587           friend_p = cp_parser_friend_p (&decl_specifiers);
13588           /* If there were decl-specifiers, check to see if there was
13589              a class-declaration.  */
13590           type = check_tag_decl (&decl_specifiers);
13591           /* Nested classes have already been added to the class, but
13592              a `friend' needs to be explicitly registered.  */
13593           if (friend_p)
13594             {
13595               /* If the `friend' keyword was present, the friend must
13596                  be introduced with a class-key.  */
13597                if (!declares_class_or_enum)
13598                  error ("a class-key must be used when declaring a friend");
13599                /* In this case:
13600
13601                     template <typename T> struct A {
13602                       friend struct A<T>::B;
13603                     };
13604
13605                   A<T>::B will be represented by a TYPENAME_TYPE, and
13606                   therefore not recognized by check_tag_decl.  */
13607                if (!type
13608                    && decl_specifiers.type
13609                    && TYPE_P (decl_specifiers.type))
13610                  type = decl_specifiers.type;
13611                if (!type || !TYPE_P (type))
13612                  error ("friend declaration does not name a class or "
13613                         "function");
13614                else
13615                  make_friend_class (current_class_type, type,
13616                                     /*complain=*/true);
13617             }
13618           /* If there is no TYPE, an error message will already have
13619              been issued.  */
13620           else if (!type || type == error_mark_node)
13621             ;
13622           /* An anonymous aggregate has to be handled specially; such
13623              a declaration really declares a data member (with a
13624              particular type), as opposed to a nested class.  */
13625           else if (ANON_AGGR_TYPE_P (type))
13626             {
13627               /* Remove constructors and such from TYPE, now that we
13628                  know it is an anonymous aggregate.  */
13629               fixup_anonymous_aggr (type);
13630               /* And make the corresponding data member.  */
13631               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13632               /* Add it to the class.  */
13633               finish_member_declaration (decl);
13634             }
13635           else
13636             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13637         }
13638     }
13639   else
13640     {
13641       /* See if these declarations will be friends.  */
13642       friend_p = cp_parser_friend_p (&decl_specifiers);
13643
13644       /* Keep going until we hit the `;' at the end of the
13645          declaration.  */
13646       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13647         {
13648           tree attributes = NULL_TREE;
13649           tree first_attribute;
13650
13651           /* Peek at the next token.  */
13652           token = cp_lexer_peek_token (parser->lexer);
13653
13654           /* Check for a bitfield declaration.  */
13655           if (token->type == CPP_COLON
13656               || (token->type == CPP_NAME
13657                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13658                   == CPP_COLON))
13659             {
13660               tree identifier;
13661               tree width;
13662
13663               /* Get the name of the bitfield.  Note that we cannot just
13664                  check TOKEN here because it may have been invalidated by
13665                  the call to cp_lexer_peek_nth_token above.  */
13666               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13667                 identifier = cp_parser_identifier (parser);
13668               else
13669                 identifier = NULL_TREE;
13670
13671               /* Consume the `:' token.  */
13672               cp_lexer_consume_token (parser->lexer);
13673               /* Get the width of the bitfield.  */
13674               width
13675                 = cp_parser_constant_expression (parser,
13676                                                  /*allow_non_constant=*/false,
13677                                                  NULL);
13678
13679               /* Look for attributes that apply to the bitfield.  */
13680               attributes = cp_parser_attributes_opt (parser);
13681               /* Remember which attributes are prefix attributes and
13682                  which are not.  */
13683               first_attribute = attributes;
13684               /* Combine the attributes.  */
13685               attributes = chainon (prefix_attributes, attributes);
13686
13687               /* Create the bitfield declaration.  */
13688               decl = grokbitfield (identifier
13689                                    ? make_id_declarator (NULL_TREE,
13690                                                          identifier,
13691                                                          sfk_none)
13692                                    : NULL,
13693                                    &decl_specifiers,
13694                                    width);
13695               /* Apply the attributes.  */
13696               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13697             }
13698           else
13699             {
13700               cp_declarator *declarator;
13701               tree initializer;
13702               tree asm_specification;
13703               int ctor_dtor_or_conv_p;
13704
13705               /* Parse the declarator.  */
13706               declarator
13707                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13708                                         &ctor_dtor_or_conv_p,
13709                                         /*parenthesized_p=*/NULL,
13710                                         /*member_p=*/true);
13711
13712               /* If something went wrong parsing the declarator, make sure
13713                  that we at least consume some tokens.  */
13714               if (declarator == cp_error_declarator)
13715                 {
13716                   /* Skip to the end of the statement.  */
13717                   cp_parser_skip_to_end_of_statement (parser);
13718                   /* If the next token is not a semicolon, that is
13719                      probably because we just skipped over the body of
13720                      a function.  So, we consume a semicolon if
13721                      present, but do not issue an error message if it
13722                      is not present.  */
13723                   if (cp_lexer_next_token_is (parser->lexer,
13724                                               CPP_SEMICOLON))
13725                     cp_lexer_consume_token (parser->lexer);
13726                   return;
13727                 }
13728
13729               if (declares_class_or_enum & 2)
13730                 cp_parser_check_for_definition_in_return_type
13731                   (declarator, decl_specifiers.type);
13732
13733               /* Look for an asm-specification.  */
13734               asm_specification = cp_parser_asm_specification_opt (parser);
13735               /* Look for attributes that apply to the declaration.  */
13736               attributes = cp_parser_attributes_opt (parser);
13737               /* Remember which attributes are prefix attributes and
13738                  which are not.  */
13739               first_attribute = attributes;
13740               /* Combine the attributes.  */
13741               attributes = chainon (prefix_attributes, attributes);
13742
13743               /* If it's an `=', then we have a constant-initializer or a
13744                  pure-specifier.  It is not correct to parse the
13745                  initializer before registering the member declaration
13746                  since the member declaration should be in scope while
13747                  its initializer is processed.  However, the rest of the
13748                  front end does not yet provide an interface that allows
13749                  us to handle this correctly.  */
13750               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13751                 {
13752                   /* In [class.mem]:
13753
13754                      A pure-specifier shall be used only in the declaration of
13755                      a virtual function.
13756
13757                      A member-declarator can contain a constant-initializer
13758                      only if it declares a static member of integral or
13759                      enumeration type.
13760
13761                      Therefore, if the DECLARATOR is for a function, we look
13762                      for a pure-specifier; otherwise, we look for a
13763                      constant-initializer.  When we call `grokfield', it will
13764                      perform more stringent semantics checks.  */
13765                   if (declarator->kind == cdk_function
13766                       && declarator->declarator->kind == cdk_id)
13767                     initializer = cp_parser_pure_specifier (parser);
13768                   else
13769                     /* Parse the initializer.  */
13770                     initializer = cp_parser_constant_initializer (parser);
13771                 }
13772               /* Otherwise, there is no initializer.  */
13773               else
13774                 initializer = NULL_TREE;
13775
13776               /* See if we are probably looking at a function
13777                  definition.  We are certainly not looking at a
13778                  member-declarator.  Calling `grokfield' has
13779                  side-effects, so we must not do it unless we are sure
13780                  that we are looking at a member-declarator.  */
13781               if (cp_parser_token_starts_function_definition_p
13782                   (cp_lexer_peek_token (parser->lexer)))
13783                 {
13784                   /* The grammar does not allow a pure-specifier to be
13785                      used when a member function is defined.  (It is
13786                      possible that this fact is an oversight in the
13787                      standard, since a pure function may be defined
13788                      outside of the class-specifier.  */
13789                   if (initializer)
13790                     error ("pure-specifier on function-definition");
13791                   decl = cp_parser_save_member_function_body (parser,
13792                                                               &decl_specifiers,
13793                                                               declarator,
13794                                                               attributes);
13795                   /* If the member was not a friend, declare it here.  */
13796                   if (!friend_p)
13797                     finish_member_declaration (decl);
13798                   /* Peek at the next token.  */
13799                   token = cp_lexer_peek_token (parser->lexer);
13800                   /* If the next token is a semicolon, consume it.  */
13801                   if (token->type == CPP_SEMICOLON)
13802                     cp_lexer_consume_token (parser->lexer);
13803                   return;
13804                 }
13805               else
13806                 /* Create the declaration.  */
13807                 decl = grokfield (declarator, &decl_specifiers,
13808                                   initializer, /*init_const_expr_p=*/true,
13809                                   asm_specification,
13810                                   attributes);
13811             }
13812
13813           /* Reset PREFIX_ATTRIBUTES.  */
13814           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13815             attributes = TREE_CHAIN (attributes);
13816           if (attributes)
13817             TREE_CHAIN (attributes) = NULL_TREE;
13818
13819           /* If there is any qualification still in effect, clear it
13820              now; we will be starting fresh with the next declarator.  */
13821           parser->scope = NULL_TREE;
13822           parser->qualifying_scope = NULL_TREE;
13823           parser->object_scope = NULL_TREE;
13824           /* If it's a `,', then there are more declarators.  */
13825           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13826             cp_lexer_consume_token (parser->lexer);
13827           /* If the next token isn't a `;', then we have a parse error.  */
13828           else if (cp_lexer_next_token_is_not (parser->lexer,
13829                                                CPP_SEMICOLON))
13830             {
13831               cp_parser_error (parser, "expected %<;%>");
13832               /* Skip tokens until we find a `;'.  */
13833               cp_parser_skip_to_end_of_statement (parser);
13834
13835               break;
13836             }
13837
13838           if (decl)
13839             {
13840               /* Add DECL to the list of members.  */
13841               if (!friend_p)
13842                 finish_member_declaration (decl);
13843
13844               if (TREE_CODE (decl) == FUNCTION_DECL)
13845                 cp_parser_save_default_args (parser, decl);
13846             }
13847         }
13848     }
13849
13850   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13851 }
13852
13853 /* Parse a pure-specifier.
13854
13855    pure-specifier:
13856      = 0
13857
13858    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13859    Otherwise, ERROR_MARK_NODE is returned.  */
13860
13861 static tree
13862 cp_parser_pure_specifier (cp_parser* parser)
13863 {
13864   cp_token *token;
13865
13866   /* Look for the `=' token.  */
13867   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13868     return error_mark_node;
13869   /* Look for the `0' token.  */
13870   token = cp_lexer_consume_token (parser->lexer);
13871   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13872   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13873     {
13874       cp_parser_error (parser, 
13875                        "invalid pure specifier (only `= 0' is allowed)");
13876       cp_parser_skip_to_end_of_statement (parser);
13877       return error_mark_node;
13878     }
13879   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13880     {
13881       error ("templates may not be %<virtual%>");
13882       return error_mark_node;
13883     }
13884
13885   return integer_zero_node;
13886 }
13887
13888 /* Parse a constant-initializer.
13889
13890    constant-initializer:
13891      = constant-expression
13892
13893    Returns a representation of the constant-expression.  */
13894
13895 static tree
13896 cp_parser_constant_initializer (cp_parser* parser)
13897 {
13898   /* Look for the `=' token.  */
13899   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13900     return error_mark_node;
13901
13902   /* It is invalid to write:
13903
13904        struct S { static const int i = { 7 }; };
13905
13906      */
13907   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13908     {
13909       cp_parser_error (parser,
13910                        "a brace-enclosed initializer is not allowed here");
13911       /* Consume the opening brace.  */
13912       cp_lexer_consume_token (parser->lexer);
13913       /* Skip the initializer.  */
13914       cp_parser_skip_to_closing_brace (parser);
13915       /* Look for the trailing `}'.  */
13916       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13917
13918       return error_mark_node;
13919     }
13920
13921   return cp_parser_constant_expression (parser,
13922                                         /*allow_non_constant=*/false,
13923                                         NULL);
13924 }
13925
13926 /* Derived classes [gram.class.derived] */
13927
13928 /* Parse a base-clause.
13929
13930    base-clause:
13931      : base-specifier-list
13932
13933    base-specifier-list:
13934      base-specifier
13935      base-specifier-list , base-specifier
13936
13937    Returns a TREE_LIST representing the base-classes, in the order in
13938    which they were declared.  The representation of each node is as
13939    described by cp_parser_base_specifier.
13940
13941    In the case that no bases are specified, this function will return
13942    NULL_TREE, not ERROR_MARK_NODE.  */
13943
13944 static tree
13945 cp_parser_base_clause (cp_parser* parser)
13946 {
13947   tree bases = NULL_TREE;
13948
13949   /* Look for the `:' that begins the list.  */
13950   cp_parser_require (parser, CPP_COLON, "`:'");
13951
13952   /* Scan the base-specifier-list.  */
13953   while (true)
13954     {
13955       cp_token *token;
13956       tree base;
13957
13958       /* Look for the base-specifier.  */
13959       base = cp_parser_base_specifier (parser);
13960       /* Add BASE to the front of the list.  */
13961       if (base != error_mark_node)
13962         {
13963           TREE_CHAIN (base) = bases;
13964           bases = base;
13965         }
13966       /* Peek at the next token.  */
13967       token = cp_lexer_peek_token (parser->lexer);
13968       /* If it's not a comma, then the list is complete.  */
13969       if (token->type != CPP_COMMA)
13970         break;
13971       /* Consume the `,'.  */
13972       cp_lexer_consume_token (parser->lexer);
13973     }
13974
13975   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13976      base class had a qualified name.  However, the next name that
13977      appears is certainly not qualified.  */
13978   parser->scope = NULL_TREE;
13979   parser->qualifying_scope = NULL_TREE;
13980   parser->object_scope = NULL_TREE;
13981
13982   return nreverse (bases);
13983 }
13984
13985 /* Parse a base-specifier.
13986
13987    base-specifier:
13988      :: [opt] nested-name-specifier [opt] class-name
13989      virtual access-specifier [opt] :: [opt] nested-name-specifier
13990        [opt] class-name
13991      access-specifier virtual [opt] :: [opt] nested-name-specifier
13992        [opt] class-name
13993
13994    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13995    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13996    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13997    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13998
13999 static tree
14000 cp_parser_base_specifier (cp_parser* parser)
14001 {
14002   cp_token *token;
14003   bool done = false;
14004   bool virtual_p = false;
14005   bool duplicate_virtual_error_issued_p = false;
14006   bool duplicate_access_error_issued_p = false;
14007   bool class_scope_p, template_p;
14008   tree access = access_default_node;
14009   tree type;
14010
14011   /* Process the optional `virtual' and `access-specifier'.  */
14012   while (!done)
14013     {
14014       /* Peek at the next token.  */
14015       token = cp_lexer_peek_token (parser->lexer);
14016       /* Process `virtual'.  */
14017       switch (token->keyword)
14018         {
14019         case RID_VIRTUAL:
14020           /* If `virtual' appears more than once, issue an error.  */
14021           if (virtual_p && !duplicate_virtual_error_issued_p)
14022             {
14023               cp_parser_error (parser,
14024                                "%<virtual%> specified more than once in base-specified");
14025               duplicate_virtual_error_issued_p = true;
14026             }
14027
14028           virtual_p = true;
14029
14030           /* Consume the `virtual' token.  */
14031           cp_lexer_consume_token (parser->lexer);
14032
14033           break;
14034
14035         case RID_PUBLIC:
14036         case RID_PROTECTED:
14037         case RID_PRIVATE:
14038           /* If more than one access specifier appears, issue an
14039              error.  */
14040           if (access != access_default_node
14041               && !duplicate_access_error_issued_p)
14042             {
14043               cp_parser_error (parser,
14044                                "more than one access specifier in base-specified");
14045               duplicate_access_error_issued_p = true;
14046             }
14047
14048           access = ridpointers[(int) token->keyword];
14049
14050           /* Consume the access-specifier.  */
14051           cp_lexer_consume_token (parser->lexer);
14052
14053           break;
14054
14055         default:
14056           done = true;
14057           break;
14058         }
14059     }
14060   /* It is not uncommon to see programs mechanically, erroneously, use
14061      the 'typename' keyword to denote (dependent) qualified types
14062      as base classes.  */
14063   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14064     {
14065       if (!processing_template_decl)
14066         error ("keyword %<typename%> not allowed outside of templates");
14067       else
14068         error ("keyword %<typename%> not allowed in this context "
14069                "(the base class is implicitly a type)");
14070       cp_lexer_consume_token (parser->lexer);
14071     }
14072
14073   /* Look for the optional `::' operator.  */
14074   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14075   /* Look for the nested-name-specifier.  The simplest way to
14076      implement:
14077
14078        [temp.res]
14079
14080        The keyword `typename' is not permitted in a base-specifier or
14081        mem-initializer; in these contexts a qualified name that
14082        depends on a template-parameter is implicitly assumed to be a
14083        type name.
14084
14085      is to pretend that we have seen the `typename' keyword at this
14086      point.  */
14087   cp_parser_nested_name_specifier_opt (parser,
14088                                        /*typename_keyword_p=*/true,
14089                                        /*check_dependency_p=*/true,
14090                                        typename_type,
14091                                        /*is_declaration=*/true);
14092   /* If the base class is given by a qualified name, assume that names
14093      we see are type names or templates, as appropriate.  */
14094   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14095   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14096
14097   /* Finally, look for the class-name.  */
14098   type = cp_parser_class_name (parser,
14099                                class_scope_p,
14100                                template_p,
14101                                typename_type,
14102                                /*check_dependency_p=*/true,
14103                                /*class_head_p=*/false,
14104                                /*is_declaration=*/true);
14105
14106   if (type == error_mark_node)
14107     return error_mark_node;
14108
14109   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14110 }
14111
14112 /* Exception handling [gram.exception] */
14113
14114 /* Parse an (optional) exception-specification.
14115
14116    exception-specification:
14117      throw ( type-id-list [opt] )
14118
14119    Returns a TREE_LIST representing the exception-specification.  The
14120    TREE_VALUE of each node is a type.  */
14121
14122 static tree
14123 cp_parser_exception_specification_opt (cp_parser* parser)
14124 {
14125   cp_token *token;
14126   tree type_id_list;
14127
14128   /* Peek at the next token.  */
14129   token = cp_lexer_peek_token (parser->lexer);
14130   /* If it's not `throw', then there's no exception-specification.  */
14131   if (!cp_parser_is_keyword (token, RID_THROW))
14132     return NULL_TREE;
14133
14134   /* Consume the `throw'.  */
14135   cp_lexer_consume_token (parser->lexer);
14136
14137   /* Look for the `('.  */
14138   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14139
14140   /* Peek at the next token.  */
14141   token = cp_lexer_peek_token (parser->lexer);
14142   /* If it's not a `)', then there is a type-id-list.  */
14143   if (token->type != CPP_CLOSE_PAREN)
14144     {
14145       const char *saved_message;
14146
14147       /* Types may not be defined in an exception-specification.  */
14148       saved_message = parser->type_definition_forbidden_message;
14149       parser->type_definition_forbidden_message
14150         = "types may not be defined in an exception-specification";
14151       /* Parse the type-id-list.  */
14152       type_id_list = cp_parser_type_id_list (parser);
14153       /* Restore the saved message.  */
14154       parser->type_definition_forbidden_message = saved_message;
14155     }
14156   else
14157     type_id_list = empty_except_spec;
14158
14159   /* Look for the `)'.  */
14160   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14161
14162   return type_id_list;
14163 }
14164
14165 /* Parse an (optional) type-id-list.
14166
14167    type-id-list:
14168      type-id
14169      type-id-list , type-id
14170
14171    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14172    in the order that the types were presented.  */
14173
14174 static tree
14175 cp_parser_type_id_list (cp_parser* parser)
14176 {
14177   tree types = NULL_TREE;
14178
14179   while (true)
14180     {
14181       cp_token *token;
14182       tree type;
14183
14184       /* Get the next type-id.  */
14185       type = cp_parser_type_id (parser);
14186       /* Add it to the list.  */
14187       types = add_exception_specifier (types, type, /*complain=*/1);
14188       /* Peek at the next token.  */
14189       token = cp_lexer_peek_token (parser->lexer);
14190       /* If it is not a `,', we are done.  */
14191       if (token->type != CPP_COMMA)
14192         break;
14193       /* Consume the `,'.  */
14194       cp_lexer_consume_token (parser->lexer);
14195     }
14196
14197   return nreverse (types);
14198 }
14199
14200 /* Parse a try-block.
14201
14202    try-block:
14203      try compound-statement handler-seq  */
14204
14205 static tree
14206 cp_parser_try_block (cp_parser* parser)
14207 {
14208   tree try_block;
14209
14210   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14211   try_block = begin_try_block ();
14212   cp_parser_compound_statement (parser, NULL, true);
14213   finish_try_block (try_block);
14214   cp_parser_handler_seq (parser);
14215   finish_handler_sequence (try_block);
14216
14217   return try_block;
14218 }
14219
14220 /* Parse a function-try-block.
14221
14222    function-try-block:
14223      try ctor-initializer [opt] function-body handler-seq  */
14224
14225 static bool
14226 cp_parser_function_try_block (cp_parser* parser)
14227 {
14228   tree try_block;
14229   bool ctor_initializer_p;
14230
14231   /* Look for the `try' keyword.  */
14232   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14233     return false;
14234   /* Let the rest of the front-end know where we are.  */
14235   try_block = begin_function_try_block ();
14236   /* Parse the function-body.  */
14237   ctor_initializer_p
14238     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14239   /* We're done with the `try' part.  */
14240   finish_function_try_block (try_block);
14241   /* Parse the handlers.  */
14242   cp_parser_handler_seq (parser);
14243   /* We're done with the handlers.  */
14244   finish_function_handler_sequence (try_block);
14245
14246   return ctor_initializer_p;
14247 }
14248
14249 /* Parse a handler-seq.
14250
14251    handler-seq:
14252      handler handler-seq [opt]  */
14253
14254 static void
14255 cp_parser_handler_seq (cp_parser* parser)
14256 {
14257   while (true)
14258     {
14259       cp_token *token;
14260
14261       /* Parse the handler.  */
14262       cp_parser_handler (parser);
14263       /* Peek at the next token.  */
14264       token = cp_lexer_peek_token (parser->lexer);
14265       /* If it's not `catch' then there are no more handlers.  */
14266       if (!cp_parser_is_keyword (token, RID_CATCH))
14267         break;
14268     }
14269 }
14270
14271 /* Parse a handler.
14272
14273    handler:
14274      catch ( exception-declaration ) compound-statement  */
14275
14276 static void
14277 cp_parser_handler (cp_parser* parser)
14278 {
14279   tree handler;
14280   tree declaration;
14281
14282   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14283   handler = begin_handler ();
14284   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14285   declaration = cp_parser_exception_declaration (parser);
14286   finish_handler_parms (declaration, handler);
14287   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14288   cp_parser_compound_statement (parser, NULL, false);
14289   finish_handler (handler);
14290 }
14291
14292 /* Parse an exception-declaration.
14293
14294    exception-declaration:
14295      type-specifier-seq declarator
14296      type-specifier-seq abstract-declarator
14297      type-specifier-seq
14298      ...
14299
14300    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14301    ellipsis variant is used.  */
14302
14303 static tree
14304 cp_parser_exception_declaration (cp_parser* parser)
14305 {
14306   tree decl;
14307   cp_decl_specifier_seq type_specifiers;
14308   cp_declarator *declarator;
14309   const char *saved_message;
14310
14311   /* If it's an ellipsis, it's easy to handle.  */
14312   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14313     {
14314       /* Consume the `...' token.  */
14315       cp_lexer_consume_token (parser->lexer);
14316       return NULL_TREE;
14317     }
14318
14319   /* Types may not be defined in exception-declarations.  */
14320   saved_message = parser->type_definition_forbidden_message;
14321   parser->type_definition_forbidden_message
14322     = "types may not be defined in exception-declarations";
14323
14324   /* Parse the type-specifier-seq.  */
14325   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14326                                 &type_specifiers);
14327   /* If it's a `)', then there is no declarator.  */
14328   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14329     declarator = NULL;
14330   else
14331     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14332                                        /*ctor_dtor_or_conv_p=*/NULL,
14333                                        /*parenthesized_p=*/NULL,
14334                                        /*member_p=*/false);
14335
14336   /* Restore the saved message.  */
14337   parser->type_definition_forbidden_message = saved_message;
14338
14339   if (type_specifiers.any_specifiers_p)
14340     {
14341       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14342       if (decl == NULL_TREE)
14343         error ("invalid catch parameter");
14344     }
14345   else
14346     decl = NULL_TREE;
14347
14348   return decl;
14349 }
14350
14351 /* Parse a throw-expression.
14352
14353    throw-expression:
14354      throw assignment-expression [opt]
14355
14356    Returns a THROW_EXPR representing the throw-expression.  */
14357
14358 static tree
14359 cp_parser_throw_expression (cp_parser* parser)
14360 {
14361   tree expression;
14362   cp_token* token;
14363
14364   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14365   token = cp_lexer_peek_token (parser->lexer);
14366   /* Figure out whether or not there is an assignment-expression
14367      following the "throw" keyword.  */
14368   if (token->type == CPP_COMMA
14369       || token->type == CPP_SEMICOLON
14370       || token->type == CPP_CLOSE_PAREN
14371       || token->type == CPP_CLOSE_SQUARE
14372       || token->type == CPP_CLOSE_BRACE
14373       || token->type == CPP_COLON)
14374     expression = NULL_TREE;
14375   else
14376     expression = cp_parser_assignment_expression (parser,
14377                                                   /*cast_p=*/false);
14378
14379   return build_throw (expression);
14380 }
14381
14382 /* GNU Extensions */
14383
14384 /* Parse an (optional) asm-specification.
14385
14386    asm-specification:
14387      asm ( string-literal )
14388
14389    If the asm-specification is present, returns a STRING_CST
14390    corresponding to the string-literal.  Otherwise, returns
14391    NULL_TREE.  */
14392
14393 static tree
14394 cp_parser_asm_specification_opt (cp_parser* parser)
14395 {
14396   cp_token *token;
14397   tree asm_specification;
14398
14399   /* Peek at the next token.  */
14400   token = cp_lexer_peek_token (parser->lexer);
14401   /* If the next token isn't the `asm' keyword, then there's no
14402      asm-specification.  */
14403   if (!cp_parser_is_keyword (token, RID_ASM))
14404     return NULL_TREE;
14405
14406   /* Consume the `asm' token.  */
14407   cp_lexer_consume_token (parser->lexer);
14408   /* Look for the `('.  */
14409   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14410
14411   /* Look for the string-literal.  */
14412   asm_specification = cp_parser_string_literal (parser, false, false);
14413
14414   /* Look for the `)'.  */
14415   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14416
14417   return asm_specification;
14418 }
14419
14420 /* Parse an asm-operand-list.
14421
14422    asm-operand-list:
14423      asm-operand
14424      asm-operand-list , asm-operand
14425
14426    asm-operand:
14427      string-literal ( expression )
14428      [ string-literal ] string-literal ( expression )
14429
14430    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14431    each node is the expression.  The TREE_PURPOSE is itself a
14432    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14433    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14434    is a STRING_CST for the string literal before the parenthesis.  */
14435
14436 static tree
14437 cp_parser_asm_operand_list (cp_parser* parser)
14438 {
14439   tree asm_operands = NULL_TREE;
14440
14441   while (true)
14442     {
14443       tree string_literal;
14444       tree expression;
14445       tree name;
14446
14447       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14448         {
14449           /* Consume the `[' token.  */
14450           cp_lexer_consume_token (parser->lexer);
14451           /* Read the operand name.  */
14452           name = cp_parser_identifier (parser);
14453           if (name != error_mark_node)
14454             name = build_string (IDENTIFIER_LENGTH (name),
14455                                  IDENTIFIER_POINTER (name));
14456           /* Look for the closing `]'.  */
14457           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14458         }
14459       else
14460         name = NULL_TREE;
14461       /* Look for the string-literal.  */
14462       string_literal = cp_parser_string_literal (parser, false, false);
14463
14464       /* Look for the `('.  */
14465       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14466       /* Parse the expression.  */
14467       expression = cp_parser_expression (parser, /*cast_p=*/false);
14468       /* Look for the `)'.  */
14469       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14470
14471       /* Add this operand to the list.  */
14472       asm_operands = tree_cons (build_tree_list (name, string_literal),
14473                                 expression,
14474                                 asm_operands);
14475       /* If the next token is not a `,', there are no more
14476          operands.  */
14477       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14478         break;
14479       /* Consume the `,'.  */
14480       cp_lexer_consume_token (parser->lexer);
14481     }
14482
14483   return nreverse (asm_operands);
14484 }
14485
14486 /* Parse an asm-clobber-list.
14487
14488    asm-clobber-list:
14489      string-literal
14490      asm-clobber-list , string-literal
14491
14492    Returns a TREE_LIST, indicating the clobbers in the order that they
14493    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14494
14495 static tree
14496 cp_parser_asm_clobber_list (cp_parser* parser)
14497 {
14498   tree clobbers = NULL_TREE;
14499
14500   while (true)
14501     {
14502       tree string_literal;
14503
14504       /* Look for the string literal.  */
14505       string_literal = cp_parser_string_literal (parser, false, false);
14506       /* Add it to the list.  */
14507       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14508       /* If the next token is not a `,', then the list is
14509          complete.  */
14510       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14511         break;
14512       /* Consume the `,' token.  */
14513       cp_lexer_consume_token (parser->lexer);
14514     }
14515
14516   return clobbers;
14517 }
14518
14519 /* Parse an (optional) series of attributes.
14520
14521    attributes:
14522      attributes attribute
14523
14524    attribute:
14525      __attribute__ (( attribute-list [opt] ))
14526
14527    The return value is as for cp_parser_attribute_list.  */
14528
14529 static tree
14530 cp_parser_attributes_opt (cp_parser* parser)
14531 {
14532   tree attributes = NULL_TREE;
14533
14534   while (true)
14535     {
14536       cp_token *token;
14537       tree attribute_list;
14538
14539       /* Peek at the next token.  */
14540       token = cp_lexer_peek_token (parser->lexer);
14541       /* If it's not `__attribute__', then we're done.  */
14542       if (token->keyword != RID_ATTRIBUTE)
14543         break;
14544
14545       /* Consume the `__attribute__' keyword.  */
14546       cp_lexer_consume_token (parser->lexer);
14547       /* Look for the two `(' tokens.  */
14548       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14549       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14550
14551       /* Peek at the next token.  */
14552       token = cp_lexer_peek_token (parser->lexer);
14553       if (token->type != CPP_CLOSE_PAREN)
14554         /* Parse the attribute-list.  */
14555         attribute_list = cp_parser_attribute_list (parser);
14556       else
14557         /* If the next token is a `)', then there is no attribute
14558            list.  */
14559         attribute_list = NULL;
14560
14561       /* Look for the two `)' tokens.  */
14562       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14563       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14564
14565       /* Add these new attributes to the list.  */
14566       attributes = chainon (attributes, attribute_list);
14567     }
14568
14569   return attributes;
14570 }
14571
14572 /* Parse an attribute-list.
14573
14574    attribute-list:
14575      attribute
14576      attribute-list , attribute
14577
14578    attribute:
14579      identifier
14580      identifier ( identifier )
14581      identifier ( identifier , expression-list )
14582      identifier ( expression-list )
14583
14584    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14585    to an attribute.  The TREE_PURPOSE of each node is the identifier
14586    indicating which attribute is in use.  The TREE_VALUE represents
14587    the arguments, if any.  */
14588
14589 static tree
14590 cp_parser_attribute_list (cp_parser* parser)
14591 {
14592   tree attribute_list = NULL_TREE;
14593   bool save_translate_strings_p = parser->translate_strings_p;
14594
14595   parser->translate_strings_p = false;
14596   while (true)
14597     {
14598       cp_token *token;
14599       tree identifier;
14600       tree attribute;
14601
14602       /* Look for the identifier.  We also allow keywords here; for
14603          example `__attribute__ ((const))' is legal.  */
14604       token = cp_lexer_peek_token (parser->lexer);
14605       if (token->type == CPP_NAME
14606           || token->type == CPP_KEYWORD)
14607         {
14608           /* Consume the token.  */
14609           token = cp_lexer_consume_token (parser->lexer);
14610
14611           /* Save away the identifier that indicates which attribute
14612              this is.  */
14613           identifier = token->value;
14614           attribute = build_tree_list (identifier, NULL_TREE);
14615
14616           /* Peek at the next token.  */
14617           token = cp_lexer_peek_token (parser->lexer);
14618           /* If it's an `(', then parse the attribute arguments.  */
14619           if (token->type == CPP_OPEN_PAREN)
14620             {
14621               tree arguments;
14622
14623               arguments = (cp_parser_parenthesized_expression_list
14624                            (parser, true, /*cast_p=*/false,
14625                             /*non_constant_p=*/NULL));
14626               /* Save the identifier and arguments away.  */
14627               TREE_VALUE (attribute) = arguments;
14628             }
14629
14630           /* Add this attribute to the list.  */
14631           TREE_CHAIN (attribute) = attribute_list;
14632           attribute_list = attribute;
14633
14634           token = cp_lexer_peek_token (parser->lexer);
14635         }
14636       /* Now, look for more attributes.  If the next token isn't a
14637          `,', we're done.  */
14638       if (token->type != CPP_COMMA)
14639         break;
14640
14641       /* Consume the comma and keep going.  */
14642       cp_lexer_consume_token (parser->lexer);
14643     }
14644   parser->translate_strings_p = save_translate_strings_p;
14645
14646   /* We built up the list in reverse order.  */
14647   return nreverse (attribute_list);
14648 }
14649
14650 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14651    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14652    current value of the PEDANTIC flag, regardless of whether or not
14653    the `__extension__' keyword is present.  The caller is responsible
14654    for restoring the value of the PEDANTIC flag.  */
14655
14656 static bool
14657 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14658 {
14659   /* Save the old value of the PEDANTIC flag.  */
14660   *saved_pedantic = pedantic;
14661
14662   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14663     {
14664       /* Consume the `__extension__' token.  */
14665       cp_lexer_consume_token (parser->lexer);
14666       /* We're not being pedantic while the `__extension__' keyword is
14667          in effect.  */
14668       pedantic = 0;
14669
14670       return true;
14671     }
14672
14673   return false;
14674 }
14675
14676 /* Parse a label declaration.
14677
14678    label-declaration:
14679      __label__ label-declarator-seq ;
14680
14681    label-declarator-seq:
14682      identifier , label-declarator-seq
14683      identifier  */
14684
14685 static void
14686 cp_parser_label_declaration (cp_parser* parser)
14687 {
14688   /* Look for the `__label__' keyword.  */
14689   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14690
14691   while (true)
14692     {
14693       tree identifier;
14694
14695       /* Look for an identifier.  */
14696       identifier = cp_parser_identifier (parser);
14697       /* If we failed, stop.  */
14698       if (identifier == error_mark_node)
14699         break;
14700       /* Declare it as a label.  */
14701       finish_label_decl (identifier);
14702       /* If the next token is a `;', stop.  */
14703       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14704         break;
14705       /* Look for the `,' separating the label declarations.  */
14706       cp_parser_require (parser, CPP_COMMA, "`,'");
14707     }
14708
14709   /* Look for the final `;'.  */
14710   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14711 }
14712
14713 /* Support Functions */
14714
14715 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14716    NAME should have one of the representations used for an
14717    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14718    is returned.  If PARSER->SCOPE is a dependent type, then a
14719    SCOPE_REF is returned.
14720
14721    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14722    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14723    was formed.  Abstractly, such entities should not be passed to this
14724    function, because they do not need to be looked up, but it is
14725    simpler to check for this special case here, rather than at the
14726    call-sites.
14727
14728    In cases not explicitly covered above, this function returns a
14729    DECL, OVERLOAD, or baselink representing the result of the lookup.
14730    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14731    is returned.
14732
14733    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14734    (e.g., "struct") that was used.  In that case bindings that do not
14735    refer to types are ignored.
14736
14737    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14738    ignored.
14739
14740    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14741    are ignored.
14742
14743    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14744    types.
14745
14746    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14747    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14748    NULL_TREE otherwise.  */ 
14749
14750 static tree
14751 cp_parser_lookup_name (cp_parser *parser, tree name,
14752                        enum tag_types tag_type,
14753                        bool is_template, 
14754                        bool is_namespace,
14755                        bool check_dependency,
14756                        tree *ambiguous_decls)
14757 {
14758   int flags = 0;
14759   tree decl;
14760   tree object_type = parser->context->object_type;
14761
14762   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14763     flags |= LOOKUP_COMPLAIN;
14764
14765   /* Assume that the lookup will be unambiguous.  */
14766   if (ambiguous_decls)
14767     *ambiguous_decls = NULL_TREE;
14768
14769   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14770      no longer valid.  Note that if we are parsing tentatively, and
14771      the parse fails, OBJECT_TYPE will be automatically restored.  */
14772   parser->context->object_type = NULL_TREE;
14773
14774   if (name == error_mark_node)
14775     return error_mark_node;
14776
14777   /* A template-id has already been resolved; there is no lookup to
14778      do.  */
14779   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14780     return name;
14781   if (BASELINK_P (name))
14782     {
14783       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14784                   == TEMPLATE_ID_EXPR);
14785       return name;
14786     }
14787
14788   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14789      it should already have been checked to make sure that the name
14790      used matches the type being destroyed.  */
14791   if (TREE_CODE (name) == BIT_NOT_EXPR)
14792     {
14793       tree type;
14794
14795       /* Figure out to which type this destructor applies.  */
14796       if (parser->scope)
14797         type = parser->scope;
14798       else if (object_type)
14799         type = object_type;
14800       else
14801         type = current_class_type;
14802       /* If that's not a class type, there is no destructor.  */
14803       if (!type || !CLASS_TYPE_P (type))
14804         return error_mark_node;
14805       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14806         lazily_declare_fn (sfk_destructor, type);
14807       if (!CLASSTYPE_DESTRUCTORS (type))
14808           return error_mark_node;
14809       /* If it was a class type, return the destructor.  */
14810       return CLASSTYPE_DESTRUCTORS (type);
14811     }
14812
14813   /* By this point, the NAME should be an ordinary identifier.  If
14814      the id-expression was a qualified name, the qualifying scope is
14815      stored in PARSER->SCOPE at this point.  */
14816   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14817
14818   /* Perform the lookup.  */
14819   if (parser->scope)
14820     {
14821       bool dependent_p;
14822
14823       if (parser->scope == error_mark_node)
14824         return error_mark_node;
14825
14826       /* If the SCOPE is dependent, the lookup must be deferred until
14827          the template is instantiated -- unless we are explicitly
14828          looking up names in uninstantiated templates.  Even then, we
14829          cannot look up the name if the scope is not a class type; it
14830          might, for example, be a template type parameter.  */
14831       dependent_p = (TYPE_P (parser->scope)
14832                      && !(parser->in_declarator_p
14833                           && currently_open_class (parser->scope))
14834                      && dependent_type_p (parser->scope));
14835       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14836            && dependent_p)
14837         {
14838           if (tag_type)
14839             {
14840               tree type;
14841
14842               /* The resolution to Core Issue 180 says that `struct
14843                  A::B' should be considered a type-name, even if `A'
14844                  is dependent.  */
14845               type = make_typename_type (parser->scope, name, tag_type,
14846                                          /*complain=*/tf_error);
14847               decl = TYPE_NAME (type);
14848             }
14849           else if (is_template
14850                    && (cp_parser_next_token_ends_template_argument_p (parser)
14851                        || cp_lexer_next_token_is (parser->lexer,
14852                                                   CPP_CLOSE_PAREN)))
14853             decl = make_unbound_class_template (parser->scope,
14854                                                 name, NULL_TREE,
14855                                                 /*complain=*/tf_error);
14856           else
14857             decl = build_qualified_name (/*type=*/NULL_TREE,
14858                                          parser->scope, name,
14859                                          is_template);
14860         }
14861       else
14862         {
14863           tree pushed_scope = NULL_TREE;
14864
14865           /* If PARSER->SCOPE is a dependent type, then it must be a
14866              class type, and we must not be checking dependencies;
14867              otherwise, we would have processed this lookup above.  So
14868              that PARSER->SCOPE is not considered a dependent base by
14869              lookup_member, we must enter the scope here.  */
14870           if (dependent_p)
14871             pushed_scope = push_scope (parser->scope);
14872           /* If the PARSER->SCOPE is a template specialization, it
14873              may be instantiated during name lookup.  In that case,
14874              errors may be issued.  Even if we rollback the current
14875              tentative parse, those errors are valid.  */
14876           decl = lookup_qualified_name (parser->scope, name,
14877                                         tag_type != none_type,
14878                                         /*complain=*/true);
14879           if (pushed_scope)
14880             pop_scope (pushed_scope);
14881         }
14882       parser->qualifying_scope = parser->scope;
14883       parser->object_scope = NULL_TREE;
14884     }
14885   else if (object_type)
14886     {
14887       tree object_decl = NULL_TREE;
14888       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14889          OBJECT_TYPE is not a class.  */
14890       if (CLASS_TYPE_P (object_type))
14891         /* If the OBJECT_TYPE is a template specialization, it may
14892            be instantiated during name lookup.  In that case, errors
14893            may be issued.  Even if we rollback the current tentative
14894            parse, those errors are valid.  */
14895         object_decl = lookup_member (object_type,
14896                                      name,
14897                                      /*protect=*/0,
14898                                      tag_type != none_type);
14899       /* Look it up in the enclosing context, too.  */
14900       decl = lookup_name_real (name, tag_type != none_type,
14901                                /*nonclass=*/0,
14902                                /*block_p=*/true, is_namespace, flags);
14903       parser->object_scope = object_type;
14904       parser->qualifying_scope = NULL_TREE;
14905       if (object_decl)
14906         decl = object_decl;
14907     }
14908   else
14909     {
14910       decl = lookup_name_real (name, tag_type != none_type,
14911                                /*nonclass=*/0,
14912                                /*block_p=*/true, is_namespace, flags);
14913       parser->qualifying_scope = NULL_TREE;
14914       parser->object_scope = NULL_TREE;
14915     }
14916
14917   /* If the lookup failed, let our caller know.  */
14918   if (!decl || decl == error_mark_node)
14919     return error_mark_node;
14920
14921   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14922   if (TREE_CODE (decl) == TREE_LIST)
14923     {
14924       if (ambiguous_decls)
14925         *ambiguous_decls = decl;
14926       /* The error message we have to print is too complicated for
14927          cp_parser_error, so we incorporate its actions directly.  */
14928       if (!cp_parser_simulate_error (parser))
14929         {
14930           error ("reference to %qD is ambiguous", name);
14931           print_candidates (decl);
14932         }
14933       return error_mark_node;
14934     }
14935
14936   gcc_assert (DECL_P (decl)
14937               || TREE_CODE (decl) == OVERLOAD
14938               || TREE_CODE (decl) == SCOPE_REF
14939               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14940               || BASELINK_P (decl));
14941
14942   /* If we have resolved the name of a member declaration, check to
14943      see if the declaration is accessible.  When the name resolves to
14944      set of overloaded functions, accessibility is checked when
14945      overload resolution is done.
14946
14947      During an explicit instantiation, access is not checked at all,
14948      as per [temp.explicit].  */
14949   if (DECL_P (decl))
14950     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14951
14952   return decl;
14953 }
14954
14955 /* Like cp_parser_lookup_name, but for use in the typical case where
14956    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14957    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14958
14959 static tree
14960 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14961 {
14962   return cp_parser_lookup_name (parser, name,
14963                                 none_type,
14964                                 /*is_template=*/false,
14965                                 /*is_namespace=*/false,
14966                                 /*check_dependency=*/true,
14967                                 /*ambiguous_decls=*/NULL);
14968 }
14969
14970 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14971    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14972    true, the DECL indicates the class being defined in a class-head,
14973    or declared in an elaborated-type-specifier.
14974
14975    Otherwise, return DECL.  */
14976
14977 static tree
14978 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14979 {
14980   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14981      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14982
14983        struct A {
14984          template <typename T> struct B;
14985        };
14986
14987        template <typename T> struct A::B {};
14988
14989      Similarly, in an elaborated-type-specifier:
14990
14991        namespace N { struct X{}; }
14992
14993        struct A {
14994          template <typename T> friend struct N::X;
14995        };
14996
14997      However, if the DECL refers to a class type, and we are in
14998      the scope of the class, then the name lookup automatically
14999      finds the TYPE_DECL created by build_self_reference rather
15000      than a TEMPLATE_DECL.  For example, in:
15001
15002        template <class T> struct S {
15003          S s;
15004        };
15005
15006      there is no need to handle such case.  */
15007
15008   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15009     return DECL_TEMPLATE_RESULT (decl);
15010
15011   return decl;
15012 }
15013
15014 /* If too many, or too few, template-parameter lists apply to the
15015    declarator, issue an error message.  Returns TRUE if all went well,
15016    and FALSE otherwise.  */
15017
15018 static bool
15019 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15020                                                 cp_declarator *declarator)
15021 {
15022   unsigned num_templates;
15023
15024   /* We haven't seen any classes that involve template parameters yet.  */
15025   num_templates = 0;
15026
15027   switch (declarator->kind)
15028     {
15029     case cdk_id:
15030       if (declarator->u.id.qualifying_scope)
15031         {
15032           tree scope;
15033           tree member;
15034
15035           scope = declarator->u.id.qualifying_scope;
15036           member = declarator->u.id.unqualified_name;
15037
15038           while (scope && CLASS_TYPE_P (scope))
15039             {
15040               /* You're supposed to have one `template <...>'
15041                  for every template class, but you don't need one
15042                  for a full specialization.  For example:
15043
15044                  template <class T> struct S{};
15045                  template <> struct S<int> { void f(); };
15046                  void S<int>::f () {}
15047
15048                  is correct; there shouldn't be a `template <>' for
15049                  the definition of `S<int>::f'.  */
15050               if (CLASSTYPE_TEMPLATE_INFO (scope)
15051                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15052                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15053                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15054                 ++num_templates;
15055
15056               scope = TYPE_CONTEXT (scope);
15057             }
15058         }
15059       else if (TREE_CODE (declarator->u.id.unqualified_name)
15060                == TEMPLATE_ID_EXPR)
15061         /* If the DECLARATOR has the form `X<y>' then it uses one
15062            additional level of template parameters.  */
15063         ++num_templates;
15064
15065       return cp_parser_check_template_parameters (parser,
15066                                                   num_templates);
15067
15068     case cdk_function:
15069     case cdk_array:
15070     case cdk_pointer:
15071     case cdk_reference:
15072     case cdk_ptrmem:
15073       return (cp_parser_check_declarator_template_parameters
15074               (parser, declarator->declarator));
15075
15076     case cdk_error:
15077       return true;
15078
15079     default:
15080       gcc_unreachable ();
15081     }
15082   return false;
15083 }
15084
15085 /* NUM_TEMPLATES were used in the current declaration.  If that is
15086    invalid, return FALSE and issue an error messages.  Otherwise,
15087    return TRUE.  */
15088
15089 static bool
15090 cp_parser_check_template_parameters (cp_parser* parser,
15091                                      unsigned num_templates)
15092 {
15093   /* If there are more template classes than parameter lists, we have
15094      something like:
15095
15096        template <class T> void S<T>::R<T>::f ();  */
15097   if (parser->num_template_parameter_lists < num_templates)
15098     {
15099       error ("too few template-parameter-lists");
15100       return false;
15101     }
15102   /* If there are the same number of template classes and parameter
15103      lists, that's OK.  */
15104   if (parser->num_template_parameter_lists == num_templates)
15105     return true;
15106   /* If there are more, but only one more, then we are referring to a
15107      member template.  That's OK too.  */
15108   if (parser->num_template_parameter_lists == num_templates + 1)
15109       return true;
15110   /* Otherwise, there are too many template parameter lists.  We have
15111      something like:
15112
15113      template <class T> template <class U> void S::f();  */
15114   error ("too many template-parameter-lists");
15115   return false;
15116 }
15117
15118 /* Parse an optional `::' token indicating that the following name is
15119    from the global namespace.  If so, PARSER->SCOPE is set to the
15120    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15121    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15122    Returns the new value of PARSER->SCOPE, if the `::' token is
15123    present, and NULL_TREE otherwise.  */
15124
15125 static tree
15126 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15127 {
15128   cp_token *token;
15129
15130   /* Peek at the next token.  */
15131   token = cp_lexer_peek_token (parser->lexer);
15132   /* If we're looking at a `::' token then we're starting from the
15133      global namespace, not our current location.  */
15134   if (token->type == CPP_SCOPE)
15135     {
15136       /* Consume the `::' token.  */
15137       cp_lexer_consume_token (parser->lexer);
15138       /* Set the SCOPE so that we know where to start the lookup.  */
15139       parser->scope = global_namespace;
15140       parser->qualifying_scope = global_namespace;
15141       parser->object_scope = NULL_TREE;
15142
15143       return parser->scope;
15144     }
15145   else if (!current_scope_valid_p)
15146     {
15147       parser->scope = NULL_TREE;
15148       parser->qualifying_scope = NULL_TREE;
15149       parser->object_scope = NULL_TREE;
15150     }
15151
15152   return NULL_TREE;
15153 }
15154
15155 /* Returns TRUE if the upcoming token sequence is the start of a
15156    constructor declarator.  If FRIEND_P is true, the declarator is
15157    preceded by the `friend' specifier.  */
15158
15159 static bool
15160 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15161 {
15162   bool constructor_p;
15163   tree type_decl = NULL_TREE;
15164   bool nested_name_p;
15165   cp_token *next_token;
15166
15167   /* The common case is that this is not a constructor declarator, so
15168      try to avoid doing lots of work if at all possible.  It's not
15169      valid declare a constructor at function scope.  */
15170   if (at_function_scope_p ())
15171     return false;
15172   /* And only certain tokens can begin a constructor declarator.  */
15173   next_token = cp_lexer_peek_token (parser->lexer);
15174   if (next_token->type != CPP_NAME
15175       && next_token->type != CPP_SCOPE
15176       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15177       && next_token->type != CPP_TEMPLATE_ID)
15178     return false;
15179
15180   /* Parse tentatively; we are going to roll back all of the tokens
15181      consumed here.  */
15182   cp_parser_parse_tentatively (parser);
15183   /* Assume that we are looking at a constructor declarator.  */
15184   constructor_p = true;
15185
15186   /* Look for the optional `::' operator.  */
15187   cp_parser_global_scope_opt (parser,
15188                               /*current_scope_valid_p=*/false);
15189   /* Look for the nested-name-specifier.  */
15190   nested_name_p
15191     = (cp_parser_nested_name_specifier_opt (parser,
15192                                             /*typename_keyword_p=*/false,
15193                                             /*check_dependency_p=*/false,
15194                                             /*type_p=*/false,
15195                                             /*is_declaration=*/false)
15196        != NULL_TREE);
15197   /* Outside of a class-specifier, there must be a
15198      nested-name-specifier.  */
15199   if (!nested_name_p &&
15200       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15201        || friend_p))
15202     constructor_p = false;
15203   /* If we still think that this might be a constructor-declarator,
15204      look for a class-name.  */
15205   if (constructor_p)
15206     {
15207       /* If we have:
15208
15209            template <typename T> struct S { S(); };
15210            template <typename T> S<T>::S ();
15211
15212          we must recognize that the nested `S' names a class.
15213          Similarly, for:
15214
15215            template <typename T> S<T>::S<T> ();
15216
15217          we must recognize that the nested `S' names a template.  */
15218       type_decl = cp_parser_class_name (parser,
15219                                         /*typename_keyword_p=*/false,
15220                                         /*template_keyword_p=*/false,
15221                                         none_type,
15222                                         /*check_dependency_p=*/false,
15223                                         /*class_head_p=*/false,
15224                                         /*is_declaration=*/false);
15225       /* If there was no class-name, then this is not a constructor.  */
15226       constructor_p = !cp_parser_error_occurred (parser);
15227     }
15228
15229   /* If we're still considering a constructor, we have to see a `(',
15230      to begin the parameter-declaration-clause, followed by either a
15231      `)', an `...', or a decl-specifier.  We need to check for a
15232      type-specifier to avoid being fooled into thinking that:
15233
15234        S::S (f) (int);
15235
15236      is a constructor.  (It is actually a function named `f' that
15237      takes one parameter (of type `int') and returns a value of type
15238      `S::S'.  */
15239   if (constructor_p
15240       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15241     {
15242       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15243           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15244           /* A parameter declaration begins with a decl-specifier,
15245              which is either the "attribute" keyword, a storage class
15246              specifier, or (usually) a type-specifier.  */
15247           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15248           && !cp_parser_storage_class_specifier_opt (parser))
15249         {
15250           tree type;
15251           tree pushed_scope = NULL_TREE;
15252           unsigned saved_num_template_parameter_lists;
15253
15254           /* Names appearing in the type-specifier should be looked up
15255              in the scope of the class.  */
15256           if (current_class_type)
15257             type = NULL_TREE;
15258           else
15259             {
15260               type = TREE_TYPE (type_decl);
15261               if (TREE_CODE (type) == TYPENAME_TYPE)
15262                 {
15263                   type = resolve_typename_type (type,
15264                                                 /*only_current_p=*/false);
15265                   if (type == error_mark_node)
15266                     {
15267                       cp_parser_abort_tentative_parse (parser);
15268                       return false;
15269                     }
15270                 }
15271               pushed_scope = push_scope (type);
15272             }
15273
15274           /* Inside the constructor parameter list, surrounding
15275              template-parameter-lists do not apply.  */
15276           saved_num_template_parameter_lists
15277             = parser->num_template_parameter_lists;
15278           parser->num_template_parameter_lists = 0;
15279
15280           /* Look for the type-specifier.  */
15281           cp_parser_type_specifier (parser,
15282                                     CP_PARSER_FLAGS_NONE,
15283                                     /*decl_specs=*/NULL,
15284                                     /*is_declarator=*/true,
15285                                     /*declares_class_or_enum=*/NULL,
15286                                     /*is_cv_qualifier=*/NULL);
15287
15288           parser->num_template_parameter_lists
15289             = saved_num_template_parameter_lists;
15290
15291           /* Leave the scope of the class.  */
15292           if (pushed_scope)
15293             pop_scope (pushed_scope);
15294
15295           constructor_p = !cp_parser_error_occurred (parser);
15296         }
15297     }
15298   else
15299     constructor_p = false;
15300   /* We did not really want to consume any tokens.  */
15301   cp_parser_abort_tentative_parse (parser);
15302
15303   return constructor_p;
15304 }
15305
15306 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15307    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15308    they must be performed once we are in the scope of the function.
15309
15310    Returns the function defined.  */
15311
15312 static tree
15313 cp_parser_function_definition_from_specifiers_and_declarator
15314   (cp_parser* parser,
15315    cp_decl_specifier_seq *decl_specifiers,
15316    tree attributes,
15317    const cp_declarator *declarator)
15318 {
15319   tree fn;
15320   bool success_p;
15321
15322   /* Begin the function-definition.  */
15323   success_p = start_function (decl_specifiers, declarator, attributes);
15324
15325   /* The things we're about to see are not directly qualified by any
15326      template headers we've seen thus far.  */
15327   reset_specialization ();
15328
15329   /* If there were names looked up in the decl-specifier-seq that we
15330      did not check, check them now.  We must wait until we are in the
15331      scope of the function to perform the checks, since the function
15332      might be a friend.  */
15333   perform_deferred_access_checks ();
15334
15335   if (!success_p)
15336     {
15337       /* Skip the entire function.  */
15338       cp_parser_skip_to_end_of_block_or_statement (parser);
15339       fn = error_mark_node;
15340     }
15341   else
15342     fn = cp_parser_function_definition_after_declarator (parser,
15343                                                          /*inline_p=*/false);
15344
15345   return fn;
15346 }
15347
15348 /* Parse the part of a function-definition that follows the
15349    declarator.  INLINE_P is TRUE iff this function is an inline
15350    function defined with a class-specifier.
15351
15352    Returns the function defined.  */
15353
15354 static tree
15355 cp_parser_function_definition_after_declarator (cp_parser* parser,
15356                                                 bool inline_p)
15357 {
15358   tree fn;
15359   bool ctor_initializer_p = false;
15360   bool saved_in_unbraced_linkage_specification_p;
15361   unsigned saved_num_template_parameter_lists;
15362
15363   /* If the next token is `return', then the code may be trying to
15364      make use of the "named return value" extension that G++ used to
15365      support.  */
15366   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15367     {
15368       /* Consume the `return' keyword.  */
15369       cp_lexer_consume_token (parser->lexer);
15370       /* Look for the identifier that indicates what value is to be
15371          returned.  */
15372       cp_parser_identifier (parser);
15373       /* Issue an error message.  */
15374       error ("named return values are no longer supported");
15375       /* Skip tokens until we reach the start of the function body.  */
15376       while (true)
15377         {
15378           cp_token *token = cp_lexer_peek_token (parser->lexer);
15379           if (token->type == CPP_OPEN_BRACE
15380               || token->type == CPP_EOF
15381               || token->type == CPP_PRAGMA_EOL)
15382             break;
15383           cp_lexer_consume_token (parser->lexer);
15384         }
15385     }
15386   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15387      anything declared inside `f'.  */
15388   saved_in_unbraced_linkage_specification_p
15389     = parser->in_unbraced_linkage_specification_p;
15390   parser->in_unbraced_linkage_specification_p = false;
15391   /* Inside the function, surrounding template-parameter-lists do not
15392      apply.  */
15393   saved_num_template_parameter_lists
15394     = parser->num_template_parameter_lists;
15395   parser->num_template_parameter_lists = 0;
15396   /* If the next token is `try', then we are looking at a
15397      function-try-block.  */
15398   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15399     ctor_initializer_p = cp_parser_function_try_block (parser);
15400   /* A function-try-block includes the function-body, so we only do
15401      this next part if we're not processing a function-try-block.  */
15402   else
15403     ctor_initializer_p
15404       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15405
15406   /* Finish the function.  */
15407   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15408                         (inline_p ? 2 : 0));
15409   /* Generate code for it, if necessary.  */
15410   expand_or_defer_fn (fn);
15411   /* Restore the saved values.  */
15412   parser->in_unbraced_linkage_specification_p
15413     = saved_in_unbraced_linkage_specification_p;
15414   parser->num_template_parameter_lists
15415     = saved_num_template_parameter_lists;
15416
15417   return fn;
15418 }
15419
15420 /* Parse a template-declaration, assuming that the `export' (and
15421    `extern') keywords, if present, has already been scanned.  MEMBER_P
15422    is as for cp_parser_template_declaration.  */
15423
15424 static void
15425 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15426 {
15427   tree decl = NULL_TREE;
15428   tree checks;
15429   tree parameter_list;
15430   bool friend_p = false;
15431   bool need_lang_pop;
15432
15433   /* Look for the `template' keyword.  */
15434   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15435     return;
15436
15437   /* And the `<'.  */
15438   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15439     return;
15440   /* [temp]
15441    
15442      A template ... shall not have C linkage.  */
15443   if (current_lang_name == lang_name_c)
15444     {
15445       error ("template with C linkage");
15446       /* Give it C++ linkage to avoid confusing other parts of the
15447          front end.  */
15448       push_lang_context (lang_name_cplusplus);
15449       need_lang_pop = true;
15450     }
15451   else
15452     need_lang_pop = false;
15453
15454   /* We cannot perform access checks on the template parameter
15455      declarations until we know what is being declared, just as we
15456      cannot check the decl-specifier list.  */
15457   push_deferring_access_checks (dk_deferred);
15458
15459   /* If the next token is `>', then we have an invalid
15460      specialization.  Rather than complain about an invalid template
15461      parameter, issue an error message here.  */
15462   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15463     {
15464       cp_parser_error (parser, "invalid explicit specialization");
15465       begin_specialization ();
15466       parameter_list = NULL_TREE;
15467     }
15468   else
15469     /* Parse the template parameters.  */
15470     parameter_list = cp_parser_template_parameter_list (parser);
15471
15472   /* Get the deferred access checks from the parameter list.  These
15473      will be checked once we know what is being declared, as for a
15474      member template the checks must be performed in the scope of the
15475      class containing the member.  */
15476   checks = get_deferred_access_checks ();
15477
15478   /* Look for the `>'.  */
15479   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15480   /* We just processed one more parameter list.  */
15481   ++parser->num_template_parameter_lists;
15482   /* If the next token is `template', there are more template
15483      parameters.  */
15484   if (cp_lexer_next_token_is_keyword (parser->lexer,
15485                                       RID_TEMPLATE))
15486     cp_parser_template_declaration_after_export (parser, member_p);
15487   else
15488     {
15489       /* There are no access checks when parsing a template, as we do not
15490          know if a specialization will be a friend.  */
15491       push_deferring_access_checks (dk_no_check);
15492       decl = cp_parser_single_declaration (parser,
15493                                            checks,
15494                                            member_p,
15495                                            &friend_p);
15496       pop_deferring_access_checks ();
15497
15498       /* If this is a member template declaration, let the front
15499          end know.  */
15500       if (member_p && !friend_p && decl)
15501         {
15502           if (TREE_CODE (decl) == TYPE_DECL)
15503             cp_parser_check_access_in_redeclaration (decl);
15504
15505           decl = finish_member_template_decl (decl);
15506         }
15507       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15508         make_friend_class (current_class_type, TREE_TYPE (decl),
15509                            /*complain=*/true);
15510     }
15511   /* We are done with the current parameter list.  */
15512   --parser->num_template_parameter_lists;
15513
15514   pop_deferring_access_checks ();
15515
15516   /* Finish up.  */
15517   finish_template_decl (parameter_list);
15518
15519   /* Register member declarations.  */
15520   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15521     finish_member_declaration (decl);
15522   /* For the erroneous case of a template with C linkage, we pushed an
15523      implicit C++ linkage scope; exit that scope now.  */
15524   if (need_lang_pop)
15525     pop_lang_context ();
15526   /* If DECL is a function template, we must return to parse it later.
15527      (Even though there is no definition, there might be default
15528      arguments that need handling.)  */
15529   if (member_p && decl
15530       && (TREE_CODE (decl) == FUNCTION_DECL
15531           || DECL_FUNCTION_TEMPLATE_P (decl)))
15532     TREE_VALUE (parser->unparsed_functions_queues)
15533       = tree_cons (NULL_TREE, decl,
15534                    TREE_VALUE (parser->unparsed_functions_queues));
15535 }
15536
15537 /* Perform the deferred access checks from a template-parameter-list.
15538    CHECKS is a TREE_LIST of access checks, as returned by
15539    get_deferred_access_checks.  */
15540
15541 static void
15542 cp_parser_perform_template_parameter_access_checks (tree checks)
15543 {
15544   ++processing_template_parmlist;
15545   perform_access_checks (checks);
15546   --processing_template_parmlist;
15547 }
15548
15549 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15550    `function-definition' sequence.  MEMBER_P is true, this declaration
15551    appears in a class scope.
15552
15553    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15554    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15555
15556 static tree
15557 cp_parser_single_declaration (cp_parser* parser,
15558                               tree checks,
15559                               bool member_p,
15560                               bool* friend_p)
15561 {
15562   int declares_class_or_enum;
15563   tree decl = NULL_TREE;
15564   cp_decl_specifier_seq decl_specifiers;
15565   bool function_definition_p = false;
15566
15567   /* This function is only used when processing a template
15568      declaration.  */
15569   gcc_assert (innermost_scope_kind () == sk_template_parms
15570               || innermost_scope_kind () == sk_template_spec);
15571
15572   /* Defer access checks until we know what is being declared.  */
15573   push_deferring_access_checks (dk_deferred);
15574
15575   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15576      alternative.  */
15577   cp_parser_decl_specifier_seq (parser,
15578                                 CP_PARSER_FLAGS_OPTIONAL,
15579                                 &decl_specifiers,
15580                                 &declares_class_or_enum);
15581   if (friend_p)
15582     *friend_p = cp_parser_friend_p (&decl_specifiers);
15583
15584   /* There are no template typedefs.  */
15585   if (decl_specifiers.specs[(int) ds_typedef])
15586     {
15587       error ("template declaration of %qs", "typedef");
15588       decl = error_mark_node;
15589     }
15590
15591   /* Gather up the access checks that occurred the
15592      decl-specifier-seq.  */
15593   stop_deferring_access_checks ();
15594
15595   /* Check for the declaration of a template class.  */
15596   if (declares_class_or_enum)
15597     {
15598       if (cp_parser_declares_only_class_p (parser))
15599         {
15600           decl = shadow_tag (&decl_specifiers);
15601
15602           /* In this case:
15603
15604                struct C {
15605                  friend template <typename T> struct A<T>::B;
15606                };
15607
15608              A<T>::B will be represented by a TYPENAME_TYPE, and
15609              therefore not recognized by shadow_tag.  */
15610           if (friend_p && *friend_p
15611               && !decl
15612               && decl_specifiers.type
15613               && TYPE_P (decl_specifiers.type))
15614             decl = decl_specifiers.type;
15615
15616           if (decl && decl != error_mark_node)
15617             decl = TYPE_NAME (decl);
15618           else
15619             decl = error_mark_node;
15620
15621           /* Perform access checks for template parameters.  */
15622           cp_parser_perform_template_parameter_access_checks (checks);
15623         }
15624     }
15625   /* If it's not a template class, try for a template function.  If
15626      the next token is a `;', then this declaration does not declare
15627      anything.  But, if there were errors in the decl-specifiers, then
15628      the error might well have come from an attempted class-specifier.
15629      In that case, there's no need to warn about a missing declarator.  */
15630   if (!decl
15631       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15632           || decl_specifiers.type != error_mark_node))
15633     decl = cp_parser_init_declarator (parser,
15634                                       &decl_specifiers,
15635                                       checks,
15636                                       /*function_definition_allowed_p=*/true,
15637                                       member_p,
15638                                       declares_class_or_enum,
15639                                       &function_definition_p);
15640
15641   pop_deferring_access_checks ();
15642
15643   /* Clear any current qualification; whatever comes next is the start
15644      of something new.  */
15645   parser->scope = NULL_TREE;
15646   parser->qualifying_scope = NULL_TREE;
15647   parser->object_scope = NULL_TREE;
15648   /* Look for a trailing `;' after the declaration.  */
15649   if (!function_definition_p
15650       && (decl == error_mark_node
15651           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15652     cp_parser_skip_to_end_of_block_or_statement (parser);
15653
15654   return decl;
15655 }
15656
15657 /* Parse a cast-expression that is not the operand of a unary "&".  */
15658
15659 static tree
15660 cp_parser_simple_cast_expression (cp_parser *parser)
15661 {
15662   return cp_parser_cast_expression (parser, /*address_p=*/false,
15663                                     /*cast_p=*/false);
15664 }
15665
15666 /* Parse a functional cast to TYPE.  Returns an expression
15667    representing the cast.  */
15668
15669 static tree
15670 cp_parser_functional_cast (cp_parser* parser, tree type)
15671 {
15672   tree expression_list;
15673   tree cast;
15674
15675   expression_list
15676     = cp_parser_parenthesized_expression_list (parser, false,
15677                                                /*cast_p=*/true,
15678                                                /*non_constant_p=*/NULL);
15679
15680   cast = build_functional_cast (type, expression_list);
15681   /* [expr.const]/1: In an integral constant expression "only type
15682      conversions to integral or enumeration type can be used".  */
15683   if (TREE_CODE (type) == TYPE_DECL)
15684     type = TREE_TYPE (type);
15685   if (cast != error_mark_node && !dependent_type_p (type)
15686       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15687     {
15688       if (cp_parser_non_integral_constant_expression
15689           (parser, "a call to a constructor"))
15690         return error_mark_node;
15691     }
15692   return cast;
15693 }
15694
15695 /* Save the tokens that make up the body of a member function defined
15696    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15697    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15698    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15699    for the member function.  */
15700
15701 static tree
15702 cp_parser_save_member_function_body (cp_parser* parser,
15703                                      cp_decl_specifier_seq *decl_specifiers,
15704                                      cp_declarator *declarator,
15705                                      tree attributes)
15706 {
15707   cp_token *first;
15708   cp_token *last;
15709   tree fn;
15710
15711   /* Create the function-declaration.  */
15712   fn = start_method (decl_specifiers, declarator, attributes);
15713   /* If something went badly wrong, bail out now.  */
15714   if (fn == error_mark_node)
15715     {
15716       /* If there's a function-body, skip it.  */
15717       if (cp_parser_token_starts_function_definition_p
15718           (cp_lexer_peek_token (parser->lexer)))
15719         cp_parser_skip_to_end_of_block_or_statement (parser);
15720       return error_mark_node;
15721     }
15722
15723   /* Remember it, if there default args to post process.  */
15724   cp_parser_save_default_args (parser, fn);
15725
15726   /* Save away the tokens that make up the body of the
15727      function.  */
15728   first = parser->lexer->next_token;
15729   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15730   /* Handle function try blocks.  */
15731   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15732     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15733   last = parser->lexer->next_token;
15734
15735   /* Save away the inline definition; we will process it when the
15736      class is complete.  */
15737   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15738   DECL_PENDING_INLINE_P (fn) = 1;
15739
15740   /* We need to know that this was defined in the class, so that
15741      friend templates are handled correctly.  */
15742   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15743
15744   /* We're done with the inline definition.  */
15745   finish_method (fn);
15746
15747   /* Add FN to the queue of functions to be parsed later.  */
15748   TREE_VALUE (parser->unparsed_functions_queues)
15749     = tree_cons (NULL_TREE, fn,
15750                  TREE_VALUE (parser->unparsed_functions_queues));
15751
15752   return fn;
15753 }
15754
15755 /* Parse a template-argument-list, as well as the trailing ">" (but
15756    not the opening ">").  See cp_parser_template_argument_list for the
15757    return value.  */
15758
15759 static tree
15760 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15761 {
15762   tree arguments;
15763   tree saved_scope;
15764   tree saved_qualifying_scope;
15765   tree saved_object_scope;
15766   bool saved_greater_than_is_operator_p;
15767   bool saved_skip_evaluation;
15768
15769   /* [temp.names]
15770
15771      When parsing a template-id, the first non-nested `>' is taken as
15772      the end of the template-argument-list rather than a greater-than
15773      operator.  */
15774   saved_greater_than_is_operator_p
15775     = parser->greater_than_is_operator_p;
15776   parser->greater_than_is_operator_p = false;
15777   /* Parsing the argument list may modify SCOPE, so we save it
15778      here.  */
15779   saved_scope = parser->scope;
15780   saved_qualifying_scope = parser->qualifying_scope;
15781   saved_object_scope = parser->object_scope;
15782   /* We need to evaluate the template arguments, even though this
15783      template-id may be nested within a "sizeof".  */
15784   saved_skip_evaluation = skip_evaluation;
15785   skip_evaluation = false;
15786   /* Parse the template-argument-list itself.  */
15787   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15788     arguments = NULL_TREE;
15789   else
15790     arguments = cp_parser_template_argument_list (parser);
15791   /* Look for the `>' that ends the template-argument-list. If we find
15792      a '>>' instead, it's probably just a typo.  */
15793   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15794     {
15795       if (!saved_greater_than_is_operator_p)
15796         {
15797           /* If we're in a nested template argument list, the '>>' has
15798             to be a typo for '> >'. We emit the error message, but we
15799             continue parsing and we push a '>' as next token, so that
15800             the argument list will be parsed correctly.  Note that the
15801             global source location is still on the token before the
15802             '>>', so we need to say explicitly where we want it.  */
15803           cp_token *token = cp_lexer_peek_token (parser->lexer);
15804           error ("%H%<>>%> should be %<> >%> "
15805                  "within a nested template argument list",
15806                  &token->location);
15807
15808           /* ??? Proper recovery should terminate two levels of
15809              template argument list here.  */
15810           token->type = CPP_GREATER;
15811         }
15812       else
15813         {
15814           /* If this is not a nested template argument list, the '>>'
15815             is a typo for '>'. Emit an error message and continue.
15816             Same deal about the token location, but here we can get it
15817             right by consuming the '>>' before issuing the diagnostic.  */
15818           cp_lexer_consume_token (parser->lexer);
15819           error ("spurious %<>>%>, use %<>%> to terminate "
15820                  "a template argument list");
15821         }
15822     }
15823   else
15824     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15825   /* The `>' token might be a greater-than operator again now.  */
15826   parser->greater_than_is_operator_p
15827     = saved_greater_than_is_operator_p;
15828   /* Restore the SAVED_SCOPE.  */
15829   parser->scope = saved_scope;
15830   parser->qualifying_scope = saved_qualifying_scope;
15831   parser->object_scope = saved_object_scope;
15832   skip_evaluation = saved_skip_evaluation;
15833
15834   return arguments;
15835 }
15836
15837 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15838    arguments, or the body of the function have not yet been parsed,
15839    parse them now.  */
15840
15841 static void
15842 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15843 {
15844   /* If this member is a template, get the underlying
15845      FUNCTION_DECL.  */
15846   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15847     member_function = DECL_TEMPLATE_RESULT (member_function);
15848
15849   /* There should not be any class definitions in progress at this
15850      point; the bodies of members are only parsed outside of all class
15851      definitions.  */
15852   gcc_assert (parser->num_classes_being_defined == 0);
15853   /* While we're parsing the member functions we might encounter more
15854      classes.  We want to handle them right away, but we don't want
15855      them getting mixed up with functions that are currently in the
15856      queue.  */
15857   parser->unparsed_functions_queues
15858     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15859
15860   /* Make sure that any template parameters are in scope.  */
15861   maybe_begin_member_template_processing (member_function);
15862
15863   /* If the body of the function has not yet been parsed, parse it
15864      now.  */
15865   if (DECL_PENDING_INLINE_P (member_function))
15866     {
15867       tree function_scope;
15868       cp_token_cache *tokens;
15869
15870       /* The function is no longer pending; we are processing it.  */
15871       tokens = DECL_PENDING_INLINE_INFO (member_function);
15872       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15873       DECL_PENDING_INLINE_P (member_function) = 0;
15874
15875       /* If this is a local class, enter the scope of the containing
15876          function.  */
15877       function_scope = current_function_decl;
15878       if (function_scope)
15879         push_function_context_to (function_scope);
15880
15881
15882       /* Push the body of the function onto the lexer stack.  */
15883       cp_parser_push_lexer_for_tokens (parser, tokens);
15884
15885       /* Let the front end know that we going to be defining this
15886          function.  */
15887       start_preparsed_function (member_function, NULL_TREE,
15888                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15889
15890       /* Don't do access checking if it is a templated function.  */
15891       if (processing_template_decl)
15892         push_deferring_access_checks (dk_no_check);
15893
15894       /* Now, parse the body of the function.  */
15895       cp_parser_function_definition_after_declarator (parser,
15896                                                       /*inline_p=*/true);
15897
15898       if (processing_template_decl)
15899         pop_deferring_access_checks ();
15900
15901       /* Leave the scope of the containing function.  */
15902       if (function_scope)
15903         pop_function_context_from (function_scope);
15904       cp_parser_pop_lexer (parser);
15905     }
15906
15907   /* Remove any template parameters from the symbol table.  */
15908   maybe_end_member_template_processing ();
15909
15910   /* Restore the queue.  */
15911   parser->unparsed_functions_queues
15912     = TREE_CHAIN (parser->unparsed_functions_queues);
15913 }
15914
15915 /* If DECL contains any default args, remember it on the unparsed
15916    functions queue.  */
15917
15918 static void
15919 cp_parser_save_default_args (cp_parser* parser, tree decl)
15920 {
15921   tree probe;
15922
15923   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15924        probe;
15925        probe = TREE_CHAIN (probe))
15926     if (TREE_PURPOSE (probe))
15927       {
15928         TREE_PURPOSE (parser->unparsed_functions_queues)
15929           = tree_cons (current_class_type, decl,
15930                        TREE_PURPOSE (parser->unparsed_functions_queues));
15931         break;
15932       }
15933 }
15934
15935 /* FN is a FUNCTION_DECL which may contains a parameter with an
15936    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15937    assumes that the current scope is the scope in which the default
15938    argument should be processed.  */
15939
15940 static void
15941 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15942 {
15943   bool saved_local_variables_forbidden_p;
15944   tree parm;
15945
15946   /* While we're parsing the default args, we might (due to the
15947      statement expression extension) encounter more classes.  We want
15948      to handle them right away, but we don't want them getting mixed
15949      up with default args that are currently in the queue.  */
15950   parser->unparsed_functions_queues
15951     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15952
15953   /* Local variable names (and the `this' keyword) may not appear
15954      in a default argument.  */
15955   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15956   parser->local_variables_forbidden_p = true;
15957
15958   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15959        parm;
15960        parm = TREE_CHAIN (parm))
15961     {
15962       cp_token_cache *tokens;
15963       tree default_arg = TREE_PURPOSE (parm);
15964       tree parsed_arg;
15965       VEC(tree,gc) *insts;
15966       tree copy;
15967       unsigned ix;
15968
15969       if (!default_arg)
15970         continue;
15971
15972       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15973         /* This can happen for a friend declaration for a function
15974            already declared with default arguments.  */
15975         continue;
15976
15977        /* Push the saved tokens for the default argument onto the parser's
15978           lexer stack.  */
15979       tokens = DEFARG_TOKENS (default_arg);
15980       cp_parser_push_lexer_for_tokens (parser, tokens);
15981
15982       /* Parse the assignment-expression.  */
15983       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15984
15985       if (!processing_template_decl)
15986         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15987       
15988       TREE_PURPOSE (parm) = parsed_arg;
15989
15990       /* Update any instantiations we've already created.  */
15991       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15992            VEC_iterate (tree, insts, ix, copy); ix++)
15993         TREE_PURPOSE (copy) = parsed_arg;
15994
15995       /* If the token stream has not been completely used up, then
15996          there was extra junk after the end of the default
15997          argument.  */
15998       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15999         cp_parser_error (parser, "expected %<,%>");
16000
16001       /* Revert to the main lexer.  */
16002       cp_parser_pop_lexer (parser);
16003     }
16004
16005   /* Make sure no default arg is missing.  */
16006   check_default_args (fn);
16007
16008   /* Restore the state of local_variables_forbidden_p.  */
16009   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16010
16011   /* Restore the queue.  */
16012   parser->unparsed_functions_queues
16013     = TREE_CHAIN (parser->unparsed_functions_queues);
16014 }
16015
16016 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16017    either a TYPE or an expression, depending on the form of the
16018    input.  The KEYWORD indicates which kind of expression we have
16019    encountered.  */
16020
16021 static tree
16022 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16023 {
16024   static const char *format;
16025   tree expr = NULL_TREE;
16026   const char *saved_message;
16027   bool saved_integral_constant_expression_p;
16028   bool saved_non_integral_constant_expression_p;
16029
16030   /* Initialize FORMAT the first time we get here.  */
16031   if (!format)
16032     format = "types may not be defined in '%s' expressions";
16033
16034   /* Types cannot be defined in a `sizeof' expression.  Save away the
16035      old message.  */
16036   saved_message = parser->type_definition_forbidden_message;
16037   /* And create the new one.  */
16038   parser->type_definition_forbidden_message
16039     = XNEWVEC (const char, strlen (format)
16040                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16041                + 1 /* `\0' */);
16042   sprintf ((char *) parser->type_definition_forbidden_message,
16043            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16044
16045   /* The restrictions on constant-expressions do not apply inside
16046      sizeof expressions.  */
16047   saved_integral_constant_expression_p
16048     = parser->integral_constant_expression_p;
16049   saved_non_integral_constant_expression_p
16050     = parser->non_integral_constant_expression_p;
16051   parser->integral_constant_expression_p = false;
16052
16053   /* Do not actually evaluate the expression.  */
16054   ++skip_evaluation;
16055   /* If it's a `(', then we might be looking at the type-id
16056      construction.  */
16057   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16058     {
16059       tree type;
16060       bool saved_in_type_id_in_expr_p;
16061
16062       /* We can't be sure yet whether we're looking at a type-id or an
16063          expression.  */
16064       cp_parser_parse_tentatively (parser);
16065       /* Consume the `('.  */
16066       cp_lexer_consume_token (parser->lexer);
16067       /* Parse the type-id.  */
16068       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16069       parser->in_type_id_in_expr_p = true;
16070       type = cp_parser_type_id (parser);
16071       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16072       /* Now, look for the trailing `)'.  */
16073       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16074       /* If all went well, then we're done.  */
16075       if (cp_parser_parse_definitely (parser))
16076         {
16077           cp_decl_specifier_seq decl_specs;
16078
16079           /* Build a trivial decl-specifier-seq.  */
16080           clear_decl_specs (&decl_specs);
16081           decl_specs.type = type;
16082
16083           /* Call grokdeclarator to figure out what type this is.  */
16084           expr = grokdeclarator (NULL,
16085                                  &decl_specs,
16086                                  TYPENAME,
16087                                  /*initialized=*/0,
16088                                  /*attrlist=*/NULL);
16089         }
16090     }
16091
16092   /* If the type-id production did not work out, then we must be
16093      looking at the unary-expression production.  */
16094   if (!expr)
16095     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16096                                        /*cast_p=*/false);
16097   /* Go back to evaluating expressions.  */
16098   --skip_evaluation;
16099
16100   /* Free the message we created.  */
16101   free ((char *) parser->type_definition_forbidden_message);
16102   /* And restore the old one.  */
16103   parser->type_definition_forbidden_message = saved_message;
16104   parser->integral_constant_expression_p
16105     = saved_integral_constant_expression_p;
16106   parser->non_integral_constant_expression_p
16107     = saved_non_integral_constant_expression_p;
16108
16109   return expr;
16110 }
16111
16112 /* If the current declaration has no declarator, return true.  */
16113
16114 static bool
16115 cp_parser_declares_only_class_p (cp_parser *parser)
16116 {
16117   /* If the next token is a `;' or a `,' then there is no
16118      declarator.  */
16119   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16120           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16121 }
16122
16123 /* Update the DECL_SPECS to reflect the storage class indicated by
16124    KEYWORD.  */
16125
16126 static void
16127 cp_parser_set_storage_class (cp_parser *parser,
16128                              cp_decl_specifier_seq *decl_specs,
16129                              enum rid keyword)
16130 {
16131   cp_storage_class storage_class;
16132
16133   if (parser->in_unbraced_linkage_specification_p)
16134     {
16135       error ("invalid use of %qD in linkage specification",
16136              ridpointers[keyword]);
16137       return;
16138     }
16139   else if (decl_specs->storage_class != sc_none)
16140     {
16141       decl_specs->multiple_storage_classes_p = true;
16142       return;
16143     }
16144
16145   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16146       && decl_specs->specs[(int) ds_thread])
16147     {
16148       error ("%<__thread%> before %qD", ridpointers[keyword]);
16149       decl_specs->specs[(int) ds_thread] = 0;
16150     }
16151
16152   switch (keyword) 
16153     {
16154     case RID_AUTO:
16155       storage_class = sc_auto;
16156       break;
16157     case RID_REGISTER:
16158       storage_class = sc_register;
16159       break;
16160     case RID_STATIC:
16161       storage_class = sc_static;
16162       break;
16163     case RID_EXTERN:
16164       storage_class = sc_extern;
16165       break;
16166     case RID_MUTABLE:
16167       storage_class = sc_mutable;
16168       break;
16169     default:
16170       gcc_unreachable ();
16171     }
16172   decl_specs->storage_class = storage_class;
16173 }
16174
16175 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16176    is true, the type is a user-defined type; otherwise it is a
16177    built-in type specified by a keyword.  */
16178
16179 static void
16180 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16181                               tree type_spec,
16182                               bool user_defined_p)
16183 {
16184   decl_specs->any_specifiers_p = true;
16185
16186   /* If the user tries to redeclare bool or wchar_t (with, for
16187      example, in "typedef int wchar_t;") we remember that this is what
16188      happened.  In system headers, we ignore these declarations so
16189      that G++ can work with system headers that are not C++-safe.  */
16190   if (decl_specs->specs[(int) ds_typedef]
16191       && !user_defined_p
16192       && (type_spec == boolean_type_node
16193           || type_spec == wchar_type_node)
16194       && (decl_specs->type
16195           || decl_specs->specs[(int) ds_long]
16196           || decl_specs->specs[(int) ds_short]
16197           || decl_specs->specs[(int) ds_unsigned]
16198           || decl_specs->specs[(int) ds_signed]))
16199     {
16200       decl_specs->redefined_builtin_type = type_spec;
16201       if (!decl_specs->type)
16202         {
16203           decl_specs->type = type_spec;
16204           decl_specs->user_defined_type_p = false;
16205         }
16206     }
16207   else if (decl_specs->type)
16208     decl_specs->multiple_types_p = true;
16209   else
16210     {
16211       decl_specs->type = type_spec;
16212       decl_specs->user_defined_type_p = user_defined_p;
16213       decl_specs->redefined_builtin_type = NULL_TREE;
16214     }
16215 }
16216
16217 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16218    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16219
16220 static bool
16221 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16222 {
16223   return decl_specifiers->specs[(int) ds_friend] != 0;
16224 }
16225
16226 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16227    issue an error message indicating that TOKEN_DESC was expected.
16228
16229    Returns the token consumed, if the token had the appropriate type.
16230    Otherwise, returns NULL.  */
16231
16232 static cp_token *
16233 cp_parser_require (cp_parser* parser,
16234                    enum cpp_ttype type,
16235                    const char* token_desc)
16236 {
16237   if (cp_lexer_next_token_is (parser->lexer, type))
16238     return cp_lexer_consume_token (parser->lexer);
16239   else
16240     {
16241       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16242       if (!cp_parser_simulate_error (parser))
16243         {
16244           char *message = concat ("expected ", token_desc, NULL);
16245           cp_parser_error (parser, message);
16246           free (message);
16247         }
16248       return NULL;
16249     }
16250 }
16251
16252 /* Like cp_parser_require, except that tokens will be skipped until
16253    the desired token is found.  An error message is still produced if
16254    the next token is not as expected.  */
16255
16256 static void
16257 cp_parser_skip_until_found (cp_parser* parser,
16258                             enum cpp_ttype type,
16259                             const char* token_desc)
16260 {
16261   cp_token *token;
16262   unsigned nesting_depth = 0;
16263
16264   if (cp_parser_require (parser, type, token_desc))
16265     return;
16266
16267   /* Skip tokens until the desired token is found.  */
16268   while (true)
16269     {
16270       /* Peek at the next token.  */
16271       token = cp_lexer_peek_token (parser->lexer);
16272
16273       /* If we've reached the token we want, consume it and stop.  */
16274       if (token->type == type && !nesting_depth)
16275         {
16276           cp_lexer_consume_token (parser->lexer);
16277           return;
16278         }
16279
16280       switch (token->type)
16281         {
16282         case CPP_EOF:
16283         case CPP_PRAGMA_EOL:
16284           /* If we've run out of tokens, stop.  */
16285           return;
16286
16287         case CPP_OPEN_BRACE:
16288         case CPP_OPEN_PAREN:
16289         case CPP_OPEN_SQUARE:
16290           ++nesting_depth;
16291           break;
16292
16293         case CPP_CLOSE_BRACE:
16294         case CPP_CLOSE_PAREN:
16295         case CPP_CLOSE_SQUARE:
16296           if (nesting_depth-- == 0)
16297             return;
16298           break;
16299
16300         default:
16301           break;
16302         }
16303
16304       /* Consume this token.  */
16305       cp_lexer_consume_token (parser->lexer);
16306     }
16307 }
16308
16309 /* If the next token is the indicated keyword, consume it.  Otherwise,
16310    issue an error message indicating that TOKEN_DESC was expected.
16311
16312    Returns the token consumed, if the token had the appropriate type.
16313    Otherwise, returns NULL.  */
16314
16315 static cp_token *
16316 cp_parser_require_keyword (cp_parser* parser,
16317                            enum rid keyword,
16318                            const char* token_desc)
16319 {
16320   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16321
16322   if (token && token->keyword != keyword)
16323     {
16324       dyn_string_t error_msg;
16325
16326       /* Format the error message.  */
16327       error_msg = dyn_string_new (0);
16328       dyn_string_append_cstr (error_msg, "expected ");
16329       dyn_string_append_cstr (error_msg, token_desc);
16330       cp_parser_error (parser, error_msg->s);
16331       dyn_string_delete (error_msg);
16332       return NULL;
16333     }
16334
16335   return token;
16336 }
16337
16338 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16339    function-definition.  */
16340
16341 static bool
16342 cp_parser_token_starts_function_definition_p (cp_token* token)
16343 {
16344   return (/* An ordinary function-body begins with an `{'.  */
16345           token->type == CPP_OPEN_BRACE
16346           /* A ctor-initializer begins with a `:'.  */
16347           || token->type == CPP_COLON
16348           /* A function-try-block begins with `try'.  */
16349           || token->keyword == RID_TRY
16350           /* The named return value extension begins with `return'.  */
16351           || token->keyword == RID_RETURN);
16352 }
16353
16354 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16355    definition.  */
16356
16357 static bool
16358 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16359 {
16360   cp_token *token;
16361
16362   token = cp_lexer_peek_token (parser->lexer);
16363   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16364 }
16365
16366 /* Returns TRUE iff the next token is the "," or ">" ending a
16367    template-argument.  */
16368
16369 static bool
16370 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16371 {
16372   cp_token *token;
16373
16374   token = cp_lexer_peek_token (parser->lexer);
16375   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16376 }
16377
16378 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16379    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16380
16381 static bool
16382 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16383                                                      size_t n)
16384 {
16385   cp_token *token;
16386
16387   token = cp_lexer_peek_nth_token (parser->lexer, n);
16388   if (token->type == CPP_LESS)
16389     return true;
16390   /* Check for the sequence `<::' in the original code. It would be lexed as
16391      `[:', where `[' is a digraph, and there is no whitespace before
16392      `:'.  */
16393   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16394     {
16395       cp_token *token2;
16396       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16397       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16398         return true;
16399     }
16400   return false;
16401 }
16402
16403 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16404    or none_type otherwise.  */
16405
16406 static enum tag_types
16407 cp_parser_token_is_class_key (cp_token* token)
16408 {
16409   switch (token->keyword)
16410     {
16411     case RID_CLASS:
16412       return class_type;
16413     case RID_STRUCT:
16414       return record_type;
16415     case RID_UNION:
16416       return union_type;
16417
16418     default:
16419       return none_type;
16420     }
16421 }
16422
16423 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16424
16425 static void
16426 cp_parser_check_class_key (enum tag_types class_key, tree type)
16427 {
16428   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16429     pedwarn ("%qs tag used in naming %q#T",
16430             class_key == union_type ? "union"
16431              : class_key == record_type ? "struct" : "class",
16432              type);
16433 }
16434
16435 /* Issue an error message if DECL is redeclared with different
16436    access than its original declaration [class.access.spec/3].
16437    This applies to nested classes and nested class templates.
16438    [class.mem/1].  */
16439
16440 static void
16441 cp_parser_check_access_in_redeclaration (tree decl)
16442 {
16443   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16444     return;
16445
16446   if ((TREE_PRIVATE (decl)
16447        != (current_access_specifier == access_private_node))
16448       || (TREE_PROTECTED (decl)
16449           != (current_access_specifier == access_protected_node)))
16450     error ("%qD redeclared with different access", decl);
16451 }
16452
16453 /* Look for the `template' keyword, as a syntactic disambiguator.
16454    Return TRUE iff it is present, in which case it will be
16455    consumed.  */
16456
16457 static bool
16458 cp_parser_optional_template_keyword (cp_parser *parser)
16459 {
16460   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16461     {
16462       /* The `template' keyword can only be used within templates;
16463          outside templates the parser can always figure out what is a
16464          template and what is not.  */
16465       if (!processing_template_decl)
16466         {
16467           error ("%<template%> (as a disambiguator) is only allowed "
16468                  "within templates");
16469           /* If this part of the token stream is rescanned, the same
16470              error message would be generated.  So, we purge the token
16471              from the stream.  */
16472           cp_lexer_purge_token (parser->lexer);
16473           return false;
16474         }
16475       else
16476         {
16477           /* Consume the `template' keyword.  */
16478           cp_lexer_consume_token (parser->lexer);
16479           return true;
16480         }
16481     }
16482
16483   return false;
16484 }
16485
16486 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16487    set PARSER->SCOPE, and perform other related actions.  */
16488
16489 static void
16490 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16491 {
16492   tree value;
16493   tree check;
16494
16495   /* Get the stored value.  */
16496   value = cp_lexer_consume_token (parser->lexer)->value;
16497   /* Perform any access checks that were deferred.  */
16498   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16499     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16500   /* Set the scope from the stored value.  */
16501   parser->scope = TREE_VALUE (value);
16502   parser->qualifying_scope = TREE_TYPE (value);
16503   parser->object_scope = NULL_TREE;
16504 }
16505
16506 /* Consume tokens up through a non-nested END token.  */
16507
16508 static void
16509 cp_parser_cache_group (cp_parser *parser,
16510                        enum cpp_ttype end,
16511                        unsigned depth)
16512 {
16513   while (true)
16514     {
16515       cp_token *token;
16516
16517       /* Abort a parenthesized expression if we encounter a brace.  */
16518       if ((end == CPP_CLOSE_PAREN || depth == 0)
16519           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16520         return;
16521       /* If we've reached the end of the file, stop.  */
16522       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16523           || (end != CPP_PRAGMA_EOL
16524               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16525         return;
16526       /* Consume the next token.  */
16527       token = cp_lexer_consume_token (parser->lexer);
16528       /* See if it starts a new group.  */
16529       if (token->type == CPP_OPEN_BRACE)
16530         {
16531           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16532           if (depth == 0)
16533             return;
16534         }
16535       else if (token->type == CPP_OPEN_PAREN)
16536         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16537       else if (token->type == CPP_PRAGMA)
16538         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16539       else if (token->type == end)
16540         return;
16541     }
16542 }
16543
16544 /* Begin parsing tentatively.  We always save tokens while parsing
16545    tentatively so that if the tentative parsing fails we can restore the
16546    tokens.  */
16547
16548 static void
16549 cp_parser_parse_tentatively (cp_parser* parser)
16550 {
16551   /* Enter a new parsing context.  */
16552   parser->context = cp_parser_context_new (parser->context);
16553   /* Begin saving tokens.  */
16554   cp_lexer_save_tokens (parser->lexer);
16555   /* In order to avoid repetitive access control error messages,
16556      access checks are queued up until we are no longer parsing
16557      tentatively.  */
16558   push_deferring_access_checks (dk_deferred);
16559 }
16560
16561 /* Commit to the currently active tentative parse.  */
16562
16563 static void
16564 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16565 {
16566   cp_parser_context *context;
16567   cp_lexer *lexer;
16568
16569   /* Mark all of the levels as committed.  */
16570   lexer = parser->lexer;
16571   for (context = parser->context; context->next; context = context->next)
16572     {
16573       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16574         break;
16575       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16576       while (!cp_lexer_saving_tokens (lexer))
16577         lexer = lexer->next;
16578       cp_lexer_commit_tokens (lexer);
16579     }
16580 }
16581
16582 /* Abort the currently active tentative parse.  All consumed tokens
16583    will be rolled back, and no diagnostics will be issued.  */
16584
16585 static void
16586 cp_parser_abort_tentative_parse (cp_parser* parser)
16587 {
16588   cp_parser_simulate_error (parser);
16589   /* Now, pretend that we want to see if the construct was
16590      successfully parsed.  */
16591   cp_parser_parse_definitely (parser);
16592 }
16593
16594 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16595    token stream.  Otherwise, commit to the tokens we have consumed.
16596    Returns true if no error occurred; false otherwise.  */
16597
16598 static bool
16599 cp_parser_parse_definitely (cp_parser* parser)
16600 {
16601   bool error_occurred;
16602   cp_parser_context *context;
16603
16604   /* Remember whether or not an error occurred, since we are about to
16605      destroy that information.  */
16606   error_occurred = cp_parser_error_occurred (parser);
16607   /* Remove the topmost context from the stack.  */
16608   context = parser->context;
16609   parser->context = context->next;
16610   /* If no parse errors occurred, commit to the tentative parse.  */
16611   if (!error_occurred)
16612     {
16613       /* Commit to the tokens read tentatively, unless that was
16614          already done.  */
16615       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16616         cp_lexer_commit_tokens (parser->lexer);
16617
16618       pop_to_parent_deferring_access_checks ();
16619     }
16620   /* Otherwise, if errors occurred, roll back our state so that things
16621      are just as they were before we began the tentative parse.  */
16622   else
16623     {
16624       cp_lexer_rollback_tokens (parser->lexer);
16625       pop_deferring_access_checks ();
16626     }
16627   /* Add the context to the front of the free list.  */
16628   context->next = cp_parser_context_free_list;
16629   cp_parser_context_free_list = context;
16630
16631   return !error_occurred;
16632 }
16633
16634 /* Returns true if we are parsing tentatively and are not committed to
16635    this tentative parse.  */
16636
16637 static bool
16638 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16639 {
16640   return (cp_parser_parsing_tentatively (parser)
16641           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16642 }
16643
16644 /* Returns nonzero iff an error has occurred during the most recent
16645    tentative parse.  */
16646
16647 static bool
16648 cp_parser_error_occurred (cp_parser* parser)
16649 {
16650   return (cp_parser_parsing_tentatively (parser)
16651           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16652 }
16653
16654 /* Returns nonzero if GNU extensions are allowed.  */
16655
16656 static bool
16657 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16658 {
16659   return parser->allow_gnu_extensions_p;
16660 }
16661 \f
16662 /* Objective-C++ Productions */
16663
16664
16665 /* Parse an Objective-C expression, which feeds into a primary-expression
16666    above.
16667
16668    objc-expression:
16669      objc-message-expression
16670      objc-string-literal
16671      objc-encode-expression
16672      objc-protocol-expression
16673      objc-selector-expression
16674
16675   Returns a tree representation of the expression.  */
16676
16677 static tree
16678 cp_parser_objc_expression (cp_parser* parser)
16679 {
16680   /* Try to figure out what kind of declaration is present.  */
16681   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16682
16683   switch (kwd->type)
16684     {
16685     case CPP_OPEN_SQUARE:
16686       return cp_parser_objc_message_expression (parser);
16687
16688     case CPP_OBJC_STRING:
16689       kwd = cp_lexer_consume_token (parser->lexer);
16690       return objc_build_string_object (kwd->value);
16691
16692     case CPP_KEYWORD:
16693       switch (kwd->keyword)
16694         {
16695         case RID_AT_ENCODE:
16696           return cp_parser_objc_encode_expression (parser);
16697
16698         case RID_AT_PROTOCOL:
16699           return cp_parser_objc_protocol_expression (parser);
16700
16701         case RID_AT_SELECTOR:
16702           return cp_parser_objc_selector_expression (parser);
16703
16704         default:
16705           break;
16706         }
16707     default:
16708       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16709       cp_parser_skip_to_end_of_block_or_statement (parser);
16710     }
16711
16712   return error_mark_node;
16713 }
16714
16715 /* Parse an Objective-C message expression.
16716
16717    objc-message-expression:
16718      [ objc-message-receiver objc-message-args ]
16719
16720    Returns a representation of an Objective-C message.  */
16721
16722 static tree
16723 cp_parser_objc_message_expression (cp_parser* parser)
16724 {
16725   tree receiver, messageargs;
16726
16727   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16728   receiver = cp_parser_objc_message_receiver (parser);
16729   messageargs = cp_parser_objc_message_args (parser);
16730   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16731
16732   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16733 }
16734
16735 /* Parse an objc-message-receiver.
16736
16737    objc-message-receiver:
16738      expression
16739      simple-type-specifier
16740
16741   Returns a representation of the type or expression.  */
16742
16743 static tree
16744 cp_parser_objc_message_receiver (cp_parser* parser)
16745 {
16746   tree rcv;
16747
16748   /* An Objective-C message receiver may be either (1) a type
16749      or (2) an expression.  */
16750   cp_parser_parse_tentatively (parser);
16751   rcv = cp_parser_expression (parser, false);
16752
16753   if (cp_parser_parse_definitely (parser))
16754     return rcv;
16755
16756   rcv = cp_parser_simple_type_specifier (parser,
16757                                          /*decl_specs=*/NULL,
16758                                          CP_PARSER_FLAGS_NONE);
16759
16760   return objc_get_class_reference (rcv);
16761 }
16762
16763 /* Parse the arguments and selectors comprising an Objective-C message.
16764
16765    objc-message-args:
16766      objc-selector
16767      objc-selector-args
16768      objc-selector-args , objc-comma-args
16769
16770    objc-selector-args:
16771      objc-selector [opt] : assignment-expression
16772      objc-selector-args objc-selector [opt] : assignment-expression
16773
16774    objc-comma-args:
16775      assignment-expression
16776      objc-comma-args , assignment-expression
16777
16778    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16779    selector arguments and TREE_VALUE containing a list of comma
16780    arguments.  */
16781
16782 static tree
16783 cp_parser_objc_message_args (cp_parser* parser)
16784 {
16785   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16786   bool maybe_unary_selector_p = true;
16787   cp_token *token = cp_lexer_peek_token (parser->lexer);
16788
16789   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16790     {
16791       tree selector = NULL_TREE, arg;
16792
16793       if (token->type != CPP_COLON)
16794         selector = cp_parser_objc_selector (parser);
16795
16796       /* Detect if we have a unary selector.  */
16797       if (maybe_unary_selector_p
16798           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16799         return build_tree_list (selector, NULL_TREE);
16800
16801       maybe_unary_selector_p = false;
16802       cp_parser_require (parser, CPP_COLON, "`:'");
16803       arg = cp_parser_assignment_expression (parser, false);
16804
16805       sel_args
16806         = chainon (sel_args,
16807                    build_tree_list (selector, arg));
16808
16809       token = cp_lexer_peek_token (parser->lexer);
16810     }
16811
16812   /* Handle non-selector arguments, if any. */
16813   while (token->type == CPP_COMMA)
16814     {
16815       tree arg;
16816
16817       cp_lexer_consume_token (parser->lexer);
16818       arg = cp_parser_assignment_expression (parser, false);
16819
16820       addl_args
16821         = chainon (addl_args,
16822                    build_tree_list (NULL_TREE, arg));
16823
16824       token = cp_lexer_peek_token (parser->lexer);
16825     }
16826
16827   return build_tree_list (sel_args, addl_args);
16828 }
16829
16830 /* Parse an Objective-C encode expression.
16831
16832    objc-encode-expression:
16833      @encode objc-typename
16834
16835    Returns an encoded representation of the type argument.  */
16836
16837 static tree
16838 cp_parser_objc_encode_expression (cp_parser* parser)
16839 {
16840   tree type;
16841
16842   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16843   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16844   type = complete_type (cp_parser_type_id (parser));
16845   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16846
16847   if (!type)
16848     {
16849       error ("%<@encode%> must specify a type as an argument");
16850       return error_mark_node;
16851     }
16852
16853   return objc_build_encode_expr (type);
16854 }
16855
16856 /* Parse an Objective-C @defs expression.  */
16857
16858 static tree
16859 cp_parser_objc_defs_expression (cp_parser *parser)
16860 {
16861   tree name;
16862
16863   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16864   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16865   name = cp_parser_identifier (parser);
16866   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16867
16868   return objc_get_class_ivars (name);
16869 }
16870
16871 /* Parse an Objective-C protocol expression.
16872
16873   objc-protocol-expression:
16874     @protocol ( identifier )
16875
16876   Returns a representation of the protocol expression.  */
16877
16878 static tree
16879 cp_parser_objc_protocol_expression (cp_parser* parser)
16880 {
16881   tree proto;
16882
16883   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16884   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16885   proto = cp_parser_identifier (parser);
16886   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16887
16888   return objc_build_protocol_expr (proto);
16889 }
16890
16891 /* Parse an Objective-C selector expression.
16892
16893    objc-selector-expression:
16894      @selector ( objc-method-signature )
16895
16896    objc-method-signature:
16897      objc-selector
16898      objc-selector-seq
16899
16900    objc-selector-seq:
16901      objc-selector :
16902      objc-selector-seq objc-selector :
16903
16904   Returns a representation of the method selector.  */
16905
16906 static tree
16907 cp_parser_objc_selector_expression (cp_parser* parser)
16908 {
16909   tree sel_seq = NULL_TREE;
16910   bool maybe_unary_selector_p = true;
16911   cp_token *token;
16912
16913   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16914   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16915   token = cp_lexer_peek_token (parser->lexer);
16916
16917   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16918          || token->type == CPP_SCOPE)
16919     {
16920       tree selector = NULL_TREE;
16921
16922       if (token->type != CPP_COLON
16923           || token->type == CPP_SCOPE)
16924         selector = cp_parser_objc_selector (parser);
16925
16926       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16927           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16928         {
16929           /* Detect if we have a unary selector.  */
16930           if (maybe_unary_selector_p)
16931             {
16932               sel_seq = selector;
16933               goto finish_selector;
16934             }
16935           else
16936             {
16937               cp_parser_error (parser, "expected %<:%>");
16938             }
16939         }
16940       maybe_unary_selector_p = false;
16941       token = cp_lexer_consume_token (parser->lexer);
16942       
16943       if (token->type == CPP_SCOPE)
16944         {
16945           sel_seq
16946             = chainon (sel_seq,
16947                        build_tree_list (selector, NULL_TREE));
16948           sel_seq
16949             = chainon (sel_seq,
16950                        build_tree_list (NULL_TREE, NULL_TREE));
16951         }
16952       else
16953         sel_seq
16954           = chainon (sel_seq,
16955                      build_tree_list (selector, NULL_TREE));
16956
16957       token = cp_lexer_peek_token (parser->lexer);
16958     }
16959
16960  finish_selector:
16961   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16962
16963   return objc_build_selector_expr (sel_seq);
16964 }
16965
16966 /* Parse a list of identifiers.
16967
16968    objc-identifier-list:
16969      identifier
16970      objc-identifier-list , identifier
16971
16972    Returns a TREE_LIST of identifier nodes.  */
16973
16974 static tree
16975 cp_parser_objc_identifier_list (cp_parser* parser)
16976 {
16977   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16978   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16979
16980   while (sep->type == CPP_COMMA)
16981     {
16982       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16983       list = chainon (list,
16984                       build_tree_list (NULL_TREE,
16985                                        cp_parser_identifier (parser)));
16986       sep = cp_lexer_peek_token (parser->lexer);
16987     }
16988
16989   return list;
16990 }
16991
16992 /* Parse an Objective-C alias declaration.
16993
16994    objc-alias-declaration:
16995      @compatibility_alias identifier identifier ;
16996
16997    This function registers the alias mapping with the Objective-C front-end.
16998    It returns nothing.  */
16999
17000 static void
17001 cp_parser_objc_alias_declaration (cp_parser* parser)
17002 {
17003   tree alias, orig;
17004
17005   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17006   alias = cp_parser_identifier (parser);
17007   orig = cp_parser_identifier (parser);
17008   objc_declare_alias (alias, orig);
17009   cp_parser_consume_semicolon_at_end_of_statement (parser);
17010 }
17011
17012 /* Parse an Objective-C class forward-declaration.
17013
17014    objc-class-declaration:
17015      @class objc-identifier-list ;
17016
17017    The function registers the forward declarations with the Objective-C
17018    front-end.  It returns nothing.  */
17019
17020 static void
17021 cp_parser_objc_class_declaration (cp_parser* parser)
17022 {
17023   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17024   objc_declare_class (cp_parser_objc_identifier_list (parser));
17025   cp_parser_consume_semicolon_at_end_of_statement (parser);
17026 }
17027
17028 /* Parse a list of Objective-C protocol references.
17029
17030    objc-protocol-refs-opt:
17031      objc-protocol-refs [opt]
17032
17033    objc-protocol-refs:
17034      < objc-identifier-list >
17035
17036    Returns a TREE_LIST of identifiers, if any.  */
17037
17038 static tree
17039 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17040 {
17041   tree protorefs = NULL_TREE;
17042
17043   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17044     {
17045       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17046       protorefs = cp_parser_objc_identifier_list (parser);
17047       cp_parser_require (parser, CPP_GREATER, "`>'");
17048     }
17049
17050   return protorefs;
17051 }
17052
17053 /* Parse a Objective-C visibility specification.  */
17054
17055 static void
17056 cp_parser_objc_visibility_spec (cp_parser* parser)
17057 {
17058   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17059
17060   switch (vis->keyword)
17061     {
17062     case RID_AT_PRIVATE:
17063       objc_set_visibility (2);
17064       break;
17065     case RID_AT_PROTECTED:
17066       objc_set_visibility (0);
17067       break;
17068     case RID_AT_PUBLIC:
17069       objc_set_visibility (1);
17070       break;
17071     default:
17072       return;
17073     }
17074
17075   /* Eat '@private'/'@protected'/'@public'.  */
17076   cp_lexer_consume_token (parser->lexer);
17077 }
17078
17079 /* Parse an Objective-C method type.  */
17080
17081 static void
17082 cp_parser_objc_method_type (cp_parser* parser)
17083 {
17084   objc_set_method_type
17085    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17086     ? PLUS_EXPR
17087     : MINUS_EXPR);
17088 }
17089
17090 /* Parse an Objective-C protocol qualifier.  */
17091
17092 static tree
17093 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17094 {
17095   tree quals = NULL_TREE, node;
17096   cp_token *token = cp_lexer_peek_token (parser->lexer);
17097
17098   node = token->value;
17099
17100   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17101          && (node == ridpointers [(int) RID_IN]
17102              || node == ridpointers [(int) RID_OUT]
17103              || node == ridpointers [(int) RID_INOUT]
17104              || node == ridpointers [(int) RID_BYCOPY]
17105              || node == ridpointers [(int) RID_BYREF]
17106              || node == ridpointers [(int) RID_ONEWAY]))
17107     {
17108       quals = tree_cons (NULL_TREE, node, quals);
17109       cp_lexer_consume_token (parser->lexer);
17110       token = cp_lexer_peek_token (parser->lexer);
17111       node = token->value;
17112     }
17113
17114   return quals;
17115 }
17116
17117 /* Parse an Objective-C typename.  */
17118
17119 static tree
17120 cp_parser_objc_typename (cp_parser* parser)
17121 {
17122   tree typename = NULL_TREE;
17123
17124   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17125     {
17126       tree proto_quals, cp_type = NULL_TREE;
17127
17128       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17129       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17130
17131       /* An ObjC type name may consist of just protocol qualifiers, in which
17132          case the type shall default to 'id'.  */
17133       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17134         cp_type = cp_parser_type_id (parser);
17135
17136       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17137       typename = build_tree_list (proto_quals, cp_type);
17138     }
17139
17140   return typename;
17141 }
17142
17143 /* Check to see if TYPE refers to an Objective-C selector name.  */
17144
17145 static bool
17146 cp_parser_objc_selector_p (enum cpp_ttype type)
17147 {
17148   return (type == CPP_NAME || type == CPP_KEYWORD
17149           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17150           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17151           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17152           || type == CPP_XOR || type == CPP_XOR_EQ);
17153 }
17154
17155 /* Parse an Objective-C selector.  */
17156
17157 static tree
17158 cp_parser_objc_selector (cp_parser* parser)
17159 {
17160   cp_token *token = cp_lexer_consume_token (parser->lexer);
17161
17162   if (!cp_parser_objc_selector_p (token->type))
17163     {
17164       error ("invalid Objective-C++ selector name");
17165       return error_mark_node;
17166     }
17167
17168   /* C++ operator names are allowed to appear in ObjC selectors.  */
17169   switch (token->type)
17170     {
17171     case CPP_AND_AND: return get_identifier ("and");
17172     case CPP_AND_EQ: return get_identifier ("and_eq");
17173     case CPP_AND: return get_identifier ("bitand");
17174     case CPP_OR: return get_identifier ("bitor");
17175     case CPP_COMPL: return get_identifier ("compl");
17176     case CPP_NOT: return get_identifier ("not");
17177     case CPP_NOT_EQ: return get_identifier ("not_eq");
17178     case CPP_OR_OR: return get_identifier ("or");
17179     case CPP_OR_EQ: return get_identifier ("or_eq");
17180     case CPP_XOR: return get_identifier ("xor");
17181     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17182     default: return token->value;
17183     }
17184 }
17185
17186 /* Parse an Objective-C params list.  */
17187
17188 static tree
17189 cp_parser_objc_method_keyword_params (cp_parser* parser)
17190 {
17191   tree params = NULL_TREE;
17192   bool maybe_unary_selector_p = true;
17193   cp_token *token = cp_lexer_peek_token (parser->lexer);
17194
17195   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17196     {
17197       tree selector = NULL_TREE, typename, identifier;
17198
17199       if (token->type != CPP_COLON)
17200         selector = cp_parser_objc_selector (parser);
17201
17202       /* Detect if we have a unary selector.  */
17203       if (maybe_unary_selector_p
17204           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17205         return selector;
17206
17207       maybe_unary_selector_p = false;
17208       cp_parser_require (parser, CPP_COLON, "`:'");
17209       typename = cp_parser_objc_typename (parser);
17210       identifier = cp_parser_identifier (parser);
17211
17212       params
17213         = chainon (params,
17214                    objc_build_keyword_decl (selector,
17215                                             typename,
17216                                             identifier));
17217
17218       token = cp_lexer_peek_token (parser->lexer);
17219     }
17220
17221   return params;
17222 }
17223
17224 /* Parse the non-keyword Objective-C params.  */
17225
17226 static tree
17227 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17228 {
17229   tree params = make_node (TREE_LIST);
17230   cp_token *token = cp_lexer_peek_token (parser->lexer);
17231   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17232
17233   while (token->type == CPP_COMMA)
17234     {
17235       cp_parameter_declarator *parmdecl;
17236       tree parm;
17237
17238       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17239       token = cp_lexer_peek_token (parser->lexer);
17240
17241       if (token->type == CPP_ELLIPSIS)
17242         {
17243           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17244           *ellipsisp = true;
17245           break;
17246         }
17247
17248       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17249       parm = grokdeclarator (parmdecl->declarator,
17250                              &parmdecl->decl_specifiers,
17251                              PARM, /*initialized=*/0,
17252                              /*attrlist=*/NULL);
17253
17254       chainon (params, build_tree_list (NULL_TREE, parm));
17255       token = cp_lexer_peek_token (parser->lexer);
17256     }
17257
17258   return params;
17259 }
17260
17261 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17262
17263 static void
17264 cp_parser_objc_interstitial_code (cp_parser* parser)
17265 {
17266   cp_token *token = cp_lexer_peek_token (parser->lexer);
17267
17268   /* If the next token is `extern' and the following token is a string
17269      literal, then we have a linkage specification.  */
17270   if (token->keyword == RID_EXTERN
17271       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17272     cp_parser_linkage_specification (parser);
17273   /* Handle #pragma, if any.  */
17274   else if (token->type == CPP_PRAGMA)
17275     cp_parser_pragma (parser, pragma_external);
17276   /* Allow stray semicolons.  */
17277   else if (token->type == CPP_SEMICOLON)
17278     cp_lexer_consume_token (parser->lexer);
17279   /* Finally, try to parse a block-declaration, or a function-definition.  */
17280   else
17281     cp_parser_block_declaration (parser, /*statement_p=*/false);
17282 }
17283
17284 /* Parse a method signature.  */
17285
17286 static tree
17287 cp_parser_objc_method_signature (cp_parser* parser)
17288 {
17289   tree rettype, kwdparms, optparms;
17290   bool ellipsis = false;
17291
17292   cp_parser_objc_method_type (parser);
17293   rettype = cp_parser_objc_typename (parser);
17294   kwdparms = cp_parser_objc_method_keyword_params (parser);
17295   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17296
17297   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17298 }
17299
17300 /* Pars an Objective-C method prototype list.  */
17301
17302 static void
17303 cp_parser_objc_method_prototype_list (cp_parser* parser)
17304 {
17305   cp_token *token = cp_lexer_peek_token (parser->lexer);
17306
17307   while (token->keyword != RID_AT_END)
17308     {
17309       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17310         {
17311           objc_add_method_declaration
17312            (cp_parser_objc_method_signature (parser));
17313           cp_parser_consume_semicolon_at_end_of_statement (parser);
17314         }
17315       else
17316         /* Allow for interspersed non-ObjC++ code.  */
17317         cp_parser_objc_interstitial_code (parser);
17318
17319       token = cp_lexer_peek_token (parser->lexer);
17320     }
17321
17322   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17323   objc_finish_interface ();
17324 }
17325
17326 /* Parse an Objective-C method definition list.  */
17327
17328 static void
17329 cp_parser_objc_method_definition_list (cp_parser* parser)
17330 {
17331   cp_token *token = cp_lexer_peek_token (parser->lexer);
17332
17333   while (token->keyword != RID_AT_END)
17334     {
17335       tree meth;
17336
17337       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17338         {
17339           push_deferring_access_checks (dk_deferred);
17340           objc_start_method_definition
17341            (cp_parser_objc_method_signature (parser));
17342
17343           /* For historical reasons, we accept an optional semicolon.  */
17344           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17345             cp_lexer_consume_token (parser->lexer);
17346
17347           perform_deferred_access_checks ();
17348           stop_deferring_access_checks ();
17349           meth = cp_parser_function_definition_after_declarator (parser,
17350                                                                  false);
17351           pop_deferring_access_checks ();
17352           objc_finish_method_definition (meth);
17353         }
17354       else
17355         /* Allow for interspersed non-ObjC++ code.  */
17356         cp_parser_objc_interstitial_code (parser);
17357
17358       token = cp_lexer_peek_token (parser->lexer);
17359     }
17360
17361   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17362   objc_finish_implementation ();
17363 }
17364
17365 /* Parse Objective-C ivars.  */
17366
17367 static void
17368 cp_parser_objc_class_ivars (cp_parser* parser)
17369 {
17370   cp_token *token = cp_lexer_peek_token (parser->lexer);
17371
17372   if (token->type != CPP_OPEN_BRACE)
17373     return;     /* No ivars specified.  */
17374
17375   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17376   token = cp_lexer_peek_token (parser->lexer);
17377
17378   while (token->type != CPP_CLOSE_BRACE)
17379     {
17380       cp_decl_specifier_seq declspecs;
17381       int decl_class_or_enum_p;
17382       tree prefix_attributes;
17383
17384       cp_parser_objc_visibility_spec (parser);
17385
17386       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17387         break;
17388
17389       cp_parser_decl_specifier_seq (parser,
17390                                     CP_PARSER_FLAGS_OPTIONAL,
17391                                     &declspecs,
17392                                     &decl_class_or_enum_p);
17393       prefix_attributes = declspecs.attributes;
17394       declspecs.attributes = NULL_TREE;
17395
17396       /* Keep going until we hit the `;' at the end of the
17397          declaration.  */
17398       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17399         {
17400           tree width = NULL_TREE, attributes, first_attribute, decl;
17401           cp_declarator *declarator = NULL;
17402           int ctor_dtor_or_conv_p;
17403
17404           /* Check for a (possibly unnamed) bitfield declaration.  */
17405           token = cp_lexer_peek_token (parser->lexer);
17406           if (token->type == CPP_COLON)
17407             goto eat_colon;
17408
17409           if (token->type == CPP_NAME
17410               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17411                   == CPP_COLON))
17412             {
17413               /* Get the name of the bitfield.  */
17414               declarator = make_id_declarator (NULL_TREE,
17415                                                cp_parser_identifier (parser),
17416                                                sfk_none);
17417
17418              eat_colon:
17419               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17420               /* Get the width of the bitfield.  */
17421               width
17422                 = cp_parser_constant_expression (parser,
17423                                                  /*allow_non_constant=*/false,
17424                                                  NULL);
17425             }
17426           else
17427             {
17428               /* Parse the declarator.  */
17429               declarator
17430                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17431                                         &ctor_dtor_or_conv_p,
17432                                         /*parenthesized_p=*/NULL,
17433                                         /*member_p=*/false);
17434             }
17435
17436           /* Look for attributes that apply to the ivar.  */
17437           attributes = cp_parser_attributes_opt (parser);
17438           /* Remember which attributes are prefix attributes and
17439              which are not.  */
17440           first_attribute = attributes;
17441           /* Combine the attributes.  */
17442           attributes = chainon (prefix_attributes, attributes);
17443
17444           if (width)
17445             {
17446               /* Create the bitfield declaration.  */
17447               decl = grokbitfield (declarator, &declspecs, width);
17448               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17449             }
17450           else
17451             decl = grokfield (declarator, &declspecs, 
17452                               NULL_TREE, /*init_const_expr_p=*/false,
17453                               NULL_TREE, attributes);
17454
17455           /* Add the instance variable.  */
17456           objc_add_instance_variable (decl);
17457
17458           /* Reset PREFIX_ATTRIBUTES.  */
17459           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17460             attributes = TREE_CHAIN (attributes);
17461           if (attributes)
17462             TREE_CHAIN (attributes) = NULL_TREE;
17463
17464           token = cp_lexer_peek_token (parser->lexer);
17465
17466           if (token->type == CPP_COMMA)
17467             {
17468               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17469               continue;
17470             }
17471           break;
17472         }
17473
17474       cp_parser_consume_semicolon_at_end_of_statement (parser);
17475       token = cp_lexer_peek_token (parser->lexer);
17476     }
17477
17478   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17479   /* For historical reasons, we accept an optional semicolon.  */
17480   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17481     cp_lexer_consume_token (parser->lexer);
17482 }
17483
17484 /* Parse an Objective-C protocol declaration.  */
17485
17486 static void
17487 cp_parser_objc_protocol_declaration (cp_parser* parser)
17488 {
17489   tree proto, protorefs;
17490   cp_token *tok;
17491
17492   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17493   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17494     {
17495       error ("identifier expected after %<@protocol%>");
17496       goto finish;
17497     }
17498
17499   /* See if we have a forward declaration or a definition.  */
17500   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17501
17502   /* Try a forward declaration first.  */
17503   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17504     {
17505       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17506      finish:
17507       cp_parser_consume_semicolon_at_end_of_statement (parser);
17508     }
17509
17510   /* Ok, we got a full-fledged definition (or at least should).  */
17511   else
17512     {
17513       proto = cp_parser_identifier (parser);
17514       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17515       objc_start_protocol (proto, protorefs);
17516       cp_parser_objc_method_prototype_list (parser);
17517     }
17518 }
17519
17520 /* Parse an Objective-C superclass or category.  */
17521
17522 static void
17523 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17524                                                           tree *categ)
17525 {
17526   cp_token *next = cp_lexer_peek_token (parser->lexer);
17527
17528   *super = *categ = NULL_TREE;
17529   if (next->type == CPP_COLON)
17530     {
17531       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17532       *super = cp_parser_identifier (parser);
17533     }
17534   else if (next->type == CPP_OPEN_PAREN)
17535     {
17536       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17537       *categ = cp_parser_identifier (parser);
17538       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17539     }
17540 }
17541
17542 /* Parse an Objective-C class interface.  */
17543
17544 static void
17545 cp_parser_objc_class_interface (cp_parser* parser)
17546 {
17547   tree name, super, categ, protos;
17548
17549   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17550   name = cp_parser_identifier (parser);
17551   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17552   protos = cp_parser_objc_protocol_refs_opt (parser);
17553
17554   /* We have either a class or a category on our hands.  */
17555   if (categ)
17556     objc_start_category_interface (name, categ, protos);
17557   else
17558     {
17559       objc_start_class_interface (name, super, protos);
17560       /* Handle instance variable declarations, if any.  */
17561       cp_parser_objc_class_ivars (parser);
17562       objc_continue_interface ();
17563     }
17564
17565   cp_parser_objc_method_prototype_list (parser);
17566 }
17567
17568 /* Parse an Objective-C class implementation.  */
17569
17570 static void
17571 cp_parser_objc_class_implementation (cp_parser* parser)
17572 {
17573   tree name, super, categ;
17574
17575   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17576   name = cp_parser_identifier (parser);
17577   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17578
17579   /* We have either a class or a category on our hands.  */
17580   if (categ)
17581     objc_start_category_implementation (name, categ);
17582   else
17583     {
17584       objc_start_class_implementation (name, super);
17585       /* Handle instance variable declarations, if any.  */
17586       cp_parser_objc_class_ivars (parser);
17587       objc_continue_implementation ();
17588     }
17589
17590   cp_parser_objc_method_definition_list (parser);
17591 }
17592
17593 /* Consume the @end token and finish off the implementation.  */
17594
17595 static void
17596 cp_parser_objc_end_implementation (cp_parser* parser)
17597 {
17598   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17599   objc_finish_implementation ();
17600 }
17601
17602 /* Parse an Objective-C declaration.  */
17603
17604 static void
17605 cp_parser_objc_declaration (cp_parser* parser)
17606 {
17607   /* Try to figure out what kind of declaration is present.  */
17608   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17609
17610   switch (kwd->keyword)
17611     {
17612     case RID_AT_ALIAS:
17613       cp_parser_objc_alias_declaration (parser);
17614       break;
17615     case RID_AT_CLASS:
17616       cp_parser_objc_class_declaration (parser);
17617       break;
17618     case RID_AT_PROTOCOL:
17619       cp_parser_objc_protocol_declaration (parser);
17620       break;
17621     case RID_AT_INTERFACE:
17622       cp_parser_objc_class_interface (parser);
17623       break;
17624     case RID_AT_IMPLEMENTATION:
17625       cp_parser_objc_class_implementation (parser);
17626       break;
17627     case RID_AT_END:
17628       cp_parser_objc_end_implementation (parser);
17629       break;
17630     default:
17631       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17632       cp_parser_skip_to_end_of_block_or_statement (parser);
17633     }
17634 }
17635
17636 /* Parse an Objective-C try-catch-finally statement.
17637
17638    objc-try-catch-finally-stmt:
17639      @try compound-statement objc-catch-clause-seq [opt]
17640        objc-finally-clause [opt]
17641
17642    objc-catch-clause-seq:
17643      objc-catch-clause objc-catch-clause-seq [opt]
17644
17645    objc-catch-clause:
17646      @catch ( exception-declaration ) compound-statement
17647
17648    objc-finally-clause
17649      @finally compound-statement
17650
17651    Returns NULL_TREE.  */
17652
17653 static tree
17654 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17655   location_t location;
17656   tree stmt;
17657
17658   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17659   location = cp_lexer_peek_token (parser->lexer)->location;
17660   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17661      node, lest it get absorbed into the surrounding block.  */
17662   stmt = push_stmt_list ();
17663   cp_parser_compound_statement (parser, NULL, false);
17664   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17665
17666   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17667     {
17668       cp_parameter_declarator *parmdecl;
17669       tree parm;
17670
17671       cp_lexer_consume_token (parser->lexer);
17672       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17673       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17674       parm = grokdeclarator (parmdecl->declarator,
17675                              &parmdecl->decl_specifiers,
17676                              PARM, /*initialized=*/0,
17677                              /*attrlist=*/NULL);
17678       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17679       objc_begin_catch_clause (parm);
17680       cp_parser_compound_statement (parser, NULL, false);
17681       objc_finish_catch_clause ();
17682     }
17683
17684   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17685     {
17686       cp_lexer_consume_token (parser->lexer);
17687       location = cp_lexer_peek_token (parser->lexer)->location;
17688       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17689          node, lest it get absorbed into the surrounding block.  */
17690       stmt = push_stmt_list ();
17691       cp_parser_compound_statement (parser, NULL, false);
17692       objc_build_finally_clause (location, pop_stmt_list (stmt));
17693     }
17694
17695   return objc_finish_try_stmt ();
17696 }
17697
17698 /* Parse an Objective-C synchronized statement.
17699
17700    objc-synchronized-stmt:
17701      @synchronized ( expression ) compound-statement
17702
17703    Returns NULL_TREE.  */
17704
17705 static tree
17706 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17707   location_t location;
17708   tree lock, stmt;
17709
17710   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17711
17712   location = cp_lexer_peek_token (parser->lexer)->location;
17713   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17714   lock = cp_parser_expression (parser, false);
17715   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17716
17717   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17718      node, lest it get absorbed into the surrounding block.  */
17719   stmt = push_stmt_list ();
17720   cp_parser_compound_statement (parser, NULL, false);
17721
17722   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17723 }
17724
17725 /* Parse an Objective-C throw statement.
17726
17727    objc-throw-stmt:
17728      @throw assignment-expression [opt] ;
17729
17730    Returns a constructed '@throw' statement.  */
17731
17732 static tree
17733 cp_parser_objc_throw_statement (cp_parser *parser) {
17734   tree expr = NULL_TREE;
17735
17736   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17737
17738   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17739     expr = cp_parser_assignment_expression (parser, false);
17740
17741   cp_parser_consume_semicolon_at_end_of_statement (parser);
17742
17743   return objc_build_throw_stmt (expr);
17744 }
17745
17746 /* Parse an Objective-C statement.  */
17747
17748 static tree
17749 cp_parser_objc_statement (cp_parser * parser) {
17750   /* Try to figure out what kind of declaration is present.  */
17751   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17752
17753   switch (kwd->keyword)
17754     {
17755     case RID_AT_TRY:
17756       return cp_parser_objc_try_catch_finally_statement (parser);
17757     case RID_AT_SYNCHRONIZED:
17758       return cp_parser_objc_synchronized_statement (parser);
17759     case RID_AT_THROW:
17760       return cp_parser_objc_throw_statement (parser);
17761     default:
17762       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17763       cp_parser_skip_to_end_of_block_or_statement (parser);
17764     }
17765
17766   return error_mark_node;
17767 }
17768 \f
17769 /* OpenMP 2.5 parsing routines.  */
17770
17771 /* All OpenMP clauses.  OpenMP 2.5.  */
17772 typedef enum pragma_omp_clause {
17773   PRAGMA_OMP_CLAUSE_NONE = 0,
17774
17775   PRAGMA_OMP_CLAUSE_COPYIN,
17776   PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17777   PRAGMA_OMP_CLAUSE_DEFAULT,
17778   PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17779   PRAGMA_OMP_CLAUSE_IF,
17780   PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17781   PRAGMA_OMP_CLAUSE_NOWAIT,
17782   PRAGMA_OMP_CLAUSE_NUM_THREADS,
17783   PRAGMA_OMP_CLAUSE_ORDERED,
17784   PRAGMA_OMP_CLAUSE_PRIVATE,
17785   PRAGMA_OMP_CLAUSE_REDUCTION,
17786   PRAGMA_OMP_CLAUSE_SCHEDULE,
17787   PRAGMA_OMP_CLAUSE_SHARED
17788 } pragma_omp_clause;
17789
17790 /* Returns name of the next clause.
17791    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17792    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
17793    returned and the token is consumed.  */
17794
17795 static pragma_omp_clause
17796 cp_parser_omp_clause_name (cp_parser *parser)
17797 {
17798   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17799
17800   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17801     result = PRAGMA_OMP_CLAUSE_IF;
17802   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17803     result = PRAGMA_OMP_CLAUSE_DEFAULT;
17804   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17805     result = PRAGMA_OMP_CLAUSE_PRIVATE;
17806   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17807     {
17808       tree id = cp_lexer_peek_token (parser->lexer)->value;
17809       const char *p = IDENTIFIER_POINTER (id);
17810
17811       switch (p[0])
17812         {
17813         case 'c':
17814           if (!strcmp ("copyin", p))
17815             result = PRAGMA_OMP_CLAUSE_COPYIN;
17816           else if (!strcmp ("copyprivate", p))
17817             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17818           break;
17819         case 'f':
17820           if (!strcmp ("firstprivate", p))
17821             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17822           break;
17823         case 'l':
17824           if (!strcmp ("lastprivate", p))
17825             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17826           break;
17827         case 'n':
17828           if (!strcmp ("nowait", p))
17829             result = PRAGMA_OMP_CLAUSE_NOWAIT;
17830           else if (!strcmp ("num_threads", p))
17831             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17832           break;
17833         case 'o':
17834           if (!strcmp ("ordered", p))
17835             result = PRAGMA_OMP_CLAUSE_ORDERED;
17836           break;
17837         case 'r':
17838           if (!strcmp ("reduction", p))
17839             result = PRAGMA_OMP_CLAUSE_REDUCTION;
17840           break;
17841         case 's':
17842           if (!strcmp ("schedule", p))
17843             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17844           else if (!strcmp ("shared", p))
17845             result = PRAGMA_OMP_CLAUSE_SHARED;
17846           break;
17847         }
17848     }
17849
17850   if (result != PRAGMA_OMP_CLAUSE_NONE)
17851     cp_lexer_consume_token (parser->lexer);
17852
17853   return result;
17854 }
17855
17856 /* Validate that a clause of the given type does not already exist.  */
17857
17858 static void
17859 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17860 {
17861   tree c;
17862
17863   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17864     if (OMP_CLAUSE_CODE (c) == code)
17865       {
17866         error ("too many %qs clauses", name);
17867         break;
17868       }
17869 }
17870
17871 /* OpenMP 2.5:
17872    variable-list:
17873      identifier
17874      variable-list , identifier
17875
17876    In addition, we match a closing parenthesis.  An opening parenthesis
17877    will have been consumed by the caller.
17878
17879    If KIND is nonzero, create the appropriate node and install the decl
17880    in OMP_CLAUSE_DECL and add the node to the head of the list.
17881
17882    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17883    return the list created.  */
17884
17885 static tree
17886 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17887                                 tree list)
17888 {
17889   while (1)
17890     {
17891       tree name, decl;
17892
17893       name = cp_parser_id_expression (parser, /*template_p=*/false,
17894                                       /*check_dependency_p=*/true,
17895                                       /*template_p=*/NULL,
17896                                       /*declarator_p=*/false,
17897                                       /*optional_p=*/false);
17898       if (name == error_mark_node)
17899         goto skip_comma;
17900
17901       decl = cp_parser_lookup_name_simple (parser, name);
17902       if (decl == error_mark_node)
17903         cp_parser_name_lookup_error (parser, name, decl, NULL);
17904       else if (kind != 0)
17905         {
17906           tree u = build_omp_clause (kind);
17907           OMP_CLAUSE_DECL (u) = decl;
17908           OMP_CLAUSE_CHAIN (u) = list;
17909           list = u;
17910         }
17911       else
17912         list = tree_cons (decl, NULL_TREE, list);
17913
17914     get_comma:
17915       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17916         break;
17917       cp_lexer_consume_token (parser->lexer);
17918     }
17919
17920   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17921     {
17922       int ending;
17923
17924       /* Try to resync to an unnested comma.  Copied from
17925          cp_parser_parenthesized_expression_list.  */
17926     skip_comma:
17927       ending = cp_parser_skip_to_closing_parenthesis (parser,
17928                                                       /*recovering=*/true,
17929                                                       /*or_comma=*/true,
17930                                                       /*consume_paren=*/true);
17931       if (ending < 0)
17932         goto get_comma;
17933     }
17934
17935   return list;
17936 }
17937
17938 /* Similarly, but expect leading and trailing parenthesis.  This is a very
17939    common case for omp clauses.  */
17940
17941 static tree
17942 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17943 {
17944   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17945     return cp_parser_omp_var_list_no_open (parser, kind, list);
17946   return list;
17947 }
17948
17949 /* OpenMP 2.5:
17950    default ( shared | none ) */
17951
17952 static tree
17953 cp_parser_omp_clause_default (cp_parser *parser, tree list)
17954 {
17955   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17956   tree c;
17957
17958   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17959     return list;
17960   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17961     {
17962       tree id = cp_lexer_peek_token (parser->lexer)->value;
17963       const char *p = IDENTIFIER_POINTER (id);
17964
17965       switch (p[0])
17966         {
17967         case 'n':
17968           if (strcmp ("none", p) != 0)
17969             goto invalid_kind;
17970           kind = OMP_CLAUSE_DEFAULT_NONE;
17971           break;
17972
17973         case 's':
17974           if (strcmp ("shared", p) != 0)
17975             goto invalid_kind;
17976           kind = OMP_CLAUSE_DEFAULT_SHARED;
17977           break;
17978
17979         default:
17980           goto invalid_kind;
17981         }
17982
17983       cp_lexer_consume_token (parser->lexer);
17984     }
17985   else
17986     {
17987     invalid_kind:
17988       cp_parser_error (parser, "expected %<none%> or %<shared%>");
17989     }
17990
17991   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17992     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17993                                            /*or_comma=*/false,
17994                                            /*consume_paren=*/true);
17995   
17996   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
17997     return list;
17998
17999   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18000   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18001   OMP_CLAUSE_CHAIN (c) = list;
18002   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18003
18004   return c;
18005 }
18006
18007 /* OpenMP 2.5:
18008    if ( expression ) */
18009
18010 static tree
18011 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18012 {
18013   tree t, c;
18014
18015   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18016     return list;
18017
18018   t = cp_parser_condition (parser);
18019
18020   if (t == error_mark_node
18021       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18022     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18023                                            /*or_comma=*/false,
18024                                            /*consume_paren=*/true);
18025
18026   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18027
18028   c = build_omp_clause (OMP_CLAUSE_IF);
18029   OMP_CLAUSE_IF_EXPR (c) = t;
18030   OMP_CLAUSE_CHAIN (c) = list;
18031
18032   return c;
18033 }
18034
18035 /* OpenMP 2.5:
18036    nowait */
18037
18038 static tree
18039 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18040 {
18041   tree c;
18042
18043   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18044
18045   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18046   OMP_CLAUSE_CHAIN (c) = list;
18047   return c;
18048 }
18049
18050 /* OpenMP 2.5:
18051    num_threads ( expression ) */
18052
18053 static tree
18054 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18055 {
18056   tree t, c;
18057
18058   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18059     return list;
18060
18061   t = cp_parser_expression (parser, false);
18062
18063   if (t == error_mark_node
18064       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18065     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18066                                            /*or_comma=*/false,
18067                                            /*consume_paren=*/true);
18068
18069   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18070
18071   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18072   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18073   OMP_CLAUSE_CHAIN (c) = list;
18074
18075   return c;
18076 }
18077
18078 /* OpenMP 2.5:
18079    ordered */
18080
18081 static tree
18082 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18083 {
18084   tree c;
18085
18086   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18087
18088   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18089   OMP_CLAUSE_CHAIN (c) = list;
18090   return c;
18091 }
18092
18093 /* OpenMP 2.5:
18094    reduction ( reduction-operator : variable-list )
18095
18096    reduction-operator:
18097      One of: + * - & ^ | && || */
18098
18099 static tree
18100 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18101 {
18102   enum tree_code code;
18103   tree nlist, c;
18104
18105   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18106     return list;
18107
18108   switch (cp_lexer_peek_token (parser->lexer)->type)
18109     {
18110     case CPP_PLUS:
18111       code = PLUS_EXPR;
18112       break;
18113     case CPP_MULT:
18114       code = MULT_EXPR;
18115       break;
18116     case CPP_MINUS:
18117       code = MINUS_EXPR;
18118       break;
18119     case CPP_AND:
18120       code = BIT_AND_EXPR;
18121       break;
18122     case CPP_XOR:
18123       code = BIT_XOR_EXPR;
18124       break;
18125     case CPP_OR:
18126       code = BIT_IOR_EXPR;
18127       break;
18128     case CPP_AND_AND:
18129       code = TRUTH_ANDIF_EXPR;
18130       break;
18131     case CPP_OR_OR:
18132       code = TRUTH_ORIF_EXPR;
18133       break;
18134     default:
18135       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18136     resync_fail:
18137       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18138                                              /*or_comma=*/false,
18139                                              /*consume_paren=*/true);
18140       return list;
18141     }
18142   cp_lexer_consume_token (parser->lexer);
18143
18144   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18145     goto resync_fail;
18146
18147   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18148   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18149     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18150
18151   return nlist;
18152 }
18153
18154 /* OpenMP 2.5:
18155    schedule ( schedule-kind )
18156    schedule ( schedule-kind , expression )
18157
18158    schedule-kind:
18159      static | dynamic | guided | runtime
18160 */
18161
18162 static tree
18163 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18164 {
18165   tree c, t;
18166
18167   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18168     return list;
18169
18170   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18171
18172   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18173     {
18174       tree id = cp_lexer_peek_token (parser->lexer)->value;
18175       const char *p = IDENTIFIER_POINTER (id);
18176
18177       switch (p[0])
18178         {
18179         case 'd':
18180           if (strcmp ("dynamic", p) != 0)
18181             goto invalid_kind;
18182           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18183           break;
18184
18185         case 'g':
18186           if (strcmp ("guided", p) != 0)
18187             goto invalid_kind;
18188           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18189           break;
18190
18191         case 'r':
18192           if (strcmp ("runtime", p) != 0)
18193             goto invalid_kind;
18194           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18195           break;
18196
18197         default:
18198           goto invalid_kind;
18199         }
18200     }
18201   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18202     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18203   else
18204     goto invalid_kind;
18205   cp_lexer_consume_token (parser->lexer);
18206
18207   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18208     {
18209       cp_lexer_consume_token (parser->lexer);
18210
18211       t = cp_parser_assignment_expression (parser, false);
18212
18213       if (t == error_mark_node)
18214         goto resync_fail;
18215       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18216         error ("schedule %<runtime%> does not take "
18217                "a %<chunk_size%> parameter");
18218       else
18219         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18220
18221       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18222         goto resync_fail;
18223     }
18224   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18225     goto resync_fail;
18226
18227   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18228   OMP_CLAUSE_CHAIN (c) = list;
18229   return c;
18230
18231  invalid_kind:
18232   cp_parser_error (parser, "invalid schedule kind");
18233  resync_fail:
18234   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18235                                          /*or_comma=*/false,
18236                                          /*consume_paren=*/true);
18237   return list;
18238 }
18239
18240 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18241    is a bitmask in MASK.  Return the list of clauses found; the result
18242    of clause default goes in *pdefault.  */
18243
18244 static tree
18245 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18246                            const char *where, cp_token *pragma_tok)
18247 {
18248   tree clauses = NULL;
18249
18250   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18251     {
18252       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18253       const char *c_name;
18254       tree prev = clauses;
18255
18256       switch (c_kind)
18257         {
18258         case PRAGMA_OMP_CLAUSE_COPYIN:
18259           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18260           c_name = "copyin";
18261           break;
18262         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18263           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18264                                             clauses);
18265           c_name = "copyprivate";
18266           break;
18267         case PRAGMA_OMP_CLAUSE_DEFAULT:
18268           clauses = cp_parser_omp_clause_default (parser, clauses);
18269           c_name = "default";
18270           break;
18271         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18272           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18273                                             clauses);
18274           c_name = "firstprivate";
18275           break;
18276         case PRAGMA_OMP_CLAUSE_IF:
18277           clauses = cp_parser_omp_clause_if (parser, clauses);
18278           c_name = "if";
18279           break;
18280         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18281           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18282                                             clauses);
18283           c_name = "lastprivate";
18284           break;
18285         case PRAGMA_OMP_CLAUSE_NOWAIT:
18286           clauses = cp_parser_omp_clause_nowait (parser, clauses);
18287           c_name = "nowait";
18288           break;
18289         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18290           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18291           c_name = "num_threads";
18292           break;
18293         case PRAGMA_OMP_CLAUSE_ORDERED:
18294           clauses = cp_parser_omp_clause_ordered (parser, clauses);
18295           c_name = "ordered";
18296           break;
18297         case PRAGMA_OMP_CLAUSE_PRIVATE:
18298           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18299                                             clauses);
18300           c_name = "private";
18301           break;
18302         case PRAGMA_OMP_CLAUSE_REDUCTION:
18303           clauses = cp_parser_omp_clause_reduction (parser, clauses);
18304           c_name = "reduction";
18305           break;
18306         case PRAGMA_OMP_CLAUSE_SCHEDULE:
18307           clauses = cp_parser_omp_clause_schedule (parser, clauses);
18308           c_name = "schedule";
18309           break;
18310         case PRAGMA_OMP_CLAUSE_SHARED:
18311           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18312                                             clauses);
18313           c_name = "shared";
18314           break;
18315         default:
18316           cp_parser_error (parser, "expected %<#pragma omp%> clause");
18317           goto saw_error;
18318         }
18319
18320       if (((mask >> c_kind) & 1) == 0)
18321         {
18322           /* Remove the invalid clause(s) from the list to avoid
18323              confusing the rest of the compiler.  */
18324           clauses = prev;
18325           error ("%qs is not valid for %qs", c_name, where);
18326         }
18327     }
18328  saw_error:
18329   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18330   return finish_omp_clauses (clauses);
18331 }
18332
18333 /* OpenMP 2.5:
18334    structured-block:
18335      statement
18336
18337    In practice, we're also interested in adding the statement to an
18338    outer node.  So it is convenient if we work around the fact that
18339    cp_parser_statement calls add_stmt.  */
18340
18341 static unsigned
18342 cp_parser_begin_omp_structured_block (cp_parser *parser)
18343 {
18344   unsigned save = parser->in_statement;
18345
18346   /* Only move the values to IN_OMP_BLOCK if they weren't false.
18347      This preserves the "not within loop or switch" style error messages
18348      for nonsense cases like
18349         void foo() {
18350         #pragma omp single
18351           break;
18352         }
18353   */
18354   if (parser->in_statement)
18355     parser->in_statement = IN_OMP_BLOCK;
18356
18357   return save;
18358 }
18359
18360 static void
18361 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18362 {
18363   parser->in_statement = save;
18364 }
18365
18366 static tree
18367 cp_parser_omp_structured_block (cp_parser *parser)
18368 {
18369   tree stmt = begin_omp_structured_block ();
18370   unsigned int save = cp_parser_begin_omp_structured_block (parser);
18371
18372   cp_parser_statement (parser, NULL_TREE, false);
18373
18374   cp_parser_end_omp_structured_block (parser, save);
18375   return finish_omp_structured_block (stmt);
18376 }
18377
18378 /* OpenMP 2.5:
18379    # pragma omp atomic new-line
18380      expression-stmt
18381
18382    expression-stmt:
18383      x binop= expr | x++ | ++x | x-- | --x
18384    binop:
18385      +, *, -, /, &, ^, |, <<, >>
18386
18387   where x is an lvalue expression with scalar type.  */
18388
18389 static void
18390 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18391 {
18392   tree lhs, rhs;
18393   enum tree_code code;
18394
18395   cp_parser_require_pragma_eol (parser, pragma_tok);
18396
18397   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18398                                     /*cast_p=*/false);
18399   switch (TREE_CODE (lhs))
18400     {
18401     case ERROR_MARK:
18402       goto saw_error;
18403
18404     case PREINCREMENT_EXPR:
18405     case POSTINCREMENT_EXPR:
18406       lhs = TREE_OPERAND (lhs, 0);
18407       code = PLUS_EXPR;
18408       rhs = integer_one_node;
18409       break;
18410
18411     case PREDECREMENT_EXPR:
18412     case POSTDECREMENT_EXPR:
18413       lhs = TREE_OPERAND (lhs, 0);
18414       code = MINUS_EXPR;
18415       rhs = integer_one_node;
18416       break;
18417
18418     default:
18419       switch (cp_lexer_peek_token (parser->lexer)->type)
18420         {
18421         case CPP_MULT_EQ:
18422           code = MULT_EXPR;
18423           break;
18424         case CPP_DIV_EQ:
18425           code = TRUNC_DIV_EXPR;
18426           break;
18427         case CPP_PLUS_EQ:
18428           code = PLUS_EXPR;
18429           break;
18430         case CPP_MINUS_EQ:
18431           code = MINUS_EXPR;
18432           break;
18433         case CPP_LSHIFT_EQ:
18434           code = LSHIFT_EXPR;
18435           break;
18436         case CPP_RSHIFT_EQ:
18437           code = RSHIFT_EXPR;
18438           break;
18439         case CPP_AND_EQ:
18440           code = BIT_AND_EXPR;
18441           break;
18442         case CPP_OR_EQ:
18443           code = BIT_IOR_EXPR;
18444           break;
18445         case CPP_XOR_EQ:
18446           code = BIT_XOR_EXPR;
18447           break;
18448         default:
18449           cp_parser_error (parser,
18450                            "invalid operator for %<#pragma omp atomic%>");
18451           goto saw_error;
18452         }
18453       cp_lexer_consume_token (parser->lexer);
18454
18455       rhs = cp_parser_expression (parser, false);
18456       if (rhs == error_mark_node)
18457         goto saw_error;
18458       break;
18459     }
18460   finish_omp_atomic (code, lhs, rhs);
18461   cp_parser_consume_semicolon_at_end_of_statement (parser);
18462   return;
18463
18464  saw_error:
18465   cp_parser_skip_to_end_of_block_or_statement (parser);
18466 }
18467
18468
18469 /* OpenMP 2.5:
18470    # pragma omp barrier new-line
18471 */
18472
18473 static void
18474 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18475 {
18476   cp_parser_require_pragma_eol (parser, pragma_tok);
18477   finish_omp_barrier ();
18478 }
18479
18480 /* OpenMP 2.5:
18481    # pragma omp critical [(name)] new-line
18482      structured-block
18483 */
18484
18485 static tree
18486 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18487 {
18488   tree stmt, name = NULL;
18489
18490   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18491     {
18492       cp_lexer_consume_token (parser->lexer);
18493
18494       name = cp_parser_identifier (parser);
18495       
18496       if (name == error_mark_node
18497           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18498         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18499                                                /*or_comma=*/false,
18500                                                /*consume_paren=*/true);
18501       if (name == error_mark_node)
18502         name = NULL;
18503     }
18504   cp_parser_require_pragma_eol (parser, pragma_tok);
18505
18506   stmt = cp_parser_omp_structured_block (parser);
18507   return c_finish_omp_critical (stmt, name);
18508 }
18509
18510 /* OpenMP 2.5:
18511    # pragma omp flush flush-vars[opt] new-line
18512
18513    flush-vars:
18514      ( variable-list ) */
18515
18516 static void
18517 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18518 {
18519   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18520     (void) cp_parser_omp_var_list (parser, 0, NULL);
18521   cp_parser_require_pragma_eol (parser, pragma_tok);
18522
18523   finish_omp_flush ();
18524 }
18525
18526 /* Parse the restricted form of the for statment allowed by OpenMP.  */
18527
18528 static tree
18529 cp_parser_omp_for_loop (cp_parser *parser)
18530 {
18531   tree init, cond, incr, body, decl, pre_body;
18532   location_t loc;
18533
18534   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18535     {
18536       cp_parser_error (parser, "for statement expected");
18537       return NULL;
18538     }
18539   loc = cp_lexer_consume_token (parser->lexer)->location;
18540   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18541     return NULL;
18542
18543   init = decl = NULL;
18544   pre_body = push_stmt_list ();
18545   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18546     {
18547       cp_decl_specifier_seq type_specifiers;
18548
18549       /* First, try to parse as an initialized declaration.  See
18550          cp_parser_condition, from whence the bulk of this is copied.  */
18551
18552       cp_parser_parse_tentatively (parser);
18553       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18554                                     &type_specifiers);
18555       if (!cp_parser_error_occurred (parser))
18556         {
18557           tree asm_specification, attributes;
18558           cp_declarator *declarator;
18559
18560           declarator = cp_parser_declarator (parser,
18561                                              CP_PARSER_DECLARATOR_NAMED,
18562                                              /*ctor_dtor_or_conv_p=*/NULL,
18563                                              /*parenthesized_p=*/NULL,
18564                                              /*member_p=*/false);
18565           attributes = cp_parser_attributes_opt (parser);
18566           asm_specification = cp_parser_asm_specification_opt (parser);
18567
18568           cp_parser_require (parser, CPP_EQ, "`='");
18569           if (cp_parser_parse_definitely (parser))
18570             {
18571               tree pushed_scope;
18572
18573               decl = start_decl (declarator, &type_specifiers,
18574                                  /*initialized_p=*/false, attributes,
18575                                  /*prefix_attributes=*/NULL_TREE,
18576                                  &pushed_scope);
18577
18578               init = cp_parser_assignment_expression (parser, false);
18579
18580               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18581                               asm_specification, LOOKUP_ONLYCONVERTING);
18582
18583               if (pushed_scope)
18584                 pop_scope (pushed_scope);
18585             }
18586         }
18587       else
18588         cp_parser_abort_tentative_parse (parser);
18589
18590       /* If parsing as an initialized declaration failed, try again as
18591          a simple expression.  */
18592       if (decl == NULL)
18593         init = cp_parser_expression (parser, false);
18594     }
18595   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18596   pre_body = pop_stmt_list (pre_body);
18597
18598   cond = NULL;
18599   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18600     cond = cp_parser_condition (parser);
18601   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18602
18603   incr = NULL;
18604   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18605     incr = cp_parser_expression (parser, false);
18606
18607   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18608     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18609                                            /*or_comma=*/false,
18610                                            /*consume_paren=*/true);
18611
18612   /* Note that we saved the original contents of this flag when we entered
18613      the structured block, and so we don't need to re-save it here.  */
18614   parser->in_statement = IN_OMP_FOR;
18615
18616   /* Note that the grammar doesn't call for a structured block here,
18617      though the loop as a whole is a structured block.  */
18618   body = push_stmt_list ();
18619   cp_parser_statement (parser, NULL_TREE, false);
18620   body = pop_stmt_list (body);
18621
18622   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18623 }
18624
18625 /* OpenMP 2.5:
18626    #pragma omp for for-clause[optseq] new-line
18627      for-loop
18628 */
18629
18630 #define OMP_FOR_CLAUSE_MASK                             \
18631         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18632         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18633         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18634         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18635         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
18636         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
18637         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18638
18639 static tree
18640 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18641 {
18642   tree clauses, sb, ret;
18643   unsigned int save;
18644
18645   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18646                                        "#pragma omp for", pragma_tok);
18647
18648   sb = begin_omp_structured_block ();
18649   save = cp_parser_begin_omp_structured_block (parser);
18650
18651   ret = cp_parser_omp_for_loop (parser);
18652   if (ret)
18653     OMP_FOR_CLAUSES (ret) = clauses;
18654
18655   cp_parser_end_omp_structured_block (parser, save);
18656   add_stmt (finish_omp_structured_block (sb));
18657
18658   return ret;
18659 }
18660
18661 /* OpenMP 2.5:
18662    # pragma omp master new-line
18663      structured-block
18664 */
18665
18666 static tree
18667 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18668 {
18669   cp_parser_require_pragma_eol (parser, pragma_tok);
18670   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18671 }
18672
18673 /* OpenMP 2.5:
18674    # pragma omp ordered new-line
18675      structured-block
18676 */
18677
18678 static tree
18679 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18680 {
18681   cp_parser_require_pragma_eol (parser, pragma_tok);
18682   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18683 }
18684
18685 /* OpenMP 2.5:
18686
18687    section-scope:
18688      { section-sequence }
18689
18690    section-sequence:
18691      section-directive[opt] structured-block
18692      section-sequence section-directive structured-block  */
18693
18694 static tree
18695 cp_parser_omp_sections_scope (cp_parser *parser)
18696 {
18697   tree stmt, substmt;
18698   bool error_suppress = false;
18699   cp_token *tok;
18700
18701   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18702     return NULL_TREE;
18703
18704   stmt = push_stmt_list ();
18705
18706   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18707     {
18708       unsigned save;
18709
18710       substmt = begin_omp_structured_block ();
18711       save = cp_parser_begin_omp_structured_block (parser);
18712
18713       while (1)
18714         {
18715           cp_parser_statement (parser, NULL_TREE, false);
18716
18717           tok = cp_lexer_peek_token (parser->lexer);
18718           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18719             break;
18720           if (tok->type == CPP_CLOSE_BRACE)
18721             break;
18722           if (tok->type == CPP_EOF)
18723             break;
18724         }
18725
18726       cp_parser_end_omp_structured_block (parser, save);
18727       substmt = finish_omp_structured_block (substmt);
18728       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18729       add_stmt (substmt);
18730     }
18731
18732   while (1)
18733     {
18734       tok = cp_lexer_peek_token (parser->lexer);
18735       if (tok->type == CPP_CLOSE_BRACE)
18736         break;
18737       if (tok->type == CPP_EOF)
18738         break;
18739
18740       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18741         {
18742           cp_lexer_consume_token (parser->lexer);
18743           cp_parser_require_pragma_eol (parser, tok);
18744           error_suppress = false;
18745         }
18746       else if (!error_suppress)
18747         {
18748           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18749           error_suppress = true;
18750         }
18751
18752       substmt = cp_parser_omp_structured_block (parser);
18753       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18754       add_stmt (substmt);
18755     }
18756   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18757
18758   substmt = pop_stmt_list (stmt);
18759
18760   stmt = make_node (OMP_SECTIONS);
18761   TREE_TYPE (stmt) = void_type_node;
18762   OMP_SECTIONS_BODY (stmt) = substmt;
18763
18764   add_stmt (stmt);
18765   return stmt;
18766 }
18767
18768 /* OpenMP 2.5:
18769    # pragma omp sections sections-clause[optseq] newline
18770      sections-scope
18771 */
18772
18773 #define OMP_SECTIONS_CLAUSE_MASK                        \
18774         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18775         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18776         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18777         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18778         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18779
18780 static tree
18781 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18782 {
18783   tree clauses, ret;
18784
18785   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18786                                        "#pragma omp sections", pragma_tok);
18787
18788   ret = cp_parser_omp_sections_scope (parser);
18789   if (ret)
18790     OMP_SECTIONS_CLAUSES (ret) = clauses;
18791
18792   return ret;
18793 }
18794
18795 /* OpenMP 2.5:
18796    # pragma parallel parallel-clause new-line
18797    # pragma parallel for parallel-for-clause new-line
18798    # pragma parallel sections parallel-sections-clause new-line
18799 */
18800
18801 #define OMP_PARALLEL_CLAUSE_MASK                        \
18802         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
18803         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18804         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18805         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
18806         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
18807         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
18808         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18809         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18810
18811 static tree
18812 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18813 {
18814   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18815   const char *p_name = "#pragma omp parallel";
18816   tree stmt, clauses, par_clause, ws_clause, block;
18817   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18818   unsigned int save;
18819
18820   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18821     {
18822       cp_lexer_consume_token (parser->lexer);
18823       p_kind = PRAGMA_OMP_PARALLEL_FOR;
18824       p_name = "#pragma omp parallel for";
18825       mask |= OMP_FOR_CLAUSE_MASK;
18826       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18827     }
18828   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18829     {
18830       tree id = cp_lexer_peek_token (parser->lexer)->value;
18831       const char *p = IDENTIFIER_POINTER (id);
18832       if (strcmp (p, "sections") == 0)
18833         {
18834           cp_lexer_consume_token (parser->lexer);
18835           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18836           p_name = "#pragma omp parallel sections";
18837           mask |= OMP_SECTIONS_CLAUSE_MASK;
18838           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18839         }
18840     }
18841
18842   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18843   block = begin_omp_parallel ();
18844   save = cp_parser_begin_omp_structured_block (parser);
18845
18846   switch (p_kind)
18847     {
18848     case PRAGMA_OMP_PARALLEL:
18849       cp_parser_already_scoped_statement (parser);
18850       par_clause = clauses;
18851       break;
18852
18853     case PRAGMA_OMP_PARALLEL_FOR:
18854       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18855       stmt = cp_parser_omp_for_loop (parser);
18856       if (stmt)
18857         OMP_FOR_CLAUSES (stmt) = ws_clause;
18858       break;
18859
18860     case PRAGMA_OMP_PARALLEL_SECTIONS:
18861       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18862       stmt = cp_parser_omp_sections_scope (parser);
18863       if (stmt)
18864         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18865       break;
18866
18867     default:
18868       gcc_unreachable ();
18869     }
18870
18871   cp_parser_end_omp_structured_block (parser, save);
18872   stmt = finish_omp_parallel (par_clause, block);
18873   if (p_kind != PRAGMA_OMP_PARALLEL)
18874     OMP_PARALLEL_COMBINED (stmt) = 1;
18875   return stmt;
18876 }
18877
18878 /* OpenMP 2.5:
18879    # pragma omp single single-clause[optseq] new-line
18880      structured-block
18881 */
18882
18883 #define OMP_SINGLE_CLAUSE_MASK                          \
18884         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18885         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18886         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
18887         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18888
18889 static tree
18890 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18891 {
18892   tree stmt = make_node (OMP_SINGLE);
18893   TREE_TYPE (stmt) = void_type_node;
18894
18895   OMP_SINGLE_CLAUSES (stmt)
18896     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18897                                  "#pragma omp single", pragma_tok);
18898   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18899
18900   return add_stmt (stmt);
18901 }
18902
18903 /* OpenMP 2.5:
18904    # pragma omp threadprivate (variable-list) */
18905
18906 static void
18907 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18908 {
18909   tree vars;
18910
18911   vars = cp_parser_omp_var_list (parser, 0, NULL);
18912   cp_parser_require_pragma_eol (parser, pragma_tok);
18913
18914   if (!targetm.have_tls)
18915     sorry ("threadprivate variables not supported in this target");
18916
18917   finish_omp_threadprivate (vars);
18918 }
18919
18920 /* Main entry point to OpenMP statement pragmas.  */
18921
18922 static void
18923 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18924 {
18925   tree stmt;
18926
18927   switch (pragma_tok->pragma_kind)
18928     {
18929     case PRAGMA_OMP_ATOMIC:
18930       cp_parser_omp_atomic (parser, pragma_tok);
18931       return;
18932     case PRAGMA_OMP_CRITICAL:
18933       stmt = cp_parser_omp_critical (parser, pragma_tok);
18934       break;
18935     case PRAGMA_OMP_FOR:
18936       stmt = cp_parser_omp_for (parser, pragma_tok);
18937       break;
18938     case PRAGMA_OMP_MASTER:
18939       stmt = cp_parser_omp_master (parser, pragma_tok);
18940       break;
18941     case PRAGMA_OMP_ORDERED:
18942       stmt = cp_parser_omp_ordered (parser, pragma_tok);
18943       break;
18944     case PRAGMA_OMP_PARALLEL:
18945       stmt = cp_parser_omp_parallel (parser, pragma_tok);
18946       break;
18947     case PRAGMA_OMP_SECTIONS:
18948       stmt = cp_parser_omp_sections (parser, pragma_tok);
18949       break;
18950     case PRAGMA_OMP_SINGLE:
18951       stmt = cp_parser_omp_single (parser, pragma_tok);
18952       break;
18953     default:
18954       gcc_unreachable ();
18955     }
18956
18957   if (stmt)
18958     SET_EXPR_LOCATION (stmt, pragma_tok->location);
18959 }
18960 \f
18961 /* The parser.  */
18962
18963 static GTY (()) cp_parser *the_parser;
18964
18965 \f
18966 /* Special handling for the first token or line in the file.  The first
18967    thing in the file might be #pragma GCC pch_preprocess, which loads a
18968    PCH file, which is a GC collection point.  So we need to handle this
18969    first pragma without benefit of an existing lexer structure.
18970
18971    Always returns one token to the caller in *FIRST_TOKEN.  This is 
18972    either the true first token of the file, or the first token after
18973    the initial pragma.  */
18974
18975 static void
18976 cp_parser_initial_pragma (cp_token *first_token)
18977 {
18978   tree name = NULL;
18979
18980   cp_lexer_get_preprocessor_token (NULL, first_token);
18981   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18982     return;
18983
18984   cp_lexer_get_preprocessor_token (NULL, first_token);
18985   if (first_token->type == CPP_STRING)
18986     {
18987       name = first_token->value;
18988
18989       cp_lexer_get_preprocessor_token (NULL, first_token);
18990       if (first_token->type != CPP_PRAGMA_EOL)
18991         error ("junk at end of %<#pragma GCC pch_preprocess%>");
18992     }
18993   else
18994     error ("expected string literal");
18995
18996   /* Skip to the end of the pragma.  */
18997   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
18998     cp_lexer_get_preprocessor_token (NULL, first_token);
18999
19000   /* Read one more token to return to our caller.  */
19001   cp_lexer_get_preprocessor_token (NULL, first_token);
19002
19003   /* Now actually load the PCH file.  */
19004   if (name)
19005     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19006 }
19007
19008 /* Normal parsing of a pragma token.  Here we can (and must) use the
19009    regular lexer.  */
19010
19011 static bool
19012 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19013 {
19014   cp_token *pragma_tok;
19015   unsigned int id;
19016
19017   pragma_tok = cp_lexer_consume_token (parser->lexer);
19018   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19019   parser->lexer->in_pragma = true;
19020
19021   id = pragma_tok->pragma_kind;
19022   switch (id)
19023     {
19024     case PRAGMA_GCC_PCH_PREPROCESS:
19025       error ("%<#pragma GCC pch_preprocess%> must be first");
19026       break;
19027
19028     case PRAGMA_OMP_BARRIER:
19029       switch (context)
19030         {
19031         case pragma_compound:
19032           cp_parser_omp_barrier (parser, pragma_tok);
19033           return false;
19034         case pragma_stmt:
19035           error ("%<#pragma omp barrier%> may only be "
19036                  "used in compound statements");
19037           break;
19038         default:
19039           goto bad_stmt;
19040         }
19041       break;
19042
19043     case PRAGMA_OMP_FLUSH:
19044       switch (context)
19045         {
19046         case pragma_compound:
19047           cp_parser_omp_flush (parser, pragma_tok);
19048           return false;
19049         case pragma_stmt:
19050           error ("%<#pragma omp flush%> may only be "
19051                  "used in compound statements");
19052           break;
19053         default:
19054           goto bad_stmt;
19055         }
19056       break;
19057
19058     case PRAGMA_OMP_THREADPRIVATE:
19059       cp_parser_omp_threadprivate (parser, pragma_tok);
19060       return false;
19061
19062     case PRAGMA_OMP_ATOMIC:
19063     case PRAGMA_OMP_CRITICAL:
19064     case PRAGMA_OMP_FOR:
19065     case PRAGMA_OMP_MASTER:
19066     case PRAGMA_OMP_ORDERED:
19067     case PRAGMA_OMP_PARALLEL:
19068     case PRAGMA_OMP_SECTIONS:
19069     case PRAGMA_OMP_SINGLE:
19070       if (context == pragma_external)
19071         goto bad_stmt;
19072       cp_parser_omp_construct (parser, pragma_tok);
19073       return true;
19074
19075     case PRAGMA_OMP_SECTION:
19076       error ("%<#pragma omp section%> may only be used in "
19077              "%<#pragma omp sections%> construct");
19078       break;
19079
19080     default:
19081       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19082       c_invoke_pragma_handler (id);
19083       break;
19084
19085     bad_stmt:
19086       cp_parser_error (parser, "expected declaration specifiers");
19087       break;
19088     }
19089
19090   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19091   return false;
19092 }
19093
19094 /* The interface the pragma parsers have to the lexer.  */
19095
19096 enum cpp_ttype
19097 pragma_lex (tree *value)
19098 {
19099   cp_token *tok;
19100   enum cpp_ttype ret;
19101
19102   tok = cp_lexer_peek_token (the_parser->lexer);
19103
19104   ret = tok->type;
19105   *value = tok->value;
19106
19107   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19108     ret = CPP_EOF;
19109   else if (ret == CPP_STRING)
19110     *value = cp_parser_string_literal (the_parser, false, false);
19111   else
19112     {
19113       cp_lexer_consume_token (the_parser->lexer);
19114       if (ret == CPP_KEYWORD)
19115         ret = CPP_NAME;
19116     }
19117
19118   return ret;
19119 }
19120
19121 \f
19122 /* External interface.  */
19123
19124 /* Parse one entire translation unit.  */
19125
19126 void
19127 c_parse_file (void)
19128 {
19129   bool error_occurred;
19130   static bool already_called = false;
19131
19132   if (already_called)
19133     {
19134       sorry ("inter-module optimizations not implemented for C++");
19135       return;
19136     }
19137   already_called = true;
19138
19139   the_parser = cp_parser_new ();
19140   push_deferring_access_checks (flag_access_control
19141                                 ? dk_no_deferred : dk_no_check);
19142   error_occurred = cp_parser_translation_unit (the_parser);
19143   the_parser = NULL;
19144 }
19145
19146 /* This variable must be provided by every front end.  */
19147
19148 int yydebug;
19149
19150 #include "gt-cp-parser.h"