OSDN Git Service

af4f312a036d4bfb458dfc27155b7160dff9e594
[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 input file stack index at which this token was found.  */
70   unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
71   /* The value associated with this token, if any.  */
72   tree value;
73   /* The location at which this token was found.  */
74   location_t location;
75 } cp_token;
76
77 /* We use a stack of token pointer for saving token sets.  */
78 typedef struct cp_token *cp_token_position;
79 DEF_VEC_P (cp_token_position);
80 DEF_VEC_ALLOC_P (cp_token_position,heap);
81
82 static const cp_token eof_token =
83 {
84   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, NULL_TREE,
85 #if USE_MAPPED_LOCATION
86   0
87 #else
88   {0, 0}
89 #endif
90 };
91
92 /* The cp_lexer structure represents the C++ lexer.  It is responsible
93    for managing the token stream from the preprocessor and supplying
94    it to the parser.  Tokens are never added to the cp_lexer after
95    it is created.  */
96
97 typedef struct cp_lexer GTY (())
98 {
99   /* The memory allocated for the buffer.  NULL if this lexer does not
100      own the token buffer.  */
101   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
102   /* If the lexer owns the buffer, this is the number of tokens in the
103      buffer.  */
104   size_t buffer_length;
105
106   /* A pointer just past the last available token.  The tokens
107      in this lexer are [buffer, last_token).  */
108   cp_token_position GTY ((skip)) last_token;
109
110   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
111      no more available tokens.  */
112   cp_token_position GTY ((skip)) next_token;
113
114   /* A stack indicating positions at which cp_lexer_save_tokens was
115      called.  The top entry is the most recent position at which we
116      began saving tokens.  If the stack is non-empty, we are saving
117      tokens.  */
118   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
119
120   /* The next lexer in a linked list of lexers.  */
121   struct cp_lexer *next;
122
123   /* True if we should output debugging information.  */
124   bool debugging_p;
125
126   /* True if we're in the context of parsing a pragma, and should not
127      increment past the end-of-line marker.  */
128   bool in_pragma;
129 } cp_lexer;
130
131 /* cp_token_cache is a range of tokens.  There is no need to represent
132    allocate heap memory for it, since tokens are never removed from the
133    lexer's array.  There is also no need for the GC to walk through
134    a cp_token_cache, since everything in here is referenced through
135    a lexer.  */
136
137 typedef struct cp_token_cache GTY(())
138 {
139   /* The beginning of the token range.  */
140   cp_token * GTY((skip)) first;
141
142   /* Points immediately after the last token in the range.  */
143   cp_token * GTY ((skip)) last;
144 } cp_token_cache;
145
146 /* Prototypes.  */
147
148 static cp_lexer *cp_lexer_new_main
149   (void);
150 static cp_lexer *cp_lexer_new_from_tokens
151   (cp_token_cache *tokens);
152 static void cp_lexer_destroy
153   (cp_lexer *);
154 static int cp_lexer_saving_tokens
155   (const cp_lexer *);
156 static cp_token_position cp_lexer_token_position
157   (cp_lexer *, bool);
158 static cp_token *cp_lexer_token_at
159   (cp_lexer *, cp_token_position);
160 static void cp_lexer_get_preprocessor_token
161   (cp_lexer *, cp_token *);
162 static inline cp_token *cp_lexer_peek_token
163   (cp_lexer *);
164 static cp_token *cp_lexer_peek_nth_token
165   (cp_lexer *, size_t);
166 static inline bool cp_lexer_next_token_is
167   (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_not
169   (cp_lexer *, enum cpp_ttype);
170 static bool cp_lexer_next_token_is_keyword
171   (cp_lexer *, enum rid);
172 static cp_token *cp_lexer_consume_token
173   (cp_lexer *);
174 static void cp_lexer_purge_token
175   (cp_lexer *);
176 static void cp_lexer_purge_tokens_after
177   (cp_lexer *, cp_token_position);
178 static void cp_lexer_save_tokens
179   (cp_lexer *);
180 static void cp_lexer_commit_tokens
181   (cp_lexer *);
182 static void cp_lexer_rollback_tokens
183   (cp_lexer *);
184 #ifdef ENABLE_CHECKING
185 static void cp_lexer_print_token
186   (FILE *, cp_token *);
187 static inline bool cp_lexer_debugging_p
188   (cp_lexer *);
189 static void cp_lexer_start_debugging
190   (cp_lexer *) ATTRIBUTE_UNUSED;
191 static void cp_lexer_stop_debugging
192   (cp_lexer *) ATTRIBUTE_UNUSED;
193 #else
194 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
195    about passing NULL to functions that require non-NULL arguments
196    (fputs, fprintf).  It will never be used, so all we need is a value
197    of the right type that's guaranteed not to be NULL.  */
198 #define cp_lexer_debug_stream stdout
199 #define cp_lexer_print_token(str, tok) (void) 0
200 #define cp_lexer_debugging_p(lexer) 0
201 #endif /* ENABLE_CHECKING */
202
203 static cp_token_cache *cp_token_cache_new
204   (cp_token *, cp_token *);
205
206 static void cp_parser_initial_pragma
207   (cp_token *);
208
209 /* Manifest constants.  */
210 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
211 #define CP_SAVED_TOKEN_STACK 5
212
213 /* A token type for keywords, as opposed to ordinary identifiers.  */
214 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
215
216 /* A token type for template-ids.  If a template-id is processed while
217    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
218    the value of the CPP_TEMPLATE_ID is whatever was returned by
219    cp_parser_template_id.  */
220 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
221
222 /* A token type for nested-name-specifiers.  If a
223    nested-name-specifier is processed while parsing tentatively, it is
224    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
225    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
226    cp_parser_nested_name_specifier_opt.  */
227 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
228
229 /* A token type for tokens that are not tokens at all; these are used
230    to represent slots in the array where there used to be a token
231    that has now been deleted.  */
232 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
233
234 /* The number of token types, including C++-specific ones.  */
235 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
236
237 /* Variables.  */
238
239 #ifdef ENABLE_CHECKING
240 /* The stream to which debugging output should be written.  */
241 static FILE *cp_lexer_debug_stream;
242 #endif /* ENABLE_CHECKING */
243
244 /* Create a new main C++ lexer, the lexer that gets tokens from the
245    preprocessor.  */
246
247 static cp_lexer *
248 cp_lexer_new_main (void)
249 {
250   cp_token first_token;
251   cp_lexer *lexer;
252   cp_token *pos;
253   size_t alloc;
254   size_t space;
255   cp_token *buffer;
256
257   /* It's possible that parsing the first pragma will load a PCH file,
258      which is a GC collection point.  So we have to do that before
259      allocating any memory.  */
260   cp_parser_initial_pragma (&first_token);
261
262   /* Tell c_lex_with_flags not to merge string constants.  */
263   c_lex_return_raw_strings = true;
264
265   c_common_no_more_pch ();
266
267   /* Allocate the memory.  */
268   lexer = GGC_CNEW (cp_lexer);
269
270 #ifdef ENABLE_CHECKING
271   /* Initially we are not debugging.  */
272   lexer->debugging_p = false;
273 #endif /* ENABLE_CHECKING */
274   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
275                                    CP_SAVED_TOKEN_STACK);
276
277   /* Create the buffer.  */
278   alloc = CP_LEXER_BUFFER_SIZE;
279   buffer = GGC_NEWVEC (cp_token, alloc);
280
281   /* Put the first token in the buffer.  */
282   space = alloc;
283   pos = buffer;
284   *pos = first_token;
285
286   /* Get the remaining tokens from the preprocessor.  */
287   while (pos->type != CPP_EOF)
288     {
289       pos++;
290       if (!--space)
291         {
292           space = alloc;
293           alloc *= 2;
294           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
295           pos = buffer + space;
296         }
297       cp_lexer_get_preprocessor_token (lexer, pos);
298     }
299   lexer->buffer = buffer;
300   lexer->buffer_length = alloc - space;
301   lexer->last_token = pos;
302   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
303
304   /* Subsequent preprocessor diagnostics should use compiler
305      diagnostic functions to get the compiler source location.  */
306   cpp_get_options (parse_in)->client_diagnostic = true;
307   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
308
309   gcc_assert (lexer->next_token->type != CPP_PURGED);
310   return lexer;
311 }
312
313 /* Create a new lexer whose token stream is primed with the tokens in
314    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
315
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
318 {
319   cp_token *first = cache->first;
320   cp_token *last = cache->last;
321   cp_lexer *lexer = GGC_CNEW (cp_lexer);
322
323   /* We do not own the buffer.  */
324   lexer->buffer = NULL;
325   lexer->buffer_length = 0;
326   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327   lexer->last_token = last;
328
329   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330                                    CP_SAVED_TOKEN_STACK);
331
332 #ifdef ENABLE_CHECKING
333   /* Initially we are not debugging.  */
334   lexer->debugging_p = false;
335 #endif
336
337   gcc_assert (lexer->next_token->type != CPP_PURGED);
338   return lexer;
339 }
340
341 /* Frees all resources associated with LEXER.  */
342
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
345 {
346   if (lexer->buffer)
347     ggc_free (lexer->buffer);
348   VEC_free (cp_token_position, heap, lexer->saved_tokens);
349   ggc_free (lexer);
350 }
351
352 /* Returns nonzero if debugging information should be output.  */
353
354 #ifdef ENABLE_CHECKING
355
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
358 {
359   return lexer->debugging_p;
360 }
361
362 #endif /* ENABLE_CHECKING */
363
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
366 {
367   gcc_assert (!previous_p || lexer->next_token != &eof_token);
368
369   return lexer->next_token - previous_p;
370 }
371
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
374 {
375   return pos;
376 }
377
378 /* nonzero if we are presently saving tokens.  */
379
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
382 {
383   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 }
385
386 /* Store the next token from the preprocessor in *TOKEN.  Return true
387    if we reach EOF.  */
388
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391                                  cp_token *token)
392 {
393   static int is_extern_c = 0;
394
395    /* Get a new token from the preprocessor.  */
396   token->type
397     = c_lex_with_flags (&token->value, &token->location, &token->flags);
398   token->input_file_stack_index = input_file_stack_tick;
399   token->keyword = RID_MAX;
400   token->pragma_kind = PRAGMA_NONE;
401   token->in_system_header = in_system_header;
402
403   /* On some systems, some header files are surrounded by an
404      implicit extern "C" block.  Set a flag in the token if it
405      comes from such a header.  */
406   is_extern_c += pending_lang_change;
407   pending_lang_change = 0;
408   token->implicit_extern_c = is_extern_c > 0;
409
410   /* Check to see if this token is a keyword.  */
411   if (token->type == CPP_NAME)
412     {
413       if (C_IS_RESERVED_WORD (token->value))
414         {
415           /* Mark this token as a keyword.  */
416           token->type = CPP_KEYWORD;
417           /* Record which keyword.  */
418           token->keyword = C_RID_CODE (token->value);
419           /* Update the value.  Some keywords are mapped to particular
420              entities, rather than simply having the value of the
421              corresponding IDENTIFIER_NODE.  For example, `__const' is
422              mapped to `const'.  */
423           token->value = ridpointers[token->keyword];
424         }
425       else
426         {
427           token->ambiguous_p = false;
428           token->keyword = RID_MAX;
429         }
430     }
431   /* Handle Objective-C++ keywords.  */
432   else if (token->type == CPP_AT_NAME)
433     {
434       token->type = CPP_KEYWORD;
435       switch (C_RID_CODE (token->value))
436         {
437         /* Map 'class' to '@class', 'private' to '@private', etc.  */
438         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
439         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
440         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
441         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
442         case RID_THROW: token->keyword = RID_AT_THROW; break;
443         case RID_TRY: token->keyword = RID_AT_TRY; break;
444         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
445         default: token->keyword = C_RID_CODE (token->value);
446         }
447     }
448   else if (token->type == CPP_PRAGMA)
449     {
450       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
451       token->pragma_kind = TREE_INT_CST_LOW (token->value);
452       token->value = NULL;
453     }
454 }
455
456 /* Update the globals input_location and in_system_header and the
457    input file stack from TOKEN.  */
458 static inline void
459 cp_lexer_set_source_position_from_token (cp_token *token)
460 {
461   if (token->type != CPP_EOF)
462     {
463       input_location = token->location;
464       in_system_header = token->in_system_header;
465       restore_input_file_stack (token->input_file_stack_index);
466     }
467 }
468
469 /* Return a pointer to the next token in the token stream, but do not
470    consume it.  */
471
472 static inline cp_token *
473 cp_lexer_peek_token (cp_lexer *lexer)
474 {
475   if (cp_lexer_debugging_p (lexer))
476     {
477       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
478       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
479       putc ('\n', cp_lexer_debug_stream);
480     }
481   return lexer->next_token;
482 }
483
484 /* Return true if the next token has the indicated TYPE.  */
485
486 static inline bool
487 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
488 {
489   return cp_lexer_peek_token (lexer)->type == type;
490 }
491
492 /* Return true if the next token does not have the indicated TYPE.  */
493
494 static inline bool
495 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
496 {
497   return !cp_lexer_next_token_is (lexer, type);
498 }
499
500 /* Return true if the next token is the indicated KEYWORD.  */
501
502 static inline bool
503 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
504 {
505   return cp_lexer_peek_token (lexer)->keyword == keyword;
506 }
507
508 /* Return a pointer to the Nth token in the token stream.  If N is 1,
509    then this is precisely equivalent to cp_lexer_peek_token (except
510    that it is not inline).  One would like to disallow that case, but
511    there is one case (cp_parser_nth_token_starts_template_id) where
512    the caller passes a variable for N and it might be 1.  */
513
514 static cp_token *
515 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
516 {
517   cp_token *token;
518
519   /* N is 1-based, not zero-based.  */
520   gcc_assert (n > 0);
521
522   if (cp_lexer_debugging_p (lexer))
523     fprintf (cp_lexer_debug_stream,
524              "cp_lexer: peeking ahead %ld at token: ", (long)n);
525
526   --n;
527   token = lexer->next_token;
528   gcc_assert (!n || token != &eof_token);
529   while (n != 0)
530     {
531       ++token;
532       if (token == lexer->last_token)
533         {
534           token = (cp_token *)&eof_token;
535           break;
536         }
537
538       if (token->type != CPP_PURGED)
539         --n;
540     }
541
542   if (cp_lexer_debugging_p (lexer))
543     {
544       cp_lexer_print_token (cp_lexer_debug_stream, token);
545       putc ('\n', cp_lexer_debug_stream);
546     }
547
548   return token;
549 }
550
551 /* Return the next token, and advance the lexer's next_token pointer
552    to point to the next non-purged token.  */
553
554 static cp_token *
555 cp_lexer_consume_token (cp_lexer* lexer)
556 {
557   cp_token *token = lexer->next_token;
558
559   gcc_assert (token != &eof_token);
560   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
561
562   do
563     {
564       lexer->next_token++;
565       if (lexer->next_token == lexer->last_token)
566         {
567           lexer->next_token = (cp_token *)&eof_token;
568           break;
569         }
570
571     }
572   while (lexer->next_token->type == CPP_PURGED);
573
574   cp_lexer_set_source_position_from_token (token);
575
576   /* Provide debugging output.  */
577   if (cp_lexer_debugging_p (lexer))
578     {
579       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
580       cp_lexer_print_token (cp_lexer_debug_stream, token);
581       putc ('\n', cp_lexer_debug_stream);
582     }
583
584   return token;
585 }
586
587 /* Permanently remove the next token from the token stream, and
588    advance the next_token pointer to refer to the next non-purged
589    token.  */
590
591 static void
592 cp_lexer_purge_token (cp_lexer *lexer)
593 {
594   cp_token *tok = lexer->next_token;
595
596   gcc_assert (tok != &eof_token);
597   tok->type = CPP_PURGED;
598   tok->location = UNKNOWN_LOCATION;
599   tok->value = NULL_TREE;
600   tok->keyword = RID_MAX;
601
602   do
603     {
604       tok++;
605       if (tok == lexer->last_token)
606         {
607           tok = (cp_token *)&eof_token;
608           break;
609         }
610     }
611   while (tok->type == CPP_PURGED);
612   lexer->next_token = tok;
613 }
614
615 /* Permanently remove all tokens after TOK, up to, but not
616    including, the token that will be returned next by
617    cp_lexer_peek_token.  */
618
619 static void
620 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
621 {
622   cp_token *peek = lexer->next_token;
623
624   if (peek == &eof_token)
625     peek = lexer->last_token;
626
627   gcc_assert (tok < peek);
628
629   for ( tok += 1; tok != peek; tok += 1)
630     {
631       tok->type = CPP_PURGED;
632       tok->location = UNKNOWN_LOCATION;
633       tok->value = NULL_TREE;
634       tok->keyword = RID_MAX;
635     }
636 }
637
638 /* Begin saving tokens.  All tokens consumed after this point will be
639    preserved.  */
640
641 static void
642 cp_lexer_save_tokens (cp_lexer* lexer)
643 {
644   /* Provide debugging output.  */
645   if (cp_lexer_debugging_p (lexer))
646     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
647
648   VEC_safe_push (cp_token_position, heap,
649                  lexer->saved_tokens, lexer->next_token);
650 }
651
652 /* Commit to the portion of the token stream most recently saved.  */
653
654 static void
655 cp_lexer_commit_tokens (cp_lexer* lexer)
656 {
657   /* Provide debugging output.  */
658   if (cp_lexer_debugging_p (lexer))
659     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
660
661   VEC_pop (cp_token_position, lexer->saved_tokens);
662 }
663
664 /* Return all tokens saved since the last call to cp_lexer_save_tokens
665    to the token stream.  Stop saving tokens.  */
666
667 static void
668 cp_lexer_rollback_tokens (cp_lexer* lexer)
669 {
670   /* Provide debugging output.  */
671   if (cp_lexer_debugging_p (lexer))
672     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
673
674   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
675 }
676
677 /* Print a representation of the TOKEN on the STREAM.  */
678
679 #ifdef ENABLE_CHECKING
680
681 static void
682 cp_lexer_print_token (FILE * stream, cp_token *token)
683 {
684   /* We don't use cpp_type2name here because the parser defines
685      a few tokens of its own.  */
686   static const char *const token_names[] = {
687     /* cpplib-defined token types */
688 #define OP(e, s) #e,
689 #define TK(e, s) #e,
690     TTYPE_TABLE
691 #undef OP
692 #undef TK
693     /* C++ parser token types - see "Manifest constants", above.  */
694     "KEYWORD",
695     "TEMPLATE_ID",
696     "NESTED_NAME_SPECIFIER",
697     "PURGED"
698   };
699
700   /* If we have a name for the token, print it out.  Otherwise, we
701      simply give the numeric code.  */
702   gcc_assert (token->type < ARRAY_SIZE(token_names));
703   fputs (token_names[token->type], stream);
704
705   /* For some tokens, print the associated data.  */
706   switch (token->type)
707     {
708     case CPP_KEYWORD:
709       /* Some keywords have a value that is not an IDENTIFIER_NODE.
710          For example, `struct' is mapped to an INTEGER_CST.  */
711       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
712         break;
713       /* else fall through */
714     case CPP_NAME:
715       fputs (IDENTIFIER_POINTER (token->value), stream);
716       break;
717
718     case CPP_STRING:
719     case CPP_WSTRING:
720       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
721       break;
722
723     default:
724       break;
725     }
726 }
727
728 /* Start emitting debugging information.  */
729
730 static void
731 cp_lexer_start_debugging (cp_lexer* lexer)
732 {
733   lexer->debugging_p = true;
734 }
735
736 /* Stop emitting debugging information.  */
737
738 static void
739 cp_lexer_stop_debugging (cp_lexer* lexer)
740 {
741   lexer->debugging_p = false;
742 }
743
744 #endif /* ENABLE_CHECKING */
745
746 /* Create a new cp_token_cache, representing a range of tokens.  */
747
748 static cp_token_cache *
749 cp_token_cache_new (cp_token *first, cp_token *last)
750 {
751   cp_token_cache *cache = GGC_NEW (cp_token_cache);
752   cache->first = first;
753   cache->last = last;
754   return cache;
755 }
756
757 \f
758 /* Decl-specifiers.  */
759
760 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
761
762 static void
763 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
764 {
765   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
766 }
767
768 /* Declarators.  */
769
770 /* Nothing other than the parser should be creating declarators;
771    declarators are a semi-syntactic representation of C++ entities.
772    Other parts of the front end that need to create entities (like
773    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
774
775 static cp_declarator *make_call_declarator
776   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
777 static cp_declarator *make_array_declarator
778   (cp_declarator *, tree);
779 static cp_declarator *make_pointer_declarator
780   (cp_cv_quals, cp_declarator *);
781 static cp_declarator *make_reference_declarator
782   (cp_cv_quals, cp_declarator *);
783 static cp_parameter_declarator *make_parameter_declarator
784   (cp_decl_specifier_seq *, cp_declarator *, tree);
785 static cp_declarator *make_ptrmem_declarator
786   (cp_cv_quals, tree, cp_declarator *);
787
788 /* An erroneous declarator.  */
789 static cp_declarator *cp_error_declarator;
790
791 /* The obstack on which declarators and related data structures are
792    allocated.  */
793 static struct obstack declarator_obstack;
794
795 /* Alloc BYTES from the declarator memory pool.  */
796
797 static inline void *
798 alloc_declarator (size_t bytes)
799 {
800   return obstack_alloc (&declarator_obstack, bytes);
801 }
802
803 /* Allocate a declarator of the indicated KIND.  Clear fields that are
804    common to all declarators.  */
805
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
808 {
809   cp_declarator *declarator;
810
811   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812   declarator->kind = kind;
813   declarator->attributes = NULL_TREE;
814   declarator->declarator = NULL;
815
816   return declarator;
817 }
818
819 /* Make a declarator for a generalized identifier.  If
820    QUALIFYING_SCOPE is non-NULL, the identifier is
821    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
822    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
823    is, if any.   */
824
825 static cp_declarator *
826 make_id_declarator (tree qualifying_scope, tree unqualified_name,
827                     special_function_kind sfk)
828 {
829   cp_declarator *declarator;
830
831   /* It is valid to write:
832
833        class C { void f(); };
834        typedef C D;
835        void D::f();
836
837      The standard is not clear about whether `typedef const C D' is
838      legal; as of 2002-09-15 the committee is considering that
839      question.  EDG 3.0 allows that syntax.  Therefore, we do as
840      well.  */
841   if (qualifying_scope && TYPE_P (qualifying_scope))
842     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
843
844   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
845               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
846               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
847
848   declarator = make_declarator (cdk_id);
849   declarator->u.id.qualifying_scope = qualifying_scope;
850   declarator->u.id.unqualified_name = unqualified_name;
851   declarator->u.id.sfk = sfk;
852
853   return declarator;
854 }
855
856 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
857    of modifiers such as const or volatile to apply to the pointer
858    type, represented as identifiers.  */
859
860 cp_declarator *
861 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
862 {
863   cp_declarator *declarator;
864
865   declarator = make_declarator (cdk_pointer);
866   declarator->declarator = target;
867   declarator->u.pointer.qualifiers = cv_qualifiers;
868   declarator->u.pointer.class_type = NULL_TREE;
869
870   return declarator;
871 }
872
873 /* Like make_pointer_declarator -- but for references.  */
874
875 cp_declarator *
876 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
877 {
878   cp_declarator *declarator;
879
880   declarator = make_declarator (cdk_reference);
881   declarator->declarator = target;
882   declarator->u.pointer.qualifiers = cv_qualifiers;
883   declarator->u.pointer.class_type = NULL_TREE;
884
885   return declarator;
886 }
887
888 /* Like make_pointer_declarator -- but for a pointer to a non-static
889    member of CLASS_TYPE.  */
890
891 cp_declarator *
892 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
893                         cp_declarator *pointee)
894 {
895   cp_declarator *declarator;
896
897   declarator = make_declarator (cdk_ptrmem);
898   declarator->declarator = pointee;
899   declarator->u.pointer.qualifiers = cv_qualifiers;
900   declarator->u.pointer.class_type = class_type;
901
902   return declarator;
903 }
904
905 /* Make a declarator for the function given by TARGET, with the
906    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
907    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
908    indicates what exceptions can be thrown.  */
909
910 cp_declarator *
911 make_call_declarator (cp_declarator *target,
912                       cp_parameter_declarator *parms,
913                       cp_cv_quals cv_qualifiers,
914                       tree exception_specification)
915 {
916   cp_declarator *declarator;
917
918   declarator = make_declarator (cdk_function);
919   declarator->declarator = target;
920   declarator->u.function.parameters = parms;
921   declarator->u.function.qualifiers = cv_qualifiers;
922   declarator->u.function.exception_specification = exception_specification;
923
924   return declarator;
925 }
926
927 /* Make a declarator for an array of BOUNDS elements, each of which is
928    defined by ELEMENT.  */
929
930 cp_declarator *
931 make_array_declarator (cp_declarator *element, tree bounds)
932 {
933   cp_declarator *declarator;
934
935   declarator = make_declarator (cdk_array);
936   declarator->declarator = element;
937   declarator->u.array.bounds = bounds;
938
939   return declarator;
940 }
941
942 cp_parameter_declarator *no_parameters;
943
944 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
945    DECLARATOR and DEFAULT_ARGUMENT.  */
946
947 cp_parameter_declarator *
948 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
949                            cp_declarator *declarator,
950                            tree default_argument)
951 {
952   cp_parameter_declarator *parameter;
953
954   parameter = ((cp_parameter_declarator *)
955                alloc_declarator (sizeof (cp_parameter_declarator)));
956   parameter->next = NULL;
957   if (decl_specifiers)
958     parameter->decl_specifiers = *decl_specifiers;
959   else
960     clear_decl_specs (&parameter->decl_specifiers);
961   parameter->declarator = declarator;
962   parameter->default_argument = default_argument;
963   parameter->ellipsis_p = false;
964
965   return parameter;
966 }
967
968 /* The parser.  */
969
970 /* Overview
971    --------
972
973    A cp_parser parses the token stream as specified by the C++
974    grammar.  Its job is purely parsing, not semantic analysis.  For
975    example, the parser breaks the token stream into declarators,
976    expressions, statements, and other similar syntactic constructs.
977    It does not check that the types of the expressions on either side
978    of an assignment-statement are compatible, or that a function is
979    not declared with a parameter of type `void'.
980
981    The parser invokes routines elsewhere in the compiler to perform
982    semantic analysis and to build up the abstract syntax tree for the
983    code processed.
984
985    The parser (and the template instantiation code, which is, in a
986    way, a close relative of parsing) are the only parts of the
987    compiler that should be calling push_scope and pop_scope, or
988    related functions.  The parser (and template instantiation code)
989    keeps track of what scope is presently active; everything else
990    should simply honor that.  (The code that generates static
991    initializers may also need to set the scope, in order to check
992    access control correctly when emitting the initializers.)
993
994    Methodology
995    -----------
996
997    The parser is of the standard recursive-descent variety.  Upcoming
998    tokens in the token stream are examined in order to determine which
999    production to use when parsing a non-terminal.  Some C++ constructs
1000    require arbitrary look ahead to disambiguate.  For example, it is
1001    impossible, in the general case, to tell whether a statement is an
1002    expression or declaration without scanning the entire statement.
1003    Therefore, the parser is capable of "parsing tentatively."  When the
1004    parser is not sure what construct comes next, it enters this mode.
1005    Then, while we attempt to parse the construct, the parser queues up
1006    error messages, rather than issuing them immediately, and saves the
1007    tokens it consumes.  If the construct is parsed successfully, the
1008    parser "commits", i.e., it issues any queued error messages and
1009    the tokens that were being preserved are permanently discarded.
1010    If, however, the construct is not parsed successfully, the parser
1011    rolls back its state completely so that it can resume parsing using
1012    a different alternative.
1013
1014    Future Improvements
1015    -------------------
1016
1017    The performance of the parser could probably be improved substantially.
1018    We could often eliminate the need to parse tentatively by looking ahead
1019    a little bit.  In some places, this approach might not entirely eliminate
1020    the need to parse tentatively, but it might still speed up the average
1021    case.  */
1022
1023 /* Flags that are passed to some parsing functions.  These values can
1024    be bitwise-ored together.  */
1025
1026 typedef enum cp_parser_flags
1027 {
1028   /* No flags.  */
1029   CP_PARSER_FLAGS_NONE = 0x0,
1030   /* The construct is optional.  If it is not present, then no error
1031      should be issued.  */
1032   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1033   /* When parsing a type-specifier, do not allow user-defined types.  */
1034   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1035 } cp_parser_flags;
1036
1037 /* The different kinds of declarators we want to parse.  */
1038
1039 typedef enum cp_parser_declarator_kind
1040 {
1041   /* We want an abstract declarator.  */
1042   CP_PARSER_DECLARATOR_ABSTRACT,
1043   /* We want a named declarator.  */
1044   CP_PARSER_DECLARATOR_NAMED,
1045   /* We don't mind, but the name must be an unqualified-id.  */
1046   CP_PARSER_DECLARATOR_EITHER
1047 } cp_parser_declarator_kind;
1048
1049 /* The precedence values used to parse binary expressions.  The minimum value
1050    of PREC must be 1, because zero is reserved to quickly discriminate
1051    binary operators from other tokens.  */
1052
1053 enum cp_parser_prec
1054 {
1055   PREC_NOT_OPERATOR,
1056   PREC_LOGICAL_OR_EXPRESSION,
1057   PREC_LOGICAL_AND_EXPRESSION,
1058   PREC_INCLUSIVE_OR_EXPRESSION,
1059   PREC_EXCLUSIVE_OR_EXPRESSION,
1060   PREC_AND_EXPRESSION,
1061   PREC_EQUALITY_EXPRESSION,
1062   PREC_RELATIONAL_EXPRESSION,
1063   PREC_SHIFT_EXPRESSION,
1064   PREC_ADDITIVE_EXPRESSION,
1065   PREC_MULTIPLICATIVE_EXPRESSION,
1066   PREC_PM_EXPRESSION,
1067   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1068 };
1069
1070 /* A mapping from a token type to a corresponding tree node type, with a
1071    precedence value.  */
1072
1073 typedef struct cp_parser_binary_operations_map_node
1074 {
1075   /* The token type.  */
1076   enum cpp_ttype token_type;
1077   /* The corresponding tree code.  */
1078   enum tree_code tree_type;
1079   /* The precedence of this operator.  */
1080   enum cp_parser_prec prec;
1081 } cp_parser_binary_operations_map_node;
1082
1083 /* The status of a tentative parse.  */
1084
1085 typedef enum cp_parser_status_kind
1086 {
1087   /* No errors have occurred.  */
1088   CP_PARSER_STATUS_KIND_NO_ERROR,
1089   /* An error has occurred.  */
1090   CP_PARSER_STATUS_KIND_ERROR,
1091   /* We are committed to this tentative parse, whether or not an error
1092      has occurred.  */
1093   CP_PARSER_STATUS_KIND_COMMITTED
1094 } cp_parser_status_kind;
1095
1096 typedef struct cp_parser_expression_stack_entry
1097 {
1098   tree lhs;
1099   enum tree_code tree_type;
1100   int prec;
1101 } cp_parser_expression_stack_entry;
1102
1103 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1104    entries because precedence levels on the stack are monotonically
1105    increasing.  */
1106 typedef struct cp_parser_expression_stack_entry
1107   cp_parser_expression_stack[NUM_PREC_VALUES];
1108
1109 /* Context that is saved and restored when parsing tentatively.  */
1110 typedef struct cp_parser_context GTY (())
1111 {
1112   /* If this is a tentative parsing context, the status of the
1113      tentative parse.  */
1114   enum cp_parser_status_kind status;
1115   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1116      that are looked up in this context must be looked up both in the
1117      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1118      the context of the containing expression.  */
1119   tree object_type;
1120
1121   /* The next parsing context in the stack.  */
1122   struct cp_parser_context *next;
1123 } cp_parser_context;
1124
1125 /* Prototypes.  */
1126
1127 /* Constructors and destructors.  */
1128
1129 static cp_parser_context *cp_parser_context_new
1130   (cp_parser_context *);
1131
1132 /* Class variables.  */
1133
1134 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1135
1136 /* The operator-precedence table used by cp_parser_binary_expression.
1137    Transformed into an associative array (binops_by_token) by
1138    cp_parser_new.  */
1139
1140 static const cp_parser_binary_operations_map_node binops[] = {
1141   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1142   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1143
1144   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1145   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1146   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1147
1148   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1149   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1150
1151   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1152   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1153
1154   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1155   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1156   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1157   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1158
1159   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1160   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1161
1162   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1163
1164   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1165
1166   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1167
1168   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1169
1170   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1171 };
1172
1173 /* The same as binops, but initialized by cp_parser_new so that
1174    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1175    for speed.  */
1176 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1177
1178 /* Constructors and destructors.  */
1179
1180 /* Construct a new context.  The context below this one on the stack
1181    is given by NEXT.  */
1182
1183 static cp_parser_context *
1184 cp_parser_context_new (cp_parser_context* next)
1185 {
1186   cp_parser_context *context;
1187
1188   /* Allocate the storage.  */
1189   if (cp_parser_context_free_list != NULL)
1190     {
1191       /* Pull the first entry from the free list.  */
1192       context = cp_parser_context_free_list;
1193       cp_parser_context_free_list = context->next;
1194       memset (context, 0, sizeof (*context));
1195     }
1196   else
1197     context = GGC_CNEW (cp_parser_context);
1198
1199   /* No errors have occurred yet in this context.  */
1200   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1201   /* If this is not the bottomost context, copy information that we
1202      need from the previous context.  */
1203   if (next)
1204     {
1205       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1206          expression, then we are parsing one in this context, too.  */
1207       context->object_type = next->object_type;
1208       /* Thread the stack.  */
1209       context->next = next;
1210     }
1211
1212   return context;
1213 }
1214
1215 /* The cp_parser structure represents the C++ parser.  */
1216
1217 typedef struct cp_parser GTY(())
1218 {
1219   /* The lexer from which we are obtaining tokens.  */
1220   cp_lexer *lexer;
1221
1222   /* The scope in which names should be looked up.  If NULL_TREE, then
1223      we look up names in the scope that is currently open in the
1224      source program.  If non-NULL, this is either a TYPE or
1225      NAMESPACE_DECL for the scope in which we should look.  It can
1226      also be ERROR_MARK, when we've parsed a bogus scope.
1227
1228      This value is not cleared automatically after a name is looked
1229      up, so we must be careful to clear it before starting a new look
1230      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1231      will look up `Z' in the scope of `X', rather than the current
1232      scope.)  Unfortunately, it is difficult to tell when name lookup
1233      is complete, because we sometimes peek at a token, look it up,
1234      and then decide not to consume it.   */
1235   tree scope;
1236
1237   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1238      last lookup took place.  OBJECT_SCOPE is used if an expression
1239      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1240      respectively.  QUALIFYING_SCOPE is used for an expression of the
1241      form "X::Y"; it refers to X.  */
1242   tree object_scope;
1243   tree qualifying_scope;
1244
1245   /* A stack of parsing contexts.  All but the bottom entry on the
1246      stack will be tentative contexts.
1247
1248      We parse tentatively in order to determine which construct is in
1249      use in some situations.  For example, in order to determine
1250      whether a statement is an expression-statement or a
1251      declaration-statement we parse it tentatively as a
1252      declaration-statement.  If that fails, we then reparse the same
1253      token stream as an expression-statement.  */
1254   cp_parser_context *context;
1255
1256   /* True if we are parsing GNU C++.  If this flag is not set, then
1257      GNU extensions are not recognized.  */
1258   bool allow_gnu_extensions_p;
1259
1260   /* TRUE if the `>' token should be interpreted as the greater-than
1261      operator.  FALSE if it is the end of a template-id or
1262      template-parameter-list.  */
1263   bool greater_than_is_operator_p;
1264
1265   /* TRUE if default arguments are allowed within a parameter list
1266      that starts at this point. FALSE if only a gnu extension makes
1267      them permissible.  */
1268   bool default_arg_ok_p;
1269
1270   /* TRUE if we are parsing an integral constant-expression.  See
1271      [expr.const] for a precise definition.  */
1272   bool integral_constant_expression_p;
1273
1274   /* TRUE if we are parsing an integral constant-expression -- but a
1275      non-constant expression should be permitted as well.  This flag
1276      is used when parsing an array bound so that GNU variable-length
1277      arrays are tolerated.  */
1278   bool allow_non_integral_constant_expression_p;
1279
1280   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1281      been seen that makes the expression non-constant.  */
1282   bool non_integral_constant_expression_p;
1283
1284   /* TRUE if local variable names and `this' are forbidden in the
1285      current context.  */
1286   bool local_variables_forbidden_p;
1287
1288   /* TRUE if the declaration we are parsing is part of a
1289      linkage-specification of the form `extern string-literal
1290      declaration'.  */
1291   bool in_unbraced_linkage_specification_p;
1292
1293   /* TRUE if we are presently parsing a declarator, after the
1294      direct-declarator.  */
1295   bool in_declarator_p;
1296
1297   /* TRUE if we are presently parsing a template-argument-list.  */
1298   bool in_template_argument_list_p;
1299
1300   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1301      to IN_OMP_BLOCK if parsing OpenMP structured block and
1302      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1303      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1304      iteration-statement, OpenMP block or loop within that switch.  */
1305 #define IN_SWITCH_STMT          1
1306 #define IN_ITERATION_STMT       2
1307 #define IN_OMP_BLOCK            4
1308 #define IN_OMP_FOR              8
1309   unsigned char in_statement;
1310
1311   /* TRUE if we are presently parsing the body of a switch statement.
1312      Note that this doesn't quite overlap with in_statement above.
1313      The difference relates to giving the right sets of error messages:
1314      "case not in switch" vs "break statement used with OpenMP...".  */
1315   bool in_switch_statement_p;
1316
1317   /* TRUE if we are parsing a type-id in an expression context.  In
1318      such a situation, both "type (expr)" and "type (type)" are valid
1319      alternatives.  */
1320   bool in_type_id_in_expr_p;
1321
1322   /* TRUE if we are currently in a header file where declarations are
1323      implicitly extern "C".  */
1324   bool implicit_extern_c;
1325
1326   /* TRUE if strings in expressions should be translated to the execution
1327      character set.  */
1328   bool translate_strings_p;
1329
1330   /* If non-NULL, then we are parsing a construct where new type
1331      definitions are not permitted.  The string stored here will be
1332      issued as an error message if a type is defined.  */
1333   const char *type_definition_forbidden_message;
1334
1335   /* A list of lists. The outer list is a stack, used for member
1336      functions of local classes. At each level there are two sub-list,
1337      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1338      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1339      TREE_VALUE's. The functions are chained in reverse declaration
1340      order.
1341
1342      The TREE_PURPOSE sublist contains those functions with default
1343      arguments that need post processing, and the TREE_VALUE sublist
1344      contains those functions with definitions that need post
1345      processing.
1346
1347      These lists can only be processed once the outermost class being
1348      defined is complete.  */
1349   tree unparsed_functions_queues;
1350
1351   /* The number of classes whose definitions are currently in
1352      progress.  */
1353   unsigned num_classes_being_defined;
1354
1355   /* The number of template parameter lists that apply directly to the
1356      current declaration.  */
1357   unsigned num_template_parameter_lists;
1358 } cp_parser;
1359
1360 /* Prototypes.  */
1361
1362 /* Constructors and destructors.  */
1363
1364 static cp_parser *cp_parser_new
1365   (void);
1366
1367 /* Routines to parse various constructs.
1368
1369    Those that return `tree' will return the error_mark_node (rather
1370    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1371    Sometimes, they will return an ordinary node if error-recovery was
1372    attempted, even though a parse error occurred.  So, to check
1373    whether or not a parse error occurred, you should always use
1374    cp_parser_error_occurred.  If the construct is optional (indicated
1375    either by an `_opt' in the name of the function that does the
1376    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1377    the construct is not present.  */
1378
1379 /* Lexical conventions [gram.lex]  */
1380
1381 static tree cp_parser_identifier
1382   (cp_parser *);
1383 static tree cp_parser_string_literal
1384   (cp_parser *, bool, bool);
1385
1386 /* Basic concepts [gram.basic]  */
1387
1388 static bool cp_parser_translation_unit
1389   (cp_parser *);
1390
1391 /* Expressions [gram.expr]  */
1392
1393 static tree cp_parser_primary_expression
1394   (cp_parser *, bool, bool, bool, cp_id_kind *);
1395 static tree cp_parser_id_expression
1396   (cp_parser *, bool, bool, bool *, bool, bool);
1397 static tree cp_parser_unqualified_id
1398   (cp_parser *, bool, bool, bool, bool);
1399 static tree cp_parser_nested_name_specifier_opt
1400   (cp_parser *, bool, bool, bool, bool);
1401 static tree cp_parser_nested_name_specifier
1402   (cp_parser *, bool, bool, bool, bool);
1403 static tree cp_parser_class_or_namespace_name
1404   (cp_parser *, bool, bool, bool, bool, bool);
1405 static tree cp_parser_postfix_expression
1406   (cp_parser *, bool, bool);
1407 static tree cp_parser_postfix_open_square_expression
1408   (cp_parser *, tree, bool);
1409 static tree cp_parser_postfix_dot_deref_expression
1410   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1411 static tree cp_parser_parenthesized_expression_list
1412   (cp_parser *, bool, bool, bool *);
1413 static void cp_parser_pseudo_destructor_name
1414   (cp_parser *, tree *, tree *);
1415 static tree cp_parser_unary_expression
1416   (cp_parser *, bool, bool);
1417 static enum tree_code cp_parser_unary_operator
1418   (cp_token *);
1419 static tree cp_parser_new_expression
1420   (cp_parser *);
1421 static tree cp_parser_new_placement
1422   (cp_parser *);
1423 static tree cp_parser_new_type_id
1424   (cp_parser *, tree *);
1425 static cp_declarator *cp_parser_new_declarator_opt
1426   (cp_parser *);
1427 static cp_declarator *cp_parser_direct_new_declarator
1428   (cp_parser *);
1429 static tree cp_parser_new_initializer
1430   (cp_parser *);
1431 static tree cp_parser_delete_expression
1432   (cp_parser *);
1433 static tree cp_parser_cast_expression
1434   (cp_parser *, bool, bool);
1435 static tree cp_parser_binary_expression
1436   (cp_parser *, bool);
1437 static tree cp_parser_question_colon_clause
1438   (cp_parser *, tree);
1439 static tree cp_parser_assignment_expression
1440   (cp_parser *, bool);
1441 static enum tree_code cp_parser_assignment_operator_opt
1442   (cp_parser *);
1443 static tree cp_parser_expression
1444   (cp_parser *, bool);
1445 static tree cp_parser_constant_expression
1446   (cp_parser *, bool, bool *);
1447 static tree cp_parser_builtin_offsetof
1448   (cp_parser *);
1449
1450 /* Statements [gram.stmt.stmt]  */
1451
1452 static void cp_parser_statement
1453   (cp_parser *, tree, bool);
1454 static tree cp_parser_labeled_statement
1455   (cp_parser *, tree, bool);
1456 static tree cp_parser_expression_statement
1457   (cp_parser *, tree);
1458 static tree cp_parser_compound_statement
1459   (cp_parser *, tree, bool);
1460 static void cp_parser_statement_seq_opt
1461   (cp_parser *, tree);
1462 static tree cp_parser_selection_statement
1463   (cp_parser *);
1464 static tree cp_parser_condition
1465   (cp_parser *);
1466 static tree cp_parser_iteration_statement
1467   (cp_parser *);
1468 static void cp_parser_for_init_statement
1469   (cp_parser *);
1470 static tree cp_parser_jump_statement
1471   (cp_parser *);
1472 static void cp_parser_declaration_statement
1473   (cp_parser *);
1474
1475 static tree cp_parser_implicitly_scoped_statement
1476   (cp_parser *);
1477 static void cp_parser_already_scoped_statement
1478   (cp_parser *);
1479
1480 /* Declarations [gram.dcl.dcl] */
1481
1482 static void cp_parser_declaration_seq_opt
1483   (cp_parser *);
1484 static void cp_parser_declaration
1485   (cp_parser *);
1486 static void cp_parser_block_declaration
1487   (cp_parser *, bool);
1488 static void cp_parser_simple_declaration
1489   (cp_parser *, bool);
1490 static void cp_parser_decl_specifier_seq
1491   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1492 static tree cp_parser_storage_class_specifier_opt
1493   (cp_parser *);
1494 static tree cp_parser_function_specifier_opt
1495   (cp_parser *, cp_decl_specifier_seq *);
1496 static tree cp_parser_type_specifier
1497   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1498    int *, bool *);
1499 static tree cp_parser_simple_type_specifier
1500   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1501 static tree cp_parser_type_name
1502   (cp_parser *);
1503 static tree cp_parser_elaborated_type_specifier
1504   (cp_parser *, bool, bool);
1505 static tree cp_parser_enum_specifier
1506   (cp_parser *);
1507 static void cp_parser_enumerator_list
1508   (cp_parser *, tree);
1509 static void cp_parser_enumerator_definition
1510   (cp_parser *, tree);
1511 static tree cp_parser_namespace_name
1512   (cp_parser *);
1513 static void cp_parser_namespace_definition
1514   (cp_parser *);
1515 static void cp_parser_namespace_body
1516   (cp_parser *);
1517 static tree cp_parser_qualified_namespace_specifier
1518   (cp_parser *);
1519 static void cp_parser_namespace_alias_definition
1520   (cp_parser *);
1521 static void cp_parser_using_declaration
1522   (cp_parser *);
1523 static void cp_parser_using_directive
1524   (cp_parser *);
1525 static void cp_parser_asm_definition
1526   (cp_parser *);
1527 static void cp_parser_linkage_specification
1528   (cp_parser *);
1529
1530 /* Declarators [gram.dcl.decl] */
1531
1532 static tree cp_parser_init_declarator
1533   (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1534 static cp_declarator *cp_parser_declarator
1535   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1536 static cp_declarator *cp_parser_direct_declarator
1537   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1538 static enum tree_code cp_parser_ptr_operator
1539   (cp_parser *, tree *, cp_cv_quals *);
1540 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1541   (cp_parser *);
1542 static tree cp_parser_declarator_id
1543   (cp_parser *, bool);
1544 static tree cp_parser_type_id
1545   (cp_parser *);
1546 static void cp_parser_type_specifier_seq
1547   (cp_parser *, bool, cp_decl_specifier_seq *);
1548 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1549   (cp_parser *);
1550 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1551   (cp_parser *, bool *);
1552 static cp_parameter_declarator *cp_parser_parameter_declaration
1553   (cp_parser *, bool, bool *);
1554 static void cp_parser_function_body
1555   (cp_parser *);
1556 static tree cp_parser_initializer
1557   (cp_parser *, bool *, bool *);
1558 static tree cp_parser_initializer_clause
1559   (cp_parser *, bool *);
1560 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1561   (cp_parser *, bool *);
1562
1563 static bool cp_parser_ctor_initializer_opt_and_function_body
1564   (cp_parser *);
1565
1566 /* Classes [gram.class] */
1567
1568 static tree cp_parser_class_name
1569   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1570 static tree cp_parser_class_specifier
1571   (cp_parser *);
1572 static tree cp_parser_class_head
1573   (cp_parser *, bool *, tree *);
1574 static enum tag_types cp_parser_class_key
1575   (cp_parser *);
1576 static void cp_parser_member_specification_opt
1577   (cp_parser *);
1578 static void cp_parser_member_declaration
1579   (cp_parser *);
1580 static tree cp_parser_pure_specifier
1581   (cp_parser *);
1582 static tree cp_parser_constant_initializer
1583   (cp_parser *);
1584
1585 /* Derived classes [gram.class.derived] */
1586
1587 static tree cp_parser_base_clause
1588   (cp_parser *);
1589 static tree cp_parser_base_specifier
1590   (cp_parser *);
1591
1592 /* Special member functions [gram.special] */
1593
1594 static tree cp_parser_conversion_function_id
1595   (cp_parser *);
1596 static tree cp_parser_conversion_type_id
1597   (cp_parser *);
1598 static cp_declarator *cp_parser_conversion_declarator_opt
1599   (cp_parser *);
1600 static bool cp_parser_ctor_initializer_opt
1601   (cp_parser *);
1602 static void cp_parser_mem_initializer_list
1603   (cp_parser *);
1604 static tree cp_parser_mem_initializer
1605   (cp_parser *);
1606 static tree cp_parser_mem_initializer_id
1607   (cp_parser *);
1608
1609 /* Overloading [gram.over] */
1610
1611 static tree cp_parser_operator_function_id
1612   (cp_parser *);
1613 static tree cp_parser_operator
1614   (cp_parser *);
1615
1616 /* Templates [gram.temp] */
1617
1618 static void cp_parser_template_declaration
1619   (cp_parser *, bool);
1620 static tree cp_parser_template_parameter_list
1621   (cp_parser *);
1622 static tree cp_parser_template_parameter
1623   (cp_parser *, bool *);
1624 static tree cp_parser_type_parameter
1625   (cp_parser *);
1626 static tree cp_parser_template_id
1627   (cp_parser *, bool, bool, bool);
1628 static tree cp_parser_template_name
1629   (cp_parser *, bool, bool, bool, bool *);
1630 static tree cp_parser_template_argument_list
1631   (cp_parser *);
1632 static tree cp_parser_template_argument
1633   (cp_parser *);
1634 static void cp_parser_explicit_instantiation
1635   (cp_parser *);
1636 static void cp_parser_explicit_specialization
1637   (cp_parser *);
1638
1639 /* Exception handling [gram.exception] */
1640
1641 static tree cp_parser_try_block
1642   (cp_parser *);
1643 static bool cp_parser_function_try_block
1644   (cp_parser *);
1645 static void cp_parser_handler_seq
1646   (cp_parser *);
1647 static void cp_parser_handler
1648   (cp_parser *);
1649 static tree cp_parser_exception_declaration
1650   (cp_parser *);
1651 static tree cp_parser_throw_expression
1652   (cp_parser *);
1653 static tree cp_parser_exception_specification_opt
1654   (cp_parser *);
1655 static tree cp_parser_type_id_list
1656   (cp_parser *);
1657
1658 /* GNU Extensions */
1659
1660 static tree cp_parser_asm_specification_opt
1661   (cp_parser *);
1662 static tree cp_parser_asm_operand_list
1663   (cp_parser *);
1664 static tree cp_parser_asm_clobber_list
1665   (cp_parser *);
1666 static tree cp_parser_attributes_opt
1667   (cp_parser *);
1668 static tree cp_parser_attribute_list
1669   (cp_parser *);
1670 static bool cp_parser_extension_opt
1671   (cp_parser *, int *);
1672 static void cp_parser_label_declaration
1673   (cp_parser *);
1674
1675 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1676 static bool cp_parser_pragma
1677   (cp_parser *, enum pragma_context);
1678
1679 /* Objective-C++ Productions */
1680
1681 static tree cp_parser_objc_message_receiver
1682   (cp_parser *);
1683 static tree cp_parser_objc_message_args
1684   (cp_parser *);
1685 static tree cp_parser_objc_message_expression
1686   (cp_parser *);
1687 static tree cp_parser_objc_encode_expression
1688   (cp_parser *);
1689 static tree cp_parser_objc_defs_expression
1690   (cp_parser *);
1691 static tree cp_parser_objc_protocol_expression
1692   (cp_parser *);
1693 static tree cp_parser_objc_selector_expression
1694   (cp_parser *);
1695 static tree cp_parser_objc_expression
1696   (cp_parser *);
1697 static bool cp_parser_objc_selector_p
1698   (enum cpp_ttype);
1699 static tree cp_parser_objc_selector
1700   (cp_parser *);
1701 static tree cp_parser_objc_protocol_refs_opt
1702   (cp_parser *);
1703 static void cp_parser_objc_declaration
1704   (cp_parser *);
1705 static tree cp_parser_objc_statement
1706   (cp_parser *);
1707
1708 /* Utility Routines */
1709
1710 static tree cp_parser_lookup_name
1711   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1712 static tree cp_parser_lookup_name_simple
1713   (cp_parser *, tree);
1714 static tree cp_parser_maybe_treat_template_as_class
1715   (tree, bool);
1716 static bool cp_parser_check_declarator_template_parameters
1717   (cp_parser *, cp_declarator *);
1718 static bool cp_parser_check_template_parameters
1719   (cp_parser *, unsigned);
1720 static tree cp_parser_simple_cast_expression
1721   (cp_parser *);
1722 static tree cp_parser_global_scope_opt
1723   (cp_parser *, bool);
1724 static bool cp_parser_constructor_declarator_p
1725   (cp_parser *, bool);
1726 static tree cp_parser_function_definition_from_specifiers_and_declarator
1727   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1728 static tree cp_parser_function_definition_after_declarator
1729   (cp_parser *, bool);
1730 static void cp_parser_template_declaration_after_export
1731   (cp_parser *, bool);
1732 static void cp_parser_perform_template_parameter_access_checks
1733   (tree);
1734 static tree cp_parser_single_declaration
1735   (cp_parser *, tree, bool, bool *);
1736 static tree cp_parser_functional_cast
1737   (cp_parser *, tree);
1738 static tree cp_parser_save_member_function_body
1739   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1740 static tree cp_parser_enclosed_template_argument_list
1741   (cp_parser *);
1742 static void cp_parser_save_default_args
1743   (cp_parser *, tree);
1744 static void cp_parser_late_parsing_for_member
1745   (cp_parser *, tree);
1746 static void cp_parser_late_parsing_default_args
1747   (cp_parser *, tree);
1748 static tree cp_parser_sizeof_operand
1749   (cp_parser *, enum rid);
1750 static bool cp_parser_declares_only_class_p
1751   (cp_parser *);
1752 static void cp_parser_set_storage_class
1753   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1754 static void cp_parser_set_decl_spec_type
1755   (cp_decl_specifier_seq *, tree, bool);
1756 static bool cp_parser_friend_p
1757   (const cp_decl_specifier_seq *);
1758 static cp_token *cp_parser_require
1759   (cp_parser *, enum cpp_ttype, const char *);
1760 static cp_token *cp_parser_require_keyword
1761   (cp_parser *, enum rid, const char *);
1762 static bool cp_parser_token_starts_function_definition_p
1763   (cp_token *);
1764 static bool cp_parser_next_token_starts_class_definition_p
1765   (cp_parser *);
1766 static bool cp_parser_next_token_ends_template_argument_p
1767   (cp_parser *);
1768 static bool cp_parser_nth_token_starts_template_argument_list_p
1769   (cp_parser *, size_t);
1770 static enum tag_types cp_parser_token_is_class_key
1771   (cp_token *);
1772 static void cp_parser_check_class_key
1773   (enum tag_types, tree type);
1774 static void cp_parser_check_access_in_redeclaration
1775   (tree type);
1776 static bool cp_parser_optional_template_keyword
1777   (cp_parser *);
1778 static void cp_parser_pre_parsed_nested_name_specifier
1779   (cp_parser *);
1780 static void cp_parser_cache_group
1781   (cp_parser *, enum cpp_ttype, unsigned);
1782 static void cp_parser_parse_tentatively
1783   (cp_parser *);
1784 static void cp_parser_commit_to_tentative_parse
1785   (cp_parser *);
1786 static void cp_parser_abort_tentative_parse
1787   (cp_parser *);
1788 static bool cp_parser_parse_definitely
1789   (cp_parser *);
1790 static inline bool cp_parser_parsing_tentatively
1791   (cp_parser *);
1792 static bool cp_parser_uncommitted_to_tentative_parse_p
1793   (cp_parser *);
1794 static void cp_parser_error
1795   (cp_parser *, const char *);
1796 static void cp_parser_name_lookup_error
1797   (cp_parser *, tree, tree, const char *);
1798 static bool cp_parser_simulate_error
1799   (cp_parser *);
1800 static void cp_parser_check_type_definition
1801   (cp_parser *);
1802 static void cp_parser_check_for_definition_in_return_type
1803   (cp_declarator *, tree);
1804 static void cp_parser_check_for_invalid_template_id
1805   (cp_parser *, tree);
1806 static bool cp_parser_non_integral_constant_expression
1807   (cp_parser *, const char *);
1808 static void cp_parser_diagnose_invalid_type_name
1809   (cp_parser *, tree, tree);
1810 static bool cp_parser_parse_and_diagnose_invalid_type_name
1811   (cp_parser *);
1812 static int cp_parser_skip_to_closing_parenthesis
1813   (cp_parser *, bool, bool, bool);
1814 static void cp_parser_skip_to_end_of_statement
1815   (cp_parser *);
1816 static void cp_parser_consume_semicolon_at_end_of_statement
1817   (cp_parser *);
1818 static void cp_parser_skip_to_end_of_block_or_statement
1819   (cp_parser *);
1820 static void cp_parser_skip_to_closing_brace
1821   (cp_parser *);
1822 static void cp_parser_skip_to_end_of_template_parameter_list
1823   (cp_parser *);
1824 static void cp_parser_skip_to_pragma_eol
1825   (cp_parser*, cp_token *);
1826 static bool cp_parser_error_occurred
1827   (cp_parser *);
1828 static bool cp_parser_allow_gnu_extensions_p
1829   (cp_parser *);
1830 static bool cp_parser_is_string_literal
1831   (cp_token *);
1832 static bool cp_parser_is_keyword
1833   (cp_token *, enum rid);
1834 static tree cp_parser_make_typename_type
1835   (cp_parser *, tree, tree);
1836
1837 /* Returns nonzero if we are parsing tentatively.  */
1838
1839 static inline bool
1840 cp_parser_parsing_tentatively (cp_parser* parser)
1841 {
1842   return parser->context->next != NULL;
1843 }
1844
1845 /* Returns nonzero if TOKEN is a string literal.  */
1846
1847 static bool
1848 cp_parser_is_string_literal (cp_token* token)
1849 {
1850   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1851 }
1852
1853 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1854
1855 static bool
1856 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1857 {
1858   return token->keyword == keyword;
1859 }
1860
1861 /* If not parsing tentatively, issue a diagnostic of the form
1862       FILE:LINE: MESSAGE before TOKEN
1863    where TOKEN is the next token in the input stream.  MESSAGE
1864    (specified by the caller) is usually of the form "expected
1865    OTHER-TOKEN".  */
1866
1867 static void
1868 cp_parser_error (cp_parser* parser, const char* message)
1869 {
1870   if (!cp_parser_simulate_error (parser))
1871     {
1872       cp_token *token = cp_lexer_peek_token (parser->lexer);
1873       /* This diagnostic makes more sense if it is tagged to the line
1874          of the token we just peeked at.  */
1875       cp_lexer_set_source_position_from_token (token);
1876
1877       if (token->type == CPP_PRAGMA)
1878         {
1879           error ("%<#pragma%> is not allowed here");
1880           cp_parser_skip_to_pragma_eol (parser, token);
1881           return;
1882         }
1883
1884       c_parse_error (message,
1885                      /* Because c_parser_error does not understand
1886                         CPP_KEYWORD, keywords are treated like
1887                         identifiers.  */
1888                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1889                      token->value);
1890     }
1891 }
1892
1893 /* Issue an error about name-lookup failing.  NAME is the
1894    IDENTIFIER_NODE DECL is the result of
1895    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1896    the thing that we hoped to find.  */
1897
1898 static void
1899 cp_parser_name_lookup_error (cp_parser* parser,
1900                              tree name,
1901                              tree decl,
1902                              const char* desired)
1903 {
1904   /* If name lookup completely failed, tell the user that NAME was not
1905      declared.  */
1906   if (decl == error_mark_node)
1907     {
1908       if (parser->scope && parser->scope != global_namespace)
1909         error ("%<%D::%D%> has not been declared",
1910                parser->scope, name);
1911       else if (parser->scope == global_namespace)
1912         error ("%<::%D%> has not been declared", name);
1913       else if (parser->object_scope
1914                && !CLASS_TYPE_P (parser->object_scope))
1915         error ("request for member %qD in non-class type %qT",
1916                name, parser->object_scope);
1917       else if (parser->object_scope)
1918         error ("%<%T::%D%> has not been declared",
1919                parser->object_scope, name);
1920       else
1921         error ("%qD has not been declared", name);
1922     }
1923   else if (parser->scope && parser->scope != global_namespace)
1924     error ("%<%D::%D%> %s", parser->scope, name, desired);
1925   else if (parser->scope == global_namespace)
1926     error ("%<::%D%> %s", name, desired);
1927   else
1928     error ("%qD %s", name, desired);
1929 }
1930
1931 /* If we are parsing tentatively, remember that an error has occurred
1932    during this tentative parse.  Returns true if the error was
1933    simulated; false if a message should be issued by the caller.  */
1934
1935 static bool
1936 cp_parser_simulate_error (cp_parser* parser)
1937 {
1938   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1939     {
1940       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1941       return true;
1942     }
1943   return false;
1944 }
1945
1946 /* Check for repeated decl-specifiers.  */
1947
1948 static void
1949 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1950 {
1951   cp_decl_spec ds;
1952
1953   for (ds = ds_first; ds != ds_last; ++ds)
1954     {
1955       unsigned count = decl_specs->specs[(int)ds];
1956       if (count < 2)
1957         continue;
1958       /* The "long" specifier is a special case because of "long long".  */
1959       if (ds == ds_long)
1960         {
1961           if (count > 2)
1962             error ("%<long long long%> is too long for GCC");
1963           else if (pedantic && !in_system_header && warn_long_long)
1964             pedwarn ("ISO C++ does not support %<long long%>");
1965         }
1966       else if (count > 1)
1967         {
1968           static const char *const decl_spec_names[] = {
1969             "signed",
1970             "unsigned",
1971             "short",
1972             "long",
1973             "const",
1974             "volatile",
1975             "restrict",
1976             "inline",
1977             "virtual",
1978             "explicit",
1979             "friend",
1980             "typedef",
1981             "__complex",
1982             "__thread"
1983           };
1984           error ("duplicate %qs", decl_spec_names[(int)ds]);
1985         }
1986     }
1987 }
1988
1989 /* This function is called when a type is defined.  If type
1990    definitions are forbidden at this point, an error message is
1991    issued.  */
1992
1993 static void
1994 cp_parser_check_type_definition (cp_parser* parser)
1995 {
1996   /* If types are forbidden here, issue a message.  */
1997   if (parser->type_definition_forbidden_message)
1998     /* Use `%s' to print the string in case there are any escape
1999        characters in the message.  */
2000     error ("%s", parser->type_definition_forbidden_message);
2001 }
2002
2003 /* This function is called when the DECLARATOR is processed.  The TYPE
2004    was a type defined in the decl-specifiers.  If it is invalid to
2005    define a type in the decl-specifiers for DECLARATOR, an error is
2006    issued.  */
2007
2008 static void
2009 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2010                                                tree type)
2011 {
2012   /* [dcl.fct] forbids type definitions in return types.
2013      Unfortunately, it's not easy to know whether or not we are
2014      processing a return type until after the fact.  */
2015   while (declarator
2016          && (declarator->kind == cdk_pointer
2017              || declarator->kind == cdk_reference
2018              || declarator->kind == cdk_ptrmem))
2019     declarator = declarator->declarator;
2020   if (declarator
2021       && declarator->kind == cdk_function)
2022     {
2023       error ("new types may not be defined in a return type");
2024       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2025               type);
2026     }
2027 }
2028
2029 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2030    "<" in any valid C++ program.  If the next token is indeed "<",
2031    issue a message warning the user about what appears to be an
2032    invalid attempt to form a template-id.  */
2033
2034 static void
2035 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2036                                          tree type)
2037 {
2038   cp_token_position start = 0;
2039
2040   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2041     {
2042       if (TYPE_P (type))
2043         error ("%qT is not a template", type);
2044       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2045         error ("%qE is not a template", type);
2046       else
2047         error ("invalid template-id");
2048       /* Remember the location of the invalid "<".  */
2049       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2050         start = cp_lexer_token_position (parser->lexer, true);
2051       /* Consume the "<".  */
2052       cp_lexer_consume_token (parser->lexer);
2053       /* Parse the template arguments.  */
2054       cp_parser_enclosed_template_argument_list (parser);
2055       /* Permanently remove the invalid template arguments so that
2056          this error message is not issued again.  */
2057       if (start)
2058         cp_lexer_purge_tokens_after (parser->lexer, start);
2059     }
2060 }
2061
2062 /* If parsing an integral constant-expression, issue an error message
2063    about the fact that THING appeared and return true.  Otherwise,
2064    return false.  In either case, set
2065    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2066
2067 static bool
2068 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2069                                             const char *thing)
2070 {
2071   parser->non_integral_constant_expression_p = true;
2072   if (parser->integral_constant_expression_p)
2073     {
2074       if (!parser->allow_non_integral_constant_expression_p)
2075         {
2076           error ("%s cannot appear in a constant-expression", thing);
2077           return true;
2078         }
2079     }
2080   return false;
2081 }
2082
2083 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2084    qualifying scope (or NULL, if none) for ID.  This function commits
2085    to the current active tentative parse, if any.  (Otherwise, the
2086    problematic construct might be encountered again later, resulting
2087    in duplicate error messages.)  */
2088
2089 static void
2090 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2091 {
2092   tree decl, old_scope;
2093   /* Try to lookup the identifier.  */
2094   old_scope = parser->scope;
2095   parser->scope = scope;
2096   decl = cp_parser_lookup_name_simple (parser, id);
2097   parser->scope = old_scope;
2098   /* If the lookup found a template-name, it means that the user forgot
2099   to specify an argument list. Emit a useful error message.  */
2100   if (TREE_CODE (decl) == TEMPLATE_DECL)
2101     error ("invalid use of template-name %qE without an argument list", decl);
2102   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2103     error ("invalid use of destructor %qD as a type", id);
2104   else if (TREE_CODE (decl) == TYPE_DECL)
2105     /* Something like 'unsigned A a;'  */
2106     error ("invalid combination of multiple type-specifiers");
2107   else if (!parser->scope)
2108     {
2109       /* Issue an error message.  */
2110       error ("%qE does not name a type", id);
2111       /* If we're in a template class, it's possible that the user was
2112          referring to a type from a base class.  For example:
2113
2114            template <typename T> struct A { typedef T X; };
2115            template <typename T> struct B : public A<T> { X x; };
2116
2117          The user should have said "typename A<T>::X".  */
2118       if (processing_template_decl && current_class_type
2119           && TYPE_BINFO (current_class_type))
2120         {
2121           tree b;
2122
2123           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2124                b;
2125                b = TREE_CHAIN (b))
2126             {
2127               tree base_type = BINFO_TYPE (b);
2128               if (CLASS_TYPE_P (base_type)
2129                   && dependent_type_p (base_type))
2130                 {
2131                   tree field;
2132                   /* Go from a particular instantiation of the
2133                      template (which will have an empty TYPE_FIELDs),
2134                      to the main version.  */
2135                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2136                   for (field = TYPE_FIELDS (base_type);
2137                        field;
2138                        field = TREE_CHAIN (field))
2139                     if (TREE_CODE (field) == TYPE_DECL
2140                         && DECL_NAME (field) == id)
2141                       {
2142                         inform ("(perhaps %<typename %T::%E%> was intended)",
2143                                 BINFO_TYPE (b), id);
2144                         break;
2145                       }
2146                   if (field)
2147                     break;
2148                 }
2149             }
2150         }
2151     }
2152   /* Here we diagnose qualified-ids where the scope is actually correct,
2153      but the identifier does not resolve to a valid type name.  */
2154   else if (parser->scope != error_mark_node)
2155     {
2156       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2157         error ("%qE in namespace %qE does not name a type",
2158                id, parser->scope);
2159       else if (TYPE_P (parser->scope))
2160         error ("%qE in class %qT does not name a type", id, parser->scope);
2161       else
2162         gcc_unreachable ();
2163     }
2164   cp_parser_commit_to_tentative_parse (parser);
2165 }
2166
2167 /* Check for a common situation where a type-name should be present,
2168    but is not, and issue a sensible error message.  Returns true if an
2169    invalid type-name was detected.
2170
2171    The situation handled by this function are variable declarations of the
2172    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2173    Usually, `ID' should name a type, but if we got here it means that it
2174    does not. We try to emit the best possible error message depending on
2175    how exactly the id-expression looks like.  */
2176
2177 static bool
2178 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2179 {
2180   tree id;
2181
2182   cp_parser_parse_tentatively (parser);
2183   id = cp_parser_id_expression (parser,
2184                                 /*template_keyword_p=*/false,
2185                                 /*check_dependency_p=*/true,
2186                                 /*template_p=*/NULL,
2187                                 /*declarator_p=*/true,
2188                                 /*optional_p=*/false);
2189   /* After the id-expression, there should be a plain identifier,
2190      otherwise this is not a simple variable declaration. Also, if
2191      the scope is dependent, we cannot do much.  */
2192   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2193       || (parser->scope && TYPE_P (parser->scope)
2194           && dependent_type_p (parser->scope)))
2195     {
2196       cp_parser_abort_tentative_parse (parser);
2197       return false;
2198     }
2199   if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2200     return false;
2201
2202   /* Emit a diagnostic for the invalid type.  */
2203   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2204   /* Skip to the end of the declaration; there's no point in
2205      trying to process it.  */
2206   cp_parser_skip_to_end_of_block_or_statement (parser);
2207   return true;
2208 }
2209
2210 /* Consume tokens up to, and including, the next non-nested closing `)'.
2211    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2212    are doing error recovery. Returns -1 if OR_COMMA is true and we
2213    found an unnested comma.  */
2214
2215 static int
2216 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2217                                        bool recovering,
2218                                        bool or_comma,
2219                                        bool consume_paren)
2220 {
2221   unsigned paren_depth = 0;
2222   unsigned brace_depth = 0;
2223
2224   if (recovering && !or_comma
2225       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2226     return 0;
2227
2228   while (true)
2229     {
2230       cp_token * token = cp_lexer_peek_token (parser->lexer);
2231
2232       switch (token->type)
2233         {
2234         case CPP_EOF:
2235         case CPP_PRAGMA_EOL:
2236           /* If we've run out of tokens, then there is no closing `)'.  */
2237           return 0;
2238
2239         case CPP_SEMICOLON:
2240           /* This matches the processing in skip_to_end_of_statement.  */
2241           if (!brace_depth)
2242             return 0;
2243           break;
2244
2245         case CPP_OPEN_BRACE:
2246           ++brace_depth;
2247           break;
2248         case CPP_CLOSE_BRACE:
2249           if (!brace_depth--)
2250             return 0;
2251           break;
2252
2253         case CPP_COMMA:
2254           if (recovering && or_comma && !brace_depth && !paren_depth)
2255             return -1;
2256           break;
2257
2258         case CPP_OPEN_PAREN:
2259           if (!brace_depth)
2260             ++paren_depth;
2261           break;
2262
2263         case CPP_CLOSE_PAREN:
2264           if (!brace_depth && !paren_depth--)
2265             {
2266               if (consume_paren)
2267                 cp_lexer_consume_token (parser->lexer);
2268               return 1;
2269             }
2270           break;
2271
2272         default:
2273           break;
2274         }
2275
2276       /* Consume the token.  */
2277       cp_lexer_consume_token (parser->lexer);
2278     }
2279 }
2280
2281 /* Consume tokens until we reach the end of the current statement.
2282    Normally, that will be just before consuming a `;'.  However, if a
2283    non-nested `}' comes first, then we stop before consuming that.  */
2284
2285 static void
2286 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2287 {
2288   unsigned nesting_depth = 0;
2289
2290   while (true)
2291     {
2292       cp_token *token = cp_lexer_peek_token (parser->lexer);
2293
2294       switch (token->type)
2295         {
2296         case CPP_EOF:
2297         case CPP_PRAGMA_EOL:
2298           /* If we've run out of tokens, stop.  */
2299           return;
2300
2301         case CPP_SEMICOLON:
2302           /* If the next token is a `;', we have reached the end of the
2303              statement.  */
2304           if (!nesting_depth)
2305             return;
2306           break;
2307
2308         case CPP_CLOSE_BRACE:
2309           /* If this is a non-nested '}', stop before consuming it.
2310              That way, when confronted with something like:
2311
2312                { 3 + }
2313
2314              we stop before consuming the closing '}', even though we
2315              have not yet reached a `;'.  */
2316           if (nesting_depth == 0)
2317             return;
2318
2319           /* If it is the closing '}' for a block that we have
2320              scanned, stop -- but only after consuming the token.
2321              That way given:
2322
2323                 void f g () { ... }
2324                 typedef int I;
2325
2326              we will stop after the body of the erroneously declared
2327              function, but before consuming the following `typedef'
2328              declaration.  */
2329           if (--nesting_depth == 0)
2330             {
2331               cp_lexer_consume_token (parser->lexer);
2332               return;
2333             }
2334
2335         case CPP_OPEN_BRACE:
2336           ++nesting_depth;
2337           break;
2338
2339         default:
2340           break;
2341         }
2342
2343       /* Consume the token.  */
2344       cp_lexer_consume_token (parser->lexer);
2345     }
2346 }
2347
2348 /* This function is called at the end of a statement or declaration.
2349    If the next token is a semicolon, it is consumed; otherwise, error
2350    recovery is attempted.  */
2351
2352 static void
2353 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2354 {
2355   /* Look for the trailing `;'.  */
2356   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2357     {
2358       /* If there is additional (erroneous) input, skip to the end of
2359          the statement.  */
2360       cp_parser_skip_to_end_of_statement (parser);
2361       /* If the next token is now a `;', consume it.  */
2362       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2363         cp_lexer_consume_token (parser->lexer);
2364     }
2365 }
2366
2367 /* Skip tokens until we have consumed an entire block, or until we
2368    have consumed a non-nested `;'.  */
2369
2370 static void
2371 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2372 {
2373   int nesting_depth = 0;
2374
2375   while (nesting_depth >= 0)
2376     {
2377       cp_token *token = cp_lexer_peek_token (parser->lexer);
2378
2379       switch (token->type)
2380         {
2381         case CPP_EOF:
2382         case CPP_PRAGMA_EOL:
2383           /* If we've run out of tokens, stop.  */
2384           return;
2385
2386         case CPP_SEMICOLON:
2387           /* Stop if this is an unnested ';'. */
2388           if (!nesting_depth)
2389             nesting_depth = -1;
2390           break;
2391
2392         case CPP_CLOSE_BRACE:
2393           /* Stop if this is an unnested '}', or closes the outermost
2394              nesting level.  */
2395           nesting_depth--;
2396           if (!nesting_depth)
2397             nesting_depth = -1;
2398           break;
2399
2400         case CPP_OPEN_BRACE:
2401           /* Nest. */
2402           nesting_depth++;
2403           break;
2404
2405         default:
2406           break;
2407         }
2408
2409       /* Consume the token.  */
2410       cp_lexer_consume_token (parser->lexer);
2411     }
2412 }
2413
2414 /* Skip tokens until a non-nested closing curly brace is the next
2415    token.  */
2416
2417 static void
2418 cp_parser_skip_to_closing_brace (cp_parser *parser)
2419 {
2420   unsigned nesting_depth = 0;
2421
2422   while (true)
2423     {
2424       cp_token *token = cp_lexer_peek_token (parser->lexer);
2425
2426       switch (token->type)
2427         {
2428         case CPP_EOF:
2429         case CPP_PRAGMA_EOL:
2430           /* If we've run out of tokens, stop.  */
2431           return;
2432
2433         case CPP_CLOSE_BRACE:
2434           /* If the next token is a non-nested `}', then we have reached
2435              the end of the current block.  */
2436           if (nesting_depth-- == 0)
2437             return;
2438           break;
2439
2440         case CPP_OPEN_BRACE:
2441           /* If it the next token is a `{', then we are entering a new
2442              block.  Consume the entire block.  */
2443           ++nesting_depth;
2444           break;
2445
2446         default:
2447           break;
2448         }
2449
2450       /* Consume the token.  */
2451       cp_lexer_consume_token (parser->lexer);
2452     }
2453 }
2454
2455 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2456    parameter is the PRAGMA token, allowing us to purge the entire pragma
2457    sequence.  */
2458
2459 static void
2460 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2461 {
2462   cp_token *token;
2463
2464   parser->lexer->in_pragma = false;
2465
2466   do
2467     token = cp_lexer_consume_token (parser->lexer);
2468   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2469
2470   /* Ensure that the pragma is not parsed again.  */
2471   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2472 }
2473
2474 /* Require pragma end of line, resyncing with it as necessary.  The
2475    arguments are as for cp_parser_skip_to_pragma_eol.  */
2476
2477 static void
2478 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2479 {
2480   parser->lexer->in_pragma = false;
2481   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2482     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2483 }
2484
2485 /* This is a simple wrapper around make_typename_type. When the id is
2486    an unresolved identifier node, we can provide a superior diagnostic
2487    using cp_parser_diagnose_invalid_type_name.  */
2488
2489 static tree
2490 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2491 {
2492   tree result;
2493   if (TREE_CODE (id) == IDENTIFIER_NODE)
2494     {
2495       result = make_typename_type (scope, id, typename_type,
2496                                    /*complain=*/tf_none);
2497       if (result == error_mark_node)
2498         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2499       return result;
2500     }
2501   return make_typename_type (scope, id, typename_type, tf_error);
2502 }
2503
2504
2505 /* Create a new C++ parser.  */
2506
2507 static cp_parser *
2508 cp_parser_new (void)
2509 {
2510   cp_parser *parser;
2511   cp_lexer *lexer;
2512   unsigned i;
2513
2514   /* cp_lexer_new_main is called before calling ggc_alloc because
2515      cp_lexer_new_main might load a PCH file.  */
2516   lexer = cp_lexer_new_main ();
2517
2518   /* Initialize the binops_by_token so that we can get the tree
2519      directly from the token.  */
2520   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2521     binops_by_token[binops[i].token_type] = binops[i];
2522
2523   parser = GGC_CNEW (cp_parser);
2524   parser->lexer = lexer;
2525   parser->context = cp_parser_context_new (NULL);
2526
2527   /* For now, we always accept GNU extensions.  */
2528   parser->allow_gnu_extensions_p = 1;
2529
2530   /* The `>' token is a greater-than operator, not the end of a
2531      template-id.  */
2532   parser->greater_than_is_operator_p = true;
2533
2534   parser->default_arg_ok_p = true;
2535
2536   /* We are not parsing a constant-expression.  */
2537   parser->integral_constant_expression_p = false;
2538   parser->allow_non_integral_constant_expression_p = false;
2539   parser->non_integral_constant_expression_p = false;
2540
2541   /* Local variable names are not forbidden.  */
2542   parser->local_variables_forbidden_p = false;
2543
2544   /* We are not processing an `extern "C"' declaration.  */
2545   parser->in_unbraced_linkage_specification_p = false;
2546
2547   /* We are not processing a declarator.  */
2548   parser->in_declarator_p = false;
2549
2550   /* We are not processing a template-argument-list.  */
2551   parser->in_template_argument_list_p = false;
2552
2553   /* We are not in an iteration statement.  */
2554   parser->in_statement = 0;
2555
2556   /* We are not in a switch statement.  */
2557   parser->in_switch_statement_p = false;
2558
2559   /* We are not parsing a type-id inside an expression.  */
2560   parser->in_type_id_in_expr_p = false;
2561
2562   /* Declarations aren't implicitly extern "C".  */
2563   parser->implicit_extern_c = false;
2564
2565   /* String literals should be translated to the execution character set.  */
2566   parser->translate_strings_p = true;
2567
2568   /* The unparsed function queue is empty.  */
2569   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2570
2571   /* There are no classes being defined.  */
2572   parser->num_classes_being_defined = 0;
2573
2574   /* No template parameters apply.  */
2575   parser->num_template_parameter_lists = 0;
2576
2577   return parser;
2578 }
2579
2580 /* Create a cp_lexer structure which will emit the tokens in CACHE
2581    and push it onto the parser's lexer stack.  This is used for delayed
2582    parsing of in-class method bodies and default arguments, and should
2583    not be confused with tentative parsing.  */
2584 static void
2585 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2586 {
2587   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2588   lexer->next = parser->lexer;
2589   parser->lexer = lexer;
2590
2591   /* Move the current source position to that of the first token in the
2592      new lexer.  */
2593   cp_lexer_set_source_position_from_token (lexer->next_token);
2594 }
2595
2596 /* Pop the top lexer off the parser stack.  This is never used for the
2597    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2598 static void
2599 cp_parser_pop_lexer (cp_parser *parser)
2600 {
2601   cp_lexer *lexer = parser->lexer;
2602   parser->lexer = lexer->next;
2603   cp_lexer_destroy (lexer);
2604
2605   /* Put the current source position back where it was before this
2606      lexer was pushed.  */
2607   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2608 }
2609
2610 /* Lexical conventions [gram.lex]  */
2611
2612 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2613    identifier.  */
2614
2615 static tree
2616 cp_parser_identifier (cp_parser* parser)
2617 {
2618   cp_token *token;
2619
2620   /* Look for the identifier.  */
2621   token = cp_parser_require (parser, CPP_NAME, "identifier");
2622   /* Return the value.  */
2623   return token ? token->value : error_mark_node;
2624 }
2625
2626 /* Parse a sequence of adjacent string constants.  Returns a
2627    TREE_STRING representing the combined, nul-terminated string
2628    constant.  If TRANSLATE is true, translate the string to the
2629    execution character set.  If WIDE_OK is true, a wide string is
2630    invalid here.
2631
2632    C++98 [lex.string] says that if a narrow string literal token is
2633    adjacent to a wide string literal token, the behavior is undefined.
2634    However, C99 6.4.5p4 says that this results in a wide string literal.
2635    We follow C99 here, for consistency with the C front end.
2636
2637    This code is largely lifted from lex_string() in c-lex.c.
2638
2639    FUTURE: ObjC++ will need to handle @-strings here.  */
2640 static tree
2641 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2642 {
2643   tree value;
2644   bool wide = false;
2645   size_t count;
2646   struct obstack str_ob;
2647   cpp_string str, istr, *strs;
2648   cp_token *tok;
2649
2650   tok = cp_lexer_peek_token (parser->lexer);
2651   if (!cp_parser_is_string_literal (tok))
2652     {
2653       cp_parser_error (parser, "expected string-literal");
2654       return error_mark_node;
2655     }
2656
2657   /* Try to avoid the overhead of creating and destroying an obstack
2658      for the common case of just one string.  */
2659   if (!cp_parser_is_string_literal
2660       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2661     {
2662       cp_lexer_consume_token (parser->lexer);
2663
2664       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2665       str.len = TREE_STRING_LENGTH (tok->value);
2666       count = 1;
2667       if (tok->type == CPP_WSTRING)
2668         wide = true;
2669
2670       strs = &str;
2671     }
2672   else
2673     {
2674       gcc_obstack_init (&str_ob);
2675       count = 0;
2676
2677       do
2678         {
2679           cp_lexer_consume_token (parser->lexer);
2680           count++;
2681           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2682           str.len = TREE_STRING_LENGTH (tok->value);
2683           if (tok->type == CPP_WSTRING)
2684             wide = true;
2685
2686           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2687
2688           tok = cp_lexer_peek_token (parser->lexer);
2689         }
2690       while (cp_parser_is_string_literal (tok));
2691
2692       strs = (cpp_string *) obstack_finish (&str_ob);
2693     }
2694
2695   if (wide && !wide_ok)
2696     {
2697       cp_parser_error (parser, "a wide string is invalid in this context");
2698       wide = false;
2699     }
2700
2701   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2702       (parse_in, strs, count, &istr, wide))
2703     {
2704       value = build_string (istr.len, (char *)istr.text);
2705       free ((void *)istr.text);
2706
2707       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2708       value = fix_string_type (value);
2709     }
2710   else
2711     /* cpp_interpret_string has issued an error.  */
2712     value = error_mark_node;
2713
2714   if (count > 1)
2715     obstack_free (&str_ob, 0);
2716
2717   return value;
2718 }
2719
2720
2721 /* Basic concepts [gram.basic]  */
2722
2723 /* Parse a translation-unit.
2724
2725    translation-unit:
2726      declaration-seq [opt]
2727
2728    Returns TRUE if all went well.  */
2729
2730 static bool
2731 cp_parser_translation_unit (cp_parser* parser)
2732 {
2733   /* The address of the first non-permanent object on the declarator
2734      obstack.  */
2735   static void *declarator_obstack_base;
2736
2737   bool success;
2738
2739   /* Create the declarator obstack, if necessary.  */
2740   if (!cp_error_declarator)
2741     {
2742       gcc_obstack_init (&declarator_obstack);
2743       /* Create the error declarator.  */
2744       cp_error_declarator = make_declarator (cdk_error);
2745       /* Create the empty parameter list.  */
2746       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2747       /* Remember where the base of the declarator obstack lies.  */
2748       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2749     }
2750
2751   cp_parser_declaration_seq_opt (parser);
2752
2753   /* If there are no tokens left then all went well.  */
2754   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2755     {
2756       /* Get rid of the token array; we don't need it any more.  */
2757       cp_lexer_destroy (parser->lexer);
2758       parser->lexer = NULL;
2759
2760       /* This file might have been a context that's implicitly extern
2761          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2762       if (parser->implicit_extern_c)
2763         {
2764           pop_lang_context ();
2765           parser->implicit_extern_c = false;
2766         }
2767
2768       /* Finish up.  */
2769       finish_translation_unit ();
2770
2771       success = true;
2772     }
2773   else
2774     {
2775       cp_parser_error (parser, "expected declaration");
2776       success = false;
2777     }
2778
2779   /* Make sure the declarator obstack was fully cleaned up.  */
2780   gcc_assert (obstack_next_free (&declarator_obstack)
2781               == declarator_obstack_base);
2782
2783   /* All went well.  */
2784   return success;
2785 }
2786
2787 /* Expressions [gram.expr] */
2788
2789 /* Parse a primary-expression.
2790
2791    primary-expression:
2792      literal
2793      this
2794      ( expression )
2795      id-expression
2796
2797    GNU Extensions:
2798
2799    primary-expression:
2800      ( compound-statement )
2801      __builtin_va_arg ( assignment-expression , type-id )
2802      __builtin_offsetof ( type-id , offsetof-expression )
2803
2804    Objective-C++ Extension:
2805
2806    primary-expression:
2807      objc-expression
2808
2809    literal:
2810      __null
2811
2812    ADDRESS_P is true iff this expression was immediately preceded by
2813    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2814    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2815    true iff this expression is a template argument.
2816
2817    Returns a representation of the expression.  Upon return, *IDK
2818    indicates what kind of id-expression (if any) was present.  */
2819
2820 static tree
2821 cp_parser_primary_expression (cp_parser *parser,
2822                               bool address_p,
2823                               bool cast_p,
2824                               bool template_arg_p,
2825                               cp_id_kind *idk)
2826 {
2827   cp_token *token;
2828
2829   /* Assume the primary expression is not an id-expression.  */
2830   *idk = CP_ID_KIND_NONE;
2831
2832   /* Peek at the next token.  */
2833   token = cp_lexer_peek_token (parser->lexer);
2834   switch (token->type)
2835     {
2836       /* literal:
2837            integer-literal
2838            character-literal
2839            floating-literal
2840            string-literal
2841            boolean-literal  */
2842     case CPP_CHAR:
2843     case CPP_WCHAR:
2844     case CPP_NUMBER:
2845       token = cp_lexer_consume_token (parser->lexer);
2846       /* Floating-point literals are only allowed in an integral
2847          constant expression if they are cast to an integral or
2848          enumeration type.  */
2849       if (TREE_CODE (token->value) == REAL_CST
2850           && parser->integral_constant_expression_p
2851           && pedantic)
2852         {
2853           /* CAST_P will be set even in invalid code like "int(2.7 +
2854              ...)".   Therefore, we have to check that the next token
2855              is sure to end the cast.  */
2856           if (cast_p)
2857             {
2858               cp_token *next_token;
2859
2860               next_token = cp_lexer_peek_token (parser->lexer);
2861               if (/* The comma at the end of an
2862                      enumerator-definition.  */
2863                   next_token->type != CPP_COMMA
2864                   /* The curly brace at the end of an enum-specifier.  */
2865                   && next_token->type != CPP_CLOSE_BRACE
2866                   /* The end of a statement.  */
2867                   && next_token->type != CPP_SEMICOLON
2868                   /* The end of the cast-expression.  */
2869                   && next_token->type != CPP_CLOSE_PAREN
2870                   /* The end of an array bound.  */
2871                   && next_token->type != CPP_CLOSE_SQUARE
2872                   /* The closing ">" in a template-argument-list.  */
2873                   && (next_token->type != CPP_GREATER
2874                       || parser->greater_than_is_operator_p))
2875                 cast_p = false;
2876             }
2877
2878           /* If we are within a cast, then the constraint that the
2879              cast is to an integral or enumeration type will be
2880              checked at that point.  If we are not within a cast, then
2881              this code is invalid.  */
2882           if (!cast_p)
2883             cp_parser_non_integral_constant_expression
2884               (parser, "floating-point literal");
2885         }
2886       return token->value;
2887
2888     case CPP_STRING:
2889     case CPP_WSTRING:
2890       /* ??? Should wide strings be allowed when parser->translate_strings_p
2891          is false (i.e. in attributes)?  If not, we can kill the third
2892          argument to cp_parser_string_literal.  */
2893       return cp_parser_string_literal (parser,
2894                                        parser->translate_strings_p,
2895                                        true);
2896
2897     case CPP_OPEN_PAREN:
2898       {
2899         tree expr;
2900         bool saved_greater_than_is_operator_p;
2901
2902         /* Consume the `('.  */
2903         cp_lexer_consume_token (parser->lexer);
2904         /* Within a parenthesized expression, a `>' token is always
2905            the greater-than operator.  */
2906         saved_greater_than_is_operator_p
2907           = parser->greater_than_is_operator_p;
2908         parser->greater_than_is_operator_p = true;
2909         /* If we see `( { ' then we are looking at the beginning of
2910            a GNU statement-expression.  */
2911         if (cp_parser_allow_gnu_extensions_p (parser)
2912             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2913           {
2914             /* Statement-expressions are not allowed by the standard.  */
2915             if (pedantic)
2916               pedwarn ("ISO C++ forbids braced-groups within expressions");
2917
2918             /* And they're not allowed outside of a function-body; you
2919                cannot, for example, write:
2920
2921                  int i = ({ int j = 3; j + 1; });
2922
2923                at class or namespace scope.  */
2924             if (!at_function_scope_p ())
2925               error ("statement-expressions are allowed only inside functions");
2926             /* Start the statement-expression.  */
2927             expr = begin_stmt_expr ();
2928             /* Parse the compound-statement.  */
2929             cp_parser_compound_statement (parser, expr, false);
2930             /* Finish up.  */
2931             expr = finish_stmt_expr (expr, false);
2932           }
2933         else
2934           {
2935             /* Parse the parenthesized expression.  */
2936             expr = cp_parser_expression (parser, cast_p);
2937             /* Let the front end know that this expression was
2938                enclosed in parentheses. This matters in case, for
2939                example, the expression is of the form `A::B', since
2940                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2941                not.  */
2942             finish_parenthesized_expr (expr);
2943           }
2944         /* The `>' token might be the end of a template-id or
2945            template-parameter-list now.  */
2946         parser->greater_than_is_operator_p
2947           = saved_greater_than_is_operator_p;
2948         /* Consume the `)'.  */
2949         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2950           cp_parser_skip_to_end_of_statement (parser);
2951
2952         return expr;
2953       }
2954
2955     case CPP_KEYWORD:
2956       switch (token->keyword)
2957         {
2958           /* These two are the boolean literals.  */
2959         case RID_TRUE:
2960           cp_lexer_consume_token (parser->lexer);
2961           return boolean_true_node;
2962         case RID_FALSE:
2963           cp_lexer_consume_token (parser->lexer);
2964           return boolean_false_node;
2965
2966           /* The `__null' literal.  */
2967         case RID_NULL:
2968           cp_lexer_consume_token (parser->lexer);
2969           return null_node;
2970
2971           /* Recognize the `this' keyword.  */
2972         case RID_THIS:
2973           cp_lexer_consume_token (parser->lexer);
2974           if (parser->local_variables_forbidden_p)
2975             {
2976               error ("%<this%> may not be used in this context");
2977               return error_mark_node;
2978             }
2979           /* Pointers cannot appear in constant-expressions.  */
2980           if (cp_parser_non_integral_constant_expression (parser,
2981                                                           "`this'"))
2982             return error_mark_node;
2983           return finish_this_expr ();
2984
2985           /* The `operator' keyword can be the beginning of an
2986              id-expression.  */
2987         case RID_OPERATOR:
2988           goto id_expression;
2989
2990         case RID_FUNCTION_NAME:
2991         case RID_PRETTY_FUNCTION_NAME:
2992         case RID_C99_FUNCTION_NAME:
2993           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2994              __func__ are the names of variables -- but they are
2995              treated specially.  Therefore, they are handled here,
2996              rather than relying on the generic id-expression logic
2997              below.  Grammatically, these names are id-expressions.
2998
2999              Consume the token.  */
3000           token = cp_lexer_consume_token (parser->lexer);
3001           /* Look up the name.  */
3002           return finish_fname (token->value);
3003
3004         case RID_VA_ARG:
3005           {
3006             tree expression;
3007             tree type;
3008
3009             /* The `__builtin_va_arg' construct is used to handle
3010                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3011             cp_lexer_consume_token (parser->lexer);
3012             /* Look for the opening `('.  */
3013             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3014             /* Now, parse the assignment-expression.  */
3015             expression = cp_parser_assignment_expression (parser,
3016                                                           /*cast_p=*/false);
3017             /* Look for the `,'.  */
3018             cp_parser_require (parser, CPP_COMMA, "`,'");
3019             /* Parse the type-id.  */
3020             type = cp_parser_type_id (parser);
3021             /* Look for the closing `)'.  */
3022             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3023             /* Using `va_arg' in a constant-expression is not
3024                allowed.  */
3025             if (cp_parser_non_integral_constant_expression (parser,
3026                                                             "`va_arg'"))
3027               return error_mark_node;
3028             return build_x_va_arg (expression, type);
3029           }
3030
3031         case RID_OFFSETOF:
3032           return cp_parser_builtin_offsetof (parser);
3033
3034           /* Objective-C++ expressions.  */
3035         case RID_AT_ENCODE:
3036         case RID_AT_PROTOCOL:
3037         case RID_AT_SELECTOR:
3038           return cp_parser_objc_expression (parser);
3039
3040         default:
3041           cp_parser_error (parser, "expected primary-expression");
3042           return error_mark_node;
3043         }
3044
3045       /* An id-expression can start with either an identifier, a
3046          `::' as the beginning of a qualified-id, or the "operator"
3047          keyword.  */
3048     case CPP_NAME:
3049     case CPP_SCOPE:
3050     case CPP_TEMPLATE_ID:
3051     case CPP_NESTED_NAME_SPECIFIER:
3052       {
3053         tree id_expression;
3054         tree decl;
3055         const char *error_msg;
3056         bool template_p;
3057         bool done;
3058
3059       id_expression:
3060         /* Parse the id-expression.  */
3061         id_expression
3062           = cp_parser_id_expression (parser,
3063                                      /*template_keyword_p=*/false,
3064                                      /*check_dependency_p=*/true,
3065                                      &template_p,
3066                                      /*declarator_p=*/false,
3067                                      /*optional_p=*/false);
3068         if (id_expression == error_mark_node)
3069           return error_mark_node;
3070         token = cp_lexer_peek_token (parser->lexer);
3071         done = (token->type != CPP_OPEN_SQUARE
3072                 && token->type != CPP_OPEN_PAREN
3073                 && token->type != CPP_DOT
3074                 && token->type != CPP_DEREF
3075                 && token->type != CPP_PLUS_PLUS
3076                 && token->type != CPP_MINUS_MINUS);
3077         /* If we have a template-id, then no further lookup is
3078            required.  If the template-id was for a template-class, we
3079            will sometimes have a TYPE_DECL at this point.  */
3080         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3081                  || TREE_CODE (id_expression) == TYPE_DECL)
3082           decl = id_expression;
3083         /* Look up the name.  */
3084         else
3085           {
3086             tree ambiguous_decls;
3087
3088             decl = cp_parser_lookup_name (parser, id_expression,
3089                                           none_type,
3090                                           template_p,
3091                                           /*is_namespace=*/false,
3092                                           /*check_dependency=*/true,
3093                                           &ambiguous_decls);
3094             /* If the lookup was ambiguous, an error will already have
3095                been issued.  */
3096             if (ambiguous_decls)
3097               return error_mark_node;
3098
3099             /* In Objective-C++, an instance variable (ivar) may be preferred
3100                to whatever cp_parser_lookup_name() found.  */
3101             decl = objc_lookup_ivar (decl, id_expression);
3102
3103             /* If name lookup gives us a SCOPE_REF, then the
3104                qualifying scope was dependent.  */
3105             if (TREE_CODE (decl) == SCOPE_REF)
3106               return decl;
3107             /* Check to see if DECL is a local variable in a context
3108                where that is forbidden.  */
3109             if (parser->local_variables_forbidden_p
3110                 && local_variable_p (decl))
3111               {
3112                 /* It might be that we only found DECL because we are
3113                    trying to be generous with pre-ISO scoping rules.
3114                    For example, consider:
3115
3116                      int i;
3117                      void g() {
3118                        for (int i = 0; i < 10; ++i) {}
3119                        extern void f(int j = i);
3120                      }
3121
3122                    Here, name look up will originally find the out
3123                    of scope `i'.  We need to issue a warning message,
3124                    but then use the global `i'.  */
3125                 decl = check_for_out_of_scope_variable (decl);
3126                 if (local_variable_p (decl))
3127                   {
3128                     error ("local variable %qD may not appear in this context",
3129                            decl);
3130                     return error_mark_node;
3131                   }
3132               }
3133           }
3134
3135         decl = (finish_id_expression
3136                 (id_expression, decl, parser->scope,
3137                  idk,
3138                  parser->integral_constant_expression_p,
3139                  parser->allow_non_integral_constant_expression_p,
3140                  &parser->non_integral_constant_expression_p,
3141                  template_p, done, address_p,
3142                  template_arg_p,
3143                  &error_msg));
3144         if (error_msg)
3145           cp_parser_error (parser, error_msg);
3146         return decl;
3147       }
3148
3149       /* Anything else is an error.  */
3150     default:
3151       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3152       if (c_dialect_objc ()
3153           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3154         return cp_parser_objc_expression (parser);
3155
3156       cp_parser_error (parser, "expected primary-expression");
3157       return error_mark_node;
3158     }
3159 }
3160
3161 /* Parse an id-expression.
3162
3163    id-expression:
3164      unqualified-id
3165      qualified-id
3166
3167    qualified-id:
3168      :: [opt] nested-name-specifier template [opt] unqualified-id
3169      :: identifier
3170      :: operator-function-id
3171      :: template-id
3172
3173    Return a representation of the unqualified portion of the
3174    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3175    a `::' or nested-name-specifier.
3176
3177    Often, if the id-expression was a qualified-id, the caller will
3178    want to make a SCOPE_REF to represent the qualified-id.  This
3179    function does not do this in order to avoid wastefully creating
3180    SCOPE_REFs when they are not required.
3181
3182    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3183    `template' keyword.
3184
3185    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3186    uninstantiated templates.
3187
3188    If *TEMPLATE_P is non-NULL, it is set to true iff the
3189    `template' keyword is used to explicitly indicate that the entity
3190    named is a template.
3191
3192    If DECLARATOR_P is true, the id-expression is appearing as part of
3193    a declarator, rather than as part of an expression.  */
3194
3195 static tree
3196 cp_parser_id_expression (cp_parser *parser,
3197                          bool template_keyword_p,
3198                          bool check_dependency_p,
3199                          bool *template_p,
3200                          bool declarator_p,
3201                          bool optional_p)
3202 {
3203   bool global_scope_p;
3204   bool nested_name_specifier_p;
3205
3206   /* Assume the `template' keyword was not used.  */
3207   if (template_p)
3208     *template_p = template_keyword_p;
3209
3210   /* Look for the optional `::' operator.  */
3211   global_scope_p
3212     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3213        != NULL_TREE);
3214   /* Look for the optional nested-name-specifier.  */
3215   nested_name_specifier_p
3216     = (cp_parser_nested_name_specifier_opt (parser,
3217                                             /*typename_keyword_p=*/false,
3218                                             check_dependency_p,
3219                                             /*type_p=*/false,
3220                                             declarator_p)
3221        != NULL_TREE);
3222   /* If there is a nested-name-specifier, then we are looking at
3223      the first qualified-id production.  */
3224   if (nested_name_specifier_p)
3225     {
3226       tree saved_scope;
3227       tree saved_object_scope;
3228       tree saved_qualifying_scope;
3229       tree unqualified_id;
3230       bool is_template;
3231
3232       /* See if the next token is the `template' keyword.  */
3233       if (!template_p)
3234         template_p = &is_template;
3235       *template_p = cp_parser_optional_template_keyword (parser);
3236       /* Name lookup we do during the processing of the
3237          unqualified-id might obliterate SCOPE.  */
3238       saved_scope = parser->scope;
3239       saved_object_scope = parser->object_scope;
3240       saved_qualifying_scope = parser->qualifying_scope;
3241       /* Process the final unqualified-id.  */
3242       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3243                                                  check_dependency_p,
3244                                                  declarator_p,
3245                                                  /*optional_p=*/false);
3246       /* Restore the SAVED_SCOPE for our caller.  */
3247       parser->scope = saved_scope;
3248       parser->object_scope = saved_object_scope;
3249       parser->qualifying_scope = saved_qualifying_scope;
3250
3251       return unqualified_id;
3252     }
3253   /* Otherwise, if we are in global scope, then we are looking at one
3254      of the other qualified-id productions.  */
3255   else if (global_scope_p)
3256     {
3257       cp_token *token;
3258       tree id;
3259
3260       /* Peek at the next token.  */
3261       token = cp_lexer_peek_token (parser->lexer);
3262
3263       /* If it's an identifier, and the next token is not a "<", then
3264          we can avoid the template-id case.  This is an optimization
3265          for this common case.  */
3266       if (token->type == CPP_NAME
3267           && !cp_parser_nth_token_starts_template_argument_list_p
3268                (parser, 2))
3269         return cp_parser_identifier (parser);
3270
3271       cp_parser_parse_tentatively (parser);
3272       /* Try a template-id.  */
3273       id = cp_parser_template_id (parser,
3274                                   /*template_keyword_p=*/false,
3275                                   /*check_dependency_p=*/true,
3276                                   declarator_p);
3277       /* If that worked, we're done.  */
3278       if (cp_parser_parse_definitely (parser))
3279         return id;
3280
3281       /* Peek at the next token.  (Changes in the token buffer may
3282          have invalidated the pointer obtained above.)  */
3283       token = cp_lexer_peek_token (parser->lexer);
3284
3285       switch (token->type)
3286         {
3287         case CPP_NAME:
3288           return cp_parser_identifier (parser);
3289
3290         case CPP_KEYWORD:
3291           if (token->keyword == RID_OPERATOR)
3292             return cp_parser_operator_function_id (parser);
3293           /* Fall through.  */
3294
3295         default:
3296           cp_parser_error (parser, "expected id-expression");
3297           return error_mark_node;
3298         }
3299     }
3300   else
3301     return cp_parser_unqualified_id (parser, template_keyword_p,
3302                                      /*check_dependency_p=*/true,
3303                                      declarator_p,
3304                                      optional_p);
3305 }
3306
3307 /* Parse an unqualified-id.
3308
3309    unqualified-id:
3310      identifier
3311      operator-function-id
3312      conversion-function-id
3313      ~ class-name
3314      template-id
3315
3316    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3317    keyword, in a construct like `A::template ...'.
3318
3319    Returns a representation of unqualified-id.  For the `identifier'
3320    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3321    production a BIT_NOT_EXPR is returned; the operand of the
3322    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3323    other productions, see the documentation accompanying the
3324    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3325    names are looked up in uninstantiated templates.  If DECLARATOR_P
3326    is true, the unqualified-id is appearing as part of a declarator,
3327    rather than as part of an expression.  */
3328
3329 static tree
3330 cp_parser_unqualified_id (cp_parser* parser,
3331                           bool template_keyword_p,
3332                           bool check_dependency_p,
3333                           bool declarator_p,
3334                           bool optional_p)
3335 {
3336   cp_token *token;
3337
3338   /* Peek at the next token.  */
3339   token = cp_lexer_peek_token (parser->lexer);
3340
3341   switch (token->type)
3342     {
3343     case CPP_NAME:
3344       {
3345         tree id;
3346
3347         /* We don't know yet whether or not this will be a
3348            template-id.  */
3349         cp_parser_parse_tentatively (parser);
3350         /* Try a template-id.  */
3351         id = cp_parser_template_id (parser, template_keyword_p,
3352                                     check_dependency_p,
3353                                     declarator_p);
3354         /* If it worked, we're done.  */
3355         if (cp_parser_parse_definitely (parser))
3356           return id;
3357         /* Otherwise, it's an ordinary identifier.  */
3358         return cp_parser_identifier (parser);
3359       }
3360
3361     case CPP_TEMPLATE_ID:
3362       return cp_parser_template_id (parser, template_keyword_p,
3363                                     check_dependency_p,
3364                                     declarator_p);
3365
3366     case CPP_COMPL:
3367       {
3368         tree type_decl;
3369         tree qualifying_scope;
3370         tree object_scope;
3371         tree scope;
3372         bool done;
3373
3374         /* Consume the `~' token.  */
3375         cp_lexer_consume_token (parser->lexer);
3376         /* Parse the class-name.  The standard, as written, seems to
3377            say that:
3378
3379              template <typename T> struct S { ~S (); };
3380              template <typename T> S<T>::~S() {}
3381
3382            is invalid, since `~' must be followed by a class-name, but
3383            `S<T>' is dependent, and so not known to be a class.
3384            That's not right; we need to look in uninstantiated
3385            templates.  A further complication arises from:
3386
3387              template <typename T> void f(T t) {
3388                t.T::~T();
3389              }
3390
3391            Here, it is not possible to look up `T' in the scope of `T'
3392            itself.  We must look in both the current scope, and the
3393            scope of the containing complete expression.
3394
3395            Yet another issue is:
3396
3397              struct S {
3398                int S;
3399                ~S();
3400              };
3401
3402              S::~S() {}
3403
3404            The standard does not seem to say that the `S' in `~S'
3405            should refer to the type `S' and not the data member
3406            `S::S'.  */
3407
3408         /* DR 244 says that we look up the name after the "~" in the
3409            same scope as we looked up the qualifying name.  That idea
3410            isn't fully worked out; it's more complicated than that.  */
3411         scope = parser->scope;
3412         object_scope = parser->object_scope;
3413         qualifying_scope = parser->qualifying_scope;
3414
3415         /* Check for invalid scopes.  */
3416         if (scope == error_mark_node)
3417           {
3418             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3419               cp_lexer_consume_token (parser->lexer);
3420             return error_mark_node;
3421           }
3422         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3423           {
3424             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3425               error ("scope %qT before %<~%> is not a class-name", scope);
3426             cp_parser_simulate_error (parser);
3427             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3428               cp_lexer_consume_token (parser->lexer);
3429             return error_mark_node;
3430           }
3431         gcc_assert (!scope || TYPE_P (scope));
3432
3433         /* If the name is of the form "X::~X" it's OK.  */
3434         token = cp_lexer_peek_token (parser->lexer);
3435         if (scope
3436             && token->type == CPP_NAME
3437             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3438                 == CPP_OPEN_PAREN)
3439             && constructor_name_p (token->value, scope))
3440           {
3441             cp_lexer_consume_token (parser->lexer);
3442             return build_nt (BIT_NOT_EXPR, scope);
3443           }
3444
3445         /* If there was an explicit qualification (S::~T), first look
3446            in the scope given by the qualification (i.e., S).  */
3447         done = false;
3448         type_decl = NULL_TREE;
3449         if (scope)
3450           {
3451             cp_parser_parse_tentatively (parser);
3452             type_decl = cp_parser_class_name (parser,
3453                                               /*typename_keyword_p=*/false,
3454                                               /*template_keyword_p=*/false,
3455                                               none_type,
3456                                               /*check_dependency=*/false,
3457                                               /*class_head_p=*/false,
3458                                               declarator_p);
3459             if (cp_parser_parse_definitely (parser))
3460               done = true;
3461           }
3462         /* In "N::S::~S", look in "N" as well.  */
3463         if (!done && scope && qualifying_scope)
3464           {
3465             cp_parser_parse_tentatively (parser);
3466             parser->scope = qualifying_scope;
3467             parser->object_scope = NULL_TREE;
3468             parser->qualifying_scope = NULL_TREE;
3469             type_decl
3470               = cp_parser_class_name (parser,
3471                                       /*typename_keyword_p=*/false,
3472                                       /*template_keyword_p=*/false,
3473                                       none_type,
3474                                       /*check_dependency=*/false,
3475                                       /*class_head_p=*/false,
3476                                       declarator_p);
3477             if (cp_parser_parse_definitely (parser))
3478               done = true;
3479           }
3480         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3481         else if (!done && object_scope)
3482           {
3483             cp_parser_parse_tentatively (parser);
3484             parser->scope = object_scope;
3485             parser->object_scope = NULL_TREE;
3486             parser->qualifying_scope = NULL_TREE;
3487             type_decl
3488               = cp_parser_class_name (parser,
3489                                       /*typename_keyword_p=*/false,
3490                                       /*template_keyword_p=*/false,
3491                                       none_type,
3492                                       /*check_dependency=*/false,
3493                                       /*class_head_p=*/false,
3494                                       declarator_p);
3495             if (cp_parser_parse_definitely (parser))
3496               done = true;
3497           }
3498         /* Look in the surrounding context.  */
3499         if (!done)
3500           {
3501             parser->scope = NULL_TREE;
3502             parser->object_scope = NULL_TREE;
3503             parser->qualifying_scope = NULL_TREE;
3504             type_decl
3505               = cp_parser_class_name (parser,
3506                                       /*typename_keyword_p=*/false,
3507                                       /*template_keyword_p=*/false,
3508                                       none_type,
3509                                       /*check_dependency=*/false,
3510                                       /*class_head_p=*/false,
3511                                       declarator_p);
3512           }
3513         /* If an error occurred, assume that the name of the
3514            destructor is the same as the name of the qualifying
3515            class.  That allows us to keep parsing after running
3516            into ill-formed destructor names.  */
3517         if (type_decl == error_mark_node && scope)
3518           return build_nt (BIT_NOT_EXPR, scope);
3519         else if (type_decl == error_mark_node)
3520           return error_mark_node;
3521
3522         /* Check that destructor name and scope match.  */
3523         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3524           {
3525             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3526               error ("declaration of %<~%T%> as member of %qT",
3527                      type_decl, scope);
3528             cp_parser_simulate_error (parser);
3529             return error_mark_node;
3530           }
3531
3532         /* [class.dtor]
3533
3534            A typedef-name that names a class shall not be used as the
3535            identifier in the declarator for a destructor declaration.  */
3536         if (declarator_p
3537             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3538             && !DECL_SELF_REFERENCE_P (type_decl)
3539             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3540           error ("typedef-name %qD used as destructor declarator",
3541                  type_decl);
3542
3543         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3544       }
3545
3546     case CPP_KEYWORD:
3547       if (token->keyword == RID_OPERATOR)
3548         {
3549           tree id;
3550
3551           /* This could be a template-id, so we try that first.  */
3552           cp_parser_parse_tentatively (parser);
3553           /* Try a template-id.  */
3554           id = cp_parser_template_id (parser, template_keyword_p,
3555                                       /*check_dependency_p=*/true,
3556                                       declarator_p);
3557           /* If that worked, we're done.  */
3558           if (cp_parser_parse_definitely (parser))
3559             return id;
3560           /* We still don't know whether we're looking at an
3561              operator-function-id or a conversion-function-id.  */
3562           cp_parser_parse_tentatively (parser);
3563           /* Try an operator-function-id.  */
3564           id = cp_parser_operator_function_id (parser);
3565           /* If that didn't work, try a conversion-function-id.  */
3566           if (!cp_parser_parse_definitely (parser))
3567             id = cp_parser_conversion_function_id (parser);
3568
3569           return id;
3570         }
3571       /* Fall through.  */
3572
3573     default:
3574       if (optional_p)
3575         return NULL_TREE;
3576       cp_parser_error (parser, "expected unqualified-id");
3577       return error_mark_node;
3578     }
3579 }
3580
3581 /* Parse an (optional) nested-name-specifier.
3582
3583    nested-name-specifier:
3584      class-or-namespace-name :: nested-name-specifier [opt]
3585      class-or-namespace-name :: template nested-name-specifier [opt]
3586
3587    PARSER->SCOPE should be set appropriately before this function is
3588    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3589    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3590    in name lookups.
3591
3592    Sets PARSER->SCOPE to the class (TYPE) or namespace
3593    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3594    it unchanged if there is no nested-name-specifier.  Returns the new
3595    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3596
3597    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3598    part of a declaration and/or decl-specifier.  */
3599
3600 static tree
3601 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3602                                      bool typename_keyword_p,
3603                                      bool check_dependency_p,
3604                                      bool type_p,
3605                                      bool is_declaration)
3606 {
3607   bool success = false;
3608   cp_token_position start = 0;
3609   cp_token *token;
3610
3611   /* Remember where the nested-name-specifier starts.  */
3612   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3613     {
3614       start = cp_lexer_token_position (parser->lexer, false);
3615       push_deferring_access_checks (dk_deferred);
3616     }
3617
3618   while (true)
3619     {
3620       tree new_scope;
3621       tree old_scope;
3622       tree saved_qualifying_scope;
3623       bool template_keyword_p;
3624
3625       /* Spot cases that cannot be the beginning of a
3626          nested-name-specifier.  */
3627       token = cp_lexer_peek_token (parser->lexer);
3628
3629       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3630          the already parsed nested-name-specifier.  */
3631       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3632         {
3633           /* Grab the nested-name-specifier and continue the loop.  */
3634           cp_parser_pre_parsed_nested_name_specifier (parser);
3635           success = true;
3636           continue;
3637         }
3638
3639       /* Spot cases that cannot be the beginning of a
3640          nested-name-specifier.  On the second and subsequent times
3641          through the loop, we look for the `template' keyword.  */
3642       if (success && token->keyword == RID_TEMPLATE)
3643         ;
3644       /* A template-id can start a nested-name-specifier.  */
3645       else if (token->type == CPP_TEMPLATE_ID)
3646         ;
3647       else
3648         {
3649           /* If the next token is not an identifier, then it is
3650              definitely not a class-or-namespace-name.  */
3651           if (token->type != CPP_NAME)
3652             break;
3653           /* If the following token is neither a `<' (to begin a
3654              template-id), nor a `::', then we are not looking at a
3655              nested-name-specifier.  */
3656           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3657           if (token->type != CPP_SCOPE
3658               && !cp_parser_nth_token_starts_template_argument_list_p
3659                   (parser, 2))
3660             break;
3661         }
3662
3663       /* The nested-name-specifier is optional, so we parse
3664          tentatively.  */
3665       cp_parser_parse_tentatively (parser);
3666
3667       /* Look for the optional `template' keyword, if this isn't the
3668          first time through the loop.  */
3669       if (success)
3670         template_keyword_p = cp_parser_optional_template_keyword (parser);
3671       else
3672         template_keyword_p = false;
3673
3674       /* Save the old scope since the name lookup we are about to do
3675          might destroy it.  */
3676       old_scope = parser->scope;
3677       saved_qualifying_scope = parser->qualifying_scope;
3678       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3679          look up names in "X<T>::I" in order to determine that "Y" is
3680          a template.  So, if we have a typename at this point, we make
3681          an effort to look through it.  */
3682       if (is_declaration
3683           && !typename_keyword_p
3684           && parser->scope
3685           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3686         parser->scope = resolve_typename_type (parser->scope,
3687                                                /*only_current_p=*/false);
3688       /* Parse the qualifying entity.  */
3689       new_scope
3690         = cp_parser_class_or_namespace_name (parser,
3691                                              typename_keyword_p,
3692                                              template_keyword_p,
3693                                              check_dependency_p,
3694                                              type_p,
3695                                              is_declaration);
3696       /* Look for the `::' token.  */
3697       cp_parser_require (parser, CPP_SCOPE, "`::'");
3698
3699       /* If we found what we wanted, we keep going; otherwise, we're
3700          done.  */
3701       if (!cp_parser_parse_definitely (parser))
3702         {
3703           bool error_p = false;
3704
3705           /* Restore the OLD_SCOPE since it was valid before the
3706              failed attempt at finding the last
3707              class-or-namespace-name.  */
3708           parser->scope = old_scope;
3709           parser->qualifying_scope = saved_qualifying_scope;
3710           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3711             break;
3712           /* If the next token is an identifier, and the one after
3713              that is a `::', then any valid interpretation would have
3714              found a class-or-namespace-name.  */
3715           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3716                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3717                      == CPP_SCOPE)
3718                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3719                      != CPP_COMPL))
3720             {
3721               token = cp_lexer_consume_token (parser->lexer);
3722               if (!error_p)
3723                 {
3724                   if (!token->ambiguous_p)
3725                     {
3726                       tree decl;
3727                       tree ambiguous_decls;
3728
3729                       decl = cp_parser_lookup_name (parser, token->value,
3730                                                     none_type,
3731                                                     /*is_template=*/false,
3732                                                     /*is_namespace=*/false,
3733                                                     /*check_dependency=*/true,
3734                                                     &ambiguous_decls);
3735                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3736                         error ("%qD used without template parameters", decl);
3737                       else if (ambiguous_decls)
3738                         {
3739                           error ("reference to %qD is ambiguous",
3740                                  token->value);
3741                           print_candidates (ambiguous_decls);
3742                           decl = error_mark_node;
3743                         }
3744                       else
3745                         cp_parser_name_lookup_error
3746                           (parser, token->value, decl,
3747                            "is not a class or namespace");
3748                     }
3749                   parser->scope = error_mark_node;
3750                   error_p = true;
3751                   /* Treat this as a successful nested-name-specifier
3752                      due to:
3753
3754                      [basic.lookup.qual]
3755
3756                      If the name found is not a class-name (clause
3757                      _class_) or namespace-name (_namespace.def_), the
3758                      program is ill-formed.  */
3759                   success = true;
3760                 }
3761               cp_lexer_consume_token (parser->lexer);
3762             }
3763           break;
3764         }
3765       /* We've found one valid nested-name-specifier.  */
3766       success = true;
3767       /* Name lookup always gives us a DECL.  */
3768       if (TREE_CODE (new_scope) == TYPE_DECL)
3769         new_scope = TREE_TYPE (new_scope);
3770       /* Uses of "template" must be followed by actual templates.  */
3771       if (template_keyword_p
3772           && !(CLASS_TYPE_P (new_scope)
3773                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3774                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3775                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3776           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3777                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3778                    == TEMPLATE_ID_EXPR)))
3779         pedwarn (TYPE_P (new_scope)
3780                  ? "%qT is not a template"
3781                  : "%qD is not a template",
3782                  new_scope);
3783       /* If it is a class scope, try to complete it; we are about to
3784          be looking up names inside the class.  */
3785       if (TYPE_P (new_scope)
3786           /* Since checking types for dependency can be expensive,
3787              avoid doing it if the type is already complete.  */
3788           && !COMPLETE_TYPE_P (new_scope)
3789           /* Do not try to complete dependent types.  */
3790           && !dependent_type_p (new_scope))
3791         new_scope = complete_type (new_scope);
3792       /* Make sure we look in the right scope the next time through
3793          the loop.  */
3794       parser->scope = new_scope;
3795     }
3796
3797   /* If parsing tentatively, replace the sequence of tokens that makes
3798      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3799      token.  That way, should we re-parse the token stream, we will
3800      not have to repeat the effort required to do the parse, nor will
3801      we issue duplicate error messages.  */
3802   if (success && start)
3803     {
3804       cp_token *token;
3805       tree access_checks;
3806
3807       token = cp_lexer_token_at (parser->lexer, start);
3808       /* Reset the contents of the START token.  */
3809       token->type = CPP_NESTED_NAME_SPECIFIER;
3810       /* Retrieve any deferred checks.  Do not pop this access checks yet
3811          so the memory will not be reclaimed during token replacing below.  */
3812       access_checks = get_deferred_access_checks ();
3813       token->value = build_tree_list (copy_list (access_checks),
3814                                       parser->scope);
3815       TREE_TYPE (token->value) = parser->qualifying_scope;
3816       token->keyword = RID_MAX;
3817
3818       /* Purge all subsequent tokens.  */
3819       cp_lexer_purge_tokens_after (parser->lexer, start);
3820     }
3821
3822   if (start)
3823     pop_to_parent_deferring_access_checks ();
3824
3825   return success ? parser->scope : NULL_TREE;
3826 }
3827
3828 /* Parse a nested-name-specifier.  See
3829    cp_parser_nested_name_specifier_opt for details.  This function
3830    behaves identically, except that it will an issue an error if no
3831    nested-name-specifier is present.  */
3832
3833 static tree
3834 cp_parser_nested_name_specifier (cp_parser *parser,
3835                                  bool typename_keyword_p,
3836                                  bool check_dependency_p,
3837                                  bool type_p,
3838                                  bool is_declaration)
3839 {
3840   tree scope;
3841
3842   /* Look for the nested-name-specifier.  */
3843   scope = cp_parser_nested_name_specifier_opt (parser,
3844                                                typename_keyword_p,
3845                                                check_dependency_p,
3846                                                type_p,
3847                                                is_declaration);
3848   /* If it was not present, issue an error message.  */
3849   if (!scope)
3850     {
3851       cp_parser_error (parser, "expected nested-name-specifier");
3852       parser->scope = NULL_TREE;
3853     }
3854
3855   return scope;
3856 }
3857
3858 /* Parse a class-or-namespace-name.
3859
3860    class-or-namespace-name:
3861      class-name
3862      namespace-name
3863
3864    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3865    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3866    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3867    TYPE_P is TRUE iff the next name should be taken as a class-name,
3868    even the same name is declared to be another entity in the same
3869    scope.
3870
3871    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3872    specified by the class-or-namespace-name.  If neither is found the
3873    ERROR_MARK_NODE is returned.  */
3874
3875 static tree
3876 cp_parser_class_or_namespace_name (cp_parser *parser,
3877                                    bool typename_keyword_p,
3878                                    bool template_keyword_p,
3879                                    bool check_dependency_p,
3880                                    bool type_p,
3881                                    bool is_declaration)
3882 {
3883   tree saved_scope;
3884   tree saved_qualifying_scope;
3885   tree saved_object_scope;
3886   tree scope;
3887   bool only_class_p;
3888
3889   /* Before we try to parse the class-name, we must save away the
3890      current PARSER->SCOPE since cp_parser_class_name will destroy
3891      it.  */
3892   saved_scope = parser->scope;
3893   saved_qualifying_scope = parser->qualifying_scope;
3894   saved_object_scope = parser->object_scope;
3895   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3896      there is no need to look for a namespace-name.  */
3897   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3898   if (!only_class_p)
3899     cp_parser_parse_tentatively (parser);
3900   scope = cp_parser_class_name (parser,
3901                                 typename_keyword_p,
3902                                 template_keyword_p,
3903                                 type_p ? class_type : none_type,
3904                                 check_dependency_p,
3905                                 /*class_head_p=*/false,
3906                                 is_declaration);
3907   /* If that didn't work, try for a namespace-name.  */
3908   if (!only_class_p && !cp_parser_parse_definitely (parser))
3909     {
3910       /* Restore the saved scope.  */
3911       parser->scope = saved_scope;
3912       parser->qualifying_scope = saved_qualifying_scope;
3913       parser->object_scope = saved_object_scope;
3914       /* If we are not looking at an identifier followed by the scope
3915          resolution operator, then this is not part of a
3916          nested-name-specifier.  (Note that this function is only used
3917          to parse the components of a nested-name-specifier.)  */
3918       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3919           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3920         return error_mark_node;
3921       scope = cp_parser_namespace_name (parser);
3922     }
3923
3924   return scope;
3925 }
3926
3927 /* Parse a postfix-expression.
3928
3929    postfix-expression:
3930      primary-expression
3931      postfix-expression [ expression ]
3932      postfix-expression ( expression-list [opt] )
3933      simple-type-specifier ( expression-list [opt] )
3934      typename :: [opt] nested-name-specifier identifier
3935        ( expression-list [opt] )
3936      typename :: [opt] nested-name-specifier template [opt] template-id
3937        ( expression-list [opt] )
3938      postfix-expression . template [opt] id-expression
3939      postfix-expression -> template [opt] id-expression
3940      postfix-expression . pseudo-destructor-name
3941      postfix-expression -> pseudo-destructor-name
3942      postfix-expression ++
3943      postfix-expression --
3944      dynamic_cast < type-id > ( expression )
3945      static_cast < type-id > ( expression )
3946      reinterpret_cast < type-id > ( expression )
3947      const_cast < type-id > ( expression )
3948      typeid ( expression )
3949      typeid ( type-id )
3950
3951    GNU Extension:
3952
3953    postfix-expression:
3954      ( type-id ) { initializer-list , [opt] }
3955
3956    This extension is a GNU version of the C99 compound-literal
3957    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3958    but they are essentially the same concept.)
3959
3960    If ADDRESS_P is true, the postfix expression is the operand of the
3961    `&' operator.  CAST_P is true if this expression is the target of a
3962    cast.
3963
3964    Returns a representation of the expression.  */
3965
3966 static tree
3967 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3968 {
3969   cp_token *token;
3970   enum rid keyword;
3971   cp_id_kind idk = CP_ID_KIND_NONE;
3972   tree postfix_expression = NULL_TREE;
3973
3974   /* Peek at the next token.  */
3975   token = cp_lexer_peek_token (parser->lexer);
3976   /* Some of the productions are determined by keywords.  */
3977   keyword = token->keyword;
3978   switch (keyword)
3979     {
3980     case RID_DYNCAST:
3981     case RID_STATCAST:
3982     case RID_REINTCAST:
3983     case RID_CONSTCAST:
3984       {
3985         tree type;
3986         tree expression;
3987         const char *saved_message;
3988
3989         /* All of these can be handled in the same way from the point
3990            of view of parsing.  Begin by consuming the token
3991            identifying the cast.  */
3992         cp_lexer_consume_token (parser->lexer);
3993
3994         /* New types cannot be defined in the cast.  */
3995         saved_message = parser->type_definition_forbidden_message;
3996         parser->type_definition_forbidden_message
3997           = "types may not be defined in casts";
3998
3999         /* Look for the opening `<'.  */
4000         cp_parser_require (parser, CPP_LESS, "`<'");
4001         /* Parse the type to which we are casting.  */
4002         type = cp_parser_type_id (parser);
4003         /* Look for the closing `>'.  */
4004         cp_parser_require (parser, CPP_GREATER, "`>'");
4005         /* Restore the old message.  */
4006         parser->type_definition_forbidden_message = saved_message;
4007
4008         /* And the expression which is being cast.  */
4009         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4010         expression = cp_parser_expression (parser, /*cast_p=*/true);
4011         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4012
4013         /* Only type conversions to integral or enumeration types
4014            can be used in constant-expressions.  */
4015         if (!cast_valid_in_integral_constant_expression_p (type)
4016             && (cp_parser_non_integral_constant_expression
4017                 (parser,
4018                  "a cast to a type other than an integral or "
4019                  "enumeration type")))
4020           return error_mark_node;
4021
4022         switch (keyword)
4023           {
4024           case RID_DYNCAST:
4025             postfix_expression
4026               = build_dynamic_cast (type, expression);
4027             break;
4028           case RID_STATCAST:
4029             postfix_expression
4030               = build_static_cast (type, expression);
4031             break;
4032           case RID_REINTCAST:
4033             postfix_expression
4034               = build_reinterpret_cast (type, expression);
4035             break;
4036           case RID_CONSTCAST:
4037             postfix_expression
4038               = build_const_cast (type, expression);
4039             break;
4040           default:
4041             gcc_unreachable ();
4042           }
4043       }
4044       break;
4045
4046     case RID_TYPEID:
4047       {
4048         tree type;
4049         const char *saved_message;
4050         bool saved_in_type_id_in_expr_p;
4051
4052         /* Consume the `typeid' token.  */
4053         cp_lexer_consume_token (parser->lexer);
4054         /* Look for the `(' token.  */
4055         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4056         /* Types cannot be defined in a `typeid' expression.  */
4057         saved_message = parser->type_definition_forbidden_message;
4058         parser->type_definition_forbidden_message
4059           = "types may not be defined in a `typeid\' expression";
4060         /* We can't be sure yet whether we're looking at a type-id or an
4061            expression.  */
4062         cp_parser_parse_tentatively (parser);
4063         /* Try a type-id first.  */
4064         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4065         parser->in_type_id_in_expr_p = true;
4066         type = cp_parser_type_id (parser);
4067         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4068         /* Look for the `)' token.  Otherwise, we can't be sure that
4069            we're not looking at an expression: consider `typeid (int
4070            (3))', for example.  */
4071         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4072         /* If all went well, simply lookup the type-id.  */
4073         if (cp_parser_parse_definitely (parser))
4074           postfix_expression = get_typeid (type);
4075         /* Otherwise, fall back to the expression variant.  */
4076         else
4077           {
4078             tree expression;
4079
4080             /* Look for an expression.  */
4081             expression = cp_parser_expression (parser, /*cast_p=*/false);
4082             /* Compute its typeid.  */
4083             postfix_expression = build_typeid (expression);
4084             /* Look for the `)' token.  */
4085             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4086           }
4087         /* Restore the saved message.  */
4088         parser->type_definition_forbidden_message = saved_message;
4089         /* `typeid' may not appear in an integral constant expression.  */
4090         if (cp_parser_non_integral_constant_expression(parser,
4091                                                        "`typeid' operator"))
4092           return error_mark_node;
4093       }
4094       break;
4095
4096     case RID_TYPENAME:
4097       {
4098         tree type;
4099         /* The syntax permitted here is the same permitted for an
4100            elaborated-type-specifier.  */
4101         type = cp_parser_elaborated_type_specifier (parser,
4102                                                     /*is_friend=*/false,
4103                                                     /*is_declaration=*/false);
4104         postfix_expression = cp_parser_functional_cast (parser, type);
4105       }
4106       break;
4107
4108     default:
4109       {
4110         tree type;
4111
4112         /* If the next thing is a simple-type-specifier, we may be
4113            looking at a functional cast.  We could also be looking at
4114            an id-expression.  So, we try the functional cast, and if
4115            that doesn't work we fall back to the primary-expression.  */
4116         cp_parser_parse_tentatively (parser);
4117         /* Look for the simple-type-specifier.  */
4118         type = cp_parser_simple_type_specifier (parser,
4119                                                 /*decl_specs=*/NULL,
4120                                                 CP_PARSER_FLAGS_NONE);
4121         /* Parse the cast itself.  */
4122         if (!cp_parser_error_occurred (parser))
4123           postfix_expression
4124             = cp_parser_functional_cast (parser, type);
4125         /* If that worked, we're done.  */
4126         if (cp_parser_parse_definitely (parser))
4127           break;
4128
4129         /* If the functional-cast didn't work out, try a
4130            compound-literal.  */
4131         if (cp_parser_allow_gnu_extensions_p (parser)
4132             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4133           {
4134             VEC(constructor_elt,gc) *initializer_list = NULL;
4135             bool saved_in_type_id_in_expr_p;
4136
4137             cp_parser_parse_tentatively (parser);
4138             /* Consume the `('.  */
4139             cp_lexer_consume_token (parser->lexer);
4140             /* Parse the type.  */
4141             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4142             parser->in_type_id_in_expr_p = true;
4143             type = cp_parser_type_id (parser);
4144             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4145             /* Look for the `)'.  */
4146             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4147             /* Look for the `{'.  */
4148             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4149             /* If things aren't going well, there's no need to
4150                keep going.  */
4151             if (!cp_parser_error_occurred (parser))
4152               {
4153                 bool non_constant_p;
4154                 /* Parse the initializer-list.  */
4155                 initializer_list
4156                   = cp_parser_initializer_list (parser, &non_constant_p);
4157                 /* Allow a trailing `,'.  */
4158                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4159                   cp_lexer_consume_token (parser->lexer);
4160                 /* Look for the final `}'.  */
4161                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4162               }
4163             /* If that worked, we're definitely looking at a
4164                compound-literal expression.  */
4165             if (cp_parser_parse_definitely (parser))
4166               {
4167                 /* Warn the user that a compound literal is not
4168                    allowed in standard C++.  */
4169                 if (pedantic)
4170                   pedwarn ("ISO C++ forbids compound-literals");
4171                 /* Form the representation of the compound-literal.  */
4172                 postfix_expression
4173                   = finish_compound_literal (type, initializer_list);
4174                 break;
4175               }
4176           }
4177
4178         /* It must be a primary-expression.  */
4179         postfix_expression
4180           = cp_parser_primary_expression (parser, address_p, cast_p,
4181                                           /*template_arg_p=*/false,
4182                                           &idk);
4183       }
4184       break;
4185     }
4186
4187   /* Keep looping until the postfix-expression is complete.  */
4188   while (true)
4189     {
4190       if (idk == CP_ID_KIND_UNQUALIFIED
4191           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4192           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4193         /* It is not a Koenig lookup function call.  */
4194         postfix_expression
4195           = unqualified_name_lookup_error (postfix_expression);
4196
4197       /* Peek at the next token.  */
4198       token = cp_lexer_peek_token (parser->lexer);
4199
4200       switch (token->type)
4201         {
4202         case CPP_OPEN_SQUARE:
4203           postfix_expression
4204             = cp_parser_postfix_open_square_expression (parser,
4205                                                         postfix_expression,
4206                                                         false);
4207           idk = CP_ID_KIND_NONE;
4208           break;
4209
4210         case CPP_OPEN_PAREN:
4211           /* postfix-expression ( expression-list [opt] ) */
4212           {
4213             bool koenig_p;
4214             bool is_builtin_constant_p;
4215             bool saved_integral_constant_expression_p = false;
4216             bool saved_non_integral_constant_expression_p = false;
4217             tree args;
4218
4219             is_builtin_constant_p
4220               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4221             if (is_builtin_constant_p)
4222               {
4223                 /* The whole point of __builtin_constant_p is to allow
4224                    non-constant expressions to appear as arguments.  */
4225                 saved_integral_constant_expression_p
4226                   = parser->integral_constant_expression_p;
4227                 saved_non_integral_constant_expression_p
4228                   = parser->non_integral_constant_expression_p;
4229                 parser->integral_constant_expression_p = false;
4230               }
4231             args = (cp_parser_parenthesized_expression_list
4232                     (parser, /*is_attribute_list=*/false,
4233                      /*cast_p=*/false,
4234                      /*non_constant_p=*/NULL));
4235             if (is_builtin_constant_p)
4236               {
4237                 parser->integral_constant_expression_p
4238                   = saved_integral_constant_expression_p;
4239                 parser->non_integral_constant_expression_p
4240                   = saved_non_integral_constant_expression_p;
4241               }
4242
4243             if (args == error_mark_node)
4244               {
4245                 postfix_expression = error_mark_node;
4246                 break;
4247               }
4248
4249             /* Function calls are not permitted in
4250                constant-expressions.  */
4251             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4252                 && cp_parser_non_integral_constant_expression (parser,
4253                                                                "a function call"))
4254               {
4255                 postfix_expression = error_mark_node;
4256                 break;
4257               }
4258
4259             koenig_p = false;
4260             if (idk == CP_ID_KIND_UNQUALIFIED)
4261               {
4262                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4263                   {
4264                     if (args)
4265                       {
4266                         koenig_p = true;
4267                         postfix_expression
4268                           = perform_koenig_lookup (postfix_expression, args);
4269                       }
4270                     else
4271                       postfix_expression
4272                         = unqualified_fn_lookup_error (postfix_expression);
4273                   }
4274                 /* We do not perform argument-dependent lookup if
4275                    normal lookup finds a non-function, in accordance
4276                    with the expected resolution of DR 218.  */
4277                 else if (args && is_overloaded_fn (postfix_expression))
4278                   {
4279                     tree fn = get_first_fn (postfix_expression);
4280
4281                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4282                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4283
4284                     /* Only do argument dependent lookup if regular
4285                        lookup does not find a set of member functions.
4286                        [basic.lookup.koenig]/2a  */
4287                     if (!DECL_FUNCTION_MEMBER_P (fn))
4288                       {
4289                         koenig_p = true;
4290                         postfix_expression
4291                           = perform_koenig_lookup (postfix_expression, args);
4292                       }
4293                   }
4294               }
4295
4296             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4297               {
4298                 tree instance = TREE_OPERAND (postfix_expression, 0);
4299                 tree fn = TREE_OPERAND (postfix_expression, 1);
4300
4301                 if (processing_template_decl
4302                     && (type_dependent_expression_p (instance)
4303                         || (!BASELINK_P (fn)
4304                             && TREE_CODE (fn) != FIELD_DECL)
4305                         || type_dependent_expression_p (fn)
4306                         || any_type_dependent_arguments_p (args)))
4307                   {
4308                     postfix_expression
4309                       = build_min_nt (CALL_EXPR, postfix_expression,
4310                                       args, NULL_TREE);
4311                     break;
4312                   }
4313
4314                 if (BASELINK_P (fn))
4315                   postfix_expression
4316                     = (build_new_method_call
4317                        (instance, fn, args, NULL_TREE,
4318                         (idk == CP_ID_KIND_QUALIFIED
4319                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4320                         /*fn_p=*/NULL));
4321                 else
4322                   postfix_expression
4323                     = finish_call_expr (postfix_expression, args,
4324                                         /*disallow_virtual=*/false,
4325                                         /*koenig_p=*/false);
4326               }
4327             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4328                      || TREE_CODE (postfix_expression) == MEMBER_REF
4329                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4330               postfix_expression = (build_offset_ref_call_from_tree
4331                                     (postfix_expression, args));
4332             else if (idk == CP_ID_KIND_QUALIFIED)
4333               /* A call to a static class member, or a namespace-scope
4334                  function.  */
4335               postfix_expression
4336                 = finish_call_expr (postfix_expression, args,
4337                                     /*disallow_virtual=*/true,
4338                                     koenig_p);
4339             else
4340               /* All other function calls.  */
4341               postfix_expression
4342                 = finish_call_expr (postfix_expression, args,
4343                                     /*disallow_virtual=*/false,
4344                                     koenig_p);
4345
4346             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4347             idk = CP_ID_KIND_NONE;
4348           }
4349           break;
4350
4351         case CPP_DOT:
4352         case CPP_DEREF:
4353           /* postfix-expression . template [opt] id-expression
4354              postfix-expression . pseudo-destructor-name
4355              postfix-expression -> template [opt] id-expression
4356              postfix-expression -> pseudo-destructor-name */
4357
4358           /* Consume the `.' or `->' operator.  */
4359           cp_lexer_consume_token (parser->lexer);
4360
4361           postfix_expression
4362             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4363                                                       postfix_expression,
4364                                                       false, &idk);
4365           break;
4366
4367         case CPP_PLUS_PLUS:
4368           /* postfix-expression ++  */
4369           /* Consume the `++' token.  */
4370           cp_lexer_consume_token (parser->lexer);
4371           /* Generate a representation for the complete expression.  */
4372           postfix_expression
4373             = finish_increment_expr (postfix_expression,
4374                                      POSTINCREMENT_EXPR);
4375           /* Increments may not appear in constant-expressions.  */
4376           if (cp_parser_non_integral_constant_expression (parser,
4377                                                           "an increment"))
4378             postfix_expression = error_mark_node;
4379           idk = CP_ID_KIND_NONE;
4380           break;
4381
4382         case CPP_MINUS_MINUS:
4383           /* postfix-expression -- */
4384           /* Consume the `--' token.  */
4385           cp_lexer_consume_token (parser->lexer);
4386           /* Generate a representation for the complete expression.  */
4387           postfix_expression
4388             = finish_increment_expr (postfix_expression,
4389                                      POSTDECREMENT_EXPR);
4390           /* Decrements may not appear in constant-expressions.  */
4391           if (cp_parser_non_integral_constant_expression (parser,
4392                                                           "a decrement"))
4393             postfix_expression = error_mark_node;
4394           idk = CP_ID_KIND_NONE;
4395           break;
4396
4397         default:
4398           return postfix_expression;
4399         }
4400     }
4401
4402   /* We should never get here.  */
4403   gcc_unreachable ();
4404   return error_mark_node;
4405 }
4406
4407 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4408    by cp_parser_builtin_offsetof.  We're looking for
4409
4410      postfix-expression [ expression ]
4411
4412    FOR_OFFSETOF is set if we're being called in that context, which
4413    changes how we deal with integer constant expressions.  */
4414
4415 static tree
4416 cp_parser_postfix_open_square_expression (cp_parser *parser,
4417                                           tree postfix_expression,
4418                                           bool for_offsetof)
4419 {
4420   tree index;
4421
4422   /* Consume the `[' token.  */
4423   cp_lexer_consume_token (parser->lexer);
4424
4425   /* Parse the index expression.  */
4426   /* ??? For offsetof, there is a question of what to allow here.  If
4427      offsetof is not being used in an integral constant expression context,
4428      then we *could* get the right answer by computing the value at runtime.
4429      If we are in an integral constant expression context, then we might
4430      could accept any constant expression; hard to say without analysis.
4431      Rather than open the barn door too wide right away, allow only integer
4432      constant expressions here.  */
4433   if (for_offsetof)
4434     index = cp_parser_constant_expression (parser, false, NULL);
4435   else
4436     index = cp_parser_expression (parser, /*cast_p=*/false);
4437
4438   /* Look for the closing `]'.  */
4439   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4440
4441   /* Build the ARRAY_REF.  */
4442   postfix_expression = grok_array_decl (postfix_expression, index);
4443
4444   /* When not doing offsetof, array references are not permitted in
4445      constant-expressions.  */
4446   if (!for_offsetof
4447       && (cp_parser_non_integral_constant_expression
4448           (parser, "an array reference")))
4449     postfix_expression = error_mark_node;
4450
4451   return postfix_expression;
4452 }
4453
4454 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4455    by cp_parser_builtin_offsetof.  We're looking for
4456
4457      postfix-expression . template [opt] id-expression
4458      postfix-expression . pseudo-destructor-name
4459      postfix-expression -> template [opt] id-expression
4460      postfix-expression -> pseudo-destructor-name
4461
4462    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4463    limits what of the above we'll actually accept, but nevermind.
4464    TOKEN_TYPE is the "." or "->" token, which will already have been
4465    removed from the stream.  */
4466
4467 static tree
4468 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4469                                         enum cpp_ttype token_type,
4470                                         tree postfix_expression,
4471                                         bool for_offsetof, cp_id_kind *idk)
4472 {
4473   tree name;
4474   bool dependent_p;
4475   bool pseudo_destructor_p;
4476   tree scope = NULL_TREE;
4477
4478   /* If this is a `->' operator, dereference the pointer.  */
4479   if (token_type == CPP_DEREF)
4480     postfix_expression = build_x_arrow (postfix_expression);
4481   /* Check to see whether or not the expression is type-dependent.  */
4482   dependent_p = type_dependent_expression_p (postfix_expression);
4483   /* The identifier following the `->' or `.' is not qualified.  */
4484   parser->scope = NULL_TREE;
4485   parser->qualifying_scope = NULL_TREE;
4486   parser->object_scope = NULL_TREE;
4487   *idk = CP_ID_KIND_NONE;
4488   /* Enter the scope corresponding to the type of the object
4489      given by the POSTFIX_EXPRESSION.  */
4490   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4491     {
4492       scope = TREE_TYPE (postfix_expression);
4493       /* According to the standard, no expression should ever have
4494          reference type.  Unfortunately, we do not currently match
4495          the standard in this respect in that our internal representation
4496          of an expression may have reference type even when the standard
4497          says it does not.  Therefore, we have to manually obtain the
4498          underlying type here.  */
4499       scope = non_reference (scope);
4500       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4501       if (scope == unknown_type_node)
4502         {
4503           error ("%qE does not have class type", postfix_expression);
4504           scope = NULL_TREE;
4505         }
4506       else
4507         scope = complete_type_or_else (scope, NULL_TREE);
4508       /* Let the name lookup machinery know that we are processing a
4509          class member access expression.  */
4510       parser->context->object_type = scope;
4511       /* If something went wrong, we want to be able to discern that case,
4512          as opposed to the case where there was no SCOPE due to the type
4513          of expression being dependent.  */
4514       if (!scope)
4515         scope = error_mark_node;
4516       /* If the SCOPE was erroneous, make the various semantic analysis
4517          functions exit quickly -- and without issuing additional error
4518          messages.  */
4519       if (scope == error_mark_node)
4520         postfix_expression = error_mark_node;
4521     }
4522
4523   /* Assume this expression is not a pseudo-destructor access.  */
4524   pseudo_destructor_p = false;
4525
4526   /* If the SCOPE is a scalar type, then, if this is a valid program,
4527      we must be looking at a pseudo-destructor-name.  */
4528   if (scope && SCALAR_TYPE_P (scope))
4529     {
4530       tree s;
4531       tree type;
4532
4533       cp_parser_parse_tentatively (parser);
4534       /* Parse the pseudo-destructor-name.  */
4535       s = NULL_TREE;
4536       cp_parser_pseudo_destructor_name (parser, &s, &type);
4537       if (cp_parser_parse_definitely (parser))
4538         {
4539           pseudo_destructor_p = true;
4540           postfix_expression
4541             = finish_pseudo_destructor_expr (postfix_expression,
4542                                              s, TREE_TYPE (type));
4543         }
4544     }
4545
4546   if (!pseudo_destructor_p)
4547     {
4548       /* If the SCOPE is not a scalar type, we are looking at an
4549          ordinary class member access expression, rather than a
4550          pseudo-destructor-name.  */
4551       bool template_p;
4552       /* Parse the id-expression.  */
4553       name = (cp_parser_id_expression
4554               (parser,
4555                cp_parser_optional_template_keyword (parser),
4556                /*check_dependency_p=*/true,
4557                &template_p,
4558                /*declarator_p=*/false,
4559                /*optional_p=*/false));
4560       /* In general, build a SCOPE_REF if the member name is qualified.
4561          However, if the name was not dependent and has already been
4562          resolved; there is no need to build the SCOPE_REF.  For example;
4563
4564              struct X { void f(); };
4565              template <typename T> void f(T* t) { t->X::f(); }
4566
4567          Even though "t" is dependent, "X::f" is not and has been resolved
4568          to a BASELINK; there is no need to include scope information.  */
4569
4570       /* But we do need to remember that there was an explicit scope for
4571          virtual function calls.  */
4572       if (parser->scope)
4573         *idk = CP_ID_KIND_QUALIFIED;
4574
4575       /* If the name is a template-id that names a type, we will get a
4576          TYPE_DECL here.  That is invalid code.  */
4577       if (TREE_CODE (name) == TYPE_DECL)
4578         {
4579           error ("invalid use of %qD", name);
4580           postfix_expression = error_mark_node;
4581         }
4582       else
4583         {
4584           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4585             {
4586               name = build_qualified_name (/*type=*/NULL_TREE,
4587                                            parser->scope,
4588                                            name,
4589                                            template_p);
4590               parser->scope = NULL_TREE;
4591               parser->qualifying_scope = NULL_TREE;
4592               parser->object_scope = NULL_TREE;
4593             }
4594           if (scope && name && BASELINK_P (name))
4595             adjust_result_of_qualified_name_lookup
4596               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4597           postfix_expression
4598             = finish_class_member_access_expr (postfix_expression, name,
4599                                                template_p);
4600         }
4601     }
4602
4603   /* We no longer need to look up names in the scope of the object on
4604      the left-hand side of the `.' or `->' operator.  */
4605   parser->context->object_type = NULL_TREE;
4606
4607   /* Outside of offsetof, these operators may not appear in
4608      constant-expressions.  */
4609   if (!for_offsetof
4610       && (cp_parser_non_integral_constant_expression
4611           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4612     postfix_expression = error_mark_node;
4613
4614   return postfix_expression;
4615 }
4616
4617 /* Parse a parenthesized expression-list.
4618
4619    expression-list:
4620      assignment-expression
4621      expression-list, assignment-expression
4622
4623    attribute-list:
4624      expression-list
4625      identifier
4626      identifier, expression-list
4627
4628    CAST_P is true if this expression is the target of a cast.
4629
4630    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4631    representation of an assignment-expression.  Note that a TREE_LIST
4632    is returned even if there is only a single expression in the list.
4633    error_mark_node is returned if the ( and or ) are
4634    missing. NULL_TREE is returned on no expressions. The parentheses
4635    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4636    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4637    indicates whether or not all of the expressions in the list were
4638    constant.  */
4639
4640 static tree
4641 cp_parser_parenthesized_expression_list (cp_parser* parser,
4642                                          bool is_attribute_list,
4643                                          bool cast_p,
4644                                          bool *non_constant_p)
4645 {
4646   tree expression_list = NULL_TREE;
4647   bool fold_expr_p = is_attribute_list;
4648   tree identifier = NULL_TREE;
4649
4650   /* Assume all the expressions will be constant.  */
4651   if (non_constant_p)
4652     *non_constant_p = false;
4653
4654   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4655     return error_mark_node;
4656
4657   /* Consume expressions until there are no more.  */
4658   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4659     while (true)
4660       {
4661         tree expr;
4662
4663         /* At the beginning of attribute lists, check to see if the
4664            next token is an identifier.  */
4665         if (is_attribute_list
4666             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4667           {
4668             cp_token *token;
4669
4670             /* Consume the identifier.  */
4671             token = cp_lexer_consume_token (parser->lexer);
4672             /* Save the identifier.  */
4673             identifier = token->value;
4674           }
4675         else
4676           {
4677             /* Parse the next assignment-expression.  */
4678             if (non_constant_p)
4679               {
4680                 bool expr_non_constant_p;
4681                 expr = (cp_parser_constant_expression
4682                         (parser, /*allow_non_constant_p=*/true,
4683                          &expr_non_constant_p));
4684                 if (expr_non_constant_p)
4685                   *non_constant_p = true;
4686               }
4687             else
4688               expr = cp_parser_assignment_expression (parser, cast_p);
4689
4690             if (fold_expr_p)
4691               expr = fold_non_dependent_expr (expr);
4692
4693              /* Add it to the list.  We add error_mark_node
4694                 expressions to the list, so that we can still tell if
4695                 the correct form for a parenthesized expression-list
4696                 is found. That gives better errors.  */
4697             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4698
4699             if (expr == error_mark_node)
4700               goto skip_comma;
4701           }
4702
4703         /* After the first item, attribute lists look the same as
4704            expression lists.  */
4705         is_attribute_list = false;
4706
4707       get_comma:;
4708         /* If the next token isn't a `,', then we are done.  */
4709         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4710           break;
4711
4712         /* Otherwise, consume the `,' and keep going.  */
4713         cp_lexer_consume_token (parser->lexer);
4714       }
4715
4716   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4717     {
4718       int ending;
4719
4720     skip_comma:;
4721       /* We try and resync to an unnested comma, as that will give the
4722          user better diagnostics.  */
4723       ending = cp_parser_skip_to_closing_parenthesis (parser,
4724                                                       /*recovering=*/true,
4725                                                       /*or_comma=*/true,
4726                                                       /*consume_paren=*/true);
4727       if (ending < 0)
4728         goto get_comma;
4729       if (!ending)
4730         return error_mark_node;
4731     }
4732
4733   /* We built up the list in reverse order so we must reverse it now.  */
4734   expression_list = nreverse (expression_list);
4735   if (identifier)
4736     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4737
4738   return expression_list;
4739 }
4740
4741 /* Parse a pseudo-destructor-name.
4742
4743    pseudo-destructor-name:
4744      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4745      :: [opt] nested-name-specifier template template-id :: ~ type-name
4746      :: [opt] nested-name-specifier [opt] ~ type-name
4747
4748    If either of the first two productions is used, sets *SCOPE to the
4749    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4750    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4751    or ERROR_MARK_NODE if the parse fails.  */
4752
4753 static void
4754 cp_parser_pseudo_destructor_name (cp_parser* parser,
4755                                   tree* scope,
4756                                   tree* type)
4757 {
4758   bool nested_name_specifier_p;
4759
4760   /* Assume that things will not work out.  */
4761   *type = error_mark_node;
4762
4763   /* Look for the optional `::' operator.  */
4764   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4765   /* Look for the optional nested-name-specifier.  */
4766   nested_name_specifier_p
4767     = (cp_parser_nested_name_specifier_opt (parser,
4768                                             /*typename_keyword_p=*/false,
4769                                             /*check_dependency_p=*/true,
4770                                             /*type_p=*/false,
4771                                             /*is_declaration=*/true)
4772        != NULL_TREE);
4773   /* Now, if we saw a nested-name-specifier, we might be doing the
4774      second production.  */
4775   if (nested_name_specifier_p
4776       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4777     {
4778       /* Consume the `template' keyword.  */
4779       cp_lexer_consume_token (parser->lexer);
4780       /* Parse the template-id.  */
4781       cp_parser_template_id (parser,
4782                              /*template_keyword_p=*/true,
4783                              /*check_dependency_p=*/false,
4784                              /*is_declaration=*/true);
4785       /* Look for the `::' token.  */
4786       cp_parser_require (parser, CPP_SCOPE, "`::'");
4787     }
4788   /* If the next token is not a `~', then there might be some
4789      additional qualification.  */
4790   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4791     {
4792       /* Look for the type-name.  */
4793       *scope = TREE_TYPE (cp_parser_type_name (parser));
4794
4795       if (*scope == error_mark_node)
4796         return;
4797
4798       /* If we don't have ::~, then something has gone wrong.  Since
4799          the only caller of this function is looking for something
4800          after `.' or `->' after a scalar type, most likely the
4801          program is trying to get a member of a non-aggregate
4802          type.  */
4803       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4804           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4805         {
4806           cp_parser_error (parser, "request for member of non-aggregate type");
4807           return;
4808         }
4809
4810       /* Look for the `::' token.  */
4811       cp_parser_require (parser, CPP_SCOPE, "`::'");
4812     }
4813   else
4814     *scope = NULL_TREE;
4815
4816   /* Look for the `~'.  */
4817   cp_parser_require (parser, CPP_COMPL, "`~'");
4818   /* Look for the type-name again.  We are not responsible for
4819      checking that it matches the first type-name.  */
4820   *type = cp_parser_type_name (parser);
4821 }
4822
4823 /* Parse a unary-expression.
4824
4825    unary-expression:
4826      postfix-expression
4827      ++ cast-expression
4828      -- cast-expression
4829      unary-operator cast-expression
4830      sizeof unary-expression
4831      sizeof ( type-id )
4832      new-expression
4833      delete-expression
4834
4835    GNU Extensions:
4836
4837    unary-expression:
4838      __extension__ cast-expression
4839      __alignof__ unary-expression
4840      __alignof__ ( type-id )
4841      __real__ cast-expression
4842      __imag__ cast-expression
4843      && identifier
4844
4845    ADDRESS_P is true iff the unary-expression is appearing as the
4846    operand of the `&' operator.   CAST_P is true if this expression is
4847    the target of a cast.
4848
4849    Returns a representation of the expression.  */
4850
4851 static tree
4852 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4853 {
4854   cp_token *token;
4855   enum tree_code unary_operator;
4856
4857   /* Peek at the next token.  */
4858   token = cp_lexer_peek_token (parser->lexer);
4859   /* Some keywords give away the kind of expression.  */
4860   if (token->type == CPP_KEYWORD)
4861     {
4862       enum rid keyword = token->keyword;
4863
4864       switch (keyword)
4865         {
4866         case RID_ALIGNOF:
4867         case RID_SIZEOF:
4868           {
4869             tree operand;
4870             enum tree_code op;
4871
4872             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4873             /* Consume the token.  */
4874             cp_lexer_consume_token (parser->lexer);
4875             /* Parse the operand.  */
4876             operand = cp_parser_sizeof_operand (parser, keyword);
4877
4878             if (TYPE_P (operand))
4879               return cxx_sizeof_or_alignof_type (operand, op, true);
4880             else
4881               return cxx_sizeof_or_alignof_expr (operand, op);
4882           }
4883
4884         case RID_NEW:
4885           return cp_parser_new_expression (parser);
4886
4887         case RID_DELETE:
4888           return cp_parser_delete_expression (parser);
4889
4890         case RID_EXTENSION:
4891           {
4892             /* The saved value of the PEDANTIC flag.  */
4893             int saved_pedantic;
4894             tree expr;
4895
4896             /* Save away the PEDANTIC flag.  */
4897             cp_parser_extension_opt (parser, &saved_pedantic);
4898             /* Parse the cast-expression.  */
4899             expr = cp_parser_simple_cast_expression (parser);
4900             /* Restore the PEDANTIC flag.  */
4901             pedantic = saved_pedantic;
4902
4903             return expr;
4904           }
4905
4906         case RID_REALPART:
4907         case RID_IMAGPART:
4908           {
4909             tree expression;
4910
4911             /* Consume the `__real__' or `__imag__' token.  */
4912             cp_lexer_consume_token (parser->lexer);
4913             /* Parse the cast-expression.  */
4914             expression = cp_parser_simple_cast_expression (parser);
4915             /* Create the complete representation.  */
4916             return build_x_unary_op ((keyword == RID_REALPART
4917                                       ? REALPART_EXPR : IMAGPART_EXPR),
4918                                      expression);
4919           }
4920           break;
4921
4922         default:
4923           break;
4924         }
4925     }
4926
4927   /* Look for the `:: new' and `:: delete', which also signal the
4928      beginning of a new-expression, or delete-expression,
4929      respectively.  If the next token is `::', then it might be one of
4930      these.  */
4931   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4932     {
4933       enum rid keyword;
4934
4935       /* See if the token after the `::' is one of the keywords in
4936          which we're interested.  */
4937       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4938       /* If it's `new', we have a new-expression.  */
4939       if (keyword == RID_NEW)
4940         return cp_parser_new_expression (parser);
4941       /* Similarly, for `delete'.  */
4942       else if (keyword == RID_DELETE)
4943         return cp_parser_delete_expression (parser);
4944     }
4945
4946   /* Look for a unary operator.  */
4947   unary_operator = cp_parser_unary_operator (token);
4948   /* The `++' and `--' operators can be handled similarly, even though
4949      they are not technically unary-operators in the grammar.  */
4950   if (unary_operator == ERROR_MARK)
4951     {
4952       if (token->type == CPP_PLUS_PLUS)
4953         unary_operator = PREINCREMENT_EXPR;
4954       else if (token->type == CPP_MINUS_MINUS)
4955         unary_operator = PREDECREMENT_EXPR;
4956       /* Handle the GNU address-of-label extension.  */
4957       else if (cp_parser_allow_gnu_extensions_p (parser)
4958                && token->type == CPP_AND_AND)
4959         {
4960           tree identifier;
4961
4962           /* Consume the '&&' token.  */
4963           cp_lexer_consume_token (parser->lexer);
4964           /* Look for the identifier.  */
4965           identifier = cp_parser_identifier (parser);
4966           /* Create an expression representing the address.  */
4967           return finish_label_address_expr (identifier);
4968         }
4969     }
4970   if (unary_operator != ERROR_MARK)
4971     {
4972       tree cast_expression;
4973       tree expression = error_mark_node;
4974       const char *non_constant_p = NULL;
4975
4976       /* Consume the operator token.  */
4977       token = cp_lexer_consume_token (parser->lexer);
4978       /* Parse the cast-expression.  */
4979       cast_expression
4980         = cp_parser_cast_expression (parser,
4981                                      unary_operator == ADDR_EXPR,
4982                                      /*cast_p=*/false);
4983       /* Now, build an appropriate representation.  */
4984       switch (unary_operator)
4985         {
4986         case INDIRECT_REF:
4987           non_constant_p = "`*'";
4988           expression = build_x_indirect_ref (cast_expression, "unary *");
4989           break;
4990
4991         case ADDR_EXPR:
4992           non_constant_p = "`&'";
4993           /* Fall through.  */
4994         case BIT_NOT_EXPR:
4995           expression = build_x_unary_op (unary_operator, cast_expression);
4996           break;
4997
4998         case PREINCREMENT_EXPR:
4999         case PREDECREMENT_EXPR:
5000           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5001                             ? "`++'" : "`--'");
5002           /* Fall through.  */
5003         case UNARY_PLUS_EXPR:
5004         case NEGATE_EXPR:
5005         case TRUTH_NOT_EXPR:
5006           expression = finish_unary_op_expr (unary_operator, cast_expression);
5007           break;
5008
5009         default:
5010           gcc_unreachable ();
5011         }
5012
5013       if (non_constant_p
5014           && cp_parser_non_integral_constant_expression (parser,
5015                                                          non_constant_p))
5016         expression = error_mark_node;
5017
5018       return expression;
5019     }
5020
5021   return cp_parser_postfix_expression (parser, address_p, cast_p);
5022 }
5023
5024 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5025    unary-operator, the corresponding tree code is returned.  */
5026
5027 static enum tree_code
5028 cp_parser_unary_operator (cp_token* token)
5029 {
5030   switch (token->type)
5031     {
5032     case CPP_MULT:
5033       return INDIRECT_REF;
5034
5035     case CPP_AND:
5036       return ADDR_EXPR;
5037
5038     case CPP_PLUS:
5039       return UNARY_PLUS_EXPR;
5040
5041     case CPP_MINUS:
5042       return NEGATE_EXPR;
5043
5044     case CPP_NOT:
5045       return TRUTH_NOT_EXPR;
5046
5047     case CPP_COMPL:
5048       return BIT_NOT_EXPR;
5049
5050     default:
5051       return ERROR_MARK;
5052     }
5053 }
5054
5055 /* Parse a new-expression.
5056
5057    new-expression:
5058      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5059      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5060
5061    Returns a representation of the expression.  */
5062
5063 static tree
5064 cp_parser_new_expression (cp_parser* parser)
5065 {
5066   bool global_scope_p;
5067   tree placement;
5068   tree type;
5069   tree initializer;
5070   tree nelts;
5071
5072   /* Look for the optional `::' operator.  */
5073   global_scope_p
5074     = (cp_parser_global_scope_opt (parser,
5075                                    /*current_scope_valid_p=*/false)
5076        != NULL_TREE);
5077   /* Look for the `new' operator.  */
5078   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5079   /* There's no easy way to tell a new-placement from the
5080      `( type-id )' construct.  */
5081   cp_parser_parse_tentatively (parser);
5082   /* Look for a new-placement.  */
5083   placement = cp_parser_new_placement (parser);
5084   /* If that didn't work out, there's no new-placement.  */
5085   if (!cp_parser_parse_definitely (parser))
5086     placement = NULL_TREE;
5087
5088   /* If the next token is a `(', then we have a parenthesized
5089      type-id.  */
5090   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5091     {
5092       /* Consume the `('.  */
5093       cp_lexer_consume_token (parser->lexer);
5094       /* Parse the type-id.  */
5095       type = cp_parser_type_id (parser);
5096       /* Look for the closing `)'.  */
5097       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5098       /* There should not be a direct-new-declarator in this production,
5099          but GCC used to allowed this, so we check and emit a sensible error
5100          message for this case.  */
5101       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5102         {
5103           error ("array bound forbidden after parenthesized type-id");
5104           inform ("try removing the parentheses around the type-id");
5105           cp_parser_direct_new_declarator (parser);
5106         }
5107       nelts = NULL_TREE;
5108     }
5109   /* Otherwise, there must be a new-type-id.  */
5110   else
5111     type = cp_parser_new_type_id (parser, &nelts);
5112
5113   /* If the next token is a `(', then we have a new-initializer.  */
5114   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5115     initializer = cp_parser_new_initializer (parser);
5116   else
5117     initializer = NULL_TREE;
5118
5119   /* A new-expression may not appear in an integral constant
5120      expression.  */
5121   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5122     return error_mark_node;
5123
5124   /* Create a representation of the new-expression.  */
5125   return build_new (placement, type, nelts, initializer, global_scope_p);
5126 }
5127
5128 /* Parse a new-placement.
5129
5130    new-placement:
5131      ( expression-list )
5132
5133    Returns the same representation as for an expression-list.  */
5134
5135 static tree
5136 cp_parser_new_placement (cp_parser* parser)
5137 {
5138   tree expression_list;
5139
5140   /* Parse the expression-list.  */
5141   expression_list = (cp_parser_parenthesized_expression_list
5142                      (parser, false, /*cast_p=*/false,
5143                       /*non_constant_p=*/NULL));
5144
5145   return expression_list;
5146 }
5147
5148 /* Parse a new-type-id.
5149
5150    new-type-id:
5151      type-specifier-seq new-declarator [opt]
5152
5153    Returns the TYPE allocated.  If the new-type-id indicates an array
5154    type, *NELTS is set to the number of elements in the last array
5155    bound; the TYPE will not include the last array bound.  */
5156
5157 static tree
5158 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5159 {
5160   cp_decl_specifier_seq type_specifier_seq;
5161   cp_declarator *new_declarator;
5162   cp_declarator *declarator;
5163   cp_declarator *outer_declarator;
5164   const char *saved_message;
5165   tree type;
5166
5167   /* The type-specifier sequence must not contain type definitions.
5168      (It cannot contain declarations of new types either, but if they
5169      are not definitions we will catch that because they are not
5170      complete.)  */
5171   saved_message = parser->type_definition_forbidden_message;
5172   parser->type_definition_forbidden_message
5173     = "types may not be defined in a new-type-id";
5174   /* Parse the type-specifier-seq.  */
5175   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5176                                 &type_specifier_seq);
5177   /* Restore the old message.  */
5178   parser->type_definition_forbidden_message = saved_message;
5179   /* Parse the new-declarator.  */
5180   new_declarator = cp_parser_new_declarator_opt (parser);
5181
5182   /* Determine the number of elements in the last array dimension, if
5183      any.  */
5184   *nelts = NULL_TREE;
5185   /* Skip down to the last array dimension.  */
5186   declarator = new_declarator;
5187   outer_declarator = NULL;
5188   while (declarator && (declarator->kind == cdk_pointer
5189                         || declarator->kind == cdk_ptrmem))
5190     {
5191       outer_declarator = declarator;
5192       declarator = declarator->declarator;
5193     }
5194   while (declarator
5195          && declarator->kind == cdk_array
5196          && declarator->declarator
5197          && declarator->declarator->kind == cdk_array)
5198     {
5199       outer_declarator = declarator;
5200       declarator = declarator->declarator;
5201     }
5202
5203   if (declarator && declarator->kind == cdk_array)
5204     {
5205       *nelts = declarator->u.array.bounds;
5206       if (*nelts == error_mark_node)
5207         *nelts = integer_one_node;
5208
5209       if (outer_declarator)
5210         outer_declarator->declarator = declarator->declarator;
5211       else
5212         new_declarator = NULL;
5213     }
5214
5215   type = groktypename (&type_specifier_seq, new_declarator);
5216   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5217     {
5218       *nelts = array_type_nelts_top (type);
5219       type = TREE_TYPE (type);
5220     }
5221   return type;
5222 }
5223
5224 /* Parse an (optional) new-declarator.
5225
5226    new-declarator:
5227      ptr-operator new-declarator [opt]
5228      direct-new-declarator
5229
5230    Returns the declarator.  */
5231
5232 static cp_declarator *
5233 cp_parser_new_declarator_opt (cp_parser* parser)
5234 {
5235   enum tree_code code;
5236   tree type;
5237   cp_cv_quals cv_quals;
5238
5239   /* We don't know if there's a ptr-operator next, or not.  */
5240   cp_parser_parse_tentatively (parser);
5241   /* Look for a ptr-operator.  */
5242   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5243   /* If that worked, look for more new-declarators.  */
5244   if (cp_parser_parse_definitely (parser))
5245     {
5246       cp_declarator *declarator;
5247
5248       /* Parse another optional declarator.  */
5249       declarator = cp_parser_new_declarator_opt (parser);
5250
5251       /* Create the representation of the declarator.  */
5252       if (type)
5253         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5254       else if (code == INDIRECT_REF)
5255         declarator = make_pointer_declarator (cv_quals, declarator);
5256       else
5257         declarator = make_reference_declarator (cv_quals, declarator);
5258
5259       return declarator;
5260     }
5261
5262   /* If the next token is a `[', there is a direct-new-declarator.  */
5263   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5264     return cp_parser_direct_new_declarator (parser);
5265
5266   return NULL;
5267 }
5268
5269 /* Parse a direct-new-declarator.
5270
5271    direct-new-declarator:
5272      [ expression ]
5273      direct-new-declarator [constant-expression]
5274
5275    */
5276
5277 static cp_declarator *
5278 cp_parser_direct_new_declarator (cp_parser* parser)
5279 {
5280   cp_declarator *declarator = NULL;
5281
5282   while (true)
5283     {
5284       tree expression;
5285
5286       /* Look for the opening `['.  */
5287       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5288       /* The first expression is not required to be constant.  */
5289       if (!declarator)
5290         {
5291           expression = cp_parser_expression (parser, /*cast_p=*/false);
5292           /* The standard requires that the expression have integral
5293              type.  DR 74 adds enumeration types.  We believe that the
5294              real intent is that these expressions be handled like the
5295              expression in a `switch' condition, which also allows
5296              classes with a single conversion to integral or
5297              enumeration type.  */
5298           if (!processing_template_decl)
5299             {
5300               expression
5301                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5302                                               expression,
5303                                               /*complain=*/true);
5304               if (!expression)
5305                 {
5306                   error ("expression in new-declarator must have integral "
5307                          "or enumeration type");
5308                   expression = error_mark_node;
5309                 }
5310             }
5311         }
5312       /* But all the other expressions must be.  */
5313       else
5314         expression
5315           = cp_parser_constant_expression (parser,
5316                                            /*allow_non_constant=*/false,
5317                                            NULL);
5318       /* Look for the closing `]'.  */
5319       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5320
5321       /* Add this bound to the declarator.  */
5322       declarator = make_array_declarator (declarator, expression);
5323
5324       /* If the next token is not a `[', then there are no more
5325          bounds.  */
5326       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5327         break;
5328     }
5329
5330   return declarator;
5331 }
5332
5333 /* Parse a new-initializer.
5334
5335    new-initializer:
5336      ( expression-list [opt] )
5337
5338    Returns a representation of the expression-list.  If there is no
5339    expression-list, VOID_ZERO_NODE is returned.  */
5340
5341 static tree
5342 cp_parser_new_initializer (cp_parser* parser)
5343 {
5344   tree expression_list;
5345
5346   expression_list = (cp_parser_parenthesized_expression_list
5347                      (parser, false, /*cast_p=*/false,
5348                       /*non_constant_p=*/NULL));
5349   if (!expression_list)
5350     expression_list = void_zero_node;
5351
5352   return expression_list;
5353 }
5354
5355 /* Parse a delete-expression.
5356
5357    delete-expression:
5358      :: [opt] delete cast-expression
5359      :: [opt] delete [ ] cast-expression
5360
5361    Returns a representation of the expression.  */
5362
5363 static tree
5364 cp_parser_delete_expression (cp_parser* parser)
5365 {
5366   bool global_scope_p;
5367   bool array_p;
5368   tree expression;
5369
5370   /* Look for the optional `::' operator.  */
5371   global_scope_p
5372     = (cp_parser_global_scope_opt (parser,
5373                                    /*current_scope_valid_p=*/false)
5374        != NULL_TREE);
5375   /* Look for the `delete' keyword.  */
5376   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5377   /* See if the array syntax is in use.  */
5378   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5379     {
5380       /* Consume the `[' token.  */
5381       cp_lexer_consume_token (parser->lexer);
5382       /* Look for the `]' token.  */
5383       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5384       /* Remember that this is the `[]' construct.  */
5385       array_p = true;
5386     }
5387   else
5388     array_p = false;
5389
5390   /* Parse the cast-expression.  */
5391   expression = cp_parser_simple_cast_expression (parser);
5392
5393   /* A delete-expression may not appear in an integral constant
5394      expression.  */
5395   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5396     return error_mark_node;
5397
5398   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5399 }
5400
5401 /* Parse a cast-expression.
5402
5403    cast-expression:
5404      unary-expression
5405      ( type-id ) cast-expression
5406
5407    ADDRESS_P is true iff the unary-expression is appearing as the
5408    operand of the `&' operator.   CAST_P is true if this expression is
5409    the target of a cast.
5410
5411    Returns a representation of the expression.  */
5412
5413 static tree
5414 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5415 {
5416   /* If it's a `(', then we might be looking at a cast.  */
5417   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5418     {
5419       tree type = NULL_TREE;
5420       tree expr = NULL_TREE;
5421       bool compound_literal_p;
5422       const char *saved_message;
5423
5424       /* There's no way to know yet whether or not this is a cast.
5425          For example, `(int (3))' is a unary-expression, while `(int)
5426          3' is a cast.  So, we resort to parsing tentatively.  */
5427       cp_parser_parse_tentatively (parser);
5428       /* Types may not be defined in a cast.  */
5429       saved_message = parser->type_definition_forbidden_message;
5430       parser->type_definition_forbidden_message
5431         = "types may not be defined in casts";
5432       /* Consume the `('.  */
5433       cp_lexer_consume_token (parser->lexer);
5434       /* A very tricky bit is that `(struct S) { 3 }' is a
5435          compound-literal (which we permit in C++ as an extension).
5436          But, that construct is not a cast-expression -- it is a
5437          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5438          is legal; if the compound-literal were a cast-expression,
5439          you'd need an extra set of parentheses.)  But, if we parse
5440          the type-id, and it happens to be a class-specifier, then we
5441          will commit to the parse at that point, because we cannot
5442          undo the action that is done when creating a new class.  So,
5443          then we cannot back up and do a postfix-expression.
5444
5445          Therefore, we scan ahead to the closing `)', and check to see
5446          if the token after the `)' is a `{'.  If so, we are not
5447          looking at a cast-expression.
5448
5449          Save tokens so that we can put them back.  */
5450       cp_lexer_save_tokens (parser->lexer);
5451       /* Skip tokens until the next token is a closing parenthesis.
5452          If we find the closing `)', and the next token is a `{', then
5453          we are looking at a compound-literal.  */
5454       compound_literal_p
5455         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5456                                                   /*consume_paren=*/true)
5457            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5458       /* Roll back the tokens we skipped.  */
5459       cp_lexer_rollback_tokens (parser->lexer);
5460       /* If we were looking at a compound-literal, simulate an error
5461          so that the call to cp_parser_parse_definitely below will
5462          fail.  */
5463       if (compound_literal_p)
5464         cp_parser_simulate_error (parser);
5465       else
5466         {
5467           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5468           parser->in_type_id_in_expr_p = true;
5469           /* Look for the type-id.  */
5470           type = cp_parser_type_id (parser);
5471           /* Look for the closing `)'.  */
5472           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5473           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5474         }
5475
5476       /* Restore the saved message.  */
5477       parser->type_definition_forbidden_message = saved_message;
5478
5479       /* If ok so far, parse the dependent expression. We cannot be
5480          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5481          ctor of T, but looks like a cast to function returning T
5482          without a dependent expression.  */
5483       if (!cp_parser_error_occurred (parser))
5484         expr = cp_parser_cast_expression (parser,
5485                                           /*address_p=*/false,
5486                                           /*cast_p=*/true);
5487
5488       if (cp_parser_parse_definitely (parser))
5489         {
5490           /* Warn about old-style casts, if so requested.  */
5491           if (warn_old_style_cast
5492               && !in_system_header
5493               && !VOID_TYPE_P (type)
5494               && current_lang_name != lang_name_c)
5495             warning (OPT_Wold_style_cast, "use of old-style cast");
5496
5497           /* Only type conversions to integral or enumeration types
5498              can be used in constant-expressions.  */
5499           if (!cast_valid_in_integral_constant_expression_p (type)
5500               && (cp_parser_non_integral_constant_expression
5501                   (parser,
5502                    "a cast to a type other than an integral or "
5503                    "enumeration type")))
5504             return error_mark_node;
5505
5506           /* Perform the cast.  */
5507           expr = build_c_cast (type, expr);
5508           return expr;
5509         }
5510     }
5511
5512   /* If we get here, then it's not a cast, so it must be a
5513      unary-expression.  */
5514   return cp_parser_unary_expression (parser, address_p, cast_p);
5515 }
5516
5517 /* Parse a binary expression of the general form:
5518
5519    pm-expression:
5520      cast-expression
5521      pm-expression .* cast-expression
5522      pm-expression ->* cast-expression
5523
5524    multiplicative-expression:
5525      pm-expression
5526      multiplicative-expression * pm-expression
5527      multiplicative-expression / pm-expression
5528      multiplicative-expression % pm-expression
5529
5530    additive-expression:
5531      multiplicative-expression
5532      additive-expression + multiplicative-expression
5533      additive-expression - multiplicative-expression
5534
5535    shift-expression:
5536      additive-expression
5537      shift-expression << additive-expression
5538      shift-expression >> additive-expression
5539
5540    relational-expression:
5541      shift-expression
5542      relational-expression < shift-expression
5543      relational-expression > shift-expression
5544      relational-expression <= shift-expression
5545      relational-expression >= shift-expression
5546
5547   GNU Extension:
5548
5549    relational-expression:
5550      relational-expression <? shift-expression
5551      relational-expression >? shift-expression
5552
5553    equality-expression:
5554      relational-expression
5555      equality-expression == relational-expression
5556      equality-expression != relational-expression
5557
5558    and-expression:
5559      equality-expression
5560      and-expression & equality-expression
5561
5562    exclusive-or-expression:
5563      and-expression
5564      exclusive-or-expression ^ and-expression
5565
5566    inclusive-or-expression:
5567      exclusive-or-expression
5568      inclusive-or-expression | exclusive-or-expression
5569
5570    logical-and-expression:
5571      inclusive-or-expression
5572      logical-and-expression && inclusive-or-expression
5573
5574    logical-or-expression:
5575      logical-and-expression
5576      logical-or-expression || logical-and-expression
5577
5578    All these are implemented with a single function like:
5579
5580    binary-expression:
5581      simple-cast-expression
5582      binary-expression <token> binary-expression
5583
5584    CAST_P is true if this expression is the target of a cast.
5585
5586    The binops_by_token map is used to get the tree codes for each <token> type.
5587    binary-expressions are associated according to a precedence table.  */
5588
5589 #define TOKEN_PRECEDENCE(token) \
5590   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5591    ? PREC_NOT_OPERATOR \
5592    : binops_by_token[token->type].prec)
5593
5594 static tree
5595 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5596 {
5597   cp_parser_expression_stack stack;
5598   cp_parser_expression_stack_entry *sp = &stack[0];
5599   tree lhs, rhs;
5600   cp_token *token;
5601   enum tree_code tree_type;
5602   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5603   bool overloaded_p;
5604
5605   /* Parse the first expression.  */
5606   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5607
5608   for (;;)
5609     {
5610       /* Get an operator token.  */
5611       token = cp_lexer_peek_token (parser->lexer);
5612
5613       new_prec = TOKEN_PRECEDENCE (token);
5614
5615       /* Popping an entry off the stack means we completed a subexpression:
5616          - either we found a token which is not an operator (`>' where it is not
5617            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5618            will happen repeatedly;
5619          - or, we found an operator which has lower priority.  This is the case
5620            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5621            parsing `3 * 4'.  */
5622       if (new_prec <= prec)
5623         {
5624           if (sp == stack)
5625             break;
5626           else
5627             goto pop;
5628         }
5629
5630      get_rhs:
5631       tree_type = binops_by_token[token->type].tree_type;
5632
5633       /* We used the operator token.  */
5634       cp_lexer_consume_token (parser->lexer);
5635
5636       /* Extract another operand.  It may be the RHS of this expression
5637          or the LHS of a new, higher priority expression.  */
5638       rhs = cp_parser_simple_cast_expression (parser);
5639
5640       /* Get another operator token.  Look up its precedence to avoid
5641          building a useless (immediately popped) stack entry for common
5642          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5643       token = cp_lexer_peek_token (parser->lexer);
5644       lookahead_prec = TOKEN_PRECEDENCE (token);
5645       if (lookahead_prec > new_prec)
5646         {
5647           /* ... and prepare to parse the RHS of the new, higher priority
5648              expression.  Since precedence levels on the stack are
5649              monotonically increasing, we do not have to care about
5650              stack overflows.  */
5651           sp->prec = prec;
5652           sp->tree_type = tree_type;
5653           sp->lhs = lhs;
5654           sp++;
5655           lhs = rhs;
5656           prec = new_prec;
5657           new_prec = lookahead_prec;
5658           goto get_rhs;
5659
5660          pop:
5661           /* If the stack is not empty, we have parsed into LHS the right side
5662              (`4' in the example above) of an expression we had suspended.
5663              We can use the information on the stack to recover the LHS (`3')
5664              from the stack together with the tree code (`MULT_EXPR'), and
5665              the precedence of the higher level subexpression
5666              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5667              which will be used to actually build the additive expression.  */
5668           --sp;
5669           prec = sp->prec;
5670           tree_type = sp->tree_type;
5671           rhs = lhs;
5672           lhs = sp->lhs;
5673         }
5674
5675       overloaded_p = false;
5676       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5677
5678       /* If the binary operator required the use of an overloaded operator,
5679          then this expression cannot be an integral constant-expression.
5680          An overloaded operator can be used even if both operands are
5681          otherwise permissible in an integral constant-expression if at
5682          least one of the operands is of enumeration type.  */
5683
5684       if (overloaded_p
5685           && (cp_parser_non_integral_constant_expression
5686               (parser, "calls to overloaded operators")))
5687         return error_mark_node;
5688     }
5689
5690   return lhs;
5691 }
5692
5693
5694 /* Parse the `? expression : assignment-expression' part of a
5695    conditional-expression.  The LOGICAL_OR_EXPR is the
5696    logical-or-expression that started the conditional-expression.
5697    Returns a representation of the entire conditional-expression.
5698
5699    This routine is used by cp_parser_assignment_expression.
5700
5701      ? expression : assignment-expression
5702
5703    GNU Extensions:
5704
5705      ? : assignment-expression */
5706
5707 static tree
5708 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5709 {
5710   tree expr;
5711   tree assignment_expr;
5712
5713   /* Consume the `?' token.  */
5714   cp_lexer_consume_token (parser->lexer);
5715   if (cp_parser_allow_gnu_extensions_p (parser)
5716       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5717     /* Implicit true clause.  */
5718     expr = NULL_TREE;
5719   else
5720     /* Parse the expression.  */
5721     expr = cp_parser_expression (parser, /*cast_p=*/false);
5722
5723   /* The next token should be a `:'.  */
5724   cp_parser_require (parser, CPP_COLON, "`:'");
5725   /* Parse the assignment-expression.  */
5726   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5727
5728   /* Build the conditional-expression.  */
5729   return build_x_conditional_expr (logical_or_expr,
5730                                    expr,
5731                                    assignment_expr);
5732 }
5733
5734 /* Parse an assignment-expression.
5735
5736    assignment-expression:
5737      conditional-expression
5738      logical-or-expression assignment-operator assignment_expression
5739      throw-expression
5740
5741    CAST_P is true if this expression is the target of a cast.
5742
5743    Returns a representation for the expression.  */
5744
5745 static tree
5746 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5747 {
5748   tree expr;
5749
5750   /* If the next token is the `throw' keyword, then we're looking at
5751      a throw-expression.  */
5752   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5753     expr = cp_parser_throw_expression (parser);
5754   /* Otherwise, it must be that we are looking at a
5755      logical-or-expression.  */
5756   else
5757     {
5758       /* Parse the binary expressions (logical-or-expression).  */
5759       expr = cp_parser_binary_expression (parser, cast_p);
5760       /* If the next token is a `?' then we're actually looking at a
5761          conditional-expression.  */
5762       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5763         return cp_parser_question_colon_clause (parser, expr);
5764       else
5765         {
5766           enum tree_code assignment_operator;
5767
5768           /* If it's an assignment-operator, we're using the second
5769              production.  */
5770           assignment_operator
5771             = cp_parser_assignment_operator_opt (parser);
5772           if (assignment_operator != ERROR_MARK)
5773             {
5774               tree rhs;
5775
5776               /* Parse the right-hand side of the assignment.  */
5777               rhs = cp_parser_assignment_expression (parser, cast_p);
5778               /* An assignment may not appear in a
5779                  constant-expression.  */
5780               if (cp_parser_non_integral_constant_expression (parser,
5781                                                               "an assignment"))
5782                 return error_mark_node;
5783               /* Build the assignment expression.  */
5784               expr = build_x_modify_expr (expr,
5785                                           assignment_operator,
5786                                           rhs);
5787             }
5788         }
5789     }
5790
5791   return expr;
5792 }
5793
5794 /* Parse an (optional) assignment-operator.
5795
5796    assignment-operator: one of
5797      = *= /= %= += -= >>= <<= &= ^= |=
5798
5799    GNU Extension:
5800
5801    assignment-operator: one of
5802      <?= >?=
5803
5804    If the next token is an assignment operator, the corresponding tree
5805    code is returned, and the token is consumed.  For example, for
5806    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5807    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5808    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5809    operator, ERROR_MARK is returned.  */
5810
5811 static enum tree_code
5812 cp_parser_assignment_operator_opt (cp_parser* parser)
5813 {
5814   enum tree_code op;
5815   cp_token *token;
5816
5817   /* Peek at the next toen.  */
5818   token = cp_lexer_peek_token (parser->lexer);
5819
5820   switch (token->type)
5821     {
5822     case CPP_EQ:
5823       op = NOP_EXPR;
5824       break;
5825
5826     case CPP_MULT_EQ:
5827       op = MULT_EXPR;
5828       break;
5829
5830     case CPP_DIV_EQ:
5831       op = TRUNC_DIV_EXPR;
5832       break;
5833
5834     case CPP_MOD_EQ:
5835       op = TRUNC_MOD_EXPR;
5836       break;
5837
5838     case CPP_PLUS_EQ:
5839       op = PLUS_EXPR;
5840       break;
5841
5842     case CPP_MINUS_EQ:
5843       op = MINUS_EXPR;
5844       break;
5845
5846     case CPP_RSHIFT_EQ:
5847       op = RSHIFT_EXPR;
5848       break;
5849
5850     case CPP_LSHIFT_EQ:
5851       op = LSHIFT_EXPR;
5852       break;
5853
5854     case CPP_AND_EQ:
5855       op = BIT_AND_EXPR;
5856       break;
5857
5858     case CPP_XOR_EQ:
5859       op = BIT_XOR_EXPR;
5860       break;
5861
5862     case CPP_OR_EQ:
5863       op = BIT_IOR_EXPR;
5864       break;
5865
5866     default:
5867       /* Nothing else is an assignment operator.  */
5868       op = ERROR_MARK;
5869     }
5870
5871   /* If it was an assignment operator, consume it.  */
5872   if (op != ERROR_MARK)
5873     cp_lexer_consume_token (parser->lexer);
5874
5875   return op;
5876 }
5877
5878 /* Parse an expression.
5879
5880    expression:
5881      assignment-expression
5882      expression , assignment-expression
5883
5884    CAST_P is true if this expression is the target of a cast.
5885
5886    Returns a representation of the expression.  */
5887
5888 static tree
5889 cp_parser_expression (cp_parser* parser, bool cast_p)
5890 {
5891   tree expression = NULL_TREE;
5892
5893   while (true)
5894     {
5895       tree assignment_expression;
5896
5897       /* Parse the next assignment-expression.  */
5898       assignment_expression
5899         = cp_parser_assignment_expression (parser, cast_p);
5900       /* If this is the first assignment-expression, we can just
5901          save it away.  */
5902       if (!expression)
5903         expression = assignment_expression;
5904       else
5905         expression = build_x_compound_expr (expression,
5906                                             assignment_expression);
5907       /* If the next token is not a comma, then we are done with the
5908          expression.  */
5909       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5910         break;
5911       /* Consume the `,'.  */
5912       cp_lexer_consume_token (parser->lexer);
5913       /* A comma operator cannot appear in a constant-expression.  */
5914       if (cp_parser_non_integral_constant_expression (parser,
5915                                                       "a comma operator"))
5916         expression = error_mark_node;
5917     }
5918
5919   return expression;
5920 }
5921
5922 /* Parse a constant-expression.
5923
5924    constant-expression:
5925      conditional-expression
5926
5927   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5928   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5929   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5930   is false, NON_CONSTANT_P should be NULL.  */
5931
5932 static tree
5933 cp_parser_constant_expression (cp_parser* parser,
5934                                bool allow_non_constant_p,
5935                                bool *non_constant_p)
5936 {
5937   bool saved_integral_constant_expression_p;
5938   bool saved_allow_non_integral_constant_expression_p;
5939   bool saved_non_integral_constant_expression_p;
5940   tree expression;
5941
5942   /* It might seem that we could simply parse the
5943      conditional-expression, and then check to see if it were
5944      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5945      one that the compiler can figure out is constant, possibly after
5946      doing some simplifications or optimizations.  The standard has a
5947      precise definition of constant-expression, and we must honor
5948      that, even though it is somewhat more restrictive.
5949
5950      For example:
5951
5952        int i[(2, 3)];
5953
5954      is not a legal declaration, because `(2, 3)' is not a
5955      constant-expression.  The `,' operator is forbidden in a
5956      constant-expression.  However, GCC's constant-folding machinery
5957      will fold this operation to an INTEGER_CST for `3'.  */
5958
5959   /* Save the old settings.  */
5960   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5961   saved_allow_non_integral_constant_expression_p
5962     = parser->allow_non_integral_constant_expression_p;
5963   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5964   /* We are now parsing a constant-expression.  */
5965   parser->integral_constant_expression_p = true;
5966   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5967   parser->non_integral_constant_expression_p = false;
5968   /* Although the grammar says "conditional-expression", we parse an
5969      "assignment-expression", which also permits "throw-expression"
5970      and the use of assignment operators.  In the case that
5971      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5972      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5973      actually essential that we look for an assignment-expression.
5974      For example, cp_parser_initializer_clauses uses this function to
5975      determine whether a particular assignment-expression is in fact
5976      constant.  */
5977   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5978   /* Restore the old settings.  */
5979   parser->integral_constant_expression_p
5980     = saved_integral_constant_expression_p;
5981   parser->allow_non_integral_constant_expression_p
5982     = saved_allow_non_integral_constant_expression_p;
5983   if (allow_non_constant_p)
5984     *non_constant_p = parser->non_integral_constant_expression_p;
5985   else if (parser->non_integral_constant_expression_p)
5986     expression = error_mark_node;
5987   parser->non_integral_constant_expression_p
5988     = saved_non_integral_constant_expression_p;
5989
5990   return expression;
5991 }
5992
5993 /* Parse __builtin_offsetof.
5994
5995    offsetof-expression:
5996      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5997
5998    offsetof-member-designator:
5999      id-expression
6000      | offsetof-member-designator "." id-expression
6001      | offsetof-member-designator "[" expression "]"  */
6002
6003 static tree
6004 cp_parser_builtin_offsetof (cp_parser *parser)
6005 {
6006   int save_ice_p, save_non_ice_p;
6007   tree type, expr;
6008   cp_id_kind dummy;
6009
6010   /* We're about to accept non-integral-constant things, but will
6011      definitely yield an integral constant expression.  Save and
6012      restore these values around our local parsing.  */
6013   save_ice_p = parser->integral_constant_expression_p;
6014   save_non_ice_p = parser->non_integral_constant_expression_p;
6015
6016   /* Consume the "__builtin_offsetof" token.  */
6017   cp_lexer_consume_token (parser->lexer);
6018   /* Consume the opening `('.  */
6019   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6020   /* Parse the type-id.  */
6021   type = cp_parser_type_id (parser);
6022   /* Look for the `,'.  */
6023   cp_parser_require (parser, CPP_COMMA, "`,'");
6024
6025   /* Build the (type *)null that begins the traditional offsetof macro.  */
6026   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6027
6028   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6029   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6030                                                  true, &dummy);
6031   while (true)
6032     {
6033       cp_token *token = cp_lexer_peek_token (parser->lexer);
6034       switch (token->type)
6035         {
6036         case CPP_OPEN_SQUARE:
6037           /* offsetof-member-designator "[" expression "]" */
6038           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6039           break;
6040
6041         case CPP_DOT:
6042           /* offsetof-member-designator "." identifier */
6043           cp_lexer_consume_token (parser->lexer);
6044           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6045                                                          true, &dummy);
6046           break;
6047
6048         case CPP_CLOSE_PAREN:
6049           /* Consume the ")" token.  */
6050           cp_lexer_consume_token (parser->lexer);
6051           goto success;
6052
6053         default:
6054           /* Error.  We know the following require will fail, but
6055              that gives the proper error message.  */
6056           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6057           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6058           expr = error_mark_node;
6059           goto failure;
6060         }
6061     }
6062
6063  success:
6064   /* If we're processing a template, we can't finish the semantics yet.
6065      Otherwise we can fold the entire expression now.  */
6066   if (processing_template_decl)
6067     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6068   else
6069     expr = finish_offsetof (expr);
6070
6071  failure:
6072   parser->integral_constant_expression_p = save_ice_p;
6073   parser->non_integral_constant_expression_p = save_non_ice_p;
6074
6075   return expr;
6076 }
6077
6078 /* Statements [gram.stmt.stmt]  */
6079
6080 /* Parse a statement.
6081
6082    statement:
6083      labeled-statement
6084      expression-statement
6085      compound-statement
6086      selection-statement
6087      iteration-statement
6088      jump-statement
6089      declaration-statement
6090      try-block
6091
6092   IN_COMPOUND is true when the statement is nested inside a
6093   cp_parser_compound_statement; this matters for certain pragmas.  */
6094
6095 static void
6096 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6097                      bool in_compound)
6098 {
6099   tree statement;
6100   cp_token *token;
6101   location_t statement_location;
6102
6103  restart:
6104   /* There is no statement yet.  */
6105   statement = NULL_TREE;
6106   /* Peek at the next token.  */
6107   token = cp_lexer_peek_token (parser->lexer);
6108   /* Remember the location of the first token in the statement.  */
6109   statement_location = token->location;
6110   /* If this is a keyword, then that will often determine what kind of
6111      statement we have.  */
6112   if (token->type == CPP_KEYWORD)
6113     {
6114       enum rid keyword = token->keyword;
6115
6116       switch (keyword)
6117         {
6118         case RID_CASE:
6119         case RID_DEFAULT:
6120           statement = cp_parser_labeled_statement (parser, in_statement_expr,
6121                                                    in_compound);
6122           break;
6123
6124         case RID_IF:
6125         case RID_SWITCH:
6126           statement = cp_parser_selection_statement (parser);
6127           break;
6128
6129         case RID_WHILE:
6130         case RID_DO:
6131         case RID_FOR:
6132           statement = cp_parser_iteration_statement (parser);
6133           break;
6134
6135         case RID_BREAK:
6136         case RID_CONTINUE:
6137         case RID_RETURN:
6138         case RID_GOTO:
6139           statement = cp_parser_jump_statement (parser);
6140           break;
6141
6142           /* Objective-C++ exception-handling constructs.  */
6143         case RID_AT_TRY:
6144         case RID_AT_CATCH:
6145         case RID_AT_FINALLY:
6146         case RID_AT_SYNCHRONIZED:
6147         case RID_AT_THROW:
6148           statement = cp_parser_objc_statement (parser);
6149           break;
6150
6151         case RID_TRY:
6152           statement = cp_parser_try_block (parser);
6153           break;
6154
6155         default:
6156           /* It might be a keyword like `int' that can start a
6157              declaration-statement.  */
6158           break;
6159         }
6160     }
6161   else if (token->type == CPP_NAME)
6162     {
6163       /* If the next token is a `:', then we are looking at a
6164          labeled-statement.  */
6165       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6166       if (token->type == CPP_COLON)
6167         statement = cp_parser_labeled_statement (parser, in_statement_expr,
6168                                                  in_compound);
6169     }
6170   /* Anything that starts with a `{' must be a compound-statement.  */
6171   else if (token->type == CPP_OPEN_BRACE)
6172     statement = cp_parser_compound_statement (parser, NULL, false);
6173   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6174      a statement all its own.  */
6175   else if (token->type == CPP_PRAGMA)
6176     {
6177       /* Only certain OpenMP pragmas are attached to statements, and thus
6178          are considered statements themselves.  All others are not.  In
6179          the context of a compound, accept the pragma as a "statement" and
6180          return so that we can check for a close brace.  Otherwise we
6181          require a real statement and must go back and read one.  */
6182       if (in_compound)
6183         cp_parser_pragma (parser, pragma_compound);
6184       else if (!cp_parser_pragma (parser, pragma_stmt))
6185         goto restart;
6186       return;
6187     }
6188   else if (token->type == CPP_EOF)
6189     {
6190       cp_parser_error (parser, "expected statement");
6191       return;
6192     }
6193
6194   /* Everything else must be a declaration-statement or an
6195      expression-statement.  Try for the declaration-statement
6196      first, unless we are looking at a `;', in which case we know that
6197      we have an expression-statement.  */
6198   if (!statement)
6199     {
6200       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6201         {
6202           cp_parser_parse_tentatively (parser);
6203           /* Try to parse the declaration-statement.  */
6204           cp_parser_declaration_statement (parser);
6205           /* If that worked, we're done.  */
6206           if (cp_parser_parse_definitely (parser))
6207             return;
6208         }
6209       /* Look for an expression-statement instead.  */
6210       statement = cp_parser_expression_statement (parser, in_statement_expr);
6211     }
6212
6213   /* Set the line number for the statement.  */
6214   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6215     SET_EXPR_LOCATION (statement, statement_location);
6216 }
6217
6218 /* Parse a labeled-statement.
6219
6220    labeled-statement:
6221      identifier : statement
6222      case constant-expression : statement
6223      default : statement
6224
6225    GNU Extension:
6226
6227    labeled-statement:
6228      case constant-expression ... constant-expression : statement
6229
6230    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6231    For an ordinary label, returns a LABEL_EXPR.
6232
6233    IN_COMPOUND is as for cp_parser_statement: true when we're nested
6234    inside a compound.  */
6235
6236 static tree
6237 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6238                              bool in_compound)
6239 {
6240   cp_token *token;
6241   tree statement = error_mark_node;
6242
6243   /* The next token should be an identifier.  */
6244   token = cp_lexer_peek_token (parser->lexer);
6245   if (token->type != CPP_NAME
6246       && token->type != CPP_KEYWORD)
6247     {
6248       cp_parser_error (parser, "expected labeled-statement");
6249       return error_mark_node;
6250     }
6251
6252   switch (token->keyword)
6253     {
6254     case RID_CASE:
6255       {
6256         tree expr, expr_hi;
6257         cp_token *ellipsis;
6258
6259         /* Consume the `case' token.  */
6260         cp_lexer_consume_token (parser->lexer);
6261         /* Parse the constant-expression.  */
6262         expr = cp_parser_constant_expression (parser,
6263                                               /*allow_non_constant_p=*/false,
6264                                               NULL);
6265
6266         ellipsis = cp_lexer_peek_token (parser->lexer);
6267         if (ellipsis->type == CPP_ELLIPSIS)
6268           {
6269             /* Consume the `...' token.  */
6270             cp_lexer_consume_token (parser->lexer);
6271             expr_hi =
6272               cp_parser_constant_expression (parser,
6273                                              /*allow_non_constant_p=*/false,
6274                                              NULL);
6275             /* We don't need to emit warnings here, as the common code
6276                will do this for us.  */
6277           }
6278         else
6279           expr_hi = NULL_TREE;
6280
6281         if (parser->in_switch_statement_p)
6282           statement = finish_case_label (expr, expr_hi);
6283         else
6284           error ("case label %qE not within a switch statement", expr);
6285       }
6286       break;
6287
6288     case RID_DEFAULT:
6289       /* Consume the `default' token.  */
6290       cp_lexer_consume_token (parser->lexer);
6291
6292       if (parser->in_switch_statement_p)
6293         statement = finish_case_label (NULL_TREE, NULL_TREE);
6294       else
6295         error ("case label not within a switch statement");
6296       break;
6297
6298     default:
6299       /* Anything else must be an ordinary label.  */
6300       statement = finish_label_stmt (cp_parser_identifier (parser));
6301       break;
6302     }
6303
6304   /* Require the `:' token.  */
6305   cp_parser_require (parser, CPP_COLON, "`:'");
6306   /* Parse the labeled statement.  */
6307   cp_parser_statement (parser, in_statement_expr, in_compound);
6308
6309   /* Return the label, in the case of a `case' or `default' label.  */
6310   return statement;
6311 }
6312
6313 /* Parse an expression-statement.
6314
6315    expression-statement:
6316      expression [opt] ;
6317
6318    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6319    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6320    indicates whether this expression-statement is part of an
6321    expression statement.  */
6322
6323 static tree
6324 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6325 {
6326   tree statement = NULL_TREE;
6327
6328   /* If the next token is a ';', then there is no expression
6329      statement.  */
6330   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6331     statement = cp_parser_expression (parser, /*cast_p=*/false);
6332
6333   /* Consume the final `;'.  */
6334   cp_parser_consume_semicolon_at_end_of_statement (parser);
6335
6336   if (in_statement_expr
6337       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6338     /* This is the final expression statement of a statement
6339        expression.  */
6340     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6341   else if (statement)
6342     statement = finish_expr_stmt (statement);
6343   else
6344     finish_stmt ();
6345
6346   return statement;
6347 }
6348
6349 /* Parse a compound-statement.
6350
6351    compound-statement:
6352      { statement-seq [opt] }
6353
6354    Returns a tree representing the statement.  */
6355
6356 static tree
6357 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6358                               bool in_try)
6359 {
6360   tree compound_stmt;
6361
6362   /* Consume the `{'.  */
6363   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6364     return error_mark_node;
6365   /* Begin the compound-statement.  */
6366   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6367   /* Parse an (optional) statement-seq.  */
6368   cp_parser_statement_seq_opt (parser, in_statement_expr);
6369   /* Finish the compound-statement.  */
6370   finish_compound_stmt (compound_stmt);
6371   /* Consume the `}'.  */
6372   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6373
6374   return compound_stmt;
6375 }
6376
6377 /* Parse an (optional) statement-seq.
6378
6379    statement-seq:
6380      statement
6381      statement-seq [opt] statement  */
6382
6383 static void
6384 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6385 {
6386   /* Scan statements until there aren't any more.  */
6387   while (true)
6388     {
6389       cp_token *token = cp_lexer_peek_token (parser->lexer);
6390
6391       /* If we're looking at a `}', then we've run out of statements.  */
6392       if (token->type == CPP_CLOSE_BRACE
6393           || token->type == CPP_EOF
6394           || token->type == CPP_PRAGMA_EOL)
6395         break;
6396
6397       /* Parse the statement.  */
6398       cp_parser_statement (parser, in_statement_expr, true);
6399     }
6400 }
6401
6402 /* Parse a selection-statement.
6403
6404    selection-statement:
6405      if ( condition ) statement
6406      if ( condition ) statement else statement
6407      switch ( condition ) statement
6408
6409    Returns the new IF_STMT or SWITCH_STMT.  */
6410
6411 static tree
6412 cp_parser_selection_statement (cp_parser* parser)
6413 {
6414   cp_token *token;
6415   enum rid keyword;
6416
6417   /* Peek at the next token.  */
6418   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6419
6420   /* See what kind of keyword it is.  */
6421   keyword = token->keyword;
6422   switch (keyword)
6423     {
6424     case RID_IF:
6425     case RID_SWITCH:
6426       {
6427         tree statement;
6428         tree condition;
6429
6430         /* Look for the `('.  */
6431         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6432           {
6433             cp_parser_skip_to_end_of_statement (parser);
6434             return error_mark_node;
6435           }
6436
6437         /* Begin the selection-statement.  */
6438         if (keyword == RID_IF)
6439           statement = begin_if_stmt ();
6440         else
6441           statement = begin_switch_stmt ();
6442
6443         /* Parse the condition.  */
6444         condition = cp_parser_condition (parser);
6445         /* Look for the `)'.  */
6446         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6447           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6448                                                  /*consume_paren=*/true);
6449
6450         if (keyword == RID_IF)
6451           {
6452             /* Add the condition.  */
6453             finish_if_stmt_cond (condition, statement);
6454
6455             /* Parse the then-clause.  */
6456             cp_parser_implicitly_scoped_statement (parser);
6457             finish_then_clause (statement);
6458
6459             /* If the next token is `else', parse the else-clause.  */
6460             if (cp_lexer_next_token_is_keyword (parser->lexer,
6461                                                 RID_ELSE))
6462               {
6463                 /* Consume the `else' keyword.  */
6464                 cp_lexer_consume_token (parser->lexer);
6465                 begin_else_clause (statement);
6466                 /* Parse the else-clause.  */
6467                 cp_parser_implicitly_scoped_statement (parser);
6468                 finish_else_clause (statement);
6469               }
6470
6471             /* Now we're all done with the if-statement.  */
6472             finish_if_stmt (statement);
6473           }
6474         else
6475           {
6476             bool in_switch_statement_p;
6477             unsigned char in_statement;
6478
6479             /* Add the condition.  */
6480             finish_switch_cond (condition, statement);
6481
6482             /* Parse the body of the switch-statement.  */
6483             in_switch_statement_p = parser->in_switch_statement_p;
6484             in_statement = parser->in_statement;
6485             parser->in_switch_statement_p = true;
6486             parser->in_statement |= IN_SWITCH_STMT;
6487             cp_parser_implicitly_scoped_statement (parser);
6488             parser->in_switch_statement_p = in_switch_statement_p;
6489             parser->in_statement = in_statement;
6490
6491             /* Now we're all done with the switch-statement.  */
6492             finish_switch_stmt (statement);
6493           }
6494
6495         return statement;
6496       }
6497       break;
6498
6499     default:
6500       cp_parser_error (parser, "expected selection-statement");
6501       return error_mark_node;
6502     }
6503 }
6504
6505 /* Parse a condition.
6506
6507    condition:
6508      expression
6509      type-specifier-seq declarator = assignment-expression
6510
6511    GNU Extension:
6512
6513    condition:
6514      type-specifier-seq declarator asm-specification [opt]
6515        attributes [opt] = assignment-expression
6516
6517    Returns the expression that should be tested.  */
6518
6519 static tree
6520 cp_parser_condition (cp_parser* parser)
6521 {
6522   cp_decl_specifier_seq type_specifiers;
6523   const char *saved_message;
6524
6525   /* Try the declaration first.  */
6526   cp_parser_parse_tentatively (parser);
6527   /* New types are not allowed in the type-specifier-seq for a
6528      condition.  */
6529   saved_message = parser->type_definition_forbidden_message;
6530   parser->type_definition_forbidden_message
6531     = "types may not be defined in conditions";
6532   /* Parse the type-specifier-seq.  */
6533   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6534                                 &type_specifiers);
6535   /* Restore the saved message.  */
6536   parser->type_definition_forbidden_message = saved_message;
6537   /* If all is well, we might be looking at a declaration.  */
6538   if (!cp_parser_error_occurred (parser))
6539     {
6540       tree decl;
6541       tree asm_specification;
6542       tree attributes;
6543       cp_declarator *declarator;
6544       tree initializer = NULL_TREE;
6545
6546       /* Parse the declarator.  */
6547       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6548                                          /*ctor_dtor_or_conv_p=*/NULL,
6549                                          /*parenthesized_p=*/NULL,
6550                                          /*member_p=*/false);
6551       /* Parse the attributes.  */
6552       attributes = cp_parser_attributes_opt (parser);
6553       /* Parse the asm-specification.  */
6554       asm_specification = cp_parser_asm_specification_opt (parser);
6555       /* If the next token is not an `=', then we might still be
6556          looking at an expression.  For example:
6557
6558            if (A(a).x)
6559
6560          looks like a decl-specifier-seq and a declarator -- but then
6561          there is no `=', so this is an expression.  */
6562       cp_parser_require (parser, CPP_EQ, "`='");
6563       /* If we did see an `=', then we are looking at a declaration
6564          for sure.  */
6565       if (cp_parser_parse_definitely (parser))
6566         {
6567           tree pushed_scope;
6568           bool non_constant_p;
6569
6570           /* Create the declaration.  */
6571           decl = start_decl (declarator, &type_specifiers,
6572                              /*initialized_p=*/true,
6573                              attributes, /*prefix_attributes=*/NULL_TREE,
6574                              &pushed_scope);
6575           /* Parse the assignment-expression.  */
6576           initializer
6577             = cp_parser_constant_expression (parser,
6578                                              /*allow_non_constant_p=*/true,
6579                                              &non_constant_p);
6580           if (!non_constant_p)
6581             initializer = fold_non_dependent_expr (initializer);
6582
6583           /* Process the initializer.  */
6584           cp_finish_decl (decl,
6585                           initializer, !non_constant_p,
6586                           asm_specification,
6587                           LOOKUP_ONLYCONVERTING);
6588
6589           if (pushed_scope)
6590             pop_scope (pushed_scope);
6591
6592           return convert_from_reference (decl);
6593         }
6594     }
6595   /* If we didn't even get past the declarator successfully, we are
6596      definitely not looking at a declaration.  */
6597   else
6598     cp_parser_abort_tentative_parse (parser);
6599
6600   /* Otherwise, we are looking at an expression.  */
6601   return cp_parser_expression (parser, /*cast_p=*/false);
6602 }
6603
6604 /* Parse an iteration-statement.
6605
6606    iteration-statement:
6607      while ( condition ) statement
6608      do statement while ( expression ) ;
6609      for ( for-init-statement condition [opt] ; expression [opt] )
6610        statement
6611
6612    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6613
6614 static tree
6615 cp_parser_iteration_statement (cp_parser* parser)
6616 {
6617   cp_token *token;
6618   enum rid keyword;
6619   tree statement;
6620   unsigned char in_statement;
6621
6622   /* Peek at the next token.  */
6623   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6624   if (!token)
6625     return error_mark_node;
6626
6627   /* Remember whether or not we are already within an iteration
6628      statement.  */
6629   in_statement = parser->in_statement;
6630
6631   /* See what kind of keyword it is.  */
6632   keyword = token->keyword;
6633   switch (keyword)
6634     {
6635     case RID_WHILE:
6636       {
6637         tree condition;
6638
6639         /* Begin the while-statement.  */
6640         statement = begin_while_stmt ();
6641         /* Look for the `('.  */
6642         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6643         /* Parse the condition.  */
6644         condition = cp_parser_condition (parser);
6645         finish_while_stmt_cond (condition, statement);
6646         /* Look for the `)'.  */
6647         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6648         /* Parse the dependent statement.  */
6649         parser->in_statement = IN_ITERATION_STMT;
6650         cp_parser_already_scoped_statement (parser);
6651         parser->in_statement = in_statement;
6652         /* We're done with the while-statement.  */
6653         finish_while_stmt (statement);
6654       }
6655       break;
6656
6657     case RID_DO:
6658       {
6659         tree expression;
6660
6661         /* Begin the do-statement.  */
6662         statement = begin_do_stmt ();
6663         /* Parse the body of the do-statement.  */
6664         parser->in_statement = IN_ITERATION_STMT;
6665         cp_parser_implicitly_scoped_statement (parser);
6666         parser->in_statement = in_statement;
6667         finish_do_body (statement);
6668         /* Look for the `while' keyword.  */
6669         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6670         /* Look for the `('.  */
6671         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6672         /* Parse the expression.  */
6673         expression = cp_parser_expression (parser, /*cast_p=*/false);
6674         /* We're done with the do-statement.  */
6675         finish_do_stmt (expression, statement);
6676         /* Look for the `)'.  */
6677         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6678         /* Look for the `;'.  */
6679         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6680       }
6681       break;
6682
6683     case RID_FOR:
6684       {
6685         tree condition = NULL_TREE;
6686         tree expression = NULL_TREE;
6687
6688         /* Begin the for-statement.  */
6689         statement = begin_for_stmt ();
6690         /* Look for the `('.  */
6691         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6692         /* Parse the initialization.  */
6693         cp_parser_for_init_statement (parser);
6694         finish_for_init_stmt (statement);
6695
6696         /* If there's a condition, process it.  */
6697         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6698           condition = cp_parser_condition (parser);
6699         finish_for_cond (condition, statement);
6700         /* Look for the `;'.  */
6701         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6702
6703         /* If there's an expression, process it.  */
6704         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6705           expression = cp_parser_expression (parser, /*cast_p=*/false);
6706         finish_for_expr (expression, statement);
6707         /* Look for the `)'.  */
6708         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6709
6710         /* Parse the body of the for-statement.  */
6711         parser->in_statement = IN_ITERATION_STMT;
6712         cp_parser_already_scoped_statement (parser);
6713         parser->in_statement = in_statement;
6714
6715         /* We're done with the for-statement.  */
6716         finish_for_stmt (statement);
6717       }
6718       break;
6719
6720     default:
6721       cp_parser_error (parser, "expected iteration-statement");
6722       statement = error_mark_node;
6723       break;
6724     }
6725
6726   return statement;
6727 }
6728
6729 /* Parse a for-init-statement.
6730
6731    for-init-statement:
6732      expression-statement
6733      simple-declaration  */
6734
6735 static void
6736 cp_parser_for_init_statement (cp_parser* parser)
6737 {
6738   /* If the next token is a `;', then we have an empty
6739      expression-statement.  Grammatically, this is also a
6740      simple-declaration, but an invalid one, because it does not
6741      declare anything.  Therefore, if we did not handle this case
6742      specially, we would issue an error message about an invalid
6743      declaration.  */
6744   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6745     {
6746       /* We're going to speculatively look for a declaration, falling back
6747          to an expression, if necessary.  */
6748       cp_parser_parse_tentatively (parser);
6749       /* Parse the declaration.  */
6750       cp_parser_simple_declaration (parser,
6751                                     /*function_definition_allowed_p=*/false);
6752       /* If the tentative parse failed, then we shall need to look for an
6753          expression-statement.  */
6754       if (cp_parser_parse_definitely (parser))
6755         return;
6756     }
6757
6758   cp_parser_expression_statement (parser, false);
6759 }
6760
6761 /* Parse a jump-statement.
6762
6763    jump-statement:
6764      break ;
6765      continue ;
6766      return expression [opt] ;
6767      goto identifier ;
6768
6769    GNU extension:
6770
6771    jump-statement:
6772      goto * expression ;
6773
6774    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6775
6776 static tree
6777 cp_parser_jump_statement (cp_parser* parser)
6778 {
6779   tree statement = error_mark_node;
6780   cp_token *token;
6781   enum rid keyword;
6782
6783   /* Peek at the next token.  */
6784   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6785   if (!token)
6786     return error_mark_node;
6787
6788   /* See what kind of keyword it is.  */
6789   keyword = token->keyword;
6790   switch (keyword)
6791     {
6792     case RID_BREAK:
6793       switch (parser->in_statement)
6794         {
6795         case 0:
6796           error ("break statement not within loop or switch");
6797           break;
6798         default:
6799           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6800                       || parser->in_statement == IN_ITERATION_STMT);
6801           statement = finish_break_stmt ();
6802           break;
6803         case IN_OMP_BLOCK:
6804           error ("invalid exit from OpenMP structured block");
6805           break;
6806         case IN_OMP_FOR:
6807           error ("break statement used with OpenMP for loop");
6808           break;
6809         }
6810       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6811       break;
6812
6813     case RID_CONTINUE:
6814       switch (parser->in_statement & ~IN_SWITCH_STMT)
6815         {
6816         case 0:
6817           error ("continue statement not within a loop");
6818           break;
6819         case IN_ITERATION_STMT:
6820         case IN_OMP_FOR:
6821           statement = finish_continue_stmt ();
6822           break;
6823         case IN_OMP_BLOCK:
6824           error ("invalid exit from OpenMP structured block");
6825           break;
6826         default:
6827           gcc_unreachable ();
6828         }
6829       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6830       break;
6831
6832     case RID_RETURN:
6833       {
6834         tree expr;
6835
6836         /* If the next token is a `;', then there is no
6837            expression.  */
6838         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6839           expr = cp_parser_expression (parser, /*cast_p=*/false);
6840         else
6841           expr = NULL_TREE;
6842         /* Build the return-statement.  */
6843         statement = finish_return_stmt (expr);
6844         /* Look for the final `;'.  */
6845         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6846       }
6847       break;
6848
6849     case RID_GOTO:
6850       /* Create the goto-statement.  */
6851       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6852         {
6853           /* Issue a warning about this use of a GNU extension.  */
6854           if (pedantic)
6855             pedwarn ("ISO C++ forbids computed gotos");
6856           /* Consume the '*' token.  */
6857           cp_lexer_consume_token (parser->lexer);
6858           /* Parse the dependent expression.  */
6859           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6860         }
6861       else
6862         finish_goto_stmt (cp_parser_identifier (parser));
6863       /* Look for the final `;'.  */
6864       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6865       break;
6866
6867     default:
6868       cp_parser_error (parser, "expected jump-statement");
6869       break;
6870     }
6871
6872   return statement;
6873 }
6874
6875 /* Parse a declaration-statement.
6876
6877    declaration-statement:
6878      block-declaration  */
6879
6880 static void
6881 cp_parser_declaration_statement (cp_parser* parser)
6882 {
6883   void *p;
6884
6885   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6886   p = obstack_alloc (&declarator_obstack, 0);
6887
6888  /* Parse the block-declaration.  */
6889   cp_parser_block_declaration (parser, /*statement_p=*/true);
6890
6891   /* Free any declarators allocated.  */
6892   obstack_free (&declarator_obstack, p);
6893
6894   /* Finish off the statement.  */
6895   finish_stmt ();
6896 }
6897
6898 /* Some dependent statements (like `if (cond) statement'), are
6899    implicitly in their own scope.  In other words, if the statement is
6900    a single statement (as opposed to a compound-statement), it is
6901    none-the-less treated as if it were enclosed in braces.  Any
6902    declarations appearing in the dependent statement are out of scope
6903    after control passes that point.  This function parses a statement,
6904    but ensures that is in its own scope, even if it is not a
6905    compound-statement.
6906
6907    Returns the new statement.  */
6908
6909 static tree
6910 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6911 {
6912   tree statement;
6913
6914   /* Mark if () ; with a special NOP_EXPR.  */
6915   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6916     {
6917       cp_lexer_consume_token (parser->lexer);
6918       statement = add_stmt (build_empty_stmt ());
6919     }
6920   /* if a compound is opened, we simply parse the statement directly.  */
6921   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6922     statement = cp_parser_compound_statement (parser, NULL, false);
6923   /* If the token is not a `{', then we must take special action.  */
6924   else
6925     {
6926       /* Create a compound-statement.  */
6927       statement = begin_compound_stmt (0);
6928       /* Parse the dependent-statement.  */
6929       cp_parser_statement (parser, NULL_TREE, false);
6930       /* Finish the dummy compound-statement.  */
6931       finish_compound_stmt (statement);
6932     }
6933
6934   /* Return the statement.  */
6935   return statement;
6936 }
6937
6938 /* For some dependent statements (like `while (cond) statement'), we
6939    have already created a scope.  Therefore, even if the dependent
6940    statement is a compound-statement, we do not want to create another
6941    scope.  */
6942
6943 static void
6944 cp_parser_already_scoped_statement (cp_parser* parser)
6945 {
6946   /* If the token is a `{', then we must take special action.  */
6947   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6948     cp_parser_statement (parser, NULL_TREE, false);
6949   else
6950     {
6951       /* Avoid calling cp_parser_compound_statement, so that we
6952          don't create a new scope.  Do everything else by hand.  */
6953       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6954       cp_parser_statement_seq_opt (parser, NULL_TREE);
6955       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6956     }
6957 }
6958
6959 /* Declarations [gram.dcl.dcl] */
6960
6961 /* Parse an optional declaration-sequence.
6962
6963    declaration-seq:
6964      declaration
6965      declaration-seq declaration  */
6966
6967 static void
6968 cp_parser_declaration_seq_opt (cp_parser* parser)
6969 {
6970   while (true)
6971     {
6972       cp_token *token;
6973
6974       token = cp_lexer_peek_token (parser->lexer);
6975
6976       if (token->type == CPP_CLOSE_BRACE
6977           || token->type == CPP_EOF
6978           || token->type == CPP_PRAGMA_EOL)
6979         break;
6980
6981       if (token->type == CPP_SEMICOLON)
6982         {
6983           /* A declaration consisting of a single semicolon is
6984              invalid.  Allow it unless we're being pedantic.  */
6985           cp_lexer_consume_token (parser->lexer);
6986           if (pedantic && !in_system_header)
6987             pedwarn ("extra %<;%>");
6988           continue;
6989         }
6990
6991       /* If we're entering or exiting a region that's implicitly
6992          extern "C", modify the lang context appropriately.  */
6993       if (!parser->implicit_extern_c && token->implicit_extern_c)
6994         {
6995           push_lang_context (lang_name_c);
6996           parser->implicit_extern_c = true;
6997         }
6998       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6999         {
7000           pop_lang_context ();
7001           parser->implicit_extern_c = false;
7002         }
7003
7004       if (token->type == CPP_PRAGMA)
7005         {
7006           /* A top-level declaration can consist solely of a #pragma.
7007              A nested declaration cannot, so this is done here and not
7008              in cp_parser_declaration.  (A #pragma at block scope is
7009              handled in cp_parser_statement.)  */
7010           cp_parser_pragma (parser, pragma_external);
7011           continue;
7012         }
7013
7014       /* Parse the declaration itself.  */
7015       cp_parser_declaration (parser);
7016     }
7017 }
7018
7019 /* Parse a declaration.
7020
7021    declaration:
7022      block-declaration
7023      function-definition
7024      template-declaration
7025      explicit-instantiation
7026      explicit-specialization
7027      linkage-specification
7028      namespace-definition
7029
7030    GNU extension:
7031
7032    declaration:
7033       __extension__ declaration */
7034
7035 static void
7036 cp_parser_declaration (cp_parser* parser)
7037 {
7038   cp_token token1;
7039   cp_token token2;
7040   int saved_pedantic;
7041   void *p;
7042
7043   /* Check for the `__extension__' keyword.  */
7044   if (cp_parser_extension_opt (parser, &saved_pedantic))
7045     {
7046       /* Parse the qualified declaration.  */
7047       cp_parser_declaration (parser);
7048       /* Restore the PEDANTIC flag.  */
7049       pedantic = saved_pedantic;
7050
7051       return;
7052     }
7053
7054   /* Try to figure out what kind of declaration is present.  */
7055   token1 = *cp_lexer_peek_token (parser->lexer);
7056
7057   if (token1.type != CPP_EOF)
7058     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7059   else
7060     {
7061       token2.type = CPP_EOF;
7062       token2.keyword = RID_MAX;
7063     }
7064
7065   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7066   p = obstack_alloc (&declarator_obstack, 0);
7067
7068   /* If the next token is `extern' and the following token is a string
7069      literal, then we have a linkage specification.  */
7070   if (token1.keyword == RID_EXTERN
7071       && cp_parser_is_string_literal (&token2))
7072     cp_parser_linkage_specification (parser);
7073   /* If the next token is `template', then we have either a template
7074      declaration, an explicit instantiation, or an explicit
7075      specialization.  */
7076   else if (token1.keyword == RID_TEMPLATE)
7077     {
7078       /* `template <>' indicates a template specialization.  */
7079       if (token2.type == CPP_LESS
7080           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7081         cp_parser_explicit_specialization (parser);
7082       /* `template <' indicates a template declaration.  */
7083       else if (token2.type == CPP_LESS)
7084         cp_parser_template_declaration (parser, /*member_p=*/false);
7085       /* Anything else must be an explicit instantiation.  */
7086       else
7087         cp_parser_explicit_instantiation (parser);
7088     }
7089   /* If the next token is `export', then we have a template
7090      declaration.  */
7091   else if (token1.keyword == RID_EXPORT)
7092     cp_parser_template_declaration (parser, /*member_p=*/false);
7093   /* If the next token is `extern', 'static' or 'inline' and the one
7094      after that is `template', we have a GNU extended explicit
7095      instantiation directive.  */
7096   else if (cp_parser_allow_gnu_extensions_p (parser)
7097            && (token1.keyword == RID_EXTERN
7098                || token1.keyword == RID_STATIC
7099                || token1.keyword == RID_INLINE)
7100            && token2.keyword == RID_TEMPLATE)
7101     cp_parser_explicit_instantiation (parser);
7102   /* If the next token is `namespace', check for a named or unnamed
7103      namespace definition.  */
7104   else if (token1.keyword == RID_NAMESPACE
7105            && (/* A named namespace definition.  */
7106                (token2.type == CPP_NAME
7107                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7108                     != CPP_EQ))
7109                /* An unnamed namespace definition.  */
7110                || token2.type == CPP_OPEN_BRACE
7111                || token2.keyword == RID_ATTRIBUTE))
7112     cp_parser_namespace_definition (parser);
7113   /* Objective-C++ declaration/definition.  */
7114   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7115     cp_parser_objc_declaration (parser);
7116   /* We must have either a block declaration or a function
7117      definition.  */
7118   else
7119     /* Try to parse a block-declaration, or a function-definition.  */
7120     cp_parser_block_declaration (parser, /*statement_p=*/false);
7121
7122   /* Free any declarators allocated.  */
7123   obstack_free (&declarator_obstack, p);
7124 }
7125
7126 /* Parse a block-declaration.
7127
7128    block-declaration:
7129      simple-declaration
7130      asm-definition
7131      namespace-alias-definition
7132      using-declaration
7133      using-directive
7134
7135    GNU Extension:
7136
7137    block-declaration:
7138      __extension__ block-declaration
7139      label-declaration
7140
7141    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7142    part of a declaration-statement.  */
7143
7144 static void
7145 cp_parser_block_declaration (cp_parser *parser,
7146                              bool      statement_p)
7147 {
7148   cp_token *token1;
7149   int saved_pedantic;
7150
7151   /* Check for the `__extension__' keyword.  */
7152   if (cp_parser_extension_opt (parser, &saved_pedantic))
7153     {
7154       /* Parse the qualified declaration.  */
7155       cp_parser_block_declaration (parser, statement_p);
7156       /* Restore the PEDANTIC flag.  */
7157       pedantic = saved_pedantic;
7158
7159       return;
7160     }
7161
7162   /* Peek at the next token to figure out which kind of declaration is
7163      present.  */
7164   token1 = cp_lexer_peek_token (parser->lexer);
7165
7166   /* If the next keyword is `asm', we have an asm-definition.  */
7167   if (token1->keyword == RID_ASM)
7168     {
7169       if (statement_p)
7170         cp_parser_commit_to_tentative_parse (parser);
7171       cp_parser_asm_definition (parser);
7172     }
7173   /* If the next keyword is `namespace', we have a
7174      namespace-alias-definition.  */
7175   else if (token1->keyword == RID_NAMESPACE)
7176     cp_parser_namespace_alias_definition (parser);
7177   /* If the next keyword is `using', we have either a
7178      using-declaration or a using-directive.  */
7179   else if (token1->keyword == RID_USING)
7180     {
7181       cp_token *token2;
7182
7183       if (statement_p)
7184         cp_parser_commit_to_tentative_parse (parser);
7185       /* If the token after `using' is `namespace', then we have a
7186          using-directive.  */
7187       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7188       if (token2->keyword == RID_NAMESPACE)
7189         cp_parser_using_directive (parser);
7190       /* Otherwise, it's a using-declaration.  */
7191       else
7192         cp_parser_using_declaration (parser);
7193     }
7194   /* If the next keyword is `__label__' we have a label declaration.  */
7195   else if (token1->keyword == RID_LABEL)
7196     {
7197       if (statement_p)
7198         cp_parser_commit_to_tentative_parse (parser);
7199       cp_parser_label_declaration (parser);
7200     }
7201   /* Anything else must be a simple-declaration.  */
7202   else
7203     cp_parser_simple_declaration (parser, !statement_p);
7204 }
7205
7206 /* Parse a simple-declaration.
7207
7208    simple-declaration:
7209      decl-specifier-seq [opt] init-declarator-list [opt] ;
7210
7211    init-declarator-list:
7212      init-declarator
7213      init-declarator-list , init-declarator
7214
7215    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7216    function-definition as a simple-declaration.  */
7217
7218 static void
7219 cp_parser_simple_declaration (cp_parser* parser,
7220                               bool function_definition_allowed_p)
7221 {
7222   cp_decl_specifier_seq decl_specifiers;
7223   int declares_class_or_enum;
7224   bool saw_declarator;
7225
7226   /* Defer access checks until we know what is being declared; the
7227      checks for names appearing in the decl-specifier-seq should be
7228      done as if we were in the scope of the thing being declared.  */
7229   push_deferring_access_checks (dk_deferred);
7230
7231   /* Parse the decl-specifier-seq.  We have to keep track of whether
7232      or not the decl-specifier-seq declares a named class or
7233      enumeration type, since that is the only case in which the
7234      init-declarator-list is allowed to be empty.
7235
7236      [dcl.dcl]
7237
7238      In a simple-declaration, the optional init-declarator-list can be
7239      omitted only when declaring a class or enumeration, that is when
7240      the decl-specifier-seq contains either a class-specifier, an
7241      elaborated-type-specifier, or an enum-specifier.  */
7242   cp_parser_decl_specifier_seq (parser,
7243                                 CP_PARSER_FLAGS_OPTIONAL,
7244                                 &decl_specifiers,
7245                                 &declares_class_or_enum);
7246   /* We no longer need to defer access checks.  */
7247   stop_deferring_access_checks ();
7248
7249   /* In a block scope, a valid declaration must always have a
7250      decl-specifier-seq.  By not trying to parse declarators, we can
7251      resolve the declaration/expression ambiguity more quickly.  */
7252   if (!function_definition_allowed_p
7253       && !decl_specifiers.any_specifiers_p)
7254     {
7255       cp_parser_error (parser, "expected declaration");
7256       goto done;
7257     }
7258
7259   /* If the next two tokens are both identifiers, the code is
7260      erroneous. The usual cause of this situation is code like:
7261
7262        T t;
7263
7264      where "T" should name a type -- but does not.  */
7265   if (!decl_specifiers.type
7266       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7267     {
7268       /* If parsing tentatively, we should commit; we really are
7269          looking at a declaration.  */
7270       cp_parser_commit_to_tentative_parse (parser);
7271       /* Give up.  */
7272       goto done;
7273     }
7274
7275   /* If we have seen at least one decl-specifier, and the next token
7276      is not a parenthesis, then we must be looking at a declaration.
7277      (After "int (" we might be looking at a functional cast.)  */
7278   if (decl_specifiers.any_specifiers_p
7279       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7280     cp_parser_commit_to_tentative_parse (parser);
7281
7282   /* Keep going until we hit the `;' at the end of the simple
7283      declaration.  */
7284   saw_declarator = false;
7285   while (cp_lexer_next_token_is_not (parser->lexer,
7286                                      CPP_SEMICOLON))
7287     {
7288       cp_token *token;
7289       bool function_definition_p;
7290       tree decl;
7291
7292       if (saw_declarator)
7293         {
7294           /* If we are processing next declarator, coma is expected */
7295           token = cp_lexer_peek_token (parser->lexer);
7296           gcc_assert (token->type == CPP_COMMA);
7297           cp_lexer_consume_token (parser->lexer);
7298         }
7299       else
7300         saw_declarator = true;
7301
7302       /* Parse the init-declarator.  */
7303       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7304                                         /*checks=*/NULL_TREE,
7305                                         function_definition_allowed_p,
7306                                         /*member_p=*/false,
7307                                         declares_class_or_enum,
7308                                         &function_definition_p);
7309       /* If an error occurred while parsing tentatively, exit quickly.
7310          (That usually happens when in the body of a function; each
7311          statement is treated as a declaration-statement until proven
7312          otherwise.)  */
7313       if (cp_parser_error_occurred (parser))
7314         goto done;
7315       /* Handle function definitions specially.  */
7316       if (function_definition_p)
7317         {
7318           /* If the next token is a `,', then we are probably
7319              processing something like:
7320
7321                void f() {}, *p;
7322
7323              which is erroneous.  */
7324           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7325             error ("mixing declarations and function-definitions is forbidden");
7326           /* Otherwise, we're done with the list of declarators.  */
7327           else
7328             {
7329               pop_deferring_access_checks ();
7330               return;
7331             }
7332         }
7333       /* The next token should be either a `,' or a `;'.  */
7334       token = cp_lexer_peek_token (parser->lexer);
7335       /* If it's a `,', there are more declarators to come.  */
7336       if (token->type == CPP_COMMA)
7337         /* will be consumed next time around */;
7338       /* If it's a `;', we are done.  */
7339       else if (token->type == CPP_SEMICOLON)
7340         break;
7341       /* Anything else is an error.  */
7342       else
7343         {
7344           /* If we have already issued an error message we don't need
7345              to issue another one.  */
7346           if (decl != error_mark_node
7347               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7348             cp_parser_error (parser, "expected %<,%> or %<;%>");
7349           /* Skip tokens until we reach the end of the statement.  */
7350           cp_parser_skip_to_end_of_statement (parser);
7351           /* If the next token is now a `;', consume it.  */
7352           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7353             cp_lexer_consume_token (parser->lexer);
7354           goto done;
7355         }
7356       /* After the first time around, a function-definition is not
7357          allowed -- even if it was OK at first.  For example:
7358
7359            int i, f() {}
7360
7361          is not valid.  */
7362       function_definition_allowed_p = false;
7363     }
7364
7365   /* Issue an error message if no declarators are present, and the
7366      decl-specifier-seq does not itself declare a class or
7367      enumeration.  */
7368   if (!saw_declarator)
7369     {
7370       if (cp_parser_declares_only_class_p (parser))
7371         shadow_tag (&decl_specifiers);
7372       /* Perform any deferred access checks.  */
7373       perform_deferred_access_checks ();
7374     }
7375
7376   /* Consume the `;'.  */
7377   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7378
7379  done:
7380   pop_deferring_access_checks ();
7381 }
7382
7383 /* Parse a decl-specifier-seq.
7384
7385    decl-specifier-seq:
7386      decl-specifier-seq [opt] decl-specifier
7387
7388    decl-specifier:
7389      storage-class-specifier
7390      type-specifier
7391      function-specifier
7392      friend
7393      typedef
7394
7395    GNU Extension:
7396
7397    decl-specifier:
7398      attributes
7399
7400    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7401
7402    The parser flags FLAGS is used to control type-specifier parsing.
7403
7404    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7405    flags:
7406
7407      1: one of the decl-specifiers is an elaborated-type-specifier
7408         (i.e., a type declaration)
7409      2: one of the decl-specifiers is an enum-specifier or a
7410         class-specifier (i.e., a type definition)
7411
7412    */
7413
7414 static void
7415 cp_parser_decl_specifier_seq (cp_parser* parser,
7416                               cp_parser_flags flags,
7417                               cp_decl_specifier_seq *decl_specs,
7418                               int* declares_class_or_enum)
7419 {
7420   bool constructor_possible_p = !parser->in_declarator_p;
7421
7422   /* Clear DECL_SPECS.  */
7423   clear_decl_specs (decl_specs);
7424
7425   /* Assume no class or enumeration type is declared.  */
7426   *declares_class_or_enum = 0;
7427
7428   /* Keep reading specifiers until there are no more to read.  */
7429   while (true)
7430     {
7431       bool constructor_p;
7432       bool found_decl_spec;
7433       cp_token *token;
7434
7435       /* Peek at the next token.  */
7436       token = cp_lexer_peek_token (parser->lexer);
7437       /* Handle attributes.  */
7438       if (token->keyword == RID_ATTRIBUTE)
7439         {
7440           /* Parse the attributes.  */
7441           decl_specs->attributes
7442             = chainon (decl_specs->attributes,
7443                        cp_parser_attributes_opt (parser));
7444           continue;
7445         }
7446       /* Assume we will find a decl-specifier keyword.  */
7447       found_decl_spec = true;
7448       /* If the next token is an appropriate keyword, we can simply
7449          add it to the list.  */
7450       switch (token->keyword)
7451         {
7452           /* decl-specifier:
7453                friend  */
7454         case RID_FRIEND:
7455           if (!at_class_scope_p ())
7456             {
7457               error ("%<friend%> used outside of class");
7458               cp_lexer_purge_token (parser->lexer);
7459             }
7460           else
7461             {
7462               ++decl_specs->specs[(int) ds_friend];
7463               /* Consume the token.  */
7464               cp_lexer_consume_token (parser->lexer);
7465             }
7466           break;
7467
7468           /* function-specifier:
7469                inline
7470                virtual
7471                explicit  */
7472         case RID_INLINE:
7473         case RID_VIRTUAL:
7474         case RID_EXPLICIT:
7475           cp_parser_function_specifier_opt (parser, decl_specs);
7476           break;
7477
7478           /* decl-specifier:
7479                typedef  */
7480         case RID_TYPEDEF:
7481           ++decl_specs->specs[(int) ds_typedef];
7482           /* Consume the token.  */
7483           cp_lexer_consume_token (parser->lexer);
7484           /* A constructor declarator cannot appear in a typedef.  */
7485           constructor_possible_p = false;
7486           /* The "typedef" keyword can only occur in a declaration; we
7487              may as well commit at this point.  */
7488           cp_parser_commit_to_tentative_parse (parser);
7489           break;
7490
7491           /* storage-class-specifier:
7492                auto
7493                register
7494                static
7495                extern
7496                mutable
7497
7498              GNU Extension:
7499                thread  */
7500         case RID_AUTO:
7501         case RID_REGISTER:
7502         case RID_STATIC:
7503         case RID_EXTERN:
7504         case RID_MUTABLE:
7505           /* Consume the token.  */
7506           cp_lexer_consume_token (parser->lexer);
7507           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7508           break;
7509         case RID_THREAD:
7510           /* Consume the token.  */
7511           cp_lexer_consume_token (parser->lexer);
7512           ++decl_specs->specs[(int) ds_thread];
7513           break;
7514
7515         default:
7516           /* We did not yet find a decl-specifier yet.  */
7517           found_decl_spec = false;
7518           break;
7519         }
7520
7521       /* Constructors are a special case.  The `S' in `S()' is not a
7522          decl-specifier; it is the beginning of the declarator.  */
7523       constructor_p
7524         = (!found_decl_spec
7525            && constructor_possible_p
7526            && (cp_parser_constructor_declarator_p
7527                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7528
7529       /* If we don't have a DECL_SPEC yet, then we must be looking at
7530          a type-specifier.  */
7531       if (!found_decl_spec && !constructor_p)
7532         {
7533           int decl_spec_declares_class_or_enum;
7534           bool is_cv_qualifier;
7535           tree type_spec;
7536
7537           type_spec
7538             = cp_parser_type_specifier (parser, flags,
7539                                         decl_specs,
7540                                         /*is_declaration=*/true,
7541                                         &decl_spec_declares_class_or_enum,
7542                                         &is_cv_qualifier);
7543
7544           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7545
7546           /* If this type-specifier referenced a user-defined type
7547              (a typedef, class-name, etc.), then we can't allow any
7548              more such type-specifiers henceforth.
7549
7550              [dcl.spec]
7551
7552              The longest sequence of decl-specifiers that could
7553              possibly be a type name is taken as the
7554              decl-specifier-seq of a declaration.  The sequence shall
7555              be self-consistent as described below.
7556
7557              [dcl.type]
7558
7559              As a general rule, at most one type-specifier is allowed
7560              in the complete decl-specifier-seq of a declaration.  The
7561              only exceptions are the following:
7562
7563              -- const or volatile can be combined with any other
7564                 type-specifier.
7565
7566              -- signed or unsigned can be combined with char, long,
7567                 short, or int.
7568
7569              -- ..
7570
7571              Example:
7572
7573                typedef char* Pc;
7574                void g (const int Pc);
7575
7576              Here, Pc is *not* part of the decl-specifier seq; it's
7577              the declarator.  Therefore, once we see a type-specifier
7578              (other than a cv-qualifier), we forbid any additional
7579              user-defined types.  We *do* still allow things like `int
7580              int' to be considered a decl-specifier-seq, and issue the
7581              error message later.  */
7582           if (type_spec && !is_cv_qualifier)
7583             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7584           /* A constructor declarator cannot follow a type-specifier.  */
7585           if (type_spec)
7586             {
7587               constructor_possible_p = false;
7588               found_decl_spec = true;
7589             }
7590         }
7591
7592       /* If we still do not have a DECL_SPEC, then there are no more
7593          decl-specifiers.  */
7594       if (!found_decl_spec)
7595         break;
7596
7597       decl_specs->any_specifiers_p = true;
7598       /* After we see one decl-specifier, further decl-specifiers are
7599          always optional.  */
7600       flags |= CP_PARSER_FLAGS_OPTIONAL;
7601     }
7602
7603   cp_parser_check_decl_spec (decl_specs);
7604
7605   /* Don't allow a friend specifier with a class definition.  */
7606   if (decl_specs->specs[(int) ds_friend] != 0
7607       && (*declares_class_or_enum & 2))
7608     error ("class definition may not be declared a friend");
7609 }
7610
7611 /* Parse an (optional) storage-class-specifier.
7612
7613    storage-class-specifier:
7614      auto
7615      register
7616      static
7617      extern
7618      mutable
7619
7620    GNU Extension:
7621
7622    storage-class-specifier:
7623      thread
7624
7625    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7626
7627 static tree
7628 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7629 {
7630   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7631     {
7632     case RID_AUTO:
7633     case RID_REGISTER:
7634     case RID_STATIC:
7635     case RID_EXTERN:
7636     case RID_MUTABLE:
7637     case RID_THREAD:
7638       /* Consume the token.  */
7639       return cp_lexer_consume_token (parser->lexer)->value;
7640
7641     default:
7642       return NULL_TREE;
7643     }
7644 }
7645
7646 /* Parse an (optional) function-specifier.
7647
7648    function-specifier:
7649      inline
7650      virtual
7651      explicit
7652
7653    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7654    Updates DECL_SPECS, if it is non-NULL.  */
7655
7656 static tree
7657 cp_parser_function_specifier_opt (cp_parser* parser,
7658                                   cp_decl_specifier_seq *decl_specs)
7659 {
7660   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7661     {
7662     case RID_INLINE:
7663       if (decl_specs)
7664         ++decl_specs->specs[(int) ds_inline];
7665       break;
7666
7667     case RID_VIRTUAL:
7668       /* 14.5.2.3 [temp.mem]
7669
7670          A member function template shall not be virtual.  */
7671       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7672         error ("templates may not be %<virtual%>");
7673       else if (decl_specs)
7674         ++decl_specs->specs[(int) ds_virtual];
7675       break;
7676
7677     case RID_EXPLICIT:
7678       if (decl_specs)
7679         ++decl_specs->specs[(int) ds_explicit];
7680       break;
7681
7682     default:
7683       return NULL_TREE;
7684     }
7685
7686   /* Consume the token.  */
7687   return cp_lexer_consume_token (parser->lexer)->value;
7688 }
7689
7690 /* Parse a linkage-specification.
7691
7692    linkage-specification:
7693      extern string-literal { declaration-seq [opt] }
7694      extern string-literal declaration  */
7695
7696 static void
7697 cp_parser_linkage_specification (cp_parser* parser)
7698 {
7699   tree linkage;
7700
7701   /* Look for the `extern' keyword.  */
7702   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7703
7704   /* Look for the string-literal.  */
7705   linkage = cp_parser_string_literal (parser, false, false);
7706
7707   /* Transform the literal into an identifier.  If the literal is a
7708      wide-character string, or contains embedded NULs, then we can't
7709      handle it as the user wants.  */
7710   if (strlen (TREE_STRING_POINTER (linkage))
7711       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7712     {
7713       cp_parser_error (parser, "invalid linkage-specification");
7714       /* Assume C++ linkage.  */
7715       linkage = lang_name_cplusplus;
7716     }
7717   else
7718     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7719
7720   /* We're now using the new linkage.  */
7721   push_lang_context (linkage);
7722
7723   /* If the next token is a `{', then we're using the first
7724      production.  */
7725   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7726     {
7727       /* Consume the `{' token.  */
7728       cp_lexer_consume_token (parser->lexer);
7729       /* Parse the declarations.  */
7730       cp_parser_declaration_seq_opt (parser);
7731       /* Look for the closing `}'.  */
7732       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7733     }
7734   /* Otherwise, there's just one declaration.  */
7735   else
7736     {
7737       bool saved_in_unbraced_linkage_specification_p;
7738
7739       saved_in_unbraced_linkage_specification_p
7740         = parser->in_unbraced_linkage_specification_p;
7741       parser->in_unbraced_linkage_specification_p = true;
7742       cp_parser_declaration (parser);
7743       parser->in_unbraced_linkage_specification_p
7744         = saved_in_unbraced_linkage_specification_p;
7745     }
7746
7747   /* We're done with the linkage-specification.  */
7748   pop_lang_context ();
7749 }
7750
7751 /* Special member functions [gram.special] */
7752
7753 /* Parse a conversion-function-id.
7754
7755    conversion-function-id:
7756      operator conversion-type-id
7757
7758    Returns an IDENTIFIER_NODE representing the operator.  */
7759
7760 static tree
7761 cp_parser_conversion_function_id (cp_parser* parser)
7762 {
7763   tree type;
7764   tree saved_scope;
7765   tree saved_qualifying_scope;
7766   tree saved_object_scope;
7767   tree pushed_scope = NULL_TREE;
7768
7769   /* Look for the `operator' token.  */
7770   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7771     return error_mark_node;
7772   /* When we parse the conversion-type-id, the current scope will be
7773      reset.  However, we need that information in able to look up the
7774      conversion function later, so we save it here.  */
7775   saved_scope = parser->scope;
7776   saved_qualifying_scope = parser->qualifying_scope;
7777   saved_object_scope = parser->object_scope;
7778   /* We must enter the scope of the class so that the names of
7779      entities declared within the class are available in the
7780      conversion-type-id.  For example, consider:
7781
7782        struct S {
7783          typedef int I;
7784          operator I();
7785        };
7786
7787        S::operator I() { ... }
7788
7789      In order to see that `I' is a type-name in the definition, we
7790      must be in the scope of `S'.  */
7791   if (saved_scope)
7792     pushed_scope = push_scope (saved_scope);
7793   /* Parse the conversion-type-id.  */
7794   type = cp_parser_conversion_type_id (parser);
7795   /* Leave the scope of the class, if any.  */
7796   if (pushed_scope)
7797     pop_scope (pushed_scope);
7798   /* Restore the saved scope.  */
7799   parser->scope = saved_scope;
7800   parser->qualifying_scope = saved_qualifying_scope;
7801   parser->object_scope = saved_object_scope;
7802   /* If the TYPE is invalid, indicate failure.  */
7803   if (type == error_mark_node)
7804     return error_mark_node;
7805   return mangle_conv_op_name_for_type (type);
7806 }
7807
7808 /* Parse a conversion-type-id:
7809
7810    conversion-type-id:
7811      type-specifier-seq conversion-declarator [opt]
7812
7813    Returns the TYPE specified.  */
7814
7815 static tree
7816 cp_parser_conversion_type_id (cp_parser* parser)
7817 {
7818   tree attributes;
7819   cp_decl_specifier_seq type_specifiers;
7820   cp_declarator *declarator;
7821   tree type_specified;
7822
7823   /* Parse the attributes.  */
7824   attributes = cp_parser_attributes_opt (parser);
7825   /* Parse the type-specifiers.  */
7826   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7827                                 &type_specifiers);
7828   /* If that didn't work, stop.  */
7829   if (type_specifiers.type == error_mark_node)
7830     return error_mark_node;
7831   /* Parse the conversion-declarator.  */
7832   declarator = cp_parser_conversion_declarator_opt (parser);
7833
7834   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7835                                     /*initialized=*/0, &attributes);
7836   if (attributes)
7837     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7838   return type_specified;
7839 }
7840
7841 /* Parse an (optional) conversion-declarator.
7842
7843    conversion-declarator:
7844      ptr-operator conversion-declarator [opt]
7845
7846    */
7847
7848 static cp_declarator *
7849 cp_parser_conversion_declarator_opt (cp_parser* parser)
7850 {
7851   enum tree_code code;
7852   tree class_type;
7853   cp_cv_quals cv_quals;
7854
7855   /* We don't know if there's a ptr-operator next, or not.  */
7856   cp_parser_parse_tentatively (parser);
7857   /* Try the ptr-operator.  */
7858   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7859   /* If it worked, look for more conversion-declarators.  */
7860   if (cp_parser_parse_definitely (parser))
7861     {
7862       cp_declarator *declarator;
7863
7864       /* Parse another optional declarator.  */
7865       declarator = cp_parser_conversion_declarator_opt (parser);
7866
7867       /* Create the representation of the declarator.  */
7868       if (class_type)
7869         declarator = make_ptrmem_declarator (cv_quals, class_type,
7870                                              declarator);
7871       else if (code == INDIRECT_REF)
7872         declarator = make_pointer_declarator (cv_quals, declarator);
7873       else
7874         declarator = make_reference_declarator (cv_quals, declarator);
7875
7876       return declarator;
7877    }
7878
7879   return NULL;
7880 }
7881
7882 /* Parse an (optional) ctor-initializer.
7883
7884    ctor-initializer:
7885      : mem-initializer-list
7886
7887    Returns TRUE iff the ctor-initializer was actually present.  */
7888
7889 static bool
7890 cp_parser_ctor_initializer_opt (cp_parser* parser)
7891 {
7892   /* If the next token is not a `:', then there is no
7893      ctor-initializer.  */
7894   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7895     {
7896       /* Do default initialization of any bases and members.  */
7897       if (DECL_CONSTRUCTOR_P (current_function_decl))
7898         finish_mem_initializers (NULL_TREE);
7899
7900       return false;
7901     }
7902
7903   /* Consume the `:' token.  */
7904   cp_lexer_consume_token (parser->lexer);
7905   /* And the mem-initializer-list.  */
7906   cp_parser_mem_initializer_list (parser);
7907
7908   return true;
7909 }
7910
7911 /* Parse a mem-initializer-list.
7912
7913    mem-initializer-list:
7914      mem-initializer
7915      mem-initializer , mem-initializer-list  */
7916
7917 static void
7918 cp_parser_mem_initializer_list (cp_parser* parser)
7919 {
7920   tree mem_initializer_list = NULL_TREE;
7921
7922   /* Let the semantic analysis code know that we are starting the
7923      mem-initializer-list.  */
7924   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7925     error ("only constructors take base initializers");
7926
7927   /* Loop through the list.  */
7928   while (true)
7929     {
7930       tree mem_initializer;
7931
7932       /* Parse the mem-initializer.  */
7933       mem_initializer = cp_parser_mem_initializer (parser);
7934       /* Add it to the list, unless it was erroneous.  */
7935       if (mem_initializer != error_mark_node)
7936         {
7937           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7938           mem_initializer_list = mem_initializer;
7939         }
7940       /* If the next token is not a `,', we're done.  */
7941       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7942         break;
7943       /* Consume the `,' token.  */
7944       cp_lexer_consume_token (parser->lexer);
7945     }
7946
7947   /* Perform semantic analysis.  */
7948   if (DECL_CONSTRUCTOR_P (current_function_decl))
7949     finish_mem_initializers (mem_initializer_list);
7950 }
7951
7952 /* Parse a mem-initializer.
7953
7954    mem-initializer:
7955      mem-initializer-id ( expression-list [opt] )
7956
7957    GNU extension:
7958
7959    mem-initializer:
7960      ( expression-list [opt] )
7961
7962    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7963    class) or FIELD_DECL (for a non-static data member) to initialize;
7964    the TREE_VALUE is the expression-list.  An empty initialization
7965    list is represented by void_list_node.  */
7966
7967 static tree
7968 cp_parser_mem_initializer (cp_parser* parser)
7969 {
7970   tree mem_initializer_id;
7971   tree expression_list;
7972   tree member;
7973
7974   /* Find out what is being initialized.  */
7975   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7976     {
7977       pedwarn ("anachronistic old-style base class initializer");
7978       mem_initializer_id = NULL_TREE;
7979     }
7980   else
7981     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7982   member = expand_member_init (mem_initializer_id);
7983   if (member && !DECL_P (member))
7984     in_base_initializer = 1;
7985
7986   expression_list
7987     = cp_parser_parenthesized_expression_list (parser, false,
7988                                                /*cast_p=*/false,
7989                                                /*non_constant_p=*/NULL);
7990   if (expression_list == error_mark_node)
7991     return error_mark_node;
7992   if (!expression_list)
7993     expression_list = void_type_node;
7994
7995   in_base_initializer = 0;
7996
7997   return member ? build_tree_list (member, expression_list) : error_mark_node;
7998 }
7999
8000 /* Parse a mem-initializer-id.
8001
8002    mem-initializer-id:
8003      :: [opt] nested-name-specifier [opt] class-name
8004      identifier
8005
8006    Returns a TYPE indicating the class to be initializer for the first
8007    production.  Returns an IDENTIFIER_NODE indicating the data member
8008    to be initialized for the second production.  */
8009
8010 static tree
8011 cp_parser_mem_initializer_id (cp_parser* parser)
8012 {
8013   bool global_scope_p;
8014   bool nested_name_specifier_p;
8015   bool template_p = false;
8016   tree id;
8017
8018   /* `typename' is not allowed in this context ([temp.res]).  */
8019   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8020     {
8021       error ("keyword %<typename%> not allowed in this context (a qualified "
8022              "member initializer is implicitly a type)");
8023       cp_lexer_consume_token (parser->lexer);
8024     }
8025   /* Look for the optional `::' operator.  */
8026   global_scope_p
8027     = (cp_parser_global_scope_opt (parser,
8028                                    /*current_scope_valid_p=*/false)
8029        != NULL_TREE);
8030   /* Look for the optional nested-name-specifier.  The simplest way to
8031      implement:
8032
8033        [temp.res]
8034
8035        The keyword `typename' is not permitted in a base-specifier or
8036        mem-initializer; in these contexts a qualified name that
8037        depends on a template-parameter is implicitly assumed to be a
8038        type name.
8039
8040      is to assume that we have seen the `typename' keyword at this
8041      point.  */
8042   nested_name_specifier_p
8043     = (cp_parser_nested_name_specifier_opt (parser,
8044                                             /*typename_keyword_p=*/true,
8045                                             /*check_dependency_p=*/true,
8046                                             /*type_p=*/true,
8047                                             /*is_declaration=*/true)
8048        != NULL_TREE);
8049   if (nested_name_specifier_p)
8050     template_p = cp_parser_optional_template_keyword (parser);
8051   /* If there is a `::' operator or a nested-name-specifier, then we
8052      are definitely looking for a class-name.  */
8053   if (global_scope_p || nested_name_specifier_p)
8054     return cp_parser_class_name (parser,
8055                                  /*typename_keyword_p=*/true,
8056                                  /*template_keyword_p=*/template_p,
8057                                  none_type,
8058                                  /*check_dependency_p=*/true,
8059                                  /*class_head_p=*/false,
8060                                  /*is_declaration=*/true);
8061   /* Otherwise, we could also be looking for an ordinary identifier.  */
8062   cp_parser_parse_tentatively (parser);
8063   /* Try a class-name.  */
8064   id = cp_parser_class_name (parser,
8065                              /*typename_keyword_p=*/true,
8066                              /*template_keyword_p=*/false,
8067                              none_type,
8068                              /*check_dependency_p=*/true,
8069                              /*class_head_p=*/false,
8070                              /*is_declaration=*/true);
8071   /* If we found one, we're done.  */
8072   if (cp_parser_parse_definitely (parser))
8073     return id;
8074   /* Otherwise, look for an ordinary identifier.  */
8075   return cp_parser_identifier (parser);
8076 }
8077
8078 /* Overloading [gram.over] */
8079
8080 /* Parse an operator-function-id.
8081
8082    operator-function-id:
8083      operator operator
8084
8085    Returns an IDENTIFIER_NODE for the operator which is a
8086    human-readable spelling of the identifier, e.g., `operator +'.  */
8087
8088 static tree
8089 cp_parser_operator_function_id (cp_parser* parser)
8090 {
8091   /* Look for the `operator' keyword.  */
8092   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8093     return error_mark_node;
8094   /* And then the name of the operator itself.  */
8095   return cp_parser_operator (parser);
8096 }
8097
8098 /* Parse an operator.
8099
8100    operator:
8101      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8102      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8103      || ++ -- , ->* -> () []
8104
8105    GNU Extensions:
8106
8107    operator:
8108      <? >? <?= >?=
8109
8110    Returns an IDENTIFIER_NODE for the operator which is a
8111    human-readable spelling of the identifier, e.g., `operator +'.  */
8112
8113 static tree
8114 cp_parser_operator (cp_parser* parser)
8115 {
8116   tree id = NULL_TREE;
8117   cp_token *token;
8118
8119   /* Peek at the next token.  */
8120   token = cp_lexer_peek_token (parser->lexer);
8121   /* Figure out which operator we have.  */
8122   switch (token->type)
8123     {
8124     case CPP_KEYWORD:
8125       {
8126         enum tree_code op;
8127
8128         /* The keyword should be either `new' or `delete'.  */
8129         if (token->keyword == RID_NEW)
8130           op = NEW_EXPR;
8131         else if (token->keyword == RID_DELETE)
8132           op = DELETE_EXPR;
8133         else
8134           break;
8135
8136         /* Consume the `new' or `delete' token.  */
8137         cp_lexer_consume_token (parser->lexer);
8138
8139         /* Peek at the next token.  */
8140         token = cp_lexer_peek_token (parser->lexer);
8141         /* If it's a `[' token then this is the array variant of the
8142            operator.  */
8143         if (token->type == CPP_OPEN_SQUARE)
8144           {
8145             /* Consume the `[' token.  */
8146             cp_lexer_consume_token (parser->lexer);
8147             /* Look for the `]' token.  */
8148             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8149             id = ansi_opname (op == NEW_EXPR
8150                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8151           }
8152         /* Otherwise, we have the non-array variant.  */
8153         else
8154           id = ansi_opname (op);
8155
8156         return id;
8157       }
8158
8159     case CPP_PLUS:
8160       id = ansi_opname (PLUS_EXPR);
8161       break;
8162
8163     case CPP_MINUS:
8164       id = ansi_opname (MINUS_EXPR);
8165       break;
8166
8167     case CPP_MULT:
8168       id = ansi_opname (MULT_EXPR);
8169       break;
8170
8171     case CPP_DIV:
8172       id = ansi_opname (TRUNC_DIV_EXPR);
8173       break;
8174
8175     case CPP_MOD:
8176       id = ansi_opname (TRUNC_MOD_EXPR);
8177       break;
8178
8179     case CPP_XOR:
8180       id = ansi_opname (BIT_XOR_EXPR);
8181       break;
8182
8183     case CPP_AND:
8184       id = ansi_opname (BIT_AND_EXPR);
8185       break;
8186
8187     case CPP_OR:
8188       id = ansi_opname (BIT_IOR_EXPR);
8189       break;
8190
8191     case CPP_COMPL:
8192       id = ansi_opname (BIT_NOT_EXPR);
8193       break;
8194
8195     case CPP_NOT:
8196       id = ansi_opname (TRUTH_NOT_EXPR);
8197       break;
8198
8199     case CPP_EQ:
8200       id = ansi_assopname (NOP_EXPR);
8201       break;
8202
8203     case CPP_LESS:
8204       id = ansi_opname (LT_EXPR);
8205       break;
8206
8207     case CPP_GREATER:
8208       id = ansi_opname (GT_EXPR);
8209       break;
8210
8211     case CPP_PLUS_EQ:
8212       id = ansi_assopname (PLUS_EXPR);
8213       break;
8214
8215     case CPP_MINUS_EQ:
8216       id = ansi_assopname (MINUS_EXPR);
8217       break;
8218
8219     case CPP_MULT_EQ:
8220       id = ansi_assopname (MULT_EXPR);
8221       break;
8222
8223     case CPP_DIV_EQ:
8224       id = ansi_assopname (TRUNC_DIV_EXPR);
8225       break;
8226
8227     case CPP_MOD_EQ:
8228       id = ansi_assopname (TRUNC_MOD_EXPR);
8229       break;
8230
8231     case CPP_XOR_EQ:
8232       id = ansi_assopname (BIT_XOR_EXPR);
8233       break;
8234
8235     case CPP_AND_EQ:
8236       id = ansi_assopname (BIT_AND_EXPR);
8237       break;
8238
8239     case CPP_OR_EQ:
8240       id = ansi_assopname (BIT_IOR_EXPR);
8241       break;
8242
8243     case CPP_LSHIFT:
8244       id = ansi_opname (LSHIFT_EXPR);
8245       break;
8246
8247     case CPP_RSHIFT:
8248       id = ansi_opname (RSHIFT_EXPR);
8249       break;
8250
8251     case CPP_LSHIFT_EQ:
8252       id = ansi_assopname (LSHIFT_EXPR);
8253       break;
8254
8255     case CPP_RSHIFT_EQ:
8256       id = ansi_assopname (RSHIFT_EXPR);
8257       break;
8258
8259     case CPP_EQ_EQ:
8260       id = ansi_opname (EQ_EXPR);
8261       break;
8262
8263     case CPP_NOT_EQ:
8264       id = ansi_opname (NE_EXPR);
8265       break;
8266
8267     case CPP_LESS_EQ:
8268       id = ansi_opname (LE_EXPR);
8269       break;
8270
8271     case CPP_GREATER_EQ:
8272       id = ansi_opname (GE_EXPR);
8273       break;
8274
8275     case CPP_AND_AND:
8276       id = ansi_opname (TRUTH_ANDIF_EXPR);
8277       break;
8278
8279     case CPP_OR_OR:
8280       id = ansi_opname (TRUTH_ORIF_EXPR);
8281       break;
8282
8283     case CPP_PLUS_PLUS:
8284       id = ansi_opname (POSTINCREMENT_EXPR);
8285       break;
8286
8287     case CPP_MINUS_MINUS:
8288       id = ansi_opname (PREDECREMENT_EXPR);
8289       break;
8290
8291     case CPP_COMMA:
8292       id = ansi_opname (COMPOUND_EXPR);
8293       break;
8294
8295     case CPP_DEREF_STAR:
8296       id = ansi_opname (MEMBER_REF);
8297       break;
8298
8299     case CPP_DEREF:
8300       id = ansi_opname (COMPONENT_REF);
8301       break;
8302
8303     case CPP_OPEN_PAREN:
8304       /* Consume the `('.  */
8305       cp_lexer_consume_token (parser->lexer);
8306       /* Look for the matching `)'.  */
8307       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8308       return ansi_opname (CALL_EXPR);
8309
8310     case CPP_OPEN_SQUARE:
8311       /* Consume the `['.  */
8312       cp_lexer_consume_token (parser->lexer);
8313       /* Look for the matching `]'.  */
8314       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8315       return ansi_opname (ARRAY_REF);
8316
8317     default:
8318       /* Anything else is an error.  */
8319       break;
8320     }
8321
8322   /* If we have selected an identifier, we need to consume the
8323      operator token.  */
8324   if (id)
8325     cp_lexer_consume_token (parser->lexer);
8326   /* Otherwise, no valid operator name was present.  */
8327   else
8328     {
8329       cp_parser_error (parser, "expected operator");
8330       id = error_mark_node;
8331     }
8332
8333   return id;
8334 }
8335
8336 /* Parse a template-declaration.
8337
8338    template-declaration:
8339      export [opt] template < template-parameter-list > declaration
8340
8341    If MEMBER_P is TRUE, this template-declaration occurs within a
8342    class-specifier.
8343
8344    The grammar rule given by the standard isn't correct.  What
8345    is really meant is:
8346
8347    template-declaration:
8348      export [opt] template-parameter-list-seq
8349        decl-specifier-seq [opt] init-declarator [opt] ;
8350      export [opt] template-parameter-list-seq
8351        function-definition
8352
8353    template-parameter-list-seq:
8354      template-parameter-list-seq [opt]
8355      template < template-parameter-list >  */
8356
8357 static void
8358 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8359 {
8360   /* Check for `export'.  */
8361   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8362     {
8363       /* Consume the `export' token.  */
8364       cp_lexer_consume_token (parser->lexer);
8365       /* Warn that we do not support `export'.  */
8366       warning (0, "keyword %<export%> not implemented, and will be ignored");
8367     }
8368
8369   cp_parser_template_declaration_after_export (parser, member_p);
8370 }
8371
8372 /* Parse a template-parameter-list.
8373
8374    template-parameter-list:
8375      template-parameter
8376      template-parameter-list , template-parameter
8377
8378    Returns a TREE_LIST.  Each node represents a template parameter.
8379    The nodes are connected via their TREE_CHAINs.  */
8380
8381 static tree
8382 cp_parser_template_parameter_list (cp_parser* parser)
8383 {
8384   tree parameter_list = NULL_TREE;
8385
8386   begin_template_parm_list ();
8387   while (true)
8388     {
8389       tree parameter;
8390       cp_token *token;
8391       bool is_non_type;
8392
8393       /* Parse the template-parameter.  */
8394       parameter = cp_parser_template_parameter (parser, &is_non_type);
8395       /* Add it to the list.  */
8396       if (parameter != error_mark_node)
8397         parameter_list = process_template_parm (parameter_list,
8398                                                 parameter,
8399                                                 is_non_type);
8400       else
8401        {
8402          tree err_parm = build_tree_list (parameter, parameter);
8403          TREE_VALUE (err_parm) = error_mark_node;
8404          parameter_list = chainon (parameter_list, err_parm);
8405        }
8406
8407       /* Peek at the next token.  */
8408       token = cp_lexer_peek_token (parser->lexer);
8409       /* If it's not a `,', we're done.  */
8410       if (token->type != CPP_COMMA)
8411         break;
8412       /* Otherwise, consume the `,' token.  */
8413       cp_lexer_consume_token (parser->lexer);
8414     }
8415
8416   return end_template_parm_list (parameter_list);
8417 }
8418
8419 /* Parse a template-parameter.
8420
8421    template-parameter:
8422      type-parameter
8423      parameter-declaration
8424
8425    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8426    the parameter.  The TREE_PURPOSE is the default value, if any.
8427    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8428    iff this parameter is a non-type parameter.  */
8429
8430 static tree
8431 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8432 {
8433   cp_token *token;
8434   cp_parameter_declarator *parameter_declarator;
8435   tree parm;
8436
8437   /* Assume it is a type parameter or a template parameter.  */
8438   *is_non_type = false;
8439   /* Peek at the next token.  */
8440   token = cp_lexer_peek_token (parser->lexer);
8441   /* If it is `class' or `template', we have a type-parameter.  */
8442   if (token->keyword == RID_TEMPLATE)
8443     return cp_parser_type_parameter (parser);
8444   /* If it is `class' or `typename' we do not know yet whether it is a
8445      type parameter or a non-type parameter.  Consider:
8446
8447        template <typename T, typename T::X X> ...
8448
8449      or:
8450
8451        template <class C, class D*> ...
8452
8453      Here, the first parameter is a type parameter, and the second is
8454      a non-type parameter.  We can tell by looking at the token after
8455      the identifier -- if it is a `,', `=', or `>' then we have a type
8456      parameter.  */
8457   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8458     {
8459       /* Peek at the token after `class' or `typename'.  */
8460       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8461       /* If it's an identifier, skip it.  */
8462       if (token->type == CPP_NAME)
8463         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8464       /* Now, see if the token looks like the end of a template
8465          parameter.  */
8466       if (token->type == CPP_COMMA
8467           || token->type == CPP_EQ
8468           || token->type == CPP_GREATER)
8469         return cp_parser_type_parameter (parser);
8470     }
8471
8472   /* Otherwise, it is a non-type parameter.
8473
8474      [temp.param]
8475
8476      When parsing a default template-argument for a non-type
8477      template-parameter, the first non-nested `>' is taken as the end
8478      of the template parameter-list rather than a greater-than
8479      operator.  */
8480   *is_non_type = true;
8481   parameter_declarator
8482      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8483                                         /*parenthesized_p=*/NULL);
8484   parm = grokdeclarator (parameter_declarator->declarator,
8485                          &parameter_declarator->decl_specifiers,
8486                          PARM, /*initialized=*/0,
8487                          /*attrlist=*/NULL);
8488   if (parm == error_mark_node)
8489     return error_mark_node;
8490   return build_tree_list (parameter_declarator->default_argument, parm);
8491 }
8492
8493 /* Parse a type-parameter.
8494
8495    type-parameter:
8496      class identifier [opt]
8497      class identifier [opt] = type-id
8498      typename identifier [opt]
8499      typename identifier [opt] = type-id
8500      template < template-parameter-list > class identifier [opt]
8501      template < template-parameter-list > class identifier [opt]
8502        = id-expression
8503
8504    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8505    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8506    the declaration of the parameter.  */
8507
8508 static tree
8509 cp_parser_type_parameter (cp_parser* parser)
8510 {
8511   cp_token *token;
8512   tree parameter;
8513
8514   /* Look for a keyword to tell us what kind of parameter this is.  */
8515   token = cp_parser_require (parser, CPP_KEYWORD,
8516                              "`class', `typename', or `template'");
8517   if (!token)
8518     return error_mark_node;
8519
8520   switch (token->keyword)
8521     {
8522     case RID_CLASS:
8523     case RID_TYPENAME:
8524       {
8525         tree identifier;
8526         tree default_argument;
8527
8528         /* If the next token is an identifier, then it names the
8529            parameter.  */
8530         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8531           identifier = cp_parser_identifier (parser);
8532         else
8533           identifier = NULL_TREE;
8534
8535         /* Create the parameter.  */
8536         parameter = finish_template_type_parm (class_type_node, identifier);
8537
8538         /* If the next token is an `=', we have a default argument.  */
8539         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8540           {
8541             /* Consume the `=' token.  */
8542             cp_lexer_consume_token (parser->lexer);
8543             /* Parse the default-argument.  */
8544             push_deferring_access_checks (dk_no_deferred);
8545             default_argument = cp_parser_type_id (parser);
8546             pop_deferring_access_checks ();
8547           }
8548         else
8549           default_argument = NULL_TREE;
8550
8551         /* Create the combined representation of the parameter and the
8552            default argument.  */
8553         parameter = build_tree_list (default_argument, parameter);
8554       }
8555       break;
8556
8557     case RID_TEMPLATE:
8558       {
8559         tree parameter_list;
8560         tree identifier;
8561         tree default_argument;
8562
8563         /* Look for the `<'.  */
8564         cp_parser_require (parser, CPP_LESS, "`<'");
8565         /* Parse the template-parameter-list.  */
8566         parameter_list = cp_parser_template_parameter_list (parser);
8567         /* Look for the `>'.  */
8568         cp_parser_require (parser, CPP_GREATER, "`>'");
8569         /* Look for the `class' keyword.  */
8570         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8571         /* If the next token is an `=', then there is a
8572            default-argument.  If the next token is a `>', we are at
8573            the end of the parameter-list.  If the next token is a `,',
8574            then we are at the end of this parameter.  */
8575         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8576             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8577             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8578           {
8579             identifier = cp_parser_identifier (parser);
8580             /* Treat invalid names as if the parameter were nameless.  */
8581             if (identifier == error_mark_node)
8582               identifier = NULL_TREE;
8583           }
8584         else
8585           identifier = NULL_TREE;
8586
8587         /* Create the template parameter.  */
8588         parameter = finish_template_template_parm (class_type_node,
8589                                                    identifier);
8590
8591         /* If the next token is an `=', then there is a
8592            default-argument.  */
8593         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8594           {
8595             bool is_template;
8596
8597             /* Consume the `='.  */
8598             cp_lexer_consume_token (parser->lexer);
8599             /* Parse the id-expression.  */
8600             push_deferring_access_checks (dk_no_deferred);
8601             default_argument
8602               = cp_parser_id_expression (parser,
8603                                          /*template_keyword_p=*/false,
8604                                          /*check_dependency_p=*/true,
8605                                          /*template_p=*/&is_template,
8606                                          /*declarator_p=*/false,
8607                                          /*optional_p=*/false);
8608             if (TREE_CODE (default_argument) == TYPE_DECL)
8609               /* If the id-expression was a template-id that refers to
8610                  a template-class, we already have the declaration here,
8611                  so no further lookup is needed.  */
8612                  ;
8613             else
8614               /* Look up the name.  */
8615               default_argument
8616                 = cp_parser_lookup_name (parser, default_argument,
8617                                          none_type,
8618                                          /*is_template=*/is_template,
8619                                          /*is_namespace=*/false,
8620                                          /*check_dependency=*/true,
8621                                          /*ambiguous_decls=*/NULL);
8622             /* See if the default argument is valid.  */
8623             default_argument
8624               = check_template_template_default_arg (default_argument);
8625             pop_deferring_access_checks ();
8626           }
8627         else
8628           default_argument = NULL_TREE;
8629
8630         /* Create the combined representation of the parameter and the
8631            default argument.  */
8632         parameter = build_tree_list (default_argument, parameter);
8633       }
8634       break;
8635
8636     default:
8637       gcc_unreachable ();
8638       break;
8639     }
8640
8641   return parameter;
8642 }
8643
8644 /* Parse a template-id.
8645
8646    template-id:
8647      template-name < template-argument-list [opt] >
8648
8649    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8650    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8651    returned.  Otherwise, if the template-name names a function, or set
8652    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8653    names a class, returns a TYPE_DECL for the specialization.
8654
8655    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8656    uninstantiated templates.  */
8657
8658 static tree
8659 cp_parser_template_id (cp_parser *parser,
8660                        bool template_keyword_p,
8661                        bool check_dependency_p,
8662                        bool is_declaration)
8663 {
8664   tree template;
8665   tree arguments;
8666   tree template_id;
8667   cp_token_position start_of_id = 0;
8668   tree access_check = NULL_TREE;
8669   cp_token *next_token, *next_token_2;
8670   bool is_identifier;
8671
8672   /* If the next token corresponds to a template-id, there is no need
8673      to reparse it.  */
8674   next_token = cp_lexer_peek_token (parser->lexer);
8675   if (next_token->type == CPP_TEMPLATE_ID)
8676     {
8677       tree value;
8678       tree check;
8679
8680       /* Get the stored value.  */
8681       value = cp_lexer_consume_token (parser->lexer)->value;
8682       /* Perform any access checks that were deferred.  */
8683       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8684         perform_or_defer_access_check (TREE_PURPOSE (check),
8685                                        TREE_VALUE (check));
8686       /* Return the stored value.  */
8687       return TREE_VALUE (value);
8688     }
8689
8690   /* Avoid performing name lookup if there is no possibility of
8691      finding a template-id.  */
8692   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8693       || (next_token->type == CPP_NAME
8694           && !cp_parser_nth_token_starts_template_argument_list_p
8695                (parser, 2)))
8696     {
8697       cp_parser_error (parser, "expected template-id");
8698       return error_mark_node;
8699     }
8700
8701   /* Remember where the template-id starts.  */
8702   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8703     start_of_id = cp_lexer_token_position (parser->lexer, false);
8704
8705   push_deferring_access_checks (dk_deferred);
8706
8707   /* Parse the template-name.  */
8708   is_identifier = false;
8709   template = cp_parser_template_name (parser, template_keyword_p,
8710                                       check_dependency_p,
8711                                       is_declaration,
8712                                       &is_identifier);
8713   if (template == error_mark_node || is_identifier)
8714     {
8715       pop_deferring_access_checks ();
8716       return template;
8717     }
8718
8719   /* If we find the sequence `[:' after a template-name, it's probably
8720      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8721      parse correctly the argument list.  */
8722   next_token = cp_lexer_peek_token (parser->lexer);
8723   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8724   if (next_token->type == CPP_OPEN_SQUARE
8725       && next_token->flags & DIGRAPH
8726       && next_token_2->type == CPP_COLON
8727       && !(next_token_2->flags & PREV_WHITE))
8728     {
8729       cp_parser_parse_tentatively (parser);
8730       /* Change `:' into `::'.  */
8731       next_token_2->type = CPP_SCOPE;
8732       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8733          CPP_LESS.  */
8734       cp_lexer_consume_token (parser->lexer);
8735       /* Parse the arguments.  */
8736       arguments = cp_parser_enclosed_template_argument_list (parser);
8737       if (!cp_parser_parse_definitely (parser))
8738         {
8739           /* If we couldn't parse an argument list, then we revert our changes
8740              and return simply an error. Maybe this is not a template-id
8741              after all.  */
8742           next_token_2->type = CPP_COLON;
8743           cp_parser_error (parser, "expected %<<%>");
8744           pop_deferring_access_checks ();
8745           return error_mark_node;
8746         }
8747       /* Otherwise, emit an error about the invalid digraph, but continue
8748          parsing because we got our argument list.  */
8749       pedwarn ("%<<::%> cannot begin a template-argument list");
8750       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8751               "between %<<%> and %<::%>");
8752       if (!flag_permissive)
8753         {
8754           static bool hint;
8755           if (!hint)
8756             {
8757               inform ("(if you use -fpermissive G++ will accept your code)");
8758               hint = true;
8759             }
8760         }
8761     }
8762   else
8763     {
8764       /* Look for the `<' that starts the template-argument-list.  */
8765       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8766         {
8767           pop_deferring_access_checks ();
8768           return error_mark_node;
8769         }
8770       /* Parse the arguments.  */
8771       arguments = cp_parser_enclosed_template_argument_list (parser);
8772     }
8773
8774   /* Build a representation of the specialization.  */
8775   if (TREE_CODE (template) == IDENTIFIER_NODE)
8776     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8777   else if (DECL_CLASS_TEMPLATE_P (template)
8778            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8779     {
8780       bool entering_scope;
8781       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8782          template (rather than some instantiation thereof) only if
8783          is not nested within some other construct.  For example, in
8784          "template <typename T> void f(T) { A<T>::", A<T> is just an
8785          instantiation of A.  */
8786       entering_scope = (template_parm_scope_p ()
8787                         && cp_lexer_next_token_is (parser->lexer,
8788                                                    CPP_SCOPE));
8789       template_id
8790         = finish_template_type (template, arguments, entering_scope);
8791     }
8792   else
8793     {
8794       /* If it's not a class-template or a template-template, it should be
8795          a function-template.  */
8796       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8797                    || TREE_CODE (template) == OVERLOAD
8798                    || BASELINK_P (template)));
8799
8800       template_id = lookup_template_function (template, arguments);
8801     }
8802
8803   /* Retrieve any deferred checks.  Do not pop this access checks yet
8804      so the memory will not be reclaimed during token replacing below.  */
8805   access_check = get_deferred_access_checks ();
8806
8807   /* If parsing tentatively, replace the sequence of tokens that makes
8808      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8809      should we re-parse the token stream, we will not have to repeat
8810      the effort required to do the parse, nor will we issue duplicate
8811      error messages about problems during instantiation of the
8812      template.  */
8813   if (start_of_id)
8814     {
8815       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8816
8817       /* Reset the contents of the START_OF_ID token.  */
8818       token->type = CPP_TEMPLATE_ID;
8819       token->value = build_tree_list (access_check, template_id);
8820       token->keyword = RID_MAX;
8821
8822       /* Purge all subsequent tokens.  */
8823       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8824
8825       /* ??? Can we actually assume that, if template_id ==
8826          error_mark_node, we will have issued a diagnostic to the
8827          user, as opposed to simply marking the tentative parse as
8828          failed?  */
8829       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8830         error ("parse error in template argument list");
8831     }
8832
8833   pop_deferring_access_checks ();
8834   return template_id;
8835 }
8836
8837 /* Parse a template-name.
8838
8839    template-name:
8840      identifier
8841
8842    The standard should actually say:
8843
8844    template-name:
8845      identifier
8846      operator-function-id
8847
8848    A defect report has been filed about this issue.
8849
8850    A conversion-function-id cannot be a template name because they cannot
8851    be part of a template-id. In fact, looking at this code:
8852
8853    a.operator K<int>()
8854
8855    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8856    It is impossible to call a templated conversion-function-id with an
8857    explicit argument list, since the only allowed template parameter is
8858    the type to which it is converting.
8859
8860    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8861    `template' keyword, in a construction like:
8862
8863      T::template f<3>()
8864
8865    In that case `f' is taken to be a template-name, even though there
8866    is no way of knowing for sure.
8867
8868    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8869    name refers to a set of overloaded functions, at least one of which
8870    is a template, or an IDENTIFIER_NODE with the name of the template,
8871    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8872    names are looked up inside uninstantiated templates.  */
8873
8874 static tree
8875 cp_parser_template_name (cp_parser* parser,
8876                          bool template_keyword_p,
8877                          bool check_dependency_p,
8878                          bool is_declaration,
8879                          bool *is_identifier)
8880 {
8881   tree identifier;
8882   tree decl;
8883   tree fns;
8884
8885   /* If the next token is `operator', then we have either an
8886      operator-function-id or a conversion-function-id.  */
8887   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8888     {
8889       /* We don't know whether we're looking at an
8890          operator-function-id or a conversion-function-id.  */
8891       cp_parser_parse_tentatively (parser);
8892       /* Try an operator-function-id.  */
8893       identifier = cp_parser_operator_function_id (parser);
8894       /* If that didn't work, try a conversion-function-id.  */
8895       if (!cp_parser_parse_definitely (parser))
8896         {
8897           cp_parser_error (parser, "expected template-name");
8898           return error_mark_node;
8899         }
8900     }
8901   /* Look for the identifier.  */
8902   else
8903     identifier = cp_parser_identifier (parser);
8904
8905   /* If we didn't find an identifier, we don't have a template-id.  */
8906   if (identifier == error_mark_node)
8907     return error_mark_node;
8908
8909   /* If the name immediately followed the `template' keyword, then it
8910      is a template-name.  However, if the next token is not `<', then
8911      we do not treat it as a template-name, since it is not being used
8912      as part of a template-id.  This enables us to handle constructs
8913      like:
8914
8915        template <typename T> struct S { S(); };
8916        template <typename T> S<T>::S();
8917
8918      correctly.  We would treat `S' as a template -- if it were `S<T>'
8919      -- but we do not if there is no `<'.  */
8920
8921   if (processing_template_decl
8922       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8923     {
8924       /* In a declaration, in a dependent context, we pretend that the
8925          "template" keyword was present in order to improve error
8926          recovery.  For example, given:
8927
8928            template <typename T> void f(T::X<int>);
8929
8930          we want to treat "X<int>" as a template-id.  */
8931       if (is_declaration
8932           && !template_keyword_p
8933           && parser->scope && TYPE_P (parser->scope)
8934           && check_dependency_p
8935           && dependent_type_p (parser->scope)
8936           /* Do not do this for dtors (or ctors), since they never
8937              need the template keyword before their name.  */
8938           && !constructor_name_p (identifier, parser->scope))
8939         {
8940           cp_token_position start = 0;
8941
8942           /* Explain what went wrong.  */
8943           error ("non-template %qD used as template", identifier);
8944           inform ("use %<%T::template %D%> to indicate that it is a template",
8945                   parser->scope, identifier);
8946           /* If parsing tentatively, find the location of the "<" token.  */
8947           if (cp_parser_simulate_error (parser))
8948             start = cp_lexer_token_position (parser->lexer, true);
8949           /* Parse the template arguments so that we can issue error
8950              messages about them.  */
8951           cp_lexer_consume_token (parser->lexer);
8952           cp_parser_enclosed_template_argument_list (parser);
8953           /* Skip tokens until we find a good place from which to
8954              continue parsing.  */
8955           cp_parser_skip_to_closing_parenthesis (parser,
8956                                                  /*recovering=*/true,
8957                                                  /*or_comma=*/true,
8958                                                  /*consume_paren=*/false);
8959           /* If parsing tentatively, permanently remove the
8960              template argument list.  That will prevent duplicate
8961              error messages from being issued about the missing
8962              "template" keyword.  */
8963           if (start)
8964             cp_lexer_purge_tokens_after (parser->lexer, start);
8965           if (is_identifier)
8966             *is_identifier = true;
8967           return identifier;
8968         }
8969
8970       /* If the "template" keyword is present, then there is generally
8971          no point in doing name-lookup, so we just return IDENTIFIER.
8972          But, if the qualifying scope is non-dependent then we can
8973          (and must) do name-lookup normally.  */
8974       if (template_keyword_p
8975           && (!parser->scope
8976               || (TYPE_P (parser->scope)
8977                   && dependent_type_p (parser->scope))))
8978         return identifier;
8979     }
8980
8981   /* Look up the name.  */
8982   decl = cp_parser_lookup_name (parser, identifier,
8983                                 none_type,
8984                                 /*is_template=*/false,
8985                                 /*is_namespace=*/false,
8986                                 check_dependency_p,
8987                                 /*ambiguous_decls=*/NULL);
8988   decl = maybe_get_template_decl_from_type_decl (decl);
8989
8990   /* If DECL is a template, then the name was a template-name.  */
8991   if (TREE_CODE (decl) == TEMPLATE_DECL)
8992     ;
8993   else
8994     {
8995       tree fn = NULL_TREE;
8996
8997       /* The standard does not explicitly indicate whether a name that
8998          names a set of overloaded declarations, some of which are
8999          templates, is a template-name.  However, such a name should
9000          be a template-name; otherwise, there is no way to form a
9001          template-id for the overloaded templates.  */
9002       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9003       if (TREE_CODE (fns) == OVERLOAD)
9004         for (fn = fns; fn; fn = OVL_NEXT (fn))
9005           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9006             break;
9007
9008       if (!fn)
9009         {
9010           /* The name does not name a template.  */
9011           cp_parser_error (parser, "expected template-name");
9012           return error_mark_node;
9013         }
9014     }
9015
9016   /* If DECL is dependent, and refers to a function, then just return
9017      its name; we will look it up again during template instantiation.  */
9018   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9019     {
9020       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9021       if (TYPE_P (scope) && dependent_type_p (scope))
9022         return identifier;
9023     }
9024
9025   return decl;
9026 }
9027
9028 /* Parse a template-argument-list.
9029
9030    template-argument-list:
9031      template-argument
9032      template-argument-list , template-argument
9033
9034    Returns a TREE_VEC containing the arguments.  */
9035
9036 static tree
9037 cp_parser_template_argument_list (cp_parser* parser)
9038 {
9039   tree fixed_args[10];
9040   unsigned n_args = 0;
9041   unsigned alloced = 10;
9042   tree *arg_ary = fixed_args;
9043   tree vec;
9044   bool saved_in_template_argument_list_p;
9045   bool saved_ice_p;
9046   bool saved_non_ice_p;
9047
9048   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9049   parser->in_template_argument_list_p = true;
9050   /* Even if the template-id appears in an integral
9051      constant-expression, the contents of the argument list do
9052      not.  */
9053   saved_ice_p = parser->integral_constant_expression_p;
9054   parser->integral_constant_expression_p = false;
9055   saved_non_ice_p = parser->non_integral_constant_expression_p;
9056   parser->non_integral_constant_expression_p = false;
9057   /* Parse the arguments.  */
9058   do
9059     {
9060       tree argument;
9061
9062       if (n_args)
9063         /* Consume the comma.  */
9064         cp_lexer_consume_token (parser->lexer);
9065
9066       /* Parse the template-argument.  */
9067       argument = cp_parser_template_argument (parser);
9068       if (n_args == alloced)
9069         {
9070           alloced *= 2;
9071
9072           if (arg_ary == fixed_args)
9073             {
9074               arg_ary = XNEWVEC (tree, alloced);
9075               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9076             }
9077           else
9078             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9079         }
9080       arg_ary[n_args++] = argument;
9081     }
9082   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9083
9084   vec = make_tree_vec (n_args);
9085
9086   while (n_args--)
9087     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9088
9089   if (arg_ary != fixed_args)
9090     free (arg_ary);
9091   parser->non_integral_constant_expression_p = saved_non_ice_p;
9092   parser->integral_constant_expression_p = saved_ice_p;
9093   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9094   return vec;
9095 }
9096
9097 /* Parse a template-argument.
9098
9099    template-argument:
9100      assignment-expression
9101      type-id
9102      id-expression
9103
9104    The representation is that of an assignment-expression, type-id, or
9105    id-expression -- except that the qualified id-expression is
9106    evaluated, so that the value returned is either a DECL or an
9107    OVERLOAD.
9108
9109    Although the standard says "assignment-expression", it forbids
9110    throw-expressions or assignments in the template argument.
9111    Therefore, we use "conditional-expression" instead.  */
9112
9113 static tree
9114 cp_parser_template_argument (cp_parser* parser)
9115 {
9116   tree argument;
9117   bool template_p;
9118   bool address_p;
9119   bool maybe_type_id = false;
9120   cp_token *token;
9121   cp_id_kind idk;
9122
9123   /* There's really no way to know what we're looking at, so we just
9124      try each alternative in order.
9125
9126        [temp.arg]
9127
9128        In a template-argument, an ambiguity between a type-id and an
9129        expression is resolved to a type-id, regardless of the form of
9130        the corresponding template-parameter.
9131
9132      Therefore, we try a type-id first.  */
9133   cp_parser_parse_tentatively (parser);
9134   argument = cp_parser_type_id (parser);
9135   /* If there was no error parsing the type-id but the next token is a '>>',
9136      we probably found a typo for '> >'. But there are type-id which are
9137      also valid expressions. For instance:
9138
9139      struct X { int operator >> (int); };
9140      template <int V> struct Foo {};
9141      Foo<X () >> 5> r;
9142
9143      Here 'X()' is a valid type-id of a function type, but the user just
9144      wanted to write the expression "X() >> 5". Thus, we remember that we
9145      found a valid type-id, but we still try to parse the argument as an
9146      expression to see what happens.  */
9147   if (!cp_parser_error_occurred (parser)
9148       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9149     {
9150       maybe_type_id = true;
9151       cp_parser_abort_tentative_parse (parser);
9152     }
9153   else
9154     {
9155       /* If the next token isn't a `,' or a `>', then this argument wasn't
9156       really finished. This means that the argument is not a valid
9157       type-id.  */
9158       if (!cp_parser_next_token_ends_template_argument_p (parser))
9159         cp_parser_error (parser, "expected template-argument");
9160       /* If that worked, we're done.  */
9161       if (cp_parser_parse_definitely (parser))
9162         return argument;
9163     }
9164   /* We're still not sure what the argument will be.  */
9165   cp_parser_parse_tentatively (parser);
9166   /* Try a template.  */
9167   argument = cp_parser_id_expression (parser,
9168                                       /*template_keyword_p=*/false,
9169                                       /*check_dependency_p=*/true,
9170                                       &template_p,
9171                                       /*declarator_p=*/false,
9172                                       /*optional_p=*/false);
9173   /* If the next token isn't a `,' or a `>', then this argument wasn't
9174      really finished.  */
9175   if (!cp_parser_next_token_ends_template_argument_p (parser))
9176     cp_parser_error (parser, "expected template-argument");
9177   if (!cp_parser_error_occurred (parser))
9178     {
9179       /* Figure out what is being referred to.  If the id-expression
9180          was for a class template specialization, then we will have a
9181          TYPE_DECL at this point.  There is no need to do name lookup
9182          at this point in that case.  */
9183       if (TREE_CODE (argument) != TYPE_DECL)
9184         argument = cp_parser_lookup_name (parser, argument,
9185                                           none_type,
9186                                           /*is_template=*/template_p,
9187                                           /*is_namespace=*/false,
9188                                           /*check_dependency=*/true,
9189                                           /*ambiguous_decls=*/NULL);
9190       if (TREE_CODE (argument) != TEMPLATE_DECL
9191           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9192         cp_parser_error (parser, "expected template-name");
9193     }
9194   if (cp_parser_parse_definitely (parser))
9195     return argument;
9196   /* It must be a non-type argument.  There permitted cases are given
9197      in [temp.arg.nontype]:
9198
9199      -- an integral constant-expression of integral or enumeration
9200         type; or
9201
9202      -- the name of a non-type template-parameter; or
9203
9204      -- the name of an object or function with external linkage...
9205
9206      -- the address of an object or function with external linkage...
9207
9208      -- a pointer to member...  */
9209   /* Look for a non-type template parameter.  */
9210   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9211     {
9212       cp_parser_parse_tentatively (parser);
9213       argument = cp_parser_primary_expression (parser,
9214                                                /*adress_p=*/false,
9215                                                /*cast_p=*/false,
9216                                                /*template_arg_p=*/true,
9217                                                &idk);
9218       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9219           || !cp_parser_next_token_ends_template_argument_p (parser))
9220         cp_parser_simulate_error (parser);
9221       if (cp_parser_parse_definitely (parser))
9222         return argument;
9223     }
9224
9225   /* If the next token is "&", the argument must be the address of an
9226      object or function with external linkage.  */
9227   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9228   if (address_p)
9229     cp_lexer_consume_token (parser->lexer);
9230   /* See if we might have an id-expression.  */
9231   token = cp_lexer_peek_token (parser->lexer);
9232   if (token->type == CPP_NAME
9233       || token->keyword == RID_OPERATOR
9234       || token->type == CPP_SCOPE
9235       || token->type == CPP_TEMPLATE_ID
9236       || token->type == CPP_NESTED_NAME_SPECIFIER)
9237     {
9238       cp_parser_parse_tentatively (parser);
9239       argument = cp_parser_primary_expression (parser,
9240                                                address_p,
9241                                                /*cast_p=*/false,
9242                                                /*template_arg_p=*/true,
9243                                                &idk);
9244       if (cp_parser_error_occurred (parser)
9245           || !cp_parser_next_token_ends_template_argument_p (parser))
9246         cp_parser_abort_tentative_parse (parser);
9247       else
9248         {
9249           if (TREE_CODE (argument) == INDIRECT_REF)
9250             {
9251               gcc_assert (REFERENCE_REF_P (argument));
9252               argument = TREE_OPERAND (argument, 0);
9253             }
9254
9255           if (TREE_CODE (argument) == VAR_DECL)
9256             {
9257               /* A variable without external linkage might still be a
9258                  valid constant-expression, so no error is issued here
9259                  if the external-linkage check fails.  */
9260               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9261                 cp_parser_simulate_error (parser);
9262             }
9263           else if (is_overloaded_fn (argument))
9264             /* All overloaded functions are allowed; if the external
9265                linkage test does not pass, an error will be issued
9266                later.  */
9267             ;
9268           else if (address_p
9269                    && (TREE_CODE (argument) == OFFSET_REF
9270                        || TREE_CODE (argument) == SCOPE_REF))
9271             /* A pointer-to-member.  */
9272             ;
9273           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9274             ;
9275           else
9276             cp_parser_simulate_error (parser);
9277
9278           if (cp_parser_parse_definitely (parser))
9279             {
9280               if (address_p)
9281                 argument = build_x_unary_op (ADDR_EXPR, argument);
9282               return argument;
9283             }
9284         }
9285     }
9286   /* If the argument started with "&", there are no other valid
9287      alternatives at this point.  */
9288   if (address_p)
9289     {
9290       cp_parser_error (parser, "invalid non-type template argument");
9291       return error_mark_node;
9292     }
9293
9294   /* If the argument wasn't successfully parsed as a type-id followed
9295      by '>>', the argument can only be a constant expression now.
9296      Otherwise, we try parsing the constant-expression tentatively,
9297      because the argument could really be a type-id.  */
9298   if (maybe_type_id)
9299     cp_parser_parse_tentatively (parser);
9300   argument = cp_parser_constant_expression (parser,
9301                                             /*allow_non_constant_p=*/false,
9302                                             /*non_constant_p=*/NULL);
9303   argument = fold_non_dependent_expr (argument);
9304   if (!maybe_type_id)
9305     return argument;
9306   if (!cp_parser_next_token_ends_template_argument_p (parser))
9307     cp_parser_error (parser, "expected template-argument");
9308   if (cp_parser_parse_definitely (parser))
9309     return argument;
9310   /* We did our best to parse the argument as a non type-id, but that
9311      was the only alternative that matched (albeit with a '>' after
9312      it). We can assume it's just a typo from the user, and a
9313      diagnostic will then be issued.  */
9314   return cp_parser_type_id (parser);
9315 }
9316
9317 /* Parse an explicit-instantiation.
9318
9319    explicit-instantiation:
9320      template declaration
9321
9322    Although the standard says `declaration', what it really means is:
9323
9324    explicit-instantiation:
9325      template decl-specifier-seq [opt] declarator [opt] ;
9326
9327    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9328    supposed to be allowed.  A defect report has been filed about this
9329    issue.
9330
9331    GNU Extension:
9332
9333    explicit-instantiation:
9334      storage-class-specifier template
9335        decl-specifier-seq [opt] declarator [opt] ;
9336      function-specifier template
9337        decl-specifier-seq [opt] declarator [opt] ;  */
9338
9339 static void
9340 cp_parser_explicit_instantiation (cp_parser* parser)
9341 {
9342   int declares_class_or_enum;
9343   cp_decl_specifier_seq decl_specifiers;
9344   tree extension_specifier = NULL_TREE;
9345
9346   /* Look for an (optional) storage-class-specifier or
9347      function-specifier.  */
9348   if (cp_parser_allow_gnu_extensions_p (parser))
9349     {
9350       extension_specifier
9351         = cp_parser_storage_class_specifier_opt (parser);
9352       if (!extension_specifier)
9353         extension_specifier
9354           = cp_parser_function_specifier_opt (parser,
9355                                               /*decl_specs=*/NULL);
9356     }
9357
9358   /* Look for the `template' keyword.  */
9359   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9360   /* Let the front end know that we are processing an explicit
9361      instantiation.  */
9362   begin_explicit_instantiation ();
9363   /* [temp.explicit] says that we are supposed to ignore access
9364      control while processing explicit instantiation directives.  */
9365   push_deferring_access_checks (dk_no_check);
9366   /* Parse a decl-specifier-seq.  */
9367   cp_parser_decl_specifier_seq (parser,
9368                                 CP_PARSER_FLAGS_OPTIONAL,
9369                                 &decl_specifiers,
9370                                 &declares_class_or_enum);
9371   /* If there was exactly one decl-specifier, and it declared a class,
9372      and there's no declarator, then we have an explicit type
9373      instantiation.  */
9374   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9375     {
9376       tree type;
9377
9378       type = check_tag_decl (&decl_specifiers);
9379       /* Turn access control back on for names used during
9380          template instantiation.  */
9381       pop_deferring_access_checks ();
9382       if (type)
9383         do_type_instantiation (type, extension_specifier,
9384                                /*complain=*/tf_error);
9385     }
9386   else
9387     {
9388       cp_declarator *declarator;
9389       tree decl;
9390
9391       /* Parse the declarator.  */
9392       declarator
9393         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9394                                 /*ctor_dtor_or_conv_p=*/NULL,
9395                                 /*parenthesized_p=*/NULL,
9396                                 /*member_p=*/false);
9397       if (declares_class_or_enum & 2)
9398         cp_parser_check_for_definition_in_return_type (declarator,
9399                                                        decl_specifiers.type);
9400       if (declarator != cp_error_declarator)
9401         {
9402           decl = grokdeclarator (declarator, &decl_specifiers,
9403                                  NORMAL, 0, &decl_specifiers.attributes);
9404           /* Turn access control back on for names used during
9405              template instantiation.  */
9406           pop_deferring_access_checks ();
9407           /* Do the explicit instantiation.  */
9408           do_decl_instantiation (decl, extension_specifier);
9409         }
9410       else
9411         {
9412           pop_deferring_access_checks ();
9413           /* Skip the body of the explicit instantiation.  */
9414           cp_parser_skip_to_end_of_statement (parser);
9415         }
9416     }
9417   /* We're done with the instantiation.  */
9418   end_explicit_instantiation ();
9419
9420   cp_parser_consume_semicolon_at_end_of_statement (parser);
9421 }
9422
9423 /* Parse an explicit-specialization.
9424
9425    explicit-specialization:
9426      template < > declaration
9427
9428    Although the standard says `declaration', what it really means is:
9429
9430    explicit-specialization:
9431      template <> decl-specifier [opt] init-declarator [opt] ;
9432      template <> function-definition
9433      template <> explicit-specialization
9434      template <> template-declaration  */
9435
9436 static void
9437 cp_parser_explicit_specialization (cp_parser* parser)
9438 {
9439   bool need_lang_pop;
9440   /* Look for the `template' keyword.  */
9441   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9442   /* Look for the `<'.  */
9443   cp_parser_require (parser, CPP_LESS, "`<'");
9444   /* Look for the `>'.  */
9445   cp_parser_require (parser, CPP_GREATER, "`>'");
9446   /* We have processed another parameter list.  */
9447   ++parser->num_template_parameter_lists;
9448   /* [temp]
9449
9450      A template ... explicit specialization ... shall not have C
9451      linkage.  */
9452   if (current_lang_name == lang_name_c)
9453     {
9454       error ("template specialization with C linkage");
9455       /* Give it C++ linkage to avoid confusing other parts of the
9456          front end.  */
9457       push_lang_context (lang_name_cplusplus);
9458       need_lang_pop = true;
9459     }
9460   else
9461     need_lang_pop = false;
9462   /* Let the front end know that we are beginning a specialization.  */
9463   begin_specialization ();
9464   /* If the next keyword is `template', we need to figure out whether
9465      or not we're looking a template-declaration.  */
9466   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9467     {
9468       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9469           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9470         cp_parser_template_declaration_after_export (parser,
9471                                                      /*member_p=*/false);
9472       else
9473         cp_parser_explicit_specialization (parser);
9474     }
9475   else
9476     /* Parse the dependent declaration.  */
9477     cp_parser_single_declaration (parser,
9478                                   /*checks=*/NULL_TREE,
9479                                   /*member_p=*/false,
9480                                   /*friend_p=*/NULL);
9481   /* We're done with the specialization.  */
9482   end_specialization ();
9483   /* For the erroneous case of a template with C linkage, we pushed an
9484      implicit C++ linkage scope; exit that scope now.  */
9485   if (need_lang_pop)
9486     pop_lang_context ();
9487   /* We're done with this parameter list.  */
9488   --parser->num_template_parameter_lists;
9489 }
9490
9491 /* Parse a type-specifier.
9492
9493    type-specifier:
9494      simple-type-specifier
9495      class-specifier
9496      enum-specifier
9497      elaborated-type-specifier
9498      cv-qualifier
9499
9500    GNU Extension:
9501
9502    type-specifier:
9503      __complex__
9504
9505    Returns a representation of the type-specifier.  For a
9506    class-specifier, enum-specifier, or elaborated-type-specifier, a
9507    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9508
9509    The parser flags FLAGS is used to control type-specifier parsing.
9510
9511    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9512    in a decl-specifier-seq.
9513
9514    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9515    class-specifier, enum-specifier, or elaborated-type-specifier, then
9516    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9517    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9518    zero.
9519
9520    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9521    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9522    is set to FALSE.  */
9523
9524 static tree
9525 cp_parser_type_specifier (cp_parser* parser,
9526                           cp_parser_flags flags,
9527                           cp_decl_specifier_seq *decl_specs,
9528                           bool is_declaration,
9529                           int* declares_class_or_enum,
9530                           bool* is_cv_qualifier)
9531 {
9532   tree type_spec = NULL_TREE;
9533   cp_token *token;
9534   enum rid keyword;
9535   cp_decl_spec ds = ds_last;
9536
9537   /* Assume this type-specifier does not declare a new type.  */
9538   if (declares_class_or_enum)
9539     *declares_class_or_enum = 0;
9540   /* And that it does not specify a cv-qualifier.  */
9541   if (is_cv_qualifier)
9542     *is_cv_qualifier = false;
9543   /* Peek at the next token.  */
9544   token = cp_lexer_peek_token (parser->lexer);
9545
9546   /* If we're looking at a keyword, we can use that to guide the
9547      production we choose.  */
9548   keyword = token->keyword;
9549   switch (keyword)
9550     {
9551     case RID_ENUM:
9552       /* Look for the enum-specifier.  */
9553       type_spec = cp_parser_enum_specifier (parser);
9554       /* If that worked, we're done.  */
9555       if (type_spec)
9556         {
9557           if (declares_class_or_enum)
9558             *declares_class_or_enum = 2;
9559           if (decl_specs)
9560             cp_parser_set_decl_spec_type (decl_specs,
9561                                           type_spec,
9562                                           /*user_defined_p=*/true);
9563           return type_spec;
9564         }
9565       else
9566         goto elaborated_type_specifier;
9567
9568       /* Any of these indicate either a class-specifier, or an
9569          elaborated-type-specifier.  */
9570     case RID_CLASS:
9571     case RID_STRUCT:
9572     case RID_UNION:
9573       /* Parse tentatively so that we can back up if we don't find a
9574          class-specifier.  */
9575       cp_parser_parse_tentatively (parser);
9576       /* Look for the class-specifier.  */
9577       type_spec = cp_parser_class_specifier (parser);
9578       /* If that worked, we're done.  */
9579       if (cp_parser_parse_definitely (parser))
9580         {
9581           if (declares_class_or_enum)
9582             *declares_class_or_enum = 2;
9583           if (decl_specs)
9584             cp_parser_set_decl_spec_type (decl_specs,
9585                                           type_spec,
9586                                           /*user_defined_p=*/true);
9587           return type_spec;
9588         }
9589
9590       /* Fall through.  */
9591     elaborated_type_specifier:
9592       /* We're declaring (not defining) a class or enum.  */
9593       if (declares_class_or_enum)
9594         *declares_class_or_enum = 1;
9595
9596       /* Fall through.  */
9597     case RID_TYPENAME:
9598       /* Look for an elaborated-type-specifier.  */
9599       type_spec
9600         = (cp_parser_elaborated_type_specifier
9601            (parser,
9602             decl_specs && decl_specs->specs[(int) ds_friend],
9603             is_declaration));
9604       if (decl_specs)
9605         cp_parser_set_decl_spec_type (decl_specs,
9606                                       type_spec,
9607                                       /*user_defined_p=*/true);
9608       return type_spec;
9609
9610     case RID_CONST:
9611       ds = ds_const;
9612       if (is_cv_qualifier)
9613         *is_cv_qualifier = true;
9614       break;
9615
9616     case RID_VOLATILE:
9617       ds = ds_volatile;
9618       if (is_cv_qualifier)
9619         *is_cv_qualifier = true;
9620       break;
9621
9622     case RID_RESTRICT:
9623       ds = ds_restrict;
9624       if (is_cv_qualifier)
9625         *is_cv_qualifier = true;
9626       break;
9627
9628     case RID_COMPLEX:
9629       /* The `__complex__' keyword is a GNU extension.  */
9630       ds = ds_complex;
9631       break;
9632
9633     default:
9634       break;
9635     }
9636
9637   /* Handle simple keywords.  */
9638   if (ds != ds_last)
9639     {
9640       if (decl_specs)
9641         {
9642           ++decl_specs->specs[(int)ds];
9643           decl_specs->any_specifiers_p = true;
9644         }
9645       return cp_lexer_consume_token (parser->lexer)->value;
9646     }
9647
9648   /* If we do not already have a type-specifier, assume we are looking
9649      at a simple-type-specifier.  */
9650   type_spec = cp_parser_simple_type_specifier (parser,
9651                                                decl_specs,
9652                                                flags);
9653
9654   /* If we didn't find a type-specifier, and a type-specifier was not
9655      optional in this context, issue an error message.  */
9656   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9657     {
9658       cp_parser_error (parser, "expected type specifier");
9659       return error_mark_node;
9660     }
9661
9662   return type_spec;
9663 }
9664
9665 /* Parse a simple-type-specifier.
9666
9667    simple-type-specifier:
9668      :: [opt] nested-name-specifier [opt] type-name
9669      :: [opt] nested-name-specifier template template-id
9670      char
9671      wchar_t
9672      bool
9673      short
9674      int
9675      long
9676      signed
9677      unsigned
9678      float
9679      double
9680      void
9681
9682    GNU Extension:
9683
9684    simple-type-specifier:
9685      __typeof__ unary-expression
9686      __typeof__ ( type-id )
9687
9688    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9689    appropriately updated.  */
9690
9691 static tree
9692 cp_parser_simple_type_specifier (cp_parser* parser,
9693                                  cp_decl_specifier_seq *decl_specs,
9694                                  cp_parser_flags flags)
9695 {
9696   tree type = NULL_TREE;
9697   cp_token *token;
9698
9699   /* Peek at the next token.  */
9700   token = cp_lexer_peek_token (parser->lexer);
9701
9702   /* If we're looking at a keyword, things are easy.  */
9703   switch (token->keyword)
9704     {
9705     case RID_CHAR:
9706       if (decl_specs)
9707         decl_specs->explicit_char_p = true;
9708       type = char_type_node;
9709       break;
9710     case RID_WCHAR:
9711       type = wchar_type_node;
9712       break;
9713     case RID_BOOL:
9714       type = boolean_type_node;
9715       break;
9716     case RID_SHORT:
9717       if (decl_specs)
9718         ++decl_specs->specs[(int) ds_short];
9719       type = short_integer_type_node;
9720       break;
9721     case RID_INT:
9722       if (decl_specs)
9723         decl_specs->explicit_int_p = true;
9724       type = integer_type_node;
9725       break;
9726     case RID_LONG:
9727       if (decl_specs)
9728         ++decl_specs->specs[(int) ds_long];
9729       type = long_integer_type_node;
9730       break;
9731     case RID_SIGNED:
9732       if (decl_specs)
9733         ++decl_specs->specs[(int) ds_signed];
9734       type = integer_type_node;
9735       break;
9736     case RID_UNSIGNED:
9737       if (decl_specs)
9738         ++decl_specs->specs[(int) ds_unsigned];
9739       type = unsigned_type_node;
9740       break;
9741     case RID_FLOAT:
9742       type = float_type_node;
9743       break;
9744     case RID_DOUBLE:
9745       type = double_type_node;
9746       break;
9747     case RID_VOID:
9748       type = void_type_node;
9749       break;
9750
9751     case RID_TYPEOF:
9752       /* Consume the `typeof' token.  */
9753       cp_lexer_consume_token (parser->lexer);
9754       /* Parse the operand to `typeof'.  */
9755       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9756       /* If it is not already a TYPE, take its type.  */
9757       if (!TYPE_P (type))
9758         type = finish_typeof (type);
9759
9760       if (decl_specs)
9761         cp_parser_set_decl_spec_type (decl_specs, type,
9762                                       /*user_defined_p=*/true);
9763
9764       return type;
9765
9766     default:
9767       break;
9768     }
9769
9770   /* If the type-specifier was for a built-in type, we're done.  */
9771   if (type)
9772     {
9773       tree id;
9774
9775       /* Record the type.  */
9776       if (decl_specs
9777           && (token->keyword != RID_SIGNED
9778               && token->keyword != RID_UNSIGNED
9779               && token->keyword != RID_SHORT
9780               && token->keyword != RID_LONG))
9781         cp_parser_set_decl_spec_type (decl_specs,
9782                                       type,
9783                                       /*user_defined=*/false);
9784       if (decl_specs)
9785         decl_specs->any_specifiers_p = true;
9786
9787       /* Consume the token.  */
9788       id = cp_lexer_consume_token (parser->lexer)->value;
9789
9790       /* There is no valid C++ program where a non-template type is
9791          followed by a "<".  That usually indicates that the user thought
9792          that the type was a template.  */
9793       cp_parser_check_for_invalid_template_id (parser, type);
9794
9795       return TYPE_NAME (type);
9796     }
9797
9798   /* The type-specifier must be a user-defined type.  */
9799   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9800     {
9801       bool qualified_p;
9802       bool global_p;
9803
9804       /* Don't gobble tokens or issue error messages if this is an
9805          optional type-specifier.  */
9806       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9807         cp_parser_parse_tentatively (parser);
9808
9809       /* Look for the optional `::' operator.  */
9810       global_p
9811         = (cp_parser_global_scope_opt (parser,
9812                                        /*current_scope_valid_p=*/false)
9813            != NULL_TREE);
9814       /* Look for the nested-name specifier.  */
9815       qualified_p
9816         = (cp_parser_nested_name_specifier_opt (parser,
9817                                                 /*typename_keyword_p=*/false,
9818                                                 /*check_dependency_p=*/true,
9819                                                 /*type_p=*/false,
9820                                                 /*is_declaration=*/false)
9821            != NULL_TREE);
9822       /* If we have seen a nested-name-specifier, and the next token
9823          is `template', then we are using the template-id production.  */
9824       if (parser->scope
9825           && cp_parser_optional_template_keyword (parser))
9826         {
9827           /* Look for the template-id.  */
9828           type = cp_parser_template_id (parser,
9829                                         /*template_keyword_p=*/true,
9830                                         /*check_dependency_p=*/true,
9831                                         /*is_declaration=*/false);
9832           /* If the template-id did not name a type, we are out of
9833              luck.  */
9834           if (TREE_CODE (type) != TYPE_DECL)
9835             {
9836               cp_parser_error (parser, "expected template-id for type");
9837               type = NULL_TREE;
9838             }
9839         }
9840       /* Otherwise, look for a type-name.  */
9841       else
9842         type = cp_parser_type_name (parser);
9843       /* Keep track of all name-lookups performed in class scopes.  */
9844       if (type
9845           && !global_p
9846           && !qualified_p
9847           && TREE_CODE (type) == TYPE_DECL
9848           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9849         maybe_note_name_used_in_class (DECL_NAME (type), type);
9850       /* If it didn't work out, we don't have a TYPE.  */
9851       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9852           && !cp_parser_parse_definitely (parser))
9853         type = NULL_TREE;
9854       if (type && decl_specs)
9855         cp_parser_set_decl_spec_type (decl_specs, type,
9856                                       /*user_defined=*/true);
9857     }
9858
9859   /* If we didn't get a type-name, issue an error message.  */
9860   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9861     {
9862       cp_parser_error (parser, "expected type-name");
9863       return error_mark_node;
9864     }
9865
9866   /* There is no valid C++ program where a non-template type is
9867      followed by a "<".  That usually indicates that the user thought
9868      that the type was a template.  */
9869   if (type && type != error_mark_node)
9870     {
9871       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9872          If it is, then the '<'...'>' enclose protocol names rather than
9873          template arguments, and so everything is fine.  */
9874       if (c_dialect_objc ()
9875           && (objc_is_id (type) || objc_is_class_name (type)))
9876         {
9877           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9878           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9879
9880           /* Clobber the "unqualified" type previously entered into
9881              DECL_SPECS with the new, improved protocol-qualified version.  */
9882           if (decl_specs)
9883             decl_specs->type = qual_type;
9884
9885           return qual_type;
9886         }
9887
9888       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9889     }
9890
9891   return type;
9892 }
9893
9894 /* Parse a type-name.
9895
9896    type-name:
9897      class-name
9898      enum-name
9899      typedef-name
9900
9901    enum-name:
9902      identifier
9903
9904    typedef-name:
9905      identifier
9906
9907    Returns a TYPE_DECL for the type.  */
9908
9909 static tree
9910 cp_parser_type_name (cp_parser* parser)
9911 {
9912   tree type_decl;
9913   tree identifier;
9914
9915   /* We can't know yet whether it is a class-name or not.  */
9916   cp_parser_parse_tentatively (parser);
9917   /* Try a class-name.  */
9918   type_decl = cp_parser_class_name (parser,
9919                                     /*typename_keyword_p=*/false,
9920                                     /*template_keyword_p=*/false,
9921                                     none_type,
9922                                     /*check_dependency_p=*/true,
9923                                     /*class_head_p=*/false,
9924                                     /*is_declaration=*/false);
9925   /* If it's not a class-name, keep looking.  */
9926   if (!cp_parser_parse_definitely (parser))
9927     {
9928       /* It must be a typedef-name or an enum-name.  */
9929       identifier = cp_parser_identifier (parser);
9930       if (identifier == error_mark_node)
9931         return error_mark_node;
9932
9933       /* Look up the type-name.  */
9934       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9935
9936       if (TREE_CODE (type_decl) != TYPE_DECL
9937           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9938         {
9939           /* See if this is an Objective-C type.  */
9940           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9941           tree type = objc_get_protocol_qualified_type (identifier, protos);
9942           if (type)
9943             type_decl = TYPE_NAME (type);
9944         }
9945
9946       /* Issue an error if we did not find a type-name.  */
9947       if (TREE_CODE (type_decl) != TYPE_DECL)
9948         {
9949           if (!cp_parser_simulate_error (parser))
9950             cp_parser_name_lookup_error (parser, identifier, type_decl,
9951                                          "is not a type");
9952           type_decl = error_mark_node;
9953         }
9954       /* Remember that the name was used in the definition of the
9955          current class so that we can check later to see if the
9956          meaning would have been different after the class was
9957          entirely defined.  */
9958       else if (type_decl != error_mark_node
9959                && !parser->scope)
9960         maybe_note_name_used_in_class (identifier, type_decl);
9961     }
9962
9963   return type_decl;
9964 }
9965
9966
9967 /* Parse an elaborated-type-specifier.  Note that the grammar given
9968    here incorporates the resolution to DR68.
9969
9970    elaborated-type-specifier:
9971      class-key :: [opt] nested-name-specifier [opt] identifier
9972      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9973      enum :: [opt] nested-name-specifier [opt] identifier
9974      typename :: [opt] nested-name-specifier identifier
9975      typename :: [opt] nested-name-specifier template [opt]
9976        template-id
9977
9978    GNU extension:
9979
9980    elaborated-type-specifier:
9981      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9982      class-key attributes :: [opt] nested-name-specifier [opt]
9983                template [opt] template-id
9984      enum attributes :: [opt] nested-name-specifier [opt] identifier
9985
9986    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9987    declared `friend'.  If IS_DECLARATION is TRUE, then this
9988    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9989    something is being declared.
9990
9991    Returns the TYPE specified.  */
9992
9993 static tree
9994 cp_parser_elaborated_type_specifier (cp_parser* parser,
9995                                      bool is_friend,
9996                                      bool is_declaration)
9997 {
9998   enum tag_types tag_type;
9999   tree identifier;
10000   tree type = NULL_TREE;
10001   tree attributes = NULL_TREE;
10002
10003   /* See if we're looking at the `enum' keyword.  */
10004   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10005     {
10006       /* Consume the `enum' token.  */
10007       cp_lexer_consume_token (parser->lexer);
10008       /* Remember that it's an enumeration type.  */
10009       tag_type = enum_type;
10010       /* Parse the attributes.  */
10011       attributes = cp_parser_attributes_opt (parser);
10012     }
10013   /* Or, it might be `typename'.  */
10014   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10015                                            RID_TYPENAME))
10016     {
10017       /* Consume the `typename' token.  */
10018       cp_lexer_consume_token (parser->lexer);
10019       /* Remember that it's a `typename' type.  */
10020       tag_type = typename_type;
10021       /* The `typename' keyword is only allowed in templates.  */
10022       if (!processing_template_decl)
10023         pedwarn ("using %<typename%> outside of template");
10024     }
10025   /* Otherwise it must be a class-key.  */
10026   else
10027     {
10028       tag_type = cp_parser_class_key (parser);
10029       if (tag_type == none_type)
10030         return error_mark_node;
10031       /* Parse the attributes.  */
10032       attributes = cp_parser_attributes_opt (parser);
10033     }
10034
10035   /* Look for the `::' operator.  */
10036   cp_parser_global_scope_opt (parser,
10037                               /*current_scope_valid_p=*/false);
10038   /* Look for the nested-name-specifier.  */
10039   if (tag_type == typename_type)
10040     {
10041       if (!cp_parser_nested_name_specifier (parser,
10042                                            /*typename_keyword_p=*/true,
10043                                            /*check_dependency_p=*/true,
10044                                            /*type_p=*/true,
10045                                             is_declaration))
10046         return error_mark_node;
10047     }
10048   else
10049     /* Even though `typename' is not present, the proposed resolution
10050        to Core Issue 180 says that in `class A<T>::B', `B' should be
10051        considered a type-name, even if `A<T>' is dependent.  */
10052     cp_parser_nested_name_specifier_opt (parser,
10053                                          /*typename_keyword_p=*/true,
10054                                          /*check_dependency_p=*/true,
10055                                          /*type_p=*/true,
10056                                          is_declaration);
10057   /* For everything but enumeration types, consider a template-id.  */
10058   /* For an enumeration type, consider only a plain identifier.  */
10059   if (tag_type != enum_type)
10060     {
10061       bool template_p = false;
10062       tree decl;
10063
10064       /* Allow the `template' keyword.  */
10065       template_p = cp_parser_optional_template_keyword (parser);
10066       /* If we didn't see `template', we don't know if there's a
10067          template-id or not.  */
10068       if (!template_p)
10069         cp_parser_parse_tentatively (parser);
10070       /* Parse the template-id.  */
10071       decl = cp_parser_template_id (parser, template_p,
10072                                     /*check_dependency_p=*/true,
10073                                     is_declaration);
10074       /* If we didn't find a template-id, look for an ordinary
10075          identifier.  */
10076       if (!template_p && !cp_parser_parse_definitely (parser))
10077         ;
10078       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10079          in effect, then we must assume that, upon instantiation, the
10080          template will correspond to a class.  */
10081       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10082                && tag_type == typename_type)
10083         type = make_typename_type (parser->scope, decl,
10084                                    typename_type,
10085                                    /*complain=*/tf_error);
10086       else
10087         type = TREE_TYPE (decl);
10088     }
10089
10090   if (!type)
10091     {
10092       identifier = cp_parser_identifier (parser);
10093
10094       if (identifier == error_mark_node)
10095         {
10096           parser->scope = NULL_TREE;
10097           return error_mark_node;
10098         }
10099
10100       /* For a `typename', we needn't call xref_tag.  */
10101       if (tag_type == typename_type
10102           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10103         return cp_parser_make_typename_type (parser, parser->scope,
10104                                              identifier);
10105       /* Look up a qualified name in the usual way.  */
10106       if (parser->scope)
10107         {
10108           tree decl;
10109
10110           decl = cp_parser_lookup_name (parser, identifier,
10111                                         tag_type,
10112                                         /*is_template=*/false,
10113                                         /*is_namespace=*/false,
10114                                         /*check_dependency=*/true,
10115                                         /*ambiguous_decls=*/NULL);
10116
10117           /* If we are parsing friend declaration, DECL may be a
10118              TEMPLATE_DECL tree node here.  However, we need to check
10119              whether this TEMPLATE_DECL results in valid code.  Consider
10120              the following example:
10121
10122                namespace N {
10123                  template <class T> class C {};
10124                }
10125                class X {
10126                  template <class T> friend class N::C; // #1, valid code
10127                };
10128                template <class T> class Y {
10129                  friend class N::C;                    // #2, invalid code
10130                };
10131
10132              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10133              name lookup of `N::C'.  We see that friend declaration must
10134              be template for the code to be valid.  Note that
10135              processing_template_decl does not work here since it is
10136              always 1 for the above two cases.  */
10137
10138           decl = (cp_parser_maybe_treat_template_as_class
10139                   (decl, /*tag_name_p=*/is_friend
10140                          && parser->num_template_parameter_lists));
10141
10142           if (TREE_CODE (decl) != TYPE_DECL)
10143             {
10144               cp_parser_diagnose_invalid_type_name (parser,
10145                                                     parser->scope,
10146                                                     identifier);
10147               return error_mark_node;
10148             }
10149
10150           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10151             check_elaborated_type_specifier
10152               (tag_type, decl,
10153                (parser->num_template_parameter_lists
10154                 || DECL_SELF_REFERENCE_P (decl)));
10155
10156           type = TREE_TYPE (decl);
10157         }
10158       else
10159         {
10160           /* An elaborated-type-specifier sometimes introduces a new type and
10161              sometimes names an existing type.  Normally, the rule is that it
10162              introduces a new type only if there is not an existing type of
10163              the same name already in scope.  For example, given:
10164
10165                struct S {};
10166                void f() { struct S s; }
10167
10168              the `struct S' in the body of `f' is the same `struct S' as in
10169              the global scope; the existing definition is used.  However, if
10170              there were no global declaration, this would introduce a new
10171              local class named `S'.
10172
10173              An exception to this rule applies to the following code:
10174
10175                namespace N { struct S; }
10176
10177              Here, the elaborated-type-specifier names a new type
10178              unconditionally; even if there is already an `S' in the
10179              containing scope this declaration names a new type.
10180              This exception only applies if the elaborated-type-specifier
10181              forms the complete declaration:
10182
10183                [class.name]
10184
10185                A declaration consisting solely of `class-key identifier ;' is
10186                either a redeclaration of the name in the current scope or a
10187                forward declaration of the identifier as a class name.  It
10188                introduces the name into the current scope.
10189
10190              We are in this situation precisely when the next token is a `;'.
10191
10192              An exception to the exception is that a `friend' declaration does
10193              *not* name a new type; i.e., given:
10194
10195                struct S { friend struct T; };
10196
10197              `T' is not a new type in the scope of `S'.
10198
10199              Also, `new struct S' or `sizeof (struct S)' never results in the
10200              definition of a new type; a new type can only be declared in a
10201              declaration context.  */
10202
10203           tag_scope ts;
10204           bool template_p;
10205
10206           if (is_friend)
10207             /* Friends have special name lookup rules.  */
10208             ts = ts_within_enclosing_non_class;
10209           else if (is_declaration
10210                    && cp_lexer_next_token_is (parser->lexer,
10211                                               CPP_SEMICOLON))
10212             /* This is a `class-key identifier ;' */
10213             ts = ts_current;
10214           else
10215             ts = ts_global;
10216
10217           template_p =
10218             (parser->num_template_parameter_lists
10219              && (cp_parser_next_token_starts_class_definition_p (parser)
10220                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10221           /* An unqualified name was used to reference this type, so
10222              there were no qualifying templates.  */
10223           if (!cp_parser_check_template_parameters (parser,
10224                                                     /*num_templates=*/0))
10225             return error_mark_node;
10226           type = xref_tag (tag_type, identifier, ts, template_p);
10227         }
10228     }
10229
10230   if (type == error_mark_node)
10231     return error_mark_node;
10232
10233   /* Allow attributes on forward declarations of classes.  */
10234   if (attributes)
10235     {
10236       if (TREE_CODE (type) == TYPENAME_TYPE)
10237         warning (OPT_Wattributes,
10238                  "attributes ignored on uninstantiated type");
10239       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10240                && ! processing_explicit_instantiation)
10241         warning (OPT_Wattributes,
10242                  "attributes ignored on template instantiation");
10243       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10244         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10245       else
10246         warning (OPT_Wattributes,
10247                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10248     }
10249
10250   if (tag_type != enum_type)
10251     cp_parser_check_class_key (tag_type, type);
10252
10253   /* A "<" cannot follow an elaborated type specifier.  If that
10254      happens, the user was probably trying to form a template-id.  */
10255   cp_parser_check_for_invalid_template_id (parser, type);
10256
10257   return type;
10258 }
10259
10260 /* Parse an enum-specifier.
10261
10262    enum-specifier:
10263      enum identifier [opt] { enumerator-list [opt] }
10264
10265    GNU Extensions:
10266      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10267        attributes[opt]
10268
10269    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10270    if the token stream isn't an enum-specifier after all.  */
10271
10272 static tree
10273 cp_parser_enum_specifier (cp_parser* parser)
10274 {
10275   tree identifier;
10276   tree type;
10277   tree attributes;
10278
10279   /* Parse tentatively so that we can back up if we don't find a
10280      enum-specifier.  */
10281   cp_parser_parse_tentatively (parser);
10282
10283   /* Caller guarantees that the current token is 'enum', an identifier
10284      possibly follows, and the token after that is an opening brace.
10285      If we don't have an identifier, fabricate an anonymous name for
10286      the enumeration being defined.  */
10287   cp_lexer_consume_token (parser->lexer);
10288
10289   attributes = cp_parser_attributes_opt (parser);
10290
10291   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10292     identifier = cp_parser_identifier (parser);
10293   else
10294     identifier = make_anon_name ();
10295
10296   /* Look for the `{' but don't consume it yet.  */
10297   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10298     cp_parser_simulate_error (parser);
10299
10300   if (!cp_parser_parse_definitely (parser))
10301     return NULL_TREE;
10302
10303   /* Issue an error message if type-definitions are forbidden here.  */
10304   cp_parser_check_type_definition (parser);
10305
10306   /* Create the new type.  We do this before consuming the opening brace
10307      so the enum will be recorded as being on the line of its tag (or the
10308      'enum' keyword, if there is no tag).  */
10309   type = start_enum (identifier);
10310
10311   /* Consume the opening brace.  */
10312   cp_lexer_consume_token (parser->lexer);
10313
10314   if (type == error_mark_node)
10315     {
10316       cp_parser_skip_to_end_of_block_or_statement (parser);
10317       return error_mark_node;
10318     }
10319
10320   /* If the next token is not '}', then there are some enumerators.  */
10321   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10322     cp_parser_enumerator_list (parser, type);
10323
10324   /* Consume the final '}'.  */
10325   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10326
10327   /* Look for trailing attributes to apply to this enumeration, and
10328      apply them if appropriate.  */
10329   if (cp_parser_allow_gnu_extensions_p (parser))
10330     {
10331       tree trailing_attr = cp_parser_attributes_opt (parser);
10332       cplus_decl_attributes (&type,
10333                              trailing_attr,
10334                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10335     }
10336
10337   /* Finish up the enumeration.  */
10338   finish_enum (type);
10339
10340   return type;
10341 }
10342
10343 /* Parse an enumerator-list.  The enumerators all have the indicated
10344    TYPE.
10345
10346    enumerator-list:
10347      enumerator-definition
10348      enumerator-list , enumerator-definition  */
10349
10350 static void
10351 cp_parser_enumerator_list (cp_parser* parser, tree type)
10352 {
10353   while (true)
10354     {
10355       /* Parse an enumerator-definition.  */
10356       cp_parser_enumerator_definition (parser, type);
10357
10358       /* If the next token is not a ',', we've reached the end of
10359          the list.  */
10360       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10361         break;
10362       /* Otherwise, consume the `,' and keep going.  */
10363       cp_lexer_consume_token (parser->lexer);
10364       /* If the next token is a `}', there is a trailing comma.  */
10365       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10366         {
10367           if (pedantic && !in_system_header)
10368             pedwarn ("comma at end of enumerator list");
10369           break;
10370         }
10371     }
10372 }
10373
10374 /* Parse an enumerator-definition.  The enumerator has the indicated
10375    TYPE.
10376
10377    enumerator-definition:
10378      enumerator
10379      enumerator = constant-expression
10380
10381    enumerator:
10382      identifier  */
10383
10384 static void
10385 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10386 {
10387   tree identifier;
10388   tree value;
10389
10390   /* Look for the identifier.  */
10391   identifier = cp_parser_identifier (parser);
10392   if (identifier == error_mark_node)
10393     return;
10394
10395   /* If the next token is an '=', then there is an explicit value.  */
10396   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10397     {
10398       /* Consume the `=' token.  */
10399       cp_lexer_consume_token (parser->lexer);
10400       /* Parse the value.  */
10401       value = cp_parser_constant_expression (parser,
10402                                              /*allow_non_constant_p=*/false,
10403                                              NULL);
10404     }
10405   else
10406     value = NULL_TREE;
10407
10408   /* Create the enumerator.  */
10409   build_enumerator (identifier, value, type);
10410 }
10411
10412 /* Parse a namespace-name.
10413
10414    namespace-name:
10415      original-namespace-name
10416      namespace-alias
10417
10418    Returns the NAMESPACE_DECL for the namespace.  */
10419
10420 static tree
10421 cp_parser_namespace_name (cp_parser* parser)
10422 {
10423   tree identifier;
10424   tree namespace_decl;
10425
10426   /* Get the name of the namespace.  */
10427   identifier = cp_parser_identifier (parser);
10428   if (identifier == error_mark_node)
10429     return error_mark_node;
10430
10431   /* Look up the identifier in the currently active scope.  Look only
10432      for namespaces, due to:
10433
10434        [basic.lookup.udir]
10435
10436        When looking up a namespace-name in a using-directive or alias
10437        definition, only namespace names are considered.
10438
10439      And:
10440
10441        [basic.lookup.qual]
10442
10443        During the lookup of a name preceding the :: scope resolution
10444        operator, object, function, and enumerator names are ignored.
10445
10446      (Note that cp_parser_class_or_namespace_name only calls this
10447      function if the token after the name is the scope resolution
10448      operator.)  */
10449   namespace_decl = cp_parser_lookup_name (parser, identifier,
10450                                           none_type,
10451                                           /*is_template=*/false,
10452                                           /*is_namespace=*/true,
10453                                           /*check_dependency=*/true,
10454                                           /*ambiguous_decls=*/NULL);
10455   /* If it's not a namespace, issue an error.  */
10456   if (namespace_decl == error_mark_node
10457       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10458     {
10459       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10460         error ("%qD is not a namespace-name", identifier);
10461       cp_parser_error (parser, "expected namespace-name");
10462       namespace_decl = error_mark_node;
10463     }
10464
10465   return namespace_decl;
10466 }
10467
10468 /* Parse a namespace-definition.
10469
10470    namespace-definition:
10471      named-namespace-definition
10472      unnamed-namespace-definition
10473
10474    named-namespace-definition:
10475      original-namespace-definition
10476      extension-namespace-definition
10477
10478    original-namespace-definition:
10479      namespace identifier { namespace-body }
10480
10481    extension-namespace-definition:
10482      namespace original-namespace-name { namespace-body }
10483
10484    unnamed-namespace-definition:
10485      namespace { namespace-body } */
10486
10487 static void
10488 cp_parser_namespace_definition (cp_parser* parser)
10489 {
10490   tree identifier, attribs;
10491
10492   /* Look for the `namespace' keyword.  */
10493   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10494
10495   /* Get the name of the namespace.  We do not attempt to distinguish
10496      between an original-namespace-definition and an
10497      extension-namespace-definition at this point.  The semantic
10498      analysis routines are responsible for that.  */
10499   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10500     identifier = cp_parser_identifier (parser);
10501   else
10502     identifier = NULL_TREE;
10503
10504   /* Parse any specified attributes.  */
10505   attribs = cp_parser_attributes_opt (parser);
10506
10507   /* Look for the `{' to start the namespace.  */
10508   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10509   /* Start the namespace.  */
10510   push_namespace_with_attribs (identifier, attribs);
10511   /* Parse the body of the namespace.  */
10512   cp_parser_namespace_body (parser);
10513   /* Finish the namespace.  */
10514   pop_namespace ();
10515   /* Look for the final `}'.  */
10516   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10517 }
10518
10519 /* Parse a namespace-body.
10520
10521    namespace-body:
10522      declaration-seq [opt]  */
10523
10524 static void
10525 cp_parser_namespace_body (cp_parser* parser)
10526 {
10527   cp_parser_declaration_seq_opt (parser);
10528 }
10529
10530 /* Parse a namespace-alias-definition.
10531
10532    namespace-alias-definition:
10533      namespace identifier = qualified-namespace-specifier ;  */
10534
10535 static void
10536 cp_parser_namespace_alias_definition (cp_parser* parser)
10537 {
10538   tree identifier;
10539   tree namespace_specifier;
10540
10541   /* Look for the `namespace' keyword.  */
10542   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10543   /* Look for the identifier.  */
10544   identifier = cp_parser_identifier (parser);
10545   if (identifier == error_mark_node)
10546     return;
10547   /* Look for the `=' token.  */
10548   cp_parser_require (parser, CPP_EQ, "`='");
10549   /* Look for the qualified-namespace-specifier.  */
10550   namespace_specifier
10551     = cp_parser_qualified_namespace_specifier (parser);
10552   /* Look for the `;' token.  */
10553   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10554
10555   /* Register the alias in the symbol table.  */
10556   do_namespace_alias (identifier, namespace_specifier);
10557 }
10558
10559 /* Parse a qualified-namespace-specifier.
10560
10561    qualified-namespace-specifier:
10562      :: [opt] nested-name-specifier [opt] namespace-name
10563
10564    Returns a NAMESPACE_DECL corresponding to the specified
10565    namespace.  */
10566
10567 static tree
10568 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10569 {
10570   /* Look for the optional `::'.  */
10571   cp_parser_global_scope_opt (parser,
10572                               /*current_scope_valid_p=*/false);
10573
10574   /* Look for the optional nested-name-specifier.  */
10575   cp_parser_nested_name_specifier_opt (parser,
10576                                        /*typename_keyword_p=*/false,
10577                                        /*check_dependency_p=*/true,
10578                                        /*type_p=*/false,
10579                                        /*is_declaration=*/true);
10580
10581   return cp_parser_namespace_name (parser);
10582 }
10583
10584 /* Parse a using-declaration.
10585
10586    using-declaration:
10587      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10588      using :: unqualified-id ;  */
10589
10590 static void
10591 cp_parser_using_declaration (cp_parser* parser)
10592 {
10593   cp_token *token;
10594   bool typename_p = false;
10595   bool global_scope_p;
10596   tree decl;
10597   tree identifier;
10598   tree qscope;
10599
10600   /* Look for the `using' keyword.  */
10601   cp_parser_require_keyword (parser, RID_USING, "`using'");
10602
10603   /* Peek at the next token.  */
10604   token = cp_lexer_peek_token (parser->lexer);
10605   /* See if it's `typename'.  */
10606   if (token->keyword == RID_TYPENAME)
10607     {
10608       /* Remember that we've seen it.  */
10609       typename_p = true;
10610       /* Consume the `typename' token.  */
10611       cp_lexer_consume_token (parser->lexer);
10612     }
10613
10614   /* Look for the optional global scope qualification.  */
10615   global_scope_p
10616     = (cp_parser_global_scope_opt (parser,
10617                                    /*current_scope_valid_p=*/false)
10618        != NULL_TREE);
10619
10620   /* If we saw `typename', or didn't see `::', then there must be a
10621      nested-name-specifier present.  */
10622   if (typename_p || !global_scope_p)
10623     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10624                                               /*check_dependency_p=*/true,
10625                                               /*type_p=*/false,
10626                                               /*is_declaration=*/true);
10627   /* Otherwise, we could be in either of the two productions.  In that
10628      case, treat the nested-name-specifier as optional.  */
10629   else
10630     qscope = cp_parser_nested_name_specifier_opt (parser,
10631                                                   /*typename_keyword_p=*/false,
10632                                                   /*check_dependency_p=*/true,
10633                                                   /*type_p=*/false,
10634                                                   /*is_declaration=*/true);
10635   if (!qscope)
10636     qscope = global_namespace;
10637
10638   /* Parse the unqualified-id.  */
10639   identifier = cp_parser_unqualified_id (parser,
10640                                          /*template_keyword_p=*/false,
10641                                          /*check_dependency_p=*/true,
10642                                          /*declarator_p=*/true,
10643                                          /*optional_p=*/false);
10644
10645   /* The function we call to handle a using-declaration is different
10646      depending on what scope we are in.  */
10647   if (qscope == error_mark_node || identifier == error_mark_node)
10648     ;
10649   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10650            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10651     /* [namespace.udecl]
10652
10653        A using declaration shall not name a template-id.  */
10654     error ("a template-id may not appear in a using-declaration");
10655   else
10656     {
10657       if (at_class_scope_p ())
10658         {
10659           /* Create the USING_DECL.  */
10660           decl = do_class_using_decl (parser->scope, identifier);
10661           /* Add it to the list of members in this class.  */
10662           finish_member_declaration (decl);
10663         }
10664       else
10665         {
10666           decl = cp_parser_lookup_name_simple (parser, identifier);
10667           if (decl == error_mark_node)
10668             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10669           else if (!at_namespace_scope_p ())
10670             do_local_using_decl (decl, qscope, identifier);
10671           else
10672             do_toplevel_using_decl (decl, qscope, identifier);
10673         }
10674     }
10675
10676   /* Look for the final `;'.  */
10677   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10678 }
10679
10680 /* Parse a using-directive.
10681
10682    using-directive:
10683      using namespace :: [opt] nested-name-specifier [opt]
10684        namespace-name ;  */
10685
10686 static void
10687 cp_parser_using_directive (cp_parser* parser)
10688 {
10689   tree namespace_decl;
10690   tree attribs;
10691
10692   /* Look for the `using' keyword.  */
10693   cp_parser_require_keyword (parser, RID_USING, "`using'");
10694   /* And the `namespace' keyword.  */
10695   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10696   /* Look for the optional `::' operator.  */
10697   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10698   /* And the optional nested-name-specifier.  */
10699   cp_parser_nested_name_specifier_opt (parser,
10700                                        /*typename_keyword_p=*/false,
10701                                        /*check_dependency_p=*/true,
10702                                        /*type_p=*/false,
10703                                        /*is_declaration=*/true);
10704   /* Get the namespace being used.  */
10705   namespace_decl = cp_parser_namespace_name (parser);
10706   /* And any specified attributes.  */
10707   attribs = cp_parser_attributes_opt (parser);
10708   /* Update the symbol table.  */
10709   parse_using_directive (namespace_decl, attribs);
10710   /* Look for the final `;'.  */
10711   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10712 }
10713
10714 /* Parse an asm-definition.
10715
10716    asm-definition:
10717      asm ( string-literal ) ;
10718
10719    GNU Extension:
10720
10721    asm-definition:
10722      asm volatile [opt] ( string-literal ) ;
10723      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10724      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10725                           : asm-operand-list [opt] ) ;
10726      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10727                           : asm-operand-list [opt]
10728                           : asm-operand-list [opt] ) ;  */
10729
10730 static void
10731 cp_parser_asm_definition (cp_parser* parser)
10732 {
10733   tree string;
10734   tree outputs = NULL_TREE;
10735   tree inputs = NULL_TREE;
10736   tree clobbers = NULL_TREE;
10737   tree asm_stmt;
10738   bool volatile_p = false;
10739   bool extended_p = false;
10740
10741   /* Look for the `asm' keyword.  */
10742   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10743   /* See if the next token is `volatile'.  */
10744   if (cp_parser_allow_gnu_extensions_p (parser)
10745       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10746     {
10747       /* Remember that we saw the `volatile' keyword.  */
10748       volatile_p = true;
10749       /* Consume the token.  */
10750       cp_lexer_consume_token (parser->lexer);
10751     }
10752   /* Look for the opening `('.  */
10753   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10754     return;
10755   /* Look for the string.  */
10756   string = cp_parser_string_literal (parser, false, false);
10757   if (string == error_mark_node)
10758     {
10759       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10760                                              /*consume_paren=*/true);
10761       return;
10762     }
10763
10764   /* If we're allowing GNU extensions, check for the extended assembly
10765      syntax.  Unfortunately, the `:' tokens need not be separated by
10766      a space in C, and so, for compatibility, we tolerate that here
10767      too.  Doing that means that we have to treat the `::' operator as
10768      two `:' tokens.  */
10769   if (cp_parser_allow_gnu_extensions_p (parser)
10770       && at_function_scope_p ()
10771       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10772           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10773     {
10774       bool inputs_p = false;
10775       bool clobbers_p = false;
10776
10777       /* The extended syntax was used.  */
10778       extended_p = true;
10779
10780       /* Look for outputs.  */
10781       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10782         {
10783           /* Consume the `:'.  */
10784           cp_lexer_consume_token (parser->lexer);
10785           /* Parse the output-operands.  */
10786           if (cp_lexer_next_token_is_not (parser->lexer,
10787                                           CPP_COLON)
10788               && cp_lexer_next_token_is_not (parser->lexer,
10789                                              CPP_SCOPE)
10790               && cp_lexer_next_token_is_not (parser->lexer,
10791                                              CPP_CLOSE_PAREN))
10792             outputs = cp_parser_asm_operand_list (parser);
10793         }
10794       /* If the next token is `::', there are no outputs, and the
10795          next token is the beginning of the inputs.  */
10796       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10797         /* The inputs are coming next.  */
10798         inputs_p = true;
10799
10800       /* Look for inputs.  */
10801       if (inputs_p
10802           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10803         {
10804           /* Consume the `:' or `::'.  */
10805           cp_lexer_consume_token (parser->lexer);
10806           /* Parse the output-operands.  */
10807           if (cp_lexer_next_token_is_not (parser->lexer,
10808                                           CPP_COLON)
10809               && cp_lexer_next_token_is_not (parser->lexer,
10810                                              CPP_CLOSE_PAREN))
10811             inputs = cp_parser_asm_operand_list (parser);
10812         }
10813       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10814         /* The clobbers are coming next.  */
10815         clobbers_p = true;
10816
10817       /* Look for clobbers.  */
10818       if (clobbers_p
10819           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10820         {
10821           /* Consume the `:' or `::'.  */
10822           cp_lexer_consume_token (parser->lexer);
10823           /* Parse the clobbers.  */
10824           if (cp_lexer_next_token_is_not (parser->lexer,
10825                                           CPP_CLOSE_PAREN))
10826             clobbers = cp_parser_asm_clobber_list (parser);
10827         }
10828     }
10829   /* Look for the closing `)'.  */
10830   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10831     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10832                                            /*consume_paren=*/true);
10833   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10834
10835   /* Create the ASM_EXPR.  */
10836   if (at_function_scope_p ())
10837     {
10838       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10839                                   inputs, clobbers);
10840       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10841       if (!extended_p)
10842         {
10843           tree temp = asm_stmt;
10844           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10845             temp = TREE_OPERAND (temp, 0);
10846
10847           ASM_INPUT_P (temp) = 1;
10848         }
10849     }
10850   else
10851     cgraph_add_asm_node (string);
10852 }
10853
10854 /* Declarators [gram.dcl.decl] */
10855
10856 /* Parse an init-declarator.
10857
10858    init-declarator:
10859      declarator initializer [opt]
10860
10861    GNU Extension:
10862
10863    init-declarator:
10864      declarator asm-specification [opt] attributes [opt] initializer [opt]
10865
10866    function-definition:
10867      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10868        function-body
10869      decl-specifier-seq [opt] declarator function-try-block
10870
10871    GNU Extension:
10872
10873    function-definition:
10874      __extension__ function-definition
10875
10876    The DECL_SPECIFIERS apply to this declarator.  Returns a
10877    representation of the entity declared.  If MEMBER_P is TRUE, then
10878    this declarator appears in a class scope.  The new DECL created by
10879    this declarator is returned.
10880
10881    The CHECKS are access checks that should be performed once we know
10882    what entity is being declared (and, therefore, what classes have
10883    befriended it).
10884
10885    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10886    for a function-definition here as well.  If the declarator is a
10887    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10888    be TRUE upon return.  By that point, the function-definition will
10889    have been completely parsed.
10890
10891    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10892    is FALSE.  */
10893
10894 static tree
10895 cp_parser_init_declarator (cp_parser* parser,
10896                            cp_decl_specifier_seq *decl_specifiers,
10897                            tree checks,
10898                            bool function_definition_allowed_p,
10899                            bool member_p,
10900                            int declares_class_or_enum,
10901                            bool* function_definition_p)
10902 {
10903   cp_token *token;
10904   cp_declarator *declarator;
10905   tree prefix_attributes;
10906   tree attributes;
10907   tree asm_specification;
10908   tree initializer;
10909   tree decl = NULL_TREE;
10910   tree scope;
10911   bool is_initialized;
10912   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
10913      initialized with "= ..", CPP_OPEN_PAREN if initialized with
10914      "(...)".  */
10915   enum cpp_ttype initialization_kind;
10916   bool is_parenthesized_init = false;
10917   bool is_non_constant_init;
10918   int ctor_dtor_or_conv_p;
10919   bool friend_p;
10920   tree pushed_scope = NULL;
10921
10922   /* Gather the attributes that were provided with the
10923      decl-specifiers.  */
10924   prefix_attributes = decl_specifiers->attributes;
10925
10926   /* Assume that this is not the declarator for a function
10927      definition.  */
10928   if (function_definition_p)
10929     *function_definition_p = false;
10930
10931   /* Defer access checks while parsing the declarator; we cannot know
10932      what names are accessible until we know what is being
10933      declared.  */
10934   resume_deferring_access_checks ();
10935
10936   /* Parse the declarator.  */
10937   declarator
10938     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10939                             &ctor_dtor_or_conv_p,
10940                             /*parenthesized_p=*/NULL,
10941                             /*member_p=*/false);
10942   /* Gather up the deferred checks.  */
10943   stop_deferring_access_checks ();
10944
10945   /* If the DECLARATOR was erroneous, there's no need to go
10946      further.  */
10947   if (declarator == cp_error_declarator)
10948     return error_mark_node;
10949
10950   if (declares_class_or_enum & 2)
10951     cp_parser_check_for_definition_in_return_type (declarator,
10952                                                    decl_specifiers->type);
10953
10954   /* Figure out what scope the entity declared by the DECLARATOR is
10955      located in.  `grokdeclarator' sometimes changes the scope, so
10956      we compute it now.  */
10957   scope = get_scope_of_declarator (declarator);
10958
10959   /* If we're allowing GNU extensions, look for an asm-specification
10960      and attributes.  */
10961   if (cp_parser_allow_gnu_extensions_p (parser))
10962     {
10963       /* Look for an asm-specification.  */
10964       asm_specification = cp_parser_asm_specification_opt (parser);
10965       /* And attributes.  */
10966       attributes = cp_parser_attributes_opt (parser);
10967     }
10968   else
10969     {
10970       asm_specification = NULL_TREE;
10971       attributes = NULL_TREE;
10972     }
10973
10974   /* Peek at the next token.  */
10975   token = cp_lexer_peek_token (parser->lexer);
10976   /* Check to see if the token indicates the start of a
10977      function-definition.  */
10978   if (cp_parser_token_starts_function_definition_p (token))
10979     {
10980       if (!function_definition_allowed_p)
10981         {
10982           /* If a function-definition should not appear here, issue an
10983              error message.  */
10984           cp_parser_error (parser,
10985                            "a function-definition is not allowed here");
10986           return error_mark_node;
10987         }
10988       else
10989         {
10990           /* Neither attributes nor an asm-specification are allowed
10991              on a function-definition.  */
10992           if (asm_specification)
10993             error ("an asm-specification is not allowed on a function-definition");
10994           if (attributes)
10995             error ("attributes are not allowed on a function-definition");
10996           /* This is a function-definition.  */
10997           *function_definition_p = true;
10998
10999           /* Parse the function definition.  */
11000           if (member_p)
11001             decl = cp_parser_save_member_function_body (parser,
11002                                                         decl_specifiers,
11003                                                         declarator,
11004                                                         prefix_attributes);
11005           else
11006             decl
11007               = (cp_parser_function_definition_from_specifiers_and_declarator
11008                  (parser, decl_specifiers, prefix_attributes, declarator));
11009
11010           return decl;
11011         }
11012     }
11013
11014   /* [dcl.dcl]
11015
11016      Only in function declarations for constructors, destructors, and
11017      type conversions can the decl-specifier-seq be omitted.
11018
11019      We explicitly postpone this check past the point where we handle
11020      function-definitions because we tolerate function-definitions
11021      that are missing their return types in some modes.  */
11022   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11023     {
11024       cp_parser_error (parser,
11025                        "expected constructor, destructor, or type conversion");
11026       return error_mark_node;
11027     }
11028
11029   /* An `=' or an `(' indicates an initializer.  */
11030   if (token->type == CPP_EQ
11031       || token->type == CPP_OPEN_PAREN)
11032     {
11033       is_initialized = true;
11034       initialization_kind = token->type;
11035     }
11036   else
11037     {
11038       /* If the init-declarator isn't initialized and isn't followed by a
11039          `,' or `;', it's not a valid init-declarator.  */
11040       if (token->type != CPP_COMMA
11041           && token->type != CPP_SEMICOLON)
11042         {
11043           cp_parser_error (parser, "expected initializer");
11044           return error_mark_node;
11045         }
11046       is_initialized = false;
11047       initialization_kind = CPP_EOF;
11048     }
11049
11050   /* Because start_decl has side-effects, we should only call it if we
11051      know we're going ahead.  By this point, we know that we cannot
11052      possibly be looking at any other construct.  */
11053   cp_parser_commit_to_tentative_parse (parser);
11054
11055   /* If the decl specifiers were bad, issue an error now that we're
11056      sure this was intended to be a declarator.  Then continue
11057      declaring the variable(s), as int, to try to cut down on further
11058      errors.  */
11059   if (decl_specifiers->any_specifiers_p
11060       && decl_specifiers->type == error_mark_node)
11061     {
11062       cp_parser_error (parser, "invalid type in declaration");
11063       decl_specifiers->type = integer_type_node;
11064     }
11065
11066   /* Check to see whether or not this declaration is a friend.  */
11067   friend_p = cp_parser_friend_p (decl_specifiers);
11068
11069   /* Check that the number of template-parameter-lists is OK.  */
11070   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11071     return error_mark_node;
11072
11073   /* Enter the newly declared entry in the symbol table.  If we're
11074      processing a declaration in a class-specifier, we wait until
11075      after processing the initializer.  */
11076   if (!member_p)
11077     {
11078       if (parser->in_unbraced_linkage_specification_p)
11079         decl_specifiers->storage_class = sc_extern;
11080       decl = start_decl (declarator, decl_specifiers,
11081                          is_initialized, attributes, prefix_attributes,
11082                          &pushed_scope);
11083     }
11084   else if (scope)
11085     /* Enter the SCOPE.  That way unqualified names appearing in the
11086        initializer will be looked up in SCOPE.  */
11087     pushed_scope = push_scope (scope);
11088
11089   /* Perform deferred access control checks, now that we know in which
11090      SCOPE the declared entity resides.  */
11091   if (!member_p && decl)
11092     {
11093       tree saved_current_function_decl = NULL_TREE;
11094
11095       /* If the entity being declared is a function, pretend that we
11096          are in its scope.  If it is a `friend', it may have access to
11097          things that would not otherwise be accessible.  */
11098       if (TREE_CODE (decl) == FUNCTION_DECL)
11099         {
11100           saved_current_function_decl = current_function_decl;
11101           current_function_decl = decl;
11102         }
11103
11104       /* Perform access checks for template parameters.  */
11105       cp_parser_perform_template_parameter_access_checks (checks);
11106
11107       /* Perform the access control checks for the declarator and the
11108          the decl-specifiers.  */
11109       perform_deferred_access_checks ();
11110
11111       /* Restore the saved value.  */
11112       if (TREE_CODE (decl) == FUNCTION_DECL)
11113         current_function_decl = saved_current_function_decl;
11114     }
11115
11116   /* Parse the initializer.  */
11117   initializer = NULL_TREE;
11118   is_parenthesized_init = false;
11119   is_non_constant_init = true;
11120   if (is_initialized)
11121     {
11122       if (declarator->kind == cdk_function
11123           && declarator->declarator->kind == cdk_id
11124           && initialization_kind == CPP_EQ)
11125         initializer = cp_parser_pure_specifier (parser);
11126       else
11127         initializer = cp_parser_initializer (parser,
11128                                              &is_parenthesized_init,
11129                                              &is_non_constant_init);
11130     }
11131
11132   /* The old parser allows attributes to appear after a parenthesized
11133      initializer.  Mark Mitchell proposed removing this functionality
11134      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11135      attributes -- but ignores them.  */
11136   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11137     if (cp_parser_attributes_opt (parser))
11138       warning (OPT_Wattributes,
11139                "attributes after parenthesized initializer ignored");
11140
11141   /* For an in-class declaration, use `grokfield' to create the
11142      declaration.  */
11143   if (member_p)
11144     {
11145       if (pushed_scope)
11146         {
11147           pop_scope (pushed_scope);
11148           pushed_scope = false;
11149         }
11150       decl = grokfield (declarator, decl_specifiers,
11151                         initializer, !is_non_constant_init,
11152                         /*asmspec=*/NULL_TREE,
11153                         prefix_attributes);
11154       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11155         cp_parser_save_default_args (parser, decl);
11156     }
11157
11158   /* Finish processing the declaration.  But, skip friend
11159      declarations.  */
11160   if (!friend_p && decl && decl != error_mark_node)
11161     {
11162       cp_finish_decl (decl,
11163                       initializer, !is_non_constant_init,
11164                       asm_specification,
11165                       /* If the initializer is in parentheses, then this is
11166                          a direct-initialization, which means that an
11167                          `explicit' constructor is OK.  Otherwise, an
11168                          `explicit' constructor cannot be used.  */
11169                       ((is_parenthesized_init || !is_initialized)
11170                      ? 0 : LOOKUP_ONLYCONVERTING));
11171     }
11172   if (!friend_p && pushed_scope)
11173     pop_scope (pushed_scope);
11174
11175   return decl;
11176 }
11177
11178 /* Parse a declarator.
11179
11180    declarator:
11181      direct-declarator
11182      ptr-operator declarator
11183
11184    abstract-declarator:
11185      ptr-operator abstract-declarator [opt]
11186      direct-abstract-declarator
11187
11188    GNU Extensions:
11189
11190    declarator:
11191      attributes [opt] direct-declarator
11192      attributes [opt] ptr-operator declarator
11193
11194    abstract-declarator:
11195      attributes [opt] ptr-operator abstract-declarator [opt]
11196      attributes [opt] direct-abstract-declarator
11197
11198    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11199    detect constructor, destructor or conversion operators. It is set
11200    to -1 if the declarator is a name, and +1 if it is a
11201    function. Otherwise it is set to zero. Usually you just want to
11202    test for >0, but internally the negative value is used.
11203
11204    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11205    a decl-specifier-seq unless it declares a constructor, destructor,
11206    or conversion.  It might seem that we could check this condition in
11207    semantic analysis, rather than parsing, but that makes it difficult
11208    to handle something like `f()'.  We want to notice that there are
11209    no decl-specifiers, and therefore realize that this is an
11210    expression, not a declaration.)
11211
11212    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11213    the declarator is a direct-declarator of the form "(...)".
11214
11215    MEMBER_P is true iff this declarator is a member-declarator.  */
11216
11217 static cp_declarator *
11218 cp_parser_declarator (cp_parser* parser,
11219                       cp_parser_declarator_kind dcl_kind,
11220                       int* ctor_dtor_or_conv_p,
11221                       bool* parenthesized_p,
11222                       bool member_p)
11223 {
11224   cp_token *token;
11225   cp_declarator *declarator;
11226   enum tree_code code;
11227   cp_cv_quals cv_quals;
11228   tree class_type;
11229   tree attributes = NULL_TREE;
11230
11231   /* Assume this is not a constructor, destructor, or type-conversion
11232      operator.  */
11233   if (ctor_dtor_or_conv_p)
11234     *ctor_dtor_or_conv_p = 0;
11235
11236   if (cp_parser_allow_gnu_extensions_p (parser))
11237     attributes = cp_parser_attributes_opt (parser);
11238
11239   /* Peek at the next token.  */
11240   token = cp_lexer_peek_token (parser->lexer);
11241
11242   /* Check for the ptr-operator production.  */
11243   cp_parser_parse_tentatively (parser);
11244   /* Parse the ptr-operator.  */
11245   code = cp_parser_ptr_operator (parser,
11246                                  &class_type,
11247                                  &cv_quals);
11248   /* If that worked, then we have a ptr-operator.  */
11249   if (cp_parser_parse_definitely (parser))
11250     {
11251       /* If a ptr-operator was found, then this declarator was not
11252          parenthesized.  */
11253       if (parenthesized_p)
11254         *parenthesized_p = true;
11255       /* The dependent declarator is optional if we are parsing an
11256          abstract-declarator.  */
11257       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11258         cp_parser_parse_tentatively (parser);
11259
11260       /* Parse the dependent declarator.  */
11261       declarator = cp_parser_declarator (parser, dcl_kind,
11262                                          /*ctor_dtor_or_conv_p=*/NULL,
11263                                          /*parenthesized_p=*/NULL,
11264                                          /*member_p=*/false);
11265
11266       /* If we are parsing an abstract-declarator, we must handle the
11267          case where the dependent declarator is absent.  */
11268       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11269           && !cp_parser_parse_definitely (parser))
11270         declarator = NULL;
11271
11272       /* Build the representation of the ptr-operator.  */
11273       if (class_type)
11274         declarator = make_ptrmem_declarator (cv_quals,
11275                                              class_type,
11276                                              declarator);
11277       else if (code == INDIRECT_REF)
11278         declarator = make_pointer_declarator (cv_quals, declarator);
11279       else
11280         declarator = make_reference_declarator (cv_quals, declarator);
11281     }
11282   /* Everything else is a direct-declarator.  */
11283   else
11284     {
11285       if (parenthesized_p)
11286         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11287                                                    CPP_OPEN_PAREN);
11288       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11289                                                 ctor_dtor_or_conv_p,
11290                                                 member_p);
11291     }
11292
11293   if (attributes && declarator && declarator != cp_error_declarator)
11294     declarator->attributes = attributes;
11295
11296   return declarator;
11297 }
11298
11299 /* Parse a direct-declarator or direct-abstract-declarator.
11300
11301    direct-declarator:
11302      declarator-id
11303      direct-declarator ( parameter-declaration-clause )
11304        cv-qualifier-seq [opt]
11305        exception-specification [opt]
11306      direct-declarator [ constant-expression [opt] ]
11307      ( declarator )
11308
11309    direct-abstract-declarator:
11310      direct-abstract-declarator [opt]
11311        ( parameter-declaration-clause )
11312        cv-qualifier-seq [opt]
11313        exception-specification [opt]
11314      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11315      ( abstract-declarator )
11316
11317    Returns a representation of the declarator.  DCL_KIND is
11318    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11319    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11320    we are parsing a direct-declarator.  It is
11321    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11322    of ambiguity we prefer an abstract declarator, as per
11323    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11324    cp_parser_declarator.  */
11325
11326 static cp_declarator *
11327 cp_parser_direct_declarator (cp_parser* parser,
11328                              cp_parser_declarator_kind dcl_kind,
11329                              int* ctor_dtor_or_conv_p,
11330                              bool member_p)
11331 {
11332   cp_token *token;
11333   cp_declarator *declarator = NULL;
11334   tree scope = NULL_TREE;
11335   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11336   bool saved_in_declarator_p = parser->in_declarator_p;
11337   bool first = true;
11338   tree pushed_scope = NULL_TREE;
11339
11340   while (true)
11341     {
11342       /* Peek at the next token.  */
11343       token = cp_lexer_peek_token (parser->lexer);
11344       if (token->type == CPP_OPEN_PAREN)
11345         {
11346           /* This is either a parameter-declaration-clause, or a
11347              parenthesized declarator. When we know we are parsing a
11348              named declarator, it must be a parenthesized declarator
11349              if FIRST is true. For instance, `(int)' is a
11350              parameter-declaration-clause, with an omitted
11351              direct-abstract-declarator. But `((*))', is a
11352              parenthesized abstract declarator. Finally, when T is a
11353              template parameter `(T)' is a
11354              parameter-declaration-clause, and not a parenthesized
11355              named declarator.
11356
11357              We first try and parse a parameter-declaration-clause,
11358              and then try a nested declarator (if FIRST is true).
11359
11360              It is not an error for it not to be a
11361              parameter-declaration-clause, even when FIRST is
11362              false. Consider,
11363
11364                int i (int);
11365                int i (3);
11366
11367              The first is the declaration of a function while the
11368              second is a the definition of a variable, including its
11369              initializer.
11370
11371              Having seen only the parenthesis, we cannot know which of
11372              these two alternatives should be selected.  Even more
11373              complex are examples like:
11374
11375                int i (int (a));
11376                int i (int (3));
11377
11378              The former is a function-declaration; the latter is a
11379              variable initialization.
11380
11381              Thus again, we try a parameter-declaration-clause, and if
11382              that fails, we back out and return.  */
11383
11384           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11385             {
11386               cp_parameter_declarator *params;
11387               unsigned saved_num_template_parameter_lists;
11388
11389               /* In a member-declarator, the only valid interpretation
11390                  of a parenthesis is the start of a
11391                  parameter-declaration-clause.  (It is invalid to
11392                  initialize a static data member with a parenthesized
11393                  initializer; only the "=" form of initialization is
11394                  permitted.)  */
11395               if (!member_p)
11396                 cp_parser_parse_tentatively (parser);
11397
11398               /* Consume the `('.  */
11399               cp_lexer_consume_token (parser->lexer);
11400               if (first)
11401                 {
11402                   /* If this is going to be an abstract declarator, we're
11403                      in a declarator and we can't have default args.  */
11404                   parser->default_arg_ok_p = false;
11405                   parser->in_declarator_p = true;
11406                 }
11407
11408               /* Inside the function parameter list, surrounding
11409                  template-parameter-lists do not apply.  */
11410               saved_num_template_parameter_lists
11411                 = parser->num_template_parameter_lists;
11412               parser->num_template_parameter_lists = 0;
11413
11414               /* Parse the parameter-declaration-clause.  */
11415               params = cp_parser_parameter_declaration_clause (parser);
11416
11417               parser->num_template_parameter_lists
11418                 = saved_num_template_parameter_lists;
11419
11420               /* If all went well, parse the cv-qualifier-seq and the
11421                  exception-specification.  */
11422               if (member_p || cp_parser_parse_definitely (parser))
11423                 {
11424                   cp_cv_quals cv_quals;
11425                   tree exception_specification;
11426
11427                   if (ctor_dtor_or_conv_p)
11428                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11429                   first = false;
11430                   /* Consume the `)'.  */
11431                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11432
11433                   /* Parse the cv-qualifier-seq.  */
11434                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11435                   /* And the exception-specification.  */
11436                   exception_specification
11437                     = cp_parser_exception_specification_opt (parser);
11438
11439                   /* Create the function-declarator.  */
11440                   declarator = make_call_declarator (declarator,
11441                                                      params,
11442                                                      cv_quals,
11443                                                      exception_specification);
11444                   /* Any subsequent parameter lists are to do with
11445                      return type, so are not those of the declared
11446                      function.  */
11447                   parser->default_arg_ok_p = false;
11448
11449                   /* Repeat the main loop.  */
11450                   continue;
11451                 }
11452             }
11453
11454           /* If this is the first, we can try a parenthesized
11455              declarator.  */
11456           if (first)
11457             {
11458               bool saved_in_type_id_in_expr_p;
11459
11460               parser->default_arg_ok_p = saved_default_arg_ok_p;
11461               parser->in_declarator_p = saved_in_declarator_p;
11462
11463               /* Consume the `('.  */
11464               cp_lexer_consume_token (parser->lexer);
11465               /* Parse the nested declarator.  */
11466               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11467               parser->in_type_id_in_expr_p = true;
11468               declarator
11469                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11470                                         /*parenthesized_p=*/NULL,
11471                                         member_p);
11472               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11473               first = false;
11474               /* Expect a `)'.  */
11475               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11476                 declarator = cp_error_declarator;
11477               if (declarator == cp_error_declarator)
11478                 break;
11479
11480               goto handle_declarator;
11481             }
11482           /* Otherwise, we must be done.  */
11483           else
11484             break;
11485         }
11486       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11487                && token->type == CPP_OPEN_SQUARE)
11488         {
11489           /* Parse an array-declarator.  */
11490           tree bounds;
11491
11492           if (ctor_dtor_or_conv_p)
11493             *ctor_dtor_or_conv_p = 0;
11494
11495           first = false;
11496           parser->default_arg_ok_p = false;
11497           parser->in_declarator_p = true;
11498           /* Consume the `['.  */
11499           cp_lexer_consume_token (parser->lexer);
11500           /* Peek at the next token.  */
11501           token = cp_lexer_peek_token (parser->lexer);
11502           /* If the next token is `]', then there is no
11503              constant-expression.  */
11504           if (token->type != CPP_CLOSE_SQUARE)
11505             {
11506               bool non_constant_p;
11507
11508               bounds
11509                 = cp_parser_constant_expression (parser,
11510                                                  /*allow_non_constant=*/true,
11511                                                  &non_constant_p);
11512               if (!non_constant_p)
11513                 bounds = fold_non_dependent_expr (bounds);
11514               /* Normally, the array bound must be an integral constant
11515                  expression.  However, as an extension, we allow VLAs
11516                  in function scopes.  */
11517               else if (!at_function_scope_p ())
11518                 {
11519                   error ("array bound is not an integer constant");
11520                   bounds = error_mark_node;
11521                 }
11522             }
11523           else
11524             bounds = NULL_TREE;
11525           /* Look for the closing `]'.  */
11526           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11527             {
11528               declarator = cp_error_declarator;
11529               break;
11530             }
11531
11532           declarator = make_array_declarator (declarator, bounds);
11533         }
11534       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11535         {
11536           tree qualifying_scope;
11537           tree unqualified_name;
11538           special_function_kind sfk;
11539           bool abstract_ok;
11540
11541           /* Parse a declarator-id */
11542           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11543           if (abstract_ok)
11544             cp_parser_parse_tentatively (parser);
11545           unqualified_name
11546             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11547           qualifying_scope = parser->scope;
11548           if (abstract_ok)
11549             {
11550               if (!cp_parser_parse_definitely (parser))
11551                 unqualified_name = error_mark_node;
11552               else if (unqualified_name
11553                        && (qualifying_scope
11554                            || (TREE_CODE (unqualified_name)
11555                                != IDENTIFIER_NODE)))
11556                 {
11557                   cp_parser_error (parser, "expected unqualified-id");
11558                   unqualified_name = error_mark_node;
11559                 }
11560             }
11561
11562           if (!unqualified_name)
11563             return NULL;
11564           if (unqualified_name == error_mark_node)
11565             {
11566               declarator = cp_error_declarator;
11567               break;
11568             }
11569
11570           if (qualifying_scope && at_namespace_scope_p ()
11571               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11572             {
11573               /* In the declaration of a member of a template class
11574                  outside of the class itself, the SCOPE will sometimes
11575                  be a TYPENAME_TYPE.  For example, given:
11576
11577                  template <typename T>
11578                  int S<T>::R::i = 3;
11579
11580                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11581                  this context, we must resolve S<T>::R to an ordinary
11582                  type, rather than a typename type.
11583
11584                  The reason we normally avoid resolving TYPENAME_TYPEs
11585                  is that a specialization of `S' might render
11586                  `S<T>::R' not a type.  However, if `S' is
11587                  specialized, then this `i' will not be used, so there
11588                  is no harm in resolving the types here.  */
11589               tree type;
11590
11591               /* Resolve the TYPENAME_TYPE.  */
11592               type = resolve_typename_type (qualifying_scope,
11593                                             /*only_current_p=*/false);
11594               /* If that failed, the declarator is invalid.  */
11595               if (type == error_mark_node)
11596                 error ("%<%T::%D%> is not a type",
11597                        TYPE_CONTEXT (qualifying_scope),
11598                        TYPE_IDENTIFIER (qualifying_scope));
11599               qualifying_scope = type;
11600             }
11601
11602           sfk = sfk_none;
11603           if (unqualified_name)
11604             {
11605               tree class_type;
11606
11607               if (qualifying_scope
11608                   && CLASS_TYPE_P (qualifying_scope))
11609                 class_type = qualifying_scope;
11610               else
11611                 class_type = current_class_type;
11612
11613               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11614                 {
11615                   tree name_type = TREE_TYPE (unqualified_name);
11616                   if (class_type && same_type_p (name_type, class_type))
11617                     {
11618                       if (qualifying_scope
11619                           && CLASSTYPE_USE_TEMPLATE (name_type))
11620                         {
11621                           error ("invalid use of constructor as a template");
11622                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11623                                   "name the constructor in a qualified name",
11624                                   class_type,
11625                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11626                                   class_type, name_type);
11627                           declarator = cp_error_declarator;
11628                           break;
11629                         }
11630                       else
11631                         unqualified_name = constructor_name (class_type);
11632                     }
11633                   else
11634                     {
11635                       /* We do not attempt to print the declarator
11636                          here because we do not have enough
11637                          information about its original syntactic
11638                          form.  */
11639                       cp_parser_error (parser, "invalid declarator");
11640                       declarator = cp_error_declarator;
11641                       break;
11642                     }
11643                 }
11644
11645               if (class_type)
11646                 {
11647                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11648                     sfk = sfk_destructor;
11649                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11650                     sfk = sfk_conversion;
11651                   else if (/* There's no way to declare a constructor
11652                               for an anonymous type, even if the type
11653                               got a name for linkage purposes.  */
11654                            !TYPE_WAS_ANONYMOUS (class_type)
11655                            && constructor_name_p (unqualified_name,
11656                                                   class_type))
11657                     {
11658                       unqualified_name = constructor_name (class_type);
11659                       sfk = sfk_constructor;
11660                     }
11661
11662                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11663                     *ctor_dtor_or_conv_p = -1;
11664                 }
11665             }
11666           declarator = make_id_declarator (qualifying_scope,
11667                                            unqualified_name,
11668                                            sfk);
11669           declarator->id_loc = token->location;
11670
11671         handle_declarator:;
11672           scope = get_scope_of_declarator (declarator);
11673           if (scope)
11674             /* Any names that appear after the declarator-id for a
11675                member are looked up in the containing scope.  */
11676             pushed_scope = push_scope (scope);
11677           parser->in_declarator_p = true;
11678           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11679               || (declarator && declarator->kind == cdk_id))
11680             /* Default args are only allowed on function
11681                declarations.  */
11682             parser->default_arg_ok_p = saved_default_arg_ok_p;
11683           else
11684             parser->default_arg_ok_p = false;
11685
11686           first = false;
11687         }
11688       /* We're done.  */
11689       else
11690         break;
11691     }
11692
11693   /* For an abstract declarator, we might wind up with nothing at this
11694      point.  That's an error; the declarator is not optional.  */
11695   if (!declarator)
11696     cp_parser_error (parser, "expected declarator");
11697
11698   /* If we entered a scope, we must exit it now.  */
11699   if (pushed_scope)
11700     pop_scope (pushed_scope);
11701
11702   parser->default_arg_ok_p = saved_default_arg_ok_p;
11703   parser->in_declarator_p = saved_in_declarator_p;
11704
11705   return declarator;
11706 }
11707
11708 /* Parse a ptr-operator.
11709
11710    ptr-operator:
11711      * cv-qualifier-seq [opt]
11712      &
11713      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11714
11715    GNU Extension:
11716
11717    ptr-operator:
11718      & cv-qualifier-seq [opt]
11719
11720    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11721    Returns ADDR_EXPR if a reference was used.  In the case of a
11722    pointer-to-member, *TYPE is filled in with the TYPE containing the
11723    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11724    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11725    ERROR_MARK if an error occurred.  */
11726
11727 static enum tree_code
11728 cp_parser_ptr_operator (cp_parser* parser,
11729                         tree* type,
11730                         cp_cv_quals *cv_quals)
11731 {
11732   enum tree_code code = ERROR_MARK;
11733   cp_token *token;
11734
11735   /* Assume that it's not a pointer-to-member.  */
11736   *type = NULL_TREE;
11737   /* And that there are no cv-qualifiers.  */
11738   *cv_quals = TYPE_UNQUALIFIED;
11739
11740   /* Peek at the next token.  */
11741   token = cp_lexer_peek_token (parser->lexer);
11742   /* If it's a `*' or `&' we have a pointer or reference.  */
11743   if (token->type == CPP_MULT || token->type == CPP_AND)
11744     {
11745       /* Remember which ptr-operator we were processing.  */
11746       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11747
11748       /* Consume the `*' or `&'.  */
11749       cp_lexer_consume_token (parser->lexer);
11750
11751       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11752          `&', if we are allowing GNU extensions.  (The only qualifier
11753          that can legally appear after `&' is `restrict', but that is
11754          enforced during semantic analysis.  */
11755       if (code == INDIRECT_REF
11756           || cp_parser_allow_gnu_extensions_p (parser))
11757         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11758     }
11759   else
11760     {
11761       /* Try the pointer-to-member case.  */
11762       cp_parser_parse_tentatively (parser);
11763       /* Look for the optional `::' operator.  */
11764       cp_parser_global_scope_opt (parser,
11765                                   /*current_scope_valid_p=*/false);
11766       /* Look for the nested-name specifier.  */
11767       cp_parser_nested_name_specifier (parser,
11768                                        /*typename_keyword_p=*/false,
11769                                        /*check_dependency_p=*/true,
11770                                        /*type_p=*/false,
11771                                        /*is_declaration=*/false);
11772       /* If we found it, and the next token is a `*', then we are
11773          indeed looking at a pointer-to-member operator.  */
11774       if (!cp_parser_error_occurred (parser)
11775           && cp_parser_require (parser, CPP_MULT, "`*'"))
11776         {
11777           /* Indicate that the `*' operator was used.  */
11778           code = INDIRECT_REF;
11779
11780           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11781             error ("%qD is a namespace", parser->scope);
11782           else
11783             {
11784               /* The type of which the member is a member is given by the
11785                  current SCOPE.  */
11786               *type = parser->scope;
11787               /* The next name will not be qualified.  */
11788               parser->scope = NULL_TREE;
11789               parser->qualifying_scope = NULL_TREE;
11790               parser->object_scope = NULL_TREE;
11791               /* Look for the optional cv-qualifier-seq.  */
11792               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11793             }
11794         }
11795       /* If that didn't work we don't have a ptr-operator.  */
11796       if (!cp_parser_parse_definitely (parser))
11797         cp_parser_error (parser, "expected ptr-operator");
11798     }
11799
11800   return code;
11801 }
11802
11803 /* Parse an (optional) cv-qualifier-seq.
11804
11805    cv-qualifier-seq:
11806      cv-qualifier cv-qualifier-seq [opt]
11807
11808    cv-qualifier:
11809      const
11810      volatile
11811
11812    GNU Extension:
11813
11814    cv-qualifier:
11815      __restrict__
11816
11817    Returns a bitmask representing the cv-qualifiers.  */
11818
11819 static cp_cv_quals
11820 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11821 {
11822   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11823
11824   while (true)
11825     {
11826       cp_token *token;
11827       cp_cv_quals cv_qualifier;
11828
11829       /* Peek at the next token.  */
11830       token = cp_lexer_peek_token (parser->lexer);
11831       /* See if it's a cv-qualifier.  */
11832       switch (token->keyword)
11833         {
11834         case RID_CONST:
11835           cv_qualifier = TYPE_QUAL_CONST;
11836           break;
11837
11838         case RID_VOLATILE:
11839           cv_qualifier = TYPE_QUAL_VOLATILE;
11840           break;
11841
11842         case RID_RESTRICT:
11843           cv_qualifier = TYPE_QUAL_RESTRICT;
11844           break;
11845
11846         default:
11847           cv_qualifier = TYPE_UNQUALIFIED;
11848           break;
11849         }
11850
11851       if (!cv_qualifier)
11852         break;
11853
11854       if (cv_quals & cv_qualifier)
11855         {
11856           error ("duplicate cv-qualifier");
11857           cp_lexer_purge_token (parser->lexer);
11858         }
11859       else
11860         {
11861           cp_lexer_consume_token (parser->lexer);
11862           cv_quals |= cv_qualifier;
11863         }
11864     }
11865
11866   return cv_quals;
11867 }
11868
11869 /* Parse a declarator-id.
11870
11871    declarator-id:
11872      id-expression
11873      :: [opt] nested-name-specifier [opt] type-name
11874
11875    In the `id-expression' case, the value returned is as for
11876    cp_parser_id_expression if the id-expression was an unqualified-id.
11877    If the id-expression was a qualified-id, then a SCOPE_REF is
11878    returned.  The first operand is the scope (either a NAMESPACE_DECL
11879    or TREE_TYPE), but the second is still just a representation of an
11880    unqualified-id.  */
11881
11882 static tree
11883 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11884 {
11885   tree id;
11886   /* The expression must be an id-expression.  Assume that qualified
11887      names are the names of types so that:
11888
11889        template <class T>
11890        int S<T>::R::i = 3;
11891
11892      will work; we must treat `S<T>::R' as the name of a type.
11893      Similarly, assume that qualified names are templates, where
11894      required, so that:
11895
11896        template <class T>
11897        int S<T>::R<T>::i = 3;
11898
11899      will work, too.  */
11900   id = cp_parser_id_expression (parser,
11901                                 /*template_keyword_p=*/false,
11902                                 /*check_dependency_p=*/false,
11903                                 /*template_p=*/NULL,
11904                                 /*declarator_p=*/true,
11905                                 optional_p);
11906   if (id && BASELINK_P (id))
11907     id = BASELINK_FUNCTIONS (id);
11908   return id;
11909 }
11910
11911 /* Parse a type-id.
11912
11913    type-id:
11914      type-specifier-seq abstract-declarator [opt]
11915
11916    Returns the TYPE specified.  */
11917
11918 static tree
11919 cp_parser_type_id (cp_parser* parser)
11920 {
11921   cp_decl_specifier_seq type_specifier_seq;
11922   cp_declarator *abstract_declarator;
11923
11924   /* Parse the type-specifier-seq.  */
11925   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11926                                 &type_specifier_seq);
11927   if (type_specifier_seq.type == error_mark_node)
11928     return error_mark_node;
11929
11930   /* There might or might not be an abstract declarator.  */
11931   cp_parser_parse_tentatively (parser);
11932   /* Look for the declarator.  */
11933   abstract_declarator
11934     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11935                             /*parenthesized_p=*/NULL,
11936                             /*member_p=*/false);
11937   /* Check to see if there really was a declarator.  */
11938   if (!cp_parser_parse_definitely (parser))
11939     abstract_declarator = NULL;
11940
11941   return groktypename (&type_specifier_seq, abstract_declarator);
11942 }
11943
11944 /* Parse a type-specifier-seq.
11945
11946    type-specifier-seq:
11947      type-specifier type-specifier-seq [opt]
11948
11949    GNU extension:
11950
11951    type-specifier-seq:
11952      attributes type-specifier-seq [opt]
11953
11954    If IS_CONDITION is true, we are at the start of a "condition",
11955    e.g., we've just seen "if (".
11956
11957    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11958
11959 static void
11960 cp_parser_type_specifier_seq (cp_parser* parser,
11961                               bool is_condition,
11962                               cp_decl_specifier_seq *type_specifier_seq)
11963 {
11964   bool seen_type_specifier = false;
11965   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11966
11967   /* Clear the TYPE_SPECIFIER_SEQ.  */
11968   clear_decl_specs (type_specifier_seq);
11969
11970   /* Parse the type-specifiers and attributes.  */
11971   while (true)
11972     {
11973       tree type_specifier;
11974       bool is_cv_qualifier;
11975
11976       /* Check for attributes first.  */
11977       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11978         {
11979           type_specifier_seq->attributes =
11980             chainon (type_specifier_seq->attributes,
11981                      cp_parser_attributes_opt (parser));
11982           continue;
11983         }
11984
11985       /* Look for the type-specifier.  */
11986       type_specifier = cp_parser_type_specifier (parser,
11987                                                  flags,
11988                                                  type_specifier_seq,
11989                                                  /*is_declaration=*/false,
11990                                                  NULL,
11991                                                  &is_cv_qualifier);
11992       if (!type_specifier)
11993         {
11994           /* If the first type-specifier could not be found, this is not a
11995              type-specifier-seq at all.  */
11996           if (!seen_type_specifier)
11997             {
11998               cp_parser_error (parser, "expected type-specifier");
11999               type_specifier_seq->type = error_mark_node;
12000               return;
12001             }
12002           /* If subsequent type-specifiers could not be found, the
12003              type-specifier-seq is complete.  */
12004           break;
12005         }
12006
12007       seen_type_specifier = true;
12008       /* The standard says that a condition can be:
12009
12010             type-specifier-seq declarator = assignment-expression
12011
12012          However, given:
12013
12014            struct S {};
12015            if (int S = ...)
12016
12017          we should treat the "S" as a declarator, not as a
12018          type-specifier.  The standard doesn't say that explicitly for
12019          type-specifier-seq, but it does say that for
12020          decl-specifier-seq in an ordinary declaration.  Perhaps it
12021          would be clearer just to allow a decl-specifier-seq here, and
12022          then add a semantic restriction that if any decl-specifiers
12023          that are not type-specifiers appear, the program is invalid.  */
12024       if (is_condition && !is_cv_qualifier)
12025         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12026     }
12027
12028   cp_parser_check_decl_spec (type_specifier_seq);
12029 }
12030
12031 /* Parse a parameter-declaration-clause.
12032
12033    parameter-declaration-clause:
12034      parameter-declaration-list [opt] ... [opt]
12035      parameter-declaration-list , ...
12036
12037    Returns a representation for the parameter declarations.  A return
12038    value of NULL indicates a parameter-declaration-clause consisting
12039    only of an ellipsis.  */
12040
12041 static cp_parameter_declarator *
12042 cp_parser_parameter_declaration_clause (cp_parser* parser)
12043 {
12044   cp_parameter_declarator *parameters;
12045   cp_token *token;
12046   bool ellipsis_p;
12047   bool is_error;
12048
12049   /* Peek at the next token.  */
12050   token = cp_lexer_peek_token (parser->lexer);
12051   /* Check for trivial parameter-declaration-clauses.  */
12052   if (token->type == CPP_ELLIPSIS)
12053     {
12054       /* Consume the `...' token.  */
12055       cp_lexer_consume_token (parser->lexer);
12056       return NULL;
12057     }
12058   else if (token->type == CPP_CLOSE_PAREN)
12059     /* There are no parameters.  */
12060     {
12061 #ifndef NO_IMPLICIT_EXTERN_C
12062       if (in_system_header && current_class_type == NULL
12063           && current_lang_name == lang_name_c)
12064         return NULL;
12065       else
12066 #endif
12067         return no_parameters;
12068     }
12069   /* Check for `(void)', too, which is a special case.  */
12070   else if (token->keyword == RID_VOID
12071            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12072                == CPP_CLOSE_PAREN))
12073     {
12074       /* Consume the `void' token.  */
12075       cp_lexer_consume_token (parser->lexer);
12076       /* There are no parameters.  */
12077       return no_parameters;
12078     }
12079
12080   /* Parse the parameter-declaration-list.  */
12081   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12082   /* If a parse error occurred while parsing the
12083      parameter-declaration-list, then the entire
12084      parameter-declaration-clause is erroneous.  */
12085   if (is_error)
12086     return NULL;
12087
12088   /* Peek at the next token.  */
12089   token = cp_lexer_peek_token (parser->lexer);
12090   /* If it's a `,', the clause should terminate with an ellipsis.  */
12091   if (token->type == CPP_COMMA)
12092     {
12093       /* Consume the `,'.  */
12094       cp_lexer_consume_token (parser->lexer);
12095       /* Expect an ellipsis.  */
12096       ellipsis_p
12097         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12098     }
12099   /* It might also be `...' if the optional trailing `,' was
12100      omitted.  */
12101   else if (token->type == CPP_ELLIPSIS)
12102     {
12103       /* Consume the `...' token.  */
12104       cp_lexer_consume_token (parser->lexer);
12105       /* And remember that we saw it.  */
12106       ellipsis_p = true;
12107     }
12108   else
12109     ellipsis_p = false;
12110
12111   /* Finish the parameter list.  */
12112   if (parameters && ellipsis_p)
12113     parameters->ellipsis_p = true;
12114
12115   return parameters;
12116 }
12117
12118 /* Parse a parameter-declaration-list.
12119
12120    parameter-declaration-list:
12121      parameter-declaration
12122      parameter-declaration-list , parameter-declaration
12123
12124    Returns a representation of the parameter-declaration-list, as for
12125    cp_parser_parameter_declaration_clause.  However, the
12126    `void_list_node' is never appended to the list.  Upon return,
12127    *IS_ERROR will be true iff an error occurred.  */
12128
12129 static cp_parameter_declarator *
12130 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12131 {
12132   cp_parameter_declarator *parameters = NULL;
12133   cp_parameter_declarator **tail = &parameters;
12134   bool saved_in_unbraced_linkage_specification_p;
12135
12136   /* Assume all will go well.  */
12137   *is_error = false;
12138   /* The special considerations that apply to a function within an
12139      unbraced linkage specifications do not apply to the parameters
12140      to the function.  */
12141   saved_in_unbraced_linkage_specification_p 
12142     = parser->in_unbraced_linkage_specification_p;
12143   parser->in_unbraced_linkage_specification_p = false;
12144
12145   /* Look for more parameters.  */
12146   while (true)
12147     {
12148       cp_parameter_declarator *parameter;
12149       bool parenthesized_p;
12150       /* Parse the parameter.  */
12151       parameter
12152         = cp_parser_parameter_declaration (parser,
12153                                            /*template_parm_p=*/false,
12154                                            &parenthesized_p);
12155
12156       /* If a parse error occurred parsing the parameter declaration,
12157          then the entire parameter-declaration-list is erroneous.  */
12158       if (!parameter)
12159         {
12160           *is_error = true;
12161           parameters = NULL;
12162           break;
12163         }
12164       /* Add the new parameter to the list.  */
12165       *tail = parameter;
12166       tail = &parameter->next;
12167
12168       /* Peek at the next token.  */
12169       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12170           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12171           /* These are for Objective-C++ */
12172           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12173           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12174         /* The parameter-declaration-list is complete.  */
12175         break;
12176       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12177         {
12178           cp_token *token;
12179
12180           /* Peek at the next token.  */
12181           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12182           /* If it's an ellipsis, then the list is complete.  */
12183           if (token->type == CPP_ELLIPSIS)
12184             break;
12185           /* Otherwise, there must be more parameters.  Consume the
12186              `,'.  */
12187           cp_lexer_consume_token (parser->lexer);
12188           /* When parsing something like:
12189
12190                 int i(float f, double d)
12191
12192              we can tell after seeing the declaration for "f" that we
12193              are not looking at an initialization of a variable "i",
12194              but rather at the declaration of a function "i".
12195
12196              Due to the fact that the parsing of template arguments
12197              (as specified to a template-id) requires backtracking we
12198              cannot use this technique when inside a template argument
12199              list.  */
12200           if (!parser->in_template_argument_list_p
12201               && !parser->in_type_id_in_expr_p
12202               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12203               /* However, a parameter-declaration of the form
12204                  "foat(f)" (which is a valid declaration of a
12205                  parameter "f") can also be interpreted as an
12206                  expression (the conversion of "f" to "float").  */
12207               && !parenthesized_p)
12208             cp_parser_commit_to_tentative_parse (parser);
12209         }
12210       else
12211         {
12212           cp_parser_error (parser, "expected %<,%> or %<...%>");
12213           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12214             cp_parser_skip_to_closing_parenthesis (parser,
12215                                                    /*recovering=*/true,
12216                                                    /*or_comma=*/false,
12217                                                    /*consume_paren=*/false);
12218           break;
12219         }
12220     }
12221
12222   parser->in_unbraced_linkage_specification_p
12223     = saved_in_unbraced_linkage_specification_p;
12224
12225   return parameters;
12226 }
12227
12228 /* Parse a parameter declaration.
12229
12230    parameter-declaration:
12231      decl-specifier-seq declarator
12232      decl-specifier-seq declarator = assignment-expression
12233      decl-specifier-seq abstract-declarator [opt]
12234      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12235
12236    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12237    declares a template parameter.  (In that case, a non-nested `>'
12238    token encountered during the parsing of the assignment-expression
12239    is not interpreted as a greater-than operator.)
12240
12241    Returns a representation of the parameter, or NULL if an error
12242    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12243    true iff the declarator is of the form "(p)".  */
12244
12245 static cp_parameter_declarator *
12246 cp_parser_parameter_declaration (cp_parser *parser,
12247                                  bool template_parm_p,
12248                                  bool *parenthesized_p)
12249 {
12250   int declares_class_or_enum;
12251   bool greater_than_is_operator_p;
12252   cp_decl_specifier_seq decl_specifiers;
12253   cp_declarator *declarator;
12254   tree default_argument;
12255   cp_token *token;
12256   const char *saved_message;
12257
12258   /* In a template parameter, `>' is not an operator.
12259
12260      [temp.param]
12261
12262      When parsing a default template-argument for a non-type
12263      template-parameter, the first non-nested `>' is taken as the end
12264      of the template parameter-list rather than a greater-than
12265      operator.  */
12266   greater_than_is_operator_p = !template_parm_p;
12267
12268   /* Type definitions may not appear in parameter types.  */
12269   saved_message = parser->type_definition_forbidden_message;
12270   parser->type_definition_forbidden_message
12271     = "types may not be defined in parameter types";
12272
12273   /* Parse the declaration-specifiers.  */
12274   cp_parser_decl_specifier_seq (parser,
12275                                 CP_PARSER_FLAGS_NONE,
12276                                 &decl_specifiers,
12277                                 &declares_class_or_enum);
12278   /* If an error occurred, there's no reason to attempt to parse the
12279      rest of the declaration.  */
12280   if (cp_parser_error_occurred (parser))
12281     {
12282       parser->type_definition_forbidden_message = saved_message;
12283       return NULL;
12284     }
12285
12286   /* Peek at the next token.  */
12287   token = cp_lexer_peek_token (parser->lexer);
12288   /* If the next token is a `)', `,', `=', `>', or `...', then there
12289      is no declarator.  */
12290   if (token->type == CPP_CLOSE_PAREN
12291       || token->type == CPP_COMMA
12292       || token->type == CPP_EQ
12293       || token->type == CPP_ELLIPSIS
12294       || token->type == CPP_GREATER)
12295     {
12296       declarator = NULL;
12297       if (parenthesized_p)
12298         *parenthesized_p = false;
12299     }
12300   /* Otherwise, there should be a declarator.  */
12301   else
12302     {
12303       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12304       parser->default_arg_ok_p = false;
12305
12306       /* After seeing a decl-specifier-seq, if the next token is not a
12307          "(", there is no possibility that the code is a valid
12308          expression.  Therefore, if parsing tentatively, we commit at
12309          this point.  */
12310       if (!parser->in_template_argument_list_p
12311           /* In an expression context, having seen:
12312
12313                (int((char ...
12314
12315              we cannot be sure whether we are looking at a
12316              function-type (taking a "char" as a parameter) or a cast
12317              of some object of type "char" to "int".  */
12318           && !parser->in_type_id_in_expr_p
12319           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12320           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12321         cp_parser_commit_to_tentative_parse (parser);
12322       /* Parse the declarator.  */
12323       declarator = cp_parser_declarator (parser,
12324                                          CP_PARSER_DECLARATOR_EITHER,
12325                                          /*ctor_dtor_or_conv_p=*/NULL,
12326                                          parenthesized_p,
12327                                          /*member_p=*/false);
12328       parser->default_arg_ok_p = saved_default_arg_ok_p;
12329       /* After the declarator, allow more attributes.  */
12330       decl_specifiers.attributes
12331         = chainon (decl_specifiers.attributes,
12332                    cp_parser_attributes_opt (parser));
12333     }
12334
12335   /* The restriction on defining new types applies only to the type
12336      of the parameter, not to the default argument.  */
12337   parser->type_definition_forbidden_message = saved_message;
12338
12339   /* If the next token is `=', then process a default argument.  */
12340   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12341     {
12342       bool saved_greater_than_is_operator_p;
12343       /* Consume the `='.  */
12344       cp_lexer_consume_token (parser->lexer);
12345
12346       /* If we are defining a class, then the tokens that make up the
12347          default argument must be saved and processed later.  */
12348       if (!template_parm_p && at_class_scope_p ()
12349           && TYPE_BEING_DEFINED (current_class_type))
12350         {
12351           unsigned depth = 0;
12352           cp_token *first_token;
12353           cp_token *token;
12354
12355           /* Add tokens until we have processed the entire default
12356              argument.  We add the range [first_token, token).  */
12357           first_token = cp_lexer_peek_token (parser->lexer);
12358           while (true)
12359             {
12360               bool done = false;
12361
12362               /* Peek at the next token.  */
12363               token = cp_lexer_peek_token (parser->lexer);
12364               /* What we do depends on what token we have.  */
12365               switch (token->type)
12366                 {
12367                   /* In valid code, a default argument must be
12368                      immediately followed by a `,' `)', or `...'.  */
12369                 case CPP_COMMA:
12370                 case CPP_CLOSE_PAREN:
12371                 case CPP_ELLIPSIS:
12372                   /* If we run into a non-nested `;', `}', or `]',
12373                      then the code is invalid -- but the default
12374                      argument is certainly over.  */
12375                 case CPP_SEMICOLON:
12376                 case CPP_CLOSE_BRACE:
12377                 case CPP_CLOSE_SQUARE:
12378                   if (depth == 0)
12379                     done = true;
12380                   /* Update DEPTH, if necessary.  */
12381                   else if (token->type == CPP_CLOSE_PAREN
12382                            || token->type == CPP_CLOSE_BRACE
12383                            || token->type == CPP_CLOSE_SQUARE)
12384                     --depth;
12385                   break;
12386
12387                 case CPP_OPEN_PAREN:
12388                 case CPP_OPEN_SQUARE:
12389                 case CPP_OPEN_BRACE:
12390                   ++depth;
12391                   break;
12392
12393                 case CPP_GREATER:
12394                   /* If we see a non-nested `>', and `>' is not an
12395                      operator, then it marks the end of the default
12396                      argument.  */
12397                   if (!depth && !greater_than_is_operator_p)
12398                     done = true;
12399                   break;
12400
12401                   /* If we run out of tokens, issue an error message.  */
12402                 case CPP_EOF:
12403                 case CPP_PRAGMA_EOL:
12404                   error ("file ends in default argument");
12405                   done = true;
12406                   break;
12407
12408                 case CPP_NAME:
12409                 case CPP_SCOPE:
12410                   /* In these cases, we should look for template-ids.
12411                      For example, if the default argument is
12412                      `X<int, double>()', we need to do name lookup to
12413                      figure out whether or not `X' is a template; if
12414                      so, the `,' does not end the default argument.
12415
12416                      That is not yet done.  */
12417                   break;
12418
12419                 default:
12420                   break;
12421                 }
12422
12423               /* If we've reached the end, stop.  */
12424               if (done)
12425                 break;
12426
12427               /* Add the token to the token block.  */
12428               token = cp_lexer_consume_token (parser->lexer);
12429             }
12430
12431           /* Create a DEFAULT_ARG to represented the unparsed default
12432              argument.  */
12433           default_argument = make_node (DEFAULT_ARG);
12434           DEFARG_TOKENS (default_argument)
12435             = cp_token_cache_new (first_token, token);
12436           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12437         }
12438       /* Outside of a class definition, we can just parse the
12439          assignment-expression.  */
12440       else
12441         {
12442           bool saved_local_variables_forbidden_p;
12443
12444           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12445              set correctly.  */
12446           saved_greater_than_is_operator_p
12447             = parser->greater_than_is_operator_p;
12448           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12449           /* Local variable names (and the `this' keyword) may not
12450              appear in a default argument.  */
12451           saved_local_variables_forbidden_p
12452             = parser->local_variables_forbidden_p;
12453           parser->local_variables_forbidden_p = true;
12454           /* The default argument expression may cause implicitly
12455              defined member functions to be synthesized, which will
12456              result in garbage collection.  We must treat this
12457              situation as if we were within the body of function so as
12458              to avoid collecting live data on the stack.  */
12459           ++function_depth;
12460           /* Parse the assignment-expression.  */
12461           if (template_parm_p)
12462             push_deferring_access_checks (dk_no_deferred);
12463           default_argument
12464             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12465           if (template_parm_p)
12466             pop_deferring_access_checks ();
12467           /* Restore saved state.  */
12468           --function_depth;
12469           parser->greater_than_is_operator_p
12470             = saved_greater_than_is_operator_p;
12471           parser->local_variables_forbidden_p
12472             = saved_local_variables_forbidden_p;
12473         }
12474       if (!parser->default_arg_ok_p)
12475         {
12476           if (!flag_pedantic_errors)
12477             warning (0, "deprecated use of default argument for parameter of non-function");
12478           else
12479             {
12480               error ("default arguments are only permitted for function parameters");
12481               default_argument = NULL_TREE;
12482             }
12483         }
12484     }
12485   else
12486     default_argument = NULL_TREE;
12487
12488   return make_parameter_declarator (&decl_specifiers,
12489                                     declarator,
12490                                     default_argument);
12491 }
12492
12493 /* Parse a function-body.
12494
12495    function-body:
12496      compound_statement  */
12497
12498 static void
12499 cp_parser_function_body (cp_parser *parser)
12500 {
12501   cp_parser_compound_statement (parser, NULL, false);
12502 }
12503
12504 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12505    true if a ctor-initializer was present.  */
12506
12507 static bool
12508 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12509 {
12510   tree body;
12511   bool ctor_initializer_p;
12512
12513   /* Begin the function body.  */
12514   body = begin_function_body ();
12515   /* Parse the optional ctor-initializer.  */
12516   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12517   /* Parse the function-body.  */
12518   cp_parser_function_body (parser);
12519   /* Finish the function body.  */
12520   finish_function_body (body);
12521
12522   return ctor_initializer_p;
12523 }
12524
12525 /* Parse an initializer.
12526
12527    initializer:
12528      = initializer-clause
12529      ( expression-list )
12530
12531    Returns an expression representing the initializer.  If no
12532    initializer is present, NULL_TREE is returned.
12533
12534    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12535    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12536    set to FALSE if there is no initializer present.  If there is an
12537    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12538    is set to true; otherwise it is set to false.  */
12539
12540 static tree
12541 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12542                        bool* non_constant_p)
12543 {
12544   cp_token *token;
12545   tree init;
12546
12547   /* Peek at the next token.  */
12548   token = cp_lexer_peek_token (parser->lexer);
12549
12550   /* Let our caller know whether or not this initializer was
12551      parenthesized.  */
12552   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12553   /* Assume that the initializer is constant.  */
12554   *non_constant_p = false;
12555
12556   if (token->type == CPP_EQ)
12557     {
12558       /* Consume the `='.  */
12559       cp_lexer_consume_token (parser->lexer);
12560       /* Parse the initializer-clause.  */
12561       init = cp_parser_initializer_clause (parser, non_constant_p);
12562     }
12563   else if (token->type == CPP_OPEN_PAREN)
12564     init = cp_parser_parenthesized_expression_list (parser, false,
12565                                                     /*cast_p=*/false,
12566                                                     non_constant_p);
12567   else
12568     {
12569       /* Anything else is an error.  */
12570       cp_parser_error (parser, "expected initializer");
12571       init = error_mark_node;
12572     }
12573
12574   return init;
12575 }
12576
12577 /* Parse an initializer-clause.
12578
12579    initializer-clause:
12580      assignment-expression
12581      { initializer-list , [opt] }
12582      { }
12583
12584    Returns an expression representing the initializer.
12585
12586    If the `assignment-expression' production is used the value
12587    returned is simply a representation for the expression.
12588
12589    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12590    the elements of the initializer-list (or NULL, if the last
12591    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12592    NULL_TREE.  There is no way to detect whether or not the optional
12593    trailing `,' was provided.  NON_CONSTANT_P is as for
12594    cp_parser_initializer.  */
12595
12596 static tree
12597 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12598 {
12599   tree initializer;
12600
12601   /* Assume the expression is constant.  */
12602   *non_constant_p = false;
12603
12604   /* If it is not a `{', then we are looking at an
12605      assignment-expression.  */
12606   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12607     {
12608       initializer
12609         = cp_parser_constant_expression (parser,
12610                                         /*allow_non_constant_p=*/true,
12611                                         non_constant_p);
12612       if (!*non_constant_p)
12613         initializer = fold_non_dependent_expr (initializer);
12614     }
12615   else
12616     {
12617       /* Consume the `{' token.  */
12618       cp_lexer_consume_token (parser->lexer);
12619       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12620       initializer = make_node (CONSTRUCTOR);
12621       /* If it's not a `}', then there is a non-trivial initializer.  */
12622       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12623         {
12624           /* Parse the initializer list.  */
12625           CONSTRUCTOR_ELTS (initializer)
12626             = cp_parser_initializer_list (parser, non_constant_p);
12627           /* A trailing `,' token is allowed.  */
12628           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12629             cp_lexer_consume_token (parser->lexer);
12630         }
12631       /* Now, there should be a trailing `}'.  */
12632       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12633     }
12634
12635   return initializer;
12636 }
12637
12638 /* Parse an initializer-list.
12639
12640    initializer-list:
12641      initializer-clause
12642      initializer-list , initializer-clause
12643
12644    GNU Extension:
12645
12646    initializer-list:
12647      identifier : initializer-clause
12648      initializer-list, identifier : initializer-clause
12649
12650    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12651    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12652    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12653    as for cp_parser_initializer.  */
12654
12655 static VEC(constructor_elt,gc) *
12656 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12657 {
12658   VEC(constructor_elt,gc) *v = NULL;
12659
12660   /* Assume all of the expressions are constant.  */
12661   *non_constant_p = false;
12662
12663   /* Parse the rest of the list.  */
12664   while (true)
12665     {
12666       cp_token *token;
12667       tree identifier;
12668       tree initializer;
12669       bool clause_non_constant_p;
12670
12671       /* If the next token is an identifier and the following one is a
12672          colon, we are looking at the GNU designated-initializer
12673          syntax.  */
12674       if (cp_parser_allow_gnu_extensions_p (parser)
12675           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12676           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12677         {
12678           /* Consume the identifier.  */
12679           identifier = cp_lexer_consume_token (parser->lexer)->value;
12680           /* Consume the `:'.  */
12681           cp_lexer_consume_token (parser->lexer);
12682         }
12683       else
12684         identifier = NULL_TREE;
12685
12686       /* Parse the initializer.  */
12687       initializer = cp_parser_initializer_clause (parser,
12688                                                   &clause_non_constant_p);
12689       /* If any clause is non-constant, so is the entire initializer.  */
12690       if (clause_non_constant_p)
12691         *non_constant_p = true;
12692
12693       /* Add it to the vector.  */
12694       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12695
12696       /* If the next token is not a comma, we have reached the end of
12697          the list.  */
12698       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12699         break;
12700
12701       /* Peek at the next token.  */
12702       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12703       /* If the next token is a `}', then we're still done.  An
12704          initializer-clause can have a trailing `,' after the
12705          initializer-list and before the closing `}'.  */
12706       if (token->type == CPP_CLOSE_BRACE)
12707         break;
12708
12709       /* Consume the `,' token.  */
12710       cp_lexer_consume_token (parser->lexer);
12711     }
12712
12713   return v;
12714 }
12715
12716 /* Classes [gram.class] */
12717
12718 /* Parse a class-name.
12719
12720    class-name:
12721      identifier
12722      template-id
12723
12724    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12725    to indicate that names looked up in dependent types should be
12726    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12727    keyword has been used to indicate that the name that appears next
12728    is a template.  TAG_TYPE indicates the explicit tag given before
12729    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12730    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12731    is the class being defined in a class-head.
12732
12733    Returns the TYPE_DECL representing the class.  */
12734
12735 static tree
12736 cp_parser_class_name (cp_parser *parser,
12737                       bool typename_keyword_p,
12738                       bool template_keyword_p,
12739                       enum tag_types tag_type,
12740                       bool check_dependency_p,
12741                       bool class_head_p,
12742                       bool is_declaration)
12743 {
12744   tree decl;
12745   tree scope;
12746   bool typename_p;
12747   cp_token *token;
12748
12749   /* All class-names start with an identifier.  */
12750   token = cp_lexer_peek_token (parser->lexer);
12751   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12752     {
12753       cp_parser_error (parser, "expected class-name");
12754       return error_mark_node;
12755     }
12756
12757   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12758      to a template-id, so we save it here.  */
12759   scope = parser->scope;
12760   if (scope == error_mark_node)
12761     return error_mark_node;
12762
12763   /* Any name names a type if we're following the `typename' keyword
12764      in a qualified name where the enclosing scope is type-dependent.  */
12765   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12766                 && dependent_type_p (scope));
12767   /* Handle the common case (an identifier, but not a template-id)
12768      efficiently.  */
12769   if (token->type == CPP_NAME
12770       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12771     {
12772       cp_token *identifier_token;
12773       tree identifier;
12774       bool ambiguous_p;
12775
12776       /* Look for the identifier.  */
12777       identifier_token = cp_lexer_peek_token (parser->lexer);
12778       ambiguous_p = identifier_token->ambiguous_p;
12779       identifier = cp_parser_identifier (parser);
12780       /* If the next token isn't an identifier, we are certainly not
12781          looking at a class-name.  */
12782       if (identifier == error_mark_node)
12783         decl = error_mark_node;
12784       /* If we know this is a type-name, there's no need to look it
12785          up.  */
12786       else if (typename_p)
12787         decl = identifier;
12788       else
12789         {
12790           tree ambiguous_decls;
12791           /* If we already know that this lookup is ambiguous, then
12792              we've already issued an error message; there's no reason
12793              to check again.  */
12794           if (ambiguous_p)
12795             {
12796               cp_parser_simulate_error (parser);
12797               return error_mark_node;
12798             }
12799           /* If the next token is a `::', then the name must be a type
12800              name.
12801
12802              [basic.lookup.qual]
12803
12804              During the lookup for a name preceding the :: scope
12805              resolution operator, object, function, and enumerator
12806              names are ignored.  */
12807           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12808             tag_type = typename_type;
12809           /* Look up the name.  */
12810           decl = cp_parser_lookup_name (parser, identifier,
12811                                         tag_type,
12812                                         /*is_template=*/false,
12813                                         /*is_namespace=*/false,
12814                                         check_dependency_p,
12815                                         &ambiguous_decls);
12816           if (ambiguous_decls)
12817             {
12818               error ("reference to %qD is ambiguous", identifier);
12819               print_candidates (ambiguous_decls);
12820               if (cp_parser_parsing_tentatively (parser))
12821                 {
12822                   identifier_token->ambiguous_p = true;
12823                   cp_parser_simulate_error (parser);
12824                 }
12825               return error_mark_node;
12826             }
12827         }
12828     }
12829   else
12830     {
12831       /* Try a template-id.  */
12832       decl = cp_parser_template_id (parser, template_keyword_p,
12833                                     check_dependency_p,
12834                                     is_declaration);
12835       if (decl == error_mark_node)
12836         return error_mark_node;
12837     }
12838
12839   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12840
12841   /* If this is a typename, create a TYPENAME_TYPE.  */
12842   if (typename_p && decl != error_mark_node)
12843     {
12844       decl = make_typename_type (scope, decl, typename_type,
12845                                  /*complain=*/tf_error);
12846       if (decl != error_mark_node)
12847         decl = TYPE_NAME (decl);
12848     }
12849
12850   /* Check to see that it is really the name of a class.  */
12851   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12852       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12853       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12854     /* Situations like this:
12855
12856          template <typename T> struct A {
12857            typename T::template X<int>::I i;
12858          };
12859
12860        are problematic.  Is `T::template X<int>' a class-name?  The
12861        standard does not seem to be definitive, but there is no other
12862        valid interpretation of the following `::'.  Therefore, those
12863        names are considered class-names.  */
12864     {
12865       decl = make_typename_type (scope, decl, tag_type, tf_error);
12866       if (decl != error_mark_node)
12867         decl = TYPE_NAME (decl);
12868     }
12869   else if (TREE_CODE (decl) != TYPE_DECL
12870            || TREE_TYPE (decl) == error_mark_node
12871            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12872     decl = error_mark_node;
12873
12874   if (decl == error_mark_node)
12875     cp_parser_error (parser, "expected class-name");
12876
12877   return decl;
12878 }
12879
12880 /* Parse a class-specifier.
12881
12882    class-specifier:
12883      class-head { member-specification [opt] }
12884
12885    Returns the TREE_TYPE representing the class.  */
12886
12887 static tree
12888 cp_parser_class_specifier (cp_parser* parser)
12889 {
12890   cp_token *token;
12891   tree type;
12892   tree attributes = NULL_TREE;
12893   int has_trailing_semicolon;
12894   bool nested_name_specifier_p;
12895   unsigned saved_num_template_parameter_lists;
12896   tree old_scope = NULL_TREE;
12897   tree scope = NULL_TREE;
12898
12899   push_deferring_access_checks (dk_no_deferred);
12900
12901   /* Parse the class-head.  */
12902   type = cp_parser_class_head (parser,
12903                                &nested_name_specifier_p,
12904                                &attributes);
12905   /* If the class-head was a semantic disaster, skip the entire body
12906      of the class.  */
12907   if (!type)
12908     {
12909       cp_parser_skip_to_end_of_block_or_statement (parser);
12910       pop_deferring_access_checks ();
12911       return error_mark_node;
12912     }
12913
12914   /* Look for the `{'.  */
12915   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12916     {
12917       pop_deferring_access_checks ();
12918       return error_mark_node;
12919     }
12920
12921   /* Issue an error message if type-definitions are forbidden here.  */
12922   cp_parser_check_type_definition (parser);
12923   /* Remember that we are defining one more class.  */
12924   ++parser->num_classes_being_defined;
12925   /* Inside the class, surrounding template-parameter-lists do not
12926      apply.  */
12927   saved_num_template_parameter_lists
12928     = parser->num_template_parameter_lists;
12929   parser->num_template_parameter_lists = 0;
12930
12931   /* Start the class.  */
12932   if (nested_name_specifier_p)
12933     {
12934       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12935       old_scope = push_inner_scope (scope);
12936     }
12937   type = begin_class_definition (type, attributes);
12938
12939   if (type == error_mark_node)
12940     /* If the type is erroneous, skip the entire body of the class.  */
12941     cp_parser_skip_to_closing_brace (parser);
12942   else
12943     /* Parse the member-specification.  */
12944     cp_parser_member_specification_opt (parser);
12945
12946   /* Look for the trailing `}'.  */
12947   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12948   /* We get better error messages by noticing a common problem: a
12949      missing trailing `;'.  */
12950   token = cp_lexer_peek_token (parser->lexer);
12951   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12952   /* Look for trailing attributes to apply to this class.  */
12953   if (cp_parser_allow_gnu_extensions_p (parser))
12954     attributes = cp_parser_attributes_opt (parser);
12955   if (type != error_mark_node)
12956     type = finish_struct (type, attributes);
12957   if (nested_name_specifier_p)
12958     pop_inner_scope (old_scope, scope);
12959   /* If this class is not itself within the scope of another class,
12960      then we need to parse the bodies of all of the queued function
12961      definitions.  Note that the queued functions defined in a class
12962      are not always processed immediately following the
12963      class-specifier for that class.  Consider:
12964
12965        struct A {
12966          struct B { void f() { sizeof (A); } };
12967        };
12968
12969      If `f' were processed before the processing of `A' were
12970      completed, there would be no way to compute the size of `A'.
12971      Note that the nesting we are interested in here is lexical --
12972      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12973      for:
12974
12975        struct A { struct B; };
12976        struct A::B { void f() { } };
12977
12978      there is no need to delay the parsing of `A::B::f'.  */
12979   if (--parser->num_classes_being_defined == 0)
12980     {
12981       tree queue_entry;
12982       tree fn;
12983       tree class_type = NULL_TREE;
12984       tree pushed_scope = NULL_TREE;
12985
12986       /* In a first pass, parse default arguments to the functions.
12987          Then, in a second pass, parse the bodies of the functions.
12988          This two-phased approach handles cases like:
12989
12990             struct S {
12991               void f() { g(); }
12992               void g(int i = 3);
12993             };
12994
12995          */
12996       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12997              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12998            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12999            TREE_PURPOSE (parser->unparsed_functions_queues)
13000              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13001         {
13002           fn = TREE_VALUE (queue_entry);
13003           /* If there are default arguments that have not yet been processed,
13004              take care of them now.  */
13005           if (class_type != TREE_PURPOSE (queue_entry))
13006             {
13007               if (pushed_scope)
13008                 pop_scope (pushed_scope);
13009               class_type = TREE_PURPOSE (queue_entry);
13010               pushed_scope = push_scope (class_type);
13011             }
13012           /* Make sure that any template parameters are in scope.  */
13013           maybe_begin_member_template_processing (fn);
13014           /* Parse the default argument expressions.  */
13015           cp_parser_late_parsing_default_args (parser, fn);
13016           /* Remove any template parameters from the symbol table.  */
13017           maybe_end_member_template_processing ();
13018         }
13019       if (pushed_scope)
13020         pop_scope (pushed_scope);
13021       /* Now parse the body of the functions.  */
13022       for (TREE_VALUE (parser->unparsed_functions_queues)
13023              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13024            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13025            TREE_VALUE (parser->unparsed_functions_queues)
13026              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13027         {
13028           /* Figure out which function we need to process.  */
13029           fn = TREE_VALUE (queue_entry);
13030           /* Parse the function.  */
13031           cp_parser_late_parsing_for_member (parser, fn);
13032         }
13033     }
13034
13035   /* Put back any saved access checks.  */
13036   pop_deferring_access_checks ();
13037
13038   /* Restore the count of active template-parameter-lists.  */
13039   parser->num_template_parameter_lists
13040     = saved_num_template_parameter_lists;
13041
13042   return type;
13043 }
13044
13045 /* Parse a class-head.
13046
13047    class-head:
13048      class-key identifier [opt] base-clause [opt]
13049      class-key nested-name-specifier identifier base-clause [opt]
13050      class-key nested-name-specifier [opt] template-id
13051        base-clause [opt]
13052
13053    GNU Extensions:
13054      class-key attributes identifier [opt] base-clause [opt]
13055      class-key attributes nested-name-specifier identifier base-clause [opt]
13056      class-key attributes nested-name-specifier [opt] template-id
13057        base-clause [opt]
13058
13059    Returns the TYPE of the indicated class.  Sets
13060    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13061    involving a nested-name-specifier was used, and FALSE otherwise.
13062
13063    Returns error_mark_node if this is not a class-head.
13064
13065    Returns NULL_TREE if the class-head is syntactically valid, but
13066    semantically invalid in a way that means we should skip the entire
13067    body of the class.  */
13068
13069 static tree
13070 cp_parser_class_head (cp_parser* parser,
13071                       bool* nested_name_specifier_p,
13072                       tree *attributes_p)
13073 {
13074   tree nested_name_specifier;
13075   enum tag_types class_key;
13076   tree id = NULL_TREE;
13077   tree type = NULL_TREE;
13078   tree attributes;
13079   bool template_id_p = false;
13080   bool qualified_p = false;
13081   bool invalid_nested_name_p = false;
13082   bool invalid_explicit_specialization_p = false;
13083   tree pushed_scope = NULL_TREE;
13084   unsigned num_templates;
13085   tree bases;
13086
13087   /* Assume no nested-name-specifier will be present.  */
13088   *nested_name_specifier_p = false;
13089   /* Assume no template parameter lists will be used in defining the
13090      type.  */
13091   num_templates = 0;
13092
13093   /* Look for the class-key.  */
13094   class_key = cp_parser_class_key (parser);
13095   if (class_key == none_type)
13096     return error_mark_node;
13097
13098   /* Parse the attributes.  */
13099   attributes = cp_parser_attributes_opt (parser);
13100
13101   /* If the next token is `::', that is invalid -- but sometimes
13102      people do try to write:
13103
13104        struct ::S {};
13105
13106      Handle this gracefully by accepting the extra qualifier, and then
13107      issuing an error about it later if this really is a
13108      class-head.  If it turns out just to be an elaborated type
13109      specifier, remain silent.  */
13110   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13111     qualified_p = true;
13112
13113   push_deferring_access_checks (dk_no_check);
13114
13115   /* Determine the name of the class.  Begin by looking for an
13116      optional nested-name-specifier.  */
13117   nested_name_specifier
13118     = cp_parser_nested_name_specifier_opt (parser,
13119                                            /*typename_keyword_p=*/false,
13120                                            /*check_dependency_p=*/false,
13121                                            /*type_p=*/false,
13122                                            /*is_declaration=*/false);
13123   /* If there was a nested-name-specifier, then there *must* be an
13124      identifier.  */
13125   if (nested_name_specifier)
13126     {
13127       /* Although the grammar says `identifier', it really means
13128          `class-name' or `template-name'.  You are only allowed to
13129          define a class that has already been declared with this
13130          syntax.
13131
13132          The proposed resolution for Core Issue 180 says that wherever
13133          you see `class T::X' you should treat `X' as a type-name.
13134
13135          It is OK to define an inaccessible class; for example:
13136
13137            class A { class B; };
13138            class A::B {};
13139
13140          We do not know if we will see a class-name, or a
13141          template-name.  We look for a class-name first, in case the
13142          class-name is a template-id; if we looked for the
13143          template-name first we would stop after the template-name.  */
13144       cp_parser_parse_tentatively (parser);
13145       type = cp_parser_class_name (parser,
13146                                    /*typename_keyword_p=*/false,
13147                                    /*template_keyword_p=*/false,
13148                                    class_type,
13149                                    /*check_dependency_p=*/false,
13150                                    /*class_head_p=*/true,
13151                                    /*is_declaration=*/false);
13152       /* If that didn't work, ignore the nested-name-specifier.  */
13153       if (!cp_parser_parse_definitely (parser))
13154         {
13155           invalid_nested_name_p = true;
13156           id = cp_parser_identifier (parser);
13157           if (id == error_mark_node)
13158             id = NULL_TREE;
13159         }
13160       /* If we could not find a corresponding TYPE, treat this
13161          declaration like an unqualified declaration.  */
13162       if (type == error_mark_node)
13163         nested_name_specifier = NULL_TREE;
13164       /* Otherwise, count the number of templates used in TYPE and its
13165          containing scopes.  */
13166       else
13167         {
13168           tree scope;
13169
13170           for (scope = TREE_TYPE (type);
13171                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13172                scope = (TYPE_P (scope)
13173                         ? TYPE_CONTEXT (scope)
13174                         : DECL_CONTEXT (scope)))
13175             if (TYPE_P (scope)
13176                 && CLASS_TYPE_P (scope)
13177                 && CLASSTYPE_TEMPLATE_INFO (scope)
13178                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13179                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13180               ++num_templates;
13181         }
13182     }
13183   /* Otherwise, the identifier is optional.  */
13184   else
13185     {
13186       /* We don't know whether what comes next is a template-id,
13187          an identifier, or nothing at all.  */
13188       cp_parser_parse_tentatively (parser);
13189       /* Check for a template-id.  */
13190       id = cp_parser_template_id (parser,
13191                                   /*template_keyword_p=*/false,
13192                                   /*check_dependency_p=*/true,
13193                                   /*is_declaration=*/true);
13194       /* If that didn't work, it could still be an identifier.  */
13195       if (!cp_parser_parse_definitely (parser))
13196         {
13197           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13198             id = cp_parser_identifier (parser);
13199           else
13200             id = NULL_TREE;
13201         }
13202       else
13203         {
13204           template_id_p = true;
13205           ++num_templates;
13206         }
13207     }
13208
13209   pop_deferring_access_checks ();
13210
13211   if (id)
13212     cp_parser_check_for_invalid_template_id (parser, id);
13213
13214   /* If it's not a `:' or a `{' then we can't really be looking at a
13215      class-head, since a class-head only appears as part of a
13216      class-specifier.  We have to detect this situation before calling
13217      xref_tag, since that has irreversible side-effects.  */
13218   if (!cp_parser_next_token_starts_class_definition_p (parser))
13219     {
13220       cp_parser_error (parser, "expected %<{%> or %<:%>");
13221       return error_mark_node;
13222     }
13223
13224   /* At this point, we're going ahead with the class-specifier, even
13225      if some other problem occurs.  */
13226   cp_parser_commit_to_tentative_parse (parser);
13227   /* Issue the error about the overly-qualified name now.  */
13228   if (qualified_p)
13229     cp_parser_error (parser,
13230                      "global qualification of class name is invalid");
13231   else if (invalid_nested_name_p)
13232     cp_parser_error (parser,
13233                      "qualified name does not name a class");
13234   else if (nested_name_specifier)
13235     {
13236       tree scope;
13237
13238       /* Reject typedef-names in class heads.  */
13239       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13240         {
13241           error ("invalid class name in declaration of %qD", type);
13242           type = NULL_TREE;
13243           goto done;
13244         }
13245
13246       /* Figure out in what scope the declaration is being placed.  */
13247       scope = current_scope ();
13248       /* If that scope does not contain the scope in which the
13249          class was originally declared, the program is invalid.  */
13250       if (scope && !is_ancestor (scope, nested_name_specifier))
13251         {
13252           error ("declaration of %qD in %qD which does not enclose %qD",
13253                  type, scope, nested_name_specifier);
13254           type = NULL_TREE;
13255           goto done;
13256         }
13257       /* [dcl.meaning]
13258
13259          A declarator-id shall not be qualified exception of the
13260          definition of a ... nested class outside of its class
13261          ... [or] a the definition or explicit instantiation of a
13262          class member of a namespace outside of its namespace.  */
13263       if (scope == nested_name_specifier)
13264         {
13265           pedwarn ("extra qualification ignored");
13266           nested_name_specifier = NULL_TREE;
13267           num_templates = 0;
13268         }
13269     }
13270   /* An explicit-specialization must be preceded by "template <>".  If
13271      it is not, try to recover gracefully.  */
13272   if (at_namespace_scope_p ()
13273       && parser->num_template_parameter_lists == 0
13274       && template_id_p)
13275     {
13276       error ("an explicit specialization must be preceded by %<template <>%>");
13277       invalid_explicit_specialization_p = true;
13278       /* Take the same action that would have been taken by
13279          cp_parser_explicit_specialization.  */
13280       ++parser->num_template_parameter_lists;
13281       begin_specialization ();
13282     }
13283   /* There must be no "return" statements between this point and the
13284      end of this function; set "type "to the correct return value and
13285      use "goto done;" to return.  */
13286   /* Make sure that the right number of template parameters were
13287      present.  */
13288   if (!cp_parser_check_template_parameters (parser, num_templates))
13289     {
13290       /* If something went wrong, there is no point in even trying to
13291          process the class-definition.  */
13292       type = NULL_TREE;
13293       goto done;
13294     }
13295
13296   /* Look up the type.  */
13297   if (template_id_p)
13298     {
13299       type = TREE_TYPE (id);
13300       type = maybe_process_partial_specialization (type);
13301       if (nested_name_specifier)
13302         pushed_scope = push_scope (nested_name_specifier);
13303     }
13304   else if (nested_name_specifier)
13305     {
13306       tree class_type;
13307
13308       /* Given:
13309
13310             template <typename T> struct S { struct T };
13311             template <typename T> struct S<T>::T { };
13312
13313          we will get a TYPENAME_TYPE when processing the definition of
13314          `S::T'.  We need to resolve it to the actual type before we
13315          try to define it.  */
13316       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13317         {
13318           class_type = resolve_typename_type (TREE_TYPE (type),
13319                                               /*only_current_p=*/false);
13320           if (class_type != error_mark_node)
13321             type = TYPE_NAME (class_type);
13322           else
13323             {
13324               cp_parser_error (parser, "could not resolve typename type");
13325               type = error_mark_node;
13326             }
13327         }
13328
13329       maybe_process_partial_specialization (TREE_TYPE (type));
13330       class_type = current_class_type;
13331       /* Enter the scope indicated by the nested-name-specifier.  */
13332       pushed_scope = push_scope (nested_name_specifier);
13333       /* Get the canonical version of this type.  */
13334       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13335       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13336           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13337         {
13338           type = push_template_decl (type);
13339           if (type == error_mark_node)
13340             {
13341               type = NULL_TREE;
13342               goto done;
13343             }
13344         }
13345
13346       type = TREE_TYPE (type);
13347       *nested_name_specifier_p = true;
13348     }
13349   else      /* The name is not a nested name.  */
13350     {
13351       /* If the class was unnamed, create a dummy name.  */
13352       if (!id)
13353         id = make_anon_name ();
13354       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13355                        parser->num_template_parameter_lists);
13356     }
13357
13358   /* Indicate whether this class was declared as a `class' or as a
13359      `struct'.  */
13360   if (TREE_CODE (type) == RECORD_TYPE)
13361     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13362   cp_parser_check_class_key (class_key, type);
13363
13364   /* If this type was already complete, and we see another definition,
13365      that's an error.  */
13366   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13367     {
13368       error ("redefinition of %q#T", type);
13369       error ("previous definition of %q+#T", type);
13370       type = NULL_TREE;
13371       goto done;
13372     }
13373
13374   /* We will have entered the scope containing the class; the names of
13375      base classes should be looked up in that context.  For example:
13376
13377        struct A { struct B {}; struct C; };
13378        struct A::C : B {};
13379
13380      is valid.  */
13381   bases = NULL_TREE;
13382
13383   /* Get the list of base-classes, if there is one.  */
13384   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13385     bases = cp_parser_base_clause (parser);
13386
13387   /* Process the base classes.  */
13388   xref_basetypes (type, bases);
13389
13390  done:
13391   /* Leave the scope given by the nested-name-specifier.  We will
13392      enter the class scope itself while processing the members.  */
13393   if (pushed_scope)
13394     pop_scope (pushed_scope);
13395
13396   if (invalid_explicit_specialization_p)
13397     {
13398       end_specialization ();
13399       --parser->num_template_parameter_lists;
13400     }
13401   *attributes_p = attributes;
13402   return type;
13403 }
13404
13405 /* Parse a class-key.
13406
13407    class-key:
13408      class
13409      struct
13410      union
13411
13412    Returns the kind of class-key specified, or none_type to indicate
13413    error.  */
13414
13415 static enum tag_types
13416 cp_parser_class_key (cp_parser* parser)
13417 {
13418   cp_token *token;
13419   enum tag_types tag_type;
13420
13421   /* Look for the class-key.  */
13422   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13423   if (!token)
13424     return none_type;
13425
13426   /* Check to see if the TOKEN is a class-key.  */
13427   tag_type = cp_parser_token_is_class_key (token);
13428   if (!tag_type)
13429     cp_parser_error (parser, "expected class-key");
13430   return tag_type;
13431 }
13432
13433 /* Parse an (optional) member-specification.
13434
13435    member-specification:
13436      member-declaration member-specification [opt]
13437      access-specifier : member-specification [opt]  */
13438
13439 static void
13440 cp_parser_member_specification_opt (cp_parser* parser)
13441 {
13442   while (true)
13443     {
13444       cp_token *token;
13445       enum rid keyword;
13446
13447       /* Peek at the next token.  */
13448       token = cp_lexer_peek_token (parser->lexer);
13449       /* If it's a `}', or EOF then we've seen all the members.  */
13450       if (token->type == CPP_CLOSE_BRACE
13451           || token->type == CPP_EOF
13452           || token->type == CPP_PRAGMA_EOL)
13453         break;
13454
13455       /* See if this token is a keyword.  */
13456       keyword = token->keyword;
13457       switch (keyword)
13458         {
13459         case RID_PUBLIC:
13460         case RID_PROTECTED:
13461         case RID_PRIVATE:
13462           /* Consume the access-specifier.  */
13463           cp_lexer_consume_token (parser->lexer);
13464           /* Remember which access-specifier is active.  */
13465           current_access_specifier = token->value;
13466           /* Look for the `:'.  */
13467           cp_parser_require (parser, CPP_COLON, "`:'");
13468           break;
13469
13470         default:
13471           /* Accept #pragmas at class scope.  */
13472           if (token->type == CPP_PRAGMA)
13473             {
13474               cp_parser_pragma (parser, pragma_external);
13475               break;
13476             }
13477
13478           /* Otherwise, the next construction must be a
13479              member-declaration.  */
13480           cp_parser_member_declaration (parser);
13481         }
13482     }
13483 }
13484
13485 /* Parse a member-declaration.
13486
13487    member-declaration:
13488      decl-specifier-seq [opt] member-declarator-list [opt] ;
13489      function-definition ; [opt]
13490      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13491      using-declaration
13492      template-declaration
13493
13494    member-declarator-list:
13495      member-declarator
13496      member-declarator-list , member-declarator
13497
13498    member-declarator:
13499      declarator pure-specifier [opt]
13500      declarator constant-initializer [opt]
13501      identifier [opt] : constant-expression
13502
13503    GNU Extensions:
13504
13505    member-declaration:
13506      __extension__ member-declaration
13507
13508    member-declarator:
13509      declarator attributes [opt] pure-specifier [opt]
13510      declarator attributes [opt] constant-initializer [opt]
13511      identifier [opt] attributes [opt] : constant-expression  */
13512
13513 static void
13514 cp_parser_member_declaration (cp_parser* parser)
13515 {
13516   cp_decl_specifier_seq decl_specifiers;
13517   tree prefix_attributes;
13518   tree decl;
13519   int declares_class_or_enum;
13520   bool friend_p;
13521   cp_token *token;
13522   int saved_pedantic;
13523
13524   /* Check for the `__extension__' keyword.  */
13525   if (cp_parser_extension_opt (parser, &saved_pedantic))
13526     {
13527       /* Recurse.  */
13528       cp_parser_member_declaration (parser);
13529       /* Restore the old value of the PEDANTIC flag.  */
13530       pedantic = saved_pedantic;
13531
13532       return;
13533     }
13534
13535   /* Check for a template-declaration.  */
13536   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13537     {
13538       /* An explicit specialization here is an error condition, and we
13539          expect the specialization handler to detect and report this.  */
13540       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13541           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13542         cp_parser_explicit_specialization (parser);
13543       else
13544         cp_parser_template_declaration (parser, /*member_p=*/true);
13545
13546       return;
13547     }
13548
13549   /* Check for a using-declaration.  */
13550   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13551     {
13552       /* Parse the using-declaration.  */
13553       cp_parser_using_declaration (parser);
13554
13555       return;
13556     }
13557
13558   /* Check for @defs.  */
13559   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13560     {
13561       tree ivar, member;
13562       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13563       ivar = ivar_chains;
13564       while (ivar)
13565         {
13566           member = ivar;
13567           ivar = TREE_CHAIN (member);
13568           TREE_CHAIN (member) = NULL_TREE;
13569           finish_member_declaration (member);
13570         }
13571       return;
13572     }
13573
13574   /* Parse the decl-specifier-seq.  */
13575   cp_parser_decl_specifier_seq (parser,
13576                                 CP_PARSER_FLAGS_OPTIONAL,
13577                                 &decl_specifiers,
13578                                 &declares_class_or_enum);
13579   prefix_attributes = decl_specifiers.attributes;
13580   decl_specifiers.attributes = NULL_TREE;
13581   /* Check for an invalid type-name.  */
13582   if (!decl_specifiers.type
13583       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13584     return;
13585   /* If there is no declarator, then the decl-specifier-seq should
13586      specify a type.  */
13587   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13588     {
13589       /* If there was no decl-specifier-seq, and the next token is a
13590          `;', then we have something like:
13591
13592            struct S { ; };
13593
13594          [class.mem]
13595
13596          Each member-declaration shall declare at least one member
13597          name of the class.  */
13598       if (!decl_specifiers.any_specifiers_p)
13599         {
13600           cp_token *token = cp_lexer_peek_token (parser->lexer);
13601           if (pedantic && !token->in_system_header)
13602             pedwarn ("%Hextra %<;%>", &token->location);
13603         }
13604       else
13605         {
13606           tree type;
13607
13608           /* See if this declaration is a friend.  */
13609           friend_p = cp_parser_friend_p (&decl_specifiers);
13610           /* If there were decl-specifiers, check to see if there was
13611              a class-declaration.  */
13612           type = check_tag_decl (&decl_specifiers);
13613           /* Nested classes have already been added to the class, but
13614              a `friend' needs to be explicitly registered.  */
13615           if (friend_p)
13616             {
13617               /* If the `friend' keyword was present, the friend must
13618                  be introduced with a class-key.  */
13619                if (!declares_class_or_enum)
13620                  error ("a class-key must be used when declaring a friend");
13621                /* In this case:
13622
13623                     template <typename T> struct A {
13624                       friend struct A<T>::B;
13625                     };
13626
13627                   A<T>::B will be represented by a TYPENAME_TYPE, and
13628                   therefore not recognized by check_tag_decl.  */
13629                if (!type
13630                    && decl_specifiers.type
13631                    && TYPE_P (decl_specifiers.type))
13632                  type = decl_specifiers.type;
13633                if (!type || !TYPE_P (type))
13634                  error ("friend declaration does not name a class or "
13635                         "function");
13636                else
13637                  make_friend_class (current_class_type, type,
13638                                     /*complain=*/true);
13639             }
13640           /* If there is no TYPE, an error message will already have
13641              been issued.  */
13642           else if (!type || type == error_mark_node)
13643             ;
13644           /* An anonymous aggregate has to be handled specially; such
13645              a declaration really declares a data member (with a
13646              particular type), as opposed to a nested class.  */
13647           else if (ANON_AGGR_TYPE_P (type))
13648             {
13649               /* Remove constructors and such from TYPE, now that we
13650                  know it is an anonymous aggregate.  */
13651               fixup_anonymous_aggr (type);
13652               /* And make the corresponding data member.  */
13653               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13654               /* Add it to the class.  */
13655               finish_member_declaration (decl);
13656             }
13657           else
13658             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13659         }
13660     }
13661   else
13662     {
13663       /* See if these declarations will be friends.  */
13664       friend_p = cp_parser_friend_p (&decl_specifiers);
13665
13666       /* Keep going until we hit the `;' at the end of the
13667          declaration.  */
13668       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13669         {
13670           tree attributes = NULL_TREE;
13671           tree first_attribute;
13672
13673           /* Peek at the next token.  */
13674           token = cp_lexer_peek_token (parser->lexer);
13675
13676           /* Check for a bitfield declaration.  */
13677           if (token->type == CPP_COLON
13678               || (token->type == CPP_NAME
13679                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13680                   == CPP_COLON))
13681             {
13682               tree identifier;
13683               tree width;
13684
13685               /* Get the name of the bitfield.  Note that we cannot just
13686                  check TOKEN here because it may have been invalidated by
13687                  the call to cp_lexer_peek_nth_token above.  */
13688               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13689                 identifier = cp_parser_identifier (parser);
13690               else
13691                 identifier = NULL_TREE;
13692
13693               /* Consume the `:' token.  */
13694               cp_lexer_consume_token (parser->lexer);
13695               /* Get the width of the bitfield.  */
13696               width
13697                 = cp_parser_constant_expression (parser,
13698                                                  /*allow_non_constant=*/false,
13699                                                  NULL);
13700
13701               /* Look for attributes that apply to the bitfield.  */
13702               attributes = cp_parser_attributes_opt (parser);
13703               /* Remember which attributes are prefix attributes and
13704                  which are not.  */
13705               first_attribute = attributes;
13706               /* Combine the attributes.  */
13707               attributes = chainon (prefix_attributes, attributes);
13708
13709               /* Create the bitfield declaration.  */
13710               decl = grokbitfield (identifier
13711                                    ? make_id_declarator (NULL_TREE,
13712                                                          identifier,
13713                                                          sfk_none)
13714                                    : NULL,
13715                                    &decl_specifiers,
13716                                    width);
13717               /* Apply the attributes.  */
13718               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13719             }
13720           else
13721             {
13722               cp_declarator *declarator;
13723               tree initializer;
13724               tree asm_specification;
13725               int ctor_dtor_or_conv_p;
13726
13727               /* Parse the declarator.  */
13728               declarator
13729                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13730                                         &ctor_dtor_or_conv_p,
13731                                         /*parenthesized_p=*/NULL,
13732                                         /*member_p=*/true);
13733
13734               /* If something went wrong parsing the declarator, make sure
13735                  that we at least consume some tokens.  */
13736               if (declarator == cp_error_declarator)
13737                 {
13738                   /* Skip to the end of the statement.  */
13739                   cp_parser_skip_to_end_of_statement (parser);
13740                   /* If the next token is not a semicolon, that is
13741                      probably because we just skipped over the body of
13742                      a function.  So, we consume a semicolon if
13743                      present, but do not issue an error message if it
13744                      is not present.  */
13745                   if (cp_lexer_next_token_is (parser->lexer,
13746                                               CPP_SEMICOLON))
13747                     cp_lexer_consume_token (parser->lexer);
13748                   return;
13749                 }
13750
13751               if (declares_class_or_enum & 2)
13752                 cp_parser_check_for_definition_in_return_type
13753                   (declarator, decl_specifiers.type);
13754
13755               /* Look for an asm-specification.  */
13756               asm_specification = cp_parser_asm_specification_opt (parser);
13757               /* Look for attributes that apply to the declaration.  */
13758               attributes = cp_parser_attributes_opt (parser);
13759               /* Remember which attributes are prefix attributes and
13760                  which are not.  */
13761               first_attribute = attributes;
13762               /* Combine the attributes.  */
13763               attributes = chainon (prefix_attributes, attributes);
13764
13765               /* If it's an `=', then we have a constant-initializer or a
13766                  pure-specifier.  It is not correct to parse the
13767                  initializer before registering the member declaration
13768                  since the member declaration should be in scope while
13769                  its initializer is processed.  However, the rest of the
13770                  front end does not yet provide an interface that allows
13771                  us to handle this correctly.  */
13772               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13773                 {
13774                   /* In [class.mem]:
13775
13776                      A pure-specifier shall be used only in the declaration of
13777                      a virtual function.
13778
13779                      A member-declarator can contain a constant-initializer
13780                      only if it declares a static member of integral or
13781                      enumeration type.
13782
13783                      Therefore, if the DECLARATOR is for a function, we look
13784                      for a pure-specifier; otherwise, we look for a
13785                      constant-initializer.  When we call `grokfield', it will
13786                      perform more stringent semantics checks.  */
13787                   if (declarator->kind == cdk_function
13788                       && declarator->declarator->kind == cdk_id)
13789                     initializer = cp_parser_pure_specifier (parser);
13790                   else
13791                     /* Parse the initializer.  */
13792                     initializer = cp_parser_constant_initializer (parser);
13793                 }
13794               /* Otherwise, there is no initializer.  */
13795               else
13796                 initializer = NULL_TREE;
13797
13798               /* See if we are probably looking at a function
13799                  definition.  We are certainly not looking at a
13800                  member-declarator.  Calling `grokfield' has
13801                  side-effects, so we must not do it unless we are sure
13802                  that we are looking at a member-declarator.  */
13803               if (cp_parser_token_starts_function_definition_p
13804                   (cp_lexer_peek_token (parser->lexer)))
13805                 {
13806                   /* The grammar does not allow a pure-specifier to be
13807                      used when a member function is defined.  (It is
13808                      possible that this fact is an oversight in the
13809                      standard, since a pure function may be defined
13810                      outside of the class-specifier.  */
13811                   if (initializer)
13812                     error ("pure-specifier on function-definition");
13813                   decl = cp_parser_save_member_function_body (parser,
13814                                                               &decl_specifiers,
13815                                                               declarator,
13816                                                               attributes);
13817                   /* If the member was not a friend, declare it here.  */
13818                   if (!friend_p)
13819                     finish_member_declaration (decl);
13820                   /* Peek at the next token.  */
13821                   token = cp_lexer_peek_token (parser->lexer);
13822                   /* If the next token is a semicolon, consume it.  */
13823                   if (token->type == CPP_SEMICOLON)
13824                     cp_lexer_consume_token (parser->lexer);
13825                   return;
13826                 }
13827               else
13828                 /* Create the declaration.  */
13829                 decl = grokfield (declarator, &decl_specifiers,
13830                                   initializer, /*init_const_expr_p=*/true,
13831                                   asm_specification,
13832                                   attributes);
13833             }
13834
13835           /* Reset PREFIX_ATTRIBUTES.  */
13836           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13837             attributes = TREE_CHAIN (attributes);
13838           if (attributes)
13839             TREE_CHAIN (attributes) = NULL_TREE;
13840
13841           /* If there is any qualification still in effect, clear it
13842              now; we will be starting fresh with the next declarator.  */
13843           parser->scope = NULL_TREE;
13844           parser->qualifying_scope = NULL_TREE;
13845           parser->object_scope = NULL_TREE;
13846           /* If it's a `,', then there are more declarators.  */
13847           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13848             cp_lexer_consume_token (parser->lexer);
13849           /* If the next token isn't a `;', then we have a parse error.  */
13850           else if (cp_lexer_next_token_is_not (parser->lexer,
13851                                                CPP_SEMICOLON))
13852             {
13853               cp_parser_error (parser, "expected %<;%>");
13854               /* Skip tokens until we find a `;'.  */
13855               cp_parser_skip_to_end_of_statement (parser);
13856
13857               break;
13858             }
13859
13860           if (decl)
13861             {
13862               /* Add DECL to the list of members.  */
13863               if (!friend_p)
13864                 finish_member_declaration (decl);
13865
13866               if (TREE_CODE (decl) == FUNCTION_DECL)
13867                 cp_parser_save_default_args (parser, decl);
13868             }
13869         }
13870     }
13871
13872   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13873 }
13874
13875 /* Parse a pure-specifier.
13876
13877    pure-specifier:
13878      = 0
13879
13880    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13881    Otherwise, ERROR_MARK_NODE is returned.  */
13882
13883 static tree
13884 cp_parser_pure_specifier (cp_parser* parser)
13885 {
13886   cp_token *token;
13887
13888   /* Look for the `=' token.  */
13889   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13890     return error_mark_node;
13891   /* Look for the `0' token.  */
13892   token = cp_lexer_consume_token (parser->lexer);
13893   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
13894   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13895     {
13896       cp_parser_error (parser,
13897                        "invalid pure specifier (only `= 0' is allowed)");
13898       cp_parser_skip_to_end_of_statement (parser);
13899       return error_mark_node;
13900     }
13901   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13902     {
13903       error ("templates may not be %<virtual%>");
13904       return error_mark_node;
13905     }
13906
13907   return integer_zero_node;
13908 }
13909
13910 /* Parse a constant-initializer.
13911
13912    constant-initializer:
13913      = constant-expression
13914
13915    Returns a representation of the constant-expression.  */
13916
13917 static tree
13918 cp_parser_constant_initializer (cp_parser* parser)
13919 {
13920   /* Look for the `=' token.  */
13921   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13922     return error_mark_node;
13923
13924   /* It is invalid to write:
13925
13926        struct S { static const int i = { 7 }; };
13927
13928      */
13929   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13930     {
13931       cp_parser_error (parser,
13932                        "a brace-enclosed initializer is not allowed here");
13933       /* Consume the opening brace.  */
13934       cp_lexer_consume_token (parser->lexer);
13935       /* Skip the initializer.  */
13936       cp_parser_skip_to_closing_brace (parser);
13937       /* Look for the trailing `}'.  */
13938       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13939
13940       return error_mark_node;
13941     }
13942
13943   return cp_parser_constant_expression (parser,
13944                                         /*allow_non_constant=*/false,
13945                                         NULL);
13946 }
13947
13948 /* Derived classes [gram.class.derived] */
13949
13950 /* Parse a base-clause.
13951
13952    base-clause:
13953      : base-specifier-list
13954
13955    base-specifier-list:
13956      base-specifier
13957      base-specifier-list , base-specifier
13958
13959    Returns a TREE_LIST representing the base-classes, in the order in
13960    which they were declared.  The representation of each node is as
13961    described by cp_parser_base_specifier.
13962
13963    In the case that no bases are specified, this function will return
13964    NULL_TREE, not ERROR_MARK_NODE.  */
13965
13966 static tree
13967 cp_parser_base_clause (cp_parser* parser)
13968 {
13969   tree bases = NULL_TREE;
13970
13971   /* Look for the `:' that begins the list.  */
13972   cp_parser_require (parser, CPP_COLON, "`:'");
13973
13974   /* Scan the base-specifier-list.  */
13975   while (true)
13976     {
13977       cp_token *token;
13978       tree base;
13979
13980       /* Look for the base-specifier.  */
13981       base = cp_parser_base_specifier (parser);
13982       /* Add BASE to the front of the list.  */
13983       if (base != error_mark_node)
13984         {
13985           TREE_CHAIN (base) = bases;
13986           bases = base;
13987         }
13988       /* Peek at the next token.  */
13989       token = cp_lexer_peek_token (parser->lexer);
13990       /* If it's not a comma, then the list is complete.  */
13991       if (token->type != CPP_COMMA)
13992         break;
13993       /* Consume the `,'.  */
13994       cp_lexer_consume_token (parser->lexer);
13995     }
13996
13997   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13998      base class had a qualified name.  However, the next name that
13999      appears is certainly not qualified.  */
14000   parser->scope = NULL_TREE;
14001   parser->qualifying_scope = NULL_TREE;
14002   parser->object_scope = NULL_TREE;
14003
14004   return nreverse (bases);
14005 }
14006
14007 /* Parse a base-specifier.
14008
14009    base-specifier:
14010      :: [opt] nested-name-specifier [opt] class-name
14011      virtual access-specifier [opt] :: [opt] nested-name-specifier
14012        [opt] class-name
14013      access-specifier virtual [opt] :: [opt] nested-name-specifier
14014        [opt] class-name
14015
14016    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14017    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14018    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14019    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14020
14021 static tree
14022 cp_parser_base_specifier (cp_parser* parser)
14023 {
14024   cp_token *token;
14025   bool done = false;
14026   bool virtual_p = false;
14027   bool duplicate_virtual_error_issued_p = false;
14028   bool duplicate_access_error_issued_p = false;
14029   bool class_scope_p, template_p;
14030   tree access = access_default_node;
14031   tree type;
14032
14033   /* Process the optional `virtual' and `access-specifier'.  */
14034   while (!done)
14035     {
14036       /* Peek at the next token.  */
14037       token = cp_lexer_peek_token (parser->lexer);
14038       /* Process `virtual'.  */
14039       switch (token->keyword)
14040         {
14041         case RID_VIRTUAL:
14042           /* If `virtual' appears more than once, issue an error.  */
14043           if (virtual_p && !duplicate_virtual_error_issued_p)
14044             {
14045               cp_parser_error (parser,
14046                                "%<virtual%> specified more than once in base-specified");
14047               duplicate_virtual_error_issued_p = true;
14048             }
14049
14050           virtual_p = true;
14051
14052           /* Consume the `virtual' token.  */
14053           cp_lexer_consume_token (parser->lexer);
14054
14055           break;
14056
14057         case RID_PUBLIC:
14058         case RID_PROTECTED:
14059         case RID_PRIVATE:
14060           /* If more than one access specifier appears, issue an
14061              error.  */
14062           if (access != access_default_node
14063               && !duplicate_access_error_issued_p)
14064             {
14065               cp_parser_error (parser,
14066                                "more than one access specifier in base-specified");
14067               duplicate_access_error_issued_p = true;
14068             }
14069
14070           access = ridpointers[(int) token->keyword];
14071
14072           /* Consume the access-specifier.  */
14073           cp_lexer_consume_token (parser->lexer);
14074
14075           break;
14076
14077         default:
14078           done = true;
14079           break;
14080         }
14081     }
14082   /* It is not uncommon to see programs mechanically, erroneously, use
14083      the 'typename' keyword to denote (dependent) qualified types
14084      as base classes.  */
14085   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14086     {
14087       if (!processing_template_decl)
14088         error ("keyword %<typename%> not allowed outside of templates");
14089       else
14090         error ("keyword %<typename%> not allowed in this context "
14091                "(the base class is implicitly a type)");
14092       cp_lexer_consume_token (parser->lexer);
14093     }
14094
14095   /* Look for the optional `::' operator.  */
14096   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14097   /* Look for the nested-name-specifier.  The simplest way to
14098      implement:
14099
14100        [temp.res]
14101
14102        The keyword `typename' is not permitted in a base-specifier or
14103        mem-initializer; in these contexts a qualified name that
14104        depends on a template-parameter is implicitly assumed to be a
14105        type name.
14106
14107      is to pretend that we have seen the `typename' keyword at this
14108      point.  */
14109   cp_parser_nested_name_specifier_opt (parser,
14110                                        /*typename_keyword_p=*/true,
14111                                        /*check_dependency_p=*/true,
14112                                        typename_type,
14113                                        /*is_declaration=*/true);
14114   /* If the base class is given by a qualified name, assume that names
14115      we see are type names or templates, as appropriate.  */
14116   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14117   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14118
14119   /* Finally, look for the class-name.  */
14120   type = cp_parser_class_name (parser,
14121                                class_scope_p,
14122                                template_p,
14123                                typename_type,
14124                                /*check_dependency_p=*/true,
14125                                /*class_head_p=*/false,
14126                                /*is_declaration=*/true);
14127
14128   if (type == error_mark_node)
14129     return error_mark_node;
14130
14131   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14132 }
14133
14134 /* Exception handling [gram.exception] */
14135
14136 /* Parse an (optional) exception-specification.
14137
14138    exception-specification:
14139      throw ( type-id-list [opt] )
14140
14141    Returns a TREE_LIST representing the exception-specification.  The
14142    TREE_VALUE of each node is a type.  */
14143
14144 static tree
14145 cp_parser_exception_specification_opt (cp_parser* parser)
14146 {
14147   cp_token *token;
14148   tree type_id_list;
14149
14150   /* Peek at the next token.  */
14151   token = cp_lexer_peek_token (parser->lexer);
14152   /* If it's not `throw', then there's no exception-specification.  */
14153   if (!cp_parser_is_keyword (token, RID_THROW))
14154     return NULL_TREE;
14155
14156   /* Consume the `throw'.  */
14157   cp_lexer_consume_token (parser->lexer);
14158
14159   /* Look for the `('.  */
14160   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14161
14162   /* Peek at the next token.  */
14163   token = cp_lexer_peek_token (parser->lexer);
14164   /* If it's not a `)', then there is a type-id-list.  */
14165   if (token->type != CPP_CLOSE_PAREN)
14166     {
14167       const char *saved_message;
14168
14169       /* Types may not be defined in an exception-specification.  */
14170       saved_message = parser->type_definition_forbidden_message;
14171       parser->type_definition_forbidden_message
14172         = "types may not be defined in an exception-specification";
14173       /* Parse the type-id-list.  */
14174       type_id_list = cp_parser_type_id_list (parser);
14175       /* Restore the saved message.  */
14176       parser->type_definition_forbidden_message = saved_message;
14177     }
14178   else
14179     type_id_list = empty_except_spec;
14180
14181   /* Look for the `)'.  */
14182   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14183
14184   return type_id_list;
14185 }
14186
14187 /* Parse an (optional) type-id-list.
14188
14189    type-id-list:
14190      type-id
14191      type-id-list , type-id
14192
14193    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14194    in the order that the types were presented.  */
14195
14196 static tree
14197 cp_parser_type_id_list (cp_parser* parser)
14198 {
14199   tree types = NULL_TREE;
14200
14201   while (true)
14202     {
14203       cp_token *token;
14204       tree type;
14205
14206       /* Get the next type-id.  */
14207       type = cp_parser_type_id (parser);
14208       /* Add it to the list.  */
14209       types = add_exception_specifier (types, type, /*complain=*/1);
14210       /* Peek at the next token.  */
14211       token = cp_lexer_peek_token (parser->lexer);
14212       /* If it is not a `,', we are done.  */
14213       if (token->type != CPP_COMMA)
14214         break;
14215       /* Consume the `,'.  */
14216       cp_lexer_consume_token (parser->lexer);
14217     }
14218
14219   return nreverse (types);
14220 }
14221
14222 /* Parse a try-block.
14223
14224    try-block:
14225      try compound-statement handler-seq  */
14226
14227 static tree
14228 cp_parser_try_block (cp_parser* parser)
14229 {
14230   tree try_block;
14231
14232   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14233   try_block = begin_try_block ();
14234   cp_parser_compound_statement (parser, NULL, true);
14235   finish_try_block (try_block);
14236   cp_parser_handler_seq (parser);
14237   finish_handler_sequence (try_block);
14238
14239   return try_block;
14240 }
14241
14242 /* Parse a function-try-block.
14243
14244    function-try-block:
14245      try ctor-initializer [opt] function-body handler-seq  */
14246
14247 static bool
14248 cp_parser_function_try_block (cp_parser* parser)
14249 {
14250   tree compound_stmt;
14251   tree try_block;
14252   bool ctor_initializer_p;
14253
14254   /* Look for the `try' keyword.  */
14255   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14256     return false;
14257   /* Let the rest of the front-end know where we are.  */
14258   try_block = begin_function_try_block (&compound_stmt);
14259   /* Parse the function-body.  */
14260   ctor_initializer_p
14261     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14262   /* We're done with the `try' part.  */
14263   finish_function_try_block (try_block);
14264   /* Parse the handlers.  */
14265   cp_parser_handler_seq (parser);
14266   /* We're done with the handlers.  */
14267   finish_function_handler_sequence (try_block, compound_stmt);
14268
14269   return ctor_initializer_p;
14270 }
14271
14272 /* Parse a handler-seq.
14273
14274    handler-seq:
14275      handler handler-seq [opt]  */
14276
14277 static void
14278 cp_parser_handler_seq (cp_parser* parser)
14279 {
14280   while (true)
14281     {
14282       cp_token *token;
14283
14284       /* Parse the handler.  */
14285       cp_parser_handler (parser);
14286       /* Peek at the next token.  */
14287       token = cp_lexer_peek_token (parser->lexer);
14288       /* If it's not `catch' then there are no more handlers.  */
14289       if (!cp_parser_is_keyword (token, RID_CATCH))
14290         break;
14291     }
14292 }
14293
14294 /* Parse a handler.
14295
14296    handler:
14297      catch ( exception-declaration ) compound-statement  */
14298
14299 static void
14300 cp_parser_handler (cp_parser* parser)
14301 {
14302   tree handler;
14303   tree declaration;
14304
14305   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14306   handler = begin_handler ();
14307   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14308   declaration = cp_parser_exception_declaration (parser);
14309   finish_handler_parms (declaration, handler);
14310   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14311   cp_parser_compound_statement (parser, NULL, false);
14312   finish_handler (handler);
14313 }
14314
14315 /* Parse an exception-declaration.
14316
14317    exception-declaration:
14318      type-specifier-seq declarator
14319      type-specifier-seq abstract-declarator
14320      type-specifier-seq
14321      ...
14322
14323    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14324    ellipsis variant is used.  */
14325
14326 static tree
14327 cp_parser_exception_declaration (cp_parser* parser)
14328 {
14329   cp_decl_specifier_seq type_specifiers;
14330   cp_declarator *declarator;
14331   const char *saved_message;
14332
14333   /* If it's an ellipsis, it's easy to handle.  */
14334   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14335     {
14336       /* Consume the `...' token.  */
14337       cp_lexer_consume_token (parser->lexer);
14338       return NULL_TREE;
14339     }
14340
14341   /* Types may not be defined in exception-declarations.  */
14342   saved_message = parser->type_definition_forbidden_message;
14343   parser->type_definition_forbidden_message
14344     = "types may not be defined in exception-declarations";
14345
14346   /* Parse the type-specifier-seq.  */
14347   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14348                                 &type_specifiers);
14349   /* If it's a `)', then there is no declarator.  */
14350   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14351     declarator = NULL;
14352   else
14353     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14354                                        /*ctor_dtor_or_conv_p=*/NULL,
14355                                        /*parenthesized_p=*/NULL,
14356                                        /*member_p=*/false);
14357
14358   /* Restore the saved message.  */
14359   parser->type_definition_forbidden_message = saved_message;
14360
14361   if (!type_specifiers.any_specifiers_p)
14362     return error_mark_node;
14363
14364   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14365 }
14366
14367 /* Parse a throw-expression.
14368
14369    throw-expression:
14370      throw assignment-expression [opt]
14371
14372    Returns a THROW_EXPR representing the throw-expression.  */
14373
14374 static tree
14375 cp_parser_throw_expression (cp_parser* parser)
14376 {
14377   tree expression;
14378   cp_token* token;
14379
14380   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14381   token = cp_lexer_peek_token (parser->lexer);
14382   /* Figure out whether or not there is an assignment-expression
14383      following the "throw" keyword.  */
14384   if (token->type == CPP_COMMA
14385       || token->type == CPP_SEMICOLON
14386       || token->type == CPP_CLOSE_PAREN
14387       || token->type == CPP_CLOSE_SQUARE
14388       || token->type == CPP_CLOSE_BRACE
14389       || token->type == CPP_COLON)
14390     expression = NULL_TREE;
14391   else
14392     expression = cp_parser_assignment_expression (parser,
14393                                                   /*cast_p=*/false);
14394
14395   return build_throw (expression);
14396 }
14397
14398 /* GNU Extensions */
14399
14400 /* Parse an (optional) asm-specification.
14401
14402    asm-specification:
14403      asm ( string-literal )
14404
14405    If the asm-specification is present, returns a STRING_CST
14406    corresponding to the string-literal.  Otherwise, returns
14407    NULL_TREE.  */
14408
14409 static tree
14410 cp_parser_asm_specification_opt (cp_parser* parser)
14411 {
14412   cp_token *token;
14413   tree asm_specification;
14414
14415   /* Peek at the next token.  */
14416   token = cp_lexer_peek_token (parser->lexer);
14417   /* If the next token isn't the `asm' keyword, then there's no
14418      asm-specification.  */
14419   if (!cp_parser_is_keyword (token, RID_ASM))
14420     return NULL_TREE;
14421
14422   /* Consume the `asm' token.  */
14423   cp_lexer_consume_token (parser->lexer);
14424   /* Look for the `('.  */
14425   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14426
14427   /* Look for the string-literal.  */
14428   asm_specification = cp_parser_string_literal (parser, false, false);
14429
14430   /* Look for the `)'.  */
14431   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14432
14433   return asm_specification;
14434 }
14435
14436 /* Parse an asm-operand-list.
14437
14438    asm-operand-list:
14439      asm-operand
14440      asm-operand-list , asm-operand
14441
14442    asm-operand:
14443      string-literal ( expression )
14444      [ string-literal ] string-literal ( expression )
14445
14446    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14447    each node is the expression.  The TREE_PURPOSE is itself a
14448    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14449    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14450    is a STRING_CST for the string literal before the parenthesis.  */
14451
14452 static tree
14453 cp_parser_asm_operand_list (cp_parser* parser)
14454 {
14455   tree asm_operands = NULL_TREE;
14456
14457   while (true)
14458     {
14459       tree string_literal;
14460       tree expression;
14461       tree name;
14462
14463       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14464         {
14465           /* Consume the `[' token.  */
14466           cp_lexer_consume_token (parser->lexer);
14467           /* Read the operand name.  */
14468           name = cp_parser_identifier (parser);
14469           if (name != error_mark_node)
14470             name = build_string (IDENTIFIER_LENGTH (name),
14471                                  IDENTIFIER_POINTER (name));
14472           /* Look for the closing `]'.  */
14473           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14474         }
14475       else
14476         name = NULL_TREE;
14477       /* Look for the string-literal.  */
14478       string_literal = cp_parser_string_literal (parser, false, false);
14479
14480       /* Look for the `('.  */
14481       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14482       /* Parse the expression.  */
14483       expression = cp_parser_expression (parser, /*cast_p=*/false);
14484       /* Look for the `)'.  */
14485       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14486
14487       /* Add this operand to the list.  */
14488       asm_operands = tree_cons (build_tree_list (name, string_literal),
14489                                 expression,
14490                                 asm_operands);
14491       /* If the next token is not a `,', there are no more
14492          operands.  */
14493       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14494         break;
14495       /* Consume the `,'.  */
14496       cp_lexer_consume_token (parser->lexer);
14497     }
14498
14499   return nreverse (asm_operands);
14500 }
14501
14502 /* Parse an asm-clobber-list.
14503
14504    asm-clobber-list:
14505      string-literal
14506      asm-clobber-list , string-literal
14507
14508    Returns a TREE_LIST, indicating the clobbers in the order that they
14509    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14510
14511 static tree
14512 cp_parser_asm_clobber_list (cp_parser* parser)
14513 {
14514   tree clobbers = NULL_TREE;
14515
14516   while (true)
14517     {
14518       tree string_literal;
14519
14520       /* Look for the string literal.  */
14521       string_literal = cp_parser_string_literal (parser, false, false);
14522       /* Add it to the list.  */
14523       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14524       /* If the next token is not a `,', then the list is
14525          complete.  */
14526       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14527         break;
14528       /* Consume the `,' token.  */
14529       cp_lexer_consume_token (parser->lexer);
14530     }
14531
14532   return clobbers;
14533 }
14534
14535 /* Parse an (optional) series of attributes.
14536
14537    attributes:
14538      attributes attribute
14539
14540    attribute:
14541      __attribute__ (( attribute-list [opt] ))
14542
14543    The return value is as for cp_parser_attribute_list.  */
14544
14545 static tree
14546 cp_parser_attributes_opt (cp_parser* parser)
14547 {
14548   tree attributes = NULL_TREE;
14549
14550   while (true)
14551     {
14552       cp_token *token;
14553       tree attribute_list;
14554
14555       /* Peek at the next token.  */
14556       token = cp_lexer_peek_token (parser->lexer);
14557       /* If it's not `__attribute__', then we're done.  */
14558       if (token->keyword != RID_ATTRIBUTE)
14559         break;
14560
14561       /* Consume the `__attribute__' keyword.  */
14562       cp_lexer_consume_token (parser->lexer);
14563       /* Look for the two `(' tokens.  */
14564       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14565       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14566
14567       /* Peek at the next token.  */
14568       token = cp_lexer_peek_token (parser->lexer);
14569       if (token->type != CPP_CLOSE_PAREN)
14570         /* Parse the attribute-list.  */
14571         attribute_list = cp_parser_attribute_list (parser);
14572       else
14573         /* If the next token is a `)', then there is no attribute
14574            list.  */
14575         attribute_list = NULL;
14576
14577       /* Look for the two `)' tokens.  */
14578       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14579       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14580
14581       /* Add these new attributes to the list.  */
14582       attributes = chainon (attributes, attribute_list);
14583     }
14584
14585   return attributes;
14586 }
14587
14588 /* Parse an attribute-list.
14589
14590    attribute-list:
14591      attribute
14592      attribute-list , attribute
14593
14594    attribute:
14595      identifier
14596      identifier ( identifier )
14597      identifier ( identifier , expression-list )
14598      identifier ( expression-list )
14599
14600    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14601    to an attribute.  The TREE_PURPOSE of each node is the identifier
14602    indicating which attribute is in use.  The TREE_VALUE represents
14603    the arguments, if any.  */
14604
14605 static tree
14606 cp_parser_attribute_list (cp_parser* parser)
14607 {
14608   tree attribute_list = NULL_TREE;
14609   bool save_translate_strings_p = parser->translate_strings_p;
14610
14611   parser->translate_strings_p = false;
14612   while (true)
14613     {
14614       cp_token *token;
14615       tree identifier;
14616       tree attribute;
14617
14618       /* Look for the identifier.  We also allow keywords here; for
14619          example `__attribute__ ((const))' is legal.  */
14620       token = cp_lexer_peek_token (parser->lexer);
14621       if (token->type == CPP_NAME
14622           || token->type == CPP_KEYWORD)
14623         {
14624           tree arguments = NULL_TREE;
14625
14626           /* Consume the token.  */
14627           token = cp_lexer_consume_token (parser->lexer);
14628
14629           /* Save away the identifier that indicates which attribute
14630              this is.  */
14631           identifier = token->value;
14632           attribute = build_tree_list (identifier, NULL_TREE);
14633
14634           /* Peek at the next token.  */
14635           token = cp_lexer_peek_token (parser->lexer);
14636           /* If it's an `(', then parse the attribute arguments.  */
14637           if (token->type == CPP_OPEN_PAREN)
14638             {
14639               arguments = cp_parser_parenthesized_expression_list
14640                           (parser, true, /*cast_p=*/false,
14641                            /*non_constant_p=*/NULL);
14642               /* Save the arguments away.  */
14643               TREE_VALUE (attribute) = arguments;
14644             }
14645
14646           if (arguments != error_mark_node)
14647             {
14648               /* Add this attribute to the list.  */
14649               TREE_CHAIN (attribute) = attribute_list;
14650               attribute_list = attribute;
14651             }
14652
14653           token = cp_lexer_peek_token (parser->lexer);
14654         }
14655       /* Now, look for more attributes.  If the next token isn't a
14656          `,', we're done.  */
14657       if (token->type != CPP_COMMA)
14658         break;
14659
14660       /* Consume the comma and keep going.  */
14661       cp_lexer_consume_token (parser->lexer);
14662     }
14663   parser->translate_strings_p = save_translate_strings_p;
14664
14665   /* We built up the list in reverse order.  */
14666   return nreverse (attribute_list);
14667 }
14668
14669 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14670    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14671    current value of the PEDANTIC flag, regardless of whether or not
14672    the `__extension__' keyword is present.  The caller is responsible
14673    for restoring the value of the PEDANTIC flag.  */
14674
14675 static bool
14676 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14677 {
14678   /* Save the old value of the PEDANTIC flag.  */
14679   *saved_pedantic = pedantic;
14680
14681   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14682     {
14683       /* Consume the `__extension__' token.  */
14684       cp_lexer_consume_token (parser->lexer);
14685       /* We're not being pedantic while the `__extension__' keyword is
14686          in effect.  */
14687       pedantic = 0;
14688
14689       return true;
14690     }
14691
14692   return false;
14693 }
14694
14695 /* Parse a label declaration.
14696
14697    label-declaration:
14698      __label__ label-declarator-seq ;
14699
14700    label-declarator-seq:
14701      identifier , label-declarator-seq
14702      identifier  */
14703
14704 static void
14705 cp_parser_label_declaration (cp_parser* parser)
14706 {
14707   /* Look for the `__label__' keyword.  */
14708   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14709
14710   while (true)
14711     {
14712       tree identifier;
14713
14714       /* Look for an identifier.  */
14715       identifier = cp_parser_identifier (parser);
14716       /* If we failed, stop.  */
14717       if (identifier == error_mark_node)
14718         break;
14719       /* Declare it as a label.  */
14720       finish_label_decl (identifier);
14721       /* If the next token is a `;', stop.  */
14722       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14723         break;
14724       /* Look for the `,' separating the label declarations.  */
14725       cp_parser_require (parser, CPP_COMMA, "`,'");
14726     }
14727
14728   /* Look for the final `;'.  */
14729   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14730 }
14731
14732 /* Support Functions */
14733
14734 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14735    NAME should have one of the representations used for an
14736    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14737    is returned.  If PARSER->SCOPE is a dependent type, then a
14738    SCOPE_REF is returned.
14739
14740    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14741    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14742    was formed.  Abstractly, such entities should not be passed to this
14743    function, because they do not need to be looked up, but it is
14744    simpler to check for this special case here, rather than at the
14745    call-sites.
14746
14747    In cases not explicitly covered above, this function returns a
14748    DECL, OVERLOAD, or baselink representing the result of the lookup.
14749    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14750    is returned.
14751
14752    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14753    (e.g., "struct") that was used.  In that case bindings that do not
14754    refer to types are ignored.
14755
14756    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14757    ignored.
14758
14759    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14760    are ignored.
14761
14762    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14763    types.
14764
14765    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14766    TREE_LIST of candidates if name-lookup results in an ambiguity, and
14767    NULL_TREE otherwise.  */
14768
14769 static tree
14770 cp_parser_lookup_name (cp_parser *parser, tree name,
14771                        enum tag_types tag_type,
14772                        bool is_template,
14773                        bool is_namespace,
14774                        bool check_dependency,
14775                        tree *ambiguous_decls)
14776 {
14777   int flags = 0;
14778   tree decl;
14779   tree object_type = parser->context->object_type;
14780
14781   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14782     flags |= LOOKUP_COMPLAIN;
14783
14784   /* Assume that the lookup will be unambiguous.  */
14785   if (ambiguous_decls)
14786     *ambiguous_decls = NULL_TREE;
14787
14788   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14789      no longer valid.  Note that if we are parsing tentatively, and
14790      the parse fails, OBJECT_TYPE will be automatically restored.  */
14791   parser->context->object_type = NULL_TREE;
14792
14793   if (name == error_mark_node)
14794     return error_mark_node;
14795
14796   /* A template-id has already been resolved; there is no lookup to
14797      do.  */
14798   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14799     return name;
14800   if (BASELINK_P (name))
14801     {
14802       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14803                   == TEMPLATE_ID_EXPR);
14804       return name;
14805     }
14806
14807   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14808      it should already have been checked to make sure that the name
14809      used matches the type being destroyed.  */
14810   if (TREE_CODE (name) == BIT_NOT_EXPR)
14811     {
14812       tree type;
14813
14814       /* Figure out to which type this destructor applies.  */
14815       if (parser->scope)
14816         type = parser->scope;
14817       else if (object_type)
14818         type = object_type;
14819       else
14820         type = current_class_type;
14821       /* If that's not a class type, there is no destructor.  */
14822       if (!type || !CLASS_TYPE_P (type))
14823         return error_mark_node;
14824       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14825         lazily_declare_fn (sfk_destructor, type);
14826       if (!CLASSTYPE_DESTRUCTORS (type))
14827           return error_mark_node;
14828       /* If it was a class type, return the destructor.  */
14829       return CLASSTYPE_DESTRUCTORS (type);
14830     }
14831
14832   /* By this point, the NAME should be an ordinary identifier.  If
14833      the id-expression was a qualified name, the qualifying scope is
14834      stored in PARSER->SCOPE at this point.  */
14835   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14836
14837   /* Perform the lookup.  */
14838   if (parser->scope)
14839     {
14840       bool dependent_p;
14841
14842       if (parser->scope == error_mark_node)
14843         return error_mark_node;
14844
14845       /* If the SCOPE is dependent, the lookup must be deferred until
14846          the template is instantiated -- unless we are explicitly
14847          looking up names in uninstantiated templates.  Even then, we
14848          cannot look up the name if the scope is not a class type; it
14849          might, for example, be a template type parameter.  */
14850       dependent_p = (TYPE_P (parser->scope)
14851                      && !(parser->in_declarator_p
14852                           && currently_open_class (parser->scope))
14853                      && dependent_type_p (parser->scope));
14854       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14855            && dependent_p)
14856         {
14857           if (tag_type)
14858             {
14859               tree type;
14860
14861               /* The resolution to Core Issue 180 says that `struct
14862                  A::B' should be considered a type-name, even if `A'
14863                  is dependent.  */
14864               type = make_typename_type (parser->scope, name, tag_type,
14865                                          /*complain=*/tf_error);
14866               decl = TYPE_NAME (type);
14867             }
14868           else if (is_template
14869                    && (cp_parser_next_token_ends_template_argument_p (parser)
14870                        || cp_lexer_next_token_is (parser->lexer,
14871                                                   CPP_CLOSE_PAREN)))
14872             decl = make_unbound_class_template (parser->scope,
14873                                                 name, NULL_TREE,
14874                                                 /*complain=*/tf_error);
14875           else
14876             decl = build_qualified_name (/*type=*/NULL_TREE,
14877                                          parser->scope, name,
14878                                          is_template);
14879         }
14880       else
14881         {
14882           tree pushed_scope = NULL_TREE;
14883
14884           /* If PARSER->SCOPE is a dependent type, then it must be a
14885              class type, and we must not be checking dependencies;
14886              otherwise, we would have processed this lookup above.  So
14887              that PARSER->SCOPE is not considered a dependent base by
14888              lookup_member, we must enter the scope here.  */
14889           if (dependent_p)
14890             pushed_scope = push_scope (parser->scope);
14891           /* If the PARSER->SCOPE is a template specialization, it
14892              may be instantiated during name lookup.  In that case,
14893              errors may be issued.  Even if we rollback the current
14894              tentative parse, those errors are valid.  */
14895           decl = lookup_qualified_name (parser->scope, name,
14896                                         tag_type != none_type,
14897                                         /*complain=*/true);
14898           if (pushed_scope)
14899             pop_scope (pushed_scope);
14900         }
14901       parser->qualifying_scope = parser->scope;
14902       parser->object_scope = NULL_TREE;
14903     }
14904   else if (object_type)
14905     {
14906       tree object_decl = NULL_TREE;
14907       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14908          OBJECT_TYPE is not a class.  */
14909       if (CLASS_TYPE_P (object_type))
14910         /* If the OBJECT_TYPE is a template specialization, it may
14911            be instantiated during name lookup.  In that case, errors
14912            may be issued.  Even if we rollback the current tentative
14913            parse, those errors are valid.  */
14914         object_decl = lookup_member (object_type,
14915                                      name,
14916                                      /*protect=*/0,
14917                                      tag_type != none_type);
14918       /* Look it up in the enclosing context, too.  */
14919       decl = lookup_name_real (name, tag_type != none_type,
14920                                /*nonclass=*/0,
14921                                /*block_p=*/true, is_namespace, flags);
14922       parser->object_scope = object_type;
14923       parser->qualifying_scope = NULL_TREE;
14924       if (object_decl)
14925         decl = object_decl;
14926     }
14927   else
14928     {
14929       decl = lookup_name_real (name, tag_type != none_type,
14930                                /*nonclass=*/0,
14931                                /*block_p=*/true, is_namespace, flags);
14932       parser->qualifying_scope = NULL_TREE;
14933       parser->object_scope = NULL_TREE;
14934     }
14935
14936   /* If the lookup failed, let our caller know.  */
14937   if (!decl || decl == error_mark_node)
14938     return error_mark_node;
14939
14940   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14941   if (TREE_CODE (decl) == TREE_LIST)
14942     {
14943       if (ambiguous_decls)
14944         *ambiguous_decls = decl;
14945       /* The error message we have to print is too complicated for
14946          cp_parser_error, so we incorporate its actions directly.  */
14947       if (!cp_parser_simulate_error (parser))
14948         {
14949           error ("reference to %qD is ambiguous", name);
14950           print_candidates (decl);
14951         }
14952       return error_mark_node;
14953     }
14954
14955   gcc_assert (DECL_P (decl)
14956               || TREE_CODE (decl) == OVERLOAD
14957               || TREE_CODE (decl) == SCOPE_REF
14958               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14959               || BASELINK_P (decl));
14960
14961   /* If we have resolved the name of a member declaration, check to
14962      see if the declaration is accessible.  When the name resolves to
14963      set of overloaded functions, accessibility is checked when
14964      overload resolution is done.
14965
14966      During an explicit instantiation, access is not checked at all,
14967      as per [temp.explicit].  */
14968   if (DECL_P (decl))
14969     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14970
14971   return decl;
14972 }
14973
14974 /* Like cp_parser_lookup_name, but for use in the typical case where
14975    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14976    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14977
14978 static tree
14979 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14980 {
14981   return cp_parser_lookup_name (parser, name,
14982                                 none_type,
14983                                 /*is_template=*/false,
14984                                 /*is_namespace=*/false,
14985                                 /*check_dependency=*/true,
14986                                 /*ambiguous_decls=*/NULL);
14987 }
14988
14989 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14990    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14991    true, the DECL indicates the class being defined in a class-head,
14992    or declared in an elaborated-type-specifier.
14993
14994    Otherwise, return DECL.  */
14995
14996 static tree
14997 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14998 {
14999   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15000      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15001
15002        struct A {
15003          template <typename T> struct B;
15004        };
15005
15006        template <typename T> struct A::B {};
15007
15008      Similarly, in an elaborated-type-specifier:
15009
15010        namespace N { struct X{}; }
15011
15012        struct A {
15013          template <typename T> friend struct N::X;
15014        };
15015
15016      However, if the DECL refers to a class type, and we are in
15017      the scope of the class, then the name lookup automatically
15018      finds the TYPE_DECL created by build_self_reference rather
15019      than a TEMPLATE_DECL.  For example, in:
15020
15021        template <class T> struct S {
15022          S s;
15023        };
15024
15025      there is no need to handle such case.  */
15026
15027   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15028     return DECL_TEMPLATE_RESULT (decl);
15029
15030   return decl;
15031 }
15032
15033 /* If too many, or too few, template-parameter lists apply to the
15034    declarator, issue an error message.  Returns TRUE if all went well,
15035    and FALSE otherwise.  */
15036
15037 static bool
15038 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15039                                                 cp_declarator *declarator)
15040 {
15041   unsigned num_templates;
15042
15043   /* We haven't seen any classes that involve template parameters yet.  */
15044   num_templates = 0;
15045
15046   switch (declarator->kind)
15047     {
15048     case cdk_id:
15049       if (declarator->u.id.qualifying_scope)
15050         {
15051           tree scope;
15052           tree member;
15053
15054           scope = declarator->u.id.qualifying_scope;
15055           member = declarator->u.id.unqualified_name;
15056
15057           while (scope && CLASS_TYPE_P (scope))
15058             {
15059               /* You're supposed to have one `template <...>'
15060                  for every template class, but you don't need one
15061                  for a full specialization.  For example:
15062
15063                  template <class T> struct S{};
15064                  template <> struct S<int> { void f(); };
15065                  void S<int>::f () {}
15066
15067                  is correct; there shouldn't be a `template <>' for
15068                  the definition of `S<int>::f'.  */
15069               if (CLASSTYPE_TEMPLATE_INFO (scope)
15070                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15071                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15072                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15073                 ++num_templates;
15074
15075               scope = TYPE_CONTEXT (scope);
15076             }
15077         }
15078       else if (TREE_CODE (declarator->u.id.unqualified_name)
15079                == TEMPLATE_ID_EXPR)
15080         /* If the DECLARATOR has the form `X<y>' then it uses one
15081            additional level of template parameters.  */
15082         ++num_templates;
15083
15084       return cp_parser_check_template_parameters (parser,
15085                                                   num_templates);
15086
15087     case cdk_function:
15088     case cdk_array:
15089     case cdk_pointer:
15090     case cdk_reference:
15091     case cdk_ptrmem:
15092       return (cp_parser_check_declarator_template_parameters
15093               (parser, declarator->declarator));
15094
15095     case cdk_error:
15096       return true;
15097
15098     default:
15099       gcc_unreachable ();
15100     }
15101   return false;
15102 }
15103
15104 /* NUM_TEMPLATES were used in the current declaration.  If that is
15105    invalid, return FALSE and issue an error messages.  Otherwise,
15106    return TRUE.  */
15107
15108 static bool
15109 cp_parser_check_template_parameters (cp_parser* parser,
15110                                      unsigned num_templates)
15111 {
15112   /* If there are more template classes than parameter lists, we have
15113      something like:
15114
15115        template <class T> void S<T>::R<T>::f ();  */
15116   if (parser->num_template_parameter_lists < num_templates)
15117     {
15118       error ("too few template-parameter-lists");
15119       return false;
15120     }
15121   /* If there are the same number of template classes and parameter
15122      lists, that's OK.  */
15123   if (parser->num_template_parameter_lists == num_templates)
15124     return true;
15125   /* If there are more, but only one more, then we are referring to a
15126      member template.  That's OK too.  */
15127   if (parser->num_template_parameter_lists == num_templates + 1)
15128       return true;
15129   /* Otherwise, there are too many template parameter lists.  We have
15130      something like:
15131
15132      template <class T> template <class U> void S::f();  */
15133   error ("too many template-parameter-lists");
15134   return false;
15135 }
15136
15137 /* Parse an optional `::' token indicating that the following name is
15138    from the global namespace.  If so, PARSER->SCOPE is set to the
15139    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15140    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15141    Returns the new value of PARSER->SCOPE, if the `::' token is
15142    present, and NULL_TREE otherwise.  */
15143
15144 static tree
15145 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15146 {
15147   cp_token *token;
15148
15149   /* Peek at the next token.  */
15150   token = cp_lexer_peek_token (parser->lexer);
15151   /* If we're looking at a `::' token then we're starting from the
15152      global namespace, not our current location.  */
15153   if (token->type == CPP_SCOPE)
15154     {
15155       /* Consume the `::' token.  */
15156       cp_lexer_consume_token (parser->lexer);
15157       /* Set the SCOPE so that we know where to start the lookup.  */
15158       parser->scope = global_namespace;
15159       parser->qualifying_scope = global_namespace;
15160       parser->object_scope = NULL_TREE;
15161
15162       return parser->scope;
15163     }
15164   else if (!current_scope_valid_p)
15165     {
15166       parser->scope = NULL_TREE;
15167       parser->qualifying_scope = NULL_TREE;
15168       parser->object_scope = NULL_TREE;
15169     }
15170
15171   return NULL_TREE;
15172 }
15173
15174 /* Returns TRUE if the upcoming token sequence is the start of a
15175    constructor declarator.  If FRIEND_P is true, the declarator is
15176    preceded by the `friend' specifier.  */
15177
15178 static bool
15179 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15180 {
15181   bool constructor_p;
15182   tree type_decl = NULL_TREE;
15183   bool nested_name_p;
15184   cp_token *next_token;
15185
15186   /* The common case is that this is not a constructor declarator, so
15187      try to avoid doing lots of work if at all possible.  It's not
15188      valid declare a constructor at function scope.  */
15189   if (at_function_scope_p ())
15190     return false;
15191   /* And only certain tokens can begin a constructor declarator.  */
15192   next_token = cp_lexer_peek_token (parser->lexer);
15193   if (next_token->type != CPP_NAME
15194       && next_token->type != CPP_SCOPE
15195       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15196       && next_token->type != CPP_TEMPLATE_ID)
15197     return false;
15198
15199   /* Parse tentatively; we are going to roll back all of the tokens
15200      consumed here.  */
15201   cp_parser_parse_tentatively (parser);
15202   /* Assume that we are looking at a constructor declarator.  */
15203   constructor_p = true;
15204
15205   /* Look for the optional `::' operator.  */
15206   cp_parser_global_scope_opt (parser,
15207                               /*current_scope_valid_p=*/false);
15208   /* Look for the nested-name-specifier.  */
15209   nested_name_p
15210     = (cp_parser_nested_name_specifier_opt (parser,
15211                                             /*typename_keyword_p=*/false,
15212                                             /*check_dependency_p=*/false,
15213                                             /*type_p=*/false,
15214                                             /*is_declaration=*/false)
15215        != NULL_TREE);
15216   /* Outside of a class-specifier, there must be a
15217      nested-name-specifier.  */
15218   if (!nested_name_p &&
15219       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15220        || friend_p))
15221     constructor_p = false;
15222   /* If we still think that this might be a constructor-declarator,
15223      look for a class-name.  */
15224   if (constructor_p)
15225     {
15226       /* If we have:
15227
15228            template <typename T> struct S { S(); };
15229            template <typename T> S<T>::S ();
15230
15231          we must recognize that the nested `S' names a class.
15232          Similarly, for:
15233
15234            template <typename T> S<T>::S<T> ();
15235
15236          we must recognize that the nested `S' names a template.  */
15237       type_decl = cp_parser_class_name (parser,
15238                                         /*typename_keyword_p=*/false,
15239                                         /*template_keyword_p=*/false,
15240                                         none_type,
15241                                         /*check_dependency_p=*/false,
15242                                         /*class_head_p=*/false,
15243                                         /*is_declaration=*/false);
15244       /* If there was no class-name, then this is not a constructor.  */
15245       constructor_p = !cp_parser_error_occurred (parser);
15246     }
15247
15248   /* If we're still considering a constructor, we have to see a `(',
15249      to begin the parameter-declaration-clause, followed by either a
15250      `)', an `...', or a decl-specifier.  We need to check for a
15251      type-specifier to avoid being fooled into thinking that:
15252
15253        S::S (f) (int);
15254
15255      is a constructor.  (It is actually a function named `f' that
15256      takes one parameter (of type `int') and returns a value of type
15257      `S::S'.  */
15258   if (constructor_p
15259       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15260     {
15261       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15262           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15263           /* A parameter declaration begins with a decl-specifier,
15264              which is either the "attribute" keyword, a storage class
15265              specifier, or (usually) a type-specifier.  */
15266           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15267           && !cp_parser_storage_class_specifier_opt (parser))
15268         {
15269           tree type;
15270           tree pushed_scope = NULL_TREE;
15271           unsigned saved_num_template_parameter_lists;
15272
15273           /* Names appearing in the type-specifier should be looked up
15274              in the scope of the class.  */
15275           if (current_class_type)
15276             type = NULL_TREE;
15277           else
15278             {
15279               type = TREE_TYPE (type_decl);
15280               if (TREE_CODE (type) == TYPENAME_TYPE)
15281                 {
15282                   type = resolve_typename_type (type,
15283                                                 /*only_current_p=*/false);
15284                   if (type == error_mark_node)
15285                     {
15286                       cp_parser_abort_tentative_parse (parser);
15287                       return false;
15288                     }
15289                 }
15290               pushed_scope = push_scope (type);
15291             }
15292
15293           /* Inside the constructor parameter list, surrounding
15294              template-parameter-lists do not apply.  */
15295           saved_num_template_parameter_lists
15296             = parser->num_template_parameter_lists;
15297           parser->num_template_parameter_lists = 0;
15298
15299           /* Look for the type-specifier.  */
15300           cp_parser_type_specifier (parser,
15301                                     CP_PARSER_FLAGS_NONE,
15302                                     /*decl_specs=*/NULL,
15303                                     /*is_declarator=*/true,
15304                                     /*declares_class_or_enum=*/NULL,
15305                                     /*is_cv_qualifier=*/NULL);
15306
15307           parser->num_template_parameter_lists
15308             = saved_num_template_parameter_lists;
15309
15310           /* Leave the scope of the class.  */
15311           if (pushed_scope)
15312             pop_scope (pushed_scope);
15313
15314           constructor_p = !cp_parser_error_occurred (parser);
15315         }
15316     }
15317   else
15318     constructor_p = false;
15319   /* We did not really want to consume any tokens.  */
15320   cp_parser_abort_tentative_parse (parser);
15321
15322   return constructor_p;
15323 }
15324
15325 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15326    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15327    they must be performed once we are in the scope of the function.
15328
15329    Returns the function defined.  */
15330
15331 static tree
15332 cp_parser_function_definition_from_specifiers_and_declarator
15333   (cp_parser* parser,
15334    cp_decl_specifier_seq *decl_specifiers,
15335    tree attributes,
15336    const cp_declarator *declarator)
15337 {
15338   tree fn;
15339   bool success_p;
15340
15341   /* Begin the function-definition.  */
15342   success_p = start_function (decl_specifiers, declarator, attributes);
15343
15344   /* The things we're about to see are not directly qualified by any
15345      template headers we've seen thus far.  */
15346   reset_specialization ();
15347
15348   /* If there were names looked up in the decl-specifier-seq that we
15349      did not check, check them now.  We must wait until we are in the
15350      scope of the function to perform the checks, since the function
15351      might be a friend.  */
15352   perform_deferred_access_checks ();
15353
15354   if (!success_p)
15355     {
15356       /* Skip the entire function.  */
15357       cp_parser_skip_to_end_of_block_or_statement (parser);
15358       fn = error_mark_node;
15359     }
15360   else
15361     fn = cp_parser_function_definition_after_declarator (parser,
15362                                                          /*inline_p=*/false);
15363
15364   return fn;
15365 }
15366
15367 /* Parse the part of a function-definition that follows the
15368    declarator.  INLINE_P is TRUE iff this function is an inline
15369    function defined with a class-specifier.
15370
15371    Returns the function defined.  */
15372
15373 static tree
15374 cp_parser_function_definition_after_declarator (cp_parser* parser,
15375                                                 bool inline_p)
15376 {
15377   tree fn;
15378   bool ctor_initializer_p = false;
15379   bool saved_in_unbraced_linkage_specification_p;
15380   unsigned saved_num_template_parameter_lists;
15381
15382   /* If the next token is `return', then the code may be trying to
15383      make use of the "named return value" extension that G++ used to
15384      support.  */
15385   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15386     {
15387       /* Consume the `return' keyword.  */
15388       cp_lexer_consume_token (parser->lexer);
15389       /* Look for the identifier that indicates what value is to be
15390          returned.  */
15391       cp_parser_identifier (parser);
15392       /* Issue an error message.  */
15393       error ("named return values are no longer supported");
15394       /* Skip tokens until we reach the start of the function body.  */
15395       while (true)
15396         {
15397           cp_token *token = cp_lexer_peek_token (parser->lexer);
15398           if (token->type == CPP_OPEN_BRACE
15399               || token->type == CPP_EOF
15400               || token->type == CPP_PRAGMA_EOL)
15401             break;
15402           cp_lexer_consume_token (parser->lexer);
15403         }
15404     }
15405   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15406      anything declared inside `f'.  */
15407   saved_in_unbraced_linkage_specification_p
15408     = parser->in_unbraced_linkage_specification_p;
15409   parser->in_unbraced_linkage_specification_p = false;
15410   /* Inside the function, surrounding template-parameter-lists do not
15411      apply.  */
15412   saved_num_template_parameter_lists
15413     = parser->num_template_parameter_lists;
15414   parser->num_template_parameter_lists = 0;
15415   /* If the next token is `try', then we are looking at a
15416      function-try-block.  */
15417   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15418     ctor_initializer_p = cp_parser_function_try_block (parser);
15419   /* A function-try-block includes the function-body, so we only do
15420      this next part if we're not processing a function-try-block.  */
15421   else
15422     ctor_initializer_p
15423       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15424
15425   /* Finish the function.  */
15426   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15427                         (inline_p ? 2 : 0));
15428   /* Generate code for it, if necessary.  */
15429   expand_or_defer_fn (fn);
15430   /* Restore the saved values.  */
15431   parser->in_unbraced_linkage_specification_p
15432     = saved_in_unbraced_linkage_specification_p;
15433   parser->num_template_parameter_lists
15434     = saved_num_template_parameter_lists;
15435
15436   return fn;
15437 }
15438
15439 /* Parse a template-declaration, assuming that the `export' (and
15440    `extern') keywords, if present, has already been scanned.  MEMBER_P
15441    is as for cp_parser_template_declaration.  */
15442
15443 static void
15444 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15445 {
15446   tree decl = NULL_TREE;
15447   tree checks;
15448   tree parameter_list;
15449   bool friend_p = false;
15450   bool need_lang_pop;
15451
15452   /* Look for the `template' keyword.  */
15453   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15454     return;
15455
15456   /* And the `<'.  */
15457   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15458     return;
15459   /* [temp]
15460
15461      A template ... shall not have C linkage.  */
15462   if (current_lang_name == lang_name_c)
15463     {
15464       error ("template with C linkage");
15465       /* Give it C++ linkage to avoid confusing other parts of the
15466          front end.  */
15467       push_lang_context (lang_name_cplusplus);
15468       need_lang_pop = true;
15469     }
15470   else
15471     need_lang_pop = false;
15472
15473   /* We cannot perform access checks on the template parameter
15474      declarations until we know what is being declared, just as we
15475      cannot check the decl-specifier list.  */
15476   push_deferring_access_checks (dk_deferred);
15477
15478   /* If the next token is `>', then we have an invalid
15479      specialization.  Rather than complain about an invalid template
15480      parameter, issue an error message here.  */
15481   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15482     {
15483       cp_parser_error (parser, "invalid explicit specialization");
15484       begin_specialization ();
15485       parameter_list = NULL_TREE;
15486     }
15487   else
15488     /* Parse the template parameters.  */
15489     parameter_list = cp_parser_template_parameter_list (parser);
15490
15491   /* Get the deferred access checks from the parameter list.  These
15492      will be checked once we know what is being declared, as for a
15493      member template the checks must be performed in the scope of the
15494      class containing the member.  */
15495   checks = get_deferred_access_checks ();
15496
15497   /* Look for the `>'.  */
15498   cp_parser_skip_to_end_of_template_parameter_list (parser);
15499   /* We just processed one more parameter list.  */
15500   ++parser->num_template_parameter_lists;
15501   /* If the next token is `template', there are more template
15502      parameters.  */
15503   if (cp_lexer_next_token_is_keyword (parser->lexer,
15504                                       RID_TEMPLATE))
15505     cp_parser_template_declaration_after_export (parser, member_p);
15506   else
15507     {
15508       /* There are no access checks when parsing a template, as we do not
15509          know if a specialization will be a friend.  */
15510       push_deferring_access_checks (dk_no_check);
15511       decl = cp_parser_single_declaration (parser,
15512                                            checks,
15513                                            member_p,
15514                                            &friend_p);
15515       pop_deferring_access_checks ();
15516
15517       /* If this is a member template declaration, let the front
15518          end know.  */
15519       if (member_p && !friend_p && decl)
15520         {
15521           if (TREE_CODE (decl) == TYPE_DECL)
15522             cp_parser_check_access_in_redeclaration (decl);
15523
15524           decl = finish_member_template_decl (decl);
15525         }
15526       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15527         make_friend_class (current_class_type, TREE_TYPE (decl),
15528                            /*complain=*/true);
15529     }
15530   /* We are done with the current parameter list.  */
15531   --parser->num_template_parameter_lists;
15532
15533   pop_deferring_access_checks ();
15534
15535   /* Finish up.  */
15536   finish_template_decl (parameter_list);
15537
15538   /* Register member declarations.  */
15539   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15540     finish_member_declaration (decl);
15541   /* For the erroneous case of a template with C linkage, we pushed an
15542      implicit C++ linkage scope; exit that scope now.  */
15543   if (need_lang_pop)
15544     pop_lang_context ();
15545   /* If DECL is a function template, we must return to parse it later.
15546      (Even though there is no definition, there might be default
15547      arguments that need handling.)  */
15548   if (member_p && decl
15549       && (TREE_CODE (decl) == FUNCTION_DECL
15550           || DECL_FUNCTION_TEMPLATE_P (decl)))
15551     TREE_VALUE (parser->unparsed_functions_queues)
15552       = tree_cons (NULL_TREE, decl,
15553                    TREE_VALUE (parser->unparsed_functions_queues));
15554 }
15555
15556 /* Perform the deferred access checks from a template-parameter-list.
15557    CHECKS is a TREE_LIST of access checks, as returned by
15558    get_deferred_access_checks.  */
15559
15560 static void
15561 cp_parser_perform_template_parameter_access_checks (tree checks)
15562 {
15563   ++processing_template_parmlist;
15564   perform_access_checks (checks);
15565   --processing_template_parmlist;
15566 }
15567
15568 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15569    `function-definition' sequence.  MEMBER_P is true, this declaration
15570    appears in a class scope.
15571
15572    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15573    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15574
15575 static tree
15576 cp_parser_single_declaration (cp_parser* parser,
15577                               tree checks,
15578                               bool member_p,
15579                               bool* friend_p)
15580 {
15581   int declares_class_or_enum;
15582   tree decl = NULL_TREE;
15583   cp_decl_specifier_seq decl_specifiers;
15584   bool function_definition_p = false;
15585
15586   /* This function is only used when processing a template
15587      declaration.  */
15588   gcc_assert (innermost_scope_kind () == sk_template_parms
15589               || innermost_scope_kind () == sk_template_spec);
15590
15591   /* Defer access checks until we know what is being declared.  */
15592   push_deferring_access_checks (dk_deferred);
15593
15594   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15595      alternative.  */
15596   cp_parser_decl_specifier_seq (parser,
15597                                 CP_PARSER_FLAGS_OPTIONAL,
15598                                 &decl_specifiers,
15599                                 &declares_class_or_enum);
15600   if (friend_p)
15601     *friend_p = cp_parser_friend_p (&decl_specifiers);
15602
15603   /* There are no template typedefs.  */
15604   if (decl_specifiers.specs[(int) ds_typedef])
15605     {
15606       error ("template declaration of %qs", "typedef");
15607       decl = error_mark_node;
15608     }
15609
15610   /* Gather up the access checks that occurred the
15611      decl-specifier-seq.  */
15612   stop_deferring_access_checks ();
15613
15614   /* Check for the declaration of a template class.  */
15615   if (declares_class_or_enum)
15616     {
15617       if (cp_parser_declares_only_class_p (parser))
15618         {
15619           decl = shadow_tag (&decl_specifiers);
15620
15621           /* In this case:
15622
15623                struct C {
15624                  friend template <typename T> struct A<T>::B;
15625                };
15626
15627              A<T>::B will be represented by a TYPENAME_TYPE, and
15628              therefore not recognized by shadow_tag.  */
15629           if (friend_p && *friend_p
15630               && !decl
15631               && decl_specifiers.type
15632               && TYPE_P (decl_specifiers.type))
15633             decl = decl_specifiers.type;
15634
15635           if (decl && decl != error_mark_node)
15636             decl = TYPE_NAME (decl);
15637           else
15638             decl = error_mark_node;
15639
15640           /* Perform access checks for template parameters.  */
15641           cp_parser_perform_template_parameter_access_checks (checks);
15642         }
15643     }
15644   /* If it's not a template class, try for a template function.  If
15645      the next token is a `;', then this declaration does not declare
15646      anything.  But, if there were errors in the decl-specifiers, then
15647      the error might well have come from an attempted class-specifier.
15648      In that case, there's no need to warn about a missing declarator.  */
15649   if (!decl
15650       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15651           || decl_specifiers.type != error_mark_node))
15652     decl = cp_parser_init_declarator (parser,
15653                                       &decl_specifiers,
15654                                       checks,
15655                                       /*function_definition_allowed_p=*/true,
15656                                       member_p,
15657                                       declares_class_or_enum,
15658                                       &function_definition_p);
15659
15660   pop_deferring_access_checks ();
15661
15662   /* Clear any current qualification; whatever comes next is the start
15663      of something new.  */
15664   parser->scope = NULL_TREE;
15665   parser->qualifying_scope = NULL_TREE;
15666   parser->object_scope = NULL_TREE;
15667   /* Look for a trailing `;' after the declaration.  */
15668   if (!function_definition_p
15669       && (decl == error_mark_node
15670           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15671     cp_parser_skip_to_end_of_block_or_statement (parser);
15672
15673   return decl;
15674 }
15675
15676 /* Parse a cast-expression that is not the operand of a unary "&".  */
15677
15678 static tree
15679 cp_parser_simple_cast_expression (cp_parser *parser)
15680 {
15681   return cp_parser_cast_expression (parser, /*address_p=*/false,
15682                                     /*cast_p=*/false);
15683 }
15684
15685 /* Parse a functional cast to TYPE.  Returns an expression
15686    representing the cast.  */
15687
15688 static tree
15689 cp_parser_functional_cast (cp_parser* parser, tree type)
15690 {
15691   tree expression_list;
15692   tree cast;
15693
15694   expression_list
15695     = cp_parser_parenthesized_expression_list (parser, false,
15696                                                /*cast_p=*/true,
15697                                                /*non_constant_p=*/NULL);
15698
15699   cast = build_functional_cast (type, expression_list);
15700   /* [expr.const]/1: In an integral constant expression "only type
15701      conversions to integral or enumeration type can be used".  */
15702   if (TREE_CODE (type) == TYPE_DECL)
15703     type = TREE_TYPE (type);
15704   if (cast != error_mark_node
15705       && !cast_valid_in_integral_constant_expression_p (type)
15706       && (cp_parser_non_integral_constant_expression
15707           (parser, "a call to a constructor")))
15708     return error_mark_node;
15709   return cast;
15710 }
15711
15712 /* Save the tokens that make up the body of a member function defined
15713    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15714    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15715    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15716    for the member function.  */
15717
15718 static tree
15719 cp_parser_save_member_function_body (cp_parser* parser,
15720                                      cp_decl_specifier_seq *decl_specifiers,
15721                                      cp_declarator *declarator,
15722                                      tree attributes)
15723 {
15724   cp_token *first;
15725   cp_token *last;
15726   tree fn;
15727
15728   /* Create the function-declaration.  */
15729   fn = start_method (decl_specifiers, declarator, attributes);
15730   /* If something went badly wrong, bail out now.  */
15731   if (fn == error_mark_node)
15732     {
15733       /* If there's a function-body, skip it.  */
15734       if (cp_parser_token_starts_function_definition_p
15735           (cp_lexer_peek_token (parser->lexer)))
15736         cp_parser_skip_to_end_of_block_or_statement (parser);
15737       return error_mark_node;
15738     }
15739
15740   /* Remember it, if there default args to post process.  */
15741   cp_parser_save_default_args (parser, fn);
15742
15743   /* Save away the tokens that make up the body of the
15744      function.  */
15745   first = parser->lexer->next_token;
15746   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15747   /* Handle function try blocks.  */
15748   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15749     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15750   last = parser->lexer->next_token;
15751
15752   /* Save away the inline definition; we will process it when the
15753      class is complete.  */
15754   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15755   DECL_PENDING_INLINE_P (fn) = 1;
15756
15757   /* We need to know that this was defined in the class, so that
15758      friend templates are handled correctly.  */
15759   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15760
15761   /* We're done with the inline definition.  */
15762   finish_method (fn);
15763
15764   /* Add FN to the queue of functions to be parsed later.  */
15765   TREE_VALUE (parser->unparsed_functions_queues)
15766     = tree_cons (NULL_TREE, fn,
15767                  TREE_VALUE (parser->unparsed_functions_queues));
15768
15769   return fn;
15770 }
15771
15772 /* Parse a template-argument-list, as well as the trailing ">" (but
15773    not the opening ">").  See cp_parser_template_argument_list for the
15774    return value.  */
15775
15776 static tree
15777 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15778 {
15779   tree arguments;
15780   tree saved_scope;
15781   tree saved_qualifying_scope;
15782   tree saved_object_scope;
15783   bool saved_greater_than_is_operator_p;
15784   bool saved_skip_evaluation;
15785
15786   /* [temp.names]
15787
15788      When parsing a template-id, the first non-nested `>' is taken as
15789      the end of the template-argument-list rather than a greater-than
15790      operator.  */
15791   saved_greater_than_is_operator_p
15792     = parser->greater_than_is_operator_p;
15793   parser->greater_than_is_operator_p = false;
15794   /* Parsing the argument list may modify SCOPE, so we save it
15795      here.  */
15796   saved_scope = parser->scope;
15797   saved_qualifying_scope = parser->qualifying_scope;
15798   saved_object_scope = parser->object_scope;
15799   /* We need to evaluate the template arguments, even though this
15800      template-id may be nested within a "sizeof".  */
15801   saved_skip_evaluation = skip_evaluation;
15802   skip_evaluation = false;
15803   /* Parse the template-argument-list itself.  */
15804   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15805     arguments = NULL_TREE;
15806   else
15807     arguments = cp_parser_template_argument_list (parser);
15808   /* Look for the `>' that ends the template-argument-list. If we find
15809      a '>>' instead, it's probably just a typo.  */
15810   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15811     {
15812       if (!saved_greater_than_is_operator_p)
15813         {
15814           /* If we're in a nested template argument list, the '>>' has
15815             to be a typo for '> >'. We emit the error message, but we
15816             continue parsing and we push a '>' as next token, so that
15817             the argument list will be parsed correctly.  Note that the
15818             global source location is still on the token before the
15819             '>>', so we need to say explicitly where we want it.  */
15820           cp_token *token = cp_lexer_peek_token (parser->lexer);
15821           error ("%H%<>>%> should be %<> >%> "
15822                  "within a nested template argument list",
15823                  &token->location);
15824
15825           /* ??? Proper recovery should terminate two levels of
15826              template argument list here.  */
15827           token->type = CPP_GREATER;
15828         }
15829       else
15830         {
15831           /* If this is not a nested template argument list, the '>>'
15832             is a typo for '>'. Emit an error message and continue.
15833             Same deal about the token location, but here we can get it
15834             right by consuming the '>>' before issuing the diagnostic.  */
15835           cp_lexer_consume_token (parser->lexer);
15836           error ("spurious %<>>%>, use %<>%> to terminate "
15837                  "a template argument list");
15838         }
15839     }
15840   else
15841     cp_parser_skip_to_end_of_template_parameter_list (parser);
15842   /* The `>' token might be a greater-than operator again now.  */
15843   parser->greater_than_is_operator_p
15844     = saved_greater_than_is_operator_p;
15845   /* Restore the SAVED_SCOPE.  */
15846   parser->scope = saved_scope;
15847   parser->qualifying_scope = saved_qualifying_scope;
15848   parser->object_scope = saved_object_scope;
15849   skip_evaluation = saved_skip_evaluation;
15850
15851   return arguments;
15852 }
15853
15854 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15855    arguments, or the body of the function have not yet been parsed,
15856    parse them now.  */
15857
15858 static void
15859 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15860 {
15861   /* If this member is a template, get the underlying
15862      FUNCTION_DECL.  */
15863   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15864     member_function = DECL_TEMPLATE_RESULT (member_function);
15865
15866   /* There should not be any class definitions in progress at this
15867      point; the bodies of members are only parsed outside of all class
15868      definitions.  */
15869   gcc_assert (parser->num_classes_being_defined == 0);
15870   /* While we're parsing the member functions we might encounter more
15871      classes.  We want to handle them right away, but we don't want
15872      them getting mixed up with functions that are currently in the
15873      queue.  */
15874   parser->unparsed_functions_queues
15875     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15876
15877   /* Make sure that any template parameters are in scope.  */
15878   maybe_begin_member_template_processing (member_function);
15879
15880   /* If the body of the function has not yet been parsed, parse it
15881      now.  */
15882   if (DECL_PENDING_INLINE_P (member_function))
15883     {
15884       tree function_scope;
15885       cp_token_cache *tokens;
15886
15887       /* The function is no longer pending; we are processing it.  */
15888       tokens = DECL_PENDING_INLINE_INFO (member_function);
15889       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15890       DECL_PENDING_INLINE_P (member_function) = 0;
15891
15892       /* If this is a local class, enter the scope of the containing
15893          function.  */
15894       function_scope = current_function_decl;
15895       if (function_scope)
15896         push_function_context_to (function_scope);
15897
15898
15899       /* Push the body of the function onto the lexer stack.  */
15900       cp_parser_push_lexer_for_tokens (parser, tokens);
15901
15902       /* Let the front end know that we going to be defining this
15903          function.  */
15904       start_preparsed_function (member_function, NULL_TREE,
15905                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15906
15907       /* Don't do access checking if it is a templated function.  */
15908       if (processing_template_decl)
15909         push_deferring_access_checks (dk_no_check);
15910
15911       /* Now, parse the body of the function.  */
15912       cp_parser_function_definition_after_declarator (parser,
15913                                                       /*inline_p=*/true);
15914
15915       if (processing_template_decl)
15916         pop_deferring_access_checks ();
15917
15918       /* Leave the scope of the containing function.  */
15919       if (function_scope)
15920         pop_function_context_from (function_scope);
15921       cp_parser_pop_lexer (parser);
15922     }
15923
15924   /* Remove any template parameters from the symbol table.  */
15925   maybe_end_member_template_processing ();
15926
15927   /* Restore the queue.  */
15928   parser->unparsed_functions_queues
15929     = TREE_CHAIN (parser->unparsed_functions_queues);
15930 }
15931
15932 /* If DECL contains any default args, remember it on the unparsed
15933    functions queue.  */
15934
15935 static void
15936 cp_parser_save_default_args (cp_parser* parser, tree decl)
15937 {
15938   tree probe;
15939
15940   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15941        probe;
15942        probe = TREE_CHAIN (probe))
15943     if (TREE_PURPOSE (probe))
15944       {
15945         TREE_PURPOSE (parser->unparsed_functions_queues)
15946           = tree_cons (current_class_type, decl,
15947                        TREE_PURPOSE (parser->unparsed_functions_queues));
15948         break;
15949       }
15950 }
15951
15952 /* FN is a FUNCTION_DECL which may contains a parameter with an
15953    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15954    assumes that the current scope is the scope in which the default
15955    argument should be processed.  */
15956
15957 static void
15958 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15959 {
15960   bool saved_local_variables_forbidden_p;
15961   tree parm;
15962
15963   /* While we're parsing the default args, we might (due to the
15964      statement expression extension) encounter more classes.  We want
15965      to handle them right away, but we don't want them getting mixed
15966      up with default args that are currently in the queue.  */
15967   parser->unparsed_functions_queues
15968     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15969
15970   /* Local variable names (and the `this' keyword) may not appear
15971      in a default argument.  */
15972   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15973   parser->local_variables_forbidden_p = true;
15974
15975   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15976        parm;
15977        parm = TREE_CHAIN (parm))
15978     {
15979       cp_token_cache *tokens;
15980       tree default_arg = TREE_PURPOSE (parm);
15981       tree parsed_arg;
15982       VEC(tree,gc) *insts;
15983       tree copy;
15984       unsigned ix;
15985
15986       if (!default_arg)
15987         continue;
15988
15989       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15990         /* This can happen for a friend declaration for a function
15991            already declared with default arguments.  */
15992         continue;
15993
15994        /* Push the saved tokens for the default argument onto the parser's
15995           lexer stack.  */
15996       tokens = DEFARG_TOKENS (default_arg);
15997       cp_parser_push_lexer_for_tokens (parser, tokens);
15998
15999       /* Parse the assignment-expression.  */
16000       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16001
16002       if (!processing_template_decl)
16003         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16004
16005       TREE_PURPOSE (parm) = parsed_arg;
16006
16007       /* Update any instantiations we've already created.  */
16008       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16009            VEC_iterate (tree, insts, ix, copy); ix++)
16010         TREE_PURPOSE (copy) = parsed_arg;
16011
16012       /* If the token stream has not been completely used up, then
16013          there was extra junk after the end of the default
16014          argument.  */
16015       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16016         cp_parser_error (parser, "expected %<,%>");
16017
16018       /* Revert to the main lexer.  */
16019       cp_parser_pop_lexer (parser);
16020     }
16021
16022   /* Make sure no default arg is missing.  */
16023   check_default_args (fn);
16024
16025   /* Restore the state of local_variables_forbidden_p.  */
16026   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16027
16028   /* Restore the queue.  */
16029   parser->unparsed_functions_queues
16030     = TREE_CHAIN (parser->unparsed_functions_queues);
16031 }
16032
16033 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16034    either a TYPE or an expression, depending on the form of the
16035    input.  The KEYWORD indicates which kind of expression we have
16036    encountered.  */
16037
16038 static tree
16039 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16040 {
16041   static const char *format;
16042   tree expr = NULL_TREE;
16043   const char *saved_message;
16044   bool saved_integral_constant_expression_p;
16045   bool saved_non_integral_constant_expression_p;
16046
16047   /* Initialize FORMAT the first time we get here.  */
16048   if (!format)
16049     format = "types may not be defined in '%s' expressions";
16050
16051   /* Types cannot be defined in a `sizeof' expression.  Save away the
16052      old message.  */
16053   saved_message = parser->type_definition_forbidden_message;
16054   /* And create the new one.  */
16055   parser->type_definition_forbidden_message
16056     = XNEWVEC (const char, strlen (format)
16057                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16058                + 1 /* `\0' */);
16059   sprintf ((char *) parser->type_definition_forbidden_message,
16060            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16061
16062   /* The restrictions on constant-expressions do not apply inside
16063      sizeof expressions.  */
16064   saved_integral_constant_expression_p
16065     = parser->integral_constant_expression_p;
16066   saved_non_integral_constant_expression_p
16067     = parser->non_integral_constant_expression_p;
16068   parser->integral_constant_expression_p = false;
16069
16070   /* Do not actually evaluate the expression.  */
16071   ++skip_evaluation;
16072   /* If it's a `(', then we might be looking at the type-id
16073      construction.  */
16074   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16075     {
16076       tree type;
16077       bool saved_in_type_id_in_expr_p;
16078
16079       /* We can't be sure yet whether we're looking at a type-id or an
16080          expression.  */
16081       cp_parser_parse_tentatively (parser);
16082       /* Consume the `('.  */
16083       cp_lexer_consume_token (parser->lexer);
16084       /* Parse the type-id.  */
16085       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16086       parser->in_type_id_in_expr_p = true;
16087       type = cp_parser_type_id (parser);
16088       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16089       /* Now, look for the trailing `)'.  */
16090       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16091       /* If all went well, then we're done.  */
16092       if (cp_parser_parse_definitely (parser))
16093         {
16094           cp_decl_specifier_seq decl_specs;
16095
16096           /* Build a trivial decl-specifier-seq.  */
16097           clear_decl_specs (&decl_specs);
16098           decl_specs.type = type;
16099
16100           /* Call grokdeclarator to figure out what type this is.  */
16101           expr = grokdeclarator (NULL,
16102                                  &decl_specs,
16103                                  TYPENAME,
16104                                  /*initialized=*/0,
16105                                  /*attrlist=*/NULL);
16106         }
16107     }
16108
16109   /* If the type-id production did not work out, then we must be
16110      looking at the unary-expression production.  */
16111   if (!expr)
16112     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16113                                        /*cast_p=*/false);
16114   /* Go back to evaluating expressions.  */
16115   --skip_evaluation;
16116
16117   /* Free the message we created.  */
16118   free ((char *) parser->type_definition_forbidden_message);
16119   /* And restore the old one.  */
16120   parser->type_definition_forbidden_message = saved_message;
16121   parser->integral_constant_expression_p
16122     = saved_integral_constant_expression_p;
16123   parser->non_integral_constant_expression_p
16124     = saved_non_integral_constant_expression_p;
16125
16126   return expr;
16127 }
16128
16129 /* If the current declaration has no declarator, return true.  */
16130
16131 static bool
16132 cp_parser_declares_only_class_p (cp_parser *parser)
16133 {
16134   /* If the next token is a `;' or a `,' then there is no
16135      declarator.  */
16136   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16137           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16138 }
16139
16140 /* Update the DECL_SPECS to reflect the storage class indicated by
16141    KEYWORD.  */
16142
16143 static void
16144 cp_parser_set_storage_class (cp_parser *parser,
16145                              cp_decl_specifier_seq *decl_specs,
16146                              enum rid keyword)
16147 {
16148   cp_storage_class storage_class;
16149
16150   if (parser->in_unbraced_linkage_specification_p)
16151     {
16152       error ("invalid use of %qD in linkage specification",
16153              ridpointers[keyword]);
16154       return;
16155     }
16156   else if (decl_specs->storage_class != sc_none)
16157     {
16158       decl_specs->multiple_storage_classes_p = true;
16159       return;
16160     }
16161
16162   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16163       && decl_specs->specs[(int) ds_thread])
16164     {
16165       error ("%<__thread%> before %qD", ridpointers[keyword]);
16166       decl_specs->specs[(int) ds_thread] = 0;
16167     }
16168
16169   switch (keyword)
16170     {
16171     case RID_AUTO:
16172       storage_class = sc_auto;
16173       break;
16174     case RID_REGISTER:
16175       storage_class = sc_register;
16176       break;
16177     case RID_STATIC:
16178       storage_class = sc_static;
16179       break;
16180     case RID_EXTERN:
16181       storage_class = sc_extern;
16182       break;
16183     case RID_MUTABLE:
16184       storage_class = sc_mutable;
16185       break;
16186     default:
16187       gcc_unreachable ();
16188     }
16189   decl_specs->storage_class = storage_class;
16190 }
16191
16192 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16193    is true, the type is a user-defined type; otherwise it is a
16194    built-in type specified by a keyword.  */
16195
16196 static void
16197 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16198                               tree type_spec,
16199                               bool user_defined_p)
16200 {
16201   decl_specs->any_specifiers_p = true;
16202
16203   /* If the user tries to redeclare bool or wchar_t (with, for
16204      example, in "typedef int wchar_t;") we remember that this is what
16205      happened.  In system headers, we ignore these declarations so
16206      that G++ can work with system headers that are not C++-safe.  */
16207   if (decl_specs->specs[(int) ds_typedef]
16208       && !user_defined_p
16209       && (type_spec == boolean_type_node
16210           || type_spec == wchar_type_node)
16211       && (decl_specs->type
16212           || decl_specs->specs[(int) ds_long]
16213           || decl_specs->specs[(int) ds_short]
16214           || decl_specs->specs[(int) ds_unsigned]
16215           || decl_specs->specs[(int) ds_signed]))
16216     {
16217       decl_specs->redefined_builtin_type = type_spec;
16218       if (!decl_specs->type)
16219         {
16220           decl_specs->type = type_spec;
16221           decl_specs->user_defined_type_p = false;
16222         }
16223     }
16224   else if (decl_specs->type)
16225     decl_specs->multiple_types_p = true;
16226   else
16227     {
16228       decl_specs->type = type_spec;
16229       decl_specs->user_defined_type_p = user_defined_p;
16230       decl_specs->redefined_builtin_type = NULL_TREE;
16231     }
16232 }
16233
16234 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16235    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16236
16237 static bool
16238 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16239 {
16240   return decl_specifiers->specs[(int) ds_friend] != 0;
16241 }
16242
16243 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16244    issue an error message indicating that TOKEN_DESC was expected.
16245
16246    Returns the token consumed, if the token had the appropriate type.
16247    Otherwise, returns NULL.  */
16248
16249 static cp_token *
16250 cp_parser_require (cp_parser* parser,
16251                    enum cpp_ttype type,
16252                    const char* token_desc)
16253 {
16254   if (cp_lexer_next_token_is (parser->lexer, type))
16255     return cp_lexer_consume_token (parser->lexer);
16256   else
16257     {
16258       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16259       if (!cp_parser_simulate_error (parser))
16260         {
16261           char *message = concat ("expected ", token_desc, NULL);
16262           cp_parser_error (parser, message);
16263           free (message);
16264         }
16265       return NULL;
16266     }
16267 }
16268
16269 /* An error message is produced if the next token is not '>'.
16270    All further tokens are skipped until the desired token is
16271    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
16272
16273 static void
16274 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16275 {
16276   /* Current level of '< ... >'.  */
16277   unsigned level = 0;
16278   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
16279   unsigned nesting_depth = 0;
16280
16281   /* Are we ready, yet?  If not, issue error message.  */
16282   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16283     return;
16284
16285   /* Skip tokens until the desired token is found.  */
16286   while (true)
16287     {
16288       /* Peek at the next token.  */
16289       switch (cp_lexer_peek_token (parser->lexer)->type)
16290         {
16291         case CPP_LESS:
16292           if (!nesting_depth)
16293             ++level;
16294           break;
16295
16296         case CPP_GREATER:
16297           if (!nesting_depth && level-- == 0)
16298             {
16299               /* We've reached the token we want, consume it and stop.  */
16300               cp_lexer_consume_token (parser->lexer);
16301               return;
16302             }
16303           break;
16304
16305         case CPP_OPEN_PAREN:
16306         case CPP_OPEN_SQUARE:
16307           ++nesting_depth;
16308           break;
16309
16310         case CPP_CLOSE_PAREN:
16311         case CPP_CLOSE_SQUARE:
16312           if (nesting_depth-- == 0)
16313             return;
16314           break;
16315
16316         case CPP_EOF:
16317         case CPP_PRAGMA_EOL:
16318         case CPP_SEMICOLON:
16319         case CPP_OPEN_BRACE:
16320         case CPP_CLOSE_BRACE:
16321           /* The '>' was probably forgotten, don't look further.  */
16322           return;
16323
16324         default:
16325           break;
16326         }
16327
16328       /* Consume this token.  */
16329       cp_lexer_consume_token (parser->lexer);
16330     }
16331 }
16332
16333 /* If the next token is the indicated keyword, consume it.  Otherwise,
16334    issue an error message indicating that TOKEN_DESC was expected.
16335
16336    Returns the token consumed, if the token had the appropriate type.
16337    Otherwise, returns NULL.  */
16338
16339 static cp_token *
16340 cp_parser_require_keyword (cp_parser* parser,
16341                            enum rid keyword,
16342                            const char* token_desc)
16343 {
16344   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16345
16346   if (token && token->keyword != keyword)
16347     {
16348       dyn_string_t error_msg;
16349
16350       /* Format the error message.  */
16351       error_msg = dyn_string_new (0);
16352       dyn_string_append_cstr (error_msg, "expected ");
16353       dyn_string_append_cstr (error_msg, token_desc);
16354       cp_parser_error (parser, error_msg->s);
16355       dyn_string_delete (error_msg);
16356       return NULL;
16357     }
16358
16359   return token;
16360 }
16361
16362 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16363    function-definition.  */
16364
16365 static bool
16366 cp_parser_token_starts_function_definition_p (cp_token* token)
16367 {
16368   return (/* An ordinary function-body begins with an `{'.  */
16369           token->type == CPP_OPEN_BRACE
16370           /* A ctor-initializer begins with a `:'.  */
16371           || token->type == CPP_COLON
16372           /* A function-try-block begins with `try'.  */
16373           || token->keyword == RID_TRY
16374           /* The named return value extension begins with `return'.  */
16375           || token->keyword == RID_RETURN);
16376 }
16377
16378 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16379    definition.  */
16380
16381 static bool
16382 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16383 {
16384   cp_token *token;
16385
16386   token = cp_lexer_peek_token (parser->lexer);
16387   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16388 }
16389
16390 /* Returns TRUE iff the next token is the "," or ">" ending a
16391    template-argument.  */
16392
16393 static bool
16394 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16395 {
16396   cp_token *token;
16397
16398   token = cp_lexer_peek_token (parser->lexer);
16399   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16400 }
16401
16402 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16403    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16404
16405 static bool
16406 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16407                                                      size_t n)
16408 {
16409   cp_token *token;
16410
16411   token = cp_lexer_peek_nth_token (parser->lexer, n);
16412   if (token->type == CPP_LESS)
16413     return true;
16414   /* Check for the sequence `<::' in the original code. It would be lexed as
16415      `[:', where `[' is a digraph, and there is no whitespace before
16416      `:'.  */
16417   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16418     {
16419       cp_token *token2;
16420       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16421       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16422         return true;
16423     }
16424   return false;
16425 }
16426
16427 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16428    or none_type otherwise.  */
16429
16430 static enum tag_types
16431 cp_parser_token_is_class_key (cp_token* token)
16432 {
16433   switch (token->keyword)
16434     {
16435     case RID_CLASS:
16436       return class_type;
16437     case RID_STRUCT:
16438       return record_type;
16439     case RID_UNION:
16440       return union_type;
16441
16442     default:
16443       return none_type;
16444     }
16445 }
16446
16447 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16448
16449 static void
16450 cp_parser_check_class_key (enum tag_types class_key, tree type)
16451 {
16452   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16453     pedwarn ("%qs tag used in naming %q#T",
16454             class_key == union_type ? "union"
16455              : class_key == record_type ? "struct" : "class",
16456              type);
16457 }
16458
16459 /* Issue an error message if DECL is redeclared with different
16460    access than its original declaration [class.access.spec/3].
16461    This applies to nested classes and nested class templates.
16462    [class.mem/1].  */
16463
16464 static void
16465 cp_parser_check_access_in_redeclaration (tree decl)
16466 {
16467   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16468     return;
16469
16470   if ((TREE_PRIVATE (decl)
16471        != (current_access_specifier == access_private_node))
16472       || (TREE_PROTECTED (decl)
16473           != (current_access_specifier == access_protected_node)))
16474     error ("%qD redeclared with different access", decl);
16475 }
16476
16477 /* Look for the `template' keyword, as a syntactic disambiguator.
16478    Return TRUE iff it is present, in which case it will be
16479    consumed.  */
16480
16481 static bool
16482 cp_parser_optional_template_keyword (cp_parser *parser)
16483 {
16484   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16485     {
16486       /* The `template' keyword can only be used within templates;
16487          outside templates the parser can always figure out what is a
16488          template and what is not.  */
16489       if (!processing_template_decl)
16490         {
16491           error ("%<template%> (as a disambiguator) is only allowed "
16492                  "within templates");
16493           /* If this part of the token stream is rescanned, the same
16494              error message would be generated.  So, we purge the token
16495              from the stream.  */
16496           cp_lexer_purge_token (parser->lexer);
16497           return false;
16498         }
16499       else
16500         {
16501           /* Consume the `template' keyword.  */
16502           cp_lexer_consume_token (parser->lexer);
16503           return true;
16504         }
16505     }
16506
16507   return false;
16508 }
16509
16510 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16511    set PARSER->SCOPE, and perform other related actions.  */
16512
16513 static void
16514 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16515 {
16516   tree value;
16517   tree check;
16518
16519   /* Get the stored value.  */
16520   value = cp_lexer_consume_token (parser->lexer)->value;
16521   /* Perform any access checks that were deferred.  */
16522   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16523     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16524   /* Set the scope from the stored value.  */
16525   parser->scope = TREE_VALUE (value);
16526   parser->qualifying_scope = TREE_TYPE (value);
16527   parser->object_scope = NULL_TREE;
16528 }
16529
16530 /* Consume tokens up through a non-nested END token.  */
16531
16532 static void
16533 cp_parser_cache_group (cp_parser *parser,
16534                        enum cpp_ttype end,
16535                        unsigned depth)
16536 {
16537   while (true)
16538     {
16539       cp_token *token;
16540
16541       /* Abort a parenthesized expression if we encounter a brace.  */
16542       if ((end == CPP_CLOSE_PAREN || depth == 0)
16543           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16544         return;
16545       /* If we've reached the end of the file, stop.  */
16546       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16547           || (end != CPP_PRAGMA_EOL
16548               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16549         return;
16550       /* Consume the next token.  */
16551       token = cp_lexer_consume_token (parser->lexer);
16552       /* See if it starts a new group.  */
16553       if (token->type == CPP_OPEN_BRACE)
16554         {
16555           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16556           if (depth == 0)
16557             return;
16558         }
16559       else if (token->type == CPP_OPEN_PAREN)
16560         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16561       else if (token->type == CPP_PRAGMA)
16562         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16563       else if (token->type == end)
16564         return;
16565     }
16566 }
16567
16568 /* Begin parsing tentatively.  We always save tokens while parsing
16569    tentatively so that if the tentative parsing fails we can restore the
16570    tokens.  */
16571
16572 static void
16573 cp_parser_parse_tentatively (cp_parser* parser)
16574 {
16575   /* Enter a new parsing context.  */
16576   parser->context = cp_parser_context_new (parser->context);
16577   /* Begin saving tokens.  */
16578   cp_lexer_save_tokens (parser->lexer);
16579   /* In order to avoid repetitive access control error messages,
16580      access checks are queued up until we are no longer parsing
16581      tentatively.  */
16582   push_deferring_access_checks (dk_deferred);
16583 }
16584
16585 /* Commit to the currently active tentative parse.  */
16586
16587 static void
16588 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16589 {
16590   cp_parser_context *context;
16591   cp_lexer *lexer;
16592
16593   /* Mark all of the levels as committed.  */
16594   lexer = parser->lexer;
16595   for (context = parser->context; context->next; context = context->next)
16596     {
16597       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16598         break;
16599       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16600       while (!cp_lexer_saving_tokens (lexer))
16601         lexer = lexer->next;
16602       cp_lexer_commit_tokens (lexer);
16603     }
16604 }
16605
16606 /* Abort the currently active tentative parse.  All consumed tokens
16607    will be rolled back, and no diagnostics will be issued.  */
16608
16609 static void
16610 cp_parser_abort_tentative_parse (cp_parser* parser)
16611 {
16612   cp_parser_simulate_error (parser);
16613   /* Now, pretend that we want to see if the construct was
16614      successfully parsed.  */
16615   cp_parser_parse_definitely (parser);
16616 }
16617
16618 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16619    token stream.  Otherwise, commit to the tokens we have consumed.
16620    Returns true if no error occurred; false otherwise.  */
16621
16622 static bool
16623 cp_parser_parse_definitely (cp_parser* parser)
16624 {
16625   bool error_occurred;
16626   cp_parser_context *context;
16627
16628   /* Remember whether or not an error occurred, since we are about to
16629      destroy that information.  */
16630   error_occurred = cp_parser_error_occurred (parser);
16631   /* Remove the topmost context from the stack.  */
16632   context = parser->context;
16633   parser->context = context->next;
16634   /* If no parse errors occurred, commit to the tentative parse.  */
16635   if (!error_occurred)
16636     {
16637       /* Commit to the tokens read tentatively, unless that was
16638          already done.  */
16639       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16640         cp_lexer_commit_tokens (parser->lexer);
16641
16642       pop_to_parent_deferring_access_checks ();
16643     }
16644   /* Otherwise, if errors occurred, roll back our state so that things
16645      are just as they were before we began the tentative parse.  */
16646   else
16647     {
16648       cp_lexer_rollback_tokens (parser->lexer);
16649       pop_deferring_access_checks ();
16650     }
16651   /* Add the context to the front of the free list.  */
16652   context->next = cp_parser_context_free_list;
16653   cp_parser_context_free_list = context;
16654
16655   return !error_occurred;
16656 }
16657
16658 /* Returns true if we are parsing tentatively and are not committed to
16659    this tentative parse.  */
16660
16661 static bool
16662 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16663 {
16664   return (cp_parser_parsing_tentatively (parser)
16665           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16666 }
16667
16668 /* Returns nonzero iff an error has occurred during the most recent
16669    tentative parse.  */
16670
16671 static bool
16672 cp_parser_error_occurred (cp_parser* parser)
16673 {
16674   return (cp_parser_parsing_tentatively (parser)
16675           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16676 }
16677
16678 /* Returns nonzero if GNU extensions are allowed.  */
16679
16680 static bool
16681 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16682 {
16683   return parser->allow_gnu_extensions_p;
16684 }
16685 \f
16686 /* Objective-C++ Productions */
16687
16688
16689 /* Parse an Objective-C expression, which feeds into a primary-expression
16690    above.
16691
16692    objc-expression:
16693      objc-message-expression
16694      objc-string-literal
16695      objc-encode-expression
16696      objc-protocol-expression
16697      objc-selector-expression
16698
16699   Returns a tree representation of the expression.  */
16700
16701 static tree
16702 cp_parser_objc_expression (cp_parser* parser)
16703 {
16704   /* Try to figure out what kind of declaration is present.  */
16705   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16706
16707   switch (kwd->type)
16708     {
16709     case CPP_OPEN_SQUARE:
16710       return cp_parser_objc_message_expression (parser);
16711
16712     case CPP_OBJC_STRING:
16713       kwd = cp_lexer_consume_token (parser->lexer);
16714       return objc_build_string_object (kwd->value);
16715
16716     case CPP_KEYWORD:
16717       switch (kwd->keyword)
16718         {
16719         case RID_AT_ENCODE:
16720           return cp_parser_objc_encode_expression (parser);
16721
16722         case RID_AT_PROTOCOL:
16723           return cp_parser_objc_protocol_expression (parser);
16724
16725         case RID_AT_SELECTOR:
16726           return cp_parser_objc_selector_expression (parser);
16727
16728         default:
16729           break;
16730         }
16731     default:
16732       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16733       cp_parser_skip_to_end_of_block_or_statement (parser);
16734     }
16735
16736   return error_mark_node;
16737 }
16738
16739 /* Parse an Objective-C message expression.
16740
16741    objc-message-expression:
16742      [ objc-message-receiver objc-message-args ]
16743
16744    Returns a representation of an Objective-C message.  */
16745
16746 static tree
16747 cp_parser_objc_message_expression (cp_parser* parser)
16748 {
16749   tree receiver, messageargs;
16750
16751   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16752   receiver = cp_parser_objc_message_receiver (parser);
16753   messageargs = cp_parser_objc_message_args (parser);
16754   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16755
16756   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16757 }
16758
16759 /* Parse an objc-message-receiver.
16760
16761    objc-message-receiver:
16762      expression
16763      simple-type-specifier
16764
16765   Returns a representation of the type or expression.  */
16766
16767 static tree
16768 cp_parser_objc_message_receiver (cp_parser* parser)
16769 {
16770   tree rcv;
16771
16772   /* An Objective-C message receiver may be either (1) a type
16773      or (2) an expression.  */
16774   cp_parser_parse_tentatively (parser);
16775   rcv = cp_parser_expression (parser, false);
16776
16777   if (cp_parser_parse_definitely (parser))
16778     return rcv;
16779
16780   rcv = cp_parser_simple_type_specifier (parser,
16781                                          /*decl_specs=*/NULL,
16782                                          CP_PARSER_FLAGS_NONE);
16783
16784   return objc_get_class_reference (rcv);
16785 }
16786
16787 /* Parse the arguments and selectors comprising an Objective-C message.
16788
16789    objc-message-args:
16790      objc-selector
16791      objc-selector-args
16792      objc-selector-args , objc-comma-args
16793
16794    objc-selector-args:
16795      objc-selector [opt] : assignment-expression
16796      objc-selector-args objc-selector [opt] : assignment-expression
16797
16798    objc-comma-args:
16799      assignment-expression
16800      objc-comma-args , assignment-expression
16801
16802    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16803    selector arguments and TREE_VALUE containing a list of comma
16804    arguments.  */
16805
16806 static tree
16807 cp_parser_objc_message_args (cp_parser* parser)
16808 {
16809   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16810   bool maybe_unary_selector_p = true;
16811   cp_token *token = cp_lexer_peek_token (parser->lexer);
16812
16813   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16814     {
16815       tree selector = NULL_TREE, arg;
16816
16817       if (token->type != CPP_COLON)
16818         selector = cp_parser_objc_selector (parser);
16819
16820       /* Detect if we have a unary selector.  */
16821       if (maybe_unary_selector_p
16822           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16823         return build_tree_list (selector, NULL_TREE);
16824
16825       maybe_unary_selector_p = false;
16826       cp_parser_require (parser, CPP_COLON, "`:'");
16827       arg = cp_parser_assignment_expression (parser, false);
16828
16829       sel_args
16830         = chainon (sel_args,
16831                    build_tree_list (selector, arg));
16832
16833       token = cp_lexer_peek_token (parser->lexer);
16834     }
16835
16836   /* Handle non-selector arguments, if any. */
16837   while (token->type == CPP_COMMA)
16838     {
16839       tree arg;
16840
16841       cp_lexer_consume_token (parser->lexer);
16842       arg = cp_parser_assignment_expression (parser, false);
16843
16844       addl_args
16845         = chainon (addl_args,
16846                    build_tree_list (NULL_TREE, arg));
16847
16848       token = cp_lexer_peek_token (parser->lexer);
16849     }
16850
16851   return build_tree_list (sel_args, addl_args);
16852 }
16853
16854 /* Parse an Objective-C encode expression.
16855
16856    objc-encode-expression:
16857      @encode objc-typename
16858
16859    Returns an encoded representation of the type argument.  */
16860
16861 static tree
16862 cp_parser_objc_encode_expression (cp_parser* parser)
16863 {
16864   tree type;
16865
16866   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16867   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16868   type = complete_type (cp_parser_type_id (parser));
16869   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16870
16871   if (!type)
16872     {
16873       error ("%<@encode%> must specify a type as an argument");
16874       return error_mark_node;
16875     }
16876
16877   return objc_build_encode_expr (type);
16878 }
16879
16880 /* Parse an Objective-C @defs expression.  */
16881
16882 static tree
16883 cp_parser_objc_defs_expression (cp_parser *parser)
16884 {
16885   tree name;
16886
16887   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16888   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16889   name = cp_parser_identifier (parser);
16890   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16891
16892   return objc_get_class_ivars (name);
16893 }
16894
16895 /* Parse an Objective-C protocol expression.
16896
16897   objc-protocol-expression:
16898     @protocol ( identifier )
16899
16900   Returns a representation of the protocol expression.  */
16901
16902 static tree
16903 cp_parser_objc_protocol_expression (cp_parser* parser)
16904 {
16905   tree proto;
16906
16907   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16908   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16909   proto = cp_parser_identifier (parser);
16910   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16911
16912   return objc_build_protocol_expr (proto);
16913 }
16914
16915 /* Parse an Objective-C selector expression.
16916
16917    objc-selector-expression:
16918      @selector ( objc-method-signature )
16919
16920    objc-method-signature:
16921      objc-selector
16922      objc-selector-seq
16923
16924    objc-selector-seq:
16925      objc-selector :
16926      objc-selector-seq objc-selector :
16927
16928   Returns a representation of the method selector.  */
16929
16930 static tree
16931 cp_parser_objc_selector_expression (cp_parser* parser)
16932 {
16933   tree sel_seq = NULL_TREE;
16934   bool maybe_unary_selector_p = true;
16935   cp_token *token;
16936
16937   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16938   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16939   token = cp_lexer_peek_token (parser->lexer);
16940
16941   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16942          || token->type == CPP_SCOPE)
16943     {
16944       tree selector = NULL_TREE;
16945
16946       if (token->type != CPP_COLON
16947           || token->type == CPP_SCOPE)
16948         selector = cp_parser_objc_selector (parser);
16949
16950       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16951           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16952         {
16953           /* Detect if we have a unary selector.  */
16954           if (maybe_unary_selector_p)
16955             {
16956               sel_seq = selector;
16957               goto finish_selector;
16958             }
16959           else
16960             {
16961               cp_parser_error (parser, "expected %<:%>");
16962             }
16963         }
16964       maybe_unary_selector_p = false;
16965       token = cp_lexer_consume_token (parser->lexer);
16966
16967       if (token->type == CPP_SCOPE)
16968         {
16969           sel_seq
16970             = chainon (sel_seq,
16971                        build_tree_list (selector, NULL_TREE));
16972           sel_seq
16973             = chainon (sel_seq,
16974                        build_tree_list (NULL_TREE, NULL_TREE));
16975         }
16976       else
16977         sel_seq
16978           = chainon (sel_seq,
16979                      build_tree_list (selector, NULL_TREE));
16980
16981       token = cp_lexer_peek_token (parser->lexer);
16982     }
16983
16984  finish_selector:
16985   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16986
16987   return objc_build_selector_expr (sel_seq);
16988 }
16989
16990 /* Parse a list of identifiers.
16991
16992    objc-identifier-list:
16993      identifier
16994      objc-identifier-list , identifier
16995
16996    Returns a TREE_LIST of identifier nodes.  */
16997
16998 static tree
16999 cp_parser_objc_identifier_list (cp_parser* parser)
17000 {
17001   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17002   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17003
17004   while (sep->type == CPP_COMMA)
17005     {
17006       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17007       list = chainon (list,
17008                       build_tree_list (NULL_TREE,
17009                                        cp_parser_identifier (parser)));
17010       sep = cp_lexer_peek_token (parser->lexer);
17011     }
17012
17013   return list;
17014 }
17015
17016 /* Parse an Objective-C alias declaration.
17017
17018    objc-alias-declaration:
17019      @compatibility_alias identifier identifier ;
17020
17021    This function registers the alias mapping with the Objective-C front-end.
17022    It returns nothing.  */
17023
17024 static void
17025 cp_parser_objc_alias_declaration (cp_parser* parser)
17026 {
17027   tree alias, orig;
17028
17029   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17030   alias = cp_parser_identifier (parser);
17031   orig = cp_parser_identifier (parser);
17032   objc_declare_alias (alias, orig);
17033   cp_parser_consume_semicolon_at_end_of_statement (parser);
17034 }
17035
17036 /* Parse an Objective-C class forward-declaration.
17037
17038    objc-class-declaration:
17039      @class objc-identifier-list ;
17040
17041    The function registers the forward declarations with the Objective-C
17042    front-end.  It returns nothing.  */
17043
17044 static void
17045 cp_parser_objc_class_declaration (cp_parser* parser)
17046 {
17047   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17048   objc_declare_class (cp_parser_objc_identifier_list (parser));
17049   cp_parser_consume_semicolon_at_end_of_statement (parser);
17050 }
17051
17052 /* Parse a list of Objective-C protocol references.
17053
17054    objc-protocol-refs-opt:
17055      objc-protocol-refs [opt]
17056
17057    objc-protocol-refs:
17058      < objc-identifier-list >
17059
17060    Returns a TREE_LIST of identifiers, if any.  */
17061
17062 static tree
17063 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17064 {
17065   tree protorefs = NULL_TREE;
17066
17067   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17068     {
17069       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17070       protorefs = cp_parser_objc_identifier_list (parser);
17071       cp_parser_require (parser, CPP_GREATER, "`>'");
17072     }
17073
17074   return protorefs;
17075 }
17076
17077 /* Parse a Objective-C visibility specification.  */
17078
17079 static void
17080 cp_parser_objc_visibility_spec (cp_parser* parser)
17081 {
17082   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17083
17084   switch (vis->keyword)
17085     {
17086     case RID_AT_PRIVATE:
17087       objc_set_visibility (2);
17088       break;
17089     case RID_AT_PROTECTED:
17090       objc_set_visibility (0);
17091       break;
17092     case RID_AT_PUBLIC:
17093       objc_set_visibility (1);
17094       break;
17095     default:
17096       return;
17097     }
17098
17099   /* Eat '@private'/'@protected'/'@public'.  */
17100   cp_lexer_consume_token (parser->lexer);
17101 }
17102
17103 /* Parse an Objective-C method type.  */
17104
17105 static void
17106 cp_parser_objc_method_type (cp_parser* parser)
17107 {
17108   objc_set_method_type
17109    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17110     ? PLUS_EXPR
17111     : MINUS_EXPR);
17112 }
17113
17114 /* Parse an Objective-C protocol qualifier.  */
17115
17116 static tree
17117 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17118 {
17119   tree quals = NULL_TREE, node;
17120   cp_token *token = cp_lexer_peek_token (parser->lexer);
17121
17122   node = token->value;
17123
17124   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17125          && (node == ridpointers [(int) RID_IN]
17126              || node == ridpointers [(int) RID_OUT]
17127              || node == ridpointers [(int) RID_INOUT]
17128              || node == ridpointers [(int) RID_BYCOPY]
17129              || node == ridpointers [(int) RID_BYREF]
17130              || node == ridpointers [(int) RID_ONEWAY]))
17131     {
17132       quals = tree_cons (NULL_TREE, node, quals);
17133       cp_lexer_consume_token (parser->lexer);
17134       token = cp_lexer_peek_token (parser->lexer);
17135       node = token->value;
17136     }
17137
17138   return quals;
17139 }
17140
17141 /* Parse an Objective-C typename.  */
17142
17143 static tree
17144 cp_parser_objc_typename (cp_parser* parser)
17145 {
17146   tree typename = NULL_TREE;
17147
17148   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17149     {
17150       tree proto_quals, cp_type = NULL_TREE;
17151
17152       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17153       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17154
17155       /* An ObjC type name may consist of just protocol qualifiers, in which
17156          case the type shall default to 'id'.  */
17157       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17158         cp_type = cp_parser_type_id (parser);
17159
17160       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17161       typename = build_tree_list (proto_quals, cp_type);
17162     }
17163
17164   return typename;
17165 }
17166
17167 /* Check to see if TYPE refers to an Objective-C selector name.  */
17168
17169 static bool
17170 cp_parser_objc_selector_p (enum cpp_ttype type)
17171 {
17172   return (type == CPP_NAME || type == CPP_KEYWORD
17173           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17174           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17175           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17176           || type == CPP_XOR || type == CPP_XOR_EQ);
17177 }
17178
17179 /* Parse an Objective-C selector.  */
17180
17181 static tree
17182 cp_parser_objc_selector (cp_parser* parser)
17183 {
17184   cp_token *token = cp_lexer_consume_token (parser->lexer);
17185
17186   if (!cp_parser_objc_selector_p (token->type))
17187     {
17188       error ("invalid Objective-C++ selector name");
17189       return error_mark_node;
17190     }
17191
17192   /* C++ operator names are allowed to appear in ObjC selectors.  */
17193   switch (token->type)
17194     {
17195     case CPP_AND_AND: return get_identifier ("and");
17196     case CPP_AND_EQ: return get_identifier ("and_eq");
17197     case CPP_AND: return get_identifier ("bitand");
17198     case CPP_OR: return get_identifier ("bitor");
17199     case CPP_COMPL: return get_identifier ("compl");
17200     case CPP_NOT: return get_identifier ("not");
17201     case CPP_NOT_EQ: return get_identifier ("not_eq");
17202     case CPP_OR_OR: return get_identifier ("or");
17203     case CPP_OR_EQ: return get_identifier ("or_eq");
17204     case CPP_XOR: return get_identifier ("xor");
17205     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17206     default: return token->value;
17207     }
17208 }
17209
17210 /* Parse an Objective-C params list.  */
17211
17212 static tree
17213 cp_parser_objc_method_keyword_params (cp_parser* parser)
17214 {
17215   tree params = NULL_TREE;
17216   bool maybe_unary_selector_p = true;
17217   cp_token *token = cp_lexer_peek_token (parser->lexer);
17218
17219   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17220     {
17221       tree selector = NULL_TREE, typename, identifier;
17222
17223       if (token->type != CPP_COLON)
17224         selector = cp_parser_objc_selector (parser);
17225
17226       /* Detect if we have a unary selector.  */
17227       if (maybe_unary_selector_p
17228           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17229         return selector;
17230
17231       maybe_unary_selector_p = false;
17232       cp_parser_require (parser, CPP_COLON, "`:'");
17233       typename = cp_parser_objc_typename (parser);
17234       identifier = cp_parser_identifier (parser);
17235
17236       params
17237         = chainon (params,
17238                    objc_build_keyword_decl (selector,
17239                                             typename,
17240                                             identifier));
17241
17242       token = cp_lexer_peek_token (parser->lexer);
17243     }
17244
17245   return params;
17246 }
17247
17248 /* Parse the non-keyword Objective-C params.  */
17249
17250 static tree
17251 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17252 {
17253   tree params = make_node (TREE_LIST);
17254   cp_token *token = cp_lexer_peek_token (parser->lexer);
17255   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17256
17257   while (token->type == CPP_COMMA)
17258     {
17259       cp_parameter_declarator *parmdecl;
17260       tree parm;
17261
17262       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17263       token = cp_lexer_peek_token (parser->lexer);
17264
17265       if (token->type == CPP_ELLIPSIS)
17266         {
17267           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17268           *ellipsisp = true;
17269           break;
17270         }
17271
17272       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17273       parm = grokdeclarator (parmdecl->declarator,
17274                              &parmdecl->decl_specifiers,
17275                              PARM, /*initialized=*/0,
17276                              /*attrlist=*/NULL);
17277
17278       chainon (params, build_tree_list (NULL_TREE, parm));
17279       token = cp_lexer_peek_token (parser->lexer);
17280     }
17281
17282   return params;
17283 }
17284
17285 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17286
17287 static void
17288 cp_parser_objc_interstitial_code (cp_parser* parser)
17289 {
17290   cp_token *token = cp_lexer_peek_token (parser->lexer);
17291
17292   /* If the next token is `extern' and the following token is a string
17293      literal, then we have a linkage specification.  */
17294   if (token->keyword == RID_EXTERN
17295       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17296     cp_parser_linkage_specification (parser);
17297   /* Handle #pragma, if any.  */
17298   else if (token->type == CPP_PRAGMA)
17299     cp_parser_pragma (parser, pragma_external);
17300   /* Allow stray semicolons.  */
17301   else if (token->type == CPP_SEMICOLON)
17302     cp_lexer_consume_token (parser->lexer);
17303   /* Finally, try to parse a block-declaration, or a function-definition.  */
17304   else
17305     cp_parser_block_declaration (parser, /*statement_p=*/false);
17306 }
17307
17308 /* Parse a method signature.  */
17309
17310 static tree
17311 cp_parser_objc_method_signature (cp_parser* parser)
17312 {
17313   tree rettype, kwdparms, optparms;
17314   bool ellipsis = false;
17315
17316   cp_parser_objc_method_type (parser);
17317   rettype = cp_parser_objc_typename (parser);
17318   kwdparms = cp_parser_objc_method_keyword_params (parser);
17319   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17320
17321   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17322 }
17323
17324 /* Pars an Objective-C method prototype list.  */
17325
17326 static void
17327 cp_parser_objc_method_prototype_list (cp_parser* parser)
17328 {
17329   cp_token *token = cp_lexer_peek_token (parser->lexer);
17330
17331   while (token->keyword != RID_AT_END)
17332     {
17333       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17334         {
17335           objc_add_method_declaration
17336            (cp_parser_objc_method_signature (parser));
17337           cp_parser_consume_semicolon_at_end_of_statement (parser);
17338         }
17339       else
17340         /* Allow for interspersed non-ObjC++ code.  */
17341         cp_parser_objc_interstitial_code (parser);
17342
17343       token = cp_lexer_peek_token (parser->lexer);
17344     }
17345
17346   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17347   objc_finish_interface ();
17348 }
17349
17350 /* Parse an Objective-C method definition list.  */
17351
17352 static void
17353 cp_parser_objc_method_definition_list (cp_parser* parser)
17354 {
17355   cp_token *token = cp_lexer_peek_token (parser->lexer);
17356
17357   while (token->keyword != RID_AT_END)
17358     {
17359       tree meth;
17360
17361       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17362         {
17363           push_deferring_access_checks (dk_deferred);
17364           objc_start_method_definition
17365            (cp_parser_objc_method_signature (parser));
17366
17367           /* For historical reasons, we accept an optional semicolon.  */
17368           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17369             cp_lexer_consume_token (parser->lexer);
17370
17371           perform_deferred_access_checks ();
17372           stop_deferring_access_checks ();
17373           meth = cp_parser_function_definition_after_declarator (parser,
17374                                                                  false);
17375           pop_deferring_access_checks ();
17376           objc_finish_method_definition (meth);
17377         }
17378       else
17379         /* Allow for interspersed non-ObjC++ code.  */
17380         cp_parser_objc_interstitial_code (parser);
17381
17382       token = cp_lexer_peek_token (parser->lexer);
17383     }
17384
17385   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17386   objc_finish_implementation ();
17387 }
17388
17389 /* Parse Objective-C ivars.  */
17390
17391 static void
17392 cp_parser_objc_class_ivars (cp_parser* parser)
17393 {
17394   cp_token *token = cp_lexer_peek_token (parser->lexer);
17395
17396   if (token->type != CPP_OPEN_BRACE)
17397     return;     /* No ivars specified.  */
17398
17399   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17400   token = cp_lexer_peek_token (parser->lexer);
17401
17402   while (token->type != CPP_CLOSE_BRACE)
17403     {
17404       cp_decl_specifier_seq declspecs;
17405       int decl_class_or_enum_p;
17406       tree prefix_attributes;
17407
17408       cp_parser_objc_visibility_spec (parser);
17409
17410       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17411         break;
17412
17413       cp_parser_decl_specifier_seq (parser,
17414                                     CP_PARSER_FLAGS_OPTIONAL,
17415                                     &declspecs,
17416                                     &decl_class_or_enum_p);
17417       prefix_attributes = declspecs.attributes;
17418       declspecs.attributes = NULL_TREE;
17419
17420       /* Keep going until we hit the `;' at the end of the
17421          declaration.  */
17422       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17423         {
17424           tree width = NULL_TREE, attributes, first_attribute, decl;
17425           cp_declarator *declarator = NULL;
17426           int ctor_dtor_or_conv_p;
17427
17428           /* Check for a (possibly unnamed) bitfield declaration.  */
17429           token = cp_lexer_peek_token (parser->lexer);
17430           if (token->type == CPP_COLON)
17431             goto eat_colon;
17432
17433           if (token->type == CPP_NAME
17434               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17435                   == CPP_COLON))
17436             {
17437               /* Get the name of the bitfield.  */
17438               declarator = make_id_declarator (NULL_TREE,
17439                                                cp_parser_identifier (parser),
17440                                                sfk_none);
17441
17442              eat_colon:
17443               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17444               /* Get the width of the bitfield.  */
17445               width
17446                 = cp_parser_constant_expression (parser,
17447                                                  /*allow_non_constant=*/false,
17448                                                  NULL);
17449             }
17450           else
17451             {
17452               /* Parse the declarator.  */
17453               declarator
17454                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17455                                         &ctor_dtor_or_conv_p,
17456                                         /*parenthesized_p=*/NULL,
17457                                         /*member_p=*/false);
17458             }
17459
17460           /* Look for attributes that apply to the ivar.  */
17461           attributes = cp_parser_attributes_opt (parser);
17462           /* Remember which attributes are prefix attributes and
17463              which are not.  */
17464           first_attribute = attributes;
17465           /* Combine the attributes.  */
17466           attributes = chainon (prefix_attributes, attributes);
17467
17468           if (width)
17469             {
17470               /* Create the bitfield declaration.  */
17471               decl = grokbitfield (declarator, &declspecs, width);
17472               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17473             }
17474           else
17475             decl = grokfield (declarator, &declspecs,
17476                               NULL_TREE, /*init_const_expr_p=*/false,
17477                               NULL_TREE, attributes);
17478
17479           /* Add the instance variable.  */
17480           objc_add_instance_variable (decl);
17481
17482           /* Reset PREFIX_ATTRIBUTES.  */
17483           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17484             attributes = TREE_CHAIN (attributes);
17485           if (attributes)
17486             TREE_CHAIN (attributes) = NULL_TREE;
17487
17488           token = cp_lexer_peek_token (parser->lexer);
17489
17490           if (token->type == CPP_COMMA)
17491             {
17492               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17493               continue;
17494             }
17495           break;
17496         }
17497
17498       cp_parser_consume_semicolon_at_end_of_statement (parser);
17499       token = cp_lexer_peek_token (parser->lexer);
17500     }
17501
17502   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17503   /* For historical reasons, we accept an optional semicolon.  */
17504   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17505     cp_lexer_consume_token (parser->lexer);
17506 }
17507
17508 /* Parse an Objective-C protocol declaration.  */
17509
17510 static void
17511 cp_parser_objc_protocol_declaration (cp_parser* parser)
17512 {
17513   tree proto, protorefs;
17514   cp_token *tok;
17515
17516   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17517   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17518     {
17519       error ("identifier expected after %<@protocol%>");
17520       goto finish;
17521     }
17522
17523   /* See if we have a forward declaration or a definition.  */
17524   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17525
17526   /* Try a forward declaration first.  */
17527   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17528     {
17529       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17530      finish:
17531       cp_parser_consume_semicolon_at_end_of_statement (parser);
17532     }
17533
17534   /* Ok, we got a full-fledged definition (or at least should).  */
17535   else
17536     {
17537       proto = cp_parser_identifier (parser);
17538       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17539       objc_start_protocol (proto, protorefs);
17540       cp_parser_objc_method_prototype_list (parser);
17541     }
17542 }
17543
17544 /* Parse an Objective-C superclass or category.  */
17545
17546 static void
17547 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17548                                                           tree *categ)
17549 {
17550   cp_token *next = cp_lexer_peek_token (parser->lexer);
17551
17552   *super = *categ = NULL_TREE;
17553   if (next->type == CPP_COLON)
17554     {
17555       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17556       *super = cp_parser_identifier (parser);
17557     }
17558   else if (next->type == CPP_OPEN_PAREN)
17559     {
17560       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17561       *categ = cp_parser_identifier (parser);
17562       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17563     }
17564 }
17565
17566 /* Parse an Objective-C class interface.  */
17567
17568 static void
17569 cp_parser_objc_class_interface (cp_parser* parser)
17570 {
17571   tree name, super, categ, protos;
17572
17573   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17574   name = cp_parser_identifier (parser);
17575   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17576   protos = cp_parser_objc_protocol_refs_opt (parser);
17577
17578   /* We have either a class or a category on our hands.  */
17579   if (categ)
17580     objc_start_category_interface (name, categ, protos);
17581   else
17582     {
17583       objc_start_class_interface (name, super, protos);
17584       /* Handle instance variable declarations, if any.  */
17585       cp_parser_objc_class_ivars (parser);
17586       objc_continue_interface ();
17587     }
17588
17589   cp_parser_objc_method_prototype_list (parser);
17590 }
17591
17592 /* Parse an Objective-C class implementation.  */
17593
17594 static void
17595 cp_parser_objc_class_implementation (cp_parser* parser)
17596 {
17597   tree name, super, categ;
17598
17599   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17600   name = cp_parser_identifier (parser);
17601   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17602
17603   /* We have either a class or a category on our hands.  */
17604   if (categ)
17605     objc_start_category_implementation (name, categ);
17606   else
17607     {
17608       objc_start_class_implementation (name, super);
17609       /* Handle instance variable declarations, if any.  */
17610       cp_parser_objc_class_ivars (parser);
17611       objc_continue_implementation ();
17612     }
17613
17614   cp_parser_objc_method_definition_list (parser);
17615 }
17616
17617 /* Consume the @end token and finish off the implementation.  */
17618
17619 static void
17620 cp_parser_objc_end_implementation (cp_parser* parser)
17621 {
17622   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17623   objc_finish_implementation ();
17624 }
17625
17626 /* Parse an Objective-C declaration.  */
17627
17628 static void
17629 cp_parser_objc_declaration (cp_parser* parser)
17630 {
17631   /* Try to figure out what kind of declaration is present.  */
17632   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17633
17634   switch (kwd->keyword)
17635     {
17636     case RID_AT_ALIAS:
17637       cp_parser_objc_alias_declaration (parser);
17638       break;
17639     case RID_AT_CLASS:
17640       cp_parser_objc_class_declaration (parser);
17641       break;
17642     case RID_AT_PROTOCOL:
17643       cp_parser_objc_protocol_declaration (parser);
17644       break;
17645     case RID_AT_INTERFACE:
17646       cp_parser_objc_class_interface (parser);
17647       break;
17648     case RID_AT_IMPLEMENTATION:
17649       cp_parser_objc_class_implementation (parser);
17650       break;
17651     case RID_AT_END:
17652       cp_parser_objc_end_implementation (parser);
17653       break;
17654     default:
17655       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17656       cp_parser_skip_to_end_of_block_or_statement (parser);
17657     }
17658 }
17659
17660 /* Parse an Objective-C try-catch-finally statement.
17661
17662    objc-try-catch-finally-stmt:
17663      @try compound-statement objc-catch-clause-seq [opt]
17664        objc-finally-clause [opt]
17665
17666    objc-catch-clause-seq:
17667      objc-catch-clause objc-catch-clause-seq [opt]
17668
17669    objc-catch-clause:
17670      @catch ( exception-declaration ) compound-statement
17671
17672    objc-finally-clause
17673      @finally compound-statement
17674
17675    Returns NULL_TREE.  */
17676
17677 static tree
17678 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17679   location_t location;
17680   tree stmt;
17681
17682   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17683   location = cp_lexer_peek_token (parser->lexer)->location;
17684   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17685      node, lest it get absorbed into the surrounding block.  */
17686   stmt = push_stmt_list ();
17687   cp_parser_compound_statement (parser, NULL, false);
17688   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17689
17690   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17691     {
17692       cp_parameter_declarator *parmdecl;
17693       tree parm;
17694
17695       cp_lexer_consume_token (parser->lexer);
17696       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17697       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17698       parm = grokdeclarator (parmdecl->declarator,
17699                              &parmdecl->decl_specifiers,
17700                              PARM, /*initialized=*/0,
17701                              /*attrlist=*/NULL);
17702       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17703       objc_begin_catch_clause (parm);
17704       cp_parser_compound_statement (parser, NULL, false);
17705       objc_finish_catch_clause ();
17706     }
17707
17708   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17709     {
17710       cp_lexer_consume_token (parser->lexer);
17711       location = cp_lexer_peek_token (parser->lexer)->location;
17712       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17713          node, lest it get absorbed into the surrounding block.  */
17714       stmt = push_stmt_list ();
17715       cp_parser_compound_statement (parser, NULL, false);
17716       objc_build_finally_clause (location, pop_stmt_list (stmt));
17717     }
17718
17719   return objc_finish_try_stmt ();
17720 }
17721
17722 /* Parse an Objective-C synchronized statement.
17723
17724    objc-synchronized-stmt:
17725      @synchronized ( expression ) compound-statement
17726
17727    Returns NULL_TREE.  */
17728
17729 static tree
17730 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17731   location_t location;
17732   tree lock, stmt;
17733
17734   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17735
17736   location = cp_lexer_peek_token (parser->lexer)->location;
17737   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17738   lock = cp_parser_expression (parser, false);
17739   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17740
17741   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17742      node, lest it get absorbed into the surrounding block.  */
17743   stmt = push_stmt_list ();
17744   cp_parser_compound_statement (parser, NULL, false);
17745
17746   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17747 }
17748
17749 /* Parse an Objective-C throw statement.
17750
17751    objc-throw-stmt:
17752      @throw assignment-expression [opt] ;
17753
17754    Returns a constructed '@throw' statement.  */
17755
17756 static tree
17757 cp_parser_objc_throw_statement (cp_parser *parser) {
17758   tree expr = NULL_TREE;
17759
17760   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17761
17762   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17763     expr = cp_parser_assignment_expression (parser, false);
17764
17765   cp_parser_consume_semicolon_at_end_of_statement (parser);
17766
17767   return objc_build_throw_stmt (expr);
17768 }
17769
17770 /* Parse an Objective-C statement.  */
17771
17772 static tree
17773 cp_parser_objc_statement (cp_parser * parser) {
17774   /* Try to figure out what kind of declaration is present.  */
17775   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17776
17777   switch (kwd->keyword)
17778     {
17779     case RID_AT_TRY:
17780       return cp_parser_objc_try_catch_finally_statement (parser);
17781     case RID_AT_SYNCHRONIZED:
17782       return cp_parser_objc_synchronized_statement (parser);
17783     case RID_AT_THROW:
17784       return cp_parser_objc_throw_statement (parser);
17785     default:
17786       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17787       cp_parser_skip_to_end_of_block_or_statement (parser);
17788     }
17789
17790   return error_mark_node;
17791 }
17792 \f
17793 /* OpenMP 2.5 parsing routines.  */
17794
17795 /* All OpenMP clauses.  OpenMP 2.5.  */
17796 typedef enum pragma_omp_clause {
17797   PRAGMA_OMP_CLAUSE_NONE = 0,
17798
17799   PRAGMA_OMP_CLAUSE_COPYIN,
17800   PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17801   PRAGMA_OMP_CLAUSE_DEFAULT,
17802   PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17803   PRAGMA_OMP_CLAUSE_IF,
17804   PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17805   PRAGMA_OMP_CLAUSE_NOWAIT,
17806   PRAGMA_OMP_CLAUSE_NUM_THREADS,
17807   PRAGMA_OMP_CLAUSE_ORDERED,
17808   PRAGMA_OMP_CLAUSE_PRIVATE,
17809   PRAGMA_OMP_CLAUSE_REDUCTION,
17810   PRAGMA_OMP_CLAUSE_SCHEDULE,
17811   PRAGMA_OMP_CLAUSE_SHARED
17812 } pragma_omp_clause;
17813
17814 /* Returns name of the next clause.
17815    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17816    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
17817    returned and the token is consumed.  */
17818
17819 static pragma_omp_clause
17820 cp_parser_omp_clause_name (cp_parser *parser)
17821 {
17822   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17823
17824   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17825     result = PRAGMA_OMP_CLAUSE_IF;
17826   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17827     result = PRAGMA_OMP_CLAUSE_DEFAULT;
17828   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17829     result = PRAGMA_OMP_CLAUSE_PRIVATE;
17830   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17831     {
17832       tree id = cp_lexer_peek_token (parser->lexer)->value;
17833       const char *p = IDENTIFIER_POINTER (id);
17834
17835       switch (p[0])
17836         {
17837         case 'c':
17838           if (!strcmp ("copyin", p))
17839             result = PRAGMA_OMP_CLAUSE_COPYIN;
17840           else if (!strcmp ("copyprivate", p))
17841             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17842           break;
17843         case 'f':
17844           if (!strcmp ("firstprivate", p))
17845             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17846           break;
17847         case 'l':
17848           if (!strcmp ("lastprivate", p))
17849             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17850           break;
17851         case 'n':
17852           if (!strcmp ("nowait", p))
17853             result = PRAGMA_OMP_CLAUSE_NOWAIT;
17854           else if (!strcmp ("num_threads", p))
17855             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17856           break;
17857         case 'o':
17858           if (!strcmp ("ordered", p))
17859             result = PRAGMA_OMP_CLAUSE_ORDERED;
17860           break;
17861         case 'r':
17862           if (!strcmp ("reduction", p))
17863             result = PRAGMA_OMP_CLAUSE_REDUCTION;
17864           break;
17865         case 's':
17866           if (!strcmp ("schedule", p))
17867             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17868           else if (!strcmp ("shared", p))
17869             result = PRAGMA_OMP_CLAUSE_SHARED;
17870           break;
17871         }
17872     }
17873
17874   if (result != PRAGMA_OMP_CLAUSE_NONE)
17875     cp_lexer_consume_token (parser->lexer);
17876
17877   return result;
17878 }
17879
17880 /* Validate that a clause of the given type does not already exist.  */
17881
17882 static void
17883 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17884 {
17885   tree c;
17886
17887   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17888     if (OMP_CLAUSE_CODE (c) == code)
17889       {
17890         error ("too many %qs clauses", name);
17891         break;
17892       }
17893 }
17894
17895 /* OpenMP 2.5:
17896    variable-list:
17897      identifier
17898      variable-list , identifier
17899
17900    In addition, we match a closing parenthesis.  An opening parenthesis
17901    will have been consumed by the caller.
17902
17903    If KIND is nonzero, create the appropriate node and install the decl
17904    in OMP_CLAUSE_DECL and add the node to the head of the list.
17905
17906    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17907    return the list created.  */
17908
17909 static tree
17910 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17911                                 tree list)
17912 {
17913   while (1)
17914     {
17915       tree name, decl;
17916
17917       name = cp_parser_id_expression (parser, /*template_p=*/false,
17918                                       /*check_dependency_p=*/true,
17919                                       /*template_p=*/NULL,
17920                                       /*declarator_p=*/false,
17921                                       /*optional_p=*/false);
17922       if (name == error_mark_node)
17923         goto skip_comma;
17924
17925       decl = cp_parser_lookup_name_simple (parser, name);
17926       if (decl == error_mark_node)
17927         cp_parser_name_lookup_error (parser, name, decl, NULL);
17928       else if (kind != 0)
17929         {
17930           tree u = build_omp_clause (kind);
17931           OMP_CLAUSE_DECL (u) = decl;
17932           OMP_CLAUSE_CHAIN (u) = list;
17933           list = u;
17934         }
17935       else
17936         list = tree_cons (decl, NULL_TREE, list);
17937
17938     get_comma:
17939       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17940         break;
17941       cp_lexer_consume_token (parser->lexer);
17942     }
17943
17944   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17945     {
17946       int ending;
17947
17948       /* Try to resync to an unnested comma.  Copied from
17949          cp_parser_parenthesized_expression_list.  */
17950     skip_comma:
17951       ending = cp_parser_skip_to_closing_parenthesis (parser,
17952                                                       /*recovering=*/true,
17953                                                       /*or_comma=*/true,
17954                                                       /*consume_paren=*/true);
17955       if (ending < 0)
17956         goto get_comma;
17957     }
17958
17959   return list;
17960 }
17961
17962 /* Similarly, but expect leading and trailing parenthesis.  This is a very
17963    common case for omp clauses.  */
17964
17965 static tree
17966 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17967 {
17968   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17969     return cp_parser_omp_var_list_no_open (parser, kind, list);
17970   return list;
17971 }
17972
17973 /* OpenMP 2.5:
17974    default ( shared | none ) */
17975
17976 static tree
17977 cp_parser_omp_clause_default (cp_parser *parser, tree list)
17978 {
17979   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17980   tree c;
17981
17982   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17983     return list;
17984   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17985     {
17986       tree id = cp_lexer_peek_token (parser->lexer)->value;
17987       const char *p = IDENTIFIER_POINTER (id);
17988
17989       switch (p[0])
17990         {
17991         case 'n':
17992           if (strcmp ("none", p) != 0)
17993             goto invalid_kind;
17994           kind = OMP_CLAUSE_DEFAULT_NONE;
17995           break;
17996
17997         case 's':
17998           if (strcmp ("shared", p) != 0)
17999             goto invalid_kind;
18000           kind = OMP_CLAUSE_DEFAULT_SHARED;
18001           break;
18002
18003         default:
18004           goto invalid_kind;
18005         }
18006
18007       cp_lexer_consume_token (parser->lexer);
18008     }
18009   else
18010     {
18011     invalid_kind:
18012       cp_parser_error (parser, "expected %<none%> or %<shared%>");
18013     }
18014
18015   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18016     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18017                                            /*or_comma=*/false,
18018                                            /*consume_paren=*/true);
18019
18020   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18021     return list;
18022
18023   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18024   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18025   OMP_CLAUSE_CHAIN (c) = list;
18026   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18027
18028   return c;
18029 }
18030
18031 /* OpenMP 2.5:
18032    if ( expression ) */
18033
18034 static tree
18035 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18036 {
18037   tree t, c;
18038
18039   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18040     return list;
18041
18042   t = cp_parser_condition (parser);
18043
18044   if (t == error_mark_node
18045       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18046     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18047                                            /*or_comma=*/false,
18048                                            /*consume_paren=*/true);
18049
18050   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18051
18052   c = build_omp_clause (OMP_CLAUSE_IF);
18053   OMP_CLAUSE_IF_EXPR (c) = t;
18054   OMP_CLAUSE_CHAIN (c) = list;
18055
18056   return c;
18057 }
18058
18059 /* OpenMP 2.5:
18060    nowait */
18061
18062 static tree
18063 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18064 {
18065   tree c;
18066
18067   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18068
18069   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18070   OMP_CLAUSE_CHAIN (c) = list;
18071   return c;
18072 }
18073
18074 /* OpenMP 2.5:
18075    num_threads ( expression ) */
18076
18077 static tree
18078 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18079 {
18080   tree t, c;
18081
18082   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18083     return list;
18084
18085   t = cp_parser_expression (parser, false);
18086
18087   if (t == error_mark_node
18088       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18089     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18090                                            /*or_comma=*/false,
18091                                            /*consume_paren=*/true);
18092
18093   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18094
18095   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18096   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18097   OMP_CLAUSE_CHAIN (c) = list;
18098
18099   return c;
18100 }
18101
18102 /* OpenMP 2.5:
18103    ordered */
18104
18105 static tree
18106 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18107 {
18108   tree c;
18109
18110   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18111
18112   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18113   OMP_CLAUSE_CHAIN (c) = list;
18114   return c;
18115 }
18116
18117 /* OpenMP 2.5:
18118    reduction ( reduction-operator : variable-list )
18119
18120    reduction-operator:
18121      One of: + * - & ^ | && || */
18122
18123 static tree
18124 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18125 {
18126   enum tree_code code;
18127   tree nlist, c;
18128
18129   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18130     return list;
18131
18132   switch (cp_lexer_peek_token (parser->lexer)->type)
18133     {
18134     case CPP_PLUS:
18135       code = PLUS_EXPR;
18136       break;
18137     case CPP_MULT:
18138       code = MULT_EXPR;
18139       break;
18140     case CPP_MINUS:
18141       code = MINUS_EXPR;
18142       break;
18143     case CPP_AND:
18144       code = BIT_AND_EXPR;
18145       break;
18146     case CPP_XOR:
18147       code = BIT_XOR_EXPR;
18148       break;
18149     case CPP_OR:
18150       code = BIT_IOR_EXPR;
18151       break;
18152     case CPP_AND_AND:
18153       code = TRUTH_ANDIF_EXPR;
18154       break;
18155     case CPP_OR_OR:
18156       code = TRUTH_ORIF_EXPR;
18157       break;
18158     default:
18159       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18160     resync_fail:
18161       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18162                                              /*or_comma=*/false,
18163                                              /*consume_paren=*/true);
18164       return list;
18165     }
18166   cp_lexer_consume_token (parser->lexer);
18167
18168   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18169     goto resync_fail;
18170
18171   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18172   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18173     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18174
18175   return nlist;
18176 }
18177
18178 /* OpenMP 2.5:
18179    schedule ( schedule-kind )
18180    schedule ( schedule-kind , expression )
18181
18182    schedule-kind:
18183      static | dynamic | guided | runtime  */
18184
18185 static tree
18186 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18187 {
18188   tree c, t;
18189
18190   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18191     return list;
18192
18193   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18194
18195   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18196     {
18197       tree id = cp_lexer_peek_token (parser->lexer)->value;
18198       const char *p = IDENTIFIER_POINTER (id);
18199
18200       switch (p[0])
18201         {
18202         case 'd':
18203           if (strcmp ("dynamic", p) != 0)
18204             goto invalid_kind;
18205           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18206           break;
18207
18208         case 'g':
18209           if (strcmp ("guided", p) != 0)
18210             goto invalid_kind;
18211           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18212           break;
18213
18214         case 'r':
18215           if (strcmp ("runtime", p) != 0)
18216             goto invalid_kind;
18217           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18218           break;
18219
18220         default:
18221           goto invalid_kind;
18222         }
18223     }
18224   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18225     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18226   else
18227     goto invalid_kind;
18228   cp_lexer_consume_token (parser->lexer);
18229
18230   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18231     {
18232       cp_lexer_consume_token (parser->lexer);
18233
18234       t = cp_parser_assignment_expression (parser, false);
18235
18236       if (t == error_mark_node)
18237         goto resync_fail;
18238       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18239         error ("schedule %<runtime%> does not take "
18240                "a %<chunk_size%> parameter");
18241       else
18242         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18243
18244       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18245         goto resync_fail;
18246     }
18247   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18248     goto resync_fail;
18249
18250   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18251   OMP_CLAUSE_CHAIN (c) = list;
18252   return c;
18253
18254  invalid_kind:
18255   cp_parser_error (parser, "invalid schedule kind");
18256  resync_fail:
18257   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18258                                          /*or_comma=*/false,
18259                                          /*consume_paren=*/true);
18260   return list;
18261 }
18262
18263 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18264    is a bitmask in MASK.  Return the list of clauses found; the result
18265    of clause default goes in *pdefault.  */
18266
18267 static tree
18268 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18269                            const char *where, cp_token *pragma_tok)
18270 {
18271   tree clauses = NULL;
18272
18273   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18274     {
18275       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18276       const char *c_name;
18277       tree prev = clauses;
18278
18279       switch (c_kind)
18280         {
18281         case PRAGMA_OMP_CLAUSE_COPYIN:
18282           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18283           c_name = "copyin";
18284           break;
18285         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18286           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18287                                             clauses);
18288           c_name = "copyprivate";
18289           break;
18290         case PRAGMA_OMP_CLAUSE_DEFAULT:
18291           clauses = cp_parser_omp_clause_default (parser, clauses);
18292           c_name = "default";
18293           break;
18294         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18295           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18296                                             clauses);
18297           c_name = "firstprivate";
18298           break;
18299         case PRAGMA_OMP_CLAUSE_IF:
18300           clauses = cp_parser_omp_clause_if (parser, clauses);
18301           c_name = "if";
18302           break;
18303         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18304           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18305                                             clauses);
18306           c_name = "lastprivate";
18307           break;
18308         case PRAGMA_OMP_CLAUSE_NOWAIT:
18309           clauses = cp_parser_omp_clause_nowait (parser, clauses);
18310           c_name = "nowait";
18311           break;
18312         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18313           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18314           c_name = "num_threads";
18315           break;
18316         case PRAGMA_OMP_CLAUSE_ORDERED:
18317           clauses = cp_parser_omp_clause_ordered (parser, clauses);
18318           c_name = "ordered";
18319           break;
18320         case PRAGMA_OMP_CLAUSE_PRIVATE:
18321           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18322                                             clauses);
18323           c_name = "private";
18324           break;
18325         case PRAGMA_OMP_CLAUSE_REDUCTION:
18326           clauses = cp_parser_omp_clause_reduction (parser, clauses);
18327           c_name = "reduction";
18328           break;
18329         case PRAGMA_OMP_CLAUSE_SCHEDULE:
18330           clauses = cp_parser_omp_clause_schedule (parser, clauses);
18331           c_name = "schedule";
18332           break;
18333         case PRAGMA_OMP_CLAUSE_SHARED:
18334           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18335                                             clauses);
18336           c_name = "shared";
18337           break;
18338         default:
18339           cp_parser_error (parser, "expected %<#pragma omp%> clause");
18340           goto saw_error;
18341         }
18342
18343       if (((mask >> c_kind) & 1) == 0)
18344         {
18345           /* Remove the invalid clause(s) from the list to avoid
18346              confusing the rest of the compiler.  */
18347           clauses = prev;
18348           error ("%qs is not valid for %qs", c_name, where);
18349         }
18350     }
18351  saw_error:
18352   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18353   return finish_omp_clauses (clauses);
18354 }
18355
18356 /* OpenMP 2.5:
18357    structured-block:
18358      statement
18359
18360    In practice, we're also interested in adding the statement to an
18361    outer node.  So it is convenient if we work around the fact that
18362    cp_parser_statement calls add_stmt.  */
18363
18364 static unsigned
18365 cp_parser_begin_omp_structured_block (cp_parser *parser)
18366 {
18367   unsigned save = parser->in_statement;
18368
18369   /* Only move the values to IN_OMP_BLOCK if they weren't false.
18370      This preserves the "not within loop or switch" style error messages
18371      for nonsense cases like
18372         void foo() {
18373         #pragma omp single
18374           break;
18375         }
18376   */
18377   if (parser->in_statement)
18378     parser->in_statement = IN_OMP_BLOCK;
18379
18380   return save;
18381 }
18382
18383 static void
18384 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18385 {
18386   parser->in_statement = save;
18387 }
18388
18389 static tree
18390 cp_parser_omp_structured_block (cp_parser *parser)
18391 {
18392   tree stmt = begin_omp_structured_block ();
18393   unsigned int save = cp_parser_begin_omp_structured_block (parser);
18394
18395   cp_parser_statement (parser, NULL_TREE, false);
18396
18397   cp_parser_end_omp_structured_block (parser, save);
18398   return finish_omp_structured_block (stmt);
18399 }
18400
18401 /* OpenMP 2.5:
18402    # pragma omp atomic new-line
18403      expression-stmt
18404
18405    expression-stmt:
18406      x binop= expr | x++ | ++x | x-- | --x
18407    binop:
18408      +, *, -, /, &, ^, |, <<, >>
18409
18410   where x is an lvalue expression with scalar type.  */
18411
18412 static void
18413 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18414 {
18415   tree lhs, rhs;
18416   enum tree_code code;
18417
18418   cp_parser_require_pragma_eol (parser, pragma_tok);
18419
18420   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18421                                     /*cast_p=*/false);
18422   switch (TREE_CODE (lhs))
18423     {
18424     case ERROR_MARK:
18425       goto saw_error;
18426
18427     case PREINCREMENT_EXPR:
18428     case POSTINCREMENT_EXPR:
18429       lhs = TREE_OPERAND (lhs, 0);
18430       code = PLUS_EXPR;
18431       rhs = integer_one_node;
18432       break;
18433
18434     case PREDECREMENT_EXPR:
18435     case POSTDECREMENT_EXPR:
18436       lhs = TREE_OPERAND (lhs, 0);
18437       code = MINUS_EXPR;
18438       rhs = integer_one_node;
18439       break;
18440
18441     default:
18442       switch (cp_lexer_peek_token (parser->lexer)->type)
18443         {
18444         case CPP_MULT_EQ:
18445           code = MULT_EXPR;
18446           break;
18447         case CPP_DIV_EQ:
18448           code = TRUNC_DIV_EXPR;
18449           break;
18450         case CPP_PLUS_EQ:
18451           code = PLUS_EXPR;
18452           break;
18453         case CPP_MINUS_EQ:
18454           code = MINUS_EXPR;
18455           break;
18456         case CPP_LSHIFT_EQ:
18457           code = LSHIFT_EXPR;
18458           break;
18459         case CPP_RSHIFT_EQ:
18460           code = RSHIFT_EXPR;
18461           break;
18462         case CPP_AND_EQ:
18463           code = BIT_AND_EXPR;
18464           break;
18465         case CPP_OR_EQ:
18466           code = BIT_IOR_EXPR;
18467           break;
18468         case CPP_XOR_EQ:
18469           code = BIT_XOR_EXPR;
18470           break;
18471         default:
18472           cp_parser_error (parser,
18473                            "invalid operator for %<#pragma omp atomic%>");
18474           goto saw_error;
18475         }
18476       cp_lexer_consume_token (parser->lexer);
18477
18478       rhs = cp_parser_expression (parser, false);
18479       if (rhs == error_mark_node)
18480         goto saw_error;
18481       break;
18482     }
18483   finish_omp_atomic (code, lhs, rhs);
18484   cp_parser_consume_semicolon_at_end_of_statement (parser);
18485   return;
18486
18487  saw_error:
18488   cp_parser_skip_to_end_of_block_or_statement (parser);
18489 }
18490
18491
18492 /* OpenMP 2.5:
18493    # pragma omp barrier new-line  */
18494
18495 static void
18496 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18497 {
18498   cp_parser_require_pragma_eol (parser, pragma_tok);
18499   finish_omp_barrier ();
18500 }
18501
18502 /* OpenMP 2.5:
18503    # pragma omp critical [(name)] new-line
18504      structured-block  */
18505
18506 static tree
18507 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18508 {
18509   tree stmt, name = NULL;
18510
18511   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18512     {
18513       cp_lexer_consume_token (parser->lexer);
18514
18515       name = cp_parser_identifier (parser);
18516
18517       if (name == error_mark_node
18518           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18519         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18520                                                /*or_comma=*/false,
18521                                                /*consume_paren=*/true);
18522       if (name == error_mark_node)
18523         name = NULL;
18524     }
18525   cp_parser_require_pragma_eol (parser, pragma_tok);
18526
18527   stmt = cp_parser_omp_structured_block (parser);
18528   return c_finish_omp_critical (stmt, name);
18529 }
18530
18531 /* OpenMP 2.5:
18532    # pragma omp flush flush-vars[opt] new-line
18533
18534    flush-vars:
18535      ( variable-list ) */
18536
18537 static void
18538 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18539 {
18540   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18541     (void) cp_parser_omp_var_list (parser, 0, NULL);
18542   cp_parser_require_pragma_eol (parser, pragma_tok);
18543
18544   finish_omp_flush ();
18545 }
18546
18547 /* Parse the restricted form of the for statment allowed by OpenMP.  */
18548
18549 static tree
18550 cp_parser_omp_for_loop (cp_parser *parser)
18551 {
18552   tree init, cond, incr, body, decl, pre_body;
18553   location_t loc;
18554
18555   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18556     {
18557       cp_parser_error (parser, "for statement expected");
18558       return NULL;
18559     }
18560   loc = cp_lexer_consume_token (parser->lexer)->location;
18561   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18562     return NULL;
18563
18564   init = decl = NULL;
18565   pre_body = push_stmt_list ();
18566   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18567     {
18568       cp_decl_specifier_seq type_specifiers;
18569
18570       /* First, try to parse as an initialized declaration.  See
18571          cp_parser_condition, from whence the bulk of this is copied.  */
18572
18573       cp_parser_parse_tentatively (parser);
18574       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18575                                     &type_specifiers);
18576       if (!cp_parser_error_occurred (parser))
18577         {
18578           tree asm_specification, attributes;
18579           cp_declarator *declarator;
18580
18581           declarator = cp_parser_declarator (parser,
18582                                              CP_PARSER_DECLARATOR_NAMED,
18583                                              /*ctor_dtor_or_conv_p=*/NULL,
18584                                              /*parenthesized_p=*/NULL,
18585                                              /*member_p=*/false);
18586           attributes = cp_parser_attributes_opt (parser);
18587           asm_specification = cp_parser_asm_specification_opt (parser);
18588
18589           cp_parser_require (parser, CPP_EQ, "`='");
18590           if (cp_parser_parse_definitely (parser))
18591             {
18592               tree pushed_scope;
18593
18594               decl = start_decl (declarator, &type_specifiers,
18595                                  /*initialized_p=*/false, attributes,
18596                                  /*prefix_attributes=*/NULL_TREE,
18597                                  &pushed_scope);
18598
18599               init = cp_parser_assignment_expression (parser, false);
18600
18601               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18602                               asm_specification, LOOKUP_ONLYCONVERTING);
18603
18604               if (pushed_scope)
18605                 pop_scope (pushed_scope);
18606             }
18607         }
18608       else
18609         cp_parser_abort_tentative_parse (parser);
18610
18611       /* If parsing as an initialized declaration failed, try again as
18612          a simple expression.  */
18613       if (decl == NULL)
18614         init = cp_parser_expression (parser, false);
18615     }
18616   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18617   pre_body = pop_stmt_list (pre_body);
18618
18619   cond = NULL;
18620   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18621     cond = cp_parser_condition (parser);
18622   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18623
18624   incr = NULL;
18625   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18626     incr = cp_parser_expression (parser, false);
18627
18628   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18629     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18630                                            /*or_comma=*/false,
18631                                            /*consume_paren=*/true);
18632
18633   /* Note that we saved the original contents of this flag when we entered
18634      the structured block, and so we don't need to re-save it here.  */
18635   parser->in_statement = IN_OMP_FOR;
18636
18637   /* Note that the grammar doesn't call for a structured block here,
18638      though the loop as a whole is a structured block.  */
18639   body = push_stmt_list ();
18640   cp_parser_statement (parser, NULL_TREE, false);
18641   body = pop_stmt_list (body);
18642
18643   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18644 }
18645
18646 /* OpenMP 2.5:
18647    #pragma omp for for-clause[optseq] new-line
18648      for-loop  */
18649
18650 #define OMP_FOR_CLAUSE_MASK                             \
18651         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18652         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18653         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18654         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18655         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
18656         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
18657         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18658
18659 static tree
18660 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18661 {
18662   tree clauses, sb, ret;
18663   unsigned int save;
18664
18665   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18666                                        "#pragma omp for", pragma_tok);
18667
18668   sb = begin_omp_structured_block ();
18669   save = cp_parser_begin_omp_structured_block (parser);
18670
18671   ret = cp_parser_omp_for_loop (parser);
18672   if (ret)
18673     OMP_FOR_CLAUSES (ret) = clauses;
18674
18675   cp_parser_end_omp_structured_block (parser, save);
18676   add_stmt (finish_omp_structured_block (sb));
18677
18678   return ret;
18679 }
18680
18681 /* OpenMP 2.5:
18682    # pragma omp master new-line
18683      structured-block  */
18684
18685 static tree
18686 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18687 {
18688   cp_parser_require_pragma_eol (parser, pragma_tok);
18689   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18690 }
18691
18692 /* OpenMP 2.5:
18693    # pragma omp ordered new-line
18694      structured-block  */
18695
18696 static tree
18697 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18698 {
18699   cp_parser_require_pragma_eol (parser, pragma_tok);
18700   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18701 }
18702
18703 /* OpenMP 2.5:
18704
18705    section-scope:
18706      { section-sequence }
18707
18708    section-sequence:
18709      section-directive[opt] structured-block
18710      section-sequence section-directive structured-block  */
18711
18712 static tree
18713 cp_parser_omp_sections_scope (cp_parser *parser)
18714 {
18715   tree stmt, substmt;
18716   bool error_suppress = false;
18717   cp_token *tok;
18718
18719   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18720     return NULL_TREE;
18721
18722   stmt = push_stmt_list ();
18723
18724   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18725     {
18726       unsigned save;
18727
18728       substmt = begin_omp_structured_block ();
18729       save = cp_parser_begin_omp_structured_block (parser);
18730
18731       while (1)
18732         {
18733           cp_parser_statement (parser, NULL_TREE, false);
18734
18735           tok = cp_lexer_peek_token (parser->lexer);
18736           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18737             break;
18738           if (tok->type == CPP_CLOSE_BRACE)
18739             break;
18740           if (tok->type == CPP_EOF)
18741             break;
18742         }
18743
18744       cp_parser_end_omp_structured_block (parser, save);
18745       substmt = finish_omp_structured_block (substmt);
18746       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18747       add_stmt (substmt);
18748     }
18749
18750   while (1)
18751     {
18752       tok = cp_lexer_peek_token (parser->lexer);
18753       if (tok->type == CPP_CLOSE_BRACE)
18754         break;
18755       if (tok->type == CPP_EOF)
18756         break;
18757
18758       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18759         {
18760           cp_lexer_consume_token (parser->lexer);
18761           cp_parser_require_pragma_eol (parser, tok);
18762           error_suppress = false;
18763         }
18764       else if (!error_suppress)
18765         {
18766           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18767           error_suppress = true;
18768         }
18769
18770       substmt = cp_parser_omp_structured_block (parser);
18771       substmt = build1 (OMP_SECTION, void_type_node, substmt);
18772       add_stmt (substmt);
18773     }
18774   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18775
18776   substmt = pop_stmt_list (stmt);
18777
18778   stmt = make_node (OMP_SECTIONS);
18779   TREE_TYPE (stmt) = void_type_node;
18780   OMP_SECTIONS_BODY (stmt) = substmt;
18781
18782   add_stmt (stmt);
18783   return stmt;
18784 }
18785
18786 /* OpenMP 2.5:
18787    # pragma omp sections sections-clause[optseq] newline
18788      sections-scope  */
18789
18790 #define OMP_SECTIONS_CLAUSE_MASK                        \
18791         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18792         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18793         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18794         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18795         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18796
18797 static tree
18798 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18799 {
18800   tree clauses, ret;
18801
18802   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18803                                        "#pragma omp sections", pragma_tok);
18804
18805   ret = cp_parser_omp_sections_scope (parser);
18806   if (ret)
18807     OMP_SECTIONS_CLAUSES (ret) = clauses;
18808
18809   return ret;
18810 }
18811
18812 /* OpenMP 2.5:
18813    # pragma parallel parallel-clause new-line
18814    # pragma parallel for parallel-for-clause new-line
18815    # pragma parallel sections parallel-sections-clause new-line  */
18816
18817 #define OMP_PARALLEL_CLAUSE_MASK                        \
18818         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
18819         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18820         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18821         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
18822         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
18823         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
18824         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18825         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18826
18827 static tree
18828 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18829 {
18830   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18831   const char *p_name = "#pragma omp parallel";
18832   tree stmt, clauses, par_clause, ws_clause, block;
18833   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18834   unsigned int save;
18835
18836   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18837     {
18838       cp_lexer_consume_token (parser->lexer);
18839       p_kind = PRAGMA_OMP_PARALLEL_FOR;
18840       p_name = "#pragma omp parallel for";
18841       mask |= OMP_FOR_CLAUSE_MASK;
18842       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18843     }
18844   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18845     {
18846       tree id = cp_lexer_peek_token (parser->lexer)->value;
18847       const char *p = IDENTIFIER_POINTER (id);
18848       if (strcmp (p, "sections") == 0)
18849         {
18850           cp_lexer_consume_token (parser->lexer);
18851           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18852           p_name = "#pragma omp parallel sections";
18853           mask |= OMP_SECTIONS_CLAUSE_MASK;
18854           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18855         }
18856     }
18857
18858   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18859   block = begin_omp_parallel ();
18860   save = cp_parser_begin_omp_structured_block (parser);
18861
18862   switch (p_kind)
18863     {
18864     case PRAGMA_OMP_PARALLEL:
18865       cp_parser_already_scoped_statement (parser);
18866       par_clause = clauses;
18867       break;
18868
18869     case PRAGMA_OMP_PARALLEL_FOR:
18870       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18871       stmt = cp_parser_omp_for_loop (parser);
18872       if (stmt)
18873         OMP_FOR_CLAUSES (stmt) = ws_clause;
18874       break;
18875
18876     case PRAGMA_OMP_PARALLEL_SECTIONS:
18877       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18878       stmt = cp_parser_omp_sections_scope (parser);
18879       if (stmt)
18880         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18881       break;
18882
18883     default:
18884       gcc_unreachable ();
18885     }
18886
18887   cp_parser_end_omp_structured_block (parser, save);
18888   stmt = finish_omp_parallel (par_clause, block);
18889   if (p_kind != PRAGMA_OMP_PARALLEL)
18890     OMP_PARALLEL_COMBINED (stmt) = 1;
18891   return stmt;
18892 }
18893
18894 /* OpenMP 2.5:
18895    # pragma omp single single-clause[optseq] new-line
18896      structured-block  */
18897
18898 #define OMP_SINGLE_CLAUSE_MASK                          \
18899         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18900         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18901         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
18902         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18903
18904 static tree
18905 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18906 {
18907   tree stmt = make_node (OMP_SINGLE);
18908   TREE_TYPE (stmt) = void_type_node;
18909
18910   OMP_SINGLE_CLAUSES (stmt)
18911     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18912                                  "#pragma omp single", pragma_tok);
18913   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18914
18915   return add_stmt (stmt);
18916 }
18917
18918 /* OpenMP 2.5:
18919    # pragma omp threadprivate (variable-list) */
18920
18921 static void
18922 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18923 {
18924   tree vars;
18925
18926   vars = cp_parser_omp_var_list (parser, 0, NULL);
18927   cp_parser_require_pragma_eol (parser, pragma_tok);
18928
18929   if (!targetm.have_tls)
18930     sorry ("threadprivate variables not supported in this target");
18931
18932   finish_omp_threadprivate (vars);
18933 }
18934
18935 /* Main entry point to OpenMP statement pragmas.  */
18936
18937 static void
18938 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18939 {
18940   tree stmt;
18941
18942   switch (pragma_tok->pragma_kind)
18943     {
18944     case PRAGMA_OMP_ATOMIC:
18945       cp_parser_omp_atomic (parser, pragma_tok);
18946       return;
18947     case PRAGMA_OMP_CRITICAL:
18948       stmt = cp_parser_omp_critical (parser, pragma_tok);
18949       break;
18950     case PRAGMA_OMP_FOR:
18951       stmt = cp_parser_omp_for (parser, pragma_tok);
18952       break;
18953     case PRAGMA_OMP_MASTER:
18954       stmt = cp_parser_omp_master (parser, pragma_tok);
18955       break;
18956     case PRAGMA_OMP_ORDERED:
18957       stmt = cp_parser_omp_ordered (parser, pragma_tok);
18958       break;
18959     case PRAGMA_OMP_PARALLEL:
18960       stmt = cp_parser_omp_parallel (parser, pragma_tok);
18961       break;
18962     case PRAGMA_OMP_SECTIONS:
18963       stmt = cp_parser_omp_sections (parser, pragma_tok);
18964       break;
18965     case PRAGMA_OMP_SINGLE:
18966       stmt = cp_parser_omp_single (parser, pragma_tok);
18967       break;
18968     default:
18969       gcc_unreachable ();
18970     }
18971
18972   if (stmt)
18973     SET_EXPR_LOCATION (stmt, pragma_tok->location);
18974 }
18975 \f
18976 /* The parser.  */
18977
18978 static GTY (()) cp_parser *the_parser;
18979
18980 \f
18981 /* Special handling for the first token or line in the file.  The first
18982    thing in the file might be #pragma GCC pch_preprocess, which loads a
18983    PCH file, which is a GC collection point.  So we need to handle this
18984    first pragma without benefit of an existing lexer structure.
18985
18986    Always returns one token to the caller in *FIRST_TOKEN.  This is
18987    either the true first token of the file, or the first token after
18988    the initial pragma.  */
18989
18990 static void
18991 cp_parser_initial_pragma (cp_token *first_token)
18992 {
18993   tree name = NULL;
18994
18995   cp_lexer_get_preprocessor_token (NULL, first_token);
18996   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18997     return;
18998
18999   cp_lexer_get_preprocessor_token (NULL, first_token);
19000   if (first_token->type == CPP_STRING)
19001     {
19002       name = first_token->value;
19003
19004       cp_lexer_get_preprocessor_token (NULL, first_token);
19005       if (first_token->type != CPP_PRAGMA_EOL)
19006         error ("junk at end of %<#pragma GCC pch_preprocess%>");
19007     }
19008   else
19009     error ("expected string literal");
19010
19011   /* Skip to the end of the pragma.  */
19012   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19013     cp_lexer_get_preprocessor_token (NULL, first_token);
19014
19015   /* Now actually load the PCH file.  */
19016   if (name)
19017     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19018
19019   /* Read one more token to return to our caller.  We have to do this
19020      after reading the PCH file in, since its pointers have to be
19021      live.  */
19022   cp_lexer_get_preprocessor_token (NULL, first_token);
19023 }
19024
19025 /* Normal parsing of a pragma token.  Here we can (and must) use the
19026    regular lexer.  */
19027
19028 static bool
19029 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19030 {
19031   cp_token *pragma_tok;
19032   unsigned int id;
19033
19034   pragma_tok = cp_lexer_consume_token (parser->lexer);
19035   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19036   parser->lexer->in_pragma = true;
19037
19038   id = pragma_tok->pragma_kind;
19039   switch (id)
19040     {
19041     case PRAGMA_GCC_PCH_PREPROCESS:
19042       error ("%<#pragma GCC pch_preprocess%> must be first");
19043       break;
19044
19045     case PRAGMA_OMP_BARRIER:
19046       switch (context)
19047         {
19048         case pragma_compound:
19049           cp_parser_omp_barrier (parser, pragma_tok);
19050           return false;
19051         case pragma_stmt:
19052           error ("%<#pragma omp barrier%> may only be "
19053                  "used in compound statements");
19054           break;
19055         default:
19056           goto bad_stmt;
19057         }
19058       break;
19059
19060     case PRAGMA_OMP_FLUSH:
19061       switch (context)
19062         {
19063         case pragma_compound:
19064           cp_parser_omp_flush (parser, pragma_tok);
19065           return false;
19066         case pragma_stmt:
19067           error ("%<#pragma omp flush%> may only be "
19068                  "used in compound statements");
19069           break;
19070         default:
19071           goto bad_stmt;
19072         }
19073       break;
19074
19075     case PRAGMA_OMP_THREADPRIVATE:
19076       cp_parser_omp_threadprivate (parser, pragma_tok);
19077       return false;
19078
19079     case PRAGMA_OMP_ATOMIC:
19080     case PRAGMA_OMP_CRITICAL:
19081     case PRAGMA_OMP_FOR:
19082     case PRAGMA_OMP_MASTER:
19083     case PRAGMA_OMP_ORDERED:
19084     case PRAGMA_OMP_PARALLEL:
19085     case PRAGMA_OMP_SECTIONS:
19086     case PRAGMA_OMP_SINGLE:
19087       if (context == pragma_external)
19088         goto bad_stmt;
19089       cp_parser_omp_construct (parser, pragma_tok);
19090       return true;
19091
19092     case PRAGMA_OMP_SECTION:
19093       error ("%<#pragma omp section%> may only be used in "
19094              "%<#pragma omp sections%> construct");
19095       break;
19096
19097     default:
19098       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19099       c_invoke_pragma_handler (id);
19100       break;
19101
19102     bad_stmt:
19103       cp_parser_error (parser, "expected declaration specifiers");
19104       break;
19105     }
19106
19107   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19108   return false;
19109 }
19110
19111 /* The interface the pragma parsers have to the lexer.  */
19112
19113 enum cpp_ttype
19114 pragma_lex (tree *value)
19115 {
19116   cp_token *tok;
19117   enum cpp_ttype ret;
19118
19119   tok = cp_lexer_peek_token (the_parser->lexer);
19120
19121   ret = tok->type;
19122   *value = tok->value;
19123
19124   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19125     ret = CPP_EOF;
19126   else if (ret == CPP_STRING)
19127     *value = cp_parser_string_literal (the_parser, false, false);
19128   else
19129     {
19130       cp_lexer_consume_token (the_parser->lexer);
19131       if (ret == CPP_KEYWORD)
19132         ret = CPP_NAME;
19133     }
19134
19135   return ret;
19136 }
19137
19138 \f
19139 /* External interface.  */
19140
19141 /* Parse one entire translation unit.  */
19142
19143 void
19144 c_parse_file (void)
19145 {
19146   bool error_occurred;
19147   static bool already_called = false;
19148
19149   if (already_called)
19150     {
19151       sorry ("inter-module optimizations not implemented for C++");
19152       return;
19153     }
19154   already_called = true;
19155
19156   the_parser = cp_parser_new ();
19157   push_deferring_access_checks (flag_access_control
19158                                 ? dk_no_deferred : dk_no_check);
19159   error_occurred = cp_parser_translation_unit (the_parser);
19160   the_parser = NULL;
19161 }
19162
19163 /* This variable must be provided by every front end.  */
19164
19165 int yydebug;
19166
19167 #include "gt-cp-parser.h"