OSDN Git Service

PR c++/28606
[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 ((256 * 1024) / sizeof (cp_token))
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
1154   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1156
1157   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1158
1159   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1160
1161   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1162
1163   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1164
1165   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1166 };
1167
1168 /* The same as binops, but initialized by cp_parser_new so that
1169    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1170    for speed.  */
1171 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1172
1173 /* Constructors and destructors.  */
1174
1175 /* Construct a new context.  The context below this one on the stack
1176    is given by NEXT.  */
1177
1178 static cp_parser_context *
1179 cp_parser_context_new (cp_parser_context* next)
1180 {
1181   cp_parser_context *context;
1182
1183   /* Allocate the storage.  */
1184   if (cp_parser_context_free_list != NULL)
1185     {
1186       /* Pull the first entry from the free list.  */
1187       context = cp_parser_context_free_list;
1188       cp_parser_context_free_list = context->next;
1189       memset (context, 0, sizeof (*context));
1190     }
1191   else
1192     context = GGC_CNEW (cp_parser_context);
1193
1194   /* No errors have occurred yet in this context.  */
1195   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196   /* If this is not the bottomost context, copy information that we
1197      need from the previous context.  */
1198   if (next)
1199     {
1200       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201          expression, then we are parsing one in this context, too.  */
1202       context->object_type = next->object_type;
1203       /* Thread the stack.  */
1204       context->next = next;
1205     }
1206
1207   return context;
1208 }
1209
1210 /* The cp_parser structure represents the C++ parser.  */
1211
1212 typedef struct cp_parser GTY(())
1213 {
1214   /* The lexer from which we are obtaining tokens.  */
1215   cp_lexer *lexer;
1216
1217   /* The scope in which names should be looked up.  If NULL_TREE, then
1218      we look up names in the scope that is currently open in the
1219      source program.  If non-NULL, this is either a TYPE or
1220      NAMESPACE_DECL for the scope in which we should look.  It can
1221      also be ERROR_MARK, when we've parsed a bogus scope.
1222
1223      This value is not cleared automatically after a name is looked
1224      up, so we must be careful to clear it before starting a new look
1225      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1226      will look up `Z' in the scope of `X', rather than the current
1227      scope.)  Unfortunately, it is difficult to tell when name lookup
1228      is complete, because we sometimes peek at a token, look it up,
1229      and then decide not to consume it.   */
1230   tree scope;
1231
1232   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1233      last lookup took place.  OBJECT_SCOPE is used if an expression
1234      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1235      respectively.  QUALIFYING_SCOPE is used for an expression of the
1236      form "X::Y"; it refers to X.  */
1237   tree object_scope;
1238   tree qualifying_scope;
1239
1240   /* A stack of parsing contexts.  All but the bottom entry on the
1241      stack will be tentative contexts.
1242
1243      We parse tentatively in order to determine which construct is in
1244      use in some situations.  For example, in order to determine
1245      whether a statement is an expression-statement or a
1246      declaration-statement we parse it tentatively as a
1247      declaration-statement.  If that fails, we then reparse the same
1248      token stream as an expression-statement.  */
1249   cp_parser_context *context;
1250
1251   /* True if we are parsing GNU C++.  If this flag is not set, then
1252      GNU extensions are not recognized.  */
1253   bool allow_gnu_extensions_p;
1254
1255   /* TRUE if the `>' token should be interpreted as the greater-than
1256      operator.  FALSE if it is the end of a template-id or
1257      template-parameter-list.  */
1258   bool greater_than_is_operator_p;
1259
1260   /* TRUE if default arguments are allowed within a parameter list
1261      that starts at this point. FALSE if only a gnu extension makes
1262      them permissible.  */
1263   bool default_arg_ok_p;
1264
1265   /* TRUE if we are parsing an integral constant-expression.  See
1266      [expr.const] for a precise definition.  */
1267   bool integral_constant_expression_p;
1268
1269   /* TRUE if we are parsing an integral constant-expression -- but a
1270      non-constant expression should be permitted as well.  This flag
1271      is used when parsing an array bound so that GNU variable-length
1272      arrays are tolerated.  */
1273   bool allow_non_integral_constant_expression_p;
1274
1275   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1276      been seen that makes the expression non-constant.  */
1277   bool non_integral_constant_expression_p;
1278
1279   /* TRUE if local variable names and `this' are forbidden in the
1280      current context.  */
1281   bool local_variables_forbidden_p;
1282
1283   /* TRUE if the declaration we are parsing is part of a
1284      linkage-specification of the form `extern string-literal
1285      declaration'.  */
1286   bool in_unbraced_linkage_specification_p;
1287
1288   /* TRUE if we are presently parsing a declarator, after the
1289      direct-declarator.  */
1290   bool in_declarator_p;
1291
1292   /* TRUE if we are presently parsing a template-argument-list.  */
1293   bool in_template_argument_list_p;
1294
1295   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1296      to IN_OMP_BLOCK if parsing OpenMP structured block and
1297      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1298      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1299      iteration-statement, OpenMP block or loop within that switch.  */
1300 #define IN_SWITCH_STMT          1
1301 #define IN_ITERATION_STMT       2
1302 #define IN_OMP_BLOCK            4
1303 #define IN_OMP_FOR              8
1304   unsigned char in_statement;
1305
1306   /* TRUE if we are presently parsing the body of a switch statement.
1307      Note that this doesn't quite overlap with in_statement above.
1308      The difference relates to giving the right sets of error messages:
1309      "case not in switch" vs "break statement used with OpenMP...".  */
1310   bool in_switch_statement_p;
1311
1312   /* TRUE if we are parsing a type-id in an expression context.  In
1313      such a situation, both "type (expr)" and "type (type)" are valid
1314      alternatives.  */
1315   bool in_type_id_in_expr_p;
1316
1317   /* TRUE if we are currently in a header file where declarations are
1318      implicitly extern "C".  */
1319   bool implicit_extern_c;
1320
1321   /* TRUE if strings in expressions should be translated to the execution
1322      character set.  */
1323   bool translate_strings_p;
1324
1325   /* If non-NULL, then we are parsing a construct where new type
1326      definitions are not permitted.  The string stored here will be
1327      issued as an error message if a type is defined.  */
1328   const char *type_definition_forbidden_message;
1329
1330   /* A list of lists. The outer list is a stack, used for member
1331      functions of local classes. At each level there are two sub-list,
1332      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1333      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1334      TREE_VALUE's. The functions are chained in reverse declaration
1335      order.
1336
1337      The TREE_PURPOSE sublist contains those functions with default
1338      arguments that need post processing, and the TREE_VALUE sublist
1339      contains those functions with definitions that need post
1340      processing.
1341
1342      These lists can only be processed once the outermost class being
1343      defined is complete.  */
1344   tree unparsed_functions_queues;
1345
1346   /* The number of classes whose definitions are currently in
1347      progress.  */
1348   unsigned num_classes_being_defined;
1349
1350   /* The number of template parameter lists that apply directly to the
1351      current declaration.  */
1352   unsigned num_template_parameter_lists;
1353 } cp_parser;
1354
1355 /* Prototypes.  */
1356
1357 /* Constructors and destructors.  */
1358
1359 static cp_parser *cp_parser_new
1360   (void);
1361
1362 /* Routines to parse various constructs.
1363
1364    Those that return `tree' will return the error_mark_node (rather
1365    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1366    Sometimes, they will return an ordinary node if error-recovery was
1367    attempted, even though a parse error occurred.  So, to check
1368    whether or not a parse error occurred, you should always use
1369    cp_parser_error_occurred.  If the construct is optional (indicated
1370    either by an `_opt' in the name of the function that does the
1371    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1372    the construct is not present.  */
1373
1374 /* Lexical conventions [gram.lex]  */
1375
1376 static tree cp_parser_identifier
1377   (cp_parser *);
1378 static tree cp_parser_string_literal
1379   (cp_parser *, bool, bool);
1380
1381 /* Basic concepts [gram.basic]  */
1382
1383 static bool cp_parser_translation_unit
1384   (cp_parser *);
1385
1386 /* Expressions [gram.expr]  */
1387
1388 static tree cp_parser_primary_expression
1389   (cp_parser *, bool, bool, bool, cp_id_kind *);
1390 static tree cp_parser_id_expression
1391   (cp_parser *, bool, bool, bool *, bool, bool);
1392 static tree cp_parser_unqualified_id
1393   (cp_parser *, bool, bool, bool, bool);
1394 static tree cp_parser_nested_name_specifier_opt
1395   (cp_parser *, bool, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier
1397   (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_class_or_namespace_name
1399   (cp_parser *, bool, bool, bool, bool, bool);
1400 static tree cp_parser_postfix_expression
1401   (cp_parser *, bool, bool);
1402 static tree cp_parser_postfix_open_square_expression
1403   (cp_parser *, tree, bool);
1404 static tree cp_parser_postfix_dot_deref_expression
1405   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1406 static tree cp_parser_parenthesized_expression_list
1407   (cp_parser *, bool, bool, bool *);
1408 static void cp_parser_pseudo_destructor_name
1409   (cp_parser *, tree *, tree *);
1410 static tree cp_parser_unary_expression
1411   (cp_parser *, bool, bool);
1412 static enum tree_code cp_parser_unary_operator
1413   (cp_token *);
1414 static tree cp_parser_new_expression
1415   (cp_parser *);
1416 static tree cp_parser_new_placement
1417   (cp_parser *);
1418 static tree cp_parser_new_type_id
1419   (cp_parser *, tree *);
1420 static cp_declarator *cp_parser_new_declarator_opt
1421   (cp_parser *);
1422 static cp_declarator *cp_parser_direct_new_declarator
1423   (cp_parser *);
1424 static tree cp_parser_new_initializer
1425   (cp_parser *);
1426 static tree cp_parser_delete_expression
1427   (cp_parser *);
1428 static tree cp_parser_cast_expression
1429   (cp_parser *, bool, bool);
1430 static tree cp_parser_binary_expression
1431   (cp_parser *, bool);
1432 static tree cp_parser_question_colon_clause
1433   (cp_parser *, tree);
1434 static tree cp_parser_assignment_expression
1435   (cp_parser *, bool);
1436 static enum tree_code cp_parser_assignment_operator_opt
1437   (cp_parser *);
1438 static tree cp_parser_expression
1439   (cp_parser *, bool);
1440 static tree cp_parser_constant_expression
1441   (cp_parser *, bool, bool *);
1442 static tree cp_parser_builtin_offsetof
1443   (cp_parser *);
1444
1445 /* Statements [gram.stmt.stmt]  */
1446
1447 static void cp_parser_statement
1448   (cp_parser *, tree, bool);
1449 static tree cp_parser_labeled_statement
1450   (cp_parser *, tree, bool);
1451 static tree cp_parser_expression_statement
1452   (cp_parser *, tree);
1453 static tree cp_parser_compound_statement
1454   (cp_parser *, tree, bool);
1455 static void cp_parser_statement_seq_opt
1456   (cp_parser *, tree);
1457 static tree cp_parser_selection_statement
1458   (cp_parser *);
1459 static tree cp_parser_condition
1460   (cp_parser *);
1461 static tree cp_parser_iteration_statement
1462   (cp_parser *);
1463 static void cp_parser_for_init_statement
1464   (cp_parser *);
1465 static tree cp_parser_jump_statement
1466   (cp_parser *);
1467 static void cp_parser_declaration_statement
1468   (cp_parser *);
1469
1470 static tree cp_parser_implicitly_scoped_statement
1471   (cp_parser *);
1472 static void cp_parser_already_scoped_statement
1473   (cp_parser *);
1474
1475 /* Declarations [gram.dcl.dcl] */
1476
1477 static void cp_parser_declaration_seq_opt
1478   (cp_parser *);
1479 static void cp_parser_declaration
1480   (cp_parser *);
1481 static void cp_parser_block_declaration
1482   (cp_parser *, bool);
1483 static void cp_parser_simple_declaration
1484   (cp_parser *, bool);
1485 static void cp_parser_decl_specifier_seq
1486   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1487 static tree cp_parser_storage_class_specifier_opt
1488   (cp_parser *);
1489 static tree cp_parser_function_specifier_opt
1490   (cp_parser *, cp_decl_specifier_seq *);
1491 static tree cp_parser_type_specifier
1492   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1493    int *, bool *);
1494 static tree cp_parser_simple_type_specifier
1495   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1496 static tree cp_parser_type_name
1497   (cp_parser *);
1498 static tree cp_parser_elaborated_type_specifier
1499   (cp_parser *, bool, bool);
1500 static tree cp_parser_enum_specifier
1501   (cp_parser *);
1502 static void cp_parser_enumerator_list
1503   (cp_parser *, tree);
1504 static void cp_parser_enumerator_definition
1505   (cp_parser *, tree);
1506 static tree cp_parser_namespace_name
1507   (cp_parser *);
1508 static void cp_parser_namespace_definition
1509   (cp_parser *);
1510 static void cp_parser_namespace_body
1511   (cp_parser *);
1512 static tree cp_parser_qualified_namespace_specifier
1513   (cp_parser *);
1514 static void cp_parser_namespace_alias_definition
1515   (cp_parser *);
1516 static void cp_parser_using_declaration
1517   (cp_parser *);
1518 static void cp_parser_using_directive
1519   (cp_parser *);
1520 static void cp_parser_asm_definition
1521   (cp_parser *);
1522 static void cp_parser_linkage_specification
1523   (cp_parser *);
1524
1525 /* Declarators [gram.dcl.decl] */
1526
1527 static tree cp_parser_init_declarator
1528   (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1529 static cp_declarator *cp_parser_declarator
1530   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1531 static cp_declarator *cp_parser_direct_declarator
1532   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1533 static enum tree_code cp_parser_ptr_operator
1534   (cp_parser *, tree *, cp_cv_quals *);
1535 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1536   (cp_parser *);
1537 static tree cp_parser_declarator_id
1538   (cp_parser *, bool);
1539 static tree cp_parser_type_id
1540   (cp_parser *);
1541 static void cp_parser_type_specifier_seq
1542   (cp_parser *, bool, cp_decl_specifier_seq *);
1543 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1544   (cp_parser *);
1545 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1546   (cp_parser *, bool *);
1547 static cp_parameter_declarator *cp_parser_parameter_declaration
1548   (cp_parser *, bool, bool *);
1549 static void cp_parser_function_body
1550   (cp_parser *);
1551 static tree cp_parser_initializer
1552   (cp_parser *, bool *, bool *);
1553 static tree cp_parser_initializer_clause
1554   (cp_parser *, bool *);
1555 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1556   (cp_parser *, bool *);
1557
1558 static bool cp_parser_ctor_initializer_opt_and_function_body
1559   (cp_parser *);
1560
1561 /* Classes [gram.class] */
1562
1563 static tree cp_parser_class_name
1564   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1565 static tree cp_parser_class_specifier
1566   (cp_parser *);
1567 static tree cp_parser_class_head
1568   (cp_parser *, bool *, tree *);
1569 static enum tag_types cp_parser_class_key
1570   (cp_parser *);
1571 static void cp_parser_member_specification_opt
1572   (cp_parser *);
1573 static void cp_parser_member_declaration
1574   (cp_parser *);
1575 static tree cp_parser_pure_specifier
1576   (cp_parser *);
1577 static tree cp_parser_constant_initializer
1578   (cp_parser *);
1579
1580 /* Derived classes [gram.class.derived] */
1581
1582 static tree cp_parser_base_clause
1583   (cp_parser *);
1584 static tree cp_parser_base_specifier
1585   (cp_parser *);
1586
1587 /* Special member functions [gram.special] */
1588
1589 static tree cp_parser_conversion_function_id
1590   (cp_parser *);
1591 static tree cp_parser_conversion_type_id
1592   (cp_parser *);
1593 static cp_declarator *cp_parser_conversion_declarator_opt
1594   (cp_parser *);
1595 static bool cp_parser_ctor_initializer_opt
1596   (cp_parser *);
1597 static void cp_parser_mem_initializer_list
1598   (cp_parser *);
1599 static tree cp_parser_mem_initializer
1600   (cp_parser *);
1601 static tree cp_parser_mem_initializer_id
1602   (cp_parser *);
1603
1604 /* Overloading [gram.over] */
1605
1606 static tree cp_parser_operator_function_id
1607   (cp_parser *);
1608 static tree cp_parser_operator
1609   (cp_parser *);
1610
1611 /* Templates [gram.temp] */
1612
1613 static void cp_parser_template_declaration
1614   (cp_parser *, bool);
1615 static tree cp_parser_template_parameter_list
1616   (cp_parser *);
1617 static tree cp_parser_template_parameter
1618   (cp_parser *, bool *);
1619 static tree cp_parser_type_parameter
1620   (cp_parser *);
1621 static tree cp_parser_template_id
1622   (cp_parser *, bool, bool, bool);
1623 static tree cp_parser_template_name
1624   (cp_parser *, bool, bool, bool, bool *);
1625 static tree cp_parser_template_argument_list
1626   (cp_parser *);
1627 static tree cp_parser_template_argument
1628   (cp_parser *);
1629 static void cp_parser_explicit_instantiation
1630   (cp_parser *);
1631 static void cp_parser_explicit_specialization
1632   (cp_parser *);
1633
1634 /* Exception handling [gram.exception] */
1635
1636 static tree cp_parser_try_block
1637   (cp_parser *);
1638 static bool cp_parser_function_try_block
1639   (cp_parser *);
1640 static void cp_parser_handler_seq
1641   (cp_parser *);
1642 static void cp_parser_handler
1643   (cp_parser *);
1644 static tree cp_parser_exception_declaration
1645   (cp_parser *);
1646 static tree cp_parser_throw_expression
1647   (cp_parser *);
1648 static tree cp_parser_exception_specification_opt
1649   (cp_parser *);
1650 static tree cp_parser_type_id_list
1651   (cp_parser *);
1652
1653 /* GNU Extensions */
1654
1655 static tree cp_parser_asm_specification_opt
1656   (cp_parser *);
1657 static tree cp_parser_asm_operand_list
1658   (cp_parser *);
1659 static tree cp_parser_asm_clobber_list
1660   (cp_parser *);
1661 static tree cp_parser_attributes_opt
1662   (cp_parser *);
1663 static tree cp_parser_attribute_list
1664   (cp_parser *);
1665 static bool cp_parser_extension_opt
1666   (cp_parser *, int *);
1667 static void cp_parser_label_declaration
1668   (cp_parser *);
1669
1670 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1671 static bool cp_parser_pragma
1672   (cp_parser *, enum pragma_context);
1673
1674 /* Objective-C++ Productions */
1675
1676 static tree cp_parser_objc_message_receiver
1677   (cp_parser *);
1678 static tree cp_parser_objc_message_args
1679   (cp_parser *);
1680 static tree cp_parser_objc_message_expression
1681   (cp_parser *);
1682 static tree cp_parser_objc_encode_expression
1683   (cp_parser *);
1684 static tree cp_parser_objc_defs_expression
1685   (cp_parser *);
1686 static tree cp_parser_objc_protocol_expression
1687   (cp_parser *);
1688 static tree cp_parser_objc_selector_expression
1689   (cp_parser *);
1690 static tree cp_parser_objc_expression
1691   (cp_parser *);
1692 static bool cp_parser_objc_selector_p
1693   (enum cpp_ttype);
1694 static tree cp_parser_objc_selector
1695   (cp_parser *);
1696 static tree cp_parser_objc_protocol_refs_opt
1697   (cp_parser *);
1698 static void cp_parser_objc_declaration
1699   (cp_parser *);
1700 static tree cp_parser_objc_statement
1701   (cp_parser *);
1702
1703 /* Utility Routines */
1704
1705 static tree cp_parser_lookup_name
1706   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1707 static tree cp_parser_lookup_name_simple
1708   (cp_parser *, tree);
1709 static tree cp_parser_maybe_treat_template_as_class
1710   (tree, bool);
1711 static bool cp_parser_check_declarator_template_parameters
1712   (cp_parser *, cp_declarator *);
1713 static bool cp_parser_check_template_parameters
1714   (cp_parser *, unsigned);
1715 static tree cp_parser_simple_cast_expression
1716   (cp_parser *);
1717 static tree cp_parser_global_scope_opt
1718   (cp_parser *, bool);
1719 static bool cp_parser_constructor_declarator_p
1720   (cp_parser *, bool);
1721 static tree cp_parser_function_definition_from_specifiers_and_declarator
1722   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1723 static tree cp_parser_function_definition_after_declarator
1724   (cp_parser *, bool);
1725 static void cp_parser_template_declaration_after_export
1726   (cp_parser *, bool);
1727 static void cp_parser_perform_template_parameter_access_checks
1728   (tree);
1729 static tree cp_parser_single_declaration
1730   (cp_parser *, tree, bool, bool *);
1731 static tree cp_parser_functional_cast
1732   (cp_parser *, tree);
1733 static tree cp_parser_save_member_function_body
1734   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1735 static tree cp_parser_enclosed_template_argument_list
1736   (cp_parser *);
1737 static void cp_parser_save_default_args
1738   (cp_parser *, tree);
1739 static void cp_parser_late_parsing_for_member
1740   (cp_parser *, tree);
1741 static void cp_parser_late_parsing_default_args
1742   (cp_parser *, tree);
1743 static tree cp_parser_sizeof_operand
1744   (cp_parser *, enum rid);
1745 static bool cp_parser_declares_only_class_p
1746   (cp_parser *);
1747 static void cp_parser_set_storage_class
1748   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1749 static void cp_parser_set_decl_spec_type
1750   (cp_decl_specifier_seq *, tree, bool);
1751 static bool cp_parser_friend_p
1752   (const cp_decl_specifier_seq *);
1753 static cp_token *cp_parser_require
1754   (cp_parser *, enum cpp_ttype, const char *);
1755 static cp_token *cp_parser_require_keyword
1756   (cp_parser *, enum rid, const char *);
1757 static bool cp_parser_token_starts_function_definition_p
1758   (cp_token *);
1759 static bool cp_parser_next_token_starts_class_definition_p
1760   (cp_parser *);
1761 static bool cp_parser_next_token_ends_template_argument_p
1762   (cp_parser *);
1763 static bool cp_parser_nth_token_starts_template_argument_list_p
1764   (cp_parser *, size_t);
1765 static enum tag_types cp_parser_token_is_class_key
1766   (cp_token *);
1767 static void cp_parser_check_class_key
1768   (enum tag_types, tree type);
1769 static void cp_parser_check_access_in_redeclaration
1770   (tree type);
1771 static bool cp_parser_optional_template_keyword
1772   (cp_parser *);
1773 static void cp_parser_pre_parsed_nested_name_specifier
1774   (cp_parser *);
1775 static void cp_parser_cache_group
1776   (cp_parser *, enum cpp_ttype, unsigned);
1777 static void cp_parser_parse_tentatively
1778   (cp_parser *);
1779 static void cp_parser_commit_to_tentative_parse
1780   (cp_parser *);
1781 static void cp_parser_abort_tentative_parse
1782   (cp_parser *);
1783 static bool cp_parser_parse_definitely
1784   (cp_parser *);
1785 static inline bool cp_parser_parsing_tentatively
1786   (cp_parser *);
1787 static bool cp_parser_uncommitted_to_tentative_parse_p
1788   (cp_parser *);
1789 static void cp_parser_error
1790   (cp_parser *, const char *);
1791 static void cp_parser_name_lookup_error
1792   (cp_parser *, tree, tree, const char *);
1793 static bool cp_parser_simulate_error
1794   (cp_parser *);
1795 static void cp_parser_check_type_definition
1796   (cp_parser *);
1797 static void cp_parser_check_for_definition_in_return_type
1798   (cp_declarator *, tree);
1799 static void cp_parser_check_for_invalid_template_id
1800   (cp_parser *, tree);
1801 static bool cp_parser_non_integral_constant_expression
1802   (cp_parser *, const char *);
1803 static void cp_parser_diagnose_invalid_type_name
1804   (cp_parser *, tree, tree);
1805 static bool cp_parser_parse_and_diagnose_invalid_type_name
1806   (cp_parser *);
1807 static int cp_parser_skip_to_closing_parenthesis
1808   (cp_parser *, bool, bool, bool);
1809 static void cp_parser_skip_to_end_of_statement
1810   (cp_parser *);
1811 static void cp_parser_consume_semicolon_at_end_of_statement
1812   (cp_parser *);
1813 static void cp_parser_skip_to_end_of_block_or_statement
1814   (cp_parser *);
1815 static void cp_parser_skip_to_closing_brace
1816   (cp_parser *);
1817 static void cp_parser_skip_until_found
1818   (cp_parser *, enum cpp_ttype, const char *);
1819 static void cp_parser_skip_to_pragma_eol
1820   (cp_parser*, cp_token *);
1821 static bool cp_parser_error_occurred
1822   (cp_parser *);
1823 static bool cp_parser_allow_gnu_extensions_p
1824   (cp_parser *);
1825 static bool cp_parser_is_string_literal
1826   (cp_token *);
1827 static bool cp_parser_is_keyword
1828   (cp_token *, enum rid);
1829 static tree cp_parser_make_typename_type
1830   (cp_parser *, tree, tree);
1831
1832 /* Returns nonzero if we are parsing tentatively.  */
1833
1834 static inline bool
1835 cp_parser_parsing_tentatively (cp_parser* parser)
1836 {
1837   return parser->context->next != NULL;
1838 }
1839
1840 /* Returns nonzero if TOKEN is a string literal.  */
1841
1842 static bool
1843 cp_parser_is_string_literal (cp_token* token)
1844 {
1845   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1846 }
1847
1848 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1849
1850 static bool
1851 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1852 {
1853   return token->keyword == keyword;
1854 }
1855
1856 /* If not parsing tentatively, issue a diagnostic of the form
1857       FILE:LINE: MESSAGE before TOKEN
1858    where TOKEN is the next token in the input stream.  MESSAGE
1859    (specified by the caller) is usually of the form "expected
1860    OTHER-TOKEN".  */
1861
1862 static void
1863 cp_parser_error (cp_parser* parser, const char* message)
1864 {
1865   if (!cp_parser_simulate_error (parser))
1866     {
1867       cp_token *token = cp_lexer_peek_token (parser->lexer);
1868       /* This diagnostic makes more sense if it is tagged to the line
1869          of the token we just peeked at.  */
1870       cp_lexer_set_source_position_from_token (token);
1871
1872       if (token->type == CPP_PRAGMA)
1873         {
1874           error ("%<#pragma%> is not allowed here");
1875           cp_parser_skip_to_pragma_eol (parser, token);
1876           return;
1877         }
1878
1879       c_parse_error (message,
1880                      /* Because c_parser_error does not understand
1881                         CPP_KEYWORD, keywords are treated like
1882                         identifiers.  */
1883                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1884                      token->value);
1885     }
1886 }
1887
1888 /* Issue an error about name-lookup failing.  NAME is the
1889    IDENTIFIER_NODE DECL is the result of
1890    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1891    the thing that we hoped to find.  */
1892
1893 static void
1894 cp_parser_name_lookup_error (cp_parser* parser,
1895                              tree name,
1896                              tree decl,
1897                              const char* desired)
1898 {
1899   /* If name lookup completely failed, tell the user that NAME was not
1900      declared.  */
1901   if (decl == error_mark_node)
1902     {
1903       if (parser->scope && parser->scope != global_namespace)
1904         error ("%<%D::%D%> has not been declared",
1905                parser->scope, name);
1906       else if (parser->scope == global_namespace)
1907         error ("%<::%D%> has not been declared", name);
1908       else if (parser->object_scope
1909                && !CLASS_TYPE_P (parser->object_scope))
1910         error ("request for member %qD in non-class type %qT",
1911                name, parser->object_scope);
1912       else if (parser->object_scope)
1913         error ("%<%T::%D%> has not been declared",
1914                parser->object_scope, name);
1915       else
1916         error ("%qD has not been declared", name);
1917     }
1918   else if (parser->scope && parser->scope != global_namespace)
1919     error ("%<%D::%D%> %s", parser->scope, name, desired);
1920   else if (parser->scope == global_namespace)
1921     error ("%<::%D%> %s", name, desired);
1922   else
1923     error ("%qD %s", name, desired);
1924 }
1925
1926 /* If we are parsing tentatively, remember that an error has occurred
1927    during this tentative parse.  Returns true if the error was
1928    simulated; false if a message should be issued by the caller.  */
1929
1930 static bool
1931 cp_parser_simulate_error (cp_parser* parser)
1932 {
1933   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1934     {
1935       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1936       return true;
1937     }
1938   return false;
1939 }
1940
1941 /* Check for repeated decl-specifiers.  */
1942
1943 static void
1944 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1945 {
1946   cp_decl_spec ds;
1947
1948   for (ds = ds_first; ds != ds_last; ++ds)
1949     {
1950       unsigned count = decl_specs->specs[(int)ds];
1951       if (count < 2)
1952         continue;
1953       /* The "long" specifier is a special case because of "long long".  */
1954       if (ds == ds_long)
1955         {
1956           if (count > 2)
1957             error ("%<long long long%> is too long for GCC");
1958           else if (pedantic && !in_system_header && warn_long_long)
1959             pedwarn ("ISO C++ does not support %<long long%>");
1960         }
1961       else if (count > 1)
1962         {
1963           static const char *const decl_spec_names[] = {
1964             "signed",
1965             "unsigned",
1966             "short",
1967             "long",
1968             "const",
1969             "volatile",
1970             "restrict",
1971             "inline",
1972             "virtual",
1973             "explicit",
1974             "friend",
1975             "typedef",
1976             "__complex",
1977             "__thread"
1978           };
1979           error ("duplicate %qs", decl_spec_names[(int)ds]);
1980         }
1981     }
1982 }
1983
1984 /* This function is called when a type is defined.  If type
1985    definitions are forbidden at this point, an error message is
1986    issued.  */
1987
1988 static void
1989 cp_parser_check_type_definition (cp_parser* parser)
1990 {
1991   /* If types are forbidden here, issue a message.  */
1992   if (parser->type_definition_forbidden_message)
1993     /* Use `%s' to print the string in case there are any escape
1994        characters in the message.  */
1995     error ("%s", parser->type_definition_forbidden_message);
1996 }
1997
1998 /* This function is called when the DECLARATOR is processed.  The TYPE
1999    was a type defined in the decl-specifiers.  If it is invalid to
2000    define a type in the decl-specifiers for DECLARATOR, an error is
2001    issued.  */
2002
2003 static void
2004 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2005                                                tree type)
2006 {
2007   /* [dcl.fct] forbids type definitions in return types.
2008      Unfortunately, it's not easy to know whether or not we are
2009      processing a return type until after the fact.  */
2010   while (declarator
2011          && (declarator->kind == cdk_pointer
2012              || declarator->kind == cdk_reference
2013              || declarator->kind == cdk_ptrmem))
2014     declarator = declarator->declarator;
2015   if (declarator
2016       && declarator->kind == cdk_function)
2017     {
2018       error ("new types may not be defined in a return type");
2019       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2020               type);
2021     }
2022 }
2023
2024 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2025    "<" in any valid C++ program.  If the next token is indeed "<",
2026    issue a message warning the user about what appears to be an
2027    invalid attempt to form a template-id.  */
2028
2029 static void
2030 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2031                                          tree type)
2032 {
2033   cp_token_position start = 0;
2034
2035   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2036     {
2037       if (TYPE_P (type))
2038         error ("%qT is not a template", type);
2039       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2040         error ("%qE is not a template", type);
2041       else
2042         error ("invalid template-id");
2043       /* Remember the location of the invalid "<".  */
2044       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2045         start = cp_lexer_token_position (parser->lexer, true);
2046       /* Consume the "<".  */
2047       cp_lexer_consume_token (parser->lexer);
2048       /* Parse the template arguments.  */
2049       cp_parser_enclosed_template_argument_list (parser);
2050       /* Permanently remove the invalid template arguments so that
2051          this error message is not issued again.  */
2052       if (start)
2053         cp_lexer_purge_tokens_after (parser->lexer, start);
2054     }
2055 }
2056
2057 /* If parsing an integral constant-expression, issue an error message
2058    about the fact that THING appeared and return true.  Otherwise,
2059    return false.  In either case, set
2060    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2061
2062 static bool
2063 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2064                                             const char *thing)
2065 {
2066   parser->non_integral_constant_expression_p = true;
2067   if (parser->integral_constant_expression_p)
2068     {
2069       if (!parser->allow_non_integral_constant_expression_p)
2070         {
2071           error ("%s cannot appear in a constant-expression", thing);
2072           return true;
2073         }
2074     }
2075   return false;
2076 }
2077
2078 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2079    qualifying scope (or NULL, if none) for ID.  This function commits
2080    to the current active tentative parse, if any.  (Otherwise, the
2081    problematic construct might be encountered again later, resulting
2082    in duplicate error messages.)  */
2083
2084 static void
2085 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2086 {
2087   tree decl, old_scope;
2088   /* Try to lookup the identifier.  */
2089   old_scope = parser->scope;
2090   parser->scope = scope;
2091   decl = cp_parser_lookup_name_simple (parser, id);
2092   parser->scope = old_scope;
2093   /* If the lookup found a template-name, it means that the user forgot
2094   to specify an argument list. Emit a useful error message.  */
2095   if (TREE_CODE (decl) == TEMPLATE_DECL)
2096     error ("invalid use of template-name %qE without an argument list", decl);
2097   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2098     error ("invalid use of destructor %qD as a type", id);
2099   else if (!parser->scope)
2100     {
2101       /* Issue an error message.  */
2102       error ("%qE does not name a type", id);
2103       /* If we're in a template class, it's possible that the user was
2104          referring to a type from a base class.  For example:
2105
2106            template <typename T> struct A { typedef T X; };
2107            template <typename T> struct B : public A<T> { X x; };
2108
2109          The user should have said "typename A<T>::X".  */
2110       if (processing_template_decl && current_class_type
2111           && TYPE_BINFO (current_class_type))
2112         {
2113           tree b;
2114
2115           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2116                b;
2117                b = TREE_CHAIN (b))
2118             {
2119               tree base_type = BINFO_TYPE (b);
2120               if (CLASS_TYPE_P (base_type)
2121                   && dependent_type_p (base_type))
2122                 {
2123                   tree field;
2124                   /* Go from a particular instantiation of the
2125                      template (which will have an empty TYPE_FIELDs),
2126                      to the main version.  */
2127                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2128                   for (field = TYPE_FIELDS (base_type);
2129                        field;
2130                        field = TREE_CHAIN (field))
2131                     if (TREE_CODE (field) == TYPE_DECL
2132                         && DECL_NAME (field) == id)
2133                       {
2134                         inform ("(perhaps %<typename %T::%E%> was intended)",
2135                                 BINFO_TYPE (b), id);
2136                         break;
2137                       }
2138                   if (field)
2139                     break;
2140                 }
2141             }
2142         }
2143     }
2144   /* Here we diagnose qualified-ids where the scope is actually correct,
2145      but the identifier does not resolve to a valid type name.  */
2146   else if (parser->scope != error_mark_node)
2147     {
2148       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2149         error ("%qE in namespace %qE does not name a type",
2150                id, parser->scope);
2151       else if (TYPE_P (parser->scope))
2152         error ("%qE in class %qT does not name a type", id, parser->scope);
2153       else
2154         gcc_unreachable ();
2155     }
2156   cp_parser_commit_to_tentative_parse (parser);
2157 }
2158
2159 /* Check for a common situation where a type-name should be present,
2160    but is not, and issue a sensible error message.  Returns true if an
2161    invalid type-name was detected.
2162
2163    The situation handled by this function are variable declarations of the
2164    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2165    Usually, `ID' should name a type, but if we got here it means that it
2166    does not. We try to emit the best possible error message depending on
2167    how exactly the id-expression looks like.  */
2168
2169 static bool
2170 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2171 {
2172   tree id;
2173
2174   cp_parser_parse_tentatively (parser);
2175   id = cp_parser_id_expression (parser,
2176                                 /*template_keyword_p=*/false,
2177                                 /*check_dependency_p=*/true,
2178                                 /*template_p=*/NULL,
2179                                 /*declarator_p=*/true,
2180                                 /*optional_p=*/false);
2181   /* After the id-expression, there should be a plain identifier,
2182      otherwise this is not a simple variable declaration. Also, if
2183      the scope is dependent, we cannot do much.  */
2184   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2185       || (parser->scope && TYPE_P (parser->scope)
2186           && dependent_type_p (parser->scope)))
2187     {
2188       cp_parser_abort_tentative_parse (parser);
2189       return false;
2190     }
2191   if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2192     return false;
2193
2194   /* Emit a diagnostic for the invalid type.  */
2195   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2196   /* Skip to the end of the declaration; there's no point in
2197      trying to process it.  */
2198   cp_parser_skip_to_end_of_block_or_statement (parser);
2199   return true;
2200 }
2201
2202 /* Consume tokens up to, and including, the next non-nested closing `)'.
2203    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2204    are doing error recovery. Returns -1 if OR_COMMA is true and we
2205    found an unnested comma.  */
2206
2207 static int
2208 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2209                                        bool recovering,
2210                                        bool or_comma,
2211                                        bool consume_paren)
2212 {
2213   unsigned paren_depth = 0;
2214   unsigned brace_depth = 0;
2215
2216   if (recovering && !or_comma
2217       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2218     return 0;
2219
2220   while (true)
2221     {
2222       cp_token * token = cp_lexer_peek_token (parser->lexer);
2223
2224       switch (token->type)
2225         {
2226         case CPP_EOF:
2227         case CPP_PRAGMA_EOL:
2228           /* If we've run out of tokens, then there is no closing `)'.  */
2229           return 0;
2230
2231         case CPP_SEMICOLON:
2232           /* This matches the processing in skip_to_end_of_statement.  */
2233           if (!brace_depth)
2234             return 0;
2235           break;
2236
2237         case CPP_OPEN_BRACE:
2238           ++brace_depth;
2239           break;
2240         case CPP_CLOSE_BRACE:
2241           if (!brace_depth--)
2242             return 0;
2243           break;
2244
2245         case CPP_COMMA:
2246           if (recovering && or_comma && !brace_depth && !paren_depth)
2247             return -1;
2248           break;
2249
2250         case CPP_OPEN_PAREN:
2251           if (!brace_depth)
2252             ++paren_depth;
2253           break;
2254
2255         case CPP_CLOSE_PAREN:
2256           if (!brace_depth && !paren_depth--)
2257             {
2258               if (consume_paren)
2259                 cp_lexer_consume_token (parser->lexer);
2260               return 1;
2261             }
2262           break;
2263
2264         default:
2265           break;
2266         }
2267
2268       /* Consume the token.  */
2269       cp_lexer_consume_token (parser->lexer);
2270     }
2271 }
2272
2273 /* Consume tokens until we reach the end of the current statement.
2274    Normally, that will be just before consuming a `;'.  However, if a
2275    non-nested `}' comes first, then we stop before consuming that.  */
2276
2277 static void
2278 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2279 {
2280   unsigned nesting_depth = 0;
2281
2282   while (true)
2283     {
2284       cp_token *token = cp_lexer_peek_token (parser->lexer);
2285
2286       switch (token->type)
2287         {
2288         case CPP_EOF:
2289         case CPP_PRAGMA_EOL:
2290           /* If we've run out of tokens, stop.  */
2291           return;
2292
2293         case CPP_SEMICOLON:
2294           /* If the next token is a `;', we have reached the end of the
2295              statement.  */
2296           if (!nesting_depth)
2297             return;
2298           break;
2299
2300         case CPP_CLOSE_BRACE:
2301           /* If this is a non-nested '}', stop before consuming it.
2302              That way, when confronted with something like:
2303
2304                { 3 + }
2305
2306              we stop before consuming the closing '}', even though we
2307              have not yet reached a `;'.  */
2308           if (nesting_depth == 0)
2309             return;
2310
2311           /* If it is the closing '}' for a block that we have
2312              scanned, stop -- but only after consuming the token.
2313              That way given:
2314
2315                 void f g () { ... }
2316                 typedef int I;
2317
2318              we will stop after the body of the erroneously declared
2319              function, but before consuming the following `typedef'
2320              declaration.  */
2321           if (--nesting_depth == 0)
2322             {
2323               cp_lexer_consume_token (parser->lexer);
2324               return;
2325             }
2326
2327         case CPP_OPEN_BRACE:
2328           ++nesting_depth;
2329           break;
2330
2331         default:
2332           break;
2333         }
2334
2335       /* Consume the token.  */
2336       cp_lexer_consume_token (parser->lexer);
2337     }
2338 }
2339
2340 /* This function is called at the end of a statement or declaration.
2341    If the next token is a semicolon, it is consumed; otherwise, error
2342    recovery is attempted.  */
2343
2344 static void
2345 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2346 {
2347   /* Look for the trailing `;'.  */
2348   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2349     {
2350       /* If there is additional (erroneous) input, skip to the end of
2351          the statement.  */
2352       cp_parser_skip_to_end_of_statement (parser);
2353       /* If the next token is now a `;', consume it.  */
2354       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2355         cp_lexer_consume_token (parser->lexer);
2356     }
2357 }
2358
2359 /* Skip tokens until we have consumed an entire block, or until we
2360    have consumed a non-nested `;'.  */
2361
2362 static void
2363 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2364 {
2365   int nesting_depth = 0;
2366
2367   while (nesting_depth >= 0)
2368     {
2369       cp_token *token = cp_lexer_peek_token (parser->lexer);
2370
2371       switch (token->type)
2372         {
2373         case CPP_EOF:
2374         case CPP_PRAGMA_EOL:
2375           /* If we've run out of tokens, stop.  */
2376           return;
2377
2378         case CPP_SEMICOLON:
2379           /* Stop if this is an unnested ';'. */
2380           if (!nesting_depth)
2381             nesting_depth = -1;
2382           break;
2383
2384         case CPP_CLOSE_BRACE:
2385           /* Stop if this is an unnested '}', or closes the outermost
2386              nesting level.  */
2387           nesting_depth--;
2388           if (!nesting_depth)
2389             nesting_depth = -1;
2390           break;
2391
2392         case CPP_OPEN_BRACE:
2393           /* Nest. */
2394           nesting_depth++;
2395           break;
2396
2397         default:
2398           break;
2399         }
2400
2401       /* Consume the token.  */
2402       cp_lexer_consume_token (parser->lexer);
2403     }
2404 }
2405
2406 /* Skip tokens until a non-nested closing curly brace is the next
2407    token.  */
2408
2409 static void
2410 cp_parser_skip_to_closing_brace (cp_parser *parser)
2411 {
2412   unsigned nesting_depth = 0;
2413
2414   while (true)
2415     {
2416       cp_token *token = cp_lexer_peek_token (parser->lexer);
2417
2418       switch (token->type)
2419         {
2420         case CPP_EOF:
2421         case CPP_PRAGMA_EOL:
2422           /* If we've run out of tokens, stop.  */
2423           return;
2424
2425         case CPP_CLOSE_BRACE:
2426           /* If the next token is a non-nested `}', then we have reached
2427              the end of the current block.  */
2428           if (nesting_depth-- == 0)
2429             return;
2430           break;
2431
2432         case CPP_OPEN_BRACE:
2433           /* If it the next token is a `{', then we are entering a new
2434              block.  Consume the entire block.  */
2435           ++nesting_depth;
2436           break;
2437
2438         default:
2439           break;
2440         }
2441
2442       /* Consume the token.  */
2443       cp_lexer_consume_token (parser->lexer);
2444     }
2445 }
2446
2447 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2448    parameter is the PRAGMA token, allowing us to purge the entire pragma
2449    sequence.  */
2450
2451 static void
2452 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2453 {
2454   cp_token *token;
2455
2456   parser->lexer->in_pragma = false;
2457
2458   do
2459     token = cp_lexer_consume_token (parser->lexer);
2460   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2461
2462   /* Ensure that the pragma is not parsed again.  */
2463   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2464 }
2465
2466 /* Require pragma end of line, resyncing with it as necessary.  The
2467    arguments are as for cp_parser_skip_to_pragma_eol.  */
2468
2469 static void
2470 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2471 {
2472   parser->lexer->in_pragma = false;
2473   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2474     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2475 }
2476
2477 /* This is a simple wrapper around make_typename_type. When the id is
2478    an unresolved identifier node, we can provide a superior diagnostic
2479    using cp_parser_diagnose_invalid_type_name.  */
2480
2481 static tree
2482 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2483 {
2484   tree result;
2485   if (TREE_CODE (id) == IDENTIFIER_NODE)
2486     {
2487       result = make_typename_type (scope, id, typename_type,
2488                                    /*complain=*/tf_none);
2489       if (result == error_mark_node)
2490         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2491       return result;
2492     }
2493   return make_typename_type (scope, id, typename_type, tf_error);
2494 }
2495
2496
2497 /* Create a new C++ parser.  */
2498
2499 static cp_parser *
2500 cp_parser_new (void)
2501 {
2502   cp_parser *parser;
2503   cp_lexer *lexer;
2504   unsigned i;
2505
2506   /* cp_lexer_new_main is called before calling ggc_alloc because
2507      cp_lexer_new_main might load a PCH file.  */
2508   lexer = cp_lexer_new_main ();
2509
2510   /* Initialize the binops_by_token so that we can get the tree
2511      directly from the token.  */
2512   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2513     binops_by_token[binops[i].token_type] = binops[i];
2514
2515   parser = GGC_CNEW (cp_parser);
2516   parser->lexer = lexer;
2517   parser->context = cp_parser_context_new (NULL);
2518
2519   /* For now, we always accept GNU extensions.  */
2520   parser->allow_gnu_extensions_p = 1;
2521
2522   /* The `>' token is a greater-than operator, not the end of a
2523      template-id.  */
2524   parser->greater_than_is_operator_p = true;
2525
2526   parser->default_arg_ok_p = true;
2527
2528   /* We are not parsing a constant-expression.  */
2529   parser->integral_constant_expression_p = false;
2530   parser->allow_non_integral_constant_expression_p = false;
2531   parser->non_integral_constant_expression_p = false;
2532
2533   /* Local variable names are not forbidden.  */
2534   parser->local_variables_forbidden_p = false;
2535
2536   /* We are not processing an `extern "C"' declaration.  */
2537   parser->in_unbraced_linkage_specification_p = false;
2538
2539   /* We are not processing a declarator.  */
2540   parser->in_declarator_p = false;
2541
2542   /* We are not processing a template-argument-list.  */
2543   parser->in_template_argument_list_p = false;
2544
2545   /* We are not in an iteration statement.  */
2546   parser->in_statement = 0;
2547
2548   /* We are not in a switch statement.  */
2549   parser->in_switch_statement_p = false;
2550
2551   /* We are not parsing a type-id inside an expression.  */
2552   parser->in_type_id_in_expr_p = false;
2553
2554   /* Declarations aren't implicitly extern "C".  */
2555   parser->implicit_extern_c = false;
2556
2557   /* String literals should be translated to the execution character set.  */
2558   parser->translate_strings_p = true;
2559
2560   /* The unparsed function queue is empty.  */
2561   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2562
2563   /* There are no classes being defined.  */
2564   parser->num_classes_being_defined = 0;
2565
2566   /* No template parameters apply.  */
2567   parser->num_template_parameter_lists = 0;
2568
2569   return parser;
2570 }
2571
2572 /* Create a cp_lexer structure which will emit the tokens in CACHE
2573    and push it onto the parser's lexer stack.  This is used for delayed
2574    parsing of in-class method bodies and default arguments, and should
2575    not be confused with tentative parsing.  */
2576 static void
2577 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2578 {
2579   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2580   lexer->next = parser->lexer;
2581   parser->lexer = lexer;
2582
2583   /* Move the current source position to that of the first token in the
2584      new lexer.  */
2585   cp_lexer_set_source_position_from_token (lexer->next_token);
2586 }
2587
2588 /* Pop the top lexer off the parser stack.  This is never used for the
2589    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2590 static void
2591 cp_parser_pop_lexer (cp_parser *parser)
2592 {
2593   cp_lexer *lexer = parser->lexer;
2594   parser->lexer = lexer->next;
2595   cp_lexer_destroy (lexer);
2596
2597   /* Put the current source position back where it was before this
2598      lexer was pushed.  */
2599   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2600 }
2601
2602 /* Lexical conventions [gram.lex]  */
2603
2604 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2605    identifier.  */
2606
2607 static tree
2608 cp_parser_identifier (cp_parser* parser)
2609 {
2610   cp_token *token;
2611
2612   /* Look for the identifier.  */
2613   token = cp_parser_require (parser, CPP_NAME, "identifier");
2614   /* Return the value.  */
2615   return token ? token->value : error_mark_node;
2616 }
2617
2618 /* Parse a sequence of adjacent string constants.  Returns a
2619    TREE_STRING representing the combined, nul-terminated string
2620    constant.  If TRANSLATE is true, translate the string to the
2621    execution character set.  If WIDE_OK is true, a wide string is
2622    invalid here.
2623
2624    C++98 [lex.string] says that if a narrow string literal token is
2625    adjacent to a wide string literal token, the behavior is undefined.
2626    However, C99 6.4.5p4 says that this results in a wide string literal.
2627    We follow C99 here, for consistency with the C front end.
2628
2629    This code is largely lifted from lex_string() in c-lex.c.
2630
2631    FUTURE: ObjC++ will need to handle @-strings here.  */
2632 static tree
2633 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2634 {
2635   tree value;
2636   bool wide = false;
2637   size_t count;
2638   struct obstack str_ob;
2639   cpp_string str, istr, *strs;
2640   cp_token *tok;
2641
2642   tok = cp_lexer_peek_token (parser->lexer);
2643   if (!cp_parser_is_string_literal (tok))
2644     {
2645       cp_parser_error (parser, "expected string-literal");
2646       return error_mark_node;
2647     }
2648
2649   /* Try to avoid the overhead of creating and destroying an obstack
2650      for the common case of just one string.  */
2651   if (!cp_parser_is_string_literal
2652       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2653     {
2654       cp_lexer_consume_token (parser->lexer);
2655
2656       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2657       str.len = TREE_STRING_LENGTH (tok->value);
2658       count = 1;
2659       if (tok->type == CPP_WSTRING)
2660         wide = true;
2661
2662       strs = &str;
2663     }
2664   else
2665     {
2666       gcc_obstack_init (&str_ob);
2667       count = 0;
2668
2669       do
2670         {
2671           cp_lexer_consume_token (parser->lexer);
2672           count++;
2673           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2674           str.len = TREE_STRING_LENGTH (tok->value);
2675           if (tok->type == CPP_WSTRING)
2676             wide = true;
2677
2678           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2679
2680           tok = cp_lexer_peek_token (parser->lexer);
2681         }
2682       while (cp_parser_is_string_literal (tok));
2683
2684       strs = (cpp_string *) obstack_finish (&str_ob);
2685     }
2686
2687   if (wide && !wide_ok)
2688     {
2689       cp_parser_error (parser, "a wide string is invalid in this context");
2690       wide = false;
2691     }
2692
2693   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2694       (parse_in, strs, count, &istr, wide))
2695     {
2696       value = build_string (istr.len, (char *)istr.text);
2697       free ((void *)istr.text);
2698
2699       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2700       value = fix_string_type (value);
2701     }
2702   else
2703     /* cpp_interpret_string has issued an error.  */
2704     value = error_mark_node;
2705
2706   if (count > 1)
2707     obstack_free (&str_ob, 0);
2708
2709   return value;
2710 }
2711
2712
2713 /* Basic concepts [gram.basic]  */
2714
2715 /* Parse a translation-unit.
2716
2717    translation-unit:
2718      declaration-seq [opt]
2719
2720    Returns TRUE if all went well.  */
2721
2722 static bool
2723 cp_parser_translation_unit (cp_parser* parser)
2724 {
2725   /* The address of the first non-permanent object on the declarator
2726      obstack.  */
2727   static void *declarator_obstack_base;
2728
2729   bool success;
2730
2731   /* Create the declarator obstack, if necessary.  */
2732   if (!cp_error_declarator)
2733     {
2734       gcc_obstack_init (&declarator_obstack);
2735       /* Create the error declarator.  */
2736       cp_error_declarator = make_declarator (cdk_error);
2737       /* Create the empty parameter list.  */
2738       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2739       /* Remember where the base of the declarator obstack lies.  */
2740       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2741     }
2742
2743   cp_parser_declaration_seq_opt (parser);
2744
2745   /* If there are no tokens left then all went well.  */
2746   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2747     {
2748       /* Get rid of the token array; we don't need it any more.  */
2749       cp_lexer_destroy (parser->lexer);
2750       parser->lexer = NULL;
2751
2752       /* This file might have been a context that's implicitly extern
2753          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2754       if (parser->implicit_extern_c)
2755         {
2756           pop_lang_context ();
2757           parser->implicit_extern_c = false;
2758         }
2759
2760       /* Finish up.  */
2761       finish_translation_unit ();
2762
2763       success = true;
2764     }
2765   else
2766     {
2767       cp_parser_error (parser, "expected declaration");
2768       success = false;
2769     }
2770
2771   /* Make sure the declarator obstack was fully cleaned up.  */
2772   gcc_assert (obstack_next_free (&declarator_obstack)
2773               == declarator_obstack_base);
2774
2775   /* All went well.  */
2776   return success;
2777 }
2778
2779 /* Expressions [gram.expr] */
2780
2781 /* Parse a primary-expression.
2782
2783    primary-expression:
2784      literal
2785      this
2786      ( expression )
2787      id-expression
2788
2789    GNU Extensions:
2790
2791    primary-expression:
2792      ( compound-statement )
2793      __builtin_va_arg ( assignment-expression , type-id )
2794      __builtin_offsetof ( type-id , offsetof-expression )
2795
2796    Objective-C++ Extension:
2797
2798    primary-expression:
2799      objc-expression
2800
2801    literal:
2802      __null
2803
2804    ADDRESS_P is true iff this expression was immediately preceded by
2805    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2806    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2807    true iff this expression is a template argument.
2808
2809    Returns a representation of the expression.  Upon return, *IDK
2810    indicates what kind of id-expression (if any) was present.  */
2811
2812 static tree
2813 cp_parser_primary_expression (cp_parser *parser,
2814                               bool address_p,
2815                               bool cast_p,
2816                               bool template_arg_p,
2817                               cp_id_kind *idk)
2818 {
2819   cp_token *token;
2820
2821   /* Assume the primary expression is not an id-expression.  */
2822   *idk = CP_ID_KIND_NONE;
2823
2824   /* Peek at the next token.  */
2825   token = cp_lexer_peek_token (parser->lexer);
2826   switch (token->type)
2827     {
2828       /* literal:
2829            integer-literal
2830            character-literal
2831            floating-literal
2832            string-literal
2833            boolean-literal  */
2834     case CPP_CHAR:
2835     case CPP_WCHAR:
2836     case CPP_NUMBER:
2837       token = cp_lexer_consume_token (parser->lexer);
2838       /* Floating-point literals are only allowed in an integral
2839          constant expression if they are cast to an integral or
2840          enumeration type.  */
2841       if (TREE_CODE (token->value) == REAL_CST
2842           && parser->integral_constant_expression_p
2843           && pedantic)
2844         {
2845           /* CAST_P will be set even in invalid code like "int(2.7 +
2846              ...)".   Therefore, we have to check that the next token
2847              is sure to end the cast.  */
2848           if (cast_p)
2849             {
2850               cp_token *next_token;
2851
2852               next_token = cp_lexer_peek_token (parser->lexer);
2853               if (/* The comma at the end of an
2854                      enumerator-definition.  */
2855                   next_token->type != CPP_COMMA
2856                   /* The curly brace at the end of an enum-specifier.  */
2857                   && next_token->type != CPP_CLOSE_BRACE
2858                   /* The end of a statement.  */
2859                   && next_token->type != CPP_SEMICOLON
2860                   /* The end of the cast-expression.  */
2861                   && next_token->type != CPP_CLOSE_PAREN
2862                   /* The end of an array bound.  */
2863                   && next_token->type != CPP_CLOSE_SQUARE
2864                   /* The closing ">" in a template-argument-list.  */
2865                   && (next_token->type != CPP_GREATER
2866                       || parser->greater_than_is_operator_p))
2867                 cast_p = false;
2868             }
2869
2870           /* If we are within a cast, then the constraint that the
2871              cast is to an integral or enumeration type will be
2872              checked at that point.  If we are not within a cast, then
2873              this code is invalid.  */
2874           if (!cast_p)
2875             cp_parser_non_integral_constant_expression
2876               (parser, "floating-point literal");
2877         }
2878       return token->value;
2879
2880     case CPP_STRING:
2881     case CPP_WSTRING:
2882       /* ??? Should wide strings be allowed when parser->translate_strings_p
2883          is false (i.e. in attributes)?  If not, we can kill the third
2884          argument to cp_parser_string_literal.  */
2885       return cp_parser_string_literal (parser,
2886                                        parser->translate_strings_p,
2887                                        true);
2888
2889     case CPP_OPEN_PAREN:
2890       {
2891         tree expr;
2892         bool saved_greater_than_is_operator_p;
2893
2894         /* Consume the `('.  */
2895         cp_lexer_consume_token (parser->lexer);
2896         /* Within a parenthesized expression, a `>' token is always
2897            the greater-than operator.  */
2898         saved_greater_than_is_operator_p
2899           = parser->greater_than_is_operator_p;
2900         parser->greater_than_is_operator_p = true;
2901         /* If we see `( { ' then we are looking at the beginning of
2902            a GNU statement-expression.  */
2903         if (cp_parser_allow_gnu_extensions_p (parser)
2904             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2905           {
2906             /* Statement-expressions are not allowed by the standard.  */
2907             if (pedantic)
2908               pedwarn ("ISO C++ forbids braced-groups within expressions");
2909
2910             /* And they're not allowed outside of a function-body; you
2911                cannot, for example, write:
2912
2913                  int i = ({ int j = 3; j + 1; });
2914
2915                at class or namespace scope.  */
2916             if (!at_function_scope_p ())
2917               error ("statement-expressions are allowed only inside functions");
2918             /* Start the statement-expression.  */
2919             expr = begin_stmt_expr ();
2920             /* Parse the compound-statement.  */
2921             cp_parser_compound_statement (parser, expr, false);
2922             /* Finish up.  */
2923             expr = finish_stmt_expr (expr, false);
2924           }
2925         else
2926           {
2927             /* Parse the parenthesized expression.  */
2928             expr = cp_parser_expression (parser, cast_p);
2929             /* Let the front end know that this expression was
2930                enclosed in parentheses. This matters in case, for
2931                example, the expression is of the form `A::B', since
2932                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2933                not.  */
2934             finish_parenthesized_expr (expr);
2935           }
2936         /* The `>' token might be the end of a template-id or
2937            template-parameter-list now.  */
2938         parser->greater_than_is_operator_p
2939           = saved_greater_than_is_operator_p;
2940         /* Consume the `)'.  */
2941         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2942           cp_parser_skip_to_end_of_statement (parser);
2943
2944         return expr;
2945       }
2946
2947     case CPP_KEYWORD:
2948       switch (token->keyword)
2949         {
2950           /* These two are the boolean literals.  */
2951         case RID_TRUE:
2952           cp_lexer_consume_token (parser->lexer);
2953           return boolean_true_node;
2954         case RID_FALSE:
2955           cp_lexer_consume_token (parser->lexer);
2956           return boolean_false_node;
2957
2958           /* The `__null' literal.  */
2959         case RID_NULL:
2960           cp_lexer_consume_token (parser->lexer);
2961           return null_node;
2962
2963           /* Recognize the `this' keyword.  */
2964         case RID_THIS:
2965           cp_lexer_consume_token (parser->lexer);
2966           if (parser->local_variables_forbidden_p)
2967             {
2968               error ("%<this%> may not be used in this context");
2969               return error_mark_node;
2970             }
2971           /* Pointers cannot appear in constant-expressions.  */
2972           if (cp_parser_non_integral_constant_expression (parser,
2973                                                           "`this'"))
2974             return error_mark_node;
2975           return finish_this_expr ();
2976
2977           /* The `operator' keyword can be the beginning of an
2978              id-expression.  */
2979         case RID_OPERATOR:
2980           goto id_expression;
2981
2982         case RID_FUNCTION_NAME:
2983         case RID_PRETTY_FUNCTION_NAME:
2984         case RID_C99_FUNCTION_NAME:
2985           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2986              __func__ are the names of variables -- but they are
2987              treated specially.  Therefore, they are handled here,
2988              rather than relying on the generic id-expression logic
2989              below.  Grammatically, these names are id-expressions.
2990
2991              Consume the token.  */
2992           token = cp_lexer_consume_token (parser->lexer);
2993           /* Look up the name.  */
2994           return finish_fname (token->value);
2995
2996         case RID_VA_ARG:
2997           {
2998             tree expression;
2999             tree type;
3000
3001             /* The `__builtin_va_arg' construct is used to handle
3002                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3003             cp_lexer_consume_token (parser->lexer);
3004             /* Look for the opening `('.  */
3005             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3006             /* Now, parse the assignment-expression.  */
3007             expression = cp_parser_assignment_expression (parser,
3008                                                           /*cast_p=*/false);
3009             /* Look for the `,'.  */
3010             cp_parser_require (parser, CPP_COMMA, "`,'");
3011             /* Parse the type-id.  */
3012             type = cp_parser_type_id (parser);
3013             /* Look for the closing `)'.  */
3014             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3015             /* Using `va_arg' in a constant-expression is not
3016                allowed.  */
3017             if (cp_parser_non_integral_constant_expression (parser,
3018                                                             "`va_arg'"))
3019               return error_mark_node;
3020             return build_x_va_arg (expression, type);
3021           }
3022
3023         case RID_OFFSETOF:
3024           return cp_parser_builtin_offsetof (parser);
3025
3026           /* Objective-C++ expressions.  */
3027         case RID_AT_ENCODE:
3028         case RID_AT_PROTOCOL:
3029         case RID_AT_SELECTOR:
3030           return cp_parser_objc_expression (parser);
3031
3032         default:
3033           cp_parser_error (parser, "expected primary-expression");
3034           return error_mark_node;
3035         }
3036
3037       /* An id-expression can start with either an identifier, a
3038          `::' as the beginning of a qualified-id, or the "operator"
3039          keyword.  */
3040     case CPP_NAME:
3041     case CPP_SCOPE:
3042     case CPP_TEMPLATE_ID:
3043     case CPP_NESTED_NAME_SPECIFIER:
3044       {
3045         tree id_expression;
3046         tree decl;
3047         const char *error_msg;
3048         bool template_p;
3049         bool done;
3050
3051       id_expression:
3052         /* Parse the id-expression.  */
3053         id_expression
3054           = cp_parser_id_expression (parser,
3055                                      /*template_keyword_p=*/false,
3056                                      /*check_dependency_p=*/true,
3057                                      &template_p,
3058                                      /*declarator_p=*/false,
3059                                      /*optional_p=*/false);
3060         if (id_expression == error_mark_node)
3061           return error_mark_node;
3062         token = cp_lexer_peek_token (parser->lexer);
3063         done = (token->type != CPP_OPEN_SQUARE
3064                 && token->type != CPP_OPEN_PAREN
3065                 && token->type != CPP_DOT
3066                 && token->type != CPP_DEREF
3067                 && token->type != CPP_PLUS_PLUS
3068                 && token->type != CPP_MINUS_MINUS);
3069         /* If we have a template-id, then no further lookup is
3070            required.  If the template-id was for a template-class, we
3071            will sometimes have a TYPE_DECL at this point.  */
3072         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3073                  || TREE_CODE (id_expression) == TYPE_DECL)
3074           decl = id_expression;
3075         /* Look up the name.  */
3076         else
3077           {
3078             tree ambiguous_decls;
3079
3080             decl = cp_parser_lookup_name (parser, id_expression,
3081                                           none_type,
3082                                           template_p,
3083                                           /*is_namespace=*/false,
3084                                           /*check_dependency=*/true,
3085                                           &ambiguous_decls);
3086             /* If the lookup was ambiguous, an error will already have
3087                been issued.  */
3088             if (ambiguous_decls)
3089               return error_mark_node;
3090
3091             /* In Objective-C++, an instance variable (ivar) may be preferred
3092                to whatever cp_parser_lookup_name() found.  */
3093             decl = objc_lookup_ivar (decl, id_expression);
3094
3095             /* If name lookup gives us a SCOPE_REF, then the
3096                qualifying scope was dependent.  */
3097             if (TREE_CODE (decl) == SCOPE_REF)
3098               return decl;
3099             /* Check to see if DECL is a local variable in a context
3100                where that is forbidden.  */
3101             if (parser->local_variables_forbidden_p
3102                 && local_variable_p (decl))
3103               {
3104                 /* It might be that we only found DECL because we are
3105                    trying to be generous with pre-ISO scoping rules.
3106                    For example, consider:
3107
3108                      int i;
3109                      void g() {
3110                        for (int i = 0; i < 10; ++i) {}
3111                        extern void f(int j = i);
3112                      }
3113
3114                    Here, name look up will originally find the out
3115                    of scope `i'.  We need to issue a warning message,
3116                    but then use the global `i'.  */
3117                 decl = check_for_out_of_scope_variable (decl);
3118                 if (local_variable_p (decl))
3119                   {
3120                     error ("local variable %qD may not appear in this context",
3121                            decl);
3122                     return error_mark_node;
3123                   }
3124               }
3125           }
3126
3127         decl = (finish_id_expression
3128                 (id_expression, decl, parser->scope,
3129                  idk,
3130                  parser->integral_constant_expression_p,
3131                  parser->allow_non_integral_constant_expression_p,
3132                  &parser->non_integral_constant_expression_p,
3133                  template_p, done, address_p,
3134                  template_arg_p,
3135                  &error_msg));
3136         if (error_msg)
3137           cp_parser_error (parser, error_msg);
3138         return decl;
3139       }
3140
3141       /* Anything else is an error.  */
3142     default:
3143       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3144       if (c_dialect_objc ()
3145           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3146         return cp_parser_objc_expression (parser);
3147
3148       cp_parser_error (parser, "expected primary-expression");
3149       return error_mark_node;
3150     }
3151 }
3152
3153 /* Parse an id-expression.
3154
3155    id-expression:
3156      unqualified-id
3157      qualified-id
3158
3159    qualified-id:
3160      :: [opt] nested-name-specifier template [opt] unqualified-id
3161      :: identifier
3162      :: operator-function-id
3163      :: template-id
3164
3165    Return a representation of the unqualified portion of the
3166    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3167    a `::' or nested-name-specifier.
3168
3169    Often, if the id-expression was a qualified-id, the caller will
3170    want to make a SCOPE_REF to represent the qualified-id.  This
3171    function does not do this in order to avoid wastefully creating
3172    SCOPE_REFs when they are not required.
3173
3174    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3175    `template' keyword.
3176
3177    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3178    uninstantiated templates.
3179
3180    If *TEMPLATE_P is non-NULL, it is set to true iff the
3181    `template' keyword is used to explicitly indicate that the entity
3182    named is a template.
3183
3184    If DECLARATOR_P is true, the id-expression is appearing as part of
3185    a declarator, rather than as part of an expression.  */
3186
3187 static tree
3188 cp_parser_id_expression (cp_parser *parser,
3189                          bool template_keyword_p,
3190                          bool check_dependency_p,
3191                          bool *template_p,
3192                          bool declarator_p,
3193                          bool optional_p)
3194 {
3195   bool global_scope_p;
3196   bool nested_name_specifier_p;
3197
3198   /* Assume the `template' keyword was not used.  */
3199   if (template_p)
3200     *template_p = template_keyword_p;
3201
3202   /* Look for the optional `::' operator.  */
3203   global_scope_p
3204     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3205        != NULL_TREE);
3206   /* Look for the optional nested-name-specifier.  */
3207   nested_name_specifier_p
3208     = (cp_parser_nested_name_specifier_opt (parser,
3209                                             /*typename_keyword_p=*/false,
3210                                             check_dependency_p,
3211                                             /*type_p=*/false,
3212                                             declarator_p)
3213        != NULL_TREE);
3214   /* If there is a nested-name-specifier, then we are looking at
3215      the first qualified-id production.  */
3216   if (nested_name_specifier_p)
3217     {
3218       tree saved_scope;
3219       tree saved_object_scope;
3220       tree saved_qualifying_scope;
3221       tree unqualified_id;
3222       bool is_template;
3223
3224       /* See if the next token is the `template' keyword.  */
3225       if (!template_p)
3226         template_p = &is_template;
3227       *template_p = cp_parser_optional_template_keyword (parser);
3228       /* Name lookup we do during the processing of the
3229          unqualified-id might obliterate SCOPE.  */
3230       saved_scope = parser->scope;
3231       saved_object_scope = parser->object_scope;
3232       saved_qualifying_scope = parser->qualifying_scope;
3233       /* Process the final unqualified-id.  */
3234       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3235                                                  check_dependency_p,
3236                                                  declarator_p,
3237                                                  /*optional_p=*/false);
3238       /* Restore the SAVED_SCOPE for our caller.  */
3239       parser->scope = saved_scope;
3240       parser->object_scope = saved_object_scope;
3241       parser->qualifying_scope = saved_qualifying_scope;
3242
3243       return unqualified_id;
3244     }
3245   /* Otherwise, if we are in global scope, then we are looking at one
3246      of the other qualified-id productions.  */
3247   else if (global_scope_p)
3248     {
3249       cp_token *token;
3250       tree id;
3251
3252       /* Peek at the next token.  */
3253       token = cp_lexer_peek_token (parser->lexer);
3254
3255       /* If it's an identifier, and the next token is not a "<", then
3256          we can avoid the template-id case.  This is an optimization
3257          for this common case.  */
3258       if (token->type == CPP_NAME
3259           && !cp_parser_nth_token_starts_template_argument_list_p
3260                (parser, 2))
3261         return cp_parser_identifier (parser);
3262
3263       cp_parser_parse_tentatively (parser);
3264       /* Try a template-id.  */
3265       id = cp_parser_template_id (parser,
3266                                   /*template_keyword_p=*/false,
3267                                   /*check_dependency_p=*/true,
3268                                   declarator_p);
3269       /* If that worked, we're done.  */
3270       if (cp_parser_parse_definitely (parser))
3271         return id;
3272
3273       /* Peek at the next token.  (Changes in the token buffer may
3274          have invalidated the pointer obtained above.)  */
3275       token = cp_lexer_peek_token (parser->lexer);
3276
3277       switch (token->type)
3278         {
3279         case CPP_NAME:
3280           return cp_parser_identifier (parser);
3281
3282         case CPP_KEYWORD:
3283           if (token->keyword == RID_OPERATOR)
3284             return cp_parser_operator_function_id (parser);
3285           /* Fall through.  */
3286
3287         default:
3288           cp_parser_error (parser, "expected id-expression");
3289           return error_mark_node;
3290         }
3291     }
3292   else
3293     return cp_parser_unqualified_id (parser, template_keyword_p,
3294                                      /*check_dependency_p=*/true,
3295                                      declarator_p,
3296                                      optional_p);
3297 }
3298
3299 /* Parse an unqualified-id.
3300
3301    unqualified-id:
3302      identifier
3303      operator-function-id
3304      conversion-function-id
3305      ~ class-name
3306      template-id
3307
3308    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3309    keyword, in a construct like `A::template ...'.
3310
3311    Returns a representation of unqualified-id.  For the `identifier'
3312    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3313    production a BIT_NOT_EXPR is returned; the operand of the
3314    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3315    other productions, see the documentation accompanying the
3316    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3317    names are looked up in uninstantiated templates.  If DECLARATOR_P
3318    is true, the unqualified-id is appearing as part of a declarator,
3319    rather than as part of an expression.  */
3320
3321 static tree
3322 cp_parser_unqualified_id (cp_parser* parser,
3323                           bool template_keyword_p,
3324                           bool check_dependency_p,
3325                           bool declarator_p,
3326                           bool optional_p)
3327 {
3328   cp_token *token;
3329
3330   /* Peek at the next token.  */
3331   token = cp_lexer_peek_token (parser->lexer);
3332
3333   switch (token->type)
3334     {
3335     case CPP_NAME:
3336       {
3337         tree id;
3338
3339         /* We don't know yet whether or not this will be a
3340            template-id.  */
3341         cp_parser_parse_tentatively (parser);
3342         /* Try a template-id.  */
3343         id = cp_parser_template_id (parser, template_keyword_p,
3344                                     check_dependency_p,
3345                                     declarator_p);
3346         /* If it worked, we're done.  */
3347         if (cp_parser_parse_definitely (parser))
3348           return id;
3349         /* Otherwise, it's an ordinary identifier.  */
3350         return cp_parser_identifier (parser);
3351       }
3352
3353     case CPP_TEMPLATE_ID:
3354       return cp_parser_template_id (parser, template_keyword_p,
3355                                     check_dependency_p,
3356                                     declarator_p);
3357
3358     case CPP_COMPL:
3359       {
3360         tree type_decl;
3361         tree qualifying_scope;
3362         tree object_scope;
3363         tree scope;
3364         bool done;
3365
3366         /* Consume the `~' token.  */
3367         cp_lexer_consume_token (parser->lexer);
3368         /* Parse the class-name.  The standard, as written, seems to
3369            say that:
3370
3371              template <typename T> struct S { ~S (); };
3372              template <typename T> S<T>::~S() {}
3373
3374            is invalid, since `~' must be followed by a class-name, but
3375            `S<T>' is dependent, and so not known to be a class.
3376            That's not right; we need to look in uninstantiated
3377            templates.  A further complication arises from:
3378
3379              template <typename T> void f(T t) {
3380                t.T::~T();
3381              }
3382
3383            Here, it is not possible to look up `T' in the scope of `T'
3384            itself.  We must look in both the current scope, and the
3385            scope of the containing complete expression.
3386
3387            Yet another issue is:
3388
3389              struct S {
3390                int S;
3391                ~S();
3392              };
3393
3394              S::~S() {}
3395
3396            The standard does not seem to say that the `S' in `~S'
3397            should refer to the type `S' and not the data member
3398            `S::S'.  */
3399
3400         /* DR 244 says that we look up the name after the "~" in the
3401            same scope as we looked up the qualifying name.  That idea
3402            isn't fully worked out; it's more complicated than that.  */
3403         scope = parser->scope;
3404         object_scope = parser->object_scope;
3405         qualifying_scope = parser->qualifying_scope;
3406
3407         /* Check for invalid scopes.  */
3408         if (scope == error_mark_node)
3409           {
3410             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3411               cp_lexer_consume_token (parser->lexer);
3412             return error_mark_node;
3413           }
3414         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3415           {
3416             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3417               error ("scope %qT before %<~%> is not a class-name", scope);
3418             cp_parser_simulate_error (parser);
3419             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3420               cp_lexer_consume_token (parser->lexer);
3421             return error_mark_node;
3422           }
3423         gcc_assert (!scope || TYPE_P (scope));
3424
3425         /* If the name is of the form "X::~X" it's OK.  */
3426         token = cp_lexer_peek_token (parser->lexer);
3427         if (scope
3428             && token->type == CPP_NAME
3429             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3430                 == CPP_OPEN_PAREN)
3431             && constructor_name_p (token->value, scope))
3432           {
3433             cp_lexer_consume_token (parser->lexer);
3434             return build_nt (BIT_NOT_EXPR, scope);
3435           }
3436
3437         /* If there was an explicit qualification (S::~T), first look
3438            in the scope given by the qualification (i.e., S).  */
3439         done = false;
3440         type_decl = NULL_TREE;
3441         if (scope)
3442           {
3443             cp_parser_parse_tentatively (parser);
3444             type_decl = cp_parser_class_name (parser,
3445                                               /*typename_keyword_p=*/false,
3446                                               /*template_keyword_p=*/false,
3447                                               none_type,
3448                                               /*check_dependency=*/false,
3449                                               /*class_head_p=*/false,
3450                                               declarator_p);
3451             if (cp_parser_parse_definitely (parser))
3452               done = true;
3453           }
3454         /* In "N::S::~S", look in "N" as well.  */
3455         if (!done && scope && qualifying_scope)
3456           {
3457             cp_parser_parse_tentatively (parser);
3458             parser->scope = qualifying_scope;
3459             parser->object_scope = NULL_TREE;
3460             parser->qualifying_scope = NULL_TREE;
3461             type_decl
3462               = cp_parser_class_name (parser,
3463                                       /*typename_keyword_p=*/false,
3464                                       /*template_keyword_p=*/false,
3465                                       none_type,
3466                                       /*check_dependency=*/false,
3467                                       /*class_head_p=*/false,
3468                                       declarator_p);
3469             if (cp_parser_parse_definitely (parser))
3470               done = true;
3471           }
3472         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3473         else if (!done && object_scope)
3474           {
3475             cp_parser_parse_tentatively (parser);
3476             parser->scope = object_scope;
3477             parser->object_scope = NULL_TREE;
3478             parser->qualifying_scope = NULL_TREE;
3479             type_decl
3480               = cp_parser_class_name (parser,
3481                                       /*typename_keyword_p=*/false,
3482                                       /*template_keyword_p=*/false,
3483                                       none_type,
3484                                       /*check_dependency=*/false,
3485                                       /*class_head_p=*/false,
3486                                       declarator_p);
3487             if (cp_parser_parse_definitely (parser))
3488               done = true;
3489           }
3490         /* Look in the surrounding context.  */
3491         if (!done)
3492           {
3493             parser->scope = NULL_TREE;
3494             parser->object_scope = NULL_TREE;
3495             parser->qualifying_scope = NULL_TREE;
3496             type_decl
3497               = cp_parser_class_name (parser,
3498                                       /*typename_keyword_p=*/false,
3499                                       /*template_keyword_p=*/false,
3500                                       none_type,
3501                                       /*check_dependency=*/false,
3502                                       /*class_head_p=*/false,
3503                                       declarator_p);
3504           }
3505         /* If an error occurred, assume that the name of the
3506            destructor is the same as the name of the qualifying
3507            class.  That allows us to keep parsing after running
3508            into ill-formed destructor names.  */
3509         if (type_decl == error_mark_node && scope)
3510           return build_nt (BIT_NOT_EXPR, scope);
3511         else if (type_decl == error_mark_node)
3512           return error_mark_node;
3513
3514         /* Check that destructor name and scope match.  */
3515         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3516           {
3517             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3518               error ("declaration of %<~%T%> as member of %qT",
3519                      type_decl, scope);
3520             cp_parser_simulate_error (parser);
3521             return error_mark_node;
3522           }
3523
3524         /* [class.dtor]
3525
3526            A typedef-name that names a class shall not be used as the
3527            identifier in the declarator for a destructor declaration.  */
3528         if (declarator_p
3529             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3530             && !DECL_SELF_REFERENCE_P (type_decl)
3531             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3532           error ("typedef-name %qD used as destructor declarator",
3533                  type_decl);
3534
3535         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3536       }
3537
3538     case CPP_KEYWORD:
3539       if (token->keyword == RID_OPERATOR)
3540         {
3541           tree id;
3542
3543           /* This could be a template-id, so we try that first.  */
3544           cp_parser_parse_tentatively (parser);
3545           /* Try a template-id.  */
3546           id = cp_parser_template_id (parser, template_keyword_p,
3547                                       /*check_dependency_p=*/true,
3548                                       declarator_p);
3549           /* If that worked, we're done.  */
3550           if (cp_parser_parse_definitely (parser))
3551             return id;
3552           /* We still don't know whether we're looking at an
3553              operator-function-id or a conversion-function-id.  */
3554           cp_parser_parse_tentatively (parser);
3555           /* Try an operator-function-id.  */
3556           id = cp_parser_operator_function_id (parser);
3557           /* If that didn't work, try a conversion-function-id.  */
3558           if (!cp_parser_parse_definitely (parser))
3559             id = cp_parser_conversion_function_id (parser);
3560
3561           return id;
3562         }
3563       /* Fall through.  */
3564
3565     default:
3566       if (optional_p)
3567         return NULL_TREE;
3568       cp_parser_error (parser, "expected unqualified-id");
3569       return error_mark_node;
3570     }
3571 }
3572
3573 /* Parse an (optional) nested-name-specifier.
3574
3575    nested-name-specifier:
3576      class-or-namespace-name :: nested-name-specifier [opt]
3577      class-or-namespace-name :: template nested-name-specifier [opt]
3578
3579    PARSER->SCOPE should be set appropriately before this function is
3580    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3581    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3582    in name lookups.
3583
3584    Sets PARSER->SCOPE to the class (TYPE) or namespace
3585    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3586    it unchanged if there is no nested-name-specifier.  Returns the new
3587    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3588
3589    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3590    part of a declaration and/or decl-specifier.  */
3591
3592 static tree
3593 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3594                                      bool typename_keyword_p,
3595                                      bool check_dependency_p,
3596                                      bool type_p,
3597                                      bool is_declaration)
3598 {
3599   bool success = false;
3600   cp_token_position start = 0;
3601   cp_token *token;
3602
3603   /* Remember where the nested-name-specifier starts.  */
3604   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3605     {
3606       start = cp_lexer_token_position (parser->lexer, false);
3607       push_deferring_access_checks (dk_deferred);
3608     }
3609
3610   while (true)
3611     {
3612       tree new_scope;
3613       tree old_scope;
3614       tree saved_qualifying_scope;
3615       bool template_keyword_p;
3616
3617       /* Spot cases that cannot be the beginning of a
3618          nested-name-specifier.  */
3619       token = cp_lexer_peek_token (parser->lexer);
3620
3621       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3622          the already parsed nested-name-specifier.  */
3623       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3624         {
3625           /* Grab the nested-name-specifier and continue the loop.  */
3626           cp_parser_pre_parsed_nested_name_specifier (parser);
3627           success = true;
3628           continue;
3629         }
3630
3631       /* Spot cases that cannot be the beginning of a
3632          nested-name-specifier.  On the second and subsequent times
3633          through the loop, we look for the `template' keyword.  */
3634       if (success && token->keyword == RID_TEMPLATE)
3635         ;
3636       /* A template-id can start a nested-name-specifier.  */
3637       else if (token->type == CPP_TEMPLATE_ID)
3638         ;
3639       else
3640         {
3641           /* If the next token is not an identifier, then it is
3642              definitely not a class-or-namespace-name.  */
3643           if (token->type != CPP_NAME)
3644             break;
3645           /* If the following token is neither a `<' (to begin a
3646              template-id), nor a `::', then we are not looking at a
3647              nested-name-specifier.  */
3648           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3649           if (token->type != CPP_SCOPE
3650               && !cp_parser_nth_token_starts_template_argument_list_p
3651                   (parser, 2))
3652             break;
3653         }
3654
3655       /* The nested-name-specifier is optional, so we parse
3656          tentatively.  */
3657       cp_parser_parse_tentatively (parser);
3658
3659       /* Look for the optional `template' keyword, if this isn't the
3660          first time through the loop.  */
3661       if (success)
3662         template_keyword_p = cp_parser_optional_template_keyword (parser);
3663       else
3664         template_keyword_p = false;
3665
3666       /* Save the old scope since the name lookup we are about to do
3667          might destroy it.  */
3668       old_scope = parser->scope;
3669       saved_qualifying_scope = parser->qualifying_scope;
3670       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3671          look up names in "X<T>::I" in order to determine that "Y" is
3672          a template.  So, if we have a typename at this point, we make
3673          an effort to look through it.  */
3674       if (is_declaration
3675           && !typename_keyword_p
3676           && parser->scope
3677           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3678         parser->scope = resolve_typename_type (parser->scope,
3679                                                /*only_current_p=*/false);
3680       /* Parse the qualifying entity.  */
3681       new_scope
3682         = cp_parser_class_or_namespace_name (parser,
3683                                              typename_keyword_p,
3684                                              template_keyword_p,
3685                                              check_dependency_p,
3686                                              type_p,
3687                                              is_declaration);
3688       /* Look for the `::' token.  */
3689       cp_parser_require (parser, CPP_SCOPE, "`::'");
3690
3691       /* If we found what we wanted, we keep going; otherwise, we're
3692          done.  */
3693       if (!cp_parser_parse_definitely (parser))
3694         {
3695           bool error_p = false;
3696
3697           /* Restore the OLD_SCOPE since it was valid before the
3698              failed attempt at finding the last
3699              class-or-namespace-name.  */
3700           parser->scope = old_scope;
3701           parser->qualifying_scope = saved_qualifying_scope;
3702           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3703             break;
3704           /* If the next token is an identifier, and the one after
3705              that is a `::', then any valid interpretation would have
3706              found a class-or-namespace-name.  */
3707           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3708                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3709                      == CPP_SCOPE)
3710                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3711                      != CPP_COMPL))
3712             {
3713               token = cp_lexer_consume_token (parser->lexer);
3714               if (!error_p)
3715                 {
3716                   if (!token->ambiguous_p)
3717                     {
3718                       tree decl;
3719                       tree ambiguous_decls;
3720
3721                       decl = cp_parser_lookup_name (parser, token->value,
3722                                                     none_type,
3723                                                     /*is_template=*/false,
3724                                                     /*is_namespace=*/false,
3725                                                     /*check_dependency=*/true,
3726                                                     &ambiguous_decls);
3727                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3728                         error ("%qD used without template parameters", decl);
3729                       else if (ambiguous_decls)
3730                         {
3731                           error ("reference to %qD is ambiguous",
3732                                  token->value);
3733                           print_candidates (ambiguous_decls);
3734                           decl = error_mark_node;
3735                         }
3736                       else
3737                         cp_parser_name_lookup_error
3738                           (parser, token->value, decl,
3739                            "is not a class or namespace");
3740                     }
3741                   parser->scope = error_mark_node;
3742                   error_p = true;
3743                   /* Treat this as a successful nested-name-specifier
3744                      due to:
3745
3746                      [basic.lookup.qual]
3747
3748                      If the name found is not a class-name (clause
3749                      _class_) or namespace-name (_namespace.def_), the
3750                      program is ill-formed.  */
3751                   success = true;
3752                 }
3753               cp_lexer_consume_token (parser->lexer);
3754             }
3755           break;
3756         }
3757       /* We've found one valid nested-name-specifier.  */
3758       success = true;
3759       /* Name lookup always gives us a DECL.  */
3760       if (TREE_CODE (new_scope) == TYPE_DECL)
3761         new_scope = TREE_TYPE (new_scope);
3762       /* Uses of "template" must be followed by actual templates.  */
3763       if (template_keyword_p
3764           && !(CLASS_TYPE_P (new_scope)
3765                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3766                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3767                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3768           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3769                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3770                    == TEMPLATE_ID_EXPR)))
3771         pedwarn (TYPE_P (new_scope)
3772                  ? "%qT is not a template"
3773                  : "%qD is not a template",
3774                  new_scope);
3775       /* If it is a class scope, try to complete it; we are about to
3776          be looking up names inside the class.  */
3777       if (TYPE_P (new_scope)
3778           /* Since checking types for dependency can be expensive,
3779              avoid doing it if the type is already complete.  */
3780           && !COMPLETE_TYPE_P (new_scope)
3781           /* Do not try to complete dependent types.  */
3782           && !dependent_type_p (new_scope))
3783         new_scope = complete_type (new_scope);
3784       /* Make sure we look in the right scope the next time through
3785          the loop.  */
3786       parser->scope = new_scope;
3787     }
3788
3789   /* If parsing tentatively, replace the sequence of tokens that makes
3790      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3791      token.  That way, should we re-parse the token stream, we will
3792      not have to repeat the effort required to do the parse, nor will
3793      we issue duplicate error messages.  */
3794   if (success && start)
3795     {
3796       cp_token *token;
3797       tree access_checks;
3798
3799       token = cp_lexer_token_at (parser->lexer, start);
3800       /* Reset the contents of the START token.  */
3801       token->type = CPP_NESTED_NAME_SPECIFIER;
3802       /* Retrieve any deferred checks.  Do not pop this access checks yet
3803          so the memory will not be reclaimed during token replacing below.  */
3804       access_checks = get_deferred_access_checks ();
3805       token->value = build_tree_list (copy_list (access_checks),
3806                                       parser->scope);
3807       TREE_TYPE (token->value) = parser->qualifying_scope;
3808       token->keyword = RID_MAX;
3809
3810       /* Purge all subsequent tokens.  */
3811       cp_lexer_purge_tokens_after (parser->lexer, start);
3812     }
3813
3814   if (start)
3815     pop_to_parent_deferring_access_checks ();
3816
3817   return success ? parser->scope : NULL_TREE;
3818 }
3819
3820 /* Parse a nested-name-specifier.  See
3821    cp_parser_nested_name_specifier_opt for details.  This function
3822    behaves identically, except that it will an issue an error if no
3823    nested-name-specifier is present.  */
3824
3825 static tree
3826 cp_parser_nested_name_specifier (cp_parser *parser,
3827                                  bool typename_keyword_p,
3828                                  bool check_dependency_p,
3829                                  bool type_p,
3830                                  bool is_declaration)
3831 {
3832   tree scope;
3833
3834   /* Look for the nested-name-specifier.  */
3835   scope = cp_parser_nested_name_specifier_opt (parser,
3836                                                typename_keyword_p,
3837                                                check_dependency_p,
3838                                                type_p,
3839                                                is_declaration);
3840   /* If it was not present, issue an error message.  */
3841   if (!scope)
3842     {
3843       cp_parser_error (parser, "expected nested-name-specifier");
3844       parser->scope = NULL_TREE;
3845     }
3846
3847   return scope;
3848 }
3849
3850 /* Parse a class-or-namespace-name.
3851
3852    class-or-namespace-name:
3853      class-name
3854      namespace-name
3855
3856    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3857    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3858    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3859    TYPE_P is TRUE iff the next name should be taken as a class-name,
3860    even the same name is declared to be another entity in the same
3861    scope.
3862
3863    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3864    specified by the class-or-namespace-name.  If neither is found the
3865    ERROR_MARK_NODE is returned.  */
3866
3867 static tree
3868 cp_parser_class_or_namespace_name (cp_parser *parser,
3869                                    bool typename_keyword_p,
3870                                    bool template_keyword_p,
3871                                    bool check_dependency_p,
3872                                    bool type_p,
3873                                    bool is_declaration)
3874 {
3875   tree saved_scope;
3876   tree saved_qualifying_scope;
3877   tree saved_object_scope;
3878   tree scope;
3879   bool only_class_p;
3880
3881   /* Before we try to parse the class-name, we must save away the
3882      current PARSER->SCOPE since cp_parser_class_name will destroy
3883      it.  */
3884   saved_scope = parser->scope;
3885   saved_qualifying_scope = parser->qualifying_scope;
3886   saved_object_scope = parser->object_scope;
3887   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3888      there is no need to look for a namespace-name.  */
3889   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3890   if (!only_class_p)
3891     cp_parser_parse_tentatively (parser);
3892   scope = cp_parser_class_name (parser,
3893                                 typename_keyword_p,
3894                                 template_keyword_p,
3895                                 type_p ? class_type : none_type,
3896                                 check_dependency_p,
3897                                 /*class_head_p=*/false,
3898                                 is_declaration);
3899   /* If that didn't work, try for a namespace-name.  */
3900   if (!only_class_p && !cp_parser_parse_definitely (parser))
3901     {
3902       /* Restore the saved scope.  */
3903       parser->scope = saved_scope;
3904       parser->qualifying_scope = saved_qualifying_scope;
3905       parser->object_scope = saved_object_scope;
3906       /* If we are not looking at an identifier followed by the scope
3907          resolution operator, then this is not part of a
3908          nested-name-specifier.  (Note that this function is only used
3909          to parse the components of a nested-name-specifier.)  */
3910       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3911           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3912         return error_mark_node;
3913       scope = cp_parser_namespace_name (parser);
3914     }
3915
3916   return scope;
3917 }
3918
3919 /* Parse a postfix-expression.
3920
3921    postfix-expression:
3922      primary-expression
3923      postfix-expression [ expression ]
3924      postfix-expression ( expression-list [opt] )
3925      simple-type-specifier ( expression-list [opt] )
3926      typename :: [opt] nested-name-specifier identifier
3927        ( expression-list [opt] )
3928      typename :: [opt] nested-name-specifier template [opt] template-id
3929        ( expression-list [opt] )
3930      postfix-expression . template [opt] id-expression
3931      postfix-expression -> template [opt] id-expression
3932      postfix-expression . pseudo-destructor-name
3933      postfix-expression -> pseudo-destructor-name
3934      postfix-expression ++
3935      postfix-expression --
3936      dynamic_cast < type-id > ( expression )
3937      static_cast < type-id > ( expression )
3938      reinterpret_cast < type-id > ( expression )
3939      const_cast < type-id > ( expression )
3940      typeid ( expression )
3941      typeid ( type-id )
3942
3943    GNU Extension:
3944
3945    postfix-expression:
3946      ( type-id ) { initializer-list , [opt] }
3947
3948    This extension is a GNU version of the C99 compound-literal
3949    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3950    but they are essentially the same concept.)
3951
3952    If ADDRESS_P is true, the postfix expression is the operand of the
3953    `&' operator.  CAST_P is true if this expression is the target of a
3954    cast.
3955
3956    Returns a representation of the expression.  */
3957
3958 static tree
3959 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3960 {
3961   cp_token *token;
3962   enum rid keyword;
3963   cp_id_kind idk = CP_ID_KIND_NONE;
3964   tree postfix_expression = NULL_TREE;
3965
3966   /* Peek at the next token.  */
3967   token = cp_lexer_peek_token (parser->lexer);
3968   /* Some of the productions are determined by keywords.  */
3969   keyword = token->keyword;
3970   switch (keyword)
3971     {
3972     case RID_DYNCAST:
3973     case RID_STATCAST:
3974     case RID_REINTCAST:
3975     case RID_CONSTCAST:
3976       {
3977         tree type;
3978         tree expression;
3979         const char *saved_message;
3980
3981         /* All of these can be handled in the same way from the point
3982            of view of parsing.  Begin by consuming the token
3983            identifying the cast.  */
3984         cp_lexer_consume_token (parser->lexer);
3985
3986         /* New types cannot be defined in the cast.  */
3987         saved_message = parser->type_definition_forbidden_message;
3988         parser->type_definition_forbidden_message
3989           = "types may not be defined in casts";
3990
3991         /* Look for the opening `<'.  */
3992         cp_parser_require (parser, CPP_LESS, "`<'");
3993         /* Parse the type to which we are casting.  */
3994         type = cp_parser_type_id (parser);
3995         /* Look for the closing `>'.  */
3996         cp_parser_require (parser, CPP_GREATER, "`>'");
3997         /* Restore the old message.  */
3998         parser->type_definition_forbidden_message = saved_message;
3999
4000         /* And the expression which is being cast.  */
4001         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4002         expression = cp_parser_expression (parser, /*cast_p=*/true);
4003         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4004
4005         /* Only type conversions to integral or enumeration types
4006            can be used in constant-expressions.  */
4007         if (parser->integral_constant_expression_p
4008             && !dependent_type_p (type)
4009             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4010             && (cp_parser_non_integral_constant_expression
4011                 (parser,
4012                  "a cast to a type other than an integral or "
4013                  "enumeration type")))
4014           return error_mark_node;
4015
4016         switch (keyword)
4017           {
4018           case RID_DYNCAST:
4019             postfix_expression
4020               = build_dynamic_cast (type, expression);
4021             break;
4022           case RID_STATCAST:
4023             postfix_expression
4024               = build_static_cast (type, expression);
4025             break;
4026           case RID_REINTCAST:
4027             postfix_expression
4028               = build_reinterpret_cast (type, expression);
4029             break;
4030           case RID_CONSTCAST:
4031             postfix_expression
4032               = build_const_cast (type, expression);
4033             break;
4034           default:
4035             gcc_unreachable ();
4036           }
4037       }
4038       break;
4039
4040     case RID_TYPEID:
4041       {
4042         tree type;
4043         const char *saved_message;
4044         bool saved_in_type_id_in_expr_p;
4045
4046         /* Consume the `typeid' token.  */
4047         cp_lexer_consume_token (parser->lexer);
4048         /* Look for the `(' token.  */
4049         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4050         /* Types cannot be defined in a `typeid' expression.  */
4051         saved_message = parser->type_definition_forbidden_message;
4052         parser->type_definition_forbidden_message
4053           = "types may not be defined in a `typeid\' expression";
4054         /* We can't be sure yet whether we're looking at a type-id or an
4055            expression.  */
4056         cp_parser_parse_tentatively (parser);
4057         /* Try a type-id first.  */
4058         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4059         parser->in_type_id_in_expr_p = true;
4060         type = cp_parser_type_id (parser);
4061         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4062         /* Look for the `)' token.  Otherwise, we can't be sure that
4063            we're not looking at an expression: consider `typeid (int
4064            (3))', for example.  */
4065         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4066         /* If all went well, simply lookup the type-id.  */
4067         if (cp_parser_parse_definitely (parser))
4068           postfix_expression = get_typeid (type);
4069         /* Otherwise, fall back to the expression variant.  */
4070         else
4071           {
4072             tree expression;
4073
4074             /* Look for an expression.  */
4075             expression = cp_parser_expression (parser, /*cast_p=*/false);
4076             /* Compute its typeid.  */
4077             postfix_expression = build_typeid (expression);
4078             /* Look for the `)' token.  */
4079             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4080           }
4081         /* `typeid' may not appear in an integral constant expression.  */
4082         if (cp_parser_non_integral_constant_expression(parser,
4083                                                        "`typeid' operator"))
4084           return error_mark_node;
4085         /* Restore the saved message.  */
4086         parser->type_definition_forbidden_message = saved_message;
4087       }
4088       break;
4089
4090     case RID_TYPENAME:
4091       {
4092         tree type;
4093         /* The syntax permitted here is the same permitted for an
4094            elaborated-type-specifier.  */
4095         type = cp_parser_elaborated_type_specifier (parser,
4096                                                     /*is_friend=*/false,
4097                                                     /*is_declaration=*/false);
4098         postfix_expression = cp_parser_functional_cast (parser, type);
4099       }
4100       break;
4101
4102     default:
4103       {
4104         tree type;
4105
4106         /* If the next thing is a simple-type-specifier, we may be
4107            looking at a functional cast.  We could also be looking at
4108            an id-expression.  So, we try the functional cast, and if
4109            that doesn't work we fall back to the primary-expression.  */
4110         cp_parser_parse_tentatively (parser);
4111         /* Look for the simple-type-specifier.  */
4112         type = cp_parser_simple_type_specifier (parser,
4113                                                 /*decl_specs=*/NULL,
4114                                                 CP_PARSER_FLAGS_NONE);
4115         /* Parse the cast itself.  */
4116         if (!cp_parser_error_occurred (parser))
4117           postfix_expression
4118             = cp_parser_functional_cast (parser, type);
4119         /* If that worked, we're done.  */
4120         if (cp_parser_parse_definitely (parser))
4121           break;
4122
4123         /* If the functional-cast didn't work out, try a
4124            compound-literal.  */
4125         if (cp_parser_allow_gnu_extensions_p (parser)
4126             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4127           {
4128             VEC(constructor_elt,gc) *initializer_list = NULL;
4129             bool saved_in_type_id_in_expr_p;
4130
4131             cp_parser_parse_tentatively (parser);
4132             /* Consume the `('.  */
4133             cp_lexer_consume_token (parser->lexer);
4134             /* Parse the type.  */
4135             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4136             parser->in_type_id_in_expr_p = true;
4137             type = cp_parser_type_id (parser);
4138             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4139             /* Look for the `)'.  */
4140             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4141             /* Look for the `{'.  */
4142             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4143             /* If things aren't going well, there's no need to
4144                keep going.  */
4145             if (!cp_parser_error_occurred (parser))
4146               {
4147                 bool non_constant_p;
4148                 /* Parse the initializer-list.  */
4149                 initializer_list
4150                   = cp_parser_initializer_list (parser, &non_constant_p);
4151                 /* Allow a trailing `,'.  */
4152                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4153                   cp_lexer_consume_token (parser->lexer);
4154                 /* Look for the final `}'.  */
4155                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4156               }
4157             /* If that worked, we're definitely looking at a
4158                compound-literal expression.  */
4159             if (cp_parser_parse_definitely (parser))
4160               {
4161                 /* Warn the user that a compound literal is not
4162                    allowed in standard C++.  */
4163                 if (pedantic)
4164                   pedwarn ("ISO C++ forbids compound-literals");
4165                 /* Form the representation of the compound-literal.  */
4166                 postfix_expression
4167                   = finish_compound_literal (type, initializer_list);
4168                 break;
4169               }
4170           }
4171
4172         /* It must be a primary-expression.  */
4173         postfix_expression
4174           = cp_parser_primary_expression (parser, address_p, cast_p,
4175                                           /*template_arg_p=*/false,
4176                                           &idk);
4177       }
4178       break;
4179     }
4180
4181   /* Keep looping until the postfix-expression is complete.  */
4182   while (true)
4183     {
4184       if (idk == CP_ID_KIND_UNQUALIFIED
4185           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4186           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4187         /* It is not a Koenig lookup function call.  */
4188         postfix_expression
4189           = unqualified_name_lookup_error (postfix_expression);
4190
4191       /* Peek at the next token.  */
4192       token = cp_lexer_peek_token (parser->lexer);
4193
4194       switch (token->type)
4195         {
4196         case CPP_OPEN_SQUARE:
4197           postfix_expression
4198             = cp_parser_postfix_open_square_expression (parser,
4199                                                         postfix_expression,
4200                                                         false);
4201           idk = CP_ID_KIND_NONE;
4202           break;
4203
4204         case CPP_OPEN_PAREN:
4205           /* postfix-expression ( expression-list [opt] ) */
4206           {
4207             bool koenig_p;
4208             bool is_builtin_constant_p;
4209             bool saved_integral_constant_expression_p = false;
4210             bool saved_non_integral_constant_expression_p = false;
4211             tree args;
4212
4213             is_builtin_constant_p
4214               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4215             if (is_builtin_constant_p)
4216               {
4217                 /* The whole point of __builtin_constant_p is to allow
4218                    non-constant expressions to appear as arguments.  */
4219                 saved_integral_constant_expression_p
4220                   = parser->integral_constant_expression_p;
4221                 saved_non_integral_constant_expression_p
4222                   = parser->non_integral_constant_expression_p;
4223                 parser->integral_constant_expression_p = false;
4224               }
4225             args = (cp_parser_parenthesized_expression_list
4226                     (parser, /*is_attribute_list=*/false,
4227                      /*cast_p=*/false,
4228                      /*non_constant_p=*/NULL));
4229             if (is_builtin_constant_p)
4230               {
4231                 parser->integral_constant_expression_p
4232                   = saved_integral_constant_expression_p;
4233                 parser->non_integral_constant_expression_p
4234                   = saved_non_integral_constant_expression_p;
4235               }
4236
4237             if (args == error_mark_node)
4238               {
4239                 postfix_expression = error_mark_node;
4240                 break;
4241               }
4242
4243             /* Function calls are not permitted in
4244                constant-expressions.  */
4245             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4246                 && cp_parser_non_integral_constant_expression (parser,
4247                                                                "a function call"))
4248               {
4249                 postfix_expression = error_mark_node;
4250                 break;
4251               }
4252
4253             koenig_p = false;
4254             if (idk == CP_ID_KIND_UNQUALIFIED)
4255               {
4256                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4257                   {
4258                     if (args)
4259                       {
4260                         koenig_p = true;
4261                         postfix_expression
4262                           = perform_koenig_lookup (postfix_expression, args);
4263                       }
4264                     else
4265                       postfix_expression
4266                         = unqualified_fn_lookup_error (postfix_expression);
4267                   }
4268                 /* We do not perform argument-dependent lookup if
4269                    normal lookup finds a non-function, in accordance
4270                    with the expected resolution of DR 218.  */
4271                 else if (args && is_overloaded_fn (postfix_expression))
4272                   {
4273                     tree fn = get_first_fn (postfix_expression);
4274
4275                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4276                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4277
4278                     /* Only do argument dependent lookup if regular
4279                        lookup does not find a set of member functions.
4280                        [basic.lookup.koenig]/2a  */
4281                     if (!DECL_FUNCTION_MEMBER_P (fn))
4282                       {
4283                         koenig_p = true;
4284                         postfix_expression
4285                           = perform_koenig_lookup (postfix_expression, args);
4286                       }
4287                   }
4288               }
4289
4290             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4291               {
4292                 tree instance = TREE_OPERAND (postfix_expression, 0);
4293                 tree fn = TREE_OPERAND (postfix_expression, 1);
4294
4295                 if (processing_template_decl
4296                     && (type_dependent_expression_p (instance)
4297                         || (!BASELINK_P (fn)
4298                             && TREE_CODE (fn) != FIELD_DECL)
4299                         || type_dependent_expression_p (fn)
4300                         || any_type_dependent_arguments_p (args)))
4301                   {
4302                     postfix_expression
4303                       = build_min_nt (CALL_EXPR, postfix_expression,
4304                                       args, NULL_TREE);
4305                     break;
4306                   }
4307
4308                 if (BASELINK_P (fn))
4309                   postfix_expression
4310                     = (build_new_method_call
4311                        (instance, fn, args, NULL_TREE,
4312                         (idk == CP_ID_KIND_QUALIFIED
4313                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4314                         /*fn_p=*/NULL));
4315                 else
4316                   postfix_expression
4317                     = finish_call_expr (postfix_expression, args,
4318                                         /*disallow_virtual=*/false,
4319                                         /*koenig_p=*/false);
4320               }
4321             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4322                      || TREE_CODE (postfix_expression) == MEMBER_REF
4323                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4324               postfix_expression = (build_offset_ref_call_from_tree
4325                                     (postfix_expression, args));
4326             else if (idk == CP_ID_KIND_QUALIFIED)
4327               /* A call to a static class member, or a namespace-scope
4328                  function.  */
4329               postfix_expression
4330                 = finish_call_expr (postfix_expression, args,
4331                                     /*disallow_virtual=*/true,
4332                                     koenig_p);
4333             else
4334               /* All other function calls.  */
4335               postfix_expression
4336                 = finish_call_expr (postfix_expression, args,
4337                                     /*disallow_virtual=*/false,
4338                                     koenig_p);
4339
4340             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4341             idk = CP_ID_KIND_NONE;
4342           }
4343           break;
4344
4345         case CPP_DOT:
4346         case CPP_DEREF:
4347           /* postfix-expression . template [opt] id-expression
4348              postfix-expression . pseudo-destructor-name
4349              postfix-expression -> template [opt] id-expression
4350              postfix-expression -> pseudo-destructor-name */
4351
4352           /* Consume the `.' or `->' operator.  */
4353           cp_lexer_consume_token (parser->lexer);
4354
4355           postfix_expression
4356             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4357                                                       postfix_expression,
4358                                                       false, &idk);
4359           break;
4360
4361         case CPP_PLUS_PLUS:
4362           /* postfix-expression ++  */
4363           /* Consume the `++' token.  */
4364           cp_lexer_consume_token (parser->lexer);
4365           /* Generate a representation for the complete expression.  */
4366           postfix_expression
4367             = finish_increment_expr (postfix_expression,
4368                                      POSTINCREMENT_EXPR);
4369           /* Increments may not appear in constant-expressions.  */
4370           if (cp_parser_non_integral_constant_expression (parser,
4371                                                           "an increment"))
4372             postfix_expression = error_mark_node;
4373           idk = CP_ID_KIND_NONE;
4374           break;
4375
4376         case CPP_MINUS_MINUS:
4377           /* postfix-expression -- */
4378           /* Consume the `--' token.  */
4379           cp_lexer_consume_token (parser->lexer);
4380           /* Generate a representation for the complete expression.  */
4381           postfix_expression
4382             = finish_increment_expr (postfix_expression,
4383                                      POSTDECREMENT_EXPR);
4384           /* Decrements may not appear in constant-expressions.  */
4385           if (cp_parser_non_integral_constant_expression (parser,
4386                                                           "a decrement"))
4387             postfix_expression = error_mark_node;
4388           idk = CP_ID_KIND_NONE;
4389           break;
4390
4391         default:
4392           return postfix_expression;
4393         }
4394     }
4395
4396   /* We should never get here.  */
4397   gcc_unreachable ();
4398   return error_mark_node;
4399 }
4400
4401 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4402    by cp_parser_builtin_offsetof.  We're looking for
4403
4404      postfix-expression [ expression ]
4405
4406    FOR_OFFSETOF is set if we're being called in that context, which
4407    changes how we deal with integer constant expressions.  */
4408
4409 static tree
4410 cp_parser_postfix_open_square_expression (cp_parser *parser,
4411                                           tree postfix_expression,
4412                                           bool for_offsetof)
4413 {
4414   tree index;
4415
4416   /* Consume the `[' token.  */
4417   cp_lexer_consume_token (parser->lexer);
4418
4419   /* Parse the index expression.  */
4420   /* ??? For offsetof, there is a question of what to allow here.  If
4421      offsetof is not being used in an integral constant expression context,
4422      then we *could* get the right answer by computing the value at runtime.
4423      If we are in an integral constant expression context, then we might
4424      could accept any constant expression; hard to say without analysis.
4425      Rather than open the barn door too wide right away, allow only integer
4426      constant expressions here.  */
4427   if (for_offsetof)
4428     index = cp_parser_constant_expression (parser, false, NULL);
4429   else
4430     index = cp_parser_expression (parser, /*cast_p=*/false);
4431
4432   /* Look for the closing `]'.  */
4433   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4434
4435   /* Build the ARRAY_REF.  */
4436   postfix_expression = grok_array_decl (postfix_expression, index);
4437
4438   /* When not doing offsetof, array references are not permitted in
4439      constant-expressions.  */
4440   if (!for_offsetof
4441       && (cp_parser_non_integral_constant_expression
4442           (parser, "an array reference")))
4443     postfix_expression = error_mark_node;
4444
4445   return postfix_expression;
4446 }
4447
4448 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4449    by cp_parser_builtin_offsetof.  We're looking for
4450
4451      postfix-expression . template [opt] id-expression
4452      postfix-expression . pseudo-destructor-name
4453      postfix-expression -> template [opt] id-expression
4454      postfix-expression -> pseudo-destructor-name
4455
4456    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4457    limits what of the above we'll actually accept, but nevermind.
4458    TOKEN_TYPE is the "." or "->" token, which will already have been
4459    removed from the stream.  */
4460
4461 static tree
4462 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4463                                         enum cpp_ttype token_type,
4464                                         tree postfix_expression,
4465                                         bool for_offsetof, cp_id_kind *idk)
4466 {
4467   tree name;
4468   bool dependent_p;
4469   bool pseudo_destructor_p;
4470   tree scope = NULL_TREE;
4471
4472   /* If this is a `->' operator, dereference the pointer.  */
4473   if (token_type == CPP_DEREF)
4474     postfix_expression = build_x_arrow (postfix_expression);
4475   /* Check to see whether or not the expression is type-dependent.  */
4476   dependent_p = type_dependent_expression_p (postfix_expression);
4477   /* The identifier following the `->' or `.' is not qualified.  */
4478   parser->scope = NULL_TREE;
4479   parser->qualifying_scope = NULL_TREE;
4480   parser->object_scope = NULL_TREE;
4481   *idk = CP_ID_KIND_NONE;
4482   /* Enter the scope corresponding to the type of the object
4483      given by the POSTFIX_EXPRESSION.  */
4484   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4485     {
4486       scope = TREE_TYPE (postfix_expression);
4487       /* According to the standard, no expression should ever have
4488          reference type.  Unfortunately, we do not currently match
4489          the standard in this respect in that our internal representation
4490          of an expression may have reference type even when the standard
4491          says it does not.  Therefore, we have to manually obtain the
4492          underlying type here.  */
4493       scope = non_reference (scope);
4494       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4495       if (scope == unknown_type_node)
4496         {
4497           error ("%qE does not have class type", postfix_expression);
4498           scope = NULL_TREE;
4499         }
4500       else
4501         scope = complete_type_or_else (scope, NULL_TREE);
4502       /* Let the name lookup machinery know that we are processing a
4503          class member access expression.  */
4504       parser->context->object_type = scope;
4505       /* If something went wrong, we want to be able to discern that case,
4506          as opposed to the case where there was no SCOPE due to the type
4507          of expression being dependent.  */
4508       if (!scope)
4509         scope = error_mark_node;
4510       /* If the SCOPE was erroneous, make the various semantic analysis
4511          functions exit quickly -- and without issuing additional error
4512          messages.  */
4513       if (scope == error_mark_node)
4514         postfix_expression = error_mark_node;
4515     }
4516
4517   /* Assume this expression is not a pseudo-destructor access.  */
4518   pseudo_destructor_p = false;
4519
4520   /* If the SCOPE is a scalar type, then, if this is a valid program,
4521      we must be looking at a pseudo-destructor-name.  */
4522   if (scope && SCALAR_TYPE_P (scope))
4523     {
4524       tree s;
4525       tree type;
4526
4527       cp_parser_parse_tentatively (parser);
4528       /* Parse the pseudo-destructor-name.  */
4529       s = NULL_TREE;
4530       cp_parser_pseudo_destructor_name (parser, &s, &type);
4531       if (cp_parser_parse_definitely (parser))
4532         {
4533           pseudo_destructor_p = true;
4534           postfix_expression
4535             = finish_pseudo_destructor_expr (postfix_expression,
4536                                              s, TREE_TYPE (type));
4537         }
4538     }
4539
4540   if (!pseudo_destructor_p)
4541     {
4542       /* If the SCOPE is not a scalar type, we are looking at an
4543          ordinary class member access expression, rather than a
4544          pseudo-destructor-name.  */
4545       bool template_p;
4546       /* Parse the id-expression.  */
4547       name = (cp_parser_id_expression
4548               (parser,
4549                cp_parser_optional_template_keyword (parser),
4550                /*check_dependency_p=*/true,
4551                &template_p,
4552                /*declarator_p=*/false,
4553                /*optional_p=*/false));
4554       /* In general, build a SCOPE_REF if the member name is qualified.
4555          However, if the name was not dependent and has already been
4556          resolved; there is no need to build the SCOPE_REF.  For example;
4557
4558              struct X { void f(); };
4559              template <typename T> void f(T* t) { t->X::f(); }
4560
4561          Even though "t" is dependent, "X::f" is not and has been resolved
4562          to a BASELINK; there is no need to include scope information.  */
4563
4564       /* But we do need to remember that there was an explicit scope for
4565          virtual function calls.  */
4566       if (parser->scope)
4567         *idk = CP_ID_KIND_QUALIFIED;
4568
4569       /* If the name is a template-id that names a type, we will get a
4570          TYPE_DECL here.  That is invalid code.  */
4571       if (TREE_CODE (name) == TYPE_DECL)
4572         {
4573           error ("invalid use of %qD", name);
4574           postfix_expression = error_mark_node;
4575         }
4576       else
4577         {
4578           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4579             {
4580               name = build_qualified_name (/*type=*/NULL_TREE,
4581                                            parser->scope,
4582                                            name,
4583                                            template_p);
4584               parser->scope = NULL_TREE;
4585               parser->qualifying_scope = NULL_TREE;
4586               parser->object_scope = NULL_TREE;
4587             }
4588           if (scope && name && BASELINK_P (name))
4589             adjust_result_of_qualified_name_lookup
4590               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4591           postfix_expression
4592             = finish_class_member_access_expr (postfix_expression, name,
4593                                                template_p);
4594         }
4595     }
4596
4597   /* We no longer need to look up names in the scope of the object on
4598      the left-hand side of the `.' or `->' operator.  */
4599   parser->context->object_type = NULL_TREE;
4600
4601   /* Outside of offsetof, these operators may not appear in
4602      constant-expressions.  */
4603   if (!for_offsetof
4604       && (cp_parser_non_integral_constant_expression
4605           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4606     postfix_expression = error_mark_node;
4607
4608   return postfix_expression;
4609 }
4610
4611 /* Parse a parenthesized expression-list.
4612
4613    expression-list:
4614      assignment-expression
4615      expression-list, assignment-expression
4616
4617    attribute-list:
4618      expression-list
4619      identifier
4620      identifier, expression-list
4621
4622    CAST_P is true if this expression is the target of a cast.
4623
4624    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4625    representation of an assignment-expression.  Note that a TREE_LIST
4626    is returned even if there is only a single expression in the list.
4627    error_mark_node is returned if the ( and or ) are
4628    missing. NULL_TREE is returned on no expressions. The parentheses
4629    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4630    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4631    indicates whether or not all of the expressions in the list were
4632    constant.  */
4633
4634 static tree
4635 cp_parser_parenthesized_expression_list (cp_parser* parser,
4636                                          bool is_attribute_list,
4637                                          bool cast_p,
4638                                          bool *non_constant_p)
4639 {
4640   tree expression_list = NULL_TREE;
4641   bool fold_expr_p = is_attribute_list;
4642   tree identifier = NULL_TREE;
4643
4644   /* Assume all the expressions will be constant.  */
4645   if (non_constant_p)
4646     *non_constant_p = false;
4647
4648   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4649     return error_mark_node;
4650
4651   /* Consume expressions until there are no more.  */
4652   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4653     while (true)
4654       {
4655         tree expr;
4656
4657         /* At the beginning of attribute lists, check to see if the
4658            next token is an identifier.  */
4659         if (is_attribute_list
4660             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4661           {
4662             cp_token *token;
4663
4664             /* Consume the identifier.  */
4665             token = cp_lexer_consume_token (parser->lexer);
4666             /* Save the identifier.  */
4667             identifier = token->value;
4668           }
4669         else
4670           {
4671             /* Parse the next assignment-expression.  */
4672             if (non_constant_p)
4673               {
4674                 bool expr_non_constant_p;
4675                 expr = (cp_parser_constant_expression
4676                         (parser, /*allow_non_constant_p=*/true,
4677                          &expr_non_constant_p));
4678                 if (expr_non_constant_p)
4679                   *non_constant_p = true;
4680               }
4681             else
4682               expr = cp_parser_assignment_expression (parser, cast_p);
4683
4684             if (fold_expr_p)
4685               expr = fold_non_dependent_expr (expr);
4686
4687              /* Add it to the list.  We add error_mark_node
4688                 expressions to the list, so that we can still tell if
4689                 the correct form for a parenthesized expression-list
4690                 is found. That gives better errors.  */
4691             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4692
4693             if (expr == error_mark_node)
4694               goto skip_comma;
4695           }
4696
4697         /* After the first item, attribute lists look the same as
4698            expression lists.  */
4699         is_attribute_list = false;
4700
4701       get_comma:;
4702         /* If the next token isn't a `,', then we are done.  */
4703         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4704           break;
4705
4706         /* Otherwise, consume the `,' and keep going.  */
4707         cp_lexer_consume_token (parser->lexer);
4708       }
4709
4710   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4711     {
4712       int ending;
4713
4714     skip_comma:;
4715       /* We try and resync to an unnested comma, as that will give the
4716          user better diagnostics.  */
4717       ending = cp_parser_skip_to_closing_parenthesis (parser,
4718                                                       /*recovering=*/true,
4719                                                       /*or_comma=*/true,
4720                                                       /*consume_paren=*/true);
4721       if (ending < 0)
4722         goto get_comma;
4723       if (!ending)
4724         return error_mark_node;
4725     }
4726
4727   /* We built up the list in reverse order so we must reverse it now.  */
4728   expression_list = nreverse (expression_list);
4729   if (identifier)
4730     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4731
4732   return expression_list;
4733 }
4734
4735 /* Parse a pseudo-destructor-name.
4736
4737    pseudo-destructor-name:
4738      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4739      :: [opt] nested-name-specifier template template-id :: ~ type-name
4740      :: [opt] nested-name-specifier [opt] ~ type-name
4741
4742    If either of the first two productions is used, sets *SCOPE to the
4743    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4744    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4745    or ERROR_MARK_NODE if the parse fails.  */
4746
4747 static void
4748 cp_parser_pseudo_destructor_name (cp_parser* parser,
4749                                   tree* scope,
4750                                   tree* type)
4751 {
4752   bool nested_name_specifier_p;
4753
4754   /* Assume that things will not work out.  */
4755   *type = error_mark_node;
4756
4757   /* Look for the optional `::' operator.  */
4758   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4759   /* Look for the optional nested-name-specifier.  */
4760   nested_name_specifier_p
4761     = (cp_parser_nested_name_specifier_opt (parser,
4762                                             /*typename_keyword_p=*/false,
4763                                             /*check_dependency_p=*/true,
4764                                             /*type_p=*/false,
4765                                             /*is_declaration=*/true)
4766        != NULL_TREE);
4767   /* Now, if we saw a nested-name-specifier, we might be doing the
4768      second production.  */
4769   if (nested_name_specifier_p
4770       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4771     {
4772       /* Consume the `template' keyword.  */
4773       cp_lexer_consume_token (parser->lexer);
4774       /* Parse the template-id.  */
4775       cp_parser_template_id (parser,
4776                              /*template_keyword_p=*/true,
4777                              /*check_dependency_p=*/false,
4778                              /*is_declaration=*/true);
4779       /* Look for the `::' token.  */
4780       cp_parser_require (parser, CPP_SCOPE, "`::'");
4781     }
4782   /* If the next token is not a `~', then there might be some
4783      additional qualification.  */
4784   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4785     {
4786       /* Look for the type-name.  */
4787       *scope = TREE_TYPE (cp_parser_type_name (parser));
4788
4789       if (*scope == error_mark_node)
4790         return;
4791
4792       /* If we don't have ::~, then something has gone wrong.  Since
4793          the only caller of this function is looking for something
4794          after `.' or `->' after a scalar type, most likely the
4795          program is trying to get a member of a non-aggregate
4796          type.  */
4797       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4798           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4799         {
4800           cp_parser_error (parser, "request for member of non-aggregate type");
4801           return;
4802         }
4803
4804       /* Look for the `::' token.  */
4805       cp_parser_require (parser, CPP_SCOPE, "`::'");
4806     }
4807   else
4808     *scope = NULL_TREE;
4809
4810   /* Look for the `~'.  */
4811   cp_parser_require (parser, CPP_COMPL, "`~'");
4812   /* Look for the type-name again.  We are not responsible for
4813      checking that it matches the first type-name.  */
4814   *type = cp_parser_type_name (parser);
4815 }
4816
4817 /* Parse a unary-expression.
4818
4819    unary-expression:
4820      postfix-expression
4821      ++ cast-expression
4822      -- cast-expression
4823      unary-operator cast-expression
4824      sizeof unary-expression
4825      sizeof ( type-id )
4826      new-expression
4827      delete-expression
4828
4829    GNU Extensions:
4830
4831    unary-expression:
4832      __extension__ cast-expression
4833      __alignof__ unary-expression
4834      __alignof__ ( type-id )
4835      __real__ cast-expression
4836      __imag__ cast-expression
4837      && identifier
4838
4839    ADDRESS_P is true iff the unary-expression is appearing as the
4840    operand of the `&' operator.   CAST_P is true if this expression is
4841    the target of a cast.
4842
4843    Returns a representation of the expression.  */
4844
4845 static tree
4846 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4847 {
4848   cp_token *token;
4849   enum tree_code unary_operator;
4850
4851   /* Peek at the next token.  */
4852   token = cp_lexer_peek_token (parser->lexer);
4853   /* Some keywords give away the kind of expression.  */
4854   if (token->type == CPP_KEYWORD)
4855     {
4856       enum rid keyword = token->keyword;
4857
4858       switch (keyword)
4859         {
4860         case RID_ALIGNOF:
4861         case RID_SIZEOF:
4862           {
4863             tree operand;
4864             enum tree_code op;
4865
4866             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4867             /* Consume the token.  */
4868             cp_lexer_consume_token (parser->lexer);
4869             /* Parse the operand.  */
4870             operand = cp_parser_sizeof_operand (parser, keyword);
4871
4872             if (TYPE_P (operand))
4873               return cxx_sizeof_or_alignof_type (operand, op, true);
4874             else
4875               return cxx_sizeof_or_alignof_expr (operand, op);
4876           }
4877
4878         case RID_NEW:
4879           return cp_parser_new_expression (parser);
4880
4881         case RID_DELETE:
4882           return cp_parser_delete_expression (parser);
4883
4884         case RID_EXTENSION:
4885           {
4886             /* The saved value of the PEDANTIC flag.  */
4887             int saved_pedantic;
4888             tree expr;
4889
4890             /* Save away the PEDANTIC flag.  */
4891             cp_parser_extension_opt (parser, &saved_pedantic);
4892             /* Parse the cast-expression.  */
4893             expr = cp_parser_simple_cast_expression (parser);
4894             /* Restore the PEDANTIC flag.  */
4895             pedantic = saved_pedantic;
4896
4897             return expr;
4898           }
4899
4900         case RID_REALPART:
4901         case RID_IMAGPART:
4902           {
4903             tree expression;
4904
4905             /* Consume the `__real__' or `__imag__' token.  */
4906             cp_lexer_consume_token (parser->lexer);
4907             /* Parse the cast-expression.  */
4908             expression = cp_parser_simple_cast_expression (parser);
4909             /* Create the complete representation.  */
4910             return build_x_unary_op ((keyword == RID_REALPART
4911                                       ? REALPART_EXPR : IMAGPART_EXPR),
4912                                      expression);
4913           }
4914           break;
4915
4916         default:
4917           break;
4918         }
4919     }
4920
4921   /* Look for the `:: new' and `:: delete', which also signal the
4922      beginning of a new-expression, or delete-expression,
4923      respectively.  If the next token is `::', then it might be one of
4924      these.  */
4925   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4926     {
4927       enum rid keyword;
4928
4929       /* See if the token after the `::' is one of the keywords in
4930          which we're interested.  */
4931       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4932       /* If it's `new', we have a new-expression.  */
4933       if (keyword == RID_NEW)
4934         return cp_parser_new_expression (parser);
4935       /* Similarly, for `delete'.  */
4936       else if (keyword == RID_DELETE)
4937         return cp_parser_delete_expression (parser);
4938     }
4939
4940   /* Look for a unary operator.  */
4941   unary_operator = cp_parser_unary_operator (token);
4942   /* The `++' and `--' operators can be handled similarly, even though
4943      they are not technically unary-operators in the grammar.  */
4944   if (unary_operator == ERROR_MARK)
4945     {
4946       if (token->type == CPP_PLUS_PLUS)
4947         unary_operator = PREINCREMENT_EXPR;
4948       else if (token->type == CPP_MINUS_MINUS)
4949         unary_operator = PREDECREMENT_EXPR;
4950       /* Handle the GNU address-of-label extension.  */
4951       else if (cp_parser_allow_gnu_extensions_p (parser)
4952                && token->type == CPP_AND_AND)
4953         {
4954           tree identifier;
4955
4956           /* Consume the '&&' token.  */
4957           cp_lexer_consume_token (parser->lexer);
4958           /* Look for the identifier.  */
4959           identifier = cp_parser_identifier (parser);
4960           /* Create an expression representing the address.  */
4961           return finish_label_address_expr (identifier);
4962         }
4963     }
4964   if (unary_operator != ERROR_MARK)
4965     {
4966       tree cast_expression;
4967       tree expression = error_mark_node;
4968       const char *non_constant_p = NULL;
4969
4970       /* Consume the operator token.  */
4971       token = cp_lexer_consume_token (parser->lexer);
4972       /* Parse the cast-expression.  */
4973       cast_expression
4974         = cp_parser_cast_expression (parser,
4975                                      unary_operator == ADDR_EXPR,
4976                                      /*cast_p=*/false);
4977       /* Now, build an appropriate representation.  */
4978       switch (unary_operator)
4979         {
4980         case INDIRECT_REF:
4981           non_constant_p = "`*'";
4982           expression = build_x_indirect_ref (cast_expression, "unary *");
4983           break;
4984
4985         case ADDR_EXPR:
4986           non_constant_p = "`&'";
4987           /* Fall through.  */
4988         case BIT_NOT_EXPR:
4989           expression = build_x_unary_op (unary_operator, cast_expression);
4990           break;
4991
4992         case PREINCREMENT_EXPR:
4993         case PREDECREMENT_EXPR:
4994           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4995                             ? "`++'" : "`--'");
4996           /* Fall through.  */
4997         case UNARY_PLUS_EXPR:
4998         case NEGATE_EXPR:
4999         case TRUTH_NOT_EXPR:
5000           expression = finish_unary_op_expr (unary_operator, cast_expression);
5001           break;
5002
5003         default:
5004           gcc_unreachable ();
5005         }
5006
5007       if (non_constant_p
5008           && cp_parser_non_integral_constant_expression (parser,
5009                                                          non_constant_p))
5010         expression = error_mark_node;
5011
5012       return expression;
5013     }
5014
5015   return cp_parser_postfix_expression (parser, address_p, cast_p);
5016 }
5017
5018 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5019    unary-operator, the corresponding tree code is returned.  */
5020
5021 static enum tree_code
5022 cp_parser_unary_operator (cp_token* token)
5023 {
5024   switch (token->type)
5025     {
5026     case CPP_MULT:
5027       return INDIRECT_REF;
5028
5029     case CPP_AND:
5030       return ADDR_EXPR;
5031
5032     case CPP_PLUS:
5033       return UNARY_PLUS_EXPR;
5034
5035     case CPP_MINUS:
5036       return NEGATE_EXPR;
5037
5038     case CPP_NOT:
5039       return TRUTH_NOT_EXPR;
5040
5041     case CPP_COMPL:
5042       return BIT_NOT_EXPR;
5043
5044     default:
5045       return ERROR_MARK;
5046     }
5047 }
5048
5049 /* Parse a new-expression.
5050
5051    new-expression:
5052      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5053      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5054
5055    Returns a representation of the expression.  */
5056
5057 static tree
5058 cp_parser_new_expression (cp_parser* parser)
5059 {
5060   bool global_scope_p;
5061   tree placement;
5062   tree type;
5063   tree initializer;
5064   tree nelts;
5065
5066   /* Look for the optional `::' operator.  */
5067   global_scope_p
5068     = (cp_parser_global_scope_opt (parser,
5069                                    /*current_scope_valid_p=*/false)
5070        != NULL_TREE);
5071   /* Look for the `new' operator.  */
5072   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5073   /* There's no easy way to tell a new-placement from the
5074      `( type-id )' construct.  */
5075   cp_parser_parse_tentatively (parser);
5076   /* Look for a new-placement.  */
5077   placement = cp_parser_new_placement (parser);
5078   /* If that didn't work out, there's no new-placement.  */
5079   if (!cp_parser_parse_definitely (parser))
5080     placement = NULL_TREE;
5081
5082   /* If the next token is a `(', then we have a parenthesized
5083      type-id.  */
5084   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5085     {
5086       /* Consume the `('.  */
5087       cp_lexer_consume_token (parser->lexer);
5088       /* Parse the type-id.  */
5089       type = cp_parser_type_id (parser);
5090       /* Look for the closing `)'.  */
5091       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5092       /* There should not be a direct-new-declarator in this production,
5093          but GCC used to allowed this, so we check and emit a sensible error
5094          message for this case.  */
5095       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5096         {
5097           error ("array bound forbidden after parenthesized type-id");
5098           inform ("try removing the parentheses around the type-id");
5099           cp_parser_direct_new_declarator (parser);
5100         }
5101       nelts = NULL_TREE;
5102     }
5103   /* Otherwise, there must be a new-type-id.  */
5104   else
5105     type = cp_parser_new_type_id (parser, &nelts);
5106
5107   /* If the next token is a `(', then we have a new-initializer.  */
5108   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5109     initializer = cp_parser_new_initializer (parser);
5110   else
5111     initializer = NULL_TREE;
5112
5113   /* A new-expression may not appear in an integral constant
5114      expression.  */
5115   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5116     return error_mark_node;
5117
5118   /* Create a representation of the new-expression.  */
5119   return build_new (placement, type, nelts, initializer, global_scope_p);
5120 }
5121
5122 /* Parse a new-placement.
5123
5124    new-placement:
5125      ( expression-list )
5126
5127    Returns the same representation as for an expression-list.  */
5128
5129 static tree
5130 cp_parser_new_placement (cp_parser* parser)
5131 {
5132   tree expression_list;
5133
5134   /* Parse the expression-list.  */
5135   expression_list = (cp_parser_parenthesized_expression_list
5136                      (parser, false, /*cast_p=*/false,
5137                       /*non_constant_p=*/NULL));
5138
5139   return expression_list;
5140 }
5141
5142 /* Parse a new-type-id.
5143
5144    new-type-id:
5145      type-specifier-seq new-declarator [opt]
5146
5147    Returns the TYPE allocated.  If the new-type-id indicates an array
5148    type, *NELTS is set to the number of elements in the last array
5149    bound; the TYPE will not include the last array bound.  */
5150
5151 static tree
5152 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5153 {
5154   cp_decl_specifier_seq type_specifier_seq;
5155   cp_declarator *new_declarator;
5156   cp_declarator *declarator;
5157   cp_declarator *outer_declarator;
5158   const char *saved_message;
5159   tree type;
5160
5161   /* The type-specifier sequence must not contain type definitions.
5162      (It cannot contain declarations of new types either, but if they
5163      are not definitions we will catch that because they are not
5164      complete.)  */
5165   saved_message = parser->type_definition_forbidden_message;
5166   parser->type_definition_forbidden_message
5167     = "types may not be defined in a new-type-id";
5168   /* Parse the type-specifier-seq.  */
5169   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5170                                 &type_specifier_seq);
5171   /* Restore the old message.  */
5172   parser->type_definition_forbidden_message = saved_message;
5173   /* Parse the new-declarator.  */
5174   new_declarator = cp_parser_new_declarator_opt (parser);
5175
5176   /* Determine the number of elements in the last array dimension, if
5177      any.  */
5178   *nelts = NULL_TREE;
5179   /* Skip down to the last array dimension.  */
5180   declarator = new_declarator;
5181   outer_declarator = NULL;
5182   while (declarator && (declarator->kind == cdk_pointer
5183                         || declarator->kind == cdk_ptrmem))
5184     {
5185       outer_declarator = declarator;
5186       declarator = declarator->declarator;
5187     }
5188   while (declarator
5189          && declarator->kind == cdk_array
5190          && declarator->declarator
5191          && declarator->declarator->kind == cdk_array)
5192     {
5193       outer_declarator = declarator;
5194       declarator = declarator->declarator;
5195     }
5196
5197   if (declarator && declarator->kind == cdk_array)
5198     {
5199       *nelts = declarator->u.array.bounds;
5200       if (*nelts == error_mark_node)
5201         *nelts = integer_one_node;
5202
5203       if (outer_declarator)
5204         outer_declarator->declarator = declarator->declarator;
5205       else
5206         new_declarator = NULL;
5207     }
5208
5209   type = groktypename (&type_specifier_seq, new_declarator);
5210   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5211     {
5212       *nelts = array_type_nelts_top (type);
5213       type = TREE_TYPE (type);
5214     }
5215   return type;
5216 }
5217
5218 /* Parse an (optional) new-declarator.
5219
5220    new-declarator:
5221      ptr-operator new-declarator [opt]
5222      direct-new-declarator
5223
5224    Returns the declarator.  */
5225
5226 static cp_declarator *
5227 cp_parser_new_declarator_opt (cp_parser* parser)
5228 {
5229   enum tree_code code;
5230   tree type;
5231   cp_cv_quals cv_quals;
5232
5233   /* We don't know if there's a ptr-operator next, or not.  */
5234   cp_parser_parse_tentatively (parser);
5235   /* Look for a ptr-operator.  */
5236   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5237   /* If that worked, look for more new-declarators.  */
5238   if (cp_parser_parse_definitely (parser))
5239     {
5240       cp_declarator *declarator;
5241
5242       /* Parse another optional declarator.  */
5243       declarator = cp_parser_new_declarator_opt (parser);
5244
5245       /* Create the representation of the declarator.  */
5246       if (type)
5247         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5248       else if (code == INDIRECT_REF)
5249         declarator = make_pointer_declarator (cv_quals, declarator);
5250       else
5251         declarator = make_reference_declarator (cv_quals, declarator);
5252
5253       return declarator;
5254     }
5255
5256   /* If the next token is a `[', there is a direct-new-declarator.  */
5257   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5258     return cp_parser_direct_new_declarator (parser);
5259
5260   return NULL;
5261 }
5262
5263 /* Parse a direct-new-declarator.
5264
5265    direct-new-declarator:
5266      [ expression ]
5267      direct-new-declarator [constant-expression]
5268
5269    */
5270
5271 static cp_declarator *
5272 cp_parser_direct_new_declarator (cp_parser* parser)
5273 {
5274   cp_declarator *declarator = NULL;
5275
5276   while (true)
5277     {
5278       tree expression;
5279
5280       /* Look for the opening `['.  */
5281       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5282       /* The first expression is not required to be constant.  */
5283       if (!declarator)
5284         {
5285           expression = cp_parser_expression (parser, /*cast_p=*/false);
5286           /* The standard requires that the expression have integral
5287              type.  DR 74 adds enumeration types.  We believe that the
5288              real intent is that these expressions be handled like the
5289              expression in a `switch' condition, which also allows
5290              classes with a single conversion to integral or
5291              enumeration type.  */
5292           if (!processing_template_decl)
5293             {
5294               expression
5295                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5296                                               expression,
5297                                               /*complain=*/true);
5298               if (!expression)
5299                 {
5300                   error ("expression in new-declarator must have integral "
5301                          "or enumeration type");
5302                   expression = error_mark_node;
5303                 }
5304             }
5305         }
5306       /* But all the other expressions must be.  */
5307       else
5308         expression
5309           = cp_parser_constant_expression (parser,
5310                                            /*allow_non_constant=*/false,
5311                                            NULL);
5312       /* Look for the closing `]'.  */
5313       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5314
5315       /* Add this bound to the declarator.  */
5316       declarator = make_array_declarator (declarator, expression);
5317
5318       /* If the next token is not a `[', then there are no more
5319          bounds.  */
5320       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5321         break;
5322     }
5323
5324   return declarator;
5325 }
5326
5327 /* Parse a new-initializer.
5328
5329    new-initializer:
5330      ( expression-list [opt] )
5331
5332    Returns a representation of the expression-list.  If there is no
5333    expression-list, VOID_ZERO_NODE is returned.  */
5334
5335 static tree
5336 cp_parser_new_initializer (cp_parser* parser)
5337 {
5338   tree expression_list;
5339
5340   expression_list = (cp_parser_parenthesized_expression_list
5341                      (parser, false, /*cast_p=*/false,
5342                       /*non_constant_p=*/NULL));
5343   if (!expression_list)
5344     expression_list = void_zero_node;
5345
5346   return expression_list;
5347 }
5348
5349 /* Parse a delete-expression.
5350
5351    delete-expression:
5352      :: [opt] delete cast-expression
5353      :: [opt] delete [ ] cast-expression
5354
5355    Returns a representation of the expression.  */
5356
5357 static tree
5358 cp_parser_delete_expression (cp_parser* parser)
5359 {
5360   bool global_scope_p;
5361   bool array_p;
5362   tree expression;
5363
5364   /* Look for the optional `::' operator.  */
5365   global_scope_p
5366     = (cp_parser_global_scope_opt (parser,
5367                                    /*current_scope_valid_p=*/false)
5368        != NULL_TREE);
5369   /* Look for the `delete' keyword.  */
5370   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5371   /* See if the array syntax is in use.  */
5372   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5373     {
5374       /* Consume the `[' token.  */
5375       cp_lexer_consume_token (parser->lexer);
5376       /* Look for the `]' token.  */
5377       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5378       /* Remember that this is the `[]' construct.  */
5379       array_p = true;
5380     }
5381   else
5382     array_p = false;
5383
5384   /* Parse the cast-expression.  */
5385   expression = cp_parser_simple_cast_expression (parser);
5386
5387   /* A delete-expression may not appear in an integral constant
5388      expression.  */
5389   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5390     return error_mark_node;
5391
5392   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5393 }
5394
5395 /* Parse a cast-expression.
5396
5397    cast-expression:
5398      unary-expression
5399      ( type-id ) cast-expression
5400
5401    ADDRESS_P is true iff the unary-expression is appearing as the
5402    operand of the `&' operator.   CAST_P is true if this expression is
5403    the target of a cast.
5404
5405    Returns a representation of the expression.  */
5406
5407 static tree
5408 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5409 {
5410   /* If it's a `(', then we might be looking at a cast.  */
5411   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5412     {
5413       tree type = NULL_TREE;
5414       tree expr = NULL_TREE;
5415       bool compound_literal_p;
5416       const char *saved_message;
5417
5418       /* There's no way to know yet whether or not this is a cast.
5419          For example, `(int (3))' is a unary-expression, while `(int)
5420          3' is a cast.  So, we resort to parsing tentatively.  */
5421       cp_parser_parse_tentatively (parser);
5422       /* Types may not be defined in a cast.  */
5423       saved_message = parser->type_definition_forbidden_message;
5424       parser->type_definition_forbidden_message
5425         = "types may not be defined in casts";
5426       /* Consume the `('.  */
5427       cp_lexer_consume_token (parser->lexer);
5428       /* A very tricky bit is that `(struct S) { 3 }' is a
5429          compound-literal (which we permit in C++ as an extension).
5430          But, that construct is not a cast-expression -- it is a
5431          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5432          is legal; if the compound-literal were a cast-expression,
5433          you'd need an extra set of parentheses.)  But, if we parse
5434          the type-id, and it happens to be a class-specifier, then we
5435          will commit to the parse at that point, because we cannot
5436          undo the action that is done when creating a new class.  So,
5437          then we cannot back up and do a postfix-expression.
5438
5439          Therefore, we scan ahead to the closing `)', and check to see
5440          if the token after the `)' is a `{'.  If so, we are not
5441          looking at a cast-expression.
5442
5443          Save tokens so that we can put them back.  */
5444       cp_lexer_save_tokens (parser->lexer);
5445       /* Skip tokens until the next token is a closing parenthesis.
5446          If we find the closing `)', and the next token is a `{', then
5447          we are looking at a compound-literal.  */
5448       compound_literal_p
5449         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5450                                                   /*consume_paren=*/true)
5451            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5452       /* Roll back the tokens we skipped.  */
5453       cp_lexer_rollback_tokens (parser->lexer);
5454       /* If we were looking at a compound-literal, simulate an error
5455          so that the call to cp_parser_parse_definitely below will
5456          fail.  */
5457       if (compound_literal_p)
5458         cp_parser_simulate_error (parser);
5459       else
5460         {
5461           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5462           parser->in_type_id_in_expr_p = true;
5463           /* Look for the type-id.  */
5464           type = cp_parser_type_id (parser);
5465           /* Look for the closing `)'.  */
5466           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5467           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5468         }
5469
5470       /* Restore the saved message.  */
5471       parser->type_definition_forbidden_message = saved_message;
5472
5473       /* If ok so far, parse the dependent expression. We cannot be
5474          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5475          ctor of T, but looks like a cast to function returning T
5476          without a dependent expression.  */
5477       if (!cp_parser_error_occurred (parser))
5478         expr = cp_parser_cast_expression (parser,
5479                                           /*address_p=*/false,
5480                                           /*cast_p=*/true);
5481
5482       if (cp_parser_parse_definitely (parser))
5483         {
5484           /* Warn about old-style casts, if so requested.  */
5485           if (warn_old_style_cast
5486               && !in_system_header
5487               && !VOID_TYPE_P (type)
5488               && current_lang_name != lang_name_c)
5489             warning (OPT_Wold_style_cast, "use of old-style cast");
5490
5491           /* Only type conversions to integral or enumeration types
5492              can be used in constant-expressions.  */
5493           if (parser->integral_constant_expression_p
5494               && !dependent_type_p (type)
5495               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5496               && (cp_parser_non_integral_constant_expression
5497                   (parser,
5498                    "a cast to a type other than an integral or "
5499                    "enumeration type")))
5500             return error_mark_node;
5501
5502           /* Perform the cast.  */
5503           expr = build_c_cast (type, expr);
5504           return expr;
5505         }
5506     }
5507
5508   /* If we get here, then it's not a cast, so it must be a
5509      unary-expression.  */
5510   return cp_parser_unary_expression (parser, address_p, cast_p);
5511 }
5512
5513 /* Parse a binary expression of the general form:
5514
5515    pm-expression:
5516      cast-expression
5517      pm-expression .* cast-expression
5518      pm-expression ->* cast-expression
5519
5520    multiplicative-expression:
5521      pm-expression
5522      multiplicative-expression * pm-expression
5523      multiplicative-expression / pm-expression
5524      multiplicative-expression % pm-expression
5525
5526    additive-expression:
5527      multiplicative-expression
5528      additive-expression + multiplicative-expression
5529      additive-expression - multiplicative-expression
5530
5531    shift-expression:
5532      additive-expression
5533      shift-expression << additive-expression
5534      shift-expression >> additive-expression
5535
5536    relational-expression:
5537      shift-expression
5538      relational-expression < shift-expression
5539      relational-expression > shift-expression
5540      relational-expression <= shift-expression
5541      relational-expression >= shift-expression
5542
5543   GNU Extension:
5544
5545    relational-expression:
5546      relational-expression <? shift-expression
5547      relational-expression >? shift-expression
5548
5549    equality-expression:
5550      relational-expression
5551      equality-expression == relational-expression
5552      equality-expression != relational-expression
5553
5554    and-expression:
5555      equality-expression
5556      and-expression & equality-expression
5557
5558    exclusive-or-expression:
5559      and-expression
5560      exclusive-or-expression ^ and-expression
5561
5562    inclusive-or-expression:
5563      exclusive-or-expression
5564      inclusive-or-expression | exclusive-or-expression
5565
5566    logical-and-expression:
5567      inclusive-or-expression
5568      logical-and-expression && inclusive-or-expression
5569
5570    logical-or-expression:
5571      logical-and-expression
5572      logical-or-expression || logical-and-expression
5573
5574    All these are implemented with a single function like:
5575
5576    binary-expression:
5577      simple-cast-expression
5578      binary-expression <token> binary-expression
5579
5580    CAST_P is true if this expression is the target of a cast.
5581
5582    The binops_by_token map is used to get the tree codes for each <token> type.
5583    binary-expressions are associated according to a precedence table.  */
5584
5585 #define TOKEN_PRECEDENCE(token) \
5586   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5587    ? PREC_NOT_OPERATOR \
5588    : binops_by_token[token->type].prec)
5589
5590 static tree
5591 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5592 {
5593   cp_parser_expression_stack stack;
5594   cp_parser_expression_stack_entry *sp = &stack[0];
5595   tree lhs, rhs;
5596   cp_token *token;
5597   enum tree_code tree_type;
5598   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5599   bool overloaded_p;
5600
5601   /* Parse the first expression.  */
5602   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5603
5604   for (;;)
5605     {
5606       /* Get an operator token.  */
5607       token = cp_lexer_peek_token (parser->lexer);
5608
5609       new_prec = TOKEN_PRECEDENCE (token);
5610
5611       /* Popping an entry off the stack means we completed a subexpression:
5612          - either we found a token which is not an operator (`>' where it is not
5613            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5614            will happen repeatedly;
5615          - or, we found an operator which has lower priority.  This is the case
5616            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5617            parsing `3 * 4'.  */
5618       if (new_prec <= prec)
5619         {
5620           if (sp == stack)
5621             break;
5622           else
5623             goto pop;
5624         }
5625
5626      get_rhs:
5627       tree_type = binops_by_token[token->type].tree_type;
5628
5629       /* We used the operator token.  */
5630       cp_lexer_consume_token (parser->lexer);
5631
5632       /* Extract another operand.  It may be the RHS of this expression
5633          or the LHS of a new, higher priority expression.  */
5634       rhs = cp_parser_simple_cast_expression (parser);
5635
5636       /* Get another operator token.  Look up its precedence to avoid
5637          building a useless (immediately popped) stack entry for common
5638          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5639       token = cp_lexer_peek_token (parser->lexer);
5640       lookahead_prec = TOKEN_PRECEDENCE (token);
5641       if (lookahead_prec > new_prec)
5642         {
5643           /* ... and prepare to parse the RHS of the new, higher priority
5644              expression.  Since precedence levels on the stack are
5645              monotonically increasing, we do not have to care about
5646              stack overflows.  */
5647           sp->prec = prec;
5648           sp->tree_type = tree_type;
5649           sp->lhs = lhs;
5650           sp++;
5651           lhs = rhs;
5652           prec = new_prec;
5653           new_prec = lookahead_prec;
5654           goto get_rhs;
5655
5656          pop:
5657           /* If the stack is not empty, we have parsed into LHS the right side
5658              (`4' in the example above) of an expression we had suspended.
5659              We can use the information on the stack to recover the LHS (`3')
5660              from the stack together with the tree code (`MULT_EXPR'), and
5661              the precedence of the higher level subexpression
5662              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5663              which will be used to actually build the additive expression.  */
5664           --sp;
5665           prec = sp->prec;
5666           tree_type = sp->tree_type;
5667           rhs = lhs;
5668           lhs = sp->lhs;
5669         }
5670
5671       overloaded_p = false;
5672       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5673
5674       /* If the binary operator required the use of an overloaded operator,
5675          then this expression cannot be an integral constant-expression.
5676          An overloaded operator can be used even if both operands are
5677          otherwise permissible in an integral constant-expression if at
5678          least one of the operands is of enumeration type.  */
5679
5680       if (overloaded_p
5681           && (cp_parser_non_integral_constant_expression
5682               (parser, "calls to overloaded operators")))
5683         return error_mark_node;
5684     }
5685
5686   return lhs;
5687 }
5688
5689
5690 /* Parse the `? expression : assignment-expression' part of a
5691    conditional-expression.  The LOGICAL_OR_EXPR is the
5692    logical-or-expression that started the conditional-expression.
5693    Returns a representation of the entire conditional-expression.
5694
5695    This routine is used by cp_parser_assignment_expression.
5696
5697      ? expression : assignment-expression
5698
5699    GNU Extensions:
5700
5701      ? : assignment-expression */
5702
5703 static tree
5704 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5705 {
5706   tree expr;
5707   tree assignment_expr;
5708
5709   /* Consume the `?' token.  */
5710   cp_lexer_consume_token (parser->lexer);
5711   if (cp_parser_allow_gnu_extensions_p (parser)
5712       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5713     /* Implicit true clause.  */
5714     expr = NULL_TREE;
5715   else
5716     /* Parse the expression.  */
5717     expr = cp_parser_expression (parser, /*cast_p=*/false);
5718
5719   /* The next token should be a `:'.  */
5720   cp_parser_require (parser, CPP_COLON, "`:'");
5721   /* Parse the assignment-expression.  */
5722   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5723
5724   /* Build the conditional-expression.  */
5725   return build_x_conditional_expr (logical_or_expr,
5726                                    expr,
5727                                    assignment_expr);
5728 }
5729
5730 /* Parse an assignment-expression.
5731
5732    assignment-expression:
5733      conditional-expression
5734      logical-or-expression assignment-operator assignment_expression
5735      throw-expression
5736
5737    CAST_P is true if this expression is the target of a cast.
5738
5739    Returns a representation for the expression.  */
5740
5741 static tree
5742 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5743 {
5744   tree expr;
5745
5746   /* If the next token is the `throw' keyword, then we're looking at
5747      a throw-expression.  */
5748   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5749     expr = cp_parser_throw_expression (parser);
5750   /* Otherwise, it must be that we are looking at a
5751      logical-or-expression.  */
5752   else
5753     {
5754       /* Parse the binary expressions (logical-or-expression).  */
5755       expr = cp_parser_binary_expression (parser, cast_p);
5756       /* If the next token is a `?' then we're actually looking at a
5757          conditional-expression.  */
5758       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5759         return cp_parser_question_colon_clause (parser, expr);
5760       else
5761         {
5762           enum tree_code assignment_operator;
5763
5764           /* If it's an assignment-operator, we're using the second
5765              production.  */
5766           assignment_operator
5767             = cp_parser_assignment_operator_opt (parser);
5768           if (assignment_operator != ERROR_MARK)
5769             {
5770               tree rhs;
5771
5772               /* Parse the right-hand side of the assignment.  */
5773               rhs = cp_parser_assignment_expression (parser, cast_p);
5774               /* An assignment may not appear in a
5775                  constant-expression.  */
5776               if (cp_parser_non_integral_constant_expression (parser,
5777                                                               "an assignment"))
5778                 return error_mark_node;
5779               /* Build the assignment expression.  */
5780               expr = build_x_modify_expr (expr,
5781                                           assignment_operator,
5782                                           rhs);
5783             }
5784         }
5785     }
5786
5787   return expr;
5788 }
5789
5790 /* Parse an (optional) assignment-operator.
5791
5792    assignment-operator: one of
5793      = *= /= %= += -= >>= <<= &= ^= |=
5794
5795    GNU Extension:
5796
5797    assignment-operator: one of
5798      <?= >?=
5799
5800    If the next token is an assignment operator, the corresponding tree
5801    code is returned, and the token is consumed.  For example, for
5802    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5803    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5804    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5805    operator, ERROR_MARK is returned.  */
5806
5807 static enum tree_code
5808 cp_parser_assignment_operator_opt (cp_parser* parser)
5809 {
5810   enum tree_code op;
5811   cp_token *token;
5812
5813   /* Peek at the next toen.  */
5814   token = cp_lexer_peek_token (parser->lexer);
5815
5816   switch (token->type)
5817     {
5818     case CPP_EQ:
5819       op = NOP_EXPR;
5820       break;
5821
5822     case CPP_MULT_EQ:
5823       op = MULT_EXPR;
5824       break;
5825
5826     case CPP_DIV_EQ:
5827       op = TRUNC_DIV_EXPR;
5828       break;
5829
5830     case CPP_MOD_EQ:
5831       op = TRUNC_MOD_EXPR;
5832       break;
5833
5834     case CPP_PLUS_EQ:
5835       op = PLUS_EXPR;
5836       break;
5837
5838     case CPP_MINUS_EQ:
5839       op = MINUS_EXPR;
5840       break;
5841
5842     case CPP_RSHIFT_EQ:
5843       op = RSHIFT_EXPR;
5844       break;
5845
5846     case CPP_LSHIFT_EQ:
5847       op = LSHIFT_EXPR;
5848       break;
5849
5850     case CPP_AND_EQ:
5851       op = BIT_AND_EXPR;
5852       break;
5853
5854     case CPP_XOR_EQ:
5855       op = BIT_XOR_EXPR;
5856       break;
5857
5858     case CPP_OR_EQ:
5859       op = BIT_IOR_EXPR;
5860       break;
5861
5862     default:
5863       /* Nothing else is an assignment operator.  */
5864       op = ERROR_MARK;
5865     }
5866
5867   /* If it was an assignment operator, consume it.  */
5868   if (op != ERROR_MARK)
5869     cp_lexer_consume_token (parser->lexer);
5870
5871   return op;
5872 }
5873
5874 /* Parse an expression.
5875
5876    expression:
5877      assignment-expression
5878      expression , assignment-expression
5879
5880    CAST_P is true if this expression is the target of a cast.
5881
5882    Returns a representation of the expression.  */
5883
5884 static tree
5885 cp_parser_expression (cp_parser* parser, bool cast_p)
5886 {
5887   tree expression = NULL_TREE;
5888
5889   while (true)
5890     {
5891       tree assignment_expression;
5892
5893       /* Parse the next assignment-expression.  */
5894       assignment_expression
5895         = cp_parser_assignment_expression (parser, cast_p);
5896       /* If this is the first assignment-expression, we can just
5897          save it away.  */
5898       if (!expression)
5899         expression = assignment_expression;
5900       else
5901         expression = build_x_compound_expr (expression,
5902                                             assignment_expression);
5903       /* If the next token is not a comma, then we are done with the
5904          expression.  */
5905       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5906         break;
5907       /* Consume the `,'.  */
5908       cp_lexer_consume_token (parser->lexer);
5909       /* A comma operator cannot appear in a constant-expression.  */
5910       if (cp_parser_non_integral_constant_expression (parser,
5911                                                       "a comma operator"))
5912         expression = error_mark_node;
5913     }
5914
5915   return expression;
5916 }
5917
5918 /* Parse a constant-expression.
5919
5920    constant-expression:
5921      conditional-expression
5922
5923   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5924   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5925   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5926   is false, NON_CONSTANT_P should be NULL.  */
5927
5928 static tree
5929 cp_parser_constant_expression (cp_parser* parser,
5930                                bool allow_non_constant_p,
5931                                bool *non_constant_p)
5932 {
5933   bool saved_integral_constant_expression_p;
5934   bool saved_allow_non_integral_constant_expression_p;
5935   bool saved_non_integral_constant_expression_p;
5936   tree expression;
5937
5938   /* It might seem that we could simply parse the
5939      conditional-expression, and then check to see if it were
5940      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5941      one that the compiler can figure out is constant, possibly after
5942      doing some simplifications or optimizations.  The standard has a
5943      precise definition of constant-expression, and we must honor
5944      that, even though it is somewhat more restrictive.
5945
5946      For example:
5947
5948        int i[(2, 3)];
5949
5950      is not a legal declaration, because `(2, 3)' is not a
5951      constant-expression.  The `,' operator is forbidden in a
5952      constant-expression.  However, GCC's constant-folding machinery
5953      will fold this operation to an INTEGER_CST for `3'.  */
5954
5955   /* Save the old settings.  */
5956   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5957   saved_allow_non_integral_constant_expression_p
5958     = parser->allow_non_integral_constant_expression_p;
5959   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5960   /* We are now parsing a constant-expression.  */
5961   parser->integral_constant_expression_p = true;
5962   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5963   parser->non_integral_constant_expression_p = false;
5964   /* Although the grammar says "conditional-expression", we parse an
5965      "assignment-expression", which also permits "throw-expression"
5966      and the use of assignment operators.  In the case that
5967      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5968      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5969      actually essential that we look for an assignment-expression.
5970      For example, cp_parser_initializer_clauses uses this function to
5971      determine whether a particular assignment-expression is in fact
5972      constant.  */
5973   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5974   /* Restore the old settings.  */
5975   parser->integral_constant_expression_p
5976     = saved_integral_constant_expression_p;
5977   parser->allow_non_integral_constant_expression_p
5978     = saved_allow_non_integral_constant_expression_p;
5979   if (allow_non_constant_p)
5980     *non_constant_p = parser->non_integral_constant_expression_p;
5981   else if (parser->non_integral_constant_expression_p)
5982     expression = error_mark_node;
5983   parser->non_integral_constant_expression_p
5984     = saved_non_integral_constant_expression_p;
5985
5986   return expression;
5987 }
5988
5989 /* Parse __builtin_offsetof.
5990
5991    offsetof-expression:
5992      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5993
5994    offsetof-member-designator:
5995      id-expression
5996      | offsetof-member-designator "." id-expression
5997      | offsetof-member-designator "[" expression "]"  */
5998
5999 static tree
6000 cp_parser_builtin_offsetof (cp_parser *parser)
6001 {
6002   int save_ice_p, save_non_ice_p;
6003   tree type, expr;
6004   cp_id_kind dummy;
6005
6006   /* We're about to accept non-integral-constant things, but will
6007      definitely yield an integral constant expression.  Save and
6008      restore these values around our local parsing.  */
6009   save_ice_p = parser->integral_constant_expression_p;
6010   save_non_ice_p = parser->non_integral_constant_expression_p;
6011
6012   /* Consume the "__builtin_offsetof" token.  */
6013   cp_lexer_consume_token (parser->lexer);
6014   /* Consume the opening `('.  */
6015   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6016   /* Parse the type-id.  */
6017   type = cp_parser_type_id (parser);
6018   /* Look for the `,'.  */
6019   cp_parser_require (parser, CPP_COMMA, "`,'");
6020
6021   /* Build the (type *)null that begins the traditional offsetof macro.  */
6022   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6023
6024   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6025   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6026                                                  true, &dummy);
6027   while (true)
6028     {
6029       cp_token *token = cp_lexer_peek_token (parser->lexer);
6030       switch (token->type)
6031         {
6032         case CPP_OPEN_SQUARE:
6033           /* offsetof-member-designator "[" expression "]" */
6034           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6035           break;
6036
6037         case CPP_DOT:
6038           /* offsetof-member-designator "." identifier */
6039           cp_lexer_consume_token (parser->lexer);
6040           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6041                                                          true, &dummy);
6042           break;
6043
6044         case CPP_CLOSE_PAREN:
6045           /* Consume the ")" token.  */
6046           cp_lexer_consume_token (parser->lexer);
6047           goto success;
6048
6049         default:
6050           /* Error.  We know the following require will fail, but
6051              that gives the proper error message.  */
6052           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6053           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6054           expr = error_mark_node;
6055           goto failure;
6056         }
6057     }
6058
6059  success:
6060   /* If we're processing a template, we can't finish the semantics yet.
6061      Otherwise we can fold the entire expression now.  */
6062   if (processing_template_decl)
6063     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6064   else
6065     expr = finish_offsetof (expr);
6066
6067  failure:
6068   parser->integral_constant_expression_p = save_ice_p;
6069   parser->non_integral_constant_expression_p = save_non_ice_p;
6070
6071   return expr;
6072 }
6073
6074 /* Statements [gram.stmt.stmt]  */
6075
6076 /* Parse a statement.
6077
6078    statement:
6079      labeled-statement
6080      expression-statement
6081      compound-statement
6082      selection-statement
6083      iteration-statement
6084      jump-statement
6085      declaration-statement
6086      try-block
6087
6088   IN_COMPOUND is true when the statement is nested inside a
6089   cp_parser_compound_statement; this matters for certain pragmas.  */
6090
6091 static void
6092 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6093                      bool in_compound)
6094 {
6095   tree statement;
6096   cp_token *token;
6097   location_t statement_location;
6098
6099  restart:
6100   /* There is no statement yet.  */
6101   statement = NULL_TREE;
6102   /* Peek at the next token.  */
6103   token = cp_lexer_peek_token (parser->lexer);
6104   /* Remember the location of the first token in the statement.  */
6105   statement_location = token->location;
6106   /* If this is a keyword, then that will often determine what kind of
6107      statement we have.  */
6108   if (token->type == CPP_KEYWORD)
6109     {
6110       enum rid keyword = token->keyword;
6111
6112       switch (keyword)
6113         {
6114         case RID_CASE:
6115         case RID_DEFAULT:
6116           statement = cp_parser_labeled_statement (parser, in_statement_expr,
6117                                                    in_compound);
6118           break;
6119
6120         case RID_IF:
6121         case RID_SWITCH:
6122           statement = cp_parser_selection_statement (parser);
6123           break;
6124
6125         case RID_WHILE:
6126         case RID_DO:
6127         case RID_FOR:
6128           statement = cp_parser_iteration_statement (parser);
6129           break;
6130
6131         case RID_BREAK:
6132         case RID_CONTINUE:
6133         case RID_RETURN:
6134         case RID_GOTO:
6135           statement = cp_parser_jump_statement (parser);
6136           break;
6137
6138           /* Objective-C++ exception-handling constructs.  */
6139         case RID_AT_TRY:
6140         case RID_AT_CATCH:
6141         case RID_AT_FINALLY:
6142         case RID_AT_SYNCHRONIZED:
6143         case RID_AT_THROW:
6144           statement = cp_parser_objc_statement (parser);
6145           break;
6146
6147         case RID_TRY:
6148           statement = cp_parser_try_block (parser);
6149           break;
6150
6151         default:
6152           /* It might be a keyword like `int' that can start a
6153              declaration-statement.  */
6154           break;
6155         }
6156     }
6157   else if (token->type == CPP_NAME)
6158     {
6159       /* If the next token is a `:', then we are looking at a
6160          labeled-statement.  */
6161       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6162       if (token->type == CPP_COLON)
6163         statement = cp_parser_labeled_statement (parser, in_statement_expr,
6164                                                  in_compound);
6165     }
6166   /* Anything that starts with a `{' must be a compound-statement.  */
6167   else if (token->type == CPP_OPEN_BRACE)
6168     statement = cp_parser_compound_statement (parser, NULL, false);
6169   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6170      a statement all its own.  */
6171   else if (token->type == CPP_PRAGMA)
6172     {
6173       /* Only certain OpenMP pragmas are attached to statements, and thus
6174          are considered statements themselves.  All others are not.  In
6175          the context of a compound, accept the pragma as a "statement" and
6176          return so that we can check for a close brace.  Otherwise we
6177          require a real statement and must go back and read one.  */
6178       if (in_compound)
6179         cp_parser_pragma (parser, pragma_compound);
6180       else if (!cp_parser_pragma (parser, pragma_stmt))
6181         goto restart;
6182       return;
6183     }
6184   else if (token->type == CPP_EOF)
6185     {
6186       cp_parser_error (parser, "expected statement");
6187       return;
6188     }
6189
6190   /* Everything else must be a declaration-statement or an
6191      expression-statement.  Try for the declaration-statement
6192      first, unless we are looking at a `;', in which case we know that
6193      we have an expression-statement.  */
6194   if (!statement)
6195     {
6196       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6197         {
6198           cp_parser_parse_tentatively (parser);
6199           /* Try to parse the declaration-statement.  */
6200           cp_parser_declaration_statement (parser);
6201           /* If that worked, we're done.  */
6202           if (cp_parser_parse_definitely (parser))
6203             return;
6204         }
6205       /* Look for an expression-statement instead.  */
6206       statement = cp_parser_expression_statement (parser, in_statement_expr);
6207     }
6208
6209   /* Set the line number for the statement.  */
6210   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6211     SET_EXPR_LOCATION (statement, statement_location);
6212 }
6213
6214 /* Parse a labeled-statement.
6215
6216    labeled-statement:
6217      identifier : statement
6218      case constant-expression : statement
6219      default : statement
6220
6221    GNU Extension:
6222
6223    labeled-statement:
6224      case constant-expression ... constant-expression : statement
6225
6226    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6227    For an ordinary label, returns a LABEL_EXPR.
6228
6229    IN_COMPOUND is as for cp_parser_statement: true when we're nested
6230    inside a compound.  */
6231
6232 static tree
6233 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6234                              bool in_compound)
6235 {
6236   cp_token *token;
6237   tree statement = error_mark_node;
6238
6239   /* The next token should be an identifier.  */
6240   token = cp_lexer_peek_token (parser->lexer);
6241   if (token->type != CPP_NAME
6242       && token->type != CPP_KEYWORD)
6243     {
6244       cp_parser_error (parser, "expected labeled-statement");
6245       return error_mark_node;
6246     }
6247
6248   switch (token->keyword)
6249     {
6250     case RID_CASE:
6251       {
6252         tree expr, expr_hi;
6253         cp_token *ellipsis;
6254
6255         /* Consume the `case' token.  */
6256         cp_lexer_consume_token (parser->lexer);
6257         /* Parse the constant-expression.  */
6258         expr = cp_parser_constant_expression (parser,
6259                                               /*allow_non_constant_p=*/false,
6260                                               NULL);
6261
6262         ellipsis = cp_lexer_peek_token (parser->lexer);
6263         if (ellipsis->type == CPP_ELLIPSIS)
6264           {
6265             /* Consume the `...' token.  */
6266             cp_lexer_consume_token (parser->lexer);
6267             expr_hi =
6268               cp_parser_constant_expression (parser,
6269                                              /*allow_non_constant_p=*/false,
6270                                              NULL);
6271             /* We don't need to emit warnings here, as the common code
6272                will do this for us.  */
6273           }
6274         else
6275           expr_hi = NULL_TREE;
6276
6277         if (parser->in_switch_statement_p)
6278           statement = finish_case_label (expr, expr_hi);
6279         else
6280           error ("case label %qE not within a switch statement", expr);
6281       }
6282       break;
6283
6284     case RID_DEFAULT:
6285       /* Consume the `default' token.  */
6286       cp_lexer_consume_token (parser->lexer);
6287
6288       if (parser->in_switch_statement_p)
6289         statement = finish_case_label (NULL_TREE, NULL_TREE);
6290       else
6291         error ("case label not within a switch statement");
6292       break;
6293
6294     default:
6295       /* Anything else must be an ordinary label.  */
6296       statement = finish_label_stmt (cp_parser_identifier (parser));
6297       break;
6298     }
6299
6300   /* Require the `:' token.  */
6301   cp_parser_require (parser, CPP_COLON, "`:'");
6302   /* Parse the labeled statement.  */
6303   cp_parser_statement (parser, in_statement_expr, in_compound);
6304
6305   /* Return the label, in the case of a `case' or `default' label.  */
6306   return statement;
6307 }
6308
6309 /* Parse an expression-statement.
6310
6311    expression-statement:
6312      expression [opt] ;
6313
6314    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6315    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6316    indicates whether this expression-statement is part of an
6317    expression statement.  */
6318
6319 static tree
6320 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6321 {
6322   tree statement = NULL_TREE;
6323
6324   /* If the next token is a ';', then there is no expression
6325      statement.  */
6326   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6327     statement = cp_parser_expression (parser, /*cast_p=*/false);
6328
6329   /* Consume the final `;'.  */
6330   cp_parser_consume_semicolon_at_end_of_statement (parser);
6331
6332   if (in_statement_expr
6333       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6334     /* This is the final expression statement of a statement
6335        expression.  */
6336     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6337   else if (statement)
6338     statement = finish_expr_stmt (statement);
6339   else
6340     finish_stmt ();
6341
6342   return statement;
6343 }
6344
6345 /* Parse a compound-statement.
6346
6347    compound-statement:
6348      { statement-seq [opt] }
6349
6350    Returns a tree representing the statement.  */
6351
6352 static tree
6353 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6354                               bool in_try)
6355 {
6356   tree compound_stmt;
6357
6358   /* Consume the `{'.  */
6359   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6360     return error_mark_node;
6361   /* Begin the compound-statement.  */
6362   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6363   /* Parse an (optional) statement-seq.  */
6364   cp_parser_statement_seq_opt (parser, in_statement_expr);
6365   /* Finish the compound-statement.  */
6366   finish_compound_stmt (compound_stmt);
6367   /* Consume the `}'.  */
6368   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6369
6370   return compound_stmt;
6371 }
6372
6373 /* Parse an (optional) statement-seq.
6374
6375    statement-seq:
6376      statement
6377      statement-seq [opt] statement  */
6378
6379 static void
6380 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6381 {
6382   /* Scan statements until there aren't any more.  */
6383   while (true)
6384     {
6385       cp_token *token = cp_lexer_peek_token (parser->lexer);
6386
6387       /* If we're looking at a `}', then we've run out of statements.  */
6388       if (token->type == CPP_CLOSE_BRACE
6389           || token->type == CPP_EOF
6390           || token->type == CPP_PRAGMA_EOL)
6391         break;
6392
6393       /* Parse the statement.  */
6394       cp_parser_statement (parser, in_statement_expr, true);
6395     }
6396 }
6397
6398 /* Parse a selection-statement.
6399
6400    selection-statement:
6401      if ( condition ) statement
6402      if ( condition ) statement else statement
6403      switch ( condition ) statement
6404
6405    Returns the new IF_STMT or SWITCH_STMT.  */
6406
6407 static tree
6408 cp_parser_selection_statement (cp_parser* parser)
6409 {
6410   cp_token *token;
6411   enum rid keyword;
6412
6413   /* Peek at the next token.  */
6414   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6415
6416   /* See what kind of keyword it is.  */
6417   keyword = token->keyword;
6418   switch (keyword)
6419     {
6420     case RID_IF:
6421     case RID_SWITCH:
6422       {
6423         tree statement;
6424         tree condition;
6425
6426         /* Look for the `('.  */
6427         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6428           {
6429             cp_parser_skip_to_end_of_statement (parser);
6430             return error_mark_node;
6431           }
6432
6433         /* Begin the selection-statement.  */
6434         if (keyword == RID_IF)
6435           statement = begin_if_stmt ();
6436         else
6437           statement = begin_switch_stmt ();
6438
6439         /* Parse the condition.  */
6440         condition = cp_parser_condition (parser);
6441         /* Look for the `)'.  */
6442         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6443           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6444                                                  /*consume_paren=*/true);
6445
6446         if (keyword == RID_IF)
6447           {
6448             /* Add the condition.  */
6449             finish_if_stmt_cond (condition, statement);
6450
6451             /* Parse the then-clause.  */
6452             cp_parser_implicitly_scoped_statement (parser);
6453             finish_then_clause (statement);
6454
6455             /* If the next token is `else', parse the else-clause.  */
6456             if (cp_lexer_next_token_is_keyword (parser->lexer,
6457                                                 RID_ELSE))
6458               {
6459                 /* Consume the `else' keyword.  */
6460                 cp_lexer_consume_token (parser->lexer);
6461                 begin_else_clause (statement);
6462                 /* Parse the else-clause.  */
6463                 cp_parser_implicitly_scoped_statement (parser);
6464                 finish_else_clause (statement);
6465               }
6466
6467             /* Now we're all done with the if-statement.  */
6468             finish_if_stmt (statement);
6469           }
6470         else
6471           {
6472             bool in_switch_statement_p;
6473             unsigned char in_statement;
6474
6475             /* Add the condition.  */
6476             finish_switch_cond (condition, statement);
6477
6478             /* Parse the body of the switch-statement.  */
6479             in_switch_statement_p = parser->in_switch_statement_p;
6480             in_statement = parser->in_statement;
6481             parser->in_switch_statement_p = true;
6482             parser->in_statement |= IN_SWITCH_STMT;
6483             cp_parser_implicitly_scoped_statement (parser);
6484             parser->in_switch_statement_p = in_switch_statement_p;
6485             parser->in_statement = in_statement;
6486
6487             /* Now we're all done with the switch-statement.  */
6488             finish_switch_stmt (statement);
6489           }
6490
6491         return statement;
6492       }
6493       break;
6494
6495     default:
6496       cp_parser_error (parser, "expected selection-statement");
6497       return error_mark_node;
6498     }
6499 }
6500
6501 /* Parse a condition.
6502
6503    condition:
6504      expression
6505      type-specifier-seq declarator = assignment-expression
6506
6507    GNU Extension:
6508
6509    condition:
6510      type-specifier-seq declarator asm-specification [opt]
6511        attributes [opt] = assignment-expression
6512
6513    Returns the expression that should be tested.  */
6514
6515 static tree
6516 cp_parser_condition (cp_parser* parser)
6517 {
6518   cp_decl_specifier_seq type_specifiers;
6519   const char *saved_message;
6520
6521   /* Try the declaration first.  */
6522   cp_parser_parse_tentatively (parser);
6523   /* New types are not allowed in the type-specifier-seq for a
6524      condition.  */
6525   saved_message = parser->type_definition_forbidden_message;
6526   parser->type_definition_forbidden_message
6527     = "types may not be defined in conditions";
6528   /* Parse the type-specifier-seq.  */
6529   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6530                                 &type_specifiers);
6531   /* Restore the saved message.  */
6532   parser->type_definition_forbidden_message = saved_message;
6533   /* If all is well, we might be looking at a declaration.  */
6534   if (!cp_parser_error_occurred (parser))
6535     {
6536       tree decl;
6537       tree asm_specification;
6538       tree attributes;
6539       cp_declarator *declarator;
6540       tree initializer = NULL_TREE;
6541
6542       /* Parse the declarator.  */
6543       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6544                                          /*ctor_dtor_or_conv_p=*/NULL,
6545                                          /*parenthesized_p=*/NULL,
6546                                          /*member_p=*/false);
6547       /* Parse the attributes.  */
6548       attributes = cp_parser_attributes_opt (parser);
6549       /* Parse the asm-specification.  */
6550       asm_specification = cp_parser_asm_specification_opt (parser);
6551       /* If the next token is not an `=', then we might still be
6552          looking at an expression.  For example:
6553
6554            if (A(a).x)
6555
6556          looks like a decl-specifier-seq and a declarator -- but then
6557          there is no `=', so this is an expression.  */
6558       cp_parser_require (parser, CPP_EQ, "`='");
6559       /* If we did see an `=', then we are looking at a declaration
6560          for sure.  */
6561       if (cp_parser_parse_definitely (parser))
6562         {
6563           tree pushed_scope;
6564           bool non_constant_p;
6565
6566           /* Create the declaration.  */
6567           decl = start_decl (declarator, &type_specifiers,
6568                              /*initialized_p=*/true,
6569                              attributes, /*prefix_attributes=*/NULL_TREE,
6570                              &pushed_scope);
6571           /* Parse the assignment-expression.  */
6572           initializer
6573             = cp_parser_constant_expression (parser,
6574                                              /*allow_non_constant_p=*/true,
6575                                              &non_constant_p);
6576           if (!non_constant_p)
6577             initializer = fold_non_dependent_expr (initializer);
6578
6579           /* Process the initializer.  */
6580           cp_finish_decl (decl,
6581                           initializer, !non_constant_p,
6582                           asm_specification,
6583                           LOOKUP_ONLYCONVERTING);
6584
6585           if (pushed_scope)
6586             pop_scope (pushed_scope);
6587
6588           return convert_from_reference (decl);
6589         }
6590     }
6591   /* If we didn't even get past the declarator successfully, we are
6592      definitely not looking at a declaration.  */
6593   else
6594     cp_parser_abort_tentative_parse (parser);
6595
6596   /* Otherwise, we are looking at an expression.  */
6597   return cp_parser_expression (parser, /*cast_p=*/false);
6598 }
6599
6600 /* Parse an iteration-statement.
6601
6602    iteration-statement:
6603      while ( condition ) statement
6604      do statement while ( expression ) ;
6605      for ( for-init-statement condition [opt] ; expression [opt] )
6606        statement
6607
6608    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6609
6610 static tree
6611 cp_parser_iteration_statement (cp_parser* parser)
6612 {
6613   cp_token *token;
6614   enum rid keyword;
6615   tree statement;
6616   unsigned char in_statement;
6617
6618   /* Peek at the next token.  */
6619   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6620   if (!token)
6621     return error_mark_node;
6622
6623   /* Remember whether or not we are already within an iteration
6624      statement.  */
6625   in_statement = parser->in_statement;
6626
6627   /* See what kind of keyword it is.  */
6628   keyword = token->keyword;
6629   switch (keyword)
6630     {
6631     case RID_WHILE:
6632       {
6633         tree condition;
6634
6635         /* Begin the while-statement.  */
6636         statement = begin_while_stmt ();
6637         /* Look for the `('.  */
6638         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6639         /* Parse the condition.  */
6640         condition = cp_parser_condition (parser);
6641         finish_while_stmt_cond (condition, statement);
6642         /* Look for the `)'.  */
6643         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6644         /* Parse the dependent statement.  */
6645         parser->in_statement = IN_ITERATION_STMT;
6646         cp_parser_already_scoped_statement (parser);
6647         parser->in_statement = in_statement;
6648         /* We're done with the while-statement.  */
6649         finish_while_stmt (statement);
6650       }
6651       break;
6652
6653     case RID_DO:
6654       {
6655         tree expression;
6656
6657         /* Begin the do-statement.  */
6658         statement = begin_do_stmt ();
6659         /* Parse the body of the do-statement.  */
6660         parser->in_statement = IN_ITERATION_STMT;
6661         cp_parser_implicitly_scoped_statement (parser);
6662         parser->in_statement = in_statement;
6663         finish_do_body (statement);
6664         /* Look for the `while' keyword.  */
6665         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6666         /* Look for the `('.  */
6667         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6668         /* Parse the expression.  */
6669         expression = cp_parser_expression (parser, /*cast_p=*/false);
6670         /* We're done with the do-statement.  */
6671         finish_do_stmt (expression, statement);
6672         /* Look for the `)'.  */
6673         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6674         /* Look for the `;'.  */
6675         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6676       }
6677       break;
6678
6679     case RID_FOR:
6680       {
6681         tree condition = NULL_TREE;
6682         tree expression = NULL_TREE;
6683
6684         /* Begin the for-statement.  */
6685         statement = begin_for_stmt ();
6686         /* Look for the `('.  */
6687         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6688         /* Parse the initialization.  */
6689         cp_parser_for_init_statement (parser);
6690         finish_for_init_stmt (statement);
6691
6692         /* If there's a condition, process it.  */
6693         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6694           condition = cp_parser_condition (parser);
6695         finish_for_cond (condition, statement);
6696         /* Look for the `;'.  */
6697         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6698
6699         /* If there's an expression, process it.  */
6700         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6701           expression = cp_parser_expression (parser, /*cast_p=*/false);
6702         finish_for_expr (expression, statement);
6703         /* Look for the `)'.  */
6704         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6705
6706         /* Parse the body of the for-statement.  */
6707         parser->in_statement = IN_ITERATION_STMT;
6708         cp_parser_already_scoped_statement (parser);
6709         parser->in_statement = in_statement;
6710
6711         /* We're done with the for-statement.  */
6712         finish_for_stmt (statement);
6713       }
6714       break;
6715
6716     default:
6717       cp_parser_error (parser, "expected iteration-statement");
6718       statement = error_mark_node;
6719       break;
6720     }
6721
6722   return statement;
6723 }
6724
6725 /* Parse a for-init-statement.
6726
6727    for-init-statement:
6728      expression-statement
6729      simple-declaration  */
6730
6731 static void
6732 cp_parser_for_init_statement (cp_parser* parser)
6733 {
6734   /* If the next token is a `;', then we have an empty
6735      expression-statement.  Grammatically, this is also a
6736      simple-declaration, but an invalid one, because it does not
6737      declare anything.  Therefore, if we did not handle this case
6738      specially, we would issue an error message about an invalid
6739      declaration.  */
6740   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6741     {
6742       /* We're going to speculatively look for a declaration, falling back
6743          to an expression, if necessary.  */
6744       cp_parser_parse_tentatively (parser);
6745       /* Parse the declaration.  */
6746       cp_parser_simple_declaration (parser,
6747                                     /*function_definition_allowed_p=*/false);
6748       /* If the tentative parse failed, then we shall need to look for an
6749          expression-statement.  */
6750       if (cp_parser_parse_definitely (parser))
6751         return;
6752     }
6753
6754   cp_parser_expression_statement (parser, false);
6755 }
6756
6757 /* Parse a jump-statement.
6758
6759    jump-statement:
6760      break ;
6761      continue ;
6762      return expression [opt] ;
6763      goto identifier ;
6764
6765    GNU extension:
6766
6767    jump-statement:
6768      goto * expression ;
6769
6770    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6771
6772 static tree
6773 cp_parser_jump_statement (cp_parser* parser)
6774 {
6775   tree statement = error_mark_node;
6776   cp_token *token;
6777   enum rid keyword;
6778
6779   /* Peek at the next token.  */
6780   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6781   if (!token)
6782     return error_mark_node;
6783
6784   /* See what kind of keyword it is.  */
6785   keyword = token->keyword;
6786   switch (keyword)
6787     {
6788     case RID_BREAK:
6789       switch (parser->in_statement)
6790         {
6791         case 0:
6792           error ("break statement not within loop or switch");
6793           break;
6794         default:
6795           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6796                       || parser->in_statement == IN_ITERATION_STMT);
6797           statement = finish_break_stmt ();
6798           break;
6799         case IN_OMP_BLOCK:
6800           error ("invalid exit from OpenMP structured block");
6801           break;
6802         case IN_OMP_FOR:
6803           error ("break statement used with OpenMP for loop");
6804           break;
6805         }
6806       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6807       break;
6808
6809     case RID_CONTINUE:
6810       switch (parser->in_statement & ~IN_SWITCH_STMT)
6811         {
6812         case 0:
6813           error ("continue statement not within a loop");
6814           break;
6815         case IN_ITERATION_STMT:
6816         case IN_OMP_FOR:
6817           statement = finish_continue_stmt ();
6818           break;
6819         case IN_OMP_BLOCK:
6820           error ("invalid exit from OpenMP structured block");
6821           break;
6822         default:
6823           gcc_unreachable ();
6824         }
6825       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6826       break;
6827
6828     case RID_RETURN:
6829       {
6830         tree expr;
6831
6832         /* If the next token is a `;', then there is no
6833            expression.  */
6834         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6835           expr = cp_parser_expression (parser, /*cast_p=*/false);
6836         else
6837           expr = NULL_TREE;
6838         /* Build the return-statement.  */
6839         statement = finish_return_stmt (expr);
6840         /* Look for the final `;'.  */
6841         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6842       }
6843       break;
6844
6845     case RID_GOTO:
6846       /* Create the goto-statement.  */
6847       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6848         {
6849           /* Issue a warning about this use of a GNU extension.  */
6850           if (pedantic)
6851             pedwarn ("ISO C++ forbids computed gotos");
6852           /* Consume the '*' token.  */
6853           cp_lexer_consume_token (parser->lexer);
6854           /* Parse the dependent expression.  */
6855           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6856         }
6857       else
6858         finish_goto_stmt (cp_parser_identifier (parser));
6859       /* Look for the final `;'.  */
6860       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6861       break;
6862
6863     default:
6864       cp_parser_error (parser, "expected jump-statement");
6865       break;
6866     }
6867
6868   return statement;
6869 }
6870
6871 /* Parse a declaration-statement.
6872
6873    declaration-statement:
6874      block-declaration  */
6875
6876 static void
6877 cp_parser_declaration_statement (cp_parser* parser)
6878 {
6879   void *p;
6880
6881   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6882   p = obstack_alloc (&declarator_obstack, 0);
6883
6884  /* Parse the block-declaration.  */
6885   cp_parser_block_declaration (parser, /*statement_p=*/true);
6886
6887   /* Free any declarators allocated.  */
6888   obstack_free (&declarator_obstack, p);
6889
6890   /* Finish off the statement.  */
6891   finish_stmt ();
6892 }
6893
6894 /* Some dependent statements (like `if (cond) statement'), are
6895    implicitly in their own scope.  In other words, if the statement is
6896    a single statement (as opposed to a compound-statement), it is
6897    none-the-less treated as if it were enclosed in braces.  Any
6898    declarations appearing in the dependent statement are out of scope
6899    after control passes that point.  This function parses a statement,
6900    but ensures that is in its own scope, even if it is not a
6901    compound-statement.
6902
6903    Returns the new statement.  */
6904
6905 static tree
6906 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6907 {
6908   tree statement;
6909
6910   /* Mark if () ; with a special NOP_EXPR.  */
6911   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6912     {
6913       cp_lexer_consume_token (parser->lexer);
6914       statement = add_stmt (build_empty_stmt ());
6915     }
6916   /* if a compound is opened, we simply parse the statement directly.  */
6917   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6918     statement = cp_parser_compound_statement (parser, NULL, false);
6919   /* If the token is not a `{', then we must take special action.  */
6920   else
6921     {
6922       /* Create a compound-statement.  */
6923       statement = begin_compound_stmt (0);
6924       /* Parse the dependent-statement.  */
6925       cp_parser_statement (parser, NULL_TREE, false);
6926       /* Finish the dummy compound-statement.  */
6927       finish_compound_stmt (statement);
6928     }
6929
6930   /* Return the statement.  */
6931   return statement;
6932 }
6933
6934 /* For some dependent statements (like `while (cond) statement'), we
6935    have already created a scope.  Therefore, even if the dependent
6936    statement is a compound-statement, we do not want to create another
6937    scope.  */
6938
6939 static void
6940 cp_parser_already_scoped_statement (cp_parser* parser)
6941 {
6942   /* If the token is a `{', then we must take special action.  */
6943   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6944     cp_parser_statement (parser, NULL_TREE, false);
6945   else
6946     {
6947       /* Avoid calling cp_parser_compound_statement, so that we
6948          don't create a new scope.  Do everything else by hand.  */
6949       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6950       cp_parser_statement_seq_opt (parser, NULL_TREE);
6951       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6952     }
6953 }
6954
6955 /* Declarations [gram.dcl.dcl] */
6956
6957 /* Parse an optional declaration-sequence.
6958
6959    declaration-seq:
6960      declaration
6961      declaration-seq declaration  */
6962
6963 static void
6964 cp_parser_declaration_seq_opt (cp_parser* parser)
6965 {
6966   while (true)
6967     {
6968       cp_token *token;
6969
6970       token = cp_lexer_peek_token (parser->lexer);
6971
6972       if (token->type == CPP_CLOSE_BRACE
6973           || token->type == CPP_EOF
6974           || token->type == CPP_PRAGMA_EOL)
6975         break;
6976
6977       if (token->type == CPP_SEMICOLON)
6978         {
6979           /* A declaration consisting of a single semicolon is
6980              invalid.  Allow it unless we're being pedantic.  */
6981           cp_lexer_consume_token (parser->lexer);
6982           if (pedantic && !in_system_header)
6983             pedwarn ("extra %<;%>");
6984           continue;
6985         }
6986
6987       /* If we're entering or exiting a region that's implicitly
6988          extern "C", modify the lang context appropriately.  */
6989       if (!parser->implicit_extern_c && token->implicit_extern_c)
6990         {
6991           push_lang_context (lang_name_c);
6992           parser->implicit_extern_c = true;
6993         }
6994       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6995         {
6996           pop_lang_context ();
6997           parser->implicit_extern_c = false;
6998         }
6999
7000       if (token->type == CPP_PRAGMA)
7001         {
7002           /* A top-level declaration can consist solely of a #pragma.
7003              A nested declaration cannot, so this is done here and not
7004              in cp_parser_declaration.  (A #pragma at block scope is
7005              handled in cp_parser_statement.)  */
7006           cp_parser_pragma (parser, pragma_external);
7007           continue;
7008         }
7009
7010       /* Parse the declaration itself.  */
7011       cp_parser_declaration (parser);
7012     }
7013 }
7014
7015 /* Parse a declaration.
7016
7017    declaration:
7018      block-declaration
7019      function-definition
7020      template-declaration
7021      explicit-instantiation
7022      explicit-specialization
7023      linkage-specification
7024      namespace-definition
7025
7026    GNU extension:
7027
7028    declaration:
7029       __extension__ declaration */
7030
7031 static void
7032 cp_parser_declaration (cp_parser* parser)
7033 {
7034   cp_token token1;
7035   cp_token token2;
7036   int saved_pedantic;
7037   void *p;
7038
7039   /* Check for the `__extension__' keyword.  */
7040   if (cp_parser_extension_opt (parser, &saved_pedantic))
7041     {
7042       /* Parse the qualified declaration.  */
7043       cp_parser_declaration (parser);
7044       /* Restore the PEDANTIC flag.  */
7045       pedantic = saved_pedantic;
7046
7047       return;
7048     }
7049
7050   /* Try to figure out what kind of declaration is present.  */
7051   token1 = *cp_lexer_peek_token (parser->lexer);
7052
7053   if (token1.type != CPP_EOF)
7054     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7055   else
7056     {
7057       token2.type = CPP_EOF;
7058       token2.keyword = RID_MAX;
7059     }
7060
7061   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7062   p = obstack_alloc (&declarator_obstack, 0);
7063
7064   /* If the next token is `extern' and the following token is a string
7065      literal, then we have a linkage specification.  */
7066   if (token1.keyword == RID_EXTERN
7067       && cp_parser_is_string_literal (&token2))
7068     cp_parser_linkage_specification (parser);
7069   /* If the next token is `template', then we have either a template
7070      declaration, an explicit instantiation, or an explicit
7071      specialization.  */
7072   else if (token1.keyword == RID_TEMPLATE)
7073     {
7074       /* `template <>' indicates a template specialization.  */
7075       if (token2.type == CPP_LESS
7076           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7077         cp_parser_explicit_specialization (parser);
7078       /* `template <' indicates a template declaration.  */
7079       else if (token2.type == CPP_LESS)
7080         cp_parser_template_declaration (parser, /*member_p=*/false);
7081       /* Anything else must be an explicit instantiation.  */
7082       else
7083         cp_parser_explicit_instantiation (parser);
7084     }
7085   /* If the next token is `export', then we have a template
7086      declaration.  */
7087   else if (token1.keyword == RID_EXPORT)
7088     cp_parser_template_declaration (parser, /*member_p=*/false);
7089   /* If the next token is `extern', 'static' or 'inline' and the one
7090      after that is `template', we have a GNU extended explicit
7091      instantiation directive.  */
7092   else if (cp_parser_allow_gnu_extensions_p (parser)
7093            && (token1.keyword == RID_EXTERN
7094                || token1.keyword == RID_STATIC
7095                || token1.keyword == RID_INLINE)
7096            && token2.keyword == RID_TEMPLATE)
7097     cp_parser_explicit_instantiation (parser);
7098   /* If the next token is `namespace', check for a named or unnamed
7099      namespace definition.  */
7100   else if (token1.keyword == RID_NAMESPACE
7101            && (/* A named namespace definition.  */
7102                (token2.type == CPP_NAME
7103                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7104                     != CPP_EQ))
7105                /* An unnamed namespace definition.  */
7106                || token2.type == CPP_OPEN_BRACE
7107                || token2.keyword == RID_ATTRIBUTE))
7108     cp_parser_namespace_definition (parser);
7109   /* Objective-C++ declaration/definition.  */
7110   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7111     cp_parser_objc_declaration (parser);
7112   /* We must have either a block declaration or a function
7113      definition.  */
7114   else
7115     /* Try to parse a block-declaration, or a function-definition.  */
7116     cp_parser_block_declaration (parser, /*statement_p=*/false);
7117
7118   /* Free any declarators allocated.  */
7119   obstack_free (&declarator_obstack, p);
7120 }
7121
7122 /* Parse a block-declaration.
7123
7124    block-declaration:
7125      simple-declaration
7126      asm-definition
7127      namespace-alias-definition
7128      using-declaration
7129      using-directive
7130
7131    GNU Extension:
7132
7133    block-declaration:
7134      __extension__ block-declaration
7135      label-declaration
7136
7137    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7138    part of a declaration-statement.  */
7139
7140 static void
7141 cp_parser_block_declaration (cp_parser *parser,
7142                              bool      statement_p)
7143 {
7144   cp_token *token1;
7145   int saved_pedantic;
7146
7147   /* Check for the `__extension__' keyword.  */
7148   if (cp_parser_extension_opt (parser, &saved_pedantic))
7149     {
7150       /* Parse the qualified declaration.  */
7151       cp_parser_block_declaration (parser, statement_p);
7152       /* Restore the PEDANTIC flag.  */
7153       pedantic = saved_pedantic;
7154
7155       return;
7156     }
7157
7158   /* Peek at the next token to figure out which kind of declaration is
7159      present.  */
7160   token1 = cp_lexer_peek_token (parser->lexer);
7161
7162   /* If the next keyword is `asm', we have an asm-definition.  */
7163   if (token1->keyword == RID_ASM)
7164     {
7165       if (statement_p)
7166         cp_parser_commit_to_tentative_parse (parser);
7167       cp_parser_asm_definition (parser);
7168     }
7169   /* If the next keyword is `namespace', we have a
7170      namespace-alias-definition.  */
7171   else if (token1->keyword == RID_NAMESPACE)
7172     cp_parser_namespace_alias_definition (parser);
7173   /* If the next keyword is `using', we have either a
7174      using-declaration or a using-directive.  */
7175   else if (token1->keyword == RID_USING)
7176     {
7177       cp_token *token2;
7178
7179       if (statement_p)
7180         cp_parser_commit_to_tentative_parse (parser);
7181       /* If the token after `using' is `namespace', then we have a
7182          using-directive.  */
7183       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7184       if (token2->keyword == RID_NAMESPACE)
7185         cp_parser_using_directive (parser);
7186       /* Otherwise, it's a using-declaration.  */
7187       else
7188         cp_parser_using_declaration (parser);
7189     }
7190   /* If the next keyword is `__label__' we have a label declaration.  */
7191   else if (token1->keyword == RID_LABEL)
7192     {
7193       if (statement_p)
7194         cp_parser_commit_to_tentative_parse (parser);
7195       cp_parser_label_declaration (parser);
7196     }
7197   /* Anything else must be a simple-declaration.  */
7198   else
7199     cp_parser_simple_declaration (parser, !statement_p);
7200 }
7201
7202 /* Parse a simple-declaration.
7203
7204    simple-declaration:
7205      decl-specifier-seq [opt] init-declarator-list [opt] ;
7206
7207    init-declarator-list:
7208      init-declarator
7209      init-declarator-list , init-declarator
7210
7211    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7212    function-definition as a simple-declaration.  */
7213
7214 static void
7215 cp_parser_simple_declaration (cp_parser* parser,
7216                               bool function_definition_allowed_p)
7217 {
7218   cp_decl_specifier_seq decl_specifiers;
7219   int declares_class_or_enum;
7220   bool saw_declarator;
7221
7222   /* Defer access checks until we know what is being declared; the
7223      checks for names appearing in the decl-specifier-seq should be
7224      done as if we were in the scope of the thing being declared.  */
7225   push_deferring_access_checks (dk_deferred);
7226
7227   /* Parse the decl-specifier-seq.  We have to keep track of whether
7228      or not the decl-specifier-seq declares a named class or
7229      enumeration type, since that is the only case in which the
7230      init-declarator-list is allowed to be empty.
7231
7232      [dcl.dcl]
7233
7234      In a simple-declaration, the optional init-declarator-list can be
7235      omitted only when declaring a class or enumeration, that is when
7236      the decl-specifier-seq contains either a class-specifier, an
7237      elaborated-type-specifier, or an enum-specifier.  */
7238   cp_parser_decl_specifier_seq (parser,
7239                                 CP_PARSER_FLAGS_OPTIONAL,
7240                                 &decl_specifiers,
7241                                 &declares_class_or_enum);
7242   /* We no longer need to defer access checks.  */
7243   stop_deferring_access_checks ();
7244
7245   /* In a block scope, a valid declaration must always have a
7246      decl-specifier-seq.  By not trying to parse declarators, we can
7247      resolve the declaration/expression ambiguity more quickly.  */
7248   if (!function_definition_allowed_p
7249       && !decl_specifiers.any_specifiers_p)
7250     {
7251       cp_parser_error (parser, "expected declaration");
7252       goto done;
7253     }
7254
7255   /* If the next two tokens are both identifiers, the code is
7256      erroneous. The usual cause of this situation is code like:
7257
7258        T t;
7259
7260      where "T" should name a type -- but does not.  */
7261   if (!decl_specifiers.type
7262       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7263     {
7264       /* If parsing tentatively, we should commit; we really are
7265          looking at a declaration.  */
7266       cp_parser_commit_to_tentative_parse (parser);
7267       /* Give up.  */
7268       goto done;
7269     }
7270
7271   /* If we have seen at least one decl-specifier, and the next token
7272      is not a parenthesis, then we must be looking at a declaration.
7273      (After "int (" we might be looking at a functional cast.)  */
7274   if (decl_specifiers.any_specifiers_p
7275       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7276     cp_parser_commit_to_tentative_parse (parser);
7277
7278   /* Keep going until we hit the `;' at the end of the simple
7279      declaration.  */
7280   saw_declarator = false;
7281   while (cp_lexer_next_token_is_not (parser->lexer,
7282                                      CPP_SEMICOLON))
7283     {
7284       cp_token *token;
7285       bool function_definition_p;
7286       tree decl;
7287
7288       if (saw_declarator)
7289         {
7290           /* If we are processing next declarator, coma is expected */
7291           token = cp_lexer_peek_token (parser->lexer);
7292           gcc_assert (token->type == CPP_COMMA);
7293           cp_lexer_consume_token (parser->lexer);
7294         }
7295       else
7296         saw_declarator = true;
7297
7298       /* Parse the init-declarator.  */
7299       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7300                                         /*checks=*/NULL_TREE,
7301                                         function_definition_allowed_p,
7302                                         /*member_p=*/false,
7303                                         declares_class_or_enum,
7304                                         &function_definition_p);
7305       /* If an error occurred while parsing tentatively, exit quickly.
7306          (That usually happens when in the body of a function; each
7307          statement is treated as a declaration-statement until proven
7308          otherwise.)  */
7309       if (cp_parser_error_occurred (parser))
7310         goto done;
7311       /* Handle function definitions specially.  */
7312       if (function_definition_p)
7313         {
7314           /* If the next token is a `,', then we are probably
7315              processing something like:
7316
7317                void f() {}, *p;
7318
7319              which is erroneous.  */
7320           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7321             error ("mixing declarations and function-definitions is forbidden");
7322           /* Otherwise, we're done with the list of declarators.  */
7323           else
7324             {
7325               pop_deferring_access_checks ();
7326               return;
7327             }
7328         }
7329       /* The next token should be either a `,' or a `;'.  */
7330       token = cp_lexer_peek_token (parser->lexer);
7331       /* If it's a `,', there are more declarators to come.  */
7332       if (token->type == CPP_COMMA)
7333         /* will be consumed next time around */;
7334       /* If it's a `;', we are done.  */
7335       else if (token->type == CPP_SEMICOLON)
7336         break;
7337       /* Anything else is an error.  */
7338       else
7339         {
7340           /* If we have already issued an error message we don't need
7341              to issue another one.  */
7342           if (decl != error_mark_node
7343               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7344             cp_parser_error (parser, "expected %<,%> or %<;%>");
7345           /* Skip tokens until we reach the end of the statement.  */
7346           cp_parser_skip_to_end_of_statement (parser);
7347           /* If the next token is now a `;', consume it.  */
7348           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7349             cp_lexer_consume_token (parser->lexer);
7350           goto done;
7351         }
7352       /* After the first time around, a function-definition is not
7353          allowed -- even if it was OK at first.  For example:
7354
7355            int i, f() {}
7356
7357          is not valid.  */
7358       function_definition_allowed_p = false;
7359     }
7360
7361   /* Issue an error message if no declarators are present, and the
7362      decl-specifier-seq does not itself declare a class or
7363      enumeration.  */
7364   if (!saw_declarator)
7365     {
7366       if (cp_parser_declares_only_class_p (parser))
7367         shadow_tag (&decl_specifiers);
7368       /* Perform any deferred access checks.  */
7369       perform_deferred_access_checks ();
7370     }
7371
7372   /* Consume the `;'.  */
7373   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7374
7375  done:
7376   pop_deferring_access_checks ();
7377 }
7378
7379 /* Parse a decl-specifier-seq.
7380
7381    decl-specifier-seq:
7382      decl-specifier-seq [opt] decl-specifier
7383
7384    decl-specifier:
7385      storage-class-specifier
7386      type-specifier
7387      function-specifier
7388      friend
7389      typedef
7390
7391    GNU Extension:
7392
7393    decl-specifier:
7394      attributes
7395
7396    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7397
7398    The parser flags FLAGS is used to control type-specifier parsing.
7399
7400    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7401    flags:
7402
7403      1: one of the decl-specifiers is an elaborated-type-specifier
7404         (i.e., a type declaration)
7405      2: one of the decl-specifiers is an enum-specifier or a
7406         class-specifier (i.e., a type definition)
7407
7408    */
7409
7410 static void
7411 cp_parser_decl_specifier_seq (cp_parser* parser,
7412                               cp_parser_flags flags,
7413                               cp_decl_specifier_seq *decl_specs,
7414                               int* declares_class_or_enum)
7415 {
7416   bool constructor_possible_p = !parser->in_declarator_p;
7417
7418   /* Clear DECL_SPECS.  */
7419   clear_decl_specs (decl_specs);
7420
7421   /* Assume no class or enumeration type is declared.  */
7422   *declares_class_or_enum = 0;
7423
7424   /* Keep reading specifiers until there are no more to read.  */
7425   while (true)
7426     {
7427       bool constructor_p;
7428       bool found_decl_spec;
7429       cp_token *token;
7430
7431       /* Peek at the next token.  */
7432       token = cp_lexer_peek_token (parser->lexer);
7433       /* Handle attributes.  */
7434       if (token->keyword == RID_ATTRIBUTE)
7435         {
7436           /* Parse the attributes.  */
7437           decl_specs->attributes
7438             = chainon (decl_specs->attributes,
7439                        cp_parser_attributes_opt (parser));
7440           continue;
7441         }
7442       /* Assume we will find a decl-specifier keyword.  */
7443       found_decl_spec = true;
7444       /* If the next token is an appropriate keyword, we can simply
7445          add it to the list.  */
7446       switch (token->keyword)
7447         {
7448           /* decl-specifier:
7449                friend  */
7450         case RID_FRIEND:
7451           if (!at_class_scope_p ())
7452             {
7453               error ("%<friend%> used outside of class");
7454               cp_lexer_purge_token (parser->lexer);
7455             }
7456           else
7457             {
7458               ++decl_specs->specs[(int) ds_friend];
7459               /* Consume the token.  */
7460               cp_lexer_consume_token (parser->lexer);
7461             }
7462           break;
7463
7464           /* function-specifier:
7465                inline
7466                virtual
7467                explicit  */
7468         case RID_INLINE:
7469         case RID_VIRTUAL:
7470         case RID_EXPLICIT:
7471           cp_parser_function_specifier_opt (parser, decl_specs);
7472           break;
7473
7474           /* decl-specifier:
7475                typedef  */
7476         case RID_TYPEDEF:
7477           ++decl_specs->specs[(int) ds_typedef];
7478           /* Consume the token.  */
7479           cp_lexer_consume_token (parser->lexer);
7480           /* A constructor declarator cannot appear in a typedef.  */
7481           constructor_possible_p = false;
7482           /* The "typedef" keyword can only occur in a declaration; we
7483              may as well commit at this point.  */
7484           cp_parser_commit_to_tentative_parse (parser);
7485           break;
7486
7487           /* storage-class-specifier:
7488                auto
7489                register
7490                static
7491                extern
7492                mutable
7493
7494              GNU Extension:
7495                thread  */
7496         case RID_AUTO:
7497         case RID_REGISTER:
7498         case RID_STATIC:
7499         case RID_EXTERN:
7500         case RID_MUTABLE:
7501           /* Consume the token.  */
7502           cp_lexer_consume_token (parser->lexer);
7503           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7504           break;
7505         case RID_THREAD:
7506           /* Consume the token.  */
7507           cp_lexer_consume_token (parser->lexer);
7508           ++decl_specs->specs[(int) ds_thread];
7509           break;
7510
7511         default:
7512           /* We did not yet find a decl-specifier yet.  */
7513           found_decl_spec = false;
7514           break;
7515         }
7516
7517       /* Constructors are a special case.  The `S' in `S()' is not a
7518          decl-specifier; it is the beginning of the declarator.  */
7519       constructor_p
7520         = (!found_decl_spec
7521            && constructor_possible_p
7522            && (cp_parser_constructor_declarator_p
7523                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7524
7525       /* If we don't have a DECL_SPEC yet, then we must be looking at
7526          a type-specifier.  */
7527       if (!found_decl_spec && !constructor_p)
7528         {
7529           int decl_spec_declares_class_or_enum;
7530           bool is_cv_qualifier;
7531           tree type_spec;
7532
7533           type_spec
7534             = cp_parser_type_specifier (parser, flags,
7535                                         decl_specs,
7536                                         /*is_declaration=*/true,
7537                                         &decl_spec_declares_class_or_enum,
7538                                         &is_cv_qualifier);
7539
7540           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7541
7542           /* If this type-specifier referenced a user-defined type
7543              (a typedef, class-name, etc.), then we can't allow any
7544              more such type-specifiers henceforth.
7545
7546              [dcl.spec]
7547
7548              The longest sequence of decl-specifiers that could
7549              possibly be a type name is taken as the
7550              decl-specifier-seq of a declaration.  The sequence shall
7551              be self-consistent as described below.
7552
7553              [dcl.type]
7554
7555              As a general rule, at most one type-specifier is allowed
7556              in the complete decl-specifier-seq of a declaration.  The
7557              only exceptions are the following:
7558
7559              -- const or volatile can be combined with any other
7560                 type-specifier.
7561
7562              -- signed or unsigned can be combined with char, long,
7563                 short, or int.
7564
7565              -- ..
7566
7567              Example:
7568
7569                typedef char* Pc;
7570                void g (const int Pc);
7571
7572              Here, Pc is *not* part of the decl-specifier seq; it's
7573              the declarator.  Therefore, once we see a type-specifier
7574              (other than a cv-qualifier), we forbid any additional
7575              user-defined types.  We *do* still allow things like `int
7576              int' to be considered a decl-specifier-seq, and issue the
7577              error message later.  */
7578           if (type_spec && !is_cv_qualifier)
7579             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7580           /* A constructor declarator cannot follow a type-specifier.  */
7581           if (type_spec)
7582             {
7583               constructor_possible_p = false;
7584               found_decl_spec = true;
7585             }
7586         }
7587
7588       /* If we still do not have a DECL_SPEC, then there are no more
7589          decl-specifiers.  */
7590       if (!found_decl_spec)
7591         break;
7592
7593       decl_specs->any_specifiers_p = true;
7594       /* After we see one decl-specifier, further decl-specifiers are
7595          always optional.  */
7596       flags |= CP_PARSER_FLAGS_OPTIONAL;
7597     }
7598
7599   cp_parser_check_decl_spec (decl_specs);
7600
7601   /* Don't allow a friend specifier with a class definition.  */
7602   if (decl_specs->specs[(int) ds_friend] != 0
7603       && (*declares_class_or_enum & 2))
7604     error ("class definition may not be declared a friend");
7605 }
7606
7607 /* Parse an (optional) storage-class-specifier.
7608
7609    storage-class-specifier:
7610      auto
7611      register
7612      static
7613      extern
7614      mutable
7615
7616    GNU Extension:
7617
7618    storage-class-specifier:
7619      thread
7620
7621    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7622
7623 static tree
7624 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7625 {
7626   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7627     {
7628     case RID_AUTO:
7629     case RID_REGISTER:
7630     case RID_STATIC:
7631     case RID_EXTERN:
7632     case RID_MUTABLE:
7633     case RID_THREAD:
7634       /* Consume the token.  */
7635       return cp_lexer_consume_token (parser->lexer)->value;
7636
7637     default:
7638       return NULL_TREE;
7639     }
7640 }
7641
7642 /* Parse an (optional) function-specifier.
7643
7644    function-specifier:
7645      inline
7646      virtual
7647      explicit
7648
7649    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7650    Updates DECL_SPECS, if it is non-NULL.  */
7651
7652 static tree
7653 cp_parser_function_specifier_opt (cp_parser* parser,
7654                                   cp_decl_specifier_seq *decl_specs)
7655 {
7656   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7657     {
7658     case RID_INLINE:
7659       if (decl_specs)
7660         ++decl_specs->specs[(int) ds_inline];
7661       break;
7662
7663     case RID_VIRTUAL:
7664       /* 14.5.2.3 [temp.mem]
7665
7666          A member function template shall not be virtual.  */
7667       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7668         error ("templates may not be %<virtual%>");
7669       else if (decl_specs)
7670         ++decl_specs->specs[(int) ds_virtual];
7671       break;
7672
7673     case RID_EXPLICIT:
7674       if (decl_specs)
7675         ++decl_specs->specs[(int) ds_explicit];
7676       break;
7677
7678     default:
7679       return NULL_TREE;
7680     }
7681
7682   /* Consume the token.  */
7683   return cp_lexer_consume_token (parser->lexer)->value;
7684 }
7685
7686 /* Parse a linkage-specification.
7687
7688    linkage-specification:
7689      extern string-literal { declaration-seq [opt] }
7690      extern string-literal declaration  */
7691
7692 static void
7693 cp_parser_linkage_specification (cp_parser* parser)
7694 {
7695   tree linkage;
7696
7697   /* Look for the `extern' keyword.  */
7698   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7699
7700   /* Look for the string-literal.  */
7701   linkage = cp_parser_string_literal (parser, false, false);
7702
7703   /* Transform the literal into an identifier.  If the literal is a
7704      wide-character string, or contains embedded NULs, then we can't
7705      handle it as the user wants.  */
7706   if (strlen (TREE_STRING_POINTER (linkage))
7707       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7708     {
7709       cp_parser_error (parser, "invalid linkage-specification");
7710       /* Assume C++ linkage.  */
7711       linkage = lang_name_cplusplus;
7712     }
7713   else
7714     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7715
7716   /* We're now using the new linkage.  */
7717   push_lang_context (linkage);
7718
7719   /* If the next token is a `{', then we're using the first
7720      production.  */
7721   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7722     {
7723       /* Consume the `{' token.  */
7724       cp_lexer_consume_token (parser->lexer);
7725       /* Parse the declarations.  */
7726       cp_parser_declaration_seq_opt (parser);
7727       /* Look for the closing `}'.  */
7728       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7729     }
7730   /* Otherwise, there's just one declaration.  */
7731   else
7732     {
7733       bool saved_in_unbraced_linkage_specification_p;
7734
7735       saved_in_unbraced_linkage_specification_p
7736         = parser->in_unbraced_linkage_specification_p;
7737       parser->in_unbraced_linkage_specification_p = true;
7738       cp_parser_declaration (parser);
7739       parser->in_unbraced_linkage_specification_p
7740         = saved_in_unbraced_linkage_specification_p;
7741     }
7742
7743   /* We're done with the linkage-specification.  */
7744   pop_lang_context ();
7745 }
7746
7747 /* Special member functions [gram.special] */
7748
7749 /* Parse a conversion-function-id.
7750
7751    conversion-function-id:
7752      operator conversion-type-id
7753
7754    Returns an IDENTIFIER_NODE representing the operator.  */
7755
7756 static tree
7757 cp_parser_conversion_function_id (cp_parser* parser)
7758 {
7759   tree type;
7760   tree saved_scope;
7761   tree saved_qualifying_scope;
7762   tree saved_object_scope;
7763   tree pushed_scope = NULL_TREE;
7764
7765   /* Look for the `operator' token.  */
7766   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7767     return error_mark_node;
7768   /* When we parse the conversion-type-id, the current scope will be
7769      reset.  However, we need that information in able to look up the
7770      conversion function later, so we save it here.  */
7771   saved_scope = parser->scope;
7772   saved_qualifying_scope = parser->qualifying_scope;
7773   saved_object_scope = parser->object_scope;
7774   /* We must enter the scope of the class so that the names of
7775      entities declared within the class are available in the
7776      conversion-type-id.  For example, consider:
7777
7778        struct S {
7779          typedef int I;
7780          operator I();
7781        };
7782
7783        S::operator I() { ... }
7784
7785      In order to see that `I' is a type-name in the definition, we
7786      must be in the scope of `S'.  */
7787   if (saved_scope)
7788     pushed_scope = push_scope (saved_scope);
7789   /* Parse the conversion-type-id.  */
7790   type = cp_parser_conversion_type_id (parser);
7791   /* Leave the scope of the class, if any.  */
7792   if (pushed_scope)
7793     pop_scope (pushed_scope);
7794   /* Restore the saved scope.  */
7795   parser->scope = saved_scope;
7796   parser->qualifying_scope = saved_qualifying_scope;
7797   parser->object_scope = saved_object_scope;
7798   /* If the TYPE is invalid, indicate failure.  */
7799   if (type == error_mark_node)
7800     return error_mark_node;
7801   return mangle_conv_op_name_for_type (type);
7802 }
7803
7804 /* Parse a conversion-type-id:
7805
7806    conversion-type-id:
7807      type-specifier-seq conversion-declarator [opt]
7808
7809    Returns the TYPE specified.  */
7810
7811 static tree
7812 cp_parser_conversion_type_id (cp_parser* parser)
7813 {
7814   tree attributes;
7815   cp_decl_specifier_seq type_specifiers;
7816   cp_declarator *declarator;
7817   tree type_specified;
7818
7819   /* Parse the attributes.  */
7820   attributes = cp_parser_attributes_opt (parser);
7821   /* Parse the type-specifiers.  */
7822   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7823                                 &type_specifiers);
7824   /* If that didn't work, stop.  */
7825   if (type_specifiers.type == error_mark_node)
7826     return error_mark_node;
7827   /* Parse the conversion-declarator.  */
7828   declarator = cp_parser_conversion_declarator_opt (parser);
7829
7830   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7831                                     /*initialized=*/0, &attributes);
7832   if (attributes)
7833     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7834   return type_specified;
7835 }
7836
7837 /* Parse an (optional) conversion-declarator.
7838
7839    conversion-declarator:
7840      ptr-operator conversion-declarator [opt]
7841
7842    */
7843
7844 static cp_declarator *
7845 cp_parser_conversion_declarator_opt (cp_parser* parser)
7846 {
7847   enum tree_code code;
7848   tree class_type;
7849   cp_cv_quals cv_quals;
7850
7851   /* We don't know if there's a ptr-operator next, or not.  */
7852   cp_parser_parse_tentatively (parser);
7853   /* Try the ptr-operator.  */
7854   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7855   /* If it worked, look for more conversion-declarators.  */
7856   if (cp_parser_parse_definitely (parser))
7857     {
7858       cp_declarator *declarator;
7859
7860       /* Parse another optional declarator.  */
7861       declarator = cp_parser_conversion_declarator_opt (parser);
7862
7863       /* Create the representation of the declarator.  */
7864       if (class_type)
7865         declarator = make_ptrmem_declarator (cv_quals, class_type,
7866                                              declarator);
7867       else if (code == INDIRECT_REF)
7868         declarator = make_pointer_declarator (cv_quals, declarator);
7869       else
7870         declarator = make_reference_declarator (cv_quals, declarator);
7871
7872       return declarator;
7873    }
7874
7875   return NULL;
7876 }
7877
7878 /* Parse an (optional) ctor-initializer.
7879
7880    ctor-initializer:
7881      : mem-initializer-list
7882
7883    Returns TRUE iff the ctor-initializer was actually present.  */
7884
7885 static bool
7886 cp_parser_ctor_initializer_opt (cp_parser* parser)
7887 {
7888   /* If the next token is not a `:', then there is no
7889      ctor-initializer.  */
7890   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7891     {
7892       /* Do default initialization of any bases and members.  */
7893       if (DECL_CONSTRUCTOR_P (current_function_decl))
7894         finish_mem_initializers (NULL_TREE);
7895
7896       return false;
7897     }
7898
7899   /* Consume the `:' token.  */
7900   cp_lexer_consume_token (parser->lexer);
7901   /* And the mem-initializer-list.  */
7902   cp_parser_mem_initializer_list (parser);
7903
7904   return true;
7905 }
7906
7907 /* Parse a mem-initializer-list.
7908
7909    mem-initializer-list:
7910      mem-initializer
7911      mem-initializer , mem-initializer-list  */
7912
7913 static void
7914 cp_parser_mem_initializer_list (cp_parser* parser)
7915 {
7916   tree mem_initializer_list = NULL_TREE;
7917
7918   /* Let the semantic analysis code know that we are starting the
7919      mem-initializer-list.  */
7920   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7921     error ("only constructors take base initializers");
7922
7923   /* Loop through the list.  */
7924   while (true)
7925     {
7926       tree mem_initializer;
7927
7928       /* Parse the mem-initializer.  */
7929       mem_initializer = cp_parser_mem_initializer (parser);
7930       /* Add it to the list, unless it was erroneous.  */
7931       if (mem_initializer != error_mark_node)
7932         {
7933           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7934           mem_initializer_list = mem_initializer;
7935         }
7936       /* If the next token is not a `,', we're done.  */
7937       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7938         break;
7939       /* Consume the `,' token.  */
7940       cp_lexer_consume_token (parser->lexer);
7941     }
7942
7943   /* Perform semantic analysis.  */
7944   if (DECL_CONSTRUCTOR_P (current_function_decl))
7945     finish_mem_initializers (mem_initializer_list);
7946 }
7947
7948 /* Parse a mem-initializer.
7949
7950    mem-initializer:
7951      mem-initializer-id ( expression-list [opt] )
7952
7953    GNU extension:
7954
7955    mem-initializer:
7956      ( expression-list [opt] )
7957
7958    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7959    class) or FIELD_DECL (for a non-static data member) to initialize;
7960    the TREE_VALUE is the expression-list.  An empty initialization
7961    list is represented by void_list_node.  */
7962
7963 static tree
7964 cp_parser_mem_initializer (cp_parser* parser)
7965 {
7966   tree mem_initializer_id;
7967   tree expression_list;
7968   tree member;
7969
7970   /* Find out what is being initialized.  */
7971   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7972     {
7973       pedwarn ("anachronistic old-style base class initializer");
7974       mem_initializer_id = NULL_TREE;
7975     }
7976   else
7977     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7978   member = expand_member_init (mem_initializer_id);
7979   if (member && !DECL_P (member))
7980     in_base_initializer = 1;
7981
7982   expression_list
7983     = cp_parser_parenthesized_expression_list (parser, false,
7984                                                /*cast_p=*/false,
7985                                                /*non_constant_p=*/NULL);
7986   if (expression_list == error_mark_node)
7987     return error_mark_node;
7988   if (!expression_list)
7989     expression_list = void_type_node;
7990
7991   in_base_initializer = 0;
7992
7993   return member ? build_tree_list (member, expression_list) : error_mark_node;
7994 }
7995
7996 /* Parse a mem-initializer-id.
7997
7998    mem-initializer-id:
7999      :: [opt] nested-name-specifier [opt] class-name
8000      identifier
8001
8002    Returns a TYPE indicating the class to be initializer for the first
8003    production.  Returns an IDENTIFIER_NODE indicating the data member
8004    to be initialized for the second production.  */
8005
8006 static tree
8007 cp_parser_mem_initializer_id (cp_parser* parser)
8008 {
8009   bool global_scope_p;
8010   bool nested_name_specifier_p;
8011   bool template_p = false;
8012   tree id;
8013
8014   /* `typename' is not allowed in this context ([temp.res]).  */
8015   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8016     {
8017       error ("keyword %<typename%> not allowed in this context (a qualified "
8018              "member initializer is implicitly a type)");
8019       cp_lexer_consume_token (parser->lexer);
8020     }
8021   /* Look for the optional `::' operator.  */
8022   global_scope_p
8023     = (cp_parser_global_scope_opt (parser,
8024                                    /*current_scope_valid_p=*/false)
8025        != NULL_TREE);
8026   /* Look for the optional nested-name-specifier.  The simplest way to
8027      implement:
8028
8029        [temp.res]
8030
8031        The keyword `typename' is not permitted in a base-specifier or
8032        mem-initializer; in these contexts a qualified name that
8033        depends on a template-parameter is implicitly assumed to be a
8034        type name.
8035
8036      is to assume that we have seen the `typename' keyword at this
8037      point.  */
8038   nested_name_specifier_p
8039     = (cp_parser_nested_name_specifier_opt (parser,
8040                                             /*typename_keyword_p=*/true,
8041                                             /*check_dependency_p=*/true,
8042                                             /*type_p=*/true,
8043                                             /*is_declaration=*/true)
8044        != NULL_TREE);
8045   if (nested_name_specifier_p)
8046     template_p = cp_parser_optional_template_keyword (parser);
8047   /* If there is a `::' operator or a nested-name-specifier, then we
8048      are definitely looking for a class-name.  */
8049   if (global_scope_p || nested_name_specifier_p)
8050     return cp_parser_class_name (parser,
8051                                  /*typename_keyword_p=*/true,
8052                                  /*template_keyword_p=*/template_p,
8053                                  none_type,
8054                                  /*check_dependency_p=*/true,
8055                                  /*class_head_p=*/false,
8056                                  /*is_declaration=*/true);
8057   /* Otherwise, we could also be looking for an ordinary identifier.  */
8058   cp_parser_parse_tentatively (parser);
8059   /* Try a class-name.  */
8060   id = cp_parser_class_name (parser,
8061                              /*typename_keyword_p=*/true,
8062                              /*template_keyword_p=*/false,
8063                              none_type,
8064                              /*check_dependency_p=*/true,
8065                              /*class_head_p=*/false,
8066                              /*is_declaration=*/true);
8067   /* If we found one, we're done.  */
8068   if (cp_parser_parse_definitely (parser))
8069     return id;
8070   /* Otherwise, look for an ordinary identifier.  */
8071   return cp_parser_identifier (parser);
8072 }
8073
8074 /* Overloading [gram.over] */
8075
8076 /* Parse an operator-function-id.
8077
8078    operator-function-id:
8079      operator operator
8080
8081    Returns an IDENTIFIER_NODE for the operator which is a
8082    human-readable spelling of the identifier, e.g., `operator +'.  */
8083
8084 static tree
8085 cp_parser_operator_function_id (cp_parser* parser)
8086 {
8087   /* Look for the `operator' keyword.  */
8088   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8089     return error_mark_node;
8090   /* And then the name of the operator itself.  */
8091   return cp_parser_operator (parser);
8092 }
8093
8094 /* Parse an operator.
8095
8096    operator:
8097      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8098      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8099      || ++ -- , ->* -> () []
8100
8101    GNU Extensions:
8102
8103    operator:
8104      <? >? <?= >?=
8105
8106    Returns an IDENTIFIER_NODE for the operator which is a
8107    human-readable spelling of the identifier, e.g., `operator +'.  */
8108
8109 static tree
8110 cp_parser_operator (cp_parser* parser)
8111 {
8112   tree id = NULL_TREE;
8113   cp_token *token;
8114
8115   /* Peek at the next token.  */
8116   token = cp_lexer_peek_token (parser->lexer);
8117   /* Figure out which operator we have.  */
8118   switch (token->type)
8119     {
8120     case CPP_KEYWORD:
8121       {
8122         enum tree_code op;
8123
8124         /* The keyword should be either `new' or `delete'.  */
8125         if (token->keyword == RID_NEW)
8126           op = NEW_EXPR;
8127         else if (token->keyword == RID_DELETE)
8128           op = DELETE_EXPR;
8129         else
8130           break;
8131
8132         /* Consume the `new' or `delete' token.  */
8133         cp_lexer_consume_token (parser->lexer);
8134
8135         /* Peek at the next token.  */
8136         token = cp_lexer_peek_token (parser->lexer);
8137         /* If it's a `[' token then this is the array variant of the
8138            operator.  */
8139         if (token->type == CPP_OPEN_SQUARE)
8140           {
8141             /* Consume the `[' token.  */
8142             cp_lexer_consume_token (parser->lexer);
8143             /* Look for the `]' token.  */
8144             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8145             id = ansi_opname (op == NEW_EXPR
8146                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8147           }
8148         /* Otherwise, we have the non-array variant.  */
8149         else
8150           id = ansi_opname (op);
8151
8152         return id;
8153       }
8154
8155     case CPP_PLUS:
8156       id = ansi_opname (PLUS_EXPR);
8157       break;
8158
8159     case CPP_MINUS:
8160       id = ansi_opname (MINUS_EXPR);
8161       break;
8162
8163     case CPP_MULT:
8164       id = ansi_opname (MULT_EXPR);
8165       break;
8166
8167     case CPP_DIV:
8168       id = ansi_opname (TRUNC_DIV_EXPR);
8169       break;
8170
8171     case CPP_MOD:
8172       id = ansi_opname (TRUNC_MOD_EXPR);
8173       break;
8174
8175     case CPP_XOR:
8176       id = ansi_opname (BIT_XOR_EXPR);
8177       break;
8178
8179     case CPP_AND:
8180       id = ansi_opname (BIT_AND_EXPR);
8181       break;
8182
8183     case CPP_OR:
8184       id = ansi_opname (BIT_IOR_EXPR);
8185       break;
8186
8187     case CPP_COMPL:
8188       id = ansi_opname (BIT_NOT_EXPR);
8189       break;
8190
8191     case CPP_NOT:
8192       id = ansi_opname (TRUTH_NOT_EXPR);
8193       break;
8194
8195     case CPP_EQ:
8196       id = ansi_assopname (NOP_EXPR);
8197       break;
8198
8199     case CPP_LESS:
8200       id = ansi_opname (LT_EXPR);
8201       break;
8202
8203     case CPP_GREATER:
8204       id = ansi_opname (GT_EXPR);
8205       break;
8206
8207     case CPP_PLUS_EQ:
8208       id = ansi_assopname (PLUS_EXPR);
8209       break;
8210
8211     case CPP_MINUS_EQ:
8212       id = ansi_assopname (MINUS_EXPR);
8213       break;
8214
8215     case CPP_MULT_EQ:
8216       id = ansi_assopname (MULT_EXPR);
8217       break;
8218
8219     case CPP_DIV_EQ:
8220       id = ansi_assopname (TRUNC_DIV_EXPR);
8221       break;
8222
8223     case CPP_MOD_EQ:
8224       id = ansi_assopname (TRUNC_MOD_EXPR);
8225       break;
8226
8227     case CPP_XOR_EQ:
8228       id = ansi_assopname (BIT_XOR_EXPR);
8229       break;
8230
8231     case CPP_AND_EQ:
8232       id = ansi_assopname (BIT_AND_EXPR);
8233       break;
8234
8235     case CPP_OR_EQ:
8236       id = ansi_assopname (BIT_IOR_EXPR);
8237       break;
8238
8239     case CPP_LSHIFT:
8240       id = ansi_opname (LSHIFT_EXPR);
8241       break;
8242
8243     case CPP_RSHIFT:
8244       id = ansi_opname (RSHIFT_EXPR);
8245       break;
8246
8247     case CPP_LSHIFT_EQ:
8248       id = ansi_assopname (LSHIFT_EXPR);
8249       break;
8250
8251     case CPP_RSHIFT_EQ:
8252       id = ansi_assopname (RSHIFT_EXPR);
8253       break;
8254
8255     case CPP_EQ_EQ:
8256       id = ansi_opname (EQ_EXPR);
8257       break;
8258
8259     case CPP_NOT_EQ:
8260       id = ansi_opname (NE_EXPR);
8261       break;
8262
8263     case CPP_LESS_EQ:
8264       id = ansi_opname (LE_EXPR);
8265       break;
8266
8267     case CPP_GREATER_EQ:
8268       id = ansi_opname (GE_EXPR);
8269       break;
8270
8271     case CPP_AND_AND:
8272       id = ansi_opname (TRUTH_ANDIF_EXPR);
8273       break;
8274
8275     case CPP_OR_OR:
8276       id = ansi_opname (TRUTH_ORIF_EXPR);
8277       break;
8278
8279     case CPP_PLUS_PLUS:
8280       id = ansi_opname (POSTINCREMENT_EXPR);
8281       break;
8282
8283     case CPP_MINUS_MINUS:
8284       id = ansi_opname (PREDECREMENT_EXPR);
8285       break;
8286
8287     case CPP_COMMA:
8288       id = ansi_opname (COMPOUND_EXPR);
8289       break;
8290
8291     case CPP_DEREF_STAR:
8292       id = ansi_opname (MEMBER_REF);
8293       break;
8294
8295     case CPP_DEREF:
8296       id = ansi_opname (COMPONENT_REF);
8297       break;
8298
8299     case CPP_OPEN_PAREN:
8300       /* Consume the `('.  */
8301       cp_lexer_consume_token (parser->lexer);
8302       /* Look for the matching `)'.  */
8303       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8304       return ansi_opname (CALL_EXPR);
8305
8306     case CPP_OPEN_SQUARE:
8307       /* Consume the `['.  */
8308       cp_lexer_consume_token (parser->lexer);
8309       /* Look for the matching `]'.  */
8310       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8311       return ansi_opname (ARRAY_REF);
8312
8313     default:
8314       /* Anything else is an error.  */
8315       break;
8316     }
8317
8318   /* If we have selected an identifier, we need to consume the
8319      operator token.  */
8320   if (id)
8321     cp_lexer_consume_token (parser->lexer);
8322   /* Otherwise, no valid operator name was present.  */
8323   else
8324     {
8325       cp_parser_error (parser, "expected operator");
8326       id = error_mark_node;
8327     }
8328
8329   return id;
8330 }
8331
8332 /* Parse a template-declaration.
8333
8334    template-declaration:
8335      export [opt] template < template-parameter-list > declaration
8336
8337    If MEMBER_P is TRUE, this template-declaration occurs within a
8338    class-specifier.
8339
8340    The grammar rule given by the standard isn't correct.  What
8341    is really meant is:
8342
8343    template-declaration:
8344      export [opt] template-parameter-list-seq
8345        decl-specifier-seq [opt] init-declarator [opt] ;
8346      export [opt] template-parameter-list-seq
8347        function-definition
8348
8349    template-parameter-list-seq:
8350      template-parameter-list-seq [opt]
8351      template < template-parameter-list >  */
8352
8353 static void
8354 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8355 {
8356   /* Check for `export'.  */
8357   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8358     {
8359       /* Consume the `export' token.  */
8360       cp_lexer_consume_token (parser->lexer);
8361       /* Warn that we do not support `export'.  */
8362       warning (0, "keyword %<export%> not implemented, and will be ignored");
8363     }
8364
8365   cp_parser_template_declaration_after_export (parser, member_p);
8366 }
8367
8368 /* Parse a template-parameter-list.
8369
8370    template-parameter-list:
8371      template-parameter
8372      template-parameter-list , template-parameter
8373
8374    Returns a TREE_LIST.  Each node represents a template parameter.
8375    The nodes are connected via their TREE_CHAINs.  */
8376
8377 static tree
8378 cp_parser_template_parameter_list (cp_parser* parser)
8379 {
8380   tree parameter_list = NULL_TREE;
8381
8382   begin_template_parm_list ();
8383   while (true)
8384     {
8385       tree parameter;
8386       cp_token *token;
8387       bool is_non_type;
8388
8389       /* Parse the template-parameter.  */
8390       parameter = cp_parser_template_parameter (parser, &is_non_type);
8391       /* Add it to the list.  */
8392       if (parameter != error_mark_node)
8393         parameter_list = process_template_parm (parameter_list,
8394                                                 parameter,
8395                                                 is_non_type);
8396       /* Peek at the next token.  */
8397       token = cp_lexer_peek_token (parser->lexer);
8398       /* If it's not a `,', we're done.  */
8399       if (token->type != CPP_COMMA)
8400         break;
8401       /* Otherwise, consume the `,' token.  */
8402       cp_lexer_consume_token (parser->lexer);
8403     }
8404
8405   return end_template_parm_list (parameter_list);
8406 }
8407
8408 /* Parse a template-parameter.
8409
8410    template-parameter:
8411      type-parameter
8412      parameter-declaration
8413
8414    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8415    the parameter.  The TREE_PURPOSE is the default value, if any.
8416    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8417    iff this parameter is a non-type parameter.  */
8418
8419 static tree
8420 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8421 {
8422   cp_token *token;
8423   cp_parameter_declarator *parameter_declarator;
8424   tree parm;
8425
8426   /* Assume it is a type parameter or a template parameter.  */
8427   *is_non_type = false;
8428   /* Peek at the next token.  */
8429   token = cp_lexer_peek_token (parser->lexer);
8430   /* If it is `class' or `template', we have a type-parameter.  */
8431   if (token->keyword == RID_TEMPLATE)
8432     return cp_parser_type_parameter (parser);
8433   /* If it is `class' or `typename' we do not know yet whether it is a
8434      type parameter or a non-type parameter.  Consider:
8435
8436        template <typename T, typename T::X X> ...
8437
8438      or:
8439
8440        template <class C, class D*> ...
8441
8442      Here, the first parameter is a type parameter, and the second is
8443      a non-type parameter.  We can tell by looking at the token after
8444      the identifier -- if it is a `,', `=', or `>' then we have a type
8445      parameter.  */
8446   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8447     {
8448       /* Peek at the token after `class' or `typename'.  */
8449       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8450       /* If it's an identifier, skip it.  */
8451       if (token->type == CPP_NAME)
8452         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8453       /* Now, see if the token looks like the end of a template
8454          parameter.  */
8455       if (token->type == CPP_COMMA
8456           || token->type == CPP_EQ
8457           || token->type == CPP_GREATER)
8458         return cp_parser_type_parameter (parser);
8459     }
8460
8461   /* Otherwise, it is a non-type parameter.
8462
8463      [temp.param]
8464
8465      When parsing a default template-argument for a non-type
8466      template-parameter, the first non-nested `>' is taken as the end
8467      of the template parameter-list rather than a greater-than
8468      operator.  */
8469   *is_non_type = true;
8470   parameter_declarator
8471      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8472                                         /*parenthesized_p=*/NULL);
8473   parm = grokdeclarator (parameter_declarator->declarator,
8474                          &parameter_declarator->decl_specifiers,
8475                          PARM, /*initialized=*/0,
8476                          /*attrlist=*/NULL);
8477   if (parm == error_mark_node)
8478     return error_mark_node;
8479   return build_tree_list (parameter_declarator->default_argument, parm);
8480 }
8481
8482 /* Parse a type-parameter.
8483
8484    type-parameter:
8485      class identifier [opt]
8486      class identifier [opt] = type-id
8487      typename identifier [opt]
8488      typename identifier [opt] = type-id
8489      template < template-parameter-list > class identifier [opt]
8490      template < template-parameter-list > class identifier [opt]
8491        = id-expression
8492
8493    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8494    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8495    the declaration of the parameter.  */
8496
8497 static tree
8498 cp_parser_type_parameter (cp_parser* parser)
8499 {
8500   cp_token *token;
8501   tree parameter;
8502
8503   /* Look for a keyword to tell us what kind of parameter this is.  */
8504   token = cp_parser_require (parser, CPP_KEYWORD,
8505                              "`class', `typename', or `template'");
8506   if (!token)
8507     return error_mark_node;
8508
8509   switch (token->keyword)
8510     {
8511     case RID_CLASS:
8512     case RID_TYPENAME:
8513       {
8514         tree identifier;
8515         tree default_argument;
8516
8517         /* If the next token is an identifier, then it names the
8518            parameter.  */
8519         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8520           identifier = cp_parser_identifier (parser);
8521         else
8522           identifier = NULL_TREE;
8523
8524         /* Create the parameter.  */
8525         parameter = finish_template_type_parm (class_type_node, identifier);
8526
8527         /* If the next token is an `=', we have a default argument.  */
8528         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8529           {
8530             /* Consume the `=' token.  */
8531             cp_lexer_consume_token (parser->lexer);
8532             /* Parse the default-argument.  */
8533             push_deferring_access_checks (dk_no_deferred);
8534             default_argument = cp_parser_type_id (parser);
8535             pop_deferring_access_checks ();
8536           }
8537         else
8538           default_argument = NULL_TREE;
8539
8540         /* Create the combined representation of the parameter and the
8541            default argument.  */
8542         parameter = build_tree_list (default_argument, parameter);
8543       }
8544       break;
8545
8546     case RID_TEMPLATE:
8547       {
8548         tree parameter_list;
8549         tree identifier;
8550         tree default_argument;
8551
8552         /* Look for the `<'.  */
8553         cp_parser_require (parser, CPP_LESS, "`<'");
8554         /* Parse the template-parameter-list.  */
8555         parameter_list = cp_parser_template_parameter_list (parser);
8556         /* Look for the `>'.  */
8557         cp_parser_require (parser, CPP_GREATER, "`>'");
8558         /* Look for the `class' keyword.  */
8559         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8560         /* If the next token is an `=', then there is a
8561            default-argument.  If the next token is a `>', we are at
8562            the end of the parameter-list.  If the next token is a `,',
8563            then we are at the end of this parameter.  */
8564         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8565             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8566             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8567           {
8568             identifier = cp_parser_identifier (parser);
8569             /* Treat invalid names as if the parameter were nameless.  */
8570             if (identifier == error_mark_node)
8571               identifier = NULL_TREE;
8572           }
8573         else
8574           identifier = NULL_TREE;
8575
8576         /* Create the template parameter.  */
8577         parameter = finish_template_template_parm (class_type_node,
8578                                                    identifier);
8579
8580         /* If the next token is an `=', then there is a
8581            default-argument.  */
8582         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8583           {
8584             bool is_template;
8585
8586             /* Consume the `='.  */
8587             cp_lexer_consume_token (parser->lexer);
8588             /* Parse the id-expression.  */
8589             push_deferring_access_checks (dk_no_deferred);
8590             default_argument
8591               = cp_parser_id_expression (parser,
8592                                          /*template_keyword_p=*/false,
8593                                          /*check_dependency_p=*/true,
8594                                          /*template_p=*/&is_template,
8595                                          /*declarator_p=*/false,
8596                                          /*optional_p=*/false);
8597             if (TREE_CODE (default_argument) == TYPE_DECL)
8598               /* If the id-expression was a template-id that refers to
8599                  a template-class, we already have the declaration here,
8600                  so no further lookup is needed.  */
8601                  ;
8602             else
8603               /* Look up the name.  */
8604               default_argument
8605                 = cp_parser_lookup_name (parser, default_argument,
8606                                          none_type,
8607                                          /*is_template=*/is_template,
8608                                          /*is_namespace=*/false,
8609                                          /*check_dependency=*/true,
8610                                          /*ambiguous_decls=*/NULL);
8611             /* See if the default argument is valid.  */
8612             default_argument
8613               = check_template_template_default_arg (default_argument);
8614             pop_deferring_access_checks ();
8615           }
8616         else
8617           default_argument = NULL_TREE;
8618
8619         /* Create the combined representation of the parameter and the
8620            default argument.  */
8621         parameter = build_tree_list (default_argument, parameter);
8622       }
8623       break;
8624
8625     default:
8626       gcc_unreachable ();
8627       break;
8628     }
8629
8630   return parameter;
8631 }
8632
8633 /* Parse a template-id.
8634
8635    template-id:
8636      template-name < template-argument-list [opt] >
8637
8638    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8639    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8640    returned.  Otherwise, if the template-name names a function, or set
8641    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8642    names a class, returns a TYPE_DECL for the specialization.
8643
8644    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8645    uninstantiated templates.  */
8646
8647 static tree
8648 cp_parser_template_id (cp_parser *parser,
8649                        bool template_keyword_p,
8650                        bool check_dependency_p,
8651                        bool is_declaration)
8652 {
8653   tree template;
8654   tree arguments;
8655   tree template_id;
8656   cp_token_position start_of_id = 0;
8657   tree access_check = NULL_TREE;
8658   cp_token *next_token, *next_token_2;
8659   bool is_identifier;
8660
8661   /* If the next token corresponds to a template-id, there is no need
8662      to reparse it.  */
8663   next_token = cp_lexer_peek_token (parser->lexer);
8664   if (next_token->type == CPP_TEMPLATE_ID)
8665     {
8666       tree value;
8667       tree check;
8668
8669       /* Get the stored value.  */
8670       value = cp_lexer_consume_token (parser->lexer)->value;
8671       /* Perform any access checks that were deferred.  */
8672       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8673         perform_or_defer_access_check (TREE_PURPOSE (check),
8674                                        TREE_VALUE (check));
8675       /* Return the stored value.  */
8676       return TREE_VALUE (value);
8677     }
8678
8679   /* Avoid performing name lookup if there is no possibility of
8680      finding a template-id.  */
8681   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8682       || (next_token->type == CPP_NAME
8683           && !cp_parser_nth_token_starts_template_argument_list_p
8684                (parser, 2)))
8685     {
8686       cp_parser_error (parser, "expected template-id");
8687       return error_mark_node;
8688     }
8689
8690   /* Remember where the template-id starts.  */
8691   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8692     start_of_id = cp_lexer_token_position (parser->lexer, false);
8693
8694   push_deferring_access_checks (dk_deferred);
8695
8696   /* Parse the template-name.  */
8697   is_identifier = false;
8698   template = cp_parser_template_name (parser, template_keyword_p,
8699                                       check_dependency_p,
8700                                       is_declaration,
8701                                       &is_identifier);
8702   if (template == error_mark_node || is_identifier)
8703     {
8704       pop_deferring_access_checks ();
8705       return template;
8706     }
8707
8708   /* If we find the sequence `[:' after a template-name, it's probably
8709      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8710      parse correctly the argument list.  */
8711   next_token = cp_lexer_peek_token (parser->lexer);
8712   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8713   if (next_token->type == CPP_OPEN_SQUARE
8714       && next_token->flags & DIGRAPH
8715       && next_token_2->type == CPP_COLON
8716       && !(next_token_2->flags & PREV_WHITE))
8717     {
8718       cp_parser_parse_tentatively (parser);
8719       /* Change `:' into `::'.  */
8720       next_token_2->type = CPP_SCOPE;
8721       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8722          CPP_LESS.  */
8723       cp_lexer_consume_token (parser->lexer);
8724       /* Parse the arguments.  */
8725       arguments = cp_parser_enclosed_template_argument_list (parser);
8726       if (!cp_parser_parse_definitely (parser))
8727         {
8728           /* If we couldn't parse an argument list, then we revert our changes
8729              and return simply an error. Maybe this is not a template-id
8730              after all.  */
8731           next_token_2->type = CPP_COLON;
8732           cp_parser_error (parser, "expected %<<%>");
8733           pop_deferring_access_checks ();
8734           return error_mark_node;
8735         }
8736       /* Otherwise, emit an error about the invalid digraph, but continue
8737          parsing because we got our argument list.  */
8738       pedwarn ("%<<::%> cannot begin a template-argument list");
8739       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8740               "between %<<%> and %<::%>");
8741       if (!flag_permissive)
8742         {
8743           static bool hint;
8744           if (!hint)
8745             {
8746               inform ("(if you use -fpermissive G++ will accept your code)");
8747               hint = true;
8748             }
8749         }
8750     }
8751   else
8752     {
8753       /* Look for the `<' that starts the template-argument-list.  */
8754       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8755         {
8756           pop_deferring_access_checks ();
8757           return error_mark_node;
8758         }
8759       /* Parse the arguments.  */
8760       arguments = cp_parser_enclosed_template_argument_list (parser);
8761     }
8762
8763   /* Build a representation of the specialization.  */
8764   if (TREE_CODE (template) == IDENTIFIER_NODE)
8765     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8766   else if (DECL_CLASS_TEMPLATE_P (template)
8767            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8768     {
8769       bool entering_scope;
8770       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8771          template (rather than some instantiation thereof) only if
8772          is not nested within some other construct.  For example, in
8773          "template <typename T> void f(T) { A<T>::", A<T> is just an
8774          instantiation of A.  */
8775       entering_scope = (template_parm_scope_p ()
8776                         && cp_lexer_next_token_is (parser->lexer,
8777                                                    CPP_SCOPE));
8778       template_id
8779         = finish_template_type (template, arguments, entering_scope);
8780     }
8781   else
8782     {
8783       /* If it's not a class-template or a template-template, it should be
8784          a function-template.  */
8785       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8786                    || TREE_CODE (template) == OVERLOAD
8787                    || BASELINK_P (template)));
8788
8789       template_id = lookup_template_function (template, arguments);
8790     }
8791
8792   /* Retrieve any deferred checks.  Do not pop this access checks yet
8793      so the memory will not be reclaimed during token replacing below.  */
8794   access_check = get_deferred_access_checks ();
8795
8796   /* If parsing tentatively, replace the sequence of tokens that makes
8797      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8798      should we re-parse the token stream, we will not have to repeat
8799      the effort required to do the parse, nor will we issue duplicate
8800      error messages about problems during instantiation of the
8801      template.  */
8802   if (start_of_id)
8803     {
8804       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8805
8806       /* Reset the contents of the START_OF_ID token.  */
8807       token->type = CPP_TEMPLATE_ID;
8808       token->value = build_tree_list (access_check, template_id);
8809       token->keyword = RID_MAX;
8810
8811       /* Purge all subsequent tokens.  */
8812       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8813
8814       /* ??? Can we actually assume that, if template_id ==
8815          error_mark_node, we will have issued a diagnostic to the
8816          user, as opposed to simply marking the tentative parse as
8817          failed?  */
8818       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8819         error ("parse error in template argument list");
8820     }
8821
8822   pop_deferring_access_checks ();
8823   return template_id;
8824 }
8825
8826 /* Parse a template-name.
8827
8828    template-name:
8829      identifier
8830
8831    The standard should actually say:
8832
8833    template-name:
8834      identifier
8835      operator-function-id
8836
8837    A defect report has been filed about this issue.
8838
8839    A conversion-function-id cannot be a template name because they cannot
8840    be part of a template-id. In fact, looking at this code:
8841
8842    a.operator K<int>()
8843
8844    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8845    It is impossible to call a templated conversion-function-id with an
8846    explicit argument list, since the only allowed template parameter is
8847    the type to which it is converting.
8848
8849    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8850    `template' keyword, in a construction like:
8851
8852      T::template f<3>()
8853
8854    In that case `f' is taken to be a template-name, even though there
8855    is no way of knowing for sure.
8856
8857    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8858    name refers to a set of overloaded functions, at least one of which
8859    is a template, or an IDENTIFIER_NODE with the name of the template,
8860    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8861    names are looked up inside uninstantiated templates.  */
8862
8863 static tree
8864 cp_parser_template_name (cp_parser* parser,
8865                          bool template_keyword_p,
8866                          bool check_dependency_p,
8867                          bool is_declaration,
8868                          bool *is_identifier)
8869 {
8870   tree identifier;
8871   tree decl;
8872   tree fns;
8873
8874   /* If the next token is `operator', then we have either an
8875      operator-function-id or a conversion-function-id.  */
8876   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8877     {
8878       /* We don't know whether we're looking at an
8879          operator-function-id or a conversion-function-id.  */
8880       cp_parser_parse_tentatively (parser);
8881       /* Try an operator-function-id.  */
8882       identifier = cp_parser_operator_function_id (parser);
8883       /* If that didn't work, try a conversion-function-id.  */
8884       if (!cp_parser_parse_definitely (parser))
8885         {
8886           cp_parser_error (parser, "expected template-name");
8887           return error_mark_node;
8888         }
8889     }
8890   /* Look for the identifier.  */
8891   else
8892     identifier = cp_parser_identifier (parser);
8893
8894   /* If we didn't find an identifier, we don't have a template-id.  */
8895   if (identifier == error_mark_node)
8896     return error_mark_node;
8897
8898   /* If the name immediately followed the `template' keyword, then it
8899      is a template-name.  However, if the next token is not `<', then
8900      we do not treat it as a template-name, since it is not being used
8901      as part of a template-id.  This enables us to handle constructs
8902      like:
8903
8904        template <typename T> struct S { S(); };
8905        template <typename T> S<T>::S();
8906
8907      correctly.  We would treat `S' as a template -- if it were `S<T>'
8908      -- but we do not if there is no `<'.  */
8909
8910   if (processing_template_decl
8911       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8912     {
8913       /* In a declaration, in a dependent context, we pretend that the
8914          "template" keyword was present in order to improve error
8915          recovery.  For example, given:
8916
8917            template <typename T> void f(T::X<int>);
8918
8919          we want to treat "X<int>" as a template-id.  */
8920       if (is_declaration
8921           && !template_keyword_p
8922           && parser->scope && TYPE_P (parser->scope)
8923           && check_dependency_p
8924           && dependent_type_p (parser->scope)
8925           /* Do not do this for dtors (or ctors), since they never
8926              need the template keyword before their name.  */
8927           && !constructor_name_p (identifier, parser->scope))
8928         {
8929           cp_token_position start = 0;
8930
8931           /* Explain what went wrong.  */
8932           error ("non-template %qD used as template", identifier);
8933           inform ("use %<%T::template %D%> to indicate that it is a template",
8934                   parser->scope, identifier);
8935           /* If parsing tentatively, find the location of the "<" token.  */
8936           if (cp_parser_simulate_error (parser))
8937             start = cp_lexer_token_position (parser->lexer, true);
8938           /* Parse the template arguments so that we can issue error
8939              messages about them.  */
8940           cp_lexer_consume_token (parser->lexer);
8941           cp_parser_enclosed_template_argument_list (parser);
8942           /* Skip tokens until we find a good place from which to
8943              continue parsing.  */
8944           cp_parser_skip_to_closing_parenthesis (parser,
8945                                                  /*recovering=*/true,
8946                                                  /*or_comma=*/true,
8947                                                  /*consume_paren=*/false);
8948           /* If parsing tentatively, permanently remove the
8949              template argument list.  That will prevent duplicate
8950              error messages from being issued about the missing
8951              "template" keyword.  */
8952           if (start)
8953             cp_lexer_purge_tokens_after (parser->lexer, start);
8954           if (is_identifier)
8955             *is_identifier = true;
8956           return identifier;
8957         }
8958
8959       /* If the "template" keyword is present, then there is generally
8960          no point in doing name-lookup, so we just return IDENTIFIER.
8961          But, if the qualifying scope is non-dependent then we can
8962          (and must) do name-lookup normally.  */
8963       if (template_keyword_p
8964           && (!parser->scope
8965               || (TYPE_P (parser->scope)
8966                   && dependent_type_p (parser->scope))))
8967         return identifier;
8968     }
8969
8970   /* Look up the name.  */
8971   decl = cp_parser_lookup_name (parser, identifier,
8972                                 none_type,
8973                                 /*is_template=*/false,
8974                                 /*is_namespace=*/false,
8975                                 check_dependency_p,
8976                                 /*ambiguous_decls=*/NULL);
8977   decl = maybe_get_template_decl_from_type_decl (decl);
8978
8979   /* If DECL is a template, then the name was a template-name.  */
8980   if (TREE_CODE (decl) == TEMPLATE_DECL)
8981     ;
8982   else
8983     {
8984       tree fn = NULL_TREE;
8985
8986       /* The standard does not explicitly indicate whether a name that
8987          names a set of overloaded declarations, some of which are
8988          templates, is a template-name.  However, such a name should
8989          be a template-name; otherwise, there is no way to form a
8990          template-id for the overloaded templates.  */
8991       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8992       if (TREE_CODE (fns) == OVERLOAD)
8993         for (fn = fns; fn; fn = OVL_NEXT (fn))
8994           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8995             break;
8996
8997       if (!fn)
8998         {
8999           /* The name does not name a template.  */
9000           cp_parser_error (parser, "expected template-name");
9001           return error_mark_node;
9002         }
9003     }
9004
9005   /* If DECL is dependent, and refers to a function, then just return
9006      its name; we will look it up again during template instantiation.  */
9007   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9008     {
9009       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9010       if (TYPE_P (scope) && dependent_type_p (scope))
9011         return identifier;
9012     }
9013
9014   return decl;
9015 }
9016
9017 /* Parse a template-argument-list.
9018
9019    template-argument-list:
9020      template-argument
9021      template-argument-list , template-argument
9022
9023    Returns a TREE_VEC containing the arguments.  */
9024
9025 static tree
9026 cp_parser_template_argument_list (cp_parser* parser)
9027 {
9028   tree fixed_args[10];
9029   unsigned n_args = 0;
9030   unsigned alloced = 10;
9031   tree *arg_ary = fixed_args;
9032   tree vec;
9033   bool saved_in_template_argument_list_p;
9034   bool saved_ice_p;
9035   bool saved_non_ice_p;
9036
9037   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9038   parser->in_template_argument_list_p = true;
9039   /* Even if the template-id appears in an integral
9040      constant-expression, the contents of the argument list do
9041      not.  */
9042   saved_ice_p = parser->integral_constant_expression_p;
9043   parser->integral_constant_expression_p = false;
9044   saved_non_ice_p = parser->non_integral_constant_expression_p;
9045   parser->non_integral_constant_expression_p = false;
9046   /* Parse the arguments.  */
9047   do
9048     {
9049       tree argument;
9050
9051       if (n_args)
9052         /* Consume the comma.  */
9053         cp_lexer_consume_token (parser->lexer);
9054
9055       /* Parse the template-argument.  */
9056       argument = cp_parser_template_argument (parser);
9057       if (n_args == alloced)
9058         {
9059           alloced *= 2;
9060
9061           if (arg_ary == fixed_args)
9062             {
9063               arg_ary = XNEWVEC (tree, alloced);
9064               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9065             }
9066           else
9067             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9068         }
9069       arg_ary[n_args++] = argument;
9070     }
9071   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9072
9073   vec = make_tree_vec (n_args);
9074
9075   while (n_args--)
9076     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9077
9078   if (arg_ary != fixed_args)
9079     free (arg_ary);
9080   parser->non_integral_constant_expression_p = saved_non_ice_p;
9081   parser->integral_constant_expression_p = saved_ice_p;
9082   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9083   return vec;
9084 }
9085
9086 /* Parse a template-argument.
9087
9088    template-argument:
9089      assignment-expression
9090      type-id
9091      id-expression
9092
9093    The representation is that of an assignment-expression, type-id, or
9094    id-expression -- except that the qualified id-expression is
9095    evaluated, so that the value returned is either a DECL or an
9096    OVERLOAD.
9097
9098    Although the standard says "assignment-expression", it forbids
9099    throw-expressions or assignments in the template argument.
9100    Therefore, we use "conditional-expression" instead.  */
9101
9102 static tree
9103 cp_parser_template_argument (cp_parser* parser)
9104 {
9105   tree argument;
9106   bool template_p;
9107   bool address_p;
9108   bool maybe_type_id = false;
9109   cp_token *token;
9110   cp_id_kind idk;
9111
9112   /* There's really no way to know what we're looking at, so we just
9113      try each alternative in order.
9114
9115        [temp.arg]
9116
9117        In a template-argument, an ambiguity between a type-id and an
9118        expression is resolved to a type-id, regardless of the form of
9119        the corresponding template-parameter.
9120
9121      Therefore, we try a type-id first.  */
9122   cp_parser_parse_tentatively (parser);
9123   argument = cp_parser_type_id (parser);
9124   /* If there was no error parsing the type-id but the next token is a '>>',
9125      we probably found a typo for '> >'. But there are type-id which are
9126      also valid expressions. For instance:
9127
9128      struct X { int operator >> (int); };
9129      template <int V> struct Foo {};
9130      Foo<X () >> 5> r;
9131
9132      Here 'X()' is a valid type-id of a function type, but the user just
9133      wanted to write the expression "X() >> 5". Thus, we remember that we
9134      found a valid type-id, but we still try to parse the argument as an
9135      expression to see what happens.  */
9136   if (!cp_parser_error_occurred (parser)
9137       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9138     {
9139       maybe_type_id = true;
9140       cp_parser_abort_tentative_parse (parser);
9141     }
9142   else
9143     {
9144       /* If the next token isn't a `,' or a `>', then this argument wasn't
9145       really finished. This means that the argument is not a valid
9146       type-id.  */
9147       if (!cp_parser_next_token_ends_template_argument_p (parser))
9148         cp_parser_error (parser, "expected template-argument");
9149       /* If that worked, we're done.  */
9150       if (cp_parser_parse_definitely (parser))
9151         return argument;
9152     }
9153   /* We're still not sure what the argument will be.  */
9154   cp_parser_parse_tentatively (parser);
9155   /* Try a template.  */
9156   argument = cp_parser_id_expression (parser,
9157                                       /*template_keyword_p=*/false,
9158                                       /*check_dependency_p=*/true,
9159                                       &template_p,
9160                                       /*declarator_p=*/false,
9161                                       /*optional_p=*/false);
9162   /* If the next token isn't a `,' or a `>', then this argument wasn't
9163      really finished.  */
9164   if (!cp_parser_next_token_ends_template_argument_p (parser))
9165     cp_parser_error (parser, "expected template-argument");
9166   if (!cp_parser_error_occurred (parser))
9167     {
9168       /* Figure out what is being referred to.  If the id-expression
9169          was for a class template specialization, then we will have a
9170          TYPE_DECL at this point.  There is no need to do name lookup
9171          at this point in that case.  */
9172       if (TREE_CODE (argument) != TYPE_DECL)
9173         argument = cp_parser_lookup_name (parser, argument,
9174                                           none_type,
9175                                           /*is_template=*/template_p,
9176                                           /*is_namespace=*/false,
9177                                           /*check_dependency=*/true,
9178                                           /*ambiguous_decls=*/NULL);
9179       if (TREE_CODE (argument) != TEMPLATE_DECL
9180           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9181         cp_parser_error (parser, "expected template-name");
9182     }
9183   if (cp_parser_parse_definitely (parser))
9184     return argument;
9185   /* It must be a non-type argument.  There permitted cases are given
9186      in [temp.arg.nontype]:
9187
9188      -- an integral constant-expression of integral or enumeration
9189         type; or
9190
9191      -- the name of a non-type template-parameter; or
9192
9193      -- the name of an object or function with external linkage...
9194
9195      -- the address of an object or function with external linkage...
9196
9197      -- a pointer to member...  */
9198   /* Look for a non-type template parameter.  */
9199   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9200     {
9201       cp_parser_parse_tentatively (parser);
9202       argument = cp_parser_primary_expression (parser,
9203                                                /*adress_p=*/false,
9204                                                /*cast_p=*/false,
9205                                                /*template_arg_p=*/true,
9206                                                &idk);
9207       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9208           || !cp_parser_next_token_ends_template_argument_p (parser))
9209         cp_parser_simulate_error (parser);
9210       if (cp_parser_parse_definitely (parser))
9211         return argument;
9212     }
9213
9214   /* If the next token is "&", the argument must be the address of an
9215      object or function with external linkage.  */
9216   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9217   if (address_p)
9218     cp_lexer_consume_token (parser->lexer);
9219   /* See if we might have an id-expression.  */
9220   token = cp_lexer_peek_token (parser->lexer);
9221   if (token->type == CPP_NAME
9222       || token->keyword == RID_OPERATOR
9223       || token->type == CPP_SCOPE
9224       || token->type == CPP_TEMPLATE_ID
9225       || token->type == CPP_NESTED_NAME_SPECIFIER)
9226     {
9227       cp_parser_parse_tentatively (parser);
9228       argument = cp_parser_primary_expression (parser,
9229                                                address_p,
9230                                                /*cast_p=*/false,
9231                                                /*template_arg_p=*/true,
9232                                                &idk);
9233       if (cp_parser_error_occurred (parser)
9234           || !cp_parser_next_token_ends_template_argument_p (parser))
9235         cp_parser_abort_tentative_parse (parser);
9236       else
9237         {
9238           if (TREE_CODE (argument) == INDIRECT_REF)
9239             {
9240               gcc_assert (REFERENCE_REF_P (argument));
9241               argument = TREE_OPERAND (argument, 0);
9242             }
9243
9244           if (TREE_CODE (argument) == BASELINK)
9245             /* We don't need the information about what class was used
9246                to name the overloaded functions.  */
9247             argument = BASELINK_FUNCTIONS (argument);
9248
9249           if (TREE_CODE (argument) == VAR_DECL)
9250             {
9251               /* A variable without external linkage might still be a
9252                  valid constant-expression, so no error is issued here
9253                  if the external-linkage check fails.  */
9254               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9255                 cp_parser_simulate_error (parser);
9256             }
9257           else if (is_overloaded_fn (argument))
9258             /* All overloaded functions are allowed; if the external
9259                linkage test does not pass, an error will be issued
9260                later.  */
9261             ;
9262           else if (address_p
9263                    && (TREE_CODE (argument) == OFFSET_REF
9264                        || TREE_CODE (argument) == SCOPE_REF))
9265             /* A pointer-to-member.  */
9266             ;
9267           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9268             ;
9269           else
9270             cp_parser_simulate_error (parser);
9271
9272           if (cp_parser_parse_definitely (parser))
9273             {
9274               if (address_p)
9275                 argument = build_x_unary_op (ADDR_EXPR, argument);
9276               return argument;
9277             }
9278         }
9279     }
9280   /* If the argument started with "&", there are no other valid
9281      alternatives at this point.  */
9282   if (address_p)
9283     {
9284       cp_parser_error (parser, "invalid non-type template argument");
9285       return error_mark_node;
9286     }
9287
9288   /* If the argument wasn't successfully parsed as a type-id followed
9289      by '>>', the argument can only be a constant expression now.
9290      Otherwise, we try parsing the constant-expression tentatively,
9291      because the argument could really be a type-id.  */
9292   if (maybe_type_id)
9293     cp_parser_parse_tentatively (parser);
9294   argument = cp_parser_constant_expression (parser,
9295                                             /*allow_non_constant_p=*/false,
9296                                             /*non_constant_p=*/NULL);
9297   argument = fold_non_dependent_expr (argument);
9298   if (!maybe_type_id)
9299     return argument;
9300   if (!cp_parser_next_token_ends_template_argument_p (parser))
9301     cp_parser_error (parser, "expected template-argument");
9302   if (cp_parser_parse_definitely (parser))
9303     return argument;
9304   /* We did our best to parse the argument as a non type-id, but that
9305      was the only alternative that matched (albeit with a '>' after
9306      it). We can assume it's just a typo from the user, and a
9307      diagnostic will then be issued.  */
9308   return cp_parser_type_id (parser);
9309 }
9310
9311 /* Parse an explicit-instantiation.
9312
9313    explicit-instantiation:
9314      template declaration
9315
9316    Although the standard says `declaration', what it really means is:
9317
9318    explicit-instantiation:
9319      template decl-specifier-seq [opt] declarator [opt] ;
9320
9321    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9322    supposed to be allowed.  A defect report has been filed about this
9323    issue.
9324
9325    GNU Extension:
9326
9327    explicit-instantiation:
9328      storage-class-specifier template
9329        decl-specifier-seq [opt] declarator [opt] ;
9330      function-specifier template
9331        decl-specifier-seq [opt] declarator [opt] ;  */
9332
9333 static void
9334 cp_parser_explicit_instantiation (cp_parser* parser)
9335 {
9336   int declares_class_or_enum;
9337   cp_decl_specifier_seq decl_specifiers;
9338   tree extension_specifier = NULL_TREE;
9339
9340   /* Look for an (optional) storage-class-specifier or
9341      function-specifier.  */
9342   if (cp_parser_allow_gnu_extensions_p (parser))
9343     {
9344       extension_specifier
9345         = cp_parser_storage_class_specifier_opt (parser);
9346       if (!extension_specifier)
9347         extension_specifier
9348           = cp_parser_function_specifier_opt (parser,
9349                                               /*decl_specs=*/NULL);
9350     }
9351
9352   /* Look for the `template' keyword.  */
9353   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9354   /* Let the front end know that we are processing an explicit
9355      instantiation.  */
9356   begin_explicit_instantiation ();
9357   /* [temp.explicit] says that we are supposed to ignore access
9358      control while processing explicit instantiation directives.  */
9359   push_deferring_access_checks (dk_no_check);
9360   /* Parse a decl-specifier-seq.  */
9361   cp_parser_decl_specifier_seq (parser,
9362                                 CP_PARSER_FLAGS_OPTIONAL,
9363                                 &decl_specifiers,
9364                                 &declares_class_or_enum);
9365   /* If there was exactly one decl-specifier, and it declared a class,
9366      and there's no declarator, then we have an explicit type
9367      instantiation.  */
9368   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9369     {
9370       tree type;
9371
9372       type = check_tag_decl (&decl_specifiers);
9373       /* Turn access control back on for names used during
9374          template instantiation.  */
9375       pop_deferring_access_checks ();
9376       if (type)
9377         do_type_instantiation (type, extension_specifier,
9378                                /*complain=*/tf_error);
9379     }
9380   else
9381     {
9382       cp_declarator *declarator;
9383       tree decl;
9384
9385       /* Parse the declarator.  */
9386       declarator
9387         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9388                                 /*ctor_dtor_or_conv_p=*/NULL,
9389                                 /*parenthesized_p=*/NULL,
9390                                 /*member_p=*/false);
9391       if (declares_class_or_enum & 2)
9392         cp_parser_check_for_definition_in_return_type (declarator,
9393                                                        decl_specifiers.type);
9394       if (declarator != cp_error_declarator)
9395         {
9396           decl = grokdeclarator (declarator, &decl_specifiers,
9397                                  NORMAL, 0, &decl_specifiers.attributes);
9398           /* Turn access control back on for names used during
9399              template instantiation.  */
9400           pop_deferring_access_checks ();
9401           /* Do the explicit instantiation.  */
9402           do_decl_instantiation (decl, extension_specifier);
9403         }
9404       else
9405         {
9406           pop_deferring_access_checks ();
9407           /* Skip the body of the explicit instantiation.  */
9408           cp_parser_skip_to_end_of_statement (parser);
9409         }
9410     }
9411   /* We're done with the instantiation.  */
9412   end_explicit_instantiation ();
9413
9414   cp_parser_consume_semicolon_at_end_of_statement (parser);
9415 }
9416
9417 /* Parse an explicit-specialization.
9418
9419    explicit-specialization:
9420      template < > declaration
9421
9422    Although the standard says `declaration', what it really means is:
9423
9424    explicit-specialization:
9425      template <> decl-specifier [opt] init-declarator [opt] ;
9426      template <> function-definition
9427      template <> explicit-specialization
9428      template <> template-declaration  */
9429
9430 static void
9431 cp_parser_explicit_specialization (cp_parser* parser)
9432 {
9433   bool need_lang_pop;
9434   /* Look for the `template' keyword.  */
9435   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9436   /* Look for the `<'.  */
9437   cp_parser_require (parser, CPP_LESS, "`<'");
9438   /* Look for the `>'.  */
9439   cp_parser_require (parser, CPP_GREATER, "`>'");
9440   /* We have processed another parameter list.  */
9441   ++parser->num_template_parameter_lists;
9442   /* [temp]
9443
9444      A template ... explicit specialization ... shall not have C
9445      linkage.  */
9446   if (current_lang_name == lang_name_c)
9447     {
9448       error ("template specialization with C linkage");
9449       /* Give it C++ linkage to avoid confusing other parts of the
9450          front end.  */
9451       push_lang_context (lang_name_cplusplus);
9452       need_lang_pop = true;
9453     }
9454   else
9455     need_lang_pop = false;
9456   /* Let the front end know that we are beginning a specialization.  */
9457   begin_specialization ();
9458   /* If the next keyword is `template', we need to figure out whether
9459      or not we're looking a template-declaration.  */
9460   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9461     {
9462       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9463           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9464         cp_parser_template_declaration_after_export (parser,
9465                                                      /*member_p=*/false);
9466       else
9467         cp_parser_explicit_specialization (parser);
9468     }
9469   else
9470     /* Parse the dependent declaration.  */
9471     cp_parser_single_declaration (parser,
9472                                   /*checks=*/NULL_TREE,
9473                                   /*member_p=*/false,
9474                                   /*friend_p=*/NULL);
9475   /* We're done with the specialization.  */
9476   end_specialization ();
9477   /* For the erroneous case of a template with C linkage, we pushed an
9478      implicit C++ linkage scope; exit that scope now.  */
9479   if (need_lang_pop)
9480     pop_lang_context ();
9481   /* We're done with this parameter list.  */
9482   --parser->num_template_parameter_lists;
9483 }
9484
9485 /* Parse a type-specifier.
9486
9487    type-specifier:
9488      simple-type-specifier
9489      class-specifier
9490      enum-specifier
9491      elaborated-type-specifier
9492      cv-qualifier
9493
9494    GNU Extension:
9495
9496    type-specifier:
9497      __complex__
9498
9499    Returns a representation of the type-specifier.  For a
9500    class-specifier, enum-specifier, or elaborated-type-specifier, a
9501    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9502
9503    The parser flags FLAGS is used to control type-specifier parsing.
9504
9505    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9506    in a decl-specifier-seq.
9507
9508    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9509    class-specifier, enum-specifier, or elaborated-type-specifier, then
9510    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9511    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9512    zero.
9513
9514    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9515    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9516    is set to FALSE.  */
9517
9518 static tree
9519 cp_parser_type_specifier (cp_parser* parser,
9520                           cp_parser_flags flags,
9521                           cp_decl_specifier_seq *decl_specs,
9522                           bool is_declaration,
9523                           int* declares_class_or_enum,
9524                           bool* is_cv_qualifier)
9525 {
9526   tree type_spec = NULL_TREE;
9527   cp_token *token;
9528   enum rid keyword;
9529   cp_decl_spec ds = ds_last;
9530
9531   /* Assume this type-specifier does not declare a new type.  */
9532   if (declares_class_or_enum)
9533     *declares_class_or_enum = 0;
9534   /* And that it does not specify a cv-qualifier.  */
9535   if (is_cv_qualifier)
9536     *is_cv_qualifier = false;
9537   /* Peek at the next token.  */
9538   token = cp_lexer_peek_token (parser->lexer);
9539
9540   /* If we're looking at a keyword, we can use that to guide the
9541      production we choose.  */
9542   keyword = token->keyword;
9543   switch (keyword)
9544     {
9545     case RID_ENUM:
9546       /* Look for the enum-specifier.  */
9547       type_spec = cp_parser_enum_specifier (parser);
9548       /* If that worked, we're done.  */
9549       if (type_spec)
9550         {
9551           if (declares_class_or_enum)
9552             *declares_class_or_enum = 2;
9553           if (decl_specs)
9554             cp_parser_set_decl_spec_type (decl_specs,
9555                                           type_spec,
9556                                           /*user_defined_p=*/true);
9557           return type_spec;
9558         }
9559       else
9560         goto elaborated_type_specifier;
9561
9562       /* Any of these indicate either a class-specifier, or an
9563          elaborated-type-specifier.  */
9564     case RID_CLASS:
9565     case RID_STRUCT:
9566     case RID_UNION:
9567       /* Parse tentatively so that we can back up if we don't find a
9568          class-specifier.  */
9569       cp_parser_parse_tentatively (parser);
9570       /* Look for the class-specifier.  */
9571       type_spec = cp_parser_class_specifier (parser);
9572       /* If that worked, we're done.  */
9573       if (cp_parser_parse_definitely (parser))
9574         {
9575           if (declares_class_or_enum)
9576             *declares_class_or_enum = 2;
9577           if (decl_specs)
9578             cp_parser_set_decl_spec_type (decl_specs,
9579                                           type_spec,
9580                                           /*user_defined_p=*/true);
9581           return type_spec;
9582         }
9583
9584       /* Fall through.  */
9585     elaborated_type_specifier:
9586       /* We're declaring (not defining) a class or enum.  */
9587       if (declares_class_or_enum)
9588         *declares_class_or_enum = 1;
9589
9590       /* Fall through.  */
9591     case RID_TYPENAME:
9592       /* Look for an elaborated-type-specifier.  */
9593       type_spec
9594         = (cp_parser_elaborated_type_specifier
9595            (parser,
9596             decl_specs && decl_specs->specs[(int) ds_friend],
9597             is_declaration));
9598       if (decl_specs)
9599         cp_parser_set_decl_spec_type (decl_specs,
9600                                       type_spec,
9601                                       /*user_defined_p=*/true);
9602       return type_spec;
9603
9604     case RID_CONST:
9605       ds = ds_const;
9606       if (is_cv_qualifier)
9607         *is_cv_qualifier = true;
9608       break;
9609
9610     case RID_VOLATILE:
9611       ds = ds_volatile;
9612       if (is_cv_qualifier)
9613         *is_cv_qualifier = true;
9614       break;
9615
9616     case RID_RESTRICT:
9617       ds = ds_restrict;
9618       if (is_cv_qualifier)
9619         *is_cv_qualifier = true;
9620       break;
9621
9622     case RID_COMPLEX:
9623       /* The `__complex__' keyword is a GNU extension.  */
9624       ds = ds_complex;
9625       break;
9626
9627     default:
9628       break;
9629     }
9630
9631   /* Handle simple keywords.  */
9632   if (ds != ds_last)
9633     {
9634       if (decl_specs)
9635         {
9636           ++decl_specs->specs[(int)ds];
9637           decl_specs->any_specifiers_p = true;
9638         }
9639       return cp_lexer_consume_token (parser->lexer)->value;
9640     }
9641
9642   /* If we do not already have a type-specifier, assume we are looking
9643      at a simple-type-specifier.  */
9644   type_spec = cp_parser_simple_type_specifier (parser,
9645                                                decl_specs,
9646                                                flags);
9647
9648   /* If we didn't find a type-specifier, and a type-specifier was not
9649      optional in this context, issue an error message.  */
9650   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9651     {
9652       cp_parser_error (parser, "expected type specifier");
9653       return error_mark_node;
9654     }
9655
9656   return type_spec;
9657 }
9658
9659 /* Parse a simple-type-specifier.
9660
9661    simple-type-specifier:
9662      :: [opt] nested-name-specifier [opt] type-name
9663      :: [opt] nested-name-specifier template template-id
9664      char
9665      wchar_t
9666      bool
9667      short
9668      int
9669      long
9670      signed
9671      unsigned
9672      float
9673      double
9674      void
9675
9676    GNU Extension:
9677
9678    simple-type-specifier:
9679      __typeof__ unary-expression
9680      __typeof__ ( type-id )
9681
9682    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9683    appropriately updated.  */
9684
9685 static tree
9686 cp_parser_simple_type_specifier (cp_parser* parser,
9687                                  cp_decl_specifier_seq *decl_specs,
9688                                  cp_parser_flags flags)
9689 {
9690   tree type = NULL_TREE;
9691   cp_token *token;
9692
9693   /* Peek at the next token.  */
9694   token = cp_lexer_peek_token (parser->lexer);
9695
9696   /* If we're looking at a keyword, things are easy.  */
9697   switch (token->keyword)
9698     {
9699     case RID_CHAR:
9700       if (decl_specs)
9701         decl_specs->explicit_char_p = true;
9702       type = char_type_node;
9703       break;
9704     case RID_WCHAR:
9705       type = wchar_type_node;
9706       break;
9707     case RID_BOOL:
9708       type = boolean_type_node;
9709       break;
9710     case RID_SHORT:
9711       if (decl_specs)
9712         ++decl_specs->specs[(int) ds_short];
9713       type = short_integer_type_node;
9714       break;
9715     case RID_INT:
9716       if (decl_specs)
9717         decl_specs->explicit_int_p = true;
9718       type = integer_type_node;
9719       break;
9720     case RID_LONG:
9721       if (decl_specs)
9722         ++decl_specs->specs[(int) ds_long];
9723       type = long_integer_type_node;
9724       break;
9725     case RID_SIGNED:
9726       if (decl_specs)
9727         ++decl_specs->specs[(int) ds_signed];
9728       type = integer_type_node;
9729       break;
9730     case RID_UNSIGNED:
9731       if (decl_specs)
9732         ++decl_specs->specs[(int) ds_unsigned];
9733       type = unsigned_type_node;
9734       break;
9735     case RID_FLOAT:
9736       type = float_type_node;
9737       break;
9738     case RID_DOUBLE:
9739       type = double_type_node;
9740       break;
9741     case RID_VOID:
9742       type = void_type_node;
9743       break;
9744
9745     case RID_TYPEOF:
9746       /* Consume the `typeof' token.  */
9747       cp_lexer_consume_token (parser->lexer);
9748       /* Parse the operand to `typeof'.  */
9749       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9750       /* If it is not already a TYPE, take its type.  */
9751       if (!TYPE_P (type))
9752         type = finish_typeof (type);
9753
9754       if (decl_specs)
9755         cp_parser_set_decl_spec_type (decl_specs, type,
9756                                       /*user_defined_p=*/true);
9757
9758       return type;
9759
9760     default:
9761       break;
9762     }
9763
9764   /* If the type-specifier was for a built-in type, we're done.  */
9765   if (type)
9766     {
9767       tree id;
9768
9769       /* Record the type.  */
9770       if (decl_specs
9771           && (token->keyword != RID_SIGNED
9772               && token->keyword != RID_UNSIGNED
9773               && token->keyword != RID_SHORT
9774               && token->keyword != RID_LONG))
9775         cp_parser_set_decl_spec_type (decl_specs,
9776                                       type,
9777                                       /*user_defined=*/false);
9778       if (decl_specs)
9779         decl_specs->any_specifiers_p = true;
9780
9781       /* Consume the token.  */
9782       id = cp_lexer_consume_token (parser->lexer)->value;
9783
9784       /* There is no valid C++ program where a non-template type is
9785          followed by a "<".  That usually indicates that the user thought
9786          that the type was a template.  */
9787       cp_parser_check_for_invalid_template_id (parser, type);
9788
9789       return TYPE_NAME (type);
9790     }
9791
9792   /* The type-specifier must be a user-defined type.  */
9793   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9794     {
9795       bool qualified_p;
9796       bool global_p;
9797
9798       /* Don't gobble tokens or issue error messages if this is an
9799          optional type-specifier.  */
9800       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9801         cp_parser_parse_tentatively (parser);
9802
9803       /* Look for the optional `::' operator.  */
9804       global_p
9805         = (cp_parser_global_scope_opt (parser,
9806                                        /*current_scope_valid_p=*/false)
9807            != NULL_TREE);
9808       /* Look for the nested-name specifier.  */
9809       qualified_p
9810         = (cp_parser_nested_name_specifier_opt (parser,
9811                                                 /*typename_keyword_p=*/false,
9812                                                 /*check_dependency_p=*/true,
9813                                                 /*type_p=*/false,
9814                                                 /*is_declaration=*/false)
9815            != NULL_TREE);
9816       /* If we have seen a nested-name-specifier, and the next token
9817          is `template', then we are using the template-id production.  */
9818       if (parser->scope
9819           && cp_parser_optional_template_keyword (parser))
9820         {
9821           /* Look for the template-id.  */
9822           type = cp_parser_template_id (parser,
9823                                         /*template_keyword_p=*/true,
9824                                         /*check_dependency_p=*/true,
9825                                         /*is_declaration=*/false);
9826           /* If the template-id did not name a type, we are out of
9827              luck.  */
9828           if (TREE_CODE (type) != TYPE_DECL)
9829             {
9830               cp_parser_error (parser, "expected template-id for type");
9831               type = NULL_TREE;
9832             }
9833         }
9834       /* Otherwise, look for a type-name.  */
9835       else
9836         type = cp_parser_type_name (parser);
9837       /* Keep track of all name-lookups performed in class scopes.  */
9838       if (type
9839           && !global_p
9840           && !qualified_p
9841           && TREE_CODE (type) == TYPE_DECL
9842           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9843         maybe_note_name_used_in_class (DECL_NAME (type), type);
9844       /* If it didn't work out, we don't have a TYPE.  */
9845       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9846           && !cp_parser_parse_definitely (parser))
9847         type = NULL_TREE;
9848       if (type && decl_specs)
9849         cp_parser_set_decl_spec_type (decl_specs, type,
9850                                       /*user_defined=*/true);
9851     }
9852
9853   /* If we didn't get a type-name, issue an error message.  */
9854   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9855     {
9856       cp_parser_error (parser, "expected type-name");
9857       return error_mark_node;
9858     }
9859
9860   /* There is no valid C++ program where a non-template type is
9861      followed by a "<".  That usually indicates that the user thought
9862      that the type was a template.  */
9863   if (type && type != error_mark_node)
9864     {
9865       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9866          If it is, then the '<'...'>' enclose protocol names rather than
9867          template arguments, and so everything is fine.  */
9868       if (c_dialect_objc ()
9869           && (objc_is_id (type) || objc_is_class_name (type)))
9870         {
9871           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9872           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9873
9874           /* Clobber the "unqualified" type previously entered into
9875              DECL_SPECS with the new, improved protocol-qualified version.  */
9876           if (decl_specs)
9877             decl_specs->type = qual_type;
9878
9879           return qual_type;
9880         }
9881
9882       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9883     }
9884
9885   return type;
9886 }
9887
9888 /* Parse a type-name.
9889
9890    type-name:
9891      class-name
9892      enum-name
9893      typedef-name
9894
9895    enum-name:
9896      identifier
9897
9898    typedef-name:
9899      identifier
9900
9901    Returns a TYPE_DECL for the type.  */
9902
9903 static tree
9904 cp_parser_type_name (cp_parser* parser)
9905 {
9906   tree type_decl;
9907   tree identifier;
9908
9909   /* We can't know yet whether it is a class-name or not.  */
9910   cp_parser_parse_tentatively (parser);
9911   /* Try a class-name.  */
9912   type_decl = cp_parser_class_name (parser,
9913                                     /*typename_keyword_p=*/false,
9914                                     /*template_keyword_p=*/false,
9915                                     none_type,
9916                                     /*check_dependency_p=*/true,
9917                                     /*class_head_p=*/false,
9918                                     /*is_declaration=*/false);
9919   /* If it's not a class-name, keep looking.  */
9920   if (!cp_parser_parse_definitely (parser))
9921     {
9922       /* It must be a typedef-name or an enum-name.  */
9923       identifier = cp_parser_identifier (parser);
9924       if (identifier == error_mark_node)
9925         return error_mark_node;
9926
9927       /* Look up the type-name.  */
9928       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9929
9930       if (TREE_CODE (type_decl) != TYPE_DECL
9931           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9932         {
9933           /* See if this is an Objective-C type.  */
9934           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9935           tree type = objc_get_protocol_qualified_type (identifier, protos);
9936           if (type)
9937             type_decl = TYPE_NAME (type);
9938         }
9939
9940       /* Issue an error if we did not find a type-name.  */
9941       if (TREE_CODE (type_decl) != TYPE_DECL)
9942         {
9943           if (!cp_parser_simulate_error (parser))
9944             cp_parser_name_lookup_error (parser, identifier, type_decl,
9945                                          "is not a type");
9946           type_decl = error_mark_node;
9947         }
9948       /* Remember that the name was used in the definition of the
9949          current class so that we can check later to see if the
9950          meaning would have been different after the class was
9951          entirely defined.  */
9952       else if (type_decl != error_mark_node
9953                && !parser->scope)
9954         maybe_note_name_used_in_class (identifier, type_decl);
9955     }
9956
9957   return type_decl;
9958 }
9959
9960
9961 /* Parse an elaborated-type-specifier.  Note that the grammar given
9962    here incorporates the resolution to DR68.
9963
9964    elaborated-type-specifier:
9965      class-key :: [opt] nested-name-specifier [opt] identifier
9966      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9967      enum :: [opt] nested-name-specifier [opt] identifier
9968      typename :: [opt] nested-name-specifier identifier
9969      typename :: [opt] nested-name-specifier template [opt]
9970        template-id
9971
9972    GNU extension:
9973
9974    elaborated-type-specifier:
9975      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9976      class-key attributes :: [opt] nested-name-specifier [opt]
9977                template [opt] template-id
9978      enum attributes :: [opt] nested-name-specifier [opt] identifier
9979
9980    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9981    declared `friend'.  If IS_DECLARATION is TRUE, then this
9982    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9983    something is being declared.
9984
9985    Returns the TYPE specified.  */
9986
9987 static tree
9988 cp_parser_elaborated_type_specifier (cp_parser* parser,
9989                                      bool is_friend,
9990                                      bool is_declaration)
9991 {
9992   enum tag_types tag_type;
9993   tree identifier;
9994   tree type = NULL_TREE;
9995   tree attributes = NULL_TREE;
9996
9997   /* See if we're looking at the `enum' keyword.  */
9998   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9999     {
10000       /* Consume the `enum' token.  */
10001       cp_lexer_consume_token (parser->lexer);
10002       /* Remember that it's an enumeration type.  */
10003       tag_type = enum_type;
10004       /* Parse the attributes.  */
10005       attributes = cp_parser_attributes_opt (parser);
10006     }
10007   /* Or, it might be `typename'.  */
10008   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10009                                            RID_TYPENAME))
10010     {
10011       /* Consume the `typename' token.  */
10012       cp_lexer_consume_token (parser->lexer);
10013       /* Remember that it's a `typename' type.  */
10014       tag_type = typename_type;
10015       /* The `typename' keyword is only allowed in templates.  */
10016       if (!processing_template_decl)
10017         pedwarn ("using %<typename%> outside of template");
10018     }
10019   /* Otherwise it must be a class-key.  */
10020   else
10021     {
10022       tag_type = cp_parser_class_key (parser);
10023       if (tag_type == none_type)
10024         return error_mark_node;
10025       /* Parse the attributes.  */
10026       attributes = cp_parser_attributes_opt (parser);
10027     }
10028
10029   /* Look for the `::' operator.  */
10030   cp_parser_global_scope_opt (parser,
10031                               /*current_scope_valid_p=*/false);
10032   /* Look for the nested-name-specifier.  */
10033   if (tag_type == typename_type)
10034     {
10035       if (!cp_parser_nested_name_specifier (parser,
10036                                            /*typename_keyword_p=*/true,
10037                                            /*check_dependency_p=*/true,
10038                                            /*type_p=*/true,
10039                                             is_declaration))
10040         return error_mark_node;
10041     }
10042   else
10043     /* Even though `typename' is not present, the proposed resolution
10044        to Core Issue 180 says that in `class A<T>::B', `B' should be
10045        considered a type-name, even if `A<T>' is dependent.  */
10046     cp_parser_nested_name_specifier_opt (parser,
10047                                          /*typename_keyword_p=*/true,
10048                                          /*check_dependency_p=*/true,
10049                                          /*type_p=*/true,
10050                                          is_declaration);
10051   /* For everything but enumeration types, consider a template-id.  */
10052   /* For an enumeration type, consider only a plain identifier.  */
10053   if (tag_type != enum_type)
10054     {
10055       bool template_p = false;
10056       tree decl;
10057
10058       /* Allow the `template' keyword.  */
10059       template_p = cp_parser_optional_template_keyword (parser);
10060       /* If we didn't see `template', we don't know if there's a
10061          template-id or not.  */
10062       if (!template_p)
10063         cp_parser_parse_tentatively (parser);
10064       /* Parse the template-id.  */
10065       decl = cp_parser_template_id (parser, template_p,
10066                                     /*check_dependency_p=*/true,
10067                                     is_declaration);
10068       /* If we didn't find a template-id, look for an ordinary
10069          identifier.  */
10070       if (!template_p && !cp_parser_parse_definitely (parser))
10071         ;
10072       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10073          in effect, then we must assume that, upon instantiation, the
10074          template will correspond to a class.  */
10075       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10076                && tag_type == typename_type)
10077         type = make_typename_type (parser->scope, decl,
10078                                    typename_type,
10079                                    /*complain=*/tf_error);
10080       else
10081         type = TREE_TYPE (decl);
10082     }
10083
10084   if (!type)
10085     {
10086       identifier = cp_parser_identifier (parser);
10087
10088       if (identifier == error_mark_node)
10089         {
10090           parser->scope = NULL_TREE;
10091           return error_mark_node;
10092         }
10093
10094       /* For a `typename', we needn't call xref_tag.  */
10095       if (tag_type == typename_type
10096           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10097         return cp_parser_make_typename_type (parser, parser->scope,
10098                                              identifier);
10099       /* Look up a qualified name in the usual way.  */
10100       if (parser->scope)
10101         {
10102           tree decl;
10103
10104           decl = cp_parser_lookup_name (parser, identifier,
10105                                         tag_type,
10106                                         /*is_template=*/false,
10107                                         /*is_namespace=*/false,
10108                                         /*check_dependency=*/true,
10109                                         /*ambiguous_decls=*/NULL);
10110
10111           /* If we are parsing friend declaration, DECL may be a
10112              TEMPLATE_DECL tree node here.  However, we need to check
10113              whether this TEMPLATE_DECL results in valid code.  Consider
10114              the following example:
10115
10116                namespace N {
10117                  template <class T> class C {};
10118                }
10119                class X {
10120                  template <class T> friend class N::C; // #1, valid code
10121                };
10122                template <class T> class Y {
10123                  friend class N::C;                    // #2, invalid code
10124                };
10125
10126              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10127              name lookup of `N::C'.  We see that friend declaration must
10128              be template for the code to be valid.  Note that
10129              processing_template_decl does not work here since it is
10130              always 1 for the above two cases.  */
10131
10132           decl = (cp_parser_maybe_treat_template_as_class
10133                   (decl, /*tag_name_p=*/is_friend
10134                          && parser->num_template_parameter_lists));
10135
10136           if (TREE_CODE (decl) != TYPE_DECL)
10137             {
10138               cp_parser_diagnose_invalid_type_name (parser,
10139                                                     parser->scope,
10140                                                     identifier);
10141               return error_mark_node;
10142             }
10143
10144           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10145             check_elaborated_type_specifier
10146               (tag_type, decl,
10147                (parser->num_template_parameter_lists
10148                 || DECL_SELF_REFERENCE_P (decl)));
10149
10150           type = TREE_TYPE (decl);
10151         }
10152       else
10153         {
10154           /* An elaborated-type-specifier sometimes introduces a new type and
10155              sometimes names an existing type.  Normally, the rule is that it
10156              introduces a new type only if there is not an existing type of
10157              the same name already in scope.  For example, given:
10158
10159                struct S {};
10160                void f() { struct S s; }
10161
10162              the `struct S' in the body of `f' is the same `struct S' as in
10163              the global scope; the existing definition is used.  However, if
10164              there were no global declaration, this would introduce a new
10165              local class named `S'.
10166
10167              An exception to this rule applies to the following code:
10168
10169                namespace N { struct S; }
10170
10171              Here, the elaborated-type-specifier names a new type
10172              unconditionally; even if there is already an `S' in the
10173              containing scope this declaration names a new type.
10174              This exception only applies if the elaborated-type-specifier
10175              forms the complete declaration:
10176
10177                [class.name]
10178
10179                A declaration consisting solely of `class-key identifier ;' is
10180                either a redeclaration of the name in the current scope or a
10181                forward declaration of the identifier as a class name.  It
10182                introduces the name into the current scope.
10183
10184              We are in this situation precisely when the next token is a `;'.
10185
10186              An exception to the exception is that a `friend' declaration does
10187              *not* name a new type; i.e., given:
10188
10189                struct S { friend struct T; };
10190
10191              `T' is not a new type in the scope of `S'.
10192
10193              Also, `new struct S' or `sizeof (struct S)' never results in the
10194              definition of a new type; a new type can only be declared in a
10195              declaration context.  */
10196
10197           tag_scope ts;
10198           bool template_p;
10199
10200           if (is_friend)
10201             /* Friends have special name lookup rules.  */
10202             ts = ts_within_enclosing_non_class;
10203           else if (is_declaration
10204                    && cp_lexer_next_token_is (parser->lexer,
10205                                               CPP_SEMICOLON))
10206             /* This is a `class-key identifier ;' */
10207             ts = ts_current;
10208           else
10209             ts = ts_global;
10210
10211           template_p =
10212             (parser->num_template_parameter_lists
10213              && (cp_parser_next_token_starts_class_definition_p (parser)
10214                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10215           /* An unqualified name was used to reference this type, so
10216              there were no qualifying templates.  */
10217           if (!cp_parser_check_template_parameters (parser,
10218                                                     /*num_templates=*/0))
10219             return error_mark_node;
10220           type = xref_tag (tag_type, identifier, ts, template_p);
10221         }
10222     }
10223
10224   if (type == error_mark_node)
10225     return error_mark_node;
10226
10227   /* Allow attributes on forward declarations of classes.  */
10228   if (attributes)
10229     {
10230       if (TREE_CODE (type) == TYPENAME_TYPE)
10231         warning (OPT_Wattributes,
10232                  "attributes ignored on uninstantiated type");
10233       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10234                && ! processing_explicit_instantiation)
10235         warning (OPT_Wattributes,
10236                  "attributes ignored on template instantiation");
10237       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10238         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10239       else
10240         warning (OPT_Wattributes,
10241                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10242     }
10243
10244   if (tag_type != enum_type)
10245     cp_parser_check_class_key (tag_type, type);
10246
10247   /* A "<" cannot follow an elaborated type specifier.  If that
10248      happens, the user was probably trying to form a template-id.  */
10249   cp_parser_check_for_invalid_template_id (parser, type);
10250
10251   return type;
10252 }
10253
10254 /* Parse an enum-specifier.
10255
10256    enum-specifier:
10257      enum identifier [opt] { enumerator-list [opt] }
10258
10259    GNU Extensions:
10260      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10261        attributes[opt]
10262
10263    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10264    if the token stream isn't an enum-specifier after all.  */
10265
10266 static tree
10267 cp_parser_enum_specifier (cp_parser* parser)
10268 {
10269   tree identifier;
10270   tree type;
10271   tree attributes;
10272
10273   /* Parse tentatively so that we can back up if we don't find a
10274      enum-specifier.  */
10275   cp_parser_parse_tentatively (parser);
10276
10277   /* Caller guarantees that the current token is 'enum', an identifier
10278      possibly follows, and the token after that is an opening brace.
10279      If we don't have an identifier, fabricate an anonymous name for
10280      the enumeration being defined.  */
10281   cp_lexer_consume_token (parser->lexer);
10282
10283   attributes = cp_parser_attributes_opt (parser);
10284
10285   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10286     identifier = cp_parser_identifier (parser);
10287   else
10288     identifier = make_anon_name ();
10289
10290   /* Look for the `{' but don't consume it yet.  */
10291   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10292     cp_parser_simulate_error (parser);
10293
10294   if (!cp_parser_parse_definitely (parser))
10295     return NULL_TREE;
10296
10297   /* Issue an error message if type-definitions are forbidden here.  */
10298   cp_parser_check_type_definition (parser);
10299
10300   /* Create the new type.  We do this before consuming the opening brace
10301      so the enum will be recorded as being on the line of its tag (or the
10302      'enum' keyword, if there is no tag).  */
10303   type = start_enum (identifier);
10304
10305   /* Consume the opening brace.  */
10306   cp_lexer_consume_token (parser->lexer);
10307
10308   if (type == error_mark_node)
10309     {
10310       cp_parser_skip_to_end_of_block_or_statement (parser);
10311       return error_mark_node;
10312     }
10313
10314   /* If the next token is not '}', then there are some enumerators.  */
10315   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10316     cp_parser_enumerator_list (parser, type);
10317
10318   /* Consume the final '}'.  */
10319   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10320
10321   /* Look for trailing attributes to apply to this enumeration, and
10322      apply them if appropriate.  */
10323   if (cp_parser_allow_gnu_extensions_p (parser))
10324     {
10325       tree trailing_attr = cp_parser_attributes_opt (parser);
10326       cplus_decl_attributes (&type,
10327                              trailing_attr,
10328                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10329     }
10330
10331   /* Finish up the enumeration.  */
10332   finish_enum (type);
10333
10334   return type;
10335 }
10336
10337 /* Parse an enumerator-list.  The enumerators all have the indicated
10338    TYPE.
10339
10340    enumerator-list:
10341      enumerator-definition
10342      enumerator-list , enumerator-definition  */
10343
10344 static void
10345 cp_parser_enumerator_list (cp_parser* parser, tree type)
10346 {
10347   while (true)
10348     {
10349       /* Parse an enumerator-definition.  */
10350       cp_parser_enumerator_definition (parser, type);
10351
10352       /* If the next token is not a ',', we've reached the end of
10353          the list.  */
10354       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10355         break;
10356       /* Otherwise, consume the `,' and keep going.  */
10357       cp_lexer_consume_token (parser->lexer);
10358       /* If the next token is a `}', there is a trailing comma.  */
10359       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10360         {
10361           if (pedantic && !in_system_header)
10362             pedwarn ("comma at end of enumerator list");
10363           break;
10364         }
10365     }
10366 }
10367
10368 /* Parse an enumerator-definition.  The enumerator has the indicated
10369    TYPE.
10370
10371    enumerator-definition:
10372      enumerator
10373      enumerator = constant-expression
10374
10375    enumerator:
10376      identifier  */
10377
10378 static void
10379 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10380 {
10381   tree identifier;
10382   tree value;
10383
10384   /* Look for the identifier.  */
10385   identifier = cp_parser_identifier (parser);
10386   if (identifier == error_mark_node)
10387     return;
10388
10389   /* If the next token is an '=', then there is an explicit value.  */
10390   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10391     {
10392       /* Consume the `=' token.  */
10393       cp_lexer_consume_token (parser->lexer);
10394       /* Parse the value.  */
10395       value = cp_parser_constant_expression (parser,
10396                                              /*allow_non_constant_p=*/false,
10397                                              NULL);
10398     }
10399   else
10400     value = NULL_TREE;
10401
10402   /* Create the enumerator.  */
10403   build_enumerator (identifier, value, type);
10404 }
10405
10406 /* Parse a namespace-name.
10407
10408    namespace-name:
10409      original-namespace-name
10410      namespace-alias
10411
10412    Returns the NAMESPACE_DECL for the namespace.  */
10413
10414 static tree
10415 cp_parser_namespace_name (cp_parser* parser)
10416 {
10417   tree identifier;
10418   tree namespace_decl;
10419
10420   /* Get the name of the namespace.  */
10421   identifier = cp_parser_identifier (parser);
10422   if (identifier == error_mark_node)
10423     return error_mark_node;
10424
10425   /* Look up the identifier in the currently active scope.  Look only
10426      for namespaces, due to:
10427
10428        [basic.lookup.udir]
10429
10430        When looking up a namespace-name in a using-directive or alias
10431        definition, only namespace names are considered.
10432
10433      And:
10434
10435        [basic.lookup.qual]
10436
10437        During the lookup of a name preceding the :: scope resolution
10438        operator, object, function, and enumerator names are ignored.
10439
10440      (Note that cp_parser_class_or_namespace_name only calls this
10441      function if the token after the name is the scope resolution
10442      operator.)  */
10443   namespace_decl = cp_parser_lookup_name (parser, identifier,
10444                                           none_type,
10445                                           /*is_template=*/false,
10446                                           /*is_namespace=*/true,
10447                                           /*check_dependency=*/true,
10448                                           /*ambiguous_decls=*/NULL);
10449   /* If it's not a namespace, issue an error.  */
10450   if (namespace_decl == error_mark_node
10451       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10452     {
10453       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10454         error ("%qD is not a namespace-name", identifier);
10455       cp_parser_error (parser, "expected namespace-name");
10456       namespace_decl = error_mark_node;
10457     }
10458
10459   return namespace_decl;
10460 }
10461
10462 /* Parse a namespace-definition.
10463
10464    namespace-definition:
10465      named-namespace-definition
10466      unnamed-namespace-definition
10467
10468    named-namespace-definition:
10469      original-namespace-definition
10470      extension-namespace-definition
10471
10472    original-namespace-definition:
10473      namespace identifier { namespace-body }
10474
10475    extension-namespace-definition:
10476      namespace original-namespace-name { namespace-body }
10477
10478    unnamed-namespace-definition:
10479      namespace { namespace-body } */
10480
10481 static void
10482 cp_parser_namespace_definition (cp_parser* parser)
10483 {
10484   tree identifier, attribs;
10485
10486   /* Look for the `namespace' keyword.  */
10487   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10488
10489   /* Get the name of the namespace.  We do not attempt to distinguish
10490      between an original-namespace-definition and an
10491      extension-namespace-definition at this point.  The semantic
10492      analysis routines are responsible for that.  */
10493   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10494     identifier = cp_parser_identifier (parser);
10495   else
10496     identifier = NULL_TREE;
10497
10498   /* Parse any specified attributes.  */
10499   attribs = cp_parser_attributes_opt (parser);
10500
10501   /* Look for the `{' to start the namespace.  */
10502   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10503   /* Start the namespace.  */
10504   push_namespace_with_attribs (identifier, attribs);
10505   /* Parse the body of the namespace.  */
10506   cp_parser_namespace_body (parser);
10507   /* Finish the namespace.  */
10508   pop_namespace ();
10509   /* Look for the final `}'.  */
10510   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10511 }
10512
10513 /* Parse a namespace-body.
10514
10515    namespace-body:
10516      declaration-seq [opt]  */
10517
10518 static void
10519 cp_parser_namespace_body (cp_parser* parser)
10520 {
10521   cp_parser_declaration_seq_opt (parser);
10522 }
10523
10524 /* Parse a namespace-alias-definition.
10525
10526    namespace-alias-definition:
10527      namespace identifier = qualified-namespace-specifier ;  */
10528
10529 static void
10530 cp_parser_namespace_alias_definition (cp_parser* parser)
10531 {
10532   tree identifier;
10533   tree namespace_specifier;
10534
10535   /* Look for the `namespace' keyword.  */
10536   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10537   /* Look for the identifier.  */
10538   identifier = cp_parser_identifier (parser);
10539   if (identifier == error_mark_node)
10540     return;
10541   /* Look for the `=' token.  */
10542   cp_parser_require (parser, CPP_EQ, "`='");
10543   /* Look for the qualified-namespace-specifier.  */
10544   namespace_specifier
10545     = cp_parser_qualified_namespace_specifier (parser);
10546   /* Look for the `;' token.  */
10547   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10548
10549   /* Register the alias in the symbol table.  */
10550   do_namespace_alias (identifier, namespace_specifier);
10551 }
10552
10553 /* Parse a qualified-namespace-specifier.
10554
10555    qualified-namespace-specifier:
10556      :: [opt] nested-name-specifier [opt] namespace-name
10557
10558    Returns a NAMESPACE_DECL corresponding to the specified
10559    namespace.  */
10560
10561 static tree
10562 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10563 {
10564   /* Look for the optional `::'.  */
10565   cp_parser_global_scope_opt (parser,
10566                               /*current_scope_valid_p=*/false);
10567
10568   /* Look for the optional nested-name-specifier.  */
10569   cp_parser_nested_name_specifier_opt (parser,
10570                                        /*typename_keyword_p=*/false,
10571                                        /*check_dependency_p=*/true,
10572                                        /*type_p=*/false,
10573                                        /*is_declaration=*/true);
10574
10575   return cp_parser_namespace_name (parser);
10576 }
10577
10578 /* Parse a using-declaration.
10579
10580    using-declaration:
10581      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10582      using :: unqualified-id ;  */
10583
10584 static void
10585 cp_parser_using_declaration (cp_parser* parser)
10586 {
10587   cp_token *token;
10588   bool typename_p = false;
10589   bool global_scope_p;
10590   tree decl;
10591   tree identifier;
10592   tree qscope;
10593
10594   /* Look for the `using' keyword.  */
10595   cp_parser_require_keyword (parser, RID_USING, "`using'");
10596
10597   /* Peek at the next token.  */
10598   token = cp_lexer_peek_token (parser->lexer);
10599   /* See if it's `typename'.  */
10600   if (token->keyword == RID_TYPENAME)
10601     {
10602       /* Remember that we've seen it.  */
10603       typename_p = true;
10604       /* Consume the `typename' token.  */
10605       cp_lexer_consume_token (parser->lexer);
10606     }
10607
10608   /* Look for the optional global scope qualification.  */
10609   global_scope_p
10610     = (cp_parser_global_scope_opt (parser,
10611                                    /*current_scope_valid_p=*/false)
10612        != NULL_TREE);
10613
10614   /* If we saw `typename', or didn't see `::', then there must be a
10615      nested-name-specifier present.  */
10616   if (typename_p || !global_scope_p)
10617     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10618                                               /*check_dependency_p=*/true,
10619                                               /*type_p=*/false,
10620                                               /*is_declaration=*/true);
10621   /* Otherwise, we could be in either of the two productions.  In that
10622      case, treat the nested-name-specifier as optional.  */
10623   else
10624     qscope = cp_parser_nested_name_specifier_opt (parser,
10625                                                   /*typename_keyword_p=*/false,
10626                                                   /*check_dependency_p=*/true,
10627                                                   /*type_p=*/false,
10628                                                   /*is_declaration=*/true);
10629   if (!qscope)
10630     qscope = global_namespace;
10631
10632   /* Parse the unqualified-id.  */
10633   identifier = cp_parser_unqualified_id (parser,
10634                                          /*template_keyword_p=*/false,
10635                                          /*check_dependency_p=*/true,
10636                                          /*declarator_p=*/true,
10637                                          /*optional_p=*/false);
10638
10639   /* The function we call to handle a using-declaration is different
10640      depending on what scope we are in.  */
10641   if (qscope == error_mark_node || identifier == error_mark_node)
10642     ;
10643   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10644            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10645     /* [namespace.udecl]
10646
10647        A using declaration shall not name a template-id.  */
10648     error ("a template-id may not appear in a using-declaration");
10649   else
10650     {
10651       if (at_class_scope_p ())
10652         {
10653           /* Create the USING_DECL.  */
10654           decl = do_class_using_decl (parser->scope, identifier);
10655           /* Add it to the list of members in this class.  */
10656           finish_member_declaration (decl);
10657         }
10658       else
10659         {
10660           decl = cp_parser_lookup_name_simple (parser, identifier);
10661           if (decl == error_mark_node)
10662             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10663           else if (!at_namespace_scope_p ())
10664             do_local_using_decl (decl, qscope, identifier);
10665           else
10666             do_toplevel_using_decl (decl, qscope, identifier);
10667         }
10668     }
10669
10670   /* Look for the final `;'.  */
10671   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10672 }
10673
10674 /* Parse a using-directive.
10675
10676    using-directive:
10677      using namespace :: [opt] nested-name-specifier [opt]
10678        namespace-name ;  */
10679
10680 static void
10681 cp_parser_using_directive (cp_parser* parser)
10682 {
10683   tree namespace_decl;
10684   tree attribs;
10685
10686   /* Look for the `using' keyword.  */
10687   cp_parser_require_keyword (parser, RID_USING, "`using'");
10688   /* And the `namespace' keyword.  */
10689   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10690   /* Look for the optional `::' operator.  */
10691   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10692   /* And the optional nested-name-specifier.  */
10693   cp_parser_nested_name_specifier_opt (parser,
10694                                        /*typename_keyword_p=*/false,
10695                                        /*check_dependency_p=*/true,
10696                                        /*type_p=*/false,
10697                                        /*is_declaration=*/true);
10698   /* Get the namespace being used.  */
10699   namespace_decl = cp_parser_namespace_name (parser);
10700   /* And any specified attributes.  */
10701   attribs = cp_parser_attributes_opt (parser);
10702   /* Update the symbol table.  */
10703   parse_using_directive (namespace_decl, attribs);
10704   /* Look for the final `;'.  */
10705   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10706 }
10707
10708 /* Parse an asm-definition.
10709
10710    asm-definition:
10711      asm ( string-literal ) ;
10712
10713    GNU Extension:
10714
10715    asm-definition:
10716      asm volatile [opt] ( string-literal ) ;
10717      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10718      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10719                           : asm-operand-list [opt] ) ;
10720      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10721                           : asm-operand-list [opt]
10722                           : asm-operand-list [opt] ) ;  */
10723
10724 static void
10725 cp_parser_asm_definition (cp_parser* parser)
10726 {
10727   tree string;
10728   tree outputs = NULL_TREE;
10729   tree inputs = NULL_TREE;
10730   tree clobbers = NULL_TREE;
10731   tree asm_stmt;
10732   bool volatile_p = false;
10733   bool extended_p = false;
10734
10735   /* Look for the `asm' keyword.  */
10736   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10737   /* See if the next token is `volatile'.  */
10738   if (cp_parser_allow_gnu_extensions_p (parser)
10739       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10740     {
10741       /* Remember that we saw the `volatile' keyword.  */
10742       volatile_p = true;
10743       /* Consume the token.  */
10744       cp_lexer_consume_token (parser->lexer);
10745     }
10746   /* Look for the opening `('.  */
10747   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10748     return;
10749   /* Look for the string.  */
10750   string = cp_parser_string_literal (parser, false, false);
10751   if (string == error_mark_node)
10752     {
10753       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10754                                              /*consume_paren=*/true);
10755       return;
10756     }
10757
10758   /* If we're allowing GNU extensions, check for the extended assembly
10759      syntax.  Unfortunately, the `:' tokens need not be separated by
10760      a space in C, and so, for compatibility, we tolerate that here
10761      too.  Doing that means that we have to treat the `::' operator as
10762      two `:' tokens.  */
10763   if (cp_parser_allow_gnu_extensions_p (parser)
10764       && at_function_scope_p ()
10765       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10766           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10767     {
10768       bool inputs_p = false;
10769       bool clobbers_p = false;
10770
10771       /* The extended syntax was used.  */
10772       extended_p = true;
10773
10774       /* Look for outputs.  */
10775       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10776         {
10777           /* Consume the `:'.  */
10778           cp_lexer_consume_token (parser->lexer);
10779           /* Parse the output-operands.  */
10780           if (cp_lexer_next_token_is_not (parser->lexer,
10781                                           CPP_COLON)
10782               && cp_lexer_next_token_is_not (parser->lexer,
10783                                              CPP_SCOPE)
10784               && cp_lexer_next_token_is_not (parser->lexer,
10785                                              CPP_CLOSE_PAREN))
10786             outputs = cp_parser_asm_operand_list (parser);
10787         }
10788       /* If the next token is `::', there are no outputs, and the
10789          next token is the beginning of the inputs.  */
10790       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10791         /* The inputs are coming next.  */
10792         inputs_p = true;
10793
10794       /* Look for inputs.  */
10795       if (inputs_p
10796           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10797         {
10798           /* Consume the `:' or `::'.  */
10799           cp_lexer_consume_token (parser->lexer);
10800           /* Parse the output-operands.  */
10801           if (cp_lexer_next_token_is_not (parser->lexer,
10802                                           CPP_COLON)
10803               && cp_lexer_next_token_is_not (parser->lexer,
10804                                              CPP_CLOSE_PAREN))
10805             inputs = cp_parser_asm_operand_list (parser);
10806         }
10807       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10808         /* The clobbers are coming next.  */
10809         clobbers_p = true;
10810
10811       /* Look for clobbers.  */
10812       if (clobbers_p
10813           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10814         {
10815           /* Consume the `:' or `::'.  */
10816           cp_lexer_consume_token (parser->lexer);
10817           /* Parse the clobbers.  */
10818           if (cp_lexer_next_token_is_not (parser->lexer,
10819                                           CPP_CLOSE_PAREN))
10820             clobbers = cp_parser_asm_clobber_list (parser);
10821         }
10822     }
10823   /* Look for the closing `)'.  */
10824   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10825     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10826                                            /*consume_paren=*/true);
10827   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10828
10829   /* Create the ASM_EXPR.  */
10830   if (at_function_scope_p ())
10831     {
10832       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10833                                   inputs, clobbers);
10834       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10835       if (!extended_p)
10836         {
10837           tree temp = asm_stmt;
10838           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10839             temp = TREE_OPERAND (temp, 0);
10840
10841           ASM_INPUT_P (temp) = 1;
10842         }
10843     }
10844   else
10845     cgraph_add_asm_node (string);
10846 }
10847
10848 /* Declarators [gram.dcl.decl] */
10849
10850 /* Parse an init-declarator.
10851
10852    init-declarator:
10853      declarator initializer [opt]
10854
10855    GNU Extension:
10856
10857    init-declarator:
10858      declarator asm-specification [opt] attributes [opt] initializer [opt]
10859
10860    function-definition:
10861      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10862        function-body
10863      decl-specifier-seq [opt] declarator function-try-block
10864
10865    GNU Extension:
10866
10867    function-definition:
10868      __extension__ function-definition
10869
10870    The DECL_SPECIFIERS apply to this declarator.  Returns a
10871    representation of the entity declared.  If MEMBER_P is TRUE, then
10872    this declarator appears in a class scope.  The new DECL created by
10873    this declarator is returned.
10874
10875    The CHECKS are access checks that should be performed once we know
10876    what entity is being declared (and, therefore, what classes have
10877    befriended it).
10878
10879    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10880    for a function-definition here as well.  If the declarator is a
10881    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10882    be TRUE upon return.  By that point, the function-definition will
10883    have been completely parsed.
10884
10885    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10886    is FALSE.  */
10887
10888 static tree
10889 cp_parser_init_declarator (cp_parser* parser,
10890                            cp_decl_specifier_seq *decl_specifiers,
10891                            tree checks,
10892                            bool function_definition_allowed_p,
10893                            bool member_p,
10894                            int declares_class_or_enum,
10895                            bool* function_definition_p)
10896 {
10897   cp_token *token;
10898   cp_declarator *declarator;
10899   tree prefix_attributes;
10900   tree attributes;
10901   tree asm_specification;
10902   tree initializer;
10903   tree decl = NULL_TREE;
10904   tree scope;
10905   bool is_initialized;
10906   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
10907      initialized with "= ..", CPP_OPEN_PAREN if initialized with
10908      "(...)".  */
10909   enum cpp_ttype initialization_kind;
10910   bool is_parenthesized_init = false;
10911   bool is_non_constant_init;
10912   int ctor_dtor_or_conv_p;
10913   bool friend_p;
10914   tree pushed_scope = NULL;
10915
10916   /* Gather the attributes that were provided with the
10917      decl-specifiers.  */
10918   prefix_attributes = decl_specifiers->attributes;
10919
10920   /* Assume that this is not the declarator for a function
10921      definition.  */
10922   if (function_definition_p)
10923     *function_definition_p = false;
10924
10925   /* Defer access checks while parsing the declarator; we cannot know
10926      what names are accessible until we know what is being
10927      declared.  */
10928   resume_deferring_access_checks ();
10929
10930   /* Parse the declarator.  */
10931   declarator
10932     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10933                             &ctor_dtor_or_conv_p,
10934                             /*parenthesized_p=*/NULL,
10935                             /*member_p=*/false);
10936   /* Gather up the deferred checks.  */
10937   stop_deferring_access_checks ();
10938
10939   /* If the DECLARATOR was erroneous, there's no need to go
10940      further.  */
10941   if (declarator == cp_error_declarator)
10942     return error_mark_node;
10943
10944   if (declares_class_or_enum & 2)
10945     cp_parser_check_for_definition_in_return_type (declarator,
10946                                                    decl_specifiers->type);
10947
10948   /* Figure out what scope the entity declared by the DECLARATOR is
10949      located in.  `grokdeclarator' sometimes changes the scope, so
10950      we compute it now.  */
10951   scope = get_scope_of_declarator (declarator);
10952
10953   /* If we're allowing GNU extensions, look for an asm-specification
10954      and attributes.  */
10955   if (cp_parser_allow_gnu_extensions_p (parser))
10956     {
10957       /* Look for an asm-specification.  */
10958       asm_specification = cp_parser_asm_specification_opt (parser);
10959       /* And attributes.  */
10960       attributes = cp_parser_attributes_opt (parser);
10961     }
10962   else
10963     {
10964       asm_specification = NULL_TREE;
10965       attributes = NULL_TREE;
10966     }
10967
10968   /* Peek at the next token.  */
10969   token = cp_lexer_peek_token (parser->lexer);
10970   /* Check to see if the token indicates the start of a
10971      function-definition.  */
10972   if (cp_parser_token_starts_function_definition_p (token))
10973     {
10974       if (!function_definition_allowed_p)
10975         {
10976           /* If a function-definition should not appear here, issue an
10977              error message.  */
10978           cp_parser_error (parser,
10979                            "a function-definition is not allowed here");
10980           return error_mark_node;
10981         }
10982       else
10983         {
10984           /* Neither attributes nor an asm-specification are allowed
10985              on a function-definition.  */
10986           if (asm_specification)
10987             error ("an asm-specification is not allowed on a function-definition");
10988           if (attributes)
10989             error ("attributes are not allowed on a function-definition");
10990           /* This is a function-definition.  */
10991           *function_definition_p = true;
10992
10993           /* Parse the function definition.  */
10994           if (member_p)
10995             decl = cp_parser_save_member_function_body (parser,
10996                                                         decl_specifiers,
10997                                                         declarator,
10998                                                         prefix_attributes);
10999           else
11000             decl
11001               = (cp_parser_function_definition_from_specifiers_and_declarator
11002                  (parser, decl_specifiers, prefix_attributes, declarator));
11003
11004           return decl;
11005         }
11006     }
11007
11008   /* [dcl.dcl]
11009
11010      Only in function declarations for constructors, destructors, and
11011      type conversions can the decl-specifier-seq be omitted.
11012
11013      We explicitly postpone this check past the point where we handle
11014      function-definitions because we tolerate function-definitions
11015      that are missing their return types in some modes.  */
11016   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11017     {
11018       cp_parser_error (parser,
11019                        "expected constructor, destructor, or type conversion");
11020       return error_mark_node;
11021     }
11022
11023   /* An `=' or an `(' indicates an initializer.  */
11024   if (token->type == CPP_EQ
11025       || token->type == CPP_OPEN_PAREN)
11026     {
11027       is_initialized = true;
11028       initialization_kind = token->type;
11029     }
11030   else
11031     {
11032       /* If the init-declarator isn't initialized and isn't followed by a
11033          `,' or `;', it's not a valid init-declarator.  */
11034       if (token->type != CPP_COMMA
11035           && token->type != CPP_SEMICOLON)
11036         {
11037           cp_parser_error (parser, "expected initializer");
11038           return error_mark_node;
11039         }
11040       is_initialized = false;
11041       initialization_kind = CPP_EOF;
11042     }
11043
11044   /* Because start_decl has side-effects, we should only call it if we
11045      know we're going ahead.  By this point, we know that we cannot
11046      possibly be looking at any other construct.  */
11047   cp_parser_commit_to_tentative_parse (parser);
11048
11049   /* If the decl specifiers were bad, issue an error now that we're
11050      sure this was intended to be a declarator.  Then continue
11051      declaring the variable(s), as int, to try to cut down on further
11052      errors.  */
11053   if (decl_specifiers->any_specifiers_p
11054       && decl_specifiers->type == error_mark_node)
11055     {
11056       cp_parser_error (parser, "invalid type in declaration");
11057       decl_specifiers->type = integer_type_node;
11058     }
11059
11060   /* Check to see whether or not this declaration is a friend.  */
11061   friend_p = cp_parser_friend_p (decl_specifiers);
11062
11063   /* Check that the number of template-parameter-lists is OK.  */
11064   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11065     return error_mark_node;
11066
11067   /* Enter the newly declared entry in the symbol table.  If we're
11068      processing a declaration in a class-specifier, we wait until
11069      after processing the initializer.  */
11070   if (!member_p)
11071     {
11072       if (parser->in_unbraced_linkage_specification_p)
11073         decl_specifiers->storage_class = sc_extern;
11074       decl = start_decl (declarator, decl_specifiers,
11075                          is_initialized, attributes, prefix_attributes,
11076                          &pushed_scope);
11077     }
11078   else if (scope)
11079     /* Enter the SCOPE.  That way unqualified names appearing in the
11080        initializer will be looked up in SCOPE.  */
11081     pushed_scope = push_scope (scope);
11082
11083   /* Perform deferred access control checks, now that we know in which
11084      SCOPE the declared entity resides.  */
11085   if (!member_p && decl)
11086     {
11087       tree saved_current_function_decl = NULL_TREE;
11088
11089       /* If the entity being declared is a function, pretend that we
11090          are in its scope.  If it is a `friend', it may have access to
11091          things that would not otherwise be accessible.  */
11092       if (TREE_CODE (decl) == FUNCTION_DECL)
11093         {
11094           saved_current_function_decl = current_function_decl;
11095           current_function_decl = decl;
11096         }
11097
11098       /* Perform access checks for template parameters.  */
11099       cp_parser_perform_template_parameter_access_checks (checks);
11100
11101       /* Perform the access control checks for the declarator and the
11102          the decl-specifiers.  */
11103       perform_deferred_access_checks ();
11104
11105       /* Restore the saved value.  */
11106       if (TREE_CODE (decl) == FUNCTION_DECL)
11107         current_function_decl = saved_current_function_decl;
11108     }
11109
11110   /* Parse the initializer.  */
11111   initializer = NULL_TREE;
11112   is_parenthesized_init = false;
11113   is_non_constant_init = true;
11114   if (is_initialized)
11115     {
11116       if (declarator->kind == cdk_function
11117           && declarator->declarator->kind == cdk_id
11118           && initialization_kind == CPP_EQ)
11119         initializer = cp_parser_pure_specifier (parser);
11120       else
11121         initializer = cp_parser_initializer (parser,
11122                                              &is_parenthesized_init,
11123                                              &is_non_constant_init);
11124     }
11125
11126   /* The old parser allows attributes to appear after a parenthesized
11127      initializer.  Mark Mitchell proposed removing this functionality
11128      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11129      attributes -- but ignores them.  */
11130   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11131     if (cp_parser_attributes_opt (parser))
11132       warning (OPT_Wattributes,
11133                "attributes after parenthesized initializer ignored");
11134
11135   /* For an in-class declaration, use `grokfield' to create the
11136      declaration.  */
11137   if (member_p)
11138     {
11139       if (pushed_scope)
11140         {
11141           pop_scope (pushed_scope);
11142           pushed_scope = false;
11143         }
11144       decl = grokfield (declarator, decl_specifiers,
11145                         initializer, !is_non_constant_init,
11146                         /*asmspec=*/NULL_TREE,
11147                         prefix_attributes);
11148       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11149         cp_parser_save_default_args (parser, decl);
11150     }
11151
11152   /* Finish processing the declaration.  But, skip friend
11153      declarations.  */
11154   if (!friend_p && decl && decl != error_mark_node)
11155     {
11156       cp_finish_decl (decl,
11157                       initializer, !is_non_constant_init,
11158                       asm_specification,
11159                       /* If the initializer is in parentheses, then this is
11160                          a direct-initialization, which means that an
11161                          `explicit' constructor is OK.  Otherwise, an
11162                          `explicit' constructor cannot be used.  */
11163                       ((is_parenthesized_init || !is_initialized)
11164                      ? 0 : LOOKUP_ONLYCONVERTING));
11165     }
11166   if (!friend_p && pushed_scope)
11167     pop_scope (pushed_scope);
11168
11169   return decl;
11170 }
11171
11172 /* Parse a declarator.
11173
11174    declarator:
11175      direct-declarator
11176      ptr-operator declarator
11177
11178    abstract-declarator:
11179      ptr-operator abstract-declarator [opt]
11180      direct-abstract-declarator
11181
11182    GNU Extensions:
11183
11184    declarator:
11185      attributes [opt] direct-declarator
11186      attributes [opt] ptr-operator declarator
11187
11188    abstract-declarator:
11189      attributes [opt] ptr-operator abstract-declarator [opt]
11190      attributes [opt] direct-abstract-declarator
11191
11192    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11193    detect constructor, destructor or conversion operators. It is set
11194    to -1 if the declarator is a name, and +1 if it is a
11195    function. Otherwise it is set to zero. Usually you just want to
11196    test for >0, but internally the negative value is used.
11197
11198    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11199    a decl-specifier-seq unless it declares a constructor, destructor,
11200    or conversion.  It might seem that we could check this condition in
11201    semantic analysis, rather than parsing, but that makes it difficult
11202    to handle something like `f()'.  We want to notice that there are
11203    no decl-specifiers, and therefore realize that this is an
11204    expression, not a declaration.)
11205
11206    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11207    the declarator is a direct-declarator of the form "(...)".
11208
11209    MEMBER_P is true iff this declarator is a member-declarator.  */
11210
11211 static cp_declarator *
11212 cp_parser_declarator (cp_parser* parser,
11213                       cp_parser_declarator_kind dcl_kind,
11214                       int* ctor_dtor_or_conv_p,
11215                       bool* parenthesized_p,
11216                       bool member_p)
11217 {
11218   cp_token *token;
11219   cp_declarator *declarator;
11220   enum tree_code code;
11221   cp_cv_quals cv_quals;
11222   tree class_type;
11223   tree attributes = NULL_TREE;
11224
11225   /* Assume this is not a constructor, destructor, or type-conversion
11226      operator.  */
11227   if (ctor_dtor_or_conv_p)
11228     *ctor_dtor_or_conv_p = 0;
11229
11230   if (cp_parser_allow_gnu_extensions_p (parser))
11231     attributes = cp_parser_attributes_opt (parser);
11232
11233   /* Peek at the next token.  */
11234   token = cp_lexer_peek_token (parser->lexer);
11235
11236   /* Check for the ptr-operator production.  */
11237   cp_parser_parse_tentatively (parser);
11238   /* Parse the ptr-operator.  */
11239   code = cp_parser_ptr_operator (parser,
11240                                  &class_type,
11241                                  &cv_quals);
11242   /* If that worked, then we have a ptr-operator.  */
11243   if (cp_parser_parse_definitely (parser))
11244     {
11245       /* If a ptr-operator was found, then this declarator was not
11246          parenthesized.  */
11247       if (parenthesized_p)
11248         *parenthesized_p = true;
11249       /* The dependent declarator is optional if we are parsing an
11250          abstract-declarator.  */
11251       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11252         cp_parser_parse_tentatively (parser);
11253
11254       /* Parse the dependent declarator.  */
11255       declarator = cp_parser_declarator (parser, dcl_kind,
11256                                          /*ctor_dtor_or_conv_p=*/NULL,
11257                                          /*parenthesized_p=*/NULL,
11258                                          /*member_p=*/false);
11259
11260       /* If we are parsing an abstract-declarator, we must handle the
11261          case where the dependent declarator is absent.  */
11262       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11263           && !cp_parser_parse_definitely (parser))
11264         declarator = NULL;
11265
11266       /* Build the representation of the ptr-operator.  */
11267       if (class_type)
11268         declarator = make_ptrmem_declarator (cv_quals,
11269                                              class_type,
11270                                              declarator);
11271       else if (code == INDIRECT_REF)
11272         declarator = make_pointer_declarator (cv_quals, declarator);
11273       else
11274         declarator = make_reference_declarator (cv_quals, declarator);
11275     }
11276   /* Everything else is a direct-declarator.  */
11277   else
11278     {
11279       if (parenthesized_p)
11280         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11281                                                    CPP_OPEN_PAREN);
11282       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11283                                                 ctor_dtor_or_conv_p,
11284                                                 member_p);
11285     }
11286
11287   if (attributes && declarator && declarator != cp_error_declarator)
11288     declarator->attributes = attributes;
11289
11290   return declarator;
11291 }
11292
11293 /* Parse a direct-declarator or direct-abstract-declarator.
11294
11295    direct-declarator:
11296      declarator-id
11297      direct-declarator ( parameter-declaration-clause )
11298        cv-qualifier-seq [opt]
11299        exception-specification [opt]
11300      direct-declarator [ constant-expression [opt] ]
11301      ( declarator )
11302
11303    direct-abstract-declarator:
11304      direct-abstract-declarator [opt]
11305        ( parameter-declaration-clause )
11306        cv-qualifier-seq [opt]
11307        exception-specification [opt]
11308      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11309      ( abstract-declarator )
11310
11311    Returns a representation of the declarator.  DCL_KIND is
11312    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11313    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11314    we are parsing a direct-declarator.  It is
11315    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11316    of ambiguity we prefer an abstract declarator, as per
11317    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11318    cp_parser_declarator.  */
11319
11320 static cp_declarator *
11321 cp_parser_direct_declarator (cp_parser* parser,
11322                              cp_parser_declarator_kind dcl_kind,
11323                              int* ctor_dtor_or_conv_p,
11324                              bool member_p)
11325 {
11326   cp_token *token;
11327   cp_declarator *declarator = NULL;
11328   tree scope = NULL_TREE;
11329   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11330   bool saved_in_declarator_p = parser->in_declarator_p;
11331   bool first = true;
11332   tree pushed_scope = NULL_TREE;
11333
11334   while (true)
11335     {
11336       /* Peek at the next token.  */
11337       token = cp_lexer_peek_token (parser->lexer);
11338       if (token->type == CPP_OPEN_PAREN)
11339         {
11340           /* This is either a parameter-declaration-clause, or a
11341              parenthesized declarator. When we know we are parsing a
11342              named declarator, it must be a parenthesized declarator
11343              if FIRST is true. For instance, `(int)' is a
11344              parameter-declaration-clause, with an omitted
11345              direct-abstract-declarator. But `((*))', is a
11346              parenthesized abstract declarator. Finally, when T is a
11347              template parameter `(T)' is a
11348              parameter-declaration-clause, and not a parenthesized
11349              named declarator.
11350
11351              We first try and parse a parameter-declaration-clause,
11352              and then try a nested declarator (if FIRST is true).
11353
11354              It is not an error for it not to be a
11355              parameter-declaration-clause, even when FIRST is
11356              false. Consider,
11357
11358                int i (int);
11359                int i (3);
11360
11361              The first is the declaration of a function while the
11362              second is a the definition of a variable, including its
11363              initializer.
11364
11365              Having seen only the parenthesis, we cannot know which of
11366              these two alternatives should be selected.  Even more
11367              complex are examples like:
11368
11369                int i (int (a));
11370                int i (int (3));
11371
11372              The former is a function-declaration; the latter is a
11373              variable initialization.
11374
11375              Thus again, we try a parameter-declaration-clause, and if
11376              that fails, we back out and return.  */
11377
11378           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11379             {
11380               cp_parameter_declarator *params;
11381               unsigned saved_num_template_parameter_lists;
11382
11383               /* In a member-declarator, the only valid interpretation
11384                  of a parenthesis is the start of a
11385                  parameter-declaration-clause.  (It is invalid to
11386                  initialize a static data member with a parenthesized
11387                  initializer; only the "=" form of initialization is
11388                  permitted.)  */
11389               if (!member_p)
11390                 cp_parser_parse_tentatively (parser);
11391
11392               /* Consume the `('.  */
11393               cp_lexer_consume_token (parser->lexer);
11394               if (first)
11395                 {
11396                   /* If this is going to be an abstract declarator, we're
11397                      in a declarator and we can't have default args.  */
11398                   parser->default_arg_ok_p = false;
11399                   parser->in_declarator_p = true;
11400                 }
11401
11402               /* Inside the function parameter list, surrounding
11403                  template-parameter-lists do not apply.  */
11404               saved_num_template_parameter_lists
11405                 = parser->num_template_parameter_lists;
11406               parser->num_template_parameter_lists = 0;
11407
11408               /* Parse the parameter-declaration-clause.  */
11409               params = cp_parser_parameter_declaration_clause (parser);
11410
11411               parser->num_template_parameter_lists
11412                 = saved_num_template_parameter_lists;
11413
11414               /* If all went well, parse the cv-qualifier-seq and the
11415                  exception-specification.  */
11416               if (member_p || cp_parser_parse_definitely (parser))
11417                 {
11418                   cp_cv_quals cv_quals;
11419                   tree exception_specification;
11420
11421                   if (ctor_dtor_or_conv_p)
11422                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11423                   first = false;
11424                   /* Consume the `)'.  */
11425                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11426
11427                   /* Parse the cv-qualifier-seq.  */
11428                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11429                   /* And the exception-specification.  */
11430                   exception_specification
11431                     = cp_parser_exception_specification_opt (parser);
11432
11433                   /* Create the function-declarator.  */
11434                   declarator = make_call_declarator (declarator,
11435                                                      params,
11436                                                      cv_quals,
11437                                                      exception_specification);
11438                   /* Any subsequent parameter lists are to do with
11439                      return type, so are not those of the declared
11440                      function.  */
11441                   parser->default_arg_ok_p = false;
11442
11443                   /* Repeat the main loop.  */
11444                   continue;
11445                 }
11446             }
11447
11448           /* If this is the first, we can try a parenthesized
11449              declarator.  */
11450           if (first)
11451             {
11452               bool saved_in_type_id_in_expr_p;
11453
11454               parser->default_arg_ok_p = saved_default_arg_ok_p;
11455               parser->in_declarator_p = saved_in_declarator_p;
11456
11457               /* Consume the `('.  */
11458               cp_lexer_consume_token (parser->lexer);
11459               /* Parse the nested declarator.  */
11460               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11461               parser->in_type_id_in_expr_p = true;
11462               declarator
11463                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11464                                         /*parenthesized_p=*/NULL,
11465                                         member_p);
11466               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11467               first = false;
11468               /* Expect a `)'.  */
11469               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11470                 declarator = cp_error_declarator;
11471               if (declarator == cp_error_declarator)
11472                 break;
11473
11474               goto handle_declarator;
11475             }
11476           /* Otherwise, we must be done.  */
11477           else
11478             break;
11479         }
11480       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11481                && token->type == CPP_OPEN_SQUARE)
11482         {
11483           /* Parse an array-declarator.  */
11484           tree bounds;
11485
11486           if (ctor_dtor_or_conv_p)
11487             *ctor_dtor_or_conv_p = 0;
11488
11489           first = false;
11490           parser->default_arg_ok_p = false;
11491           parser->in_declarator_p = true;
11492           /* Consume the `['.  */
11493           cp_lexer_consume_token (parser->lexer);
11494           /* Peek at the next token.  */
11495           token = cp_lexer_peek_token (parser->lexer);
11496           /* If the next token is `]', then there is no
11497              constant-expression.  */
11498           if (token->type != CPP_CLOSE_SQUARE)
11499             {
11500               bool non_constant_p;
11501
11502               bounds
11503                 = cp_parser_constant_expression (parser,
11504                                                  /*allow_non_constant=*/true,
11505                                                  &non_constant_p);
11506               if (!non_constant_p)
11507                 bounds = fold_non_dependent_expr (bounds);
11508               /* Normally, the array bound must be an integral constant
11509                  expression.  However, as an extension, we allow VLAs
11510                  in function scopes.  */
11511               else if (!at_function_scope_p ())
11512                 {
11513                   error ("array bound is not an integer constant");
11514                   bounds = error_mark_node;
11515                 }
11516             }
11517           else
11518             bounds = NULL_TREE;
11519           /* Look for the closing `]'.  */
11520           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11521             {
11522               declarator = cp_error_declarator;
11523               break;
11524             }
11525
11526           declarator = make_array_declarator (declarator, bounds);
11527         }
11528       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11529         {
11530           tree qualifying_scope;
11531           tree unqualified_name;
11532           special_function_kind sfk;
11533           bool abstract_ok;
11534
11535           /* Parse a declarator-id */
11536           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11537           if (abstract_ok)
11538             cp_parser_parse_tentatively (parser);
11539           unqualified_name
11540             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11541           qualifying_scope = parser->scope;
11542           if (abstract_ok)
11543             {
11544               if (!cp_parser_parse_definitely (parser))
11545                 unqualified_name = error_mark_node;
11546               else if (unqualified_name
11547                        && (qualifying_scope
11548                            || (TREE_CODE (unqualified_name)
11549                                != IDENTIFIER_NODE)))
11550                 {
11551                   cp_parser_error (parser, "expected unqualified-id");
11552                   unqualified_name = error_mark_node;
11553                 }
11554             }
11555
11556           if (!unqualified_name)
11557             return NULL;
11558           if (unqualified_name == error_mark_node)
11559             {
11560               declarator = cp_error_declarator;
11561               break;
11562             }
11563
11564           if (qualifying_scope && at_namespace_scope_p ()
11565               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11566             {
11567               /* In the declaration of a member of a template class
11568                  outside of the class itself, the SCOPE will sometimes
11569                  be a TYPENAME_TYPE.  For example, given:
11570
11571                  template <typename T>
11572                  int S<T>::R::i = 3;
11573
11574                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11575                  this context, we must resolve S<T>::R to an ordinary
11576                  type, rather than a typename type.
11577
11578                  The reason we normally avoid resolving TYPENAME_TYPEs
11579                  is that a specialization of `S' might render
11580                  `S<T>::R' not a type.  However, if `S' is
11581                  specialized, then this `i' will not be used, so there
11582                  is no harm in resolving the types here.  */
11583               tree type;
11584
11585               /* Resolve the TYPENAME_TYPE.  */
11586               type = resolve_typename_type (qualifying_scope,
11587                                             /*only_current_p=*/false);
11588               /* If that failed, the declarator is invalid.  */
11589               if (type == error_mark_node)
11590                 error ("%<%T::%D%> is not a type",
11591                        TYPE_CONTEXT (qualifying_scope),
11592                        TYPE_IDENTIFIER (qualifying_scope));
11593               qualifying_scope = type;
11594             }
11595
11596           sfk = sfk_none;
11597           if (unqualified_name)
11598             {
11599               tree class_type;
11600
11601               if (qualifying_scope
11602                   && CLASS_TYPE_P (qualifying_scope))
11603                 class_type = qualifying_scope;
11604               else
11605                 class_type = current_class_type;
11606
11607               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11608                 {
11609                   tree name_type = TREE_TYPE (unqualified_name);
11610                   if (class_type && same_type_p (name_type, class_type))
11611                     {
11612                       if (qualifying_scope
11613                           && CLASSTYPE_USE_TEMPLATE (name_type))
11614                         {
11615                           error ("invalid use of constructor as a template");
11616                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11617                                   "name the constructor in a qualified name",
11618                                   class_type,
11619                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11620                                   class_type, name_type);
11621                           declarator = cp_error_declarator;
11622                           break;
11623                         }
11624                       else
11625                         unqualified_name = constructor_name (class_type);
11626                     }
11627                   else
11628                     {
11629                       /* We do not attempt to print the declarator
11630                          here because we do not have enough
11631                          information about its original syntactic
11632                          form.  */
11633                       cp_parser_error (parser, "invalid declarator");
11634                       declarator = cp_error_declarator;
11635                       break;
11636                     }
11637                 }
11638
11639               if (class_type)
11640                 {
11641                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11642                     sfk = sfk_destructor;
11643                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11644                     sfk = sfk_conversion;
11645                   else if (/* There's no way to declare a constructor
11646                               for an anonymous type, even if the type
11647                               got a name for linkage purposes.  */
11648                            !TYPE_WAS_ANONYMOUS (class_type)
11649                            && constructor_name_p (unqualified_name,
11650                                                   class_type))
11651                     {
11652                       unqualified_name = constructor_name (class_type);
11653                       sfk = sfk_constructor;
11654                     }
11655
11656                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11657                     *ctor_dtor_or_conv_p = -1;
11658                 }
11659             }
11660           declarator = make_id_declarator (qualifying_scope,
11661                                            unqualified_name,
11662                                            sfk);
11663           declarator->id_loc = token->location;
11664
11665         handle_declarator:;
11666           scope = get_scope_of_declarator (declarator);
11667           if (scope)
11668             /* Any names that appear after the declarator-id for a
11669                member are looked up in the containing scope.  */
11670             pushed_scope = push_scope (scope);
11671           parser->in_declarator_p = true;
11672           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11673               || (declarator && declarator->kind == cdk_id))
11674             /* Default args are only allowed on function
11675                declarations.  */
11676             parser->default_arg_ok_p = saved_default_arg_ok_p;
11677           else
11678             parser->default_arg_ok_p = false;
11679
11680           first = false;
11681         }
11682       /* We're done.  */
11683       else
11684         break;
11685     }
11686
11687   /* For an abstract declarator, we might wind up with nothing at this
11688      point.  That's an error; the declarator is not optional.  */
11689   if (!declarator)
11690     cp_parser_error (parser, "expected declarator");
11691
11692   /* If we entered a scope, we must exit it now.  */
11693   if (pushed_scope)
11694     pop_scope (pushed_scope);
11695
11696   parser->default_arg_ok_p = saved_default_arg_ok_p;
11697   parser->in_declarator_p = saved_in_declarator_p;
11698
11699   return declarator;
11700 }
11701
11702 /* Parse a ptr-operator.
11703
11704    ptr-operator:
11705      * cv-qualifier-seq [opt]
11706      &
11707      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11708
11709    GNU Extension:
11710
11711    ptr-operator:
11712      & cv-qualifier-seq [opt]
11713
11714    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11715    Returns ADDR_EXPR if a reference was used.  In the case of a
11716    pointer-to-member, *TYPE is filled in with the TYPE containing the
11717    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11718    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11719    ERROR_MARK if an error occurred.  */
11720
11721 static enum tree_code
11722 cp_parser_ptr_operator (cp_parser* parser,
11723                         tree* type,
11724                         cp_cv_quals *cv_quals)
11725 {
11726   enum tree_code code = ERROR_MARK;
11727   cp_token *token;
11728
11729   /* Assume that it's not a pointer-to-member.  */
11730   *type = NULL_TREE;
11731   /* And that there are no cv-qualifiers.  */
11732   *cv_quals = TYPE_UNQUALIFIED;
11733
11734   /* Peek at the next token.  */
11735   token = cp_lexer_peek_token (parser->lexer);
11736   /* If it's a `*' or `&' we have a pointer or reference.  */
11737   if (token->type == CPP_MULT || token->type == CPP_AND)
11738     {
11739       /* Remember which ptr-operator we were processing.  */
11740       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11741
11742       /* Consume the `*' or `&'.  */
11743       cp_lexer_consume_token (parser->lexer);
11744
11745       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11746          `&', if we are allowing GNU extensions.  (The only qualifier
11747          that can legally appear after `&' is `restrict', but that is
11748          enforced during semantic analysis.  */
11749       if (code == INDIRECT_REF
11750           || cp_parser_allow_gnu_extensions_p (parser))
11751         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11752     }
11753   else
11754     {
11755       /* Try the pointer-to-member case.  */
11756       cp_parser_parse_tentatively (parser);
11757       /* Look for the optional `::' operator.  */
11758       cp_parser_global_scope_opt (parser,
11759                                   /*current_scope_valid_p=*/false);
11760       /* Look for the nested-name specifier.  */
11761       cp_parser_nested_name_specifier (parser,
11762                                        /*typename_keyword_p=*/false,
11763                                        /*check_dependency_p=*/true,
11764                                        /*type_p=*/false,
11765                                        /*is_declaration=*/false);
11766       /* If we found it, and the next token is a `*', then we are
11767          indeed looking at a pointer-to-member operator.  */
11768       if (!cp_parser_error_occurred (parser)
11769           && cp_parser_require (parser, CPP_MULT, "`*'"))
11770         {
11771           /* Indicate that the `*' operator was used.  */
11772           code = INDIRECT_REF;
11773
11774           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11775             error ("%qD is a namespace", parser->scope);
11776           else
11777             {
11778               /* The type of which the member is a member is given by the
11779                  current SCOPE.  */
11780               *type = parser->scope;
11781               /* The next name will not be qualified.  */
11782               parser->scope = NULL_TREE;
11783               parser->qualifying_scope = NULL_TREE;
11784               parser->object_scope = NULL_TREE;
11785               /* Look for the optional cv-qualifier-seq.  */
11786               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11787             }
11788         }
11789       /* If that didn't work we don't have a ptr-operator.  */
11790       if (!cp_parser_parse_definitely (parser))
11791         cp_parser_error (parser, "expected ptr-operator");
11792     }
11793
11794   return code;
11795 }
11796
11797 /* Parse an (optional) cv-qualifier-seq.
11798
11799    cv-qualifier-seq:
11800      cv-qualifier cv-qualifier-seq [opt]
11801
11802    cv-qualifier:
11803      const
11804      volatile
11805
11806    GNU Extension:
11807
11808    cv-qualifier:
11809      __restrict__
11810
11811    Returns a bitmask representing the cv-qualifiers.  */
11812
11813 static cp_cv_quals
11814 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11815 {
11816   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11817
11818   while (true)
11819     {
11820       cp_token *token;
11821       cp_cv_quals cv_qualifier;
11822
11823       /* Peek at the next token.  */
11824       token = cp_lexer_peek_token (parser->lexer);
11825       /* See if it's a cv-qualifier.  */
11826       switch (token->keyword)
11827         {
11828         case RID_CONST:
11829           cv_qualifier = TYPE_QUAL_CONST;
11830           break;
11831
11832         case RID_VOLATILE:
11833           cv_qualifier = TYPE_QUAL_VOLATILE;
11834           break;
11835
11836         case RID_RESTRICT:
11837           cv_qualifier = TYPE_QUAL_RESTRICT;
11838           break;
11839
11840         default:
11841           cv_qualifier = TYPE_UNQUALIFIED;
11842           break;
11843         }
11844
11845       if (!cv_qualifier)
11846         break;
11847
11848       if (cv_quals & cv_qualifier)
11849         {
11850           error ("duplicate cv-qualifier");
11851           cp_lexer_purge_token (parser->lexer);
11852         }
11853       else
11854         {
11855           cp_lexer_consume_token (parser->lexer);
11856           cv_quals |= cv_qualifier;
11857         }
11858     }
11859
11860   return cv_quals;
11861 }
11862
11863 /* Parse a declarator-id.
11864
11865    declarator-id:
11866      id-expression
11867      :: [opt] nested-name-specifier [opt] type-name
11868
11869    In the `id-expression' case, the value returned is as for
11870    cp_parser_id_expression if the id-expression was an unqualified-id.
11871    If the id-expression was a qualified-id, then a SCOPE_REF is
11872    returned.  The first operand is the scope (either a NAMESPACE_DECL
11873    or TREE_TYPE), but the second is still just a representation of an
11874    unqualified-id.  */
11875
11876 static tree
11877 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11878 {
11879   tree id;
11880   /* The expression must be an id-expression.  Assume that qualified
11881      names are the names of types so that:
11882
11883        template <class T>
11884        int S<T>::R::i = 3;
11885
11886      will work; we must treat `S<T>::R' as the name of a type.
11887      Similarly, assume that qualified names are templates, where
11888      required, so that:
11889
11890        template <class T>
11891        int S<T>::R<T>::i = 3;
11892
11893      will work, too.  */
11894   id = cp_parser_id_expression (parser,
11895                                 /*template_keyword_p=*/false,
11896                                 /*check_dependency_p=*/false,
11897                                 /*template_p=*/NULL,
11898                                 /*declarator_p=*/true,
11899                                 optional_p);
11900   if (id && BASELINK_P (id))
11901     id = BASELINK_FUNCTIONS (id);
11902   return id;
11903 }
11904
11905 /* Parse a type-id.
11906
11907    type-id:
11908      type-specifier-seq abstract-declarator [opt]
11909
11910    Returns the TYPE specified.  */
11911
11912 static tree
11913 cp_parser_type_id (cp_parser* parser)
11914 {
11915   cp_decl_specifier_seq type_specifier_seq;
11916   cp_declarator *abstract_declarator;
11917
11918   /* Parse the type-specifier-seq.  */
11919   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11920                                 &type_specifier_seq);
11921   if (type_specifier_seq.type == error_mark_node)
11922     return error_mark_node;
11923
11924   /* There might or might not be an abstract declarator.  */
11925   cp_parser_parse_tentatively (parser);
11926   /* Look for the declarator.  */
11927   abstract_declarator
11928     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11929                             /*parenthesized_p=*/NULL,
11930                             /*member_p=*/false);
11931   /* Check to see if there really was a declarator.  */
11932   if (!cp_parser_parse_definitely (parser))
11933     abstract_declarator = NULL;
11934
11935   return groktypename (&type_specifier_seq, abstract_declarator);
11936 }
11937
11938 /* Parse a type-specifier-seq.
11939
11940    type-specifier-seq:
11941      type-specifier type-specifier-seq [opt]
11942
11943    GNU extension:
11944
11945    type-specifier-seq:
11946      attributes type-specifier-seq [opt]
11947
11948    If IS_CONDITION is true, we are at the start of a "condition",
11949    e.g., we've just seen "if (".
11950
11951    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11952
11953 static void
11954 cp_parser_type_specifier_seq (cp_parser* parser,
11955                               bool is_condition,
11956                               cp_decl_specifier_seq *type_specifier_seq)
11957 {
11958   bool seen_type_specifier = false;
11959   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11960
11961   /* Clear the TYPE_SPECIFIER_SEQ.  */
11962   clear_decl_specs (type_specifier_seq);
11963
11964   /* Parse the type-specifiers and attributes.  */
11965   while (true)
11966     {
11967       tree type_specifier;
11968       bool is_cv_qualifier;
11969
11970       /* Check for attributes first.  */
11971       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11972         {
11973           type_specifier_seq->attributes =
11974             chainon (type_specifier_seq->attributes,
11975                      cp_parser_attributes_opt (parser));
11976           continue;
11977         }
11978
11979       /* Look for the type-specifier.  */
11980       type_specifier = cp_parser_type_specifier (parser,
11981                                                  flags,
11982                                                  type_specifier_seq,
11983                                                  /*is_declaration=*/false,
11984                                                  NULL,
11985                                                  &is_cv_qualifier);
11986       if (!type_specifier)
11987         {
11988           /* If the first type-specifier could not be found, this is not a
11989              type-specifier-seq at all.  */
11990           if (!seen_type_specifier)
11991             {
11992               cp_parser_error (parser, "expected type-specifier");
11993               type_specifier_seq->type = error_mark_node;
11994               return;
11995             }
11996           /* If subsequent type-specifiers could not be found, the
11997              type-specifier-seq is complete.  */
11998           break;
11999         }
12000
12001       seen_type_specifier = true;
12002       /* The standard says that a condition can be:
12003
12004             type-specifier-seq declarator = assignment-expression
12005
12006          However, given:
12007
12008            struct S {};
12009            if (int S = ...)
12010
12011          we should treat the "S" as a declarator, not as a
12012          type-specifier.  The standard doesn't say that explicitly for
12013          type-specifier-seq, but it does say that for
12014          decl-specifier-seq in an ordinary declaration.  Perhaps it
12015          would be clearer just to allow a decl-specifier-seq here, and
12016          then add a semantic restriction that if any decl-specifiers
12017          that are not type-specifiers appear, the program is invalid.  */
12018       if (is_condition && !is_cv_qualifier)
12019         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12020     }
12021
12022   cp_parser_check_decl_spec (type_specifier_seq);
12023 }
12024
12025 /* Parse a parameter-declaration-clause.
12026
12027    parameter-declaration-clause:
12028      parameter-declaration-list [opt] ... [opt]
12029      parameter-declaration-list , ...
12030
12031    Returns a representation for the parameter declarations.  A return
12032    value of NULL indicates a parameter-declaration-clause consisting
12033    only of an ellipsis.  */
12034
12035 static cp_parameter_declarator *
12036 cp_parser_parameter_declaration_clause (cp_parser* parser)
12037 {
12038   cp_parameter_declarator *parameters;
12039   cp_token *token;
12040   bool ellipsis_p;
12041   bool is_error;
12042
12043   /* Peek at the next token.  */
12044   token = cp_lexer_peek_token (parser->lexer);
12045   /* Check for trivial parameter-declaration-clauses.  */
12046   if (token->type == CPP_ELLIPSIS)
12047     {
12048       /* Consume the `...' token.  */
12049       cp_lexer_consume_token (parser->lexer);
12050       return NULL;
12051     }
12052   else if (token->type == CPP_CLOSE_PAREN)
12053     /* There are no parameters.  */
12054     {
12055 #ifndef NO_IMPLICIT_EXTERN_C
12056       if (in_system_header && current_class_type == NULL
12057           && current_lang_name == lang_name_c)
12058         return NULL;
12059       else
12060 #endif
12061         return no_parameters;
12062     }
12063   /* Check for `(void)', too, which is a special case.  */
12064   else if (token->keyword == RID_VOID
12065            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12066                == CPP_CLOSE_PAREN))
12067     {
12068       /* Consume the `void' token.  */
12069       cp_lexer_consume_token (parser->lexer);
12070       /* There are no parameters.  */
12071       return no_parameters;
12072     }
12073
12074   /* Parse the parameter-declaration-list.  */
12075   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12076   /* If a parse error occurred while parsing the
12077      parameter-declaration-list, then the entire
12078      parameter-declaration-clause is erroneous.  */
12079   if (is_error)
12080     return NULL;
12081
12082   /* Peek at the next token.  */
12083   token = cp_lexer_peek_token (parser->lexer);
12084   /* If it's a `,', the clause should terminate with an ellipsis.  */
12085   if (token->type == CPP_COMMA)
12086     {
12087       /* Consume the `,'.  */
12088       cp_lexer_consume_token (parser->lexer);
12089       /* Expect an ellipsis.  */
12090       ellipsis_p
12091         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12092     }
12093   /* It might also be `...' if the optional trailing `,' was
12094      omitted.  */
12095   else if (token->type == CPP_ELLIPSIS)
12096     {
12097       /* Consume the `...' token.  */
12098       cp_lexer_consume_token (parser->lexer);
12099       /* And remember that we saw it.  */
12100       ellipsis_p = true;
12101     }
12102   else
12103     ellipsis_p = false;
12104
12105   /* Finish the parameter list.  */
12106   if (parameters && ellipsis_p)
12107     parameters->ellipsis_p = true;
12108
12109   return parameters;
12110 }
12111
12112 /* Parse a parameter-declaration-list.
12113
12114    parameter-declaration-list:
12115      parameter-declaration
12116      parameter-declaration-list , parameter-declaration
12117
12118    Returns a representation of the parameter-declaration-list, as for
12119    cp_parser_parameter_declaration_clause.  However, the
12120    `void_list_node' is never appended to the list.  Upon return,
12121    *IS_ERROR will be true iff an error occurred.  */
12122
12123 static cp_parameter_declarator *
12124 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12125 {
12126   cp_parameter_declarator *parameters = NULL;
12127   cp_parameter_declarator **tail = &parameters;
12128   bool saved_in_unbraced_linkage_specification_p;
12129
12130   /* Assume all will go well.  */
12131   *is_error = false;
12132   /* The special considerations that apply to a function within an
12133      unbraced linkage specifications do not apply to the parameters
12134      to the function.  */
12135   saved_in_unbraced_linkage_specification_p 
12136     = parser->in_unbraced_linkage_specification_p;
12137   parser->in_unbraced_linkage_specification_p = false;
12138
12139   /* Look for more parameters.  */
12140   while (true)
12141     {
12142       cp_parameter_declarator *parameter;
12143       bool parenthesized_p;
12144       /* Parse the parameter.  */
12145       parameter
12146         = cp_parser_parameter_declaration (parser,
12147                                            /*template_parm_p=*/false,
12148                                            &parenthesized_p);
12149
12150       /* If a parse error occurred parsing the parameter declaration,
12151          then the entire parameter-declaration-list is erroneous.  */
12152       if (!parameter)
12153         {
12154           *is_error = true;
12155           parameters = NULL;
12156           break;
12157         }
12158       /* Add the new parameter to the list.  */
12159       *tail = parameter;
12160       tail = &parameter->next;
12161
12162       /* Peek at the next token.  */
12163       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12164           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12165           /* These are for Objective-C++ */
12166           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12167           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12168         /* The parameter-declaration-list is complete.  */
12169         break;
12170       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12171         {
12172           cp_token *token;
12173
12174           /* Peek at the next token.  */
12175           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12176           /* If it's an ellipsis, then the list is complete.  */
12177           if (token->type == CPP_ELLIPSIS)
12178             break;
12179           /* Otherwise, there must be more parameters.  Consume the
12180              `,'.  */
12181           cp_lexer_consume_token (parser->lexer);
12182           /* When parsing something like:
12183
12184                 int i(float f, double d)
12185
12186              we can tell after seeing the declaration for "f" that we
12187              are not looking at an initialization of a variable "i",
12188              but rather at the declaration of a function "i".
12189
12190              Due to the fact that the parsing of template arguments
12191              (as specified to a template-id) requires backtracking we
12192              cannot use this technique when inside a template argument
12193              list.  */
12194           if (!parser->in_template_argument_list_p
12195               && !parser->in_type_id_in_expr_p
12196               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12197               /* However, a parameter-declaration of the form
12198                  "foat(f)" (which is a valid declaration of a
12199                  parameter "f") can also be interpreted as an
12200                  expression (the conversion of "f" to "float").  */
12201               && !parenthesized_p)
12202             cp_parser_commit_to_tentative_parse (parser);
12203         }
12204       else
12205         {
12206           cp_parser_error (parser, "expected %<,%> or %<...%>");
12207           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12208             cp_parser_skip_to_closing_parenthesis (parser,
12209                                                    /*recovering=*/true,
12210                                                    /*or_comma=*/false,
12211                                                    /*consume_paren=*/false);
12212           break;
12213         }
12214     }
12215
12216   parser->in_unbraced_linkage_specification_p
12217     = saved_in_unbraced_linkage_specification_p;
12218
12219   return parameters;
12220 }
12221
12222 /* Parse a parameter declaration.
12223
12224    parameter-declaration:
12225      decl-specifier-seq declarator
12226      decl-specifier-seq declarator = assignment-expression
12227      decl-specifier-seq abstract-declarator [opt]
12228      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12229
12230    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12231    declares a template parameter.  (In that case, a non-nested `>'
12232    token encountered during the parsing of the assignment-expression
12233    is not interpreted as a greater-than operator.)
12234
12235    Returns a representation of the parameter, or NULL if an error
12236    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12237    true iff the declarator is of the form "(p)".  */
12238
12239 static cp_parameter_declarator *
12240 cp_parser_parameter_declaration (cp_parser *parser,
12241                                  bool template_parm_p,
12242                                  bool *parenthesized_p)
12243 {
12244   int declares_class_or_enum;
12245   bool greater_than_is_operator_p;
12246   cp_decl_specifier_seq decl_specifiers;
12247   cp_declarator *declarator;
12248   tree default_argument;
12249   cp_token *token;
12250   const char *saved_message;
12251
12252   /* In a template parameter, `>' is not an operator.
12253
12254      [temp.param]
12255
12256      When parsing a default template-argument for a non-type
12257      template-parameter, the first non-nested `>' is taken as the end
12258      of the template parameter-list rather than a greater-than
12259      operator.  */
12260   greater_than_is_operator_p = !template_parm_p;
12261
12262   /* Type definitions may not appear in parameter types.  */
12263   saved_message = parser->type_definition_forbidden_message;
12264   parser->type_definition_forbidden_message
12265     = "types may not be defined in parameter types";
12266
12267   /* Parse the declaration-specifiers.  */
12268   cp_parser_decl_specifier_seq (parser,
12269                                 CP_PARSER_FLAGS_NONE,
12270                                 &decl_specifiers,
12271                                 &declares_class_or_enum);
12272   /* If an error occurred, there's no reason to attempt to parse the
12273      rest of the declaration.  */
12274   if (cp_parser_error_occurred (parser))
12275     {
12276       parser->type_definition_forbidden_message = saved_message;
12277       return NULL;
12278     }
12279
12280   /* Peek at the next token.  */
12281   token = cp_lexer_peek_token (parser->lexer);
12282   /* If the next token is a `)', `,', `=', `>', or `...', then there
12283      is no declarator.  */
12284   if (token->type == CPP_CLOSE_PAREN
12285       || token->type == CPP_COMMA
12286       || token->type == CPP_EQ
12287       || token->type == CPP_ELLIPSIS
12288       || token->type == CPP_GREATER)
12289     {
12290       declarator = NULL;
12291       if (parenthesized_p)
12292         *parenthesized_p = false;
12293     }
12294   /* Otherwise, there should be a declarator.  */
12295   else
12296     {
12297       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12298       parser->default_arg_ok_p = false;
12299
12300       /* After seeing a decl-specifier-seq, if the next token is not a
12301          "(", there is no possibility that the code is a valid
12302          expression.  Therefore, if parsing tentatively, we commit at
12303          this point.  */
12304       if (!parser->in_template_argument_list_p
12305           /* In an expression context, having seen:
12306
12307                (int((char ...
12308
12309              we cannot be sure whether we are looking at a
12310              function-type (taking a "char" as a parameter) or a cast
12311              of some object of type "char" to "int".  */
12312           && !parser->in_type_id_in_expr_p
12313           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12314           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12315         cp_parser_commit_to_tentative_parse (parser);
12316       /* Parse the declarator.  */
12317       declarator = cp_parser_declarator (parser,
12318                                          CP_PARSER_DECLARATOR_EITHER,
12319                                          /*ctor_dtor_or_conv_p=*/NULL,
12320                                          parenthesized_p,
12321                                          /*member_p=*/false);
12322       parser->default_arg_ok_p = saved_default_arg_ok_p;
12323       /* After the declarator, allow more attributes.  */
12324       decl_specifiers.attributes
12325         = chainon (decl_specifiers.attributes,
12326                    cp_parser_attributes_opt (parser));
12327     }
12328
12329   /* The restriction on defining new types applies only to the type
12330      of the parameter, not to the default argument.  */
12331   parser->type_definition_forbidden_message = saved_message;
12332
12333   /* If the next token is `=', then process a default argument.  */
12334   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12335     {
12336       bool saved_greater_than_is_operator_p;
12337       /* Consume the `='.  */
12338       cp_lexer_consume_token (parser->lexer);
12339
12340       /* If we are defining a class, then the tokens that make up the
12341          default argument must be saved and processed later.  */
12342       if (!template_parm_p && at_class_scope_p ()
12343           && TYPE_BEING_DEFINED (current_class_type))
12344         {
12345           unsigned depth = 0;
12346           cp_token *first_token;
12347           cp_token *token;
12348
12349           /* Add tokens until we have processed the entire default
12350              argument.  We add the range [first_token, token).  */
12351           first_token = cp_lexer_peek_token (parser->lexer);
12352           while (true)
12353             {
12354               bool done = false;
12355
12356               /* Peek at the next token.  */
12357               token = cp_lexer_peek_token (parser->lexer);
12358               /* What we do depends on what token we have.  */
12359               switch (token->type)
12360                 {
12361                   /* In valid code, a default argument must be
12362                      immediately followed by a `,' `)', or `...'.  */
12363                 case CPP_COMMA:
12364                 case CPP_CLOSE_PAREN:
12365                 case CPP_ELLIPSIS:
12366                   /* If we run into a non-nested `;', `}', or `]',
12367                      then the code is invalid -- but the default
12368                      argument is certainly over.  */
12369                 case CPP_SEMICOLON:
12370                 case CPP_CLOSE_BRACE:
12371                 case CPP_CLOSE_SQUARE:
12372                   if (depth == 0)
12373                     done = true;
12374                   /* Update DEPTH, if necessary.  */
12375                   else if (token->type == CPP_CLOSE_PAREN
12376                            || token->type == CPP_CLOSE_BRACE
12377                            || token->type == CPP_CLOSE_SQUARE)
12378                     --depth;
12379                   break;
12380
12381                 case CPP_OPEN_PAREN:
12382                 case CPP_OPEN_SQUARE:
12383                 case CPP_OPEN_BRACE:
12384                   ++depth;
12385                   break;
12386
12387                 case CPP_GREATER:
12388                   /* If we see a non-nested `>', and `>' is not an
12389                      operator, then it marks the end of the default
12390                      argument.  */
12391                   if (!depth && !greater_than_is_operator_p)
12392                     done = true;
12393                   break;
12394
12395                   /* If we run out of tokens, issue an error message.  */
12396                 case CPP_EOF:
12397                 case CPP_PRAGMA_EOL:
12398                   error ("file ends in default argument");
12399                   done = true;
12400                   break;
12401
12402                 case CPP_NAME:
12403                 case CPP_SCOPE:
12404                   /* In these cases, we should look for template-ids.
12405                      For example, if the default argument is
12406                      `X<int, double>()', we need to do name lookup to
12407                      figure out whether or not `X' is a template; if
12408                      so, the `,' does not end the default argument.
12409
12410                      That is not yet done.  */
12411                   break;
12412
12413                 default:
12414                   break;
12415                 }
12416
12417               /* If we've reached the end, stop.  */
12418               if (done)
12419                 break;
12420
12421               /* Add the token to the token block.  */
12422               token = cp_lexer_consume_token (parser->lexer);
12423             }
12424
12425           /* Create a DEFAULT_ARG to represented the unparsed default
12426              argument.  */
12427           default_argument = make_node (DEFAULT_ARG);
12428           DEFARG_TOKENS (default_argument)
12429             = cp_token_cache_new (first_token, token);
12430           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12431         }
12432       /* Outside of a class definition, we can just parse the
12433          assignment-expression.  */
12434       else
12435         {
12436           bool saved_local_variables_forbidden_p;
12437
12438           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12439              set correctly.  */
12440           saved_greater_than_is_operator_p
12441             = parser->greater_than_is_operator_p;
12442           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12443           /* Local variable names (and the `this' keyword) may not
12444              appear in a default argument.  */
12445           saved_local_variables_forbidden_p
12446             = parser->local_variables_forbidden_p;
12447           parser->local_variables_forbidden_p = true;
12448           /* The default argument expression may cause implicitly
12449              defined member functions to be synthesized, which will
12450              result in garbage collection.  We must treat this
12451              situation as if we were within the body of function so as
12452              to avoid collecting live data on the stack.  */
12453           ++function_depth;
12454           /* Parse the assignment-expression.  */
12455           if (template_parm_p)
12456             push_deferring_access_checks (dk_no_deferred);
12457           default_argument
12458             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12459           if (template_parm_p)
12460             pop_deferring_access_checks ();
12461           /* Restore saved state.  */
12462           --function_depth;
12463           parser->greater_than_is_operator_p
12464             = saved_greater_than_is_operator_p;
12465           parser->local_variables_forbidden_p
12466             = saved_local_variables_forbidden_p;
12467         }
12468       if (!parser->default_arg_ok_p)
12469         {
12470           if (!flag_pedantic_errors)
12471             warning (0, "deprecated use of default argument for parameter of non-function");
12472           else
12473             {
12474               error ("default arguments are only permitted for function parameters");
12475               default_argument = NULL_TREE;
12476             }
12477         }
12478     }
12479   else
12480     default_argument = NULL_TREE;
12481
12482   return make_parameter_declarator (&decl_specifiers,
12483                                     declarator,
12484                                     default_argument);
12485 }
12486
12487 /* Parse a function-body.
12488
12489    function-body:
12490      compound_statement  */
12491
12492 static void
12493 cp_parser_function_body (cp_parser *parser)
12494 {
12495   cp_parser_compound_statement (parser, NULL, false);
12496 }
12497
12498 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12499    true if a ctor-initializer was present.  */
12500
12501 static bool
12502 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12503 {
12504   tree body;
12505   bool ctor_initializer_p;
12506
12507   /* Begin the function body.  */
12508   body = begin_function_body ();
12509   /* Parse the optional ctor-initializer.  */
12510   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12511   /* Parse the function-body.  */
12512   cp_parser_function_body (parser);
12513   /* Finish the function body.  */
12514   finish_function_body (body);
12515
12516   return ctor_initializer_p;
12517 }
12518
12519 /* Parse an initializer.
12520
12521    initializer:
12522      = initializer-clause
12523      ( expression-list )
12524
12525    Returns an expression representing the initializer.  If no
12526    initializer is present, NULL_TREE is returned.
12527
12528    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12529    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12530    set to FALSE if there is no initializer present.  If there is an
12531    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12532    is set to true; otherwise it is set to false.  */
12533
12534 static tree
12535 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12536                        bool* non_constant_p)
12537 {
12538   cp_token *token;
12539   tree init;
12540
12541   /* Peek at the next token.  */
12542   token = cp_lexer_peek_token (parser->lexer);
12543
12544   /* Let our caller know whether or not this initializer was
12545      parenthesized.  */
12546   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12547   /* Assume that the initializer is constant.  */
12548   *non_constant_p = false;
12549
12550   if (token->type == CPP_EQ)
12551     {
12552       /* Consume the `='.  */
12553       cp_lexer_consume_token (parser->lexer);
12554       /* Parse the initializer-clause.  */
12555       init = cp_parser_initializer_clause (parser, non_constant_p);
12556     }
12557   else if (token->type == CPP_OPEN_PAREN)
12558     init = cp_parser_parenthesized_expression_list (parser, false,
12559                                                     /*cast_p=*/false,
12560                                                     non_constant_p);
12561   else
12562     {
12563       /* Anything else is an error.  */
12564       cp_parser_error (parser, "expected initializer");
12565       init = error_mark_node;
12566     }
12567
12568   return init;
12569 }
12570
12571 /* Parse an initializer-clause.
12572
12573    initializer-clause:
12574      assignment-expression
12575      { initializer-list , [opt] }
12576      { }
12577
12578    Returns an expression representing the initializer.
12579
12580    If the `assignment-expression' production is used the value
12581    returned is simply a representation for the expression.
12582
12583    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12584    the elements of the initializer-list (or NULL, if the last
12585    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12586    NULL_TREE.  There is no way to detect whether or not the optional
12587    trailing `,' was provided.  NON_CONSTANT_P is as for
12588    cp_parser_initializer.  */
12589
12590 static tree
12591 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12592 {
12593   tree initializer;
12594
12595   /* Assume the expression is constant.  */
12596   *non_constant_p = false;
12597
12598   /* If it is not a `{', then we are looking at an
12599      assignment-expression.  */
12600   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12601     {
12602       initializer
12603         = cp_parser_constant_expression (parser,
12604                                         /*allow_non_constant_p=*/true,
12605                                         non_constant_p);
12606       if (!*non_constant_p)
12607         initializer = fold_non_dependent_expr (initializer);
12608     }
12609   else
12610     {
12611       /* Consume the `{' token.  */
12612       cp_lexer_consume_token (parser->lexer);
12613       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12614       initializer = make_node (CONSTRUCTOR);
12615       /* If it's not a `}', then there is a non-trivial initializer.  */
12616       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12617         {
12618           /* Parse the initializer list.  */
12619           CONSTRUCTOR_ELTS (initializer)
12620             = cp_parser_initializer_list (parser, non_constant_p);
12621           /* A trailing `,' token is allowed.  */
12622           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12623             cp_lexer_consume_token (parser->lexer);
12624         }
12625       /* Now, there should be a trailing `}'.  */
12626       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12627     }
12628
12629   return initializer;
12630 }
12631
12632 /* Parse an initializer-list.
12633
12634    initializer-list:
12635      initializer-clause
12636      initializer-list , initializer-clause
12637
12638    GNU Extension:
12639
12640    initializer-list:
12641      identifier : initializer-clause
12642      initializer-list, identifier : initializer-clause
12643
12644    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12645    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12646    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12647    as for cp_parser_initializer.  */
12648
12649 static VEC(constructor_elt,gc) *
12650 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12651 {
12652   VEC(constructor_elt,gc) *v = NULL;
12653
12654   /* Assume all of the expressions are constant.  */
12655   *non_constant_p = false;
12656
12657   /* Parse the rest of the list.  */
12658   while (true)
12659     {
12660       cp_token *token;
12661       tree identifier;
12662       tree initializer;
12663       bool clause_non_constant_p;
12664
12665       /* If the next token is an identifier and the following one is a
12666          colon, we are looking at the GNU designated-initializer
12667          syntax.  */
12668       if (cp_parser_allow_gnu_extensions_p (parser)
12669           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12670           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12671         {
12672           /* Consume the identifier.  */
12673           identifier = cp_lexer_consume_token (parser->lexer)->value;
12674           /* Consume the `:'.  */
12675           cp_lexer_consume_token (parser->lexer);
12676         }
12677       else
12678         identifier = NULL_TREE;
12679
12680       /* Parse the initializer.  */
12681       initializer = cp_parser_initializer_clause (parser,
12682                                                   &clause_non_constant_p);
12683       /* If any clause is non-constant, so is the entire initializer.  */
12684       if (clause_non_constant_p)
12685         *non_constant_p = true;
12686
12687       /* Add it to the vector.  */
12688       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12689
12690       /* If the next token is not a comma, we have reached the end of
12691          the list.  */
12692       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12693         break;
12694
12695       /* Peek at the next token.  */
12696       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12697       /* If the next token is a `}', then we're still done.  An
12698          initializer-clause can have a trailing `,' after the
12699          initializer-list and before the closing `}'.  */
12700       if (token->type == CPP_CLOSE_BRACE)
12701         break;
12702
12703       /* Consume the `,' token.  */
12704       cp_lexer_consume_token (parser->lexer);
12705     }
12706
12707   return v;
12708 }
12709
12710 /* Classes [gram.class] */
12711
12712 /* Parse a class-name.
12713
12714    class-name:
12715      identifier
12716      template-id
12717
12718    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12719    to indicate that names looked up in dependent types should be
12720    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12721    keyword has been used to indicate that the name that appears next
12722    is a template.  TAG_TYPE indicates the explicit tag given before
12723    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12724    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12725    is the class being defined in a class-head.
12726
12727    Returns the TYPE_DECL representing the class.  */
12728
12729 static tree
12730 cp_parser_class_name (cp_parser *parser,
12731                       bool typename_keyword_p,
12732                       bool template_keyword_p,
12733                       enum tag_types tag_type,
12734                       bool check_dependency_p,
12735                       bool class_head_p,
12736                       bool is_declaration)
12737 {
12738   tree decl;
12739   tree scope;
12740   bool typename_p;
12741   cp_token *token;
12742
12743   /* All class-names start with an identifier.  */
12744   token = cp_lexer_peek_token (parser->lexer);
12745   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12746     {
12747       cp_parser_error (parser, "expected class-name");
12748       return error_mark_node;
12749     }
12750
12751   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12752      to a template-id, so we save it here.  */
12753   scope = parser->scope;
12754   if (scope == error_mark_node)
12755     return error_mark_node;
12756
12757   /* Any name names a type if we're following the `typename' keyword
12758      in a qualified name where the enclosing scope is type-dependent.  */
12759   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12760                 && dependent_type_p (scope));
12761   /* Handle the common case (an identifier, but not a template-id)
12762      efficiently.  */
12763   if (token->type == CPP_NAME
12764       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12765     {
12766       cp_token *identifier_token;
12767       tree identifier;
12768       bool ambiguous_p;
12769
12770       /* Look for the identifier.  */
12771       identifier_token = cp_lexer_peek_token (parser->lexer);
12772       ambiguous_p = identifier_token->ambiguous_p;
12773       identifier = cp_parser_identifier (parser);
12774       /* If the next token isn't an identifier, we are certainly not
12775          looking at a class-name.  */
12776       if (identifier == error_mark_node)
12777         decl = error_mark_node;
12778       /* If we know this is a type-name, there's no need to look it
12779          up.  */
12780       else if (typename_p)
12781         decl = identifier;
12782       else
12783         {
12784           tree ambiguous_decls;
12785           /* If we already know that this lookup is ambiguous, then
12786              we've already issued an error message; there's no reason
12787              to check again.  */
12788           if (ambiguous_p)
12789             {
12790               cp_parser_simulate_error (parser);
12791               return error_mark_node;
12792             }
12793           /* If the next token is a `::', then the name must be a type
12794              name.
12795
12796              [basic.lookup.qual]
12797
12798              During the lookup for a name preceding the :: scope
12799              resolution operator, object, function, and enumerator
12800              names are ignored.  */
12801           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12802             tag_type = typename_type;
12803           /* Look up the name.  */
12804           decl = cp_parser_lookup_name (parser, identifier,
12805                                         tag_type,
12806                                         /*is_template=*/false,
12807                                         /*is_namespace=*/false,
12808                                         check_dependency_p,
12809                                         &ambiguous_decls);
12810           if (ambiguous_decls)
12811             {
12812               error ("reference to %qD is ambiguous", identifier);
12813               print_candidates (ambiguous_decls);
12814               if (cp_parser_parsing_tentatively (parser))
12815                 {
12816                   identifier_token->ambiguous_p = true;
12817                   cp_parser_simulate_error (parser);
12818                 }
12819               return error_mark_node;
12820             }
12821         }
12822     }
12823   else
12824     {
12825       /* Try a template-id.  */
12826       decl = cp_parser_template_id (parser, template_keyword_p,
12827                                     check_dependency_p,
12828                                     is_declaration);
12829       if (decl == error_mark_node)
12830         return error_mark_node;
12831     }
12832
12833   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12834
12835   /* If this is a typename, create a TYPENAME_TYPE.  */
12836   if (typename_p && decl != error_mark_node)
12837     {
12838       decl = make_typename_type (scope, decl, typename_type,
12839                                  /*complain=*/tf_error);
12840       if (decl != error_mark_node)
12841         decl = TYPE_NAME (decl);
12842     }
12843
12844   /* Check to see that it is really the name of a class.  */
12845   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12846       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12847       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12848     /* Situations like this:
12849
12850          template <typename T> struct A {
12851            typename T::template X<int>::I i;
12852          };
12853
12854        are problematic.  Is `T::template X<int>' a class-name?  The
12855        standard does not seem to be definitive, but there is no other
12856        valid interpretation of the following `::'.  Therefore, those
12857        names are considered class-names.  */
12858     {
12859       decl = make_typename_type (scope, decl, tag_type, tf_error);
12860       if (decl != error_mark_node)
12861         decl = TYPE_NAME (decl);
12862     }
12863   else if (TREE_CODE (decl) != TYPE_DECL
12864            || TREE_TYPE (decl) == error_mark_node
12865            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12866     decl = error_mark_node;
12867
12868   if (decl == error_mark_node)
12869     cp_parser_error (parser, "expected class-name");
12870
12871   return decl;
12872 }
12873
12874 /* Parse a class-specifier.
12875
12876    class-specifier:
12877      class-head { member-specification [opt] }
12878
12879    Returns the TREE_TYPE representing the class.  */
12880
12881 static tree
12882 cp_parser_class_specifier (cp_parser* parser)
12883 {
12884   cp_token *token;
12885   tree type;
12886   tree attributes = NULL_TREE;
12887   int has_trailing_semicolon;
12888   bool nested_name_specifier_p;
12889   unsigned saved_num_template_parameter_lists;
12890   tree old_scope = NULL_TREE;
12891   tree scope = NULL_TREE;
12892
12893   push_deferring_access_checks (dk_no_deferred);
12894
12895   /* Parse the class-head.  */
12896   type = cp_parser_class_head (parser,
12897                                &nested_name_specifier_p,
12898                                &attributes);
12899   /* If the class-head was a semantic disaster, skip the entire body
12900      of the class.  */
12901   if (!type)
12902     {
12903       cp_parser_skip_to_end_of_block_or_statement (parser);
12904       pop_deferring_access_checks ();
12905       return error_mark_node;
12906     }
12907
12908   /* Look for the `{'.  */
12909   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12910     {
12911       pop_deferring_access_checks ();
12912       return error_mark_node;
12913     }
12914
12915   /* Issue an error message if type-definitions are forbidden here.  */
12916   cp_parser_check_type_definition (parser);
12917   /* Remember that we are defining one more class.  */
12918   ++parser->num_classes_being_defined;
12919   /* Inside the class, surrounding template-parameter-lists do not
12920      apply.  */
12921   saved_num_template_parameter_lists
12922     = parser->num_template_parameter_lists;
12923   parser->num_template_parameter_lists = 0;
12924
12925   /* Start the class.  */
12926   if (nested_name_specifier_p)
12927     {
12928       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12929       old_scope = push_inner_scope (scope);
12930     }
12931   type = begin_class_definition (type, attributes);
12932
12933   if (type == error_mark_node)
12934     /* If the type is erroneous, skip the entire body of the class.  */
12935     cp_parser_skip_to_closing_brace (parser);
12936   else
12937     /* Parse the member-specification.  */
12938     cp_parser_member_specification_opt (parser);
12939
12940   /* Look for the trailing `}'.  */
12941   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12942   /* We get better error messages by noticing a common problem: a
12943      missing trailing `;'.  */
12944   token = cp_lexer_peek_token (parser->lexer);
12945   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12946   /* Look for trailing attributes to apply to this class.  */
12947   if (cp_parser_allow_gnu_extensions_p (parser))
12948     attributes = cp_parser_attributes_opt (parser);
12949   if (type != error_mark_node)
12950     type = finish_struct (type, attributes);
12951   if (nested_name_specifier_p)
12952     pop_inner_scope (old_scope, scope);
12953   /* If this class is not itself within the scope of another class,
12954      then we need to parse the bodies of all of the queued function
12955      definitions.  Note that the queued functions defined in a class
12956      are not always processed immediately following the
12957      class-specifier for that class.  Consider:
12958
12959        struct A {
12960          struct B { void f() { sizeof (A); } };
12961        };
12962
12963      If `f' were processed before the processing of `A' were
12964      completed, there would be no way to compute the size of `A'.
12965      Note that the nesting we are interested in here is lexical --
12966      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12967      for:
12968
12969        struct A { struct B; };
12970        struct A::B { void f() { } };
12971
12972      there is no need to delay the parsing of `A::B::f'.  */
12973   if (--parser->num_classes_being_defined == 0)
12974     {
12975       tree queue_entry;
12976       tree fn;
12977       tree class_type = NULL_TREE;
12978       tree pushed_scope = NULL_TREE;
12979
12980       /* In a first pass, parse default arguments to the functions.
12981          Then, in a second pass, parse the bodies of the functions.
12982          This two-phased approach handles cases like:
12983
12984             struct S {
12985               void f() { g(); }
12986               void g(int i = 3);
12987             };
12988
12989          */
12990       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12991              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12992            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12993            TREE_PURPOSE (parser->unparsed_functions_queues)
12994              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12995         {
12996           fn = TREE_VALUE (queue_entry);
12997           /* If there are default arguments that have not yet been processed,
12998              take care of them now.  */
12999           if (class_type != TREE_PURPOSE (queue_entry))
13000             {
13001               if (pushed_scope)
13002                 pop_scope (pushed_scope);
13003               class_type = TREE_PURPOSE (queue_entry);
13004               pushed_scope = push_scope (class_type);
13005             }
13006           /* Make sure that any template parameters are in scope.  */
13007           maybe_begin_member_template_processing (fn);
13008           /* Parse the default argument expressions.  */
13009           cp_parser_late_parsing_default_args (parser, fn);
13010           /* Remove any template parameters from the symbol table.  */
13011           maybe_end_member_template_processing ();
13012         }
13013       if (pushed_scope)
13014         pop_scope (pushed_scope);
13015       /* Now parse the body of the functions.  */
13016       for (TREE_VALUE (parser->unparsed_functions_queues)
13017              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13018            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13019            TREE_VALUE (parser->unparsed_functions_queues)
13020              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13021         {
13022           /* Figure out which function we need to process.  */
13023           fn = TREE_VALUE (queue_entry);
13024           /* Parse the function.  */
13025           cp_parser_late_parsing_for_member (parser, fn);
13026         }
13027     }
13028
13029   /* Put back any saved access checks.  */
13030   pop_deferring_access_checks ();
13031
13032   /* Restore the count of active template-parameter-lists.  */
13033   parser->num_template_parameter_lists
13034     = saved_num_template_parameter_lists;
13035
13036   return type;
13037 }
13038
13039 /* Parse a class-head.
13040
13041    class-head:
13042      class-key identifier [opt] base-clause [opt]
13043      class-key nested-name-specifier identifier base-clause [opt]
13044      class-key nested-name-specifier [opt] template-id
13045        base-clause [opt]
13046
13047    GNU Extensions:
13048      class-key attributes identifier [opt] base-clause [opt]
13049      class-key attributes nested-name-specifier identifier base-clause [opt]
13050      class-key attributes nested-name-specifier [opt] template-id
13051        base-clause [opt]
13052
13053    Returns the TYPE of the indicated class.  Sets
13054    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13055    involving a nested-name-specifier was used, and FALSE otherwise.
13056
13057    Returns error_mark_node if this is not a class-head.
13058
13059    Returns NULL_TREE if the class-head is syntactically valid, but
13060    semantically invalid in a way that means we should skip the entire
13061    body of the class.  */
13062
13063 static tree
13064 cp_parser_class_head (cp_parser* parser,
13065                       bool* nested_name_specifier_p,
13066                       tree *attributes_p)
13067 {
13068   tree nested_name_specifier;
13069   enum tag_types class_key;
13070   tree id = NULL_TREE;
13071   tree type = NULL_TREE;
13072   tree attributes;
13073   bool template_id_p = false;
13074   bool qualified_p = false;
13075   bool invalid_nested_name_p = false;
13076   bool invalid_explicit_specialization_p = false;
13077   tree pushed_scope = NULL_TREE;
13078   unsigned num_templates;
13079   tree bases;
13080
13081   /* Assume no nested-name-specifier will be present.  */
13082   *nested_name_specifier_p = false;
13083   /* Assume no template parameter lists will be used in defining the
13084      type.  */
13085   num_templates = 0;
13086
13087   /* Look for the class-key.  */
13088   class_key = cp_parser_class_key (parser);
13089   if (class_key == none_type)
13090     return error_mark_node;
13091
13092   /* Parse the attributes.  */
13093   attributes = cp_parser_attributes_opt (parser);
13094
13095   /* If the next token is `::', that is invalid -- but sometimes
13096      people do try to write:
13097
13098        struct ::S {};
13099
13100      Handle this gracefully by accepting the extra qualifier, and then
13101      issuing an error about it later if this really is a
13102      class-head.  If it turns out just to be an elaborated type
13103      specifier, remain silent.  */
13104   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13105     qualified_p = true;
13106
13107   push_deferring_access_checks (dk_no_check);
13108
13109   /* Determine the name of the class.  Begin by looking for an
13110      optional nested-name-specifier.  */
13111   nested_name_specifier
13112     = cp_parser_nested_name_specifier_opt (parser,
13113                                            /*typename_keyword_p=*/false,
13114                                            /*check_dependency_p=*/false,
13115                                            /*type_p=*/false,
13116                                            /*is_declaration=*/false);
13117   /* If there was a nested-name-specifier, then there *must* be an
13118      identifier.  */
13119   if (nested_name_specifier)
13120     {
13121       /* Although the grammar says `identifier', it really means
13122          `class-name' or `template-name'.  You are only allowed to
13123          define a class that has already been declared with this
13124          syntax.
13125
13126          The proposed resolution for Core Issue 180 says that wherever
13127          you see `class T::X' you should treat `X' as a type-name.
13128
13129          It is OK to define an inaccessible class; for example:
13130
13131            class A { class B; };
13132            class A::B {};
13133
13134          We do not know if we will see a class-name, or a
13135          template-name.  We look for a class-name first, in case the
13136          class-name is a template-id; if we looked for the
13137          template-name first we would stop after the template-name.  */
13138       cp_parser_parse_tentatively (parser);
13139       type = cp_parser_class_name (parser,
13140                                    /*typename_keyword_p=*/false,
13141                                    /*template_keyword_p=*/false,
13142                                    class_type,
13143                                    /*check_dependency_p=*/false,
13144                                    /*class_head_p=*/true,
13145                                    /*is_declaration=*/false);
13146       /* If that didn't work, ignore the nested-name-specifier.  */
13147       if (!cp_parser_parse_definitely (parser))
13148         {
13149           invalid_nested_name_p = true;
13150           id = cp_parser_identifier (parser);
13151           if (id == error_mark_node)
13152             id = NULL_TREE;
13153         }
13154       /* If we could not find a corresponding TYPE, treat this
13155          declaration like an unqualified declaration.  */
13156       if (type == error_mark_node)
13157         nested_name_specifier = NULL_TREE;
13158       /* Otherwise, count the number of templates used in TYPE and its
13159          containing scopes.  */
13160       else
13161         {
13162           tree scope;
13163
13164           for (scope = TREE_TYPE (type);
13165                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13166                scope = (TYPE_P (scope)
13167                         ? TYPE_CONTEXT (scope)
13168                         : DECL_CONTEXT (scope)))
13169             if (TYPE_P (scope)
13170                 && CLASS_TYPE_P (scope)
13171                 && CLASSTYPE_TEMPLATE_INFO (scope)
13172                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13173                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13174               ++num_templates;
13175         }
13176     }
13177   /* Otherwise, the identifier is optional.  */
13178   else
13179     {
13180       /* We don't know whether what comes next is a template-id,
13181          an identifier, or nothing at all.  */
13182       cp_parser_parse_tentatively (parser);
13183       /* Check for a template-id.  */
13184       id = cp_parser_template_id (parser,
13185                                   /*template_keyword_p=*/false,
13186                                   /*check_dependency_p=*/true,
13187                                   /*is_declaration=*/true);
13188       /* If that didn't work, it could still be an identifier.  */
13189       if (!cp_parser_parse_definitely (parser))
13190         {
13191           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13192             id = cp_parser_identifier (parser);
13193           else
13194             id = NULL_TREE;
13195         }
13196       else
13197         {
13198           template_id_p = true;
13199           ++num_templates;
13200         }
13201     }
13202
13203   pop_deferring_access_checks ();
13204
13205   if (id)
13206     cp_parser_check_for_invalid_template_id (parser, id);
13207
13208   /* If it's not a `:' or a `{' then we can't really be looking at a
13209      class-head, since a class-head only appears as part of a
13210      class-specifier.  We have to detect this situation before calling
13211      xref_tag, since that has irreversible side-effects.  */
13212   if (!cp_parser_next_token_starts_class_definition_p (parser))
13213     {
13214       cp_parser_error (parser, "expected %<{%> or %<:%>");
13215       return error_mark_node;
13216     }
13217
13218   /* At this point, we're going ahead with the class-specifier, even
13219      if some other problem occurs.  */
13220   cp_parser_commit_to_tentative_parse (parser);
13221   /* Issue the error about the overly-qualified name now.  */
13222   if (qualified_p)
13223     cp_parser_error (parser,
13224                      "global qualification of class name is invalid");
13225   else if (invalid_nested_name_p)
13226     cp_parser_error (parser,
13227                      "qualified name does not name a class");
13228   else if (nested_name_specifier)
13229     {
13230       tree scope;
13231
13232       /* Reject typedef-names in class heads.  */
13233       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13234         {
13235           error ("invalid class name in declaration of %qD", type);
13236           type = NULL_TREE;
13237           goto done;
13238         }
13239
13240       /* Figure out in what scope the declaration is being placed.  */
13241       scope = current_scope ();
13242       /* If that scope does not contain the scope in which the
13243          class was originally declared, the program is invalid.  */
13244       if (scope && !is_ancestor (scope, nested_name_specifier))
13245         {
13246           error ("declaration of %qD in %qD which does not enclose %qD",
13247                  type, scope, nested_name_specifier);
13248           type = NULL_TREE;
13249           goto done;
13250         }
13251       /* [dcl.meaning]
13252
13253          A declarator-id shall not be qualified exception of the
13254          definition of a ... nested class outside of its class
13255          ... [or] a the definition or explicit instantiation of a
13256          class member of a namespace outside of its namespace.  */
13257       if (scope == nested_name_specifier)
13258         {
13259           pedwarn ("extra qualification ignored");
13260           nested_name_specifier = NULL_TREE;
13261           num_templates = 0;
13262         }
13263     }
13264   /* An explicit-specialization must be preceded by "template <>".  If
13265      it is not, try to recover gracefully.  */
13266   if (at_namespace_scope_p ()
13267       && parser->num_template_parameter_lists == 0
13268       && template_id_p)
13269     {
13270       error ("an explicit specialization must be preceded by %<template <>%>");
13271       invalid_explicit_specialization_p = true;
13272       /* Take the same action that would have been taken by
13273          cp_parser_explicit_specialization.  */
13274       ++parser->num_template_parameter_lists;
13275       begin_specialization ();
13276     }
13277   /* There must be no "return" statements between this point and the
13278      end of this function; set "type "to the correct return value and
13279      use "goto done;" to return.  */
13280   /* Make sure that the right number of template parameters were
13281      present.  */
13282   if (!cp_parser_check_template_parameters (parser, num_templates))
13283     {
13284       /* If something went wrong, there is no point in even trying to
13285          process the class-definition.  */
13286       type = NULL_TREE;
13287       goto done;
13288     }
13289
13290   /* Look up the type.  */
13291   if (template_id_p)
13292     {
13293       type = TREE_TYPE (id);
13294       maybe_process_partial_specialization (type);
13295       if (nested_name_specifier)
13296         pushed_scope = push_scope (nested_name_specifier);
13297     }
13298   else if (nested_name_specifier)
13299     {
13300       tree class_type;
13301
13302       /* Given:
13303
13304             template <typename T> struct S { struct T };
13305             template <typename T> struct S<T>::T { };
13306
13307          we will get a TYPENAME_TYPE when processing the definition of
13308          `S::T'.  We need to resolve it to the actual type before we
13309          try to define it.  */
13310       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13311         {
13312           class_type = resolve_typename_type (TREE_TYPE (type),
13313                                               /*only_current_p=*/false);
13314           if (class_type != error_mark_node)
13315             type = TYPE_NAME (class_type);
13316           else
13317             {
13318               cp_parser_error (parser, "could not resolve typename type");
13319               type = error_mark_node;
13320             }
13321         }
13322
13323       maybe_process_partial_specialization (TREE_TYPE (type));
13324       class_type = current_class_type;
13325       /* Enter the scope indicated by the nested-name-specifier.  */
13326       pushed_scope = push_scope (nested_name_specifier);
13327       /* Get the canonical version of this type.  */
13328       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13329       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13330           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13331         {
13332           type = push_template_decl (type);
13333           if (type == error_mark_node)
13334             {
13335               type = NULL_TREE;
13336               goto done;
13337             }
13338         }
13339
13340       type = TREE_TYPE (type);
13341       *nested_name_specifier_p = true;
13342     }
13343   else      /* The name is not a nested name.  */
13344     {
13345       /* If the class was unnamed, create a dummy name.  */
13346       if (!id)
13347         id = make_anon_name ();
13348       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13349                        parser->num_template_parameter_lists);
13350     }
13351
13352   /* Indicate whether this class was declared as a `class' or as a
13353      `struct'.  */
13354   if (TREE_CODE (type) == RECORD_TYPE)
13355     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13356   cp_parser_check_class_key (class_key, type);
13357
13358   /* If this type was already complete, and we see another definition,
13359      that's an error.  */
13360   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13361     {
13362       error ("redefinition of %q#T", type);
13363       error ("previous definition of %q+#T", type);
13364       type = NULL_TREE;
13365       goto done;
13366     }
13367
13368   /* We will have entered the scope containing the class; the names of
13369      base classes should be looked up in that context.  For example:
13370
13371        struct A { struct B {}; struct C; };
13372        struct A::C : B {};
13373
13374      is valid.  */
13375   bases = NULL_TREE;
13376
13377   /* Get the list of base-classes, if there is one.  */
13378   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13379     bases = cp_parser_base_clause (parser);
13380
13381   /* Process the base classes.  */
13382   xref_basetypes (type, bases);
13383
13384  done:
13385   /* Leave the scope given by the nested-name-specifier.  We will
13386      enter the class scope itself while processing the members.  */
13387   if (pushed_scope)
13388     pop_scope (pushed_scope);
13389
13390   if (invalid_explicit_specialization_p)
13391     {
13392       end_specialization ();
13393       --parser->num_template_parameter_lists;
13394     }
13395   *attributes_p = attributes;
13396   return type;
13397 }
13398
13399 /* Parse a class-key.
13400
13401    class-key:
13402      class
13403      struct
13404      union
13405
13406    Returns the kind of class-key specified, or none_type to indicate
13407    error.  */
13408
13409 static enum tag_types
13410 cp_parser_class_key (cp_parser* parser)
13411 {
13412   cp_token *token;
13413   enum tag_types tag_type;
13414
13415   /* Look for the class-key.  */
13416   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13417   if (!token)
13418     return none_type;
13419
13420   /* Check to see if the TOKEN is a class-key.  */
13421   tag_type = cp_parser_token_is_class_key (token);
13422   if (!tag_type)
13423     cp_parser_error (parser, "expected class-key");
13424   return tag_type;
13425 }
13426
13427 /* Parse an (optional) member-specification.
13428
13429    member-specification:
13430      member-declaration member-specification [opt]
13431      access-specifier : member-specification [opt]  */
13432
13433 static void
13434 cp_parser_member_specification_opt (cp_parser* parser)
13435 {
13436   while (true)
13437     {
13438       cp_token *token;
13439       enum rid keyword;
13440
13441       /* Peek at the next token.  */
13442       token = cp_lexer_peek_token (parser->lexer);
13443       /* If it's a `}', or EOF then we've seen all the members.  */
13444       if (token->type == CPP_CLOSE_BRACE
13445           || token->type == CPP_EOF
13446           || token->type == CPP_PRAGMA_EOL)
13447         break;
13448
13449       /* See if this token is a keyword.  */
13450       keyword = token->keyword;
13451       switch (keyword)
13452         {
13453         case RID_PUBLIC:
13454         case RID_PROTECTED:
13455         case RID_PRIVATE:
13456           /* Consume the access-specifier.  */
13457           cp_lexer_consume_token (parser->lexer);
13458           /* Remember which access-specifier is active.  */
13459           current_access_specifier = token->value;
13460           /* Look for the `:'.  */
13461           cp_parser_require (parser, CPP_COLON, "`:'");
13462           break;
13463
13464         default:
13465           /* Accept #pragmas at class scope.  */
13466           if (token->type == CPP_PRAGMA)
13467             {
13468               cp_parser_pragma (parser, pragma_external);
13469               break;
13470             }
13471
13472           /* Otherwise, the next construction must be a
13473              member-declaration.  */
13474           cp_parser_member_declaration (parser);
13475         }
13476     }
13477 }
13478
13479 /* Parse a member-declaration.
13480
13481    member-declaration:
13482      decl-specifier-seq [opt] member-declarator-list [opt] ;
13483      function-definition ; [opt]
13484      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13485      using-declaration
13486      template-declaration
13487
13488    member-declarator-list:
13489      member-declarator
13490      member-declarator-list , member-declarator
13491
13492    member-declarator:
13493      declarator pure-specifier [opt]
13494      declarator constant-initializer [opt]
13495      identifier [opt] : constant-expression
13496
13497    GNU Extensions:
13498
13499    member-declaration:
13500      __extension__ member-declaration
13501
13502    member-declarator:
13503      declarator attributes [opt] pure-specifier [opt]
13504      declarator attributes [opt] constant-initializer [opt]
13505      identifier [opt] attributes [opt] : constant-expression  */
13506
13507 static void
13508 cp_parser_member_declaration (cp_parser* parser)
13509 {
13510   cp_decl_specifier_seq decl_specifiers;
13511   tree prefix_attributes;
13512   tree decl;
13513   int declares_class_or_enum;
13514   bool friend_p;
13515   cp_token *token;
13516   int saved_pedantic;
13517
13518   /* Check for the `__extension__' keyword.  */
13519   if (cp_parser_extension_opt (parser, &saved_pedantic))
13520     {
13521       /* Recurse.  */
13522       cp_parser_member_declaration (parser);
13523       /* Restore the old value of the PEDANTIC flag.  */
13524       pedantic = saved_pedantic;
13525
13526       return;
13527     }
13528
13529   /* Check for a template-declaration.  */
13530   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13531     {
13532       /* An explicit specialization here is an error condition, and we
13533          expect the specialization handler to detect and report this.  */
13534       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13535           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13536         cp_parser_explicit_specialization (parser);
13537       else
13538         cp_parser_template_declaration (parser, /*member_p=*/true);
13539
13540       return;
13541     }
13542
13543   /* Check for a using-declaration.  */
13544   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13545     {
13546       /* Parse the using-declaration.  */
13547       cp_parser_using_declaration (parser);
13548
13549       return;
13550     }
13551
13552   /* Check for @defs.  */
13553   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13554     {
13555       tree ivar, member;
13556       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13557       ivar = ivar_chains;
13558       while (ivar)
13559         {
13560           member = ivar;
13561           ivar = TREE_CHAIN (member);
13562           TREE_CHAIN (member) = NULL_TREE;
13563           finish_member_declaration (member);
13564         }
13565       return;
13566     }
13567
13568   /* Parse the decl-specifier-seq.  */
13569   cp_parser_decl_specifier_seq (parser,
13570                                 CP_PARSER_FLAGS_OPTIONAL,
13571                                 &decl_specifiers,
13572                                 &declares_class_or_enum);
13573   prefix_attributes = decl_specifiers.attributes;
13574   decl_specifiers.attributes = NULL_TREE;
13575   /* Check for an invalid type-name.  */
13576   if (!decl_specifiers.type
13577       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13578     return;
13579   /* If there is no declarator, then the decl-specifier-seq should
13580      specify a type.  */
13581   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13582     {
13583       /* If there was no decl-specifier-seq, and the next token is a
13584          `;', then we have something like:
13585
13586            struct S { ; };
13587
13588          [class.mem]
13589
13590          Each member-declaration shall declare at least one member
13591          name of the class.  */
13592       if (!decl_specifiers.any_specifiers_p)
13593         {
13594           cp_token *token = cp_lexer_peek_token (parser->lexer);
13595           if (pedantic && !token->in_system_header)
13596             pedwarn ("%Hextra %<;%>", &token->location);
13597         }
13598       else
13599         {
13600           tree type;
13601
13602           /* See if this declaration is a friend.  */
13603           friend_p = cp_parser_friend_p (&decl_specifiers);
13604           /* If there were decl-specifiers, check to see if there was
13605              a class-declaration.  */
13606           type = check_tag_decl (&decl_specifiers);
13607           /* Nested classes have already been added to the class, but
13608              a `friend' needs to be explicitly registered.  */
13609           if (friend_p)
13610             {
13611               /* If the `friend' keyword was present, the friend must
13612                  be introduced with a class-key.  */
13613                if (!declares_class_or_enum)
13614                  error ("a class-key must be used when declaring a friend");
13615                /* In this case:
13616
13617                     template <typename T> struct A {
13618                       friend struct A<T>::B;
13619                     };
13620
13621                   A<T>::B will be represented by a TYPENAME_TYPE, and
13622                   therefore not recognized by check_tag_decl.  */
13623                if (!type
13624                    && decl_specifiers.type
13625                    && TYPE_P (decl_specifiers.type))
13626                  type = decl_specifiers.type;
13627                if (!type || !TYPE_P (type))
13628                  error ("friend declaration does not name a class or "
13629                         "function");
13630                else
13631                  make_friend_class (current_class_type, type,
13632                                     /*complain=*/true);
13633             }
13634           /* If there is no TYPE, an error message will already have
13635              been issued.  */
13636           else if (!type || type == error_mark_node)
13637             ;
13638           /* An anonymous aggregate has to be handled specially; such
13639              a declaration really declares a data member (with a
13640              particular type), as opposed to a nested class.  */
13641           else if (ANON_AGGR_TYPE_P (type))
13642             {
13643               /* Remove constructors and such from TYPE, now that we
13644                  know it is an anonymous aggregate.  */
13645               fixup_anonymous_aggr (type);
13646               /* And make the corresponding data member.  */
13647               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13648               /* Add it to the class.  */
13649               finish_member_declaration (decl);
13650             }
13651           else
13652             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13653         }
13654     }
13655   else
13656     {
13657       /* See if these declarations will be friends.  */
13658       friend_p = cp_parser_friend_p (&decl_specifiers);
13659
13660       /* Keep going until we hit the `;' at the end of the
13661          declaration.  */
13662       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13663         {
13664           tree attributes = NULL_TREE;
13665           tree first_attribute;
13666
13667           /* Peek at the next token.  */
13668           token = cp_lexer_peek_token (parser->lexer);
13669
13670           /* Check for a bitfield declaration.  */
13671           if (token->type == CPP_COLON
13672               || (token->type == CPP_NAME
13673                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13674                   == CPP_COLON))
13675             {
13676               tree identifier;
13677               tree width;
13678
13679               /* Get the name of the bitfield.  Note that we cannot just
13680                  check TOKEN here because it may have been invalidated by
13681                  the call to cp_lexer_peek_nth_token above.  */
13682               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13683                 identifier = cp_parser_identifier (parser);
13684               else
13685                 identifier = NULL_TREE;
13686
13687               /* Consume the `:' token.  */
13688               cp_lexer_consume_token (parser->lexer);
13689               /* Get the width of the bitfield.  */
13690               width
13691                 = cp_parser_constant_expression (parser,
13692                                                  /*allow_non_constant=*/false,
13693                                                  NULL);
13694
13695               /* Look for attributes that apply to the bitfield.  */
13696               attributes = cp_parser_attributes_opt (parser);
13697               /* Remember which attributes are prefix attributes and
13698                  which are not.  */
13699               first_attribute = attributes;
13700               /* Combine the attributes.  */
13701               attributes = chainon (prefix_attributes, attributes);
13702
13703               /* Create the bitfield declaration.  */
13704               decl = grokbitfield (identifier
13705                                    ? make_id_declarator (NULL_TREE,
13706                                                          identifier,
13707                                                          sfk_none)
13708                                    : NULL,
13709                                    &decl_specifiers,
13710                                    width);
13711               /* Apply the attributes.  */
13712               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13713             }
13714           else
13715             {
13716               cp_declarator *declarator;
13717               tree initializer;
13718               tree asm_specification;
13719               int ctor_dtor_or_conv_p;
13720
13721               /* Parse the declarator.  */
13722               declarator
13723                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13724                                         &ctor_dtor_or_conv_p,
13725                                         /*parenthesized_p=*/NULL,
13726                                         /*member_p=*/true);
13727
13728               /* If something went wrong parsing the declarator, make sure
13729                  that we at least consume some tokens.  */
13730               if (declarator == cp_error_declarator)
13731                 {
13732                   /* Skip to the end of the statement.  */
13733                   cp_parser_skip_to_end_of_statement (parser);
13734                   /* If the next token is not a semicolon, that is
13735                      probably because we just skipped over the body of
13736                      a function.  So, we consume a semicolon if
13737                      present, but do not issue an error message if it
13738                      is not present.  */
13739                   if (cp_lexer_next_token_is (parser->lexer,
13740                                               CPP_SEMICOLON))
13741                     cp_lexer_consume_token (parser->lexer);
13742                   return;
13743                 }
13744
13745               if (declares_class_or_enum & 2)
13746                 cp_parser_check_for_definition_in_return_type
13747                   (declarator, decl_specifiers.type);
13748
13749               /* Look for an asm-specification.  */
13750               asm_specification = cp_parser_asm_specification_opt (parser);
13751               /* Look for attributes that apply to the declaration.  */
13752               attributes = cp_parser_attributes_opt (parser);
13753               /* Remember which attributes are prefix attributes and
13754                  which are not.  */
13755               first_attribute = attributes;
13756               /* Combine the attributes.  */
13757               attributes = chainon (prefix_attributes, attributes);
13758
13759               /* If it's an `=', then we have a constant-initializer or a
13760                  pure-specifier.  It is not correct to parse the
13761                  initializer before registering the member declaration
13762                  since the member declaration should be in scope while
13763                  its initializer is processed.  However, the rest of the
13764                  front end does not yet provide an interface that allows
13765                  us to handle this correctly.  */
13766               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13767                 {
13768                   /* In [class.mem]:
13769
13770                      A pure-specifier shall be used only in the declaration of
13771                      a virtual function.
13772
13773                      A member-declarator can contain a constant-initializer
13774                      only if it declares a static member of integral or
13775                      enumeration type.
13776
13777                      Therefore, if the DECLARATOR is for a function, we look
13778                      for a pure-specifier; otherwise, we look for a
13779                      constant-initializer.  When we call `grokfield', it will
13780                      perform more stringent semantics checks.  */
13781                   if (declarator->kind == cdk_function
13782                       && declarator->declarator->kind == cdk_id)
13783                     initializer = cp_parser_pure_specifier (parser);
13784                   else
13785                     /* Parse the initializer.  */
13786                     initializer = cp_parser_constant_initializer (parser);
13787                 }
13788               /* Otherwise, there is no initializer.  */
13789               else
13790                 initializer = NULL_TREE;
13791
13792               /* See if we are probably looking at a function
13793                  definition.  We are certainly not looking at a
13794                  member-declarator.  Calling `grokfield' has
13795                  side-effects, so we must not do it unless we are sure
13796                  that we are looking at a member-declarator.  */
13797               if (cp_parser_token_starts_function_definition_p
13798                   (cp_lexer_peek_token (parser->lexer)))
13799                 {
13800                   /* The grammar does not allow a pure-specifier to be
13801                      used when a member function is defined.  (It is
13802                      possible that this fact is an oversight in the
13803                      standard, since a pure function may be defined
13804                      outside of the class-specifier.  */
13805                   if (initializer)
13806                     error ("pure-specifier on function-definition");
13807                   decl = cp_parser_save_member_function_body (parser,
13808                                                               &decl_specifiers,
13809                                                               declarator,
13810                                                               attributes);
13811                   /* If the member was not a friend, declare it here.  */
13812                   if (!friend_p)
13813                     finish_member_declaration (decl);
13814                   /* Peek at the next token.  */
13815                   token = cp_lexer_peek_token (parser->lexer);
13816                   /* If the next token is a semicolon, consume it.  */
13817                   if (token->type == CPP_SEMICOLON)
13818                     cp_lexer_consume_token (parser->lexer);
13819                   return;
13820                 }
13821               else
13822                 /* Create the declaration.  */
13823                 decl = grokfield (declarator, &decl_specifiers,
13824                                   initializer, /*init_const_expr_p=*/true,
13825                                   asm_specification,
13826                                   attributes);
13827             }
13828
13829           /* Reset PREFIX_ATTRIBUTES.  */
13830           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13831             attributes = TREE_CHAIN (attributes);
13832           if (attributes)
13833             TREE_CHAIN (attributes) = NULL_TREE;
13834
13835           /* If there is any qualification still in effect, clear it
13836              now; we will be starting fresh with the next declarator.  */
13837           parser->scope = NULL_TREE;
13838           parser->qualifying_scope = NULL_TREE;
13839           parser->object_scope = NULL_TREE;
13840           /* If it's a `,', then there are more declarators.  */
13841           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13842             cp_lexer_consume_token (parser->lexer);
13843           /* If the next token isn't a `;', then we have a parse error.  */
13844           else if (cp_lexer_next_token_is_not (parser->lexer,
13845                                                CPP_SEMICOLON))
13846             {
13847               cp_parser_error (parser, "expected %<;%>");
13848               /* Skip tokens until we find a `;'.  */
13849               cp_parser_skip_to_end_of_statement (parser);
13850
13851               break;
13852             }
13853
13854           if (decl)
13855             {
13856               /* Add DECL to the list of members.  */
13857               if (!friend_p)
13858                 finish_member_declaration (decl);
13859
13860               if (TREE_CODE (decl) == FUNCTION_DECL)
13861                 cp_parser_save_default_args (parser, decl);
13862             }
13863         }
13864     }
13865
13866   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13867 }
13868
13869 /* Parse a pure-specifier.
13870
13871    pure-specifier:
13872      = 0
13873
13874    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13875    Otherwise, ERROR_MARK_NODE is returned.  */
13876
13877 static tree
13878 cp_parser_pure_specifier (cp_parser* parser)
13879 {
13880   cp_token *token;
13881
13882   /* Look for the `=' token.  */
13883   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13884     return error_mark_node;
13885   /* Look for the `0' token.  */
13886   token = cp_lexer_consume_token (parser->lexer);
13887   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13888   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13889     {
13890       cp_parser_error (parser,
13891                        "invalid pure specifier (only `= 0' is allowed)");
13892       cp_parser_skip_to_end_of_statement (parser);
13893       return error_mark_node;
13894     }
13895   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13896     {
13897       error ("templates may not be %<virtual%>");
13898       return error_mark_node;
13899     }
13900
13901   return integer_zero_node;
13902 }
13903
13904 /* Parse a constant-initializer.
13905
13906    constant-initializer:
13907      = constant-expression
13908
13909    Returns a representation of the constant-expression.  */
13910
13911 static tree
13912 cp_parser_constant_initializer (cp_parser* parser)
13913 {
13914   /* Look for the `=' token.  */
13915   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13916     return error_mark_node;
13917
13918   /* It is invalid to write:
13919
13920        struct S { static const int i = { 7 }; };
13921
13922      */
13923   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13924     {
13925       cp_parser_error (parser,
13926                        "a brace-enclosed initializer is not allowed here");
13927       /* Consume the opening brace.  */
13928       cp_lexer_consume_token (parser->lexer);
13929       /* Skip the initializer.  */
13930       cp_parser_skip_to_closing_brace (parser);
13931       /* Look for the trailing `}'.  */
13932       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13933
13934       return error_mark_node;
13935     }
13936
13937   return cp_parser_constant_expression (parser,
13938                                         /*allow_non_constant=*/false,
13939                                         NULL);
13940 }
13941
13942 /* Derived classes [gram.class.derived] */
13943
13944 /* Parse a base-clause.
13945
13946    base-clause:
13947      : base-specifier-list
13948
13949    base-specifier-list:
13950      base-specifier
13951      base-specifier-list , base-specifier
13952
13953    Returns a TREE_LIST representing the base-classes, in the order in
13954    which they were declared.  The representation of each node is as
13955    described by cp_parser_base_specifier.
13956
13957    In the case that no bases are specified, this function will return
13958    NULL_TREE, not ERROR_MARK_NODE.  */
13959
13960 static tree
13961 cp_parser_base_clause (cp_parser* parser)
13962 {
13963   tree bases = NULL_TREE;
13964
13965   /* Look for the `:' that begins the list.  */
13966   cp_parser_require (parser, CPP_COLON, "`:'");
13967
13968   /* Scan the base-specifier-list.  */
13969   while (true)
13970     {
13971       cp_token *token;
13972       tree base;
13973
13974       /* Look for the base-specifier.  */
13975       base = cp_parser_base_specifier (parser);
13976       /* Add BASE to the front of the list.  */
13977       if (base != error_mark_node)
13978         {
13979           TREE_CHAIN (base) = bases;
13980           bases = base;
13981         }
13982       /* Peek at the next token.  */
13983       token = cp_lexer_peek_token (parser->lexer);
13984       /* If it's not a comma, then the list is complete.  */
13985       if (token->type != CPP_COMMA)
13986         break;
13987       /* Consume the `,'.  */
13988       cp_lexer_consume_token (parser->lexer);
13989     }
13990
13991   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13992      base class had a qualified name.  However, the next name that
13993      appears is certainly not qualified.  */
13994   parser->scope = NULL_TREE;
13995   parser->qualifying_scope = NULL_TREE;
13996   parser->object_scope = NULL_TREE;
13997
13998   return nreverse (bases);
13999 }
14000
14001 /* Parse a base-specifier.
14002
14003    base-specifier:
14004      :: [opt] nested-name-specifier [opt] class-name
14005      virtual access-specifier [opt] :: [opt] nested-name-specifier
14006        [opt] class-name
14007      access-specifier virtual [opt] :: [opt] nested-name-specifier
14008        [opt] class-name
14009
14010    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14011    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14012    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14013    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14014
14015 static tree
14016 cp_parser_base_specifier (cp_parser* parser)
14017 {
14018   cp_token *token;
14019   bool done = false;
14020   bool virtual_p = false;
14021   bool duplicate_virtual_error_issued_p = false;
14022   bool duplicate_access_error_issued_p = false;
14023   bool class_scope_p, template_p;
14024   tree access = access_default_node;
14025   tree type;
14026
14027   /* Process the optional `virtual' and `access-specifier'.  */
14028   while (!done)
14029     {
14030       /* Peek at the next token.  */
14031       token = cp_lexer_peek_token (parser->lexer);
14032       /* Process `virtual'.  */
14033       switch (token->keyword)
14034         {
14035         case RID_VIRTUAL:
14036           /* If `virtual' appears more than once, issue an error.  */
14037           if (virtual_p && !duplicate_virtual_error_issued_p)
14038             {
14039               cp_parser_error (parser,
14040                                "%<virtual%> specified more than once in base-specified");
14041               duplicate_virtual_error_issued_p = true;
14042             }
14043
14044           virtual_p = true;
14045
14046           /* Consume the `virtual' token.  */
14047           cp_lexer_consume_token (parser->lexer);
14048
14049           break;
14050
14051         case RID_PUBLIC:
14052         case RID_PROTECTED:
14053         case RID_PRIVATE:
14054           /* If more than one access specifier appears, issue an
14055              error.  */
14056           if (access != access_default_node
14057               && !duplicate_access_error_issued_p)
14058             {
14059               cp_parser_error (parser,
14060                                "more than one access specifier in base-specified");
14061               duplicate_access_error_issued_p = true;
14062             }
14063
14064           access = ridpointers[(int) token->keyword];
14065
14066           /* Consume the access-specifier.  */
14067           cp_lexer_consume_token (parser->lexer);
14068
14069           break;
14070
14071         default:
14072           done = true;
14073           break;
14074         }
14075     }
14076   /* It is not uncommon to see programs mechanically, erroneously, use
14077      the 'typename' keyword to denote (dependent) qualified types
14078      as base classes.  */
14079   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14080     {
14081       if (!processing_template_decl)
14082         error ("keyword %<typename%> not allowed outside of templates");
14083       else
14084         error ("keyword %<typename%> not allowed in this context "
14085                "(the base class is implicitly a type)");
14086       cp_lexer_consume_token (parser->lexer);
14087     }
14088
14089   /* Look for the optional `::' operator.  */
14090   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14091   /* Look for the nested-name-specifier.  The simplest way to
14092      implement:
14093
14094        [temp.res]
14095
14096        The keyword `typename' is not permitted in a base-specifier or
14097        mem-initializer; in these contexts a qualified name that
14098        depends on a template-parameter is implicitly assumed to be a
14099        type name.
14100
14101      is to pretend that we have seen the `typename' keyword at this
14102      point.  */
14103   cp_parser_nested_name_specifier_opt (parser,
14104                                        /*typename_keyword_p=*/true,
14105                                        /*check_dependency_p=*/true,
14106                                        typename_type,
14107                                        /*is_declaration=*/true);
14108   /* If the base class is given by a qualified name, assume that names
14109      we see are type names or templates, as appropriate.  */
14110   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14111   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14112
14113   /* Finally, look for the class-name.  */
14114   type = cp_parser_class_name (parser,
14115                                class_scope_p,
14116                                template_p,
14117                                typename_type,
14118                                /*check_dependency_p=*/true,
14119                                /*class_head_p=*/false,
14120                                /*is_declaration=*/true);
14121
14122   if (type == error_mark_node)
14123     return error_mark_node;
14124
14125   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14126 }
14127
14128 /* Exception handling [gram.exception] */
14129
14130 /* Parse an (optional) exception-specification.
14131
14132    exception-specification:
14133      throw ( type-id-list [opt] )
14134
14135    Returns a TREE_LIST representing the exception-specification.  The
14136    TREE_VALUE of each node is a type.  */
14137
14138 static tree
14139 cp_parser_exception_specification_opt (cp_parser* parser)
14140 {
14141   cp_token *token;
14142   tree type_id_list;
14143
14144   /* Peek at the next token.  */
14145   token = cp_lexer_peek_token (parser->lexer);
14146   /* If it's not `throw', then there's no exception-specification.  */
14147   if (!cp_parser_is_keyword (token, RID_THROW))
14148     return NULL_TREE;
14149
14150   /* Consume the `throw'.  */
14151   cp_lexer_consume_token (parser->lexer);
14152
14153   /* Look for the `('.  */
14154   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14155
14156   /* Peek at the next token.  */
14157   token = cp_lexer_peek_token (parser->lexer);
14158   /* If it's not a `)', then there is a type-id-list.  */
14159   if (token->type != CPP_CLOSE_PAREN)
14160     {
14161       const char *saved_message;
14162
14163       /* Types may not be defined in an exception-specification.  */
14164       saved_message = parser->type_definition_forbidden_message;
14165       parser->type_definition_forbidden_message
14166         = "types may not be defined in an exception-specification";
14167       /* Parse the type-id-list.  */
14168       type_id_list = cp_parser_type_id_list (parser);
14169       /* Restore the saved message.  */
14170       parser->type_definition_forbidden_message = saved_message;
14171     }
14172   else
14173     type_id_list = empty_except_spec;
14174
14175   /* Look for the `)'.  */
14176   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14177
14178   return type_id_list;
14179 }
14180
14181 /* Parse an (optional) type-id-list.
14182
14183    type-id-list:
14184      type-id
14185      type-id-list , type-id
14186
14187    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14188    in the order that the types were presented.  */
14189
14190 static tree
14191 cp_parser_type_id_list (cp_parser* parser)
14192 {
14193   tree types = NULL_TREE;
14194
14195   while (true)
14196     {
14197       cp_token *token;
14198       tree type;
14199
14200       /* Get the next type-id.  */
14201       type = cp_parser_type_id (parser);
14202       /* Add it to the list.  */
14203       types = add_exception_specifier (types, type, /*complain=*/1);
14204       /* Peek at the next token.  */
14205       token = cp_lexer_peek_token (parser->lexer);
14206       /* If it is not a `,', we are done.  */
14207       if (token->type != CPP_COMMA)
14208         break;
14209       /* Consume the `,'.  */
14210       cp_lexer_consume_token (parser->lexer);
14211     }
14212
14213   return nreverse (types);
14214 }
14215
14216 /* Parse a try-block.
14217
14218    try-block:
14219      try compound-statement handler-seq  */
14220
14221 static tree
14222 cp_parser_try_block (cp_parser* parser)
14223 {
14224   tree try_block;
14225
14226   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14227   try_block = begin_try_block ();
14228   cp_parser_compound_statement (parser, NULL, true);
14229   finish_try_block (try_block);
14230   cp_parser_handler_seq (parser);
14231   finish_handler_sequence (try_block);
14232
14233   return try_block;
14234 }
14235
14236 /* Parse a function-try-block.
14237
14238    function-try-block:
14239      try ctor-initializer [opt] function-body handler-seq  */
14240
14241 static bool
14242 cp_parser_function_try_block (cp_parser* parser)
14243 {
14244   tree compound_stmt;
14245   tree try_block;
14246   bool ctor_initializer_p;
14247
14248   /* Look for the `try' keyword.  */
14249   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14250     return false;
14251   /* Let the rest of the front-end know where we are.  */
14252   try_block = begin_function_try_block (&compound_stmt);
14253   /* Parse the function-body.  */
14254   ctor_initializer_p
14255     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14256   /* We're done with the `try' part.  */
14257   finish_function_try_block (try_block);
14258   /* Parse the handlers.  */
14259   cp_parser_handler_seq (parser);
14260   /* We're done with the handlers.  */
14261   finish_function_handler_sequence (try_block, compound_stmt);
14262
14263   return ctor_initializer_p;
14264 }
14265
14266 /* Parse a handler-seq.
14267
14268    handler-seq:
14269      handler handler-seq [opt]  */
14270
14271 static void
14272 cp_parser_handler_seq (cp_parser* parser)
14273 {
14274   while (true)
14275     {
14276       cp_token *token;
14277
14278       /* Parse the handler.  */
14279       cp_parser_handler (parser);
14280       /* Peek at the next token.  */
14281       token = cp_lexer_peek_token (parser->lexer);
14282       /* If it's not `catch' then there are no more handlers.  */
14283       if (!cp_parser_is_keyword (token, RID_CATCH))
14284         break;
14285     }
14286 }
14287
14288 /* Parse a handler.
14289
14290    handler:
14291      catch ( exception-declaration ) compound-statement  */
14292
14293 static void
14294 cp_parser_handler (cp_parser* parser)
14295 {
14296   tree handler;
14297   tree declaration;
14298
14299   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14300   handler = begin_handler ();
14301   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14302   declaration = cp_parser_exception_declaration (parser);
14303   finish_handler_parms (declaration, handler);
14304   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14305   cp_parser_compound_statement (parser, NULL, false);
14306   finish_handler (handler);
14307 }
14308
14309 /* Parse an exception-declaration.
14310
14311    exception-declaration:
14312      type-specifier-seq declarator
14313      type-specifier-seq abstract-declarator
14314      type-specifier-seq
14315      ...
14316
14317    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14318    ellipsis variant is used.  */
14319
14320 static tree
14321 cp_parser_exception_declaration (cp_parser* parser)
14322 {
14323   cp_decl_specifier_seq type_specifiers;
14324   cp_declarator *declarator;
14325   const char *saved_message;
14326
14327   /* If it's an ellipsis, it's easy to handle.  */
14328   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14329     {
14330       /* Consume the `...' token.  */
14331       cp_lexer_consume_token (parser->lexer);
14332       return NULL_TREE;
14333     }
14334
14335   /* Types may not be defined in exception-declarations.  */
14336   saved_message = parser->type_definition_forbidden_message;
14337   parser->type_definition_forbidden_message
14338     = "types may not be defined in exception-declarations";
14339
14340   /* Parse the type-specifier-seq.  */
14341   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14342                                 &type_specifiers);
14343   /* If it's a `)', then there is no declarator.  */
14344   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14345     declarator = NULL;
14346   else
14347     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14348                                        /*ctor_dtor_or_conv_p=*/NULL,
14349                                        /*parenthesized_p=*/NULL,
14350                                        /*member_p=*/false);
14351
14352   /* Restore the saved message.  */
14353   parser->type_definition_forbidden_message = saved_message;
14354
14355   if (!type_specifiers.any_specifiers_p)
14356     return error_mark_node;
14357
14358   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14359 }
14360
14361 /* Parse a throw-expression.
14362
14363    throw-expression:
14364      throw assignment-expression [opt]
14365
14366    Returns a THROW_EXPR representing the throw-expression.  */
14367
14368 static tree
14369 cp_parser_throw_expression (cp_parser* parser)
14370 {
14371   tree expression;
14372   cp_token* token;
14373
14374   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14375   token = cp_lexer_peek_token (parser->lexer);
14376   /* Figure out whether or not there is an assignment-expression
14377      following the "throw" keyword.  */
14378   if (token->type == CPP_COMMA
14379       || token->type == CPP_SEMICOLON
14380       || token->type == CPP_CLOSE_PAREN
14381       || token->type == CPP_CLOSE_SQUARE
14382       || token->type == CPP_CLOSE_BRACE
14383       || token->type == CPP_COLON)
14384     expression = NULL_TREE;
14385   else
14386     expression = cp_parser_assignment_expression (parser,
14387                                                   /*cast_p=*/false);
14388
14389   return build_throw (expression);
14390 }
14391
14392 /* GNU Extensions */
14393
14394 /* Parse an (optional) asm-specification.
14395
14396    asm-specification:
14397      asm ( string-literal )
14398
14399    If the asm-specification is present, returns a STRING_CST
14400    corresponding to the string-literal.  Otherwise, returns
14401    NULL_TREE.  */
14402
14403 static tree
14404 cp_parser_asm_specification_opt (cp_parser* parser)
14405 {
14406   cp_token *token;
14407   tree asm_specification;
14408
14409   /* Peek at the next token.  */
14410   token = cp_lexer_peek_token (parser->lexer);
14411   /* If the next token isn't the `asm' keyword, then there's no
14412      asm-specification.  */
14413   if (!cp_parser_is_keyword (token, RID_ASM))
14414     return NULL_TREE;
14415
14416   /* Consume the `asm' token.  */
14417   cp_lexer_consume_token (parser->lexer);
14418   /* Look for the `('.  */
14419   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14420
14421   /* Look for the string-literal.  */
14422   asm_specification = cp_parser_string_literal (parser, false, false);
14423
14424   /* Look for the `)'.  */
14425   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14426
14427   return asm_specification;
14428 }
14429
14430 /* Parse an asm-operand-list.
14431
14432    asm-operand-list:
14433      asm-operand
14434      asm-operand-list , asm-operand
14435
14436    asm-operand:
14437      string-literal ( expression )
14438      [ string-literal ] string-literal ( expression )
14439
14440    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14441    each node is the expression.  The TREE_PURPOSE is itself a
14442    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14443    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14444    is a STRING_CST for the string literal before the parenthesis.  */
14445
14446 static tree
14447 cp_parser_asm_operand_list (cp_parser* parser)
14448 {
14449   tree asm_operands = NULL_TREE;
14450
14451   while (true)
14452     {
14453       tree string_literal;
14454       tree expression;
14455       tree name;
14456
14457       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14458         {
14459           /* Consume the `[' token.  */
14460           cp_lexer_consume_token (parser->lexer);
14461           /* Read the operand name.  */
14462           name = cp_parser_identifier (parser);
14463           if (name != error_mark_node)
14464             name = build_string (IDENTIFIER_LENGTH (name),
14465                                  IDENTIFIER_POINTER (name));
14466           /* Look for the closing `]'.  */
14467           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14468         }
14469       else
14470         name = NULL_TREE;
14471       /* Look for the string-literal.  */
14472       string_literal = cp_parser_string_literal (parser, false, false);
14473
14474       /* Look for the `('.  */
14475       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14476       /* Parse the expression.  */
14477       expression = cp_parser_expression (parser, /*cast_p=*/false);
14478       /* Look for the `)'.  */
14479       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14480
14481       /* Add this operand to the list.  */
14482       asm_operands = tree_cons (build_tree_list (name, string_literal),
14483                                 expression,
14484                                 asm_operands);
14485       /* If the next token is not a `,', there are no more
14486          operands.  */
14487       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14488         break;
14489       /* Consume the `,'.  */
14490       cp_lexer_consume_token (parser->lexer);
14491     }
14492
14493   return nreverse (asm_operands);
14494 }
14495
14496 /* Parse an asm-clobber-list.
14497
14498    asm-clobber-list:
14499      string-literal
14500      asm-clobber-list , string-literal
14501
14502    Returns a TREE_LIST, indicating the clobbers in the order that they
14503    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14504
14505 static tree
14506 cp_parser_asm_clobber_list (cp_parser* parser)
14507 {
14508   tree clobbers = NULL_TREE;
14509
14510   while (true)
14511     {
14512       tree string_literal;
14513
14514       /* Look for the string literal.  */
14515       string_literal = cp_parser_string_literal (parser, false, false);
14516       /* Add it to the list.  */
14517       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14518       /* If the next token is not a `,', then the list is
14519          complete.  */
14520       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14521         break;
14522       /* Consume the `,' token.  */
14523       cp_lexer_consume_token (parser->lexer);
14524     }
14525
14526   return clobbers;
14527 }
14528
14529 /* Parse an (optional) series of attributes.
14530
14531    attributes:
14532      attributes attribute
14533
14534    attribute:
14535      __attribute__ (( attribute-list [opt] ))
14536
14537    The return value is as for cp_parser_attribute_list.  */
14538
14539 static tree
14540 cp_parser_attributes_opt (cp_parser* parser)
14541 {
14542   tree attributes = NULL_TREE;
14543
14544   while (true)
14545     {
14546       cp_token *token;
14547       tree attribute_list;
14548
14549       /* Peek at the next token.  */
14550       token = cp_lexer_peek_token (parser->lexer);
14551       /* If it's not `__attribute__', then we're done.  */
14552       if (token->keyword != RID_ATTRIBUTE)
14553         break;
14554
14555       /* Consume the `__attribute__' keyword.  */
14556       cp_lexer_consume_token (parser->lexer);
14557       /* Look for the two `(' tokens.  */
14558       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14559       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14560
14561       /* Peek at the next token.  */
14562       token = cp_lexer_peek_token (parser->lexer);
14563       if (token->type != CPP_CLOSE_PAREN)
14564         /* Parse the attribute-list.  */
14565         attribute_list = cp_parser_attribute_list (parser);
14566       else
14567         /* If the next token is a `)', then there is no attribute
14568            list.  */
14569         attribute_list = NULL;
14570
14571       /* Look for the two `)' tokens.  */
14572       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14573       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14574
14575       /* Add these new attributes to the list.  */
14576       attributes = chainon (attributes, attribute_list);
14577     }
14578
14579   return attributes;
14580 }
14581
14582 /* Parse an attribute-list.
14583
14584    attribute-list:
14585      attribute
14586      attribute-list , attribute
14587
14588    attribute:
14589      identifier
14590      identifier ( identifier )
14591      identifier ( identifier , expression-list )
14592      identifier ( expression-list )
14593
14594    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14595    to an attribute.  The TREE_PURPOSE of each node is the identifier
14596    indicating which attribute is in use.  The TREE_VALUE represents
14597    the arguments, if any.  */
14598
14599 static tree
14600 cp_parser_attribute_list (cp_parser* parser)
14601 {
14602   tree attribute_list = NULL_TREE;
14603   bool save_translate_strings_p = parser->translate_strings_p;
14604
14605   parser->translate_strings_p = false;
14606   while (true)
14607     {
14608       cp_token *token;
14609       tree identifier;
14610       tree attribute;
14611
14612       /* Look for the identifier.  We also allow keywords here; for
14613          example `__attribute__ ((const))' is legal.  */
14614       token = cp_lexer_peek_token (parser->lexer);
14615       if (token->type == CPP_NAME
14616           || token->type == CPP_KEYWORD)
14617         {
14618           tree arguments = NULL_TREE;
14619
14620           /* Consume the token.  */
14621           token = cp_lexer_consume_token (parser->lexer);
14622
14623           /* Save away the identifier that indicates which attribute
14624              this is.  */
14625           identifier = token->value;
14626           attribute = build_tree_list (identifier, NULL_TREE);
14627
14628           /* Peek at the next token.  */
14629           token = cp_lexer_peek_token (parser->lexer);
14630           /* If it's an `(', then parse the attribute arguments.  */
14631           if (token->type == CPP_OPEN_PAREN)
14632             {
14633               arguments = cp_parser_parenthesized_expression_list
14634                           (parser, true, /*cast_p=*/false,
14635                            /*non_constant_p=*/NULL);
14636               /* Save the arguments away.  */
14637               TREE_VALUE (attribute) = arguments;
14638             }
14639
14640           if (arguments != error_mark_node)
14641             {
14642               /* Add this attribute to the list.  */
14643               TREE_CHAIN (attribute) = attribute_list;
14644               attribute_list = attribute;
14645             }
14646
14647           token = cp_lexer_peek_token (parser->lexer);
14648         }
14649       /* Now, look for more attributes.  If the next token isn't a
14650          `,', we're done.  */
14651       if (token->type != CPP_COMMA)
14652         break;
14653
14654       /* Consume the comma and keep going.  */
14655       cp_lexer_consume_token (parser->lexer);
14656     }
14657   parser->translate_strings_p = save_translate_strings_p;
14658
14659   /* We built up the list in reverse order.  */
14660   return nreverse (attribute_list);
14661 }
14662
14663 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14664    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14665    current value of the PEDANTIC flag, regardless of whether or not
14666    the `__extension__' keyword is present.  The caller is responsible
14667    for restoring the value of the PEDANTIC flag.  */
14668
14669 static bool
14670 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14671 {
14672   /* Save the old value of the PEDANTIC flag.  */
14673   *saved_pedantic = pedantic;
14674
14675   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14676     {
14677       /* Consume the `__extension__' token.  */
14678       cp_lexer_consume_token (parser->lexer);
14679       /* We're not being pedantic while the `__extension__' keyword is
14680          in effect.  */
14681       pedantic = 0;
14682
14683       return true;
14684     }
14685
14686   return false;
14687 }
14688
14689 /* Parse a label declaration.
14690
14691    label-declaration:
14692      __label__ label-declarator-seq ;
14693
14694    label-declarator-seq:
14695      identifier , label-declarator-seq
14696      identifier  */
14697
14698 static void
14699 cp_parser_label_declaration (cp_parser* parser)
14700 {
14701   /* Look for the `__label__' keyword.  */
14702   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14703
14704   while (true)
14705     {
14706       tree identifier;
14707
14708       /* Look for an identifier.  */
14709       identifier = cp_parser_identifier (parser);
14710       /* If we failed, stop.  */
14711       if (identifier == error_mark_node)
14712         break;
14713       /* Declare it as a label.  */
14714       finish_label_decl (identifier);
14715       /* If the next token is a `;', stop.  */
14716       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14717         break;
14718       /* Look for the `,' separating the label declarations.  */
14719       cp_parser_require (parser, CPP_COMMA, "`,'");
14720     }
14721
14722   /* Look for the final `;'.  */
14723   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14724 }
14725
14726 /* Support Functions */
14727
14728 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14729    NAME should have one of the representations used for an
14730    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14731    is returned.  If PARSER->SCOPE is a dependent type, then a
14732    SCOPE_REF is returned.
14733
14734    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14735    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14736    was formed.  Abstractly, such entities should not be passed to this
14737    function, because they do not need to be looked up, but it is
14738    simpler to check for this special case here, rather than at the
14739    call-sites.
14740
14741    In cases not explicitly covered above, this function returns a
14742    DECL, OVERLOAD, or baselink representing the result of the lookup.
14743    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14744    is returned.
14745
14746    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14747    (e.g., "struct") that was used.  In that case bindings that do not
14748    refer to types are ignored.
14749
14750    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14751    ignored.
14752
14753    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14754    are ignored.
14755
14756    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14757    types.
14758
14759    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14760    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14761    NULL_TREE otherwise.  */
14762
14763 static tree
14764 cp_parser_lookup_name (cp_parser *parser, tree name,
14765                        enum tag_types tag_type,
14766                        bool is_template,
14767                        bool is_namespace,
14768                        bool check_dependency,
14769                        tree *ambiguous_decls)
14770 {
14771   int flags = 0;
14772   tree decl;
14773   tree object_type = parser->context->object_type;
14774
14775   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14776     flags |= LOOKUP_COMPLAIN;
14777
14778   /* Assume that the lookup will be unambiguous.  */
14779   if (ambiguous_decls)
14780     *ambiguous_decls = NULL_TREE;
14781
14782   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14783      no longer valid.  Note that if we are parsing tentatively, and
14784      the parse fails, OBJECT_TYPE will be automatically restored.  */
14785   parser->context->object_type = NULL_TREE;
14786
14787   if (name == error_mark_node)
14788     return error_mark_node;
14789
14790   /* A template-id has already been resolved; there is no lookup to
14791      do.  */
14792   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14793     return name;
14794   if (BASELINK_P (name))
14795     {
14796       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14797                   == TEMPLATE_ID_EXPR);
14798       return name;
14799     }
14800
14801   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14802      it should already have been checked to make sure that the name
14803      used matches the type being destroyed.  */
14804   if (TREE_CODE (name) == BIT_NOT_EXPR)
14805     {
14806       tree type;
14807
14808       /* Figure out to which type this destructor applies.  */
14809       if (parser->scope)
14810         type = parser->scope;
14811       else if (object_type)
14812         type = object_type;
14813       else
14814         type = current_class_type;
14815       /* If that's not a class type, there is no destructor.  */
14816       if (!type || !CLASS_TYPE_P (type))
14817         return error_mark_node;
14818       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14819         lazily_declare_fn (sfk_destructor, type);
14820       if (!CLASSTYPE_DESTRUCTORS (type))
14821           return error_mark_node;
14822       /* If it was a class type, return the destructor.  */
14823       return CLASSTYPE_DESTRUCTORS (type);
14824     }
14825
14826   /* By this point, the NAME should be an ordinary identifier.  If
14827      the id-expression was a qualified name, the qualifying scope is
14828      stored in PARSER->SCOPE at this point.  */
14829   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14830
14831   /* Perform the lookup.  */
14832   if (parser->scope)
14833     {
14834       bool dependent_p;
14835
14836       if (parser->scope == error_mark_node)
14837         return error_mark_node;
14838
14839       /* If the SCOPE is dependent, the lookup must be deferred until
14840          the template is instantiated -- unless we are explicitly
14841          looking up names in uninstantiated templates.  Even then, we
14842          cannot look up the name if the scope is not a class type; it
14843          might, for example, be a template type parameter.  */
14844       dependent_p = (TYPE_P (parser->scope)
14845                      && !(parser->in_declarator_p
14846                           && currently_open_class (parser->scope))
14847                      && dependent_type_p (parser->scope));
14848       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14849            && dependent_p)
14850         {
14851           if (tag_type)
14852             {
14853               tree type;
14854
14855               /* The resolution to Core Issue 180 says that `struct
14856                  A::B' should be considered a type-name, even if `A'
14857                  is dependent.  */
14858               type = make_typename_type (parser->scope, name, tag_type,
14859                                          /*complain=*/tf_error);
14860               decl = TYPE_NAME (type);
14861             }
14862           else if (is_template
14863                    && (cp_parser_next_token_ends_template_argument_p (parser)
14864                        || cp_lexer_next_token_is (parser->lexer,
14865                                                   CPP_CLOSE_PAREN)))
14866             decl = make_unbound_class_template (parser->scope,
14867                                                 name, NULL_TREE,
14868                                                 /*complain=*/tf_error);
14869           else
14870             decl = build_qualified_name (/*type=*/NULL_TREE,
14871                                          parser->scope, name,
14872                                          is_template);
14873         }
14874       else
14875         {
14876           tree pushed_scope = NULL_TREE;
14877
14878           /* If PARSER->SCOPE is a dependent type, then it must be a
14879              class type, and we must not be checking dependencies;
14880              otherwise, we would have processed this lookup above.  So
14881              that PARSER->SCOPE is not considered a dependent base by
14882              lookup_member, we must enter the scope here.  */
14883           if (dependent_p)
14884             pushed_scope = push_scope (parser->scope);
14885           /* If the PARSER->SCOPE is a template specialization, it
14886              may be instantiated during name lookup.  In that case,
14887              errors may be issued.  Even if we rollback the current
14888              tentative parse, those errors are valid.  */
14889           decl = lookup_qualified_name (parser->scope, name,
14890                                         tag_type != none_type,
14891                                         /*complain=*/true);
14892           if (pushed_scope)
14893             pop_scope (pushed_scope);
14894         }
14895       parser->qualifying_scope = parser->scope;
14896       parser->object_scope = NULL_TREE;
14897     }
14898   else if (object_type)
14899     {
14900       tree object_decl = NULL_TREE;
14901       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14902          OBJECT_TYPE is not a class.  */
14903       if (CLASS_TYPE_P (object_type))
14904         /* If the OBJECT_TYPE is a template specialization, it may
14905            be instantiated during name lookup.  In that case, errors
14906            may be issued.  Even if we rollback the current tentative
14907            parse, those errors are valid.  */
14908         object_decl = lookup_member (object_type,
14909                                      name,
14910                                      /*protect=*/0,
14911                                      tag_type != none_type);
14912       /* Look it up in the enclosing context, too.  */
14913       decl = lookup_name_real (name, tag_type != none_type,
14914                                /*nonclass=*/0,
14915                                /*block_p=*/true, is_namespace, flags);
14916       parser->object_scope = object_type;
14917       parser->qualifying_scope = NULL_TREE;
14918       if (object_decl)
14919         decl = object_decl;
14920     }
14921   else
14922     {
14923       decl = lookup_name_real (name, tag_type != none_type,
14924                                /*nonclass=*/0,
14925                                /*block_p=*/true, is_namespace, flags);
14926       parser->qualifying_scope = NULL_TREE;
14927       parser->object_scope = NULL_TREE;
14928     }
14929
14930   /* If the lookup failed, let our caller know.  */
14931   if (!decl || decl == error_mark_node)
14932     return error_mark_node;
14933
14934   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14935   if (TREE_CODE (decl) == TREE_LIST)
14936     {
14937       if (ambiguous_decls)
14938         *ambiguous_decls = decl;
14939       /* The error message we have to print is too complicated for
14940          cp_parser_error, so we incorporate its actions directly.  */
14941       if (!cp_parser_simulate_error (parser))
14942         {
14943           error ("reference to %qD is ambiguous", name);
14944           print_candidates (decl);
14945         }
14946       return error_mark_node;
14947     }
14948
14949   gcc_assert (DECL_P (decl)
14950               || TREE_CODE (decl) == OVERLOAD
14951               || TREE_CODE (decl) == SCOPE_REF
14952               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14953               || BASELINK_P (decl));
14954
14955   /* If we have resolved the name of a member declaration, check to
14956      see if the declaration is accessible.  When the name resolves to
14957      set of overloaded functions, accessibility is checked when
14958      overload resolution is done.
14959
14960      During an explicit instantiation, access is not checked at all,
14961      as per [temp.explicit].  */
14962   if (DECL_P (decl))
14963     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14964
14965   return decl;
14966 }
14967
14968 /* Like cp_parser_lookup_name, but for use in the typical case where
14969    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14970    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14971
14972 static tree
14973 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14974 {
14975   return cp_parser_lookup_name (parser, name,
14976                                 none_type,
14977                                 /*is_template=*/false,
14978                                 /*is_namespace=*/false,
14979                                 /*check_dependency=*/true,
14980                                 /*ambiguous_decls=*/NULL);
14981 }
14982
14983 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14984    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14985    true, the DECL indicates the class being defined in a class-head,
14986    or declared in an elaborated-type-specifier.
14987
14988    Otherwise, return DECL.  */
14989
14990 static tree
14991 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14992 {
14993   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14994      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14995
14996        struct A {
14997          template <typename T> struct B;
14998        };
14999
15000        template <typename T> struct A::B {};
15001
15002      Similarly, in an elaborated-type-specifier:
15003
15004        namespace N { struct X{}; }
15005
15006        struct A {
15007          template <typename T> friend struct N::X;
15008        };
15009
15010      However, if the DECL refers to a class type, and we are in
15011      the scope of the class, then the name lookup automatically
15012      finds the TYPE_DECL created by build_self_reference rather
15013      than a TEMPLATE_DECL.  For example, in:
15014
15015        template <class T> struct S {
15016          S s;
15017        };
15018
15019      there is no need to handle such case.  */
15020
15021   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15022     return DECL_TEMPLATE_RESULT (decl);
15023
15024   return decl;
15025 }
15026
15027 /* If too many, or too few, template-parameter lists apply to the
15028    declarator, issue an error message.  Returns TRUE if all went well,
15029    and FALSE otherwise.  */
15030
15031 static bool
15032 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15033                                                 cp_declarator *declarator)
15034 {
15035   unsigned num_templates;
15036
15037   /* We haven't seen any classes that involve template parameters yet.  */
15038   num_templates = 0;
15039
15040   switch (declarator->kind)
15041     {
15042     case cdk_id:
15043       if (declarator->u.id.qualifying_scope)
15044         {
15045           tree scope;
15046           tree member;
15047
15048           scope = declarator->u.id.qualifying_scope;
15049           member = declarator->u.id.unqualified_name;
15050
15051           while (scope && CLASS_TYPE_P (scope))
15052             {
15053               /* You're supposed to have one `template <...>'
15054                  for every template class, but you don't need one
15055                  for a full specialization.  For example:
15056
15057                  template <class T> struct S{};
15058                  template <> struct S<int> { void f(); };
15059                  void S<int>::f () {}
15060
15061                  is correct; there shouldn't be a `template <>' for
15062                  the definition of `S<int>::f'.  */
15063               if (CLASSTYPE_TEMPLATE_INFO (scope)
15064                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15065                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15066                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15067                 ++num_templates;
15068
15069               scope = TYPE_CONTEXT (scope);
15070             }
15071         }
15072       else if (TREE_CODE (declarator->u.id.unqualified_name)
15073                == TEMPLATE_ID_EXPR)
15074         /* If the DECLARATOR has the form `X<y>' then it uses one
15075            additional level of template parameters.  */
15076         ++num_templates;
15077
15078       return cp_parser_check_template_parameters (parser,
15079                                                   num_templates);
15080
15081     case cdk_function:
15082     case cdk_array:
15083     case cdk_pointer:
15084     case cdk_reference:
15085     case cdk_ptrmem:
15086       return (cp_parser_check_declarator_template_parameters
15087               (parser, declarator->declarator));
15088
15089     case cdk_error:
15090       return true;
15091
15092     default:
15093       gcc_unreachable ();
15094     }
15095   return false;
15096 }
15097
15098 /* NUM_TEMPLATES were used in the current declaration.  If that is
15099    invalid, return FALSE and issue an error messages.  Otherwise,
15100    return TRUE.  */
15101
15102 static bool
15103 cp_parser_check_template_parameters (cp_parser* parser,
15104                                      unsigned num_templates)
15105 {
15106   /* If there are more template classes than parameter lists, we have
15107      something like:
15108
15109        template <class T> void S<T>::R<T>::f ();  */
15110   if (parser->num_template_parameter_lists < num_templates)
15111     {
15112       error ("too few template-parameter-lists");
15113       return false;
15114     }
15115   /* If there are the same number of template classes and parameter
15116      lists, that's OK.  */
15117   if (parser->num_template_parameter_lists == num_templates)
15118     return true;
15119   /* If there are more, but only one more, then we are referring to a
15120      member template.  That's OK too.  */
15121   if (parser->num_template_parameter_lists == num_templates + 1)
15122       return true;
15123   /* Otherwise, there are too many template parameter lists.  We have
15124      something like:
15125
15126      template <class T> template <class U> void S::f();  */
15127   error ("too many template-parameter-lists");
15128   return false;
15129 }
15130
15131 /* Parse an optional `::' token indicating that the following name is
15132    from the global namespace.  If so, PARSER->SCOPE is set to the
15133    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15134    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15135    Returns the new value of PARSER->SCOPE, if the `::' token is
15136    present, and NULL_TREE otherwise.  */
15137
15138 static tree
15139 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15140 {
15141   cp_token *token;
15142
15143   /* Peek at the next token.  */
15144   token = cp_lexer_peek_token (parser->lexer);
15145   /* If we're looking at a `::' token then we're starting from the
15146      global namespace, not our current location.  */
15147   if (token->type == CPP_SCOPE)
15148     {
15149       /* Consume the `::' token.  */
15150       cp_lexer_consume_token (parser->lexer);
15151       /* Set the SCOPE so that we know where to start the lookup.  */
15152       parser->scope = global_namespace;
15153       parser->qualifying_scope = global_namespace;
15154       parser->object_scope = NULL_TREE;
15155
15156       return parser->scope;
15157     }
15158   else if (!current_scope_valid_p)
15159     {
15160       parser->scope = NULL_TREE;
15161       parser->qualifying_scope = NULL_TREE;
15162       parser->object_scope = NULL_TREE;
15163     }
15164
15165   return NULL_TREE;
15166 }
15167
15168 /* Returns TRUE if the upcoming token sequence is the start of a
15169    constructor declarator.  If FRIEND_P is true, the declarator is
15170    preceded by the `friend' specifier.  */
15171
15172 static bool
15173 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15174 {
15175   bool constructor_p;
15176   tree type_decl = NULL_TREE;
15177   bool nested_name_p;
15178   cp_token *next_token;
15179
15180   /* The common case is that this is not a constructor declarator, so
15181      try to avoid doing lots of work if at all possible.  It's not
15182      valid declare a constructor at function scope.  */
15183   if (at_function_scope_p ())
15184     return false;
15185   /* And only certain tokens can begin a constructor declarator.  */
15186   next_token = cp_lexer_peek_token (parser->lexer);
15187   if (next_token->type != CPP_NAME
15188       && next_token->type != CPP_SCOPE
15189       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15190       && next_token->type != CPP_TEMPLATE_ID)
15191     return false;
15192
15193   /* Parse tentatively; we are going to roll back all of the tokens
15194      consumed here.  */
15195   cp_parser_parse_tentatively (parser);
15196   /* Assume that we are looking at a constructor declarator.  */
15197   constructor_p = true;
15198
15199   /* Look for the optional `::' operator.  */
15200   cp_parser_global_scope_opt (parser,
15201                               /*current_scope_valid_p=*/false);
15202   /* Look for the nested-name-specifier.  */
15203   nested_name_p
15204     = (cp_parser_nested_name_specifier_opt (parser,
15205                                             /*typename_keyword_p=*/false,
15206                                             /*check_dependency_p=*/false,
15207                                             /*type_p=*/false,
15208                                             /*is_declaration=*/false)
15209        != NULL_TREE);
15210   /* Outside of a class-specifier, there must be a
15211      nested-name-specifier.  */
15212   if (!nested_name_p &&
15213       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15214        || friend_p))
15215     constructor_p = false;
15216   /* If we still think that this might be a constructor-declarator,
15217      look for a class-name.  */
15218   if (constructor_p)
15219     {
15220       /* If we have:
15221
15222            template <typename T> struct S { S(); };
15223            template <typename T> S<T>::S ();
15224
15225          we must recognize that the nested `S' names a class.
15226          Similarly, for:
15227
15228            template <typename T> S<T>::S<T> ();
15229
15230          we must recognize that the nested `S' names a template.  */
15231       type_decl = cp_parser_class_name (parser,
15232                                         /*typename_keyword_p=*/false,
15233                                         /*template_keyword_p=*/false,
15234                                         none_type,
15235                                         /*check_dependency_p=*/false,
15236                                         /*class_head_p=*/false,
15237                                         /*is_declaration=*/false);
15238       /* If there was no class-name, then this is not a constructor.  */
15239       constructor_p = !cp_parser_error_occurred (parser);
15240     }
15241
15242   /* If we're still considering a constructor, we have to see a `(',
15243      to begin the parameter-declaration-clause, followed by either a
15244      `)', an `...', or a decl-specifier.  We need to check for a
15245      type-specifier to avoid being fooled into thinking that:
15246
15247        S::S (f) (int);
15248
15249      is a constructor.  (It is actually a function named `f' that
15250      takes one parameter (of type `int') and returns a value of type
15251      `S::S'.  */
15252   if (constructor_p
15253       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15254     {
15255       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15256           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15257           /* A parameter declaration begins with a decl-specifier,
15258              which is either the "attribute" keyword, a storage class
15259              specifier, or (usually) a type-specifier.  */
15260           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15261           && !cp_parser_storage_class_specifier_opt (parser))
15262         {
15263           tree type;
15264           tree pushed_scope = NULL_TREE;
15265           unsigned saved_num_template_parameter_lists;
15266
15267           /* Names appearing in the type-specifier should be looked up
15268              in the scope of the class.  */
15269           if (current_class_type)
15270             type = NULL_TREE;
15271           else
15272             {
15273               type = TREE_TYPE (type_decl);
15274               if (TREE_CODE (type) == TYPENAME_TYPE)
15275                 {
15276                   type = resolve_typename_type (type,
15277                                                 /*only_current_p=*/false);
15278                   if (type == error_mark_node)
15279                     {
15280                       cp_parser_abort_tentative_parse (parser);
15281                       return false;
15282                     }
15283                 }
15284               pushed_scope = push_scope (type);
15285             }
15286
15287           /* Inside the constructor parameter list, surrounding
15288              template-parameter-lists do not apply.  */
15289           saved_num_template_parameter_lists
15290             = parser->num_template_parameter_lists;
15291           parser->num_template_parameter_lists = 0;
15292
15293           /* Look for the type-specifier.  */
15294           cp_parser_type_specifier (parser,
15295                                     CP_PARSER_FLAGS_NONE,
15296                                     /*decl_specs=*/NULL,
15297                                     /*is_declarator=*/true,
15298                                     /*declares_class_or_enum=*/NULL,
15299                                     /*is_cv_qualifier=*/NULL);
15300
15301           parser->num_template_parameter_lists
15302             = saved_num_template_parameter_lists;
15303
15304           /* Leave the scope of the class.  */
15305           if (pushed_scope)
15306             pop_scope (pushed_scope);
15307
15308           constructor_p = !cp_parser_error_occurred (parser);
15309         }
15310     }
15311   else
15312     constructor_p = false;
15313   /* We did not really want to consume any tokens.  */
15314   cp_parser_abort_tentative_parse (parser);
15315
15316   return constructor_p;
15317 }
15318
15319 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15320    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15321    they must be performed once we are in the scope of the function.
15322
15323    Returns the function defined.  */
15324
15325 static tree
15326 cp_parser_function_definition_from_specifiers_and_declarator
15327   (cp_parser* parser,
15328    cp_decl_specifier_seq *decl_specifiers,
15329    tree attributes,
15330    const cp_declarator *declarator)
15331 {
15332   tree fn;
15333   bool success_p;
15334
15335   /* Begin the function-definition.  */
15336   success_p = start_function (decl_specifiers, declarator, attributes);
15337
15338   /* The things we're about to see are not directly qualified by any
15339      template headers we've seen thus far.  */
15340   reset_specialization ();
15341
15342   /* If there were names looked up in the decl-specifier-seq that we
15343      did not check, check them now.  We must wait until we are in the
15344      scope of the function to perform the checks, since the function
15345      might be a friend.  */
15346   perform_deferred_access_checks ();
15347
15348   if (!success_p)
15349     {
15350       /* Skip the entire function.  */
15351       cp_parser_skip_to_end_of_block_or_statement (parser);
15352       fn = error_mark_node;
15353     }
15354   else
15355     fn = cp_parser_function_definition_after_declarator (parser,
15356                                                          /*inline_p=*/false);
15357
15358   return fn;
15359 }
15360
15361 /* Parse the part of a function-definition that follows the
15362    declarator.  INLINE_P is TRUE iff this function is an inline
15363    function defined with a class-specifier.
15364
15365    Returns the function defined.  */
15366
15367 static tree
15368 cp_parser_function_definition_after_declarator (cp_parser* parser,
15369                                                 bool inline_p)
15370 {
15371   tree fn;
15372   bool ctor_initializer_p = false;
15373   bool saved_in_unbraced_linkage_specification_p;
15374   unsigned saved_num_template_parameter_lists;
15375
15376   /* If the next token is `return', then the code may be trying to
15377      make use of the "named return value" extension that G++ used to
15378      support.  */
15379   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15380     {
15381       /* Consume the `return' keyword.  */
15382       cp_lexer_consume_token (parser->lexer);
15383       /* Look for the identifier that indicates what value is to be
15384          returned.  */
15385       cp_parser_identifier (parser);
15386       /* Issue an error message.  */
15387       error ("named return values are no longer supported");
15388       /* Skip tokens until we reach the start of the function body.  */
15389       while (true)
15390         {
15391           cp_token *token = cp_lexer_peek_token (parser->lexer);
15392           if (token->type == CPP_OPEN_BRACE
15393               || token->type == CPP_EOF
15394               || token->type == CPP_PRAGMA_EOL)
15395             break;
15396           cp_lexer_consume_token (parser->lexer);
15397         }
15398     }
15399   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15400      anything declared inside `f'.  */
15401   saved_in_unbraced_linkage_specification_p
15402     = parser->in_unbraced_linkage_specification_p;
15403   parser->in_unbraced_linkage_specification_p = false;
15404   /* Inside the function, surrounding template-parameter-lists do not
15405      apply.  */
15406   saved_num_template_parameter_lists
15407     = parser->num_template_parameter_lists;
15408   parser->num_template_parameter_lists = 0;
15409   /* If the next token is `try', then we are looking at a
15410      function-try-block.  */
15411   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15412     ctor_initializer_p = cp_parser_function_try_block (parser);
15413   /* A function-try-block includes the function-body, so we only do
15414      this next part if we're not processing a function-try-block.  */
15415   else
15416     ctor_initializer_p
15417       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15418
15419   /* Finish the function.  */
15420   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15421                         (inline_p ? 2 : 0));
15422   /* Generate code for it, if necessary.  */
15423   expand_or_defer_fn (fn);
15424   /* Restore the saved values.  */
15425   parser->in_unbraced_linkage_specification_p
15426     = saved_in_unbraced_linkage_specification_p;
15427   parser->num_template_parameter_lists
15428     = saved_num_template_parameter_lists;
15429
15430   return fn;
15431 }
15432
15433 /* Parse a template-declaration, assuming that the `export' (and
15434    `extern') keywords, if present, has already been scanned.  MEMBER_P
15435    is as for cp_parser_template_declaration.  */
15436
15437 static void
15438 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15439 {
15440   tree decl = NULL_TREE;
15441   tree checks;
15442   tree parameter_list;
15443   bool friend_p = false;
15444   bool need_lang_pop;
15445
15446   /* Look for the `template' keyword.  */
15447   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15448     return;
15449
15450   /* And the `<'.  */
15451   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15452     return;
15453   /* [temp]
15454
15455      A template ... shall not have C linkage.  */
15456   if (current_lang_name == lang_name_c)
15457     {
15458       error ("template with C linkage");
15459       /* Give it C++ linkage to avoid confusing other parts of the
15460          front end.  */
15461       push_lang_context (lang_name_cplusplus);
15462       need_lang_pop = true;
15463     }
15464   else
15465     need_lang_pop = false;
15466
15467   /* We cannot perform access checks on the template parameter
15468      declarations until we know what is being declared, just as we
15469      cannot check the decl-specifier list.  */
15470   push_deferring_access_checks (dk_deferred);
15471
15472   /* If the next token is `>', then we have an invalid
15473      specialization.  Rather than complain about an invalid template
15474      parameter, issue an error message here.  */
15475   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15476     {
15477       cp_parser_error (parser, "invalid explicit specialization");
15478       begin_specialization ();
15479       parameter_list = NULL_TREE;
15480     }
15481   else
15482     /* Parse the template parameters.  */
15483     parameter_list = cp_parser_template_parameter_list (parser);
15484
15485   /* Get the deferred access checks from the parameter list.  These
15486      will be checked once we know what is being declared, as for a
15487      member template the checks must be performed in the scope of the
15488      class containing the member.  */
15489   checks = get_deferred_access_checks ();
15490
15491   /* Look for the `>'.  */
15492   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15493   /* We just processed one more parameter list.  */
15494   ++parser->num_template_parameter_lists;
15495   /* If the next token is `template', there are more template
15496      parameters.  */
15497   if (cp_lexer_next_token_is_keyword (parser->lexer,
15498                                       RID_TEMPLATE))
15499     cp_parser_template_declaration_after_export (parser, member_p);
15500   else
15501     {
15502       /* There are no access checks when parsing a template, as we do not
15503          know if a specialization will be a friend.  */
15504       push_deferring_access_checks (dk_no_check);
15505       decl = cp_parser_single_declaration (parser,
15506                                            checks,
15507                                            member_p,
15508                                            &friend_p);
15509       pop_deferring_access_checks ();
15510
15511       /* If this is a member template declaration, let the front
15512          end know.  */
15513       if (member_p && !friend_p && decl)
15514         {
15515           if (TREE_CODE (decl) == TYPE_DECL)
15516             cp_parser_check_access_in_redeclaration (decl);
15517
15518           decl = finish_member_template_decl (decl);
15519         }
15520       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15521         make_friend_class (current_class_type, TREE_TYPE (decl),
15522                            /*complain=*/true);
15523     }
15524   /* We are done with the current parameter list.  */
15525   --parser->num_template_parameter_lists;
15526
15527   pop_deferring_access_checks ();
15528
15529   /* Finish up.  */
15530   finish_template_decl (parameter_list);
15531
15532   /* Register member declarations.  */
15533   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15534     finish_member_declaration (decl);
15535   /* For the erroneous case of a template with C linkage, we pushed an
15536      implicit C++ linkage scope; exit that scope now.  */
15537   if (need_lang_pop)
15538     pop_lang_context ();
15539   /* If DECL is a function template, we must return to parse it later.
15540      (Even though there is no definition, there might be default
15541      arguments that need handling.)  */
15542   if (member_p && decl
15543       && (TREE_CODE (decl) == FUNCTION_DECL
15544           || DECL_FUNCTION_TEMPLATE_P (decl)))
15545     TREE_VALUE (parser->unparsed_functions_queues)
15546       = tree_cons (NULL_TREE, decl,
15547                    TREE_VALUE (parser->unparsed_functions_queues));
15548 }
15549
15550 /* Perform the deferred access checks from a template-parameter-list.
15551    CHECKS is a TREE_LIST of access checks, as returned by
15552    get_deferred_access_checks.  */
15553
15554 static void
15555 cp_parser_perform_template_parameter_access_checks (tree checks)
15556 {
15557   ++processing_template_parmlist;
15558   perform_access_checks (checks);
15559   --processing_template_parmlist;
15560 }
15561
15562 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15563    `function-definition' sequence.  MEMBER_P is true, this declaration
15564    appears in a class scope.
15565
15566    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15567    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15568
15569 static tree
15570 cp_parser_single_declaration (cp_parser* parser,
15571                               tree checks,
15572                               bool member_p,
15573                               bool* friend_p)
15574 {
15575   int declares_class_or_enum;
15576   tree decl = NULL_TREE;
15577   cp_decl_specifier_seq decl_specifiers;
15578   bool function_definition_p = false;
15579
15580   /* This function is only used when processing a template
15581      declaration.  */
15582   gcc_assert (innermost_scope_kind () == sk_template_parms
15583               || innermost_scope_kind () == sk_template_spec);
15584
15585   /* Defer access checks until we know what is being declared.  */
15586   push_deferring_access_checks (dk_deferred);
15587
15588   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15589      alternative.  */
15590   cp_parser_decl_specifier_seq (parser,
15591                                 CP_PARSER_FLAGS_OPTIONAL,
15592                                 &decl_specifiers,
15593                                 &declares_class_or_enum);
15594   if (friend_p)
15595     *friend_p = cp_parser_friend_p (&decl_specifiers);
15596
15597   /* There are no template typedefs.  */
15598   if (decl_specifiers.specs[(int) ds_typedef])
15599     {
15600       error ("template declaration of %qs", "typedef");
15601       decl = error_mark_node;
15602     }
15603
15604   /* Gather up the access checks that occurred the
15605      decl-specifier-seq.  */
15606   stop_deferring_access_checks ();
15607
15608   /* Check for the declaration of a template class.  */
15609   if (declares_class_or_enum)
15610     {
15611       if (cp_parser_declares_only_class_p (parser))
15612         {
15613           decl = shadow_tag (&decl_specifiers);
15614
15615           /* In this case:
15616
15617                struct C {
15618                  friend template <typename T> struct A<T>::B;
15619                };
15620
15621              A<T>::B will be represented by a TYPENAME_TYPE, and
15622              therefore not recognized by shadow_tag.  */
15623           if (friend_p && *friend_p
15624               && !decl
15625               && decl_specifiers.type
15626               && TYPE_P (decl_specifiers.type))
15627             decl = decl_specifiers.type;
15628
15629           if (decl && decl != error_mark_node)
15630             decl = TYPE_NAME (decl);
15631           else
15632             decl = error_mark_node;
15633
15634           /* Perform access checks for template parameters.  */
15635           cp_parser_perform_template_parameter_access_checks (checks);
15636         }
15637     }
15638   /* If it's not a template class, try for a template function.  If
15639      the next token is a `;', then this declaration does not declare
15640      anything.  But, if there were errors in the decl-specifiers, then
15641      the error might well have come from an attempted class-specifier.
15642      In that case, there's no need to warn about a missing declarator.  */
15643   if (!decl
15644       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15645           || decl_specifiers.type != error_mark_node))
15646     decl = cp_parser_init_declarator (parser,
15647                                       &decl_specifiers,
15648                                       checks,
15649                                       /*function_definition_allowed_p=*/true,
15650                                       member_p,
15651                                       declares_class_or_enum,
15652                                       &function_definition_p);
15653
15654   pop_deferring_access_checks ();
15655
15656   /* Clear any current qualification; whatever comes next is the start
15657      of something new.  */
15658   parser->scope = NULL_TREE;
15659   parser->qualifying_scope = NULL_TREE;
15660   parser->object_scope = NULL_TREE;
15661   /* Look for a trailing `;' after the declaration.  */
15662   if (!function_definition_p
15663       && (decl == error_mark_node
15664           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15665     cp_parser_skip_to_end_of_block_or_statement (parser);
15666
15667   return decl;
15668 }
15669
15670 /* Parse a cast-expression that is not the operand of a unary "&".  */
15671
15672 static tree
15673 cp_parser_simple_cast_expression (cp_parser *parser)
15674 {
15675   return cp_parser_cast_expression (parser, /*address_p=*/false,
15676                                     /*cast_p=*/false);
15677 }
15678
15679 /* Parse a functional cast to TYPE.  Returns an expression
15680    representing the cast.  */
15681
15682 static tree
15683 cp_parser_functional_cast (cp_parser* parser, tree type)
15684 {
15685   tree expression_list;
15686   tree cast;
15687
15688   expression_list
15689     = cp_parser_parenthesized_expression_list (parser, false,
15690                                                /*cast_p=*/true,
15691                                                /*non_constant_p=*/NULL);
15692
15693   cast = build_functional_cast (type, expression_list);
15694   /* [expr.const]/1: In an integral constant expression "only type
15695      conversions to integral or enumeration type can be used".  */
15696   if (TREE_CODE (type) == TYPE_DECL)
15697     type = TREE_TYPE (type);
15698   if (cast != error_mark_node && !dependent_type_p (type)
15699       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15700     {
15701       if (cp_parser_non_integral_constant_expression
15702           (parser, "a call to a constructor"))
15703         return error_mark_node;
15704     }
15705   return cast;
15706 }
15707
15708 /* Save the tokens that make up the body of a member function defined
15709    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15710    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15711    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15712    for the member function.  */
15713
15714 static tree
15715 cp_parser_save_member_function_body (cp_parser* parser,
15716                                      cp_decl_specifier_seq *decl_specifiers,
15717                                      cp_declarator *declarator,
15718                                      tree attributes)
15719 {
15720   cp_token *first;
15721   cp_token *last;
15722   tree fn;
15723
15724   /* Create the function-declaration.  */
15725   fn = start_method (decl_specifiers, declarator, attributes);
15726   /* If something went badly wrong, bail out now.  */
15727   if (fn == error_mark_node)
15728     {
15729       /* If there's a function-body, skip it.  */
15730       if (cp_parser_token_starts_function_definition_p
15731           (cp_lexer_peek_token (parser->lexer)))
15732         cp_parser_skip_to_end_of_block_or_statement (parser);
15733       return error_mark_node;
15734     }
15735
15736   /* Remember it, if there default args to post process.  */
15737   cp_parser_save_default_args (parser, fn);
15738
15739   /* Save away the tokens that make up the body of the
15740      function.  */
15741   first = parser->lexer->next_token;
15742   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15743   /* Handle function try blocks.  */
15744   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15745     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15746   last = parser->lexer->next_token;
15747
15748   /* Save away the inline definition; we will process it when the
15749      class is complete.  */
15750   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15751   DECL_PENDING_INLINE_P (fn) = 1;
15752
15753   /* We need to know that this was defined in the class, so that
15754      friend templates are handled correctly.  */
15755   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15756
15757   /* We're done with the inline definition.  */
15758   finish_method (fn);
15759
15760   /* Add FN to the queue of functions to be parsed later.  */
15761   TREE_VALUE (parser->unparsed_functions_queues)
15762     = tree_cons (NULL_TREE, fn,
15763                  TREE_VALUE (parser->unparsed_functions_queues));
15764
15765   return fn;
15766 }
15767
15768 /* Parse a template-argument-list, as well as the trailing ">" (but
15769    not the opening ">").  See cp_parser_template_argument_list for the
15770    return value.  */
15771
15772 static tree
15773 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15774 {
15775   tree arguments;
15776   tree saved_scope;
15777   tree saved_qualifying_scope;
15778   tree saved_object_scope;
15779   bool saved_greater_than_is_operator_p;
15780   bool saved_skip_evaluation;
15781
15782   /* [temp.names]
15783
15784      When parsing a template-id, the first non-nested `>' is taken as
15785      the end of the template-argument-list rather than a greater-than
15786      operator.  */
15787   saved_greater_than_is_operator_p
15788     = parser->greater_than_is_operator_p;
15789   parser->greater_than_is_operator_p = false;
15790   /* Parsing the argument list may modify SCOPE, so we save it
15791      here.  */
15792   saved_scope = parser->scope;
15793   saved_qualifying_scope = parser->qualifying_scope;
15794   saved_object_scope = parser->object_scope;
15795   /* We need to evaluate the template arguments, even though this
15796      template-id may be nested within a "sizeof".  */
15797   saved_skip_evaluation = skip_evaluation;
15798   skip_evaluation = false;
15799   /* Parse the template-argument-list itself.  */
15800   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15801     arguments = NULL_TREE;
15802   else
15803     arguments = cp_parser_template_argument_list (parser);
15804   /* Look for the `>' that ends the template-argument-list. If we find
15805      a '>>' instead, it's probably just a typo.  */
15806   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15807     {
15808       if (!saved_greater_than_is_operator_p)
15809         {
15810           /* If we're in a nested template argument list, the '>>' has
15811             to be a typo for '> >'. We emit the error message, but we
15812             continue parsing and we push a '>' as next token, so that
15813             the argument list will be parsed correctly.  Note that the
15814             global source location is still on the token before the
15815             '>>', so we need to say explicitly where we want it.  */
15816           cp_token *token = cp_lexer_peek_token (parser->lexer);
15817           error ("%H%<>>%> should be %<> >%> "
15818                  "within a nested template argument list",
15819                  &token->location);
15820
15821           /* ??? Proper recovery should terminate two levels of
15822              template argument list here.  */
15823           token->type = CPP_GREATER;
15824         }
15825       else
15826         {
15827           /* If this is not a nested template argument list, the '>>'
15828             is a typo for '>'. Emit an error message and continue.
15829             Same deal about the token location, but here we can get it
15830             right by consuming the '>>' before issuing the diagnostic.  */
15831           cp_lexer_consume_token (parser->lexer);
15832           error ("spurious %<>>%>, use %<>%> to terminate "
15833                  "a template argument list");
15834         }
15835     }
15836   else
15837     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15838   /* The `>' token might be a greater-than operator again now.  */
15839   parser->greater_than_is_operator_p
15840     = saved_greater_than_is_operator_p;
15841   /* Restore the SAVED_SCOPE.  */
15842   parser->scope = saved_scope;
15843   parser->qualifying_scope = saved_qualifying_scope;
15844   parser->object_scope = saved_object_scope;
15845   skip_evaluation = saved_skip_evaluation;
15846
15847   return arguments;
15848 }
15849
15850 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15851    arguments, or the body of the function have not yet been parsed,
15852    parse them now.  */
15853
15854 static void
15855 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15856 {
15857   /* If this member is a template, get the underlying
15858      FUNCTION_DECL.  */
15859   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15860     member_function = DECL_TEMPLATE_RESULT (member_function);
15861
15862   /* There should not be any class definitions in progress at this
15863      point; the bodies of members are only parsed outside of all class
15864      definitions.  */
15865   gcc_assert (parser->num_classes_being_defined == 0);
15866   /* While we're parsing the member functions we might encounter more
15867      classes.  We want to handle them right away, but we don't want
15868      them getting mixed up with functions that are currently in the
15869      queue.  */
15870   parser->unparsed_functions_queues
15871     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15872
15873   /* Make sure that any template parameters are in scope.  */
15874   maybe_begin_member_template_processing (member_function);
15875
15876   /* If the body of the function has not yet been parsed, parse it
15877      now.  */
15878   if (DECL_PENDING_INLINE_P (member_function))
15879     {
15880       tree function_scope;
15881       cp_token_cache *tokens;
15882
15883       /* The function is no longer pending; we are processing it.  */
15884       tokens = DECL_PENDING_INLINE_INFO (member_function);
15885       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15886       DECL_PENDING_INLINE_P (member_function) = 0;
15887
15888       /* If this is a local class, enter the scope of the containing
15889          function.  */
15890       function_scope = current_function_decl;
15891       if (function_scope)
15892         push_function_context_to (function_scope);
15893
15894
15895       /* Push the body of the function onto the lexer stack.  */
15896       cp_parser_push_lexer_for_tokens (parser, tokens);
15897
15898       /* Let the front end know that we going to be defining this
15899          function.  */
15900       start_preparsed_function (member_function, NULL_TREE,
15901                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15902
15903       /* Don't do access checking if it is a templated function.  */
15904       if (processing_template_decl)
15905         push_deferring_access_checks (dk_no_check);
15906
15907       /* Now, parse the body of the function.  */
15908       cp_parser_function_definition_after_declarator (parser,
15909                                                       /*inline_p=*/true);
15910
15911       if (processing_template_decl)
15912         pop_deferring_access_checks ();
15913
15914       /* Leave the scope of the containing function.  */
15915       if (function_scope)
15916         pop_function_context_from (function_scope);
15917       cp_parser_pop_lexer (parser);
15918     }
15919
15920   /* Remove any template parameters from the symbol table.  */
15921   maybe_end_member_template_processing ();
15922
15923   /* Restore the queue.  */
15924   parser->unparsed_functions_queues
15925     = TREE_CHAIN (parser->unparsed_functions_queues);
15926 }
15927
15928 /* If DECL contains any default args, remember it on the unparsed
15929    functions queue.  */
15930
15931 static void
15932 cp_parser_save_default_args (cp_parser* parser, tree decl)
15933 {
15934   tree probe;
15935
15936   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15937        probe;
15938        probe = TREE_CHAIN (probe))
15939     if (TREE_PURPOSE (probe))
15940       {
15941         TREE_PURPOSE (parser->unparsed_functions_queues)
15942           = tree_cons (current_class_type, decl,
15943                        TREE_PURPOSE (parser->unparsed_functions_queues));
15944         break;
15945       }
15946 }
15947
15948 /* FN is a FUNCTION_DECL which may contains a parameter with an
15949    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15950    assumes that the current scope is the scope in which the default
15951    argument should be processed.  */
15952
15953 static void
15954 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15955 {
15956   bool saved_local_variables_forbidden_p;
15957   tree parm;
15958
15959   /* While we're parsing the default args, we might (due to the
15960      statement expression extension) encounter more classes.  We want
15961      to handle them right away, but we don't want them getting mixed
15962      up with default args that are currently in the queue.  */
15963   parser->unparsed_functions_queues
15964     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15965
15966   /* Local variable names (and the `this' keyword) may not appear
15967      in a default argument.  */
15968   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15969   parser->local_variables_forbidden_p = true;
15970
15971   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15972        parm;
15973        parm = TREE_CHAIN (parm))
15974     {
15975       cp_token_cache *tokens;
15976       tree default_arg = TREE_PURPOSE (parm);
15977       tree parsed_arg;
15978       VEC(tree,gc) *insts;
15979       tree copy;
15980       unsigned ix;
15981
15982       if (!default_arg)
15983         continue;
15984
15985       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15986         /* This can happen for a friend declaration for a function
15987            already declared with default arguments.  */
15988         continue;
15989
15990        /* Push the saved tokens for the default argument onto the parser's
15991           lexer stack.  */
15992       tokens = DEFARG_TOKENS (default_arg);
15993       cp_parser_push_lexer_for_tokens (parser, tokens);
15994
15995       /* Parse the assignment-expression.  */
15996       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15997
15998       if (!processing_template_decl)
15999         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16000
16001       TREE_PURPOSE (parm) = parsed_arg;
16002
16003       /* Update any instantiations we've already created.  */
16004       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16005            VEC_iterate (tree, insts, ix, copy); ix++)
16006         TREE_PURPOSE (copy) = parsed_arg;
16007
16008       /* If the token stream has not been completely used up, then
16009          there was extra junk after the end of the default
16010          argument.  */
16011       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16012         cp_parser_error (parser, "expected %<,%>");
16013
16014       /* Revert to the main lexer.  */
16015       cp_parser_pop_lexer (parser);
16016     }
16017
16018   /* Make sure no default arg is missing.  */
16019   check_default_args (fn);
16020
16021   /* Restore the state of local_variables_forbidden_p.  */
16022   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16023
16024   /* Restore the queue.  */
16025   parser->unparsed_functions_queues
16026     = TREE_CHAIN (parser->unparsed_functions_queues);
16027 }
16028
16029 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16030    either a TYPE or an expression, depending on the form of the
16031    input.  The KEYWORD indicates which kind of expression we have
16032    encountered.  */
16033
16034 static tree
16035 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16036 {
16037   static const char *format;
16038   tree expr = NULL_TREE;
16039   const char *saved_message;
16040   bool saved_integral_constant_expression_p;
16041   bool saved_non_integral_constant_expression_p;
16042
16043   /* Initialize FORMAT the first time we get here.  */
16044   if (!format)
16045     format = "types may not be defined in '%s' expressions";
16046
16047   /* Types cannot be defined in a `sizeof' expression.  Save away the
16048      old message.  */
16049   saved_message = parser->type_definition_forbidden_message;
16050   /* And create the new one.  */
16051   parser->type_definition_forbidden_message
16052     = XNEWVEC (const char, strlen (format)
16053                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16054                + 1 /* `\0' */);
16055   sprintf ((char *) parser->type_definition_forbidden_message,
16056            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16057
16058   /* The restrictions on constant-expressions do not apply inside
16059      sizeof expressions.  */
16060   saved_integral_constant_expression_p
16061     = parser->integral_constant_expression_p;
16062   saved_non_integral_constant_expression_p
16063     = parser->non_integral_constant_expression_p;
16064   parser->integral_constant_expression_p = false;
16065
16066   /* Do not actually evaluate the expression.  */
16067   ++skip_evaluation;
16068   /* If it's a `(', then we might be looking at the type-id
16069      construction.  */
16070   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16071     {
16072       tree type;
16073       bool saved_in_type_id_in_expr_p;
16074
16075       /* We can't be sure yet whether we're looking at a type-id or an
16076          expression.  */
16077       cp_parser_parse_tentatively (parser);
16078       /* Consume the `('.  */
16079       cp_lexer_consume_token (parser->lexer);
16080       /* Parse the type-id.  */
16081       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16082       parser->in_type_id_in_expr_p = true;
16083       type = cp_parser_type_id (parser);
16084       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16085       /* Now, look for the trailing `)'.  */
16086       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16087       /* If all went well, then we're done.  */
16088       if (cp_parser_parse_definitely (parser))
16089         {
16090           cp_decl_specifier_seq decl_specs;
16091
16092           /* Build a trivial decl-specifier-seq.  */
16093           clear_decl_specs (&decl_specs);
16094           decl_specs.type = type;
16095
16096           /* Call grokdeclarator to figure out what type this is.  */
16097           expr = grokdeclarator (NULL,
16098                                  &decl_specs,
16099                                  TYPENAME,
16100                                  /*initialized=*/0,
16101                                  /*attrlist=*/NULL);
16102         }
16103     }
16104
16105   /* If the type-id production did not work out, then we must be
16106      looking at the unary-expression production.  */
16107   if (!expr)
16108     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16109                                        /*cast_p=*/false);
16110   /* Go back to evaluating expressions.  */
16111   --skip_evaluation;
16112
16113   /* Free the message we created.  */
16114   free ((char *) parser->type_definition_forbidden_message);
16115   /* And restore the old one.  */
16116   parser->type_definition_forbidden_message = saved_message;
16117   parser->integral_constant_expression_p
16118     = saved_integral_constant_expression_p;
16119   parser->non_integral_constant_expression_p
16120     = saved_non_integral_constant_expression_p;
16121
16122   return expr;
16123 }
16124
16125 /* If the current declaration has no declarator, return true.  */
16126
16127 static bool
16128 cp_parser_declares_only_class_p (cp_parser *parser)
16129 {
16130   /* If the next token is a `;' or a `,' then there is no
16131      declarator.  */
16132   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16133           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16134 }
16135
16136 /* Update the DECL_SPECS to reflect the storage class indicated by
16137    KEYWORD.  */
16138
16139 static void
16140 cp_parser_set_storage_class (cp_parser *parser,
16141                              cp_decl_specifier_seq *decl_specs,
16142                              enum rid keyword)
16143 {
16144   cp_storage_class storage_class;
16145
16146   if (parser->in_unbraced_linkage_specification_p)
16147     {
16148       error ("invalid use of %qD in linkage specification",
16149              ridpointers[keyword]);
16150       return;
16151     }
16152   else if (decl_specs->storage_class != sc_none)
16153     {
16154       decl_specs->multiple_storage_classes_p = true;
16155       return;
16156     }
16157
16158   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16159       && decl_specs->specs[(int) ds_thread])
16160     {
16161       error ("%<__thread%> before %qD", ridpointers[keyword]);
16162       decl_specs->specs[(int) ds_thread] = 0;
16163     }
16164
16165   switch (keyword)
16166     {
16167     case RID_AUTO:
16168       storage_class = sc_auto;
16169       break;
16170     case RID_REGISTER:
16171       storage_class = sc_register;
16172       break;
16173     case RID_STATIC:
16174       storage_class = sc_static;
16175       break;
16176     case RID_EXTERN:
16177       storage_class = sc_extern;
16178       break;
16179     case RID_MUTABLE:
16180       storage_class = sc_mutable;
16181       break;
16182     default:
16183       gcc_unreachable ();
16184     }
16185   decl_specs->storage_class = storage_class;
16186 }
16187
16188 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16189    is true, the type is a user-defined type; otherwise it is a
16190    built-in type specified by a keyword.  */
16191
16192 static void
16193 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16194                               tree type_spec,
16195                               bool user_defined_p)
16196 {
16197   decl_specs->any_specifiers_p = true;
16198
16199   /* If the user tries to redeclare bool or wchar_t (with, for
16200      example, in "typedef int wchar_t;") we remember that this is what
16201      happened.  In system headers, we ignore these declarations so
16202      that G++ can work with system headers that are not C++-safe.  */
16203   if (decl_specs->specs[(int) ds_typedef]
16204       && !user_defined_p
16205       && (type_spec == boolean_type_node
16206           || type_spec == wchar_type_node)
16207       && (decl_specs->type
16208           || decl_specs->specs[(int) ds_long]
16209           || decl_specs->specs[(int) ds_short]
16210           || decl_specs->specs[(int) ds_unsigned]
16211           || decl_specs->specs[(int) ds_signed]))
16212     {
16213       decl_specs->redefined_builtin_type = type_spec;
16214       if (!decl_specs->type)
16215         {
16216           decl_specs->type = type_spec;
16217           decl_specs->user_defined_type_p = false;
16218         }
16219     }
16220   else if (decl_specs->type)
16221     decl_specs->multiple_types_p = true;
16222   else
16223     {
16224       decl_specs->type = type_spec;
16225       decl_specs->user_defined_type_p = user_defined_p;
16226       decl_specs->redefined_builtin_type = NULL_TREE;
16227     }
16228 }
16229
16230 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16231    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16232
16233 static bool
16234 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16235 {
16236   return decl_specifiers->specs[(int) ds_friend] != 0;
16237 }
16238
16239 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16240    issue an error message indicating that TOKEN_DESC was expected.
16241
16242    Returns the token consumed, if the token had the appropriate type.
16243    Otherwise, returns NULL.  */
16244
16245 static cp_token *
16246 cp_parser_require (cp_parser* parser,
16247                    enum cpp_ttype type,
16248                    const char* token_desc)
16249 {
16250   if (cp_lexer_next_token_is (parser->lexer, type))
16251     return cp_lexer_consume_token (parser->lexer);
16252   else
16253     {
16254       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16255       if (!cp_parser_simulate_error (parser))
16256         {
16257           char *message = concat ("expected ", token_desc, NULL);
16258           cp_parser_error (parser, message);
16259           free (message);
16260         }
16261       return NULL;
16262     }
16263 }
16264
16265 /* Like cp_parser_require, except that tokens will be skipped until
16266    the desired token is found.  An error message is still produced if
16267    the next token is not as expected.  */
16268
16269 static void
16270 cp_parser_skip_until_found (cp_parser* parser,
16271                             enum cpp_ttype type,
16272                             const char* token_desc)
16273 {
16274   cp_token *token;
16275   unsigned nesting_depth = 0;
16276
16277   if (cp_parser_require (parser, type, token_desc))
16278     return;
16279
16280   /* Skip tokens until the desired token is found.  */
16281   while (true)
16282     {
16283       /* Peek at the next token.  */
16284       token = cp_lexer_peek_token (parser->lexer);
16285
16286       /* If we've reached the token we want, consume it and stop.  */
16287       if (token->type == type && !nesting_depth)
16288         {
16289           cp_lexer_consume_token (parser->lexer);
16290           return;
16291         }
16292
16293       switch (token->type)
16294         {
16295         case CPP_EOF:
16296         case CPP_PRAGMA_EOL:
16297           /* If we've run out of tokens, stop.  */
16298           return;
16299
16300         case CPP_OPEN_BRACE:
16301         case CPP_OPEN_PAREN:
16302         case CPP_OPEN_SQUARE:
16303           ++nesting_depth;
16304           break;
16305
16306         case CPP_CLOSE_BRACE:
16307         case CPP_CLOSE_PAREN:
16308         case CPP_CLOSE_SQUARE:
16309           if (nesting_depth-- == 0)
16310             return;
16311           break;
16312
16313         default:
16314           break;
16315         }
16316
16317       /* Consume this token.  */
16318       cp_lexer_consume_token (parser->lexer);
16319     }
16320 }
16321
16322 /* If the next token is the indicated keyword, consume it.  Otherwise,
16323    issue an error message indicating that TOKEN_DESC was expected.
16324
16325    Returns the token consumed, if the token had the appropriate type.
16326    Otherwise, returns NULL.  */
16327
16328 static cp_token *
16329 cp_parser_require_keyword (cp_parser* parser,
16330                            enum rid keyword,
16331                            const char* token_desc)
16332 {
16333   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16334
16335   if (token && token->keyword != keyword)
16336     {
16337       dyn_string_t error_msg;
16338
16339       /* Format the error message.  */
16340       error_msg = dyn_string_new (0);
16341       dyn_string_append_cstr (error_msg, "expected ");
16342       dyn_string_append_cstr (error_msg, token_desc);
16343       cp_parser_error (parser, error_msg->s);
16344       dyn_string_delete (error_msg);
16345       return NULL;
16346     }
16347
16348   return token;
16349 }
16350
16351 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16352    function-definition.  */
16353
16354 static bool
16355 cp_parser_token_starts_function_definition_p (cp_token* token)
16356 {
16357   return (/* An ordinary function-body begins with an `{'.  */
16358           token->type == CPP_OPEN_BRACE
16359           /* A ctor-initializer begins with a `:'.  */
16360           || token->type == CPP_COLON
16361           /* A function-try-block begins with `try'.  */
16362           || token->keyword == RID_TRY
16363           /* The named return value extension begins with `return'.  */
16364           || token->keyword == RID_RETURN);
16365 }
16366
16367 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16368    definition.  */
16369
16370 static bool
16371 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16372 {
16373   cp_token *token;
16374
16375   token = cp_lexer_peek_token (parser->lexer);
16376   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16377 }
16378
16379 /* Returns TRUE iff the next token is the "," or ">" ending a
16380    template-argument.  */
16381
16382 static bool
16383 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16384 {
16385   cp_token *token;
16386
16387   token = cp_lexer_peek_token (parser->lexer);
16388   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16389 }
16390
16391 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16392    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16393
16394 static bool
16395 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16396                                                      size_t n)
16397 {
16398   cp_token *token;
16399
16400   token = cp_lexer_peek_nth_token (parser->lexer, n);
16401   if (token->type == CPP_LESS)
16402     return true;
16403   /* Check for the sequence `<::' in the original code. It would be lexed as
16404      `[:', where `[' is a digraph, and there is no whitespace before
16405      `:'.  */
16406   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16407     {
16408       cp_token *token2;
16409       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16410       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16411         return true;
16412     }
16413   return false;
16414 }
16415
16416 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16417    or none_type otherwise.  */
16418
16419 static enum tag_types
16420 cp_parser_token_is_class_key (cp_token* token)
16421 {
16422   switch (token->keyword)
16423     {
16424     case RID_CLASS:
16425       return class_type;
16426     case RID_STRUCT:
16427       return record_type;
16428     case RID_UNION:
16429       return union_type;
16430
16431     default:
16432       return none_type;
16433     }
16434 }
16435
16436 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16437
16438 static void
16439 cp_parser_check_class_key (enum tag_types class_key, tree type)
16440 {
16441   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16442     pedwarn ("%qs tag used in naming %q#T",
16443             class_key == union_type ? "union"
16444              : class_key == record_type ? "struct" : "class",
16445              type);
16446 }
16447
16448 /* Issue an error message if DECL is redeclared with different
16449    access than its original declaration [class.access.spec/3].
16450    This applies to nested classes and nested class templates.
16451    [class.mem/1].  */
16452
16453 static void
16454 cp_parser_check_access_in_redeclaration (tree decl)
16455 {
16456   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16457     return;
16458
16459   if ((TREE_PRIVATE (decl)
16460        != (current_access_specifier == access_private_node))
16461       || (TREE_PROTECTED (decl)
16462           != (current_access_specifier == access_protected_node)))
16463     error ("%qD redeclared with different access", decl);
16464 }
16465
16466 /* Look for the `template' keyword, as a syntactic disambiguator.
16467    Return TRUE iff it is present, in which case it will be
16468    consumed.  */
16469
16470 static bool
16471 cp_parser_optional_template_keyword (cp_parser *parser)
16472 {
16473   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16474     {
16475       /* The `template' keyword can only be used within templates;
16476          outside templates the parser can always figure out what is a
16477          template and what is not.  */
16478       if (!processing_template_decl)
16479         {
16480           error ("%<template%> (as a disambiguator) is only allowed "
16481                  "within templates");
16482           /* If this part of the token stream is rescanned, the same
16483              error message would be generated.  So, we purge the token
16484              from the stream.  */
16485           cp_lexer_purge_token (parser->lexer);
16486           return false;
16487         }
16488       else
16489         {
16490           /* Consume the `template' keyword.  */
16491           cp_lexer_consume_token (parser->lexer);
16492           return true;
16493         }
16494     }
16495
16496   return false;
16497 }
16498
16499 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16500    set PARSER->SCOPE, and perform other related actions.  */
16501
16502 static void
16503 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16504 {
16505   tree value;
16506   tree check;
16507
16508   /* Get the stored value.  */
16509   value = cp_lexer_consume_token (parser->lexer)->value;
16510   /* Perform any access checks that were deferred.  */
16511   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16512     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16513   /* Set the scope from the stored value.  */
16514   parser->scope = TREE_VALUE (value);
16515   parser->qualifying_scope = TREE_TYPE (value);
16516   parser->object_scope = NULL_TREE;
16517 }
16518
16519 /* Consume tokens up through a non-nested END token.  */
16520
16521 static void
16522 cp_parser_cache_group (cp_parser *parser,
16523                        enum cpp_ttype end,
16524                        unsigned depth)
16525 {
16526   while (true)
16527     {
16528       cp_token *token;
16529
16530       /* Abort a parenthesized expression if we encounter a brace.  */
16531       if ((end == CPP_CLOSE_PAREN || depth == 0)
16532           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16533         return;
16534       /* If we've reached the end of the file, stop.  */
16535       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16536           || (end != CPP_PRAGMA_EOL
16537               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16538         return;
16539       /* Consume the next token.  */
16540       token = cp_lexer_consume_token (parser->lexer);
16541       /* See if it starts a new group.  */
16542       if (token->type == CPP_OPEN_BRACE)
16543         {
16544           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16545           if (depth == 0)
16546             return;
16547         }
16548       else if (token->type == CPP_OPEN_PAREN)
16549         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16550       else if (token->type == CPP_PRAGMA)
16551         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16552       else if (token->type == end)
16553         return;
16554     }
16555 }
16556
16557 /* Begin parsing tentatively.  We always save tokens while parsing
16558    tentatively so that if the tentative parsing fails we can restore the
16559    tokens.  */
16560
16561 static void
16562 cp_parser_parse_tentatively (cp_parser* parser)
16563 {
16564   /* Enter a new parsing context.  */
16565   parser->context = cp_parser_context_new (parser->context);
16566   /* Begin saving tokens.  */
16567   cp_lexer_save_tokens (parser->lexer);
16568   /* In order to avoid repetitive access control error messages,
16569      access checks are queued up until we are no longer parsing
16570      tentatively.  */
16571   push_deferring_access_checks (dk_deferred);
16572 }
16573
16574 /* Commit to the currently active tentative parse.  */
16575
16576 static void
16577 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16578 {
16579   cp_parser_context *context;
16580   cp_lexer *lexer;
16581
16582   /* Mark all of the levels as committed.  */
16583   lexer = parser->lexer;
16584   for (context = parser->context; context->next; context = context->next)
16585     {
16586       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16587         break;
16588       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16589       while (!cp_lexer_saving_tokens (lexer))
16590         lexer = lexer->next;
16591       cp_lexer_commit_tokens (lexer);
16592     }
16593 }
16594
16595 /* Abort the currently active tentative parse.  All consumed tokens
16596    will be rolled back, and no diagnostics will be issued.  */
16597
16598 static void
16599 cp_parser_abort_tentative_parse (cp_parser* parser)
16600 {
16601   cp_parser_simulate_error (parser);
16602   /* Now, pretend that we want to see if the construct was
16603      successfully parsed.  */
16604   cp_parser_parse_definitely (parser);
16605 }
16606
16607 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16608    token stream.  Otherwise, commit to the tokens we have consumed.
16609    Returns true if no error occurred; false otherwise.  */
16610
16611 static bool
16612 cp_parser_parse_definitely (cp_parser* parser)
16613 {
16614   bool error_occurred;
16615   cp_parser_context *context;
16616
16617   /* Remember whether or not an error occurred, since we are about to
16618      destroy that information.  */
16619   error_occurred = cp_parser_error_occurred (parser);
16620   /* Remove the topmost context from the stack.  */
16621   context = parser->context;
16622   parser->context = context->next;
16623   /* If no parse errors occurred, commit to the tentative parse.  */
16624   if (!error_occurred)
16625     {
16626       /* Commit to the tokens read tentatively, unless that was
16627          already done.  */
16628       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16629         cp_lexer_commit_tokens (parser->lexer);
16630
16631       pop_to_parent_deferring_access_checks ();
16632     }
16633   /* Otherwise, if errors occurred, roll back our state so that things
16634      are just as they were before we began the tentative parse.  */
16635   else
16636     {
16637       cp_lexer_rollback_tokens (parser->lexer);
16638       pop_deferring_access_checks ();
16639     }
16640   /* Add the context to the front of the free list.  */
16641   context->next = cp_parser_context_free_list;
16642   cp_parser_context_free_list = context;
16643
16644   return !error_occurred;
16645 }
16646
16647 /* Returns true if we are parsing tentatively and are not committed to
16648    this tentative parse.  */
16649
16650 static bool
16651 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16652 {
16653   return (cp_parser_parsing_tentatively (parser)
16654           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16655 }
16656
16657 /* Returns nonzero iff an error has occurred during the most recent
16658    tentative parse.  */
16659
16660 static bool
16661 cp_parser_error_occurred (cp_parser* parser)
16662 {
16663   return (cp_parser_parsing_tentatively (parser)
16664           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16665 }
16666
16667 /* Returns nonzero if GNU extensions are allowed.  */
16668
16669 static bool
16670 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16671 {
16672   return parser->allow_gnu_extensions_p;
16673 }
16674 \f
16675 /* Objective-C++ Productions */
16676
16677
16678 /* Parse an Objective-C expression, which feeds into a primary-expression
16679    above.
16680
16681    objc-expression:
16682      objc-message-expression
16683      objc-string-literal
16684      objc-encode-expression
16685      objc-protocol-expression
16686      objc-selector-expression
16687
16688   Returns a tree representation of the expression.  */
16689
16690 static tree
16691 cp_parser_objc_expression (cp_parser* parser)
16692 {
16693   /* Try to figure out what kind of declaration is present.  */
16694   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16695
16696   switch (kwd->type)
16697     {
16698     case CPP_OPEN_SQUARE:
16699       return cp_parser_objc_message_expression (parser);
16700
16701     case CPP_OBJC_STRING:
16702       kwd = cp_lexer_consume_token (parser->lexer);
16703       return objc_build_string_object (kwd->value);
16704
16705     case CPP_KEYWORD:
16706       switch (kwd->keyword)
16707         {
16708         case RID_AT_ENCODE:
16709           return cp_parser_objc_encode_expression (parser);
16710
16711         case RID_AT_PROTOCOL:
16712           return cp_parser_objc_protocol_expression (parser);
16713
16714         case RID_AT_SELECTOR:
16715           return cp_parser_objc_selector_expression (parser);
16716
16717         default:
16718           break;
16719         }
16720     default:
16721       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16722       cp_parser_skip_to_end_of_block_or_statement (parser);
16723     }
16724
16725   return error_mark_node;
16726 }
16727
16728 /* Parse an Objective-C message expression.
16729
16730    objc-message-expression:
16731      [ objc-message-receiver objc-message-args ]
16732
16733    Returns a representation of an Objective-C message.  */
16734
16735 static tree
16736 cp_parser_objc_message_expression (cp_parser* parser)
16737 {
16738   tree receiver, messageargs;
16739
16740   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16741   receiver = cp_parser_objc_message_receiver (parser);
16742   messageargs = cp_parser_objc_message_args (parser);
16743   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16744
16745   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16746 }
16747
16748 /* Parse an objc-message-receiver.
16749
16750    objc-message-receiver:
16751      expression
16752      simple-type-specifier
16753
16754   Returns a representation of the type or expression.  */
16755
16756 static tree
16757 cp_parser_objc_message_receiver (cp_parser* parser)
16758 {
16759   tree rcv;
16760
16761   /* An Objective-C message receiver may be either (1) a type
16762      or (2) an expression.  */
16763   cp_parser_parse_tentatively (parser);
16764   rcv = cp_parser_expression (parser, false);
16765
16766   if (cp_parser_parse_definitely (parser))
16767     return rcv;
16768
16769   rcv = cp_parser_simple_type_specifier (parser,
16770                                          /*decl_specs=*/NULL,
16771                                          CP_PARSER_FLAGS_NONE);
16772
16773   return objc_get_class_reference (rcv);
16774 }
16775
16776 /* Parse the arguments and selectors comprising an Objective-C message.
16777
16778    objc-message-args:
16779      objc-selector
16780      objc-selector-args
16781      objc-selector-args , objc-comma-args
16782
16783    objc-selector-args:
16784      objc-selector [opt] : assignment-expression
16785      objc-selector-args objc-selector [opt] : assignment-expression
16786
16787    objc-comma-args:
16788      assignment-expression
16789      objc-comma-args , assignment-expression
16790
16791    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16792    selector arguments and TREE_VALUE containing a list of comma
16793    arguments.  */
16794
16795 static tree
16796 cp_parser_objc_message_args (cp_parser* parser)
16797 {
16798   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16799   bool maybe_unary_selector_p = true;
16800   cp_token *token = cp_lexer_peek_token (parser->lexer);
16801
16802   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16803     {
16804       tree selector = NULL_TREE, arg;
16805
16806       if (token->type != CPP_COLON)
16807         selector = cp_parser_objc_selector (parser);
16808
16809       /* Detect if we have a unary selector.  */
16810       if (maybe_unary_selector_p
16811           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16812         return build_tree_list (selector, NULL_TREE);
16813
16814       maybe_unary_selector_p = false;
16815       cp_parser_require (parser, CPP_COLON, "`:'");
16816       arg = cp_parser_assignment_expression (parser, false);
16817
16818       sel_args
16819         = chainon (sel_args,
16820                    build_tree_list (selector, arg));
16821
16822       token = cp_lexer_peek_token (parser->lexer);
16823     }
16824
16825   /* Handle non-selector arguments, if any. */
16826   while (token->type == CPP_COMMA)
16827     {
16828       tree arg;
16829
16830       cp_lexer_consume_token (parser->lexer);
16831       arg = cp_parser_assignment_expression (parser, false);
16832
16833       addl_args
16834         = chainon (addl_args,
16835                    build_tree_list (NULL_TREE, arg));
16836
16837       token = cp_lexer_peek_token (parser->lexer);
16838     }
16839
16840   return build_tree_list (sel_args, addl_args);
16841 }
16842
16843 /* Parse an Objective-C encode expression.
16844
16845    objc-encode-expression:
16846      @encode objc-typename
16847
16848    Returns an encoded representation of the type argument.  */
16849
16850 static tree
16851 cp_parser_objc_encode_expression (cp_parser* parser)
16852 {
16853   tree type;
16854
16855   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16856   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16857   type = complete_type (cp_parser_type_id (parser));
16858   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16859
16860   if (!type)
16861     {
16862       error ("%<@encode%> must specify a type as an argument");
16863       return error_mark_node;
16864     }
16865
16866   return objc_build_encode_expr (type);
16867 }
16868
16869 /* Parse an Objective-C @defs expression.  */
16870
16871 static tree
16872 cp_parser_objc_defs_expression (cp_parser *parser)
16873 {
16874   tree name;
16875
16876   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16877   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16878   name = cp_parser_identifier (parser);
16879   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16880
16881   return objc_get_class_ivars (name);
16882 }
16883
16884 /* Parse an Objective-C protocol expression.
16885
16886   objc-protocol-expression:
16887     @protocol ( identifier )
16888
16889   Returns a representation of the protocol expression.  */
16890
16891 static tree
16892 cp_parser_objc_protocol_expression (cp_parser* parser)
16893 {
16894   tree proto;
16895
16896   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16897   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16898   proto = cp_parser_identifier (parser);
16899   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16900
16901   return objc_build_protocol_expr (proto);
16902 }
16903
16904 /* Parse an Objective-C selector expression.
16905
16906    objc-selector-expression:
16907      @selector ( objc-method-signature )
16908
16909    objc-method-signature:
16910      objc-selector
16911      objc-selector-seq
16912
16913    objc-selector-seq:
16914      objc-selector :
16915      objc-selector-seq objc-selector :
16916
16917   Returns a representation of the method selector.  */
16918
16919 static tree
16920 cp_parser_objc_selector_expression (cp_parser* parser)
16921 {
16922   tree sel_seq = NULL_TREE;
16923   bool maybe_unary_selector_p = true;
16924   cp_token *token;
16925
16926   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16927   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16928   token = cp_lexer_peek_token (parser->lexer);
16929
16930   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16931          || token->type == CPP_SCOPE)
16932     {
16933       tree selector = NULL_TREE;
16934
16935       if (token->type != CPP_COLON
16936           || token->type == CPP_SCOPE)
16937         selector = cp_parser_objc_selector (parser);
16938
16939       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16940           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16941         {
16942           /* Detect if we have a unary selector.  */
16943           if (maybe_unary_selector_p)
16944             {
16945               sel_seq = selector;
16946               goto finish_selector;
16947             }
16948           else
16949             {
16950               cp_parser_error (parser, "expected %<:%>");
16951             }
16952         }
16953       maybe_unary_selector_p = false;
16954       token = cp_lexer_consume_token (parser->lexer);
16955
16956       if (token->type == CPP_SCOPE)
16957         {
16958           sel_seq
16959             = chainon (sel_seq,
16960                        build_tree_list (selector, NULL_TREE));
16961           sel_seq
16962             = chainon (sel_seq,
16963                        build_tree_list (NULL_TREE, NULL_TREE));
16964         }
16965       else
16966         sel_seq
16967           = chainon (sel_seq,
16968                      build_tree_list (selector, NULL_TREE));
16969
16970       token = cp_lexer_peek_token (parser->lexer);
16971     }
16972
16973  finish_selector:
16974   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16975
16976   return objc_build_selector_expr (sel_seq);
16977 }
16978
16979 /* Parse a list of identifiers.
16980
16981    objc-identifier-list:
16982      identifier
16983      objc-identifier-list , identifier
16984
16985    Returns a TREE_LIST of identifier nodes.  */
16986
16987 static tree
16988 cp_parser_objc_identifier_list (cp_parser* parser)
16989 {
16990   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16991   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16992
16993   while (sep->type == CPP_COMMA)
16994     {
16995       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16996       list = chainon (list,
16997                       build_tree_list (NULL_TREE,
16998                                        cp_parser_identifier (parser)));
16999       sep = cp_lexer_peek_token (parser->lexer);
17000     }
17001
17002   return list;
17003 }
17004
17005 /* Parse an Objective-C alias declaration.
17006
17007    objc-alias-declaration:
17008      @compatibility_alias identifier identifier ;
17009
17010    This function registers the alias mapping with the Objective-C front-end.
17011    It returns nothing.  */
17012
17013 static void
17014 cp_parser_objc_alias_declaration (cp_parser* parser)
17015 {
17016   tree alias, orig;
17017
17018   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17019   alias = cp_parser_identifier (parser);
17020   orig = cp_parser_identifier (parser);
17021   objc_declare_alias (alias, orig);
17022   cp_parser_consume_semicolon_at_end_of_statement (parser);
17023 }
17024
17025 /* Parse an Objective-C class forward-declaration.
17026
17027    objc-class-declaration:
17028      @class objc-identifier-list ;
17029
17030    The function registers the forward declarations with the Objective-C
17031    front-end.  It returns nothing.  */
17032
17033 static void
17034 cp_parser_objc_class_declaration (cp_parser* parser)
17035 {
17036   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17037   objc_declare_class (cp_parser_objc_identifier_list (parser));
17038   cp_parser_consume_semicolon_at_end_of_statement (parser);
17039 }
17040
17041 /* Parse a list of Objective-C protocol references.
17042
17043    objc-protocol-refs-opt:
17044      objc-protocol-refs [opt]
17045
17046    objc-protocol-refs:
17047      < objc-identifier-list >
17048
17049    Returns a TREE_LIST of identifiers, if any.  */
17050
17051 static tree
17052 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17053 {
17054   tree protorefs = NULL_TREE;
17055
17056   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17057     {
17058       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17059       protorefs = cp_parser_objc_identifier_list (parser);
17060       cp_parser_require (parser, CPP_GREATER, "`>'");
17061     }
17062
17063   return protorefs;
17064 }
17065
17066 /* Parse a Objective-C visibility specification.  */
17067
17068 static void
17069 cp_parser_objc_visibility_spec (cp_parser* parser)
17070 {
17071   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17072
17073   switch (vis->keyword)
17074     {
17075     case RID_AT_PRIVATE:
17076       objc_set_visibility (2);
17077       break;
17078     case RID_AT_PROTECTED:
17079       objc_set_visibility (0);
17080       break;
17081     case RID_AT_PUBLIC:
17082       objc_set_visibility (1);
17083       break;
17084     default:
17085       return;
17086     }
17087
17088   /* Eat '@private'/'@protected'/'@public'.  */
17089   cp_lexer_consume_token (parser->lexer);
17090 }
17091
17092 /* Parse an Objective-C method type.  */
17093
17094 static void
17095 cp_parser_objc_method_type (cp_parser* parser)
17096 {
17097   objc_set_method_type
17098    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17099     ? PLUS_EXPR
17100     : MINUS_EXPR);
17101 }
17102
17103 /* Parse an Objective-C protocol qualifier.  */
17104
17105 static tree
17106 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17107 {
17108   tree quals = NULL_TREE, node;
17109   cp_token *token = cp_lexer_peek_token (parser->lexer);
17110
17111   node = token->value;
17112
17113   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17114          && (node == ridpointers [(int) RID_IN]
17115              || node == ridpointers [(int) RID_OUT]
17116              || node == ridpointers [(int) RID_INOUT]
17117              || node == ridpointers [(int) RID_BYCOPY]
17118              || node == ridpointers [(int) RID_BYREF]
17119              || node == ridpointers [(int) RID_ONEWAY]))
17120     {
17121       quals = tree_cons (NULL_TREE, node, quals);
17122       cp_lexer_consume_token (parser->lexer);
17123       token = cp_lexer_peek_token (parser->lexer);
17124       node = token->value;
17125     }
17126
17127   return quals;
17128 }
17129
17130 /* Parse an Objective-C typename.  */
17131
17132 static tree
17133 cp_parser_objc_typename (cp_parser* parser)
17134 {
17135   tree typename = NULL_TREE;
17136
17137   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17138     {
17139       tree proto_quals, cp_type = NULL_TREE;
17140
17141       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17142       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17143
17144       /* An ObjC type name may consist of just protocol qualifiers, in which
17145          case the type shall default to 'id'.  */
17146       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17147         cp_type = cp_parser_type_id (parser);
17148
17149       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17150       typename = build_tree_list (proto_quals, cp_type);
17151     }
17152
17153   return typename;
17154 }
17155
17156 /* Check to see if TYPE refers to an Objective-C selector name.  */
17157
17158 static bool
17159 cp_parser_objc_selector_p (enum cpp_ttype type)
17160 {
17161   return (type == CPP_NAME || type == CPP_KEYWORD
17162           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17163           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17164           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17165           || type == CPP_XOR || type == CPP_XOR_EQ);
17166 }
17167
17168 /* Parse an Objective-C selector.  */
17169
17170 static tree
17171 cp_parser_objc_selector (cp_parser* parser)
17172 {
17173   cp_token *token = cp_lexer_consume_token (parser->lexer);
17174
17175   if (!cp_parser_objc_selector_p (token->type))
17176     {
17177       error ("invalid Objective-C++ selector name");
17178       return error_mark_node;
17179     }
17180
17181   /* C++ operator names are allowed to appear in ObjC selectors.  */
17182   switch (token->type)
17183     {
17184     case CPP_AND_AND: return get_identifier ("and");
17185     case CPP_AND_EQ: return get_identifier ("and_eq");
17186     case CPP_AND: return get_identifier ("bitand");
17187     case CPP_OR: return get_identifier ("bitor");
17188     case CPP_COMPL: return get_identifier ("compl");
17189     case CPP_NOT: return get_identifier ("not");
17190     case CPP_NOT_EQ: return get_identifier ("not_eq");
17191     case CPP_OR_OR: return get_identifier ("or");
17192     case CPP_OR_EQ: return get_identifier ("or_eq");
17193     case CPP_XOR: return get_identifier ("xor");
17194     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17195     default: return token->value;
17196     }
17197 }
17198
17199 /* Parse an Objective-C params list.  */
17200
17201 static tree
17202 cp_parser_objc_method_keyword_params (cp_parser* parser)
17203 {
17204   tree params = NULL_TREE;
17205   bool maybe_unary_selector_p = true;
17206   cp_token *token = cp_lexer_peek_token (parser->lexer);
17207
17208   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17209     {
17210       tree selector = NULL_TREE, typename, identifier;
17211
17212       if (token->type != CPP_COLON)
17213         selector = cp_parser_objc_selector (parser);
17214
17215       /* Detect if we have a unary selector.  */
17216       if (maybe_unary_selector_p
17217           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17218         return selector;
17219
17220       maybe_unary_selector_p = false;
17221       cp_parser_require (parser, CPP_COLON, "`:'");
17222       typename = cp_parser_objc_typename (parser);
17223       identifier = cp_parser_identifier (parser);
17224
17225       params
17226         = chainon (params,
17227                    objc_build_keyword_decl (selector,
17228                                             typename,
17229                                             identifier));
17230
17231       token = cp_lexer_peek_token (parser->lexer);
17232     }
17233
17234   return params;
17235 }
17236
17237 /* Parse the non-keyword Objective-C params.  */
17238
17239 static tree
17240 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17241 {
17242   tree params = make_node (TREE_LIST);
17243   cp_token *token = cp_lexer_peek_token (parser->lexer);
17244   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17245
17246   while (token->type == CPP_COMMA)
17247     {
17248       cp_parameter_declarator *parmdecl;
17249       tree parm;
17250
17251       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17252       token = cp_lexer_peek_token (parser->lexer);
17253
17254       if (token->type == CPP_ELLIPSIS)
17255         {
17256           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17257           *ellipsisp = true;
17258           break;
17259         }
17260
17261       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17262       parm = grokdeclarator (parmdecl->declarator,
17263                              &parmdecl->decl_specifiers,
17264                              PARM, /*initialized=*/0,
17265                              /*attrlist=*/NULL);
17266
17267       chainon (params, build_tree_list (NULL_TREE, parm));
17268       token = cp_lexer_peek_token (parser->lexer);
17269     }
17270
17271   return params;
17272 }
17273
17274 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17275
17276 static void
17277 cp_parser_objc_interstitial_code (cp_parser* parser)
17278 {
17279   cp_token *token = cp_lexer_peek_token (parser->lexer);
17280
17281   /* If the next token is `extern' and the following token is a string
17282      literal, then we have a linkage specification.  */
17283   if (token->keyword == RID_EXTERN
17284       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17285     cp_parser_linkage_specification (parser);
17286   /* Handle #pragma, if any.  */
17287   else if (token->type == CPP_PRAGMA)
17288     cp_parser_pragma (parser, pragma_external);
17289   /* Allow stray semicolons.  */
17290   else if (token->type == CPP_SEMICOLON)
17291     cp_lexer_consume_token (parser->lexer);
17292   /* Finally, try to parse a block-declaration, or a function-definition.  */
17293   else
17294     cp_parser_block_declaration (parser, /*statement_p=*/false);
17295 }
17296
17297 /* Parse a method signature.  */
17298
17299 static tree
17300 cp_parser_objc_method_signature (cp_parser* parser)
17301 {
17302   tree rettype, kwdparms, optparms;
17303   bool ellipsis = false;
17304
17305   cp_parser_objc_method_type (parser);
17306   rettype = cp_parser_objc_typename (parser);
17307   kwdparms = cp_parser_objc_method_keyword_params (parser);
17308   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17309
17310   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17311 }
17312
17313 /* Pars an Objective-C method prototype list.  */
17314
17315 static void
17316 cp_parser_objc_method_prototype_list (cp_parser* parser)
17317 {
17318   cp_token *token = cp_lexer_peek_token (parser->lexer);
17319
17320   while (token->keyword != RID_AT_END)
17321     {
17322       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17323         {
17324           objc_add_method_declaration
17325            (cp_parser_objc_method_signature (parser));
17326           cp_parser_consume_semicolon_at_end_of_statement (parser);
17327         }
17328       else
17329         /* Allow for interspersed non-ObjC++ code.  */
17330         cp_parser_objc_interstitial_code (parser);
17331
17332       token = cp_lexer_peek_token (parser->lexer);
17333     }
17334
17335   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17336   objc_finish_interface ();
17337 }
17338
17339 /* Parse an Objective-C method definition list.  */
17340
17341 static void
17342 cp_parser_objc_method_definition_list (cp_parser* parser)
17343 {
17344   cp_token *token = cp_lexer_peek_token (parser->lexer);
17345
17346   while (token->keyword != RID_AT_END)
17347     {
17348       tree meth;
17349
17350       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17351         {
17352           push_deferring_access_checks (dk_deferred);
17353           objc_start_method_definition
17354            (cp_parser_objc_method_signature (parser));
17355
17356           /* For historical reasons, we accept an optional semicolon.  */
17357           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17358             cp_lexer_consume_token (parser->lexer);
17359
17360           perform_deferred_access_checks ();
17361           stop_deferring_access_checks ();
17362           meth = cp_parser_function_definition_after_declarator (parser,
17363                                                                  false);
17364           pop_deferring_access_checks ();
17365           objc_finish_method_definition (meth);
17366         }
17367       else
17368         /* Allow for interspersed non-ObjC++ code.  */
17369         cp_parser_objc_interstitial_code (parser);
17370
17371       token = cp_lexer_peek_token (parser->lexer);
17372     }
17373
17374   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17375   objc_finish_implementation ();
17376 }
17377
17378 /* Parse Objective-C ivars.  */
17379
17380 static void
17381 cp_parser_objc_class_ivars (cp_parser* parser)
17382 {
17383   cp_token *token = cp_lexer_peek_token (parser->lexer);
17384
17385   if (token->type != CPP_OPEN_BRACE)
17386     return;     /* No ivars specified.  */
17387
17388   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17389   token = cp_lexer_peek_token (parser->lexer);
17390
17391   while (token->type != CPP_CLOSE_BRACE)
17392     {
17393       cp_decl_specifier_seq declspecs;
17394       int decl_class_or_enum_p;
17395       tree prefix_attributes;
17396
17397       cp_parser_objc_visibility_spec (parser);
17398
17399       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17400         break;
17401
17402       cp_parser_decl_specifier_seq (parser,
17403                                     CP_PARSER_FLAGS_OPTIONAL,
17404                                     &declspecs,
17405                                     &decl_class_or_enum_p);
17406       prefix_attributes = declspecs.attributes;
17407       declspecs.attributes = NULL_TREE;
17408
17409       /* Keep going until we hit the `;' at the end of the
17410          declaration.  */
17411       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17412         {
17413           tree width = NULL_TREE, attributes, first_attribute, decl;
17414           cp_declarator *declarator = NULL;
17415           int ctor_dtor_or_conv_p;
17416
17417           /* Check for a (possibly unnamed) bitfield declaration.  */
17418           token = cp_lexer_peek_token (parser->lexer);
17419           if (token->type == CPP_COLON)
17420             goto eat_colon;
17421
17422           if (token->type == CPP_NAME
17423               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17424                   == CPP_COLON))
17425             {
17426               /* Get the name of the bitfield.  */
17427               declarator = make_id_declarator (NULL_TREE,
17428                                                cp_parser_identifier (parser),
17429                                                sfk_none);
17430
17431              eat_colon:
17432               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17433               /* Get the width of the bitfield.  */
17434               width
17435                 = cp_parser_constant_expression (parser,
17436                                                  /*allow_non_constant=*/false,
17437                                                  NULL);
17438             }
17439           else
17440             {
17441               /* Parse the declarator.  */
17442               declarator
17443                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17444                                         &ctor_dtor_or_conv_p,
17445                                         /*parenthesized_p=*/NULL,
17446                                         /*member_p=*/false);
17447             }
17448
17449           /* Look for attributes that apply to the ivar.  */
17450           attributes = cp_parser_attributes_opt (parser);
17451           /* Remember which attributes are prefix attributes and
17452              which are not.  */
17453           first_attribute = attributes;
17454           /* Combine the attributes.  */
17455           attributes = chainon (prefix_attributes, attributes);
17456
17457           if (width)
17458             {
17459               /* Create the bitfield declaration.  */
17460               decl = grokbitfield (declarator, &declspecs, width);
17461               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17462             }
17463           else
17464             decl = grokfield (declarator, &declspecs,
17465                               NULL_TREE, /*init_const_expr_p=*/false,
17466                               NULL_TREE, attributes);
17467
17468           /* Add the instance variable.  */
17469           objc_add_instance_variable (decl);
17470
17471           /* Reset PREFIX_ATTRIBUTES.  */
17472           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17473             attributes = TREE_CHAIN (attributes);
17474           if (attributes)
17475             TREE_CHAIN (attributes) = NULL_TREE;
17476
17477           token = cp_lexer_peek_token (parser->lexer);
17478
17479           if (token->type == CPP_COMMA)
17480             {
17481               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17482               continue;
17483             }
17484           break;
17485         }
17486
17487       cp_parser_consume_semicolon_at_end_of_statement (parser);
17488       token = cp_lexer_peek_token (parser->lexer);
17489     }
17490
17491   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17492   /* For historical reasons, we accept an optional semicolon.  */
17493   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17494     cp_lexer_consume_token (parser->lexer);
17495 }
17496
17497 /* Parse an Objective-C protocol declaration.  */
17498
17499 static void
17500 cp_parser_objc_protocol_declaration (cp_parser* parser)
17501 {
17502   tree proto, protorefs;
17503   cp_token *tok;
17504
17505   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17506   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17507     {
17508       error ("identifier expected after %<@protocol%>");
17509       goto finish;
17510     }
17511
17512   /* See if we have a forward declaration or a definition.  */
17513   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17514
17515   /* Try a forward declaration first.  */
17516   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17517     {
17518       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17519      finish:
17520       cp_parser_consume_semicolon_at_end_of_statement (parser);
17521     }
17522
17523   /* Ok, we got a full-fledged definition (or at least should).  */
17524   else
17525     {
17526       proto = cp_parser_identifier (parser);
17527       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17528       objc_start_protocol (proto, protorefs);
17529       cp_parser_objc_method_prototype_list (parser);
17530     }
17531 }
17532
17533 /* Parse an Objective-C superclass or category.  */
17534
17535 static void
17536 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17537                                                           tree *categ)
17538 {
17539   cp_token *next = cp_lexer_peek_token (parser->lexer);
17540
17541   *super = *categ = NULL_TREE;
17542   if (next->type == CPP_COLON)
17543     {
17544       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17545       *super = cp_parser_identifier (parser);
17546     }
17547   else if (next->type == CPP_OPEN_PAREN)
17548     {
17549       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17550       *categ = cp_parser_identifier (parser);
17551       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17552     }
17553 }
17554
17555 /* Parse an Objective-C class interface.  */
17556
17557 static void
17558 cp_parser_objc_class_interface (cp_parser* parser)
17559 {
17560   tree name, super, categ, protos;
17561
17562   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17563   name = cp_parser_identifier (parser);
17564   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17565   protos = cp_parser_objc_protocol_refs_opt (parser);
17566
17567   /* We have either a class or a category on our hands.  */
17568   if (categ)
17569     objc_start_category_interface (name, categ, protos);
17570   else
17571     {
17572       objc_start_class_interface (name, super, protos);
17573       /* Handle instance variable declarations, if any.  */
17574       cp_parser_objc_class_ivars (parser);
17575       objc_continue_interface ();
17576     }
17577
17578   cp_parser_objc_method_prototype_list (parser);
17579 }
17580
17581 /* Parse an Objective-C class implementation.  */
17582
17583 static void
17584 cp_parser_objc_class_implementation (cp_parser* parser)
17585 {
17586   tree name, super, categ;
17587
17588   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17589   name = cp_parser_identifier (parser);
17590   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17591
17592   /* We have either a class or a category on our hands.  */
17593   if (categ)
17594     objc_start_category_implementation (name, categ);
17595   else
17596     {
17597       objc_start_class_implementation (name, super);
17598       /* Handle instance variable declarations, if any.  */
17599       cp_parser_objc_class_ivars (parser);
17600       objc_continue_implementation ();
17601     }
17602
17603   cp_parser_objc_method_definition_list (parser);
17604 }
17605
17606 /* Consume the @end token and finish off the implementation.  */
17607
17608 static void
17609 cp_parser_objc_end_implementation (cp_parser* parser)
17610 {
17611   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17612   objc_finish_implementation ();
17613 }
17614
17615 /* Parse an Objective-C declaration.  */
17616
17617 static void
17618 cp_parser_objc_declaration (cp_parser* parser)
17619 {
17620   /* Try to figure out what kind of declaration is present.  */
17621   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17622
17623   switch (kwd->keyword)
17624     {
17625     case RID_AT_ALIAS:
17626       cp_parser_objc_alias_declaration (parser);
17627       break;
17628     case RID_AT_CLASS:
17629       cp_parser_objc_class_declaration (parser);
17630       break;
17631     case RID_AT_PROTOCOL:
17632       cp_parser_objc_protocol_declaration (parser);
17633       break;
17634     case RID_AT_INTERFACE:
17635       cp_parser_objc_class_interface (parser);
17636       break;
17637     case RID_AT_IMPLEMENTATION:
17638       cp_parser_objc_class_implementation (parser);
17639       break;
17640     case RID_AT_END:
17641       cp_parser_objc_end_implementation (parser);
17642       break;
17643     default:
17644       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17645       cp_parser_skip_to_end_of_block_or_statement (parser);
17646     }
17647 }
17648
17649 /* Parse an Objective-C try-catch-finally statement.
17650
17651    objc-try-catch-finally-stmt:
17652      @try compound-statement objc-catch-clause-seq [opt]
17653        objc-finally-clause [opt]
17654
17655    objc-catch-clause-seq:
17656      objc-catch-clause objc-catch-clause-seq [opt]
17657
17658    objc-catch-clause:
17659      @catch ( exception-declaration ) compound-statement
17660
17661    objc-finally-clause
17662      @finally compound-statement
17663
17664    Returns NULL_TREE.  */
17665
17666 static tree
17667 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17668   location_t location;
17669   tree stmt;
17670
17671   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17672   location = cp_lexer_peek_token (parser->lexer)->location;
17673   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17674      node, lest it get absorbed into the surrounding block.  */
17675   stmt = push_stmt_list ();
17676   cp_parser_compound_statement (parser, NULL, false);
17677   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17678
17679   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17680     {
17681       cp_parameter_declarator *parmdecl;
17682       tree parm;
17683
17684       cp_lexer_consume_token (parser->lexer);
17685       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17686       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17687       parm = grokdeclarator (parmdecl->declarator,
17688                              &parmdecl->decl_specifiers,
17689                              PARM, /*initialized=*/0,
17690                              /*attrlist=*/NULL);
17691       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17692       objc_begin_catch_clause (parm);
17693       cp_parser_compound_statement (parser, NULL, false);
17694       objc_finish_catch_clause ();
17695     }
17696
17697   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17698     {
17699       cp_lexer_consume_token (parser->lexer);
17700       location = cp_lexer_peek_token (parser->lexer)->location;
17701       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17702          node, lest it get absorbed into the surrounding block.  */
17703       stmt = push_stmt_list ();
17704       cp_parser_compound_statement (parser, NULL, false);
17705       objc_build_finally_clause (location, pop_stmt_list (stmt));
17706     }
17707
17708   return objc_finish_try_stmt ();
17709 }
17710
17711 /* Parse an Objective-C synchronized statement.
17712
17713    objc-synchronized-stmt:
17714      @synchronized ( expression ) compound-statement
17715
17716    Returns NULL_TREE.  */
17717
17718 static tree
17719 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17720   location_t location;
17721   tree lock, stmt;
17722
17723   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17724
17725   location = cp_lexer_peek_token (parser->lexer)->location;
17726   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17727   lock = cp_parser_expression (parser, false);
17728   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17729
17730   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17731      node, lest it get absorbed into the surrounding block.  */
17732   stmt = push_stmt_list ();
17733   cp_parser_compound_statement (parser, NULL, false);
17734
17735   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17736 }
17737
17738 /* Parse an Objective-C throw statement.
17739
17740    objc-throw-stmt:
17741      @throw assignment-expression [opt] ;
17742
17743    Returns a constructed '@throw' statement.  */
17744
17745 static tree
17746 cp_parser_objc_throw_statement (cp_parser *parser) {
17747   tree expr = NULL_TREE;
17748
17749   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17750
17751   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17752     expr = cp_parser_assignment_expression (parser, false);
17753
17754   cp_parser_consume_semicolon_at_end_of_statement (parser);
17755
17756   return objc_build_throw_stmt (expr);
17757 }
17758
17759 /* Parse an Objective-C statement.  */
17760
17761 static tree
17762 cp_parser_objc_statement (cp_parser * parser) {
17763   /* Try to figure out what kind of declaration is present.  */
17764   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17765
17766   switch (kwd->keyword)
17767     {
17768     case RID_AT_TRY:
17769       return cp_parser_objc_try_catch_finally_statement (parser);
17770     case RID_AT_SYNCHRONIZED:
17771       return cp_parser_objc_synchronized_statement (parser);
17772     case RID_AT_THROW:
17773       return cp_parser_objc_throw_statement (parser);
17774     default:
17775       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17776       cp_parser_skip_to_end_of_block_or_statement (parser);
17777     }
17778
17779   return error_mark_node;
17780 }
17781 \f
17782 /* OpenMP 2.5 parsing routines.  */
17783
17784 /* All OpenMP clauses.  OpenMP 2.5.  */
17785 typedef enum pragma_omp_clause {
17786   PRAGMA_OMP_CLAUSE_NONE = 0,
17787
17788   PRAGMA_OMP_CLAUSE_COPYIN,
17789   PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17790   PRAGMA_OMP_CLAUSE_DEFAULT,
17791   PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17792   PRAGMA_OMP_CLAUSE_IF,
17793   PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17794   PRAGMA_OMP_CLAUSE_NOWAIT,
17795   PRAGMA_OMP_CLAUSE_NUM_THREADS,
17796   PRAGMA_OMP_CLAUSE_ORDERED,
17797   PRAGMA_OMP_CLAUSE_PRIVATE,
17798   PRAGMA_OMP_CLAUSE_REDUCTION,
17799   PRAGMA_OMP_CLAUSE_SCHEDULE,
17800   PRAGMA_OMP_CLAUSE_SHARED
17801 } pragma_omp_clause;
17802
17803 /* Returns name of the next clause.
17804    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17805    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
17806    returned and the token is consumed.  */
17807
17808 static pragma_omp_clause
17809 cp_parser_omp_clause_name (cp_parser *parser)
17810 {
17811   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17812
17813   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17814     result = PRAGMA_OMP_CLAUSE_IF;
17815   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17816     result = PRAGMA_OMP_CLAUSE_DEFAULT;
17817   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17818     result = PRAGMA_OMP_CLAUSE_PRIVATE;
17819   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17820     {
17821       tree id = cp_lexer_peek_token (parser->lexer)->value;
17822       const char *p = IDENTIFIER_POINTER (id);
17823
17824       switch (p[0])
17825         {
17826         case 'c':
17827           if (!strcmp ("copyin", p))
17828             result = PRAGMA_OMP_CLAUSE_COPYIN;
17829           else if (!strcmp ("copyprivate", p))
17830             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17831           break;
17832         case 'f':
17833           if (!strcmp ("firstprivate", p))
17834             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17835           break;
17836         case 'l':
17837           if (!strcmp ("lastprivate", p))
17838             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17839           break;
17840         case 'n':
17841           if (!strcmp ("nowait", p))
17842             result = PRAGMA_OMP_CLAUSE_NOWAIT;
17843           else if (!strcmp ("num_threads", p))
17844             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17845           break;
17846         case 'o':
17847           if (!strcmp ("ordered", p))
17848             result = PRAGMA_OMP_CLAUSE_ORDERED;
17849           break;
17850         case 'r':
17851           if (!strcmp ("reduction", p))
17852             result = PRAGMA_OMP_CLAUSE_REDUCTION;
17853           break;
17854         case 's':
17855           if (!strcmp ("schedule", p))
17856             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17857           else if (!strcmp ("shared", p))
17858             result = PRAGMA_OMP_CLAUSE_SHARED;
17859           break;
17860         }
17861     }
17862
17863   if (result != PRAGMA_OMP_CLAUSE_NONE)
17864     cp_lexer_consume_token (parser->lexer);
17865
17866   return result;
17867 }
17868
17869 /* Validate that a clause of the given type does not already exist.  */
17870
17871 static void
17872 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17873 {
17874   tree c;
17875
17876   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17877     if (OMP_CLAUSE_CODE (c) == code)
17878       {
17879         error ("too many %qs clauses", name);
17880         break;
17881       }
17882 }
17883
17884 /* OpenMP 2.5:
17885    variable-list:
17886      identifier
17887      variable-list , identifier
17888
17889    In addition, we match a closing parenthesis.  An opening parenthesis
17890    will have been consumed by the caller.
17891
17892    If KIND is nonzero, create the appropriate node and install the decl
17893    in OMP_CLAUSE_DECL and add the node to the head of the list.
17894
17895    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17896    return the list created.  */
17897
17898 static tree
17899 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17900                                 tree list)
17901 {
17902   while (1)
17903     {
17904       tree name, decl;
17905
17906       name = cp_parser_id_expression (parser, /*template_p=*/false,
17907                                       /*check_dependency_p=*/true,
17908                                       /*template_p=*/NULL,
17909                                       /*declarator_p=*/false,
17910                                       /*optional_p=*/false);
17911       if (name == error_mark_node)
17912         goto skip_comma;
17913
17914       decl = cp_parser_lookup_name_simple (parser, name);
17915       if (decl == error_mark_node)
17916         cp_parser_name_lookup_error (parser, name, decl, NULL);
17917       else if (kind != 0)
17918         {
17919           tree u = build_omp_clause (kind);
17920           OMP_CLAUSE_DECL (u) = decl;
17921           OMP_CLAUSE_CHAIN (u) = list;
17922           list = u;
17923         }
17924       else
17925         list = tree_cons (decl, NULL_TREE, list);
17926
17927     get_comma:
17928       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17929         break;
17930       cp_lexer_consume_token (parser->lexer);
17931     }
17932
17933   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17934     {
17935       int ending;
17936
17937       /* Try to resync to an unnested comma.  Copied from
17938          cp_parser_parenthesized_expression_list.  */
17939     skip_comma:
17940       ending = cp_parser_skip_to_closing_parenthesis (parser,
17941                                                       /*recovering=*/true,
17942                                                       /*or_comma=*/true,
17943                                                       /*consume_paren=*/true);
17944       if (ending < 0)
17945         goto get_comma;
17946     }
17947
17948   return list;
17949 }
17950
17951 /* Similarly, but expect leading and trailing parenthesis.  This is a very
17952    common case for omp clauses.  */
17953
17954 static tree
17955 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17956 {
17957   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17958     return cp_parser_omp_var_list_no_open (parser, kind, list);
17959   return list;
17960 }
17961
17962 /* OpenMP 2.5:
17963    default ( shared | none ) */
17964
17965 static tree
17966 cp_parser_omp_clause_default (cp_parser *parser, tree list)
17967 {
17968   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17969   tree c;
17970
17971   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17972     return list;
17973   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17974     {
17975       tree id = cp_lexer_peek_token (parser->lexer)->value;
17976       const char *p = IDENTIFIER_POINTER (id);
17977
17978       switch (p[0])
17979         {
17980         case 'n':
17981           if (strcmp ("none", p) != 0)
17982             goto invalid_kind;
17983           kind = OMP_CLAUSE_DEFAULT_NONE;
17984           break;
17985
17986         case 's':
17987           if (strcmp ("shared", p) != 0)
17988             goto invalid_kind;
17989           kind = OMP_CLAUSE_DEFAULT_SHARED;
17990           break;
17991
17992         default:
17993           goto invalid_kind;
17994         }
17995
17996       cp_lexer_consume_token (parser->lexer);
17997     }
17998   else
17999     {
18000     invalid_kind:
18001       cp_parser_error (parser, "expected %<none%> or %<shared%>");
18002     }
18003
18004   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18005     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18006                                            /*or_comma=*/false,
18007                                            /*consume_paren=*/true);
18008
18009   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18010     return list;
18011
18012   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18013   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18014   OMP_CLAUSE_CHAIN (c) = list;
18015   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18016
18017   return c;
18018 }
18019
18020 /* OpenMP 2.5:
18021    if ( expression ) */
18022
18023 static tree
18024 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18025 {
18026   tree t, c;
18027
18028   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18029     return list;
18030
18031   t = cp_parser_condition (parser);
18032
18033   if (t == error_mark_node
18034       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18035     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18036                                            /*or_comma=*/false,
18037                                            /*consume_paren=*/true);
18038
18039   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18040
18041   c = build_omp_clause (OMP_CLAUSE_IF);
18042   OMP_CLAUSE_IF_EXPR (c) = t;
18043   OMP_CLAUSE_CHAIN (c) = list;
18044
18045   return c;
18046 }
18047
18048 /* OpenMP 2.5:
18049    nowait */
18050
18051 static tree
18052 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18053 {
18054   tree c;
18055
18056   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18057
18058   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18059   OMP_CLAUSE_CHAIN (c) = list;
18060   return c;
18061 }
18062
18063 /* OpenMP 2.5:
18064    num_threads ( expression ) */
18065
18066 static tree
18067 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18068 {
18069   tree t, c;
18070
18071   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18072     return list;
18073
18074   t = cp_parser_expression (parser, false);
18075
18076   if (t == error_mark_node
18077       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18078     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18079                                            /*or_comma=*/false,
18080                                            /*consume_paren=*/true);
18081
18082   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18083
18084   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18085   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18086   OMP_CLAUSE_CHAIN (c) = list;
18087
18088   return c;
18089 }
18090
18091 /* OpenMP 2.5:
18092    ordered */
18093
18094 static tree
18095 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18096 {
18097   tree c;
18098
18099   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18100
18101   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18102   OMP_CLAUSE_CHAIN (c) = list;
18103   return c;
18104 }
18105
18106 /* OpenMP 2.5:
18107    reduction ( reduction-operator : variable-list )
18108
18109    reduction-operator:
18110      One of: + * - & ^ | && || */
18111
18112 static tree
18113 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18114 {
18115   enum tree_code code;
18116   tree nlist, c;
18117
18118   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18119     return list;
18120
18121   switch (cp_lexer_peek_token (parser->lexer)->type)
18122     {
18123     case CPP_PLUS:
18124       code = PLUS_EXPR;
18125       break;
18126     case CPP_MULT:
18127       code = MULT_EXPR;
18128       break;
18129     case CPP_MINUS:
18130       code = MINUS_EXPR;
18131       break;
18132     case CPP_AND:
18133       code = BIT_AND_EXPR;
18134       break;
18135     case CPP_XOR:
18136       code = BIT_XOR_EXPR;
18137       break;
18138     case CPP_OR:
18139       code = BIT_IOR_EXPR;
18140       break;
18141     case CPP_AND_AND:
18142       code = TRUTH_ANDIF_EXPR;
18143       break;
18144     case CPP_OR_OR:
18145       code = TRUTH_ORIF_EXPR;
18146       break;
18147     default:
18148       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18149     resync_fail:
18150       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18151                                              /*or_comma=*/false,
18152                                              /*consume_paren=*/true);
18153       return list;
18154     }
18155   cp_lexer_consume_token (parser->lexer);
18156
18157   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18158     goto resync_fail;
18159
18160   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18161   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18162     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18163
18164   return nlist;
18165 }
18166
18167 /* OpenMP 2.5:
18168    schedule ( schedule-kind )
18169    schedule ( schedule-kind , expression )
18170
18171    schedule-kind:
18172      static | dynamic | guided | runtime  */
18173
18174 static tree
18175 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18176 {
18177   tree c, t;
18178
18179   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18180     return list;
18181
18182   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18183
18184   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18185     {
18186       tree id = cp_lexer_peek_token (parser->lexer)->value;
18187       const char *p = IDENTIFIER_POINTER (id);
18188
18189       switch (p[0])
18190         {
18191         case 'd':
18192           if (strcmp ("dynamic", p) != 0)
18193             goto invalid_kind;
18194           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18195           break;
18196
18197         case 'g':
18198           if (strcmp ("guided", p) != 0)
18199             goto invalid_kind;
18200           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18201           break;
18202
18203         case 'r':
18204           if (strcmp ("runtime", p) != 0)
18205             goto invalid_kind;
18206           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18207           break;
18208
18209         default:
18210           goto invalid_kind;
18211         }
18212     }
18213   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18214     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18215   else
18216     goto invalid_kind;
18217   cp_lexer_consume_token (parser->lexer);
18218
18219   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18220     {
18221       cp_lexer_consume_token (parser->lexer);
18222
18223       t = cp_parser_assignment_expression (parser, false);
18224
18225       if (t == error_mark_node)
18226         goto resync_fail;
18227       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18228         error ("schedule %<runtime%> does not take "
18229                "a %<chunk_size%> parameter");
18230       else
18231         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18232
18233       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18234         goto resync_fail;
18235     }
18236   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18237     goto resync_fail;
18238
18239   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18240   OMP_CLAUSE_CHAIN (c) = list;
18241   return c;
18242
18243  invalid_kind:
18244   cp_parser_error (parser, "invalid schedule kind");
18245  resync_fail:
18246   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18247                                          /*or_comma=*/false,
18248                                          /*consume_paren=*/true);
18249   return list;
18250 }
18251
18252 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18253    is a bitmask in MASK.  Return the list of clauses found; the result
18254    of clause default goes in *pdefault.  */
18255
18256 static tree
18257 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18258                            const char *where, cp_token *pragma_tok)
18259 {
18260   tree clauses = NULL;
18261
18262   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18263     {
18264       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18265       const char *c_name;
18266       tree prev = clauses;
18267
18268       switch (c_kind)
18269         {
18270         case PRAGMA_OMP_CLAUSE_COPYIN:
18271           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18272           c_name = "copyin";
18273           break;
18274         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18275           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18276                                             clauses);
18277           c_name = "copyprivate";
18278           break;
18279         case PRAGMA_OMP_CLAUSE_DEFAULT:
18280           clauses = cp_parser_omp_clause_default (parser, clauses);
18281           c_name = "default";
18282           break;
18283         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18284           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18285                                             clauses);
18286           c_name = "firstprivate";
18287           break;
18288         case PRAGMA_OMP_CLAUSE_IF:
18289           clauses = cp_parser_omp_clause_if (parser, clauses);
18290           c_name = "if";
18291           break;
18292         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18293           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18294                                             clauses);
18295           c_name = "lastprivate";
18296           break;
18297         case PRAGMA_OMP_CLAUSE_NOWAIT:
18298           clauses = cp_parser_omp_clause_nowait (parser, clauses);
18299           c_name = "nowait";
18300           break;
18301         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18302           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18303           c_name = "num_threads";
18304           break;
18305         case PRAGMA_OMP_CLAUSE_ORDERED:
18306           clauses = cp_parser_omp_clause_ordered (parser, clauses);
18307           c_name = "ordered";
18308           break;
18309         case PRAGMA_OMP_CLAUSE_PRIVATE:
18310           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18311                                             clauses);
18312           c_name = "private";
18313           break;
18314         case PRAGMA_OMP_CLAUSE_REDUCTION:
18315           clauses = cp_parser_omp_clause_reduction (parser, clauses);
18316           c_name = "reduction";
18317           break;
18318         case PRAGMA_OMP_CLAUSE_SCHEDULE:
18319           clauses = cp_parser_omp_clause_schedule (parser, clauses);
18320           c_name = "schedule";
18321           break;
18322         case PRAGMA_OMP_CLAUSE_SHARED:
18323           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18324                                             clauses);
18325           c_name = "shared";
18326           break;
18327         default:
18328           cp_parser_error (parser, "expected %<#pragma omp%> clause");
18329           goto saw_error;
18330         }
18331
18332       if (((mask >> c_kind) & 1) == 0)
18333         {
18334           /* Remove the invalid clause(s) from the list to avoid
18335              confusing the rest of the compiler.  */
18336           clauses = prev;
18337           error ("%qs is not valid for %qs", c_name, where);
18338         }
18339     }
18340  saw_error:
18341   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18342   return finish_omp_clauses (clauses);
18343 }
18344
18345 /* OpenMP 2.5:
18346    structured-block:
18347      statement
18348
18349    In practice, we're also interested in adding the statement to an
18350    outer node.  So it is convenient if we work around the fact that
18351    cp_parser_statement calls add_stmt.  */
18352
18353 static unsigned
18354 cp_parser_begin_omp_structured_block (cp_parser *parser)
18355 {
18356   unsigned save = parser->in_statement;
18357
18358   /* Only move the values to IN_OMP_BLOCK if they weren't false.
18359      This preserves the "not within loop or switch" style error messages
18360      for nonsense cases like
18361         void foo() {
18362         #pragma omp single
18363           break;
18364         }
18365   */
18366   if (parser->in_statement)
18367     parser->in_statement = IN_OMP_BLOCK;
18368
18369   return save;
18370 }
18371
18372 static void
18373 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18374 {
18375   parser->in_statement = save;
18376 }
18377
18378 static tree
18379 cp_parser_omp_structured_block (cp_parser *parser)
18380 {
18381   tree stmt = begin_omp_structured_block ();
18382   unsigned int save = cp_parser_begin_omp_structured_block (parser);
18383
18384   cp_parser_statement (parser, NULL_TREE, false);
18385
18386   cp_parser_end_omp_structured_block (parser, save);
18387   return finish_omp_structured_block (stmt);
18388 }
18389
18390 /* OpenMP 2.5:
18391    # pragma omp atomic new-line
18392      expression-stmt
18393
18394    expression-stmt:
18395      x binop= expr | x++ | ++x | x-- | --x
18396    binop:
18397      +, *, -, /, &, ^, |, <<, >>
18398
18399   where x is an lvalue expression with scalar type.  */
18400
18401 static void
18402 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18403 {
18404   tree lhs, rhs;
18405   enum tree_code code;
18406
18407   cp_parser_require_pragma_eol (parser, pragma_tok);
18408
18409   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18410                                     /*cast_p=*/false);
18411   switch (TREE_CODE (lhs))
18412     {
18413     case ERROR_MARK:
18414       goto saw_error;
18415
18416     case PREINCREMENT_EXPR:
18417     case POSTINCREMENT_EXPR:
18418       lhs = TREE_OPERAND (lhs, 0);
18419       code = PLUS_EXPR;
18420       rhs = integer_one_node;
18421       break;
18422
18423     case PREDECREMENT_EXPR:
18424     case POSTDECREMENT_EXPR:
18425       lhs = TREE_OPERAND (lhs, 0);
18426       code = MINUS_EXPR;
18427       rhs = integer_one_node;
18428       break;
18429
18430     default:
18431       switch (cp_lexer_peek_token (parser->lexer)->type)
18432         {
18433         case CPP_MULT_EQ:
18434           code = MULT_EXPR;
18435           break;
18436         case CPP_DIV_EQ:
18437           code = TRUNC_DIV_EXPR;
18438           break;
18439         case CPP_PLUS_EQ:
18440           code = PLUS_EXPR;
18441           break;
18442         case CPP_MINUS_EQ:
18443           code = MINUS_EXPR;
18444           break;
18445         case CPP_LSHIFT_EQ:
18446           code = LSHIFT_EXPR;
18447           break;
18448         case CPP_RSHIFT_EQ:
18449           code = RSHIFT_EXPR;
18450           break;
18451         case CPP_AND_EQ:
18452           code = BIT_AND_EXPR;
18453           break;
18454         case CPP_OR_EQ:
18455           code = BIT_IOR_EXPR;
18456           break;
18457         case CPP_XOR_EQ:
18458           code = BIT_XOR_EXPR;
18459           break;
18460         default:
18461           cp_parser_error (parser,
18462                            "invalid operator for %<#pragma omp atomic%>");
18463           goto saw_error;
18464         }
18465       cp_lexer_consume_token (parser->lexer);
18466
18467       rhs = cp_parser_expression (parser, false);
18468       if (rhs == error_mark_node)
18469         goto saw_error;
18470       break;
18471     }
18472   finish_omp_atomic (code, lhs, rhs);
18473   cp_parser_consume_semicolon_at_end_of_statement (parser);
18474   return;
18475
18476  saw_error:
18477   cp_parser_skip_to_end_of_block_or_statement (parser);
18478 }
18479
18480
18481 /* OpenMP 2.5:
18482    # pragma omp barrier new-line  */
18483
18484 static void
18485 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18486 {
18487   cp_parser_require_pragma_eol (parser, pragma_tok);
18488   finish_omp_barrier ();
18489 }
18490
18491 /* OpenMP 2.5:
18492    # pragma omp critical [(name)] new-line
18493      structured-block  */
18494
18495 static tree
18496 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18497 {
18498   tree stmt, name = NULL;
18499
18500   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18501     {
18502       cp_lexer_consume_token (parser->lexer);
18503
18504       name = cp_parser_identifier (parser);
18505
18506       if (name == error_mark_node
18507           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18508         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18509                                                /*or_comma=*/false,
18510                                                /*consume_paren=*/true);
18511       if (name == error_mark_node)
18512         name = NULL;
18513     }
18514   cp_parser_require_pragma_eol (parser, pragma_tok);
18515
18516   stmt = cp_parser_omp_structured_block (parser);
18517   return c_finish_omp_critical (stmt, name);
18518 }
18519
18520 /* OpenMP 2.5:
18521    # pragma omp flush flush-vars[opt] new-line
18522
18523    flush-vars:
18524      ( variable-list ) */
18525
18526 static void
18527 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18528 {
18529   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18530     (void) cp_parser_omp_var_list (parser, 0, NULL);
18531   cp_parser_require_pragma_eol (parser, pragma_tok);
18532
18533   finish_omp_flush ();
18534 }
18535
18536 /* Parse the restricted form of the for statment allowed by OpenMP.  */
18537
18538 static tree
18539 cp_parser_omp_for_loop (cp_parser *parser)
18540 {
18541   tree init, cond, incr, body, decl, pre_body;
18542   location_t loc;
18543
18544   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18545     {
18546       cp_parser_error (parser, "for statement expected");
18547       return NULL;
18548     }
18549   loc = cp_lexer_consume_token (parser->lexer)->location;
18550   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18551     return NULL;
18552
18553   init = decl = NULL;
18554   pre_body = push_stmt_list ();
18555   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18556     {
18557       cp_decl_specifier_seq type_specifiers;
18558
18559       /* First, try to parse as an initialized declaration.  See
18560          cp_parser_condition, from whence the bulk of this is copied.  */
18561
18562       cp_parser_parse_tentatively (parser);
18563       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18564                                     &type_specifiers);
18565       if (!cp_parser_error_occurred (parser))
18566         {
18567           tree asm_specification, attributes;
18568           cp_declarator *declarator;
18569
18570           declarator = cp_parser_declarator (parser,
18571                                              CP_PARSER_DECLARATOR_NAMED,
18572                                              /*ctor_dtor_or_conv_p=*/NULL,
18573                                              /*parenthesized_p=*/NULL,
18574                                              /*member_p=*/false);
18575           attributes = cp_parser_attributes_opt (parser);
18576           asm_specification = cp_parser_asm_specification_opt (parser);
18577
18578           cp_parser_require (parser, CPP_EQ, "`='");
18579           if (cp_parser_parse_definitely (parser))
18580             {
18581               tree pushed_scope;
18582
18583               decl = start_decl (declarator, &type_specifiers,
18584                                  /*initialized_p=*/false, attributes,
18585                                  /*prefix_attributes=*/NULL_TREE,
18586                                  &pushed_scope);
18587
18588               init = cp_parser_assignment_expression (parser, false);
18589
18590               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18591                               asm_specification, LOOKUP_ONLYCONVERTING);
18592
18593               if (pushed_scope)
18594                 pop_scope (pushed_scope);
18595             }
18596         }
18597       else
18598         cp_parser_abort_tentative_parse (parser);
18599
18600       /* If parsing as an initialized declaration failed, try again as
18601          a simple expression.  */
18602       if (decl == NULL)
18603         init = cp_parser_expression (parser, false);
18604     }
18605   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18606   pre_body = pop_stmt_list (pre_body);
18607
18608   cond = NULL;
18609   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18610     cond = cp_parser_condition (parser);
18611   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18612
18613   incr = NULL;
18614   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18615     incr = cp_parser_expression (parser, false);
18616
18617   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18618     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18619                                            /*or_comma=*/false,
18620                                            /*consume_paren=*/true);
18621
18622   /* Note that we saved the original contents of this flag when we entered
18623      the structured block, and so we don't need to re-save it here.  */
18624   parser->in_statement = IN_OMP_FOR;
18625
18626   /* Note that the grammar doesn't call for a structured block here,
18627      though the loop as a whole is a structured block.  */
18628   body = push_stmt_list ();
18629   cp_parser_statement (parser, NULL_TREE, false);
18630   body = pop_stmt_list (body);
18631
18632   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18633 }
18634
18635 /* OpenMP 2.5:
18636    #pragma omp for for-clause[optseq] new-line
18637      for-loop  */
18638
18639 #define OMP_FOR_CLAUSE_MASK                             \
18640         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18641         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18642         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18643         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18644         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
18645         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
18646         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18647
18648 static tree
18649 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18650 {
18651   tree clauses, sb, ret;
18652   unsigned int save;
18653
18654   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18655                                        "#pragma omp for", pragma_tok);
18656
18657   sb = begin_omp_structured_block ();
18658   save = cp_parser_begin_omp_structured_block (parser);
18659
18660   ret = cp_parser_omp_for_loop (parser);
18661   if (ret)
18662     OMP_FOR_CLAUSES (ret) = clauses;
18663
18664   cp_parser_end_omp_structured_block (parser, save);
18665   add_stmt (finish_omp_structured_block (sb));
18666
18667   return ret;
18668 }
18669
18670 /* OpenMP 2.5:
18671    # pragma omp master new-line
18672      structured-block  */
18673
18674 static tree
18675 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18676 {
18677   cp_parser_require_pragma_eol (parser, pragma_tok);
18678   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18679 }
18680
18681 /* OpenMP 2.5:
18682    # pragma omp ordered new-line
18683      structured-block  */
18684
18685 static tree
18686 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18687 {
18688   cp_parser_require_pragma_eol (parser, pragma_tok);
18689   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18690 }
18691
18692 /* OpenMP 2.5:
18693
18694    section-scope:
18695      { section-sequence }
18696
18697    section-sequence:
18698      section-directive[opt] structured-block
18699      section-sequence section-directive structured-block  */
18700
18701 static tree
18702 cp_parser_omp_sections_scope (cp_parser *parser)
18703 {
18704   tree stmt, substmt;
18705   bool error_suppress = false;
18706   cp_token *tok;
18707
18708   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18709     return NULL_TREE;
18710
18711   stmt = push_stmt_list ();
18712
18713   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18714     {
18715       unsigned save;
18716
18717       substmt = begin_omp_structured_block ();
18718       save = cp_parser_begin_omp_structured_block (parser);
18719
18720       while (1)
18721         {
18722           cp_parser_statement (parser, NULL_TREE, false);
18723
18724           tok = cp_lexer_peek_token (parser->lexer);
18725           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18726             break;
18727           if (tok->type == CPP_CLOSE_BRACE)
18728             break;
18729           if (tok->type == CPP_EOF)
18730             break;
18731         }
18732
18733       cp_parser_end_omp_structured_block (parser, save);
18734       substmt = finish_omp_structured_block (substmt);
18735       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18736       add_stmt (substmt);
18737     }
18738
18739   while (1)
18740     {
18741       tok = cp_lexer_peek_token (parser->lexer);
18742       if (tok->type == CPP_CLOSE_BRACE)
18743         break;
18744       if (tok->type == CPP_EOF)
18745         break;
18746
18747       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18748         {
18749           cp_lexer_consume_token (parser->lexer);
18750           cp_parser_require_pragma_eol (parser, tok);
18751           error_suppress = false;
18752         }
18753       else if (!error_suppress)
18754         {
18755           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18756           error_suppress = true;
18757         }
18758
18759       substmt = cp_parser_omp_structured_block (parser);
18760       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18761       add_stmt (substmt);
18762     }
18763   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18764
18765   substmt = pop_stmt_list (stmt);
18766
18767   stmt = make_node (OMP_SECTIONS);
18768   TREE_TYPE (stmt) = void_type_node;
18769   OMP_SECTIONS_BODY (stmt) = substmt;
18770
18771   add_stmt (stmt);
18772   return stmt;
18773 }
18774
18775 /* OpenMP 2.5:
18776    # pragma omp sections sections-clause[optseq] newline
18777      sections-scope  */
18778
18779 #define OMP_SECTIONS_CLAUSE_MASK                        \
18780         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18781         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18782         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18783         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18784         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18785
18786 static tree
18787 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18788 {
18789   tree clauses, ret;
18790
18791   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18792                                        "#pragma omp sections", pragma_tok);
18793
18794   ret = cp_parser_omp_sections_scope (parser);
18795   if (ret)
18796     OMP_SECTIONS_CLAUSES (ret) = clauses;
18797
18798   return ret;
18799 }
18800
18801 /* OpenMP 2.5:
18802    # pragma parallel parallel-clause new-line
18803    # pragma parallel for parallel-for-clause new-line
18804    # pragma parallel sections parallel-sections-clause new-line  */
18805
18806 #define OMP_PARALLEL_CLAUSE_MASK                        \
18807         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
18808         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18809         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18810         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
18811         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
18812         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
18813         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18814         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18815
18816 static tree
18817 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18818 {
18819   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18820   const char *p_name = "#pragma omp parallel";
18821   tree stmt, clauses, par_clause, ws_clause, block;
18822   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18823   unsigned int save;
18824
18825   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18826     {
18827       cp_lexer_consume_token (parser->lexer);
18828       p_kind = PRAGMA_OMP_PARALLEL_FOR;
18829       p_name = "#pragma omp parallel for";
18830       mask |= OMP_FOR_CLAUSE_MASK;
18831       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18832     }
18833   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18834     {
18835       tree id = cp_lexer_peek_token (parser->lexer)->value;
18836       const char *p = IDENTIFIER_POINTER (id);
18837       if (strcmp (p, "sections") == 0)
18838         {
18839           cp_lexer_consume_token (parser->lexer);
18840           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18841           p_name = "#pragma omp parallel sections";
18842           mask |= OMP_SECTIONS_CLAUSE_MASK;
18843           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18844         }
18845     }
18846
18847   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18848   block = begin_omp_parallel ();
18849   save = cp_parser_begin_omp_structured_block (parser);
18850
18851   switch (p_kind)
18852     {
18853     case PRAGMA_OMP_PARALLEL:
18854       cp_parser_already_scoped_statement (parser);
18855       par_clause = clauses;
18856       break;
18857
18858     case PRAGMA_OMP_PARALLEL_FOR:
18859       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18860       stmt = cp_parser_omp_for_loop (parser);
18861       if (stmt)
18862         OMP_FOR_CLAUSES (stmt) = ws_clause;
18863       break;
18864
18865     case PRAGMA_OMP_PARALLEL_SECTIONS:
18866       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18867       stmt = cp_parser_omp_sections_scope (parser);
18868       if (stmt)
18869         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18870       break;
18871
18872     default:
18873       gcc_unreachable ();
18874     }
18875
18876   cp_parser_end_omp_structured_block (parser, save);
18877   stmt = finish_omp_parallel (par_clause, block);
18878   if (p_kind != PRAGMA_OMP_PARALLEL)
18879     OMP_PARALLEL_COMBINED (stmt) = 1;
18880   return stmt;
18881 }
18882
18883 /* OpenMP 2.5:
18884    # pragma omp single single-clause[optseq] new-line
18885      structured-block  */
18886
18887 #define OMP_SINGLE_CLAUSE_MASK                          \
18888         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18889         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18890         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
18891         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18892
18893 static tree
18894 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18895 {
18896   tree stmt = make_node (OMP_SINGLE);
18897   TREE_TYPE (stmt) = void_type_node;
18898
18899   OMP_SINGLE_CLAUSES (stmt)
18900     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18901                                  "#pragma omp single", pragma_tok);
18902   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18903
18904   return add_stmt (stmt);
18905 }
18906
18907 /* OpenMP 2.5:
18908    # pragma omp threadprivate (variable-list) */
18909
18910 static void
18911 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18912 {
18913   tree vars;
18914
18915   vars = cp_parser_omp_var_list (parser, 0, NULL);
18916   cp_parser_require_pragma_eol (parser, pragma_tok);
18917
18918   if (!targetm.have_tls)
18919     sorry ("threadprivate variables not supported in this target");
18920
18921   finish_omp_threadprivate (vars);
18922 }
18923
18924 /* Main entry point to OpenMP statement pragmas.  */
18925
18926 static void
18927 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18928 {
18929   tree stmt;
18930
18931   switch (pragma_tok->pragma_kind)
18932     {
18933     case PRAGMA_OMP_ATOMIC:
18934       cp_parser_omp_atomic (parser, pragma_tok);
18935       return;
18936     case PRAGMA_OMP_CRITICAL:
18937       stmt = cp_parser_omp_critical (parser, pragma_tok);
18938       break;
18939     case PRAGMA_OMP_FOR:
18940       stmt = cp_parser_omp_for (parser, pragma_tok);
18941       break;
18942     case PRAGMA_OMP_MASTER:
18943       stmt = cp_parser_omp_master (parser, pragma_tok);
18944       break;
18945     case PRAGMA_OMP_ORDERED:
18946       stmt = cp_parser_omp_ordered (parser, pragma_tok);
18947       break;
18948     case PRAGMA_OMP_PARALLEL:
18949       stmt = cp_parser_omp_parallel (parser, pragma_tok);
18950       break;
18951     case PRAGMA_OMP_SECTIONS:
18952       stmt = cp_parser_omp_sections (parser, pragma_tok);
18953       break;
18954     case PRAGMA_OMP_SINGLE:
18955       stmt = cp_parser_omp_single (parser, pragma_tok);
18956       break;
18957     default:
18958       gcc_unreachable ();
18959     }
18960
18961   if (stmt)
18962     SET_EXPR_LOCATION (stmt, pragma_tok->location);
18963 }
18964 \f
18965 /* The parser.  */
18966
18967 static GTY (()) cp_parser *the_parser;
18968
18969 \f
18970 /* Special handling for the first token or line in the file.  The first
18971    thing in the file might be #pragma GCC pch_preprocess, which loads a
18972    PCH file, which is a GC collection point.  So we need to handle this
18973    first pragma without benefit of an existing lexer structure.
18974
18975    Always returns one token to the caller in *FIRST_TOKEN.  This is
18976    either the true first token of the file, or the first token after
18977    the initial pragma.  */
18978
18979 static void
18980 cp_parser_initial_pragma (cp_token *first_token)
18981 {
18982   tree name = NULL;
18983
18984   cp_lexer_get_preprocessor_token (NULL, first_token);
18985   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18986     return;
18987
18988   cp_lexer_get_preprocessor_token (NULL, first_token);
18989   if (first_token->type == CPP_STRING)
18990     {
18991       name = first_token->value;
18992
18993       cp_lexer_get_preprocessor_token (NULL, first_token);
18994       if (first_token->type != CPP_PRAGMA_EOL)
18995         error ("junk at end of %<#pragma GCC pch_preprocess%>");
18996     }
18997   else
18998     error ("expected string literal");
18999
19000   /* Skip to the end of the pragma.  */
19001   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19002     cp_lexer_get_preprocessor_token (NULL, first_token);
19003
19004   /* Now actually load the PCH file.  */
19005   if (name)
19006     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19007
19008   /* Read one more token to return to our caller.  We have to do this
19009      after reading the PCH file in, since its pointers have to be
19010      live.  */
19011   cp_lexer_get_preprocessor_token (NULL, first_token);
19012 }
19013
19014 /* Normal parsing of a pragma token.  Here we can (and must) use the
19015    regular lexer.  */
19016
19017 static bool
19018 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19019 {
19020   cp_token *pragma_tok;
19021   unsigned int id;
19022
19023   pragma_tok = cp_lexer_consume_token (parser->lexer);
19024   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19025   parser->lexer->in_pragma = true;
19026
19027   id = pragma_tok->pragma_kind;
19028   switch (id)
19029     {
19030     case PRAGMA_GCC_PCH_PREPROCESS:
19031       error ("%<#pragma GCC pch_preprocess%> must be first");
19032       break;
19033
19034     case PRAGMA_OMP_BARRIER:
19035       switch (context)
19036         {
19037         case pragma_compound:
19038           cp_parser_omp_barrier (parser, pragma_tok);
19039           return false;
19040         case pragma_stmt:
19041           error ("%<#pragma omp barrier%> may only be "
19042                  "used in compound statements");
19043           break;
19044         default:
19045           goto bad_stmt;
19046         }
19047       break;
19048
19049     case PRAGMA_OMP_FLUSH:
19050       switch (context)
19051         {
19052         case pragma_compound:
19053           cp_parser_omp_flush (parser, pragma_tok);
19054           return false;
19055         case pragma_stmt:
19056           error ("%<#pragma omp flush%> may only be "
19057                  "used in compound statements");
19058           break;
19059         default:
19060           goto bad_stmt;
19061         }
19062       break;
19063
19064     case PRAGMA_OMP_THREADPRIVATE:
19065       cp_parser_omp_threadprivate (parser, pragma_tok);
19066       return false;
19067
19068     case PRAGMA_OMP_ATOMIC:
19069     case PRAGMA_OMP_CRITICAL:
19070     case PRAGMA_OMP_FOR:
19071     case PRAGMA_OMP_MASTER:
19072     case PRAGMA_OMP_ORDERED:
19073     case PRAGMA_OMP_PARALLEL:
19074     case PRAGMA_OMP_SECTIONS:
19075     case PRAGMA_OMP_SINGLE:
19076       if (context == pragma_external)
19077         goto bad_stmt;
19078       cp_parser_omp_construct (parser, pragma_tok);
19079       return true;
19080
19081     case PRAGMA_OMP_SECTION:
19082       error ("%<#pragma omp section%> may only be used in "
19083              "%<#pragma omp sections%> construct");
19084       break;
19085
19086     default:
19087       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19088       c_invoke_pragma_handler (id);
19089       break;
19090
19091     bad_stmt:
19092       cp_parser_error (parser, "expected declaration specifiers");
19093       break;
19094     }
19095
19096   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19097   return false;
19098 }
19099
19100 /* The interface the pragma parsers have to the lexer.  */
19101
19102 enum cpp_ttype
19103 pragma_lex (tree *value)
19104 {
19105   cp_token *tok;
19106   enum cpp_ttype ret;
19107
19108   tok = cp_lexer_peek_token (the_parser->lexer);
19109
19110   ret = tok->type;
19111   *value = tok->value;
19112
19113   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19114     ret = CPP_EOF;
19115   else if (ret == CPP_STRING)
19116     *value = cp_parser_string_literal (the_parser, false, false);
19117   else
19118     {
19119       cp_lexer_consume_token (the_parser->lexer);
19120       if (ret == CPP_KEYWORD)
19121         ret = CPP_NAME;
19122     }
19123
19124   return ret;
19125 }
19126
19127 \f
19128 /* External interface.  */
19129
19130 /* Parse one entire translation unit.  */
19131
19132 void
19133 c_parse_file (void)
19134 {
19135   bool error_occurred;
19136   static bool already_called = false;
19137
19138   if (already_called)
19139     {
19140       sorry ("inter-module optimizations not implemented for C++");
19141       return;
19142     }
19143   already_called = true;
19144
19145   the_parser = cp_parser_new ();
19146   push_deferring_access_checks (flag_access_control
19147                                 ? dk_no_deferred : dk_no_check);
19148   error_occurred = cp_parser_translation_unit (the_parser);
19149   the_parser = NULL;
19150 }
19151
19152 /* This variable must be provided by every front end.  */
19153
19154 int yydebug;
19155
19156 #include "gt-cp-parser.h"