68ce052612a2e6e3ff2ae2798e0704c529be26f5
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "output.h"
35 #include "target.h"
36 #include "cgraph.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "plugin.h"
40 #include "tree-pretty-print.h"
41 #include "parser.h"
42
43 \f
44 /* The lexer. */
45
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
48
49 static cp_token eof_token =
50 {
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
52 };
53
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR
109 } non_integral_constant;
110
111 /* The various kinds of errors about name-lookup failing. */
112 typedef enum name_lookup_error {
113 /* NULL */
114 NLE_NULL,
115 /* is not a type */
116 NLE_TYPE,
117 /* is not a class or namespace */
118 NLE_CXX98,
119 /* is not a class, namespace, or enumeration */
120 NLE_NOT_CXX98
121 } name_lookup_error;
122
123 /* The various kinds of required token */
124 typedef enum required_token {
125 RT_NONE,
126 RT_SEMICOLON, /* ';' */
127 RT_OPEN_PAREN, /* '(' */
128 RT_CLOSE_BRACE, /* '}' */
129 RT_OPEN_BRACE, /* '{' */
130 RT_CLOSE_SQUARE, /* ']' */
131 RT_OPEN_SQUARE, /* '[' */
132 RT_COMMA, /* ',' */
133 RT_SCOPE, /* '::' */
134 RT_LESS, /* '<' */
135 RT_GREATER, /* '>' */
136 RT_EQ, /* '=' */
137 RT_ELLIPSIS, /* '...' */
138 RT_MULT, /* '*' */
139 RT_COMPL, /* '~' */
140 RT_COLON, /* ':' */
141 RT_COLON_SCOPE, /* ':' or '::' */
142 RT_CLOSE_PAREN, /* ')' */
143 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
144 RT_PRAGMA_EOL, /* end of line */
145 RT_NAME, /* identifier */
146
147 /* The type is CPP_KEYWORD */
148 RT_NEW, /* new */
149 RT_DELETE, /* delete */
150 RT_RETURN, /* return */
151 RT_WHILE, /* while */
152 RT_EXTERN, /* extern */
153 RT_STATIC_ASSERT, /* static_assert */
154 RT_DECLTYPE, /* decltype */
155 RT_OPERATOR, /* operator */
156 RT_CLASS, /* class */
157 RT_TEMPLATE, /* template */
158 RT_NAMESPACE, /* namespace */
159 RT_USING, /* using */
160 RT_ASM, /* asm */
161 RT_TRY, /* try */
162 RT_CATCH, /* catch */
163 RT_THROW, /* throw */
164 RT_LABEL, /* __label__ */
165 RT_AT_TRY, /* @try */
166 RT_AT_SYNCHRONIZED, /* @synchronized */
167 RT_AT_THROW, /* @throw */
168
169 RT_SELECT, /* selection-statement */
170 RT_INTERATION, /* iteration-statement */
171 RT_JUMP, /* jump-statement */
172 RT_CLASS_KEY, /* class-key */
173 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
174 } required_token;
175
176 /* Prototypes. */
177
178 static cp_lexer *cp_lexer_new_main
179 (void);
180 static cp_lexer *cp_lexer_new_from_tokens
181 (cp_token_cache *tokens);
182 static void cp_lexer_destroy
183 (cp_lexer *);
184 static int cp_lexer_saving_tokens
185 (const cp_lexer *);
186 static cp_token *cp_lexer_token_at
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_get_preprocessor_token
189 (cp_lexer *, cp_token *);
190 static inline cp_token *cp_lexer_peek_token
191 (cp_lexer *);
192 static cp_token *cp_lexer_peek_nth_token
193 (cp_lexer *, size_t);
194 static inline bool cp_lexer_next_token_is
195 (cp_lexer *, enum cpp_ttype);
196 static bool cp_lexer_next_token_is_not
197 (cp_lexer *, enum cpp_ttype);
198 static bool cp_lexer_next_token_is_keyword
199 (cp_lexer *, enum rid);
200 static cp_token *cp_lexer_consume_token
201 (cp_lexer *);
202 static void cp_lexer_purge_token
203 (cp_lexer *);
204 static void cp_lexer_purge_tokens_after
205 (cp_lexer *, cp_token_position);
206 static void cp_lexer_save_tokens
207 (cp_lexer *);
208 static void cp_lexer_commit_tokens
209 (cp_lexer *);
210 static void cp_lexer_rollback_tokens
211 (cp_lexer *);
212 #ifdef ENABLE_CHECKING
213 static void cp_lexer_print_token
214 (FILE *, cp_token *);
215 static inline bool cp_lexer_debugging_p
216 (cp_lexer *);
217 static void cp_lexer_start_debugging
218 (cp_lexer *) ATTRIBUTE_UNUSED;
219 static void cp_lexer_stop_debugging
220 (cp_lexer *) ATTRIBUTE_UNUSED;
221 #else
222 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
223 about passing NULL to functions that require non-NULL arguments
224 (fputs, fprintf). It will never be used, so all we need is a value
225 of the right type that's guaranteed not to be NULL. */
226 #define cp_lexer_debug_stream stdout
227 #define cp_lexer_print_token(str, tok) (void) 0
228 #define cp_lexer_debugging_p(lexer) 0
229 #endif /* ENABLE_CHECKING */
230
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
233
234 static void cp_parser_initial_pragma
235 (cp_token *);
236
237 /* Manifest constants. */
238 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
239 #define CP_SAVED_TOKEN_STACK 5
240
241 /* Variables. */
242
243 #ifdef ENABLE_CHECKING
244 /* The stream to which debugging output should be written. */
245 static FILE *cp_lexer_debug_stream;
246 #endif /* ENABLE_CHECKING */
247
248 /* Nonzero if we are parsing an unevaluated operand: an operand to
249 sizeof, typeof, or alignof. */
250 int cp_unevaluated_operand;
251
252 #ifdef ENABLE_CHECKING
253 /* Dump up to NUM tokens in BUFFER to FILE. If NUM is 0, dump all the
254 tokens. */
255
256 void
257 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
258 {
259 unsigned i;
260 cp_token *token;
261
262 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
263
264 if (num == 0)
265 num = VEC_length (cp_token, buffer);
266
267 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
268 {
269 cp_lexer_print_token (file, token);
270 switch (token->type)
271 {
272 case CPP_SEMICOLON:
273 case CPP_OPEN_BRACE:
274 case CPP_CLOSE_BRACE:
275 case CPP_EOF:
276 fputc ('\n', file);
277 break;
278
279 default:
280 fputc (' ', file);
281 }
282 }
283
284 if (i == num && i < VEC_length (cp_token, buffer))
285 {
286 fprintf (file, " ... ");
287 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
288 VEC_length (cp_token, buffer) - 1));
289 }
290
291 fprintf (file, "\n");
292 }
293
294
295 /* Dump all tokens in BUFFER to stderr. */
296
297 void
298 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
299 {
300 cp_lexer_dump_tokens (stderr, buffer, 0);
301 }
302 #endif
303
304
305 /* Allocate memory for a new lexer object and return it. */
306
307 static cp_lexer *
308 cp_lexer_alloc (void)
309 {
310 cp_lexer *lexer;
311
312 c_common_no_more_pch ();
313
314 /* Allocate the memory. */
315 lexer = ggc_alloc_cleared_cp_lexer ();
316
317 #ifdef ENABLE_CHECKING
318 /* Initially we are not debugging. */
319 lexer->debugging_p = false;
320 #endif /* ENABLE_CHECKING */
321 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
322 CP_SAVED_TOKEN_STACK);
323
324 /* Create the buffer. */
325 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
326
327 return lexer;
328 }
329
330
331 /* Create a new main C++ lexer, the lexer that gets tokens from the
332 preprocessor. */
333
334 static cp_lexer *
335 cp_lexer_new_main (void)
336 {
337 cp_lexer *lexer;
338 cp_token token;
339
340 /* It's possible that parsing the first pragma will load a PCH file,
341 which is a GC collection point. So we have to do that before
342 allocating any memory. */
343 cp_parser_initial_pragma (&token);
344
345 lexer = cp_lexer_alloc ();
346
347 /* Put the first token in the buffer. */
348 VEC_quick_push (cp_token, lexer->buffer, &token);
349
350 /* Get the remaining tokens from the preprocessor. */
351 while (token.type != CPP_EOF)
352 {
353 cp_lexer_get_preprocessor_token (lexer, &token);
354 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
355 }
356
357 lexer->last_token = VEC_address (cp_token, lexer->buffer)
358 + VEC_length (cp_token, lexer->buffer)
359 - 1;
360 lexer->next_token = VEC_length (cp_token, lexer->buffer)
361 ? VEC_address (cp_token, lexer->buffer)
362 : &eof_token;
363
364 /* Subsequent preprocessor diagnostics should use compiler
365 diagnostic functions to get the compiler source location. */
366 done_lexing = true;
367
368 gcc_assert (!lexer->next_token->purged_p);
369 return lexer;
370 }
371
372 /* Create a new lexer whose token stream is primed with the tokens in
373 CACHE. When these tokens are exhausted, no new tokens will be read. */
374
375 static cp_lexer *
376 cp_lexer_new_from_tokens (cp_token_cache *cache)
377 {
378 cp_token *first = cache->first;
379 cp_token *last = cache->last;
380 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
381
382 /* We do not own the buffer. */
383 lexer->buffer = NULL;
384 lexer->next_token = first == last ? &eof_token : first;
385 lexer->last_token = last;
386
387 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
388 CP_SAVED_TOKEN_STACK);
389
390 #ifdef ENABLE_CHECKING
391 /* Initially we are not debugging. */
392 lexer->debugging_p = false;
393 #endif
394
395 gcc_assert (!lexer->next_token->purged_p);
396 return lexer;
397 }
398
399 /* Frees all resources associated with LEXER. */
400
401 static void
402 cp_lexer_destroy (cp_lexer *lexer)
403 {
404 VEC_free (cp_token, gc, lexer->buffer);
405 VEC_free (cp_token_position, heap, lexer->saved_tokens);
406 ggc_free (lexer);
407 }
408
409 /* Returns nonzero if debugging information should be output. */
410
411 #ifdef ENABLE_CHECKING
412
413 static inline bool
414 cp_lexer_debugging_p (cp_lexer *lexer)
415 {
416 return lexer->debugging_p;
417 }
418
419 #endif /* ENABLE_CHECKING */
420
421 static inline cp_token_position
422 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
423 {
424 gcc_assert (!previous_p || lexer->next_token != &eof_token);
425
426 return lexer->next_token - previous_p;
427 }
428
429 static inline cp_token *
430 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
431 {
432 return pos;
433 }
434
435 static inline void
436 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
437 {
438 lexer->next_token = cp_lexer_token_at (lexer, pos);
439 }
440
441 static inline cp_token_position
442 cp_lexer_previous_token_position (cp_lexer *lexer)
443 {
444 if (lexer->next_token == &eof_token)
445 return lexer->last_token - 1;
446 else
447 return cp_lexer_token_position (lexer, true);
448 }
449
450 static inline cp_token *
451 cp_lexer_previous_token (cp_lexer *lexer)
452 {
453 cp_token_position tp = cp_lexer_previous_token_position (lexer);
454
455 return cp_lexer_token_at (lexer, tp);
456 }
457
458 /* nonzero if we are presently saving tokens. */
459
460 static inline int
461 cp_lexer_saving_tokens (const cp_lexer* lexer)
462 {
463 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
464 }
465
466 /* Store the next token from the preprocessor in *TOKEN. Return true
467 if we reach EOF. If LEXER is NULL, assume we are handling an
468 initial #pragma pch_preprocess, and thus want the lexer to return
469 processed strings. */
470
471 static void
472 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
473 {
474 static int is_extern_c = 0;
475
476 /* Get a new token from the preprocessor. */
477 token->type
478 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
479 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
480 token->keyword = RID_MAX;
481 token->pragma_kind = PRAGMA_NONE;
482 token->purged_p = false;
483
484 /* On some systems, some header files are surrounded by an
485 implicit extern "C" block. Set a flag in the token if it
486 comes from such a header. */
487 is_extern_c += pending_lang_change;
488 pending_lang_change = 0;
489 token->implicit_extern_c = is_extern_c > 0;
490
491 /* Check to see if this token is a keyword. */
492 if (token->type == CPP_NAME)
493 {
494 if (C_IS_RESERVED_WORD (token->u.value))
495 {
496 /* Mark this token as a keyword. */
497 token->type = CPP_KEYWORD;
498 /* Record which keyword. */
499 token->keyword = C_RID_CODE (token->u.value);
500 }
501 else
502 {
503 if (warn_cxx0x_compat
504 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
505 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
506 {
507 /* Warn about the C++0x keyword (but still treat it as
508 an identifier). */
509 warning (OPT_Wc__0x_compat,
510 "identifier %qE will become a keyword in C++0x",
511 token->u.value);
512
513 /* Clear out the C_RID_CODE so we don't warn about this
514 particular identifier-turned-keyword again. */
515 C_SET_RID_CODE (token->u.value, RID_MAX);
516 }
517
518 token->ambiguous_p = false;
519 token->keyword = RID_MAX;
520 }
521 }
522 else if (token->type == CPP_AT_NAME)
523 {
524 /* This only happens in Objective-C++; it must be a keyword. */
525 token->type = CPP_KEYWORD;
526 switch (C_RID_CODE (token->u.value))
527 {
528 /* Replace 'class' with '@class', 'private' with '@private',
529 etc. This prevents confusion with the C++ keyword
530 'class', and makes the tokens consistent with other
531 Objective-C 'AT' keywords. For example '@class' is
532 reported as RID_AT_CLASS which is consistent with
533 '@synchronized', which is reported as
534 RID_AT_SYNCHRONIZED.
535 */
536 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
537 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
538 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
539 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
540 case RID_THROW: token->keyword = RID_AT_THROW; break;
541 case RID_TRY: token->keyword = RID_AT_TRY; break;
542 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
543 default: token->keyword = C_RID_CODE (token->u.value);
544 }
545 }
546 else if (token->type == CPP_PRAGMA)
547 {
548 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
549 token->pragma_kind = ((enum pragma_kind)
550 TREE_INT_CST_LOW (token->u.value));
551 token->u.value = NULL_TREE;
552 }
553 }
554
555 /* Update the globals input_location and the input file stack from TOKEN. */
556 static inline void
557 cp_lexer_set_source_position_from_token (cp_token *token)
558 {
559 if (token->type != CPP_EOF)
560 {
561 input_location = token->location;
562 }
563 }
564
565 /* Return a pointer to the next token in the token stream, but do not
566 consume it. */
567
568 static inline cp_token *
569 cp_lexer_peek_token (cp_lexer *lexer)
570 {
571 if (cp_lexer_debugging_p (lexer))
572 {
573 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
574 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
575 putc ('\n', cp_lexer_debug_stream);
576 }
577 return lexer->next_token;
578 }
579
580 /* Return true if the next token has the indicated TYPE. */
581
582 static inline bool
583 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
584 {
585 return cp_lexer_peek_token (lexer)->type == type;
586 }
587
588 /* Return true if the next token does not have the indicated TYPE. */
589
590 static inline bool
591 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
592 {
593 return !cp_lexer_next_token_is (lexer, type);
594 }
595
596 /* Return true if the next token is the indicated KEYWORD. */
597
598 static inline bool
599 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
600 {
601 return cp_lexer_peek_token (lexer)->keyword == keyword;
602 }
603
604 /* Return true if the next token is not the indicated KEYWORD. */
605
606 static inline bool
607 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
608 {
609 return cp_lexer_peek_token (lexer)->keyword != keyword;
610 }
611
612 /* Return true if the next token is a keyword for a decl-specifier. */
613
614 static bool
615 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
616 {
617 cp_token *token;
618
619 token = cp_lexer_peek_token (lexer);
620 switch (token->keyword)
621 {
622 /* auto specifier: storage-class-specifier in C++,
623 simple-type-specifier in C++0x. */
624 case RID_AUTO:
625 /* Storage classes. */
626 case RID_REGISTER:
627 case RID_STATIC:
628 case RID_EXTERN:
629 case RID_MUTABLE:
630 case RID_THREAD:
631 /* Elaborated type specifiers. */
632 case RID_ENUM:
633 case RID_CLASS:
634 case RID_STRUCT:
635 case RID_UNION:
636 case RID_TYPENAME:
637 /* Simple type specifiers. */
638 case RID_CHAR:
639 case RID_CHAR16:
640 case RID_CHAR32:
641 case RID_WCHAR:
642 case RID_BOOL:
643 case RID_SHORT:
644 case RID_INT:
645 case RID_LONG:
646 case RID_INT128:
647 case RID_SIGNED:
648 case RID_UNSIGNED:
649 case RID_FLOAT:
650 case RID_DOUBLE:
651 case RID_VOID:
652 /* GNU extensions. */
653 case RID_ATTRIBUTE:
654 case RID_TYPEOF:
655 /* C++0x extensions. */
656 case RID_DECLTYPE:
657 case RID_UNDERLYING_TYPE:
658 return true;
659
660 default:
661 return false;
662 }
663 }
664
665 /* Return a pointer to the Nth token in the token stream. If N is 1,
666 then this is precisely equivalent to cp_lexer_peek_token (except
667 that it is not inline). One would like to disallow that case, but
668 there is one case (cp_parser_nth_token_starts_template_id) where
669 the caller passes a variable for N and it might be 1. */
670
671 static cp_token *
672 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
673 {
674 cp_token *token;
675
676 /* N is 1-based, not zero-based. */
677 gcc_assert (n > 0);
678
679 if (cp_lexer_debugging_p (lexer))
680 fprintf (cp_lexer_debug_stream,
681 "cp_lexer: peeking ahead %ld at token: ", (long)n);
682
683 --n;
684 token = lexer->next_token;
685 gcc_assert (!n || token != &eof_token);
686 while (n != 0)
687 {
688 ++token;
689 if (token == lexer->last_token)
690 {
691 token = &eof_token;
692 break;
693 }
694
695 if (!token->purged_p)
696 --n;
697 }
698
699 if (cp_lexer_debugging_p (lexer))
700 {
701 cp_lexer_print_token (cp_lexer_debug_stream, token);
702 putc ('\n', cp_lexer_debug_stream);
703 }
704
705 return token;
706 }
707
708 /* Return the next token, and advance the lexer's next_token pointer
709 to point to the next non-purged token. */
710
711 static cp_token *
712 cp_lexer_consume_token (cp_lexer* lexer)
713 {
714 cp_token *token = lexer->next_token;
715
716 gcc_assert (token != &eof_token);
717 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
718
719 do
720 {
721 lexer->next_token++;
722 if (lexer->next_token == lexer->last_token)
723 {
724 lexer->next_token = &eof_token;
725 break;
726 }
727
728 }
729 while (lexer->next_token->purged_p);
730
731 cp_lexer_set_source_position_from_token (token);
732
733 /* Provide debugging output. */
734 if (cp_lexer_debugging_p (lexer))
735 {
736 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
737 cp_lexer_print_token (cp_lexer_debug_stream, token);
738 putc ('\n', cp_lexer_debug_stream);
739 }
740
741 return token;
742 }
743
744 /* Permanently remove the next token from the token stream, and
745 advance the next_token pointer to refer to the next non-purged
746 token. */
747
748 static void
749 cp_lexer_purge_token (cp_lexer *lexer)
750 {
751 cp_token *tok = lexer->next_token;
752
753 gcc_assert (tok != &eof_token);
754 tok->purged_p = true;
755 tok->location = UNKNOWN_LOCATION;
756 tok->u.value = NULL_TREE;
757 tok->keyword = RID_MAX;
758
759 do
760 {
761 tok++;
762 if (tok == lexer->last_token)
763 {
764 tok = &eof_token;
765 break;
766 }
767 }
768 while (tok->purged_p);
769 lexer->next_token = tok;
770 }
771
772 /* Permanently remove all tokens after TOK, up to, but not
773 including, the token that will be returned next by
774 cp_lexer_peek_token. */
775
776 static void
777 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
778 {
779 cp_token *peek = lexer->next_token;
780
781 if (peek == &eof_token)
782 peek = lexer->last_token;
783
784 gcc_assert (tok < peek);
785
786 for ( tok += 1; tok != peek; tok += 1)
787 {
788 tok->purged_p = true;
789 tok->location = UNKNOWN_LOCATION;
790 tok->u.value = NULL_TREE;
791 tok->keyword = RID_MAX;
792 }
793 }
794
795 /* Begin saving tokens. All tokens consumed after this point will be
796 preserved. */
797
798 static void
799 cp_lexer_save_tokens (cp_lexer* lexer)
800 {
801 /* Provide debugging output. */
802 if (cp_lexer_debugging_p (lexer))
803 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
804
805 VEC_safe_push (cp_token_position, heap,
806 lexer->saved_tokens, lexer->next_token);
807 }
808
809 /* Commit to the portion of the token stream most recently saved. */
810
811 static void
812 cp_lexer_commit_tokens (cp_lexer* lexer)
813 {
814 /* Provide debugging output. */
815 if (cp_lexer_debugging_p (lexer))
816 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
817
818 VEC_pop (cp_token_position, lexer->saved_tokens);
819 }
820
821 /* Return all tokens saved since the last call to cp_lexer_save_tokens
822 to the token stream. Stop saving tokens. */
823
824 static void
825 cp_lexer_rollback_tokens (cp_lexer* lexer)
826 {
827 /* Provide debugging output. */
828 if (cp_lexer_debugging_p (lexer))
829 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
830
831 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
832 }
833
834 /* Print a representation of the TOKEN on the STREAM. */
835
836 #ifdef ENABLE_CHECKING
837
838 static void
839 cp_lexer_print_token (FILE * stream, cp_token *token)
840 {
841 /* We don't use cpp_type2name here because the parser defines
842 a few tokens of its own. */
843 static const char *const token_names[] = {
844 /* cpplib-defined token types */
845 #define OP(e, s) #e,
846 #define TK(e, s) #e,
847 TTYPE_TABLE
848 #undef OP
849 #undef TK
850 /* C++ parser token types - see "Manifest constants", above. */
851 "KEYWORD",
852 "TEMPLATE_ID",
853 "NESTED_NAME_SPECIFIER",
854 };
855
856 /* For some tokens, print the associated data. */
857 switch (token->type)
858 {
859 case CPP_KEYWORD:
860 /* Some keywords have a value that is not an IDENTIFIER_NODE.
861 For example, `struct' is mapped to an INTEGER_CST. */
862 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
863 break;
864 /* else fall through */
865 case CPP_NAME:
866 fputs (IDENTIFIER_POINTER (token->u.value), stream);
867 break;
868
869 case CPP_STRING:
870 case CPP_STRING16:
871 case CPP_STRING32:
872 case CPP_WSTRING:
873 case CPP_UTF8STRING:
874 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
875 break;
876
877 case CPP_NUMBER:
878 print_generic_expr (stream, token->u.value, 0);
879 break;
880
881 default:
882 /* If we have a name for the token, print it out. Otherwise, we
883 simply give the numeric code. */
884 if (token->type < ARRAY_SIZE(token_names))
885 fputs (token_names[token->type], stream);
886 else
887 fprintf (stream, "[%d]", token->type);
888 break;
889 }
890 }
891
892 /* Start emitting debugging information. */
893
894 static void
895 cp_lexer_start_debugging (cp_lexer* lexer)
896 {
897 lexer->debugging_p = true;
898 }
899
900 /* Stop emitting debugging information. */
901
902 static void
903 cp_lexer_stop_debugging (cp_lexer* lexer)
904 {
905 lexer->debugging_p = false;
906 }
907
908 #endif /* ENABLE_CHECKING */
909
910 /* Create a new cp_token_cache, representing a range of tokens. */
911
912 static cp_token_cache *
913 cp_token_cache_new (cp_token *first, cp_token *last)
914 {
915 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
916 cache->first = first;
917 cache->last = last;
918 return cache;
919 }
920
921 \f
922 /* Decl-specifiers. */
923
924 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
925
926 static void
927 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
928 {
929 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
930 }
931
932 /* Declarators. */
933
934 /* Nothing other than the parser should be creating declarators;
935 declarators are a semi-syntactic representation of C++ entities.
936 Other parts of the front end that need to create entities (like
937 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
938
939 static cp_declarator *make_call_declarator
940 (cp_declarator *, tree, cp_cv_quals, tree, tree);
941 static cp_declarator *make_array_declarator
942 (cp_declarator *, tree);
943 static cp_declarator *make_pointer_declarator
944 (cp_cv_quals, cp_declarator *);
945 static cp_declarator *make_reference_declarator
946 (cp_cv_quals, cp_declarator *, bool);
947 static cp_parameter_declarator *make_parameter_declarator
948 (cp_decl_specifier_seq *, cp_declarator *, tree);
949 static cp_declarator *make_ptrmem_declarator
950 (cp_cv_quals, tree, cp_declarator *);
951
952 /* An erroneous declarator. */
953 static cp_declarator *cp_error_declarator;
954
955 /* The obstack on which declarators and related data structures are
956 allocated. */
957 static struct obstack declarator_obstack;
958
959 /* Alloc BYTES from the declarator memory pool. */
960
961 static inline void *
962 alloc_declarator (size_t bytes)
963 {
964 return obstack_alloc (&declarator_obstack, bytes);
965 }
966
967 /* Allocate a declarator of the indicated KIND. Clear fields that are
968 common to all declarators. */
969
970 static cp_declarator *
971 make_declarator (cp_declarator_kind kind)
972 {
973 cp_declarator *declarator;
974
975 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
976 declarator->kind = kind;
977 declarator->attributes = NULL_TREE;
978 declarator->declarator = NULL;
979 declarator->parameter_pack_p = false;
980 declarator->id_loc = UNKNOWN_LOCATION;
981
982 return declarator;
983 }
984
985 /* Make a declarator for a generalized identifier. If
986 QUALIFYING_SCOPE is non-NULL, the identifier is
987 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
988 UNQUALIFIED_NAME. SFK indicates the kind of special function this
989 is, if any. */
990
991 static cp_declarator *
992 make_id_declarator (tree qualifying_scope, tree unqualified_name,
993 special_function_kind sfk)
994 {
995 cp_declarator *declarator;
996
997 /* It is valid to write:
998
999 class C { void f(); };
1000 typedef C D;
1001 void D::f();
1002
1003 The standard is not clear about whether `typedef const C D' is
1004 legal; as of 2002-09-15 the committee is considering that
1005 question. EDG 3.0 allows that syntax. Therefore, we do as
1006 well. */
1007 if (qualifying_scope && TYPE_P (qualifying_scope))
1008 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1009
1010 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1011 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1012 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1013
1014 declarator = make_declarator (cdk_id);
1015 declarator->u.id.qualifying_scope = qualifying_scope;
1016 declarator->u.id.unqualified_name = unqualified_name;
1017 declarator->u.id.sfk = sfk;
1018
1019 return declarator;
1020 }
1021
1022 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1023 of modifiers such as const or volatile to apply to the pointer
1024 type, represented as identifiers. */
1025
1026 cp_declarator *
1027 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1028 {
1029 cp_declarator *declarator;
1030
1031 declarator = make_declarator (cdk_pointer);
1032 declarator->declarator = target;
1033 declarator->u.pointer.qualifiers = cv_qualifiers;
1034 declarator->u.pointer.class_type = NULL_TREE;
1035 if (target)
1036 {
1037 declarator->id_loc = target->id_loc;
1038 declarator->parameter_pack_p = target->parameter_pack_p;
1039 target->parameter_pack_p = false;
1040 }
1041 else
1042 declarator->parameter_pack_p = false;
1043
1044 return declarator;
1045 }
1046
1047 /* Like make_pointer_declarator -- but for references. */
1048
1049 cp_declarator *
1050 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1051 bool rvalue_ref)
1052 {
1053 cp_declarator *declarator;
1054
1055 declarator = make_declarator (cdk_reference);
1056 declarator->declarator = target;
1057 declarator->u.reference.qualifiers = cv_qualifiers;
1058 declarator->u.reference.rvalue_ref = rvalue_ref;
1059 if (target)
1060 {
1061 declarator->id_loc = target->id_loc;
1062 declarator->parameter_pack_p = target->parameter_pack_p;
1063 target->parameter_pack_p = false;
1064 }
1065 else
1066 declarator->parameter_pack_p = false;
1067
1068 return declarator;
1069 }
1070
1071 /* Like make_pointer_declarator -- but for a pointer to a non-static
1072 member of CLASS_TYPE. */
1073
1074 cp_declarator *
1075 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1076 cp_declarator *pointee)
1077 {
1078 cp_declarator *declarator;
1079
1080 declarator = make_declarator (cdk_ptrmem);
1081 declarator->declarator = pointee;
1082 declarator->u.pointer.qualifiers = cv_qualifiers;
1083 declarator->u.pointer.class_type = class_type;
1084
1085 if (pointee)
1086 {
1087 declarator->parameter_pack_p = pointee->parameter_pack_p;
1088 pointee->parameter_pack_p = false;
1089 }
1090 else
1091 declarator->parameter_pack_p = false;
1092
1093 return declarator;
1094 }
1095
1096 /* Make a declarator for the function given by TARGET, with the
1097 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1098 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1099 indicates what exceptions can be thrown. */
1100
1101 cp_declarator *
1102 make_call_declarator (cp_declarator *target,
1103 tree parms,
1104 cp_cv_quals cv_qualifiers,
1105 tree exception_specification,
1106 tree late_return_type)
1107 {
1108 cp_declarator *declarator;
1109
1110 declarator = make_declarator (cdk_function);
1111 declarator->declarator = target;
1112 declarator->u.function.parameters = parms;
1113 declarator->u.function.qualifiers = cv_qualifiers;
1114 declarator->u.function.exception_specification = exception_specification;
1115 declarator->u.function.late_return_type = late_return_type;
1116 if (target)
1117 {
1118 declarator->id_loc = target->id_loc;
1119 declarator->parameter_pack_p = target->parameter_pack_p;
1120 target->parameter_pack_p = false;
1121 }
1122 else
1123 declarator->parameter_pack_p = false;
1124
1125 return declarator;
1126 }
1127
1128 /* Make a declarator for an array of BOUNDS elements, each of which is
1129 defined by ELEMENT. */
1130
1131 cp_declarator *
1132 make_array_declarator (cp_declarator *element, tree bounds)
1133 {
1134 cp_declarator *declarator;
1135
1136 declarator = make_declarator (cdk_array);
1137 declarator->declarator = element;
1138 declarator->u.array.bounds = bounds;
1139 if (element)
1140 {
1141 declarator->id_loc = element->id_loc;
1142 declarator->parameter_pack_p = element->parameter_pack_p;
1143 element->parameter_pack_p = false;
1144 }
1145 else
1146 declarator->parameter_pack_p = false;
1147
1148 return declarator;
1149 }
1150
1151 /* Determine whether the declarator we've seen so far can be a
1152 parameter pack, when followed by an ellipsis. */
1153 static bool
1154 declarator_can_be_parameter_pack (cp_declarator *declarator)
1155 {
1156 /* Search for a declarator name, or any other declarator that goes
1157 after the point where the ellipsis could appear in a parameter
1158 pack. If we find any of these, then this declarator can not be
1159 made into a parameter pack. */
1160 bool found = false;
1161 while (declarator && !found)
1162 {
1163 switch ((int)declarator->kind)
1164 {
1165 case cdk_id:
1166 case cdk_array:
1167 found = true;
1168 break;
1169
1170 case cdk_error:
1171 return true;
1172
1173 default:
1174 declarator = declarator->declarator;
1175 break;
1176 }
1177 }
1178
1179 return !found;
1180 }
1181
1182 cp_parameter_declarator *no_parameters;
1183
1184 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1185 DECLARATOR and DEFAULT_ARGUMENT. */
1186
1187 cp_parameter_declarator *
1188 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1189 cp_declarator *declarator,
1190 tree default_argument)
1191 {
1192 cp_parameter_declarator *parameter;
1193
1194 parameter = ((cp_parameter_declarator *)
1195 alloc_declarator (sizeof (cp_parameter_declarator)));
1196 parameter->next = NULL;
1197 if (decl_specifiers)
1198 parameter->decl_specifiers = *decl_specifiers;
1199 else
1200 clear_decl_specs (&parameter->decl_specifiers);
1201 parameter->declarator = declarator;
1202 parameter->default_argument = default_argument;
1203 parameter->ellipsis_p = false;
1204
1205 return parameter;
1206 }
1207
1208 /* Returns true iff DECLARATOR is a declaration for a function. */
1209
1210 static bool
1211 function_declarator_p (const cp_declarator *declarator)
1212 {
1213 while (declarator)
1214 {
1215 if (declarator->kind == cdk_function
1216 && declarator->declarator->kind == cdk_id)
1217 return true;
1218 if (declarator->kind == cdk_id
1219 || declarator->kind == cdk_error)
1220 return false;
1221 declarator = declarator->declarator;
1222 }
1223 return false;
1224 }
1225
1226 /* The parser. */
1227
1228 /* Overview
1229 --------
1230
1231 A cp_parser parses the token stream as specified by the C++
1232 grammar. Its job is purely parsing, not semantic analysis. For
1233 example, the parser breaks the token stream into declarators,
1234 expressions, statements, and other similar syntactic constructs.
1235 It does not check that the types of the expressions on either side
1236 of an assignment-statement are compatible, or that a function is
1237 not declared with a parameter of type `void'.
1238
1239 The parser invokes routines elsewhere in the compiler to perform
1240 semantic analysis and to build up the abstract syntax tree for the
1241 code processed.
1242
1243 The parser (and the template instantiation code, which is, in a
1244 way, a close relative of parsing) are the only parts of the
1245 compiler that should be calling push_scope and pop_scope, or
1246 related functions. The parser (and template instantiation code)
1247 keeps track of what scope is presently active; everything else
1248 should simply honor that. (The code that generates static
1249 initializers may also need to set the scope, in order to check
1250 access control correctly when emitting the initializers.)
1251
1252 Methodology
1253 -----------
1254
1255 The parser is of the standard recursive-descent variety. Upcoming
1256 tokens in the token stream are examined in order to determine which
1257 production to use when parsing a non-terminal. Some C++ constructs
1258 require arbitrary look ahead to disambiguate. For example, it is
1259 impossible, in the general case, to tell whether a statement is an
1260 expression or declaration without scanning the entire statement.
1261 Therefore, the parser is capable of "parsing tentatively." When the
1262 parser is not sure what construct comes next, it enters this mode.
1263 Then, while we attempt to parse the construct, the parser queues up
1264 error messages, rather than issuing them immediately, and saves the
1265 tokens it consumes. If the construct is parsed successfully, the
1266 parser "commits", i.e., it issues any queued error messages and
1267 the tokens that were being preserved are permanently discarded.
1268 If, however, the construct is not parsed successfully, the parser
1269 rolls back its state completely so that it can resume parsing using
1270 a different alternative.
1271
1272 Future Improvements
1273 -------------------
1274
1275 The performance of the parser could probably be improved substantially.
1276 We could often eliminate the need to parse tentatively by looking ahead
1277 a little bit. In some places, this approach might not entirely eliminate
1278 the need to parse tentatively, but it might still speed up the average
1279 case. */
1280
1281 /* Flags that are passed to some parsing functions. These values can
1282 be bitwise-ored together. */
1283
1284 enum
1285 {
1286 /* No flags. */
1287 CP_PARSER_FLAGS_NONE = 0x0,
1288 /* The construct is optional. If it is not present, then no error
1289 should be issued. */
1290 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1291 /* When parsing a type-specifier, treat user-defined type-names
1292 as non-type identifiers. */
1293 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1294 /* When parsing a type-specifier, do not try to parse a class-specifier
1295 or enum-specifier. */
1296 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1297 /* When parsing a decl-specifier-seq, only allow type-specifier or
1298 constexpr. */
1299 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1300 };
1301
1302 /* This type is used for parameters and variables which hold
1303 combinations of the above flags. */
1304 typedef int cp_parser_flags;
1305
1306 /* The different kinds of declarators we want to parse. */
1307
1308 typedef enum cp_parser_declarator_kind
1309 {
1310 /* We want an abstract declarator. */
1311 CP_PARSER_DECLARATOR_ABSTRACT,
1312 /* We want a named declarator. */
1313 CP_PARSER_DECLARATOR_NAMED,
1314 /* We don't mind, but the name must be an unqualified-id. */
1315 CP_PARSER_DECLARATOR_EITHER
1316 } cp_parser_declarator_kind;
1317
1318 /* The precedence values used to parse binary expressions. The minimum value
1319 of PREC must be 1, because zero is reserved to quickly discriminate
1320 binary operators from other tokens. */
1321
1322 enum cp_parser_prec
1323 {
1324 PREC_NOT_OPERATOR,
1325 PREC_LOGICAL_OR_EXPRESSION,
1326 PREC_LOGICAL_AND_EXPRESSION,
1327 PREC_INCLUSIVE_OR_EXPRESSION,
1328 PREC_EXCLUSIVE_OR_EXPRESSION,
1329 PREC_AND_EXPRESSION,
1330 PREC_EQUALITY_EXPRESSION,
1331 PREC_RELATIONAL_EXPRESSION,
1332 PREC_SHIFT_EXPRESSION,
1333 PREC_ADDITIVE_EXPRESSION,
1334 PREC_MULTIPLICATIVE_EXPRESSION,
1335 PREC_PM_EXPRESSION,
1336 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1337 };
1338
1339 /* A mapping from a token type to a corresponding tree node type, with a
1340 precedence value. */
1341
1342 typedef struct cp_parser_binary_operations_map_node
1343 {
1344 /* The token type. */
1345 enum cpp_ttype token_type;
1346 /* The corresponding tree code. */
1347 enum tree_code tree_type;
1348 /* The precedence of this operator. */
1349 enum cp_parser_prec prec;
1350 } cp_parser_binary_operations_map_node;
1351
1352 typedef struct cp_parser_expression_stack_entry
1353 {
1354 /* Left hand side of the binary operation we are currently
1355 parsing. */
1356 tree lhs;
1357 /* Original tree code for left hand side, if it was a binary
1358 expression itself (used for -Wparentheses). */
1359 enum tree_code lhs_type;
1360 /* Tree code for the binary operation we are parsing. */
1361 enum tree_code tree_type;
1362 /* Precedence of the binary operation we are parsing. */
1363 enum cp_parser_prec prec;
1364 } cp_parser_expression_stack_entry;
1365
1366 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1367 entries because precedence levels on the stack are monotonically
1368 increasing. */
1369 typedef struct cp_parser_expression_stack_entry
1370 cp_parser_expression_stack[NUM_PREC_VALUES];
1371
1372 /* Prototypes. */
1373
1374 /* Constructors and destructors. */
1375
1376 static cp_parser_context *cp_parser_context_new
1377 (cp_parser_context *);
1378
1379 /* Class variables. */
1380
1381 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1382
1383 /* The operator-precedence table used by cp_parser_binary_expression.
1384 Transformed into an associative array (binops_by_token) by
1385 cp_parser_new. */
1386
1387 static const cp_parser_binary_operations_map_node binops[] = {
1388 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1389 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1390
1391 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1392 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1393 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1394
1395 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1396 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1397
1398 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1399 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1400
1401 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1402 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1403 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1404 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1405
1406 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1407 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1408
1409 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1410
1411 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1412
1413 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1414
1415 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1416
1417 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1418 };
1419
1420 /* The same as binops, but initialized by cp_parser_new so that
1421 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1422 for speed. */
1423 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1424
1425 /* Constructors and destructors. */
1426
1427 /* Construct a new context. The context below this one on the stack
1428 is given by NEXT. */
1429
1430 static cp_parser_context *
1431 cp_parser_context_new (cp_parser_context* next)
1432 {
1433 cp_parser_context *context;
1434
1435 /* Allocate the storage. */
1436 if (cp_parser_context_free_list != NULL)
1437 {
1438 /* Pull the first entry from the free list. */
1439 context = cp_parser_context_free_list;
1440 cp_parser_context_free_list = context->next;
1441 memset (context, 0, sizeof (*context));
1442 }
1443 else
1444 context = ggc_alloc_cleared_cp_parser_context ();
1445
1446 /* No errors have occurred yet in this context. */
1447 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1448 /* If this is not the bottommost context, copy information that we
1449 need from the previous context. */
1450 if (next)
1451 {
1452 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1453 expression, then we are parsing one in this context, too. */
1454 context->object_type = next->object_type;
1455 /* Thread the stack. */
1456 context->next = next;
1457 }
1458
1459 return context;
1460 }
1461
1462 /* Managing the unparsed function queues. */
1463
1464 #define unparsed_funs_with_default_args \
1465 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1466 #define unparsed_funs_with_definitions \
1467 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1468
1469 static void
1470 push_unparsed_function_queues (cp_parser *parser)
1471 {
1472 VEC_safe_push (cp_unparsed_functions_entry, gc,
1473 parser->unparsed_queues, NULL);
1474 unparsed_funs_with_default_args = NULL;
1475 unparsed_funs_with_definitions = make_tree_vector ();
1476 }
1477
1478 static void
1479 pop_unparsed_function_queues (cp_parser *parser)
1480 {
1481 release_tree_vector (unparsed_funs_with_definitions);
1482 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1483 }
1484
1485 /* Prototypes. */
1486
1487 /* Constructors and destructors. */
1488
1489 static cp_parser *cp_parser_new
1490 (void);
1491
1492 /* Routines to parse various constructs.
1493
1494 Those that return `tree' will return the error_mark_node (rather
1495 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1496 Sometimes, they will return an ordinary node if error-recovery was
1497 attempted, even though a parse error occurred. So, to check
1498 whether or not a parse error occurred, you should always use
1499 cp_parser_error_occurred. If the construct is optional (indicated
1500 either by an `_opt' in the name of the function that does the
1501 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1502 the construct is not present. */
1503
1504 /* Lexical conventions [gram.lex] */
1505
1506 static tree cp_parser_identifier
1507 (cp_parser *);
1508 static tree cp_parser_string_literal
1509 (cp_parser *, bool, bool);
1510
1511 /* Basic concepts [gram.basic] */
1512
1513 static bool cp_parser_translation_unit
1514 (cp_parser *);
1515
1516 /* Expressions [gram.expr] */
1517
1518 static tree cp_parser_primary_expression
1519 (cp_parser *, bool, bool, bool, cp_id_kind *);
1520 static tree cp_parser_id_expression
1521 (cp_parser *, bool, bool, bool *, bool, bool);
1522 static tree cp_parser_unqualified_id
1523 (cp_parser *, bool, bool, bool, bool);
1524 static tree cp_parser_nested_name_specifier_opt
1525 (cp_parser *, bool, bool, bool, bool);
1526 static tree cp_parser_nested_name_specifier
1527 (cp_parser *, bool, bool, bool, bool);
1528 static tree cp_parser_qualifying_entity
1529 (cp_parser *, bool, bool, bool, bool, bool);
1530 static tree cp_parser_postfix_expression
1531 (cp_parser *, bool, bool, bool, cp_id_kind *);
1532 static tree cp_parser_postfix_open_square_expression
1533 (cp_parser *, tree, bool);
1534 static tree cp_parser_postfix_dot_deref_expression
1535 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1536 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1537 (cp_parser *, int, bool, bool, bool *);
1538 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1539 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1540 static void cp_parser_pseudo_destructor_name
1541 (cp_parser *, tree *, tree *);
1542 static tree cp_parser_unary_expression
1543 (cp_parser *, bool, bool, cp_id_kind *);
1544 static enum tree_code cp_parser_unary_operator
1545 (cp_token *);
1546 static tree cp_parser_new_expression
1547 (cp_parser *);
1548 static VEC(tree,gc) *cp_parser_new_placement
1549 (cp_parser *);
1550 static tree cp_parser_new_type_id
1551 (cp_parser *, tree *);
1552 static cp_declarator *cp_parser_new_declarator_opt
1553 (cp_parser *);
1554 static cp_declarator *cp_parser_direct_new_declarator
1555 (cp_parser *);
1556 static VEC(tree,gc) *cp_parser_new_initializer
1557 (cp_parser *);
1558 static tree cp_parser_delete_expression
1559 (cp_parser *);
1560 static tree cp_parser_cast_expression
1561 (cp_parser *, bool, bool, cp_id_kind *);
1562 static tree cp_parser_binary_expression
1563 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1564 static tree cp_parser_question_colon_clause
1565 (cp_parser *, tree);
1566 static tree cp_parser_assignment_expression
1567 (cp_parser *, bool, cp_id_kind *);
1568 static enum tree_code cp_parser_assignment_operator_opt
1569 (cp_parser *);
1570 static tree cp_parser_expression
1571 (cp_parser *, bool, cp_id_kind *);
1572 static tree cp_parser_constant_expression
1573 (cp_parser *, bool, bool *);
1574 static tree cp_parser_builtin_offsetof
1575 (cp_parser *);
1576 static tree cp_parser_lambda_expression
1577 (cp_parser *);
1578 static void cp_parser_lambda_introducer
1579 (cp_parser *, tree);
1580 static void cp_parser_lambda_declarator_opt
1581 (cp_parser *, tree);
1582 static void cp_parser_lambda_body
1583 (cp_parser *, tree);
1584
1585 /* Statements [gram.stmt.stmt] */
1586
1587 static void cp_parser_statement
1588 (cp_parser *, tree, bool, bool *);
1589 static void cp_parser_label_for_labeled_statement
1590 (cp_parser *);
1591 static tree cp_parser_expression_statement
1592 (cp_parser *, tree);
1593 static tree cp_parser_compound_statement
1594 (cp_parser *, tree, bool, bool);
1595 static void cp_parser_statement_seq_opt
1596 (cp_parser *, tree);
1597 static tree cp_parser_selection_statement
1598 (cp_parser *, bool *);
1599 static tree cp_parser_condition
1600 (cp_parser *);
1601 static tree cp_parser_iteration_statement
1602 (cp_parser *);
1603 static bool cp_parser_for_init_statement
1604 (cp_parser *, tree *decl);
1605 static tree cp_parser_for
1606 (cp_parser *);
1607 static tree cp_parser_c_for
1608 (cp_parser *, tree, tree);
1609 static tree cp_parser_range_for
1610 (cp_parser *, tree, tree, tree);
1611 static tree cp_parser_perform_range_for_lookup
1612 (tree, tree *, tree *);
1613 static tree cp_parser_range_for_member_function
1614 (tree, tree);
1615 static tree cp_parser_jump_statement
1616 (cp_parser *);
1617 static void cp_parser_declaration_statement
1618 (cp_parser *);
1619
1620 static tree cp_parser_implicitly_scoped_statement
1621 (cp_parser *, bool *);
1622 static void cp_parser_already_scoped_statement
1623 (cp_parser *);
1624
1625 /* Declarations [gram.dcl.dcl] */
1626
1627 static void cp_parser_declaration_seq_opt
1628 (cp_parser *);
1629 static void cp_parser_declaration
1630 (cp_parser *);
1631 static void cp_parser_block_declaration
1632 (cp_parser *, bool);
1633 static void cp_parser_simple_declaration
1634 (cp_parser *, bool, tree *);
1635 static void cp_parser_decl_specifier_seq
1636 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1637 static tree cp_parser_storage_class_specifier_opt
1638 (cp_parser *);
1639 static tree cp_parser_function_specifier_opt
1640 (cp_parser *, cp_decl_specifier_seq *);
1641 static tree cp_parser_type_specifier
1642 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1643 int *, bool *);
1644 static tree cp_parser_simple_type_specifier
1645 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1646 static tree cp_parser_type_name
1647 (cp_parser *);
1648 static tree cp_parser_nonclass_name
1649 (cp_parser* parser);
1650 static tree cp_parser_elaborated_type_specifier
1651 (cp_parser *, bool, bool);
1652 static tree cp_parser_enum_specifier
1653 (cp_parser *);
1654 static void cp_parser_enumerator_list
1655 (cp_parser *, tree);
1656 static void cp_parser_enumerator_definition
1657 (cp_parser *, tree);
1658 static tree cp_parser_namespace_name
1659 (cp_parser *);
1660 static void cp_parser_namespace_definition
1661 (cp_parser *);
1662 static void cp_parser_namespace_body
1663 (cp_parser *);
1664 static tree cp_parser_qualified_namespace_specifier
1665 (cp_parser *);
1666 static void cp_parser_namespace_alias_definition
1667 (cp_parser *);
1668 static bool cp_parser_using_declaration
1669 (cp_parser *, bool);
1670 static void cp_parser_using_directive
1671 (cp_parser *);
1672 static void cp_parser_asm_definition
1673 (cp_parser *);
1674 static void cp_parser_linkage_specification
1675 (cp_parser *);
1676 static void cp_parser_static_assert
1677 (cp_parser *, bool);
1678 static tree cp_parser_decltype
1679 (cp_parser *);
1680
1681 /* Declarators [gram.dcl.decl] */
1682
1683 static tree cp_parser_init_declarator
1684 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1685 static cp_declarator *cp_parser_declarator
1686 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1687 static cp_declarator *cp_parser_direct_declarator
1688 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1689 static enum tree_code cp_parser_ptr_operator
1690 (cp_parser *, tree *, cp_cv_quals *);
1691 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1692 (cp_parser *);
1693 static tree cp_parser_late_return_type_opt
1694 (cp_parser *);
1695 static tree cp_parser_declarator_id
1696 (cp_parser *, bool);
1697 static tree cp_parser_type_id
1698 (cp_parser *);
1699 static tree cp_parser_template_type_arg
1700 (cp_parser *);
1701 static tree cp_parser_trailing_type_id (cp_parser *);
1702 static tree cp_parser_type_id_1
1703 (cp_parser *, bool, bool);
1704 static void cp_parser_type_specifier_seq
1705 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1706 static tree cp_parser_parameter_declaration_clause
1707 (cp_parser *);
1708 static tree cp_parser_parameter_declaration_list
1709 (cp_parser *, bool *);
1710 static cp_parameter_declarator *cp_parser_parameter_declaration
1711 (cp_parser *, bool, bool *);
1712 static tree cp_parser_default_argument
1713 (cp_parser *, bool);
1714 static void cp_parser_function_body
1715 (cp_parser *);
1716 static tree cp_parser_initializer
1717 (cp_parser *, bool *, bool *);
1718 static tree cp_parser_initializer_clause
1719 (cp_parser *, bool *);
1720 static tree cp_parser_braced_list
1721 (cp_parser*, bool*);
1722 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1723 (cp_parser *, bool *);
1724
1725 static bool cp_parser_ctor_initializer_opt_and_function_body
1726 (cp_parser *);
1727
1728 /* Classes [gram.class] */
1729
1730 static tree cp_parser_class_name
1731 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1732 static tree cp_parser_class_specifier
1733 (cp_parser *);
1734 static tree cp_parser_class_head
1735 (cp_parser *, bool *, tree *, tree *);
1736 static enum tag_types cp_parser_class_key
1737 (cp_parser *);
1738 static void cp_parser_member_specification_opt
1739 (cp_parser *);
1740 static void cp_parser_member_declaration
1741 (cp_parser *);
1742 static tree cp_parser_pure_specifier
1743 (cp_parser *);
1744 static tree cp_parser_constant_initializer
1745 (cp_parser *);
1746
1747 /* Derived classes [gram.class.derived] */
1748
1749 static tree cp_parser_base_clause
1750 (cp_parser *);
1751 static tree cp_parser_base_specifier
1752 (cp_parser *);
1753
1754 /* Special member functions [gram.special] */
1755
1756 static tree cp_parser_conversion_function_id
1757 (cp_parser *);
1758 static tree cp_parser_conversion_type_id
1759 (cp_parser *);
1760 static cp_declarator *cp_parser_conversion_declarator_opt
1761 (cp_parser *);
1762 static bool cp_parser_ctor_initializer_opt
1763 (cp_parser *);
1764 static void cp_parser_mem_initializer_list
1765 (cp_parser *);
1766 static tree cp_parser_mem_initializer
1767 (cp_parser *);
1768 static tree cp_parser_mem_initializer_id
1769 (cp_parser *);
1770
1771 /* Overloading [gram.over] */
1772
1773 static tree cp_parser_operator_function_id
1774 (cp_parser *);
1775 static tree cp_parser_operator
1776 (cp_parser *);
1777
1778 /* Templates [gram.temp] */
1779
1780 static void cp_parser_template_declaration
1781 (cp_parser *, bool);
1782 static tree cp_parser_template_parameter_list
1783 (cp_parser *);
1784 static tree cp_parser_template_parameter
1785 (cp_parser *, bool *, bool *);
1786 static tree cp_parser_type_parameter
1787 (cp_parser *, bool *);
1788 static tree cp_parser_template_id
1789 (cp_parser *, bool, bool, bool);
1790 static tree cp_parser_template_name
1791 (cp_parser *, bool, bool, bool, bool *);
1792 static tree cp_parser_template_argument_list
1793 (cp_parser *);
1794 static tree cp_parser_template_argument
1795 (cp_parser *);
1796 static void cp_parser_explicit_instantiation
1797 (cp_parser *);
1798 static void cp_parser_explicit_specialization
1799 (cp_parser *);
1800
1801 /* Exception handling [gram.exception] */
1802
1803 static tree cp_parser_try_block
1804 (cp_parser *);
1805 static bool cp_parser_function_try_block
1806 (cp_parser *);
1807 static void cp_parser_handler_seq
1808 (cp_parser *);
1809 static void cp_parser_handler
1810 (cp_parser *);
1811 static tree cp_parser_exception_declaration
1812 (cp_parser *);
1813 static tree cp_parser_throw_expression
1814 (cp_parser *);
1815 static tree cp_parser_exception_specification_opt
1816 (cp_parser *);
1817 static tree cp_parser_type_id_list
1818 (cp_parser *);
1819
1820 /* GNU Extensions */
1821
1822 static tree cp_parser_asm_specification_opt
1823 (cp_parser *);
1824 static tree cp_parser_asm_operand_list
1825 (cp_parser *);
1826 static tree cp_parser_asm_clobber_list
1827 (cp_parser *);
1828 static tree cp_parser_asm_label_list
1829 (cp_parser *);
1830 static tree cp_parser_attributes_opt
1831 (cp_parser *);
1832 static tree cp_parser_attribute_list
1833 (cp_parser *);
1834 static bool cp_parser_extension_opt
1835 (cp_parser *, int *);
1836 static void cp_parser_label_declaration
1837 (cp_parser *);
1838
1839 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1840 static bool cp_parser_pragma
1841 (cp_parser *, enum pragma_context);
1842
1843 /* Objective-C++ Productions */
1844
1845 static tree cp_parser_objc_message_receiver
1846 (cp_parser *);
1847 static tree cp_parser_objc_message_args
1848 (cp_parser *);
1849 static tree cp_parser_objc_message_expression
1850 (cp_parser *);
1851 static tree cp_parser_objc_encode_expression
1852 (cp_parser *);
1853 static tree cp_parser_objc_defs_expression
1854 (cp_parser *);
1855 static tree cp_parser_objc_protocol_expression
1856 (cp_parser *);
1857 static tree cp_parser_objc_selector_expression
1858 (cp_parser *);
1859 static tree cp_parser_objc_expression
1860 (cp_parser *);
1861 static bool cp_parser_objc_selector_p
1862 (enum cpp_ttype);
1863 static tree cp_parser_objc_selector
1864 (cp_parser *);
1865 static tree cp_parser_objc_protocol_refs_opt
1866 (cp_parser *);
1867 static void cp_parser_objc_declaration
1868 (cp_parser *, tree);
1869 static tree cp_parser_objc_statement
1870 (cp_parser *);
1871 static bool cp_parser_objc_valid_prefix_attributes
1872 (cp_parser *, tree *);
1873 static void cp_parser_objc_at_property_declaration
1874 (cp_parser *) ;
1875 static void cp_parser_objc_at_synthesize_declaration
1876 (cp_parser *) ;
1877 static void cp_parser_objc_at_dynamic_declaration
1878 (cp_parser *) ;
1879 static tree cp_parser_objc_struct_declaration
1880 (cp_parser *) ;
1881
1882 /* Utility Routines */
1883
1884 static tree cp_parser_lookup_name
1885 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1886 static tree cp_parser_lookup_name_simple
1887 (cp_parser *, tree, location_t);
1888 static tree cp_parser_maybe_treat_template_as_class
1889 (tree, bool);
1890 static bool cp_parser_check_declarator_template_parameters
1891 (cp_parser *, cp_declarator *, location_t);
1892 static bool cp_parser_check_template_parameters
1893 (cp_parser *, unsigned, location_t, cp_declarator *);
1894 static tree cp_parser_simple_cast_expression
1895 (cp_parser *);
1896 static tree cp_parser_global_scope_opt
1897 (cp_parser *, bool);
1898 static bool cp_parser_constructor_declarator_p
1899 (cp_parser *, bool);
1900 static tree cp_parser_function_definition_from_specifiers_and_declarator
1901 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1902 static tree cp_parser_function_definition_after_declarator
1903 (cp_parser *, bool);
1904 static void cp_parser_template_declaration_after_export
1905 (cp_parser *, bool);
1906 static void cp_parser_perform_template_parameter_access_checks
1907 (VEC (deferred_access_check,gc)*);
1908 static tree cp_parser_single_declaration
1909 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1910 static tree cp_parser_functional_cast
1911 (cp_parser *, tree);
1912 static tree cp_parser_save_member_function_body
1913 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1914 static tree cp_parser_enclosed_template_argument_list
1915 (cp_parser *);
1916 static void cp_parser_save_default_args
1917 (cp_parser *, tree);
1918 static void cp_parser_late_parsing_for_member
1919 (cp_parser *, tree);
1920 static void cp_parser_late_parsing_default_args
1921 (cp_parser *, tree);
1922 static tree cp_parser_sizeof_operand
1923 (cp_parser *, enum rid);
1924 static tree cp_parser_trait_expr
1925 (cp_parser *, enum rid);
1926 static bool cp_parser_declares_only_class_p
1927 (cp_parser *);
1928 static void cp_parser_set_storage_class
1929 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1930 static void cp_parser_set_decl_spec_type
1931 (cp_decl_specifier_seq *, tree, location_t, bool);
1932 static bool cp_parser_friend_p
1933 (const cp_decl_specifier_seq *);
1934 static void cp_parser_required_error
1935 (cp_parser *, required_token, bool);
1936 static cp_token *cp_parser_require
1937 (cp_parser *, enum cpp_ttype, required_token);
1938 static cp_token *cp_parser_require_keyword
1939 (cp_parser *, enum rid, required_token);
1940 static bool cp_parser_token_starts_function_definition_p
1941 (cp_token *);
1942 static bool cp_parser_next_token_starts_class_definition_p
1943 (cp_parser *);
1944 static bool cp_parser_next_token_ends_template_argument_p
1945 (cp_parser *);
1946 static bool cp_parser_nth_token_starts_template_argument_list_p
1947 (cp_parser *, size_t);
1948 static enum tag_types cp_parser_token_is_class_key
1949 (cp_token *);
1950 static void cp_parser_check_class_key
1951 (enum tag_types, tree type);
1952 static void cp_parser_check_access_in_redeclaration
1953 (tree type, location_t location);
1954 static bool cp_parser_optional_template_keyword
1955 (cp_parser *);
1956 static void cp_parser_pre_parsed_nested_name_specifier
1957 (cp_parser *);
1958 static bool cp_parser_cache_group
1959 (cp_parser *, enum cpp_ttype, unsigned);
1960 static void cp_parser_parse_tentatively
1961 (cp_parser *);
1962 static void cp_parser_commit_to_tentative_parse
1963 (cp_parser *);
1964 static void cp_parser_abort_tentative_parse
1965 (cp_parser *);
1966 static bool cp_parser_parse_definitely
1967 (cp_parser *);
1968 static inline bool cp_parser_parsing_tentatively
1969 (cp_parser *);
1970 static bool cp_parser_uncommitted_to_tentative_parse_p
1971 (cp_parser *);
1972 static void cp_parser_error
1973 (cp_parser *, const char *);
1974 static void cp_parser_name_lookup_error
1975 (cp_parser *, tree, tree, name_lookup_error, location_t);
1976 static bool cp_parser_simulate_error
1977 (cp_parser *);
1978 static bool cp_parser_check_type_definition
1979 (cp_parser *);
1980 static void cp_parser_check_for_definition_in_return_type
1981 (cp_declarator *, tree, location_t type_location);
1982 static void cp_parser_check_for_invalid_template_id
1983 (cp_parser *, tree, location_t location);
1984 static bool cp_parser_non_integral_constant_expression
1985 (cp_parser *, non_integral_constant);
1986 static void cp_parser_diagnose_invalid_type_name
1987 (cp_parser *, tree, tree, location_t);
1988 static bool cp_parser_parse_and_diagnose_invalid_type_name
1989 (cp_parser *);
1990 static int cp_parser_skip_to_closing_parenthesis
1991 (cp_parser *, bool, bool, bool);
1992 static void cp_parser_skip_to_end_of_statement
1993 (cp_parser *);
1994 static void cp_parser_consume_semicolon_at_end_of_statement
1995 (cp_parser *);
1996 static void cp_parser_skip_to_end_of_block_or_statement
1997 (cp_parser *);
1998 static bool cp_parser_skip_to_closing_brace
1999 (cp_parser *);
2000 static void cp_parser_skip_to_end_of_template_parameter_list
2001 (cp_parser *);
2002 static void cp_parser_skip_to_pragma_eol
2003 (cp_parser*, cp_token *);
2004 static bool cp_parser_error_occurred
2005 (cp_parser *);
2006 static bool cp_parser_allow_gnu_extensions_p
2007 (cp_parser *);
2008 static bool cp_parser_is_string_literal
2009 (cp_token *);
2010 static bool cp_parser_is_keyword
2011 (cp_token *, enum rid);
2012 static tree cp_parser_make_typename_type
2013 (cp_parser *, tree, tree, location_t location);
2014 static cp_declarator * cp_parser_make_indirect_declarator
2015 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2016
2017 /* Returns nonzero if we are parsing tentatively. */
2018
2019 static inline bool
2020 cp_parser_parsing_tentatively (cp_parser* parser)
2021 {
2022 return parser->context->next != NULL;
2023 }
2024
2025 /* Returns nonzero if TOKEN is a string literal. */
2026
2027 static bool
2028 cp_parser_is_string_literal (cp_token* token)
2029 {
2030 return (token->type == CPP_STRING ||
2031 token->type == CPP_STRING16 ||
2032 token->type == CPP_STRING32 ||
2033 token->type == CPP_WSTRING ||
2034 token->type == CPP_UTF8STRING);
2035 }
2036
2037 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2038
2039 static bool
2040 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2041 {
2042 return token->keyword == keyword;
2043 }
2044
2045 /* If not parsing tentatively, issue a diagnostic of the form
2046 FILE:LINE: MESSAGE before TOKEN
2047 where TOKEN is the next token in the input stream. MESSAGE
2048 (specified by the caller) is usually of the form "expected
2049 OTHER-TOKEN". */
2050
2051 static void
2052 cp_parser_error (cp_parser* parser, const char* gmsgid)
2053 {
2054 if (!cp_parser_simulate_error (parser))
2055 {
2056 cp_token *token = cp_lexer_peek_token (parser->lexer);
2057 /* This diagnostic makes more sense if it is tagged to the line
2058 of the token we just peeked at. */
2059 cp_lexer_set_source_position_from_token (token);
2060
2061 if (token->type == CPP_PRAGMA)
2062 {
2063 error_at (token->location,
2064 "%<#pragma%> is not allowed here");
2065 cp_parser_skip_to_pragma_eol (parser, token);
2066 return;
2067 }
2068
2069 c_parse_error (gmsgid,
2070 /* Because c_parser_error does not understand
2071 CPP_KEYWORD, keywords are treated like
2072 identifiers. */
2073 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2074 token->u.value, token->flags);
2075 }
2076 }
2077
2078 /* Issue an error about name-lookup failing. NAME is the
2079 IDENTIFIER_NODE DECL is the result of
2080 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2081 the thing that we hoped to find. */
2082
2083 static void
2084 cp_parser_name_lookup_error (cp_parser* parser,
2085 tree name,
2086 tree decl,
2087 name_lookup_error desired,
2088 location_t location)
2089 {
2090 /* If name lookup completely failed, tell the user that NAME was not
2091 declared. */
2092 if (decl == error_mark_node)
2093 {
2094 if (parser->scope && parser->scope != global_namespace)
2095 error_at (location, "%<%E::%E%> has not been declared",
2096 parser->scope, name);
2097 else if (parser->scope == global_namespace)
2098 error_at (location, "%<::%E%> has not been declared", name);
2099 else if (parser->object_scope
2100 && !CLASS_TYPE_P (parser->object_scope))
2101 error_at (location, "request for member %qE in non-class type %qT",
2102 name, parser->object_scope);
2103 else if (parser->object_scope)
2104 error_at (location, "%<%T::%E%> has not been declared",
2105 parser->object_scope, name);
2106 else
2107 error_at (location, "%qE has not been declared", name);
2108 }
2109 else if (parser->scope && parser->scope != global_namespace)
2110 {
2111 switch (desired)
2112 {
2113 case NLE_TYPE:
2114 error_at (location, "%<%E::%E%> is not a type",
2115 parser->scope, name);
2116 break;
2117 case NLE_CXX98:
2118 error_at (location, "%<%E::%E%> is not a class or namespace",
2119 parser->scope, name);
2120 break;
2121 case NLE_NOT_CXX98:
2122 error_at (location,
2123 "%<%E::%E%> is not a class, namespace, or enumeration",
2124 parser->scope, name);
2125 break;
2126 default:
2127 gcc_unreachable ();
2128
2129 }
2130 }
2131 else if (parser->scope == global_namespace)
2132 {
2133 switch (desired)
2134 {
2135 case NLE_TYPE:
2136 error_at (location, "%<::%E%> is not a type", name);
2137 break;
2138 case NLE_CXX98:
2139 error_at (location, "%<::%E%> is not a class or namespace", name);
2140 break;
2141 case NLE_NOT_CXX98:
2142 error_at (location,
2143 "%<::%E%> is not a class, namespace, or enumeration",
2144 name);
2145 break;
2146 default:
2147 gcc_unreachable ();
2148 }
2149 }
2150 else
2151 {
2152 switch (desired)
2153 {
2154 case NLE_TYPE:
2155 error_at (location, "%qE is not a type", name);
2156 break;
2157 case NLE_CXX98:
2158 error_at (location, "%qE is not a class or namespace", name);
2159 break;
2160 case NLE_NOT_CXX98:
2161 error_at (location,
2162 "%qE is not a class, namespace, or enumeration", name);
2163 break;
2164 default:
2165 gcc_unreachable ();
2166 }
2167 }
2168 }
2169
2170 /* If we are parsing tentatively, remember that an error has occurred
2171 during this tentative parse. Returns true if the error was
2172 simulated; false if a message should be issued by the caller. */
2173
2174 static bool
2175 cp_parser_simulate_error (cp_parser* parser)
2176 {
2177 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2178 {
2179 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2180 return true;
2181 }
2182 return false;
2183 }
2184
2185 /* Check for repeated decl-specifiers. */
2186
2187 static void
2188 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2189 location_t location)
2190 {
2191 int ds;
2192
2193 for (ds = ds_first; ds != ds_last; ++ds)
2194 {
2195 unsigned count = decl_specs->specs[ds];
2196 if (count < 2)
2197 continue;
2198 /* The "long" specifier is a special case because of "long long". */
2199 if (ds == ds_long)
2200 {
2201 if (count > 2)
2202 error_at (location, "%<long long long%> is too long for GCC");
2203 else
2204 pedwarn_cxx98 (location, OPT_Wlong_long,
2205 "ISO C++ 1998 does not support %<long long%>");
2206 }
2207 else if (count > 1)
2208 {
2209 static const char *const decl_spec_names[] = {
2210 "signed",
2211 "unsigned",
2212 "short",
2213 "long",
2214 "const",
2215 "volatile",
2216 "restrict",
2217 "inline",
2218 "virtual",
2219 "explicit",
2220 "friend",
2221 "typedef",
2222 "constexpr",
2223 "__complex",
2224 "__thread"
2225 };
2226 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2227 }
2228 }
2229 }
2230
2231 /* This function is called when a type is defined. If type
2232 definitions are forbidden at this point, an error message is
2233 issued. */
2234
2235 static bool
2236 cp_parser_check_type_definition (cp_parser* parser)
2237 {
2238 /* If types are forbidden here, issue a message. */
2239 if (parser->type_definition_forbidden_message)
2240 {
2241 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2242 in the message need to be interpreted. */
2243 error (parser->type_definition_forbidden_message);
2244 return false;
2245 }
2246 return true;
2247 }
2248
2249 /* This function is called when the DECLARATOR is processed. The TYPE
2250 was a type defined in the decl-specifiers. If it is invalid to
2251 define a type in the decl-specifiers for DECLARATOR, an error is
2252 issued. TYPE_LOCATION is the location of TYPE and is used
2253 for error reporting. */
2254
2255 static void
2256 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2257 tree type, location_t type_location)
2258 {
2259 /* [dcl.fct] forbids type definitions in return types.
2260 Unfortunately, it's not easy to know whether or not we are
2261 processing a return type until after the fact. */
2262 while (declarator
2263 && (declarator->kind == cdk_pointer
2264 || declarator->kind == cdk_reference
2265 || declarator->kind == cdk_ptrmem))
2266 declarator = declarator->declarator;
2267 if (declarator
2268 && declarator->kind == cdk_function)
2269 {
2270 error_at (type_location,
2271 "new types may not be defined in a return type");
2272 inform (type_location,
2273 "(perhaps a semicolon is missing after the definition of %qT)",
2274 type);
2275 }
2276 }
2277
2278 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2279 "<" in any valid C++ program. If the next token is indeed "<",
2280 issue a message warning the user about what appears to be an
2281 invalid attempt to form a template-id. LOCATION is the location
2282 of the type-specifier (TYPE) */
2283
2284 static void
2285 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2286 tree type, location_t location)
2287 {
2288 cp_token_position start = 0;
2289
2290 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2291 {
2292 if (TYPE_P (type))
2293 error_at (location, "%qT is not a template", type);
2294 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2295 error_at (location, "%qE is not a template", type);
2296 else
2297 error_at (location, "invalid template-id");
2298 /* Remember the location of the invalid "<". */
2299 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2300 start = cp_lexer_token_position (parser->lexer, true);
2301 /* Consume the "<". */
2302 cp_lexer_consume_token (parser->lexer);
2303 /* Parse the template arguments. */
2304 cp_parser_enclosed_template_argument_list (parser);
2305 /* Permanently remove the invalid template arguments so that
2306 this error message is not issued again. */
2307 if (start)
2308 cp_lexer_purge_tokens_after (parser->lexer, start);
2309 }
2310 }
2311
2312 /* If parsing an integral constant-expression, issue an error message
2313 about the fact that THING appeared and return true. Otherwise,
2314 return false. In either case, set
2315 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2316
2317 static bool
2318 cp_parser_non_integral_constant_expression (cp_parser *parser,
2319 non_integral_constant thing)
2320 {
2321 parser->non_integral_constant_expression_p = true;
2322 if (parser->integral_constant_expression_p)
2323 {
2324 if (!parser->allow_non_integral_constant_expression_p)
2325 {
2326 const char *msg = NULL;
2327 switch (thing)
2328 {
2329 case NIC_FLOAT:
2330 error ("floating-point literal "
2331 "cannot appear in a constant-expression");
2332 return true;
2333 case NIC_CAST:
2334 error ("a cast to a type other than an integral or "
2335 "enumeration type cannot appear in a "
2336 "constant-expression");
2337 return true;
2338 case NIC_TYPEID:
2339 error ("%<typeid%> operator "
2340 "cannot appear in a constant-expression");
2341 return true;
2342 case NIC_NCC:
2343 error ("non-constant compound literals "
2344 "cannot appear in a constant-expression");
2345 return true;
2346 case NIC_FUNC_CALL:
2347 error ("a function call "
2348 "cannot appear in a constant-expression");
2349 return true;
2350 case NIC_INC:
2351 error ("an increment "
2352 "cannot appear in a constant-expression");
2353 return true;
2354 case NIC_DEC:
2355 error ("an decrement "
2356 "cannot appear in a constant-expression");
2357 return true;
2358 case NIC_ARRAY_REF:
2359 error ("an array reference "
2360 "cannot appear in a constant-expression");
2361 return true;
2362 case NIC_ADDR_LABEL:
2363 error ("the address of a label "
2364 "cannot appear in a constant-expression");
2365 return true;
2366 case NIC_OVERLOADED:
2367 error ("calls to overloaded operators "
2368 "cannot appear in a constant-expression");
2369 return true;
2370 case NIC_ASSIGNMENT:
2371 error ("an assignment cannot appear in a constant-expression");
2372 return true;
2373 case NIC_COMMA:
2374 error ("a comma operator "
2375 "cannot appear in a constant-expression");
2376 return true;
2377 case NIC_CONSTRUCTOR:
2378 error ("a call to a constructor "
2379 "cannot appear in a constant-expression");
2380 return true;
2381 case NIC_THIS:
2382 msg = "this";
2383 break;
2384 case NIC_FUNC_NAME:
2385 msg = "__FUNCTION__";
2386 break;
2387 case NIC_PRETTY_FUNC:
2388 msg = "__PRETTY_FUNCTION__";
2389 break;
2390 case NIC_C99_FUNC:
2391 msg = "__func__";
2392 break;
2393 case NIC_VA_ARG:
2394 msg = "va_arg";
2395 break;
2396 case NIC_ARROW:
2397 msg = "->";
2398 break;
2399 case NIC_POINT:
2400 msg = ".";
2401 break;
2402 case NIC_STAR:
2403 msg = "*";
2404 break;
2405 case NIC_ADDR:
2406 msg = "&";
2407 break;
2408 case NIC_PREINCREMENT:
2409 msg = "++";
2410 break;
2411 case NIC_PREDECREMENT:
2412 msg = "--";
2413 break;
2414 case NIC_NEW:
2415 msg = "new";
2416 break;
2417 case NIC_DEL:
2418 msg = "delete";
2419 break;
2420 default:
2421 gcc_unreachable ();
2422 }
2423 if (msg)
2424 error ("%qs cannot appear in a constant-expression", msg);
2425 return true;
2426 }
2427 }
2428 return false;
2429 }
2430
2431 /* Emit a diagnostic for an invalid type name. SCOPE is the
2432 qualifying scope (or NULL, if none) for ID. This function commits
2433 to the current active tentative parse, if any. (Otherwise, the
2434 problematic construct might be encountered again later, resulting
2435 in duplicate error messages.) LOCATION is the location of ID. */
2436
2437 static void
2438 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2439 tree scope, tree id,
2440 location_t location)
2441 {
2442 tree decl, old_scope;
2443 /* Try to lookup the identifier. */
2444 old_scope = parser->scope;
2445 parser->scope = scope;
2446 decl = cp_parser_lookup_name_simple (parser, id, location);
2447 parser->scope = old_scope;
2448 /* If the lookup found a template-name, it means that the user forgot
2449 to specify an argument list. Emit a useful error message. */
2450 if (TREE_CODE (decl) == TEMPLATE_DECL)
2451 error_at (location,
2452 "invalid use of template-name %qE without an argument list",
2453 decl);
2454 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2455 error_at (location, "invalid use of destructor %qD as a type", id);
2456 else if (TREE_CODE (decl) == TYPE_DECL)
2457 /* Something like 'unsigned A a;' */
2458 error_at (location, "invalid combination of multiple type-specifiers");
2459 else if (!parser->scope)
2460 {
2461 /* Issue an error message. */
2462 error_at (location, "%qE does not name a type", id);
2463 /* If we're in a template class, it's possible that the user was
2464 referring to a type from a base class. For example:
2465
2466 template <typename T> struct A { typedef T X; };
2467 template <typename T> struct B : public A<T> { X x; };
2468
2469 The user should have said "typename A<T>::X". */
2470 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2471 inform (location, "C++0x %<constexpr%> only available with "
2472 "-std=c++0x or -std=gnu++0x");
2473 else if (processing_template_decl && current_class_type
2474 && TYPE_BINFO (current_class_type))
2475 {
2476 tree b;
2477
2478 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2479 b;
2480 b = TREE_CHAIN (b))
2481 {
2482 tree base_type = BINFO_TYPE (b);
2483 if (CLASS_TYPE_P (base_type)
2484 && dependent_type_p (base_type))
2485 {
2486 tree field;
2487 /* Go from a particular instantiation of the
2488 template (which will have an empty TYPE_FIELDs),
2489 to the main version. */
2490 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2491 for (field = TYPE_FIELDS (base_type);
2492 field;
2493 field = DECL_CHAIN (field))
2494 if (TREE_CODE (field) == TYPE_DECL
2495 && DECL_NAME (field) == id)
2496 {
2497 inform (location,
2498 "(perhaps %<typename %T::%E%> was intended)",
2499 BINFO_TYPE (b), id);
2500 break;
2501 }
2502 if (field)
2503 break;
2504 }
2505 }
2506 }
2507 }
2508 /* Here we diagnose qualified-ids where the scope is actually correct,
2509 but the identifier does not resolve to a valid type name. */
2510 else if (parser->scope != error_mark_node)
2511 {
2512 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2513 error_at (location, "%qE in namespace %qE does not name a type",
2514 id, parser->scope);
2515 else if (CLASS_TYPE_P (parser->scope)
2516 && constructor_name_p (id, parser->scope))
2517 {
2518 /* A<T>::A<T>() */
2519 error_at (location, "%<%T::%E%> names the constructor, not"
2520 " the type", parser->scope, id);
2521 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2522 error_at (location, "and %qT has no template constructors",
2523 parser->scope);
2524 }
2525 else if (TYPE_P (parser->scope)
2526 && dependent_scope_p (parser->scope))
2527 error_at (location, "need %<typename%> before %<%T::%E%> because "
2528 "%qT is a dependent scope",
2529 parser->scope, id, parser->scope);
2530 else if (TYPE_P (parser->scope))
2531 error_at (location, "%qE in class %qT does not name a type",
2532 id, parser->scope);
2533 else
2534 gcc_unreachable ();
2535 }
2536 cp_parser_commit_to_tentative_parse (parser);
2537 }
2538
2539 /* Check for a common situation where a type-name should be present,
2540 but is not, and issue a sensible error message. Returns true if an
2541 invalid type-name was detected.
2542
2543 The situation handled by this function are variable declarations of the
2544 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2545 Usually, `ID' should name a type, but if we got here it means that it
2546 does not. We try to emit the best possible error message depending on
2547 how exactly the id-expression looks like. */
2548
2549 static bool
2550 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2551 {
2552 tree id;
2553 cp_token *token = cp_lexer_peek_token (parser->lexer);
2554
2555 /* Avoid duplicate error about ambiguous lookup. */
2556 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2557 {
2558 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2559 if (next->type == CPP_NAME && next->ambiguous_p)
2560 goto out;
2561 }
2562
2563 cp_parser_parse_tentatively (parser);
2564 id = cp_parser_id_expression (parser,
2565 /*template_keyword_p=*/false,
2566 /*check_dependency_p=*/true,
2567 /*template_p=*/NULL,
2568 /*declarator_p=*/true,
2569 /*optional_p=*/false);
2570 /* If the next token is a (, this is a function with no explicit return
2571 type, i.e. constructor, destructor or conversion op. */
2572 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2573 || TREE_CODE (id) == TYPE_DECL)
2574 {
2575 cp_parser_abort_tentative_parse (parser);
2576 return false;
2577 }
2578 if (!cp_parser_parse_definitely (parser))
2579 return false;
2580
2581 /* Emit a diagnostic for the invalid type. */
2582 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2583 id, token->location);
2584 out:
2585 /* If we aren't in the middle of a declarator (i.e. in a
2586 parameter-declaration-clause), skip to the end of the declaration;
2587 there's no point in trying to process it. */
2588 if (!parser->in_declarator_p)
2589 cp_parser_skip_to_end_of_block_or_statement (parser);
2590 return true;
2591 }
2592
2593 /* Consume tokens up to, and including, the next non-nested closing `)'.
2594 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2595 are doing error recovery. Returns -1 if OR_COMMA is true and we
2596 found an unnested comma. */
2597
2598 static int
2599 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2600 bool recovering,
2601 bool or_comma,
2602 bool consume_paren)
2603 {
2604 unsigned paren_depth = 0;
2605 unsigned brace_depth = 0;
2606 unsigned square_depth = 0;
2607
2608 if (recovering && !or_comma
2609 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2610 return 0;
2611
2612 while (true)
2613 {
2614 cp_token * token = cp_lexer_peek_token (parser->lexer);
2615
2616 switch (token->type)
2617 {
2618 case CPP_EOF:
2619 case CPP_PRAGMA_EOL:
2620 /* If we've run out of tokens, then there is no closing `)'. */
2621 return 0;
2622
2623 /* This is good for lambda expression capture-lists. */
2624 case CPP_OPEN_SQUARE:
2625 ++square_depth;
2626 break;
2627 case CPP_CLOSE_SQUARE:
2628 if (!square_depth--)
2629 return 0;
2630 break;
2631
2632 case CPP_SEMICOLON:
2633 /* This matches the processing in skip_to_end_of_statement. */
2634 if (!brace_depth)
2635 return 0;
2636 break;
2637
2638 case CPP_OPEN_BRACE:
2639 ++brace_depth;
2640 break;
2641 case CPP_CLOSE_BRACE:
2642 if (!brace_depth--)
2643 return 0;
2644 break;
2645
2646 case CPP_COMMA:
2647 if (recovering && or_comma && !brace_depth && !paren_depth
2648 && !square_depth)
2649 return -1;
2650 break;
2651
2652 case CPP_OPEN_PAREN:
2653 if (!brace_depth)
2654 ++paren_depth;
2655 break;
2656
2657 case CPP_CLOSE_PAREN:
2658 if (!brace_depth && !paren_depth--)
2659 {
2660 if (consume_paren)
2661 cp_lexer_consume_token (parser->lexer);
2662 return 1;
2663 }
2664 break;
2665
2666 default:
2667 break;
2668 }
2669
2670 /* Consume the token. */
2671 cp_lexer_consume_token (parser->lexer);
2672 }
2673 }
2674
2675 /* Consume tokens until we reach the end of the current statement.
2676 Normally, that will be just before consuming a `;'. However, if a
2677 non-nested `}' comes first, then we stop before consuming that. */
2678
2679 static void
2680 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2681 {
2682 unsigned nesting_depth = 0;
2683
2684 while (true)
2685 {
2686 cp_token *token = cp_lexer_peek_token (parser->lexer);
2687
2688 switch (token->type)
2689 {
2690 case CPP_EOF:
2691 case CPP_PRAGMA_EOL:
2692 /* If we've run out of tokens, stop. */
2693 return;
2694
2695 case CPP_SEMICOLON:
2696 /* If the next token is a `;', we have reached the end of the
2697 statement. */
2698 if (!nesting_depth)
2699 return;
2700 break;
2701
2702 case CPP_CLOSE_BRACE:
2703 /* If this is a non-nested '}', stop before consuming it.
2704 That way, when confronted with something like:
2705
2706 { 3 + }
2707
2708 we stop before consuming the closing '}', even though we
2709 have not yet reached a `;'. */
2710 if (nesting_depth == 0)
2711 return;
2712
2713 /* If it is the closing '}' for a block that we have
2714 scanned, stop -- but only after consuming the token.
2715 That way given:
2716
2717 void f g () { ... }
2718 typedef int I;
2719
2720 we will stop after the body of the erroneously declared
2721 function, but before consuming the following `typedef'
2722 declaration. */
2723 if (--nesting_depth == 0)
2724 {
2725 cp_lexer_consume_token (parser->lexer);
2726 return;
2727 }
2728
2729 case CPP_OPEN_BRACE:
2730 ++nesting_depth;
2731 break;
2732
2733 default:
2734 break;
2735 }
2736
2737 /* Consume the token. */
2738 cp_lexer_consume_token (parser->lexer);
2739 }
2740 }
2741
2742 /* This function is called at the end of a statement or declaration.
2743 If the next token is a semicolon, it is consumed; otherwise, error
2744 recovery is attempted. */
2745
2746 static void
2747 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2748 {
2749 /* Look for the trailing `;'. */
2750 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2751 {
2752 /* If there is additional (erroneous) input, skip to the end of
2753 the statement. */
2754 cp_parser_skip_to_end_of_statement (parser);
2755 /* If the next token is now a `;', consume it. */
2756 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2757 cp_lexer_consume_token (parser->lexer);
2758 }
2759 }
2760
2761 /* Skip tokens until we have consumed an entire block, or until we
2762 have consumed a non-nested `;'. */
2763
2764 static void
2765 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2766 {
2767 int nesting_depth = 0;
2768
2769 while (nesting_depth >= 0)
2770 {
2771 cp_token *token = cp_lexer_peek_token (parser->lexer);
2772
2773 switch (token->type)
2774 {
2775 case CPP_EOF:
2776 case CPP_PRAGMA_EOL:
2777 /* If we've run out of tokens, stop. */
2778 return;
2779
2780 case CPP_SEMICOLON:
2781 /* Stop if this is an unnested ';'. */
2782 if (!nesting_depth)
2783 nesting_depth = -1;
2784 break;
2785
2786 case CPP_CLOSE_BRACE:
2787 /* Stop if this is an unnested '}', or closes the outermost
2788 nesting level. */
2789 nesting_depth--;
2790 if (nesting_depth < 0)
2791 return;
2792 if (!nesting_depth)
2793 nesting_depth = -1;
2794 break;
2795
2796 case CPP_OPEN_BRACE:
2797 /* Nest. */
2798 nesting_depth++;
2799 break;
2800
2801 default:
2802 break;
2803 }
2804
2805 /* Consume the token. */
2806 cp_lexer_consume_token (parser->lexer);
2807 }
2808 }
2809
2810 /* Skip tokens until a non-nested closing curly brace is the next
2811 token, or there are no more tokens. Return true in the first case,
2812 false otherwise. */
2813
2814 static bool
2815 cp_parser_skip_to_closing_brace (cp_parser *parser)
2816 {
2817 unsigned nesting_depth = 0;
2818
2819 while (true)
2820 {
2821 cp_token *token = cp_lexer_peek_token (parser->lexer);
2822
2823 switch (token->type)
2824 {
2825 case CPP_EOF:
2826 case CPP_PRAGMA_EOL:
2827 /* If we've run out of tokens, stop. */
2828 return false;
2829
2830 case CPP_CLOSE_BRACE:
2831 /* If the next token is a non-nested `}', then we have reached
2832 the end of the current block. */
2833 if (nesting_depth-- == 0)
2834 return true;
2835 break;
2836
2837 case CPP_OPEN_BRACE:
2838 /* If it the next token is a `{', then we are entering a new
2839 block. Consume the entire block. */
2840 ++nesting_depth;
2841 break;
2842
2843 default:
2844 break;
2845 }
2846
2847 /* Consume the token. */
2848 cp_lexer_consume_token (parser->lexer);
2849 }
2850 }
2851
2852 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2853 parameter is the PRAGMA token, allowing us to purge the entire pragma
2854 sequence. */
2855
2856 static void
2857 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2858 {
2859 cp_token *token;
2860
2861 parser->lexer->in_pragma = false;
2862
2863 do
2864 token = cp_lexer_consume_token (parser->lexer);
2865 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2866
2867 /* Ensure that the pragma is not parsed again. */
2868 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2869 }
2870
2871 /* Require pragma end of line, resyncing with it as necessary. The
2872 arguments are as for cp_parser_skip_to_pragma_eol. */
2873
2874 static void
2875 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2876 {
2877 parser->lexer->in_pragma = false;
2878 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2879 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2880 }
2881
2882 /* This is a simple wrapper around make_typename_type. When the id is
2883 an unresolved identifier node, we can provide a superior diagnostic
2884 using cp_parser_diagnose_invalid_type_name. */
2885
2886 static tree
2887 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2888 tree id, location_t id_location)
2889 {
2890 tree result;
2891 if (TREE_CODE (id) == IDENTIFIER_NODE)
2892 {
2893 result = make_typename_type (scope, id, typename_type,
2894 /*complain=*/tf_none);
2895 if (result == error_mark_node)
2896 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2897 return result;
2898 }
2899 return make_typename_type (scope, id, typename_type, tf_error);
2900 }
2901
2902 /* This is a wrapper around the
2903 make_{pointer,ptrmem,reference}_declarator functions that decides
2904 which one to call based on the CODE and CLASS_TYPE arguments. The
2905 CODE argument should be one of the values returned by
2906 cp_parser_ptr_operator. */
2907 static cp_declarator *
2908 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2909 cp_cv_quals cv_qualifiers,
2910 cp_declarator *target)
2911 {
2912 if (code == ERROR_MARK)
2913 return cp_error_declarator;
2914
2915 if (code == INDIRECT_REF)
2916 if (class_type == NULL_TREE)
2917 return make_pointer_declarator (cv_qualifiers, target);
2918 else
2919 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2920 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2921 return make_reference_declarator (cv_qualifiers, target, false);
2922 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2923 return make_reference_declarator (cv_qualifiers, target, true);
2924 gcc_unreachable ();
2925 }
2926
2927 /* Create a new C++ parser. */
2928
2929 static cp_parser *
2930 cp_parser_new (void)
2931 {
2932 cp_parser *parser;
2933 cp_lexer *lexer;
2934 unsigned i;
2935
2936 /* cp_lexer_new_main is called before doing GC allocation because
2937 cp_lexer_new_main might load a PCH file. */
2938 lexer = cp_lexer_new_main ();
2939
2940 /* Initialize the binops_by_token so that we can get the tree
2941 directly from the token. */
2942 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2943 binops_by_token[binops[i].token_type] = binops[i];
2944
2945 parser = ggc_alloc_cleared_cp_parser ();
2946 parser->lexer = lexer;
2947 parser->context = cp_parser_context_new (NULL);
2948
2949 /* For now, we always accept GNU extensions. */
2950 parser->allow_gnu_extensions_p = 1;
2951
2952 /* The `>' token is a greater-than operator, not the end of a
2953 template-id. */
2954 parser->greater_than_is_operator_p = true;
2955
2956 parser->default_arg_ok_p = true;
2957
2958 /* We are not parsing a constant-expression. */
2959 parser->integral_constant_expression_p = false;
2960 parser->allow_non_integral_constant_expression_p = false;
2961 parser->non_integral_constant_expression_p = false;
2962
2963 /* Local variable names are not forbidden. */
2964 parser->local_variables_forbidden_p = false;
2965
2966 /* We are not processing an `extern "C"' declaration. */
2967 parser->in_unbraced_linkage_specification_p = false;
2968
2969 /* We are not processing a declarator. */
2970 parser->in_declarator_p = false;
2971
2972 /* We are not processing a template-argument-list. */
2973 parser->in_template_argument_list_p = false;
2974
2975 /* We are not in an iteration statement. */
2976 parser->in_statement = 0;
2977
2978 /* We are not in a switch statement. */
2979 parser->in_switch_statement_p = false;
2980
2981 /* We are not parsing a type-id inside an expression. */
2982 parser->in_type_id_in_expr_p = false;
2983
2984 /* Declarations aren't implicitly extern "C". */
2985 parser->implicit_extern_c = false;
2986
2987 /* String literals should be translated to the execution character set. */
2988 parser->translate_strings_p = true;
2989
2990 /* We are not parsing a function body. */
2991 parser->in_function_body = false;
2992
2993 /* We can correct until told otherwise. */
2994 parser->colon_corrects_to_scope_p = true;
2995
2996 /* The unparsed function queue is empty. */
2997 push_unparsed_function_queues (parser);
2998
2999 /* There are no classes being defined. */
3000 parser->num_classes_being_defined = 0;
3001
3002 /* No template parameters apply. */
3003 parser->num_template_parameter_lists = 0;
3004
3005 return parser;
3006 }
3007
3008 /* Create a cp_lexer structure which will emit the tokens in CACHE
3009 and push it onto the parser's lexer stack. This is used for delayed
3010 parsing of in-class method bodies and default arguments, and should
3011 not be confused with tentative parsing. */
3012 static void
3013 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3014 {
3015 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3016 lexer->next = parser->lexer;
3017 parser->lexer = lexer;
3018
3019 /* Move the current source position to that of the first token in the
3020 new lexer. */
3021 cp_lexer_set_source_position_from_token (lexer->next_token);
3022 }
3023
3024 /* Pop the top lexer off the parser stack. This is never used for the
3025 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3026 static void
3027 cp_parser_pop_lexer (cp_parser *parser)
3028 {
3029 cp_lexer *lexer = parser->lexer;
3030 parser->lexer = lexer->next;
3031 cp_lexer_destroy (lexer);
3032
3033 /* Put the current source position back where it was before this
3034 lexer was pushed. */
3035 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3036 }
3037
3038 /* Lexical conventions [gram.lex] */
3039
3040 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3041 identifier. */
3042
3043 static tree
3044 cp_parser_identifier (cp_parser* parser)
3045 {
3046 cp_token *token;
3047
3048 /* Look for the identifier. */
3049 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3050 /* Return the value. */
3051 return token ? token->u.value : error_mark_node;
3052 }
3053
3054 /* Parse a sequence of adjacent string constants. Returns a
3055 TREE_STRING representing the combined, nul-terminated string
3056 constant. If TRANSLATE is true, translate the string to the
3057 execution character set. If WIDE_OK is true, a wide string is
3058 invalid here.
3059
3060 C++98 [lex.string] says that if a narrow string literal token is
3061 adjacent to a wide string literal token, the behavior is undefined.
3062 However, C99 6.4.5p4 says that this results in a wide string literal.
3063 We follow C99 here, for consistency with the C front end.
3064
3065 This code is largely lifted from lex_string() in c-lex.c.
3066
3067 FUTURE: ObjC++ will need to handle @-strings here. */
3068 static tree
3069 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3070 {
3071 tree value;
3072 size_t count;
3073 struct obstack str_ob;
3074 cpp_string str, istr, *strs;
3075 cp_token *tok;
3076 enum cpp_ttype type;
3077
3078 tok = cp_lexer_peek_token (parser->lexer);
3079 if (!cp_parser_is_string_literal (tok))
3080 {
3081 cp_parser_error (parser, "expected string-literal");
3082 return error_mark_node;
3083 }
3084
3085 type = tok->type;
3086
3087 /* Try to avoid the overhead of creating and destroying an obstack
3088 for the common case of just one string. */
3089 if (!cp_parser_is_string_literal
3090 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3091 {
3092 cp_lexer_consume_token (parser->lexer);
3093
3094 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3095 str.len = TREE_STRING_LENGTH (tok->u.value);
3096 count = 1;
3097
3098 strs = &str;
3099 }
3100 else
3101 {
3102 gcc_obstack_init (&str_ob);
3103 count = 0;
3104
3105 do
3106 {
3107 cp_lexer_consume_token (parser->lexer);
3108 count++;
3109 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3110 str.len = TREE_STRING_LENGTH (tok->u.value);
3111
3112 if (type != tok->type)
3113 {
3114 if (type == CPP_STRING)
3115 type = tok->type;
3116 else if (tok->type != CPP_STRING)
3117 error_at (tok->location,
3118 "unsupported non-standard concatenation "
3119 "of string literals");
3120 }
3121
3122 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3123
3124 tok = cp_lexer_peek_token (parser->lexer);
3125 }
3126 while (cp_parser_is_string_literal (tok));
3127
3128 strs = (cpp_string *) obstack_finish (&str_ob);
3129 }
3130
3131 if (type != CPP_STRING && !wide_ok)
3132 {
3133 cp_parser_error (parser, "a wide string is invalid in this context");
3134 type = CPP_STRING;
3135 }
3136
3137 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3138 (parse_in, strs, count, &istr, type))
3139 {
3140 value = build_string (istr.len, (const char *)istr.text);
3141 free (CONST_CAST (unsigned char *, istr.text));
3142
3143 switch (type)
3144 {
3145 default:
3146 case CPP_STRING:
3147 case CPP_UTF8STRING:
3148 TREE_TYPE (value) = char_array_type_node;
3149 break;
3150 case CPP_STRING16:
3151 TREE_TYPE (value) = char16_array_type_node;
3152 break;
3153 case CPP_STRING32:
3154 TREE_TYPE (value) = char32_array_type_node;
3155 break;
3156 case CPP_WSTRING:
3157 TREE_TYPE (value) = wchar_array_type_node;
3158 break;
3159 }
3160
3161 value = fix_string_type (value);
3162 }
3163 else
3164 /* cpp_interpret_string has issued an error. */
3165 value = error_mark_node;
3166
3167 if (count > 1)
3168 obstack_free (&str_ob, 0);
3169
3170 return value;
3171 }
3172
3173
3174 /* Basic concepts [gram.basic] */
3175
3176 /* Parse a translation-unit.
3177
3178 translation-unit:
3179 declaration-seq [opt]
3180
3181 Returns TRUE if all went well. */
3182
3183 static bool
3184 cp_parser_translation_unit (cp_parser* parser)
3185 {
3186 /* The address of the first non-permanent object on the declarator
3187 obstack. */
3188 static void *declarator_obstack_base;
3189
3190 bool success;
3191
3192 /* Create the declarator obstack, if necessary. */
3193 if (!cp_error_declarator)
3194 {
3195 gcc_obstack_init (&declarator_obstack);
3196 /* Create the error declarator. */
3197 cp_error_declarator = make_declarator (cdk_error);
3198 /* Create the empty parameter list. */
3199 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3200 /* Remember where the base of the declarator obstack lies. */
3201 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3202 }
3203
3204 cp_parser_declaration_seq_opt (parser);
3205
3206 /* If there are no tokens left then all went well. */
3207 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3208 {
3209 /* Get rid of the token array; we don't need it any more. */
3210 cp_lexer_destroy (parser->lexer);
3211 parser->lexer = NULL;
3212
3213 /* This file might have been a context that's implicitly extern
3214 "C". If so, pop the lang context. (Only relevant for PCH.) */
3215 if (parser->implicit_extern_c)
3216 {
3217 pop_lang_context ();
3218 parser->implicit_extern_c = false;
3219 }
3220
3221 /* Finish up. */
3222 finish_translation_unit ();
3223
3224 success = true;
3225 }
3226 else
3227 {
3228 cp_parser_error (parser, "expected declaration");
3229 success = false;
3230 }
3231
3232 /* Make sure the declarator obstack was fully cleaned up. */
3233 gcc_assert (obstack_next_free (&declarator_obstack)
3234 == declarator_obstack_base);
3235
3236 /* All went well. */
3237 return success;
3238 }
3239
3240 /* Expressions [gram.expr] */
3241
3242 /* Parse a primary-expression.
3243
3244 primary-expression:
3245 literal
3246 this
3247 ( expression )
3248 id-expression
3249
3250 GNU Extensions:
3251
3252 primary-expression:
3253 ( compound-statement )
3254 __builtin_va_arg ( assignment-expression , type-id )
3255 __builtin_offsetof ( type-id , offsetof-expression )
3256
3257 C++ Extensions:
3258 __has_nothrow_assign ( type-id )
3259 __has_nothrow_constructor ( type-id )
3260 __has_nothrow_copy ( type-id )
3261 __has_trivial_assign ( type-id )
3262 __has_trivial_constructor ( type-id )
3263 __has_trivial_copy ( type-id )
3264 __has_trivial_destructor ( type-id )
3265 __has_virtual_destructor ( type-id )
3266 __is_abstract ( type-id )
3267 __is_base_of ( type-id , type-id )
3268 __is_class ( type-id )
3269 __is_convertible_to ( type-id , type-id )
3270 __is_empty ( type-id )
3271 __is_enum ( type-id )
3272 __is_pod ( type-id )
3273 __is_polymorphic ( type-id )
3274 __is_union ( type-id )
3275
3276 Objective-C++ Extension:
3277
3278 primary-expression:
3279 objc-expression
3280
3281 literal:
3282 __null
3283
3284 ADDRESS_P is true iff this expression was immediately preceded by
3285 "&" and therefore might denote a pointer-to-member. CAST_P is true
3286 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3287 true iff this expression is a template argument.
3288
3289 Returns a representation of the expression. Upon return, *IDK
3290 indicates what kind of id-expression (if any) was present. */
3291
3292 static tree
3293 cp_parser_primary_expression (cp_parser *parser,
3294 bool address_p,
3295 bool cast_p,
3296 bool template_arg_p,
3297 cp_id_kind *idk)
3298 {
3299 cp_token *token = NULL;
3300
3301 /* Assume the primary expression is not an id-expression. */
3302 *idk = CP_ID_KIND_NONE;
3303
3304 /* Peek at the next token. */
3305 token = cp_lexer_peek_token (parser->lexer);
3306 switch (token->type)
3307 {
3308 /* literal:
3309 integer-literal
3310 character-literal
3311 floating-literal
3312 string-literal
3313 boolean-literal */
3314 case CPP_CHAR:
3315 case CPP_CHAR16:
3316 case CPP_CHAR32:
3317 case CPP_WCHAR:
3318 case CPP_NUMBER:
3319 token = cp_lexer_consume_token (parser->lexer);
3320 if (TREE_CODE (token->u.value) == FIXED_CST)
3321 {
3322 error_at (token->location,
3323 "fixed-point types not supported in C++");
3324 return error_mark_node;
3325 }
3326 /* Floating-point literals are only allowed in an integral
3327 constant expression if they are cast to an integral or
3328 enumeration type. */
3329 if (TREE_CODE (token->u.value) == REAL_CST
3330 && parser->integral_constant_expression_p
3331 && pedantic)
3332 {
3333 /* CAST_P will be set even in invalid code like "int(2.7 +
3334 ...)". Therefore, we have to check that the next token
3335 is sure to end the cast. */
3336 if (cast_p)
3337 {
3338 cp_token *next_token;
3339
3340 next_token = cp_lexer_peek_token (parser->lexer);
3341 if (/* The comma at the end of an
3342 enumerator-definition. */
3343 next_token->type != CPP_COMMA
3344 /* The curly brace at the end of an enum-specifier. */
3345 && next_token->type != CPP_CLOSE_BRACE
3346 /* The end of a statement. */
3347 && next_token->type != CPP_SEMICOLON
3348 /* The end of the cast-expression. */
3349 && next_token->type != CPP_CLOSE_PAREN
3350 /* The end of an array bound. */
3351 && next_token->type != CPP_CLOSE_SQUARE
3352 /* The closing ">" in a template-argument-list. */
3353 && (next_token->type != CPP_GREATER
3354 || parser->greater_than_is_operator_p)
3355 /* C++0x only: A ">>" treated like two ">" tokens,
3356 in a template-argument-list. */
3357 && (next_token->type != CPP_RSHIFT
3358 || (cxx_dialect == cxx98)
3359 || parser->greater_than_is_operator_p))
3360 cast_p = false;
3361 }
3362
3363 /* If we are within a cast, then the constraint that the
3364 cast is to an integral or enumeration type will be
3365 checked at that point. If we are not within a cast, then
3366 this code is invalid. */
3367 if (!cast_p)
3368 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3369 }
3370 return token->u.value;
3371
3372 case CPP_STRING:
3373 case CPP_STRING16:
3374 case CPP_STRING32:
3375 case CPP_WSTRING:
3376 case CPP_UTF8STRING:
3377 /* ??? Should wide strings be allowed when parser->translate_strings_p
3378 is false (i.e. in attributes)? If not, we can kill the third
3379 argument to cp_parser_string_literal. */
3380 return cp_parser_string_literal (parser,
3381 parser->translate_strings_p,
3382 true);
3383
3384 case CPP_OPEN_PAREN:
3385 {
3386 tree expr;
3387 bool saved_greater_than_is_operator_p;
3388
3389 /* Consume the `('. */
3390 cp_lexer_consume_token (parser->lexer);
3391 /* Within a parenthesized expression, a `>' token is always
3392 the greater-than operator. */
3393 saved_greater_than_is_operator_p
3394 = parser->greater_than_is_operator_p;
3395 parser->greater_than_is_operator_p = true;
3396 /* If we see `( { ' then we are looking at the beginning of
3397 a GNU statement-expression. */
3398 if (cp_parser_allow_gnu_extensions_p (parser)
3399 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3400 {
3401 /* Statement-expressions are not allowed by the standard. */
3402 pedwarn (token->location, OPT_pedantic,
3403 "ISO C++ forbids braced-groups within expressions");
3404
3405 /* And they're not allowed outside of a function-body; you
3406 cannot, for example, write:
3407
3408 int i = ({ int j = 3; j + 1; });
3409
3410 at class or namespace scope. */
3411 if (!parser->in_function_body
3412 || parser->in_template_argument_list_p)
3413 {
3414 error_at (token->location,
3415 "statement-expressions are not allowed outside "
3416 "functions nor in template-argument lists");
3417 cp_parser_skip_to_end_of_block_or_statement (parser);
3418 expr = error_mark_node;
3419 }
3420 else
3421 {
3422 /* Start the statement-expression. */
3423 expr = begin_stmt_expr ();
3424 /* Parse the compound-statement. */
3425 cp_parser_compound_statement (parser, expr, false, false);
3426 /* Finish up. */
3427 expr = finish_stmt_expr (expr, false);
3428 }
3429 }
3430 else
3431 {
3432 /* Parse the parenthesized expression. */
3433 expr = cp_parser_expression (parser, cast_p, idk);
3434 /* Let the front end know that this expression was
3435 enclosed in parentheses. This matters in case, for
3436 example, the expression is of the form `A::B', since
3437 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3438 not. */
3439 finish_parenthesized_expr (expr);
3440 /* DR 705: Wrapping an unqualified name in parentheses
3441 suppresses arg-dependent lookup. We want to pass back
3442 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3443 (c++/37862), but none of the others. */
3444 if (*idk != CP_ID_KIND_QUALIFIED)
3445 *idk = CP_ID_KIND_NONE;
3446 }
3447 /* The `>' token might be the end of a template-id or
3448 template-parameter-list now. */
3449 parser->greater_than_is_operator_p
3450 = saved_greater_than_is_operator_p;
3451 /* Consume the `)'. */
3452 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3453 cp_parser_skip_to_end_of_statement (parser);
3454
3455 return expr;
3456 }
3457
3458 case CPP_OPEN_SQUARE:
3459 if (c_dialect_objc ())
3460 /* We have an Objective-C++ message. */
3461 return cp_parser_objc_expression (parser);
3462 {
3463 tree lam = cp_parser_lambda_expression (parser);
3464 /* Don't warn about a failed tentative parse. */
3465 if (cp_parser_error_occurred (parser))
3466 return error_mark_node;
3467 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3468 return lam;
3469 }
3470
3471 case CPP_OBJC_STRING:
3472 if (c_dialect_objc ())
3473 /* We have an Objective-C++ string literal. */
3474 return cp_parser_objc_expression (parser);
3475 cp_parser_error (parser, "expected primary-expression");
3476 return error_mark_node;
3477
3478 case CPP_KEYWORD:
3479 switch (token->keyword)
3480 {
3481 /* These two are the boolean literals. */
3482 case RID_TRUE:
3483 cp_lexer_consume_token (parser->lexer);
3484 return boolean_true_node;
3485 case RID_FALSE:
3486 cp_lexer_consume_token (parser->lexer);
3487 return boolean_false_node;
3488
3489 /* The `__null' literal. */
3490 case RID_NULL:
3491 cp_lexer_consume_token (parser->lexer);
3492 return null_node;
3493
3494 /* The `nullptr' literal. */
3495 case RID_NULLPTR:
3496 cp_lexer_consume_token (parser->lexer);
3497 return nullptr_node;
3498
3499 /* Recognize the `this' keyword. */
3500 case RID_THIS:
3501 cp_lexer_consume_token (parser->lexer);
3502 if (parser->local_variables_forbidden_p)
3503 {
3504 error_at (token->location,
3505 "%<this%> may not be used in this context");
3506 return error_mark_node;
3507 }
3508 /* Pointers cannot appear in constant-expressions. */
3509 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3510 return error_mark_node;
3511 return finish_this_expr ();
3512
3513 /* The `operator' keyword can be the beginning of an
3514 id-expression. */
3515 case RID_OPERATOR:
3516 goto id_expression;
3517
3518 case RID_FUNCTION_NAME:
3519 case RID_PRETTY_FUNCTION_NAME:
3520 case RID_C99_FUNCTION_NAME:
3521 {
3522 non_integral_constant name;
3523
3524 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3525 __func__ are the names of variables -- but they are
3526 treated specially. Therefore, they are handled here,
3527 rather than relying on the generic id-expression logic
3528 below. Grammatically, these names are id-expressions.
3529
3530 Consume the token. */
3531 token = cp_lexer_consume_token (parser->lexer);
3532
3533 switch (token->keyword)
3534 {
3535 case RID_FUNCTION_NAME:
3536 name = NIC_FUNC_NAME;
3537 break;
3538 case RID_PRETTY_FUNCTION_NAME:
3539 name = NIC_PRETTY_FUNC;
3540 break;
3541 case RID_C99_FUNCTION_NAME:
3542 name = NIC_C99_FUNC;
3543 break;
3544 default:
3545 gcc_unreachable ();
3546 }
3547
3548 if (cp_parser_non_integral_constant_expression (parser, name))
3549 return error_mark_node;
3550
3551 /* Look up the name. */
3552 return finish_fname (token->u.value);
3553 }
3554
3555 case RID_VA_ARG:
3556 {
3557 tree expression;
3558 tree type;
3559
3560 /* The `__builtin_va_arg' construct is used to handle
3561 `va_arg'. Consume the `__builtin_va_arg' token. */
3562 cp_lexer_consume_token (parser->lexer);
3563 /* Look for the opening `('. */
3564 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3565 /* Now, parse the assignment-expression. */
3566 expression = cp_parser_assignment_expression (parser,
3567 /*cast_p=*/false, NULL);
3568 /* Look for the `,'. */
3569 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3570 /* Parse the type-id. */
3571 type = cp_parser_type_id (parser);
3572 /* Look for the closing `)'. */
3573 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3574 /* Using `va_arg' in a constant-expression is not
3575 allowed. */
3576 if (cp_parser_non_integral_constant_expression (parser,
3577 NIC_VA_ARG))
3578 return error_mark_node;
3579 return build_x_va_arg (expression, type);
3580 }
3581
3582 case RID_OFFSETOF:
3583 return cp_parser_builtin_offsetof (parser);
3584
3585 case RID_HAS_NOTHROW_ASSIGN:
3586 case RID_HAS_NOTHROW_CONSTRUCTOR:
3587 case RID_HAS_NOTHROW_COPY:
3588 case RID_HAS_TRIVIAL_ASSIGN:
3589 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3590 case RID_HAS_TRIVIAL_COPY:
3591 case RID_HAS_TRIVIAL_DESTRUCTOR:
3592 case RID_HAS_VIRTUAL_DESTRUCTOR:
3593 case RID_IS_ABSTRACT:
3594 case RID_IS_BASE_OF:
3595 case RID_IS_CLASS:
3596 case RID_IS_CONVERTIBLE_TO:
3597 case RID_IS_EMPTY:
3598 case RID_IS_ENUM:
3599 case RID_IS_POD:
3600 case RID_IS_POLYMORPHIC:
3601 case RID_IS_STD_LAYOUT:
3602 case RID_IS_TRIVIAL:
3603 case RID_IS_UNION:
3604 case RID_IS_LITERAL_TYPE:
3605 return cp_parser_trait_expr (parser, token->keyword);
3606
3607 /* Objective-C++ expressions. */
3608 case RID_AT_ENCODE:
3609 case RID_AT_PROTOCOL:
3610 case RID_AT_SELECTOR:
3611 return cp_parser_objc_expression (parser);
3612
3613 case RID_TEMPLATE:
3614 if (parser->in_function_body
3615 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3616 == CPP_LESS))
3617 {
3618 error_at (token->location,
3619 "a template declaration cannot appear at block scope");
3620 cp_parser_skip_to_end_of_block_or_statement (parser);
3621 return error_mark_node;
3622 }
3623 default:
3624 cp_parser_error (parser, "expected primary-expression");
3625 return error_mark_node;
3626 }
3627
3628 /* An id-expression can start with either an identifier, a
3629 `::' as the beginning of a qualified-id, or the "operator"
3630 keyword. */
3631 case CPP_NAME:
3632 case CPP_SCOPE:
3633 case CPP_TEMPLATE_ID:
3634 case CPP_NESTED_NAME_SPECIFIER:
3635 {
3636 tree id_expression;
3637 tree decl;
3638 const char *error_msg;
3639 bool template_p;
3640 bool done;
3641 cp_token *id_expr_token;
3642
3643 id_expression:
3644 /* Parse the id-expression. */
3645 id_expression
3646 = cp_parser_id_expression (parser,
3647 /*template_keyword_p=*/false,
3648 /*check_dependency_p=*/true,
3649 &template_p,
3650 /*declarator_p=*/false,
3651 /*optional_p=*/false);
3652 if (id_expression == error_mark_node)
3653 return error_mark_node;
3654 id_expr_token = token;
3655 token = cp_lexer_peek_token (parser->lexer);
3656 done = (token->type != CPP_OPEN_SQUARE
3657 && token->type != CPP_OPEN_PAREN
3658 && token->type != CPP_DOT
3659 && token->type != CPP_DEREF
3660 && token->type != CPP_PLUS_PLUS
3661 && token->type != CPP_MINUS_MINUS);
3662 /* If we have a template-id, then no further lookup is
3663 required. If the template-id was for a template-class, we
3664 will sometimes have a TYPE_DECL at this point. */
3665 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3666 || TREE_CODE (id_expression) == TYPE_DECL)
3667 decl = id_expression;
3668 /* Look up the name. */
3669 else
3670 {
3671 tree ambiguous_decls;
3672
3673 /* If we already know that this lookup is ambiguous, then
3674 we've already issued an error message; there's no reason
3675 to check again. */
3676 if (id_expr_token->type == CPP_NAME
3677 && id_expr_token->ambiguous_p)
3678 {
3679 cp_parser_simulate_error (parser);
3680 return error_mark_node;
3681 }
3682
3683 decl = cp_parser_lookup_name (parser, id_expression,
3684 none_type,
3685 template_p,
3686 /*is_namespace=*/false,
3687 /*check_dependency=*/true,
3688 &ambiguous_decls,
3689 id_expr_token->location);
3690 /* If the lookup was ambiguous, an error will already have
3691 been issued. */
3692 if (ambiguous_decls)
3693 return error_mark_node;
3694
3695 /* In Objective-C++, we may have an Objective-C 2.0
3696 dot-syntax for classes here. */
3697 if (c_dialect_objc ()
3698 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3699 && TREE_CODE (decl) == TYPE_DECL
3700 && objc_is_class_name (decl))
3701 {
3702 tree component;
3703 cp_lexer_consume_token (parser->lexer);
3704 component = cp_parser_identifier (parser);
3705 if (component == error_mark_node)
3706 return error_mark_node;
3707
3708 return objc_build_class_component_ref (id_expression, component);
3709 }
3710
3711 /* In Objective-C++, an instance variable (ivar) may be preferred
3712 to whatever cp_parser_lookup_name() found. */
3713 decl = objc_lookup_ivar (decl, id_expression);
3714
3715 /* If name lookup gives us a SCOPE_REF, then the
3716 qualifying scope was dependent. */
3717 if (TREE_CODE (decl) == SCOPE_REF)
3718 {
3719 /* At this point, we do not know if DECL is a valid
3720 integral constant expression. We assume that it is
3721 in fact such an expression, so that code like:
3722
3723 template <int N> struct A {
3724 int a[B<N>::i];
3725 };
3726
3727 is accepted. At template-instantiation time, we
3728 will check that B<N>::i is actually a constant. */
3729 return decl;
3730 }
3731 /* Check to see if DECL is a local variable in a context
3732 where that is forbidden. */
3733 if (parser->local_variables_forbidden_p
3734 && local_variable_p (decl))
3735 {
3736 /* It might be that we only found DECL because we are
3737 trying to be generous with pre-ISO scoping rules.
3738 For example, consider:
3739
3740 int i;
3741 void g() {
3742 for (int i = 0; i < 10; ++i) {}
3743 extern void f(int j = i);
3744 }
3745
3746 Here, name look up will originally find the out
3747 of scope `i'. We need to issue a warning message,
3748 but then use the global `i'. */
3749 decl = check_for_out_of_scope_variable (decl);
3750 if (local_variable_p (decl))
3751 {
3752 error_at (id_expr_token->location,
3753 "local variable %qD may not appear in this context",
3754 decl);
3755 return error_mark_node;
3756 }
3757 }
3758 }
3759
3760 decl = (finish_id_expression
3761 (id_expression, decl, parser->scope,
3762 idk,
3763 parser->integral_constant_expression_p,
3764 parser->allow_non_integral_constant_expression_p,
3765 &parser->non_integral_constant_expression_p,
3766 template_p, done, address_p,
3767 template_arg_p,
3768 &error_msg,
3769 id_expr_token->location));
3770 if (error_msg)
3771 cp_parser_error (parser, error_msg);
3772 return decl;
3773 }
3774
3775 /* Anything else is an error. */
3776 default:
3777 cp_parser_error (parser, "expected primary-expression");
3778 return error_mark_node;
3779 }
3780 }
3781
3782 /* Parse an id-expression.
3783
3784 id-expression:
3785 unqualified-id
3786 qualified-id
3787
3788 qualified-id:
3789 :: [opt] nested-name-specifier template [opt] unqualified-id
3790 :: identifier
3791 :: operator-function-id
3792 :: template-id
3793
3794 Return a representation of the unqualified portion of the
3795 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3796 a `::' or nested-name-specifier.
3797
3798 Often, if the id-expression was a qualified-id, the caller will
3799 want to make a SCOPE_REF to represent the qualified-id. This
3800 function does not do this in order to avoid wastefully creating
3801 SCOPE_REFs when they are not required.
3802
3803 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3804 `template' keyword.
3805
3806 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3807 uninstantiated templates.
3808
3809 If *TEMPLATE_P is non-NULL, it is set to true iff the
3810 `template' keyword is used to explicitly indicate that the entity
3811 named is a template.
3812
3813 If DECLARATOR_P is true, the id-expression is appearing as part of
3814 a declarator, rather than as part of an expression. */
3815
3816 static tree
3817 cp_parser_id_expression (cp_parser *parser,
3818 bool template_keyword_p,
3819 bool check_dependency_p,
3820 bool *template_p,
3821 bool declarator_p,
3822 bool optional_p)
3823 {
3824 bool global_scope_p;
3825 bool nested_name_specifier_p;
3826
3827 /* Assume the `template' keyword was not used. */
3828 if (template_p)
3829 *template_p = template_keyword_p;
3830
3831 /* Look for the optional `::' operator. */
3832 global_scope_p
3833 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3834 != NULL_TREE);
3835 /* Look for the optional nested-name-specifier. */
3836 nested_name_specifier_p
3837 = (cp_parser_nested_name_specifier_opt (parser,
3838 /*typename_keyword_p=*/false,
3839 check_dependency_p,
3840 /*type_p=*/false,
3841 declarator_p)
3842 != NULL_TREE);
3843 /* If there is a nested-name-specifier, then we are looking at
3844 the first qualified-id production. */
3845 if (nested_name_specifier_p)
3846 {
3847 tree saved_scope;
3848 tree saved_object_scope;
3849 tree saved_qualifying_scope;
3850 tree unqualified_id;
3851 bool is_template;
3852
3853 /* See if the next token is the `template' keyword. */
3854 if (!template_p)
3855 template_p = &is_template;
3856 *template_p = cp_parser_optional_template_keyword (parser);
3857 /* Name lookup we do during the processing of the
3858 unqualified-id might obliterate SCOPE. */
3859 saved_scope = parser->scope;
3860 saved_object_scope = parser->object_scope;
3861 saved_qualifying_scope = parser->qualifying_scope;
3862 /* Process the final unqualified-id. */
3863 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3864 check_dependency_p,
3865 declarator_p,
3866 /*optional_p=*/false);
3867 /* Restore the SAVED_SCOPE for our caller. */
3868 parser->scope = saved_scope;
3869 parser->object_scope = saved_object_scope;
3870 parser->qualifying_scope = saved_qualifying_scope;
3871
3872 return unqualified_id;
3873 }
3874 /* Otherwise, if we are in global scope, then we are looking at one
3875 of the other qualified-id productions. */
3876 else if (global_scope_p)
3877 {
3878 cp_token *token;
3879 tree id;
3880
3881 /* Peek at the next token. */
3882 token = cp_lexer_peek_token (parser->lexer);
3883
3884 /* If it's an identifier, and the next token is not a "<", then
3885 we can avoid the template-id case. This is an optimization
3886 for this common case. */
3887 if (token->type == CPP_NAME
3888 && !cp_parser_nth_token_starts_template_argument_list_p
3889 (parser, 2))
3890 return cp_parser_identifier (parser);
3891
3892 cp_parser_parse_tentatively (parser);
3893 /* Try a template-id. */
3894 id = cp_parser_template_id (parser,
3895 /*template_keyword_p=*/false,
3896 /*check_dependency_p=*/true,
3897 declarator_p);
3898 /* If that worked, we're done. */
3899 if (cp_parser_parse_definitely (parser))
3900 return id;
3901
3902 /* Peek at the next token. (Changes in the token buffer may
3903 have invalidated the pointer obtained above.) */
3904 token = cp_lexer_peek_token (parser->lexer);
3905
3906 switch (token->type)
3907 {
3908 case CPP_NAME:
3909 return cp_parser_identifier (parser);
3910
3911 case CPP_KEYWORD:
3912 if (token->keyword == RID_OPERATOR)
3913 return cp_parser_operator_function_id (parser);
3914 /* Fall through. */
3915
3916 default:
3917 cp_parser_error (parser, "expected id-expression");
3918 return error_mark_node;
3919 }
3920 }
3921 else
3922 return cp_parser_unqualified_id (parser, template_keyword_p,
3923 /*check_dependency_p=*/true,
3924 declarator_p,
3925 optional_p);
3926 }
3927
3928 /* Parse an unqualified-id.
3929
3930 unqualified-id:
3931 identifier
3932 operator-function-id
3933 conversion-function-id
3934 ~ class-name
3935 template-id
3936
3937 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3938 keyword, in a construct like `A::template ...'.
3939
3940 Returns a representation of unqualified-id. For the `identifier'
3941 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3942 production a BIT_NOT_EXPR is returned; the operand of the
3943 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3944 other productions, see the documentation accompanying the
3945 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3946 names are looked up in uninstantiated templates. If DECLARATOR_P
3947 is true, the unqualified-id is appearing as part of a declarator,
3948 rather than as part of an expression. */
3949
3950 static tree
3951 cp_parser_unqualified_id (cp_parser* parser,
3952 bool template_keyword_p,
3953 bool check_dependency_p,
3954 bool declarator_p,
3955 bool optional_p)
3956 {
3957 cp_token *token;
3958
3959 /* Peek at the next token. */
3960 token = cp_lexer_peek_token (parser->lexer);
3961
3962 switch (token->type)
3963 {
3964 case CPP_NAME:
3965 {
3966 tree id;
3967
3968 /* We don't know yet whether or not this will be a
3969 template-id. */
3970 cp_parser_parse_tentatively (parser);
3971 /* Try a template-id. */
3972 id = cp_parser_template_id (parser, template_keyword_p,
3973 check_dependency_p,
3974 declarator_p);
3975 /* If it worked, we're done. */
3976 if (cp_parser_parse_definitely (parser))
3977 return id;
3978 /* Otherwise, it's an ordinary identifier. */
3979 return cp_parser_identifier (parser);
3980 }
3981
3982 case CPP_TEMPLATE_ID:
3983 return cp_parser_template_id (parser, template_keyword_p,
3984 check_dependency_p,
3985 declarator_p);
3986
3987 case CPP_COMPL:
3988 {
3989 tree type_decl;
3990 tree qualifying_scope;
3991 tree object_scope;
3992 tree scope;
3993 bool done;
3994
3995 /* Consume the `~' token. */
3996 cp_lexer_consume_token (parser->lexer);
3997 /* Parse the class-name. The standard, as written, seems to
3998 say that:
3999
4000 template <typename T> struct S { ~S (); };
4001 template <typename T> S<T>::~S() {}
4002
4003 is invalid, since `~' must be followed by a class-name, but
4004 `S<T>' is dependent, and so not known to be a class.
4005 That's not right; we need to look in uninstantiated
4006 templates. A further complication arises from:
4007
4008 template <typename T> void f(T t) {
4009 t.T::~T();
4010 }
4011
4012 Here, it is not possible to look up `T' in the scope of `T'
4013 itself. We must look in both the current scope, and the
4014 scope of the containing complete expression.
4015
4016 Yet another issue is:
4017
4018 struct S {
4019 int S;
4020 ~S();
4021 };
4022
4023 S::~S() {}
4024
4025 The standard does not seem to say that the `S' in `~S'
4026 should refer to the type `S' and not the data member
4027 `S::S'. */
4028
4029 /* DR 244 says that we look up the name after the "~" in the
4030 same scope as we looked up the qualifying name. That idea
4031 isn't fully worked out; it's more complicated than that. */
4032 scope = parser->scope;
4033 object_scope = parser->object_scope;
4034 qualifying_scope = parser->qualifying_scope;
4035
4036 /* Check for invalid scopes. */
4037 if (scope == error_mark_node)
4038 {
4039 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4040 cp_lexer_consume_token (parser->lexer);
4041 return error_mark_node;
4042 }
4043 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4044 {
4045 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4046 error_at (token->location,
4047 "scope %qT before %<~%> is not a class-name",
4048 scope);
4049 cp_parser_simulate_error (parser);
4050 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4051 cp_lexer_consume_token (parser->lexer);
4052 return error_mark_node;
4053 }
4054 gcc_assert (!scope || TYPE_P (scope));
4055
4056 /* If the name is of the form "X::~X" it's OK even if X is a
4057 typedef. */
4058 token = cp_lexer_peek_token (parser->lexer);
4059 if (scope
4060 && token->type == CPP_NAME
4061 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4062 != CPP_LESS)
4063 && (token->u.value == TYPE_IDENTIFIER (scope)
4064 || constructor_name_p (token->u.value, scope)))
4065 {
4066 cp_lexer_consume_token (parser->lexer);
4067 return build_nt (BIT_NOT_EXPR, scope);
4068 }
4069
4070 /* If there was an explicit qualification (S::~T), first look
4071 in the scope given by the qualification (i.e., S).
4072
4073 Note: in the calls to cp_parser_class_name below we pass
4074 typename_type so that lookup finds the injected-class-name
4075 rather than the constructor. */
4076 done = false;
4077 type_decl = NULL_TREE;
4078 if (scope)
4079 {
4080 cp_parser_parse_tentatively (parser);
4081 type_decl = cp_parser_class_name (parser,
4082 /*typename_keyword_p=*/false,
4083 /*template_keyword_p=*/false,
4084 typename_type,
4085 /*check_dependency=*/false,
4086 /*class_head_p=*/false,
4087 declarator_p);
4088 if (cp_parser_parse_definitely (parser))
4089 done = true;
4090 }
4091 /* In "N::S::~S", look in "N" as well. */
4092 if (!done && scope && qualifying_scope)
4093 {
4094 cp_parser_parse_tentatively (parser);
4095 parser->scope = qualifying_scope;
4096 parser->object_scope = NULL_TREE;
4097 parser->qualifying_scope = NULL_TREE;
4098 type_decl
4099 = cp_parser_class_name (parser,
4100 /*typename_keyword_p=*/false,
4101 /*template_keyword_p=*/false,
4102 typename_type,
4103 /*check_dependency=*/false,
4104 /*class_head_p=*/false,
4105 declarator_p);
4106 if (cp_parser_parse_definitely (parser))
4107 done = true;
4108 }
4109 /* In "p->S::~T", look in the scope given by "*p" as well. */
4110 else if (!done && object_scope)
4111 {
4112 cp_parser_parse_tentatively (parser);
4113 parser->scope = object_scope;
4114 parser->object_scope = NULL_TREE;
4115 parser->qualifying_scope = NULL_TREE;
4116 type_decl
4117 = cp_parser_class_name (parser,
4118 /*typename_keyword_p=*/false,
4119 /*template_keyword_p=*/false,
4120 typename_type,
4121 /*check_dependency=*/false,
4122 /*class_head_p=*/false,
4123 declarator_p);
4124 if (cp_parser_parse_definitely (parser))
4125 done = true;
4126 }
4127 /* Look in the surrounding context. */
4128 if (!done)
4129 {
4130 parser->scope = NULL_TREE;
4131 parser->object_scope = NULL_TREE;
4132 parser->qualifying_scope = NULL_TREE;
4133 if (processing_template_decl)
4134 cp_parser_parse_tentatively (parser);
4135 type_decl
4136 = cp_parser_class_name (parser,
4137 /*typename_keyword_p=*/false,
4138 /*template_keyword_p=*/false,
4139 typename_type,
4140 /*check_dependency=*/false,
4141 /*class_head_p=*/false,
4142 declarator_p);
4143 if (processing_template_decl
4144 && ! cp_parser_parse_definitely (parser))
4145 {
4146 /* We couldn't find a type with this name, so just accept
4147 it and check for a match at instantiation time. */
4148 type_decl = cp_parser_identifier (parser);
4149 if (type_decl != error_mark_node)
4150 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4151 return type_decl;
4152 }
4153 }
4154 /* If an error occurred, assume that the name of the
4155 destructor is the same as the name of the qualifying
4156 class. That allows us to keep parsing after running
4157 into ill-formed destructor names. */
4158 if (type_decl == error_mark_node && scope)
4159 return build_nt (BIT_NOT_EXPR, scope);
4160 else if (type_decl == error_mark_node)
4161 return error_mark_node;
4162
4163 /* Check that destructor name and scope match. */
4164 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4165 {
4166 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4167 error_at (token->location,
4168 "declaration of %<~%T%> as member of %qT",
4169 type_decl, scope);
4170 cp_parser_simulate_error (parser);
4171 return error_mark_node;
4172 }
4173
4174 /* [class.dtor]
4175
4176 A typedef-name that names a class shall not be used as the
4177 identifier in the declarator for a destructor declaration. */
4178 if (declarator_p
4179 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4180 && !DECL_SELF_REFERENCE_P (type_decl)
4181 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4182 error_at (token->location,
4183 "typedef-name %qD used as destructor declarator",
4184 type_decl);
4185
4186 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4187 }
4188
4189 case CPP_KEYWORD:
4190 if (token->keyword == RID_OPERATOR)
4191 {
4192 tree id;
4193
4194 /* This could be a template-id, so we try that first. */
4195 cp_parser_parse_tentatively (parser);
4196 /* Try a template-id. */
4197 id = cp_parser_template_id (parser, template_keyword_p,
4198 /*check_dependency_p=*/true,
4199 declarator_p);
4200 /* If that worked, we're done. */
4201 if (cp_parser_parse_definitely (parser))
4202 return id;
4203 /* We still don't know whether we're looking at an
4204 operator-function-id or a conversion-function-id. */
4205 cp_parser_parse_tentatively (parser);
4206 /* Try an operator-function-id. */
4207 id = cp_parser_operator_function_id (parser);
4208 /* If that didn't work, try a conversion-function-id. */
4209 if (!cp_parser_parse_definitely (parser))
4210 id = cp_parser_conversion_function_id (parser);
4211
4212 return id;
4213 }
4214 /* Fall through. */
4215
4216 default:
4217 if (optional_p)
4218 return NULL_TREE;
4219 cp_parser_error (parser, "expected unqualified-id");
4220 return error_mark_node;
4221 }
4222 }
4223
4224 /* Parse an (optional) nested-name-specifier.
4225
4226 nested-name-specifier: [C++98]
4227 class-or-namespace-name :: nested-name-specifier [opt]
4228 class-or-namespace-name :: template nested-name-specifier [opt]
4229
4230 nested-name-specifier: [C++0x]
4231 type-name ::
4232 namespace-name ::
4233 nested-name-specifier identifier ::
4234 nested-name-specifier template [opt] simple-template-id ::
4235
4236 PARSER->SCOPE should be set appropriately before this function is
4237 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4238 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4239 in name lookups.
4240
4241 Sets PARSER->SCOPE to the class (TYPE) or namespace
4242 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4243 it unchanged if there is no nested-name-specifier. Returns the new
4244 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4245
4246 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4247 part of a declaration and/or decl-specifier. */
4248
4249 static tree
4250 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4251 bool typename_keyword_p,
4252 bool check_dependency_p,
4253 bool type_p,
4254 bool is_declaration)
4255 {
4256 bool success = false;
4257 cp_token_position start = 0;
4258 cp_token *token;
4259
4260 /* Remember where the nested-name-specifier starts. */
4261 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4262 {
4263 start = cp_lexer_token_position (parser->lexer, false);
4264 push_deferring_access_checks (dk_deferred);
4265 }
4266
4267 while (true)
4268 {
4269 tree new_scope;
4270 tree old_scope;
4271 tree saved_qualifying_scope;
4272 bool template_keyword_p;
4273
4274 /* Spot cases that cannot be the beginning of a
4275 nested-name-specifier. */
4276 token = cp_lexer_peek_token (parser->lexer);
4277
4278 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4279 the already parsed nested-name-specifier. */
4280 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4281 {
4282 /* Grab the nested-name-specifier and continue the loop. */
4283 cp_parser_pre_parsed_nested_name_specifier (parser);
4284 /* If we originally encountered this nested-name-specifier
4285 with IS_DECLARATION set to false, we will not have
4286 resolved TYPENAME_TYPEs, so we must do so here. */
4287 if (is_declaration
4288 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4289 {
4290 new_scope = resolve_typename_type (parser->scope,
4291 /*only_current_p=*/false);
4292 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4293 parser->scope = new_scope;
4294 }
4295 success = true;
4296 continue;
4297 }
4298
4299 /* Spot cases that cannot be the beginning of a
4300 nested-name-specifier. On the second and subsequent times
4301 through the loop, we look for the `template' keyword. */
4302 if (success && token->keyword == RID_TEMPLATE)
4303 ;
4304 /* A template-id can start a nested-name-specifier. */
4305 else if (token->type == CPP_TEMPLATE_ID)
4306 ;
4307 else
4308 {
4309 /* If the next token is not an identifier, then it is
4310 definitely not a type-name or namespace-name. */
4311 if (token->type != CPP_NAME)
4312 break;
4313 /* If the following token is neither a `<' (to begin a
4314 template-id), nor a `::', then we are not looking at a
4315 nested-name-specifier. */
4316 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4317
4318 if (token->type == CPP_COLON
4319 && parser->colon_corrects_to_scope_p
4320 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4321 {
4322 error_at (token->location,
4323 "found %<:%> in nested-name-specifier, expected %<::%>");
4324 token->type = CPP_SCOPE;
4325 }
4326
4327 if (token->type != CPP_SCOPE
4328 && !cp_parser_nth_token_starts_template_argument_list_p
4329 (parser, 2))
4330 break;
4331 }
4332
4333 /* The nested-name-specifier is optional, so we parse
4334 tentatively. */
4335 cp_parser_parse_tentatively (parser);
4336
4337 /* Look for the optional `template' keyword, if this isn't the
4338 first time through the loop. */
4339 if (success)
4340 template_keyword_p = cp_parser_optional_template_keyword (parser);
4341 else
4342 template_keyword_p = false;
4343
4344 /* Save the old scope since the name lookup we are about to do
4345 might destroy it. */
4346 old_scope = parser->scope;
4347 saved_qualifying_scope = parser->qualifying_scope;
4348 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4349 look up names in "X<T>::I" in order to determine that "Y" is
4350 a template. So, if we have a typename at this point, we make
4351 an effort to look through it. */
4352 if (is_declaration
4353 && !typename_keyword_p
4354 && parser->scope
4355 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4356 parser->scope = resolve_typename_type (parser->scope,
4357 /*only_current_p=*/false);
4358 /* Parse the qualifying entity. */
4359 new_scope
4360 = cp_parser_qualifying_entity (parser,
4361 typename_keyword_p,
4362 template_keyword_p,
4363 check_dependency_p,
4364 type_p,
4365 is_declaration);
4366 /* Look for the `::' token. */
4367 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4368
4369 /* If we found what we wanted, we keep going; otherwise, we're
4370 done. */
4371 if (!cp_parser_parse_definitely (parser))
4372 {
4373 bool error_p = false;
4374
4375 /* Restore the OLD_SCOPE since it was valid before the
4376 failed attempt at finding the last
4377 class-or-namespace-name. */
4378 parser->scope = old_scope;
4379 parser->qualifying_scope = saved_qualifying_scope;
4380 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4381 break;
4382 /* If the next token is an identifier, and the one after
4383 that is a `::', then any valid interpretation would have
4384 found a class-or-namespace-name. */
4385 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4386 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4387 == CPP_SCOPE)
4388 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4389 != CPP_COMPL))
4390 {
4391 token = cp_lexer_consume_token (parser->lexer);
4392 if (!error_p)
4393 {
4394 if (!token->ambiguous_p)
4395 {
4396 tree decl;
4397 tree ambiguous_decls;
4398
4399 decl = cp_parser_lookup_name (parser, token->u.value,
4400 none_type,
4401 /*is_template=*/false,
4402 /*is_namespace=*/false,
4403 /*check_dependency=*/true,
4404 &ambiguous_decls,
4405 token->location);
4406 if (TREE_CODE (decl) == TEMPLATE_DECL)
4407 error_at (token->location,
4408 "%qD used without template parameters",
4409 decl);
4410 else if (ambiguous_decls)
4411 {
4412 error_at (token->location,
4413 "reference to %qD is ambiguous",
4414 token->u.value);
4415 print_candidates (ambiguous_decls);
4416 decl = error_mark_node;
4417 }
4418 else
4419 {
4420 if (cxx_dialect != cxx98)
4421 cp_parser_name_lookup_error
4422 (parser, token->u.value, decl, NLE_NOT_CXX98,
4423 token->location);
4424 else
4425 cp_parser_name_lookup_error
4426 (parser, token->u.value, decl, NLE_CXX98,
4427 token->location);
4428 }
4429 }
4430 parser->scope = error_mark_node;
4431 error_p = true;
4432 /* Treat this as a successful nested-name-specifier
4433 due to:
4434
4435 [basic.lookup.qual]
4436
4437 If the name found is not a class-name (clause
4438 _class_) or namespace-name (_namespace.def_), the
4439 program is ill-formed. */
4440 success = true;
4441 }
4442 cp_lexer_consume_token (parser->lexer);
4443 }
4444 break;
4445 }
4446 /* We've found one valid nested-name-specifier. */
4447 success = true;
4448 /* Name lookup always gives us a DECL. */
4449 if (TREE_CODE (new_scope) == TYPE_DECL)
4450 new_scope = TREE_TYPE (new_scope);
4451 /* Uses of "template" must be followed by actual templates. */
4452 if (template_keyword_p
4453 && !(CLASS_TYPE_P (new_scope)
4454 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4455 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4456 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4457 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4458 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4459 == TEMPLATE_ID_EXPR)))
4460 permerror (input_location, TYPE_P (new_scope)
4461 ? "%qT is not a template"
4462 : "%qD is not a template",
4463 new_scope);
4464 /* If it is a class scope, try to complete it; we are about to
4465 be looking up names inside the class. */
4466 if (TYPE_P (new_scope)
4467 /* Since checking types for dependency can be expensive,
4468 avoid doing it if the type is already complete. */
4469 && !COMPLETE_TYPE_P (new_scope)
4470 /* Do not try to complete dependent types. */
4471 && !dependent_type_p (new_scope))
4472 {
4473 new_scope = complete_type (new_scope);
4474 /* If it is a typedef to current class, use the current
4475 class instead, as the typedef won't have any names inside
4476 it yet. */
4477 if (!COMPLETE_TYPE_P (new_scope)
4478 && currently_open_class (new_scope))
4479 new_scope = TYPE_MAIN_VARIANT (new_scope);
4480 }
4481 /* Make sure we look in the right scope the next time through
4482 the loop. */
4483 parser->scope = new_scope;
4484 }
4485
4486 /* If parsing tentatively, replace the sequence of tokens that makes
4487 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4488 token. That way, should we re-parse the token stream, we will
4489 not have to repeat the effort required to do the parse, nor will
4490 we issue duplicate error messages. */
4491 if (success && start)
4492 {
4493 cp_token *token;
4494
4495 token = cp_lexer_token_at (parser->lexer, start);
4496 /* Reset the contents of the START token. */
4497 token->type = CPP_NESTED_NAME_SPECIFIER;
4498 /* Retrieve any deferred checks. Do not pop this access checks yet
4499 so the memory will not be reclaimed during token replacing below. */
4500 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4501 token->u.tree_check_value->value = parser->scope;
4502 token->u.tree_check_value->checks = get_deferred_access_checks ();
4503 token->u.tree_check_value->qualifying_scope =
4504 parser->qualifying_scope;
4505 token->keyword = RID_MAX;
4506
4507 /* Purge all subsequent tokens. */
4508 cp_lexer_purge_tokens_after (parser->lexer, start);
4509 }
4510
4511 if (start)
4512 pop_to_parent_deferring_access_checks ();
4513
4514 return success ? parser->scope : NULL_TREE;
4515 }
4516
4517 /* Parse a nested-name-specifier. See
4518 cp_parser_nested_name_specifier_opt for details. This function
4519 behaves identically, except that it will an issue an error if no
4520 nested-name-specifier is present. */
4521
4522 static tree
4523 cp_parser_nested_name_specifier (cp_parser *parser,
4524 bool typename_keyword_p,
4525 bool check_dependency_p,
4526 bool type_p,
4527 bool is_declaration)
4528 {
4529 tree scope;
4530
4531 /* Look for the nested-name-specifier. */
4532 scope = cp_parser_nested_name_specifier_opt (parser,
4533 typename_keyword_p,
4534 check_dependency_p,
4535 type_p,
4536 is_declaration);
4537 /* If it was not present, issue an error message. */
4538 if (!scope)
4539 {
4540 cp_parser_error (parser, "expected nested-name-specifier");
4541 parser->scope = NULL_TREE;
4542 }
4543
4544 return scope;
4545 }
4546
4547 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4548 this is either a class-name or a namespace-name (which corresponds
4549 to the class-or-namespace-name production in the grammar). For
4550 C++0x, it can also be a type-name that refers to an enumeration
4551 type.
4552
4553 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4554 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4555 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4556 TYPE_P is TRUE iff the next name should be taken as a class-name,
4557 even the same name is declared to be another entity in the same
4558 scope.
4559
4560 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4561 specified by the class-or-namespace-name. If neither is found the
4562 ERROR_MARK_NODE is returned. */
4563
4564 static tree
4565 cp_parser_qualifying_entity (cp_parser *parser,
4566 bool typename_keyword_p,
4567 bool template_keyword_p,
4568 bool check_dependency_p,
4569 bool type_p,
4570 bool is_declaration)
4571 {
4572 tree saved_scope;
4573 tree saved_qualifying_scope;
4574 tree saved_object_scope;
4575 tree scope;
4576 bool only_class_p;
4577 bool successful_parse_p;
4578
4579 /* Before we try to parse the class-name, we must save away the
4580 current PARSER->SCOPE since cp_parser_class_name will destroy
4581 it. */
4582 saved_scope = parser->scope;
4583 saved_qualifying_scope = parser->qualifying_scope;
4584 saved_object_scope = parser->object_scope;
4585 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4586 there is no need to look for a namespace-name. */
4587 only_class_p = template_keyword_p
4588 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4589 if (!only_class_p)
4590 cp_parser_parse_tentatively (parser);
4591 scope = cp_parser_class_name (parser,
4592 typename_keyword_p,
4593 template_keyword_p,
4594 type_p ? class_type : none_type,
4595 check_dependency_p,
4596 /*class_head_p=*/false,
4597 is_declaration);
4598 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4599 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4600 if (!only_class_p
4601 && cxx_dialect != cxx98
4602 && !successful_parse_p)
4603 {
4604 /* Restore the saved scope. */
4605 parser->scope = saved_scope;
4606 parser->qualifying_scope = saved_qualifying_scope;
4607 parser->object_scope = saved_object_scope;
4608
4609 /* Parse tentatively. */
4610 cp_parser_parse_tentatively (parser);
4611
4612 /* Parse a typedef-name or enum-name. */
4613 scope = cp_parser_nonclass_name (parser);
4614
4615 /* "If the name found does not designate a namespace or a class,
4616 enumeration, or dependent type, the program is ill-formed."
4617
4618 We cover classes and dependent types above and namespaces below,
4619 so this code is only looking for enums. */
4620 if (!scope || TREE_CODE (scope) != TYPE_DECL
4621 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4622 cp_parser_simulate_error (parser);
4623
4624 successful_parse_p = cp_parser_parse_definitely (parser);
4625 }
4626 /* If that didn't work, try for a namespace-name. */
4627 if (!only_class_p && !successful_parse_p)
4628 {
4629 /* Restore the saved scope. */
4630 parser->scope = saved_scope;
4631 parser->qualifying_scope = saved_qualifying_scope;
4632 parser->object_scope = saved_object_scope;
4633 /* If we are not looking at an identifier followed by the scope
4634 resolution operator, then this is not part of a
4635 nested-name-specifier. (Note that this function is only used
4636 to parse the components of a nested-name-specifier.) */
4637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4638 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4639 return error_mark_node;
4640 scope = cp_parser_namespace_name (parser);
4641 }
4642
4643 return scope;
4644 }
4645
4646 /* Parse a postfix-expression.
4647
4648 postfix-expression:
4649 primary-expression
4650 postfix-expression [ expression ]
4651 postfix-expression ( expression-list [opt] )
4652 simple-type-specifier ( expression-list [opt] )
4653 typename :: [opt] nested-name-specifier identifier
4654 ( expression-list [opt] )
4655 typename :: [opt] nested-name-specifier template [opt] template-id
4656 ( expression-list [opt] )
4657 postfix-expression . template [opt] id-expression
4658 postfix-expression -> template [opt] id-expression
4659 postfix-expression . pseudo-destructor-name
4660 postfix-expression -> pseudo-destructor-name
4661 postfix-expression ++
4662 postfix-expression --
4663 dynamic_cast < type-id > ( expression )
4664 static_cast < type-id > ( expression )
4665 reinterpret_cast < type-id > ( expression )
4666 const_cast < type-id > ( expression )
4667 typeid ( expression )
4668 typeid ( type-id )
4669
4670 GNU Extension:
4671
4672 postfix-expression:
4673 ( type-id ) { initializer-list , [opt] }
4674
4675 This extension is a GNU version of the C99 compound-literal
4676 construct. (The C99 grammar uses `type-name' instead of `type-id',
4677 but they are essentially the same concept.)
4678
4679 If ADDRESS_P is true, the postfix expression is the operand of the
4680 `&' operator. CAST_P is true if this expression is the target of a
4681 cast.
4682
4683 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4684 class member access expressions [expr.ref].
4685
4686 Returns a representation of the expression. */
4687
4688 static tree
4689 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4690 bool member_access_only_p,
4691 cp_id_kind * pidk_return)
4692 {
4693 cp_token *token;
4694 enum rid keyword;
4695 cp_id_kind idk = CP_ID_KIND_NONE;
4696 tree postfix_expression = NULL_TREE;
4697 bool is_member_access = false;
4698
4699 /* Peek at the next token. */
4700 token = cp_lexer_peek_token (parser->lexer);
4701 /* Some of the productions are determined by keywords. */
4702 keyword = token->keyword;
4703 switch (keyword)
4704 {
4705 case RID_DYNCAST:
4706 case RID_STATCAST:
4707 case RID_REINTCAST:
4708 case RID_CONSTCAST:
4709 {
4710 tree type;
4711 tree expression;
4712 const char *saved_message;
4713
4714 /* All of these can be handled in the same way from the point
4715 of view of parsing. Begin by consuming the token
4716 identifying the cast. */
4717 cp_lexer_consume_token (parser->lexer);
4718
4719 /* New types cannot be defined in the cast. */
4720 saved_message = parser->type_definition_forbidden_message;
4721 parser->type_definition_forbidden_message
4722 = G_("types may not be defined in casts");
4723
4724 /* Look for the opening `<'. */
4725 cp_parser_require (parser, CPP_LESS, RT_LESS);
4726 /* Parse the type to which we are casting. */
4727 type = cp_parser_type_id (parser);
4728 /* Look for the closing `>'. */
4729 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4730 /* Restore the old message. */
4731 parser->type_definition_forbidden_message = saved_message;
4732
4733 /* And the expression which is being cast. */
4734 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4735 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4736 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4737
4738 /* Only type conversions to integral or enumeration types
4739 can be used in constant-expressions. */
4740 if (!cast_valid_in_integral_constant_expression_p (type)
4741 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4742 return error_mark_node;
4743
4744 switch (keyword)
4745 {
4746 case RID_DYNCAST:
4747 postfix_expression
4748 = build_dynamic_cast (type, expression, tf_warning_or_error);
4749 break;
4750 case RID_STATCAST:
4751 postfix_expression
4752 = build_static_cast (type, expression, tf_warning_or_error);
4753 break;
4754 case RID_REINTCAST:
4755 postfix_expression
4756 = build_reinterpret_cast (type, expression,
4757 tf_warning_or_error);
4758 break;
4759 case RID_CONSTCAST:
4760 postfix_expression
4761 = build_const_cast (type, expression, tf_warning_or_error);
4762 break;
4763 default:
4764 gcc_unreachable ();
4765 }
4766 }
4767 break;
4768
4769 case RID_TYPEID:
4770 {
4771 tree type;
4772 const char *saved_message;
4773 bool saved_in_type_id_in_expr_p;
4774
4775 /* Consume the `typeid' token. */
4776 cp_lexer_consume_token (parser->lexer);
4777 /* Look for the `(' token. */
4778 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4779 /* Types cannot be defined in a `typeid' expression. */
4780 saved_message = parser->type_definition_forbidden_message;
4781 parser->type_definition_forbidden_message
4782 = G_("types may not be defined in a %<typeid%> expression");
4783 /* We can't be sure yet whether we're looking at a type-id or an
4784 expression. */
4785 cp_parser_parse_tentatively (parser);
4786 /* Try a type-id first. */
4787 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4788 parser->in_type_id_in_expr_p = true;
4789 type = cp_parser_type_id (parser);
4790 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4791 /* Look for the `)' token. Otherwise, we can't be sure that
4792 we're not looking at an expression: consider `typeid (int
4793 (3))', for example. */
4794 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4795 /* If all went well, simply lookup the type-id. */
4796 if (cp_parser_parse_definitely (parser))
4797 postfix_expression = get_typeid (type);
4798 /* Otherwise, fall back to the expression variant. */
4799 else
4800 {
4801 tree expression;
4802
4803 /* Look for an expression. */
4804 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4805 /* Compute its typeid. */
4806 postfix_expression = build_typeid (expression);
4807 /* Look for the `)' token. */
4808 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4809 }
4810 /* Restore the saved message. */
4811 parser->type_definition_forbidden_message = saved_message;
4812 /* `typeid' may not appear in an integral constant expression. */
4813 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4814 return error_mark_node;
4815 }
4816 break;
4817
4818 case RID_TYPENAME:
4819 {
4820 tree type;
4821 /* The syntax permitted here is the same permitted for an
4822 elaborated-type-specifier. */
4823 type = cp_parser_elaborated_type_specifier (parser,
4824 /*is_friend=*/false,
4825 /*is_declaration=*/false);
4826 postfix_expression = cp_parser_functional_cast (parser, type);
4827 }
4828 break;
4829
4830 default:
4831 {
4832 tree type;
4833
4834 /* If the next thing is a simple-type-specifier, we may be
4835 looking at a functional cast. We could also be looking at
4836 an id-expression. So, we try the functional cast, and if
4837 that doesn't work we fall back to the primary-expression. */
4838 cp_parser_parse_tentatively (parser);
4839 /* Look for the simple-type-specifier. */
4840 type = cp_parser_simple_type_specifier (parser,
4841 /*decl_specs=*/NULL,
4842 CP_PARSER_FLAGS_NONE);
4843 /* Parse the cast itself. */
4844 if (!cp_parser_error_occurred (parser))
4845 postfix_expression
4846 = cp_parser_functional_cast (parser, type);
4847 /* If that worked, we're done. */
4848 if (cp_parser_parse_definitely (parser))
4849 break;
4850
4851 /* If the functional-cast didn't work out, try a
4852 compound-literal. */
4853 if (cp_parser_allow_gnu_extensions_p (parser)
4854 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4855 {
4856 VEC(constructor_elt,gc) *initializer_list = NULL;
4857 bool saved_in_type_id_in_expr_p;
4858
4859 cp_parser_parse_tentatively (parser);
4860 /* Consume the `('. */
4861 cp_lexer_consume_token (parser->lexer);
4862 /* Parse the type. */
4863 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4864 parser->in_type_id_in_expr_p = true;
4865 type = cp_parser_type_id (parser);
4866 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4867 /* Look for the `)'. */
4868 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4869 /* Look for the `{'. */
4870 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4871 /* If things aren't going well, there's no need to
4872 keep going. */
4873 if (!cp_parser_error_occurred (parser))
4874 {
4875 bool non_constant_p;
4876 /* Parse the initializer-list. */
4877 initializer_list
4878 = cp_parser_initializer_list (parser, &non_constant_p);
4879 /* Allow a trailing `,'. */
4880 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4881 cp_lexer_consume_token (parser->lexer);
4882 /* Look for the final `}'. */
4883 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4884 }
4885 /* If that worked, we're definitely looking at a
4886 compound-literal expression. */
4887 if (cp_parser_parse_definitely (parser))
4888 {
4889 /* Warn the user that a compound literal is not
4890 allowed in standard C++. */
4891 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4892 /* For simplicity, we disallow compound literals in
4893 constant-expressions. We could
4894 allow compound literals of integer type, whose
4895 initializer was a constant, in constant
4896 expressions. Permitting that usage, as a further
4897 extension, would not change the meaning of any
4898 currently accepted programs. (Of course, as
4899 compound literals are not part of ISO C++, the
4900 standard has nothing to say.) */
4901 if (cp_parser_non_integral_constant_expression (parser,
4902 NIC_NCC))
4903 {
4904 postfix_expression = error_mark_node;
4905 break;
4906 }
4907 /* Form the representation of the compound-literal. */
4908 postfix_expression
4909 = (finish_compound_literal
4910 (type, build_constructor (init_list_type_node,
4911 initializer_list),
4912 tf_warning_or_error));
4913 break;
4914 }
4915 }
4916
4917 /* It must be a primary-expression. */
4918 postfix_expression
4919 = cp_parser_primary_expression (parser, address_p, cast_p,
4920 /*template_arg_p=*/false,
4921 &idk);
4922 }
4923 break;
4924 }
4925
4926 /* Keep looping until the postfix-expression is complete. */
4927 while (true)
4928 {
4929 if (idk == CP_ID_KIND_UNQUALIFIED
4930 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4931 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4932 /* It is not a Koenig lookup function call. */
4933 postfix_expression
4934 = unqualified_name_lookup_error (postfix_expression);
4935
4936 /* Peek at the next token. */
4937 token = cp_lexer_peek_token (parser->lexer);
4938
4939 switch (token->type)
4940 {
4941 case CPP_OPEN_SQUARE:
4942 postfix_expression
4943 = cp_parser_postfix_open_square_expression (parser,
4944 postfix_expression,
4945 false);
4946 idk = CP_ID_KIND_NONE;
4947 is_member_access = false;
4948 break;
4949
4950 case CPP_OPEN_PAREN:
4951 /* postfix-expression ( expression-list [opt] ) */
4952 {
4953 bool koenig_p;
4954 bool is_builtin_constant_p;
4955 bool saved_integral_constant_expression_p = false;
4956 bool saved_non_integral_constant_expression_p = false;
4957 VEC(tree,gc) *args;
4958
4959 is_member_access = false;
4960
4961 is_builtin_constant_p
4962 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4963 if (is_builtin_constant_p)
4964 {
4965 /* The whole point of __builtin_constant_p is to allow
4966 non-constant expressions to appear as arguments. */
4967 saved_integral_constant_expression_p
4968 = parser->integral_constant_expression_p;
4969 saved_non_integral_constant_expression_p
4970 = parser->non_integral_constant_expression_p;
4971 parser->integral_constant_expression_p = false;
4972 }
4973 args = (cp_parser_parenthesized_expression_list
4974 (parser, non_attr,
4975 /*cast_p=*/false, /*allow_expansion_p=*/true,
4976 /*non_constant_p=*/NULL));
4977 if (is_builtin_constant_p)
4978 {
4979 parser->integral_constant_expression_p
4980 = saved_integral_constant_expression_p;
4981 parser->non_integral_constant_expression_p
4982 = saved_non_integral_constant_expression_p;
4983 }
4984
4985 if (args == NULL)
4986 {
4987 postfix_expression = error_mark_node;
4988 break;
4989 }
4990
4991 /* Function calls are not permitted in
4992 constant-expressions. */
4993 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4994 && cp_parser_non_integral_constant_expression (parser,
4995 NIC_FUNC_CALL))
4996 {
4997 postfix_expression = error_mark_node;
4998 release_tree_vector (args);
4999 break;
5000 }
5001
5002 koenig_p = false;
5003 if (idk == CP_ID_KIND_UNQUALIFIED
5004 || idk == CP_ID_KIND_TEMPLATE_ID)
5005 {
5006 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5007 {
5008 if (!VEC_empty (tree, args))
5009 {
5010 koenig_p = true;
5011 if (!any_type_dependent_arguments_p (args))
5012 postfix_expression
5013 = perform_koenig_lookup (postfix_expression, args,
5014 /*include_std=*/false);
5015 }
5016 else
5017 postfix_expression
5018 = unqualified_fn_lookup_error (postfix_expression);
5019 }
5020 /* We do not perform argument-dependent lookup if
5021 normal lookup finds a non-function, in accordance
5022 with the expected resolution of DR 218. */
5023 else if (!VEC_empty (tree, args)
5024 && is_overloaded_fn (postfix_expression))
5025 {
5026 tree fn = get_first_fn (postfix_expression);
5027 fn = STRIP_TEMPLATE (fn);
5028
5029 /* Do not do argument dependent lookup if regular
5030 lookup finds a member function or a block-scope
5031 function declaration. [basic.lookup.argdep]/3 */
5032 if (!DECL_FUNCTION_MEMBER_P (fn)
5033 && !DECL_LOCAL_FUNCTION_P (fn))
5034 {
5035 koenig_p = true;
5036 if (!any_type_dependent_arguments_p (args))
5037 postfix_expression
5038 = perform_koenig_lookup (postfix_expression, args,
5039 /*include_std=*/false);
5040 }
5041 }
5042 }
5043
5044 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5045 {
5046 tree instance = TREE_OPERAND (postfix_expression, 0);
5047 tree fn = TREE_OPERAND (postfix_expression, 1);
5048
5049 if (processing_template_decl
5050 && (type_dependent_expression_p (instance)
5051 || (!BASELINK_P (fn)
5052 && TREE_CODE (fn) != FIELD_DECL)
5053 || type_dependent_expression_p (fn)
5054 || any_type_dependent_arguments_p (args)))
5055 {
5056 postfix_expression
5057 = build_nt_call_vec (postfix_expression, args);
5058 release_tree_vector (args);
5059 break;
5060 }
5061
5062 if (BASELINK_P (fn))
5063 {
5064 postfix_expression
5065 = (build_new_method_call
5066 (instance, fn, &args, NULL_TREE,
5067 (idk == CP_ID_KIND_QUALIFIED
5068 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5069 : LOOKUP_NORMAL),
5070 /*fn_p=*/NULL,
5071 tf_warning_or_error));
5072 }
5073 else
5074 postfix_expression
5075 = finish_call_expr (postfix_expression, &args,
5076 /*disallow_virtual=*/false,
5077 /*koenig_p=*/false,
5078 tf_warning_or_error);
5079 }
5080 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5081 || TREE_CODE (postfix_expression) == MEMBER_REF
5082 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5083 postfix_expression = (build_offset_ref_call_from_tree
5084 (postfix_expression, &args));
5085 else if (idk == CP_ID_KIND_QUALIFIED)
5086 /* A call to a static class member, or a namespace-scope
5087 function. */
5088 postfix_expression
5089 = finish_call_expr (postfix_expression, &args,
5090 /*disallow_virtual=*/true,
5091 koenig_p,
5092 tf_warning_or_error);
5093 else
5094 /* All other function calls. */
5095 postfix_expression
5096 = finish_call_expr (postfix_expression, &args,
5097 /*disallow_virtual=*/false,
5098 koenig_p,
5099 tf_warning_or_error);
5100
5101 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5102 idk = CP_ID_KIND_NONE;
5103
5104 release_tree_vector (args);
5105 }
5106 break;
5107
5108 case CPP_DOT:
5109 case CPP_DEREF:
5110 /* postfix-expression . template [opt] id-expression
5111 postfix-expression . pseudo-destructor-name
5112 postfix-expression -> template [opt] id-expression
5113 postfix-expression -> pseudo-destructor-name */
5114
5115 /* Consume the `.' or `->' operator. */
5116 cp_lexer_consume_token (parser->lexer);
5117
5118 postfix_expression
5119 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5120 postfix_expression,
5121 false, &idk,
5122 token->location);
5123
5124 is_member_access = true;
5125 break;
5126
5127 case CPP_PLUS_PLUS:
5128 /* postfix-expression ++ */
5129 /* Consume the `++' token. */
5130 cp_lexer_consume_token (parser->lexer);
5131 /* Generate a representation for the complete expression. */
5132 postfix_expression
5133 = finish_increment_expr (postfix_expression,
5134 POSTINCREMENT_EXPR);
5135 /* Increments may not appear in constant-expressions. */
5136 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5137 postfix_expression = error_mark_node;
5138 idk = CP_ID_KIND_NONE;
5139 is_member_access = false;
5140 break;
5141
5142 case CPP_MINUS_MINUS:
5143 /* postfix-expression -- */
5144 /* Consume the `--' token. */
5145 cp_lexer_consume_token (parser->lexer);
5146 /* Generate a representation for the complete expression. */
5147 postfix_expression
5148 = finish_increment_expr (postfix_expression,
5149 POSTDECREMENT_EXPR);
5150 /* Decrements may not appear in constant-expressions. */
5151 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5152 postfix_expression = error_mark_node;
5153 idk = CP_ID_KIND_NONE;
5154 is_member_access = false;
5155 break;
5156
5157 default:
5158 if (pidk_return != NULL)
5159 * pidk_return = idk;
5160 if (member_access_only_p)
5161 return is_member_access? postfix_expression : error_mark_node;
5162 else
5163 return postfix_expression;
5164 }
5165 }
5166
5167 /* We should never get here. */
5168 gcc_unreachable ();
5169 return error_mark_node;
5170 }
5171
5172 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5173 by cp_parser_builtin_offsetof. We're looking for
5174
5175 postfix-expression [ expression ]
5176
5177 FOR_OFFSETOF is set if we're being called in that context, which
5178 changes how we deal with integer constant expressions. */
5179
5180 static tree
5181 cp_parser_postfix_open_square_expression (cp_parser *parser,
5182 tree postfix_expression,
5183 bool for_offsetof)
5184 {
5185 tree index;
5186
5187 /* Consume the `[' token. */
5188 cp_lexer_consume_token (parser->lexer);
5189
5190 /* Parse the index expression. */
5191 /* ??? For offsetof, there is a question of what to allow here. If
5192 offsetof is not being used in an integral constant expression context,
5193 then we *could* get the right answer by computing the value at runtime.
5194 If we are in an integral constant expression context, then we might
5195 could accept any constant expression; hard to say without analysis.
5196 Rather than open the barn door too wide right away, allow only integer
5197 constant expressions here. */
5198 if (for_offsetof)
5199 index = cp_parser_constant_expression (parser, false, NULL);
5200 else
5201 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5202
5203 /* Look for the closing `]'. */
5204 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5205
5206 /* Build the ARRAY_REF. */
5207 postfix_expression = grok_array_decl (postfix_expression, index);
5208
5209 /* When not doing offsetof, array references are not permitted in
5210 constant-expressions. */
5211 if (!for_offsetof
5212 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5213 postfix_expression = error_mark_node;
5214
5215 return postfix_expression;
5216 }
5217
5218 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5219 by cp_parser_builtin_offsetof. We're looking for
5220
5221 postfix-expression . template [opt] id-expression
5222 postfix-expression . pseudo-destructor-name
5223 postfix-expression -> template [opt] id-expression
5224 postfix-expression -> pseudo-destructor-name
5225
5226 FOR_OFFSETOF is set if we're being called in that context. That sorta
5227 limits what of the above we'll actually accept, but nevermind.
5228 TOKEN_TYPE is the "." or "->" token, which will already have been
5229 removed from the stream. */
5230
5231 static tree
5232 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5233 enum cpp_ttype token_type,
5234 tree postfix_expression,
5235 bool for_offsetof, cp_id_kind *idk,
5236 location_t location)
5237 {
5238 tree name;
5239 bool dependent_p;
5240 bool pseudo_destructor_p;
5241 tree scope = NULL_TREE;
5242
5243 /* If this is a `->' operator, dereference the pointer. */
5244 if (token_type == CPP_DEREF)
5245 postfix_expression = build_x_arrow (postfix_expression);
5246 /* Check to see whether or not the expression is type-dependent. */
5247 dependent_p = type_dependent_expression_p (postfix_expression);
5248 /* The identifier following the `->' or `.' is not qualified. */
5249 parser->scope = NULL_TREE;
5250 parser->qualifying_scope = NULL_TREE;
5251 parser->object_scope = NULL_TREE;
5252 *idk = CP_ID_KIND_NONE;
5253
5254 /* Enter the scope corresponding to the type of the object
5255 given by the POSTFIX_EXPRESSION. */
5256 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5257 {
5258 scope = TREE_TYPE (postfix_expression);
5259 /* According to the standard, no expression should ever have
5260 reference type. Unfortunately, we do not currently match
5261 the standard in this respect in that our internal representation
5262 of an expression may have reference type even when the standard
5263 says it does not. Therefore, we have to manually obtain the
5264 underlying type here. */
5265 scope = non_reference (scope);
5266 /* The type of the POSTFIX_EXPRESSION must be complete. */
5267 if (scope == unknown_type_node)
5268 {
5269 error_at (location, "%qE does not have class type",
5270 postfix_expression);
5271 scope = NULL_TREE;
5272 }
5273 else
5274 scope = complete_type_or_else (scope, NULL_TREE);
5275 /* Let the name lookup machinery know that we are processing a
5276 class member access expression. */
5277 parser->context->object_type = scope;
5278 /* If something went wrong, we want to be able to discern that case,
5279 as opposed to the case where there was no SCOPE due to the type
5280 of expression being dependent. */
5281 if (!scope)
5282 scope = error_mark_node;
5283 /* If the SCOPE was erroneous, make the various semantic analysis
5284 functions exit quickly -- and without issuing additional error
5285 messages. */
5286 if (scope == error_mark_node)
5287 postfix_expression = error_mark_node;
5288 }
5289
5290 /* Assume this expression is not a pseudo-destructor access. */
5291 pseudo_destructor_p = false;
5292
5293 /* If the SCOPE is a scalar type, then, if this is a valid program,
5294 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5295 is type dependent, it can be pseudo-destructor-name or something else.
5296 Try to parse it as pseudo-destructor-name first. */
5297 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5298 {
5299 tree s;
5300 tree type;
5301
5302 cp_parser_parse_tentatively (parser);
5303 /* Parse the pseudo-destructor-name. */
5304 s = NULL_TREE;
5305 cp_parser_pseudo_destructor_name (parser, &s, &type);
5306 if (dependent_p
5307 && (cp_parser_error_occurred (parser)
5308 || TREE_CODE (type) != TYPE_DECL
5309 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5310 cp_parser_abort_tentative_parse (parser);
5311 else if (cp_parser_parse_definitely (parser))
5312 {
5313 pseudo_destructor_p = true;
5314 postfix_expression
5315 = finish_pseudo_destructor_expr (postfix_expression,
5316 s, TREE_TYPE (type));
5317 }
5318 }
5319
5320 if (!pseudo_destructor_p)
5321 {
5322 /* If the SCOPE is not a scalar type, we are looking at an
5323 ordinary class member access expression, rather than a
5324 pseudo-destructor-name. */
5325 bool template_p;
5326 cp_token *token = cp_lexer_peek_token (parser->lexer);
5327 /* Parse the id-expression. */
5328 name = (cp_parser_id_expression
5329 (parser,
5330 cp_parser_optional_template_keyword (parser),
5331 /*check_dependency_p=*/true,
5332 &template_p,
5333 /*declarator_p=*/false,
5334 /*optional_p=*/false));
5335 /* In general, build a SCOPE_REF if the member name is qualified.
5336 However, if the name was not dependent and has already been
5337 resolved; there is no need to build the SCOPE_REF. For example;
5338
5339 struct X { void f(); };
5340 template <typename T> void f(T* t) { t->X::f(); }
5341
5342 Even though "t" is dependent, "X::f" is not and has been resolved
5343 to a BASELINK; there is no need to include scope information. */
5344
5345 /* But we do need to remember that there was an explicit scope for
5346 virtual function calls. */
5347 if (parser->scope)
5348 *idk = CP_ID_KIND_QUALIFIED;
5349
5350 /* If the name is a template-id that names a type, we will get a
5351 TYPE_DECL here. That is invalid code. */
5352 if (TREE_CODE (name) == TYPE_DECL)
5353 {
5354 error_at (token->location, "invalid use of %qD", name);
5355 postfix_expression = error_mark_node;
5356 }
5357 else
5358 {
5359 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5360 {
5361 name = build_qualified_name (/*type=*/NULL_TREE,
5362 parser->scope,
5363 name,
5364 template_p);
5365 parser->scope = NULL_TREE;
5366 parser->qualifying_scope = NULL_TREE;
5367 parser->object_scope = NULL_TREE;
5368 }
5369 if (scope && name && BASELINK_P (name))
5370 adjust_result_of_qualified_name_lookup
5371 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5372 postfix_expression
5373 = finish_class_member_access_expr (postfix_expression, name,
5374 template_p,
5375 tf_warning_or_error);
5376 }
5377 }
5378
5379 /* We no longer need to look up names in the scope of the object on
5380 the left-hand side of the `.' or `->' operator. */
5381 parser->context->object_type = NULL_TREE;
5382
5383 /* Outside of offsetof, these operators may not appear in
5384 constant-expressions. */
5385 if (!for_offsetof
5386 && (cp_parser_non_integral_constant_expression
5387 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5388 postfix_expression = error_mark_node;
5389
5390 return postfix_expression;
5391 }
5392
5393 /* Parse a parenthesized expression-list.
5394
5395 expression-list:
5396 assignment-expression
5397 expression-list, assignment-expression
5398
5399 attribute-list:
5400 expression-list
5401 identifier
5402 identifier, expression-list
5403
5404 CAST_P is true if this expression is the target of a cast.
5405
5406 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5407 argument pack.
5408
5409 Returns a vector of trees. Each element is a representation of an
5410 assignment-expression. NULL is returned if the ( and or ) are
5411 missing. An empty, but allocated, vector is returned on no
5412 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5413 if we are parsing an attribute list for an attribute that wants a
5414 plain identifier argument, normal_attr for an attribute that wants
5415 an expression, or non_attr if we aren't parsing an attribute list. If
5416 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5417 not all of the expressions in the list were constant. */
5418
5419 static VEC(tree,gc) *
5420 cp_parser_parenthesized_expression_list (cp_parser* parser,
5421 int is_attribute_list,
5422 bool cast_p,
5423 bool allow_expansion_p,
5424 bool *non_constant_p)
5425 {
5426 VEC(tree,gc) *expression_list;
5427 bool fold_expr_p = is_attribute_list != non_attr;
5428 tree identifier = NULL_TREE;
5429 bool saved_greater_than_is_operator_p;
5430
5431 /* Assume all the expressions will be constant. */
5432 if (non_constant_p)
5433 *non_constant_p = false;
5434
5435 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5436 return NULL;
5437
5438 expression_list = make_tree_vector ();
5439
5440 /* Within a parenthesized expression, a `>' token is always
5441 the greater-than operator. */
5442 saved_greater_than_is_operator_p
5443 = parser->greater_than_is_operator_p;
5444 parser->greater_than_is_operator_p = true;
5445
5446 /* Consume expressions until there are no more. */
5447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5448 while (true)
5449 {
5450 tree expr;
5451
5452 /* At the beginning of attribute lists, check to see if the
5453 next token is an identifier. */
5454 if (is_attribute_list == id_attr
5455 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5456 {
5457 cp_token *token;
5458
5459 /* Consume the identifier. */
5460 token = cp_lexer_consume_token (parser->lexer);
5461 /* Save the identifier. */
5462 identifier = token->u.value;
5463 }
5464 else
5465 {
5466 bool expr_non_constant_p;
5467
5468 /* Parse the next assignment-expression. */
5469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5470 {
5471 /* A braced-init-list. */
5472 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5473 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5474 if (non_constant_p && expr_non_constant_p)
5475 *non_constant_p = true;
5476 }
5477 else if (non_constant_p)
5478 {
5479 expr = (cp_parser_constant_expression
5480 (parser, /*allow_non_constant_p=*/true,
5481 &expr_non_constant_p));
5482 if (expr_non_constant_p)
5483 *non_constant_p = true;
5484 }
5485 else
5486 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5487
5488 if (fold_expr_p)
5489 expr = fold_non_dependent_expr (expr);
5490
5491 /* If we have an ellipsis, then this is an expression
5492 expansion. */
5493 if (allow_expansion_p
5494 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5495 {
5496 /* Consume the `...'. */
5497 cp_lexer_consume_token (parser->lexer);
5498
5499 /* Build the argument pack. */
5500 expr = make_pack_expansion (expr);
5501 }
5502
5503 /* Add it to the list. We add error_mark_node
5504 expressions to the list, so that we can still tell if
5505 the correct form for a parenthesized expression-list
5506 is found. That gives better errors. */
5507 VEC_safe_push (tree, gc, expression_list, expr);
5508
5509 if (expr == error_mark_node)
5510 goto skip_comma;
5511 }
5512
5513 /* After the first item, attribute lists look the same as
5514 expression lists. */
5515 is_attribute_list = non_attr;
5516
5517 get_comma:;
5518 /* If the next token isn't a `,', then we are done. */
5519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5520 break;
5521
5522 /* Otherwise, consume the `,' and keep going. */
5523 cp_lexer_consume_token (parser->lexer);
5524 }
5525
5526 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5527 {
5528 int ending;
5529
5530 skip_comma:;
5531 /* We try and resync to an unnested comma, as that will give the
5532 user better diagnostics. */
5533 ending = cp_parser_skip_to_closing_parenthesis (parser,
5534 /*recovering=*/true,
5535 /*or_comma=*/true,
5536 /*consume_paren=*/true);
5537 if (ending < 0)
5538 goto get_comma;
5539 if (!ending)
5540 {
5541 parser->greater_than_is_operator_p
5542 = saved_greater_than_is_operator_p;
5543 return NULL;
5544 }
5545 }
5546
5547 parser->greater_than_is_operator_p
5548 = saved_greater_than_is_operator_p;
5549
5550 if (identifier)
5551 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5552
5553 return expression_list;
5554 }
5555
5556 /* Parse a pseudo-destructor-name.
5557
5558 pseudo-destructor-name:
5559 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5560 :: [opt] nested-name-specifier template template-id :: ~ type-name
5561 :: [opt] nested-name-specifier [opt] ~ type-name
5562
5563 If either of the first two productions is used, sets *SCOPE to the
5564 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5565 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5566 or ERROR_MARK_NODE if the parse fails. */
5567
5568 static void
5569 cp_parser_pseudo_destructor_name (cp_parser* parser,
5570 tree* scope,
5571 tree* type)
5572 {
5573 bool nested_name_specifier_p;
5574
5575 /* Assume that things will not work out. */
5576 *type = error_mark_node;
5577
5578 /* Look for the optional `::' operator. */
5579 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5580 /* Look for the optional nested-name-specifier. */
5581 nested_name_specifier_p
5582 = (cp_parser_nested_name_specifier_opt (parser,
5583 /*typename_keyword_p=*/false,
5584 /*check_dependency_p=*/true,
5585 /*type_p=*/false,
5586 /*is_declaration=*/false)
5587 != NULL_TREE);
5588 /* Now, if we saw a nested-name-specifier, we might be doing the
5589 second production. */
5590 if (nested_name_specifier_p
5591 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5592 {
5593 /* Consume the `template' keyword. */
5594 cp_lexer_consume_token (parser->lexer);
5595 /* Parse the template-id. */
5596 cp_parser_template_id (parser,
5597 /*template_keyword_p=*/true,
5598 /*check_dependency_p=*/false,
5599 /*is_declaration=*/true);
5600 /* Look for the `::' token. */
5601 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5602 }
5603 /* If the next token is not a `~', then there might be some
5604 additional qualification. */
5605 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5606 {
5607 /* At this point, we're looking for "type-name :: ~". The type-name
5608 must not be a class-name, since this is a pseudo-destructor. So,
5609 it must be either an enum-name, or a typedef-name -- both of which
5610 are just identifiers. So, we peek ahead to check that the "::"
5611 and "~" tokens are present; if they are not, then we can avoid
5612 calling type_name. */
5613 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5614 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5615 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5616 {
5617 cp_parser_error (parser, "non-scalar type");
5618 return;
5619 }
5620
5621 /* Look for the type-name. */
5622 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5623 if (*scope == error_mark_node)
5624 return;
5625
5626 /* Look for the `::' token. */
5627 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5628 }
5629 else
5630 *scope = NULL_TREE;
5631
5632 /* Look for the `~'. */
5633 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5634 /* Look for the type-name again. We are not responsible for
5635 checking that it matches the first type-name. */
5636 *type = cp_parser_nonclass_name (parser);
5637 }
5638
5639 /* Parse a unary-expression.
5640
5641 unary-expression:
5642 postfix-expression
5643 ++ cast-expression
5644 -- cast-expression
5645 unary-operator cast-expression
5646 sizeof unary-expression
5647 sizeof ( type-id )
5648 alignof ( type-id ) [C++0x]
5649 new-expression
5650 delete-expression
5651
5652 GNU Extensions:
5653
5654 unary-expression:
5655 __extension__ cast-expression
5656 __alignof__ unary-expression
5657 __alignof__ ( type-id )
5658 alignof unary-expression [C++0x]
5659 __real__ cast-expression
5660 __imag__ cast-expression
5661 && identifier
5662
5663 ADDRESS_P is true iff the unary-expression is appearing as the
5664 operand of the `&' operator. CAST_P is true if this expression is
5665 the target of a cast.
5666
5667 Returns a representation of the expression. */
5668
5669 static tree
5670 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5671 cp_id_kind * pidk)
5672 {
5673 cp_token *token;
5674 enum tree_code unary_operator;
5675
5676 /* Peek at the next token. */
5677 token = cp_lexer_peek_token (parser->lexer);
5678 /* Some keywords give away the kind of expression. */
5679 if (token->type == CPP_KEYWORD)
5680 {
5681 enum rid keyword = token->keyword;
5682
5683 switch (keyword)
5684 {
5685 case RID_ALIGNOF:
5686 case RID_SIZEOF:
5687 {
5688 tree operand;
5689 enum tree_code op;
5690
5691 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5692 /* Consume the token. */
5693 cp_lexer_consume_token (parser->lexer);
5694 /* Parse the operand. */
5695 operand = cp_parser_sizeof_operand (parser, keyword);
5696
5697 if (TYPE_P (operand))
5698 return cxx_sizeof_or_alignof_type (operand, op, true);
5699 else
5700 {
5701 /* ISO C++ defines alignof only with types, not with
5702 expressions. So pedwarn if alignof is used with a non-
5703 type expression. However, __alignof__ is ok. */
5704 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5705 pedwarn (token->location, OPT_pedantic,
5706 "ISO C++ does not allow %<alignof%> "
5707 "with a non-type");
5708
5709 return cxx_sizeof_or_alignof_expr (operand, op, true);
5710 }
5711 }
5712
5713 case RID_NEW:
5714 return cp_parser_new_expression (parser);
5715
5716 case RID_DELETE:
5717 return cp_parser_delete_expression (parser);
5718
5719 case RID_EXTENSION:
5720 {
5721 /* The saved value of the PEDANTIC flag. */
5722 int saved_pedantic;
5723 tree expr;
5724
5725 /* Save away the PEDANTIC flag. */
5726 cp_parser_extension_opt (parser, &saved_pedantic);
5727 /* Parse the cast-expression. */
5728 expr = cp_parser_simple_cast_expression (parser);
5729 /* Restore the PEDANTIC flag. */
5730 pedantic = saved_pedantic;
5731
5732 return expr;
5733 }
5734
5735 case RID_REALPART:
5736 case RID_IMAGPART:
5737 {
5738 tree expression;
5739
5740 /* Consume the `__real__' or `__imag__' token. */
5741 cp_lexer_consume_token (parser->lexer);
5742 /* Parse the cast-expression. */
5743 expression = cp_parser_simple_cast_expression (parser);
5744 /* Create the complete representation. */
5745 return build_x_unary_op ((keyword == RID_REALPART
5746 ? REALPART_EXPR : IMAGPART_EXPR),
5747 expression,
5748 tf_warning_or_error);
5749 }
5750 break;
5751
5752 case RID_NOEXCEPT:
5753 {
5754 tree expr;
5755 const char *saved_message;
5756 bool saved_integral_constant_expression_p;
5757 bool saved_non_integral_constant_expression_p;
5758 bool saved_greater_than_is_operator_p;
5759
5760 cp_lexer_consume_token (parser->lexer);
5761 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5762
5763 saved_message = parser->type_definition_forbidden_message;
5764 parser->type_definition_forbidden_message
5765 = G_("types may not be defined in %<noexcept%> expressions");
5766
5767 saved_integral_constant_expression_p
5768 = parser->integral_constant_expression_p;
5769 saved_non_integral_constant_expression_p
5770 = parser->non_integral_constant_expression_p;
5771 parser->integral_constant_expression_p = false;
5772
5773 saved_greater_than_is_operator_p
5774 = parser->greater_than_is_operator_p;
5775 parser->greater_than_is_operator_p = true;
5776
5777 ++cp_unevaluated_operand;
5778 ++c_inhibit_evaluation_warnings;
5779 expr = cp_parser_expression (parser, false, NULL);
5780 --c_inhibit_evaluation_warnings;
5781 --cp_unevaluated_operand;
5782
5783 parser->greater_than_is_operator_p
5784 = saved_greater_than_is_operator_p;
5785
5786 parser->integral_constant_expression_p
5787 = saved_integral_constant_expression_p;
5788 parser->non_integral_constant_expression_p
5789 = saved_non_integral_constant_expression_p;
5790
5791 parser->type_definition_forbidden_message = saved_message;
5792
5793 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5794 return finish_noexcept_expr (expr, tf_warning_or_error);
5795 }
5796
5797 default:
5798 break;
5799 }
5800 }
5801
5802 /* Look for the `:: new' and `:: delete', which also signal the
5803 beginning of a new-expression, or delete-expression,
5804 respectively. If the next token is `::', then it might be one of
5805 these. */
5806 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5807 {
5808 enum rid keyword;
5809
5810 /* See if the token after the `::' is one of the keywords in
5811 which we're interested. */
5812 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5813 /* If it's `new', we have a new-expression. */
5814 if (keyword == RID_NEW)
5815 return cp_parser_new_expression (parser);
5816 /* Similarly, for `delete'. */
5817 else if (keyword == RID_DELETE)
5818 return cp_parser_delete_expression (parser);
5819 }
5820
5821 /* Look for a unary operator. */
5822 unary_operator = cp_parser_unary_operator (token);
5823 /* The `++' and `--' operators can be handled similarly, even though
5824 they are not technically unary-operators in the grammar. */
5825 if (unary_operator == ERROR_MARK)
5826 {
5827 if (token->type == CPP_PLUS_PLUS)
5828 unary_operator = PREINCREMENT_EXPR;
5829 else if (token->type == CPP_MINUS_MINUS)
5830 unary_operator = PREDECREMENT_EXPR;
5831 /* Handle the GNU address-of-label extension. */
5832 else if (cp_parser_allow_gnu_extensions_p (parser)
5833 && token->type == CPP_AND_AND)
5834 {
5835 tree identifier;
5836 tree expression;
5837 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5838
5839 /* Consume the '&&' token. */
5840 cp_lexer_consume_token (parser->lexer);
5841 /* Look for the identifier. */
5842 identifier = cp_parser_identifier (parser);
5843 /* Create an expression representing the address. */
5844 expression = finish_label_address_expr (identifier, loc);
5845 if (cp_parser_non_integral_constant_expression (parser,
5846 NIC_ADDR_LABEL))
5847 expression = error_mark_node;
5848 return expression;
5849 }
5850 }
5851 if (unary_operator != ERROR_MARK)
5852 {
5853 tree cast_expression;
5854 tree expression = error_mark_node;
5855 non_integral_constant non_constant_p = NIC_NONE;
5856
5857 /* Consume the operator token. */
5858 token = cp_lexer_consume_token (parser->lexer);
5859 /* Parse the cast-expression. */
5860 cast_expression
5861 = cp_parser_cast_expression (parser,
5862 unary_operator == ADDR_EXPR,
5863 /*cast_p=*/false, pidk);
5864 /* Now, build an appropriate representation. */
5865 switch (unary_operator)
5866 {
5867 case INDIRECT_REF:
5868 non_constant_p = NIC_STAR;
5869 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5870 tf_warning_or_error);
5871 break;
5872
5873 case ADDR_EXPR:
5874 non_constant_p = NIC_ADDR;
5875 /* Fall through. */
5876 case BIT_NOT_EXPR:
5877 expression = build_x_unary_op (unary_operator, cast_expression,
5878 tf_warning_or_error);
5879 break;
5880
5881 case PREINCREMENT_EXPR:
5882 case PREDECREMENT_EXPR:
5883 non_constant_p = unary_operator == PREINCREMENT_EXPR
5884 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5885 /* Fall through. */
5886 case UNARY_PLUS_EXPR:
5887 case NEGATE_EXPR:
5888 case TRUTH_NOT_EXPR:
5889 expression = finish_unary_op_expr (unary_operator, cast_expression);
5890 break;
5891
5892 default:
5893 gcc_unreachable ();
5894 }
5895
5896 if (non_constant_p != NIC_NONE
5897 && cp_parser_non_integral_constant_expression (parser,
5898 non_constant_p))
5899 expression = error_mark_node;
5900
5901 return expression;
5902 }
5903
5904 return cp_parser_postfix_expression (parser, address_p, cast_p,
5905 /*member_access_only_p=*/false,
5906 pidk);
5907 }
5908
5909 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5910 unary-operator, the corresponding tree code is returned. */
5911
5912 static enum tree_code
5913 cp_parser_unary_operator (cp_token* token)
5914 {
5915 switch (token->type)
5916 {
5917 case CPP_MULT:
5918 return INDIRECT_REF;
5919
5920 case CPP_AND:
5921 return ADDR_EXPR;
5922
5923 case CPP_PLUS:
5924 return UNARY_PLUS_EXPR;
5925
5926 case CPP_MINUS:
5927 return NEGATE_EXPR;
5928
5929 case CPP_NOT:
5930 return TRUTH_NOT_EXPR;
5931
5932 case CPP_COMPL:
5933 return BIT_NOT_EXPR;
5934
5935 default:
5936 return ERROR_MARK;
5937 }
5938 }
5939
5940 /* Parse a new-expression.
5941
5942 new-expression:
5943 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5944 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5945
5946 Returns a representation of the expression. */
5947
5948 static tree
5949 cp_parser_new_expression (cp_parser* parser)
5950 {
5951 bool global_scope_p;
5952 VEC(tree,gc) *placement;
5953 tree type;
5954 VEC(tree,gc) *initializer;
5955 tree nelts;
5956 tree ret;
5957
5958 /* Look for the optional `::' operator. */
5959 global_scope_p
5960 = (cp_parser_global_scope_opt (parser,
5961 /*current_scope_valid_p=*/false)
5962 != NULL_TREE);
5963 /* Look for the `new' operator. */
5964 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5965 /* There's no easy way to tell a new-placement from the
5966 `( type-id )' construct. */
5967 cp_parser_parse_tentatively (parser);
5968 /* Look for a new-placement. */
5969 placement = cp_parser_new_placement (parser);
5970 /* If that didn't work out, there's no new-placement. */
5971 if (!cp_parser_parse_definitely (parser))
5972 {
5973 if (placement != NULL)
5974 release_tree_vector (placement);
5975 placement = NULL;
5976 }
5977
5978 /* If the next token is a `(', then we have a parenthesized
5979 type-id. */
5980 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5981 {
5982 cp_token *token;
5983 /* Consume the `('. */
5984 cp_lexer_consume_token (parser->lexer);
5985 /* Parse the type-id. */
5986 type = cp_parser_type_id (parser);
5987 /* Look for the closing `)'. */
5988 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5989 token = cp_lexer_peek_token (parser->lexer);
5990 /* There should not be a direct-new-declarator in this production,
5991 but GCC used to allowed this, so we check and emit a sensible error
5992 message for this case. */
5993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5994 {
5995 error_at (token->location,
5996 "array bound forbidden after parenthesized type-id");
5997 inform (token->location,
5998 "try removing the parentheses around the type-id");
5999 cp_parser_direct_new_declarator (parser);
6000 }
6001 nelts = NULL_TREE;
6002 }
6003 /* Otherwise, there must be a new-type-id. */
6004 else
6005 type = cp_parser_new_type_id (parser, &nelts);
6006
6007 /* If the next token is a `(' or '{', then we have a new-initializer. */
6008 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6009 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6010 initializer = cp_parser_new_initializer (parser);
6011 else
6012 initializer = NULL;
6013
6014 /* A new-expression may not appear in an integral constant
6015 expression. */
6016 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6017 ret = error_mark_node;
6018 else
6019 {
6020 /* Create a representation of the new-expression. */
6021 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6022 tf_warning_or_error);
6023 }
6024
6025 if (placement != NULL)
6026 release_tree_vector (placement);
6027 if (initializer != NULL)
6028 release_tree_vector (initializer);
6029
6030 return ret;
6031 }
6032
6033 /* Parse a new-placement.
6034
6035 new-placement:
6036 ( expression-list )
6037
6038 Returns the same representation as for an expression-list. */
6039
6040 static VEC(tree,gc) *
6041 cp_parser_new_placement (cp_parser* parser)
6042 {
6043 VEC(tree,gc) *expression_list;
6044
6045 /* Parse the expression-list. */
6046 expression_list = (cp_parser_parenthesized_expression_list
6047 (parser, non_attr, /*cast_p=*/false,
6048 /*allow_expansion_p=*/true,
6049 /*non_constant_p=*/NULL));
6050
6051 return expression_list;
6052 }
6053
6054 /* Parse a new-type-id.
6055
6056 new-type-id:
6057 type-specifier-seq new-declarator [opt]
6058
6059 Returns the TYPE allocated. If the new-type-id indicates an array
6060 type, *NELTS is set to the number of elements in the last array
6061 bound; the TYPE will not include the last array bound. */
6062
6063 static tree
6064 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6065 {
6066 cp_decl_specifier_seq type_specifier_seq;
6067 cp_declarator *new_declarator;
6068 cp_declarator *declarator;
6069 cp_declarator *outer_declarator;
6070 const char *saved_message;
6071 tree type;
6072
6073 /* The type-specifier sequence must not contain type definitions.
6074 (It cannot contain declarations of new types either, but if they
6075 are not definitions we will catch that because they are not
6076 complete.) */
6077 saved_message = parser->type_definition_forbidden_message;
6078 parser->type_definition_forbidden_message
6079 = G_("types may not be defined in a new-type-id");
6080 /* Parse the type-specifier-seq. */
6081 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6082 /*is_trailing_return=*/false,
6083 &type_specifier_seq);
6084 /* Restore the old message. */
6085 parser->type_definition_forbidden_message = saved_message;
6086 /* Parse the new-declarator. */
6087 new_declarator = cp_parser_new_declarator_opt (parser);
6088
6089 /* Determine the number of elements in the last array dimension, if
6090 any. */
6091 *nelts = NULL_TREE;
6092 /* Skip down to the last array dimension. */
6093 declarator = new_declarator;
6094 outer_declarator = NULL;
6095 while (declarator && (declarator->kind == cdk_pointer
6096 || declarator->kind == cdk_ptrmem))
6097 {
6098 outer_declarator = declarator;
6099 declarator = declarator->declarator;
6100 }
6101 while (declarator
6102 && declarator->kind == cdk_array
6103 && declarator->declarator
6104 && declarator->declarator->kind == cdk_array)
6105 {
6106 outer_declarator = declarator;
6107 declarator = declarator->declarator;
6108 }
6109
6110 if (declarator && declarator->kind == cdk_array)
6111 {
6112 *nelts = declarator->u.array.bounds;
6113 if (*nelts == error_mark_node)
6114 *nelts = integer_one_node;
6115
6116 if (outer_declarator)
6117 outer_declarator->declarator = declarator->declarator;
6118 else
6119 new_declarator = NULL;
6120 }
6121
6122 type = groktypename (&type_specifier_seq, new_declarator, false);
6123 return type;
6124 }
6125
6126 /* Parse an (optional) new-declarator.
6127
6128 new-declarator:
6129 ptr-operator new-declarator [opt]
6130 direct-new-declarator
6131
6132 Returns the declarator. */
6133
6134 static cp_declarator *
6135 cp_parser_new_declarator_opt (cp_parser* parser)
6136 {
6137 enum tree_code code;
6138 tree type;
6139 cp_cv_quals cv_quals;
6140
6141 /* We don't know if there's a ptr-operator next, or not. */
6142 cp_parser_parse_tentatively (parser);
6143 /* Look for a ptr-operator. */
6144 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6145 /* If that worked, look for more new-declarators. */
6146 if (cp_parser_parse_definitely (parser))
6147 {
6148 cp_declarator *declarator;
6149
6150 /* Parse another optional declarator. */
6151 declarator = cp_parser_new_declarator_opt (parser);
6152
6153 return cp_parser_make_indirect_declarator
6154 (code, type, cv_quals, declarator);
6155 }
6156
6157 /* If the next token is a `[', there is a direct-new-declarator. */
6158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6159 return cp_parser_direct_new_declarator (parser);
6160
6161 return NULL;
6162 }
6163
6164 /* Parse a direct-new-declarator.
6165
6166 direct-new-declarator:
6167 [ expression ]
6168 direct-new-declarator [constant-expression]
6169
6170 */
6171
6172 static cp_declarator *
6173 cp_parser_direct_new_declarator (cp_parser* parser)
6174 {
6175 cp_declarator *declarator = NULL;
6176
6177 while (true)
6178 {
6179 tree expression;
6180
6181 /* Look for the opening `['. */
6182 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6183 /* The first expression is not required to be constant. */
6184 if (!declarator)
6185 {
6186 cp_token *token = cp_lexer_peek_token (parser->lexer);
6187 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6188 /* The standard requires that the expression have integral
6189 type. DR 74 adds enumeration types. We believe that the
6190 real intent is that these expressions be handled like the
6191 expression in a `switch' condition, which also allows
6192 classes with a single conversion to integral or
6193 enumeration type. */
6194 if (!processing_template_decl)
6195 {
6196 expression
6197 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6198 expression,
6199 /*complain=*/true);
6200 if (!expression)
6201 {
6202 error_at (token->location,
6203 "expression in new-declarator must have integral "
6204 "or enumeration type");
6205 expression = error_mark_node;
6206 }
6207 }
6208 }
6209 /* But all the other expressions must be. */
6210 else
6211 expression
6212 = cp_parser_constant_expression (parser,
6213 /*allow_non_constant=*/false,
6214 NULL);
6215 /* Look for the closing `]'. */
6216 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6217
6218 /* Add this bound to the declarator. */
6219 declarator = make_array_declarator (declarator, expression);
6220
6221 /* If the next token is not a `[', then there are no more
6222 bounds. */
6223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6224 break;
6225 }
6226
6227 return declarator;
6228 }
6229
6230 /* Parse a new-initializer.
6231
6232 new-initializer:
6233 ( expression-list [opt] )
6234 braced-init-list
6235
6236 Returns a representation of the expression-list. */
6237
6238 static VEC(tree,gc) *
6239 cp_parser_new_initializer (cp_parser* parser)
6240 {
6241 VEC(tree,gc) *expression_list;
6242
6243 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6244 {
6245 tree t;
6246 bool expr_non_constant_p;
6247 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6248 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6249 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6250 expression_list = make_tree_vector_single (t);
6251 }
6252 else
6253 expression_list = (cp_parser_parenthesized_expression_list
6254 (parser, non_attr, /*cast_p=*/false,
6255 /*allow_expansion_p=*/true,
6256 /*non_constant_p=*/NULL));
6257
6258 return expression_list;
6259 }
6260
6261 /* Parse a delete-expression.
6262
6263 delete-expression:
6264 :: [opt] delete cast-expression
6265 :: [opt] delete [ ] cast-expression
6266
6267 Returns a representation of the expression. */
6268
6269 static tree
6270 cp_parser_delete_expression (cp_parser* parser)
6271 {
6272 bool global_scope_p;
6273 bool array_p;
6274 tree expression;
6275
6276 /* Look for the optional `::' operator. */
6277 global_scope_p
6278 = (cp_parser_global_scope_opt (parser,
6279 /*current_scope_valid_p=*/false)
6280 != NULL_TREE);
6281 /* Look for the `delete' keyword. */
6282 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6283 /* See if the array syntax is in use. */
6284 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6285 {
6286 /* Consume the `[' token. */
6287 cp_lexer_consume_token (parser->lexer);
6288 /* Look for the `]' token. */
6289 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6290 /* Remember that this is the `[]' construct. */
6291 array_p = true;
6292 }
6293 else
6294 array_p = false;
6295
6296 /* Parse the cast-expression. */
6297 expression = cp_parser_simple_cast_expression (parser);
6298
6299 /* A delete-expression may not appear in an integral constant
6300 expression. */
6301 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6302 return error_mark_node;
6303
6304 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6305 tf_warning_or_error);
6306 }
6307
6308 /* Returns true if TOKEN may start a cast-expression and false
6309 otherwise. */
6310
6311 static bool
6312 cp_parser_token_starts_cast_expression (cp_token *token)
6313 {
6314 switch (token->type)
6315 {
6316 case CPP_COMMA:
6317 case CPP_SEMICOLON:
6318 case CPP_QUERY:
6319 case CPP_COLON:
6320 case CPP_CLOSE_SQUARE:
6321 case CPP_CLOSE_PAREN:
6322 case CPP_CLOSE_BRACE:
6323 case CPP_DOT:
6324 case CPP_DOT_STAR:
6325 case CPP_DEREF:
6326 case CPP_DEREF_STAR:
6327 case CPP_DIV:
6328 case CPP_MOD:
6329 case CPP_LSHIFT:
6330 case CPP_RSHIFT:
6331 case CPP_LESS:
6332 case CPP_GREATER:
6333 case CPP_LESS_EQ:
6334 case CPP_GREATER_EQ:
6335 case CPP_EQ_EQ:
6336 case CPP_NOT_EQ:
6337 case CPP_EQ:
6338 case CPP_MULT_EQ:
6339 case CPP_DIV_EQ:
6340 case CPP_MOD_EQ:
6341 case CPP_PLUS_EQ:
6342 case CPP_MINUS_EQ:
6343 case CPP_RSHIFT_EQ:
6344 case CPP_LSHIFT_EQ:
6345 case CPP_AND_EQ:
6346 case CPP_XOR_EQ:
6347 case CPP_OR_EQ:
6348 case CPP_XOR:
6349 case CPP_OR:
6350 case CPP_OR_OR:
6351 case CPP_EOF:
6352 return false;
6353
6354 /* '[' may start a primary-expression in obj-c++. */
6355 case CPP_OPEN_SQUARE:
6356 return c_dialect_objc ();
6357
6358 default:
6359 return true;
6360 }
6361 }
6362
6363 /* Parse a cast-expression.
6364
6365 cast-expression:
6366 unary-expression
6367 ( type-id ) cast-expression
6368
6369 ADDRESS_P is true iff the unary-expression is appearing as the
6370 operand of the `&' operator. CAST_P is true if this expression is
6371 the target of a cast.
6372
6373 Returns a representation of the expression. */
6374
6375 static tree
6376 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6377 cp_id_kind * pidk)
6378 {
6379 /* If it's a `(', then we might be looking at a cast. */
6380 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6381 {
6382 tree type = NULL_TREE;
6383 tree expr = NULL_TREE;
6384 bool compound_literal_p;
6385 const char *saved_message;
6386
6387 /* There's no way to know yet whether or not this is a cast.
6388 For example, `(int (3))' is a unary-expression, while `(int)
6389 3' is a cast. So, we resort to parsing tentatively. */
6390 cp_parser_parse_tentatively (parser);
6391 /* Types may not be defined in a cast. */
6392 saved_message = parser->type_definition_forbidden_message;
6393 parser->type_definition_forbidden_message
6394 = G_("types may not be defined in casts");
6395 /* Consume the `('. */
6396 cp_lexer_consume_token (parser->lexer);
6397 /* A very tricky bit is that `(struct S) { 3 }' is a
6398 compound-literal (which we permit in C++ as an extension).
6399 But, that construct is not a cast-expression -- it is a
6400 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6401 is legal; if the compound-literal were a cast-expression,
6402 you'd need an extra set of parentheses.) But, if we parse
6403 the type-id, and it happens to be a class-specifier, then we
6404 will commit to the parse at that point, because we cannot
6405 undo the action that is done when creating a new class. So,
6406 then we cannot back up and do a postfix-expression.
6407
6408 Therefore, we scan ahead to the closing `)', and check to see
6409 if the token after the `)' is a `{'. If so, we are not
6410 looking at a cast-expression.
6411
6412 Save tokens so that we can put them back. */
6413 cp_lexer_save_tokens (parser->lexer);
6414 /* Skip tokens until the next token is a closing parenthesis.
6415 If we find the closing `)', and the next token is a `{', then
6416 we are looking at a compound-literal. */
6417 compound_literal_p
6418 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6419 /*consume_paren=*/true)
6420 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6421 /* Roll back the tokens we skipped. */
6422 cp_lexer_rollback_tokens (parser->lexer);
6423 /* If we were looking at a compound-literal, simulate an error
6424 so that the call to cp_parser_parse_definitely below will
6425 fail. */
6426 if (compound_literal_p)
6427 cp_parser_simulate_error (parser);
6428 else
6429 {
6430 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6431 parser->in_type_id_in_expr_p = true;
6432 /* Look for the type-id. */
6433 type = cp_parser_type_id (parser);
6434 /* Look for the closing `)'. */
6435 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6436 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6437 }
6438
6439 /* Restore the saved message. */
6440 parser->type_definition_forbidden_message = saved_message;
6441
6442 /* At this point this can only be either a cast or a
6443 parenthesized ctor such as `(T ())' that looks like a cast to
6444 function returning T. */
6445 if (!cp_parser_error_occurred (parser)
6446 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6447 (parser->lexer)))
6448 {
6449 cp_parser_parse_definitely (parser);
6450 expr = cp_parser_cast_expression (parser,
6451 /*address_p=*/false,
6452 /*cast_p=*/true, pidk);
6453
6454 /* Warn about old-style casts, if so requested. */
6455 if (warn_old_style_cast
6456 && !in_system_header
6457 && !VOID_TYPE_P (type)
6458 && current_lang_name != lang_name_c)
6459 warning (OPT_Wold_style_cast, "use of old-style cast");
6460
6461 /* Only type conversions to integral or enumeration types
6462 can be used in constant-expressions. */
6463 if (!cast_valid_in_integral_constant_expression_p (type)
6464 && cp_parser_non_integral_constant_expression (parser,
6465 NIC_CAST))
6466 return error_mark_node;
6467
6468 /* Perform the cast. */
6469 expr = build_c_cast (input_location, type, expr);
6470 return expr;
6471 }
6472 else
6473 cp_parser_abort_tentative_parse (parser);
6474 }
6475
6476 /* If we get here, then it's not a cast, so it must be a
6477 unary-expression. */
6478 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6479 }
6480
6481 /* Parse a binary expression of the general form:
6482
6483 pm-expression:
6484 cast-expression
6485 pm-expression .* cast-expression
6486 pm-expression ->* cast-expression
6487
6488 multiplicative-expression:
6489 pm-expression
6490 multiplicative-expression * pm-expression
6491 multiplicative-expression / pm-expression
6492 multiplicative-expression % pm-expression
6493
6494 additive-expression:
6495 multiplicative-expression
6496 additive-expression + multiplicative-expression
6497 additive-expression - multiplicative-expression
6498
6499 shift-expression:
6500 additive-expression
6501 shift-expression << additive-expression
6502 shift-expression >> additive-expression
6503
6504 relational-expression:
6505 shift-expression
6506 relational-expression < shift-expression
6507 relational-expression > shift-expression
6508 relational-expression <= shift-expression
6509 relational-expression >= shift-expression
6510
6511 GNU Extension:
6512
6513 relational-expression:
6514 relational-expression <? shift-expression
6515 relational-expression >? shift-expression
6516
6517 equality-expression:
6518 relational-expression
6519 equality-expression == relational-expression
6520 equality-expression != relational-expression
6521
6522 and-expression:
6523 equality-expression
6524 and-expression & equality-expression
6525
6526 exclusive-or-expression:
6527 and-expression
6528 exclusive-or-expression ^ and-expression
6529
6530 inclusive-or-expression:
6531 exclusive-or-expression
6532 inclusive-or-expression | exclusive-or-expression
6533
6534 logical-and-expression:
6535 inclusive-or-expression
6536 logical-and-expression && inclusive-or-expression
6537
6538 logical-or-expression:
6539 logical-and-expression
6540 logical-or-expression || logical-and-expression
6541
6542 All these are implemented with a single function like:
6543
6544 binary-expression:
6545 simple-cast-expression
6546 binary-expression <token> binary-expression
6547
6548 CAST_P is true if this expression is the target of a cast.
6549
6550 The binops_by_token map is used to get the tree codes for each <token> type.
6551 binary-expressions are associated according to a precedence table. */
6552
6553 #define TOKEN_PRECEDENCE(token) \
6554 (((token->type == CPP_GREATER \
6555 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6556 && !parser->greater_than_is_operator_p) \
6557 ? PREC_NOT_OPERATOR \
6558 : binops_by_token[token->type].prec)
6559
6560 static tree
6561 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6562 bool no_toplevel_fold_p,
6563 enum cp_parser_prec prec,
6564 cp_id_kind * pidk)
6565 {
6566 cp_parser_expression_stack stack;
6567 cp_parser_expression_stack_entry *sp = &stack[0];
6568 tree lhs, rhs;
6569 cp_token *token;
6570 enum tree_code tree_type, lhs_type, rhs_type;
6571 enum cp_parser_prec new_prec, lookahead_prec;
6572 bool overloaded_p;
6573
6574 /* Parse the first expression. */
6575 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6576 lhs_type = ERROR_MARK;
6577
6578 for (;;)
6579 {
6580 /* Get an operator token. */
6581 token = cp_lexer_peek_token (parser->lexer);
6582
6583 if (warn_cxx0x_compat
6584 && token->type == CPP_RSHIFT
6585 && !parser->greater_than_is_operator_p)
6586 {
6587 if (warning_at (token->location, OPT_Wc__0x_compat,
6588 "%<>>%> operator will be treated as"
6589 " two right angle brackets in C++0x"))
6590 inform (token->location,
6591 "suggest parentheses around %<>>%> expression");
6592 }
6593
6594 new_prec = TOKEN_PRECEDENCE (token);
6595
6596 /* Popping an entry off the stack means we completed a subexpression:
6597 - either we found a token which is not an operator (`>' where it is not
6598 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6599 will happen repeatedly;
6600 - or, we found an operator which has lower priority. This is the case
6601 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6602 parsing `3 * 4'. */
6603 if (new_prec <= prec)
6604 {
6605 if (sp == stack)
6606 break;
6607 else
6608 goto pop;
6609 }
6610
6611 get_rhs:
6612 tree_type = binops_by_token[token->type].tree_type;
6613
6614 /* We used the operator token. */
6615 cp_lexer_consume_token (parser->lexer);
6616
6617 /* For "false && x" or "true || x", x will never be executed;
6618 disable warnings while evaluating it. */
6619 if (tree_type == TRUTH_ANDIF_EXPR)
6620 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6621 else if (tree_type == TRUTH_ORIF_EXPR)
6622 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6623
6624 /* Extract another operand. It may be the RHS of this expression
6625 or the LHS of a new, higher priority expression. */
6626 rhs = cp_parser_simple_cast_expression (parser);
6627 rhs_type = ERROR_MARK;
6628
6629 /* Get another operator token. Look up its precedence to avoid
6630 building a useless (immediately popped) stack entry for common
6631 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6632 token = cp_lexer_peek_token (parser->lexer);
6633 lookahead_prec = TOKEN_PRECEDENCE (token);
6634 if (lookahead_prec > new_prec)
6635 {
6636 /* ... and prepare to parse the RHS of the new, higher priority
6637 expression. Since precedence levels on the stack are
6638 monotonically increasing, we do not have to care about
6639 stack overflows. */
6640 sp->prec = prec;
6641 sp->tree_type = tree_type;
6642 sp->lhs = lhs;
6643 sp->lhs_type = lhs_type;
6644 sp++;
6645 lhs = rhs;
6646 lhs_type = rhs_type;
6647 prec = new_prec;
6648 new_prec = lookahead_prec;
6649 goto get_rhs;
6650
6651 pop:
6652 lookahead_prec = new_prec;
6653 /* If the stack is not empty, we have parsed into LHS the right side
6654 (`4' in the example above) of an expression we had suspended.
6655 We can use the information on the stack to recover the LHS (`3')
6656 from the stack together with the tree code (`MULT_EXPR'), and
6657 the precedence of the higher level subexpression
6658 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6659 which will be used to actually build the additive expression. */
6660 --sp;
6661 prec = sp->prec;
6662 tree_type = sp->tree_type;
6663 rhs = lhs;
6664 rhs_type = lhs_type;
6665 lhs = sp->lhs;
6666 lhs_type = sp->lhs_type;
6667 }
6668
6669 /* Undo the disabling of warnings done above. */
6670 if (tree_type == TRUTH_ANDIF_EXPR)
6671 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6672 else if (tree_type == TRUTH_ORIF_EXPR)
6673 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6674
6675 overloaded_p = false;
6676 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6677 ERROR_MARK for everything that is not a binary expression.
6678 This makes warn_about_parentheses miss some warnings that
6679 involve unary operators. For unary expressions we should
6680 pass the correct tree_code unless the unary expression was
6681 surrounded by parentheses.
6682 */
6683 if (no_toplevel_fold_p
6684 && lookahead_prec <= prec
6685 && sp == stack
6686 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6687 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6688 else
6689 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6690 &overloaded_p, tf_warning_or_error);
6691 lhs_type = tree_type;
6692
6693 /* If the binary operator required the use of an overloaded operator,
6694 then this expression cannot be an integral constant-expression.
6695 An overloaded operator can be used even if both operands are
6696 otherwise permissible in an integral constant-expression if at
6697 least one of the operands is of enumeration type. */
6698
6699 if (overloaded_p
6700 && cp_parser_non_integral_constant_expression (parser,
6701 NIC_OVERLOADED))
6702 return error_mark_node;
6703 }
6704
6705 return lhs;
6706 }
6707
6708
6709 /* Parse the `? expression : assignment-expression' part of a
6710 conditional-expression. The LOGICAL_OR_EXPR is the
6711 logical-or-expression that started the conditional-expression.
6712 Returns a representation of the entire conditional-expression.
6713
6714 This routine is used by cp_parser_assignment_expression.
6715
6716 ? expression : assignment-expression
6717
6718 GNU Extensions:
6719
6720 ? : assignment-expression */
6721
6722 static tree
6723 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6724 {
6725 tree expr;
6726 tree assignment_expr;
6727 struct cp_token *token;
6728
6729 /* Consume the `?' token. */
6730 cp_lexer_consume_token (parser->lexer);
6731 token = cp_lexer_peek_token (parser->lexer);
6732 if (cp_parser_allow_gnu_extensions_p (parser)
6733 && token->type == CPP_COLON)
6734 {
6735 pedwarn (token->location, OPT_pedantic,
6736 "ISO C++ does not allow ?: with omitted middle operand");
6737 /* Implicit true clause. */
6738 expr = NULL_TREE;
6739 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6740 warn_for_omitted_condop (token->location, logical_or_expr);
6741 }
6742 else
6743 {
6744 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6745 parser->colon_corrects_to_scope_p = false;
6746 /* Parse the expression. */
6747 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6748 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6749 c_inhibit_evaluation_warnings +=
6750 ((logical_or_expr == truthvalue_true_node)
6751 - (logical_or_expr == truthvalue_false_node));
6752 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6753 }
6754
6755 /* The next token should be a `:'. */
6756 cp_parser_require (parser, CPP_COLON, RT_COLON);
6757 /* Parse the assignment-expression. */
6758 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6759 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6760
6761 /* Build the conditional-expression. */
6762 return build_x_conditional_expr (logical_or_expr,
6763 expr,
6764 assignment_expr,
6765 tf_warning_or_error);
6766 }
6767
6768 /* Parse an assignment-expression.
6769
6770 assignment-expression:
6771 conditional-expression
6772 logical-or-expression assignment-operator assignment_expression
6773 throw-expression
6774
6775 CAST_P is true if this expression is the target of a cast.
6776
6777 Returns a representation for the expression. */
6778
6779 static tree
6780 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6781 cp_id_kind * pidk)
6782 {
6783 tree expr;
6784
6785 /* If the next token is the `throw' keyword, then we're looking at
6786 a throw-expression. */
6787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6788 expr = cp_parser_throw_expression (parser);
6789 /* Otherwise, it must be that we are looking at a
6790 logical-or-expression. */
6791 else
6792 {
6793 /* Parse the binary expressions (logical-or-expression). */
6794 expr = cp_parser_binary_expression (parser, cast_p, false,
6795 PREC_NOT_OPERATOR, pidk);
6796 /* If the next token is a `?' then we're actually looking at a
6797 conditional-expression. */
6798 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6799 return cp_parser_question_colon_clause (parser, expr);
6800 else
6801 {
6802 enum tree_code assignment_operator;
6803
6804 /* If it's an assignment-operator, we're using the second
6805 production. */
6806 assignment_operator
6807 = cp_parser_assignment_operator_opt (parser);
6808 if (assignment_operator != ERROR_MARK)
6809 {
6810 bool non_constant_p;
6811
6812 /* Parse the right-hand side of the assignment. */
6813 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6814
6815 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6816 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6817
6818 /* An assignment may not appear in a
6819 constant-expression. */
6820 if (cp_parser_non_integral_constant_expression (parser,
6821 NIC_ASSIGNMENT))
6822 return error_mark_node;
6823 /* Build the assignment expression. */
6824 expr = build_x_modify_expr (expr,
6825 assignment_operator,
6826 rhs,
6827 tf_warning_or_error);
6828 }
6829 }
6830 }
6831
6832 return expr;
6833 }
6834
6835 /* Parse an (optional) assignment-operator.
6836
6837 assignment-operator: one of
6838 = *= /= %= += -= >>= <<= &= ^= |=
6839
6840 GNU Extension:
6841
6842 assignment-operator: one of
6843 <?= >?=
6844
6845 If the next token is an assignment operator, the corresponding tree
6846 code is returned, and the token is consumed. For example, for
6847 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6848 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6849 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6850 operator, ERROR_MARK is returned. */
6851
6852 static enum tree_code
6853 cp_parser_assignment_operator_opt (cp_parser* parser)
6854 {
6855 enum tree_code op;
6856 cp_token *token;
6857
6858 /* Peek at the next token. */
6859 token = cp_lexer_peek_token (parser->lexer);
6860
6861 switch (token->type)
6862 {
6863 case CPP_EQ:
6864 op = NOP_EXPR;
6865 break;
6866
6867 case CPP_MULT_EQ:
6868 op = MULT_EXPR;
6869 break;
6870
6871 case CPP_DIV_EQ:
6872 op = TRUNC_DIV_EXPR;
6873 break;
6874
6875 case CPP_MOD_EQ:
6876 op = TRUNC_MOD_EXPR;
6877 break;
6878
6879 case CPP_PLUS_EQ:
6880 op = PLUS_EXPR;
6881 break;
6882
6883 case CPP_MINUS_EQ:
6884 op = MINUS_EXPR;
6885 break;
6886
6887 case CPP_RSHIFT_EQ:
6888 op = RSHIFT_EXPR;
6889 break;
6890
6891 case CPP_LSHIFT_EQ:
6892 op = LSHIFT_EXPR;
6893 break;
6894
6895 case CPP_AND_EQ:
6896 op = BIT_AND_EXPR;
6897 break;
6898
6899 case CPP_XOR_EQ:
6900 op = BIT_XOR_EXPR;
6901 break;
6902
6903 case CPP_OR_EQ:
6904 op = BIT_IOR_EXPR;
6905 break;
6906
6907 default:
6908 /* Nothing else is an assignment operator. */
6909 op = ERROR_MARK;
6910 }
6911
6912 /* If it was an assignment operator, consume it. */
6913 if (op != ERROR_MARK)
6914 cp_lexer_consume_token (parser->lexer);
6915
6916 return op;
6917 }
6918
6919 /* Parse an expression.
6920
6921 expression:
6922 assignment-expression
6923 expression , assignment-expression
6924
6925 CAST_P is true if this expression is the target of a cast.
6926
6927 Returns a representation of the expression. */
6928
6929 static tree
6930 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6931 {
6932 tree expression = NULL_TREE;
6933
6934 while (true)
6935 {
6936 tree assignment_expression;
6937
6938 /* Parse the next assignment-expression. */
6939 assignment_expression
6940 = cp_parser_assignment_expression (parser, cast_p, pidk);
6941 /* If this is the first assignment-expression, we can just
6942 save it away. */
6943 if (!expression)
6944 expression = assignment_expression;
6945 else
6946 expression = build_x_compound_expr (expression,
6947 assignment_expression,
6948 tf_warning_or_error);
6949 /* If the next token is not a comma, then we are done with the
6950 expression. */
6951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6952 break;
6953 /* Consume the `,'. */
6954 cp_lexer_consume_token (parser->lexer);
6955 /* A comma operator cannot appear in a constant-expression. */
6956 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6957 expression = error_mark_node;
6958 }
6959
6960 return expression;
6961 }
6962
6963 /* Parse a constant-expression.
6964
6965 constant-expression:
6966 conditional-expression
6967
6968 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6969 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6970 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6971 is false, NON_CONSTANT_P should be NULL. */
6972
6973 static tree
6974 cp_parser_constant_expression (cp_parser* parser,
6975 bool allow_non_constant_p,
6976 bool *non_constant_p)
6977 {
6978 bool saved_integral_constant_expression_p;
6979 bool saved_allow_non_integral_constant_expression_p;
6980 bool saved_non_integral_constant_expression_p;
6981 tree expression;
6982
6983 /* It might seem that we could simply parse the
6984 conditional-expression, and then check to see if it were
6985 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6986 one that the compiler can figure out is constant, possibly after
6987 doing some simplifications or optimizations. The standard has a
6988 precise definition of constant-expression, and we must honor
6989 that, even though it is somewhat more restrictive.
6990
6991 For example:
6992
6993 int i[(2, 3)];
6994
6995 is not a legal declaration, because `(2, 3)' is not a
6996 constant-expression. The `,' operator is forbidden in a
6997 constant-expression. However, GCC's constant-folding machinery
6998 will fold this operation to an INTEGER_CST for `3'. */
6999
7000 /* Save the old settings. */
7001 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7002 saved_allow_non_integral_constant_expression_p
7003 = parser->allow_non_integral_constant_expression_p;
7004 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7005 /* We are now parsing a constant-expression. */
7006 parser->integral_constant_expression_p = true;
7007 parser->allow_non_integral_constant_expression_p
7008 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7009 parser->non_integral_constant_expression_p = false;
7010 /* Although the grammar says "conditional-expression", we parse an
7011 "assignment-expression", which also permits "throw-expression"
7012 and the use of assignment operators. In the case that
7013 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7014 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7015 actually essential that we look for an assignment-expression.
7016 For example, cp_parser_initializer_clauses uses this function to
7017 determine whether a particular assignment-expression is in fact
7018 constant. */
7019 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7020 /* Restore the old settings. */
7021 parser->integral_constant_expression_p
7022 = saved_integral_constant_expression_p;
7023 parser->allow_non_integral_constant_expression_p
7024 = saved_allow_non_integral_constant_expression_p;
7025 if (cxx_dialect >= cxx0x)
7026 {
7027 /* Require an rvalue constant expression here; that's what our
7028 callers expect. Reference constant expressions are handled
7029 separately in e.g. cp_parser_template_argument. */
7030 bool is_const = potential_rvalue_constant_expression (expression);
7031 parser->non_integral_constant_expression_p = !is_const;
7032 if (!is_const && !allow_non_constant_p)
7033 require_potential_rvalue_constant_expression (expression);
7034 }
7035 if (allow_non_constant_p)
7036 *non_constant_p = parser->non_integral_constant_expression_p;
7037 else if (parser->non_integral_constant_expression_p)
7038 expression = error_mark_node;
7039 parser->non_integral_constant_expression_p
7040 = saved_non_integral_constant_expression_p;
7041
7042 return expression;
7043 }
7044
7045 /* Parse __builtin_offsetof.
7046
7047 offsetof-expression:
7048 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7049
7050 offsetof-member-designator:
7051 id-expression
7052 | offsetof-member-designator "." id-expression
7053 | offsetof-member-designator "[" expression "]"
7054 | offsetof-member-designator "->" id-expression */
7055
7056 static tree
7057 cp_parser_builtin_offsetof (cp_parser *parser)
7058 {
7059 int save_ice_p, save_non_ice_p;
7060 tree type, expr;
7061 cp_id_kind dummy;
7062 cp_token *token;
7063
7064 /* We're about to accept non-integral-constant things, but will
7065 definitely yield an integral constant expression. Save and
7066 restore these values around our local parsing. */
7067 save_ice_p = parser->integral_constant_expression_p;
7068 save_non_ice_p = parser->non_integral_constant_expression_p;
7069
7070 /* Consume the "__builtin_offsetof" token. */
7071 cp_lexer_consume_token (parser->lexer);
7072 /* Consume the opening `('. */
7073 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7074 /* Parse the type-id. */
7075 type = cp_parser_type_id (parser);
7076 /* Look for the `,'. */
7077 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7078 token = cp_lexer_peek_token (parser->lexer);
7079
7080 /* Build the (type *)null that begins the traditional offsetof macro. */
7081 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7082 tf_warning_or_error);
7083
7084 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7085 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7086 true, &dummy, token->location);
7087 while (true)
7088 {
7089 token = cp_lexer_peek_token (parser->lexer);
7090 switch (token->type)
7091 {
7092 case CPP_OPEN_SQUARE:
7093 /* offsetof-member-designator "[" expression "]" */
7094 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7095 break;
7096
7097 case CPP_DEREF:
7098 /* offsetof-member-designator "->" identifier */
7099 expr = grok_array_decl (expr, integer_zero_node);
7100 /* FALLTHRU */
7101
7102 case CPP_DOT:
7103 /* offsetof-member-designator "." identifier */
7104 cp_lexer_consume_token (parser->lexer);
7105 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7106 expr, true, &dummy,
7107 token->location);
7108 break;
7109
7110 case CPP_CLOSE_PAREN:
7111 /* Consume the ")" token. */
7112 cp_lexer_consume_token (parser->lexer);
7113 goto success;
7114
7115 default:
7116 /* Error. We know the following require will fail, but
7117 that gives the proper error message. */
7118 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7119 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7120 expr = error_mark_node;
7121 goto failure;
7122 }
7123 }
7124
7125 success:
7126 /* If we're processing a template, we can't finish the semantics yet.
7127 Otherwise we can fold the entire expression now. */
7128 if (processing_template_decl)
7129 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7130 else
7131 expr = finish_offsetof (expr);
7132
7133 failure:
7134 parser->integral_constant_expression_p = save_ice_p;
7135 parser->non_integral_constant_expression_p = save_non_ice_p;
7136
7137 return expr;
7138 }
7139
7140 /* Parse a trait expression.
7141
7142 Returns a representation of the expression, the underlying type
7143 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7144
7145 static tree
7146 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7147 {
7148 cp_trait_kind kind;
7149 tree type1, type2 = NULL_TREE;
7150 bool binary = false;
7151 cp_decl_specifier_seq decl_specs;
7152
7153 switch (keyword)
7154 {
7155 case RID_HAS_NOTHROW_ASSIGN:
7156 kind = CPTK_HAS_NOTHROW_ASSIGN;
7157 break;
7158 case RID_HAS_NOTHROW_CONSTRUCTOR:
7159 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7160 break;
7161 case RID_HAS_NOTHROW_COPY:
7162 kind = CPTK_HAS_NOTHROW_COPY;
7163 break;
7164 case RID_HAS_TRIVIAL_ASSIGN:
7165 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7166 break;
7167 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7168 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7169 break;
7170 case RID_HAS_TRIVIAL_COPY:
7171 kind = CPTK_HAS_TRIVIAL_COPY;
7172 break;
7173 case RID_HAS_TRIVIAL_DESTRUCTOR:
7174 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7175 break;
7176 case RID_HAS_VIRTUAL_DESTRUCTOR:
7177 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7178 break;
7179 case RID_IS_ABSTRACT:
7180 kind = CPTK_IS_ABSTRACT;
7181 break;
7182 case RID_IS_BASE_OF:
7183 kind = CPTK_IS_BASE_OF;
7184 binary = true;
7185 break;
7186 case RID_IS_CLASS:
7187 kind = CPTK_IS_CLASS;
7188 break;
7189 case RID_IS_CONVERTIBLE_TO:
7190 kind = CPTK_IS_CONVERTIBLE_TO;
7191 binary = true;
7192 break;
7193 case RID_IS_EMPTY:
7194 kind = CPTK_IS_EMPTY;
7195 break;
7196 case RID_IS_ENUM:
7197 kind = CPTK_IS_ENUM;
7198 break;
7199 case RID_IS_LITERAL_TYPE:
7200 kind = CPTK_IS_LITERAL_TYPE;
7201 break;
7202 case RID_IS_POD:
7203 kind = CPTK_IS_POD;
7204 break;
7205 case RID_IS_POLYMORPHIC:
7206 kind = CPTK_IS_POLYMORPHIC;
7207 break;
7208 case RID_IS_STD_LAYOUT:
7209 kind = CPTK_IS_STD_LAYOUT;
7210 break;
7211 case RID_IS_TRIVIAL:
7212 kind = CPTK_IS_TRIVIAL;
7213 break;
7214 case RID_IS_UNION:
7215 kind = CPTK_IS_UNION;
7216 break;
7217 case RID_UNDERLYING_TYPE:
7218 kind = CPTK_UNDERLYING_TYPE;
7219 break;
7220 default:
7221 gcc_unreachable ();
7222 }
7223
7224 /* Consume the token. */
7225 cp_lexer_consume_token (parser->lexer);
7226
7227 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7228
7229 type1 = cp_parser_type_id (parser);
7230
7231 if (type1 == error_mark_node)
7232 return error_mark_node;
7233
7234 /* Build a trivial decl-specifier-seq. */
7235 clear_decl_specs (&decl_specs);
7236 decl_specs.type = type1;
7237
7238 /* Call grokdeclarator to figure out what type this is. */
7239 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7240 /*initialized=*/0, /*attrlist=*/NULL);
7241
7242 if (binary)
7243 {
7244 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7245
7246 type2 = cp_parser_type_id (parser);
7247
7248 if (type2 == error_mark_node)
7249 return error_mark_node;
7250
7251 /* Build a trivial decl-specifier-seq. */
7252 clear_decl_specs (&decl_specs);
7253 decl_specs.type = type2;
7254
7255 /* Call grokdeclarator to figure out what type this is. */
7256 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7257 /*initialized=*/0, /*attrlist=*/NULL);
7258 }
7259
7260 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7261
7262 /* Complete the trait expression, which may mean either processing
7263 the trait expr now or saving it for template instantiation. */
7264 return kind != CPTK_UNDERLYING_TYPE
7265 ? finish_trait_expr (kind, type1, type2)
7266 : finish_underlying_type (type1);
7267 }
7268
7269 /* Lambdas that appear in variable initializer or default argument scope
7270 get that in their mangling, so we need to record it. We might as well
7271 use the count for function and namespace scopes as well. */
7272 static GTY(()) tree lambda_scope;
7273 static GTY(()) int lambda_count;
7274 typedef struct GTY(()) tree_int
7275 {
7276 tree t;
7277 int i;
7278 } tree_int;
7279 DEF_VEC_O(tree_int);
7280 DEF_VEC_ALLOC_O(tree_int,gc);
7281 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7282
7283 static void
7284 start_lambda_scope (tree decl)
7285 {
7286 tree_int ti;
7287 gcc_assert (decl);
7288 /* Once we're inside a function, we ignore other scopes and just push
7289 the function again so that popping works properly. */
7290 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7291 decl = current_function_decl;
7292 ti.t = lambda_scope;
7293 ti.i = lambda_count;
7294 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7295 if (lambda_scope != decl)
7296 {
7297 /* Don't reset the count if we're still in the same function. */
7298 lambda_scope = decl;
7299 lambda_count = 0;
7300 }
7301 }
7302
7303 static void
7304 record_lambda_scope (tree lambda)
7305 {
7306 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7307 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7308 }
7309
7310 static void
7311 finish_lambda_scope (void)
7312 {
7313 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7314 if (lambda_scope != p->t)
7315 {
7316 lambda_scope = p->t;
7317 lambda_count = p->i;
7318 }
7319 VEC_pop (tree_int, lambda_scope_stack);
7320 }
7321
7322 /* Parse a lambda expression.
7323
7324 lambda-expression:
7325 lambda-introducer lambda-declarator [opt] compound-statement
7326
7327 Returns a representation of the expression. */
7328
7329 static tree
7330 cp_parser_lambda_expression (cp_parser* parser)
7331 {
7332 tree lambda_expr = build_lambda_expr ();
7333 tree type;
7334
7335 LAMBDA_EXPR_LOCATION (lambda_expr)
7336 = cp_lexer_peek_token (parser->lexer)->location;
7337
7338 if (cp_unevaluated_operand)
7339 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7340 "lambda-expression in unevaluated context");
7341
7342 /* We may be in the middle of deferred access check. Disable
7343 it now. */
7344 push_deferring_access_checks (dk_no_deferred);
7345
7346 cp_parser_lambda_introducer (parser, lambda_expr);
7347
7348 type = begin_lambda_type (lambda_expr);
7349
7350 record_lambda_scope (lambda_expr);
7351
7352 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7353 determine_visibility (TYPE_NAME (type));
7354
7355 /* Now that we've started the type, add the capture fields for any
7356 explicit captures. */
7357 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7358
7359 {
7360 /* Inside the class, surrounding template-parameter-lists do not apply. */
7361 unsigned int saved_num_template_parameter_lists
7362 = parser->num_template_parameter_lists;
7363
7364 parser->num_template_parameter_lists = 0;
7365
7366 /* By virtue of defining a local class, a lambda expression has access to
7367 the private variables of enclosing classes. */
7368
7369 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7370
7371 cp_parser_lambda_body (parser, lambda_expr);
7372
7373 /* The capture list was built up in reverse order; fix that now. */
7374 {
7375 tree newlist = NULL_TREE;
7376 tree elt, next;
7377
7378 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7379 elt; elt = next)
7380 {
7381 tree field = TREE_PURPOSE (elt);
7382 char *buf;
7383
7384 next = TREE_CHAIN (elt);
7385 TREE_CHAIN (elt) = newlist;
7386 newlist = elt;
7387
7388 /* Also add __ to the beginning of the field name so that code
7389 outside the lambda body can't see the captured name. We could
7390 just remove the name entirely, but this is more useful for
7391 debugging. */
7392 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7393 /* The 'this' capture already starts with __. */
7394 continue;
7395
7396 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7397 buf[1] = buf[0] = '_';
7398 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7399 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7400 DECL_NAME (field) = get_identifier (buf);
7401 }
7402 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7403 }
7404
7405 maybe_add_lambda_conv_op (type);
7406
7407 type = finish_struct (type, /*attributes=*/NULL_TREE);
7408
7409 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7410 }
7411
7412 pop_deferring_access_checks ();
7413
7414 return build_lambda_object (lambda_expr);
7415 }
7416
7417 /* Parse the beginning of a lambda expression.
7418
7419 lambda-introducer:
7420 [ lambda-capture [opt] ]
7421
7422 LAMBDA_EXPR is the current representation of the lambda expression. */
7423
7424 static void
7425 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7426 {
7427 /* Need commas after the first capture. */
7428 bool first = true;
7429
7430 /* Eat the leading `['. */
7431 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7432
7433 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7434 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7435 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7436 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7437 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7438 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7439
7440 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7441 {
7442 cp_lexer_consume_token (parser->lexer);
7443 first = false;
7444 }
7445
7446 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7447 {
7448 cp_token* capture_token;
7449 tree capture_id;
7450 tree capture_init_expr;
7451 cp_id_kind idk = CP_ID_KIND_NONE;
7452 bool explicit_init_p = false;
7453
7454 enum capture_kind_type
7455 {
7456 BY_COPY,
7457 BY_REFERENCE
7458 };
7459 enum capture_kind_type capture_kind = BY_COPY;
7460
7461 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7462 {
7463 error ("expected end of capture-list");
7464 return;
7465 }
7466
7467 if (first)
7468 first = false;
7469 else
7470 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7471
7472 /* Possibly capture `this'. */
7473 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7474 {
7475 cp_lexer_consume_token (parser->lexer);
7476 add_capture (lambda_expr,
7477 /*id=*/get_identifier ("__this"),
7478 /*initializer=*/finish_this_expr(),
7479 /*by_reference_p=*/false,
7480 explicit_init_p);
7481 continue;
7482 }
7483
7484 /* Remember whether we want to capture as a reference or not. */
7485 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7486 {
7487 capture_kind = BY_REFERENCE;
7488 cp_lexer_consume_token (parser->lexer);
7489 }
7490
7491 /* Get the identifier. */
7492 capture_token = cp_lexer_peek_token (parser->lexer);
7493 capture_id = cp_parser_identifier (parser);
7494
7495 if (capture_id == error_mark_node)
7496 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7497 delimiters, but I modified this to stop on unnested ']' as well. It
7498 was already changed to stop on unnested '}', so the
7499 "closing_parenthesis" name is no more misleading with my change. */
7500 {
7501 cp_parser_skip_to_closing_parenthesis (parser,
7502 /*recovering=*/true,
7503 /*or_comma=*/true,
7504 /*consume_paren=*/true);
7505 break;
7506 }
7507
7508 /* Find the initializer for this capture. */
7509 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7510 {
7511 /* An explicit expression exists. */
7512 cp_lexer_consume_token (parser->lexer);
7513 pedwarn (input_location, OPT_pedantic,
7514 "ISO C++ does not allow initializers "
7515 "in lambda expression capture lists");
7516 capture_init_expr = cp_parser_assignment_expression (parser,
7517 /*cast_p=*/true,
7518 &idk);
7519 explicit_init_p = true;
7520 }
7521 else
7522 {
7523 const char* error_msg;
7524
7525 /* Turn the identifier into an id-expression. */
7526 capture_init_expr
7527 = cp_parser_lookup_name
7528 (parser,
7529 capture_id,
7530 none_type,
7531 /*is_template=*/false,
7532 /*is_namespace=*/false,
7533 /*check_dependency=*/true,
7534 /*ambiguous_decls=*/NULL,
7535 capture_token->location);
7536
7537 capture_init_expr
7538 = finish_id_expression
7539 (capture_id,
7540 capture_init_expr,
7541 parser->scope,
7542 &idk,
7543 /*integral_constant_expression_p=*/false,
7544 /*allow_non_integral_constant_expression_p=*/false,
7545 /*non_integral_constant_expression_p=*/NULL,
7546 /*template_p=*/false,
7547 /*done=*/true,
7548 /*address_p=*/false,
7549 /*template_arg_p=*/false,
7550 &error_msg,
7551 capture_token->location);
7552 }
7553
7554 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7555 capture_init_expr
7556 = unqualified_name_lookup_error (capture_init_expr);
7557
7558 add_capture (lambda_expr,
7559 capture_id,
7560 capture_init_expr,
7561 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7562 explicit_init_p);
7563 }
7564
7565 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7566 }
7567
7568 /* Parse the (optional) middle of a lambda expression.
7569
7570 lambda-declarator:
7571 ( parameter-declaration-clause [opt] )
7572 attribute-specifier [opt]
7573 mutable [opt]
7574 exception-specification [opt]
7575 lambda-return-type-clause [opt]
7576
7577 LAMBDA_EXPR is the current representation of the lambda expression. */
7578
7579 static void
7580 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7581 {
7582 /* 5.1.1.4 of the standard says:
7583 If a lambda-expression does not include a lambda-declarator, it is as if
7584 the lambda-declarator were ().
7585 This means an empty parameter list, no attributes, and no exception
7586 specification. */
7587 tree param_list = void_list_node;
7588 tree attributes = NULL_TREE;
7589 tree exception_spec = NULL_TREE;
7590 tree t;
7591
7592 /* The lambda-declarator is optional, but must begin with an opening
7593 parenthesis if present. */
7594 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7595 {
7596 cp_lexer_consume_token (parser->lexer);
7597
7598 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7599
7600 /* Parse parameters. */
7601 param_list = cp_parser_parameter_declaration_clause (parser);
7602
7603 /* Default arguments shall not be specified in the
7604 parameter-declaration-clause of a lambda-declarator. */
7605 for (t = param_list; t; t = TREE_CHAIN (t))
7606 if (TREE_PURPOSE (t))
7607 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7608 "default argument specified for lambda parameter");
7609
7610 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7611
7612 attributes = cp_parser_attributes_opt (parser);
7613
7614 /* Parse optional `mutable' keyword. */
7615 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7616 {
7617 cp_lexer_consume_token (parser->lexer);
7618 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7619 }
7620
7621 /* Parse optional exception specification. */
7622 exception_spec = cp_parser_exception_specification_opt (parser);
7623
7624 /* Parse optional trailing return type. */
7625 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7626 {
7627 cp_lexer_consume_token (parser->lexer);
7628 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7629 }
7630
7631 /* The function parameters must be in scope all the way until after the
7632 trailing-return-type in case of decltype. */
7633 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7634 pop_binding (DECL_NAME (t), t);
7635
7636 leave_scope ();
7637 }
7638
7639 /* Create the function call operator.
7640
7641 Messing with declarators like this is no uglier than building up the
7642 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7643 other code. */
7644 {
7645 cp_decl_specifier_seq return_type_specs;
7646 cp_declarator* declarator;
7647 tree fco;
7648 int quals;
7649 void *p;
7650
7651 clear_decl_specs (&return_type_specs);
7652 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7653 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7654 else
7655 /* Maybe we will deduce the return type later, but we can use void
7656 as a placeholder return type anyways. */
7657 return_type_specs.type = void_type_node;
7658
7659 p = obstack_alloc (&declarator_obstack, 0);
7660
7661 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7662 sfk_none);
7663
7664 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7665 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7666 declarator = make_call_declarator (declarator, param_list, quals,
7667 exception_spec,
7668 /*late_return_type=*/NULL_TREE);
7669 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7670
7671 fco = grokmethod (&return_type_specs,
7672 declarator,
7673 attributes);
7674 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7675 DECL_ARTIFICIAL (fco) = 1;
7676
7677 finish_member_declaration (fco);
7678
7679 obstack_free (&declarator_obstack, p);
7680 }
7681 }
7682
7683 /* Parse the body of a lambda expression, which is simply
7684
7685 compound-statement
7686
7687 but which requires special handling.
7688 LAMBDA_EXPR is the current representation of the lambda expression. */
7689
7690 static void
7691 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7692 {
7693 bool nested = (current_function_decl != NULL_TREE);
7694 if (nested)
7695 push_function_context ();
7696
7697 /* Finish the function call operator
7698 - class_specifier
7699 + late_parsing_for_member
7700 + function_definition_after_declarator
7701 + ctor_initializer_opt_and_function_body */
7702 {
7703 tree fco = lambda_function (lambda_expr);
7704 tree body;
7705 bool done = false;
7706
7707 /* Let the front end know that we are going to be defining this
7708 function. */
7709 start_preparsed_function (fco,
7710 NULL_TREE,
7711 SF_PRE_PARSED | SF_INCLASS_INLINE);
7712
7713 start_lambda_scope (fco);
7714 body = begin_function_body ();
7715
7716 /* 5.1.1.4 of the standard says:
7717 If a lambda-expression does not include a trailing-return-type, it
7718 is as if the trailing-return-type denotes the following type:
7719 * if the compound-statement is of the form
7720 { return attribute-specifier [opt] expression ; }
7721 the type of the returned expression after lvalue-to-rvalue
7722 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7723 (_conv.array_ 4.2), and function-to-pointer conversion
7724 (_conv.func_ 4.3);
7725 * otherwise, void. */
7726
7727 /* In a lambda that has neither a lambda-return-type-clause
7728 nor a deducible form, errors should be reported for return statements
7729 in the body. Since we used void as the placeholder return type, parsing
7730 the body as usual will give such desired behavior. */
7731 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7732 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7733 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7734 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7735 {
7736 tree compound_stmt;
7737 tree expr = NULL_TREE;
7738 cp_id_kind idk = CP_ID_KIND_NONE;
7739
7740 /* Parse tentatively in case there's more after the initial return
7741 statement. */
7742 cp_parser_parse_tentatively (parser);
7743
7744 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7745 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7746
7747 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7748
7749 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7750 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7751
7752 if (cp_parser_parse_definitely (parser))
7753 {
7754 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7755
7756 compound_stmt = begin_compound_stmt (0);
7757 /* Will get error here if type not deduced yet. */
7758 finish_return_stmt (expr);
7759 finish_compound_stmt (compound_stmt);
7760
7761 done = true;
7762 }
7763 }
7764
7765 if (!done)
7766 {
7767 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7768 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7769 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7770 cp_parser_compound_stmt does not pass it. */
7771 cp_parser_function_body (parser);
7772 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7773 }
7774
7775 finish_function_body (body);
7776 finish_lambda_scope ();
7777
7778 /* Finish the function and generate code for it if necessary. */
7779 expand_or_defer_fn (finish_function (/*inline*/2));
7780 }
7781
7782 if (nested)
7783 pop_function_context();
7784 }
7785
7786 /* Statements [gram.stmt.stmt] */
7787
7788 /* Parse a statement.
7789
7790 statement:
7791 labeled-statement
7792 expression-statement
7793 compound-statement
7794 selection-statement
7795 iteration-statement
7796 jump-statement
7797 declaration-statement
7798 try-block
7799
7800 IN_COMPOUND is true when the statement is nested inside a
7801 cp_parser_compound_statement; this matters for certain pragmas.
7802
7803 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7804 is a (possibly labeled) if statement which is not enclosed in braces
7805 and has an else clause. This is used to implement -Wparentheses. */
7806
7807 static void
7808 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7809 bool in_compound, bool *if_p)
7810 {
7811 tree statement;
7812 cp_token *token;
7813 location_t statement_location;
7814
7815 restart:
7816 if (if_p != NULL)
7817 *if_p = false;
7818 /* There is no statement yet. */
7819 statement = NULL_TREE;
7820 /* Peek at the next token. */
7821 token = cp_lexer_peek_token (parser->lexer);
7822 /* Remember the location of the first token in the statement. */
7823 statement_location = token->location;
7824 /* If this is a keyword, then that will often determine what kind of
7825 statement we have. */
7826 if (token->type == CPP_KEYWORD)
7827 {
7828 enum rid keyword = token->keyword;
7829
7830 switch (keyword)
7831 {
7832 case RID_CASE:
7833 case RID_DEFAULT:
7834 /* Looks like a labeled-statement with a case label.
7835 Parse the label, and then use tail recursion to parse
7836 the statement. */
7837 cp_parser_label_for_labeled_statement (parser);
7838 goto restart;
7839
7840 case RID_IF:
7841 case RID_SWITCH:
7842 statement = cp_parser_selection_statement (parser, if_p);
7843 break;
7844
7845 case RID_WHILE:
7846 case RID_DO:
7847 case RID_FOR:
7848 statement = cp_parser_iteration_statement (parser);
7849 break;
7850
7851 case RID_BREAK:
7852 case RID_CONTINUE:
7853 case RID_RETURN:
7854 case RID_GOTO:
7855 statement = cp_parser_jump_statement (parser);
7856 break;
7857
7858 /* Objective-C++ exception-handling constructs. */
7859 case RID_AT_TRY:
7860 case RID_AT_CATCH:
7861 case RID_AT_FINALLY:
7862 case RID_AT_SYNCHRONIZED:
7863 case RID_AT_THROW:
7864 statement = cp_parser_objc_statement (parser);
7865 break;
7866
7867 case RID_TRY:
7868 statement = cp_parser_try_block (parser);
7869 break;
7870
7871 case RID_NAMESPACE:
7872 /* This must be a namespace alias definition. */
7873 cp_parser_declaration_statement (parser);
7874 return;
7875
7876 default:
7877 /* It might be a keyword like `int' that can start a
7878 declaration-statement. */
7879 break;
7880 }
7881 }
7882 else if (token->type == CPP_NAME)
7883 {
7884 /* If the next token is a `:', then we are looking at a
7885 labeled-statement. */
7886 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7887 if (token->type == CPP_COLON)
7888 {
7889 /* Looks like a labeled-statement with an ordinary label.
7890 Parse the label, and then use tail recursion to parse
7891 the statement. */
7892 cp_parser_label_for_labeled_statement (parser);
7893 goto restart;
7894 }
7895 }
7896 /* Anything that starts with a `{' must be a compound-statement. */
7897 else if (token->type == CPP_OPEN_BRACE)
7898 statement = cp_parser_compound_statement (parser, NULL, false, false);
7899 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7900 a statement all its own. */
7901 else if (token->type == CPP_PRAGMA)
7902 {
7903 /* Only certain OpenMP pragmas are attached to statements, and thus
7904 are considered statements themselves. All others are not. In
7905 the context of a compound, accept the pragma as a "statement" and
7906 return so that we can check for a close brace. Otherwise we
7907 require a real statement and must go back and read one. */
7908 if (in_compound)
7909 cp_parser_pragma (parser, pragma_compound);
7910 else if (!cp_parser_pragma (parser, pragma_stmt))
7911 goto restart;
7912 return;
7913 }
7914 else if (token->type == CPP_EOF)
7915 {
7916 cp_parser_error (parser, "expected statement");
7917 return;
7918 }
7919
7920 /* Everything else must be a declaration-statement or an
7921 expression-statement. Try for the declaration-statement
7922 first, unless we are looking at a `;', in which case we know that
7923 we have an expression-statement. */
7924 if (!statement)
7925 {
7926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7927 {
7928 cp_parser_parse_tentatively (parser);
7929 /* Try to parse the declaration-statement. */
7930 cp_parser_declaration_statement (parser);
7931 /* If that worked, we're done. */
7932 if (cp_parser_parse_definitely (parser))
7933 return;
7934 }
7935 /* Look for an expression-statement instead. */
7936 statement = cp_parser_expression_statement (parser, in_statement_expr);
7937 }
7938
7939 /* Set the line number for the statement. */
7940 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7941 SET_EXPR_LOCATION (statement, statement_location);
7942 }
7943
7944 /* Parse the label for a labeled-statement, i.e.
7945
7946 identifier :
7947 case constant-expression :
7948 default :
7949
7950 GNU Extension:
7951 case constant-expression ... constant-expression : statement
7952
7953 When a label is parsed without errors, the label is added to the
7954 parse tree by the finish_* functions, so this function doesn't
7955 have to return the label. */
7956
7957 static void
7958 cp_parser_label_for_labeled_statement (cp_parser* parser)
7959 {
7960 cp_token *token;
7961 tree label = NULL_TREE;
7962 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7963
7964 /* The next token should be an identifier. */
7965 token = cp_lexer_peek_token (parser->lexer);
7966 if (token->type != CPP_NAME
7967 && token->type != CPP_KEYWORD)
7968 {
7969 cp_parser_error (parser, "expected labeled-statement");
7970 return;
7971 }
7972
7973 parser->colon_corrects_to_scope_p = false;
7974 switch (token->keyword)
7975 {
7976 case RID_CASE:
7977 {
7978 tree expr, expr_hi;
7979 cp_token *ellipsis;
7980
7981 /* Consume the `case' token. */
7982 cp_lexer_consume_token (parser->lexer);
7983 /* Parse the constant-expression. */
7984 expr = cp_parser_constant_expression (parser,
7985 /*allow_non_constant_p=*/false,
7986 NULL);
7987
7988 ellipsis = cp_lexer_peek_token (parser->lexer);
7989 if (ellipsis->type == CPP_ELLIPSIS)
7990 {
7991 /* Consume the `...' token. */
7992 cp_lexer_consume_token (parser->lexer);
7993 expr_hi =
7994 cp_parser_constant_expression (parser,
7995 /*allow_non_constant_p=*/false,
7996 NULL);
7997 /* We don't need to emit warnings here, as the common code
7998 will do this for us. */
7999 }
8000 else
8001 expr_hi = NULL_TREE;
8002
8003 if (parser->in_switch_statement_p)
8004 finish_case_label (token->location, expr, expr_hi);
8005 else
8006 error_at (token->location,
8007 "case label %qE not within a switch statement",
8008 expr);
8009 }
8010 break;
8011
8012 case RID_DEFAULT:
8013 /* Consume the `default' token. */
8014 cp_lexer_consume_token (parser->lexer);
8015
8016 if (parser->in_switch_statement_p)
8017 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8018 else
8019 error_at (token->location, "case label not within a switch statement");
8020 break;
8021
8022 default:
8023 /* Anything else must be an ordinary label. */
8024 label = finish_label_stmt (cp_parser_identifier (parser));
8025 break;
8026 }
8027
8028 /* Require the `:' token. */
8029 cp_parser_require (parser, CPP_COLON, RT_COLON);
8030
8031 /* An ordinary label may optionally be followed by attributes.
8032 However, this is only permitted if the attributes are then
8033 followed by a semicolon. This is because, for backward
8034 compatibility, when parsing
8035 lab: __attribute__ ((unused)) int i;
8036 we want the attribute to attach to "i", not "lab". */
8037 if (label != NULL_TREE
8038 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8039 {
8040 tree attrs;
8041
8042 cp_parser_parse_tentatively (parser);
8043 attrs = cp_parser_attributes_opt (parser);
8044 if (attrs == NULL_TREE
8045 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8046 cp_parser_abort_tentative_parse (parser);
8047 else if (!cp_parser_parse_definitely (parser))
8048 ;
8049 else
8050 cplus_decl_attributes (&label, attrs, 0);
8051 }
8052
8053 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8054 }
8055
8056 /* Parse an expression-statement.
8057
8058 expression-statement:
8059 expression [opt] ;
8060
8061 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8062 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8063 indicates whether this expression-statement is part of an
8064 expression statement. */
8065
8066 static tree
8067 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8068 {
8069 tree statement = NULL_TREE;
8070 cp_token *token = cp_lexer_peek_token (parser->lexer);
8071
8072 /* If the next token is a ';', then there is no expression
8073 statement. */
8074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8075 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8076
8077 /* Give a helpful message for "A<T>::type t;" and the like. */
8078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8079 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8080 {
8081 if (TREE_CODE (statement) == SCOPE_REF)
8082 error_at (token->location, "need %<typename%> before %qE because "
8083 "%qT is a dependent scope",
8084 statement, TREE_OPERAND (statement, 0));
8085 else if (is_overloaded_fn (statement)
8086 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8087 {
8088 /* A::A a; */
8089 tree fn = get_first_fn (statement);
8090 error_at (token->location,
8091 "%<%T::%D%> names the constructor, not the type",
8092 DECL_CONTEXT (fn), DECL_NAME (fn));
8093 }
8094 }
8095
8096 /* Consume the final `;'. */
8097 cp_parser_consume_semicolon_at_end_of_statement (parser);
8098
8099 if (in_statement_expr
8100 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8101 /* This is the final expression statement of a statement
8102 expression. */
8103 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8104 else if (statement)
8105 statement = finish_expr_stmt (statement);
8106 else
8107 finish_stmt ();
8108
8109 return statement;
8110 }
8111
8112 /* Parse a compound-statement.
8113
8114 compound-statement:
8115 { statement-seq [opt] }
8116
8117 GNU extension:
8118
8119 compound-statement:
8120 { label-declaration-seq [opt] statement-seq [opt] }
8121
8122 label-declaration-seq:
8123 label-declaration
8124 label-declaration-seq label-declaration
8125
8126 Returns a tree representing the statement. */
8127
8128 static tree
8129 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8130 bool in_try, bool function_body)
8131 {
8132 tree compound_stmt;
8133
8134 /* Consume the `{'. */
8135 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8136 return error_mark_node;
8137 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8138 && !function_body)
8139 pedwarn (input_location, OPT_pedantic,
8140 "compound-statement in constexpr function");
8141 /* Begin the compound-statement. */
8142 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8143 /* If the next keyword is `__label__' we have a label declaration. */
8144 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8145 cp_parser_label_declaration (parser);
8146 /* Parse an (optional) statement-seq. */
8147 cp_parser_statement_seq_opt (parser, in_statement_expr);
8148 /* Finish the compound-statement. */
8149 finish_compound_stmt (compound_stmt);
8150 /* Consume the `}'. */
8151 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8152
8153 return compound_stmt;
8154 }
8155
8156 /* Parse an (optional) statement-seq.
8157
8158 statement-seq:
8159 statement
8160 statement-seq [opt] statement */
8161
8162 static void
8163 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8164 {
8165 /* Scan statements until there aren't any more. */
8166 while (true)
8167 {
8168 cp_token *token = cp_lexer_peek_token (parser->lexer);
8169
8170 /* If we are looking at a `}', then we have run out of
8171 statements; the same is true if we have reached the end
8172 of file, or have stumbled upon a stray '@end'. */
8173 if (token->type == CPP_CLOSE_BRACE
8174 || token->type == CPP_EOF
8175 || token->type == CPP_PRAGMA_EOL
8176 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8177 break;
8178
8179 /* If we are in a compound statement and find 'else' then
8180 something went wrong. */
8181 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8182 {
8183 if (parser->in_statement & IN_IF_STMT)
8184 break;
8185 else
8186 {
8187 token = cp_lexer_consume_token (parser->lexer);
8188 error_at (token->location, "%<else%> without a previous %<if%>");
8189 }
8190 }
8191
8192 /* Parse the statement. */
8193 cp_parser_statement (parser, in_statement_expr, true, NULL);
8194 }
8195 }
8196
8197 /* Parse a selection-statement.
8198
8199 selection-statement:
8200 if ( condition ) statement
8201 if ( condition ) statement else statement
8202 switch ( condition ) statement
8203
8204 Returns the new IF_STMT or SWITCH_STMT.
8205
8206 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8207 is a (possibly labeled) if statement which is not enclosed in
8208 braces and has an else clause. This is used to implement
8209 -Wparentheses. */
8210
8211 static tree
8212 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8213 {
8214 cp_token *token;
8215 enum rid keyword;
8216
8217 if (if_p != NULL)
8218 *if_p = false;
8219
8220 /* Peek at the next token. */
8221 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8222
8223 /* See what kind of keyword it is. */
8224 keyword = token->keyword;
8225 switch (keyword)
8226 {
8227 case RID_IF:
8228 case RID_SWITCH:
8229 {
8230 tree statement;
8231 tree condition;
8232
8233 /* Look for the `('. */
8234 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8235 {
8236 cp_parser_skip_to_end_of_statement (parser);
8237 return error_mark_node;
8238 }
8239
8240 /* Begin the selection-statement. */
8241 if (keyword == RID_IF)
8242 statement = begin_if_stmt ();
8243 else
8244 statement = begin_switch_stmt ();
8245
8246 /* Parse the condition. */
8247 condition = cp_parser_condition (parser);
8248 /* Look for the `)'. */
8249 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8250 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8251 /*consume_paren=*/true);
8252
8253 if (keyword == RID_IF)
8254 {
8255 bool nested_if;
8256 unsigned char in_statement;
8257
8258 /* Add the condition. */
8259 finish_if_stmt_cond (condition, statement);
8260
8261 /* Parse the then-clause. */
8262 in_statement = parser->in_statement;
8263 parser->in_statement |= IN_IF_STMT;
8264 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8265 {
8266 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8267 add_stmt (build_empty_stmt (loc));
8268 cp_lexer_consume_token (parser->lexer);
8269 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8270 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8271 "empty body in an %<if%> statement");
8272 nested_if = false;
8273 }
8274 else
8275 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8276 parser->in_statement = in_statement;
8277
8278 finish_then_clause (statement);
8279
8280 /* If the next token is `else', parse the else-clause. */
8281 if (cp_lexer_next_token_is_keyword (parser->lexer,
8282 RID_ELSE))
8283 {
8284 /* Consume the `else' keyword. */
8285 cp_lexer_consume_token (parser->lexer);
8286 begin_else_clause (statement);
8287 /* Parse the else-clause. */
8288 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8289 {
8290 location_t loc;
8291 loc = cp_lexer_peek_token (parser->lexer)->location;
8292 warning_at (loc,
8293 OPT_Wempty_body, "suggest braces around "
8294 "empty body in an %<else%> statement");
8295 add_stmt (build_empty_stmt (loc));
8296 cp_lexer_consume_token (parser->lexer);
8297 }
8298 else
8299 cp_parser_implicitly_scoped_statement (parser, NULL);
8300
8301 finish_else_clause (statement);
8302
8303 /* If we are currently parsing a then-clause, then
8304 IF_P will not be NULL. We set it to true to
8305 indicate that this if statement has an else clause.
8306 This may trigger the Wparentheses warning below
8307 when we get back up to the parent if statement. */
8308 if (if_p != NULL)
8309 *if_p = true;
8310 }
8311 else
8312 {
8313 /* This if statement does not have an else clause. If
8314 NESTED_IF is true, then the then-clause is an if
8315 statement which does have an else clause. We warn
8316 about the potential ambiguity. */
8317 if (nested_if)
8318 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8319 "suggest explicit braces to avoid ambiguous"
8320 " %<else%>");
8321 }
8322
8323 /* Now we're all done with the if-statement. */
8324 finish_if_stmt (statement);
8325 }
8326 else
8327 {
8328 bool in_switch_statement_p;
8329 unsigned char in_statement;
8330
8331 /* Add the condition. */
8332 finish_switch_cond (condition, statement);
8333
8334 /* Parse the body of the switch-statement. */
8335 in_switch_statement_p = parser->in_switch_statement_p;
8336 in_statement = parser->in_statement;
8337 parser->in_switch_statement_p = true;
8338 parser->in_statement |= IN_SWITCH_STMT;
8339 cp_parser_implicitly_scoped_statement (parser, NULL);
8340 parser->in_switch_statement_p = in_switch_statement_p;
8341 parser->in_statement = in_statement;
8342
8343 /* Now we're all done with the switch-statement. */
8344 finish_switch_stmt (statement);
8345 }
8346
8347 return statement;
8348 }
8349 break;
8350
8351 default:
8352 cp_parser_error (parser, "expected selection-statement");
8353 return error_mark_node;
8354 }
8355 }
8356
8357 /* Parse a condition.
8358
8359 condition:
8360 expression
8361 type-specifier-seq declarator = initializer-clause
8362 type-specifier-seq declarator braced-init-list
8363
8364 GNU Extension:
8365
8366 condition:
8367 type-specifier-seq declarator asm-specification [opt]
8368 attributes [opt] = assignment-expression
8369
8370 Returns the expression that should be tested. */
8371
8372 static tree
8373 cp_parser_condition (cp_parser* parser)
8374 {
8375 cp_decl_specifier_seq type_specifiers;
8376 const char *saved_message;
8377 int declares_class_or_enum;
8378
8379 /* Try the declaration first. */
8380 cp_parser_parse_tentatively (parser);
8381 /* New types are not allowed in the type-specifier-seq for a
8382 condition. */
8383 saved_message = parser->type_definition_forbidden_message;
8384 parser->type_definition_forbidden_message
8385 = G_("types may not be defined in conditions");
8386 /* Parse the type-specifier-seq. */
8387 cp_parser_decl_specifier_seq (parser,
8388 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8389 &type_specifiers,
8390 &declares_class_or_enum);
8391 /* Restore the saved message. */
8392 parser->type_definition_forbidden_message = saved_message;
8393 /* If all is well, we might be looking at a declaration. */
8394 if (!cp_parser_error_occurred (parser))
8395 {
8396 tree decl;
8397 tree asm_specification;
8398 tree attributes;
8399 cp_declarator *declarator;
8400 tree initializer = NULL_TREE;
8401
8402 /* Parse the declarator. */
8403 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8404 /*ctor_dtor_or_conv_p=*/NULL,
8405 /*parenthesized_p=*/NULL,
8406 /*member_p=*/false);
8407 /* Parse the attributes. */
8408 attributes = cp_parser_attributes_opt (parser);
8409 /* Parse the asm-specification. */
8410 asm_specification = cp_parser_asm_specification_opt (parser);
8411 /* If the next token is not an `=' or '{', then we might still be
8412 looking at an expression. For example:
8413
8414 if (A(a).x)
8415
8416 looks like a decl-specifier-seq and a declarator -- but then
8417 there is no `=', so this is an expression. */
8418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8419 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8420 cp_parser_simulate_error (parser);
8421
8422 /* If we did see an `=' or '{', then we are looking at a declaration
8423 for sure. */
8424 if (cp_parser_parse_definitely (parser))
8425 {
8426 tree pushed_scope;
8427 bool non_constant_p;
8428 bool flags = LOOKUP_ONLYCONVERTING;
8429
8430 /* Create the declaration. */
8431 decl = start_decl (declarator, &type_specifiers,
8432 /*initialized_p=*/true,
8433 attributes, /*prefix_attributes=*/NULL_TREE,
8434 &pushed_scope);
8435
8436 /* Parse the initializer. */
8437 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8438 {
8439 initializer = cp_parser_braced_list (parser, &non_constant_p);
8440 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8441 flags = 0;
8442 }
8443 else
8444 {
8445 /* Consume the `='. */
8446 cp_parser_require (parser, CPP_EQ, RT_EQ);
8447 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8448 }
8449 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8451
8452 /* Process the initializer. */
8453 cp_finish_decl (decl,
8454 initializer, !non_constant_p,
8455 asm_specification,
8456 flags);
8457
8458 if (pushed_scope)
8459 pop_scope (pushed_scope);
8460
8461 return convert_from_reference (decl);
8462 }
8463 }
8464 /* If we didn't even get past the declarator successfully, we are
8465 definitely not looking at a declaration. */
8466 else
8467 cp_parser_abort_tentative_parse (parser);
8468
8469 /* Otherwise, we are looking at an expression. */
8470 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8471 }
8472
8473 /* Parses a for-statement or range-for-statement until the closing ')',
8474 not included. */
8475
8476 static tree
8477 cp_parser_for (cp_parser *parser)
8478 {
8479 tree init, scope, decl;
8480 bool is_range_for;
8481
8482 /* Begin the for-statement. */
8483 scope = begin_for_scope (&init);
8484
8485 /* Parse the initialization. */
8486 is_range_for = cp_parser_for_init_statement (parser, &decl);
8487
8488 if (is_range_for)
8489 return cp_parser_range_for (parser, scope, init, decl);
8490 else
8491 return cp_parser_c_for (parser, scope, init);
8492 }
8493
8494 static tree
8495 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8496 {
8497 /* Normal for loop */
8498 tree condition = NULL_TREE;
8499 tree expression = NULL_TREE;
8500 tree stmt;
8501
8502 stmt = begin_for_stmt (scope, init);
8503 /* The for-init-statement has already been parsed in
8504 cp_parser_for_init_statement, so no work is needed here. */
8505 finish_for_init_stmt (stmt);
8506
8507 /* If there's a condition, process it. */
8508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8509 condition = cp_parser_condition (parser);
8510 finish_for_cond (condition, stmt);
8511 /* Look for the `;'. */
8512 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8513
8514 /* If there's an expression, process it. */
8515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8516 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8517 finish_for_expr (expression, stmt);
8518
8519 return stmt;
8520 }
8521
8522 /* Tries to parse a range-based for-statement:
8523
8524 range-based-for:
8525 decl-specifier-seq declarator : expression
8526
8527 The decl-specifier-seq declarator and the `:' are already parsed by
8528 cp_parser_for_init_statement. If processing_template_decl it returns a
8529 newly created RANGE_FOR_STMT; if not, it is converted to a
8530 regular FOR_STMT. */
8531
8532 static tree
8533 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8534 {
8535 tree stmt, range_expr;
8536
8537 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8538 {
8539 bool expr_non_constant_p;
8540 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8541 }
8542 else
8543 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8544
8545 /* If in template, STMT is converted to a normal for-statement
8546 at instantiation. If not, it is done just ahead. */
8547 if (processing_template_decl)
8548 {
8549 stmt = begin_range_for_stmt (scope, init);
8550 finish_range_for_decl (stmt, range_decl, range_expr);
8551 }
8552 else
8553 {
8554 stmt = begin_for_stmt (scope, init);
8555 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8556 }
8557 return stmt;
8558 }
8559
8560 /* Converts a range-based for-statement into a normal
8561 for-statement, as per the definition.
8562
8563 for (RANGE_DECL : RANGE_EXPR)
8564 BLOCK
8565
8566 should be equivalent to:
8567
8568 {
8569 auto &&__range = RANGE_EXPR;
8570 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8571 __begin != __end;
8572 ++__begin)
8573 {
8574 RANGE_DECL = *__begin;
8575 BLOCK
8576 }
8577 }
8578
8579 If RANGE_EXPR is an array:
8580 BEGIN_EXPR = __range
8581 END_EXPR = __range + ARRAY_SIZE(__range)
8582 Else if RANGE_EXPR has a member 'begin' or 'end':
8583 BEGIN_EXPR = __range.begin()
8584 END_EXPR = __range.end()
8585 Else:
8586 BEGIN_EXPR = begin(__range)
8587 END_EXPR = end(__range);
8588
8589 If __range has a member 'begin' but not 'end', or vice versa, we must
8590 still use the second alternative (it will surely fail, however).
8591 When calling begin()/end() in the third alternative we must use
8592 argument dependent lookup, but always considering 'std' as an associated
8593 namespace. */
8594
8595 tree
8596 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8597 {
8598 tree range_type, range_temp;
8599 tree begin, end;
8600 tree iter_type, begin_expr, end_expr;
8601 tree condition, expression;
8602
8603 if (range_decl == error_mark_node || range_expr == error_mark_node)
8604 /* If an error happened previously do nothing or else a lot of
8605 unhelpful errors would be issued. */
8606 begin_expr = end_expr = iter_type = error_mark_node;
8607 else
8608 {
8609 /* Find out the type deduced by the declaration
8610 `auto &&__range = range_expr'. */
8611 range_type = cp_build_reference_type (make_auto (), true);
8612 range_type = do_auto_deduction (range_type, range_expr,
8613 type_uses_auto (range_type));
8614
8615 /* Create the __range variable. */
8616 range_temp = build_decl (input_location, VAR_DECL,
8617 get_identifier ("__for_range"), range_type);
8618 TREE_USED (range_temp) = 1;
8619 DECL_ARTIFICIAL (range_temp) = 1;
8620 pushdecl (range_temp);
8621 cp_finish_decl (range_temp, range_expr,
8622 /*is_constant_init*/false, NULL_TREE,
8623 LOOKUP_ONLYCONVERTING);
8624
8625 range_temp = convert_from_reference (range_temp);
8626 iter_type = cp_parser_perform_range_for_lookup (range_temp,
8627 &begin_expr, &end_expr);
8628 }
8629
8630 /* The new for initialization statement. */
8631 begin = build_decl (input_location, VAR_DECL,
8632 get_identifier ("__for_begin"), iter_type);
8633 TREE_USED (begin) = 1;
8634 DECL_ARTIFICIAL (begin) = 1;
8635 pushdecl (begin);
8636 cp_finish_decl (begin, begin_expr,
8637 /*is_constant_init*/false, NULL_TREE,
8638 LOOKUP_ONLYCONVERTING);
8639
8640 end = build_decl (input_location, VAR_DECL,
8641 get_identifier ("__for_end"), iter_type);
8642 TREE_USED (end) = 1;
8643 DECL_ARTIFICIAL (end) = 1;
8644 pushdecl (end);
8645 cp_finish_decl (end, end_expr,
8646 /*is_constant_init*/false, NULL_TREE,
8647 LOOKUP_ONLYCONVERTING);
8648
8649 finish_for_init_stmt (statement);
8650
8651 /* The new for condition. */
8652 condition = build_x_binary_op (NE_EXPR,
8653 begin, ERROR_MARK,
8654 end, ERROR_MARK,
8655 NULL, tf_warning_or_error);
8656 finish_for_cond (condition, statement);
8657
8658 /* The new increment expression. */
8659 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8660 finish_for_expr (expression, statement);
8661
8662 /* The declaration is initialized with *__begin inside the loop body. */
8663 cp_finish_decl (range_decl,
8664 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8665 /*is_constant_init*/false, NULL_TREE,
8666 LOOKUP_ONLYCONVERTING);
8667
8668 return statement;
8669 }
8670
8671 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8672 We need to solve both at the same time because the method used
8673 depends on the existence of members begin or end.
8674 Returns the type deduced for the iterator expression. */
8675
8676 static tree
8677 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8678 {
8679 if (!COMPLETE_TYPE_P (TREE_TYPE (range)))
8680 {
8681 error ("range-based %<for%> expression of type %qT "
8682 "has incomplete type", TREE_TYPE (range));
8683 *begin = *end = error_mark_node;
8684 return error_mark_node;
8685 }
8686 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8687 {
8688 /* If RANGE is an array, we will use pointer arithmetic. */
8689 *begin = range;
8690 *end = build_binary_op (input_location, PLUS_EXPR,
8691 range,
8692 array_type_nelts_top (TREE_TYPE (range)),
8693 0);
8694 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8695 }
8696 else
8697 {
8698 /* If it is not an array, we must do a bit of magic. */
8699 tree id_begin, id_end;
8700 tree member_begin, member_end;
8701
8702 *begin = *end = error_mark_node;
8703
8704 id_begin = get_identifier ("begin");
8705 id_end = get_identifier ("end");
8706 member_begin = lookup_member (TREE_TYPE (range), id_begin,
8707 /*protect=*/2, /*want_type=*/false);
8708 member_end = lookup_member (TREE_TYPE (range), id_end,
8709 /*protect=*/2, /*want_type=*/false);
8710
8711 if (member_begin != NULL_TREE || member_end != NULL_TREE)
8712 {
8713 /* Use the member functions. */
8714 if (member_begin != NULL_TREE)
8715 *begin = cp_parser_range_for_member_function (range, id_begin);
8716 else
8717 error ("range-based %<for%> expression of type %qT has an "
8718 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8719
8720 if (member_end != NULL_TREE)
8721 *end = cp_parser_range_for_member_function (range, id_end);
8722 else
8723 error ("range-based %<for%> expression of type %qT has a "
8724 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8725 }
8726 else
8727 {
8728 /* Use global functions with ADL. */
8729 VEC(tree,gc) *vec;
8730 vec = make_tree_vector ();
8731
8732 VEC_safe_push (tree, gc, vec, range);
8733
8734 member_begin = perform_koenig_lookup (id_begin, vec,
8735 /*include_std=*/true);
8736 *begin = finish_call_expr (member_begin, &vec, false, true,
8737 tf_warning_or_error);
8738 member_end = perform_koenig_lookup (id_end, vec,
8739 /*include_std=*/true);
8740 *end = finish_call_expr (member_end, &vec, false, true,
8741 tf_warning_or_error);
8742
8743 release_tree_vector (vec);
8744 }
8745
8746 /* Last common checks. */
8747 if (*begin == error_mark_node || *end == error_mark_node)
8748 {
8749 /* If one of the expressions is an error do no more checks. */
8750 *begin = *end = error_mark_node;
8751 return error_mark_node;
8752 }
8753 else
8754 {
8755 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8756 /* The unqualified type of the __begin and __end temporaries should
8757 be the same, as required by the multiple auto declaration. */
8758 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8759 error ("inconsistent begin/end types in range-based %<for%> "
8760 "statement: %qT and %qT",
8761 TREE_TYPE (*begin), TREE_TYPE (*end));
8762 return iter_type;
8763 }
8764 }
8765 }
8766
8767 /* Helper function for cp_parser_perform_range_for_lookup.
8768 Builds a tree for RANGE.IDENTIFIER(). */
8769
8770 static tree
8771 cp_parser_range_for_member_function (tree range, tree identifier)
8772 {
8773 tree member, res;
8774 VEC(tree,gc) *vec;
8775
8776 member = finish_class_member_access_expr (range, identifier,
8777 false, tf_warning_or_error);
8778 if (member == error_mark_node)
8779 return error_mark_node;
8780
8781 vec = make_tree_vector ();
8782 res = finish_call_expr (member, &vec,
8783 /*disallow_virtual=*/false,
8784 /*koenig_p=*/false,
8785 tf_warning_or_error);
8786 release_tree_vector (vec);
8787 return res;
8788 }
8789
8790 /* Parse an iteration-statement.
8791
8792 iteration-statement:
8793 while ( condition ) statement
8794 do statement while ( expression ) ;
8795 for ( for-init-statement condition [opt] ; expression [opt] )
8796 statement
8797
8798 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8799
8800 static tree
8801 cp_parser_iteration_statement (cp_parser* parser)
8802 {
8803 cp_token *token;
8804 enum rid keyword;
8805 tree statement;
8806 unsigned char in_statement;
8807
8808 /* Peek at the next token. */
8809 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8810 if (!token)
8811 return error_mark_node;
8812
8813 /* Remember whether or not we are already within an iteration
8814 statement. */
8815 in_statement = parser->in_statement;
8816
8817 /* See what kind of keyword it is. */
8818 keyword = token->keyword;
8819 switch (keyword)
8820 {
8821 case RID_WHILE:
8822 {
8823 tree condition;
8824
8825 /* Begin the while-statement. */
8826 statement = begin_while_stmt ();
8827 /* Look for the `('. */
8828 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8829 /* Parse the condition. */
8830 condition = cp_parser_condition (parser);
8831 finish_while_stmt_cond (condition, statement);
8832 /* Look for the `)'. */
8833 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8834 /* Parse the dependent statement. */
8835 parser->in_statement = IN_ITERATION_STMT;
8836 cp_parser_already_scoped_statement (parser);
8837 parser->in_statement = in_statement;
8838 /* We're done with the while-statement. */
8839 finish_while_stmt (statement);
8840 }
8841 break;
8842
8843 case RID_DO:
8844 {
8845 tree expression;
8846
8847 /* Begin the do-statement. */
8848 statement = begin_do_stmt ();
8849 /* Parse the body of the do-statement. */
8850 parser->in_statement = IN_ITERATION_STMT;
8851 cp_parser_implicitly_scoped_statement (parser, NULL);
8852 parser->in_statement = in_statement;
8853 finish_do_body (statement);
8854 /* Look for the `while' keyword. */
8855 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8856 /* Look for the `('. */
8857 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8858 /* Parse the expression. */
8859 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8860 /* We're done with the do-statement. */
8861 finish_do_stmt (expression, statement);
8862 /* Look for the `)'. */
8863 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8864 /* Look for the `;'. */
8865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8866 }
8867 break;
8868
8869 case RID_FOR:
8870 {
8871 /* Look for the `('. */
8872 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8873
8874 statement = cp_parser_for (parser);
8875
8876 /* Look for the `)'. */
8877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8878
8879 /* Parse the body of the for-statement. */
8880 parser->in_statement = IN_ITERATION_STMT;
8881 cp_parser_already_scoped_statement (parser);
8882 parser->in_statement = in_statement;
8883
8884 /* We're done with the for-statement. */
8885 finish_for_stmt (statement);
8886 }
8887 break;
8888
8889 default:
8890 cp_parser_error (parser, "expected iteration-statement");
8891 statement = error_mark_node;
8892 break;
8893 }
8894
8895 return statement;
8896 }
8897
8898 /* Parse a for-init-statement or the declarator of a range-based-for.
8899 Returns true if a range-based-for declaration is seen.
8900
8901 for-init-statement:
8902 expression-statement
8903 simple-declaration */
8904
8905 static bool
8906 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8907 {
8908 /* If the next token is a `;', then we have an empty
8909 expression-statement. Grammatically, this is also a
8910 simple-declaration, but an invalid one, because it does not
8911 declare anything. Therefore, if we did not handle this case
8912 specially, we would issue an error message about an invalid
8913 declaration. */
8914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8915 {
8916 bool is_range_for = false;
8917 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8918
8919 parser->colon_corrects_to_scope_p = false;
8920
8921 /* We're going to speculatively look for a declaration, falling back
8922 to an expression, if necessary. */
8923 cp_parser_parse_tentatively (parser);
8924 /* Parse the declaration. */
8925 cp_parser_simple_declaration (parser,
8926 /*function_definition_allowed_p=*/false,
8927 decl);
8928 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8929 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8930 {
8931 /* It is a range-for, consume the ':' */
8932 cp_lexer_consume_token (parser->lexer);
8933 is_range_for = true;
8934 if (cxx_dialect < cxx0x)
8935 {
8936 error_at (cp_lexer_peek_token (parser->lexer)->location,
8937 "range-based %<for%> loops are not allowed "
8938 "in C++98 mode");
8939 *decl = error_mark_node;
8940 }
8941 }
8942 else
8943 /* The ';' is not consumed yet because we told
8944 cp_parser_simple_declaration not to. */
8945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8946
8947 if (cp_parser_parse_definitely (parser))
8948 return is_range_for;
8949 /* If the tentative parse failed, then we shall need to look for an
8950 expression-statement. */
8951 }
8952 /* If we are here, it is an expression-statement. */
8953 cp_parser_expression_statement (parser, NULL_TREE);
8954 return false;
8955 }
8956
8957 /* Parse a jump-statement.
8958
8959 jump-statement:
8960 break ;
8961 continue ;
8962 return expression [opt] ;
8963 return braced-init-list ;
8964 goto identifier ;
8965
8966 GNU extension:
8967
8968 jump-statement:
8969 goto * expression ;
8970
8971 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8972
8973 static tree
8974 cp_parser_jump_statement (cp_parser* parser)
8975 {
8976 tree statement = error_mark_node;
8977 cp_token *token;
8978 enum rid keyword;
8979 unsigned char in_statement;
8980
8981 /* Peek at the next token. */
8982 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8983 if (!token)
8984 return error_mark_node;
8985
8986 /* See what kind of keyword it is. */
8987 keyword = token->keyword;
8988 switch (keyword)
8989 {
8990 case RID_BREAK:
8991 in_statement = parser->in_statement & ~IN_IF_STMT;
8992 switch (in_statement)
8993 {
8994 case 0:
8995 error_at (token->location, "break statement not within loop or switch");
8996 break;
8997 default:
8998 gcc_assert ((in_statement & IN_SWITCH_STMT)
8999 || in_statement == IN_ITERATION_STMT);
9000 statement = finish_break_stmt ();
9001 break;
9002 case IN_OMP_BLOCK:
9003 error_at (token->location, "invalid exit from OpenMP structured block");
9004 break;
9005 case IN_OMP_FOR:
9006 error_at (token->location, "break statement used with OpenMP for loop");
9007 break;
9008 }
9009 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9010 break;
9011
9012 case RID_CONTINUE:
9013 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9014 {
9015 case 0:
9016 error_at (token->location, "continue statement not within a loop");
9017 break;
9018 case IN_ITERATION_STMT:
9019 case IN_OMP_FOR:
9020 statement = finish_continue_stmt ();
9021 break;
9022 case IN_OMP_BLOCK:
9023 error_at (token->location, "invalid exit from OpenMP structured block");
9024 break;
9025 default:
9026 gcc_unreachable ();
9027 }
9028 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9029 break;
9030
9031 case RID_RETURN:
9032 {
9033 tree expr;
9034 bool expr_non_constant_p;
9035
9036 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9037 {
9038 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9039 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9040 }
9041 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9042 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9043 else
9044 /* If the next token is a `;', then there is no
9045 expression. */
9046 expr = NULL_TREE;
9047 /* Build the return-statement. */
9048 statement = finish_return_stmt (expr);
9049 /* Look for the final `;'. */
9050 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9051 }
9052 break;
9053
9054 case RID_GOTO:
9055 /* Create the goto-statement. */
9056 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9057 {
9058 /* Issue a warning about this use of a GNU extension. */
9059 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9060 /* Consume the '*' token. */
9061 cp_lexer_consume_token (parser->lexer);
9062 /* Parse the dependent expression. */
9063 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9064 }
9065 else
9066 finish_goto_stmt (cp_parser_identifier (parser));
9067 /* Look for the final `;'. */
9068 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9069 break;
9070
9071 default:
9072 cp_parser_error (parser, "expected jump-statement");
9073 break;
9074 }
9075
9076 return statement;
9077 }
9078
9079 /* Parse a declaration-statement.
9080
9081 declaration-statement:
9082 block-declaration */
9083
9084 static void
9085 cp_parser_declaration_statement (cp_parser* parser)
9086 {
9087 void *p;
9088
9089 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9090 p = obstack_alloc (&declarator_obstack, 0);
9091
9092 /* Parse the block-declaration. */
9093 cp_parser_block_declaration (parser, /*statement_p=*/true);
9094
9095 /* Free any declarators allocated. */
9096 obstack_free (&declarator_obstack, p);
9097
9098 /* Finish off the statement. */
9099 finish_stmt ();
9100 }
9101
9102 /* Some dependent statements (like `if (cond) statement'), are
9103 implicitly in their own scope. In other words, if the statement is
9104 a single statement (as opposed to a compound-statement), it is
9105 none-the-less treated as if it were enclosed in braces. Any
9106 declarations appearing in the dependent statement are out of scope
9107 after control passes that point. This function parses a statement,
9108 but ensures that is in its own scope, even if it is not a
9109 compound-statement.
9110
9111 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9112 is a (possibly labeled) if statement which is not enclosed in
9113 braces and has an else clause. This is used to implement
9114 -Wparentheses.
9115
9116 Returns the new statement. */
9117
9118 static tree
9119 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9120 {
9121 tree statement;
9122
9123 if (if_p != NULL)
9124 *if_p = false;
9125
9126 /* Mark if () ; with a special NOP_EXPR. */
9127 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9128 {
9129 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9130 cp_lexer_consume_token (parser->lexer);
9131 statement = add_stmt (build_empty_stmt (loc));
9132 }
9133 /* if a compound is opened, we simply parse the statement directly. */
9134 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9135 statement = cp_parser_compound_statement (parser, NULL, false, false);
9136 /* If the token is not a `{', then we must take special action. */
9137 else
9138 {
9139 /* Create a compound-statement. */
9140 statement = begin_compound_stmt (0);
9141 /* Parse the dependent-statement. */
9142 cp_parser_statement (parser, NULL_TREE, false, if_p);
9143 /* Finish the dummy compound-statement. */
9144 finish_compound_stmt (statement);
9145 }
9146
9147 /* Return the statement. */
9148 return statement;
9149 }
9150
9151 /* For some dependent statements (like `while (cond) statement'), we
9152 have already created a scope. Therefore, even if the dependent
9153 statement is a compound-statement, we do not want to create another
9154 scope. */
9155
9156 static void
9157 cp_parser_already_scoped_statement (cp_parser* parser)
9158 {
9159 /* If the token is a `{', then we must take special action. */
9160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9161 cp_parser_statement (parser, NULL_TREE, false, NULL);
9162 else
9163 {
9164 /* Avoid calling cp_parser_compound_statement, so that we
9165 don't create a new scope. Do everything else by hand. */
9166 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9167 /* If the next keyword is `__label__' we have a label declaration. */
9168 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9169 cp_parser_label_declaration (parser);
9170 /* Parse an (optional) statement-seq. */
9171 cp_parser_statement_seq_opt (parser, NULL_TREE);
9172 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9173 }
9174 }
9175
9176 /* Declarations [gram.dcl.dcl] */
9177
9178 /* Parse an optional declaration-sequence.
9179
9180 declaration-seq:
9181 declaration
9182 declaration-seq declaration */
9183
9184 static void
9185 cp_parser_declaration_seq_opt (cp_parser* parser)
9186 {
9187 while (true)
9188 {
9189 cp_token *token;
9190
9191 token = cp_lexer_peek_token (parser->lexer);
9192
9193 if (token->type == CPP_CLOSE_BRACE
9194 || token->type == CPP_EOF
9195 || token->type == CPP_PRAGMA_EOL)
9196 break;
9197
9198 if (token->type == CPP_SEMICOLON)
9199 {
9200 /* A declaration consisting of a single semicolon is
9201 invalid. Allow it unless we're being pedantic. */
9202 cp_lexer_consume_token (parser->lexer);
9203 if (!in_system_header)
9204 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9205 continue;
9206 }
9207
9208 /* If we're entering or exiting a region that's implicitly
9209 extern "C", modify the lang context appropriately. */
9210 if (!parser->implicit_extern_c && token->implicit_extern_c)
9211 {
9212 push_lang_context (lang_name_c);
9213 parser->implicit_extern_c = true;
9214 }
9215 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9216 {
9217 pop_lang_context ();
9218 parser->implicit_extern_c = false;
9219 }
9220
9221 if (token->type == CPP_PRAGMA)
9222 {
9223 /* A top-level declaration can consist solely of a #pragma.
9224 A nested declaration cannot, so this is done here and not
9225 in cp_parser_declaration. (A #pragma at block scope is
9226 handled in cp_parser_statement.) */
9227 cp_parser_pragma (parser, pragma_external);
9228 continue;
9229 }
9230
9231 /* Parse the declaration itself. */
9232 cp_parser_declaration (parser);
9233 }
9234 }
9235
9236 /* Parse a declaration.
9237
9238 declaration:
9239 block-declaration
9240 function-definition
9241 template-declaration
9242 explicit-instantiation
9243 explicit-specialization
9244 linkage-specification
9245 namespace-definition
9246
9247 GNU extension:
9248
9249 declaration:
9250 __extension__ declaration */
9251
9252 static void
9253 cp_parser_declaration (cp_parser* parser)
9254 {
9255 cp_token token1;
9256 cp_token token2;
9257 int saved_pedantic;
9258 void *p;
9259 tree attributes = NULL_TREE;
9260
9261 /* Check for the `__extension__' keyword. */
9262 if (cp_parser_extension_opt (parser, &saved_pedantic))
9263 {
9264 /* Parse the qualified declaration. */
9265 cp_parser_declaration (parser);
9266 /* Restore the PEDANTIC flag. */
9267 pedantic = saved_pedantic;
9268
9269 return;
9270 }
9271
9272 /* Try to figure out what kind of declaration is present. */
9273 token1 = *cp_lexer_peek_token (parser->lexer);
9274
9275 if (token1.type != CPP_EOF)
9276 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9277 else
9278 {
9279 token2.type = CPP_EOF;
9280 token2.keyword = RID_MAX;
9281 }
9282
9283 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9284 p = obstack_alloc (&declarator_obstack, 0);
9285
9286 /* If the next token is `extern' and the following token is a string
9287 literal, then we have a linkage specification. */
9288 if (token1.keyword == RID_EXTERN
9289 && cp_parser_is_string_literal (&token2))
9290 cp_parser_linkage_specification (parser);
9291 /* If the next token is `template', then we have either a template
9292 declaration, an explicit instantiation, or an explicit
9293 specialization. */
9294 else if (token1.keyword == RID_TEMPLATE)
9295 {
9296 /* `template <>' indicates a template specialization. */
9297 if (token2.type == CPP_LESS
9298 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9299 cp_parser_explicit_specialization (parser);
9300 /* `template <' indicates a template declaration. */
9301 else if (token2.type == CPP_LESS)
9302 cp_parser_template_declaration (parser, /*member_p=*/false);
9303 /* Anything else must be an explicit instantiation. */
9304 else
9305 cp_parser_explicit_instantiation (parser);
9306 }
9307 /* If the next token is `export', then we have a template
9308 declaration. */
9309 else if (token1.keyword == RID_EXPORT)
9310 cp_parser_template_declaration (parser, /*member_p=*/false);
9311 /* If the next token is `extern', 'static' or 'inline' and the one
9312 after that is `template', we have a GNU extended explicit
9313 instantiation directive. */
9314 else if (cp_parser_allow_gnu_extensions_p (parser)
9315 && (token1.keyword == RID_EXTERN
9316 || token1.keyword == RID_STATIC
9317 || token1.keyword == RID_INLINE)
9318 && token2.keyword == RID_TEMPLATE)
9319 cp_parser_explicit_instantiation (parser);
9320 /* If the next token is `namespace', check for a named or unnamed
9321 namespace definition. */
9322 else if (token1.keyword == RID_NAMESPACE
9323 && (/* A named namespace definition. */
9324 (token2.type == CPP_NAME
9325 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9326 != CPP_EQ))
9327 /* An unnamed namespace definition. */
9328 || token2.type == CPP_OPEN_BRACE
9329 || token2.keyword == RID_ATTRIBUTE))
9330 cp_parser_namespace_definition (parser);
9331 /* An inline (associated) namespace definition. */
9332 else if (token1.keyword == RID_INLINE
9333 && token2.keyword == RID_NAMESPACE)
9334 cp_parser_namespace_definition (parser);
9335 /* Objective-C++ declaration/definition. */
9336 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9337 cp_parser_objc_declaration (parser, NULL_TREE);
9338 else if (c_dialect_objc ()
9339 && token1.keyword == RID_ATTRIBUTE
9340 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9341 cp_parser_objc_declaration (parser, attributes);
9342 /* We must have either a block declaration or a function
9343 definition. */
9344 else
9345 /* Try to parse a block-declaration, or a function-definition. */
9346 cp_parser_block_declaration (parser, /*statement_p=*/false);
9347
9348 /* Free any declarators allocated. */
9349 obstack_free (&declarator_obstack, p);
9350 }
9351
9352 /* Parse a block-declaration.
9353
9354 block-declaration:
9355 simple-declaration
9356 asm-definition
9357 namespace-alias-definition
9358 using-declaration
9359 using-directive
9360
9361 GNU Extension:
9362
9363 block-declaration:
9364 __extension__ block-declaration
9365
9366 C++0x Extension:
9367
9368 block-declaration:
9369 static_assert-declaration
9370
9371 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9372 part of a declaration-statement. */
9373
9374 static void
9375 cp_parser_block_declaration (cp_parser *parser,
9376 bool statement_p)
9377 {
9378 cp_token *token1;
9379 int saved_pedantic;
9380
9381 /* Check for the `__extension__' keyword. */
9382 if (cp_parser_extension_opt (parser, &saved_pedantic))
9383 {
9384 /* Parse the qualified declaration. */
9385 cp_parser_block_declaration (parser, statement_p);
9386 /* Restore the PEDANTIC flag. */
9387 pedantic = saved_pedantic;
9388
9389 return;
9390 }
9391
9392 /* Peek at the next token to figure out which kind of declaration is
9393 present. */
9394 token1 = cp_lexer_peek_token (parser->lexer);
9395
9396 /* If the next keyword is `asm', we have an asm-definition. */
9397 if (token1->keyword == RID_ASM)
9398 {
9399 if (statement_p)
9400 cp_parser_commit_to_tentative_parse (parser);
9401 cp_parser_asm_definition (parser);
9402 }
9403 /* If the next keyword is `namespace', we have a
9404 namespace-alias-definition. */
9405 else if (token1->keyword == RID_NAMESPACE)
9406 cp_parser_namespace_alias_definition (parser);
9407 /* If the next keyword is `using', we have either a
9408 using-declaration or a using-directive. */
9409 else if (token1->keyword == RID_USING)
9410 {
9411 cp_token *token2;
9412
9413 if (statement_p)
9414 cp_parser_commit_to_tentative_parse (parser);
9415 /* If the token after `using' is `namespace', then we have a
9416 using-directive. */
9417 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9418 if (token2->keyword == RID_NAMESPACE)
9419 cp_parser_using_directive (parser);
9420 /* Otherwise, it's a using-declaration. */
9421 else
9422 cp_parser_using_declaration (parser,
9423 /*access_declaration_p=*/false);
9424 }
9425 /* If the next keyword is `__label__' we have a misplaced label
9426 declaration. */
9427 else if (token1->keyword == RID_LABEL)
9428 {
9429 cp_lexer_consume_token (parser->lexer);
9430 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9431 cp_parser_skip_to_end_of_statement (parser);
9432 /* If the next token is now a `;', consume it. */
9433 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9434 cp_lexer_consume_token (parser->lexer);
9435 }
9436 /* If the next token is `static_assert' we have a static assertion. */
9437 else if (token1->keyword == RID_STATIC_ASSERT)
9438 cp_parser_static_assert (parser, /*member_p=*/false);
9439 /* Anything else must be a simple-declaration. */
9440 else
9441 cp_parser_simple_declaration (parser, !statement_p,
9442 /*maybe_range_for_decl*/NULL);
9443 }
9444
9445 /* Parse a simple-declaration.
9446
9447 simple-declaration:
9448 decl-specifier-seq [opt] init-declarator-list [opt] ;
9449
9450 init-declarator-list:
9451 init-declarator
9452 init-declarator-list , init-declarator
9453
9454 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9455 function-definition as a simple-declaration.
9456
9457 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9458 parsed declaration if it is an uninitialized single declarator not followed
9459 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9460 if present, will not be consumed. */
9461
9462 static void
9463 cp_parser_simple_declaration (cp_parser* parser,
9464 bool function_definition_allowed_p,
9465 tree *maybe_range_for_decl)
9466 {
9467 cp_decl_specifier_seq decl_specifiers;
9468 int declares_class_or_enum;
9469 bool saw_declarator;
9470
9471 if (maybe_range_for_decl)
9472 *maybe_range_for_decl = NULL_TREE;
9473
9474 /* Defer access checks until we know what is being declared; the
9475 checks for names appearing in the decl-specifier-seq should be
9476 done as if we were in the scope of the thing being declared. */
9477 push_deferring_access_checks (dk_deferred);
9478
9479 /* Parse the decl-specifier-seq. We have to keep track of whether
9480 or not the decl-specifier-seq declares a named class or
9481 enumeration type, since that is the only case in which the
9482 init-declarator-list is allowed to be empty.
9483
9484 [dcl.dcl]
9485
9486 In a simple-declaration, the optional init-declarator-list can be
9487 omitted only when declaring a class or enumeration, that is when
9488 the decl-specifier-seq contains either a class-specifier, an
9489 elaborated-type-specifier, or an enum-specifier. */
9490 cp_parser_decl_specifier_seq (parser,
9491 CP_PARSER_FLAGS_OPTIONAL,
9492 &decl_specifiers,
9493 &declares_class_or_enum);
9494 /* We no longer need to defer access checks. */
9495 stop_deferring_access_checks ();
9496
9497 /* In a block scope, a valid declaration must always have a
9498 decl-specifier-seq. By not trying to parse declarators, we can
9499 resolve the declaration/expression ambiguity more quickly. */
9500 if (!function_definition_allowed_p
9501 && !decl_specifiers.any_specifiers_p)
9502 {
9503 cp_parser_error (parser, "expected declaration");
9504 goto done;
9505 }
9506
9507 /* If the next two tokens are both identifiers, the code is
9508 erroneous. The usual cause of this situation is code like:
9509
9510 T t;
9511
9512 where "T" should name a type -- but does not. */
9513 if (!decl_specifiers.any_type_specifiers_p
9514 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9515 {
9516 /* If parsing tentatively, we should commit; we really are
9517 looking at a declaration. */
9518 cp_parser_commit_to_tentative_parse (parser);
9519 /* Give up. */
9520 goto done;
9521 }
9522
9523 /* If we have seen at least one decl-specifier, and the next token
9524 is not a parenthesis, then we must be looking at a declaration.
9525 (After "int (" we might be looking at a functional cast.) */
9526 if (decl_specifiers.any_specifiers_p
9527 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9528 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9529 && !cp_parser_error_occurred (parser))
9530 cp_parser_commit_to_tentative_parse (parser);
9531
9532 /* Keep going until we hit the `;' at the end of the simple
9533 declaration. */
9534 saw_declarator = false;
9535 while (cp_lexer_next_token_is_not (parser->lexer,
9536 CPP_SEMICOLON))
9537 {
9538 cp_token *token;
9539 bool function_definition_p;
9540 tree decl;
9541
9542 if (saw_declarator)
9543 {
9544 /* If we are processing next declarator, coma is expected */
9545 token = cp_lexer_peek_token (parser->lexer);
9546 gcc_assert (token->type == CPP_COMMA);
9547 cp_lexer_consume_token (parser->lexer);
9548 if (maybe_range_for_decl)
9549 *maybe_range_for_decl = error_mark_node;
9550 }
9551 else
9552 saw_declarator = true;
9553
9554 /* Parse the init-declarator. */
9555 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9556 /*checks=*/NULL,
9557 function_definition_allowed_p,
9558 /*member_p=*/false,
9559 declares_class_or_enum,
9560 &function_definition_p,
9561 maybe_range_for_decl);
9562 /* If an error occurred while parsing tentatively, exit quickly.
9563 (That usually happens when in the body of a function; each
9564 statement is treated as a declaration-statement until proven
9565 otherwise.) */
9566 if (cp_parser_error_occurred (parser))
9567 goto done;
9568 /* Handle function definitions specially. */
9569 if (function_definition_p)
9570 {
9571 /* If the next token is a `,', then we are probably
9572 processing something like:
9573
9574 void f() {}, *p;
9575
9576 which is erroneous. */
9577 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9578 {
9579 cp_token *token = cp_lexer_peek_token (parser->lexer);
9580 error_at (token->location,
9581 "mixing"
9582 " declarations and function-definitions is forbidden");
9583 }
9584 /* Otherwise, we're done with the list of declarators. */
9585 else
9586 {
9587 pop_deferring_access_checks ();
9588 return;
9589 }
9590 }
9591 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9592 *maybe_range_for_decl = decl;
9593 /* The next token should be either a `,' or a `;'. */
9594 token = cp_lexer_peek_token (parser->lexer);
9595 /* If it's a `,', there are more declarators to come. */
9596 if (token->type == CPP_COMMA)
9597 /* will be consumed next time around */;
9598 /* If it's a `;', we are done. */
9599 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9600 break;
9601 /* Anything else is an error. */
9602 else
9603 {
9604 /* If we have already issued an error message we don't need
9605 to issue another one. */
9606 if (decl != error_mark_node
9607 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9608 cp_parser_error (parser, "expected %<,%> or %<;%>");
9609 /* Skip tokens until we reach the end of the statement. */
9610 cp_parser_skip_to_end_of_statement (parser);
9611 /* If the next token is now a `;', consume it. */
9612 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9613 cp_lexer_consume_token (parser->lexer);
9614 goto done;
9615 }
9616 /* After the first time around, a function-definition is not
9617 allowed -- even if it was OK at first. For example:
9618
9619 int i, f() {}
9620
9621 is not valid. */
9622 function_definition_allowed_p = false;
9623 }
9624
9625 /* Issue an error message if no declarators are present, and the
9626 decl-specifier-seq does not itself declare a class or
9627 enumeration. */
9628 if (!saw_declarator)
9629 {
9630 if (cp_parser_declares_only_class_p (parser))
9631 shadow_tag (&decl_specifiers);
9632 /* Perform any deferred access checks. */
9633 perform_deferred_access_checks ();
9634 }
9635
9636 /* Consume the `;'. */
9637 if (!maybe_range_for_decl)
9638 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9639
9640 done:
9641 pop_deferring_access_checks ();
9642 }
9643
9644 /* Parse a decl-specifier-seq.
9645
9646 decl-specifier-seq:
9647 decl-specifier-seq [opt] decl-specifier
9648
9649 decl-specifier:
9650 storage-class-specifier
9651 type-specifier
9652 function-specifier
9653 friend
9654 typedef
9655
9656 GNU Extension:
9657
9658 decl-specifier:
9659 attributes
9660
9661 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9662
9663 The parser flags FLAGS is used to control type-specifier parsing.
9664
9665 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9666 flags:
9667
9668 1: one of the decl-specifiers is an elaborated-type-specifier
9669 (i.e., a type declaration)
9670 2: one of the decl-specifiers is an enum-specifier or a
9671 class-specifier (i.e., a type definition)
9672
9673 */
9674
9675 static void
9676 cp_parser_decl_specifier_seq (cp_parser* parser,
9677 cp_parser_flags flags,
9678 cp_decl_specifier_seq *decl_specs,
9679 int* declares_class_or_enum)
9680 {
9681 bool constructor_possible_p = !parser->in_declarator_p;
9682 cp_token *start_token = NULL;
9683
9684 /* Clear DECL_SPECS. */
9685 clear_decl_specs (decl_specs);
9686
9687 /* Assume no class or enumeration type is declared. */
9688 *declares_class_or_enum = 0;
9689
9690 /* Keep reading specifiers until there are no more to read. */
9691 while (true)
9692 {
9693 bool constructor_p;
9694 bool found_decl_spec;
9695 cp_token *token;
9696
9697 /* Peek at the next token. */
9698 token = cp_lexer_peek_token (parser->lexer);
9699
9700 /* Save the first token of the decl spec list for error
9701 reporting. */
9702 if (!start_token)
9703 start_token = token;
9704 /* Handle attributes. */
9705 if (token->keyword == RID_ATTRIBUTE)
9706 {
9707 /* Parse the attributes. */
9708 decl_specs->attributes
9709 = chainon (decl_specs->attributes,
9710 cp_parser_attributes_opt (parser));
9711 continue;
9712 }
9713 /* Assume we will find a decl-specifier keyword. */
9714 found_decl_spec = true;
9715 /* If the next token is an appropriate keyword, we can simply
9716 add it to the list. */
9717 switch (token->keyword)
9718 {
9719 /* decl-specifier:
9720 friend
9721 constexpr */
9722 case RID_FRIEND:
9723 if (!at_class_scope_p ())
9724 {
9725 error_at (token->location, "%<friend%> used outside of class");
9726 cp_lexer_purge_token (parser->lexer);
9727 }
9728 else
9729 {
9730 ++decl_specs->specs[(int) ds_friend];
9731 /* Consume the token. */
9732 cp_lexer_consume_token (parser->lexer);
9733 }
9734 break;
9735
9736 case RID_CONSTEXPR:
9737 ++decl_specs->specs[(int) ds_constexpr];
9738 cp_lexer_consume_token (parser->lexer);
9739 break;
9740
9741 /* function-specifier:
9742 inline
9743 virtual
9744 explicit */
9745 case RID_INLINE:
9746 case RID_VIRTUAL:
9747 case RID_EXPLICIT:
9748 cp_parser_function_specifier_opt (parser, decl_specs);
9749 break;
9750
9751 /* decl-specifier:
9752 typedef */
9753 case RID_TYPEDEF:
9754 ++decl_specs->specs[(int) ds_typedef];
9755 /* Consume the token. */
9756 cp_lexer_consume_token (parser->lexer);
9757 /* A constructor declarator cannot appear in a typedef. */
9758 constructor_possible_p = false;
9759 /* The "typedef" keyword can only occur in a declaration; we
9760 may as well commit at this point. */
9761 cp_parser_commit_to_tentative_parse (parser);
9762
9763 if (decl_specs->storage_class != sc_none)
9764 decl_specs->conflicting_specifiers_p = true;
9765 break;
9766
9767 /* storage-class-specifier:
9768 auto
9769 register
9770 static
9771 extern
9772 mutable
9773
9774 GNU Extension:
9775 thread */
9776 case RID_AUTO:
9777 if (cxx_dialect == cxx98)
9778 {
9779 /* Consume the token. */
9780 cp_lexer_consume_token (parser->lexer);
9781
9782 /* Complain about `auto' as a storage specifier, if
9783 we're complaining about C++0x compatibility. */
9784 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9785 " will change meaning in C++0x; please remove it");
9786
9787 /* Set the storage class anyway. */
9788 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9789 token->location);
9790 }
9791 else
9792 /* C++0x auto type-specifier. */
9793 found_decl_spec = false;
9794 break;
9795
9796 case RID_REGISTER:
9797 case RID_STATIC:
9798 case RID_EXTERN:
9799 case RID_MUTABLE:
9800 /* Consume the token. */
9801 cp_lexer_consume_token (parser->lexer);
9802 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9803 token->location);
9804 break;
9805 case RID_THREAD:
9806 /* Consume the token. */
9807 cp_lexer_consume_token (parser->lexer);
9808 ++decl_specs->specs[(int) ds_thread];
9809 break;
9810
9811 default:
9812 /* We did not yet find a decl-specifier yet. */
9813 found_decl_spec = false;
9814 break;
9815 }
9816
9817 if (found_decl_spec
9818 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9819 && token->keyword != RID_CONSTEXPR)
9820 error ("decl-specifier invalid in condition");
9821
9822 /* Constructors are a special case. The `S' in `S()' is not a
9823 decl-specifier; it is the beginning of the declarator. */
9824 constructor_p
9825 = (!found_decl_spec
9826 && constructor_possible_p
9827 && (cp_parser_constructor_declarator_p
9828 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9829
9830 /* If we don't have a DECL_SPEC yet, then we must be looking at
9831 a type-specifier. */
9832 if (!found_decl_spec && !constructor_p)
9833 {
9834 int decl_spec_declares_class_or_enum;
9835 bool is_cv_qualifier;
9836 tree type_spec;
9837
9838 type_spec
9839 = cp_parser_type_specifier (parser, flags,
9840 decl_specs,
9841 /*is_declaration=*/true,
9842 &decl_spec_declares_class_or_enum,
9843 &is_cv_qualifier);
9844 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9845
9846 /* If this type-specifier referenced a user-defined type
9847 (a typedef, class-name, etc.), then we can't allow any
9848 more such type-specifiers henceforth.
9849
9850 [dcl.spec]
9851
9852 The longest sequence of decl-specifiers that could
9853 possibly be a type name is taken as the
9854 decl-specifier-seq of a declaration. The sequence shall
9855 be self-consistent as described below.
9856
9857 [dcl.type]
9858
9859 As a general rule, at most one type-specifier is allowed
9860 in the complete decl-specifier-seq of a declaration. The
9861 only exceptions are the following:
9862
9863 -- const or volatile can be combined with any other
9864 type-specifier.
9865
9866 -- signed or unsigned can be combined with char, long,
9867 short, or int.
9868
9869 -- ..
9870
9871 Example:
9872
9873 typedef char* Pc;
9874 void g (const int Pc);
9875
9876 Here, Pc is *not* part of the decl-specifier seq; it's
9877 the declarator. Therefore, once we see a type-specifier
9878 (other than a cv-qualifier), we forbid any additional
9879 user-defined types. We *do* still allow things like `int
9880 int' to be considered a decl-specifier-seq, and issue the
9881 error message later. */
9882 if (type_spec && !is_cv_qualifier)
9883 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9884 /* A constructor declarator cannot follow a type-specifier. */
9885 if (type_spec)
9886 {
9887 constructor_possible_p = false;
9888 found_decl_spec = true;
9889 if (!is_cv_qualifier)
9890 decl_specs->any_type_specifiers_p = true;
9891 }
9892 }
9893
9894 /* If we still do not have a DECL_SPEC, then there are no more
9895 decl-specifiers. */
9896 if (!found_decl_spec)
9897 break;
9898
9899 decl_specs->any_specifiers_p = true;
9900 /* After we see one decl-specifier, further decl-specifiers are
9901 always optional. */
9902 flags |= CP_PARSER_FLAGS_OPTIONAL;
9903 }
9904
9905 cp_parser_check_decl_spec (decl_specs, start_token->location);
9906
9907 /* Don't allow a friend specifier with a class definition. */
9908 if (decl_specs->specs[(int) ds_friend] != 0
9909 && (*declares_class_or_enum & 2))
9910 error_at (start_token->location,
9911 "class definition may not be declared a friend");
9912 }
9913
9914 /* Parse an (optional) storage-class-specifier.
9915
9916 storage-class-specifier:
9917 auto
9918 register
9919 static
9920 extern
9921 mutable
9922
9923 GNU Extension:
9924
9925 storage-class-specifier:
9926 thread
9927
9928 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9929
9930 static tree
9931 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9932 {
9933 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9934 {
9935 case RID_AUTO:
9936 if (cxx_dialect != cxx98)
9937 return NULL_TREE;
9938 /* Fall through for C++98. */
9939
9940 case RID_REGISTER:
9941 case RID_STATIC:
9942 case RID_EXTERN:
9943 case RID_MUTABLE:
9944 case RID_THREAD:
9945 /* Consume the token. */
9946 return cp_lexer_consume_token (parser->lexer)->u.value;
9947
9948 default:
9949 return NULL_TREE;
9950 }
9951 }
9952
9953 /* Parse an (optional) function-specifier.
9954
9955 function-specifier:
9956 inline
9957 virtual
9958 explicit
9959
9960 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9961 Updates DECL_SPECS, if it is non-NULL. */
9962
9963 static tree
9964 cp_parser_function_specifier_opt (cp_parser* parser,
9965 cp_decl_specifier_seq *decl_specs)
9966 {
9967 cp_token *token = cp_lexer_peek_token (parser->lexer);
9968 switch (token->keyword)
9969 {
9970 case RID_INLINE:
9971 if (decl_specs)
9972 ++decl_specs->specs[(int) ds_inline];
9973 break;
9974
9975 case RID_VIRTUAL:
9976 /* 14.5.2.3 [temp.mem]
9977
9978 A member function template shall not be virtual. */
9979 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9980 error_at (token->location, "templates may not be %<virtual%>");
9981 else if (decl_specs)
9982 ++decl_specs->specs[(int) ds_virtual];
9983 break;
9984
9985 case RID_EXPLICIT:
9986 if (decl_specs)
9987 ++decl_specs->specs[(int) ds_explicit];
9988 break;
9989
9990 default:
9991 return NULL_TREE;
9992 }
9993
9994 /* Consume the token. */
9995 return cp_lexer_consume_token (parser->lexer)->u.value;
9996 }
9997
9998 /* Parse a linkage-specification.
9999
10000 linkage-specification:
10001 extern string-literal { declaration-seq [opt] }
10002 extern string-literal declaration */
10003
10004 static void
10005 cp_parser_linkage_specification (cp_parser* parser)
10006 {
10007 tree linkage;
10008
10009 /* Look for the `extern' keyword. */
10010 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10011
10012 /* Look for the string-literal. */
10013 linkage = cp_parser_string_literal (parser, false, false);
10014
10015 /* Transform the literal into an identifier. If the literal is a
10016 wide-character string, or contains embedded NULs, then we can't
10017 handle it as the user wants. */
10018 if (strlen (TREE_STRING_POINTER (linkage))
10019 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10020 {
10021 cp_parser_error (parser, "invalid linkage-specification");
10022 /* Assume C++ linkage. */
10023 linkage = lang_name_cplusplus;
10024 }
10025 else
10026 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10027
10028 /* We're now using the new linkage. */
10029 push_lang_context (linkage);
10030
10031 /* If the next token is a `{', then we're using the first
10032 production. */
10033 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10034 {
10035 /* Consume the `{' token. */
10036 cp_lexer_consume_token (parser->lexer);
10037 /* Parse the declarations. */
10038 cp_parser_declaration_seq_opt (parser);
10039 /* Look for the closing `}'. */
10040 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10041 }
10042 /* Otherwise, there's just one declaration. */
10043 else
10044 {
10045 bool saved_in_unbraced_linkage_specification_p;
10046
10047 saved_in_unbraced_linkage_specification_p
10048 = parser->in_unbraced_linkage_specification_p;
10049 parser->in_unbraced_linkage_specification_p = true;
10050 cp_parser_declaration (parser);
10051 parser->in_unbraced_linkage_specification_p
10052 = saved_in_unbraced_linkage_specification_p;
10053 }
10054
10055 /* We're done with the linkage-specification. */
10056 pop_lang_context ();
10057 }
10058
10059 /* Parse a static_assert-declaration.
10060
10061 static_assert-declaration:
10062 static_assert ( constant-expression , string-literal ) ;
10063
10064 If MEMBER_P, this static_assert is a class member. */
10065
10066 static void
10067 cp_parser_static_assert(cp_parser *parser, bool member_p)
10068 {
10069 tree condition;
10070 tree message;
10071 cp_token *token;
10072 location_t saved_loc;
10073 bool dummy;
10074
10075 /* Peek at the `static_assert' token so we can keep track of exactly
10076 where the static assertion started. */
10077 token = cp_lexer_peek_token (parser->lexer);
10078 saved_loc = token->location;
10079
10080 /* Look for the `static_assert' keyword. */
10081 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10082 RT_STATIC_ASSERT))
10083 return;
10084
10085 /* We know we are in a static assertion; commit to any tentative
10086 parse. */
10087 if (cp_parser_parsing_tentatively (parser))
10088 cp_parser_commit_to_tentative_parse (parser);
10089
10090 /* Parse the `(' starting the static assertion condition. */
10091 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10092
10093 /* Parse the constant-expression. Allow a non-constant expression
10094 here in order to give better diagnostics in finish_static_assert. */
10095 condition =
10096 cp_parser_constant_expression (parser,
10097 /*allow_non_constant_p=*/true,
10098 /*non_constant_p=*/&dummy);
10099
10100 /* Parse the separating `,'. */
10101 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10102
10103 /* Parse the string-literal message. */
10104 message = cp_parser_string_literal (parser,
10105 /*translate=*/false,
10106 /*wide_ok=*/true);
10107
10108 /* A `)' completes the static assertion. */
10109 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10110 cp_parser_skip_to_closing_parenthesis (parser,
10111 /*recovering=*/true,
10112 /*or_comma=*/false,
10113 /*consume_paren=*/true);
10114
10115 /* A semicolon terminates the declaration. */
10116 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10117
10118 /* Complete the static assertion, which may mean either processing
10119 the static assert now or saving it for template instantiation. */
10120 finish_static_assert (condition, message, saved_loc, member_p);
10121 }
10122
10123 /* Parse a `decltype' type. Returns the type.
10124
10125 simple-type-specifier:
10126 decltype ( expression ) */
10127
10128 static tree
10129 cp_parser_decltype (cp_parser *parser)
10130 {
10131 tree expr;
10132 bool id_expression_or_member_access_p = false;
10133 const char *saved_message;
10134 bool saved_integral_constant_expression_p;
10135 bool saved_non_integral_constant_expression_p;
10136 cp_token *id_expr_start_token;
10137
10138 /* Look for the `decltype' token. */
10139 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10140 return error_mark_node;
10141
10142 /* Types cannot be defined in a `decltype' expression. Save away the
10143 old message. */
10144 saved_message = parser->type_definition_forbidden_message;
10145
10146 /* And create the new one. */
10147 parser->type_definition_forbidden_message
10148 = G_("types may not be defined in %<decltype%> expressions");
10149
10150 /* The restrictions on constant-expressions do not apply inside
10151 decltype expressions. */
10152 saved_integral_constant_expression_p
10153 = parser->integral_constant_expression_p;
10154 saved_non_integral_constant_expression_p
10155 = parser->non_integral_constant_expression_p;
10156 parser->integral_constant_expression_p = false;
10157
10158 /* Do not actually evaluate the expression. */
10159 ++cp_unevaluated_operand;
10160
10161 /* Do not warn about problems with the expression. */
10162 ++c_inhibit_evaluation_warnings;
10163
10164 /* Parse the opening `('. */
10165 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10166 return error_mark_node;
10167
10168 /* First, try parsing an id-expression. */
10169 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10170 cp_parser_parse_tentatively (parser);
10171 expr = cp_parser_id_expression (parser,
10172 /*template_keyword_p=*/false,
10173 /*check_dependency_p=*/true,
10174 /*template_p=*/NULL,
10175 /*declarator_p=*/false,
10176 /*optional_p=*/false);
10177
10178 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10179 {
10180 bool non_integral_constant_expression_p = false;
10181 tree id_expression = expr;
10182 cp_id_kind idk;
10183 const char *error_msg;
10184
10185 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10186 /* Lookup the name we got back from the id-expression. */
10187 expr = cp_parser_lookup_name (parser, expr,
10188 none_type,
10189 /*is_template=*/false,
10190 /*is_namespace=*/false,
10191 /*check_dependency=*/true,
10192 /*ambiguous_decls=*/NULL,
10193 id_expr_start_token->location);
10194
10195 if (expr
10196 && expr != error_mark_node
10197 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10198 && TREE_CODE (expr) != TYPE_DECL
10199 && (TREE_CODE (expr) != BIT_NOT_EXPR
10200 || !TYPE_P (TREE_OPERAND (expr, 0)))
10201 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10202 {
10203 /* Complete lookup of the id-expression. */
10204 expr = (finish_id_expression
10205 (id_expression, expr, parser->scope, &idk,
10206 /*integral_constant_expression_p=*/false,
10207 /*allow_non_integral_constant_expression_p=*/true,
10208 &non_integral_constant_expression_p,
10209 /*template_p=*/false,
10210 /*done=*/true,
10211 /*address_p=*/false,
10212 /*template_arg_p=*/false,
10213 &error_msg,
10214 id_expr_start_token->location));
10215
10216 if (expr == error_mark_node)
10217 /* We found an id-expression, but it was something that we
10218 should not have found. This is an error, not something
10219 we can recover from, so note that we found an
10220 id-expression and we'll recover as gracefully as
10221 possible. */
10222 id_expression_or_member_access_p = true;
10223 }
10224
10225 if (expr
10226 && expr != error_mark_node
10227 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10228 /* We have an id-expression. */
10229 id_expression_or_member_access_p = true;
10230 }
10231
10232 if (!id_expression_or_member_access_p)
10233 {
10234 /* Abort the id-expression parse. */
10235 cp_parser_abort_tentative_parse (parser);
10236
10237 /* Parsing tentatively, again. */
10238 cp_parser_parse_tentatively (parser);
10239
10240 /* Parse a class member access. */
10241 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10242 /*cast_p=*/false,
10243 /*member_access_only_p=*/true, NULL);
10244
10245 if (expr
10246 && expr != error_mark_node
10247 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10248 /* We have an id-expression. */
10249 id_expression_or_member_access_p = true;
10250 }
10251
10252 if (id_expression_or_member_access_p)
10253 /* We have parsed the complete id-expression or member access. */
10254 cp_parser_parse_definitely (parser);
10255 else
10256 {
10257 bool saved_greater_than_is_operator_p;
10258
10259 /* Abort our attempt to parse an id-expression or member access
10260 expression. */
10261 cp_parser_abort_tentative_parse (parser);
10262
10263 /* Within a parenthesized expression, a `>' token is always
10264 the greater-than operator. */
10265 saved_greater_than_is_operator_p
10266 = parser->greater_than_is_operator_p;
10267 parser->greater_than_is_operator_p = true;
10268
10269 /* Parse a full expression. */
10270 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10271
10272 /* The `>' token might be the end of a template-id or
10273 template-parameter-list now. */
10274 parser->greater_than_is_operator_p
10275 = saved_greater_than_is_operator_p;
10276 }
10277
10278 /* Go back to evaluating expressions. */
10279 --cp_unevaluated_operand;
10280 --c_inhibit_evaluation_warnings;
10281
10282 /* Restore the old message and the integral constant expression
10283 flags. */
10284 parser->type_definition_forbidden_message = saved_message;
10285 parser->integral_constant_expression_p
10286 = saved_integral_constant_expression_p;
10287 parser->non_integral_constant_expression_p
10288 = saved_non_integral_constant_expression_p;
10289
10290 if (expr == error_mark_node)
10291 {
10292 /* Skip everything up to the closing `)'. */
10293 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10294 /*consume_paren=*/true);
10295 return error_mark_node;
10296 }
10297
10298 /* Parse to the closing `)'. */
10299 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10300 {
10301 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10302 /*consume_paren=*/true);
10303 return error_mark_node;
10304 }
10305
10306 return finish_decltype_type (expr, id_expression_or_member_access_p,
10307 tf_warning_or_error);
10308 }
10309
10310 /* Special member functions [gram.special] */
10311
10312 /* Parse a conversion-function-id.
10313
10314 conversion-function-id:
10315 operator conversion-type-id
10316
10317 Returns an IDENTIFIER_NODE representing the operator. */
10318
10319 static tree
10320 cp_parser_conversion_function_id (cp_parser* parser)
10321 {
10322 tree type;
10323 tree saved_scope;
10324 tree saved_qualifying_scope;
10325 tree saved_object_scope;
10326 tree pushed_scope = NULL_TREE;
10327
10328 /* Look for the `operator' token. */
10329 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10330 return error_mark_node;
10331 /* When we parse the conversion-type-id, the current scope will be
10332 reset. However, we need that information in able to look up the
10333 conversion function later, so we save it here. */
10334 saved_scope = parser->scope;
10335 saved_qualifying_scope = parser->qualifying_scope;
10336 saved_object_scope = parser->object_scope;
10337 /* We must enter the scope of the class so that the names of
10338 entities declared within the class are available in the
10339 conversion-type-id. For example, consider:
10340
10341 struct S {
10342 typedef int I;
10343 operator I();
10344 };
10345
10346 S::operator I() { ... }
10347
10348 In order to see that `I' is a type-name in the definition, we
10349 must be in the scope of `S'. */
10350 if (saved_scope)
10351 pushed_scope = push_scope (saved_scope);
10352 /* Parse the conversion-type-id. */
10353 type = cp_parser_conversion_type_id (parser);
10354 /* Leave the scope of the class, if any. */
10355 if (pushed_scope)
10356 pop_scope (pushed_scope);
10357 /* Restore the saved scope. */
10358 parser->scope = saved_scope;
10359 parser->qualifying_scope = saved_qualifying_scope;
10360 parser->object_scope = saved_object_scope;
10361 /* If the TYPE is invalid, indicate failure. */
10362 if (type == error_mark_node)
10363 return error_mark_node;
10364 return mangle_conv_op_name_for_type (type);
10365 }
10366
10367 /* Parse a conversion-type-id:
10368
10369 conversion-type-id:
10370 type-specifier-seq conversion-declarator [opt]
10371
10372 Returns the TYPE specified. */
10373
10374 static tree
10375 cp_parser_conversion_type_id (cp_parser* parser)
10376 {
10377 tree attributes;
10378 cp_decl_specifier_seq type_specifiers;
10379 cp_declarator *declarator;
10380 tree type_specified;
10381
10382 /* Parse the attributes. */
10383 attributes = cp_parser_attributes_opt (parser);
10384 /* Parse the type-specifiers. */
10385 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10386 /*is_trailing_return=*/false,
10387 &type_specifiers);
10388 /* If that didn't work, stop. */
10389 if (type_specifiers.type == error_mark_node)
10390 return error_mark_node;
10391 /* Parse the conversion-declarator. */
10392 declarator = cp_parser_conversion_declarator_opt (parser);
10393
10394 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10395 /*initialized=*/0, &attributes);
10396 if (attributes)
10397 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10398
10399 /* Don't give this error when parsing tentatively. This happens to
10400 work because we always parse this definitively once. */
10401 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10402 && type_uses_auto (type_specified))
10403 {
10404 error ("invalid use of %<auto%> in conversion operator");
10405 return error_mark_node;
10406 }
10407
10408 return type_specified;
10409 }
10410
10411 /* Parse an (optional) conversion-declarator.
10412
10413 conversion-declarator:
10414 ptr-operator conversion-declarator [opt]
10415
10416 */
10417
10418 static cp_declarator *
10419 cp_parser_conversion_declarator_opt (cp_parser* parser)
10420 {
10421 enum tree_code code;
10422 tree class_type;
10423 cp_cv_quals cv_quals;
10424
10425 /* We don't know if there's a ptr-operator next, or not. */
10426 cp_parser_parse_tentatively (parser);
10427 /* Try the ptr-operator. */
10428 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10429 /* If it worked, look for more conversion-declarators. */
10430 if (cp_parser_parse_definitely (parser))
10431 {
10432 cp_declarator *declarator;
10433
10434 /* Parse another optional declarator. */
10435 declarator = cp_parser_conversion_declarator_opt (parser);
10436
10437 return cp_parser_make_indirect_declarator
10438 (code, class_type, cv_quals, declarator);
10439 }
10440
10441 return NULL;
10442 }
10443
10444 /* Parse an (optional) ctor-initializer.
10445
10446 ctor-initializer:
10447 : mem-initializer-list
10448
10449 Returns TRUE iff the ctor-initializer was actually present. */
10450
10451 static bool
10452 cp_parser_ctor_initializer_opt (cp_parser* parser)
10453 {
10454 /* If the next token is not a `:', then there is no
10455 ctor-initializer. */
10456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10457 {
10458 /* Do default initialization of any bases and members. */
10459 if (DECL_CONSTRUCTOR_P (current_function_decl))
10460 finish_mem_initializers (NULL_TREE);
10461
10462 return false;
10463 }
10464
10465 /* Consume the `:' token. */
10466 cp_lexer_consume_token (parser->lexer);
10467 /* And the mem-initializer-list. */
10468 cp_parser_mem_initializer_list (parser);
10469
10470 return true;
10471 }
10472
10473 /* Parse a mem-initializer-list.
10474
10475 mem-initializer-list:
10476 mem-initializer ... [opt]
10477 mem-initializer ... [opt] , mem-initializer-list */
10478
10479 static void
10480 cp_parser_mem_initializer_list (cp_parser* parser)
10481 {
10482 tree mem_initializer_list = NULL_TREE;
10483 cp_token *token = cp_lexer_peek_token (parser->lexer);
10484
10485 /* Let the semantic analysis code know that we are starting the
10486 mem-initializer-list. */
10487 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10488 error_at (token->location,
10489 "only constructors take member initializers");
10490
10491 /* Loop through the list. */
10492 while (true)
10493 {
10494 tree mem_initializer;
10495
10496 token = cp_lexer_peek_token (parser->lexer);
10497 /* Parse the mem-initializer. */
10498 mem_initializer = cp_parser_mem_initializer (parser);
10499 /* If the next token is a `...', we're expanding member initializers. */
10500 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10501 {
10502 /* Consume the `...'. */
10503 cp_lexer_consume_token (parser->lexer);
10504
10505 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10506 can be expanded but members cannot. */
10507 if (mem_initializer != error_mark_node
10508 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10509 {
10510 error_at (token->location,
10511 "cannot expand initializer for member %<%D%>",
10512 TREE_PURPOSE (mem_initializer));
10513 mem_initializer = error_mark_node;
10514 }
10515
10516 /* Construct the pack expansion type. */
10517 if (mem_initializer != error_mark_node)
10518 mem_initializer = make_pack_expansion (mem_initializer);
10519 }
10520 /* Add it to the list, unless it was erroneous. */
10521 if (mem_initializer != error_mark_node)
10522 {
10523 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10524 mem_initializer_list = mem_initializer;
10525 }
10526 /* If the next token is not a `,', we're done. */
10527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10528 break;
10529 /* Consume the `,' token. */
10530 cp_lexer_consume_token (parser->lexer);
10531 }
10532
10533 /* Perform semantic analysis. */
10534 if (DECL_CONSTRUCTOR_P (current_function_decl))
10535 finish_mem_initializers (mem_initializer_list);
10536 }
10537
10538 /* Parse a mem-initializer.
10539
10540 mem-initializer:
10541 mem-initializer-id ( expression-list [opt] )
10542 mem-initializer-id braced-init-list
10543
10544 GNU extension:
10545
10546 mem-initializer:
10547 ( expression-list [opt] )
10548
10549 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10550 class) or FIELD_DECL (for a non-static data member) to initialize;
10551 the TREE_VALUE is the expression-list. An empty initialization
10552 list is represented by void_list_node. */
10553
10554 static tree
10555 cp_parser_mem_initializer (cp_parser* parser)
10556 {
10557 tree mem_initializer_id;
10558 tree expression_list;
10559 tree member;
10560 cp_token *token = cp_lexer_peek_token (parser->lexer);
10561
10562 /* Find out what is being initialized. */
10563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10564 {
10565 permerror (token->location,
10566 "anachronistic old-style base class initializer");
10567 mem_initializer_id = NULL_TREE;
10568 }
10569 else
10570 {
10571 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10572 if (mem_initializer_id == error_mark_node)
10573 return mem_initializer_id;
10574 }
10575 member = expand_member_init (mem_initializer_id);
10576 if (member && !DECL_P (member))
10577 in_base_initializer = 1;
10578
10579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10580 {
10581 bool expr_non_constant_p;
10582 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10583 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10584 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10585 expression_list = build_tree_list (NULL_TREE, expression_list);
10586 }
10587 else
10588 {
10589 VEC(tree,gc)* vec;
10590 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10591 /*cast_p=*/false,
10592 /*allow_expansion_p=*/true,
10593 /*non_constant_p=*/NULL);
10594 if (vec == NULL)
10595 return error_mark_node;
10596 expression_list = build_tree_list_vec (vec);
10597 release_tree_vector (vec);
10598 }
10599
10600 if (expression_list == error_mark_node)
10601 return error_mark_node;
10602 if (!expression_list)
10603 expression_list = void_type_node;
10604
10605 in_base_initializer = 0;
10606
10607 return member ? build_tree_list (member, expression_list) : error_mark_node;
10608 }
10609
10610 /* Parse a mem-initializer-id.
10611
10612 mem-initializer-id:
10613 :: [opt] nested-name-specifier [opt] class-name
10614 identifier
10615
10616 Returns a TYPE indicating the class to be initializer for the first
10617 production. Returns an IDENTIFIER_NODE indicating the data member
10618 to be initialized for the second production. */
10619
10620 static tree
10621 cp_parser_mem_initializer_id (cp_parser* parser)
10622 {
10623 bool global_scope_p;
10624 bool nested_name_specifier_p;
10625 bool template_p = false;
10626 tree id;
10627
10628 cp_token *token = cp_lexer_peek_token (parser->lexer);
10629
10630 /* `typename' is not allowed in this context ([temp.res]). */
10631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10632 {
10633 error_at (token->location,
10634 "keyword %<typename%> not allowed in this context (a qualified "
10635 "member initializer is implicitly a type)");
10636 cp_lexer_consume_token (parser->lexer);
10637 }
10638 /* Look for the optional `::' operator. */
10639 global_scope_p
10640 = (cp_parser_global_scope_opt (parser,
10641 /*current_scope_valid_p=*/false)
10642 != NULL_TREE);
10643 /* Look for the optional nested-name-specifier. The simplest way to
10644 implement:
10645
10646 [temp.res]
10647
10648 The keyword `typename' is not permitted in a base-specifier or
10649 mem-initializer; in these contexts a qualified name that
10650 depends on a template-parameter is implicitly assumed to be a
10651 type name.
10652
10653 is to assume that we have seen the `typename' keyword at this
10654 point. */
10655 nested_name_specifier_p
10656 = (cp_parser_nested_name_specifier_opt (parser,
10657 /*typename_keyword_p=*/true,
10658 /*check_dependency_p=*/true,
10659 /*type_p=*/true,
10660 /*is_declaration=*/true)
10661 != NULL_TREE);
10662 if (nested_name_specifier_p)
10663 template_p = cp_parser_optional_template_keyword (parser);
10664 /* If there is a `::' operator or a nested-name-specifier, then we
10665 are definitely looking for a class-name. */
10666 if (global_scope_p || nested_name_specifier_p)
10667 return cp_parser_class_name (parser,
10668 /*typename_keyword_p=*/true,
10669 /*template_keyword_p=*/template_p,
10670 typename_type,
10671 /*check_dependency_p=*/true,
10672 /*class_head_p=*/false,
10673 /*is_declaration=*/true);
10674 /* Otherwise, we could also be looking for an ordinary identifier. */
10675 cp_parser_parse_tentatively (parser);
10676 /* Try a class-name. */
10677 id = cp_parser_class_name (parser,
10678 /*typename_keyword_p=*/true,
10679 /*template_keyword_p=*/false,
10680 none_type,
10681 /*check_dependency_p=*/true,
10682 /*class_head_p=*/false,
10683 /*is_declaration=*/true);
10684 /* If we found one, we're done. */
10685 if (cp_parser_parse_definitely (parser))
10686 return id;
10687 /* Otherwise, look for an ordinary identifier. */
10688 return cp_parser_identifier (parser);
10689 }
10690
10691 /* Overloading [gram.over] */
10692
10693 /* Parse an operator-function-id.
10694
10695 operator-function-id:
10696 operator operator
10697
10698 Returns an IDENTIFIER_NODE for the operator which is a
10699 human-readable spelling of the identifier, e.g., `operator +'. */
10700
10701 static tree
10702 cp_parser_operator_function_id (cp_parser* parser)
10703 {
10704 /* Look for the `operator' keyword. */
10705 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10706 return error_mark_node;
10707 /* And then the name of the operator itself. */
10708 return cp_parser_operator (parser);
10709 }
10710
10711 /* Parse an operator.
10712
10713 operator:
10714 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10715 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10716 || ++ -- , ->* -> () []
10717
10718 GNU Extensions:
10719
10720 operator:
10721 <? >? <?= >?=
10722
10723 Returns an IDENTIFIER_NODE for the operator which is a
10724 human-readable spelling of the identifier, e.g., `operator +'. */
10725
10726 static tree
10727 cp_parser_operator (cp_parser* parser)
10728 {
10729 tree id = NULL_TREE;
10730 cp_token *token;
10731
10732 /* Peek at the next token. */
10733 token = cp_lexer_peek_token (parser->lexer);
10734 /* Figure out which operator we have. */
10735 switch (token->type)
10736 {
10737 case CPP_KEYWORD:
10738 {
10739 enum tree_code op;
10740
10741 /* The keyword should be either `new' or `delete'. */
10742 if (token->keyword == RID_NEW)
10743 op = NEW_EXPR;
10744 else if (token->keyword == RID_DELETE)
10745 op = DELETE_EXPR;
10746 else
10747 break;
10748
10749 /* Consume the `new' or `delete' token. */
10750 cp_lexer_consume_token (parser->lexer);
10751
10752 /* Peek at the next token. */
10753 token = cp_lexer_peek_token (parser->lexer);
10754 /* If it's a `[' token then this is the array variant of the
10755 operator. */
10756 if (token->type == CPP_OPEN_SQUARE)
10757 {
10758 /* Consume the `[' token. */
10759 cp_lexer_consume_token (parser->lexer);
10760 /* Look for the `]' token. */
10761 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10762 id = ansi_opname (op == NEW_EXPR
10763 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10764 }
10765 /* Otherwise, we have the non-array variant. */
10766 else
10767 id = ansi_opname (op);
10768
10769 return id;
10770 }
10771
10772 case CPP_PLUS:
10773 id = ansi_opname (PLUS_EXPR);
10774 break;
10775
10776 case CPP_MINUS:
10777 id = ansi_opname (MINUS_EXPR);
10778 break;
10779
10780 case CPP_MULT:
10781 id = ansi_opname (MULT_EXPR);
10782 break;
10783
10784 case CPP_DIV:
10785 id = ansi_opname (TRUNC_DIV_EXPR);
10786 break;
10787
10788 case CPP_MOD:
10789 id = ansi_opname (TRUNC_MOD_EXPR);
10790 break;
10791
10792 case CPP_XOR:
10793 id = ansi_opname (BIT_XOR_EXPR);
10794 break;
10795
10796 case CPP_AND:
10797 id = ansi_opname (BIT_AND_EXPR);
10798 break;
10799
10800 case CPP_OR:
10801 id = ansi_opname (BIT_IOR_EXPR);
10802 break;
10803
10804 case CPP_COMPL:
10805 id = ansi_opname (BIT_NOT_EXPR);
10806 break;
10807
10808 case CPP_NOT:
10809 id = ansi_opname (TRUTH_NOT_EXPR);
10810 break;
10811
10812 case CPP_EQ:
10813 id = ansi_assopname (NOP_EXPR);
10814 break;
10815
10816 case CPP_LESS:
10817 id = ansi_opname (LT_EXPR);
10818 break;
10819
10820 case CPP_GREATER:
10821 id = ansi_opname (GT_EXPR);
10822 break;
10823
10824 case CPP_PLUS_EQ:
10825 id = ansi_assopname (PLUS_EXPR);
10826 break;
10827
10828 case CPP_MINUS_EQ:
10829 id = ansi_assopname (MINUS_EXPR);
10830 break;
10831
10832 case CPP_MULT_EQ:
10833 id = ansi_assopname (MULT_EXPR);
10834 break;
10835
10836 case CPP_DIV_EQ:
10837 id = ansi_assopname (TRUNC_DIV_EXPR);
10838 break;
10839
10840 case CPP_MOD_EQ:
10841 id = ansi_assopname (TRUNC_MOD_EXPR);
10842 break;
10843
10844 case CPP_XOR_EQ:
10845 id = ansi_assopname (BIT_XOR_EXPR);
10846 break;
10847
10848 case CPP_AND_EQ:
10849 id = ansi_assopname (BIT_AND_EXPR);
10850 break;
10851
10852 case CPP_OR_EQ:
10853 id = ansi_assopname (BIT_IOR_EXPR);
10854 break;
10855
10856 case CPP_LSHIFT:
10857 id = ansi_opname (LSHIFT_EXPR);
10858 break;
10859
10860 case CPP_RSHIFT:
10861 id = ansi_opname (RSHIFT_EXPR);
10862 break;
10863
10864 case CPP_LSHIFT_EQ:
10865 id = ansi_assopname (LSHIFT_EXPR);
10866 break;
10867
10868 case CPP_RSHIFT_EQ:
10869 id = ansi_assopname (RSHIFT_EXPR);
10870 break;
10871
10872 case CPP_EQ_EQ:
10873 id = ansi_opname (EQ_EXPR);
10874 break;
10875
10876 case CPP_NOT_EQ:
10877 id = ansi_opname (NE_EXPR);
10878 break;
10879
10880 case CPP_LESS_EQ:
10881 id = ansi_opname (LE_EXPR);
10882 break;
10883
10884 case CPP_GREATER_EQ:
10885 id = ansi_opname (GE_EXPR);
10886 break;
10887
10888 case CPP_AND_AND:
10889 id = ansi_opname (TRUTH_ANDIF_EXPR);
10890 break;
10891
10892 case CPP_OR_OR:
10893 id = ansi_opname (TRUTH_ORIF_EXPR);
10894 break;
10895
10896 case CPP_PLUS_PLUS:
10897 id = ansi_opname (POSTINCREMENT_EXPR);
10898 break;
10899
10900 case CPP_MINUS_MINUS:
10901 id = ansi_opname (PREDECREMENT_EXPR);
10902 break;
10903
10904 case CPP_COMMA:
10905 id = ansi_opname (COMPOUND_EXPR);
10906 break;
10907
10908 case CPP_DEREF_STAR:
10909 id = ansi_opname (MEMBER_REF);
10910 break;
10911
10912 case CPP_DEREF:
10913 id = ansi_opname (COMPONENT_REF);
10914 break;
10915
10916 case CPP_OPEN_PAREN:
10917 /* Consume the `('. */
10918 cp_lexer_consume_token (parser->lexer);
10919 /* Look for the matching `)'. */
10920 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10921 return ansi_opname (CALL_EXPR);
10922
10923 case CPP_OPEN_SQUARE:
10924 /* Consume the `['. */
10925 cp_lexer_consume_token (parser->lexer);
10926 /* Look for the matching `]'. */
10927 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10928 return ansi_opname (ARRAY_REF);
10929
10930 default:
10931 /* Anything else is an error. */
10932 break;
10933 }
10934
10935 /* If we have selected an identifier, we need to consume the
10936 operator token. */
10937 if (id)
10938 cp_lexer_consume_token (parser->lexer);
10939 /* Otherwise, no valid operator name was present. */
10940 else
10941 {
10942 cp_parser_error (parser, "expected operator");
10943 id = error_mark_node;
10944 }
10945
10946 return id;
10947 }
10948
10949 /* Parse a template-declaration.
10950
10951 template-declaration:
10952 export [opt] template < template-parameter-list > declaration
10953
10954 If MEMBER_P is TRUE, this template-declaration occurs within a
10955 class-specifier.
10956
10957 The grammar rule given by the standard isn't correct. What
10958 is really meant is:
10959
10960 template-declaration:
10961 export [opt] template-parameter-list-seq
10962 decl-specifier-seq [opt] init-declarator [opt] ;
10963 export [opt] template-parameter-list-seq
10964 function-definition
10965
10966 template-parameter-list-seq:
10967 template-parameter-list-seq [opt]
10968 template < template-parameter-list > */
10969
10970 static void
10971 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10972 {
10973 /* Check for `export'. */
10974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10975 {
10976 /* Consume the `export' token. */
10977 cp_lexer_consume_token (parser->lexer);
10978 /* Warn that we do not support `export'. */
10979 warning (0, "keyword %<export%> not implemented, and will be ignored");
10980 }
10981
10982 cp_parser_template_declaration_after_export (parser, member_p);
10983 }
10984
10985 /* Parse a template-parameter-list.
10986
10987 template-parameter-list:
10988 template-parameter
10989 template-parameter-list , template-parameter
10990
10991 Returns a TREE_LIST. Each node represents a template parameter.
10992 The nodes are connected via their TREE_CHAINs. */
10993
10994 static tree
10995 cp_parser_template_parameter_list (cp_parser* parser)
10996 {
10997 tree parameter_list = NULL_TREE;
10998
10999 begin_template_parm_list ();
11000
11001 /* The loop below parses the template parms. We first need to know
11002 the total number of template parms to be able to compute proper
11003 canonical types of each dependent type. So after the loop, when
11004 we know the total number of template parms,
11005 end_template_parm_list computes the proper canonical types and
11006 fixes up the dependent types accordingly. */
11007 while (true)
11008 {
11009 tree parameter;
11010 bool is_non_type;
11011 bool is_parameter_pack;
11012 location_t parm_loc;
11013
11014 /* Parse the template-parameter. */
11015 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11016 parameter = cp_parser_template_parameter (parser,
11017 &is_non_type,
11018 &is_parameter_pack);
11019 /* Add it to the list. */
11020 if (parameter != error_mark_node)
11021 parameter_list = process_template_parm (parameter_list,
11022 parm_loc,
11023 parameter,
11024 is_non_type,
11025 is_parameter_pack,
11026 0);
11027 else
11028 {
11029 tree err_parm = build_tree_list (parameter, parameter);
11030 parameter_list = chainon (parameter_list, err_parm);
11031 }
11032
11033 /* If the next token is not a `,', we're done. */
11034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11035 break;
11036 /* Otherwise, consume the `,' token. */
11037 cp_lexer_consume_token (parser->lexer);
11038 }
11039
11040 return end_template_parm_list (parameter_list);
11041 }
11042
11043 /* Parse a template-parameter.
11044
11045 template-parameter:
11046 type-parameter
11047 parameter-declaration
11048
11049 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11050 the parameter. The TREE_PURPOSE is the default value, if any.
11051 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11052 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11053 set to true iff this parameter is a parameter pack. */
11054
11055 static tree
11056 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11057 bool *is_parameter_pack)
11058 {
11059 cp_token *token;
11060 cp_parameter_declarator *parameter_declarator;
11061 cp_declarator *id_declarator;
11062 tree parm;
11063
11064 /* Assume it is a type parameter or a template parameter. */
11065 *is_non_type = false;
11066 /* Assume it not a parameter pack. */
11067 *is_parameter_pack = false;
11068 /* Peek at the next token. */
11069 token = cp_lexer_peek_token (parser->lexer);
11070 /* If it is `class' or `template', we have a type-parameter. */
11071 if (token->keyword == RID_TEMPLATE)
11072 return cp_parser_type_parameter (parser, is_parameter_pack);
11073 /* If it is `class' or `typename' we do not know yet whether it is a
11074 type parameter or a non-type parameter. Consider:
11075
11076 template <typename T, typename T::X X> ...
11077
11078 or:
11079
11080 template <class C, class D*> ...
11081
11082 Here, the first parameter is a type parameter, and the second is
11083 a non-type parameter. We can tell by looking at the token after
11084 the identifier -- if it is a `,', `=', or `>' then we have a type
11085 parameter. */
11086 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11087 {
11088 /* Peek at the token after `class' or `typename'. */
11089 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11090 /* If it's an ellipsis, we have a template type parameter
11091 pack. */
11092 if (token->type == CPP_ELLIPSIS)
11093 return cp_parser_type_parameter (parser, is_parameter_pack);
11094 /* If it's an identifier, skip it. */
11095 if (token->type == CPP_NAME)
11096 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11097 /* Now, see if the token looks like the end of a template
11098 parameter. */
11099 if (token->type == CPP_COMMA
11100 || token->type == CPP_EQ
11101 || token->type == CPP_GREATER)
11102 return cp_parser_type_parameter (parser, is_parameter_pack);
11103 }
11104
11105 /* Otherwise, it is a non-type parameter.
11106
11107 [temp.param]
11108
11109 When parsing a default template-argument for a non-type
11110 template-parameter, the first non-nested `>' is taken as the end
11111 of the template parameter-list rather than a greater-than
11112 operator. */
11113 *is_non_type = true;
11114 parameter_declarator
11115 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11116 /*parenthesized_p=*/NULL);
11117
11118 /* If the parameter declaration is marked as a parameter pack, set
11119 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11120 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11121 grokdeclarator. */
11122 if (parameter_declarator
11123 && parameter_declarator->declarator
11124 && parameter_declarator->declarator->parameter_pack_p)
11125 {
11126 *is_parameter_pack = true;
11127 parameter_declarator->declarator->parameter_pack_p = false;
11128 }
11129
11130 /* If the next token is an ellipsis, and we don't already have it
11131 marked as a parameter pack, then we have a parameter pack (that
11132 has no declarator). */
11133 if (!*is_parameter_pack
11134 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11135 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11136 {
11137 /* Consume the `...'. */
11138 cp_lexer_consume_token (parser->lexer);
11139 maybe_warn_variadic_templates ();
11140
11141 *is_parameter_pack = true;
11142 }
11143 /* We might end up with a pack expansion as the type of the non-type
11144 template parameter, in which case this is a non-type template
11145 parameter pack. */
11146 else if (parameter_declarator
11147 && parameter_declarator->decl_specifiers.type
11148 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11149 {
11150 *is_parameter_pack = true;
11151 parameter_declarator->decl_specifiers.type =
11152 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11153 }
11154
11155 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11156 {
11157 /* Parameter packs cannot have default arguments. However, a
11158 user may try to do so, so we'll parse them and give an
11159 appropriate diagnostic here. */
11160
11161 /* Consume the `='. */
11162 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11163 cp_lexer_consume_token (parser->lexer);
11164
11165 /* Find the name of the parameter pack. */
11166 id_declarator = parameter_declarator->declarator;
11167 while (id_declarator && id_declarator->kind != cdk_id)
11168 id_declarator = id_declarator->declarator;
11169
11170 if (id_declarator && id_declarator->kind == cdk_id)
11171 error_at (start_token->location,
11172 "template parameter pack %qD cannot have a default argument",
11173 id_declarator->u.id.unqualified_name);
11174 else
11175 error_at (start_token->location,
11176 "template parameter pack cannot have a default argument");
11177
11178 /* Parse the default argument, but throw away the result. */
11179 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11180 }
11181
11182 parm = grokdeclarator (parameter_declarator->declarator,
11183 &parameter_declarator->decl_specifiers,
11184 TPARM, /*initialized=*/0,
11185 /*attrlist=*/NULL);
11186 if (parm == error_mark_node)
11187 return error_mark_node;
11188
11189 return build_tree_list (parameter_declarator->default_argument, parm);
11190 }
11191
11192 /* Parse a type-parameter.
11193
11194 type-parameter:
11195 class identifier [opt]
11196 class identifier [opt] = type-id
11197 typename identifier [opt]
11198 typename identifier [opt] = type-id
11199 template < template-parameter-list > class identifier [opt]
11200 template < template-parameter-list > class identifier [opt]
11201 = id-expression
11202
11203 GNU Extension (variadic templates):
11204
11205 type-parameter:
11206 class ... identifier [opt]
11207 typename ... identifier [opt]
11208
11209 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11210 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11211 the declaration of the parameter.
11212
11213 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11214
11215 static tree
11216 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11217 {
11218 cp_token *token;
11219 tree parameter;
11220
11221 /* Look for a keyword to tell us what kind of parameter this is. */
11222 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11223 if (!token)
11224 return error_mark_node;
11225
11226 switch (token->keyword)
11227 {
11228 case RID_CLASS:
11229 case RID_TYPENAME:
11230 {
11231 tree identifier;
11232 tree default_argument;
11233
11234 /* If the next token is an ellipsis, we have a template
11235 argument pack. */
11236 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11237 {
11238 /* Consume the `...' token. */
11239 cp_lexer_consume_token (parser->lexer);
11240 maybe_warn_variadic_templates ();
11241
11242 *is_parameter_pack = true;
11243 }
11244
11245 /* If the next token is an identifier, then it names the
11246 parameter. */
11247 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11248 identifier = cp_parser_identifier (parser);
11249 else
11250 identifier = NULL_TREE;
11251
11252 /* Create the parameter. */
11253 parameter = finish_template_type_parm (class_type_node, identifier);
11254
11255 /* If the next token is an `=', we have a default argument. */
11256 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11257 {
11258 /* Consume the `=' token. */
11259 cp_lexer_consume_token (parser->lexer);
11260 /* Parse the default-argument. */
11261 push_deferring_access_checks (dk_no_deferred);
11262 default_argument = cp_parser_type_id (parser);
11263
11264 /* Template parameter packs cannot have default
11265 arguments. */
11266 if (*is_parameter_pack)
11267 {
11268 if (identifier)
11269 error_at (token->location,
11270 "template parameter pack %qD cannot have a "
11271 "default argument", identifier);
11272 else
11273 error_at (token->location,
11274 "template parameter packs cannot have "
11275 "default arguments");
11276 default_argument = NULL_TREE;
11277 }
11278 pop_deferring_access_checks ();
11279 }
11280 else
11281 default_argument = NULL_TREE;
11282
11283 /* Create the combined representation of the parameter and the
11284 default argument. */
11285 parameter = build_tree_list (default_argument, parameter);
11286 }
11287 break;
11288
11289 case RID_TEMPLATE:
11290 {
11291 tree identifier;
11292 tree default_argument;
11293
11294 /* Look for the `<'. */
11295 cp_parser_require (parser, CPP_LESS, RT_LESS);
11296 /* Parse the template-parameter-list. */
11297 cp_parser_template_parameter_list (parser);
11298 /* Look for the `>'. */
11299 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11300 /* Look for the `class' keyword. */
11301 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11302 /* If the next token is an ellipsis, we have a template
11303 argument pack. */
11304 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11305 {
11306 /* Consume the `...' token. */
11307 cp_lexer_consume_token (parser->lexer);
11308 maybe_warn_variadic_templates ();
11309
11310 *is_parameter_pack = true;
11311 }
11312 /* If the next token is an `=', then there is a
11313 default-argument. If the next token is a `>', we are at
11314 the end of the parameter-list. If the next token is a `,',
11315 then we are at the end of this parameter. */
11316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11317 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11318 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11319 {
11320 identifier = cp_parser_identifier (parser);
11321 /* Treat invalid names as if the parameter were nameless. */
11322 if (identifier == error_mark_node)
11323 identifier = NULL_TREE;
11324 }
11325 else
11326 identifier = NULL_TREE;
11327
11328 /* Create the template parameter. */
11329 parameter = finish_template_template_parm (class_type_node,
11330 identifier);
11331
11332 /* If the next token is an `=', then there is a
11333 default-argument. */
11334 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11335 {
11336 bool is_template;
11337
11338 /* Consume the `='. */
11339 cp_lexer_consume_token (parser->lexer);
11340 /* Parse the id-expression. */
11341 push_deferring_access_checks (dk_no_deferred);
11342 /* save token before parsing the id-expression, for error
11343 reporting */
11344 token = cp_lexer_peek_token (parser->lexer);
11345 default_argument
11346 = cp_parser_id_expression (parser,
11347 /*template_keyword_p=*/false,
11348 /*check_dependency_p=*/true,
11349 /*template_p=*/&is_template,
11350 /*declarator_p=*/false,
11351 /*optional_p=*/false);
11352 if (TREE_CODE (default_argument) == TYPE_DECL)
11353 /* If the id-expression was a template-id that refers to
11354 a template-class, we already have the declaration here,
11355 so no further lookup is needed. */
11356 ;
11357 else
11358 /* Look up the name. */
11359 default_argument
11360 = cp_parser_lookup_name (parser, default_argument,
11361 none_type,
11362 /*is_template=*/is_template,
11363 /*is_namespace=*/false,
11364 /*check_dependency=*/true,
11365 /*ambiguous_decls=*/NULL,
11366 token->location);
11367 /* See if the default argument is valid. */
11368 default_argument
11369 = check_template_template_default_arg (default_argument);
11370
11371 /* Template parameter packs cannot have default
11372 arguments. */
11373 if (*is_parameter_pack)
11374 {
11375 if (identifier)
11376 error_at (token->location,
11377 "template parameter pack %qD cannot "
11378 "have a default argument",
11379 identifier);
11380 else
11381 error_at (token->location, "template parameter packs cannot "
11382 "have default arguments");
11383 default_argument = NULL_TREE;
11384 }
11385 pop_deferring_access_checks ();
11386 }
11387 else
11388 default_argument = NULL_TREE;
11389
11390 /* Create the combined representation of the parameter and the
11391 default argument. */
11392 parameter = build_tree_list (default_argument, parameter);
11393 }
11394 break;
11395
11396 default:
11397 gcc_unreachable ();
11398 break;
11399 }
11400
11401 return parameter;
11402 }
11403
11404 /* Parse a template-id.
11405
11406 template-id:
11407 template-name < template-argument-list [opt] >
11408
11409 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11410 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11411 returned. Otherwise, if the template-name names a function, or set
11412 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11413 names a class, returns a TYPE_DECL for the specialization.
11414
11415 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11416 uninstantiated templates. */
11417
11418 static tree
11419 cp_parser_template_id (cp_parser *parser,
11420 bool template_keyword_p,
11421 bool check_dependency_p,
11422 bool is_declaration)
11423 {
11424 int i;
11425 tree templ;
11426 tree arguments;
11427 tree template_id;
11428 cp_token_position start_of_id = 0;
11429 deferred_access_check *chk;
11430 VEC (deferred_access_check,gc) *access_check;
11431 cp_token *next_token = NULL, *next_token_2 = NULL;
11432 bool is_identifier;
11433
11434 /* If the next token corresponds to a template-id, there is no need
11435 to reparse it. */
11436 next_token = cp_lexer_peek_token (parser->lexer);
11437 if (next_token->type == CPP_TEMPLATE_ID)
11438 {
11439 struct tree_check *check_value;
11440
11441 /* Get the stored value. */
11442 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11443 /* Perform any access checks that were deferred. */
11444 access_check = check_value->checks;
11445 if (access_check)
11446 {
11447 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11448 perform_or_defer_access_check (chk->binfo,
11449 chk->decl,
11450 chk->diag_decl);
11451 }
11452 /* Return the stored value. */
11453 return check_value->value;
11454 }
11455
11456 /* Avoid performing name lookup if there is no possibility of
11457 finding a template-id. */
11458 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11459 || (next_token->type == CPP_NAME
11460 && !cp_parser_nth_token_starts_template_argument_list_p
11461 (parser, 2)))
11462 {
11463 cp_parser_error (parser, "expected template-id");
11464 return error_mark_node;
11465 }
11466
11467 /* Remember where the template-id starts. */
11468 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11469 start_of_id = cp_lexer_token_position (parser->lexer, false);
11470
11471 push_deferring_access_checks (dk_deferred);
11472
11473 /* Parse the template-name. */
11474 is_identifier = false;
11475 templ = cp_parser_template_name (parser, template_keyword_p,
11476 check_dependency_p,
11477 is_declaration,
11478 &is_identifier);
11479 if (templ == error_mark_node || is_identifier)
11480 {
11481 pop_deferring_access_checks ();
11482 return templ;
11483 }
11484
11485 /* If we find the sequence `[:' after a template-name, it's probably
11486 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11487 parse correctly the argument list. */
11488 next_token = cp_lexer_peek_token (parser->lexer);
11489 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11490 if (next_token->type == CPP_OPEN_SQUARE
11491 && next_token->flags & DIGRAPH
11492 && next_token_2->type == CPP_COLON
11493 && !(next_token_2->flags & PREV_WHITE))
11494 {
11495 cp_parser_parse_tentatively (parser);
11496 /* Change `:' into `::'. */
11497 next_token_2->type = CPP_SCOPE;
11498 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11499 CPP_LESS. */
11500 cp_lexer_consume_token (parser->lexer);
11501
11502 /* Parse the arguments. */
11503 arguments = cp_parser_enclosed_template_argument_list (parser);
11504 if (!cp_parser_parse_definitely (parser))
11505 {
11506 /* If we couldn't parse an argument list, then we revert our changes
11507 and return simply an error. Maybe this is not a template-id
11508 after all. */
11509 next_token_2->type = CPP_COLON;
11510 cp_parser_error (parser, "expected %<<%>");
11511 pop_deferring_access_checks ();
11512 return error_mark_node;
11513 }
11514 /* Otherwise, emit an error about the invalid digraph, but continue
11515 parsing because we got our argument list. */
11516 if (permerror (next_token->location,
11517 "%<<::%> cannot begin a template-argument list"))
11518 {
11519 static bool hint = false;
11520 inform (next_token->location,
11521 "%<<:%> is an alternate spelling for %<[%>."
11522 " Insert whitespace between %<<%> and %<::%>");
11523 if (!hint && !flag_permissive)
11524 {
11525 inform (next_token->location, "(if you use %<-fpermissive%>"
11526 " G++ will accept your code)");
11527 hint = true;
11528 }
11529 }
11530 }
11531 else
11532 {
11533 /* Look for the `<' that starts the template-argument-list. */
11534 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11535 {
11536 pop_deferring_access_checks ();
11537 return error_mark_node;
11538 }
11539 /* Parse the arguments. */
11540 arguments = cp_parser_enclosed_template_argument_list (parser);
11541 }
11542
11543 /* Build a representation of the specialization. */
11544 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11545 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11546 else if (DECL_CLASS_TEMPLATE_P (templ)
11547 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11548 {
11549 bool entering_scope;
11550 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11551 template (rather than some instantiation thereof) only if
11552 is not nested within some other construct. For example, in
11553 "template <typename T> void f(T) { A<T>::", A<T> is just an
11554 instantiation of A. */
11555 entering_scope = (template_parm_scope_p ()
11556 && cp_lexer_next_token_is (parser->lexer,
11557 CPP_SCOPE));
11558 template_id
11559 = finish_template_type (templ, arguments, entering_scope);
11560 }
11561 else
11562 {
11563 /* If it's not a class-template or a template-template, it should be
11564 a function-template. */
11565 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11566 || TREE_CODE (templ) == OVERLOAD
11567 || BASELINK_P (templ)));
11568
11569 template_id = lookup_template_function (templ, arguments);
11570 }
11571
11572 /* If parsing tentatively, replace the sequence of tokens that makes
11573 up the template-id with a CPP_TEMPLATE_ID token. That way,
11574 should we re-parse the token stream, we will not have to repeat
11575 the effort required to do the parse, nor will we issue duplicate
11576 error messages about problems during instantiation of the
11577 template. */
11578 if (start_of_id)
11579 {
11580 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11581
11582 /* Reset the contents of the START_OF_ID token. */
11583 token->type = CPP_TEMPLATE_ID;
11584 /* Retrieve any deferred checks. Do not pop this access checks yet
11585 so the memory will not be reclaimed during token replacing below. */
11586 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11587 token->u.tree_check_value->value = template_id;
11588 token->u.tree_check_value->checks = get_deferred_access_checks ();
11589 token->keyword = RID_MAX;
11590
11591 /* Purge all subsequent tokens. */
11592 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11593
11594 /* ??? Can we actually assume that, if template_id ==
11595 error_mark_node, we will have issued a diagnostic to the
11596 user, as opposed to simply marking the tentative parse as
11597 failed? */
11598 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11599 error_at (token->location, "parse error in template argument list");
11600 }
11601
11602 pop_deferring_access_checks ();
11603 return template_id;
11604 }
11605
11606 /* Parse a template-name.
11607
11608 template-name:
11609 identifier
11610
11611 The standard should actually say:
11612
11613 template-name:
11614 identifier
11615 operator-function-id
11616
11617 A defect report has been filed about this issue.
11618
11619 A conversion-function-id cannot be a template name because they cannot
11620 be part of a template-id. In fact, looking at this code:
11621
11622 a.operator K<int>()
11623
11624 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11625 It is impossible to call a templated conversion-function-id with an
11626 explicit argument list, since the only allowed template parameter is
11627 the type to which it is converting.
11628
11629 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11630 `template' keyword, in a construction like:
11631
11632 T::template f<3>()
11633
11634 In that case `f' is taken to be a template-name, even though there
11635 is no way of knowing for sure.
11636
11637 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11638 name refers to a set of overloaded functions, at least one of which
11639 is a template, or an IDENTIFIER_NODE with the name of the template,
11640 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11641 names are looked up inside uninstantiated templates. */
11642
11643 static tree
11644 cp_parser_template_name (cp_parser* parser,
11645 bool template_keyword_p,
11646 bool check_dependency_p,
11647 bool is_declaration,
11648 bool *is_identifier)
11649 {
11650 tree identifier;
11651 tree decl;
11652 tree fns;
11653 cp_token *token = cp_lexer_peek_token (parser->lexer);
11654
11655 /* If the next token is `operator', then we have either an
11656 operator-function-id or a conversion-function-id. */
11657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11658 {
11659 /* We don't know whether we're looking at an
11660 operator-function-id or a conversion-function-id. */
11661 cp_parser_parse_tentatively (parser);
11662 /* Try an operator-function-id. */
11663 identifier = cp_parser_operator_function_id (parser);
11664 /* If that didn't work, try a conversion-function-id. */
11665 if (!cp_parser_parse_definitely (parser))
11666 {
11667 cp_parser_error (parser, "expected template-name");
11668 return error_mark_node;
11669 }
11670 }
11671 /* Look for the identifier. */
11672 else
11673 identifier = cp_parser_identifier (parser);
11674
11675 /* If we didn't find an identifier, we don't have a template-id. */
11676 if (identifier == error_mark_node)
11677 return error_mark_node;
11678
11679 /* If the name immediately followed the `template' keyword, then it
11680 is a template-name. However, if the next token is not `<', then
11681 we do not treat it as a template-name, since it is not being used
11682 as part of a template-id. This enables us to handle constructs
11683 like:
11684
11685 template <typename T> struct S { S(); };
11686 template <typename T> S<T>::S();
11687
11688 correctly. We would treat `S' as a template -- if it were `S<T>'
11689 -- but we do not if there is no `<'. */
11690
11691 if (processing_template_decl
11692 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11693 {
11694 /* In a declaration, in a dependent context, we pretend that the
11695 "template" keyword was present in order to improve error
11696 recovery. For example, given:
11697
11698 template <typename T> void f(T::X<int>);
11699
11700 we want to treat "X<int>" as a template-id. */
11701 if (is_declaration
11702 && !template_keyword_p
11703 && parser->scope && TYPE_P (parser->scope)
11704 && check_dependency_p
11705 && dependent_scope_p (parser->scope)
11706 /* Do not do this for dtors (or ctors), since they never
11707 need the template keyword before their name. */
11708 && !constructor_name_p (identifier, parser->scope))
11709 {
11710 cp_token_position start = 0;
11711
11712 /* Explain what went wrong. */
11713 error_at (token->location, "non-template %qD used as template",
11714 identifier);
11715 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11716 parser->scope, identifier);
11717 /* If parsing tentatively, find the location of the "<" token. */
11718 if (cp_parser_simulate_error (parser))
11719 start = cp_lexer_token_position (parser->lexer, true);
11720 /* Parse the template arguments so that we can issue error
11721 messages about them. */
11722 cp_lexer_consume_token (parser->lexer);
11723 cp_parser_enclosed_template_argument_list (parser);
11724 /* Skip tokens until we find a good place from which to
11725 continue parsing. */
11726 cp_parser_skip_to_closing_parenthesis (parser,
11727 /*recovering=*/true,
11728 /*or_comma=*/true,
11729 /*consume_paren=*/false);
11730 /* If parsing tentatively, permanently remove the
11731 template argument list. That will prevent duplicate
11732 error messages from being issued about the missing
11733 "template" keyword. */
11734 if (start)
11735 cp_lexer_purge_tokens_after (parser->lexer, start);
11736 if (is_identifier)
11737 *is_identifier = true;
11738 return identifier;
11739 }
11740
11741 /* If the "template" keyword is present, then there is generally
11742 no point in doing name-lookup, so we just return IDENTIFIER.
11743 But, if the qualifying scope is non-dependent then we can
11744 (and must) do name-lookup normally. */
11745 if (template_keyword_p
11746 && (!parser->scope
11747 || (TYPE_P (parser->scope)
11748 && dependent_type_p (parser->scope))))
11749 return identifier;
11750 }
11751
11752 /* Look up the name. */
11753 decl = cp_parser_lookup_name (parser, identifier,
11754 none_type,
11755 /*is_template=*/true,
11756 /*is_namespace=*/false,
11757 check_dependency_p,
11758 /*ambiguous_decls=*/NULL,
11759 token->location);
11760
11761 /* If DECL is a template, then the name was a template-name. */
11762 if (TREE_CODE (decl) == TEMPLATE_DECL)
11763 ;
11764 else
11765 {
11766 tree fn = NULL_TREE;
11767
11768 /* The standard does not explicitly indicate whether a name that
11769 names a set of overloaded declarations, some of which are
11770 templates, is a template-name. However, such a name should
11771 be a template-name; otherwise, there is no way to form a
11772 template-id for the overloaded templates. */
11773 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11774 if (TREE_CODE (fns) == OVERLOAD)
11775 for (fn = fns; fn; fn = OVL_NEXT (fn))
11776 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11777 break;
11778
11779 if (!fn)
11780 {
11781 /* The name does not name a template. */
11782 cp_parser_error (parser, "expected template-name");
11783 return error_mark_node;
11784 }
11785 }
11786
11787 /* If DECL is dependent, and refers to a function, then just return
11788 its name; we will look it up again during template instantiation. */
11789 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11790 {
11791 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11792 if (TYPE_P (scope) && dependent_type_p (scope))
11793 return identifier;
11794 }
11795
11796 return decl;
11797 }
11798
11799 /* Parse a template-argument-list.
11800
11801 template-argument-list:
11802 template-argument ... [opt]
11803 template-argument-list , template-argument ... [opt]
11804
11805 Returns a TREE_VEC containing the arguments. */
11806
11807 static tree
11808 cp_parser_template_argument_list (cp_parser* parser)
11809 {
11810 tree fixed_args[10];
11811 unsigned n_args = 0;
11812 unsigned alloced = 10;
11813 tree *arg_ary = fixed_args;
11814 tree vec;
11815 bool saved_in_template_argument_list_p;
11816 bool saved_ice_p;
11817 bool saved_non_ice_p;
11818
11819 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11820 parser->in_template_argument_list_p = true;
11821 /* Even if the template-id appears in an integral
11822 constant-expression, the contents of the argument list do
11823 not. */
11824 saved_ice_p = parser->integral_constant_expression_p;
11825 parser->integral_constant_expression_p = false;
11826 saved_non_ice_p = parser->non_integral_constant_expression_p;
11827 parser->non_integral_constant_expression_p = false;
11828 /* Parse the arguments. */
11829 do
11830 {
11831 tree argument;
11832
11833 if (n_args)
11834 /* Consume the comma. */
11835 cp_lexer_consume_token (parser->lexer);
11836
11837 /* Parse the template-argument. */
11838 argument = cp_parser_template_argument (parser);
11839
11840 /* If the next token is an ellipsis, we're expanding a template
11841 argument pack. */
11842 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11843 {
11844 if (argument == error_mark_node)
11845 {
11846 cp_token *token = cp_lexer_peek_token (parser->lexer);
11847 error_at (token->location,
11848 "expected parameter pack before %<...%>");
11849 }
11850 /* Consume the `...' token. */
11851 cp_lexer_consume_token (parser->lexer);
11852
11853 /* Make the argument into a TYPE_PACK_EXPANSION or
11854 EXPR_PACK_EXPANSION. */
11855 argument = make_pack_expansion (argument);
11856 }
11857
11858 if (n_args == alloced)
11859 {
11860 alloced *= 2;
11861
11862 if (arg_ary == fixed_args)
11863 {
11864 arg_ary = XNEWVEC (tree, alloced);
11865 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11866 }
11867 else
11868 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11869 }
11870 arg_ary[n_args++] = argument;
11871 }
11872 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11873
11874 vec = make_tree_vec (n_args);
11875
11876 while (n_args--)
11877 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11878
11879 if (arg_ary != fixed_args)
11880 free (arg_ary);
11881 parser->non_integral_constant_expression_p = saved_non_ice_p;
11882 parser->integral_constant_expression_p = saved_ice_p;
11883 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11884 #ifdef ENABLE_CHECKING
11885 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11886 #endif
11887 return vec;
11888 }
11889
11890 /* Parse a template-argument.
11891
11892 template-argument:
11893 assignment-expression
11894 type-id
11895 id-expression
11896
11897 The representation is that of an assignment-expression, type-id, or
11898 id-expression -- except that the qualified id-expression is
11899 evaluated, so that the value returned is either a DECL or an
11900 OVERLOAD.
11901
11902 Although the standard says "assignment-expression", it forbids
11903 throw-expressions or assignments in the template argument.
11904 Therefore, we use "conditional-expression" instead. */
11905
11906 static tree
11907 cp_parser_template_argument (cp_parser* parser)
11908 {
11909 tree argument;
11910 bool template_p;
11911 bool address_p;
11912 bool maybe_type_id = false;
11913 cp_token *token = NULL, *argument_start_token = NULL;
11914 cp_id_kind idk;
11915
11916 /* There's really no way to know what we're looking at, so we just
11917 try each alternative in order.
11918
11919 [temp.arg]
11920
11921 In a template-argument, an ambiguity between a type-id and an
11922 expression is resolved to a type-id, regardless of the form of
11923 the corresponding template-parameter.
11924
11925 Therefore, we try a type-id first. */
11926 cp_parser_parse_tentatively (parser);
11927 argument = cp_parser_template_type_arg (parser);
11928 /* If there was no error parsing the type-id but the next token is a
11929 '>>', our behavior depends on which dialect of C++ we're
11930 parsing. In C++98, we probably found a typo for '> >'. But there
11931 are type-id which are also valid expressions. For instance:
11932
11933 struct X { int operator >> (int); };
11934 template <int V> struct Foo {};
11935 Foo<X () >> 5> r;
11936
11937 Here 'X()' is a valid type-id of a function type, but the user just
11938 wanted to write the expression "X() >> 5". Thus, we remember that we
11939 found a valid type-id, but we still try to parse the argument as an
11940 expression to see what happens.
11941
11942 In C++0x, the '>>' will be considered two separate '>'
11943 tokens. */
11944 if (!cp_parser_error_occurred (parser)
11945 && cxx_dialect == cxx98
11946 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11947 {
11948 maybe_type_id = true;
11949 cp_parser_abort_tentative_parse (parser);
11950 }
11951 else
11952 {
11953 /* If the next token isn't a `,' or a `>', then this argument wasn't
11954 really finished. This means that the argument is not a valid
11955 type-id. */
11956 if (!cp_parser_next_token_ends_template_argument_p (parser))
11957 cp_parser_error (parser, "expected template-argument");
11958 /* If that worked, we're done. */
11959 if (cp_parser_parse_definitely (parser))
11960 return argument;
11961 }
11962 /* We're still not sure what the argument will be. */
11963 cp_parser_parse_tentatively (parser);
11964 /* Try a template. */
11965 argument_start_token = cp_lexer_peek_token (parser->lexer);
11966 argument = cp_parser_id_expression (parser,
11967 /*template_keyword_p=*/false,
11968 /*check_dependency_p=*/true,
11969 &template_p,
11970 /*declarator_p=*/false,
11971 /*optional_p=*/false);
11972 /* If the next token isn't a `,' or a `>', then this argument wasn't
11973 really finished. */
11974 if (!cp_parser_next_token_ends_template_argument_p (parser))
11975 cp_parser_error (parser, "expected template-argument");
11976 if (!cp_parser_error_occurred (parser))
11977 {
11978 /* Figure out what is being referred to. If the id-expression
11979 was for a class template specialization, then we will have a
11980 TYPE_DECL at this point. There is no need to do name lookup
11981 at this point in that case. */
11982 if (TREE_CODE (argument) != TYPE_DECL)
11983 argument = cp_parser_lookup_name (parser, argument,
11984 none_type,
11985 /*is_template=*/template_p,
11986 /*is_namespace=*/false,
11987 /*check_dependency=*/true,
11988 /*ambiguous_decls=*/NULL,
11989 argument_start_token->location);
11990 if (TREE_CODE (argument) != TEMPLATE_DECL
11991 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11992 cp_parser_error (parser, "expected template-name");
11993 }
11994 if (cp_parser_parse_definitely (parser))
11995 return argument;
11996 /* It must be a non-type argument. There permitted cases are given
11997 in [temp.arg.nontype]:
11998
11999 -- an integral constant-expression of integral or enumeration
12000 type; or
12001
12002 -- the name of a non-type template-parameter; or
12003
12004 -- the name of an object or function with external linkage...
12005
12006 -- the address of an object or function with external linkage...
12007
12008 -- a pointer to member... */
12009 /* Look for a non-type template parameter. */
12010 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12011 {
12012 cp_parser_parse_tentatively (parser);
12013 argument = cp_parser_primary_expression (parser,
12014 /*address_p=*/false,
12015 /*cast_p=*/false,
12016 /*template_arg_p=*/true,
12017 &idk);
12018 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12019 || !cp_parser_next_token_ends_template_argument_p (parser))
12020 cp_parser_simulate_error (parser);
12021 if (cp_parser_parse_definitely (parser))
12022 return argument;
12023 }
12024
12025 /* If the next token is "&", the argument must be the address of an
12026 object or function with external linkage. */
12027 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12028 if (address_p)
12029 cp_lexer_consume_token (parser->lexer);
12030 /* See if we might have an id-expression. */
12031 token = cp_lexer_peek_token (parser->lexer);
12032 if (token->type == CPP_NAME
12033 || token->keyword == RID_OPERATOR
12034 || token->type == CPP_SCOPE
12035 || token->type == CPP_TEMPLATE_ID
12036 || token->type == CPP_NESTED_NAME_SPECIFIER)
12037 {
12038 cp_parser_parse_tentatively (parser);
12039 argument = cp_parser_primary_expression (parser,
12040 address_p,
12041 /*cast_p=*/false,
12042 /*template_arg_p=*/true,
12043 &idk);
12044 if (cp_parser_error_occurred (parser)
12045 || !cp_parser_next_token_ends_template_argument_p (parser))
12046 cp_parser_abort_tentative_parse (parser);
12047 else
12048 {
12049 tree probe;
12050
12051 if (TREE_CODE (argument) == INDIRECT_REF)
12052 {
12053 gcc_assert (REFERENCE_REF_P (argument));
12054 argument = TREE_OPERAND (argument, 0);
12055 }
12056
12057 /* If we're in a template, we represent a qualified-id referring
12058 to a static data member as a SCOPE_REF even if the scope isn't
12059 dependent so that we can check access control later. */
12060 probe = argument;
12061 if (TREE_CODE (probe) == SCOPE_REF)
12062 probe = TREE_OPERAND (probe, 1);
12063 if (TREE_CODE (probe) == VAR_DECL)
12064 {
12065 /* A variable without external linkage might still be a
12066 valid constant-expression, so no error is issued here
12067 if the external-linkage check fails. */
12068 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12069 cp_parser_simulate_error (parser);
12070 }
12071 else if (is_overloaded_fn (argument))
12072 /* All overloaded functions are allowed; if the external
12073 linkage test does not pass, an error will be issued
12074 later. */
12075 ;
12076 else if (address_p
12077 && (TREE_CODE (argument) == OFFSET_REF
12078 || TREE_CODE (argument) == SCOPE_REF))
12079 /* A pointer-to-member. */
12080 ;
12081 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12082 ;
12083 else
12084 cp_parser_simulate_error (parser);
12085
12086 if (cp_parser_parse_definitely (parser))
12087 {
12088 if (address_p)
12089 argument = build_x_unary_op (ADDR_EXPR, argument,
12090 tf_warning_or_error);
12091 return argument;
12092 }
12093 }
12094 }
12095 /* If the argument started with "&", there are no other valid
12096 alternatives at this point. */
12097 if (address_p)
12098 {
12099 cp_parser_error (parser, "invalid non-type template argument");
12100 return error_mark_node;
12101 }
12102
12103 /* If the argument wasn't successfully parsed as a type-id followed
12104 by '>>', the argument can only be a constant expression now.
12105 Otherwise, we try parsing the constant-expression tentatively,
12106 because the argument could really be a type-id. */
12107 if (maybe_type_id)
12108 cp_parser_parse_tentatively (parser);
12109 argument = cp_parser_constant_expression (parser,
12110 /*allow_non_constant_p=*/false,
12111 /*non_constant_p=*/NULL);
12112 argument = fold_non_dependent_expr (argument);
12113 if (!maybe_type_id)
12114 return argument;
12115 if (!cp_parser_next_token_ends_template_argument_p (parser))
12116 cp_parser_error (parser, "expected template-argument");
12117 if (cp_parser_parse_definitely (parser))
12118 return argument;
12119 /* We did our best to parse the argument as a non type-id, but that
12120 was the only alternative that matched (albeit with a '>' after
12121 it). We can assume it's just a typo from the user, and a
12122 diagnostic will then be issued. */
12123 return cp_parser_template_type_arg (parser);
12124 }
12125
12126 /* Parse an explicit-instantiation.
12127
12128 explicit-instantiation:
12129 template declaration
12130
12131 Although the standard says `declaration', what it really means is:
12132
12133 explicit-instantiation:
12134 template decl-specifier-seq [opt] declarator [opt] ;
12135
12136 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12137 supposed to be allowed. A defect report has been filed about this
12138 issue.
12139
12140 GNU Extension:
12141
12142 explicit-instantiation:
12143 storage-class-specifier template
12144 decl-specifier-seq [opt] declarator [opt] ;
12145 function-specifier template
12146 decl-specifier-seq [opt] declarator [opt] ; */
12147
12148 static void
12149 cp_parser_explicit_instantiation (cp_parser* parser)
12150 {
12151 int declares_class_or_enum;
12152 cp_decl_specifier_seq decl_specifiers;
12153 tree extension_specifier = NULL_TREE;
12154
12155 /* Look for an (optional) storage-class-specifier or
12156 function-specifier. */
12157 if (cp_parser_allow_gnu_extensions_p (parser))
12158 {
12159 extension_specifier
12160 = cp_parser_storage_class_specifier_opt (parser);
12161 if (!extension_specifier)
12162 extension_specifier
12163 = cp_parser_function_specifier_opt (parser,
12164 /*decl_specs=*/NULL);
12165 }
12166
12167 /* Look for the `template' keyword. */
12168 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12169 /* Let the front end know that we are processing an explicit
12170 instantiation. */
12171 begin_explicit_instantiation ();
12172 /* [temp.explicit] says that we are supposed to ignore access
12173 control while processing explicit instantiation directives. */
12174 push_deferring_access_checks (dk_no_check);
12175 /* Parse a decl-specifier-seq. */
12176 cp_parser_decl_specifier_seq (parser,
12177 CP_PARSER_FLAGS_OPTIONAL,
12178 &decl_specifiers,
12179 &declares_class_or_enum);
12180 /* If there was exactly one decl-specifier, and it declared a class,
12181 and there's no declarator, then we have an explicit type
12182 instantiation. */
12183 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12184 {
12185 tree type;
12186
12187 type = check_tag_decl (&decl_specifiers);
12188 /* Turn access control back on for names used during
12189 template instantiation. */
12190 pop_deferring_access_checks ();
12191 if (type)
12192 do_type_instantiation (type, extension_specifier,
12193 /*complain=*/tf_error);
12194 }
12195 else
12196 {
12197 cp_declarator *declarator;
12198 tree decl;
12199
12200 /* Parse the declarator. */
12201 declarator
12202 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12203 /*ctor_dtor_or_conv_p=*/NULL,
12204 /*parenthesized_p=*/NULL,
12205 /*member_p=*/false);
12206 if (declares_class_or_enum & 2)
12207 cp_parser_check_for_definition_in_return_type (declarator,
12208 decl_specifiers.type,
12209 decl_specifiers.type_location);
12210 if (declarator != cp_error_declarator)
12211 {
12212 if (decl_specifiers.specs[(int)ds_inline])
12213 permerror (input_location, "explicit instantiation shall not use"
12214 " %<inline%> specifier");
12215 if (decl_specifiers.specs[(int)ds_constexpr])
12216 permerror (input_location, "explicit instantiation shall not use"
12217 " %<constexpr%> specifier");
12218
12219 decl = grokdeclarator (declarator, &decl_specifiers,
12220 NORMAL, 0, &decl_specifiers.attributes);
12221 /* Turn access control back on for names used during
12222 template instantiation. */
12223 pop_deferring_access_checks ();
12224 /* Do the explicit instantiation. */
12225 do_decl_instantiation (decl, extension_specifier);
12226 }
12227 else
12228 {
12229 pop_deferring_access_checks ();
12230 /* Skip the body of the explicit instantiation. */
12231 cp_parser_skip_to_end_of_statement (parser);
12232 }
12233 }
12234 /* We're done with the instantiation. */
12235 end_explicit_instantiation ();
12236
12237 cp_parser_consume_semicolon_at_end_of_statement (parser);
12238 }
12239
12240 /* Parse an explicit-specialization.
12241
12242 explicit-specialization:
12243 template < > declaration
12244
12245 Although the standard says `declaration', what it really means is:
12246
12247 explicit-specialization:
12248 template <> decl-specifier [opt] init-declarator [opt] ;
12249 template <> function-definition
12250 template <> explicit-specialization
12251 template <> template-declaration */
12252
12253 static void
12254 cp_parser_explicit_specialization (cp_parser* parser)
12255 {
12256 bool need_lang_pop;
12257 cp_token *token = cp_lexer_peek_token (parser->lexer);
12258
12259 /* Look for the `template' keyword. */
12260 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12261 /* Look for the `<'. */
12262 cp_parser_require (parser, CPP_LESS, RT_LESS);
12263 /* Look for the `>'. */
12264 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12265 /* We have processed another parameter list. */
12266 ++parser->num_template_parameter_lists;
12267 /* [temp]
12268
12269 A template ... explicit specialization ... shall not have C
12270 linkage. */
12271 if (current_lang_name == lang_name_c)
12272 {
12273 error_at (token->location, "template specialization with C linkage");
12274 /* Give it C++ linkage to avoid confusing other parts of the
12275 front end. */
12276 push_lang_context (lang_name_cplusplus);
12277 need_lang_pop = true;
12278 }
12279 else
12280 need_lang_pop = false;
12281 /* Let the front end know that we are beginning a specialization. */
12282 if (!begin_specialization ())
12283 {
12284 end_specialization ();
12285 return;
12286 }
12287
12288 /* If the next keyword is `template', we need to figure out whether
12289 or not we're looking a template-declaration. */
12290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12291 {
12292 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12293 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12294 cp_parser_template_declaration_after_export (parser,
12295 /*member_p=*/false);
12296 else
12297 cp_parser_explicit_specialization (parser);
12298 }
12299 else
12300 /* Parse the dependent declaration. */
12301 cp_parser_single_declaration (parser,
12302 /*checks=*/NULL,
12303 /*member_p=*/false,
12304 /*explicit_specialization_p=*/true,
12305 /*friend_p=*/NULL);
12306 /* We're done with the specialization. */
12307 end_specialization ();
12308 /* For the erroneous case of a template with C linkage, we pushed an
12309 implicit C++ linkage scope; exit that scope now. */
12310 if (need_lang_pop)
12311 pop_lang_context ();
12312 /* We're done with this parameter list. */
12313 --parser->num_template_parameter_lists;
12314 }
12315
12316 /* Parse a type-specifier.
12317
12318 type-specifier:
12319 simple-type-specifier
12320 class-specifier
12321 enum-specifier
12322 elaborated-type-specifier
12323 cv-qualifier
12324
12325 GNU Extension:
12326
12327 type-specifier:
12328 __complex__
12329
12330 Returns a representation of the type-specifier. For a
12331 class-specifier, enum-specifier, or elaborated-type-specifier, a
12332 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12333
12334 The parser flags FLAGS is used to control type-specifier parsing.
12335
12336 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12337 in a decl-specifier-seq.
12338
12339 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12340 class-specifier, enum-specifier, or elaborated-type-specifier, then
12341 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12342 if a type is declared; 2 if it is defined. Otherwise, it is set to
12343 zero.
12344
12345 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12346 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12347 is set to FALSE. */
12348
12349 static tree
12350 cp_parser_type_specifier (cp_parser* parser,
12351 cp_parser_flags flags,
12352 cp_decl_specifier_seq *decl_specs,
12353 bool is_declaration,
12354 int* declares_class_or_enum,
12355 bool* is_cv_qualifier)
12356 {
12357 tree type_spec = NULL_TREE;
12358 cp_token *token;
12359 enum rid keyword;
12360 cp_decl_spec ds = ds_last;
12361
12362 /* Assume this type-specifier does not declare a new type. */
12363 if (declares_class_or_enum)
12364 *declares_class_or_enum = 0;
12365 /* And that it does not specify a cv-qualifier. */
12366 if (is_cv_qualifier)
12367 *is_cv_qualifier = false;
12368 /* Peek at the next token. */
12369 token = cp_lexer_peek_token (parser->lexer);
12370
12371 /* If we're looking at a keyword, we can use that to guide the
12372 production we choose. */
12373 keyword = token->keyword;
12374 switch (keyword)
12375 {
12376 case RID_ENUM:
12377 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12378 goto elaborated_type_specifier;
12379
12380 /* Look for the enum-specifier. */
12381 type_spec = cp_parser_enum_specifier (parser);
12382 /* If that worked, we're done. */
12383 if (type_spec)
12384 {
12385 if (declares_class_or_enum)
12386 *declares_class_or_enum = 2;
12387 if (decl_specs)
12388 cp_parser_set_decl_spec_type (decl_specs,
12389 type_spec,
12390 token->location,
12391 /*user_defined_p=*/true);
12392 return type_spec;
12393 }
12394 else
12395 goto elaborated_type_specifier;
12396
12397 /* Any of these indicate either a class-specifier, or an
12398 elaborated-type-specifier. */
12399 case RID_CLASS:
12400 case RID_STRUCT:
12401 case RID_UNION:
12402 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12403 goto elaborated_type_specifier;
12404
12405 /* Parse tentatively so that we can back up if we don't find a
12406 class-specifier. */
12407 cp_parser_parse_tentatively (parser);
12408 /* Look for the class-specifier. */
12409 type_spec = cp_parser_class_specifier (parser);
12410 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12411 /* If that worked, we're done. */
12412 if (cp_parser_parse_definitely (parser))
12413 {
12414 if (declares_class_or_enum)
12415 *declares_class_or_enum = 2;
12416 if (decl_specs)
12417 cp_parser_set_decl_spec_type (decl_specs,
12418 type_spec,
12419 token->location,
12420 /*user_defined_p=*/true);
12421 return type_spec;
12422 }
12423
12424 /* Fall through. */
12425 elaborated_type_specifier:
12426 /* We're declaring (not defining) a class or enum. */
12427 if (declares_class_or_enum)
12428 *declares_class_or_enum = 1;
12429
12430 /* Fall through. */
12431 case RID_TYPENAME:
12432 /* Look for an elaborated-type-specifier. */
12433 type_spec
12434 = (cp_parser_elaborated_type_specifier
12435 (parser,
12436 decl_specs && decl_specs->specs[(int) ds_friend],
12437 is_declaration));
12438 if (decl_specs)
12439 cp_parser_set_decl_spec_type (decl_specs,
12440 type_spec,
12441 token->location,
12442 /*user_defined_p=*/true);
12443 return type_spec;
12444
12445 case RID_CONST:
12446 ds = ds_const;
12447 if (is_cv_qualifier)
12448 *is_cv_qualifier = true;
12449 break;
12450
12451 case RID_VOLATILE:
12452 ds = ds_volatile;
12453 if (is_cv_qualifier)
12454 *is_cv_qualifier = true;
12455 break;
12456
12457 case RID_RESTRICT:
12458 ds = ds_restrict;
12459 if (is_cv_qualifier)
12460 *is_cv_qualifier = true;
12461 break;
12462
12463 case RID_COMPLEX:
12464 /* The `__complex__' keyword is a GNU extension. */
12465 ds = ds_complex;
12466 break;
12467
12468 default:
12469 break;
12470 }
12471
12472 /* Handle simple keywords. */
12473 if (ds != ds_last)
12474 {
12475 if (decl_specs)
12476 {
12477 ++decl_specs->specs[(int)ds];
12478 decl_specs->any_specifiers_p = true;
12479 }
12480 return cp_lexer_consume_token (parser->lexer)->u.value;
12481 }
12482
12483 /* If we do not already have a type-specifier, assume we are looking
12484 at a simple-type-specifier. */
12485 type_spec = cp_parser_simple_type_specifier (parser,
12486 decl_specs,
12487 flags);
12488
12489 /* If we didn't find a type-specifier, and a type-specifier was not
12490 optional in this context, issue an error message. */
12491 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12492 {
12493 cp_parser_error (parser, "expected type specifier");
12494 return error_mark_node;
12495 }
12496
12497 return type_spec;
12498 }
12499
12500 /* Parse a simple-type-specifier.
12501
12502 simple-type-specifier:
12503 :: [opt] nested-name-specifier [opt] type-name
12504 :: [opt] nested-name-specifier template template-id
12505 char
12506 wchar_t
12507 bool
12508 short
12509 int
12510 long
12511 signed
12512 unsigned
12513 float
12514 double
12515 void
12516
12517 C++0x Extension:
12518
12519 simple-type-specifier:
12520 auto
12521 decltype ( expression )
12522 char16_t
12523 char32_t
12524 __underlying_type ( type-id )
12525
12526 GNU Extension:
12527
12528 simple-type-specifier:
12529 __int128
12530 __typeof__ unary-expression
12531 __typeof__ ( type-id )
12532
12533 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12534 appropriately updated. */
12535
12536 static tree
12537 cp_parser_simple_type_specifier (cp_parser* parser,
12538 cp_decl_specifier_seq *decl_specs,
12539 cp_parser_flags flags)
12540 {
12541 tree type = NULL_TREE;
12542 cp_token *token;
12543
12544 /* Peek at the next token. */
12545 token = cp_lexer_peek_token (parser->lexer);
12546
12547 /* If we're looking at a keyword, things are easy. */
12548 switch (token->keyword)
12549 {
12550 case RID_CHAR:
12551 if (decl_specs)
12552 decl_specs->explicit_char_p = true;
12553 type = char_type_node;
12554 break;
12555 case RID_CHAR16:
12556 type = char16_type_node;
12557 break;
12558 case RID_CHAR32:
12559 type = char32_type_node;
12560 break;
12561 case RID_WCHAR:
12562 type = wchar_type_node;
12563 break;
12564 case RID_BOOL:
12565 type = boolean_type_node;
12566 break;
12567 case RID_SHORT:
12568 if (decl_specs)
12569 ++decl_specs->specs[(int) ds_short];
12570 type = short_integer_type_node;
12571 break;
12572 case RID_INT:
12573 if (decl_specs)
12574 decl_specs->explicit_int_p = true;
12575 type = integer_type_node;
12576 break;
12577 case RID_INT128:
12578 if (!int128_integer_type_node)
12579 break;
12580 if (decl_specs)
12581 decl_specs->explicit_int128_p = true;
12582 type = int128_integer_type_node;
12583 break;
12584 case RID_LONG:
12585 if (decl_specs)
12586 ++decl_specs->specs[(int) ds_long];
12587 type = long_integer_type_node;
12588 break;
12589 case RID_SIGNED:
12590 if (decl_specs)
12591 ++decl_specs->specs[(int) ds_signed];
12592 type = integer_type_node;
12593 break;
12594 case RID_UNSIGNED:
12595 if (decl_specs)
12596 ++decl_specs->specs[(int) ds_unsigned];
12597 type = unsigned_type_node;
12598 break;
12599 case RID_FLOAT:
12600 type = float_type_node;
12601 break;
12602 case RID_DOUBLE:
12603 type = double_type_node;
12604 break;
12605 case RID_VOID:
12606 type = void_type_node;
12607 break;
12608
12609 case RID_AUTO:
12610 maybe_warn_cpp0x (CPP0X_AUTO);
12611 type = make_auto ();
12612 break;
12613
12614 case RID_DECLTYPE:
12615 /* Parse the `decltype' type. */
12616 type = cp_parser_decltype (parser);
12617
12618 if (decl_specs)
12619 cp_parser_set_decl_spec_type (decl_specs, type,
12620 token->location,
12621 /*user_defined_p=*/true);
12622
12623 return type;
12624
12625 case RID_TYPEOF:
12626 /* Consume the `typeof' token. */
12627 cp_lexer_consume_token (parser->lexer);
12628 /* Parse the operand to `typeof'. */
12629 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12630 /* If it is not already a TYPE, take its type. */
12631 if (!TYPE_P (type))
12632 type = finish_typeof (type);
12633
12634 if (decl_specs)
12635 cp_parser_set_decl_spec_type (decl_specs, type,
12636 token->location,
12637 /*user_defined_p=*/true);
12638
12639 return type;
12640
12641 case RID_UNDERLYING_TYPE:
12642 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12643
12644 if (decl_specs)
12645 cp_parser_set_decl_spec_type (decl_specs, type,
12646 token->location,
12647 /*user_defined_p=*/true);
12648
12649 return type;
12650
12651 default:
12652 break;
12653 }
12654
12655 /* If the type-specifier was for a built-in type, we're done. */
12656 if (type)
12657 {
12658 /* Record the type. */
12659 if (decl_specs
12660 && (token->keyword != RID_SIGNED
12661 && token->keyword != RID_UNSIGNED
12662 && token->keyword != RID_SHORT
12663 && token->keyword != RID_LONG))
12664 cp_parser_set_decl_spec_type (decl_specs,
12665 type,
12666 token->location,
12667 /*user_defined=*/false);
12668 if (decl_specs)
12669 decl_specs->any_specifiers_p = true;
12670
12671 /* Consume the token. */
12672 cp_lexer_consume_token (parser->lexer);
12673
12674 /* There is no valid C++ program where a non-template type is
12675 followed by a "<". That usually indicates that the user thought
12676 that the type was a template. */
12677 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12678
12679 return TYPE_NAME (type);
12680 }
12681
12682 /* The type-specifier must be a user-defined type. */
12683 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12684 {
12685 bool qualified_p;
12686 bool global_p;
12687
12688 /* Don't gobble tokens or issue error messages if this is an
12689 optional type-specifier. */
12690 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12691 cp_parser_parse_tentatively (parser);
12692
12693 /* Look for the optional `::' operator. */
12694 global_p
12695 = (cp_parser_global_scope_opt (parser,
12696 /*current_scope_valid_p=*/false)
12697 != NULL_TREE);
12698 /* Look for the nested-name specifier. */
12699 qualified_p
12700 = (cp_parser_nested_name_specifier_opt (parser,
12701 /*typename_keyword_p=*/false,
12702 /*check_dependency_p=*/true,
12703 /*type_p=*/false,
12704 /*is_declaration=*/false)
12705 != NULL_TREE);
12706 token = cp_lexer_peek_token (parser->lexer);
12707 /* If we have seen a nested-name-specifier, and the next token
12708 is `template', then we are using the template-id production. */
12709 if (parser->scope
12710 && cp_parser_optional_template_keyword (parser))
12711 {
12712 /* Look for the template-id. */
12713 type = cp_parser_template_id (parser,
12714 /*template_keyword_p=*/true,
12715 /*check_dependency_p=*/true,
12716 /*is_declaration=*/false);
12717 /* If the template-id did not name a type, we are out of
12718 luck. */
12719 if (TREE_CODE (type) != TYPE_DECL)
12720 {
12721 cp_parser_error (parser, "expected template-id for type");
12722 type = NULL_TREE;
12723 }
12724 }
12725 /* Otherwise, look for a type-name. */
12726 else
12727 type = cp_parser_type_name (parser);
12728 /* Keep track of all name-lookups performed in class scopes. */
12729 if (type
12730 && !global_p
12731 && !qualified_p
12732 && TREE_CODE (type) == TYPE_DECL
12733 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12734 maybe_note_name_used_in_class (DECL_NAME (type), type);
12735 /* If it didn't work out, we don't have a TYPE. */
12736 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12737 && !cp_parser_parse_definitely (parser))
12738 type = NULL_TREE;
12739 if (type && decl_specs)
12740 cp_parser_set_decl_spec_type (decl_specs, type,
12741 token->location,
12742 /*user_defined=*/true);
12743 }
12744
12745 /* If we didn't get a type-name, issue an error message. */
12746 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12747 {
12748 cp_parser_error (parser, "expected type-name");
12749 return error_mark_node;
12750 }
12751
12752 if (type && type != error_mark_node)
12753 {
12754 /* See if TYPE is an Objective-C type, and if so, parse and
12755 accept any protocol references following it. Do this before
12756 the cp_parser_check_for_invalid_template_id() call, because
12757 Objective-C types can be followed by '<...>' which would
12758 enclose protocol names rather than template arguments, and so
12759 everything is fine. */
12760 if (c_dialect_objc () && !parser->scope
12761 && (objc_is_id (type) || objc_is_class_name (type)))
12762 {
12763 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12764 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12765
12766 /* Clobber the "unqualified" type previously entered into
12767 DECL_SPECS with the new, improved protocol-qualified version. */
12768 if (decl_specs)
12769 decl_specs->type = qual_type;
12770
12771 return qual_type;
12772 }
12773
12774 /* There is no valid C++ program where a non-template type is
12775 followed by a "<". That usually indicates that the user
12776 thought that the type was a template. */
12777 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12778 token->location);
12779 }
12780
12781 return type;
12782 }
12783
12784 /* Parse a type-name.
12785
12786 type-name:
12787 class-name
12788 enum-name
12789 typedef-name
12790
12791 enum-name:
12792 identifier
12793
12794 typedef-name:
12795 identifier
12796
12797 Returns a TYPE_DECL for the type. */
12798
12799 static tree
12800 cp_parser_type_name (cp_parser* parser)
12801 {
12802 tree type_decl;
12803
12804 /* We can't know yet whether it is a class-name or not. */
12805 cp_parser_parse_tentatively (parser);
12806 /* Try a class-name. */
12807 type_decl = cp_parser_class_name (parser,
12808 /*typename_keyword_p=*/false,
12809 /*template_keyword_p=*/false,
12810 none_type,
12811 /*check_dependency_p=*/true,
12812 /*class_head_p=*/false,
12813 /*is_declaration=*/false);
12814 /* If it's not a class-name, keep looking. */
12815 if (!cp_parser_parse_definitely (parser))
12816 {
12817 /* It must be a typedef-name or an enum-name. */
12818 return cp_parser_nonclass_name (parser);
12819 }
12820
12821 return type_decl;
12822 }
12823
12824 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12825
12826 enum-name:
12827 identifier
12828
12829 typedef-name:
12830 identifier
12831
12832 Returns a TYPE_DECL for the type. */
12833
12834 static tree
12835 cp_parser_nonclass_name (cp_parser* parser)
12836 {
12837 tree type_decl;
12838 tree identifier;
12839
12840 cp_token *token = cp_lexer_peek_token (parser->lexer);
12841 identifier = cp_parser_identifier (parser);
12842 if (identifier == error_mark_node)
12843 return error_mark_node;
12844
12845 /* Look up the type-name. */
12846 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12847
12848 if (TREE_CODE (type_decl) != TYPE_DECL
12849 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12850 {
12851 /* See if this is an Objective-C type. */
12852 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12853 tree type = objc_get_protocol_qualified_type (identifier, protos);
12854 if (type)
12855 type_decl = TYPE_NAME (type);
12856 }
12857
12858 /* Issue an error if we did not find a type-name. */
12859 if (TREE_CODE (type_decl) != TYPE_DECL
12860 /* In Objective-C, we have the complication that class names are
12861 normally type names and start declarations (eg, the
12862 "NSObject" in "NSObject *object;"), but can be used in an
12863 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12864 is an expression. So, a classname followed by a dot is not a
12865 valid type-name. */
12866 || (objc_is_class_name (TREE_TYPE (type_decl))
12867 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12868 {
12869 if (!cp_parser_simulate_error (parser))
12870 cp_parser_name_lookup_error (parser, identifier, type_decl,
12871 NLE_TYPE, token->location);
12872 return error_mark_node;
12873 }
12874 /* Remember that the name was used in the definition of the
12875 current class so that we can check later to see if the
12876 meaning would have been different after the class was
12877 entirely defined. */
12878 else if (type_decl != error_mark_node
12879 && !parser->scope)
12880 maybe_note_name_used_in_class (identifier, type_decl);
12881
12882 return type_decl;
12883 }
12884
12885 /* Parse an elaborated-type-specifier. Note that the grammar given
12886 here incorporates the resolution to DR68.
12887
12888 elaborated-type-specifier:
12889 class-key :: [opt] nested-name-specifier [opt] identifier
12890 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12891 enum-key :: [opt] nested-name-specifier [opt] identifier
12892 typename :: [opt] nested-name-specifier identifier
12893 typename :: [opt] nested-name-specifier template [opt]
12894 template-id
12895
12896 GNU extension:
12897
12898 elaborated-type-specifier:
12899 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12900 class-key attributes :: [opt] nested-name-specifier [opt]
12901 template [opt] template-id
12902 enum attributes :: [opt] nested-name-specifier [opt] identifier
12903
12904 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12905 declared `friend'. If IS_DECLARATION is TRUE, then this
12906 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12907 something is being declared.
12908
12909 Returns the TYPE specified. */
12910
12911 static tree
12912 cp_parser_elaborated_type_specifier (cp_parser* parser,
12913 bool is_friend,
12914 bool is_declaration)
12915 {
12916 enum tag_types tag_type;
12917 tree identifier;
12918 tree type = NULL_TREE;
12919 tree attributes = NULL_TREE;
12920 tree globalscope;
12921 cp_token *token = NULL;
12922
12923 /* See if we're looking at the `enum' keyword. */
12924 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12925 {
12926 /* Consume the `enum' token. */
12927 cp_lexer_consume_token (parser->lexer);
12928 /* Remember that it's an enumeration type. */
12929 tag_type = enum_type;
12930 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12931 enums) is used here. */
12932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12933 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12934 {
12935 pedwarn (input_location, 0, "elaborated-type-specifier "
12936 "for a scoped enum must not use the %<%D%> keyword",
12937 cp_lexer_peek_token (parser->lexer)->u.value);
12938 /* Consume the `struct' or `class' and parse it anyway. */
12939 cp_lexer_consume_token (parser->lexer);
12940 }
12941 /* Parse the attributes. */
12942 attributes = cp_parser_attributes_opt (parser);
12943 }
12944 /* Or, it might be `typename'. */
12945 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12946 RID_TYPENAME))
12947 {
12948 /* Consume the `typename' token. */
12949 cp_lexer_consume_token (parser->lexer);
12950 /* Remember that it's a `typename' type. */
12951 tag_type = typename_type;
12952 }
12953 /* Otherwise it must be a class-key. */
12954 else
12955 {
12956 tag_type = cp_parser_class_key (parser);
12957 if (tag_type == none_type)
12958 return error_mark_node;
12959 /* Parse the attributes. */
12960 attributes = cp_parser_attributes_opt (parser);
12961 }
12962
12963 /* Look for the `::' operator. */
12964 globalscope = cp_parser_global_scope_opt (parser,
12965 /*current_scope_valid_p=*/false);
12966 /* Look for the nested-name-specifier. */
12967 if (tag_type == typename_type && !globalscope)
12968 {
12969 if (!cp_parser_nested_name_specifier (parser,
12970 /*typename_keyword_p=*/true,
12971 /*check_dependency_p=*/true,
12972 /*type_p=*/true,
12973 is_declaration))
12974 return error_mark_node;
12975 }
12976 else
12977 /* Even though `typename' is not present, the proposed resolution
12978 to Core Issue 180 says that in `class A<T>::B', `B' should be
12979 considered a type-name, even if `A<T>' is dependent. */
12980 cp_parser_nested_name_specifier_opt (parser,
12981 /*typename_keyword_p=*/true,
12982 /*check_dependency_p=*/true,
12983 /*type_p=*/true,
12984 is_declaration);
12985 /* For everything but enumeration types, consider a template-id.
12986 For an enumeration type, consider only a plain identifier. */
12987 if (tag_type != enum_type)
12988 {
12989 bool template_p = false;
12990 tree decl;
12991
12992 /* Allow the `template' keyword. */
12993 template_p = cp_parser_optional_template_keyword (parser);
12994 /* If we didn't see `template', we don't know if there's a
12995 template-id or not. */
12996 if (!template_p)
12997 cp_parser_parse_tentatively (parser);
12998 /* Parse the template-id. */
12999 token = cp_lexer_peek_token (parser->lexer);
13000 decl = cp_parser_template_id (parser, template_p,
13001 /*check_dependency_p=*/true,
13002 is_declaration);
13003 /* If we didn't find a template-id, look for an ordinary
13004 identifier. */
13005 if (!template_p && !cp_parser_parse_definitely (parser))
13006 ;
13007 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13008 in effect, then we must assume that, upon instantiation, the
13009 template will correspond to a class. */
13010 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13011 && tag_type == typename_type)
13012 type = make_typename_type (parser->scope, decl,
13013 typename_type,
13014 /*complain=*/tf_error);
13015 /* If the `typename' keyword is in effect and DECL is not a type
13016 decl. Then type is non existant. */
13017 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13018 type = NULL_TREE;
13019 else
13020 type = TREE_TYPE (decl);
13021 }
13022
13023 if (!type)
13024 {
13025 token = cp_lexer_peek_token (parser->lexer);
13026 identifier = cp_parser_identifier (parser);
13027
13028 if (identifier == error_mark_node)
13029 {
13030 parser->scope = NULL_TREE;
13031 return error_mark_node;
13032 }
13033
13034 /* For a `typename', we needn't call xref_tag. */
13035 if (tag_type == typename_type
13036 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13037 return cp_parser_make_typename_type (parser, parser->scope,
13038 identifier,
13039 token->location);
13040 /* Look up a qualified name in the usual way. */
13041 if (parser->scope)
13042 {
13043 tree decl;
13044 tree ambiguous_decls;
13045
13046 decl = cp_parser_lookup_name (parser, identifier,
13047 tag_type,
13048 /*is_template=*/false,
13049 /*is_namespace=*/false,
13050 /*check_dependency=*/true,
13051 &ambiguous_decls,
13052 token->location);
13053
13054 /* If the lookup was ambiguous, an error will already have been
13055 issued. */
13056 if (ambiguous_decls)
13057 return error_mark_node;
13058
13059 /* If we are parsing friend declaration, DECL may be a
13060 TEMPLATE_DECL tree node here. However, we need to check
13061 whether this TEMPLATE_DECL results in valid code. Consider
13062 the following example:
13063
13064 namespace N {
13065 template <class T> class C {};
13066 }
13067 class X {
13068 template <class T> friend class N::C; // #1, valid code
13069 };
13070 template <class T> class Y {
13071 friend class N::C; // #2, invalid code
13072 };
13073
13074 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13075 name lookup of `N::C'. We see that friend declaration must
13076 be template for the code to be valid. Note that
13077 processing_template_decl does not work here since it is
13078 always 1 for the above two cases. */
13079
13080 decl = (cp_parser_maybe_treat_template_as_class
13081 (decl, /*tag_name_p=*/is_friend
13082 && parser->num_template_parameter_lists));
13083
13084 if (TREE_CODE (decl) != TYPE_DECL)
13085 {
13086 cp_parser_diagnose_invalid_type_name (parser,
13087 parser->scope,
13088 identifier,
13089 token->location);
13090 return error_mark_node;
13091 }
13092
13093 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13094 {
13095 bool allow_template = (parser->num_template_parameter_lists
13096 || DECL_SELF_REFERENCE_P (decl));
13097 type = check_elaborated_type_specifier (tag_type, decl,
13098 allow_template);
13099
13100 if (type == error_mark_node)
13101 return error_mark_node;
13102 }
13103
13104 /* Forward declarations of nested types, such as
13105
13106 class C1::C2;
13107 class C1::C2::C3;
13108
13109 are invalid unless all components preceding the final '::'
13110 are complete. If all enclosing types are complete, these
13111 declarations become merely pointless.
13112
13113 Invalid forward declarations of nested types are errors
13114 caught elsewhere in parsing. Those that are pointless arrive
13115 here. */
13116
13117 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13118 && !is_friend && !processing_explicit_instantiation)
13119 warning (0, "declaration %qD does not declare anything", decl);
13120
13121 type = TREE_TYPE (decl);
13122 }
13123 else
13124 {
13125 /* An elaborated-type-specifier sometimes introduces a new type and
13126 sometimes names an existing type. Normally, the rule is that it
13127 introduces a new type only if there is not an existing type of
13128 the same name already in scope. For example, given:
13129
13130 struct S {};
13131 void f() { struct S s; }
13132
13133 the `struct S' in the body of `f' is the same `struct S' as in
13134 the global scope; the existing definition is used. However, if
13135 there were no global declaration, this would introduce a new
13136 local class named `S'.
13137
13138 An exception to this rule applies to the following code:
13139
13140 namespace N { struct S; }
13141
13142 Here, the elaborated-type-specifier names a new type
13143 unconditionally; even if there is already an `S' in the
13144 containing scope this declaration names a new type.
13145 This exception only applies if the elaborated-type-specifier
13146 forms the complete declaration:
13147
13148 [class.name]
13149
13150 A declaration consisting solely of `class-key identifier ;' is
13151 either a redeclaration of the name in the current scope or a
13152 forward declaration of the identifier as a class name. It
13153 introduces the name into the current scope.
13154
13155 We are in this situation precisely when the next token is a `;'.
13156
13157 An exception to the exception is that a `friend' declaration does
13158 *not* name a new type; i.e., given:
13159
13160 struct S { friend struct T; };
13161
13162 `T' is not a new type in the scope of `S'.
13163
13164 Also, `new struct S' or `sizeof (struct S)' never results in the
13165 definition of a new type; a new type can only be declared in a
13166 declaration context. */
13167
13168 tag_scope ts;
13169 bool template_p;
13170
13171 if (is_friend)
13172 /* Friends have special name lookup rules. */
13173 ts = ts_within_enclosing_non_class;
13174 else if (is_declaration
13175 && cp_lexer_next_token_is (parser->lexer,
13176 CPP_SEMICOLON))
13177 /* This is a `class-key identifier ;' */
13178 ts = ts_current;
13179 else
13180 ts = ts_global;
13181
13182 template_p =
13183 (parser->num_template_parameter_lists
13184 && (cp_parser_next_token_starts_class_definition_p (parser)
13185 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13186 /* An unqualified name was used to reference this type, so
13187 there were no qualifying templates. */
13188 if (!cp_parser_check_template_parameters (parser,
13189 /*num_templates=*/0,
13190 token->location,
13191 /*declarator=*/NULL))
13192 return error_mark_node;
13193 type = xref_tag (tag_type, identifier, ts, template_p);
13194 }
13195 }
13196
13197 if (type == error_mark_node)
13198 return error_mark_node;
13199
13200 /* Allow attributes on forward declarations of classes. */
13201 if (attributes)
13202 {
13203 if (TREE_CODE (type) == TYPENAME_TYPE)
13204 warning (OPT_Wattributes,
13205 "attributes ignored on uninstantiated type");
13206 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13207 && ! processing_explicit_instantiation)
13208 warning (OPT_Wattributes,
13209 "attributes ignored on template instantiation");
13210 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13211 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13212 else
13213 warning (OPT_Wattributes,
13214 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13215 }
13216
13217 if (tag_type != enum_type)
13218 cp_parser_check_class_key (tag_type, type);
13219
13220 /* A "<" cannot follow an elaborated type specifier. If that
13221 happens, the user was probably trying to form a template-id. */
13222 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13223
13224 return type;
13225 }
13226
13227 /* Parse an enum-specifier.
13228
13229 enum-specifier:
13230 enum-head { enumerator-list [opt] }
13231
13232 enum-head:
13233 enum-key identifier [opt] enum-base [opt]
13234 enum-key nested-name-specifier identifier enum-base [opt]
13235
13236 enum-key:
13237 enum
13238 enum class [C++0x]
13239 enum struct [C++0x]
13240
13241 enum-base: [C++0x]
13242 : type-specifier-seq
13243
13244 opaque-enum-specifier:
13245 enum-key identifier enum-base [opt] ;
13246
13247 GNU Extensions:
13248 enum-key attributes[opt] identifier [opt] enum-base [opt]
13249 { enumerator-list [opt] }attributes[opt]
13250
13251 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13252 if the token stream isn't an enum-specifier after all. */
13253
13254 static tree
13255 cp_parser_enum_specifier (cp_parser* parser)
13256 {
13257 tree identifier;
13258 tree type = NULL_TREE;
13259 tree prev_scope;
13260 tree nested_name_specifier = NULL_TREE;
13261 tree attributes;
13262 bool scoped_enum_p = false;
13263 bool has_underlying_type = false;
13264 bool nested_being_defined = false;
13265 bool new_value_list = false;
13266 bool is_new_type = false;
13267 bool is_anonymous = false;
13268 tree underlying_type = NULL_TREE;
13269 cp_token *type_start_token = NULL;
13270 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13271
13272 parser->colon_corrects_to_scope_p = false;
13273
13274 /* Parse tentatively so that we can back up if we don't find a
13275 enum-specifier. */
13276 cp_parser_parse_tentatively (parser);
13277
13278 /* Caller guarantees that the current token is 'enum', an identifier
13279 possibly follows, and the token after that is an opening brace.
13280 If we don't have an identifier, fabricate an anonymous name for
13281 the enumeration being defined. */
13282 cp_lexer_consume_token (parser->lexer);
13283
13284 /* Parse the "class" or "struct", which indicates a scoped
13285 enumeration type in C++0x. */
13286 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13287 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13288 {
13289 if (cxx_dialect < cxx0x)
13290 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13291
13292 /* Consume the `struct' or `class' token. */
13293 cp_lexer_consume_token (parser->lexer);
13294
13295 scoped_enum_p = true;
13296 }
13297
13298 attributes = cp_parser_attributes_opt (parser);
13299
13300 /* Clear the qualification. */
13301 parser->scope = NULL_TREE;
13302 parser->qualifying_scope = NULL_TREE;
13303 parser->object_scope = NULL_TREE;
13304
13305 /* Figure out in what scope the declaration is being placed. */
13306 prev_scope = current_scope ();
13307
13308 type_start_token = cp_lexer_peek_token (parser->lexer);
13309
13310 push_deferring_access_checks (dk_no_check);
13311 nested_name_specifier
13312 = cp_parser_nested_name_specifier_opt (parser,
13313 /*typename_keyword_p=*/true,
13314 /*check_dependency_p=*/false,
13315 /*type_p=*/false,
13316 /*is_declaration=*/false);
13317
13318 if (nested_name_specifier)
13319 {
13320 tree name;
13321
13322 identifier = cp_parser_identifier (parser);
13323 name = cp_parser_lookup_name (parser, identifier,
13324 enum_type,
13325 /*is_template=*/false,
13326 /*is_namespace=*/false,
13327 /*check_dependency=*/true,
13328 /*ambiguous_decls=*/NULL,
13329 input_location);
13330 if (name)
13331 {
13332 type = TREE_TYPE (name);
13333 if (TREE_CODE (type) == TYPENAME_TYPE)
13334 {
13335 /* Are template enums allowed in ISO? */
13336 if (template_parm_scope_p ())
13337 pedwarn (type_start_token->location, OPT_pedantic,
13338 "%qD is an enumeration template", name);
13339 /* ignore a typename reference, for it will be solved by name
13340 in start_enum. */
13341 type = NULL_TREE;
13342 }
13343 }
13344 else
13345 error_at (type_start_token->location,
13346 "%qD is not an enumerator-name", identifier);
13347 }
13348 else
13349 {
13350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13351 identifier = cp_parser_identifier (parser);
13352 else
13353 {
13354 identifier = make_anon_name ();
13355 is_anonymous = true;
13356 }
13357 }
13358 pop_deferring_access_checks ();
13359
13360 /* Check for the `:' that denotes a specified underlying type in C++0x.
13361 Note that a ':' could also indicate a bitfield width, however. */
13362 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13363 {
13364 cp_decl_specifier_seq type_specifiers;
13365
13366 /* Consume the `:'. */
13367 cp_lexer_consume_token (parser->lexer);
13368
13369 /* Parse the type-specifier-seq. */
13370 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13371 /*is_trailing_return=*/false,
13372 &type_specifiers);
13373
13374 /* At this point this is surely not elaborated type specifier. */
13375 if (!cp_parser_parse_definitely (parser))
13376 return NULL_TREE;
13377
13378 if (cxx_dialect < cxx0x)
13379 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13380
13381 has_underlying_type = true;
13382
13383 /* If that didn't work, stop. */
13384 if (type_specifiers.type != error_mark_node)
13385 {
13386 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13387 /*initialized=*/0, NULL);
13388 if (underlying_type == error_mark_node)
13389 underlying_type = NULL_TREE;
13390 }
13391 }
13392
13393 /* Look for the `{' but don't consume it yet. */
13394 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13395 {
13396 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13397 {
13398 cp_parser_error (parser, "expected %<{%>");
13399 if (has_underlying_type)
13400 {
13401 type = NULL_TREE;
13402 goto out;
13403 }
13404 }
13405 /* An opaque-enum-specifier must have a ';' here. */
13406 if ((scoped_enum_p || underlying_type)
13407 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13408 {
13409 cp_parser_error (parser, "expected %<;%> or %<{%>");
13410 if (has_underlying_type)
13411 {
13412 type = NULL_TREE;
13413 goto out;
13414 }
13415 }
13416 }
13417
13418 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13419 return NULL_TREE;
13420
13421 if (nested_name_specifier)
13422 {
13423 if (CLASS_TYPE_P (nested_name_specifier))
13424 {
13425 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13426 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13427 push_scope (nested_name_specifier);
13428 }
13429 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13430 {
13431 push_nested_namespace (nested_name_specifier);
13432 }
13433 }
13434
13435 /* Issue an error message if type-definitions are forbidden here. */
13436 if (!cp_parser_check_type_definition (parser))
13437 type = error_mark_node;
13438 else
13439 /* Create the new type. We do this before consuming the opening
13440 brace so the enum will be recorded as being on the line of its
13441 tag (or the 'enum' keyword, if there is no tag). */
13442 type = start_enum (identifier, type, underlying_type,
13443 scoped_enum_p, &is_new_type);
13444
13445 /* If the next token is not '{' it is an opaque-enum-specifier or an
13446 elaborated-type-specifier. */
13447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13448 {
13449 if (nested_name_specifier)
13450 {
13451 /* The following catches invalid code such as:
13452 enum class S<int>::E { A, B, C }; */
13453 if (!processing_specialization
13454 && CLASS_TYPE_P (nested_name_specifier)
13455 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13456 error_at (type_start_token->location, "cannot add an enumerator "
13457 "list to a template instantiation");
13458
13459 /* If that scope does not contain the scope in which the
13460 class was originally declared, the program is invalid. */
13461 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13462 {
13463 if (at_namespace_scope_p ())
13464 error_at (type_start_token->location,
13465 "declaration of %qD in namespace %qD which does not "
13466 "enclose %qD",
13467 type, prev_scope, nested_name_specifier);
13468 else
13469 error_at (type_start_token->location,
13470 "declaration of %qD in %qD which does not enclose %qD",
13471 type, prev_scope, nested_name_specifier);
13472 type = error_mark_node;
13473 }
13474 }
13475
13476 if (scoped_enum_p)
13477 begin_scope (sk_scoped_enum, type);
13478
13479 /* Consume the opening brace. */
13480 cp_lexer_consume_token (parser->lexer);
13481
13482 if (type == error_mark_node)
13483 ; /* Nothing to add */
13484 else if (OPAQUE_ENUM_P (type)
13485 || (cxx_dialect > cxx98 && processing_specialization))
13486 {
13487 new_value_list = true;
13488 SET_OPAQUE_ENUM_P (type, false);
13489 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13490 }
13491 else
13492 {
13493 error_at (type_start_token->location, "multiple definition of %q#T", type);
13494 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13495 "previous definition here");
13496 type = error_mark_node;
13497 }
13498
13499 if (type == error_mark_node)
13500 cp_parser_skip_to_end_of_block_or_statement (parser);
13501 /* If the next token is not '}', then there are some enumerators. */
13502 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13503 cp_parser_enumerator_list (parser, type);
13504
13505 /* Consume the final '}'. */
13506 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13507
13508 if (scoped_enum_p)
13509 finish_scope ();
13510 }
13511 else
13512 {
13513 /* If a ';' follows, then it is an opaque-enum-specifier
13514 and additional restrictions apply. */
13515 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13516 {
13517 if (is_anonymous)
13518 error_at (type_start_token->location,
13519 "opaque-enum-specifier without name");
13520 else if (nested_name_specifier)
13521 error_at (type_start_token->location,
13522 "opaque-enum-specifier must use a simple identifier");
13523 }
13524 }
13525
13526 /* Look for trailing attributes to apply to this enumeration, and
13527 apply them if appropriate. */
13528 if (cp_parser_allow_gnu_extensions_p (parser))
13529 {
13530 tree trailing_attr = cp_parser_attributes_opt (parser);
13531 trailing_attr = chainon (trailing_attr, attributes);
13532 cplus_decl_attributes (&type,
13533 trailing_attr,
13534 (int) ATTR_FLAG_TYPE_IN_PLACE);
13535 }
13536
13537 /* Finish up the enumeration. */
13538 if (type != error_mark_node)
13539 {
13540 if (new_value_list)
13541 finish_enum_value_list (type);
13542 if (is_new_type)
13543 finish_enum (type);
13544 }
13545
13546 if (nested_name_specifier)
13547 {
13548 if (CLASS_TYPE_P (nested_name_specifier))
13549 {
13550 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13551 pop_scope (nested_name_specifier);
13552 }
13553 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13554 {
13555 pop_nested_namespace (nested_name_specifier);
13556 }
13557 }
13558 out:
13559 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13560 return type;
13561 }
13562
13563 /* Parse an enumerator-list. The enumerators all have the indicated
13564 TYPE.
13565
13566 enumerator-list:
13567 enumerator-definition
13568 enumerator-list , enumerator-definition */
13569
13570 static void
13571 cp_parser_enumerator_list (cp_parser* parser, tree type)
13572 {
13573 while (true)
13574 {
13575 /* Parse an enumerator-definition. */
13576 cp_parser_enumerator_definition (parser, type);
13577
13578 /* If the next token is not a ',', we've reached the end of
13579 the list. */
13580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13581 break;
13582 /* Otherwise, consume the `,' and keep going. */
13583 cp_lexer_consume_token (parser->lexer);
13584 /* If the next token is a `}', there is a trailing comma. */
13585 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13586 {
13587 if (!in_system_header)
13588 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13589 break;
13590 }
13591 }
13592 }
13593
13594 /* Parse an enumerator-definition. The enumerator has the indicated
13595 TYPE.
13596
13597 enumerator-definition:
13598 enumerator
13599 enumerator = constant-expression
13600
13601 enumerator:
13602 identifier */
13603
13604 static void
13605 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13606 {
13607 tree identifier;
13608 tree value;
13609 location_t loc;
13610
13611 /* Save the input location because we are interested in the location
13612 of the identifier and not the location of the explicit value. */
13613 loc = cp_lexer_peek_token (parser->lexer)->location;
13614
13615 /* Look for the identifier. */
13616 identifier = cp_parser_identifier (parser);
13617 if (identifier == error_mark_node)
13618 return;
13619
13620 /* If the next token is an '=', then there is an explicit value. */
13621 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13622 {
13623 /* Consume the `=' token. */
13624 cp_lexer_consume_token (parser->lexer);
13625 /* Parse the value. */
13626 value = cp_parser_constant_expression (parser,
13627 /*allow_non_constant_p=*/false,
13628 NULL);
13629 }
13630 else
13631 value = NULL_TREE;
13632
13633 /* If we are processing a template, make sure the initializer of the
13634 enumerator doesn't contain any bare template parameter pack. */
13635 if (check_for_bare_parameter_packs (value))
13636 value = error_mark_node;
13637
13638 /* integral_constant_value will pull out this expression, so make sure
13639 it's folded as appropriate. */
13640 value = fold_non_dependent_expr (value);
13641
13642 /* Create the enumerator. */
13643 build_enumerator (identifier, value, type, loc);
13644 }
13645
13646 /* Parse a namespace-name.
13647
13648 namespace-name:
13649 original-namespace-name
13650 namespace-alias
13651
13652 Returns the NAMESPACE_DECL for the namespace. */
13653
13654 static tree
13655 cp_parser_namespace_name (cp_parser* parser)
13656 {
13657 tree identifier;
13658 tree namespace_decl;
13659
13660 cp_token *token = cp_lexer_peek_token (parser->lexer);
13661
13662 /* Get the name of the namespace. */
13663 identifier = cp_parser_identifier (parser);
13664 if (identifier == error_mark_node)
13665 return error_mark_node;
13666
13667 /* Look up the identifier in the currently active scope. Look only
13668 for namespaces, due to:
13669
13670 [basic.lookup.udir]
13671
13672 When looking up a namespace-name in a using-directive or alias
13673 definition, only namespace names are considered.
13674
13675 And:
13676
13677 [basic.lookup.qual]
13678
13679 During the lookup of a name preceding the :: scope resolution
13680 operator, object, function, and enumerator names are ignored.
13681
13682 (Note that cp_parser_qualifying_entity only calls this
13683 function if the token after the name is the scope resolution
13684 operator.) */
13685 namespace_decl = cp_parser_lookup_name (parser, identifier,
13686 none_type,
13687 /*is_template=*/false,
13688 /*is_namespace=*/true,
13689 /*check_dependency=*/true,
13690 /*ambiguous_decls=*/NULL,
13691 token->location);
13692 /* If it's not a namespace, issue an error. */
13693 if (namespace_decl == error_mark_node
13694 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13695 {
13696 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13697 error_at (token->location, "%qD is not a namespace-name", identifier);
13698 cp_parser_error (parser, "expected namespace-name");
13699 namespace_decl = error_mark_node;
13700 }
13701
13702 return namespace_decl;
13703 }
13704
13705 /* Parse a namespace-definition.
13706
13707 namespace-definition:
13708 named-namespace-definition
13709 unnamed-namespace-definition
13710
13711 named-namespace-definition:
13712 original-namespace-definition
13713 extension-namespace-definition
13714
13715 original-namespace-definition:
13716 namespace identifier { namespace-body }
13717
13718 extension-namespace-definition:
13719 namespace original-namespace-name { namespace-body }
13720
13721 unnamed-namespace-definition:
13722 namespace { namespace-body } */
13723
13724 static void
13725 cp_parser_namespace_definition (cp_parser* parser)
13726 {
13727 tree identifier, attribs;
13728 bool has_visibility;
13729 bool is_inline;
13730
13731 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13732 {
13733 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13734 is_inline = true;
13735 cp_lexer_consume_token (parser->lexer);
13736 }
13737 else
13738 is_inline = false;
13739
13740 /* Look for the `namespace' keyword. */
13741 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13742
13743 /* Get the name of the namespace. We do not attempt to distinguish
13744 between an original-namespace-definition and an
13745 extension-namespace-definition at this point. The semantic
13746 analysis routines are responsible for that. */
13747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13748 identifier = cp_parser_identifier (parser);
13749 else
13750 identifier = NULL_TREE;
13751
13752 /* Parse any specified attributes. */
13753 attribs = cp_parser_attributes_opt (parser);
13754
13755 /* Look for the `{' to start the namespace. */
13756 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13757 /* Start the namespace. */
13758 push_namespace (identifier);
13759
13760 /* "inline namespace" is equivalent to a stub namespace definition
13761 followed by a strong using directive. */
13762 if (is_inline)
13763 {
13764 tree name_space = current_namespace;
13765 /* Set up namespace association. */
13766 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13767 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13768 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13769 /* Import the contents of the inline namespace. */
13770 pop_namespace ();
13771 do_using_directive (name_space);
13772 push_namespace (identifier);
13773 }
13774
13775 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13776
13777 /* Parse the body of the namespace. */
13778 cp_parser_namespace_body (parser);
13779
13780 if (has_visibility)
13781 pop_visibility (1);
13782
13783 /* Finish the namespace. */
13784 pop_namespace ();
13785 /* Look for the final `}'. */
13786 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13787 }
13788
13789 /* Parse a namespace-body.
13790
13791 namespace-body:
13792 declaration-seq [opt] */
13793
13794 static void
13795 cp_parser_namespace_body (cp_parser* parser)
13796 {
13797 cp_parser_declaration_seq_opt (parser);
13798 }
13799
13800 /* Parse a namespace-alias-definition.
13801
13802 namespace-alias-definition:
13803 namespace identifier = qualified-namespace-specifier ; */
13804
13805 static void
13806 cp_parser_namespace_alias_definition (cp_parser* parser)
13807 {
13808 tree identifier;
13809 tree namespace_specifier;
13810
13811 cp_token *token = cp_lexer_peek_token (parser->lexer);
13812
13813 /* Look for the `namespace' keyword. */
13814 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13815 /* Look for the identifier. */
13816 identifier = cp_parser_identifier (parser);
13817 if (identifier == error_mark_node)
13818 return;
13819 /* Look for the `=' token. */
13820 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13821 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13822 {
13823 error_at (token->location, "%<namespace%> definition is not allowed here");
13824 /* Skip the definition. */
13825 cp_lexer_consume_token (parser->lexer);
13826 if (cp_parser_skip_to_closing_brace (parser))
13827 cp_lexer_consume_token (parser->lexer);
13828 return;
13829 }
13830 cp_parser_require (parser, CPP_EQ, RT_EQ);
13831 /* Look for the qualified-namespace-specifier. */
13832 namespace_specifier
13833 = cp_parser_qualified_namespace_specifier (parser);
13834 /* Look for the `;' token. */
13835 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13836
13837 /* Register the alias in the symbol table. */
13838 do_namespace_alias (identifier, namespace_specifier);
13839 }
13840
13841 /* Parse a qualified-namespace-specifier.
13842
13843 qualified-namespace-specifier:
13844 :: [opt] nested-name-specifier [opt] namespace-name
13845
13846 Returns a NAMESPACE_DECL corresponding to the specified
13847 namespace. */
13848
13849 static tree
13850 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13851 {
13852 /* Look for the optional `::'. */
13853 cp_parser_global_scope_opt (parser,
13854 /*current_scope_valid_p=*/false);
13855
13856 /* Look for the optional nested-name-specifier. */
13857 cp_parser_nested_name_specifier_opt (parser,
13858 /*typename_keyword_p=*/false,
13859 /*check_dependency_p=*/true,
13860 /*type_p=*/false,
13861 /*is_declaration=*/true);
13862
13863 return cp_parser_namespace_name (parser);
13864 }
13865
13866 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13867 access declaration.
13868
13869 using-declaration:
13870 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13871 using :: unqualified-id ;
13872
13873 access-declaration:
13874 qualified-id ;
13875
13876 */
13877
13878 static bool
13879 cp_parser_using_declaration (cp_parser* parser,
13880 bool access_declaration_p)
13881 {
13882 cp_token *token;
13883 bool typename_p = false;
13884 bool global_scope_p;
13885 tree decl;
13886 tree identifier;
13887 tree qscope;
13888
13889 if (access_declaration_p)
13890 cp_parser_parse_tentatively (parser);
13891 else
13892 {
13893 /* Look for the `using' keyword. */
13894 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13895
13896 /* Peek at the next token. */
13897 token = cp_lexer_peek_token (parser->lexer);
13898 /* See if it's `typename'. */
13899 if (token->keyword == RID_TYPENAME)
13900 {
13901 /* Remember that we've seen it. */
13902 typename_p = true;
13903 /* Consume the `typename' token. */
13904 cp_lexer_consume_token (parser->lexer);
13905 }
13906 }
13907
13908 /* Look for the optional global scope qualification. */
13909 global_scope_p
13910 = (cp_parser_global_scope_opt (parser,
13911 /*current_scope_valid_p=*/false)
13912 != NULL_TREE);
13913
13914 /* If we saw `typename', or didn't see `::', then there must be a
13915 nested-name-specifier present. */
13916 if (typename_p || !global_scope_p)
13917 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13918 /*check_dependency_p=*/true,
13919 /*type_p=*/false,
13920 /*is_declaration=*/true);
13921 /* Otherwise, we could be in either of the two productions. In that
13922 case, treat the nested-name-specifier as optional. */
13923 else
13924 qscope = cp_parser_nested_name_specifier_opt (parser,
13925 /*typename_keyword_p=*/false,
13926 /*check_dependency_p=*/true,
13927 /*type_p=*/false,
13928 /*is_declaration=*/true);
13929 if (!qscope)
13930 qscope = global_namespace;
13931
13932 if (access_declaration_p && cp_parser_error_occurred (parser))
13933 /* Something has already gone wrong; there's no need to parse
13934 further. Since an error has occurred, the return value of
13935 cp_parser_parse_definitely will be false, as required. */
13936 return cp_parser_parse_definitely (parser);
13937
13938 token = cp_lexer_peek_token (parser->lexer);
13939 /* Parse the unqualified-id. */
13940 identifier = cp_parser_unqualified_id (parser,
13941 /*template_keyword_p=*/false,
13942 /*check_dependency_p=*/true,
13943 /*declarator_p=*/true,
13944 /*optional_p=*/false);
13945
13946 if (access_declaration_p)
13947 {
13948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13949 cp_parser_simulate_error (parser);
13950 if (!cp_parser_parse_definitely (parser))
13951 return false;
13952 }
13953
13954 /* The function we call to handle a using-declaration is different
13955 depending on what scope we are in. */
13956 if (qscope == error_mark_node || identifier == error_mark_node)
13957 ;
13958 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13959 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13960 /* [namespace.udecl]
13961
13962 A using declaration shall not name a template-id. */
13963 error_at (token->location,
13964 "a template-id may not appear in a using-declaration");
13965 else
13966 {
13967 if (at_class_scope_p ())
13968 {
13969 /* Create the USING_DECL. */
13970 decl = do_class_using_decl (parser->scope, identifier);
13971
13972 if (check_for_bare_parameter_packs (decl))
13973 return false;
13974 else
13975 /* Add it to the list of members in this class. */
13976 finish_member_declaration (decl);
13977 }
13978 else
13979 {
13980 decl = cp_parser_lookup_name_simple (parser,
13981 identifier,
13982 token->location);
13983 if (decl == error_mark_node)
13984 cp_parser_name_lookup_error (parser, identifier,
13985 decl, NLE_NULL,
13986 token->location);
13987 else if (check_for_bare_parameter_packs (decl))
13988 return false;
13989 else if (!at_namespace_scope_p ())
13990 do_local_using_decl (decl, qscope, identifier);
13991 else
13992 do_toplevel_using_decl (decl, qscope, identifier);
13993 }
13994 }
13995
13996 /* Look for the final `;'. */
13997 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13998
13999 return true;
14000 }
14001
14002 /* Parse a using-directive.
14003
14004 using-directive:
14005 using namespace :: [opt] nested-name-specifier [opt]
14006 namespace-name ; */
14007
14008 static void
14009 cp_parser_using_directive (cp_parser* parser)
14010 {
14011 tree namespace_decl;
14012 tree attribs;
14013
14014 /* Look for the `using' keyword. */
14015 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14016 /* And the `namespace' keyword. */
14017 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14018 /* Look for the optional `::' operator. */
14019 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14020 /* And the optional nested-name-specifier. */
14021 cp_parser_nested_name_specifier_opt (parser,
14022 /*typename_keyword_p=*/false,
14023 /*check_dependency_p=*/true,
14024 /*type_p=*/false,
14025 /*is_declaration=*/true);
14026 /* Get the namespace being used. */
14027 namespace_decl = cp_parser_namespace_name (parser);
14028 /* And any specified attributes. */
14029 attribs = cp_parser_attributes_opt (parser);
14030 /* Update the symbol table. */
14031 parse_using_directive (namespace_decl, attribs);
14032 /* Look for the final `;'. */
14033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14034 }
14035
14036 /* Parse an asm-definition.
14037
14038 asm-definition:
14039 asm ( string-literal ) ;
14040
14041 GNU Extension:
14042
14043 asm-definition:
14044 asm volatile [opt] ( string-literal ) ;
14045 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14046 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14047 : asm-operand-list [opt] ) ;
14048 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14049 : asm-operand-list [opt]
14050 : asm-clobber-list [opt] ) ;
14051 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14052 : asm-clobber-list [opt]
14053 : asm-goto-list ) ; */
14054
14055 static void
14056 cp_parser_asm_definition (cp_parser* parser)
14057 {
14058 tree string;
14059 tree outputs = NULL_TREE;
14060 tree inputs = NULL_TREE;
14061 tree clobbers = NULL_TREE;
14062 tree labels = NULL_TREE;
14063 tree asm_stmt;
14064 bool volatile_p = false;
14065 bool extended_p = false;
14066 bool invalid_inputs_p = false;
14067 bool invalid_outputs_p = false;
14068 bool goto_p = false;
14069 required_token missing = RT_NONE;
14070
14071 /* Look for the `asm' keyword. */
14072 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14073 /* See if the next token is `volatile'. */
14074 if (cp_parser_allow_gnu_extensions_p (parser)
14075 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14076 {
14077 /* Remember that we saw the `volatile' keyword. */
14078 volatile_p = true;
14079 /* Consume the token. */
14080 cp_lexer_consume_token (parser->lexer);
14081 }
14082 if (cp_parser_allow_gnu_extensions_p (parser)
14083 && parser->in_function_body
14084 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14085 {
14086 /* Remember that we saw the `goto' keyword. */
14087 goto_p = true;
14088 /* Consume the token. */
14089 cp_lexer_consume_token (parser->lexer);
14090 }
14091 /* Look for the opening `('. */
14092 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14093 return;
14094 /* Look for the string. */
14095 string = cp_parser_string_literal (parser, false, false);
14096 if (string == error_mark_node)
14097 {
14098 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14099 /*consume_paren=*/true);
14100 return;
14101 }
14102
14103 /* If we're allowing GNU extensions, check for the extended assembly
14104 syntax. Unfortunately, the `:' tokens need not be separated by
14105 a space in C, and so, for compatibility, we tolerate that here
14106 too. Doing that means that we have to treat the `::' operator as
14107 two `:' tokens. */
14108 if (cp_parser_allow_gnu_extensions_p (parser)
14109 && parser->in_function_body
14110 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14111 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14112 {
14113 bool inputs_p = false;
14114 bool clobbers_p = false;
14115 bool labels_p = false;
14116
14117 /* The extended syntax was used. */
14118 extended_p = true;
14119
14120 /* Look for outputs. */
14121 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14122 {
14123 /* Consume the `:'. */
14124 cp_lexer_consume_token (parser->lexer);
14125 /* Parse the output-operands. */
14126 if (cp_lexer_next_token_is_not (parser->lexer,
14127 CPP_COLON)
14128 && cp_lexer_next_token_is_not (parser->lexer,
14129 CPP_SCOPE)
14130 && cp_lexer_next_token_is_not (parser->lexer,
14131 CPP_CLOSE_PAREN)
14132 && !goto_p)
14133 outputs = cp_parser_asm_operand_list (parser);
14134
14135 if (outputs == error_mark_node)
14136 invalid_outputs_p = true;
14137 }
14138 /* If the next token is `::', there are no outputs, and the
14139 next token is the beginning of the inputs. */
14140 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14141 /* The inputs are coming next. */
14142 inputs_p = true;
14143
14144 /* Look for inputs. */
14145 if (inputs_p
14146 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14147 {
14148 /* Consume the `:' or `::'. */
14149 cp_lexer_consume_token (parser->lexer);
14150 /* Parse the output-operands. */
14151 if (cp_lexer_next_token_is_not (parser->lexer,
14152 CPP_COLON)
14153 && cp_lexer_next_token_is_not (parser->lexer,
14154 CPP_SCOPE)
14155 && cp_lexer_next_token_is_not (parser->lexer,
14156 CPP_CLOSE_PAREN))
14157 inputs = cp_parser_asm_operand_list (parser);
14158
14159 if (inputs == error_mark_node)
14160 invalid_inputs_p = true;
14161 }
14162 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14163 /* The clobbers are coming next. */
14164 clobbers_p = true;
14165
14166 /* Look for clobbers. */
14167 if (clobbers_p
14168 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14169 {
14170 clobbers_p = true;
14171 /* Consume the `:' or `::'. */
14172 cp_lexer_consume_token (parser->lexer);
14173 /* Parse the clobbers. */
14174 if (cp_lexer_next_token_is_not (parser->lexer,
14175 CPP_COLON)
14176 && cp_lexer_next_token_is_not (parser->lexer,
14177 CPP_CLOSE_PAREN))
14178 clobbers = cp_parser_asm_clobber_list (parser);
14179 }
14180 else if (goto_p
14181 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14182 /* The labels are coming next. */
14183 labels_p = true;
14184
14185 /* Look for labels. */
14186 if (labels_p
14187 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14188 {
14189 labels_p = true;
14190 /* Consume the `:' or `::'. */
14191 cp_lexer_consume_token (parser->lexer);
14192 /* Parse the labels. */
14193 labels = cp_parser_asm_label_list (parser);
14194 }
14195
14196 if (goto_p && !labels_p)
14197 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14198 }
14199 else if (goto_p)
14200 missing = RT_COLON_SCOPE;
14201
14202 /* Look for the closing `)'. */
14203 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14204 missing ? missing : RT_CLOSE_PAREN))
14205 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14206 /*consume_paren=*/true);
14207 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14208
14209 if (!invalid_inputs_p && !invalid_outputs_p)
14210 {
14211 /* Create the ASM_EXPR. */
14212 if (parser->in_function_body)
14213 {
14214 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14215 inputs, clobbers, labels);
14216 /* If the extended syntax was not used, mark the ASM_EXPR. */
14217 if (!extended_p)
14218 {
14219 tree temp = asm_stmt;
14220 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14221 temp = TREE_OPERAND (temp, 0);
14222
14223 ASM_INPUT_P (temp) = 1;
14224 }
14225 }
14226 else
14227 cgraph_add_asm_node (string);
14228 }
14229 }
14230
14231 /* Declarators [gram.dcl.decl] */
14232
14233 /* Parse an init-declarator.
14234
14235 init-declarator:
14236 declarator initializer [opt]
14237
14238 GNU Extension:
14239
14240 init-declarator:
14241 declarator asm-specification [opt] attributes [opt] initializer [opt]
14242
14243 function-definition:
14244 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14245 function-body
14246 decl-specifier-seq [opt] declarator function-try-block
14247
14248 GNU Extension:
14249
14250 function-definition:
14251 __extension__ function-definition
14252
14253 The DECL_SPECIFIERS apply to this declarator. Returns a
14254 representation of the entity declared. If MEMBER_P is TRUE, then
14255 this declarator appears in a class scope. The new DECL created by
14256 this declarator is returned.
14257
14258 The CHECKS are access checks that should be performed once we know
14259 what entity is being declared (and, therefore, what classes have
14260 befriended it).
14261
14262 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14263 for a function-definition here as well. If the declarator is a
14264 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14265 be TRUE upon return. By that point, the function-definition will
14266 have been completely parsed.
14267
14268 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14269 is FALSE.
14270
14271 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14272 parsed declaration if it is an uninitialized single declarator not followed
14273 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14274 if present, will not be consumed. If returned, this declarator will be
14275 created with SD_INITIALIZED but will not call cp_finish_decl. */
14276
14277 static tree
14278 cp_parser_init_declarator (cp_parser* parser,
14279 cp_decl_specifier_seq *decl_specifiers,
14280 VEC (deferred_access_check,gc)* checks,
14281 bool function_definition_allowed_p,
14282 bool member_p,
14283 int declares_class_or_enum,
14284 bool* function_definition_p,
14285 tree* maybe_range_for_decl)
14286 {
14287 cp_token *token = NULL, *asm_spec_start_token = NULL,
14288 *attributes_start_token = NULL;
14289 cp_declarator *declarator;
14290 tree prefix_attributes;
14291 tree attributes;
14292 tree asm_specification;
14293 tree initializer;
14294 tree decl = NULL_TREE;
14295 tree scope;
14296 int is_initialized;
14297 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14298 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14299 "(...)". */
14300 enum cpp_ttype initialization_kind;
14301 bool is_direct_init = false;
14302 bool is_non_constant_init;
14303 int ctor_dtor_or_conv_p;
14304 bool friend_p;
14305 tree pushed_scope = NULL;
14306 bool range_for_decl_p = false;
14307
14308 /* Gather the attributes that were provided with the
14309 decl-specifiers. */
14310 prefix_attributes = decl_specifiers->attributes;
14311
14312 /* Assume that this is not the declarator for a function
14313 definition. */
14314 if (function_definition_p)
14315 *function_definition_p = false;
14316
14317 /* Defer access checks while parsing the declarator; we cannot know
14318 what names are accessible until we know what is being
14319 declared. */
14320 resume_deferring_access_checks ();
14321
14322 /* Parse the declarator. */
14323 token = cp_lexer_peek_token (parser->lexer);
14324 declarator
14325 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14326 &ctor_dtor_or_conv_p,
14327 /*parenthesized_p=*/NULL,
14328 /*member_p=*/false);
14329 /* Gather up the deferred checks. */
14330 stop_deferring_access_checks ();
14331
14332 /* If the DECLARATOR was erroneous, there's no need to go
14333 further. */
14334 if (declarator == cp_error_declarator)
14335 return error_mark_node;
14336
14337 /* Check that the number of template-parameter-lists is OK. */
14338 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14339 token->location))
14340 return error_mark_node;
14341
14342 if (declares_class_or_enum & 2)
14343 cp_parser_check_for_definition_in_return_type (declarator,
14344 decl_specifiers->type,
14345 decl_specifiers->type_location);
14346
14347 /* Figure out what scope the entity declared by the DECLARATOR is
14348 located in. `grokdeclarator' sometimes changes the scope, so
14349 we compute it now. */
14350 scope = get_scope_of_declarator (declarator);
14351
14352 /* Perform any lookups in the declared type which were thought to be
14353 dependent, but are not in the scope of the declarator. */
14354 decl_specifiers->type
14355 = maybe_update_decl_type (decl_specifiers->type, scope);
14356
14357 /* If we're allowing GNU extensions, look for an asm-specification
14358 and attributes. */
14359 if (cp_parser_allow_gnu_extensions_p (parser))
14360 {
14361 /* Look for an asm-specification. */
14362 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14363 asm_specification = cp_parser_asm_specification_opt (parser);
14364 /* And attributes. */
14365 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14366 attributes = cp_parser_attributes_opt (parser);
14367 }
14368 else
14369 {
14370 asm_specification = NULL_TREE;
14371 attributes = NULL_TREE;
14372 }
14373
14374 /* Peek at the next token. */
14375 token = cp_lexer_peek_token (parser->lexer);
14376 /* Check to see if the token indicates the start of a
14377 function-definition. */
14378 if (function_declarator_p (declarator)
14379 && cp_parser_token_starts_function_definition_p (token))
14380 {
14381 if (!function_definition_allowed_p)
14382 {
14383 /* If a function-definition should not appear here, issue an
14384 error message. */
14385 cp_parser_error (parser,
14386 "a function-definition is not allowed here");
14387 return error_mark_node;
14388 }
14389 else
14390 {
14391 location_t func_brace_location
14392 = cp_lexer_peek_token (parser->lexer)->location;
14393
14394 /* Neither attributes nor an asm-specification are allowed
14395 on a function-definition. */
14396 if (asm_specification)
14397 error_at (asm_spec_start_token->location,
14398 "an asm-specification is not allowed "
14399 "on a function-definition");
14400 if (attributes)
14401 error_at (attributes_start_token->location,
14402 "attributes are not allowed on a function-definition");
14403 /* This is a function-definition. */
14404 *function_definition_p = true;
14405
14406 /* Parse the function definition. */
14407 if (member_p)
14408 decl = cp_parser_save_member_function_body (parser,
14409 decl_specifiers,
14410 declarator,
14411 prefix_attributes);
14412 else
14413 decl
14414 = (cp_parser_function_definition_from_specifiers_and_declarator
14415 (parser, decl_specifiers, prefix_attributes, declarator));
14416
14417 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14418 {
14419 /* This is where the prologue starts... */
14420 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14421 = func_brace_location;
14422 }
14423
14424 return decl;
14425 }
14426 }
14427
14428 /* [dcl.dcl]
14429
14430 Only in function declarations for constructors, destructors, and
14431 type conversions can the decl-specifier-seq be omitted.
14432
14433 We explicitly postpone this check past the point where we handle
14434 function-definitions because we tolerate function-definitions
14435 that are missing their return types in some modes. */
14436 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14437 {
14438 cp_parser_error (parser,
14439 "expected constructor, destructor, or type conversion");
14440 return error_mark_node;
14441 }
14442
14443 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14444 if (token->type == CPP_EQ
14445 || token->type == CPP_OPEN_PAREN
14446 || token->type == CPP_OPEN_BRACE)
14447 {
14448 is_initialized = SD_INITIALIZED;
14449 initialization_kind = token->type;
14450 if (maybe_range_for_decl)
14451 *maybe_range_for_decl = error_mark_node;
14452
14453 if (token->type == CPP_EQ
14454 && function_declarator_p (declarator))
14455 {
14456 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14457 if (t2->keyword == RID_DEFAULT)
14458 is_initialized = SD_DEFAULTED;
14459 else if (t2->keyword == RID_DELETE)
14460 is_initialized = SD_DELETED;
14461 }
14462 }
14463 else
14464 {
14465 /* If the init-declarator isn't initialized and isn't followed by a
14466 `,' or `;', it's not a valid init-declarator. */
14467 if (token->type != CPP_COMMA
14468 && token->type != CPP_SEMICOLON)
14469 {
14470 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14471 range_for_decl_p = true;
14472 else
14473 {
14474 cp_parser_error (parser, "expected initializer");
14475 return error_mark_node;
14476 }
14477 }
14478 is_initialized = SD_UNINITIALIZED;
14479 initialization_kind = CPP_EOF;
14480 }
14481
14482 /* Because start_decl has side-effects, we should only call it if we
14483 know we're going ahead. By this point, we know that we cannot
14484 possibly be looking at any other construct. */
14485 cp_parser_commit_to_tentative_parse (parser);
14486
14487 /* If the decl specifiers were bad, issue an error now that we're
14488 sure this was intended to be a declarator. Then continue
14489 declaring the variable(s), as int, to try to cut down on further
14490 errors. */
14491 if (decl_specifiers->any_specifiers_p
14492 && decl_specifiers->type == error_mark_node)
14493 {
14494 cp_parser_error (parser, "invalid type in declaration");
14495 decl_specifiers->type = integer_type_node;
14496 }
14497
14498 /* Check to see whether or not this declaration is a friend. */
14499 friend_p = cp_parser_friend_p (decl_specifiers);
14500
14501 /* Enter the newly declared entry in the symbol table. If we're
14502 processing a declaration in a class-specifier, we wait until
14503 after processing the initializer. */
14504 if (!member_p)
14505 {
14506 if (parser->in_unbraced_linkage_specification_p)
14507 decl_specifiers->storage_class = sc_extern;
14508 decl = start_decl (declarator, decl_specifiers,
14509 range_for_decl_p? SD_INITIALIZED : is_initialized,
14510 attributes, prefix_attributes,
14511 &pushed_scope);
14512 /* Adjust location of decl if declarator->id_loc is more appropriate:
14513 set, and decl wasn't merged with another decl, in which case its
14514 location would be different from input_location, and more accurate. */
14515 if (DECL_P (decl)
14516 && declarator->id_loc != UNKNOWN_LOCATION
14517 && DECL_SOURCE_LOCATION (decl) == input_location)
14518 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14519 }
14520 else if (scope)
14521 /* Enter the SCOPE. That way unqualified names appearing in the
14522 initializer will be looked up in SCOPE. */
14523 pushed_scope = push_scope (scope);
14524
14525 /* Perform deferred access control checks, now that we know in which
14526 SCOPE the declared entity resides. */
14527 if (!member_p && decl)
14528 {
14529 tree saved_current_function_decl = NULL_TREE;
14530
14531 /* If the entity being declared is a function, pretend that we
14532 are in its scope. If it is a `friend', it may have access to
14533 things that would not otherwise be accessible. */
14534 if (TREE_CODE (decl) == FUNCTION_DECL)
14535 {
14536 saved_current_function_decl = current_function_decl;
14537 current_function_decl = decl;
14538 }
14539
14540 /* Perform access checks for template parameters. */
14541 cp_parser_perform_template_parameter_access_checks (checks);
14542
14543 /* Perform the access control checks for the declarator and the
14544 decl-specifiers. */
14545 perform_deferred_access_checks ();
14546
14547 /* Restore the saved value. */
14548 if (TREE_CODE (decl) == FUNCTION_DECL)
14549 current_function_decl = saved_current_function_decl;
14550 }
14551
14552 /* Parse the initializer. */
14553 initializer = NULL_TREE;
14554 is_direct_init = false;
14555 is_non_constant_init = true;
14556 if (is_initialized)
14557 {
14558 if (function_declarator_p (declarator))
14559 {
14560 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14561 if (initialization_kind == CPP_EQ)
14562 initializer = cp_parser_pure_specifier (parser);
14563 else
14564 {
14565 /* If the declaration was erroneous, we don't really
14566 know what the user intended, so just silently
14567 consume the initializer. */
14568 if (decl != error_mark_node)
14569 error_at (initializer_start_token->location,
14570 "initializer provided for function");
14571 cp_parser_skip_to_closing_parenthesis (parser,
14572 /*recovering=*/true,
14573 /*or_comma=*/false,
14574 /*consume_paren=*/true);
14575 }
14576 }
14577 else
14578 {
14579 /* We want to record the extra mangling scope for in-class
14580 initializers of class members and initializers of static data
14581 member templates. The former is a C++0x feature which isn't
14582 implemented yet, and I expect it will involve deferring
14583 parsing of the initializer until end of class as with default
14584 arguments. So right here we only handle the latter. */
14585 if (!member_p && processing_template_decl)
14586 start_lambda_scope (decl);
14587 initializer = cp_parser_initializer (parser,
14588 &is_direct_init,
14589 &is_non_constant_init);
14590 if (!member_p && processing_template_decl)
14591 finish_lambda_scope ();
14592 }
14593 }
14594
14595 /* The old parser allows attributes to appear after a parenthesized
14596 initializer. Mark Mitchell proposed removing this functionality
14597 on the GCC mailing lists on 2002-08-13. This parser accepts the
14598 attributes -- but ignores them. */
14599 if (cp_parser_allow_gnu_extensions_p (parser)
14600 && initialization_kind == CPP_OPEN_PAREN)
14601 if (cp_parser_attributes_opt (parser))
14602 warning (OPT_Wattributes,
14603 "attributes after parenthesized initializer ignored");
14604
14605 /* For an in-class declaration, use `grokfield' to create the
14606 declaration. */
14607 if (member_p)
14608 {
14609 if (pushed_scope)
14610 {
14611 pop_scope (pushed_scope);
14612 pushed_scope = false;
14613 }
14614 decl = grokfield (declarator, decl_specifiers,
14615 initializer, !is_non_constant_init,
14616 /*asmspec=*/NULL_TREE,
14617 prefix_attributes);
14618 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14619 cp_parser_save_default_args (parser, decl);
14620 }
14621
14622 /* Finish processing the declaration. But, skip member
14623 declarations. */
14624 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14625 {
14626 cp_finish_decl (decl,
14627 initializer, !is_non_constant_init,
14628 asm_specification,
14629 /* If the initializer is in parentheses, then this is
14630 a direct-initialization, which means that an
14631 `explicit' constructor is OK. Otherwise, an
14632 `explicit' constructor cannot be used. */
14633 ((is_direct_init || !is_initialized)
14634 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14635 }
14636 else if ((cxx_dialect != cxx98) && friend_p
14637 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14638 /* Core issue #226 (C++0x only): A default template-argument
14639 shall not be specified in a friend class template
14640 declaration. */
14641 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14642 /*is_partial=*/0, /*is_friend_decl=*/1);
14643
14644 if (!friend_p && pushed_scope)
14645 pop_scope (pushed_scope);
14646
14647 return decl;
14648 }
14649
14650 /* Parse a declarator.
14651
14652 declarator:
14653 direct-declarator
14654 ptr-operator declarator
14655
14656 abstract-declarator:
14657 ptr-operator abstract-declarator [opt]
14658 direct-abstract-declarator
14659
14660 GNU Extensions:
14661
14662 declarator:
14663 attributes [opt] direct-declarator
14664 attributes [opt] ptr-operator declarator
14665
14666 abstract-declarator:
14667 attributes [opt] ptr-operator abstract-declarator [opt]
14668 attributes [opt] direct-abstract-declarator
14669
14670 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14671 detect constructor, destructor or conversion operators. It is set
14672 to -1 if the declarator is a name, and +1 if it is a
14673 function. Otherwise it is set to zero. Usually you just want to
14674 test for >0, but internally the negative value is used.
14675
14676 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14677 a decl-specifier-seq unless it declares a constructor, destructor,
14678 or conversion. It might seem that we could check this condition in
14679 semantic analysis, rather than parsing, but that makes it difficult
14680 to handle something like `f()'. We want to notice that there are
14681 no decl-specifiers, and therefore realize that this is an
14682 expression, not a declaration.)
14683
14684 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14685 the declarator is a direct-declarator of the form "(...)".
14686
14687 MEMBER_P is true iff this declarator is a member-declarator. */
14688
14689 static cp_declarator *
14690 cp_parser_declarator (cp_parser* parser,
14691 cp_parser_declarator_kind dcl_kind,
14692 int* ctor_dtor_or_conv_p,
14693 bool* parenthesized_p,
14694 bool member_p)
14695 {
14696 cp_declarator *declarator;
14697 enum tree_code code;
14698 cp_cv_quals cv_quals;
14699 tree class_type;
14700 tree attributes = NULL_TREE;
14701
14702 /* Assume this is not a constructor, destructor, or type-conversion
14703 operator. */
14704 if (ctor_dtor_or_conv_p)
14705 *ctor_dtor_or_conv_p = 0;
14706
14707 if (cp_parser_allow_gnu_extensions_p (parser))
14708 attributes = cp_parser_attributes_opt (parser);
14709
14710 /* Check for the ptr-operator production. */
14711 cp_parser_parse_tentatively (parser);
14712 /* Parse the ptr-operator. */
14713 code = cp_parser_ptr_operator (parser,
14714 &class_type,
14715 &cv_quals);
14716 /* If that worked, then we have a ptr-operator. */
14717 if (cp_parser_parse_definitely (parser))
14718 {
14719 /* If a ptr-operator was found, then this declarator was not
14720 parenthesized. */
14721 if (parenthesized_p)
14722 *parenthesized_p = true;
14723 /* The dependent declarator is optional if we are parsing an
14724 abstract-declarator. */
14725 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14726 cp_parser_parse_tentatively (parser);
14727
14728 /* Parse the dependent declarator. */
14729 declarator = cp_parser_declarator (parser, dcl_kind,
14730 /*ctor_dtor_or_conv_p=*/NULL,
14731 /*parenthesized_p=*/NULL,
14732 /*member_p=*/false);
14733
14734 /* If we are parsing an abstract-declarator, we must handle the
14735 case where the dependent declarator is absent. */
14736 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14737 && !cp_parser_parse_definitely (parser))
14738 declarator = NULL;
14739
14740 declarator = cp_parser_make_indirect_declarator
14741 (code, class_type, cv_quals, declarator);
14742 }
14743 /* Everything else is a direct-declarator. */
14744 else
14745 {
14746 if (parenthesized_p)
14747 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14748 CPP_OPEN_PAREN);
14749 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14750 ctor_dtor_or_conv_p,
14751 member_p);
14752 }
14753
14754 if (attributes && declarator && declarator != cp_error_declarator)
14755 declarator->attributes = attributes;
14756
14757 return declarator;
14758 }
14759
14760 /* Parse a direct-declarator or direct-abstract-declarator.
14761
14762 direct-declarator:
14763 declarator-id
14764 direct-declarator ( parameter-declaration-clause )
14765 cv-qualifier-seq [opt]
14766 exception-specification [opt]
14767 direct-declarator [ constant-expression [opt] ]
14768 ( declarator )
14769
14770 direct-abstract-declarator:
14771 direct-abstract-declarator [opt]
14772 ( parameter-declaration-clause )
14773 cv-qualifier-seq [opt]
14774 exception-specification [opt]
14775 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14776 ( abstract-declarator )
14777
14778 Returns a representation of the declarator. DCL_KIND is
14779 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14780 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14781 we are parsing a direct-declarator. It is
14782 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14783 of ambiguity we prefer an abstract declarator, as per
14784 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14785 cp_parser_declarator. */
14786
14787 static cp_declarator *
14788 cp_parser_direct_declarator (cp_parser* parser,
14789 cp_parser_declarator_kind dcl_kind,
14790 int* ctor_dtor_or_conv_p,
14791 bool member_p)
14792 {
14793 cp_token *token;
14794 cp_declarator *declarator = NULL;
14795 tree scope = NULL_TREE;
14796 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14797 bool saved_in_declarator_p = parser->in_declarator_p;
14798 bool first = true;
14799 tree pushed_scope = NULL_TREE;
14800
14801 while (true)
14802 {
14803 /* Peek at the next token. */
14804 token = cp_lexer_peek_token (parser->lexer);
14805 if (token->type == CPP_OPEN_PAREN)
14806 {
14807 /* This is either a parameter-declaration-clause, or a
14808 parenthesized declarator. When we know we are parsing a
14809 named declarator, it must be a parenthesized declarator
14810 if FIRST is true. For instance, `(int)' is a
14811 parameter-declaration-clause, with an omitted
14812 direct-abstract-declarator. But `((*))', is a
14813 parenthesized abstract declarator. Finally, when T is a
14814 template parameter `(T)' is a
14815 parameter-declaration-clause, and not a parenthesized
14816 named declarator.
14817
14818 We first try and parse a parameter-declaration-clause,
14819 and then try a nested declarator (if FIRST is true).
14820
14821 It is not an error for it not to be a
14822 parameter-declaration-clause, even when FIRST is
14823 false. Consider,
14824
14825 int i (int);
14826 int i (3);
14827
14828 The first is the declaration of a function while the
14829 second is the definition of a variable, including its
14830 initializer.
14831
14832 Having seen only the parenthesis, we cannot know which of
14833 these two alternatives should be selected. Even more
14834 complex are examples like:
14835
14836 int i (int (a));
14837 int i (int (3));
14838
14839 The former is a function-declaration; the latter is a
14840 variable initialization.
14841
14842 Thus again, we try a parameter-declaration-clause, and if
14843 that fails, we back out and return. */
14844
14845 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14846 {
14847 tree params;
14848 unsigned saved_num_template_parameter_lists;
14849 bool is_declarator = false;
14850 tree t;
14851
14852 /* In a member-declarator, the only valid interpretation
14853 of a parenthesis is the start of a
14854 parameter-declaration-clause. (It is invalid to
14855 initialize a static data member with a parenthesized
14856 initializer; only the "=" form of initialization is
14857 permitted.) */
14858 if (!member_p)
14859 cp_parser_parse_tentatively (parser);
14860
14861 /* Consume the `('. */
14862 cp_lexer_consume_token (parser->lexer);
14863 if (first)
14864 {
14865 /* If this is going to be an abstract declarator, we're
14866 in a declarator and we can't have default args. */
14867 parser->default_arg_ok_p = false;
14868 parser->in_declarator_p = true;
14869 }
14870
14871 /* Inside the function parameter list, surrounding
14872 template-parameter-lists do not apply. */
14873 saved_num_template_parameter_lists
14874 = parser->num_template_parameter_lists;
14875 parser->num_template_parameter_lists = 0;
14876
14877 begin_scope (sk_function_parms, NULL_TREE);
14878
14879 /* Parse the parameter-declaration-clause. */
14880 params = cp_parser_parameter_declaration_clause (parser);
14881
14882 parser->num_template_parameter_lists
14883 = saved_num_template_parameter_lists;
14884
14885 /* If all went well, parse the cv-qualifier-seq and the
14886 exception-specification. */
14887 if (member_p || cp_parser_parse_definitely (parser))
14888 {
14889 cp_cv_quals cv_quals;
14890 tree exception_specification;
14891 tree late_return;
14892
14893 is_declarator = true;
14894
14895 if (ctor_dtor_or_conv_p)
14896 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14897 first = false;
14898 /* Consume the `)'. */
14899 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14900
14901 /* Parse the cv-qualifier-seq. */
14902 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14903 /* And the exception-specification. */
14904 exception_specification
14905 = cp_parser_exception_specification_opt (parser);
14906
14907 late_return
14908 = cp_parser_late_return_type_opt (parser);
14909
14910 /* Create the function-declarator. */
14911 declarator = make_call_declarator (declarator,
14912 params,
14913 cv_quals,
14914 exception_specification,
14915 late_return);
14916 /* Any subsequent parameter lists are to do with
14917 return type, so are not those of the declared
14918 function. */
14919 parser->default_arg_ok_p = false;
14920 }
14921
14922 /* Remove the function parms from scope. */
14923 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14924 pop_binding (DECL_NAME (t), t);
14925 leave_scope();
14926
14927 if (is_declarator)
14928 /* Repeat the main loop. */
14929 continue;
14930 }
14931
14932 /* If this is the first, we can try a parenthesized
14933 declarator. */
14934 if (first)
14935 {
14936 bool saved_in_type_id_in_expr_p;
14937
14938 parser->default_arg_ok_p = saved_default_arg_ok_p;
14939 parser->in_declarator_p = saved_in_declarator_p;
14940
14941 /* Consume the `('. */
14942 cp_lexer_consume_token (parser->lexer);
14943 /* Parse the nested declarator. */
14944 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14945 parser->in_type_id_in_expr_p = true;
14946 declarator
14947 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14948 /*parenthesized_p=*/NULL,
14949 member_p);
14950 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14951 first = false;
14952 /* Expect a `)'. */
14953 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14954 declarator = cp_error_declarator;
14955 if (declarator == cp_error_declarator)
14956 break;
14957
14958 goto handle_declarator;
14959 }
14960 /* Otherwise, we must be done. */
14961 else
14962 break;
14963 }
14964 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14965 && token->type == CPP_OPEN_SQUARE)
14966 {
14967 /* Parse an array-declarator. */
14968 tree bounds;
14969
14970 if (ctor_dtor_or_conv_p)
14971 *ctor_dtor_or_conv_p = 0;
14972
14973 first = false;
14974 parser->default_arg_ok_p = false;
14975 parser->in_declarator_p = true;
14976 /* Consume the `['. */
14977 cp_lexer_consume_token (parser->lexer);
14978 /* Peek at the next token. */
14979 token = cp_lexer_peek_token (parser->lexer);
14980 /* If the next token is `]', then there is no
14981 constant-expression. */
14982 if (token->type != CPP_CLOSE_SQUARE)
14983 {
14984 bool non_constant_p;
14985
14986 bounds
14987 = cp_parser_constant_expression (parser,
14988 /*allow_non_constant=*/true,
14989 &non_constant_p);
14990 if (!non_constant_p)
14991 /* OK */;
14992 /* Normally, the array bound must be an integral constant
14993 expression. However, as an extension, we allow VLAs
14994 in function scopes as long as they aren't part of a
14995 parameter declaration. */
14996 else if (!parser->in_function_body
14997 || current_binding_level->kind == sk_function_parms)
14998 {
14999 cp_parser_error (parser,
15000 "array bound is not an integer constant");
15001 bounds = error_mark_node;
15002 }
15003 else if (processing_template_decl && !error_operand_p (bounds))
15004 {
15005 /* Remember this wasn't a constant-expression. */
15006 bounds = build_nop (TREE_TYPE (bounds), bounds);
15007 TREE_SIDE_EFFECTS (bounds) = 1;
15008 }
15009 }
15010 else
15011 bounds = NULL_TREE;
15012 /* Look for the closing `]'. */
15013 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15014 {
15015 declarator = cp_error_declarator;
15016 break;
15017 }
15018
15019 declarator = make_array_declarator (declarator, bounds);
15020 }
15021 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15022 {
15023 {
15024 tree qualifying_scope;
15025 tree unqualified_name;
15026 special_function_kind sfk;
15027 bool abstract_ok;
15028 bool pack_expansion_p = false;
15029 cp_token *declarator_id_start_token;
15030
15031 /* Parse a declarator-id */
15032 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15033 if (abstract_ok)
15034 {
15035 cp_parser_parse_tentatively (parser);
15036
15037 /* If we see an ellipsis, we should be looking at a
15038 parameter pack. */
15039 if (token->type == CPP_ELLIPSIS)
15040 {
15041 /* Consume the `...' */
15042 cp_lexer_consume_token (parser->lexer);
15043
15044 pack_expansion_p = true;
15045 }
15046 }
15047
15048 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15049 unqualified_name
15050 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15051 qualifying_scope = parser->scope;
15052 if (abstract_ok)
15053 {
15054 bool okay = false;
15055
15056 if (!unqualified_name && pack_expansion_p)
15057 {
15058 /* Check whether an error occurred. */
15059 okay = !cp_parser_error_occurred (parser);
15060
15061 /* We already consumed the ellipsis to mark a
15062 parameter pack, but we have no way to report it,
15063 so abort the tentative parse. We will be exiting
15064 immediately anyway. */
15065 cp_parser_abort_tentative_parse (parser);
15066 }
15067 else
15068 okay = cp_parser_parse_definitely (parser);
15069
15070 if (!okay)
15071 unqualified_name = error_mark_node;
15072 else if (unqualified_name
15073 && (qualifying_scope
15074 || (TREE_CODE (unqualified_name)
15075 != IDENTIFIER_NODE)))
15076 {
15077 cp_parser_error (parser, "expected unqualified-id");
15078 unqualified_name = error_mark_node;
15079 }
15080 }
15081
15082 if (!unqualified_name)
15083 return NULL;
15084 if (unqualified_name == error_mark_node)
15085 {
15086 declarator = cp_error_declarator;
15087 pack_expansion_p = false;
15088 declarator->parameter_pack_p = false;
15089 break;
15090 }
15091
15092 if (qualifying_scope && at_namespace_scope_p ()
15093 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15094 {
15095 /* In the declaration of a member of a template class
15096 outside of the class itself, the SCOPE will sometimes
15097 be a TYPENAME_TYPE. For example, given:
15098
15099 template <typename T>
15100 int S<T>::R::i = 3;
15101
15102 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15103 this context, we must resolve S<T>::R to an ordinary
15104 type, rather than a typename type.
15105
15106 The reason we normally avoid resolving TYPENAME_TYPEs
15107 is that a specialization of `S' might render
15108 `S<T>::R' not a type. However, if `S' is
15109 specialized, then this `i' will not be used, so there
15110 is no harm in resolving the types here. */
15111 tree type;
15112
15113 /* Resolve the TYPENAME_TYPE. */
15114 type = resolve_typename_type (qualifying_scope,
15115 /*only_current_p=*/false);
15116 /* If that failed, the declarator is invalid. */
15117 if (TREE_CODE (type) == TYPENAME_TYPE)
15118 {
15119 if (typedef_variant_p (type))
15120 error_at (declarator_id_start_token->location,
15121 "cannot define member of dependent typedef "
15122 "%qT", type);
15123 else
15124 error_at (declarator_id_start_token->location,
15125 "%<%T::%E%> is not a type",
15126 TYPE_CONTEXT (qualifying_scope),
15127 TYPE_IDENTIFIER (qualifying_scope));
15128 }
15129 qualifying_scope = type;
15130 }
15131
15132 sfk = sfk_none;
15133
15134 if (unqualified_name)
15135 {
15136 tree class_type;
15137
15138 if (qualifying_scope
15139 && CLASS_TYPE_P (qualifying_scope))
15140 class_type = qualifying_scope;
15141 else
15142 class_type = current_class_type;
15143
15144 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15145 {
15146 tree name_type = TREE_TYPE (unqualified_name);
15147 if (class_type && same_type_p (name_type, class_type))
15148 {
15149 if (qualifying_scope
15150 && CLASSTYPE_USE_TEMPLATE (name_type))
15151 {
15152 error_at (declarator_id_start_token->location,
15153 "invalid use of constructor as a template");
15154 inform (declarator_id_start_token->location,
15155 "use %<%T::%D%> instead of %<%T::%D%> to "
15156 "name the constructor in a qualified name",
15157 class_type,
15158 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15159 class_type, name_type);
15160 declarator = cp_error_declarator;
15161 break;
15162 }
15163 else
15164 unqualified_name = constructor_name (class_type);
15165 }
15166 else
15167 {
15168 /* We do not attempt to print the declarator
15169 here because we do not have enough
15170 information about its original syntactic
15171 form. */
15172 cp_parser_error (parser, "invalid declarator");
15173 declarator = cp_error_declarator;
15174 break;
15175 }
15176 }
15177
15178 if (class_type)
15179 {
15180 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15181 sfk = sfk_destructor;
15182 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15183 sfk = sfk_conversion;
15184 else if (/* There's no way to declare a constructor
15185 for an anonymous type, even if the type
15186 got a name for linkage purposes. */
15187 !TYPE_WAS_ANONYMOUS (class_type)
15188 && constructor_name_p (unqualified_name,
15189 class_type))
15190 {
15191 unqualified_name = constructor_name (class_type);
15192 sfk = sfk_constructor;
15193 }
15194 else if (is_overloaded_fn (unqualified_name)
15195 && DECL_CONSTRUCTOR_P (get_first_fn
15196 (unqualified_name)))
15197 sfk = sfk_constructor;
15198
15199 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15200 *ctor_dtor_or_conv_p = -1;
15201 }
15202 }
15203 declarator = make_id_declarator (qualifying_scope,
15204 unqualified_name,
15205 sfk);
15206 declarator->id_loc = token->location;
15207 declarator->parameter_pack_p = pack_expansion_p;
15208
15209 if (pack_expansion_p)
15210 maybe_warn_variadic_templates ();
15211 }
15212
15213 handle_declarator:;
15214 scope = get_scope_of_declarator (declarator);
15215 if (scope)
15216 /* Any names that appear after the declarator-id for a
15217 member are looked up in the containing scope. */
15218 pushed_scope = push_scope (scope);
15219 parser->in_declarator_p = true;
15220 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15221 || (declarator && declarator->kind == cdk_id))
15222 /* Default args are only allowed on function
15223 declarations. */
15224 parser->default_arg_ok_p = saved_default_arg_ok_p;
15225 else
15226 parser->default_arg_ok_p = false;
15227
15228 first = false;
15229 }
15230 /* We're done. */
15231 else
15232 break;
15233 }
15234
15235 /* For an abstract declarator, we might wind up with nothing at this
15236 point. That's an error; the declarator is not optional. */
15237 if (!declarator)
15238 cp_parser_error (parser, "expected declarator");
15239
15240 /* If we entered a scope, we must exit it now. */
15241 if (pushed_scope)
15242 pop_scope (pushed_scope);
15243
15244 parser->default_arg_ok_p = saved_default_arg_ok_p;
15245 parser->in_declarator_p = saved_in_declarator_p;
15246
15247 return declarator;
15248 }
15249
15250 /* Parse a ptr-operator.
15251
15252 ptr-operator:
15253 * cv-qualifier-seq [opt]
15254 &
15255 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15256
15257 GNU Extension:
15258
15259 ptr-operator:
15260 & cv-qualifier-seq [opt]
15261
15262 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15263 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15264 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15265 filled in with the TYPE containing the member. *CV_QUALS is
15266 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15267 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15268 Note that the tree codes returned by this function have nothing
15269 to do with the types of trees that will be eventually be created
15270 to represent the pointer or reference type being parsed. They are
15271 just constants with suggestive names. */
15272 static enum tree_code
15273 cp_parser_ptr_operator (cp_parser* parser,
15274 tree* type,
15275 cp_cv_quals *cv_quals)
15276 {
15277 enum tree_code code = ERROR_MARK;
15278 cp_token *token;
15279
15280 /* Assume that it's not a pointer-to-member. */
15281 *type = NULL_TREE;
15282 /* And that there are no cv-qualifiers. */
15283 *cv_quals = TYPE_UNQUALIFIED;
15284
15285 /* Peek at the next token. */
15286 token = cp_lexer_peek_token (parser->lexer);
15287
15288 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15289 if (token->type == CPP_MULT)
15290 code = INDIRECT_REF;
15291 else if (token->type == CPP_AND)
15292 code = ADDR_EXPR;
15293 else if ((cxx_dialect != cxx98) &&
15294 token->type == CPP_AND_AND) /* C++0x only */
15295 code = NON_LVALUE_EXPR;
15296
15297 if (code != ERROR_MARK)
15298 {
15299 /* Consume the `*', `&' or `&&'. */
15300 cp_lexer_consume_token (parser->lexer);
15301
15302 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15303 `&', if we are allowing GNU extensions. (The only qualifier
15304 that can legally appear after `&' is `restrict', but that is
15305 enforced during semantic analysis. */
15306 if (code == INDIRECT_REF
15307 || cp_parser_allow_gnu_extensions_p (parser))
15308 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15309 }
15310 else
15311 {
15312 /* Try the pointer-to-member case. */
15313 cp_parser_parse_tentatively (parser);
15314 /* Look for the optional `::' operator. */
15315 cp_parser_global_scope_opt (parser,
15316 /*current_scope_valid_p=*/false);
15317 /* Look for the nested-name specifier. */
15318 token = cp_lexer_peek_token (parser->lexer);
15319 cp_parser_nested_name_specifier (parser,
15320 /*typename_keyword_p=*/false,
15321 /*check_dependency_p=*/true,
15322 /*type_p=*/false,
15323 /*is_declaration=*/false);
15324 /* If we found it, and the next token is a `*', then we are
15325 indeed looking at a pointer-to-member operator. */
15326 if (!cp_parser_error_occurred (parser)
15327 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15328 {
15329 /* Indicate that the `*' operator was used. */
15330 code = INDIRECT_REF;
15331
15332 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15333 error_at (token->location, "%qD is a namespace", parser->scope);
15334 else
15335 {
15336 /* The type of which the member is a member is given by the
15337 current SCOPE. */
15338 *type = parser->scope;
15339 /* The next name will not be qualified. */
15340 parser->scope = NULL_TREE;
15341 parser->qualifying_scope = NULL_TREE;
15342 parser->object_scope = NULL_TREE;
15343 /* Look for the optional cv-qualifier-seq. */
15344 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15345 }
15346 }
15347 /* If that didn't work we don't have a ptr-operator. */
15348 if (!cp_parser_parse_definitely (parser))
15349 cp_parser_error (parser, "expected ptr-operator");
15350 }
15351
15352 return code;
15353 }
15354
15355 /* Parse an (optional) cv-qualifier-seq.
15356
15357 cv-qualifier-seq:
15358 cv-qualifier cv-qualifier-seq [opt]
15359
15360 cv-qualifier:
15361 const
15362 volatile
15363
15364 GNU Extension:
15365
15366 cv-qualifier:
15367 __restrict__
15368
15369 Returns a bitmask representing the cv-qualifiers. */
15370
15371 static cp_cv_quals
15372 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15373 {
15374 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15375
15376 while (true)
15377 {
15378 cp_token *token;
15379 cp_cv_quals cv_qualifier;
15380
15381 /* Peek at the next token. */
15382 token = cp_lexer_peek_token (parser->lexer);
15383 /* See if it's a cv-qualifier. */
15384 switch (token->keyword)
15385 {
15386 case RID_CONST:
15387 cv_qualifier = TYPE_QUAL_CONST;
15388 break;
15389
15390 case RID_VOLATILE:
15391 cv_qualifier = TYPE_QUAL_VOLATILE;
15392 break;
15393
15394 case RID_RESTRICT:
15395 cv_qualifier = TYPE_QUAL_RESTRICT;
15396 break;
15397
15398 default:
15399 cv_qualifier = TYPE_UNQUALIFIED;
15400 break;
15401 }
15402
15403 if (!cv_qualifier)
15404 break;
15405
15406 if (cv_quals & cv_qualifier)
15407 {
15408 error_at (token->location, "duplicate cv-qualifier");
15409 cp_lexer_purge_token (parser->lexer);
15410 }
15411 else
15412 {
15413 cp_lexer_consume_token (parser->lexer);
15414 cv_quals |= cv_qualifier;
15415 }
15416 }
15417
15418 return cv_quals;
15419 }
15420
15421 /* Parse a late-specified return type, if any. This is not a separate
15422 non-terminal, but part of a function declarator, which looks like
15423
15424 -> trailing-type-specifier-seq abstract-declarator(opt)
15425
15426 Returns the type indicated by the type-id. */
15427
15428 static tree
15429 cp_parser_late_return_type_opt (cp_parser* parser)
15430 {
15431 cp_token *token;
15432
15433 /* Peek at the next token. */
15434 token = cp_lexer_peek_token (parser->lexer);
15435 /* A late-specified return type is indicated by an initial '->'. */
15436 if (token->type != CPP_DEREF)
15437 return NULL_TREE;
15438
15439 /* Consume the ->. */
15440 cp_lexer_consume_token (parser->lexer);
15441
15442 return cp_parser_trailing_type_id (parser);
15443 }
15444
15445 /* Parse a declarator-id.
15446
15447 declarator-id:
15448 id-expression
15449 :: [opt] nested-name-specifier [opt] type-name
15450
15451 In the `id-expression' case, the value returned is as for
15452 cp_parser_id_expression if the id-expression was an unqualified-id.
15453 If the id-expression was a qualified-id, then a SCOPE_REF is
15454 returned. The first operand is the scope (either a NAMESPACE_DECL
15455 or TREE_TYPE), but the second is still just a representation of an
15456 unqualified-id. */
15457
15458 static tree
15459 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15460 {
15461 tree id;
15462 /* The expression must be an id-expression. Assume that qualified
15463 names are the names of types so that:
15464
15465 template <class T>
15466 int S<T>::R::i = 3;
15467
15468 will work; we must treat `S<T>::R' as the name of a type.
15469 Similarly, assume that qualified names are templates, where
15470 required, so that:
15471
15472 template <class T>
15473 int S<T>::R<T>::i = 3;
15474
15475 will work, too. */
15476 id = cp_parser_id_expression (parser,
15477 /*template_keyword_p=*/false,
15478 /*check_dependency_p=*/false,
15479 /*template_p=*/NULL,
15480 /*declarator_p=*/true,
15481 optional_p);
15482 if (id && BASELINK_P (id))
15483 id = BASELINK_FUNCTIONS (id);
15484 return id;
15485 }
15486
15487 /* Parse a type-id.
15488
15489 type-id:
15490 type-specifier-seq abstract-declarator [opt]
15491
15492 Returns the TYPE specified. */
15493
15494 static tree
15495 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15496 bool is_trailing_return)
15497 {
15498 cp_decl_specifier_seq type_specifier_seq;
15499 cp_declarator *abstract_declarator;
15500
15501 /* Parse the type-specifier-seq. */
15502 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15503 is_trailing_return,
15504 &type_specifier_seq);
15505 if (type_specifier_seq.type == error_mark_node)
15506 return error_mark_node;
15507
15508 /* There might or might not be an abstract declarator. */
15509 cp_parser_parse_tentatively (parser);
15510 /* Look for the declarator. */
15511 abstract_declarator
15512 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15513 /*parenthesized_p=*/NULL,
15514 /*member_p=*/false);
15515 /* Check to see if there really was a declarator. */
15516 if (!cp_parser_parse_definitely (parser))
15517 abstract_declarator = NULL;
15518
15519 if (type_specifier_seq.type
15520 && type_uses_auto (type_specifier_seq.type))
15521 {
15522 /* A type-id with type 'auto' is only ok if the abstract declarator
15523 is a function declarator with a late-specified return type. */
15524 if (abstract_declarator
15525 && abstract_declarator->kind == cdk_function
15526 && abstract_declarator->u.function.late_return_type)
15527 /* OK */;
15528 else
15529 {
15530 error ("invalid use of %<auto%>");
15531 return error_mark_node;
15532 }
15533 }
15534
15535 return groktypename (&type_specifier_seq, abstract_declarator,
15536 is_template_arg);
15537 }
15538
15539 static tree cp_parser_type_id (cp_parser *parser)
15540 {
15541 return cp_parser_type_id_1 (parser, false, false);
15542 }
15543
15544 static tree cp_parser_template_type_arg (cp_parser *parser)
15545 {
15546 tree r;
15547 const char *saved_message = parser->type_definition_forbidden_message;
15548 parser->type_definition_forbidden_message
15549 = G_("types may not be defined in template arguments");
15550 r = cp_parser_type_id_1 (parser, true, false);
15551 parser->type_definition_forbidden_message = saved_message;
15552 return r;
15553 }
15554
15555 static tree cp_parser_trailing_type_id (cp_parser *parser)
15556 {
15557 return cp_parser_type_id_1 (parser, false, true);
15558 }
15559
15560 /* Parse a type-specifier-seq.
15561
15562 type-specifier-seq:
15563 type-specifier type-specifier-seq [opt]
15564
15565 GNU extension:
15566
15567 type-specifier-seq:
15568 attributes type-specifier-seq [opt]
15569
15570 If IS_DECLARATION is true, we are at the start of a "condition" or
15571 exception-declaration, so we might be followed by a declarator-id.
15572
15573 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15574 i.e. we've just seen "->".
15575
15576 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15577
15578 static void
15579 cp_parser_type_specifier_seq (cp_parser* parser,
15580 bool is_declaration,
15581 bool is_trailing_return,
15582 cp_decl_specifier_seq *type_specifier_seq)
15583 {
15584 bool seen_type_specifier = false;
15585 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15586 cp_token *start_token = NULL;
15587
15588 /* Clear the TYPE_SPECIFIER_SEQ. */
15589 clear_decl_specs (type_specifier_seq);
15590
15591 /* In the context of a trailing return type, enum E { } is an
15592 elaborated-type-specifier followed by a function-body, not an
15593 enum-specifier. */
15594 if (is_trailing_return)
15595 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15596
15597 /* Parse the type-specifiers and attributes. */
15598 while (true)
15599 {
15600 tree type_specifier;
15601 bool is_cv_qualifier;
15602
15603 /* Check for attributes first. */
15604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15605 {
15606 type_specifier_seq->attributes =
15607 chainon (type_specifier_seq->attributes,
15608 cp_parser_attributes_opt (parser));
15609 continue;
15610 }
15611
15612 /* record the token of the beginning of the type specifier seq,
15613 for error reporting purposes*/
15614 if (!start_token)
15615 start_token = cp_lexer_peek_token (parser->lexer);
15616
15617 /* Look for the type-specifier. */
15618 type_specifier = cp_parser_type_specifier (parser,
15619 flags,
15620 type_specifier_seq,
15621 /*is_declaration=*/false,
15622 NULL,
15623 &is_cv_qualifier);
15624 if (!type_specifier)
15625 {
15626 /* If the first type-specifier could not be found, this is not a
15627 type-specifier-seq at all. */
15628 if (!seen_type_specifier)
15629 {
15630 cp_parser_error (parser, "expected type-specifier");
15631 type_specifier_seq->type = error_mark_node;
15632 return;
15633 }
15634 /* If subsequent type-specifiers could not be found, the
15635 type-specifier-seq is complete. */
15636 break;
15637 }
15638
15639 seen_type_specifier = true;
15640 /* The standard says that a condition can be:
15641
15642 type-specifier-seq declarator = assignment-expression
15643
15644 However, given:
15645
15646 struct S {};
15647 if (int S = ...)
15648
15649 we should treat the "S" as a declarator, not as a
15650 type-specifier. The standard doesn't say that explicitly for
15651 type-specifier-seq, but it does say that for
15652 decl-specifier-seq in an ordinary declaration. Perhaps it
15653 would be clearer just to allow a decl-specifier-seq here, and
15654 then add a semantic restriction that if any decl-specifiers
15655 that are not type-specifiers appear, the program is invalid. */
15656 if (is_declaration && !is_cv_qualifier)
15657 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15658 }
15659
15660 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15661 }
15662
15663 /* Parse a parameter-declaration-clause.
15664
15665 parameter-declaration-clause:
15666 parameter-declaration-list [opt] ... [opt]
15667 parameter-declaration-list , ...
15668
15669 Returns a representation for the parameter declarations. A return
15670 value of NULL indicates a parameter-declaration-clause consisting
15671 only of an ellipsis. */
15672
15673 static tree
15674 cp_parser_parameter_declaration_clause (cp_parser* parser)
15675 {
15676 tree parameters;
15677 cp_token *token;
15678 bool ellipsis_p;
15679 bool is_error;
15680
15681 /* Peek at the next token. */
15682 token = cp_lexer_peek_token (parser->lexer);
15683 /* Check for trivial parameter-declaration-clauses. */
15684 if (token->type == CPP_ELLIPSIS)
15685 {
15686 /* Consume the `...' token. */
15687 cp_lexer_consume_token (parser->lexer);
15688 return NULL_TREE;
15689 }
15690 else if (token->type == CPP_CLOSE_PAREN)
15691 /* There are no parameters. */
15692 {
15693 #ifndef NO_IMPLICIT_EXTERN_C
15694 if (in_system_header && current_class_type == NULL
15695 && current_lang_name == lang_name_c)
15696 return NULL_TREE;
15697 else
15698 #endif
15699 return void_list_node;
15700 }
15701 /* Check for `(void)', too, which is a special case. */
15702 else if (token->keyword == RID_VOID
15703 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15704 == CPP_CLOSE_PAREN))
15705 {
15706 /* Consume the `void' token. */
15707 cp_lexer_consume_token (parser->lexer);
15708 /* There are no parameters. */
15709 return void_list_node;
15710 }
15711
15712 /* Parse the parameter-declaration-list. */
15713 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15714 /* If a parse error occurred while parsing the
15715 parameter-declaration-list, then the entire
15716 parameter-declaration-clause is erroneous. */
15717 if (is_error)
15718 return NULL;
15719
15720 /* Peek at the next token. */
15721 token = cp_lexer_peek_token (parser->lexer);
15722 /* If it's a `,', the clause should terminate with an ellipsis. */
15723 if (token->type == CPP_COMMA)
15724 {
15725 /* Consume the `,'. */
15726 cp_lexer_consume_token (parser->lexer);
15727 /* Expect an ellipsis. */
15728 ellipsis_p
15729 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15730 }
15731 /* It might also be `...' if the optional trailing `,' was
15732 omitted. */
15733 else if (token->type == CPP_ELLIPSIS)
15734 {
15735 /* Consume the `...' token. */
15736 cp_lexer_consume_token (parser->lexer);
15737 /* And remember that we saw it. */
15738 ellipsis_p = true;
15739 }
15740 else
15741 ellipsis_p = false;
15742
15743 /* Finish the parameter list. */
15744 if (!ellipsis_p)
15745 parameters = chainon (parameters, void_list_node);
15746
15747 return parameters;
15748 }
15749
15750 /* Parse a parameter-declaration-list.
15751
15752 parameter-declaration-list:
15753 parameter-declaration
15754 parameter-declaration-list , parameter-declaration
15755
15756 Returns a representation of the parameter-declaration-list, as for
15757 cp_parser_parameter_declaration_clause. However, the
15758 `void_list_node' is never appended to the list. Upon return,
15759 *IS_ERROR will be true iff an error occurred. */
15760
15761 static tree
15762 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15763 {
15764 tree parameters = NULL_TREE;
15765 tree *tail = &parameters;
15766 bool saved_in_unbraced_linkage_specification_p;
15767 int index = 0;
15768
15769 /* Assume all will go well. */
15770 *is_error = false;
15771 /* The special considerations that apply to a function within an
15772 unbraced linkage specifications do not apply to the parameters
15773 to the function. */
15774 saved_in_unbraced_linkage_specification_p
15775 = parser->in_unbraced_linkage_specification_p;
15776 parser->in_unbraced_linkage_specification_p = false;
15777
15778 /* Look for more parameters. */
15779 while (true)
15780 {
15781 cp_parameter_declarator *parameter;
15782 tree decl = error_mark_node;
15783 bool parenthesized_p;
15784 /* Parse the parameter. */
15785 parameter
15786 = cp_parser_parameter_declaration (parser,
15787 /*template_parm_p=*/false,
15788 &parenthesized_p);
15789
15790 /* We don't know yet if the enclosing context is deprecated, so wait
15791 and warn in grokparms if appropriate. */
15792 deprecated_state = DEPRECATED_SUPPRESS;
15793
15794 if (parameter)
15795 decl = grokdeclarator (parameter->declarator,
15796 &parameter->decl_specifiers,
15797 PARM,
15798 parameter->default_argument != NULL_TREE,
15799 &parameter->decl_specifiers.attributes);
15800
15801 deprecated_state = DEPRECATED_NORMAL;
15802
15803 /* If a parse error occurred parsing the parameter declaration,
15804 then the entire parameter-declaration-list is erroneous. */
15805 if (decl == error_mark_node)
15806 {
15807 *is_error = true;
15808 parameters = error_mark_node;
15809 break;
15810 }
15811
15812 if (parameter->decl_specifiers.attributes)
15813 cplus_decl_attributes (&decl,
15814 parameter->decl_specifiers.attributes,
15815 0);
15816 if (DECL_NAME (decl))
15817 decl = pushdecl (decl);
15818
15819 if (decl != error_mark_node)
15820 {
15821 retrofit_lang_decl (decl);
15822 DECL_PARM_INDEX (decl) = ++index;
15823 DECL_PARM_LEVEL (decl) = function_parm_depth ();
15824 }
15825
15826 /* Add the new parameter to the list. */
15827 *tail = build_tree_list (parameter->default_argument, decl);
15828 tail = &TREE_CHAIN (*tail);
15829
15830 /* Peek at the next token. */
15831 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15832 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15833 /* These are for Objective-C++ */
15834 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15835 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15836 /* The parameter-declaration-list is complete. */
15837 break;
15838 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15839 {
15840 cp_token *token;
15841
15842 /* Peek at the next token. */
15843 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15844 /* If it's an ellipsis, then the list is complete. */
15845 if (token->type == CPP_ELLIPSIS)
15846 break;
15847 /* Otherwise, there must be more parameters. Consume the
15848 `,'. */
15849 cp_lexer_consume_token (parser->lexer);
15850 /* When parsing something like:
15851
15852 int i(float f, double d)
15853
15854 we can tell after seeing the declaration for "f" that we
15855 are not looking at an initialization of a variable "i",
15856 but rather at the declaration of a function "i".
15857
15858 Due to the fact that the parsing of template arguments
15859 (as specified to a template-id) requires backtracking we
15860 cannot use this technique when inside a template argument
15861 list. */
15862 if (!parser->in_template_argument_list_p
15863 && !parser->in_type_id_in_expr_p
15864 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15865 /* However, a parameter-declaration of the form
15866 "foat(f)" (which is a valid declaration of a
15867 parameter "f") can also be interpreted as an
15868 expression (the conversion of "f" to "float"). */
15869 && !parenthesized_p)
15870 cp_parser_commit_to_tentative_parse (parser);
15871 }
15872 else
15873 {
15874 cp_parser_error (parser, "expected %<,%> or %<...%>");
15875 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15876 cp_parser_skip_to_closing_parenthesis (parser,
15877 /*recovering=*/true,
15878 /*or_comma=*/false,
15879 /*consume_paren=*/false);
15880 break;
15881 }
15882 }
15883
15884 parser->in_unbraced_linkage_specification_p
15885 = saved_in_unbraced_linkage_specification_p;
15886
15887 return parameters;
15888 }
15889
15890 /* Parse a parameter declaration.
15891
15892 parameter-declaration:
15893 decl-specifier-seq ... [opt] declarator
15894 decl-specifier-seq declarator = assignment-expression
15895 decl-specifier-seq ... [opt] abstract-declarator [opt]
15896 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15897
15898 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15899 declares a template parameter. (In that case, a non-nested `>'
15900 token encountered during the parsing of the assignment-expression
15901 is not interpreted as a greater-than operator.)
15902
15903 Returns a representation of the parameter, or NULL if an error
15904 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15905 true iff the declarator is of the form "(p)". */
15906
15907 static cp_parameter_declarator *
15908 cp_parser_parameter_declaration (cp_parser *parser,
15909 bool template_parm_p,
15910 bool *parenthesized_p)
15911 {
15912 int declares_class_or_enum;
15913 cp_decl_specifier_seq decl_specifiers;
15914 cp_declarator *declarator;
15915 tree default_argument;
15916 cp_token *token = NULL, *declarator_token_start = NULL;
15917 const char *saved_message;
15918
15919 /* In a template parameter, `>' is not an operator.
15920
15921 [temp.param]
15922
15923 When parsing a default template-argument for a non-type
15924 template-parameter, the first non-nested `>' is taken as the end
15925 of the template parameter-list rather than a greater-than
15926 operator. */
15927
15928 /* Type definitions may not appear in parameter types. */
15929 saved_message = parser->type_definition_forbidden_message;
15930 parser->type_definition_forbidden_message
15931 = G_("types may not be defined in parameter types");
15932
15933 /* Parse the declaration-specifiers. */
15934 cp_parser_decl_specifier_seq (parser,
15935 CP_PARSER_FLAGS_NONE,
15936 &decl_specifiers,
15937 &declares_class_or_enum);
15938
15939 /* Complain about missing 'typename' or other invalid type names. */
15940 if (!decl_specifiers.any_type_specifiers_p)
15941 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15942
15943 /* If an error occurred, there's no reason to attempt to parse the
15944 rest of the declaration. */
15945 if (cp_parser_error_occurred (parser))
15946 {
15947 parser->type_definition_forbidden_message = saved_message;
15948 return NULL;
15949 }
15950
15951 /* Peek at the next token. */
15952 token = cp_lexer_peek_token (parser->lexer);
15953
15954 /* If the next token is a `)', `,', `=', `>', or `...', then there
15955 is no declarator. However, when variadic templates are enabled,
15956 there may be a declarator following `...'. */
15957 if (token->type == CPP_CLOSE_PAREN
15958 || token->type == CPP_COMMA
15959 || token->type == CPP_EQ
15960 || token->type == CPP_GREATER)
15961 {
15962 declarator = NULL;
15963 if (parenthesized_p)
15964 *parenthesized_p = false;
15965 }
15966 /* Otherwise, there should be a declarator. */
15967 else
15968 {
15969 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15970 parser->default_arg_ok_p = false;
15971
15972 /* After seeing a decl-specifier-seq, if the next token is not a
15973 "(", there is no possibility that the code is a valid
15974 expression. Therefore, if parsing tentatively, we commit at
15975 this point. */
15976 if (!parser->in_template_argument_list_p
15977 /* In an expression context, having seen:
15978
15979 (int((char ...
15980
15981 we cannot be sure whether we are looking at a
15982 function-type (taking a "char" as a parameter) or a cast
15983 of some object of type "char" to "int". */
15984 && !parser->in_type_id_in_expr_p
15985 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15986 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15987 cp_parser_commit_to_tentative_parse (parser);
15988 /* Parse the declarator. */
15989 declarator_token_start = token;
15990 declarator = cp_parser_declarator (parser,
15991 CP_PARSER_DECLARATOR_EITHER,
15992 /*ctor_dtor_or_conv_p=*/NULL,
15993 parenthesized_p,
15994 /*member_p=*/false);
15995 parser->default_arg_ok_p = saved_default_arg_ok_p;
15996 /* After the declarator, allow more attributes. */
15997 decl_specifiers.attributes
15998 = chainon (decl_specifiers.attributes,
15999 cp_parser_attributes_opt (parser));
16000 }
16001
16002 /* If the next token is an ellipsis, and we have not seen a
16003 declarator name, and the type of the declarator contains parameter
16004 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16005 a parameter pack expansion expression. Otherwise, leave the
16006 ellipsis for a C-style variadic function. */
16007 token = cp_lexer_peek_token (parser->lexer);
16008 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16009 {
16010 tree type = decl_specifiers.type;
16011
16012 if (type && DECL_P (type))
16013 type = TREE_TYPE (type);
16014
16015 if (type
16016 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16017 && declarator_can_be_parameter_pack (declarator)
16018 && (!declarator || !declarator->parameter_pack_p)
16019 && uses_parameter_packs (type))
16020 {
16021 /* Consume the `...'. */
16022 cp_lexer_consume_token (parser->lexer);
16023 maybe_warn_variadic_templates ();
16024
16025 /* Build a pack expansion type */
16026 if (declarator)
16027 declarator->parameter_pack_p = true;
16028 else
16029 decl_specifiers.type = make_pack_expansion (type);
16030 }
16031 }
16032
16033 /* The restriction on defining new types applies only to the type
16034 of the parameter, not to the default argument. */
16035 parser->type_definition_forbidden_message = saved_message;
16036
16037 /* If the next token is `=', then process a default argument. */
16038 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16039 {
16040 /* Consume the `='. */
16041 cp_lexer_consume_token (parser->lexer);
16042
16043 /* If we are defining a class, then the tokens that make up the
16044 default argument must be saved and processed later. */
16045 if (!template_parm_p && at_class_scope_p ()
16046 && TYPE_BEING_DEFINED (current_class_type)
16047 && !LAMBDA_TYPE_P (current_class_type))
16048 {
16049 unsigned depth = 0;
16050 int maybe_template_id = 0;
16051 cp_token *first_token;
16052 cp_token *token;
16053
16054 /* Add tokens until we have processed the entire default
16055 argument. We add the range [first_token, token). */
16056 first_token = cp_lexer_peek_token (parser->lexer);
16057 while (true)
16058 {
16059 bool done = false;
16060
16061 /* Peek at the next token. */
16062 token = cp_lexer_peek_token (parser->lexer);
16063 /* What we do depends on what token we have. */
16064 switch (token->type)
16065 {
16066 /* In valid code, a default argument must be
16067 immediately followed by a `,' `)', or `...'. */
16068 case CPP_COMMA:
16069 if (depth == 0 && maybe_template_id)
16070 {
16071 /* If we've seen a '<', we might be in a
16072 template-argument-list. Until Core issue 325 is
16073 resolved, we don't know how this situation ought
16074 to be handled, so try to DTRT. We check whether
16075 what comes after the comma is a valid parameter
16076 declaration list. If it is, then the comma ends
16077 the default argument; otherwise the default
16078 argument continues. */
16079 bool error = false;
16080 tree t;
16081
16082 /* Set ITALP so cp_parser_parameter_declaration_list
16083 doesn't decide to commit to this parse. */
16084 bool saved_italp = parser->in_template_argument_list_p;
16085 parser->in_template_argument_list_p = true;
16086
16087 cp_parser_parse_tentatively (parser);
16088 cp_lexer_consume_token (parser->lexer);
16089 begin_scope (sk_function_parms, NULL_TREE);
16090 cp_parser_parameter_declaration_list (parser, &error);
16091 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16092 pop_binding (DECL_NAME (t), t);
16093 leave_scope ();
16094 if (!cp_parser_error_occurred (parser) && !error)
16095 done = true;
16096 cp_parser_abort_tentative_parse (parser);
16097
16098 parser->in_template_argument_list_p = saved_italp;
16099 break;
16100 }
16101 case CPP_CLOSE_PAREN:
16102 case CPP_ELLIPSIS:
16103 /* If we run into a non-nested `;', `}', or `]',
16104 then the code is invalid -- but the default
16105 argument is certainly over. */
16106 case CPP_SEMICOLON:
16107 case CPP_CLOSE_BRACE:
16108 case CPP_CLOSE_SQUARE:
16109 if (depth == 0)
16110 done = true;
16111 /* Update DEPTH, if necessary. */
16112 else if (token->type == CPP_CLOSE_PAREN
16113 || token->type == CPP_CLOSE_BRACE
16114 || token->type == CPP_CLOSE_SQUARE)
16115 --depth;
16116 break;
16117
16118 case CPP_OPEN_PAREN:
16119 case CPP_OPEN_SQUARE:
16120 case CPP_OPEN_BRACE:
16121 ++depth;
16122 break;
16123
16124 case CPP_LESS:
16125 if (depth == 0)
16126 /* This might be the comparison operator, or it might
16127 start a template argument list. */
16128 ++maybe_template_id;
16129 break;
16130
16131 case CPP_RSHIFT:
16132 if (cxx_dialect == cxx98)
16133 break;
16134 /* Fall through for C++0x, which treats the `>>'
16135 operator like two `>' tokens in certain
16136 cases. */
16137
16138 case CPP_GREATER:
16139 if (depth == 0)
16140 {
16141 /* This might be an operator, or it might close a
16142 template argument list. But if a previous '<'
16143 started a template argument list, this will have
16144 closed it, so we can't be in one anymore. */
16145 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16146 if (maybe_template_id < 0)
16147 maybe_template_id = 0;
16148 }
16149 break;
16150
16151 /* If we run out of tokens, issue an error message. */
16152 case CPP_EOF:
16153 case CPP_PRAGMA_EOL:
16154 error_at (token->location, "file ends in default argument");
16155 done = true;
16156 break;
16157
16158 case CPP_NAME:
16159 case CPP_SCOPE:
16160 /* In these cases, we should look for template-ids.
16161 For example, if the default argument is
16162 `X<int, double>()', we need to do name lookup to
16163 figure out whether or not `X' is a template; if
16164 so, the `,' does not end the default argument.
16165
16166 That is not yet done. */
16167 break;
16168
16169 default:
16170 break;
16171 }
16172
16173 /* If we've reached the end, stop. */
16174 if (done)
16175 break;
16176
16177 /* Add the token to the token block. */
16178 token = cp_lexer_consume_token (parser->lexer);
16179 }
16180
16181 /* Create a DEFAULT_ARG to represent the unparsed default
16182 argument. */
16183 default_argument = make_node (DEFAULT_ARG);
16184 DEFARG_TOKENS (default_argument)
16185 = cp_token_cache_new (first_token, token);
16186 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16187 }
16188 /* Outside of a class definition, we can just parse the
16189 assignment-expression. */
16190 else
16191 {
16192 token = cp_lexer_peek_token (parser->lexer);
16193 default_argument
16194 = cp_parser_default_argument (parser, template_parm_p);
16195 }
16196
16197 if (!parser->default_arg_ok_p)
16198 {
16199 if (flag_permissive)
16200 warning (0, "deprecated use of default argument for parameter of non-function");
16201 else
16202 {
16203 error_at (token->location,
16204 "default arguments are only "
16205 "permitted for function parameters");
16206 default_argument = NULL_TREE;
16207 }
16208 }
16209 else if ((declarator && declarator->parameter_pack_p)
16210 || (decl_specifiers.type
16211 && PACK_EXPANSION_P (decl_specifiers.type)))
16212 {
16213 /* Find the name of the parameter pack. */
16214 cp_declarator *id_declarator = declarator;
16215 while (id_declarator && id_declarator->kind != cdk_id)
16216 id_declarator = id_declarator->declarator;
16217
16218 if (id_declarator && id_declarator->kind == cdk_id)
16219 error_at (declarator_token_start->location,
16220 template_parm_p
16221 ? "template parameter pack %qD"
16222 " cannot have a default argument"
16223 : "parameter pack %qD cannot have a default argument",
16224 id_declarator->u.id.unqualified_name);
16225 else
16226 error_at (declarator_token_start->location,
16227 template_parm_p
16228 ? "template parameter pack cannot have a default argument"
16229 : "parameter pack cannot have a default argument");
16230
16231 default_argument = NULL_TREE;
16232 }
16233 }
16234 else
16235 default_argument = NULL_TREE;
16236
16237 return make_parameter_declarator (&decl_specifiers,
16238 declarator,
16239 default_argument);
16240 }
16241
16242 /* Parse a default argument and return it.
16243
16244 TEMPLATE_PARM_P is true if this is a default argument for a
16245 non-type template parameter. */
16246 static tree
16247 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16248 {
16249 tree default_argument = NULL_TREE;
16250 bool saved_greater_than_is_operator_p;
16251 bool saved_local_variables_forbidden_p;
16252
16253 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16254 set correctly. */
16255 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16256 parser->greater_than_is_operator_p = !template_parm_p;
16257 /* Local variable names (and the `this' keyword) may not
16258 appear in a default argument. */
16259 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16260 parser->local_variables_forbidden_p = true;
16261 /* Parse the assignment-expression. */
16262 if (template_parm_p)
16263 push_deferring_access_checks (dk_no_deferred);
16264 default_argument
16265 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16266 if (template_parm_p)
16267 pop_deferring_access_checks ();
16268 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16269 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16270
16271 return default_argument;
16272 }
16273
16274 /* Parse a function-body.
16275
16276 function-body:
16277 compound_statement */
16278
16279 static void
16280 cp_parser_function_body (cp_parser *parser)
16281 {
16282 cp_parser_compound_statement (parser, NULL, false, true);
16283 }
16284
16285 /* Parse a ctor-initializer-opt followed by a function-body. Return
16286 true if a ctor-initializer was present. */
16287
16288 static bool
16289 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16290 {
16291 tree body, list;
16292 bool ctor_initializer_p;
16293 const bool check_body_p =
16294 DECL_CONSTRUCTOR_P (current_function_decl)
16295 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16296 tree last = NULL;
16297
16298 /* Begin the function body. */
16299 body = begin_function_body ();
16300 /* Parse the optional ctor-initializer. */
16301 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16302
16303 /* If we're parsing a constexpr constructor definition, we need
16304 to check that the constructor body is indeed empty. However,
16305 before we get to cp_parser_function_body lot of junk has been
16306 generated, so we can't just check that we have an empty block.
16307 Rather we take a snapshot of the outermost block, and check whether
16308 cp_parser_function_body changed its state. */
16309 if (check_body_p)
16310 {
16311 list = body;
16312 if (TREE_CODE (list) == BIND_EXPR)
16313 list = BIND_EXPR_BODY (list);
16314 if (TREE_CODE (list) == STATEMENT_LIST
16315 && STATEMENT_LIST_TAIL (list) != NULL)
16316 last = STATEMENT_LIST_TAIL (list)->stmt;
16317 }
16318 /* Parse the function-body. */
16319 cp_parser_function_body (parser);
16320 if (check_body_p)
16321 check_constexpr_ctor_body (last, list);
16322 /* Finish the function body. */
16323 finish_function_body (body);
16324
16325 return ctor_initializer_p;
16326 }
16327
16328 /* Parse an initializer.
16329
16330 initializer:
16331 = initializer-clause
16332 ( expression-list )
16333
16334 Returns an expression representing the initializer. If no
16335 initializer is present, NULL_TREE is returned.
16336
16337 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16338 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16339 set to TRUE if there is no initializer present. If there is an
16340 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16341 is set to true; otherwise it is set to false. */
16342
16343 static tree
16344 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16345 bool* non_constant_p)
16346 {
16347 cp_token *token;
16348 tree init;
16349
16350 /* Peek at the next token. */
16351 token = cp_lexer_peek_token (parser->lexer);
16352
16353 /* Let our caller know whether or not this initializer was
16354 parenthesized. */
16355 *is_direct_init = (token->type != CPP_EQ);
16356 /* Assume that the initializer is constant. */
16357 *non_constant_p = false;
16358
16359 if (token->type == CPP_EQ)
16360 {
16361 /* Consume the `='. */
16362 cp_lexer_consume_token (parser->lexer);
16363 /* Parse the initializer-clause. */
16364 init = cp_parser_initializer_clause (parser, non_constant_p);
16365 }
16366 else if (token->type == CPP_OPEN_PAREN)
16367 {
16368 VEC(tree,gc) *vec;
16369 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16370 /*cast_p=*/false,
16371 /*allow_expansion_p=*/true,
16372 non_constant_p);
16373 if (vec == NULL)
16374 return error_mark_node;
16375 init = build_tree_list_vec (vec);
16376 release_tree_vector (vec);
16377 }
16378 else if (token->type == CPP_OPEN_BRACE)
16379 {
16380 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16381 init = cp_parser_braced_list (parser, non_constant_p);
16382 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16383 }
16384 else
16385 {
16386 /* Anything else is an error. */
16387 cp_parser_error (parser, "expected initializer");
16388 init = error_mark_node;
16389 }
16390
16391 return init;
16392 }
16393
16394 /* Parse an initializer-clause.
16395
16396 initializer-clause:
16397 assignment-expression
16398 braced-init-list
16399
16400 Returns an expression representing the initializer.
16401
16402 If the `assignment-expression' production is used the value
16403 returned is simply a representation for the expression.
16404
16405 Otherwise, calls cp_parser_braced_list. */
16406
16407 static tree
16408 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16409 {
16410 tree initializer;
16411
16412 /* Assume the expression is constant. */
16413 *non_constant_p = false;
16414
16415 /* If it is not a `{', then we are looking at an
16416 assignment-expression. */
16417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16418 {
16419 initializer
16420 = cp_parser_constant_expression (parser,
16421 /*allow_non_constant_p=*/true,
16422 non_constant_p);
16423 if (!*non_constant_p)
16424 {
16425 /* We only want to fold if this is really a constant
16426 expression. FIXME Actually, we don't want to fold here, but in
16427 cp_finish_decl. */
16428 tree folded = fold_non_dependent_expr (initializer);
16429 folded = maybe_constant_value (folded);
16430 if (TREE_CONSTANT (folded))
16431 initializer = folded;
16432 }
16433 }
16434 else
16435 initializer = cp_parser_braced_list (parser, non_constant_p);
16436
16437 return initializer;
16438 }
16439
16440 /* Parse a brace-enclosed initializer list.
16441
16442 braced-init-list:
16443 { initializer-list , [opt] }
16444 { }
16445
16446 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16447 the elements of the initializer-list (or NULL, if the last
16448 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16449 NULL_TREE. There is no way to detect whether or not the optional
16450 trailing `,' was provided. NON_CONSTANT_P is as for
16451 cp_parser_initializer. */
16452
16453 static tree
16454 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16455 {
16456 tree initializer;
16457
16458 /* Consume the `{' token. */
16459 cp_lexer_consume_token (parser->lexer);
16460 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16461 initializer = make_node (CONSTRUCTOR);
16462 /* If it's not a `}', then there is a non-trivial initializer. */
16463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16464 {
16465 /* Parse the initializer list. */
16466 CONSTRUCTOR_ELTS (initializer)
16467 = cp_parser_initializer_list (parser, non_constant_p);
16468 /* A trailing `,' token is allowed. */
16469 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16470 cp_lexer_consume_token (parser->lexer);
16471 }
16472 /* Now, there should be a trailing `}'. */
16473 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16474 TREE_TYPE (initializer) = init_list_type_node;
16475 return initializer;
16476 }
16477
16478 /* Parse an initializer-list.
16479
16480 initializer-list:
16481 initializer-clause ... [opt]
16482 initializer-list , initializer-clause ... [opt]
16483
16484 GNU Extension:
16485
16486 initializer-list:
16487 identifier : initializer-clause
16488 initializer-list, identifier : initializer-clause
16489
16490 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16491 for the initializer. If the INDEX of the elt is non-NULL, it is the
16492 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16493 as for cp_parser_initializer. */
16494
16495 static VEC(constructor_elt,gc) *
16496 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16497 {
16498 VEC(constructor_elt,gc) *v = NULL;
16499
16500 /* Assume all of the expressions are constant. */
16501 *non_constant_p = false;
16502
16503 /* Parse the rest of the list. */
16504 while (true)
16505 {
16506 cp_token *token;
16507 tree identifier;
16508 tree initializer;
16509 bool clause_non_constant_p;
16510
16511 /* If the next token is an identifier and the following one is a
16512 colon, we are looking at the GNU designated-initializer
16513 syntax. */
16514 if (cp_parser_allow_gnu_extensions_p (parser)
16515 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16516 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16517 {
16518 /* Warn the user that they are using an extension. */
16519 pedwarn (input_location, OPT_pedantic,
16520 "ISO C++ does not allow designated initializers");
16521 /* Consume the identifier. */
16522 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16523 /* Consume the `:'. */
16524 cp_lexer_consume_token (parser->lexer);
16525 }
16526 else
16527 identifier = NULL_TREE;
16528
16529 /* Parse the initializer. */
16530 initializer = cp_parser_initializer_clause (parser,
16531 &clause_non_constant_p);
16532 /* If any clause is non-constant, so is the entire initializer. */
16533 if (clause_non_constant_p)
16534 *non_constant_p = true;
16535
16536 /* If we have an ellipsis, this is an initializer pack
16537 expansion. */
16538 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16539 {
16540 /* Consume the `...'. */
16541 cp_lexer_consume_token (parser->lexer);
16542
16543 /* Turn the initializer into an initializer expansion. */
16544 initializer = make_pack_expansion (initializer);
16545 }
16546
16547 /* Add it to the vector. */
16548 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16549
16550 /* If the next token is not a comma, we have reached the end of
16551 the list. */
16552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16553 break;
16554
16555 /* Peek at the next token. */
16556 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16557 /* If the next token is a `}', then we're still done. An
16558 initializer-clause can have a trailing `,' after the
16559 initializer-list and before the closing `}'. */
16560 if (token->type == CPP_CLOSE_BRACE)
16561 break;
16562
16563 /* Consume the `,' token. */
16564 cp_lexer_consume_token (parser->lexer);
16565 }
16566
16567 return v;
16568 }
16569
16570 /* Classes [gram.class] */
16571
16572 /* Parse a class-name.
16573
16574 class-name:
16575 identifier
16576 template-id
16577
16578 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16579 to indicate that names looked up in dependent types should be
16580 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16581 keyword has been used to indicate that the name that appears next
16582 is a template. TAG_TYPE indicates the explicit tag given before
16583 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16584 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16585 is the class being defined in a class-head.
16586
16587 Returns the TYPE_DECL representing the class. */
16588
16589 static tree
16590 cp_parser_class_name (cp_parser *parser,
16591 bool typename_keyword_p,
16592 bool template_keyword_p,
16593 enum tag_types tag_type,
16594 bool check_dependency_p,
16595 bool class_head_p,
16596 bool is_declaration)
16597 {
16598 tree decl;
16599 tree scope;
16600 bool typename_p;
16601 cp_token *token;
16602 tree identifier = NULL_TREE;
16603
16604 /* All class-names start with an identifier. */
16605 token = cp_lexer_peek_token (parser->lexer);
16606 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16607 {
16608 cp_parser_error (parser, "expected class-name");
16609 return error_mark_node;
16610 }
16611
16612 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16613 to a template-id, so we save it here. */
16614 scope = parser->scope;
16615 if (scope == error_mark_node)
16616 return error_mark_node;
16617
16618 /* Any name names a type if we're following the `typename' keyword
16619 in a qualified name where the enclosing scope is type-dependent. */
16620 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16621 && dependent_type_p (scope));
16622 /* Handle the common case (an identifier, but not a template-id)
16623 efficiently. */
16624 if (token->type == CPP_NAME
16625 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16626 {
16627 cp_token *identifier_token;
16628 bool ambiguous_p;
16629
16630 /* Look for the identifier. */
16631 identifier_token = cp_lexer_peek_token (parser->lexer);
16632 ambiguous_p = identifier_token->ambiguous_p;
16633 identifier = cp_parser_identifier (parser);
16634 /* If the next token isn't an identifier, we are certainly not
16635 looking at a class-name. */
16636 if (identifier == error_mark_node)
16637 decl = error_mark_node;
16638 /* If we know this is a type-name, there's no need to look it
16639 up. */
16640 else if (typename_p)
16641 decl = identifier;
16642 else
16643 {
16644 tree ambiguous_decls;
16645 /* If we already know that this lookup is ambiguous, then
16646 we've already issued an error message; there's no reason
16647 to check again. */
16648 if (ambiguous_p)
16649 {
16650 cp_parser_simulate_error (parser);
16651 return error_mark_node;
16652 }
16653 /* If the next token is a `::', then the name must be a type
16654 name.
16655
16656 [basic.lookup.qual]
16657
16658 During the lookup for a name preceding the :: scope
16659 resolution operator, object, function, and enumerator
16660 names are ignored. */
16661 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16662 tag_type = typename_type;
16663 /* Look up the name. */
16664 decl = cp_parser_lookup_name (parser, identifier,
16665 tag_type,
16666 /*is_template=*/false,
16667 /*is_namespace=*/false,
16668 check_dependency_p,
16669 &ambiguous_decls,
16670 identifier_token->location);
16671 if (ambiguous_decls)
16672 {
16673 if (cp_parser_parsing_tentatively (parser))
16674 cp_parser_simulate_error (parser);
16675 return error_mark_node;
16676 }
16677 }
16678 }
16679 else
16680 {
16681 /* Try a template-id. */
16682 decl = cp_parser_template_id (parser, template_keyword_p,
16683 check_dependency_p,
16684 is_declaration);
16685 if (decl == error_mark_node)
16686 return error_mark_node;
16687 }
16688
16689 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16690
16691 /* If this is a typename, create a TYPENAME_TYPE. */
16692 if (typename_p && decl != error_mark_node)
16693 {
16694 decl = make_typename_type (scope, decl, typename_type,
16695 /*complain=*/tf_error);
16696 if (decl != error_mark_node)
16697 decl = TYPE_NAME (decl);
16698 }
16699
16700 /* Check to see that it is really the name of a class. */
16701 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16702 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16703 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16704 /* Situations like this:
16705
16706 template <typename T> struct A {
16707 typename T::template X<int>::I i;
16708 };
16709
16710 are problematic. Is `T::template X<int>' a class-name? The
16711 standard does not seem to be definitive, but there is no other
16712 valid interpretation of the following `::'. Therefore, those
16713 names are considered class-names. */
16714 {
16715 decl = make_typename_type (scope, decl, tag_type, tf_error);
16716 if (decl != error_mark_node)
16717 decl = TYPE_NAME (decl);
16718 }
16719 else if (TREE_CODE (decl) != TYPE_DECL
16720 || TREE_TYPE (decl) == error_mark_node
16721 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16722 /* In Objective-C 2.0, a classname followed by '.' starts a
16723 dot-syntax expression, and it's not a type-name. */
16724 || (c_dialect_objc ()
16725 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16726 && objc_is_class_name (decl)))
16727 decl = error_mark_node;
16728
16729 if (decl == error_mark_node)
16730 cp_parser_error (parser, "expected class-name");
16731 else if (identifier && !parser->scope)
16732 maybe_note_name_used_in_class (identifier, decl);
16733
16734 return decl;
16735 }
16736
16737 /* Parse a class-specifier.
16738
16739 class-specifier:
16740 class-head { member-specification [opt] }
16741
16742 Returns the TREE_TYPE representing the class. */
16743
16744 static tree
16745 cp_parser_class_specifier (cp_parser* parser)
16746 {
16747 tree type;
16748 tree attributes = NULL_TREE;
16749 bool nested_name_specifier_p;
16750 unsigned saved_num_template_parameter_lists;
16751 bool saved_in_function_body;
16752 bool saved_in_unbraced_linkage_specification_p;
16753 tree old_scope = NULL_TREE;
16754 tree scope = NULL_TREE;
16755 tree bases;
16756 cp_token *closing_brace;
16757
16758 push_deferring_access_checks (dk_no_deferred);
16759
16760 /* Parse the class-head. */
16761 type = cp_parser_class_head (parser,
16762 &nested_name_specifier_p,
16763 &attributes,
16764 &bases);
16765 /* If the class-head was a semantic disaster, skip the entire body
16766 of the class. */
16767 if (!type)
16768 {
16769 cp_parser_skip_to_end_of_block_or_statement (parser);
16770 pop_deferring_access_checks ();
16771 return error_mark_node;
16772 }
16773
16774 /* Look for the `{'. */
16775 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16776 {
16777 pop_deferring_access_checks ();
16778 return error_mark_node;
16779 }
16780
16781 /* Process the base classes. If they're invalid, skip the
16782 entire class body. */
16783 if (!xref_basetypes (type, bases))
16784 {
16785 /* Consuming the closing brace yields better error messages
16786 later on. */
16787 if (cp_parser_skip_to_closing_brace (parser))
16788 cp_lexer_consume_token (parser->lexer);
16789 pop_deferring_access_checks ();
16790 return error_mark_node;
16791 }
16792
16793 /* Issue an error message if type-definitions are forbidden here. */
16794 cp_parser_check_type_definition (parser);
16795 /* Remember that we are defining one more class. */
16796 ++parser->num_classes_being_defined;
16797 /* Inside the class, surrounding template-parameter-lists do not
16798 apply. */
16799 saved_num_template_parameter_lists
16800 = parser->num_template_parameter_lists;
16801 parser->num_template_parameter_lists = 0;
16802 /* We are not in a function body. */
16803 saved_in_function_body = parser->in_function_body;
16804 parser->in_function_body = false;
16805 /* We are not immediately inside an extern "lang" block. */
16806 saved_in_unbraced_linkage_specification_p
16807 = parser->in_unbraced_linkage_specification_p;
16808 parser->in_unbraced_linkage_specification_p = false;
16809
16810 /* Start the class. */
16811 if (nested_name_specifier_p)
16812 {
16813 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16814 old_scope = push_inner_scope (scope);
16815 }
16816 type = begin_class_definition (type, attributes);
16817
16818 if (type == error_mark_node)
16819 /* If the type is erroneous, skip the entire body of the class. */
16820 cp_parser_skip_to_closing_brace (parser);
16821 else
16822 /* Parse the member-specification. */
16823 cp_parser_member_specification_opt (parser);
16824
16825 /* Look for the trailing `}'. */
16826 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16827 /* Look for trailing attributes to apply to this class. */
16828 if (cp_parser_allow_gnu_extensions_p (parser))
16829 attributes = cp_parser_attributes_opt (parser);
16830 if (type != error_mark_node)
16831 type = finish_struct (type, attributes);
16832 if (nested_name_specifier_p)
16833 pop_inner_scope (old_scope, scope);
16834
16835 /* We've finished a type definition. Check for the common syntax
16836 error of forgetting a semicolon after the definition. We need to
16837 be careful, as we can't just check for not-a-semicolon and be done
16838 with it; the user might have typed:
16839
16840 class X { } c = ...;
16841 class X { } *p = ...;
16842
16843 and so forth. Instead, enumerate all the possible tokens that
16844 might follow this production; if we don't see one of them, then
16845 complain and silently insert the semicolon. */
16846 {
16847 cp_token *token = cp_lexer_peek_token (parser->lexer);
16848 bool want_semicolon = true;
16849
16850 switch (token->type)
16851 {
16852 case CPP_NAME:
16853 case CPP_SEMICOLON:
16854 case CPP_MULT:
16855 case CPP_AND:
16856 case CPP_OPEN_PAREN:
16857 case CPP_CLOSE_PAREN:
16858 case CPP_COMMA:
16859 want_semicolon = false;
16860 break;
16861
16862 /* While it's legal for type qualifiers and storage class
16863 specifiers to follow type definitions in the grammar, only
16864 compiler testsuites contain code like that. Assume that if
16865 we see such code, then what we're really seeing is a case
16866 like:
16867
16868 class X { }
16869 const <type> var = ...;
16870
16871 or
16872
16873 class Y { }
16874 static <type> func (...) ...
16875
16876 i.e. the qualifier or specifier applies to the next
16877 declaration. To do so, however, we need to look ahead one
16878 more token to see if *that* token is a type specifier.
16879
16880 This code could be improved to handle:
16881
16882 class Z { }
16883 static const <type> var = ...; */
16884 case CPP_KEYWORD:
16885 if (keyword_is_decl_specifier (token->keyword))
16886 {
16887 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16888
16889 /* Handling user-defined types here would be nice, but very
16890 tricky. */
16891 want_semicolon
16892 = (lookahead->type == CPP_KEYWORD
16893 && keyword_begins_type_specifier (lookahead->keyword));
16894 }
16895 break;
16896 default:
16897 break;
16898 }
16899
16900 /* If we don't have a type, then something is very wrong and we
16901 shouldn't try to do anything clever. Likewise for not seeing the
16902 closing brace. */
16903 if (closing_brace && TYPE_P (type) && want_semicolon)
16904 {
16905 cp_token_position prev
16906 = cp_lexer_previous_token_position (parser->lexer);
16907 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16908 location_t loc = prev_token->location;
16909
16910 if (CLASSTYPE_DECLARED_CLASS (type))
16911 error_at (loc, "expected %<;%> after class definition");
16912 else if (TREE_CODE (type) == RECORD_TYPE)
16913 error_at (loc, "expected %<;%> after struct definition");
16914 else if (TREE_CODE (type) == UNION_TYPE)
16915 error_at (loc, "expected %<;%> after union definition");
16916 else
16917 gcc_unreachable ();
16918
16919 /* Unget one token and smash it to look as though we encountered
16920 a semicolon in the input stream. */
16921 cp_lexer_set_token_position (parser->lexer, prev);
16922 token = cp_lexer_peek_token (parser->lexer);
16923 token->type = CPP_SEMICOLON;
16924 token->keyword = RID_MAX;
16925 }
16926 }
16927
16928 /* If this class is not itself within the scope of another class,
16929 then we need to parse the bodies of all of the queued function
16930 definitions. Note that the queued functions defined in a class
16931 are not always processed immediately following the
16932 class-specifier for that class. Consider:
16933
16934 struct A {
16935 struct B { void f() { sizeof (A); } };
16936 };
16937
16938 If `f' were processed before the processing of `A' were
16939 completed, there would be no way to compute the size of `A'.
16940 Note that the nesting we are interested in here is lexical --
16941 not the semantic nesting given by TYPE_CONTEXT. In particular,
16942 for:
16943
16944 struct A { struct B; };
16945 struct A::B { void f() { } };
16946
16947 there is no need to delay the parsing of `A::B::f'. */
16948 if (--parser->num_classes_being_defined == 0)
16949 {
16950 tree fn;
16951 tree class_type = NULL_TREE;
16952 tree pushed_scope = NULL_TREE;
16953 unsigned ix;
16954 cp_default_arg_entry *e;
16955
16956 /* In a first pass, parse default arguments to the functions.
16957 Then, in a second pass, parse the bodies of the functions.
16958 This two-phased approach handles cases like:
16959
16960 struct S {
16961 void f() { g(); }
16962 void g(int i = 3);
16963 };
16964
16965 */
16966 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16967 ix, e)
16968 {
16969 fn = e->decl;
16970 /* If there are default arguments that have not yet been processed,
16971 take care of them now. */
16972 if (class_type != e->class_type)
16973 {
16974 if (pushed_scope)
16975 pop_scope (pushed_scope);
16976 class_type = e->class_type;
16977 pushed_scope = push_scope (class_type);
16978 }
16979 /* Make sure that any template parameters are in scope. */
16980 maybe_begin_member_template_processing (fn);
16981 /* Parse the default argument expressions. */
16982 cp_parser_late_parsing_default_args (parser, fn);
16983 /* Remove any template parameters from the symbol table. */
16984 maybe_end_member_template_processing ();
16985 }
16986 if (pushed_scope)
16987 pop_scope (pushed_scope);
16988 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16989 /* Now parse the body of the functions. */
16990 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16991 cp_parser_late_parsing_for_member (parser, fn);
16992 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16993 }
16994
16995 /* Put back any saved access checks. */
16996 pop_deferring_access_checks ();
16997
16998 /* Restore saved state. */
16999 parser->in_function_body = saved_in_function_body;
17000 parser->num_template_parameter_lists
17001 = saved_num_template_parameter_lists;
17002 parser->in_unbraced_linkage_specification_p
17003 = saved_in_unbraced_linkage_specification_p;
17004
17005 return type;
17006 }
17007
17008 /* Parse a class-head.
17009
17010 class-head:
17011 class-key identifier [opt] base-clause [opt]
17012 class-key nested-name-specifier identifier base-clause [opt]
17013 class-key nested-name-specifier [opt] template-id
17014 base-clause [opt]
17015
17016 GNU Extensions:
17017 class-key attributes identifier [opt] base-clause [opt]
17018 class-key attributes nested-name-specifier identifier base-clause [opt]
17019 class-key attributes nested-name-specifier [opt] template-id
17020 base-clause [opt]
17021
17022 Upon return BASES is initialized to the list of base classes (or
17023 NULL, if there are none) in the same form returned by
17024 cp_parser_base_clause.
17025
17026 Returns the TYPE of the indicated class. Sets
17027 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17028 involving a nested-name-specifier was used, and FALSE otherwise.
17029
17030 Returns error_mark_node if this is not a class-head.
17031
17032 Returns NULL_TREE if the class-head is syntactically valid, but
17033 semantically invalid in a way that means we should skip the entire
17034 body of the class. */
17035
17036 static tree
17037 cp_parser_class_head (cp_parser* parser,
17038 bool* nested_name_specifier_p,
17039 tree *attributes_p,
17040 tree *bases)
17041 {
17042 tree nested_name_specifier;
17043 enum tag_types class_key;
17044 tree id = NULL_TREE;
17045 tree type = NULL_TREE;
17046 tree attributes;
17047 bool template_id_p = false;
17048 bool qualified_p = false;
17049 bool invalid_nested_name_p = false;
17050 bool invalid_explicit_specialization_p = false;
17051 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17052 tree pushed_scope = NULL_TREE;
17053 unsigned num_templates;
17054 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17055 /* Assume no nested-name-specifier will be present. */
17056 *nested_name_specifier_p = false;
17057 /* Assume no template parameter lists will be used in defining the
17058 type. */
17059 num_templates = 0;
17060 parser->colon_corrects_to_scope_p = false;
17061
17062 *bases = NULL_TREE;
17063
17064 /* Look for the class-key. */
17065 class_key = cp_parser_class_key (parser);
17066 if (class_key == none_type)
17067 return error_mark_node;
17068
17069 /* Parse the attributes. */
17070 attributes = cp_parser_attributes_opt (parser);
17071
17072 /* If the next token is `::', that is invalid -- but sometimes
17073 people do try to write:
17074
17075 struct ::S {};
17076
17077 Handle this gracefully by accepting the extra qualifier, and then
17078 issuing an error about it later if this really is a
17079 class-head. If it turns out just to be an elaborated type
17080 specifier, remain silent. */
17081 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17082 qualified_p = true;
17083
17084 push_deferring_access_checks (dk_no_check);
17085
17086 /* Determine the name of the class. Begin by looking for an
17087 optional nested-name-specifier. */
17088 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17089 nested_name_specifier
17090 = cp_parser_nested_name_specifier_opt (parser,
17091 /*typename_keyword_p=*/false,
17092 /*check_dependency_p=*/false,
17093 /*type_p=*/false,
17094 /*is_declaration=*/false);
17095 /* If there was a nested-name-specifier, then there *must* be an
17096 identifier. */
17097 if (nested_name_specifier)
17098 {
17099 type_start_token = cp_lexer_peek_token (parser->lexer);
17100 /* Although the grammar says `identifier', it really means
17101 `class-name' or `template-name'. You are only allowed to
17102 define a class that has already been declared with this
17103 syntax.
17104
17105 The proposed resolution for Core Issue 180 says that wherever
17106 you see `class T::X' you should treat `X' as a type-name.
17107
17108 It is OK to define an inaccessible class; for example:
17109
17110 class A { class B; };
17111 class A::B {};
17112
17113 We do not know if we will see a class-name, or a
17114 template-name. We look for a class-name first, in case the
17115 class-name is a template-id; if we looked for the
17116 template-name first we would stop after the template-name. */
17117 cp_parser_parse_tentatively (parser);
17118 type = cp_parser_class_name (parser,
17119 /*typename_keyword_p=*/false,
17120 /*template_keyword_p=*/false,
17121 class_type,
17122 /*check_dependency_p=*/false,
17123 /*class_head_p=*/true,
17124 /*is_declaration=*/false);
17125 /* If that didn't work, ignore the nested-name-specifier. */
17126 if (!cp_parser_parse_definitely (parser))
17127 {
17128 invalid_nested_name_p = true;
17129 type_start_token = cp_lexer_peek_token (parser->lexer);
17130 id = cp_parser_identifier (parser);
17131 if (id == error_mark_node)
17132 id = NULL_TREE;
17133 }
17134 /* If we could not find a corresponding TYPE, treat this
17135 declaration like an unqualified declaration. */
17136 if (type == error_mark_node)
17137 nested_name_specifier = NULL_TREE;
17138 /* Otherwise, count the number of templates used in TYPE and its
17139 containing scopes. */
17140 else
17141 {
17142 tree scope;
17143
17144 for (scope = TREE_TYPE (type);
17145 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17146 scope = (TYPE_P (scope)
17147 ? TYPE_CONTEXT (scope)
17148 : DECL_CONTEXT (scope)))
17149 if (TYPE_P (scope)
17150 && CLASS_TYPE_P (scope)
17151 && CLASSTYPE_TEMPLATE_INFO (scope)
17152 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17153 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17154 ++num_templates;
17155 }
17156 }
17157 /* Otherwise, the identifier is optional. */
17158 else
17159 {
17160 /* We don't know whether what comes next is a template-id,
17161 an identifier, or nothing at all. */
17162 cp_parser_parse_tentatively (parser);
17163 /* Check for a template-id. */
17164 type_start_token = cp_lexer_peek_token (parser->lexer);
17165 id = cp_parser_template_id (parser,
17166 /*template_keyword_p=*/false,
17167 /*check_dependency_p=*/true,
17168 /*is_declaration=*/true);
17169 /* If that didn't work, it could still be an identifier. */
17170 if (!cp_parser_parse_definitely (parser))
17171 {
17172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17173 {
17174 type_start_token = cp_lexer_peek_token (parser->lexer);
17175 id = cp_parser_identifier (parser);
17176 }
17177 else
17178 id = NULL_TREE;
17179 }
17180 else
17181 {
17182 template_id_p = true;
17183 ++num_templates;
17184 }
17185 }
17186
17187 pop_deferring_access_checks ();
17188
17189 if (id)
17190 cp_parser_check_for_invalid_template_id (parser, id,
17191 type_start_token->location);
17192
17193 /* If it's not a `:' or a `{' then we can't really be looking at a
17194 class-head, since a class-head only appears as part of a
17195 class-specifier. We have to detect this situation before calling
17196 xref_tag, since that has irreversible side-effects. */
17197 if (!cp_parser_next_token_starts_class_definition_p (parser))
17198 {
17199 cp_parser_error (parser, "expected %<{%> or %<:%>");
17200 type = error_mark_node;
17201 goto out;
17202 }
17203
17204 /* At this point, we're going ahead with the class-specifier, even
17205 if some other problem occurs. */
17206 cp_parser_commit_to_tentative_parse (parser);
17207 /* Issue the error about the overly-qualified name now. */
17208 if (qualified_p)
17209 {
17210 cp_parser_error (parser,
17211 "global qualification of class name is invalid");
17212 type = error_mark_node;
17213 goto out;
17214 }
17215 else if (invalid_nested_name_p)
17216 {
17217 cp_parser_error (parser,
17218 "qualified name does not name a class");
17219 type = error_mark_node;
17220 goto out;
17221 }
17222 else if (nested_name_specifier)
17223 {
17224 tree scope;
17225
17226 /* Reject typedef-names in class heads. */
17227 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17228 {
17229 error_at (type_start_token->location,
17230 "invalid class name in declaration of %qD",
17231 type);
17232 type = NULL_TREE;
17233 goto done;
17234 }
17235
17236 /* Figure out in what scope the declaration is being placed. */
17237 scope = current_scope ();
17238 /* If that scope does not contain the scope in which the
17239 class was originally declared, the program is invalid. */
17240 if (scope && !is_ancestor (scope, nested_name_specifier))
17241 {
17242 if (at_namespace_scope_p ())
17243 error_at (type_start_token->location,
17244 "declaration of %qD in namespace %qD which does not "
17245 "enclose %qD",
17246 type, scope, nested_name_specifier);
17247 else
17248 error_at (type_start_token->location,
17249 "declaration of %qD in %qD which does not enclose %qD",
17250 type, scope, nested_name_specifier);
17251 type = NULL_TREE;
17252 goto done;
17253 }
17254 /* [dcl.meaning]
17255
17256 A declarator-id shall not be qualified except for the
17257 definition of a ... nested class outside of its class
17258 ... [or] the definition or explicit instantiation of a
17259 class member of a namespace outside of its namespace. */
17260 if (scope == nested_name_specifier)
17261 {
17262 permerror (nested_name_specifier_token_start->location,
17263 "extra qualification not allowed");
17264 nested_name_specifier = NULL_TREE;
17265 num_templates = 0;
17266 }
17267 }
17268 /* An explicit-specialization must be preceded by "template <>". If
17269 it is not, try to recover gracefully. */
17270 if (at_namespace_scope_p ()
17271 && parser->num_template_parameter_lists == 0
17272 && template_id_p)
17273 {
17274 error_at (type_start_token->location,
17275 "an explicit specialization must be preceded by %<template <>%>");
17276 invalid_explicit_specialization_p = true;
17277 /* Take the same action that would have been taken by
17278 cp_parser_explicit_specialization. */
17279 ++parser->num_template_parameter_lists;
17280 begin_specialization ();
17281 }
17282 /* There must be no "return" statements between this point and the
17283 end of this function; set "type "to the correct return value and
17284 use "goto done;" to return. */
17285 /* Make sure that the right number of template parameters were
17286 present. */
17287 if (!cp_parser_check_template_parameters (parser, num_templates,
17288 type_start_token->location,
17289 /*declarator=*/NULL))
17290 {
17291 /* If something went wrong, there is no point in even trying to
17292 process the class-definition. */
17293 type = NULL_TREE;
17294 goto done;
17295 }
17296
17297 /* Look up the type. */
17298 if (template_id_p)
17299 {
17300 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17301 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17302 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17303 {
17304 error_at (type_start_token->location,
17305 "function template %qD redeclared as a class template", id);
17306 type = error_mark_node;
17307 }
17308 else
17309 {
17310 type = TREE_TYPE (id);
17311 type = maybe_process_partial_specialization (type);
17312 }
17313 if (nested_name_specifier)
17314 pushed_scope = push_scope (nested_name_specifier);
17315 }
17316 else if (nested_name_specifier)
17317 {
17318 tree class_type;
17319
17320 /* Given:
17321
17322 template <typename T> struct S { struct T };
17323 template <typename T> struct S<T>::T { };
17324
17325 we will get a TYPENAME_TYPE when processing the definition of
17326 `S::T'. We need to resolve it to the actual type before we
17327 try to define it. */
17328 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17329 {
17330 class_type = resolve_typename_type (TREE_TYPE (type),
17331 /*only_current_p=*/false);
17332 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17333 type = TYPE_NAME (class_type);
17334 else
17335 {
17336 cp_parser_error (parser, "could not resolve typename type");
17337 type = error_mark_node;
17338 }
17339 }
17340
17341 if (maybe_process_partial_specialization (TREE_TYPE (type))
17342 == error_mark_node)
17343 {
17344 type = NULL_TREE;
17345 goto done;
17346 }
17347
17348 class_type = current_class_type;
17349 /* Enter the scope indicated by the nested-name-specifier. */
17350 pushed_scope = push_scope (nested_name_specifier);
17351 /* Get the canonical version of this type. */
17352 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17353 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17354 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17355 {
17356 type = push_template_decl (type);
17357 if (type == error_mark_node)
17358 {
17359 type = NULL_TREE;
17360 goto done;
17361 }
17362 }
17363
17364 type = TREE_TYPE (type);
17365 *nested_name_specifier_p = true;
17366 }
17367 else /* The name is not a nested name. */
17368 {
17369 /* If the class was unnamed, create a dummy name. */
17370 if (!id)
17371 id = make_anon_name ();
17372 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17373 parser->num_template_parameter_lists);
17374 }
17375
17376 /* Indicate whether this class was declared as a `class' or as a
17377 `struct'. */
17378 if (TREE_CODE (type) == RECORD_TYPE)
17379 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17380 cp_parser_check_class_key (class_key, type);
17381
17382 /* If this type was already complete, and we see another definition,
17383 that's an error. */
17384 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17385 {
17386 error_at (type_start_token->location, "redefinition of %q#T",
17387 type);
17388 error_at (type_start_token->location, "previous definition of %q+#T",
17389 type);
17390 type = NULL_TREE;
17391 goto done;
17392 }
17393 else if (type == error_mark_node)
17394 type = NULL_TREE;
17395
17396 /* We will have entered the scope containing the class; the names of
17397 base classes should be looked up in that context. For example:
17398
17399 struct A { struct B {}; struct C; };
17400 struct A::C : B {};
17401
17402 is valid. */
17403
17404 /* Get the list of base-classes, if there is one. */
17405 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17406 *bases = cp_parser_base_clause (parser);
17407
17408 done:
17409 /* Leave the scope given by the nested-name-specifier. We will
17410 enter the class scope itself while processing the members. */
17411 if (pushed_scope)
17412 pop_scope (pushed_scope);
17413
17414 if (invalid_explicit_specialization_p)
17415 {
17416 end_specialization ();
17417 --parser->num_template_parameter_lists;
17418 }
17419
17420 if (type)
17421 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17422 *attributes_p = attributes;
17423 out:
17424 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17425 return type;
17426 }
17427
17428 /* Parse a class-key.
17429
17430 class-key:
17431 class
17432 struct
17433 union
17434
17435 Returns the kind of class-key specified, or none_type to indicate
17436 error. */
17437
17438 static enum tag_types
17439 cp_parser_class_key (cp_parser* parser)
17440 {
17441 cp_token *token;
17442 enum tag_types tag_type;
17443
17444 /* Look for the class-key. */
17445 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17446 if (!token)
17447 return none_type;
17448
17449 /* Check to see if the TOKEN is a class-key. */
17450 tag_type = cp_parser_token_is_class_key (token);
17451 if (!tag_type)
17452 cp_parser_error (parser, "expected class-key");
17453 return tag_type;
17454 }
17455
17456 /* Parse an (optional) member-specification.
17457
17458 member-specification:
17459 member-declaration member-specification [opt]
17460 access-specifier : member-specification [opt] */
17461
17462 static void
17463 cp_parser_member_specification_opt (cp_parser* parser)
17464 {
17465 while (true)
17466 {
17467 cp_token *token;
17468 enum rid keyword;
17469
17470 /* Peek at the next token. */
17471 token = cp_lexer_peek_token (parser->lexer);
17472 /* If it's a `}', or EOF then we've seen all the members. */
17473 if (token->type == CPP_CLOSE_BRACE
17474 || token->type == CPP_EOF
17475 || token->type == CPP_PRAGMA_EOL)
17476 break;
17477
17478 /* See if this token is a keyword. */
17479 keyword = token->keyword;
17480 switch (keyword)
17481 {
17482 case RID_PUBLIC:
17483 case RID_PROTECTED:
17484 case RID_PRIVATE:
17485 /* Consume the access-specifier. */
17486 cp_lexer_consume_token (parser->lexer);
17487 /* Remember which access-specifier is active. */
17488 current_access_specifier = token->u.value;
17489 /* Look for the `:'. */
17490 cp_parser_require (parser, CPP_COLON, RT_COLON);
17491 break;
17492
17493 default:
17494 /* Accept #pragmas at class scope. */
17495 if (token->type == CPP_PRAGMA)
17496 {
17497 cp_parser_pragma (parser, pragma_external);
17498 break;
17499 }
17500
17501 /* Otherwise, the next construction must be a
17502 member-declaration. */
17503 cp_parser_member_declaration (parser);
17504 }
17505 }
17506 }
17507
17508 /* Parse a member-declaration.
17509
17510 member-declaration:
17511 decl-specifier-seq [opt] member-declarator-list [opt] ;
17512 function-definition ; [opt]
17513 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17514 using-declaration
17515 template-declaration
17516
17517 member-declarator-list:
17518 member-declarator
17519 member-declarator-list , member-declarator
17520
17521 member-declarator:
17522 declarator pure-specifier [opt]
17523 declarator constant-initializer [opt]
17524 identifier [opt] : constant-expression
17525
17526 GNU Extensions:
17527
17528 member-declaration:
17529 __extension__ member-declaration
17530
17531 member-declarator:
17532 declarator attributes [opt] pure-specifier [opt]
17533 declarator attributes [opt] constant-initializer [opt]
17534 identifier [opt] attributes [opt] : constant-expression
17535
17536 C++0x Extensions:
17537
17538 member-declaration:
17539 static_assert-declaration */
17540
17541 static void
17542 cp_parser_member_declaration (cp_parser* parser)
17543 {
17544 cp_decl_specifier_seq decl_specifiers;
17545 tree prefix_attributes;
17546 tree decl;
17547 int declares_class_or_enum;
17548 bool friend_p;
17549 cp_token *token = NULL;
17550 cp_token *decl_spec_token_start = NULL;
17551 cp_token *initializer_token_start = NULL;
17552 int saved_pedantic;
17553 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17554
17555 /* Check for the `__extension__' keyword. */
17556 if (cp_parser_extension_opt (parser, &saved_pedantic))
17557 {
17558 /* Recurse. */
17559 cp_parser_member_declaration (parser);
17560 /* Restore the old value of the PEDANTIC flag. */
17561 pedantic = saved_pedantic;
17562
17563 return;
17564 }
17565
17566 /* Check for a template-declaration. */
17567 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17568 {
17569 /* An explicit specialization here is an error condition, and we
17570 expect the specialization handler to detect and report this. */
17571 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17572 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17573 cp_parser_explicit_specialization (parser);
17574 else
17575 cp_parser_template_declaration (parser, /*member_p=*/true);
17576
17577 return;
17578 }
17579
17580 /* Check for a using-declaration. */
17581 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17582 {
17583 /* Parse the using-declaration. */
17584 cp_parser_using_declaration (parser,
17585 /*access_declaration_p=*/false);
17586 return;
17587 }
17588
17589 /* Check for @defs. */
17590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17591 {
17592 tree ivar, member;
17593 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17594 ivar = ivar_chains;
17595 while (ivar)
17596 {
17597 member = ivar;
17598 ivar = TREE_CHAIN (member);
17599 TREE_CHAIN (member) = NULL_TREE;
17600 finish_member_declaration (member);
17601 }
17602 return;
17603 }
17604
17605 /* If the next token is `static_assert' we have a static assertion. */
17606 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17607 {
17608 cp_parser_static_assert (parser, /*member_p=*/true);
17609 return;
17610 }
17611
17612 parser->colon_corrects_to_scope_p = false;
17613
17614 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17615 goto out;
17616
17617 /* Parse the decl-specifier-seq. */
17618 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17619 cp_parser_decl_specifier_seq (parser,
17620 CP_PARSER_FLAGS_OPTIONAL,
17621 &decl_specifiers,
17622 &declares_class_or_enum);
17623 prefix_attributes = decl_specifiers.attributes;
17624 decl_specifiers.attributes = NULL_TREE;
17625 /* Check for an invalid type-name. */
17626 if (!decl_specifiers.any_type_specifiers_p
17627 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17628 goto out;
17629 /* If there is no declarator, then the decl-specifier-seq should
17630 specify a type. */
17631 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17632 {
17633 /* If there was no decl-specifier-seq, and the next token is a
17634 `;', then we have something like:
17635
17636 struct S { ; };
17637
17638 [class.mem]
17639
17640 Each member-declaration shall declare at least one member
17641 name of the class. */
17642 if (!decl_specifiers.any_specifiers_p)
17643 {
17644 cp_token *token = cp_lexer_peek_token (parser->lexer);
17645 if (!in_system_header_at (token->location))
17646 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17647 }
17648 else
17649 {
17650 tree type;
17651
17652 /* See if this declaration is a friend. */
17653 friend_p = cp_parser_friend_p (&decl_specifiers);
17654 /* If there were decl-specifiers, check to see if there was
17655 a class-declaration. */
17656 type = check_tag_decl (&decl_specifiers);
17657 /* Nested classes have already been added to the class, but
17658 a `friend' needs to be explicitly registered. */
17659 if (friend_p)
17660 {
17661 /* If the `friend' keyword was present, the friend must
17662 be introduced with a class-key. */
17663 if (!declares_class_or_enum)
17664 error_at (decl_spec_token_start->location,
17665 "a class-key must be used when declaring a friend");
17666 /* In this case:
17667
17668 template <typename T> struct A {
17669 friend struct A<T>::B;
17670 };
17671
17672 A<T>::B will be represented by a TYPENAME_TYPE, and
17673 therefore not recognized by check_tag_decl. */
17674 if (!type
17675 && decl_specifiers.type
17676 && TYPE_P (decl_specifiers.type))
17677 type = decl_specifiers.type;
17678 if (!type || !TYPE_P (type))
17679 error_at (decl_spec_token_start->location,
17680 "friend declaration does not name a class or "
17681 "function");
17682 else
17683 make_friend_class (current_class_type, type,
17684 /*complain=*/true);
17685 }
17686 /* If there is no TYPE, an error message will already have
17687 been issued. */
17688 else if (!type || type == error_mark_node)
17689 ;
17690 /* An anonymous aggregate has to be handled specially; such
17691 a declaration really declares a data member (with a
17692 particular type), as opposed to a nested class. */
17693 else if (ANON_AGGR_TYPE_P (type))
17694 {
17695 /* Remove constructors and such from TYPE, now that we
17696 know it is an anonymous aggregate. */
17697 fixup_anonymous_aggr (type);
17698 /* And make the corresponding data member. */
17699 decl = build_decl (decl_spec_token_start->location,
17700 FIELD_DECL, NULL_TREE, type);
17701 /* Add it to the class. */
17702 finish_member_declaration (decl);
17703 }
17704 else
17705 cp_parser_check_access_in_redeclaration
17706 (TYPE_NAME (type),
17707 decl_spec_token_start->location);
17708 }
17709 }
17710 else
17711 {
17712 bool assume_semicolon = false;
17713
17714 /* See if these declarations will be friends. */
17715 friend_p = cp_parser_friend_p (&decl_specifiers);
17716
17717 /* Keep going until we hit the `;' at the end of the
17718 declaration. */
17719 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17720 {
17721 tree attributes = NULL_TREE;
17722 tree first_attribute;
17723
17724 /* Peek at the next token. */
17725 token = cp_lexer_peek_token (parser->lexer);
17726
17727 /* Check for a bitfield declaration. */
17728 if (token->type == CPP_COLON
17729 || (token->type == CPP_NAME
17730 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17731 == CPP_COLON))
17732 {
17733 tree identifier;
17734 tree width;
17735
17736 /* Get the name of the bitfield. Note that we cannot just
17737 check TOKEN here because it may have been invalidated by
17738 the call to cp_lexer_peek_nth_token above. */
17739 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17740 identifier = cp_parser_identifier (parser);
17741 else
17742 identifier = NULL_TREE;
17743
17744 /* Consume the `:' token. */
17745 cp_lexer_consume_token (parser->lexer);
17746 /* Get the width of the bitfield. */
17747 width
17748 = cp_parser_constant_expression (parser,
17749 /*allow_non_constant=*/false,
17750 NULL);
17751
17752 /* Look for attributes that apply to the bitfield. */
17753 attributes = cp_parser_attributes_opt (parser);
17754 /* Remember which attributes are prefix attributes and
17755 which are not. */
17756 first_attribute = attributes;
17757 /* Combine the attributes. */
17758 attributes = chainon (prefix_attributes, attributes);
17759
17760 /* Create the bitfield declaration. */
17761 decl = grokbitfield (identifier
17762 ? make_id_declarator (NULL_TREE,
17763 identifier,
17764 sfk_none)
17765 : NULL,
17766 &decl_specifiers,
17767 width,
17768 attributes);
17769 }
17770 else
17771 {
17772 cp_declarator *declarator;
17773 tree initializer;
17774 tree asm_specification;
17775 int ctor_dtor_or_conv_p;
17776
17777 /* Parse the declarator. */
17778 declarator
17779 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17780 &ctor_dtor_or_conv_p,
17781 /*parenthesized_p=*/NULL,
17782 /*member_p=*/true);
17783
17784 /* If something went wrong parsing the declarator, make sure
17785 that we at least consume some tokens. */
17786 if (declarator == cp_error_declarator)
17787 {
17788 /* Skip to the end of the statement. */
17789 cp_parser_skip_to_end_of_statement (parser);
17790 /* If the next token is not a semicolon, that is
17791 probably because we just skipped over the body of
17792 a function. So, we consume a semicolon if
17793 present, but do not issue an error message if it
17794 is not present. */
17795 if (cp_lexer_next_token_is (parser->lexer,
17796 CPP_SEMICOLON))
17797 cp_lexer_consume_token (parser->lexer);
17798 goto out;
17799 }
17800
17801 if (declares_class_or_enum & 2)
17802 cp_parser_check_for_definition_in_return_type
17803 (declarator, decl_specifiers.type,
17804 decl_specifiers.type_location);
17805
17806 /* Look for an asm-specification. */
17807 asm_specification = cp_parser_asm_specification_opt (parser);
17808 /* Look for attributes that apply to the declaration. */
17809 attributes = cp_parser_attributes_opt (parser);
17810 /* Remember which attributes are prefix attributes and
17811 which are not. */
17812 first_attribute = attributes;
17813 /* Combine the attributes. */
17814 attributes = chainon (prefix_attributes, attributes);
17815
17816 /* If it's an `=', then we have a constant-initializer or a
17817 pure-specifier. It is not correct to parse the
17818 initializer before registering the member declaration
17819 since the member declaration should be in scope while
17820 its initializer is processed. However, the rest of the
17821 front end does not yet provide an interface that allows
17822 us to handle this correctly. */
17823 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17824 {
17825 /* In [class.mem]:
17826
17827 A pure-specifier shall be used only in the declaration of
17828 a virtual function.
17829
17830 A member-declarator can contain a constant-initializer
17831 only if it declares a static member of integral or
17832 enumeration type.
17833
17834 Therefore, if the DECLARATOR is for a function, we look
17835 for a pure-specifier; otherwise, we look for a
17836 constant-initializer. When we call `grokfield', it will
17837 perform more stringent semantics checks. */
17838 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17839 if (function_declarator_p (declarator))
17840 initializer = cp_parser_pure_specifier (parser);
17841 else
17842 /* Parse the initializer. */
17843 initializer = cp_parser_constant_initializer (parser);
17844 }
17845 /* Otherwise, there is no initializer. */
17846 else
17847 initializer = NULL_TREE;
17848
17849 /* See if we are probably looking at a function
17850 definition. We are certainly not looking at a
17851 member-declarator. Calling `grokfield' has
17852 side-effects, so we must not do it unless we are sure
17853 that we are looking at a member-declarator. */
17854 if (cp_parser_token_starts_function_definition_p
17855 (cp_lexer_peek_token (parser->lexer)))
17856 {
17857 /* The grammar does not allow a pure-specifier to be
17858 used when a member function is defined. (It is
17859 possible that this fact is an oversight in the
17860 standard, since a pure function may be defined
17861 outside of the class-specifier. */
17862 if (initializer)
17863 error_at (initializer_token_start->location,
17864 "pure-specifier on function-definition");
17865 decl = cp_parser_save_member_function_body (parser,
17866 &decl_specifiers,
17867 declarator,
17868 attributes);
17869 /* If the member was not a friend, declare it here. */
17870 if (!friend_p)
17871 finish_member_declaration (decl);
17872 /* Peek at the next token. */
17873 token = cp_lexer_peek_token (parser->lexer);
17874 /* If the next token is a semicolon, consume it. */
17875 if (token->type == CPP_SEMICOLON)
17876 cp_lexer_consume_token (parser->lexer);
17877 goto out;
17878 }
17879 else
17880 if (declarator->kind == cdk_function)
17881 declarator->id_loc = token->location;
17882 /* Create the declaration. */
17883 decl = grokfield (declarator, &decl_specifiers,
17884 initializer, /*init_const_expr_p=*/true,
17885 asm_specification,
17886 attributes);
17887 }
17888
17889 /* Reset PREFIX_ATTRIBUTES. */
17890 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17891 attributes = TREE_CHAIN (attributes);
17892 if (attributes)
17893 TREE_CHAIN (attributes) = NULL_TREE;
17894
17895 /* If there is any qualification still in effect, clear it
17896 now; we will be starting fresh with the next declarator. */
17897 parser->scope = NULL_TREE;
17898 parser->qualifying_scope = NULL_TREE;
17899 parser->object_scope = NULL_TREE;
17900 /* If it's a `,', then there are more declarators. */
17901 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17902 cp_lexer_consume_token (parser->lexer);
17903 /* If the next token isn't a `;', then we have a parse error. */
17904 else if (cp_lexer_next_token_is_not (parser->lexer,
17905 CPP_SEMICOLON))
17906 {
17907 /* The next token might be a ways away from where the
17908 actual semicolon is missing. Find the previous token
17909 and use that for our error position. */
17910 cp_token *token = cp_lexer_previous_token (parser->lexer);
17911 error_at (token->location,
17912 "expected %<;%> at end of member declaration");
17913
17914 /* Assume that the user meant to provide a semicolon. If
17915 we were to cp_parser_skip_to_end_of_statement, we might
17916 skip to a semicolon inside a member function definition
17917 and issue nonsensical error messages. */
17918 assume_semicolon = true;
17919 }
17920
17921 if (decl)
17922 {
17923 /* Add DECL to the list of members. */
17924 if (!friend_p)
17925 finish_member_declaration (decl);
17926
17927 if (TREE_CODE (decl) == FUNCTION_DECL)
17928 cp_parser_save_default_args (parser, decl);
17929 }
17930
17931 if (assume_semicolon)
17932 goto out;
17933 }
17934 }
17935
17936 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17937 out:
17938 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17939 }
17940
17941 /* Parse a pure-specifier.
17942
17943 pure-specifier:
17944 = 0
17945
17946 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17947 Otherwise, ERROR_MARK_NODE is returned. */
17948
17949 static tree
17950 cp_parser_pure_specifier (cp_parser* parser)
17951 {
17952 cp_token *token;
17953
17954 /* Look for the `=' token. */
17955 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17956 return error_mark_node;
17957 /* Look for the `0' token. */
17958 token = cp_lexer_peek_token (parser->lexer);
17959
17960 if (token->type == CPP_EOF
17961 || token->type == CPP_PRAGMA_EOL)
17962 return error_mark_node;
17963
17964 cp_lexer_consume_token (parser->lexer);
17965
17966 /* Accept = default or = delete in c++0x mode. */
17967 if (token->keyword == RID_DEFAULT
17968 || token->keyword == RID_DELETE)
17969 {
17970 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17971 return token->u.value;
17972 }
17973
17974 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17975 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17976 {
17977 cp_parser_error (parser,
17978 "invalid pure specifier (only %<= 0%> is allowed)");
17979 cp_parser_skip_to_end_of_statement (parser);
17980 return error_mark_node;
17981 }
17982 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17983 {
17984 error_at (token->location, "templates may not be %<virtual%>");
17985 return error_mark_node;
17986 }
17987
17988 return integer_zero_node;
17989 }
17990
17991 /* Parse a constant-initializer.
17992
17993 constant-initializer:
17994 = constant-expression
17995
17996 Returns a representation of the constant-expression. */
17997
17998 static tree
17999 cp_parser_constant_initializer (cp_parser* parser)
18000 {
18001 /* Look for the `=' token. */
18002 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18003 return error_mark_node;
18004
18005 /* It is invalid to write:
18006
18007 struct S { static const int i = { 7 }; };
18008
18009 */
18010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18011 {
18012 cp_parser_error (parser,
18013 "a brace-enclosed initializer is not allowed here");
18014 /* Consume the opening brace. */
18015 cp_lexer_consume_token (parser->lexer);
18016 /* Skip the initializer. */
18017 cp_parser_skip_to_closing_brace (parser);
18018 /* Look for the trailing `}'. */
18019 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18020
18021 return error_mark_node;
18022 }
18023
18024 return cp_parser_constant_expression (parser,
18025 /*allow_non_constant=*/false,
18026 NULL);
18027 }
18028
18029 /* Derived classes [gram.class.derived] */
18030
18031 /* Parse a base-clause.
18032
18033 base-clause:
18034 : base-specifier-list
18035
18036 base-specifier-list:
18037 base-specifier ... [opt]
18038 base-specifier-list , base-specifier ... [opt]
18039
18040 Returns a TREE_LIST representing the base-classes, in the order in
18041 which they were declared. The representation of each node is as
18042 described by cp_parser_base_specifier.
18043
18044 In the case that no bases are specified, this function will return
18045 NULL_TREE, not ERROR_MARK_NODE. */
18046
18047 static tree
18048 cp_parser_base_clause (cp_parser* parser)
18049 {
18050 tree bases = NULL_TREE;
18051
18052 /* Look for the `:' that begins the list. */
18053 cp_parser_require (parser, CPP_COLON, RT_COLON);
18054
18055 /* Scan the base-specifier-list. */
18056 while (true)
18057 {
18058 cp_token *token;
18059 tree base;
18060 bool pack_expansion_p = false;
18061
18062 /* Look for the base-specifier. */
18063 base = cp_parser_base_specifier (parser);
18064 /* Look for the (optional) ellipsis. */
18065 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18066 {
18067 /* Consume the `...'. */
18068 cp_lexer_consume_token (parser->lexer);
18069
18070 pack_expansion_p = true;
18071 }
18072
18073 /* Add BASE to the front of the list. */
18074 if (base != error_mark_node)
18075 {
18076 if (pack_expansion_p)
18077 /* Make this a pack expansion type. */
18078 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18079
18080
18081 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18082 {
18083 TREE_CHAIN (base) = bases;
18084 bases = base;
18085 }
18086 }
18087 /* Peek at the next token. */
18088 token = cp_lexer_peek_token (parser->lexer);
18089 /* If it's not a comma, then the list is complete. */
18090 if (token->type != CPP_COMMA)
18091 break;
18092 /* Consume the `,'. */
18093 cp_lexer_consume_token (parser->lexer);
18094 }
18095
18096 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18097 base class had a qualified name. However, the next name that
18098 appears is certainly not qualified. */
18099 parser->scope = NULL_TREE;
18100 parser->qualifying_scope = NULL_TREE;
18101 parser->object_scope = NULL_TREE;
18102
18103 return nreverse (bases);
18104 }
18105
18106 /* Parse a base-specifier.
18107
18108 base-specifier:
18109 :: [opt] nested-name-specifier [opt] class-name
18110 virtual access-specifier [opt] :: [opt] nested-name-specifier
18111 [opt] class-name
18112 access-specifier virtual [opt] :: [opt] nested-name-specifier
18113 [opt] class-name
18114
18115 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18116 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18117 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18118 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18119
18120 static tree
18121 cp_parser_base_specifier (cp_parser* parser)
18122 {
18123 cp_token *token;
18124 bool done = false;
18125 bool virtual_p = false;
18126 bool duplicate_virtual_error_issued_p = false;
18127 bool duplicate_access_error_issued_p = false;
18128 bool class_scope_p, template_p;
18129 tree access = access_default_node;
18130 tree type;
18131
18132 /* Process the optional `virtual' and `access-specifier'. */
18133 while (!done)
18134 {
18135 /* Peek at the next token. */
18136 token = cp_lexer_peek_token (parser->lexer);
18137 /* Process `virtual'. */
18138 switch (token->keyword)
18139 {
18140 case RID_VIRTUAL:
18141 /* If `virtual' appears more than once, issue an error. */
18142 if (virtual_p && !duplicate_virtual_error_issued_p)
18143 {
18144 cp_parser_error (parser,
18145 "%<virtual%> specified more than once in base-specified");
18146 duplicate_virtual_error_issued_p = true;
18147 }
18148
18149 virtual_p = true;
18150
18151 /* Consume the `virtual' token. */
18152 cp_lexer_consume_token (parser->lexer);
18153
18154 break;
18155
18156 case RID_PUBLIC:
18157 case RID_PROTECTED:
18158 case RID_PRIVATE:
18159 /* If more than one access specifier appears, issue an
18160 error. */
18161 if (access != access_default_node
18162 && !duplicate_access_error_issued_p)
18163 {
18164 cp_parser_error (parser,
18165 "more than one access specifier in base-specified");
18166 duplicate_access_error_issued_p = true;
18167 }
18168
18169 access = ridpointers[(int) token->keyword];
18170
18171 /* Consume the access-specifier. */
18172 cp_lexer_consume_token (parser->lexer);
18173
18174 break;
18175
18176 default:
18177 done = true;
18178 break;
18179 }
18180 }
18181 /* It is not uncommon to see programs mechanically, erroneously, use
18182 the 'typename' keyword to denote (dependent) qualified types
18183 as base classes. */
18184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18185 {
18186 token = cp_lexer_peek_token (parser->lexer);
18187 if (!processing_template_decl)
18188 error_at (token->location,
18189 "keyword %<typename%> not allowed outside of templates");
18190 else
18191 error_at (token->location,
18192 "keyword %<typename%> not allowed in this context "
18193 "(the base class is implicitly a type)");
18194 cp_lexer_consume_token (parser->lexer);
18195 }
18196
18197 /* Look for the optional `::' operator. */
18198 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18199 /* Look for the nested-name-specifier. The simplest way to
18200 implement:
18201
18202 [temp.res]
18203
18204 The keyword `typename' is not permitted in a base-specifier or
18205 mem-initializer; in these contexts a qualified name that
18206 depends on a template-parameter is implicitly assumed to be a
18207 type name.
18208
18209 is to pretend that we have seen the `typename' keyword at this
18210 point. */
18211 cp_parser_nested_name_specifier_opt (parser,
18212 /*typename_keyword_p=*/true,
18213 /*check_dependency_p=*/true,
18214 typename_type,
18215 /*is_declaration=*/true);
18216 /* If the base class is given by a qualified name, assume that names
18217 we see are type names or templates, as appropriate. */
18218 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18219 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18220
18221 /* Finally, look for the class-name. */
18222 type = cp_parser_class_name (parser,
18223 class_scope_p,
18224 template_p,
18225 typename_type,
18226 /*check_dependency_p=*/true,
18227 /*class_head_p=*/false,
18228 /*is_declaration=*/true);
18229
18230 if (type == error_mark_node)
18231 return error_mark_node;
18232
18233 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18234 }
18235
18236 /* Exception handling [gram.exception] */
18237
18238 /* Parse an (optional) exception-specification.
18239
18240 exception-specification:
18241 throw ( type-id-list [opt] )
18242
18243 Returns a TREE_LIST representing the exception-specification. The
18244 TREE_VALUE of each node is a type. */
18245
18246 static tree
18247 cp_parser_exception_specification_opt (cp_parser* parser)
18248 {
18249 cp_token *token;
18250 tree type_id_list;
18251 const char *saved_message;
18252
18253 /* Peek at the next token. */
18254 token = cp_lexer_peek_token (parser->lexer);
18255
18256 /* Is it a noexcept-specification? */
18257 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18258 {
18259 tree expr;
18260 cp_lexer_consume_token (parser->lexer);
18261
18262 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18263 {
18264 cp_lexer_consume_token (parser->lexer);
18265
18266 /* Types may not be defined in an exception-specification. */
18267 saved_message = parser->type_definition_forbidden_message;
18268 parser->type_definition_forbidden_message
18269 = G_("types may not be defined in an exception-specification");
18270
18271 expr = cp_parser_constant_expression (parser, false, NULL);
18272
18273 /* Restore the saved message. */
18274 parser->type_definition_forbidden_message = saved_message;
18275
18276 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18277 }
18278 else
18279 expr = boolean_true_node;
18280
18281 return build_noexcept_spec (expr, tf_warning_or_error);
18282 }
18283
18284 /* If it's not `throw', then there's no exception-specification. */
18285 if (!cp_parser_is_keyword (token, RID_THROW))
18286 return NULL_TREE;
18287
18288 #if 0
18289 /* Enable this once a lot of code has transitioned to noexcept? */
18290 if (cxx_dialect == cxx0x && !in_system_header)
18291 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18292 "deprecated in C++0x; use %<noexcept%> instead");
18293 #endif
18294
18295 /* Consume the `throw'. */
18296 cp_lexer_consume_token (parser->lexer);
18297
18298 /* Look for the `('. */
18299 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18300
18301 /* Peek at the next token. */
18302 token = cp_lexer_peek_token (parser->lexer);
18303 /* If it's not a `)', then there is a type-id-list. */
18304 if (token->type != CPP_CLOSE_PAREN)
18305 {
18306 /* Types may not be defined in an exception-specification. */
18307 saved_message = parser->type_definition_forbidden_message;
18308 parser->type_definition_forbidden_message
18309 = G_("types may not be defined in an exception-specification");
18310 /* Parse the type-id-list. */
18311 type_id_list = cp_parser_type_id_list (parser);
18312 /* Restore the saved message. */
18313 parser->type_definition_forbidden_message = saved_message;
18314 }
18315 else
18316 type_id_list = empty_except_spec;
18317
18318 /* Look for the `)'. */
18319 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18320
18321 return type_id_list;
18322 }
18323
18324 /* Parse an (optional) type-id-list.
18325
18326 type-id-list:
18327 type-id ... [opt]
18328 type-id-list , type-id ... [opt]
18329
18330 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18331 in the order that the types were presented. */
18332
18333 static tree
18334 cp_parser_type_id_list (cp_parser* parser)
18335 {
18336 tree types = NULL_TREE;
18337
18338 while (true)
18339 {
18340 cp_token *token;
18341 tree type;
18342
18343 /* Get the next type-id. */
18344 type = cp_parser_type_id (parser);
18345 /* Parse the optional ellipsis. */
18346 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18347 {
18348 /* Consume the `...'. */
18349 cp_lexer_consume_token (parser->lexer);
18350
18351 /* Turn the type into a pack expansion expression. */
18352 type = make_pack_expansion (type);
18353 }
18354 /* Add it to the list. */
18355 types = add_exception_specifier (types, type, /*complain=*/1);
18356 /* Peek at the next token. */
18357 token = cp_lexer_peek_token (parser->lexer);
18358 /* If it is not a `,', we are done. */
18359 if (token->type != CPP_COMMA)
18360 break;
18361 /* Consume the `,'. */
18362 cp_lexer_consume_token (parser->lexer);
18363 }
18364
18365 return nreverse (types);
18366 }
18367
18368 /* Parse a try-block.
18369
18370 try-block:
18371 try compound-statement handler-seq */
18372
18373 static tree
18374 cp_parser_try_block (cp_parser* parser)
18375 {
18376 tree try_block;
18377
18378 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18379 try_block = begin_try_block ();
18380 cp_parser_compound_statement (parser, NULL, true, false);
18381 finish_try_block (try_block);
18382 cp_parser_handler_seq (parser);
18383 finish_handler_sequence (try_block);
18384
18385 return try_block;
18386 }
18387
18388 /* Parse a function-try-block.
18389
18390 function-try-block:
18391 try ctor-initializer [opt] function-body handler-seq */
18392
18393 static bool
18394 cp_parser_function_try_block (cp_parser* parser)
18395 {
18396 tree compound_stmt;
18397 tree try_block;
18398 bool ctor_initializer_p;
18399
18400 /* Look for the `try' keyword. */
18401 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18402 return false;
18403 /* Let the rest of the front end know where we are. */
18404 try_block = begin_function_try_block (&compound_stmt);
18405 /* Parse the function-body. */
18406 ctor_initializer_p
18407 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18408 /* We're done with the `try' part. */
18409 finish_function_try_block (try_block);
18410 /* Parse the handlers. */
18411 cp_parser_handler_seq (parser);
18412 /* We're done with the handlers. */
18413 finish_function_handler_sequence (try_block, compound_stmt);
18414
18415 return ctor_initializer_p;
18416 }
18417
18418 /* Parse a handler-seq.
18419
18420 handler-seq:
18421 handler handler-seq [opt] */
18422
18423 static void
18424 cp_parser_handler_seq (cp_parser* parser)
18425 {
18426 while (true)
18427 {
18428 cp_token *token;
18429
18430 /* Parse the handler. */
18431 cp_parser_handler (parser);
18432 /* Peek at the next token. */
18433 token = cp_lexer_peek_token (parser->lexer);
18434 /* If it's not `catch' then there are no more handlers. */
18435 if (!cp_parser_is_keyword (token, RID_CATCH))
18436 break;
18437 }
18438 }
18439
18440 /* Parse a handler.
18441
18442 handler:
18443 catch ( exception-declaration ) compound-statement */
18444
18445 static void
18446 cp_parser_handler (cp_parser* parser)
18447 {
18448 tree handler;
18449 tree declaration;
18450
18451 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18452 handler = begin_handler ();
18453 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18454 declaration = cp_parser_exception_declaration (parser);
18455 finish_handler_parms (declaration, handler);
18456 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18457 cp_parser_compound_statement (parser, NULL, false, false);
18458 finish_handler (handler);
18459 }
18460
18461 /* Parse an exception-declaration.
18462
18463 exception-declaration:
18464 type-specifier-seq declarator
18465 type-specifier-seq abstract-declarator
18466 type-specifier-seq
18467 ...
18468
18469 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18470 ellipsis variant is used. */
18471
18472 static tree
18473 cp_parser_exception_declaration (cp_parser* parser)
18474 {
18475 cp_decl_specifier_seq type_specifiers;
18476 cp_declarator *declarator;
18477 const char *saved_message;
18478
18479 /* If it's an ellipsis, it's easy to handle. */
18480 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18481 {
18482 /* Consume the `...' token. */
18483 cp_lexer_consume_token (parser->lexer);
18484 return NULL_TREE;
18485 }
18486
18487 /* Types may not be defined in exception-declarations. */
18488 saved_message = parser->type_definition_forbidden_message;
18489 parser->type_definition_forbidden_message
18490 = G_("types may not be defined in exception-declarations");
18491
18492 /* Parse the type-specifier-seq. */
18493 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18494 /*is_trailing_return=*/false,
18495 &type_specifiers);
18496 /* If it's a `)', then there is no declarator. */
18497 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18498 declarator = NULL;
18499 else
18500 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18501 /*ctor_dtor_or_conv_p=*/NULL,
18502 /*parenthesized_p=*/NULL,
18503 /*member_p=*/false);
18504
18505 /* Restore the saved message. */
18506 parser->type_definition_forbidden_message = saved_message;
18507
18508 if (!type_specifiers.any_specifiers_p)
18509 return error_mark_node;
18510
18511 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18512 }
18513
18514 /* Parse a throw-expression.
18515
18516 throw-expression:
18517 throw assignment-expression [opt]
18518
18519 Returns a THROW_EXPR representing the throw-expression. */
18520
18521 static tree
18522 cp_parser_throw_expression (cp_parser* parser)
18523 {
18524 tree expression;
18525 cp_token* token;
18526
18527 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18528 token = cp_lexer_peek_token (parser->lexer);
18529 /* Figure out whether or not there is an assignment-expression
18530 following the "throw" keyword. */
18531 if (token->type == CPP_COMMA
18532 || token->type == CPP_SEMICOLON
18533 || token->type == CPP_CLOSE_PAREN
18534 || token->type == CPP_CLOSE_SQUARE
18535 || token->type == CPP_CLOSE_BRACE
18536 || token->type == CPP_COLON)
18537 expression = NULL_TREE;
18538 else
18539 expression = cp_parser_assignment_expression (parser,
18540 /*cast_p=*/false, NULL);
18541
18542 return build_throw (expression);
18543 }
18544
18545 /* GNU Extensions */
18546
18547 /* Parse an (optional) asm-specification.
18548
18549 asm-specification:
18550 asm ( string-literal )
18551
18552 If the asm-specification is present, returns a STRING_CST
18553 corresponding to the string-literal. Otherwise, returns
18554 NULL_TREE. */
18555
18556 static tree
18557 cp_parser_asm_specification_opt (cp_parser* parser)
18558 {
18559 cp_token *token;
18560 tree asm_specification;
18561
18562 /* Peek at the next token. */
18563 token = cp_lexer_peek_token (parser->lexer);
18564 /* If the next token isn't the `asm' keyword, then there's no
18565 asm-specification. */
18566 if (!cp_parser_is_keyword (token, RID_ASM))
18567 return NULL_TREE;
18568
18569 /* Consume the `asm' token. */
18570 cp_lexer_consume_token (parser->lexer);
18571 /* Look for the `('. */
18572 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18573
18574 /* Look for the string-literal. */
18575 asm_specification = cp_parser_string_literal (parser, false, false);
18576
18577 /* Look for the `)'. */
18578 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18579
18580 return asm_specification;
18581 }
18582
18583 /* Parse an asm-operand-list.
18584
18585 asm-operand-list:
18586 asm-operand
18587 asm-operand-list , asm-operand
18588
18589 asm-operand:
18590 string-literal ( expression )
18591 [ string-literal ] string-literal ( expression )
18592
18593 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18594 each node is the expression. The TREE_PURPOSE is itself a
18595 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18596 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18597 is a STRING_CST for the string literal before the parenthesis. Returns
18598 ERROR_MARK_NODE if any of the operands are invalid. */
18599
18600 static tree
18601 cp_parser_asm_operand_list (cp_parser* parser)
18602 {
18603 tree asm_operands = NULL_TREE;
18604 bool invalid_operands = false;
18605
18606 while (true)
18607 {
18608 tree string_literal;
18609 tree expression;
18610 tree name;
18611
18612 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18613 {
18614 /* Consume the `[' token. */
18615 cp_lexer_consume_token (parser->lexer);
18616 /* Read the operand name. */
18617 name = cp_parser_identifier (parser);
18618 if (name != error_mark_node)
18619 name = build_string (IDENTIFIER_LENGTH (name),
18620 IDENTIFIER_POINTER (name));
18621 /* Look for the closing `]'. */
18622 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18623 }
18624 else
18625 name = NULL_TREE;
18626 /* Look for the string-literal. */
18627 string_literal = cp_parser_string_literal (parser, false, false);
18628
18629 /* Look for the `('. */
18630 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18631 /* Parse the expression. */
18632 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18633 /* Look for the `)'. */
18634 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18635
18636 if (name == error_mark_node
18637 || string_literal == error_mark_node
18638 || expression == error_mark_node)
18639 invalid_operands = true;
18640
18641 /* Add this operand to the list. */
18642 asm_operands = tree_cons (build_tree_list (name, string_literal),
18643 expression,
18644 asm_operands);
18645 /* If the next token is not a `,', there are no more
18646 operands. */
18647 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18648 break;
18649 /* Consume the `,'. */
18650 cp_lexer_consume_token (parser->lexer);
18651 }
18652
18653 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18654 }
18655
18656 /* Parse an asm-clobber-list.
18657
18658 asm-clobber-list:
18659 string-literal
18660 asm-clobber-list , string-literal
18661
18662 Returns a TREE_LIST, indicating the clobbers in the order that they
18663 appeared. The TREE_VALUE of each node is a STRING_CST. */
18664
18665 static tree
18666 cp_parser_asm_clobber_list (cp_parser* parser)
18667 {
18668 tree clobbers = NULL_TREE;
18669
18670 while (true)
18671 {
18672 tree string_literal;
18673
18674 /* Look for the string literal. */
18675 string_literal = cp_parser_string_literal (parser, false, false);
18676 /* Add it to the list. */
18677 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18678 /* If the next token is not a `,', then the list is
18679 complete. */
18680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18681 break;
18682 /* Consume the `,' token. */
18683 cp_lexer_consume_token (parser->lexer);
18684 }
18685
18686 return clobbers;
18687 }
18688
18689 /* Parse an asm-label-list.
18690
18691 asm-label-list:
18692 identifier
18693 asm-label-list , identifier
18694
18695 Returns a TREE_LIST, indicating the labels in the order that they
18696 appeared. The TREE_VALUE of each node is a label. */
18697
18698 static tree
18699 cp_parser_asm_label_list (cp_parser* parser)
18700 {
18701 tree labels = NULL_TREE;
18702
18703 while (true)
18704 {
18705 tree identifier, label, name;
18706
18707 /* Look for the identifier. */
18708 identifier = cp_parser_identifier (parser);
18709 if (!error_operand_p (identifier))
18710 {
18711 label = lookup_label (identifier);
18712 if (TREE_CODE (label) == LABEL_DECL)
18713 {
18714 TREE_USED (label) = 1;
18715 check_goto (label);
18716 name = build_string (IDENTIFIER_LENGTH (identifier),
18717 IDENTIFIER_POINTER (identifier));
18718 labels = tree_cons (name, label, labels);
18719 }
18720 }
18721 /* If the next token is not a `,', then the list is
18722 complete. */
18723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18724 break;
18725 /* Consume the `,' token. */
18726 cp_lexer_consume_token (parser->lexer);
18727 }
18728
18729 return nreverse (labels);
18730 }
18731
18732 /* Parse an (optional) series of attributes.
18733
18734 attributes:
18735 attributes attribute
18736
18737 attribute:
18738 __attribute__ (( attribute-list [opt] ))
18739
18740 The return value is as for cp_parser_attribute_list. */
18741
18742 static tree
18743 cp_parser_attributes_opt (cp_parser* parser)
18744 {
18745 tree attributes = NULL_TREE;
18746
18747 while (true)
18748 {
18749 cp_token *token;
18750 tree attribute_list;
18751
18752 /* Peek at the next token. */
18753 token = cp_lexer_peek_token (parser->lexer);
18754 /* If it's not `__attribute__', then we're done. */
18755 if (token->keyword != RID_ATTRIBUTE)
18756 break;
18757
18758 /* Consume the `__attribute__' keyword. */
18759 cp_lexer_consume_token (parser->lexer);
18760 /* Look for the two `(' tokens. */
18761 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18762 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18763
18764 /* Peek at the next token. */
18765 token = cp_lexer_peek_token (parser->lexer);
18766 if (token->type != CPP_CLOSE_PAREN)
18767 /* Parse the attribute-list. */
18768 attribute_list = cp_parser_attribute_list (parser);
18769 else
18770 /* If the next token is a `)', then there is no attribute
18771 list. */
18772 attribute_list = NULL;
18773
18774 /* Look for the two `)' tokens. */
18775 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18776 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18777
18778 /* Add these new attributes to the list. */
18779 attributes = chainon (attributes, attribute_list);
18780 }
18781
18782 return attributes;
18783 }
18784
18785 /* Parse an attribute-list.
18786
18787 attribute-list:
18788 attribute
18789 attribute-list , attribute
18790
18791 attribute:
18792 identifier
18793 identifier ( identifier )
18794 identifier ( identifier , expression-list )
18795 identifier ( expression-list )
18796
18797 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18798 to an attribute. The TREE_PURPOSE of each node is the identifier
18799 indicating which attribute is in use. The TREE_VALUE represents
18800 the arguments, if any. */
18801
18802 static tree
18803 cp_parser_attribute_list (cp_parser* parser)
18804 {
18805 tree attribute_list = NULL_TREE;
18806 bool save_translate_strings_p = parser->translate_strings_p;
18807
18808 parser->translate_strings_p = false;
18809 while (true)
18810 {
18811 cp_token *token;
18812 tree identifier;
18813 tree attribute;
18814
18815 /* Look for the identifier. We also allow keywords here; for
18816 example `__attribute__ ((const))' is legal. */
18817 token = cp_lexer_peek_token (parser->lexer);
18818 if (token->type == CPP_NAME
18819 || token->type == CPP_KEYWORD)
18820 {
18821 tree arguments = NULL_TREE;
18822
18823 /* Consume the token. */
18824 token = cp_lexer_consume_token (parser->lexer);
18825
18826 /* Save away the identifier that indicates which attribute
18827 this is. */
18828 identifier = (token->type == CPP_KEYWORD)
18829 /* For keywords, use the canonical spelling, not the
18830 parsed identifier. */
18831 ? ridpointers[(int) token->keyword]
18832 : token->u.value;
18833
18834 attribute = build_tree_list (identifier, NULL_TREE);
18835
18836 /* Peek at the next token. */
18837 token = cp_lexer_peek_token (parser->lexer);
18838 /* If it's an `(', then parse the attribute arguments. */
18839 if (token->type == CPP_OPEN_PAREN)
18840 {
18841 VEC(tree,gc) *vec;
18842 int attr_flag = (attribute_takes_identifier_p (identifier)
18843 ? id_attr : normal_attr);
18844 vec = cp_parser_parenthesized_expression_list
18845 (parser, attr_flag, /*cast_p=*/false,
18846 /*allow_expansion_p=*/false,
18847 /*non_constant_p=*/NULL);
18848 if (vec == NULL)
18849 arguments = error_mark_node;
18850 else
18851 {
18852 arguments = build_tree_list_vec (vec);
18853 release_tree_vector (vec);
18854 }
18855 /* Save the arguments away. */
18856 TREE_VALUE (attribute) = arguments;
18857 }
18858
18859 if (arguments != error_mark_node)
18860 {
18861 /* Add this attribute to the list. */
18862 TREE_CHAIN (attribute) = attribute_list;
18863 attribute_list = attribute;
18864 }
18865
18866 token = cp_lexer_peek_token (parser->lexer);
18867 }
18868 /* Now, look for more attributes. If the next token isn't a
18869 `,', we're done. */
18870 if (token->type != CPP_COMMA)
18871 break;
18872
18873 /* Consume the comma and keep going. */
18874 cp_lexer_consume_token (parser->lexer);
18875 }
18876 parser->translate_strings_p = save_translate_strings_p;
18877
18878 /* We built up the list in reverse order. */
18879 return nreverse (attribute_list);
18880 }
18881
18882 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18883 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18884 current value of the PEDANTIC flag, regardless of whether or not
18885 the `__extension__' keyword is present. The caller is responsible
18886 for restoring the value of the PEDANTIC flag. */
18887
18888 static bool
18889 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18890 {
18891 /* Save the old value of the PEDANTIC flag. */
18892 *saved_pedantic = pedantic;
18893
18894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18895 {
18896 /* Consume the `__extension__' token. */
18897 cp_lexer_consume_token (parser->lexer);
18898 /* We're not being pedantic while the `__extension__' keyword is
18899 in effect. */
18900 pedantic = 0;
18901
18902 return true;
18903 }
18904
18905 return false;
18906 }
18907
18908 /* Parse a label declaration.
18909
18910 label-declaration:
18911 __label__ label-declarator-seq ;
18912
18913 label-declarator-seq:
18914 identifier , label-declarator-seq
18915 identifier */
18916
18917 static void
18918 cp_parser_label_declaration (cp_parser* parser)
18919 {
18920 /* Look for the `__label__' keyword. */
18921 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18922
18923 while (true)
18924 {
18925 tree identifier;
18926
18927 /* Look for an identifier. */
18928 identifier = cp_parser_identifier (parser);
18929 /* If we failed, stop. */
18930 if (identifier == error_mark_node)
18931 break;
18932 /* Declare it as a label. */
18933 finish_label_decl (identifier);
18934 /* If the next token is a `;', stop. */
18935 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18936 break;
18937 /* Look for the `,' separating the label declarations. */
18938 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18939 }
18940
18941 /* Look for the final `;'. */
18942 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18943 }
18944
18945 /* Support Functions */
18946
18947 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18948 NAME should have one of the representations used for an
18949 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18950 is returned. If PARSER->SCOPE is a dependent type, then a
18951 SCOPE_REF is returned.
18952
18953 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18954 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18955 was formed. Abstractly, such entities should not be passed to this
18956 function, because they do not need to be looked up, but it is
18957 simpler to check for this special case here, rather than at the
18958 call-sites.
18959
18960 In cases not explicitly covered above, this function returns a
18961 DECL, OVERLOAD, or baselink representing the result of the lookup.
18962 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18963 is returned.
18964
18965 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18966 (e.g., "struct") that was used. In that case bindings that do not
18967 refer to types are ignored.
18968
18969 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18970 ignored.
18971
18972 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18973 are ignored.
18974
18975 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18976 types.
18977
18978 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18979 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18980 NULL_TREE otherwise. */
18981
18982 static tree
18983 cp_parser_lookup_name (cp_parser *parser, tree name,
18984 enum tag_types tag_type,
18985 bool is_template,
18986 bool is_namespace,
18987 bool check_dependency,
18988 tree *ambiguous_decls,
18989 location_t name_location)
18990 {
18991 int flags = 0;
18992 tree decl;
18993 tree object_type = parser->context->object_type;
18994
18995 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18996 flags |= LOOKUP_COMPLAIN;
18997
18998 /* Assume that the lookup will be unambiguous. */
18999 if (ambiguous_decls)
19000 *ambiguous_decls = NULL_TREE;
19001
19002 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19003 no longer valid. Note that if we are parsing tentatively, and
19004 the parse fails, OBJECT_TYPE will be automatically restored. */
19005 parser->context->object_type = NULL_TREE;
19006
19007 if (name == error_mark_node)
19008 return error_mark_node;
19009
19010 /* A template-id has already been resolved; there is no lookup to
19011 do. */
19012 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19013 return name;
19014 if (BASELINK_P (name))
19015 {
19016 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19017 == TEMPLATE_ID_EXPR);
19018 return name;
19019 }
19020
19021 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19022 it should already have been checked to make sure that the name
19023 used matches the type being destroyed. */
19024 if (TREE_CODE (name) == BIT_NOT_EXPR)
19025 {
19026 tree type;
19027
19028 /* Figure out to which type this destructor applies. */
19029 if (parser->scope)
19030 type = parser->scope;
19031 else if (object_type)
19032 type = object_type;
19033 else
19034 type = current_class_type;
19035 /* If that's not a class type, there is no destructor. */
19036 if (!type || !CLASS_TYPE_P (type))
19037 return error_mark_node;
19038 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19039 lazily_declare_fn (sfk_destructor, type);
19040 if (!CLASSTYPE_DESTRUCTORS (type))
19041 return error_mark_node;
19042 /* If it was a class type, return the destructor. */
19043 return CLASSTYPE_DESTRUCTORS (type);
19044 }
19045
19046 /* By this point, the NAME should be an ordinary identifier. If
19047 the id-expression was a qualified name, the qualifying scope is
19048 stored in PARSER->SCOPE at this point. */
19049 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19050
19051 /* Perform the lookup. */
19052 if (parser->scope)
19053 {
19054 bool dependent_p;
19055
19056 if (parser->scope == error_mark_node)
19057 return error_mark_node;
19058
19059 /* If the SCOPE is dependent, the lookup must be deferred until
19060 the template is instantiated -- unless we are explicitly
19061 looking up names in uninstantiated templates. Even then, we
19062 cannot look up the name if the scope is not a class type; it
19063 might, for example, be a template type parameter. */
19064 dependent_p = (TYPE_P (parser->scope)
19065 && dependent_scope_p (parser->scope));
19066 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19067 && dependent_p)
19068 /* Defer lookup. */
19069 decl = error_mark_node;
19070 else
19071 {
19072 tree pushed_scope = NULL_TREE;
19073
19074 /* If PARSER->SCOPE is a dependent type, then it must be a
19075 class type, and we must not be checking dependencies;
19076 otherwise, we would have processed this lookup above. So
19077 that PARSER->SCOPE is not considered a dependent base by
19078 lookup_member, we must enter the scope here. */
19079 if (dependent_p)
19080 pushed_scope = push_scope (parser->scope);
19081
19082 /* If the PARSER->SCOPE is a template specialization, it
19083 may be instantiated during name lookup. In that case,
19084 errors may be issued. Even if we rollback the current
19085 tentative parse, those errors are valid. */
19086 decl = lookup_qualified_name (parser->scope, name,
19087 tag_type != none_type,
19088 /*complain=*/true);
19089
19090 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19091 lookup result and the nested-name-specifier nominates a class C:
19092 * if the name specified after the nested-name-specifier, when
19093 looked up in C, is the injected-class-name of C (Clause 9), or
19094 * if the name specified after the nested-name-specifier is the
19095 same as the identifier or the simple-template-id's template-
19096 name in the last component of the nested-name-specifier,
19097 the name is instead considered to name the constructor of
19098 class C. [ Note: for example, the constructor is not an
19099 acceptable lookup result in an elaborated-type-specifier so
19100 the constructor would not be used in place of the
19101 injected-class-name. --end note ] Such a constructor name
19102 shall be used only in the declarator-id of a declaration that
19103 names a constructor or in a using-declaration. */
19104 if (tag_type == none_type
19105 && DECL_SELF_REFERENCE_P (decl)
19106 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19107 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19108 tag_type != none_type,
19109 /*complain=*/true);
19110
19111 /* If we have a single function from a using decl, pull it out. */
19112 if (TREE_CODE (decl) == OVERLOAD
19113 && !really_overloaded_fn (decl))
19114 decl = OVL_FUNCTION (decl);
19115
19116 if (pushed_scope)
19117 pop_scope (pushed_scope);
19118 }
19119
19120 /* If the scope is a dependent type and either we deferred lookup or
19121 we did lookup but didn't find the name, rememeber the name. */
19122 if (decl == error_mark_node && TYPE_P (parser->scope)
19123 && dependent_type_p (parser->scope))
19124 {
19125 if (tag_type)
19126 {
19127 tree type;
19128
19129 /* The resolution to Core Issue 180 says that `struct
19130 A::B' should be considered a type-name, even if `A'
19131 is dependent. */
19132 type = make_typename_type (parser->scope, name, tag_type,
19133 /*complain=*/tf_error);
19134 decl = TYPE_NAME (type);
19135 }
19136 else if (is_template
19137 && (cp_parser_next_token_ends_template_argument_p (parser)
19138 || cp_lexer_next_token_is (parser->lexer,
19139 CPP_CLOSE_PAREN)))
19140 decl = make_unbound_class_template (parser->scope,
19141 name, NULL_TREE,
19142 /*complain=*/tf_error);
19143 else
19144 decl = build_qualified_name (/*type=*/NULL_TREE,
19145 parser->scope, name,
19146 is_template);
19147 }
19148 parser->qualifying_scope = parser->scope;
19149 parser->object_scope = NULL_TREE;
19150 }
19151 else if (object_type)
19152 {
19153 tree object_decl = NULL_TREE;
19154 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19155 OBJECT_TYPE is not a class. */
19156 if (CLASS_TYPE_P (object_type))
19157 /* If the OBJECT_TYPE is a template specialization, it may
19158 be instantiated during name lookup. In that case, errors
19159 may be issued. Even if we rollback the current tentative
19160 parse, those errors are valid. */
19161 object_decl = lookup_member (object_type,
19162 name,
19163 /*protect=*/0,
19164 tag_type != none_type);
19165 /* Look it up in the enclosing context, too. */
19166 decl = lookup_name_real (name, tag_type != none_type,
19167 /*nonclass=*/0,
19168 /*block_p=*/true, is_namespace, flags);
19169 parser->object_scope = object_type;
19170 parser->qualifying_scope = NULL_TREE;
19171 if (object_decl)
19172 decl = object_decl;
19173 }
19174 else
19175 {
19176 decl = lookup_name_real (name, tag_type != none_type,
19177 /*nonclass=*/0,
19178 /*block_p=*/true, is_namespace, flags);
19179 parser->qualifying_scope = NULL_TREE;
19180 parser->object_scope = NULL_TREE;
19181 }
19182
19183 /* If the lookup failed, let our caller know. */
19184 if (!decl || decl == error_mark_node)
19185 return error_mark_node;
19186
19187 /* Pull out the template from an injected-class-name (or multiple). */
19188 if (is_template)
19189 decl = maybe_get_template_decl_from_type_decl (decl);
19190
19191 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19192 if (TREE_CODE (decl) == TREE_LIST)
19193 {
19194 if (ambiguous_decls)
19195 *ambiguous_decls = decl;
19196 /* The error message we have to print is too complicated for
19197 cp_parser_error, so we incorporate its actions directly. */
19198 if (!cp_parser_simulate_error (parser))
19199 {
19200 error_at (name_location, "reference to %qD is ambiguous",
19201 name);
19202 print_candidates (decl);
19203 }
19204 return error_mark_node;
19205 }
19206
19207 gcc_assert (DECL_P (decl)
19208 || TREE_CODE (decl) == OVERLOAD
19209 || TREE_CODE (decl) == SCOPE_REF
19210 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19211 || BASELINK_P (decl));
19212
19213 /* If we have resolved the name of a member declaration, check to
19214 see if the declaration is accessible. When the name resolves to
19215 set of overloaded functions, accessibility is checked when
19216 overload resolution is done.
19217
19218 During an explicit instantiation, access is not checked at all,
19219 as per [temp.explicit]. */
19220 if (DECL_P (decl))
19221 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19222
19223 return decl;
19224 }
19225
19226 /* Like cp_parser_lookup_name, but for use in the typical case where
19227 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19228 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19229
19230 static tree
19231 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19232 {
19233 return cp_parser_lookup_name (parser, name,
19234 none_type,
19235 /*is_template=*/false,
19236 /*is_namespace=*/false,
19237 /*check_dependency=*/true,
19238 /*ambiguous_decls=*/NULL,
19239 location);
19240 }
19241
19242 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19243 the current context, return the TYPE_DECL. If TAG_NAME_P is
19244 true, the DECL indicates the class being defined in a class-head,
19245 or declared in an elaborated-type-specifier.
19246
19247 Otherwise, return DECL. */
19248
19249 static tree
19250 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19251 {
19252 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19253 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19254
19255 struct A {
19256 template <typename T> struct B;
19257 };
19258
19259 template <typename T> struct A::B {};
19260
19261 Similarly, in an elaborated-type-specifier:
19262
19263 namespace N { struct X{}; }
19264
19265 struct A {
19266 template <typename T> friend struct N::X;
19267 };
19268
19269 However, if the DECL refers to a class type, and we are in
19270 the scope of the class, then the name lookup automatically
19271 finds the TYPE_DECL created by build_self_reference rather
19272 than a TEMPLATE_DECL. For example, in:
19273
19274 template <class T> struct S {
19275 S s;
19276 };
19277
19278 there is no need to handle such case. */
19279
19280 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19281 return DECL_TEMPLATE_RESULT (decl);
19282
19283 return decl;
19284 }
19285
19286 /* If too many, or too few, template-parameter lists apply to the
19287 declarator, issue an error message. Returns TRUE if all went well,
19288 and FALSE otherwise. */
19289
19290 static bool
19291 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19292 cp_declarator *declarator,
19293 location_t declarator_location)
19294 {
19295 unsigned num_templates;
19296
19297 /* We haven't seen any classes that involve template parameters yet. */
19298 num_templates = 0;
19299
19300 switch (declarator->kind)
19301 {
19302 case cdk_id:
19303 if (declarator->u.id.qualifying_scope)
19304 {
19305 tree scope;
19306
19307 scope = declarator->u.id.qualifying_scope;
19308
19309 while (scope && CLASS_TYPE_P (scope))
19310 {
19311 /* You're supposed to have one `template <...>'
19312 for every template class, but you don't need one
19313 for a full specialization. For example:
19314
19315 template <class T> struct S{};
19316 template <> struct S<int> { void f(); };
19317 void S<int>::f () {}
19318
19319 is correct; there shouldn't be a `template <>' for
19320 the definition of `S<int>::f'. */
19321 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19322 /* If SCOPE does not have template information of any
19323 kind, then it is not a template, nor is it nested
19324 within a template. */
19325 break;
19326 if (explicit_class_specialization_p (scope))
19327 break;
19328 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19329 ++num_templates;
19330
19331 scope = TYPE_CONTEXT (scope);
19332 }
19333 }
19334 else if (TREE_CODE (declarator->u.id.unqualified_name)
19335 == TEMPLATE_ID_EXPR)
19336 /* If the DECLARATOR has the form `X<y>' then it uses one
19337 additional level of template parameters. */
19338 ++num_templates;
19339
19340 return cp_parser_check_template_parameters
19341 (parser, num_templates, declarator_location, declarator);
19342
19343
19344 case cdk_function:
19345 case cdk_array:
19346 case cdk_pointer:
19347 case cdk_reference:
19348 case cdk_ptrmem:
19349 return (cp_parser_check_declarator_template_parameters
19350 (parser, declarator->declarator, declarator_location));
19351
19352 case cdk_error:
19353 return true;
19354
19355 default:
19356 gcc_unreachable ();
19357 }
19358 return false;
19359 }
19360
19361 /* NUM_TEMPLATES were used in the current declaration. If that is
19362 invalid, return FALSE and issue an error messages. Otherwise,
19363 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19364 declarator and we can print more accurate diagnostics. */
19365
19366 static bool
19367 cp_parser_check_template_parameters (cp_parser* parser,
19368 unsigned num_templates,
19369 location_t location,
19370 cp_declarator *declarator)
19371 {
19372 /* If there are the same number of template classes and parameter
19373 lists, that's OK. */
19374 if (parser->num_template_parameter_lists == num_templates)
19375 return true;
19376 /* If there are more, but only one more, then we are referring to a
19377 member template. That's OK too. */
19378 if (parser->num_template_parameter_lists == num_templates + 1)
19379 return true;
19380 /* If there are more template classes than parameter lists, we have
19381 something like:
19382
19383 template <class T> void S<T>::R<T>::f (); */
19384 if (parser->num_template_parameter_lists < num_templates)
19385 {
19386 if (declarator && !current_function_decl)
19387 error_at (location, "specializing member %<%T::%E%> "
19388 "requires %<template<>%> syntax",
19389 declarator->u.id.qualifying_scope,
19390 declarator->u.id.unqualified_name);
19391 else if (declarator)
19392 error_at (location, "invalid declaration of %<%T::%E%>",
19393 declarator->u.id.qualifying_scope,
19394 declarator->u.id.unqualified_name);
19395 else
19396 error_at (location, "too few template-parameter-lists");
19397 return false;
19398 }
19399 /* Otherwise, there are too many template parameter lists. We have
19400 something like:
19401
19402 template <class T> template <class U> void S::f(); */
19403 error_at (location, "too many template-parameter-lists");
19404 return false;
19405 }
19406
19407 /* Parse an optional `::' token indicating that the following name is
19408 from the global namespace. If so, PARSER->SCOPE is set to the
19409 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19410 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19411 Returns the new value of PARSER->SCOPE, if the `::' token is
19412 present, and NULL_TREE otherwise. */
19413
19414 static tree
19415 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19416 {
19417 cp_token *token;
19418
19419 /* Peek at the next token. */
19420 token = cp_lexer_peek_token (parser->lexer);
19421 /* If we're looking at a `::' token then we're starting from the
19422 global namespace, not our current location. */
19423 if (token->type == CPP_SCOPE)
19424 {
19425 /* Consume the `::' token. */
19426 cp_lexer_consume_token (parser->lexer);
19427 /* Set the SCOPE so that we know where to start the lookup. */
19428 parser->scope = global_namespace;
19429 parser->qualifying_scope = global_namespace;
19430 parser->object_scope = NULL_TREE;
19431
19432 return parser->scope;
19433 }
19434 else if (!current_scope_valid_p)
19435 {
19436 parser->scope = NULL_TREE;
19437 parser->qualifying_scope = NULL_TREE;
19438 parser->object_scope = NULL_TREE;
19439 }
19440
19441 return NULL_TREE;
19442 }
19443
19444 /* Returns TRUE if the upcoming token sequence is the start of a
19445 constructor declarator. If FRIEND_P is true, the declarator is
19446 preceded by the `friend' specifier. */
19447
19448 static bool
19449 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19450 {
19451 bool constructor_p;
19452 tree nested_name_specifier;
19453 cp_token *next_token;
19454
19455 /* The common case is that this is not a constructor declarator, so
19456 try to avoid doing lots of work if at all possible. It's not
19457 valid declare a constructor at function scope. */
19458 if (parser->in_function_body)
19459 return false;
19460 /* And only certain tokens can begin a constructor declarator. */
19461 next_token = cp_lexer_peek_token (parser->lexer);
19462 if (next_token->type != CPP_NAME
19463 && next_token->type != CPP_SCOPE
19464 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19465 && next_token->type != CPP_TEMPLATE_ID)
19466 return false;
19467
19468 /* Parse tentatively; we are going to roll back all of the tokens
19469 consumed here. */
19470 cp_parser_parse_tentatively (parser);
19471 /* Assume that we are looking at a constructor declarator. */
19472 constructor_p = true;
19473
19474 /* Look for the optional `::' operator. */
19475 cp_parser_global_scope_opt (parser,
19476 /*current_scope_valid_p=*/false);
19477 /* Look for the nested-name-specifier. */
19478 nested_name_specifier
19479 = (cp_parser_nested_name_specifier_opt (parser,
19480 /*typename_keyword_p=*/false,
19481 /*check_dependency_p=*/false,
19482 /*type_p=*/false,
19483 /*is_declaration=*/false));
19484 /* Outside of a class-specifier, there must be a
19485 nested-name-specifier. */
19486 if (!nested_name_specifier &&
19487 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19488 || friend_p))
19489 constructor_p = false;
19490 else if (nested_name_specifier == error_mark_node)
19491 constructor_p = false;
19492
19493 /* If we have a class scope, this is easy; DR 147 says that S::S always
19494 names the constructor, and no other qualified name could. */
19495 if (constructor_p && nested_name_specifier
19496 && TYPE_P (nested_name_specifier))
19497 {
19498 tree id = cp_parser_unqualified_id (parser,
19499 /*template_keyword_p=*/false,
19500 /*check_dependency_p=*/false,
19501 /*declarator_p=*/true,
19502 /*optional_p=*/false);
19503 if (is_overloaded_fn (id))
19504 id = DECL_NAME (get_first_fn (id));
19505 if (!constructor_name_p (id, nested_name_specifier))
19506 constructor_p = false;
19507 }
19508 /* If we still think that this might be a constructor-declarator,
19509 look for a class-name. */
19510 else if (constructor_p)
19511 {
19512 /* If we have:
19513
19514 template <typename T> struct S {
19515 S();
19516 };
19517
19518 we must recognize that the nested `S' names a class. */
19519 tree type_decl;
19520 type_decl = cp_parser_class_name (parser,
19521 /*typename_keyword_p=*/false,
19522 /*template_keyword_p=*/false,
19523 none_type,
19524 /*check_dependency_p=*/false,
19525 /*class_head_p=*/false,
19526 /*is_declaration=*/false);
19527 /* If there was no class-name, then this is not a constructor. */
19528 constructor_p = !cp_parser_error_occurred (parser);
19529
19530 /* If we're still considering a constructor, we have to see a `(',
19531 to begin the parameter-declaration-clause, followed by either a
19532 `)', an `...', or a decl-specifier. We need to check for a
19533 type-specifier to avoid being fooled into thinking that:
19534
19535 S (f) (int);
19536
19537 is a constructor. (It is actually a function named `f' that
19538 takes one parameter (of type `int') and returns a value of type
19539 `S'. */
19540 if (constructor_p
19541 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19542 constructor_p = false;
19543
19544 if (constructor_p
19545 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19546 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19547 /* A parameter declaration begins with a decl-specifier,
19548 which is either the "attribute" keyword, a storage class
19549 specifier, or (usually) a type-specifier. */
19550 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19551 {
19552 tree type;
19553 tree pushed_scope = NULL_TREE;
19554 unsigned saved_num_template_parameter_lists;
19555
19556 /* Names appearing in the type-specifier should be looked up
19557 in the scope of the class. */
19558 if (current_class_type)
19559 type = NULL_TREE;
19560 else
19561 {
19562 type = TREE_TYPE (type_decl);
19563 if (TREE_CODE (type) == TYPENAME_TYPE)
19564 {
19565 type = resolve_typename_type (type,
19566 /*only_current_p=*/false);
19567 if (TREE_CODE (type) == TYPENAME_TYPE)
19568 {
19569 cp_parser_abort_tentative_parse (parser);
19570 return false;
19571 }
19572 }
19573 pushed_scope = push_scope (type);
19574 }
19575
19576 /* Inside the constructor parameter list, surrounding
19577 template-parameter-lists do not apply. */
19578 saved_num_template_parameter_lists
19579 = parser->num_template_parameter_lists;
19580 parser->num_template_parameter_lists = 0;
19581
19582 /* Look for the type-specifier. */
19583 cp_parser_type_specifier (parser,
19584 CP_PARSER_FLAGS_NONE,
19585 /*decl_specs=*/NULL,
19586 /*is_declarator=*/true,
19587 /*declares_class_or_enum=*/NULL,
19588 /*is_cv_qualifier=*/NULL);
19589
19590 parser->num_template_parameter_lists
19591 = saved_num_template_parameter_lists;
19592
19593 /* Leave the scope of the class. */
19594 if (pushed_scope)
19595 pop_scope (pushed_scope);
19596
19597 constructor_p = !cp_parser_error_occurred (parser);
19598 }
19599 }
19600
19601 /* We did not really want to consume any tokens. */
19602 cp_parser_abort_tentative_parse (parser);
19603
19604 return constructor_p;
19605 }
19606
19607 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19608 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19609 they must be performed once we are in the scope of the function.
19610
19611 Returns the function defined. */
19612
19613 static tree
19614 cp_parser_function_definition_from_specifiers_and_declarator
19615 (cp_parser* parser,
19616 cp_decl_specifier_seq *decl_specifiers,
19617 tree attributes,
19618 const cp_declarator *declarator)
19619 {
19620 tree fn;
19621 bool success_p;
19622
19623 /* Begin the function-definition. */
19624 success_p = start_function (decl_specifiers, declarator, attributes);
19625
19626 /* The things we're about to see are not directly qualified by any
19627 template headers we've seen thus far. */
19628 reset_specialization ();
19629
19630 /* If there were names looked up in the decl-specifier-seq that we
19631 did not check, check them now. We must wait until we are in the
19632 scope of the function to perform the checks, since the function
19633 might be a friend. */
19634 perform_deferred_access_checks ();
19635
19636 if (!success_p)
19637 {
19638 /* Skip the entire function. */
19639 cp_parser_skip_to_end_of_block_or_statement (parser);
19640 fn = error_mark_node;
19641 }
19642 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19643 {
19644 /* Seen already, skip it. An error message has already been output. */
19645 cp_parser_skip_to_end_of_block_or_statement (parser);
19646 fn = current_function_decl;
19647 current_function_decl = NULL_TREE;
19648 /* If this is a function from a class, pop the nested class. */
19649 if (current_class_name)
19650 pop_nested_class ();
19651 }
19652 else
19653 fn = cp_parser_function_definition_after_declarator (parser,
19654 /*inline_p=*/false);
19655
19656 return fn;
19657 }
19658
19659 /* Parse the part of a function-definition that follows the
19660 declarator. INLINE_P is TRUE iff this function is an inline
19661 function defined within a class-specifier.
19662
19663 Returns the function defined. */
19664
19665 static tree
19666 cp_parser_function_definition_after_declarator (cp_parser* parser,
19667 bool inline_p)
19668 {
19669 tree fn;
19670 bool ctor_initializer_p = false;
19671 bool saved_in_unbraced_linkage_specification_p;
19672 bool saved_in_function_body;
19673 unsigned saved_num_template_parameter_lists;
19674 cp_token *token;
19675
19676 saved_in_function_body = parser->in_function_body;
19677 parser->in_function_body = true;
19678 /* If the next token is `return', then the code may be trying to
19679 make use of the "named return value" extension that G++ used to
19680 support. */
19681 token = cp_lexer_peek_token (parser->lexer);
19682 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19683 {
19684 /* Consume the `return' keyword. */
19685 cp_lexer_consume_token (parser->lexer);
19686 /* Look for the identifier that indicates what value is to be
19687 returned. */
19688 cp_parser_identifier (parser);
19689 /* Issue an error message. */
19690 error_at (token->location,
19691 "named return values are no longer supported");
19692 /* Skip tokens until we reach the start of the function body. */
19693 while (true)
19694 {
19695 cp_token *token = cp_lexer_peek_token (parser->lexer);
19696 if (token->type == CPP_OPEN_BRACE
19697 || token->type == CPP_EOF
19698 || token->type == CPP_PRAGMA_EOL)
19699 break;
19700 cp_lexer_consume_token (parser->lexer);
19701 }
19702 }
19703 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19704 anything declared inside `f'. */
19705 saved_in_unbraced_linkage_specification_p
19706 = parser->in_unbraced_linkage_specification_p;
19707 parser->in_unbraced_linkage_specification_p = false;
19708 /* Inside the function, surrounding template-parameter-lists do not
19709 apply. */
19710 saved_num_template_parameter_lists
19711 = parser->num_template_parameter_lists;
19712 parser->num_template_parameter_lists = 0;
19713
19714 start_lambda_scope (current_function_decl);
19715
19716 /* If the next token is `try', then we are looking at a
19717 function-try-block. */
19718 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19719 ctor_initializer_p = cp_parser_function_try_block (parser);
19720 /* A function-try-block includes the function-body, so we only do
19721 this next part if we're not processing a function-try-block. */
19722 else
19723 ctor_initializer_p
19724 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19725
19726 finish_lambda_scope ();
19727
19728 /* Finish the function. */
19729 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19730 (inline_p ? 2 : 0));
19731 /* Generate code for it, if necessary. */
19732 expand_or_defer_fn (fn);
19733 /* Restore the saved values. */
19734 parser->in_unbraced_linkage_specification_p
19735 = saved_in_unbraced_linkage_specification_p;
19736 parser->num_template_parameter_lists
19737 = saved_num_template_parameter_lists;
19738 parser->in_function_body = saved_in_function_body;
19739
19740 return fn;
19741 }
19742
19743 /* Parse a template-declaration, assuming that the `export' (and
19744 `extern') keywords, if present, has already been scanned. MEMBER_P
19745 is as for cp_parser_template_declaration. */
19746
19747 static void
19748 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19749 {
19750 tree decl = NULL_TREE;
19751 VEC (deferred_access_check,gc) *checks;
19752 tree parameter_list;
19753 bool friend_p = false;
19754 bool need_lang_pop;
19755 cp_token *token;
19756
19757 /* Look for the `template' keyword. */
19758 token = cp_lexer_peek_token (parser->lexer);
19759 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19760 return;
19761
19762 /* And the `<'. */
19763 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19764 return;
19765 if (at_class_scope_p () && current_function_decl)
19766 {
19767 /* 14.5.2.2 [temp.mem]
19768
19769 A local class shall not have member templates. */
19770 error_at (token->location,
19771 "invalid declaration of member template in local class");
19772 cp_parser_skip_to_end_of_block_or_statement (parser);
19773 return;
19774 }
19775 /* [temp]
19776
19777 A template ... shall not have C linkage. */
19778 if (current_lang_name == lang_name_c)
19779 {
19780 error_at (token->location, "template with C linkage");
19781 /* Give it C++ linkage to avoid confusing other parts of the
19782 front end. */
19783 push_lang_context (lang_name_cplusplus);
19784 need_lang_pop = true;
19785 }
19786 else
19787 need_lang_pop = false;
19788
19789 /* We cannot perform access checks on the template parameter
19790 declarations until we know what is being declared, just as we
19791 cannot check the decl-specifier list. */
19792 push_deferring_access_checks (dk_deferred);
19793
19794 /* If the next token is `>', then we have an invalid
19795 specialization. Rather than complain about an invalid template
19796 parameter, issue an error message here. */
19797 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19798 {
19799 cp_parser_error (parser, "invalid explicit specialization");
19800 begin_specialization ();
19801 parameter_list = NULL_TREE;
19802 }
19803 else
19804 {
19805 /* Parse the template parameters. */
19806 parameter_list = cp_parser_template_parameter_list (parser);
19807 fixup_template_parms ();
19808 }
19809
19810 /* Get the deferred access checks from the parameter list. These
19811 will be checked once we know what is being declared, as for a
19812 member template the checks must be performed in the scope of the
19813 class containing the member. */
19814 checks = get_deferred_access_checks ();
19815
19816 /* Look for the `>'. */
19817 cp_parser_skip_to_end_of_template_parameter_list (parser);
19818 /* We just processed one more parameter list. */
19819 ++parser->num_template_parameter_lists;
19820 /* If the next token is `template', there are more template
19821 parameters. */
19822 if (cp_lexer_next_token_is_keyword (parser->lexer,
19823 RID_TEMPLATE))
19824 cp_parser_template_declaration_after_export (parser, member_p);
19825 else
19826 {
19827 /* There are no access checks when parsing a template, as we do not
19828 know if a specialization will be a friend. */
19829 push_deferring_access_checks (dk_no_check);
19830 token = cp_lexer_peek_token (parser->lexer);
19831 decl = cp_parser_single_declaration (parser,
19832 checks,
19833 member_p,
19834 /*explicit_specialization_p=*/false,
19835 &friend_p);
19836 pop_deferring_access_checks ();
19837
19838 /* If this is a member template declaration, let the front
19839 end know. */
19840 if (member_p && !friend_p && decl)
19841 {
19842 if (TREE_CODE (decl) == TYPE_DECL)
19843 cp_parser_check_access_in_redeclaration (decl, token->location);
19844
19845 decl = finish_member_template_decl (decl);
19846 }
19847 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19848 make_friend_class (current_class_type, TREE_TYPE (decl),
19849 /*complain=*/true);
19850 }
19851 /* We are done with the current parameter list. */
19852 --parser->num_template_parameter_lists;
19853
19854 pop_deferring_access_checks ();
19855
19856 /* Finish up. */
19857 finish_template_decl (parameter_list);
19858
19859 /* Register member declarations. */
19860 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19861 finish_member_declaration (decl);
19862 /* For the erroneous case of a template with C linkage, we pushed an
19863 implicit C++ linkage scope; exit that scope now. */
19864 if (need_lang_pop)
19865 pop_lang_context ();
19866 /* If DECL is a function template, we must return to parse it later.
19867 (Even though there is no definition, there might be default
19868 arguments that need handling.) */
19869 if (member_p && decl
19870 && (TREE_CODE (decl) == FUNCTION_DECL
19871 || DECL_FUNCTION_TEMPLATE_P (decl)))
19872 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19873 }
19874
19875 /* Perform the deferred access checks from a template-parameter-list.
19876 CHECKS is a TREE_LIST of access checks, as returned by
19877 get_deferred_access_checks. */
19878
19879 static void
19880 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19881 {
19882 ++processing_template_parmlist;
19883 perform_access_checks (checks);
19884 --processing_template_parmlist;
19885 }
19886
19887 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19888 `function-definition' sequence. MEMBER_P is true, this declaration
19889 appears in a class scope.
19890
19891 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19892 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19893
19894 static tree
19895 cp_parser_single_declaration (cp_parser* parser,
19896 VEC (deferred_access_check,gc)* checks,
19897 bool member_p,
19898 bool explicit_specialization_p,
19899 bool* friend_p)
19900 {
19901 int declares_class_or_enum;
19902 tree decl = NULL_TREE;
19903 cp_decl_specifier_seq decl_specifiers;
19904 bool function_definition_p = false;
19905 cp_token *decl_spec_token_start;
19906
19907 /* This function is only used when processing a template
19908 declaration. */
19909 gcc_assert (innermost_scope_kind () == sk_template_parms
19910 || innermost_scope_kind () == sk_template_spec);
19911
19912 /* Defer access checks until we know what is being declared. */
19913 push_deferring_access_checks (dk_deferred);
19914
19915 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19916 alternative. */
19917 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19918 cp_parser_decl_specifier_seq (parser,
19919 CP_PARSER_FLAGS_OPTIONAL,
19920 &decl_specifiers,
19921 &declares_class_or_enum);
19922 if (friend_p)
19923 *friend_p = cp_parser_friend_p (&decl_specifiers);
19924
19925 /* There are no template typedefs. */
19926 if (decl_specifiers.specs[(int) ds_typedef])
19927 {
19928 error_at (decl_spec_token_start->location,
19929 "template declaration of %<typedef%>");
19930 decl = error_mark_node;
19931 }
19932
19933 /* Gather up the access checks that occurred the
19934 decl-specifier-seq. */
19935 stop_deferring_access_checks ();
19936
19937 /* Check for the declaration of a template class. */
19938 if (declares_class_or_enum)
19939 {
19940 if (cp_parser_declares_only_class_p (parser))
19941 {
19942 decl = shadow_tag (&decl_specifiers);
19943
19944 /* In this case:
19945
19946 struct C {
19947 friend template <typename T> struct A<T>::B;
19948 };
19949
19950 A<T>::B will be represented by a TYPENAME_TYPE, and
19951 therefore not recognized by shadow_tag. */
19952 if (friend_p && *friend_p
19953 && !decl
19954 && decl_specifiers.type
19955 && TYPE_P (decl_specifiers.type))
19956 decl = decl_specifiers.type;
19957
19958 if (decl && decl != error_mark_node)
19959 decl = TYPE_NAME (decl);
19960 else
19961 decl = error_mark_node;
19962
19963 /* Perform access checks for template parameters. */
19964 cp_parser_perform_template_parameter_access_checks (checks);
19965 }
19966 }
19967
19968 /* Complain about missing 'typename' or other invalid type names. */
19969 if (!decl_specifiers.any_type_specifiers_p
19970 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19971 {
19972 /* cp_parser_parse_and_diagnose_invalid_type_name calls
19973 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
19974 the rest of this declaration. */
19975 decl = error_mark_node;
19976 goto out;
19977 }
19978
19979 /* If it's not a template class, try for a template function. If
19980 the next token is a `;', then this declaration does not declare
19981 anything. But, if there were errors in the decl-specifiers, then
19982 the error might well have come from an attempted class-specifier.
19983 In that case, there's no need to warn about a missing declarator. */
19984 if (!decl
19985 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19986 || decl_specifiers.type != error_mark_node))
19987 {
19988 decl = cp_parser_init_declarator (parser,
19989 &decl_specifiers,
19990 checks,
19991 /*function_definition_allowed_p=*/true,
19992 member_p,
19993 declares_class_or_enum,
19994 &function_definition_p,
19995 NULL);
19996
19997 /* 7.1.1-1 [dcl.stc]
19998
19999 A storage-class-specifier shall not be specified in an explicit
20000 specialization... */
20001 if (decl
20002 && explicit_specialization_p
20003 && decl_specifiers.storage_class != sc_none)
20004 {
20005 error_at (decl_spec_token_start->location,
20006 "explicit template specialization cannot have a storage class");
20007 decl = error_mark_node;
20008 }
20009 }
20010
20011 /* Look for a trailing `;' after the declaration. */
20012 if (!function_definition_p
20013 && (decl == error_mark_node
20014 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20015 cp_parser_skip_to_end_of_block_or_statement (parser);
20016
20017 out:
20018 pop_deferring_access_checks ();
20019
20020 /* Clear any current qualification; whatever comes next is the start
20021 of something new. */
20022 parser->scope = NULL_TREE;
20023 parser->qualifying_scope = NULL_TREE;
20024 parser->object_scope = NULL_TREE;
20025
20026 return decl;
20027 }
20028
20029 /* Parse a cast-expression that is not the operand of a unary "&". */
20030
20031 static tree
20032 cp_parser_simple_cast_expression (cp_parser *parser)
20033 {
20034 return cp_parser_cast_expression (parser, /*address_p=*/false,
20035 /*cast_p=*/false, NULL);
20036 }
20037
20038 /* Parse a functional cast to TYPE. Returns an expression
20039 representing the cast. */
20040
20041 static tree
20042 cp_parser_functional_cast (cp_parser* parser, tree type)
20043 {
20044 VEC(tree,gc) *vec;
20045 tree expression_list;
20046 tree cast;
20047 bool nonconst_p;
20048
20049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20050 {
20051 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20052 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20053 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20054 if (TREE_CODE (type) == TYPE_DECL)
20055 type = TREE_TYPE (type);
20056 return finish_compound_literal (type, expression_list,
20057 tf_warning_or_error);
20058 }
20059
20060
20061 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20062 /*cast_p=*/true,
20063 /*allow_expansion_p=*/true,
20064 /*non_constant_p=*/NULL);
20065 if (vec == NULL)
20066 expression_list = error_mark_node;
20067 else
20068 {
20069 expression_list = build_tree_list_vec (vec);
20070 release_tree_vector (vec);
20071 }
20072
20073 cast = build_functional_cast (type, expression_list,
20074 tf_warning_or_error);
20075 /* [expr.const]/1: In an integral constant expression "only type
20076 conversions to integral or enumeration type can be used". */
20077 if (TREE_CODE (type) == TYPE_DECL)
20078 type = TREE_TYPE (type);
20079 if (cast != error_mark_node
20080 && !cast_valid_in_integral_constant_expression_p (type)
20081 && cp_parser_non_integral_constant_expression (parser,
20082 NIC_CONSTRUCTOR))
20083 return error_mark_node;
20084 return cast;
20085 }
20086
20087 /* Save the tokens that make up the body of a member function defined
20088 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20089 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20090 specifiers applied to the declaration. Returns the FUNCTION_DECL
20091 for the member function. */
20092
20093 static tree
20094 cp_parser_save_member_function_body (cp_parser* parser,
20095 cp_decl_specifier_seq *decl_specifiers,
20096 cp_declarator *declarator,
20097 tree attributes)
20098 {
20099 cp_token *first;
20100 cp_token *last;
20101 tree fn;
20102
20103 /* Create the FUNCTION_DECL. */
20104 fn = grokmethod (decl_specifiers, declarator, attributes);
20105 /* If something went badly wrong, bail out now. */
20106 if (fn == error_mark_node)
20107 {
20108 /* If there's a function-body, skip it. */
20109 if (cp_parser_token_starts_function_definition_p
20110 (cp_lexer_peek_token (parser->lexer)))
20111 cp_parser_skip_to_end_of_block_or_statement (parser);
20112 return error_mark_node;
20113 }
20114
20115 /* Remember it, if there default args to post process. */
20116 cp_parser_save_default_args (parser, fn);
20117
20118 /* Save away the tokens that make up the body of the
20119 function. */
20120 first = parser->lexer->next_token;
20121 /* We can have braced-init-list mem-initializers before the fn body. */
20122 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20123 {
20124 cp_lexer_consume_token (parser->lexer);
20125 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20126 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20127 {
20128 /* cache_group will stop after an un-nested { } pair, too. */
20129 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20130 break;
20131
20132 /* variadic mem-inits have ... after the ')'. */
20133 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20134 cp_lexer_consume_token (parser->lexer);
20135 }
20136 }
20137 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20138 /* Handle function try blocks. */
20139 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20140 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20141 last = parser->lexer->next_token;
20142
20143 /* Save away the inline definition; we will process it when the
20144 class is complete. */
20145 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20146 DECL_PENDING_INLINE_P (fn) = 1;
20147
20148 /* We need to know that this was defined in the class, so that
20149 friend templates are handled correctly. */
20150 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20151
20152 /* Add FN to the queue of functions to be parsed later. */
20153 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20154
20155 return fn;
20156 }
20157
20158 /* Parse a template-argument-list, as well as the trailing ">" (but
20159 not the opening ">"). See cp_parser_template_argument_list for the
20160 return value. */
20161
20162 static tree
20163 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20164 {
20165 tree arguments;
20166 tree saved_scope;
20167 tree saved_qualifying_scope;
20168 tree saved_object_scope;
20169 bool saved_greater_than_is_operator_p;
20170 int saved_unevaluated_operand;
20171 int saved_inhibit_evaluation_warnings;
20172
20173 /* [temp.names]
20174
20175 When parsing a template-id, the first non-nested `>' is taken as
20176 the end of the template-argument-list rather than a greater-than
20177 operator. */
20178 saved_greater_than_is_operator_p
20179 = parser->greater_than_is_operator_p;
20180 parser->greater_than_is_operator_p = false;
20181 /* Parsing the argument list may modify SCOPE, so we save it
20182 here. */
20183 saved_scope = parser->scope;
20184 saved_qualifying_scope = parser->qualifying_scope;
20185 saved_object_scope = parser->object_scope;
20186 /* We need to evaluate the template arguments, even though this
20187 template-id may be nested within a "sizeof". */
20188 saved_unevaluated_operand = cp_unevaluated_operand;
20189 cp_unevaluated_operand = 0;
20190 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20191 c_inhibit_evaluation_warnings = 0;
20192 /* Parse the template-argument-list itself. */
20193 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20194 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20195 arguments = NULL_TREE;
20196 else
20197 arguments = cp_parser_template_argument_list (parser);
20198 /* Look for the `>' that ends the template-argument-list. If we find
20199 a '>>' instead, it's probably just a typo. */
20200 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20201 {
20202 if (cxx_dialect != cxx98)
20203 {
20204 /* In C++0x, a `>>' in a template argument list or cast
20205 expression is considered to be two separate `>'
20206 tokens. So, change the current token to a `>', but don't
20207 consume it: it will be consumed later when the outer
20208 template argument list (or cast expression) is parsed.
20209 Note that this replacement of `>' for `>>' is necessary
20210 even if we are parsing tentatively: in the tentative
20211 case, after calling
20212 cp_parser_enclosed_template_argument_list we will always
20213 throw away all of the template arguments and the first
20214 closing `>', either because the template argument list
20215 was erroneous or because we are replacing those tokens
20216 with a CPP_TEMPLATE_ID token. The second `>' (which will
20217 not have been thrown away) is needed either to close an
20218 outer template argument list or to complete a new-style
20219 cast. */
20220 cp_token *token = cp_lexer_peek_token (parser->lexer);
20221 token->type = CPP_GREATER;
20222 }
20223 else if (!saved_greater_than_is_operator_p)
20224 {
20225 /* If we're in a nested template argument list, the '>>' has
20226 to be a typo for '> >'. We emit the error message, but we
20227 continue parsing and we push a '>' as next token, so that
20228 the argument list will be parsed correctly. Note that the
20229 global source location is still on the token before the
20230 '>>', so we need to say explicitly where we want it. */
20231 cp_token *token = cp_lexer_peek_token (parser->lexer);
20232 error_at (token->location, "%<>>%> should be %<> >%> "
20233 "within a nested template argument list");
20234
20235 token->type = CPP_GREATER;
20236 }
20237 else
20238 {
20239 /* If this is not a nested template argument list, the '>>'
20240 is a typo for '>'. Emit an error message and continue.
20241 Same deal about the token location, but here we can get it
20242 right by consuming the '>>' before issuing the diagnostic. */
20243 cp_token *token = cp_lexer_consume_token (parser->lexer);
20244 error_at (token->location,
20245 "spurious %<>>%>, use %<>%> to terminate "
20246 "a template argument list");
20247 }
20248 }
20249 else
20250 cp_parser_skip_to_end_of_template_parameter_list (parser);
20251 /* The `>' token might be a greater-than operator again now. */
20252 parser->greater_than_is_operator_p
20253 = saved_greater_than_is_operator_p;
20254 /* Restore the SAVED_SCOPE. */
20255 parser->scope = saved_scope;
20256 parser->qualifying_scope = saved_qualifying_scope;
20257 parser->object_scope = saved_object_scope;
20258 cp_unevaluated_operand = saved_unevaluated_operand;
20259 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20260
20261 return arguments;
20262 }
20263
20264 /* MEMBER_FUNCTION is a member function, or a friend. If default
20265 arguments, or the body of the function have not yet been parsed,
20266 parse them now. */
20267
20268 static void
20269 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20270 {
20271 /* If this member is a template, get the underlying
20272 FUNCTION_DECL. */
20273 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20274 member_function = DECL_TEMPLATE_RESULT (member_function);
20275
20276 /* There should not be any class definitions in progress at this
20277 point; the bodies of members are only parsed outside of all class
20278 definitions. */
20279 gcc_assert (parser->num_classes_being_defined == 0);
20280 /* While we're parsing the member functions we might encounter more
20281 classes. We want to handle them right away, but we don't want
20282 them getting mixed up with functions that are currently in the
20283 queue. */
20284 push_unparsed_function_queues (parser);
20285
20286 /* Make sure that any template parameters are in scope. */
20287 maybe_begin_member_template_processing (member_function);
20288
20289 /* If the body of the function has not yet been parsed, parse it
20290 now. */
20291 if (DECL_PENDING_INLINE_P (member_function))
20292 {
20293 tree function_scope;
20294 cp_token_cache *tokens;
20295
20296 /* The function is no longer pending; we are processing it. */
20297 tokens = DECL_PENDING_INLINE_INFO (member_function);
20298 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20299 DECL_PENDING_INLINE_P (member_function) = 0;
20300
20301 /* If this is a local class, enter the scope of the containing
20302 function. */
20303 function_scope = current_function_decl;
20304 if (function_scope)
20305 push_function_context ();
20306
20307 /* Push the body of the function onto the lexer stack. */
20308 cp_parser_push_lexer_for_tokens (parser, tokens);
20309
20310 /* Let the front end know that we going to be defining this
20311 function. */
20312 start_preparsed_function (member_function, NULL_TREE,
20313 SF_PRE_PARSED | SF_INCLASS_INLINE);
20314
20315 /* Don't do access checking if it is a templated function. */
20316 if (processing_template_decl)
20317 push_deferring_access_checks (dk_no_check);
20318
20319 /* Now, parse the body of the function. */
20320 cp_parser_function_definition_after_declarator (parser,
20321 /*inline_p=*/true);
20322
20323 if (processing_template_decl)
20324 pop_deferring_access_checks ();
20325
20326 /* Leave the scope of the containing function. */
20327 if (function_scope)
20328 pop_function_context ();
20329 cp_parser_pop_lexer (parser);
20330 }
20331
20332 /* Remove any template parameters from the symbol table. */
20333 maybe_end_member_template_processing ();
20334
20335 /* Restore the queue. */
20336 pop_unparsed_function_queues (parser);
20337 }
20338
20339 /* If DECL contains any default args, remember it on the unparsed
20340 functions queue. */
20341
20342 static void
20343 cp_parser_save_default_args (cp_parser* parser, tree decl)
20344 {
20345 tree probe;
20346
20347 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20348 probe;
20349 probe = TREE_CHAIN (probe))
20350 if (TREE_PURPOSE (probe))
20351 {
20352 cp_default_arg_entry *entry
20353 = VEC_safe_push (cp_default_arg_entry, gc,
20354 unparsed_funs_with_default_args, NULL);
20355 entry->class_type = current_class_type;
20356 entry->decl = decl;
20357 break;
20358 }
20359 }
20360
20361 /* FN is a FUNCTION_DECL which may contains a parameter with an
20362 unparsed DEFAULT_ARG. Parse the default args now. This function
20363 assumes that the current scope is the scope in which the default
20364 argument should be processed. */
20365
20366 static void
20367 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20368 {
20369 bool saved_local_variables_forbidden_p;
20370 tree parm, parmdecl;
20371
20372 /* While we're parsing the default args, we might (due to the
20373 statement expression extension) encounter more classes. We want
20374 to handle them right away, but we don't want them getting mixed
20375 up with default args that are currently in the queue. */
20376 push_unparsed_function_queues (parser);
20377
20378 /* Local variable names (and the `this' keyword) may not appear
20379 in a default argument. */
20380 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20381 parser->local_variables_forbidden_p = true;
20382
20383 push_defarg_context (fn);
20384
20385 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20386 parmdecl = DECL_ARGUMENTS (fn);
20387 parm && parm != void_list_node;
20388 parm = TREE_CHAIN (parm),
20389 parmdecl = DECL_CHAIN (parmdecl))
20390 {
20391 cp_token_cache *tokens;
20392 tree default_arg = TREE_PURPOSE (parm);
20393 tree parsed_arg;
20394 VEC(tree,gc) *insts;
20395 tree copy;
20396 unsigned ix;
20397
20398 if (!default_arg)
20399 continue;
20400
20401 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20402 /* This can happen for a friend declaration for a function
20403 already declared with default arguments. */
20404 continue;
20405
20406 /* Push the saved tokens for the default argument onto the parser's
20407 lexer stack. */
20408 tokens = DEFARG_TOKENS (default_arg);
20409 cp_parser_push_lexer_for_tokens (parser, tokens);
20410
20411 start_lambda_scope (parmdecl);
20412
20413 /* Parse the assignment-expression. */
20414 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20415 if (parsed_arg == error_mark_node)
20416 {
20417 cp_parser_pop_lexer (parser);
20418 continue;
20419 }
20420
20421 if (!processing_template_decl)
20422 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20423
20424 TREE_PURPOSE (parm) = parsed_arg;
20425
20426 /* Update any instantiations we've already created. */
20427 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20428 VEC_iterate (tree, insts, ix, copy); ix++)
20429 TREE_PURPOSE (copy) = parsed_arg;
20430
20431 finish_lambda_scope ();
20432
20433 /* If the token stream has not been completely used up, then
20434 there was extra junk after the end of the default
20435 argument. */
20436 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20437 cp_parser_error (parser, "expected %<,%>");
20438
20439 /* Revert to the main lexer. */
20440 cp_parser_pop_lexer (parser);
20441 }
20442
20443 pop_defarg_context ();
20444
20445 /* Make sure no default arg is missing. */
20446 check_default_args (fn);
20447
20448 /* Restore the state of local_variables_forbidden_p. */
20449 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20450
20451 /* Restore the queue. */
20452 pop_unparsed_function_queues (parser);
20453 }
20454
20455 /* Parse the operand of `sizeof' (or a similar operator). Returns
20456 either a TYPE or an expression, depending on the form of the
20457 input. The KEYWORD indicates which kind of expression we have
20458 encountered. */
20459
20460 static tree
20461 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20462 {
20463 tree expr = NULL_TREE;
20464 const char *saved_message;
20465 char *tmp;
20466 bool saved_integral_constant_expression_p;
20467 bool saved_non_integral_constant_expression_p;
20468 bool pack_expansion_p = false;
20469
20470 /* Types cannot be defined in a `sizeof' expression. Save away the
20471 old message. */
20472 saved_message = parser->type_definition_forbidden_message;
20473 /* And create the new one. */
20474 tmp = concat ("types may not be defined in %<",
20475 IDENTIFIER_POINTER (ridpointers[keyword]),
20476 "%> expressions", NULL);
20477 parser->type_definition_forbidden_message = tmp;
20478
20479 /* The restrictions on constant-expressions do not apply inside
20480 sizeof expressions. */
20481 saved_integral_constant_expression_p
20482 = parser->integral_constant_expression_p;
20483 saved_non_integral_constant_expression_p
20484 = parser->non_integral_constant_expression_p;
20485 parser->integral_constant_expression_p = false;
20486
20487 /* If it's a `...', then we are computing the length of a parameter
20488 pack. */
20489 if (keyword == RID_SIZEOF
20490 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20491 {
20492 /* Consume the `...'. */
20493 cp_lexer_consume_token (parser->lexer);
20494 maybe_warn_variadic_templates ();
20495
20496 /* Note that this is an expansion. */
20497 pack_expansion_p = true;
20498 }
20499
20500 /* Do not actually evaluate the expression. */
20501 ++cp_unevaluated_operand;
20502 ++c_inhibit_evaluation_warnings;
20503 /* If it's a `(', then we might be looking at the type-id
20504 construction. */
20505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20506 {
20507 tree type;
20508 bool saved_in_type_id_in_expr_p;
20509
20510 /* We can't be sure yet whether we're looking at a type-id or an
20511 expression. */
20512 cp_parser_parse_tentatively (parser);
20513 /* Consume the `('. */
20514 cp_lexer_consume_token (parser->lexer);
20515 /* Parse the type-id. */
20516 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20517 parser->in_type_id_in_expr_p = true;
20518 type = cp_parser_type_id (parser);
20519 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20520 /* Now, look for the trailing `)'. */
20521 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20522 /* If all went well, then we're done. */
20523 if (cp_parser_parse_definitely (parser))
20524 {
20525 cp_decl_specifier_seq decl_specs;
20526
20527 /* Build a trivial decl-specifier-seq. */
20528 clear_decl_specs (&decl_specs);
20529 decl_specs.type = type;
20530
20531 /* Call grokdeclarator to figure out what type this is. */
20532 expr = grokdeclarator (NULL,
20533 &decl_specs,
20534 TYPENAME,
20535 /*initialized=*/0,
20536 /*attrlist=*/NULL);
20537 }
20538 }
20539
20540 /* If the type-id production did not work out, then we must be
20541 looking at the unary-expression production. */
20542 if (!expr)
20543 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20544 /*cast_p=*/false, NULL);
20545
20546 if (pack_expansion_p)
20547 /* Build a pack expansion. */
20548 expr = make_pack_expansion (expr);
20549
20550 /* Go back to evaluating expressions. */
20551 --cp_unevaluated_operand;
20552 --c_inhibit_evaluation_warnings;
20553
20554 /* Free the message we created. */
20555 free (tmp);
20556 /* And restore the old one. */
20557 parser->type_definition_forbidden_message = saved_message;
20558 parser->integral_constant_expression_p
20559 = saved_integral_constant_expression_p;
20560 parser->non_integral_constant_expression_p
20561 = saved_non_integral_constant_expression_p;
20562
20563 return expr;
20564 }
20565
20566 /* If the current declaration has no declarator, return true. */
20567
20568 static bool
20569 cp_parser_declares_only_class_p (cp_parser *parser)
20570 {
20571 /* If the next token is a `;' or a `,' then there is no
20572 declarator. */
20573 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20574 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20575 }
20576
20577 /* Update the DECL_SPECS to reflect the storage class indicated by
20578 KEYWORD. */
20579
20580 static void
20581 cp_parser_set_storage_class (cp_parser *parser,
20582 cp_decl_specifier_seq *decl_specs,
20583 enum rid keyword,
20584 location_t location)
20585 {
20586 cp_storage_class storage_class;
20587
20588 if (parser->in_unbraced_linkage_specification_p)
20589 {
20590 error_at (location, "invalid use of %qD in linkage specification",
20591 ridpointers[keyword]);
20592 return;
20593 }
20594 else if (decl_specs->storage_class != sc_none)
20595 {
20596 decl_specs->conflicting_specifiers_p = true;
20597 return;
20598 }
20599
20600 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20601 && decl_specs->specs[(int) ds_thread])
20602 {
20603 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20604 decl_specs->specs[(int) ds_thread] = 0;
20605 }
20606
20607 switch (keyword)
20608 {
20609 case RID_AUTO:
20610 storage_class = sc_auto;
20611 break;
20612 case RID_REGISTER:
20613 storage_class = sc_register;
20614 break;
20615 case RID_STATIC:
20616 storage_class = sc_static;
20617 break;
20618 case RID_EXTERN:
20619 storage_class = sc_extern;
20620 break;
20621 case RID_MUTABLE:
20622 storage_class = sc_mutable;
20623 break;
20624 default:
20625 gcc_unreachable ();
20626 }
20627 decl_specs->storage_class = storage_class;
20628
20629 /* A storage class specifier cannot be applied alongside a typedef
20630 specifier. If there is a typedef specifier present then set
20631 conflicting_specifiers_p which will trigger an error later
20632 on in grokdeclarator. */
20633 if (decl_specs->specs[(int)ds_typedef])
20634 decl_specs->conflicting_specifiers_p = true;
20635 }
20636
20637 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20638 is true, the type is a user-defined type; otherwise it is a
20639 built-in type specified by a keyword. */
20640
20641 static void
20642 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20643 tree type_spec,
20644 location_t location,
20645 bool user_defined_p)
20646 {
20647 decl_specs->any_specifiers_p = true;
20648
20649 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20650 (with, for example, in "typedef int wchar_t;") we remember that
20651 this is what happened. In system headers, we ignore these
20652 declarations so that G++ can work with system headers that are not
20653 C++-safe. */
20654 if (decl_specs->specs[(int) ds_typedef]
20655 && !user_defined_p
20656 && (type_spec == boolean_type_node
20657 || type_spec == char16_type_node
20658 || type_spec == char32_type_node
20659 || type_spec == wchar_type_node)
20660 && (decl_specs->type
20661 || decl_specs->specs[(int) ds_long]
20662 || decl_specs->specs[(int) ds_short]
20663 || decl_specs->specs[(int) ds_unsigned]
20664 || decl_specs->specs[(int) ds_signed]))
20665 {
20666 decl_specs->redefined_builtin_type = type_spec;
20667 if (!decl_specs->type)
20668 {
20669 decl_specs->type = type_spec;
20670 decl_specs->user_defined_type_p = false;
20671 decl_specs->type_location = location;
20672 }
20673 }
20674 else if (decl_specs->type)
20675 decl_specs->multiple_types_p = true;
20676 else
20677 {
20678 decl_specs->type = type_spec;
20679 decl_specs->user_defined_type_p = user_defined_p;
20680 decl_specs->redefined_builtin_type = NULL_TREE;
20681 decl_specs->type_location = location;
20682 }
20683 }
20684
20685 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20686 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20687
20688 static bool
20689 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20690 {
20691 return decl_specifiers->specs[(int) ds_friend] != 0;
20692 }
20693
20694 /* Issue an error message indicating that TOKEN_DESC was expected.
20695 If KEYWORD is true, it indicated this function is called by
20696 cp_parser_require_keword and the required token can only be
20697 a indicated keyword. */
20698
20699 static void
20700 cp_parser_required_error (cp_parser *parser,
20701 required_token token_desc,
20702 bool keyword)
20703 {
20704 switch (token_desc)
20705 {
20706 case RT_NEW:
20707 cp_parser_error (parser, "expected %<new%>");
20708 return;
20709 case RT_DELETE:
20710 cp_parser_error (parser, "expected %<delete%>");
20711 return;
20712 case RT_RETURN:
20713 cp_parser_error (parser, "expected %<return%>");
20714 return;
20715 case RT_WHILE:
20716 cp_parser_error (parser, "expected %<while%>");
20717 return;
20718 case RT_EXTERN:
20719 cp_parser_error (parser, "expected %<extern%>");
20720 return;
20721 case RT_STATIC_ASSERT:
20722 cp_parser_error (parser, "expected %<static_assert%>");
20723 return;
20724 case RT_DECLTYPE:
20725 cp_parser_error (parser, "expected %<decltype%>");
20726 return;
20727 case RT_OPERATOR:
20728 cp_parser_error (parser, "expected %<operator%>");
20729 return;
20730 case RT_CLASS:
20731 cp_parser_error (parser, "expected %<class%>");
20732 return;
20733 case RT_TEMPLATE:
20734 cp_parser_error (parser, "expected %<template%>");
20735 return;
20736 case RT_NAMESPACE:
20737 cp_parser_error (parser, "expected %<namespace%>");
20738 return;
20739 case RT_USING:
20740 cp_parser_error (parser, "expected %<using%>");
20741 return;
20742 case RT_ASM:
20743 cp_parser_error (parser, "expected %<asm%>");
20744 return;
20745 case RT_TRY:
20746 cp_parser_error (parser, "expected %<try%>");
20747 return;
20748 case RT_CATCH:
20749 cp_parser_error (parser, "expected %<catch%>");
20750 return;
20751 case RT_THROW:
20752 cp_parser_error (parser, "expected %<throw%>");
20753 return;
20754 case RT_LABEL:
20755 cp_parser_error (parser, "expected %<__label__%>");
20756 return;
20757 case RT_AT_TRY:
20758 cp_parser_error (parser, "expected %<@try%>");
20759 return;
20760 case RT_AT_SYNCHRONIZED:
20761 cp_parser_error (parser, "expected %<@synchronized%>");
20762 return;
20763 case RT_AT_THROW:
20764 cp_parser_error (parser, "expected %<@throw%>");
20765 return;
20766 default:
20767 break;
20768 }
20769 if (!keyword)
20770 {
20771 switch (token_desc)
20772 {
20773 case RT_SEMICOLON:
20774 cp_parser_error (parser, "expected %<;%>");
20775 return;
20776 case RT_OPEN_PAREN:
20777 cp_parser_error (parser, "expected %<(%>");
20778 return;
20779 case RT_CLOSE_BRACE:
20780 cp_parser_error (parser, "expected %<}%>");
20781 return;
20782 case RT_OPEN_BRACE:
20783 cp_parser_error (parser, "expected %<{%>");
20784 return;
20785 case RT_CLOSE_SQUARE:
20786 cp_parser_error (parser, "expected %<]%>");
20787 return;
20788 case RT_OPEN_SQUARE:
20789 cp_parser_error (parser, "expected %<[%>");
20790 return;
20791 case RT_COMMA:
20792 cp_parser_error (parser, "expected %<,%>");
20793 return;
20794 case RT_SCOPE:
20795 cp_parser_error (parser, "expected %<::%>");
20796 return;
20797 case RT_LESS:
20798 cp_parser_error (parser, "expected %<<%>");
20799 return;
20800 case RT_GREATER:
20801 cp_parser_error (parser, "expected %<>%>");
20802 return;
20803 case RT_EQ:
20804 cp_parser_error (parser, "expected %<=%>");
20805 return;
20806 case RT_ELLIPSIS:
20807 cp_parser_error (parser, "expected %<...%>");
20808 return;
20809 case RT_MULT:
20810 cp_parser_error (parser, "expected %<*%>");
20811 return;
20812 case RT_COMPL:
20813 cp_parser_error (parser, "expected %<~%>");
20814 return;
20815 case RT_COLON:
20816 cp_parser_error (parser, "expected %<:%>");
20817 return;
20818 case RT_COLON_SCOPE:
20819 cp_parser_error (parser, "expected %<:%> or %<::%>");
20820 return;
20821 case RT_CLOSE_PAREN:
20822 cp_parser_error (parser, "expected %<)%>");
20823 return;
20824 case RT_COMMA_CLOSE_PAREN:
20825 cp_parser_error (parser, "expected %<,%> or %<)%>");
20826 return;
20827 case RT_PRAGMA_EOL:
20828 cp_parser_error (parser, "expected end of line");
20829 return;
20830 case RT_NAME:
20831 cp_parser_error (parser, "expected identifier");
20832 return;
20833 case RT_SELECT:
20834 cp_parser_error (parser, "expected selection-statement");
20835 return;
20836 case RT_INTERATION:
20837 cp_parser_error (parser, "expected iteration-statement");
20838 return;
20839 case RT_JUMP:
20840 cp_parser_error (parser, "expected jump-statement");
20841 return;
20842 case RT_CLASS_KEY:
20843 cp_parser_error (parser, "expected class-key");
20844 return;
20845 case RT_CLASS_TYPENAME_TEMPLATE:
20846 cp_parser_error (parser,
20847 "expected %<class%>, %<typename%>, or %<template%>");
20848 return;
20849 default:
20850 gcc_unreachable ();
20851 }
20852 }
20853 else
20854 gcc_unreachable ();
20855 }
20856
20857
20858
20859 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20860 issue an error message indicating that TOKEN_DESC was expected.
20861
20862 Returns the token consumed, if the token had the appropriate type.
20863 Otherwise, returns NULL. */
20864
20865 static cp_token *
20866 cp_parser_require (cp_parser* parser,
20867 enum cpp_ttype type,
20868 required_token token_desc)
20869 {
20870 if (cp_lexer_next_token_is (parser->lexer, type))
20871 return cp_lexer_consume_token (parser->lexer);
20872 else
20873 {
20874 /* Output the MESSAGE -- unless we're parsing tentatively. */
20875 if (!cp_parser_simulate_error (parser))
20876 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20877 return NULL;
20878 }
20879 }
20880
20881 /* An error message is produced if the next token is not '>'.
20882 All further tokens are skipped until the desired token is
20883 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20884
20885 static void
20886 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20887 {
20888 /* Current level of '< ... >'. */
20889 unsigned level = 0;
20890 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20891 unsigned nesting_depth = 0;
20892
20893 /* Are we ready, yet? If not, issue error message. */
20894 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20895 return;
20896
20897 /* Skip tokens until the desired token is found. */
20898 while (true)
20899 {
20900 /* Peek at the next token. */
20901 switch (cp_lexer_peek_token (parser->lexer)->type)
20902 {
20903 case CPP_LESS:
20904 if (!nesting_depth)
20905 ++level;
20906 break;
20907
20908 case CPP_RSHIFT:
20909 if (cxx_dialect == cxx98)
20910 /* C++0x views the `>>' operator as two `>' tokens, but
20911 C++98 does not. */
20912 break;
20913 else if (!nesting_depth && level-- == 0)
20914 {
20915 /* We've hit a `>>' where the first `>' closes the
20916 template argument list, and the second `>' is
20917 spurious. Just consume the `>>' and stop; we've
20918 already produced at least one error. */
20919 cp_lexer_consume_token (parser->lexer);
20920 return;
20921 }
20922 /* Fall through for C++0x, so we handle the second `>' in
20923 the `>>'. */
20924
20925 case CPP_GREATER:
20926 if (!nesting_depth && level-- == 0)
20927 {
20928 /* We've reached the token we want, consume it and stop. */
20929 cp_lexer_consume_token (parser->lexer);
20930 return;
20931 }
20932 break;
20933
20934 case CPP_OPEN_PAREN:
20935 case CPP_OPEN_SQUARE:
20936 ++nesting_depth;
20937 break;
20938
20939 case CPP_CLOSE_PAREN:
20940 case CPP_CLOSE_SQUARE:
20941 if (nesting_depth-- == 0)
20942 return;
20943 break;
20944
20945 case CPP_EOF:
20946 case CPP_PRAGMA_EOL:
20947 case CPP_SEMICOLON:
20948 case CPP_OPEN_BRACE:
20949 case CPP_CLOSE_BRACE:
20950 /* The '>' was probably forgotten, don't look further. */
20951 return;
20952
20953 default:
20954 break;
20955 }
20956
20957 /* Consume this token. */
20958 cp_lexer_consume_token (parser->lexer);
20959 }
20960 }
20961
20962 /* If the next token is the indicated keyword, consume it. Otherwise,
20963 issue an error message indicating that TOKEN_DESC was expected.
20964
20965 Returns the token consumed, if the token had the appropriate type.
20966 Otherwise, returns NULL. */
20967
20968 static cp_token *
20969 cp_parser_require_keyword (cp_parser* parser,
20970 enum rid keyword,
20971 required_token token_desc)
20972 {
20973 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20974
20975 if (token && token->keyword != keyword)
20976 {
20977 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20978 return NULL;
20979 }
20980
20981 return token;
20982 }
20983
20984 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20985 function-definition. */
20986
20987 static bool
20988 cp_parser_token_starts_function_definition_p (cp_token* token)
20989 {
20990 return (/* An ordinary function-body begins with an `{'. */
20991 token->type == CPP_OPEN_BRACE
20992 /* A ctor-initializer begins with a `:'. */
20993 || token->type == CPP_COLON
20994 /* A function-try-block begins with `try'. */
20995 || token->keyword == RID_TRY
20996 /* The named return value extension begins with `return'. */
20997 || token->keyword == RID_RETURN);
20998 }
20999
21000 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21001 definition. */
21002
21003 static bool
21004 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21005 {
21006 cp_token *token;
21007
21008 token = cp_lexer_peek_token (parser->lexer);
21009 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21010 }
21011
21012 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21013 C++0x) ending a template-argument. */
21014
21015 static bool
21016 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21017 {
21018 cp_token *token;
21019
21020 token = cp_lexer_peek_token (parser->lexer);
21021 return (token->type == CPP_COMMA
21022 || token->type == CPP_GREATER
21023 || token->type == CPP_ELLIPSIS
21024 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21025 }
21026
21027 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21028 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21029
21030 static bool
21031 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21032 size_t n)
21033 {
21034 cp_token *token;
21035
21036 token = cp_lexer_peek_nth_token (parser->lexer, n);
21037 if (token->type == CPP_LESS)
21038 return true;
21039 /* Check for the sequence `<::' in the original code. It would be lexed as
21040 `[:', where `[' is a digraph, and there is no whitespace before
21041 `:'. */
21042 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21043 {
21044 cp_token *token2;
21045 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21046 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21047 return true;
21048 }
21049 return false;
21050 }
21051
21052 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21053 or none_type otherwise. */
21054
21055 static enum tag_types
21056 cp_parser_token_is_class_key (cp_token* token)
21057 {
21058 switch (token->keyword)
21059 {
21060 case RID_CLASS:
21061 return class_type;
21062 case RID_STRUCT:
21063 return record_type;
21064 case RID_UNION:
21065 return union_type;
21066
21067 default:
21068 return none_type;
21069 }
21070 }
21071
21072 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21073
21074 static void
21075 cp_parser_check_class_key (enum tag_types class_key, tree type)
21076 {
21077 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21078 permerror (input_location, "%qs tag used in naming %q#T",
21079 class_key == union_type ? "union"
21080 : class_key == record_type ? "struct" : "class",
21081 type);
21082 }
21083
21084 /* Issue an error message if DECL is redeclared with different
21085 access than its original declaration [class.access.spec/3].
21086 This applies to nested classes and nested class templates.
21087 [class.mem/1]. */
21088
21089 static void
21090 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21091 {
21092 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21093 return;
21094
21095 if ((TREE_PRIVATE (decl)
21096 != (current_access_specifier == access_private_node))
21097 || (TREE_PROTECTED (decl)
21098 != (current_access_specifier == access_protected_node)))
21099 error_at (location, "%qD redeclared with different access", decl);
21100 }
21101
21102 /* Look for the `template' keyword, as a syntactic disambiguator.
21103 Return TRUE iff it is present, in which case it will be
21104 consumed. */
21105
21106 static bool
21107 cp_parser_optional_template_keyword (cp_parser *parser)
21108 {
21109 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21110 {
21111 /* The `template' keyword can only be used within templates;
21112 outside templates the parser can always figure out what is a
21113 template and what is not. */
21114 if (!processing_template_decl)
21115 {
21116 cp_token *token = cp_lexer_peek_token (parser->lexer);
21117 error_at (token->location,
21118 "%<template%> (as a disambiguator) is only allowed "
21119 "within templates");
21120 /* If this part of the token stream is rescanned, the same
21121 error message would be generated. So, we purge the token
21122 from the stream. */
21123 cp_lexer_purge_token (parser->lexer);
21124 return false;
21125 }
21126 else
21127 {
21128 /* Consume the `template' keyword. */
21129 cp_lexer_consume_token (parser->lexer);
21130 return true;
21131 }
21132 }
21133
21134 return false;
21135 }
21136
21137 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21138 set PARSER->SCOPE, and perform other related actions. */
21139
21140 static void
21141 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21142 {
21143 int i;
21144 struct tree_check *check_value;
21145 deferred_access_check *chk;
21146 VEC (deferred_access_check,gc) *checks;
21147
21148 /* Get the stored value. */
21149 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21150 /* Perform any access checks that were deferred. */
21151 checks = check_value->checks;
21152 if (checks)
21153 {
21154 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21155 perform_or_defer_access_check (chk->binfo,
21156 chk->decl,
21157 chk->diag_decl);
21158 }
21159 /* Set the scope from the stored value. */
21160 parser->scope = check_value->value;
21161 parser->qualifying_scope = check_value->qualifying_scope;
21162 parser->object_scope = NULL_TREE;
21163 }
21164
21165 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21166 encounter the end of a block before what we were looking for. */
21167
21168 static bool
21169 cp_parser_cache_group (cp_parser *parser,
21170 enum cpp_ttype end,
21171 unsigned depth)
21172 {
21173 while (true)
21174 {
21175 cp_token *token = cp_lexer_peek_token (parser->lexer);
21176
21177 /* Abort a parenthesized expression if we encounter a semicolon. */
21178 if ((end == CPP_CLOSE_PAREN || depth == 0)
21179 && token->type == CPP_SEMICOLON)
21180 return true;
21181 /* If we've reached the end of the file, stop. */
21182 if (token->type == CPP_EOF
21183 || (end != CPP_PRAGMA_EOL
21184 && token->type == CPP_PRAGMA_EOL))
21185 return true;
21186 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21187 /* We've hit the end of an enclosing block, so there's been some
21188 kind of syntax error. */
21189 return true;
21190
21191 /* Consume the token. */
21192 cp_lexer_consume_token (parser->lexer);
21193 /* See if it starts a new group. */
21194 if (token->type == CPP_OPEN_BRACE)
21195 {
21196 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21197 /* In theory this should probably check end == '}', but
21198 cp_parser_save_member_function_body needs it to exit
21199 after either '}' or ')' when called with ')'. */
21200 if (depth == 0)
21201 return false;
21202 }
21203 else if (token->type == CPP_OPEN_PAREN)
21204 {
21205 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21206 if (depth == 0 && end == CPP_CLOSE_PAREN)
21207 return false;
21208 }
21209 else if (token->type == CPP_PRAGMA)
21210 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21211 else if (token->type == end)
21212 return false;
21213 }
21214 }
21215
21216 /* Begin parsing tentatively. We always save tokens while parsing
21217 tentatively so that if the tentative parsing fails we can restore the
21218 tokens. */
21219
21220 static void
21221 cp_parser_parse_tentatively (cp_parser* parser)
21222 {
21223 /* Enter a new parsing context. */
21224 parser->context = cp_parser_context_new (parser->context);
21225 /* Begin saving tokens. */
21226 cp_lexer_save_tokens (parser->lexer);
21227 /* In order to avoid repetitive access control error messages,
21228 access checks are queued up until we are no longer parsing
21229 tentatively. */
21230 push_deferring_access_checks (dk_deferred);
21231 }
21232
21233 /* Commit to the currently active tentative parse. */
21234
21235 static void
21236 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21237 {
21238 cp_parser_context *context;
21239 cp_lexer *lexer;
21240
21241 /* Mark all of the levels as committed. */
21242 lexer = parser->lexer;
21243 for (context = parser->context; context->next; context = context->next)
21244 {
21245 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21246 break;
21247 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21248 while (!cp_lexer_saving_tokens (lexer))
21249 lexer = lexer->next;
21250 cp_lexer_commit_tokens (lexer);
21251 }
21252 }
21253
21254 /* Abort the currently active tentative parse. All consumed tokens
21255 will be rolled back, and no diagnostics will be issued. */
21256
21257 static void
21258 cp_parser_abort_tentative_parse (cp_parser* parser)
21259 {
21260 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21261 || errorcount > 0);
21262 cp_parser_simulate_error (parser);
21263 /* Now, pretend that we want to see if the construct was
21264 successfully parsed. */
21265 cp_parser_parse_definitely (parser);
21266 }
21267
21268 /* Stop parsing tentatively. If a parse error has occurred, restore the
21269 token stream. Otherwise, commit to the tokens we have consumed.
21270 Returns true if no error occurred; false otherwise. */
21271
21272 static bool
21273 cp_parser_parse_definitely (cp_parser* parser)
21274 {
21275 bool error_occurred;
21276 cp_parser_context *context;
21277
21278 /* Remember whether or not an error occurred, since we are about to
21279 destroy that information. */
21280 error_occurred = cp_parser_error_occurred (parser);
21281 /* Remove the topmost context from the stack. */
21282 context = parser->context;
21283 parser->context = context->next;
21284 /* If no parse errors occurred, commit to the tentative parse. */
21285 if (!error_occurred)
21286 {
21287 /* Commit to the tokens read tentatively, unless that was
21288 already done. */
21289 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21290 cp_lexer_commit_tokens (parser->lexer);
21291
21292 pop_to_parent_deferring_access_checks ();
21293 }
21294 /* Otherwise, if errors occurred, roll back our state so that things
21295 are just as they were before we began the tentative parse. */
21296 else
21297 {
21298 cp_lexer_rollback_tokens (parser->lexer);
21299 pop_deferring_access_checks ();
21300 }
21301 /* Add the context to the front of the free list. */
21302 context->next = cp_parser_context_free_list;
21303 cp_parser_context_free_list = context;
21304
21305 return !error_occurred;
21306 }
21307
21308 /* Returns true if we are parsing tentatively and are not committed to
21309 this tentative parse. */
21310
21311 static bool
21312 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21313 {
21314 return (cp_parser_parsing_tentatively (parser)
21315 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21316 }
21317
21318 /* Returns nonzero iff an error has occurred during the most recent
21319 tentative parse. */
21320
21321 static bool
21322 cp_parser_error_occurred (cp_parser* parser)
21323 {
21324 return (cp_parser_parsing_tentatively (parser)
21325 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21326 }
21327
21328 /* Returns nonzero if GNU extensions are allowed. */
21329
21330 static bool
21331 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21332 {
21333 return parser->allow_gnu_extensions_p;
21334 }
21335 \f
21336 /* Objective-C++ Productions */
21337
21338
21339 /* Parse an Objective-C expression, which feeds into a primary-expression
21340 above.
21341
21342 objc-expression:
21343 objc-message-expression
21344 objc-string-literal
21345 objc-encode-expression
21346 objc-protocol-expression
21347 objc-selector-expression
21348
21349 Returns a tree representation of the expression. */
21350
21351 static tree
21352 cp_parser_objc_expression (cp_parser* parser)
21353 {
21354 /* Try to figure out what kind of declaration is present. */
21355 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21356
21357 switch (kwd->type)
21358 {
21359 case CPP_OPEN_SQUARE:
21360 return cp_parser_objc_message_expression (parser);
21361
21362 case CPP_OBJC_STRING:
21363 kwd = cp_lexer_consume_token (parser->lexer);
21364 return objc_build_string_object (kwd->u.value);
21365
21366 case CPP_KEYWORD:
21367 switch (kwd->keyword)
21368 {
21369 case RID_AT_ENCODE:
21370 return cp_parser_objc_encode_expression (parser);
21371
21372 case RID_AT_PROTOCOL:
21373 return cp_parser_objc_protocol_expression (parser);
21374
21375 case RID_AT_SELECTOR:
21376 return cp_parser_objc_selector_expression (parser);
21377
21378 default:
21379 break;
21380 }
21381 default:
21382 error_at (kwd->location,
21383 "misplaced %<@%D%> Objective-C++ construct",
21384 kwd->u.value);
21385 cp_parser_skip_to_end_of_block_or_statement (parser);
21386 }
21387
21388 return error_mark_node;
21389 }
21390
21391 /* Parse an Objective-C message expression.
21392
21393 objc-message-expression:
21394 [ objc-message-receiver objc-message-args ]
21395
21396 Returns a representation of an Objective-C message. */
21397
21398 static tree
21399 cp_parser_objc_message_expression (cp_parser* parser)
21400 {
21401 tree receiver, messageargs;
21402
21403 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21404 receiver = cp_parser_objc_message_receiver (parser);
21405 messageargs = cp_parser_objc_message_args (parser);
21406 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21407
21408 return objc_build_message_expr (receiver, messageargs);
21409 }
21410
21411 /* Parse an objc-message-receiver.
21412
21413 objc-message-receiver:
21414 expression
21415 simple-type-specifier
21416
21417 Returns a representation of the type or expression. */
21418
21419 static tree
21420 cp_parser_objc_message_receiver (cp_parser* parser)
21421 {
21422 tree rcv;
21423
21424 /* An Objective-C message receiver may be either (1) a type
21425 or (2) an expression. */
21426 cp_parser_parse_tentatively (parser);
21427 rcv = cp_parser_expression (parser, false, NULL);
21428
21429 if (cp_parser_parse_definitely (parser))
21430 return rcv;
21431
21432 rcv = cp_parser_simple_type_specifier (parser,
21433 /*decl_specs=*/NULL,
21434 CP_PARSER_FLAGS_NONE);
21435
21436 return objc_get_class_reference (rcv);
21437 }
21438
21439 /* Parse the arguments and selectors comprising an Objective-C message.
21440
21441 objc-message-args:
21442 objc-selector
21443 objc-selector-args
21444 objc-selector-args , objc-comma-args
21445
21446 objc-selector-args:
21447 objc-selector [opt] : assignment-expression
21448 objc-selector-args objc-selector [opt] : assignment-expression
21449
21450 objc-comma-args:
21451 assignment-expression
21452 objc-comma-args , assignment-expression
21453
21454 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21455 selector arguments and TREE_VALUE containing a list of comma
21456 arguments. */
21457
21458 static tree
21459 cp_parser_objc_message_args (cp_parser* parser)
21460 {
21461 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21462 bool maybe_unary_selector_p = true;
21463 cp_token *token = cp_lexer_peek_token (parser->lexer);
21464
21465 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21466 {
21467 tree selector = NULL_TREE, arg;
21468
21469 if (token->type != CPP_COLON)
21470 selector = cp_parser_objc_selector (parser);
21471
21472 /* Detect if we have a unary selector. */
21473 if (maybe_unary_selector_p
21474 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21475 return build_tree_list (selector, NULL_TREE);
21476
21477 maybe_unary_selector_p = false;
21478 cp_parser_require (parser, CPP_COLON, RT_COLON);
21479 arg = cp_parser_assignment_expression (parser, false, NULL);
21480
21481 sel_args
21482 = chainon (sel_args,
21483 build_tree_list (selector, arg));
21484
21485 token = cp_lexer_peek_token (parser->lexer);
21486 }
21487
21488 /* Handle non-selector arguments, if any. */
21489 while (token->type == CPP_COMMA)
21490 {
21491 tree arg;
21492
21493 cp_lexer_consume_token (parser->lexer);
21494 arg = cp_parser_assignment_expression (parser, false, NULL);
21495
21496 addl_args
21497 = chainon (addl_args,
21498 build_tree_list (NULL_TREE, arg));
21499
21500 token = cp_lexer_peek_token (parser->lexer);
21501 }
21502
21503 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21504 {
21505 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21506 return build_tree_list (error_mark_node, error_mark_node);
21507 }
21508
21509 return build_tree_list (sel_args, addl_args);
21510 }
21511
21512 /* Parse an Objective-C encode expression.
21513
21514 objc-encode-expression:
21515 @encode objc-typename
21516
21517 Returns an encoded representation of the type argument. */
21518
21519 static tree
21520 cp_parser_objc_encode_expression (cp_parser* parser)
21521 {
21522 tree type;
21523 cp_token *token;
21524
21525 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21526 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21527 token = cp_lexer_peek_token (parser->lexer);
21528 type = complete_type (cp_parser_type_id (parser));
21529 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21530
21531 if (!type)
21532 {
21533 error_at (token->location,
21534 "%<@encode%> must specify a type as an argument");
21535 return error_mark_node;
21536 }
21537
21538 /* This happens if we find @encode(T) (where T is a template
21539 typename or something dependent on a template typename) when
21540 parsing a template. In that case, we can't compile it
21541 immediately, but we rather create an AT_ENCODE_EXPR which will
21542 need to be instantiated when the template is used.
21543 */
21544 if (dependent_type_p (type))
21545 {
21546 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21547 TREE_READONLY (value) = 1;
21548 return value;
21549 }
21550
21551 return objc_build_encode_expr (type);
21552 }
21553
21554 /* Parse an Objective-C @defs expression. */
21555
21556 static tree
21557 cp_parser_objc_defs_expression (cp_parser *parser)
21558 {
21559 tree name;
21560
21561 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21562 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21563 name = cp_parser_identifier (parser);
21564 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21565
21566 return objc_get_class_ivars (name);
21567 }
21568
21569 /* Parse an Objective-C protocol expression.
21570
21571 objc-protocol-expression:
21572 @protocol ( identifier )
21573
21574 Returns a representation of the protocol expression. */
21575
21576 static tree
21577 cp_parser_objc_protocol_expression (cp_parser* parser)
21578 {
21579 tree proto;
21580
21581 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21582 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21583 proto = cp_parser_identifier (parser);
21584 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21585
21586 return objc_build_protocol_expr (proto);
21587 }
21588
21589 /* Parse an Objective-C selector expression.
21590
21591 objc-selector-expression:
21592 @selector ( objc-method-signature )
21593
21594 objc-method-signature:
21595 objc-selector
21596 objc-selector-seq
21597
21598 objc-selector-seq:
21599 objc-selector :
21600 objc-selector-seq objc-selector :
21601
21602 Returns a representation of the method selector. */
21603
21604 static tree
21605 cp_parser_objc_selector_expression (cp_parser* parser)
21606 {
21607 tree sel_seq = NULL_TREE;
21608 bool maybe_unary_selector_p = true;
21609 cp_token *token;
21610 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21611
21612 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21613 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21614 token = cp_lexer_peek_token (parser->lexer);
21615
21616 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21617 || token->type == CPP_SCOPE)
21618 {
21619 tree selector = NULL_TREE;
21620
21621 if (token->type != CPP_COLON
21622 || token->type == CPP_SCOPE)
21623 selector = cp_parser_objc_selector (parser);
21624
21625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21626 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21627 {
21628 /* Detect if we have a unary selector. */
21629 if (maybe_unary_selector_p)
21630 {
21631 sel_seq = selector;
21632 goto finish_selector;
21633 }
21634 else
21635 {
21636 cp_parser_error (parser, "expected %<:%>");
21637 }
21638 }
21639 maybe_unary_selector_p = false;
21640 token = cp_lexer_consume_token (parser->lexer);
21641
21642 if (token->type == CPP_SCOPE)
21643 {
21644 sel_seq
21645 = chainon (sel_seq,
21646 build_tree_list (selector, NULL_TREE));
21647 sel_seq
21648 = chainon (sel_seq,
21649 build_tree_list (NULL_TREE, NULL_TREE));
21650 }
21651 else
21652 sel_seq
21653 = chainon (sel_seq,
21654 build_tree_list (selector, NULL_TREE));
21655
21656 token = cp_lexer_peek_token (parser->lexer);
21657 }
21658
21659 finish_selector:
21660 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21661
21662 return objc_build_selector_expr (loc, sel_seq);
21663 }
21664
21665 /* Parse a list of identifiers.
21666
21667 objc-identifier-list:
21668 identifier
21669 objc-identifier-list , identifier
21670
21671 Returns a TREE_LIST of identifier nodes. */
21672
21673 static tree
21674 cp_parser_objc_identifier_list (cp_parser* parser)
21675 {
21676 tree identifier;
21677 tree list;
21678 cp_token *sep;
21679
21680 identifier = cp_parser_identifier (parser);
21681 if (identifier == error_mark_node)
21682 return error_mark_node;
21683
21684 list = build_tree_list (NULL_TREE, identifier);
21685 sep = cp_lexer_peek_token (parser->lexer);
21686
21687 while (sep->type == CPP_COMMA)
21688 {
21689 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21690 identifier = cp_parser_identifier (parser);
21691 if (identifier == error_mark_node)
21692 return list;
21693
21694 list = chainon (list, build_tree_list (NULL_TREE,
21695 identifier));
21696 sep = cp_lexer_peek_token (parser->lexer);
21697 }
21698
21699 return list;
21700 }
21701
21702 /* Parse an Objective-C alias declaration.
21703
21704 objc-alias-declaration:
21705 @compatibility_alias identifier identifier ;
21706
21707 This function registers the alias mapping with the Objective-C front end.
21708 It returns nothing. */
21709
21710 static void
21711 cp_parser_objc_alias_declaration (cp_parser* parser)
21712 {
21713 tree alias, orig;
21714
21715 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21716 alias = cp_parser_identifier (parser);
21717 orig = cp_parser_identifier (parser);
21718 objc_declare_alias (alias, orig);
21719 cp_parser_consume_semicolon_at_end_of_statement (parser);
21720 }
21721
21722 /* Parse an Objective-C class forward-declaration.
21723
21724 objc-class-declaration:
21725 @class objc-identifier-list ;
21726
21727 The function registers the forward declarations with the Objective-C
21728 front end. It returns nothing. */
21729
21730 static void
21731 cp_parser_objc_class_declaration (cp_parser* parser)
21732 {
21733 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21734 while (true)
21735 {
21736 tree id;
21737
21738 id = cp_parser_identifier (parser);
21739 if (id == error_mark_node)
21740 break;
21741
21742 objc_declare_class (id);
21743
21744 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21745 cp_lexer_consume_token (parser->lexer);
21746 else
21747 break;
21748 }
21749 cp_parser_consume_semicolon_at_end_of_statement (parser);
21750 }
21751
21752 /* Parse a list of Objective-C protocol references.
21753
21754 objc-protocol-refs-opt:
21755 objc-protocol-refs [opt]
21756
21757 objc-protocol-refs:
21758 < objc-identifier-list >
21759
21760 Returns a TREE_LIST of identifiers, if any. */
21761
21762 static tree
21763 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21764 {
21765 tree protorefs = NULL_TREE;
21766
21767 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21768 {
21769 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21770 protorefs = cp_parser_objc_identifier_list (parser);
21771 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21772 }
21773
21774 return protorefs;
21775 }
21776
21777 /* Parse a Objective-C visibility specification. */
21778
21779 static void
21780 cp_parser_objc_visibility_spec (cp_parser* parser)
21781 {
21782 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21783
21784 switch (vis->keyword)
21785 {
21786 case RID_AT_PRIVATE:
21787 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21788 break;
21789 case RID_AT_PROTECTED:
21790 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21791 break;
21792 case RID_AT_PUBLIC:
21793 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21794 break;
21795 case RID_AT_PACKAGE:
21796 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21797 break;
21798 default:
21799 return;
21800 }
21801
21802 /* Eat '@private'/'@protected'/'@public'. */
21803 cp_lexer_consume_token (parser->lexer);
21804 }
21805
21806 /* Parse an Objective-C method type. Return 'true' if it is a class
21807 (+) method, and 'false' if it is an instance (-) method. */
21808
21809 static inline bool
21810 cp_parser_objc_method_type (cp_parser* parser)
21811 {
21812 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21813 return true;
21814 else
21815 return false;
21816 }
21817
21818 /* Parse an Objective-C protocol qualifier. */
21819
21820 static tree
21821 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21822 {
21823 tree quals = NULL_TREE, node;
21824 cp_token *token = cp_lexer_peek_token (parser->lexer);
21825
21826 node = token->u.value;
21827
21828 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21829 && (node == ridpointers [(int) RID_IN]
21830 || node == ridpointers [(int) RID_OUT]
21831 || node == ridpointers [(int) RID_INOUT]
21832 || node == ridpointers [(int) RID_BYCOPY]
21833 || node == ridpointers [(int) RID_BYREF]
21834 || node == ridpointers [(int) RID_ONEWAY]))
21835 {
21836 quals = tree_cons (NULL_TREE, node, quals);
21837 cp_lexer_consume_token (parser->lexer);
21838 token = cp_lexer_peek_token (parser->lexer);
21839 node = token->u.value;
21840 }
21841
21842 return quals;
21843 }
21844
21845 /* Parse an Objective-C typename. */
21846
21847 static tree
21848 cp_parser_objc_typename (cp_parser* parser)
21849 {
21850 tree type_name = NULL_TREE;
21851
21852 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21853 {
21854 tree proto_quals, cp_type = NULL_TREE;
21855
21856 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21857 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21858
21859 /* An ObjC type name may consist of just protocol qualifiers, in which
21860 case the type shall default to 'id'. */
21861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21862 {
21863 cp_type = cp_parser_type_id (parser);
21864
21865 /* If the type could not be parsed, an error has already
21866 been produced. For error recovery, behave as if it had
21867 not been specified, which will use the default type
21868 'id'. */
21869 if (cp_type == error_mark_node)
21870 {
21871 cp_type = NULL_TREE;
21872 /* We need to skip to the closing parenthesis as
21873 cp_parser_type_id() does not seem to do it for
21874 us. */
21875 cp_parser_skip_to_closing_parenthesis (parser,
21876 /*recovering=*/true,
21877 /*or_comma=*/false,
21878 /*consume_paren=*/false);
21879 }
21880 }
21881
21882 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21883 type_name = build_tree_list (proto_quals, cp_type);
21884 }
21885
21886 return type_name;
21887 }
21888
21889 /* Check to see if TYPE refers to an Objective-C selector name. */
21890
21891 static bool
21892 cp_parser_objc_selector_p (enum cpp_ttype type)
21893 {
21894 return (type == CPP_NAME || type == CPP_KEYWORD
21895 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21896 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21897 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21898 || type == CPP_XOR || type == CPP_XOR_EQ);
21899 }
21900
21901 /* Parse an Objective-C selector. */
21902
21903 static tree
21904 cp_parser_objc_selector (cp_parser* parser)
21905 {
21906 cp_token *token = cp_lexer_consume_token (parser->lexer);
21907
21908 if (!cp_parser_objc_selector_p (token->type))
21909 {
21910 error_at (token->location, "invalid Objective-C++ selector name");
21911 return error_mark_node;
21912 }
21913
21914 /* C++ operator names are allowed to appear in ObjC selectors. */
21915 switch (token->type)
21916 {
21917 case CPP_AND_AND: return get_identifier ("and");
21918 case CPP_AND_EQ: return get_identifier ("and_eq");
21919 case CPP_AND: return get_identifier ("bitand");
21920 case CPP_OR: return get_identifier ("bitor");
21921 case CPP_COMPL: return get_identifier ("compl");
21922 case CPP_NOT: return get_identifier ("not");
21923 case CPP_NOT_EQ: return get_identifier ("not_eq");
21924 case CPP_OR_OR: return get_identifier ("or");
21925 case CPP_OR_EQ: return get_identifier ("or_eq");
21926 case CPP_XOR: return get_identifier ("xor");
21927 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21928 default: return token->u.value;
21929 }
21930 }
21931
21932 /* Parse an Objective-C params list. */
21933
21934 static tree
21935 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21936 {
21937 tree params = NULL_TREE;
21938 bool maybe_unary_selector_p = true;
21939 cp_token *token = cp_lexer_peek_token (parser->lexer);
21940
21941 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21942 {
21943 tree selector = NULL_TREE, type_name, identifier;
21944 tree parm_attr = NULL_TREE;
21945
21946 if (token->keyword == RID_ATTRIBUTE)
21947 break;
21948
21949 if (token->type != CPP_COLON)
21950 selector = cp_parser_objc_selector (parser);
21951
21952 /* Detect if we have a unary selector. */
21953 if (maybe_unary_selector_p
21954 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21955 {
21956 params = selector; /* Might be followed by attributes. */
21957 break;
21958 }
21959
21960 maybe_unary_selector_p = false;
21961 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21962 {
21963 /* Something went quite wrong. There should be a colon
21964 here, but there is not. Stop parsing parameters. */
21965 break;
21966 }
21967 type_name = cp_parser_objc_typename (parser);
21968 /* New ObjC allows attributes on parameters too. */
21969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21970 parm_attr = cp_parser_attributes_opt (parser);
21971 identifier = cp_parser_identifier (parser);
21972
21973 params
21974 = chainon (params,
21975 objc_build_keyword_decl (selector,
21976 type_name,
21977 identifier,
21978 parm_attr));
21979
21980 token = cp_lexer_peek_token (parser->lexer);
21981 }
21982
21983 if (params == NULL_TREE)
21984 {
21985 cp_parser_error (parser, "objective-c++ method declaration is expected");
21986 return error_mark_node;
21987 }
21988
21989 /* We allow tail attributes for the method. */
21990 if (token->keyword == RID_ATTRIBUTE)
21991 {
21992 *attributes = cp_parser_attributes_opt (parser);
21993 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21994 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21995 return params;
21996 cp_parser_error (parser,
21997 "method attributes must be specified at the end");
21998 return error_mark_node;
21999 }
22000
22001 if (params == NULL_TREE)
22002 {
22003 cp_parser_error (parser, "objective-c++ method declaration is expected");
22004 return error_mark_node;
22005 }
22006 return params;
22007 }
22008
22009 /* Parse the non-keyword Objective-C params. */
22010
22011 static tree
22012 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
22013 tree* attributes)
22014 {
22015 tree params = make_node (TREE_LIST);
22016 cp_token *token = cp_lexer_peek_token (parser->lexer);
22017 *ellipsisp = false; /* Initially, assume no ellipsis. */
22018
22019 while (token->type == CPP_COMMA)
22020 {
22021 cp_parameter_declarator *parmdecl;
22022 tree parm;
22023
22024 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22025 token = cp_lexer_peek_token (parser->lexer);
22026
22027 if (token->type == CPP_ELLIPSIS)
22028 {
22029 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22030 *ellipsisp = true;
22031 token = cp_lexer_peek_token (parser->lexer);
22032 break;
22033 }
22034
22035 /* TODO: parse attributes for tail parameters. */
22036 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22037 parm = grokdeclarator (parmdecl->declarator,
22038 &parmdecl->decl_specifiers,
22039 PARM, /*initialized=*/0,
22040 /*attrlist=*/NULL);
22041
22042 chainon (params, build_tree_list (NULL_TREE, parm));
22043 token = cp_lexer_peek_token (parser->lexer);
22044 }
22045
22046 /* We allow tail attributes for the method. */
22047 if (token->keyword == RID_ATTRIBUTE)
22048 {
22049 if (*attributes == NULL_TREE)
22050 {
22051 *attributes = cp_parser_attributes_opt (parser);
22052 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22053 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22054 return params;
22055 }
22056 else
22057 /* We have an error, but parse the attributes, so that we can
22058 carry on. */
22059 *attributes = cp_parser_attributes_opt (parser);
22060
22061 cp_parser_error (parser,
22062 "method attributes must be specified at the end");
22063 return error_mark_node;
22064 }
22065
22066 return params;
22067 }
22068
22069 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22070
22071 static void
22072 cp_parser_objc_interstitial_code (cp_parser* parser)
22073 {
22074 cp_token *token = cp_lexer_peek_token (parser->lexer);
22075
22076 /* If the next token is `extern' and the following token is a string
22077 literal, then we have a linkage specification. */
22078 if (token->keyword == RID_EXTERN
22079 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22080 cp_parser_linkage_specification (parser);
22081 /* Handle #pragma, if any. */
22082 else if (token->type == CPP_PRAGMA)
22083 cp_parser_pragma (parser, pragma_external);
22084 /* Allow stray semicolons. */
22085 else if (token->type == CPP_SEMICOLON)
22086 cp_lexer_consume_token (parser->lexer);
22087 /* Mark methods as optional or required, when building protocols. */
22088 else if (token->keyword == RID_AT_OPTIONAL)
22089 {
22090 cp_lexer_consume_token (parser->lexer);
22091 objc_set_method_opt (true);
22092 }
22093 else if (token->keyword == RID_AT_REQUIRED)
22094 {
22095 cp_lexer_consume_token (parser->lexer);
22096 objc_set_method_opt (false);
22097 }
22098 else if (token->keyword == RID_NAMESPACE)
22099 cp_parser_namespace_definition (parser);
22100 /* Other stray characters must generate errors. */
22101 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22102 {
22103 cp_lexer_consume_token (parser->lexer);
22104 error ("stray %qs between Objective-C++ methods",
22105 token->type == CPP_OPEN_BRACE ? "{" : "}");
22106 }
22107 /* Finally, try to parse a block-declaration, or a function-definition. */
22108 else
22109 cp_parser_block_declaration (parser, /*statement_p=*/false);
22110 }
22111
22112 /* Parse a method signature. */
22113
22114 static tree
22115 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22116 {
22117 tree rettype, kwdparms, optparms;
22118 bool ellipsis = false;
22119 bool is_class_method;
22120
22121 is_class_method = cp_parser_objc_method_type (parser);
22122 rettype = cp_parser_objc_typename (parser);
22123 *attributes = NULL_TREE;
22124 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22125 if (kwdparms == error_mark_node)
22126 return error_mark_node;
22127 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22128 if (optparms == error_mark_node)
22129 return error_mark_node;
22130
22131 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22132 }
22133
22134 static bool
22135 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22136 {
22137 tree tattr;
22138 cp_lexer_save_tokens (parser->lexer);
22139 tattr = cp_parser_attributes_opt (parser);
22140 gcc_assert (tattr) ;
22141
22142 /* If the attributes are followed by a method introducer, this is not allowed.
22143 Dump the attributes and flag the situation. */
22144 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22145 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22146 return true;
22147
22148 /* Otherwise, the attributes introduce some interstitial code, possibly so
22149 rewind to allow that check. */
22150 cp_lexer_rollback_tokens (parser->lexer);
22151 return false;
22152 }
22153
22154 /* Parse an Objective-C method prototype list. */
22155
22156 static void
22157 cp_parser_objc_method_prototype_list (cp_parser* parser)
22158 {
22159 cp_token *token = cp_lexer_peek_token (parser->lexer);
22160
22161 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22162 {
22163 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22164 {
22165 tree attributes, sig;
22166 bool is_class_method;
22167 if (token->type == CPP_PLUS)
22168 is_class_method = true;
22169 else
22170 is_class_method = false;
22171 sig = cp_parser_objc_method_signature (parser, &attributes);
22172 if (sig == error_mark_node)
22173 {
22174 cp_parser_skip_to_end_of_block_or_statement (parser);
22175 token = cp_lexer_peek_token (parser->lexer);
22176 continue;
22177 }
22178 objc_add_method_declaration (is_class_method, sig, attributes);
22179 cp_parser_consume_semicolon_at_end_of_statement (parser);
22180 }
22181 else if (token->keyword == RID_AT_PROPERTY)
22182 cp_parser_objc_at_property_declaration (parser);
22183 else if (token->keyword == RID_ATTRIBUTE
22184 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22185 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22186 OPT_Wattributes,
22187 "prefix attributes are ignored for methods");
22188 else
22189 /* Allow for interspersed non-ObjC++ code. */
22190 cp_parser_objc_interstitial_code (parser);
22191
22192 token = cp_lexer_peek_token (parser->lexer);
22193 }
22194
22195 if (token->type != CPP_EOF)
22196 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22197 else
22198 cp_parser_error (parser, "expected %<@end%>");
22199
22200 objc_finish_interface ();
22201 }
22202
22203 /* Parse an Objective-C method definition list. */
22204
22205 static void
22206 cp_parser_objc_method_definition_list (cp_parser* parser)
22207 {
22208 cp_token *token = cp_lexer_peek_token (parser->lexer);
22209
22210 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22211 {
22212 tree meth;
22213
22214 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22215 {
22216 cp_token *ptk;
22217 tree sig, attribute;
22218 bool is_class_method;
22219 if (token->type == CPP_PLUS)
22220 is_class_method = true;
22221 else
22222 is_class_method = false;
22223 push_deferring_access_checks (dk_deferred);
22224 sig = cp_parser_objc_method_signature (parser, &attribute);
22225 if (sig == error_mark_node)
22226 {
22227 cp_parser_skip_to_end_of_block_or_statement (parser);
22228 token = cp_lexer_peek_token (parser->lexer);
22229 continue;
22230 }
22231 objc_start_method_definition (is_class_method, sig, attribute);
22232
22233 /* For historical reasons, we accept an optional semicolon. */
22234 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22235 cp_lexer_consume_token (parser->lexer);
22236
22237 ptk = cp_lexer_peek_token (parser->lexer);
22238 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22239 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22240 {
22241 perform_deferred_access_checks ();
22242 stop_deferring_access_checks ();
22243 meth = cp_parser_function_definition_after_declarator (parser,
22244 false);
22245 pop_deferring_access_checks ();
22246 objc_finish_method_definition (meth);
22247 }
22248 }
22249 /* The following case will be removed once @synthesize is
22250 completely implemented. */
22251 else if (token->keyword == RID_AT_PROPERTY)
22252 cp_parser_objc_at_property_declaration (parser);
22253 else if (token->keyword == RID_AT_SYNTHESIZE)
22254 cp_parser_objc_at_synthesize_declaration (parser);
22255 else if (token->keyword == RID_AT_DYNAMIC)
22256 cp_parser_objc_at_dynamic_declaration (parser);
22257 else if (token->keyword == RID_ATTRIBUTE
22258 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22259 warning_at (token->location, OPT_Wattributes,
22260 "prefix attributes are ignored for methods");
22261 else
22262 /* Allow for interspersed non-ObjC++ code. */
22263 cp_parser_objc_interstitial_code (parser);
22264
22265 token = cp_lexer_peek_token (parser->lexer);
22266 }
22267
22268 if (token->type != CPP_EOF)
22269 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22270 else
22271 cp_parser_error (parser, "expected %<@end%>");
22272
22273 objc_finish_implementation ();
22274 }
22275
22276 /* Parse Objective-C ivars. */
22277
22278 static void
22279 cp_parser_objc_class_ivars (cp_parser* parser)
22280 {
22281 cp_token *token = cp_lexer_peek_token (parser->lexer);
22282
22283 if (token->type != CPP_OPEN_BRACE)
22284 return; /* No ivars specified. */
22285
22286 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22287 token = cp_lexer_peek_token (parser->lexer);
22288
22289 while (token->type != CPP_CLOSE_BRACE
22290 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22291 {
22292 cp_decl_specifier_seq declspecs;
22293 int decl_class_or_enum_p;
22294 tree prefix_attributes;
22295
22296 cp_parser_objc_visibility_spec (parser);
22297
22298 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22299 break;
22300
22301 cp_parser_decl_specifier_seq (parser,
22302 CP_PARSER_FLAGS_OPTIONAL,
22303 &declspecs,
22304 &decl_class_or_enum_p);
22305
22306 /* auto, register, static, extern, mutable. */
22307 if (declspecs.storage_class != sc_none)
22308 {
22309 cp_parser_error (parser, "invalid type for instance variable");
22310 declspecs.storage_class = sc_none;
22311 }
22312
22313 /* __thread. */
22314 if (declspecs.specs[(int) ds_thread])
22315 {
22316 cp_parser_error (parser, "invalid type for instance variable");
22317 declspecs.specs[(int) ds_thread] = 0;
22318 }
22319
22320 /* typedef. */
22321 if (declspecs.specs[(int) ds_typedef])
22322 {
22323 cp_parser_error (parser, "invalid type for instance variable");
22324 declspecs.specs[(int) ds_typedef] = 0;
22325 }
22326
22327 prefix_attributes = declspecs.attributes;
22328 declspecs.attributes = NULL_TREE;
22329
22330 /* Keep going until we hit the `;' at the end of the
22331 declaration. */
22332 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22333 {
22334 tree width = NULL_TREE, attributes, first_attribute, decl;
22335 cp_declarator *declarator = NULL;
22336 int ctor_dtor_or_conv_p;
22337
22338 /* Check for a (possibly unnamed) bitfield declaration. */
22339 token = cp_lexer_peek_token (parser->lexer);
22340 if (token->type == CPP_COLON)
22341 goto eat_colon;
22342
22343 if (token->type == CPP_NAME
22344 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22345 == CPP_COLON))
22346 {
22347 /* Get the name of the bitfield. */
22348 declarator = make_id_declarator (NULL_TREE,
22349 cp_parser_identifier (parser),
22350 sfk_none);
22351
22352 eat_colon:
22353 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22354 /* Get the width of the bitfield. */
22355 width
22356 = cp_parser_constant_expression (parser,
22357 /*allow_non_constant=*/false,
22358 NULL);
22359 }
22360 else
22361 {
22362 /* Parse the declarator. */
22363 declarator
22364 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22365 &ctor_dtor_or_conv_p,
22366 /*parenthesized_p=*/NULL,
22367 /*member_p=*/false);
22368 }
22369
22370 /* Look for attributes that apply to the ivar. */
22371 attributes = cp_parser_attributes_opt (parser);
22372 /* Remember which attributes are prefix attributes and
22373 which are not. */
22374 first_attribute = attributes;
22375 /* Combine the attributes. */
22376 attributes = chainon (prefix_attributes, attributes);
22377
22378 if (width)
22379 /* Create the bitfield declaration. */
22380 decl = grokbitfield (declarator, &declspecs,
22381 width,
22382 attributes);
22383 else
22384 decl = grokfield (declarator, &declspecs,
22385 NULL_TREE, /*init_const_expr_p=*/false,
22386 NULL_TREE, attributes);
22387
22388 /* Add the instance variable. */
22389 objc_add_instance_variable (decl);
22390
22391 /* Reset PREFIX_ATTRIBUTES. */
22392 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22393 attributes = TREE_CHAIN (attributes);
22394 if (attributes)
22395 TREE_CHAIN (attributes) = NULL_TREE;
22396
22397 token = cp_lexer_peek_token (parser->lexer);
22398
22399 if (token->type == CPP_COMMA)
22400 {
22401 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22402 continue;
22403 }
22404 break;
22405 }
22406
22407 cp_parser_consume_semicolon_at_end_of_statement (parser);
22408 token = cp_lexer_peek_token (parser->lexer);
22409 }
22410
22411 if (token->keyword == RID_AT_END)
22412 cp_parser_error (parser, "expected %<}%>");
22413
22414 /* Do not consume the RID_AT_END, so it will be read again as terminating
22415 the @interface of @implementation. */
22416 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22417 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22418
22419 /* For historical reasons, we accept an optional semicolon. */
22420 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22421 cp_lexer_consume_token (parser->lexer);
22422 }
22423
22424 /* Parse an Objective-C protocol declaration. */
22425
22426 static void
22427 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22428 {
22429 tree proto, protorefs;
22430 cp_token *tok;
22431
22432 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22434 {
22435 tok = cp_lexer_peek_token (parser->lexer);
22436 error_at (tok->location, "identifier expected after %<@protocol%>");
22437 cp_parser_consume_semicolon_at_end_of_statement (parser);
22438 return;
22439 }
22440
22441 /* See if we have a forward declaration or a definition. */
22442 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22443
22444 /* Try a forward declaration first. */
22445 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22446 {
22447 while (true)
22448 {
22449 tree id;
22450
22451 id = cp_parser_identifier (parser);
22452 if (id == error_mark_node)
22453 break;
22454
22455 objc_declare_protocol (id, attributes);
22456
22457 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22458 cp_lexer_consume_token (parser->lexer);
22459 else
22460 break;
22461 }
22462 cp_parser_consume_semicolon_at_end_of_statement (parser);
22463 }
22464
22465 /* Ok, we got a full-fledged definition (or at least should). */
22466 else
22467 {
22468 proto = cp_parser_identifier (parser);
22469 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22470 objc_start_protocol (proto, protorefs, attributes);
22471 cp_parser_objc_method_prototype_list (parser);
22472 }
22473 }
22474
22475 /* Parse an Objective-C superclass or category. */
22476
22477 static void
22478 cp_parser_objc_superclass_or_category (cp_parser *parser,
22479 bool iface_p,
22480 tree *super,
22481 tree *categ, bool *is_class_extension)
22482 {
22483 cp_token *next = cp_lexer_peek_token (parser->lexer);
22484
22485 *super = *categ = NULL_TREE;
22486 *is_class_extension = false;
22487 if (next->type == CPP_COLON)
22488 {
22489 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22490 *super = cp_parser_identifier (parser);
22491 }
22492 else if (next->type == CPP_OPEN_PAREN)
22493 {
22494 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22495
22496 /* If there is no category name, and this is an @interface, we
22497 have a class extension. */
22498 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22499 {
22500 *categ = NULL_TREE;
22501 *is_class_extension = true;
22502 }
22503 else
22504 *categ = cp_parser_identifier (parser);
22505
22506 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22507 }
22508 }
22509
22510 /* Parse an Objective-C class interface. */
22511
22512 static void
22513 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22514 {
22515 tree name, super, categ, protos;
22516 bool is_class_extension;
22517
22518 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22519 name = cp_parser_identifier (parser);
22520 if (name == error_mark_node)
22521 {
22522 /* It's hard to recover because even if valid @interface stuff
22523 is to follow, we can't compile it (or validate it) if we
22524 don't even know which class it refers to. Let's assume this
22525 was a stray '@interface' token in the stream and skip it.
22526 */
22527 return;
22528 }
22529 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22530 &is_class_extension);
22531 protos = cp_parser_objc_protocol_refs_opt (parser);
22532
22533 /* We have either a class or a category on our hands. */
22534 if (categ || is_class_extension)
22535 objc_start_category_interface (name, categ, protos, attributes);
22536 else
22537 {
22538 objc_start_class_interface (name, super, protos, attributes);
22539 /* Handle instance variable declarations, if any. */
22540 cp_parser_objc_class_ivars (parser);
22541 objc_continue_interface ();
22542 }
22543
22544 cp_parser_objc_method_prototype_list (parser);
22545 }
22546
22547 /* Parse an Objective-C class implementation. */
22548
22549 static void
22550 cp_parser_objc_class_implementation (cp_parser* parser)
22551 {
22552 tree name, super, categ;
22553 bool is_class_extension;
22554
22555 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22556 name = cp_parser_identifier (parser);
22557 if (name == error_mark_node)
22558 {
22559 /* It's hard to recover because even if valid @implementation
22560 stuff is to follow, we can't compile it (or validate it) if
22561 we don't even know which class it refers to. Let's assume
22562 this was a stray '@implementation' token in the stream and
22563 skip it.
22564 */
22565 return;
22566 }
22567 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22568 &is_class_extension);
22569
22570 /* We have either a class or a category on our hands. */
22571 if (categ)
22572 objc_start_category_implementation (name, categ);
22573 else
22574 {
22575 objc_start_class_implementation (name, super);
22576 /* Handle instance variable declarations, if any. */
22577 cp_parser_objc_class_ivars (parser);
22578 objc_continue_implementation ();
22579 }
22580
22581 cp_parser_objc_method_definition_list (parser);
22582 }
22583
22584 /* Consume the @end token and finish off the implementation. */
22585
22586 static void
22587 cp_parser_objc_end_implementation (cp_parser* parser)
22588 {
22589 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22590 objc_finish_implementation ();
22591 }
22592
22593 /* Parse an Objective-C declaration. */
22594
22595 static void
22596 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22597 {
22598 /* Try to figure out what kind of declaration is present. */
22599 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22600
22601 if (attributes)
22602 switch (kwd->keyword)
22603 {
22604 case RID_AT_ALIAS:
22605 case RID_AT_CLASS:
22606 case RID_AT_END:
22607 error_at (kwd->location, "attributes may not be specified before"
22608 " the %<@%D%> Objective-C++ keyword",
22609 kwd->u.value);
22610 attributes = NULL;
22611 break;
22612 case RID_AT_IMPLEMENTATION:
22613 warning_at (kwd->location, OPT_Wattributes,
22614 "prefix attributes are ignored before %<@%D%>",
22615 kwd->u.value);
22616 attributes = NULL;
22617 default:
22618 break;
22619 }
22620
22621 switch (kwd->keyword)
22622 {
22623 case RID_AT_ALIAS:
22624 cp_parser_objc_alias_declaration (parser);
22625 break;
22626 case RID_AT_CLASS:
22627 cp_parser_objc_class_declaration (parser);
22628 break;
22629 case RID_AT_PROTOCOL:
22630 cp_parser_objc_protocol_declaration (parser, attributes);
22631 break;
22632 case RID_AT_INTERFACE:
22633 cp_parser_objc_class_interface (parser, attributes);
22634 break;
22635 case RID_AT_IMPLEMENTATION:
22636 cp_parser_objc_class_implementation (parser);
22637 break;
22638 case RID_AT_END:
22639 cp_parser_objc_end_implementation (parser);
22640 break;
22641 default:
22642 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22643 kwd->u.value);
22644 cp_parser_skip_to_end_of_block_or_statement (parser);
22645 }
22646 }
22647
22648 /* Parse an Objective-C try-catch-finally statement.
22649
22650 objc-try-catch-finally-stmt:
22651 @try compound-statement objc-catch-clause-seq [opt]
22652 objc-finally-clause [opt]
22653
22654 objc-catch-clause-seq:
22655 objc-catch-clause objc-catch-clause-seq [opt]
22656
22657 objc-catch-clause:
22658 @catch ( objc-exception-declaration ) compound-statement
22659
22660 objc-finally-clause:
22661 @finally compound-statement
22662
22663 objc-exception-declaration:
22664 parameter-declaration
22665 '...'
22666
22667 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22668
22669 Returns NULL_TREE.
22670
22671 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22672 for C. Keep them in sync. */
22673
22674 static tree
22675 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22676 {
22677 location_t location;
22678 tree stmt;
22679
22680 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22681 location = cp_lexer_peek_token (parser->lexer)->location;
22682 objc_maybe_warn_exceptions (location);
22683 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22684 node, lest it get absorbed into the surrounding block. */
22685 stmt = push_stmt_list ();
22686 cp_parser_compound_statement (parser, NULL, false, false);
22687 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22688
22689 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22690 {
22691 cp_parameter_declarator *parm;
22692 tree parameter_declaration = error_mark_node;
22693 bool seen_open_paren = false;
22694
22695 cp_lexer_consume_token (parser->lexer);
22696 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22697 seen_open_paren = true;
22698 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22699 {
22700 /* We have "@catch (...)" (where the '...' are literally
22701 what is in the code). Skip the '...'.
22702 parameter_declaration is set to NULL_TREE, and
22703 objc_being_catch_clauses() knows that that means
22704 '...'. */
22705 cp_lexer_consume_token (parser->lexer);
22706 parameter_declaration = NULL_TREE;
22707 }
22708 else
22709 {
22710 /* We have "@catch (NSException *exception)" or something
22711 like that. Parse the parameter declaration. */
22712 parm = cp_parser_parameter_declaration (parser, false, NULL);
22713 if (parm == NULL)
22714 parameter_declaration = error_mark_node;
22715 else
22716 parameter_declaration = grokdeclarator (parm->declarator,
22717 &parm->decl_specifiers,
22718 PARM, /*initialized=*/0,
22719 /*attrlist=*/NULL);
22720 }
22721 if (seen_open_paren)
22722 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22723 else
22724 {
22725 /* If there was no open parenthesis, we are recovering from
22726 an error, and we are trying to figure out what mistake
22727 the user has made. */
22728
22729 /* If there is an immediate closing parenthesis, the user
22730 probably forgot the opening one (ie, they typed "@catch
22731 NSException *e)". Parse the closing parenthesis and keep
22732 going. */
22733 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22734 cp_lexer_consume_token (parser->lexer);
22735
22736 /* If these is no immediate closing parenthesis, the user
22737 probably doesn't know that parenthesis are required at
22738 all (ie, they typed "@catch NSException *e"). So, just
22739 forget about the closing parenthesis and keep going. */
22740 }
22741 objc_begin_catch_clause (parameter_declaration);
22742 cp_parser_compound_statement (parser, NULL, false, false);
22743 objc_finish_catch_clause ();
22744 }
22745 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22746 {
22747 cp_lexer_consume_token (parser->lexer);
22748 location = cp_lexer_peek_token (parser->lexer)->location;
22749 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22750 node, lest it get absorbed into the surrounding block. */
22751 stmt = push_stmt_list ();
22752 cp_parser_compound_statement (parser, NULL, false, false);
22753 objc_build_finally_clause (location, pop_stmt_list (stmt));
22754 }
22755
22756 return objc_finish_try_stmt ();
22757 }
22758
22759 /* Parse an Objective-C synchronized statement.
22760
22761 objc-synchronized-stmt:
22762 @synchronized ( expression ) compound-statement
22763
22764 Returns NULL_TREE. */
22765
22766 static tree
22767 cp_parser_objc_synchronized_statement (cp_parser *parser)
22768 {
22769 location_t location;
22770 tree lock, stmt;
22771
22772 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22773
22774 location = cp_lexer_peek_token (parser->lexer)->location;
22775 objc_maybe_warn_exceptions (location);
22776 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22777 lock = cp_parser_expression (parser, false, NULL);
22778 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22779
22780 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22781 node, lest it get absorbed into the surrounding block. */
22782 stmt = push_stmt_list ();
22783 cp_parser_compound_statement (parser, NULL, false, false);
22784
22785 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22786 }
22787
22788 /* Parse an Objective-C throw statement.
22789
22790 objc-throw-stmt:
22791 @throw assignment-expression [opt] ;
22792
22793 Returns a constructed '@throw' statement. */
22794
22795 static tree
22796 cp_parser_objc_throw_statement (cp_parser *parser)
22797 {
22798 tree expr = NULL_TREE;
22799 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22800
22801 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22802
22803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22804 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22805
22806 cp_parser_consume_semicolon_at_end_of_statement (parser);
22807
22808 return objc_build_throw_stmt (loc, expr);
22809 }
22810
22811 /* Parse an Objective-C statement. */
22812
22813 static tree
22814 cp_parser_objc_statement (cp_parser * parser)
22815 {
22816 /* Try to figure out what kind of declaration is present. */
22817 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22818
22819 switch (kwd->keyword)
22820 {
22821 case RID_AT_TRY:
22822 return cp_parser_objc_try_catch_finally_statement (parser);
22823 case RID_AT_SYNCHRONIZED:
22824 return cp_parser_objc_synchronized_statement (parser);
22825 case RID_AT_THROW:
22826 return cp_parser_objc_throw_statement (parser);
22827 default:
22828 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22829 kwd->u.value);
22830 cp_parser_skip_to_end_of_block_or_statement (parser);
22831 }
22832
22833 return error_mark_node;
22834 }
22835
22836 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22837 look ahead to see if an objc keyword follows the attributes. This
22838 is to detect the use of prefix attributes on ObjC @interface and
22839 @protocol. */
22840
22841 static bool
22842 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22843 {
22844 cp_lexer_save_tokens (parser->lexer);
22845 *attrib = cp_parser_attributes_opt (parser);
22846 gcc_assert (*attrib);
22847 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22848 {
22849 cp_lexer_commit_tokens (parser->lexer);
22850 return true;
22851 }
22852 cp_lexer_rollback_tokens (parser->lexer);
22853 return false;
22854 }
22855
22856 /* This routine is a minimal replacement for
22857 c_parser_struct_declaration () used when parsing the list of
22858 types/names or ObjC++ properties. For example, when parsing the
22859 code
22860
22861 @property (readonly) int a, b, c;
22862
22863 this function is responsible for parsing "int a, int b, int c" and
22864 returning the declarations as CHAIN of DECLs.
22865
22866 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22867 similar parsing. */
22868 static tree
22869 cp_parser_objc_struct_declaration (cp_parser *parser)
22870 {
22871 tree decls = NULL_TREE;
22872 cp_decl_specifier_seq declspecs;
22873 int decl_class_or_enum_p;
22874 tree prefix_attributes;
22875
22876 cp_parser_decl_specifier_seq (parser,
22877 CP_PARSER_FLAGS_NONE,
22878 &declspecs,
22879 &decl_class_or_enum_p);
22880
22881 if (declspecs.type == error_mark_node)
22882 return error_mark_node;
22883
22884 /* auto, register, static, extern, mutable. */
22885 if (declspecs.storage_class != sc_none)
22886 {
22887 cp_parser_error (parser, "invalid type for property");
22888 declspecs.storage_class = sc_none;
22889 }
22890
22891 /* __thread. */
22892 if (declspecs.specs[(int) ds_thread])
22893 {
22894 cp_parser_error (parser, "invalid type for property");
22895 declspecs.specs[(int) ds_thread] = 0;
22896 }
22897
22898 /* typedef. */
22899 if (declspecs.specs[(int) ds_typedef])
22900 {
22901 cp_parser_error (parser, "invalid type for property");
22902 declspecs.specs[(int) ds_typedef] = 0;
22903 }
22904
22905 prefix_attributes = declspecs.attributes;
22906 declspecs.attributes = NULL_TREE;
22907
22908 /* Keep going until we hit the `;' at the end of the declaration. */
22909 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22910 {
22911 tree attributes, first_attribute, decl;
22912 cp_declarator *declarator;
22913 cp_token *token;
22914
22915 /* Parse the declarator. */
22916 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22917 NULL, NULL, false);
22918
22919 /* Look for attributes that apply to the ivar. */
22920 attributes = cp_parser_attributes_opt (parser);
22921 /* Remember which attributes are prefix attributes and
22922 which are not. */
22923 first_attribute = attributes;
22924 /* Combine the attributes. */
22925 attributes = chainon (prefix_attributes, attributes);
22926
22927 decl = grokfield (declarator, &declspecs,
22928 NULL_TREE, /*init_const_expr_p=*/false,
22929 NULL_TREE, attributes);
22930
22931 if (decl == error_mark_node || decl == NULL_TREE)
22932 return error_mark_node;
22933
22934 /* Reset PREFIX_ATTRIBUTES. */
22935 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22936 attributes = TREE_CHAIN (attributes);
22937 if (attributes)
22938 TREE_CHAIN (attributes) = NULL_TREE;
22939
22940 DECL_CHAIN (decl) = decls;
22941 decls = decl;
22942
22943 token = cp_lexer_peek_token (parser->lexer);
22944 if (token->type == CPP_COMMA)
22945 {
22946 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22947 continue;
22948 }
22949 else
22950 break;
22951 }
22952 return decls;
22953 }
22954
22955 /* Parse an Objective-C @property declaration. The syntax is:
22956
22957 objc-property-declaration:
22958 '@property' objc-property-attributes[opt] struct-declaration ;
22959
22960 objc-property-attributes:
22961 '(' objc-property-attribute-list ')'
22962
22963 objc-property-attribute-list:
22964 objc-property-attribute
22965 objc-property-attribute-list, objc-property-attribute
22966
22967 objc-property-attribute
22968 'getter' = identifier
22969 'setter' = identifier
22970 'readonly'
22971 'readwrite'
22972 'assign'
22973 'retain'
22974 'copy'
22975 'nonatomic'
22976
22977 For example:
22978 @property NSString *name;
22979 @property (readonly) id object;
22980 @property (retain, nonatomic, getter=getTheName) id name;
22981 @property int a, b, c;
22982
22983 PS: This function is identical to
22984 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22985 static void
22986 cp_parser_objc_at_property_declaration (cp_parser *parser)
22987 {
22988 /* The following variables hold the attributes of the properties as
22989 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22990 seen. When we see an attribute, we set them to 'true' (if they
22991 are boolean properties) or to the identifier (if they have an
22992 argument, ie, for getter and setter). Note that here we only
22993 parse the list of attributes, check the syntax and accumulate the
22994 attributes that we find. objc_add_property_declaration() will
22995 then process the information. */
22996 bool property_assign = false;
22997 bool property_copy = false;
22998 tree property_getter_ident = NULL_TREE;
22999 bool property_nonatomic = false;
23000 bool property_readonly = false;
23001 bool property_readwrite = false;
23002 bool property_retain = false;
23003 tree property_setter_ident = NULL_TREE;
23004
23005 /* 'properties' is the list of properties that we read. Usually a
23006 single one, but maybe more (eg, in "@property int a, b, c;" there
23007 are three). */
23008 tree properties;
23009 location_t loc;
23010
23011 loc = cp_lexer_peek_token (parser->lexer)->location;
23012
23013 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
23014
23015 /* Parse the optional attribute list... */
23016 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23017 {
23018 /* Eat the '('. */
23019 cp_lexer_consume_token (parser->lexer);
23020
23021 while (true)
23022 {
23023 bool syntax_error = false;
23024 cp_token *token = cp_lexer_peek_token (parser->lexer);
23025 enum rid keyword;
23026
23027 if (token->type != CPP_NAME)
23028 {
23029 cp_parser_error (parser, "expected identifier");
23030 break;
23031 }
23032 keyword = C_RID_CODE (token->u.value);
23033 cp_lexer_consume_token (parser->lexer);
23034 switch (keyword)
23035 {
23036 case RID_ASSIGN: property_assign = true; break;
23037 case RID_COPY: property_copy = true; break;
23038 case RID_NONATOMIC: property_nonatomic = true; break;
23039 case RID_READONLY: property_readonly = true; break;
23040 case RID_READWRITE: property_readwrite = true; break;
23041 case RID_RETAIN: property_retain = true; break;
23042
23043 case RID_GETTER:
23044 case RID_SETTER:
23045 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23046 {
23047 if (keyword == RID_GETTER)
23048 cp_parser_error (parser,
23049 "missing %<=%> (after %<getter%> attribute)");
23050 else
23051 cp_parser_error (parser,
23052 "missing %<=%> (after %<setter%> attribute)");
23053 syntax_error = true;
23054 break;
23055 }
23056 cp_lexer_consume_token (parser->lexer); /* eat the = */
23057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23058 {
23059 cp_parser_error (parser, "expected identifier");
23060 syntax_error = true;
23061 break;
23062 }
23063 if (keyword == RID_SETTER)
23064 {
23065 if (property_setter_ident != NULL_TREE)
23066 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23067 else
23068 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23069 cp_lexer_consume_token (parser->lexer);
23070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23071 cp_parser_error (parser, "setter name must terminate with %<:%>");
23072 else
23073 cp_lexer_consume_token (parser->lexer);
23074 }
23075 else
23076 {
23077 if (property_getter_ident != NULL_TREE)
23078 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23079 else
23080 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23081 cp_lexer_consume_token (parser->lexer);
23082 }
23083 break;
23084 default:
23085 cp_parser_error (parser, "unknown property attribute");
23086 syntax_error = true;
23087 break;
23088 }
23089
23090 if (syntax_error)
23091 break;
23092
23093 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23094 cp_lexer_consume_token (parser->lexer);
23095 else
23096 break;
23097 }
23098
23099 /* FIXME: "@property (setter, assign);" will generate a spurious
23100 "error: expected ‘)’ before ‘,’ token". This is because
23101 cp_parser_require, unlike the C counterpart, will produce an
23102 error even if we are in error recovery. */
23103 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23104 {
23105 cp_parser_skip_to_closing_parenthesis (parser,
23106 /*recovering=*/true,
23107 /*or_comma=*/false,
23108 /*consume_paren=*/true);
23109 }
23110 }
23111
23112 /* ... and the property declaration(s). */
23113 properties = cp_parser_objc_struct_declaration (parser);
23114
23115 if (properties == error_mark_node)
23116 {
23117 cp_parser_skip_to_end_of_statement (parser);
23118 /* If the next token is now a `;', consume it. */
23119 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23120 cp_lexer_consume_token (parser->lexer);
23121 return;
23122 }
23123
23124 if (properties == NULL_TREE)
23125 cp_parser_error (parser, "expected identifier");
23126 else
23127 {
23128 /* Comma-separated properties are chained together in
23129 reverse order; add them one by one. */
23130 properties = nreverse (properties);
23131
23132 for (; properties; properties = TREE_CHAIN (properties))
23133 objc_add_property_declaration (loc, copy_node (properties),
23134 property_readonly, property_readwrite,
23135 property_assign, property_retain,
23136 property_copy, property_nonatomic,
23137 property_getter_ident, property_setter_ident);
23138 }
23139
23140 cp_parser_consume_semicolon_at_end_of_statement (parser);
23141 }
23142
23143 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23144
23145 objc-synthesize-declaration:
23146 @synthesize objc-synthesize-identifier-list ;
23147
23148 objc-synthesize-identifier-list:
23149 objc-synthesize-identifier
23150 objc-synthesize-identifier-list, objc-synthesize-identifier
23151
23152 objc-synthesize-identifier
23153 identifier
23154 identifier = identifier
23155
23156 For example:
23157 @synthesize MyProperty;
23158 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23159
23160 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23161 for C. Keep them in sync.
23162 */
23163 static void
23164 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23165 {
23166 tree list = NULL_TREE;
23167 location_t loc;
23168 loc = cp_lexer_peek_token (parser->lexer)->location;
23169
23170 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23171 while (true)
23172 {
23173 tree property, ivar;
23174 property = cp_parser_identifier (parser);
23175 if (property == error_mark_node)
23176 {
23177 cp_parser_consume_semicolon_at_end_of_statement (parser);
23178 return;
23179 }
23180 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23181 {
23182 cp_lexer_consume_token (parser->lexer);
23183 ivar = cp_parser_identifier (parser);
23184 if (ivar == error_mark_node)
23185 {
23186 cp_parser_consume_semicolon_at_end_of_statement (parser);
23187 return;
23188 }
23189 }
23190 else
23191 ivar = NULL_TREE;
23192 list = chainon (list, build_tree_list (ivar, property));
23193 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23194 cp_lexer_consume_token (parser->lexer);
23195 else
23196 break;
23197 }
23198 cp_parser_consume_semicolon_at_end_of_statement (parser);
23199 objc_add_synthesize_declaration (loc, list);
23200 }
23201
23202 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23203
23204 objc-dynamic-declaration:
23205 @dynamic identifier-list ;
23206
23207 For example:
23208 @dynamic MyProperty;
23209 @dynamic MyProperty, AnotherProperty;
23210
23211 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23212 for C. Keep them in sync.
23213 */
23214 static void
23215 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23216 {
23217 tree list = NULL_TREE;
23218 location_t loc;
23219 loc = cp_lexer_peek_token (parser->lexer)->location;
23220
23221 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23222 while (true)
23223 {
23224 tree property;
23225 property = cp_parser_identifier (parser);
23226 if (property == error_mark_node)
23227 {
23228 cp_parser_consume_semicolon_at_end_of_statement (parser);
23229 return;
23230 }
23231 list = chainon (list, build_tree_list (NULL, property));
23232 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23233 cp_lexer_consume_token (parser->lexer);
23234 else
23235 break;
23236 }
23237 cp_parser_consume_semicolon_at_end_of_statement (parser);
23238 objc_add_dynamic_declaration (loc, list);
23239 }
23240
23241 \f
23242 /* OpenMP 2.5 parsing routines. */
23243
23244 /* Returns name of the next clause.
23245 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23246 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23247 returned and the token is consumed. */
23248
23249 static pragma_omp_clause
23250 cp_parser_omp_clause_name (cp_parser *parser)
23251 {
23252 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23253
23254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23255 result = PRAGMA_OMP_CLAUSE_IF;
23256 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23257 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23258 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23259 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23260 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23261 {
23262 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23263 const char *p = IDENTIFIER_POINTER (id);
23264
23265 switch (p[0])
23266 {
23267 case 'c':
23268 if (!strcmp ("collapse", p))
23269 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23270 else if (!strcmp ("copyin", p))
23271 result = PRAGMA_OMP_CLAUSE_COPYIN;
23272 else if (!strcmp ("copyprivate", p))
23273 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23274 break;
23275 case 'f':
23276 if (!strcmp ("firstprivate", p))
23277 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23278 break;
23279 case 'l':
23280 if (!strcmp ("lastprivate", p))
23281 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23282 break;
23283 case 'n':
23284 if (!strcmp ("nowait", p))
23285 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23286 else if (!strcmp ("num_threads", p))
23287 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23288 break;
23289 case 'o':
23290 if (!strcmp ("ordered", p))
23291 result = PRAGMA_OMP_CLAUSE_ORDERED;
23292 break;
23293 case 'r':
23294 if (!strcmp ("reduction", p))
23295 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23296 break;
23297 case 's':
23298 if (!strcmp ("schedule", p))
23299 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23300 else if (!strcmp ("shared", p))
23301 result = PRAGMA_OMP_CLAUSE_SHARED;
23302 break;
23303 case 'u':
23304 if (!strcmp ("untied", p))
23305 result = PRAGMA_OMP_CLAUSE_UNTIED;
23306 break;
23307 }
23308 }
23309
23310 if (result != PRAGMA_OMP_CLAUSE_NONE)
23311 cp_lexer_consume_token (parser->lexer);
23312
23313 return result;
23314 }
23315
23316 /* Validate that a clause of the given type does not already exist. */
23317
23318 static void
23319 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23320 const char *name, location_t location)
23321 {
23322 tree c;
23323
23324 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23325 if (OMP_CLAUSE_CODE (c) == code)
23326 {
23327 error_at (location, "too many %qs clauses", name);
23328 break;
23329 }
23330 }
23331
23332 /* OpenMP 2.5:
23333 variable-list:
23334 identifier
23335 variable-list , identifier
23336
23337 In addition, we match a closing parenthesis. An opening parenthesis
23338 will have been consumed by the caller.
23339
23340 If KIND is nonzero, create the appropriate node and install the decl
23341 in OMP_CLAUSE_DECL and add the node to the head of the list.
23342
23343 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23344 return the list created. */
23345
23346 static tree
23347 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23348 tree list)
23349 {
23350 cp_token *token;
23351 while (1)
23352 {
23353 tree name, decl;
23354
23355 token = cp_lexer_peek_token (parser->lexer);
23356 name = cp_parser_id_expression (parser, /*template_p=*/false,
23357 /*check_dependency_p=*/true,
23358 /*template_p=*/NULL,
23359 /*declarator_p=*/false,
23360 /*optional_p=*/false);
23361 if (name == error_mark_node)
23362 goto skip_comma;
23363
23364 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23365 if (decl == error_mark_node)
23366 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23367 token->location);
23368 else if (kind != 0)
23369 {
23370 tree u = build_omp_clause (token->location, kind);
23371 OMP_CLAUSE_DECL (u) = decl;
23372 OMP_CLAUSE_CHAIN (u) = list;
23373 list = u;
23374 }
23375 else
23376 list = tree_cons (decl, NULL_TREE, list);
23377
23378 get_comma:
23379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23380 break;
23381 cp_lexer_consume_token (parser->lexer);
23382 }
23383
23384 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23385 {
23386 int ending;
23387
23388 /* Try to resync to an unnested comma. Copied from
23389 cp_parser_parenthesized_expression_list. */
23390 skip_comma:
23391 ending = cp_parser_skip_to_closing_parenthesis (parser,
23392 /*recovering=*/true,
23393 /*or_comma=*/true,
23394 /*consume_paren=*/true);
23395 if (ending < 0)
23396 goto get_comma;
23397 }
23398
23399 return list;
23400 }
23401
23402 /* Similarly, but expect leading and trailing parenthesis. This is a very
23403 common case for omp clauses. */
23404
23405 static tree
23406 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23407 {
23408 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23409 return cp_parser_omp_var_list_no_open (parser, kind, list);
23410 return list;
23411 }
23412
23413 /* OpenMP 3.0:
23414 collapse ( constant-expression ) */
23415
23416 static tree
23417 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23418 {
23419 tree c, num;
23420 location_t loc;
23421 HOST_WIDE_INT n;
23422
23423 loc = cp_lexer_peek_token (parser->lexer)->location;
23424 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23425 return list;
23426
23427 num = cp_parser_constant_expression (parser, false, NULL);
23428
23429 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23430 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23431 /*or_comma=*/false,
23432 /*consume_paren=*/true);
23433
23434 if (num == error_mark_node)
23435 return list;
23436 num = fold_non_dependent_expr (num);
23437 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23438 || !host_integerp (num, 0)
23439 || (n = tree_low_cst (num, 0)) <= 0
23440 || (int) n != n)
23441 {
23442 error_at (loc, "collapse argument needs positive constant integer expression");
23443 return list;
23444 }
23445
23446 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23447 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23448 OMP_CLAUSE_CHAIN (c) = list;
23449 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23450
23451 return c;
23452 }
23453
23454 /* OpenMP 2.5:
23455 default ( shared | none ) */
23456
23457 static tree
23458 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23459 {
23460 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23461 tree c;
23462
23463 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23464 return list;
23465 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23466 {
23467 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23468 const char *p = IDENTIFIER_POINTER (id);
23469
23470 switch (p[0])
23471 {
23472 case 'n':
23473 if (strcmp ("none", p) != 0)
23474 goto invalid_kind;
23475 kind = OMP_CLAUSE_DEFAULT_NONE;
23476 break;
23477
23478 case 's':
23479 if (strcmp ("shared", p) != 0)
23480 goto invalid_kind;
23481 kind = OMP_CLAUSE_DEFAULT_SHARED;
23482 break;
23483
23484 default:
23485 goto invalid_kind;
23486 }
23487
23488 cp_lexer_consume_token (parser->lexer);
23489 }
23490 else
23491 {
23492 invalid_kind:
23493 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23494 }
23495
23496 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23497 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23498 /*or_comma=*/false,
23499 /*consume_paren=*/true);
23500
23501 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23502 return list;
23503
23504 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23505 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23506 OMP_CLAUSE_CHAIN (c) = list;
23507 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23508
23509 return c;
23510 }
23511
23512 /* OpenMP 2.5:
23513 if ( expression ) */
23514
23515 static tree
23516 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23517 {
23518 tree t, c;
23519
23520 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23521 return list;
23522
23523 t = cp_parser_condition (parser);
23524
23525 if (t == error_mark_node
23526 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23527 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23528 /*or_comma=*/false,
23529 /*consume_paren=*/true);
23530
23531 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23532
23533 c = build_omp_clause (location, OMP_CLAUSE_IF);
23534 OMP_CLAUSE_IF_EXPR (c) = t;
23535 OMP_CLAUSE_CHAIN (c) = list;
23536
23537 return c;
23538 }
23539
23540 /* OpenMP 2.5:
23541 nowait */
23542
23543 static tree
23544 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23545 tree list, location_t location)
23546 {
23547 tree c;
23548
23549 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23550
23551 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23552 OMP_CLAUSE_CHAIN (c) = list;
23553 return c;
23554 }
23555
23556 /* OpenMP 2.5:
23557 num_threads ( expression ) */
23558
23559 static tree
23560 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23561 location_t location)
23562 {
23563 tree t, c;
23564
23565 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23566 return list;
23567
23568 t = cp_parser_expression (parser, false, NULL);
23569
23570 if (t == error_mark_node
23571 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23572 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23573 /*or_comma=*/false,
23574 /*consume_paren=*/true);
23575
23576 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23577 "num_threads", location);
23578
23579 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23580 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23581 OMP_CLAUSE_CHAIN (c) = list;
23582
23583 return c;
23584 }
23585
23586 /* OpenMP 2.5:
23587 ordered */
23588
23589 static tree
23590 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23591 tree list, location_t location)
23592 {
23593 tree c;
23594
23595 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23596 "ordered", location);
23597
23598 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23599 OMP_CLAUSE_CHAIN (c) = list;
23600 return c;
23601 }
23602
23603 /* OpenMP 2.5:
23604 reduction ( reduction-operator : variable-list )
23605
23606 reduction-operator:
23607 One of: + * - & ^ | && || */
23608
23609 static tree
23610 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23611 {
23612 enum tree_code code;
23613 tree nlist, c;
23614
23615 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23616 return list;
23617
23618 switch (cp_lexer_peek_token (parser->lexer)->type)
23619 {
23620 case CPP_PLUS:
23621 code = PLUS_EXPR;
23622 break;
23623 case CPP_MULT:
23624 code = MULT_EXPR;
23625 break;
23626 case CPP_MINUS:
23627 code = MINUS_EXPR;
23628 break;
23629 case CPP_AND:
23630 code = BIT_AND_EXPR;
23631 break;
23632 case CPP_XOR:
23633 code = BIT_XOR_EXPR;
23634 break;
23635 case CPP_OR:
23636 code = BIT_IOR_EXPR;
23637 break;
23638 case CPP_AND_AND:
23639 code = TRUTH_ANDIF_EXPR;
23640 break;
23641 case CPP_OR_OR:
23642 code = TRUTH_ORIF_EXPR;
23643 break;
23644 default:
23645 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23646 "%<|%>, %<&&%>, or %<||%>");
23647 resync_fail:
23648 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23649 /*or_comma=*/false,
23650 /*consume_paren=*/true);
23651 return list;
23652 }
23653 cp_lexer_consume_token (parser->lexer);
23654
23655 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23656 goto resync_fail;
23657
23658 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23659 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23660 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23661
23662 return nlist;
23663 }
23664
23665 /* OpenMP 2.5:
23666 schedule ( schedule-kind )
23667 schedule ( schedule-kind , expression )
23668
23669 schedule-kind:
23670 static | dynamic | guided | runtime | auto */
23671
23672 static tree
23673 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23674 {
23675 tree c, t;
23676
23677 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23678 return list;
23679
23680 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23681
23682 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23683 {
23684 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23685 const char *p = IDENTIFIER_POINTER (id);
23686
23687 switch (p[0])
23688 {
23689 case 'd':
23690 if (strcmp ("dynamic", p) != 0)
23691 goto invalid_kind;
23692 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23693 break;
23694
23695 case 'g':
23696 if (strcmp ("guided", p) != 0)
23697 goto invalid_kind;
23698 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23699 break;
23700
23701 case 'r':
23702 if (strcmp ("runtime", p) != 0)
23703 goto invalid_kind;
23704 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23705 break;
23706
23707 default:
23708 goto invalid_kind;
23709 }
23710 }
23711 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23712 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23713 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23714 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23715 else
23716 goto invalid_kind;
23717 cp_lexer_consume_token (parser->lexer);
23718
23719 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23720 {
23721 cp_token *token;
23722 cp_lexer_consume_token (parser->lexer);
23723
23724 token = cp_lexer_peek_token (parser->lexer);
23725 t = cp_parser_assignment_expression (parser, false, NULL);
23726
23727 if (t == error_mark_node)
23728 goto resync_fail;
23729 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23730 error_at (token->location, "schedule %<runtime%> does not take "
23731 "a %<chunk_size%> parameter");
23732 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23733 error_at (token->location, "schedule %<auto%> does not take "
23734 "a %<chunk_size%> parameter");
23735 else
23736 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23737
23738 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23739 goto resync_fail;
23740 }
23741 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23742 goto resync_fail;
23743
23744 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23745 OMP_CLAUSE_CHAIN (c) = list;
23746 return c;
23747
23748 invalid_kind:
23749 cp_parser_error (parser, "invalid schedule kind");
23750 resync_fail:
23751 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23752 /*or_comma=*/false,
23753 /*consume_paren=*/true);
23754 return list;
23755 }
23756
23757 /* OpenMP 3.0:
23758 untied */
23759
23760 static tree
23761 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23762 tree list, location_t location)
23763 {
23764 tree c;
23765
23766 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23767
23768 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23769 OMP_CLAUSE_CHAIN (c) = list;
23770 return c;
23771 }
23772
23773 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23774 is a bitmask in MASK. Return the list of clauses found; the result
23775 of clause default goes in *pdefault. */
23776
23777 static tree
23778 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23779 const char *where, cp_token *pragma_tok)
23780 {
23781 tree clauses = NULL;
23782 bool first = true;
23783 cp_token *token = NULL;
23784
23785 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23786 {
23787 pragma_omp_clause c_kind;
23788 const char *c_name;
23789 tree prev = clauses;
23790
23791 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23792 cp_lexer_consume_token (parser->lexer);
23793
23794 token = cp_lexer_peek_token (parser->lexer);
23795 c_kind = cp_parser_omp_clause_name (parser);
23796 first = false;
23797
23798 switch (c_kind)
23799 {
23800 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23801 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23802 token->location);
23803 c_name = "collapse";
23804 break;
23805 case PRAGMA_OMP_CLAUSE_COPYIN:
23806 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23807 c_name = "copyin";
23808 break;
23809 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23810 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23811 clauses);
23812 c_name = "copyprivate";
23813 break;
23814 case PRAGMA_OMP_CLAUSE_DEFAULT:
23815 clauses = cp_parser_omp_clause_default (parser, clauses,
23816 token->location);
23817 c_name = "default";
23818 break;
23819 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23820 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23821 clauses);
23822 c_name = "firstprivate";
23823 break;
23824 case PRAGMA_OMP_CLAUSE_IF:
23825 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23826 c_name = "if";
23827 break;
23828 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23829 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23830 clauses);
23831 c_name = "lastprivate";
23832 break;
23833 case PRAGMA_OMP_CLAUSE_NOWAIT:
23834 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23835 c_name = "nowait";
23836 break;
23837 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23838 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23839 token->location);
23840 c_name = "num_threads";
23841 break;
23842 case PRAGMA_OMP_CLAUSE_ORDERED:
23843 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23844 token->location);
23845 c_name = "ordered";
23846 break;
23847 case PRAGMA_OMP_CLAUSE_PRIVATE:
23848 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23849 clauses);
23850 c_name = "private";
23851 break;
23852 case PRAGMA_OMP_CLAUSE_REDUCTION:
23853 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23854 c_name = "reduction";
23855 break;
23856 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23857 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23858 token->location);
23859 c_name = "schedule";
23860 break;
23861 case PRAGMA_OMP_CLAUSE_SHARED:
23862 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23863 clauses);
23864 c_name = "shared";
23865 break;
23866 case PRAGMA_OMP_CLAUSE_UNTIED:
23867 clauses = cp_parser_omp_clause_untied (parser, clauses,
23868 token->location);
23869 c_name = "nowait";
23870 break;
23871 default:
23872 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23873 goto saw_error;
23874 }
23875
23876 if (((mask >> c_kind) & 1) == 0)
23877 {
23878 /* Remove the invalid clause(s) from the list to avoid
23879 confusing the rest of the compiler. */
23880 clauses = prev;
23881 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23882 }
23883 }
23884 saw_error:
23885 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23886 return finish_omp_clauses (clauses);
23887 }
23888
23889 /* OpenMP 2.5:
23890 structured-block:
23891 statement
23892
23893 In practice, we're also interested in adding the statement to an
23894 outer node. So it is convenient if we work around the fact that
23895 cp_parser_statement calls add_stmt. */
23896
23897 static unsigned
23898 cp_parser_begin_omp_structured_block (cp_parser *parser)
23899 {
23900 unsigned save = parser->in_statement;
23901
23902 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23903 This preserves the "not within loop or switch" style error messages
23904 for nonsense cases like
23905 void foo() {
23906 #pragma omp single
23907 break;
23908 }
23909 */
23910 if (parser->in_statement)
23911 parser->in_statement = IN_OMP_BLOCK;
23912
23913 return save;
23914 }
23915
23916 static void
23917 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23918 {
23919 parser->in_statement = save;
23920 }
23921
23922 static tree
23923 cp_parser_omp_structured_block (cp_parser *parser)
23924 {
23925 tree stmt = begin_omp_structured_block ();
23926 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23927
23928 cp_parser_statement (parser, NULL_TREE, false, NULL);
23929
23930 cp_parser_end_omp_structured_block (parser, save);
23931 return finish_omp_structured_block (stmt);
23932 }
23933
23934 /* OpenMP 2.5:
23935 # pragma omp atomic new-line
23936 expression-stmt
23937
23938 expression-stmt:
23939 x binop= expr | x++ | ++x | x-- | --x
23940 binop:
23941 +, *, -, /, &, ^, |, <<, >>
23942
23943 where x is an lvalue expression with scalar type. */
23944
23945 static void
23946 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23947 {
23948 tree lhs, rhs;
23949 enum tree_code code;
23950
23951 cp_parser_require_pragma_eol (parser, pragma_tok);
23952
23953 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23954 /*cast_p=*/false, NULL);
23955 switch (TREE_CODE (lhs))
23956 {
23957 case ERROR_MARK:
23958 goto saw_error;
23959
23960 case PREINCREMENT_EXPR:
23961 case POSTINCREMENT_EXPR:
23962 lhs = TREE_OPERAND (lhs, 0);
23963 code = PLUS_EXPR;
23964 rhs = integer_one_node;
23965 break;
23966
23967 case PREDECREMENT_EXPR:
23968 case POSTDECREMENT_EXPR:
23969 lhs = TREE_OPERAND (lhs, 0);
23970 code = MINUS_EXPR;
23971 rhs = integer_one_node;
23972 break;
23973
23974 case COMPOUND_EXPR:
23975 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23976 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23977 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23978 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23979 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23980 (TREE_OPERAND (lhs, 1), 0), 0)))
23981 == BOOLEAN_TYPE)
23982 /* Undo effects of boolean_increment for post {in,de}crement. */
23983 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23984 /* FALLTHRU */
23985 case MODIFY_EXPR:
23986 if (TREE_CODE (lhs) == MODIFY_EXPR
23987 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23988 {
23989 /* Undo effects of boolean_increment. */
23990 if (integer_onep (TREE_OPERAND (lhs, 1)))
23991 {
23992 /* This is pre or post increment. */
23993 rhs = TREE_OPERAND (lhs, 1);
23994 lhs = TREE_OPERAND (lhs, 0);
23995 code = NOP_EXPR;
23996 break;
23997 }
23998 }
23999 /* FALLTHRU */
24000 default:
24001 switch (cp_lexer_peek_token (parser->lexer)->type)
24002 {
24003 case CPP_MULT_EQ:
24004 code = MULT_EXPR;
24005 break;
24006 case CPP_DIV_EQ:
24007 code = TRUNC_DIV_EXPR;
24008 break;
24009 case CPP_PLUS_EQ:
24010 code = PLUS_EXPR;
24011 break;
24012 case CPP_MINUS_EQ:
24013 code = MINUS_EXPR;
24014 break;
24015 case CPP_LSHIFT_EQ:
24016 code = LSHIFT_EXPR;
24017 break;
24018 case CPP_RSHIFT_EQ:
24019 code = RSHIFT_EXPR;
24020 break;
24021 case CPP_AND_EQ:
24022 code = BIT_AND_EXPR;
24023 break;
24024 case CPP_OR_EQ:
24025 code = BIT_IOR_EXPR;
24026 break;
24027 case CPP_XOR_EQ:
24028 code = BIT_XOR_EXPR;
24029 break;
24030 default:
24031 cp_parser_error (parser,
24032 "invalid operator for %<#pragma omp atomic%>");
24033 goto saw_error;
24034 }
24035 cp_lexer_consume_token (parser->lexer);
24036
24037 rhs = cp_parser_expression (parser, false, NULL);
24038 if (rhs == error_mark_node)
24039 goto saw_error;
24040 break;
24041 }
24042 finish_omp_atomic (code, lhs, rhs);
24043 cp_parser_consume_semicolon_at_end_of_statement (parser);
24044 return;
24045
24046 saw_error:
24047 cp_parser_skip_to_end_of_block_or_statement (parser);
24048 }
24049
24050
24051 /* OpenMP 2.5:
24052 # pragma omp barrier new-line */
24053
24054 static void
24055 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24056 {
24057 cp_parser_require_pragma_eol (parser, pragma_tok);
24058 finish_omp_barrier ();
24059 }
24060
24061 /* OpenMP 2.5:
24062 # pragma omp critical [(name)] new-line
24063 structured-block */
24064
24065 static tree
24066 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24067 {
24068 tree stmt, name = NULL;
24069
24070 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24071 {
24072 cp_lexer_consume_token (parser->lexer);
24073
24074 name = cp_parser_identifier (parser);
24075
24076 if (name == error_mark_node
24077 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24079 /*or_comma=*/false,
24080 /*consume_paren=*/true);
24081 if (name == error_mark_node)
24082 name = NULL;
24083 }
24084 cp_parser_require_pragma_eol (parser, pragma_tok);
24085
24086 stmt = cp_parser_omp_structured_block (parser);
24087 return c_finish_omp_critical (input_location, stmt, name);
24088 }
24089
24090 /* OpenMP 2.5:
24091 # pragma omp flush flush-vars[opt] new-line
24092
24093 flush-vars:
24094 ( variable-list ) */
24095
24096 static void
24097 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24098 {
24099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24100 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24101 cp_parser_require_pragma_eol (parser, pragma_tok);
24102
24103 finish_omp_flush ();
24104 }
24105
24106 /* Helper function, to parse omp for increment expression. */
24107
24108 static tree
24109 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24110 {
24111 tree cond = cp_parser_binary_expression (parser, false, true,
24112 PREC_NOT_OPERATOR, NULL);
24113 bool overloaded_p;
24114
24115 if (cond == error_mark_node
24116 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24117 {
24118 cp_parser_skip_to_end_of_statement (parser);
24119 return error_mark_node;
24120 }
24121
24122 switch (TREE_CODE (cond))
24123 {
24124 case GT_EXPR:
24125 case GE_EXPR:
24126 case LT_EXPR:
24127 case LE_EXPR:
24128 break;
24129 default:
24130 return error_mark_node;
24131 }
24132
24133 /* If decl is an iterator, preserve LHS and RHS of the relational
24134 expr until finish_omp_for. */
24135 if (decl
24136 && (type_dependent_expression_p (decl)
24137 || CLASS_TYPE_P (TREE_TYPE (decl))))
24138 return cond;
24139
24140 return build_x_binary_op (TREE_CODE (cond),
24141 TREE_OPERAND (cond, 0), ERROR_MARK,
24142 TREE_OPERAND (cond, 1), ERROR_MARK,
24143 &overloaded_p, tf_warning_or_error);
24144 }
24145
24146 /* Helper function, to parse omp for increment expression. */
24147
24148 static tree
24149 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24150 {
24151 cp_token *token = cp_lexer_peek_token (parser->lexer);
24152 enum tree_code op;
24153 tree lhs, rhs;
24154 cp_id_kind idk;
24155 bool decl_first;
24156
24157 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24158 {
24159 op = (token->type == CPP_PLUS_PLUS
24160 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24161 cp_lexer_consume_token (parser->lexer);
24162 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24163 if (lhs != decl)
24164 return error_mark_node;
24165 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24166 }
24167
24168 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24169 if (lhs != decl)
24170 return error_mark_node;
24171
24172 token = cp_lexer_peek_token (parser->lexer);
24173 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24174 {
24175 op = (token->type == CPP_PLUS_PLUS
24176 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24177 cp_lexer_consume_token (parser->lexer);
24178 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24179 }
24180
24181 op = cp_parser_assignment_operator_opt (parser);
24182 if (op == ERROR_MARK)
24183 return error_mark_node;
24184
24185 if (op != NOP_EXPR)
24186 {
24187 rhs = cp_parser_assignment_expression (parser, false, NULL);
24188 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24189 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24190 }
24191
24192 lhs = cp_parser_binary_expression (parser, false, false,
24193 PREC_ADDITIVE_EXPRESSION, NULL);
24194 token = cp_lexer_peek_token (parser->lexer);
24195 decl_first = lhs == decl;
24196 if (decl_first)
24197 lhs = NULL_TREE;
24198 if (token->type != CPP_PLUS
24199 && token->type != CPP_MINUS)
24200 return error_mark_node;
24201
24202 do
24203 {
24204 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24205 cp_lexer_consume_token (parser->lexer);
24206 rhs = cp_parser_binary_expression (parser, false, false,
24207 PREC_ADDITIVE_EXPRESSION, NULL);
24208 token = cp_lexer_peek_token (parser->lexer);
24209 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24210 {
24211 if (lhs == NULL_TREE)
24212 {
24213 if (op == PLUS_EXPR)
24214 lhs = rhs;
24215 else
24216 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24217 }
24218 else
24219 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24220 NULL, tf_warning_or_error);
24221 }
24222 }
24223 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24224
24225 if (!decl_first)
24226 {
24227 if (rhs != decl || op == MINUS_EXPR)
24228 return error_mark_node;
24229 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24230 }
24231 else
24232 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24233
24234 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24235 }
24236
24237 /* Parse the restricted form of the for statement allowed by OpenMP. */
24238
24239 static tree
24240 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24241 {
24242 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24243 tree real_decl, initv, condv, incrv, declv;
24244 tree this_pre_body, cl;
24245 location_t loc_first;
24246 bool collapse_err = false;
24247 int i, collapse = 1, nbraces = 0;
24248 VEC(tree,gc) *for_block = make_tree_vector ();
24249
24250 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24251 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24252 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24253
24254 gcc_assert (collapse >= 1);
24255
24256 declv = make_tree_vec (collapse);
24257 initv = make_tree_vec (collapse);
24258 condv = make_tree_vec (collapse);
24259 incrv = make_tree_vec (collapse);
24260
24261 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24262
24263 for (i = 0; i < collapse; i++)
24264 {
24265 int bracecount = 0;
24266 bool add_private_clause = false;
24267 location_t loc;
24268
24269 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24270 {
24271 cp_parser_error (parser, "for statement expected");
24272 return NULL;
24273 }
24274 loc = cp_lexer_consume_token (parser->lexer)->location;
24275
24276 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24277 return NULL;
24278
24279 init = decl = real_decl = NULL;
24280 this_pre_body = push_stmt_list ();
24281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24282 {
24283 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24284
24285 init-expr:
24286 var = lb
24287 integer-type var = lb
24288 random-access-iterator-type var = lb
24289 pointer-type var = lb
24290 */
24291 cp_decl_specifier_seq type_specifiers;
24292
24293 /* First, try to parse as an initialized declaration. See
24294 cp_parser_condition, from whence the bulk of this is copied. */
24295
24296 cp_parser_parse_tentatively (parser);
24297 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24298 /*is_trailing_return=*/false,
24299 &type_specifiers);
24300 if (cp_parser_parse_definitely (parser))
24301 {
24302 /* If parsing a type specifier seq succeeded, then this
24303 MUST be a initialized declaration. */
24304 tree asm_specification, attributes;
24305 cp_declarator *declarator;
24306
24307 declarator = cp_parser_declarator (parser,
24308 CP_PARSER_DECLARATOR_NAMED,
24309 /*ctor_dtor_or_conv_p=*/NULL,
24310 /*parenthesized_p=*/NULL,
24311 /*member_p=*/false);
24312 attributes = cp_parser_attributes_opt (parser);
24313 asm_specification = cp_parser_asm_specification_opt (parser);
24314
24315 if (declarator == cp_error_declarator)
24316 cp_parser_skip_to_end_of_statement (parser);
24317
24318 else
24319 {
24320 tree pushed_scope, auto_node;
24321
24322 decl = start_decl (declarator, &type_specifiers,
24323 SD_INITIALIZED, attributes,
24324 /*prefix_attributes=*/NULL_TREE,
24325 &pushed_scope);
24326
24327 auto_node = type_uses_auto (TREE_TYPE (decl));
24328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24329 {
24330 if (cp_lexer_next_token_is (parser->lexer,
24331 CPP_OPEN_PAREN))
24332 error ("parenthesized initialization is not allowed in "
24333 "OpenMP %<for%> loop");
24334 else
24335 /* Trigger an error. */
24336 cp_parser_require (parser, CPP_EQ, RT_EQ);
24337
24338 init = error_mark_node;
24339 cp_parser_skip_to_end_of_statement (parser);
24340 }
24341 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24342 || type_dependent_expression_p (decl)
24343 || auto_node)
24344 {
24345 bool is_direct_init, is_non_constant_init;
24346
24347 init = cp_parser_initializer (parser,
24348 &is_direct_init,
24349 &is_non_constant_init);
24350
24351 if (auto_node && describable_type (init))
24352 {
24353 TREE_TYPE (decl)
24354 = do_auto_deduction (TREE_TYPE (decl), init,
24355 auto_node);
24356
24357 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24358 && !type_dependent_expression_p (decl))
24359 goto non_class;
24360 }
24361
24362 cp_finish_decl (decl, init, !is_non_constant_init,
24363 asm_specification,
24364 LOOKUP_ONLYCONVERTING);
24365 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24366 {
24367 VEC_safe_push (tree, gc, for_block, this_pre_body);
24368 init = NULL_TREE;
24369 }
24370 else
24371 init = pop_stmt_list (this_pre_body);
24372 this_pre_body = NULL_TREE;
24373 }
24374 else
24375 {
24376 /* Consume '='. */
24377 cp_lexer_consume_token (parser->lexer);
24378 init = cp_parser_assignment_expression (parser, false, NULL);
24379
24380 non_class:
24381 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24382 init = error_mark_node;
24383 else
24384 cp_finish_decl (decl, NULL_TREE,
24385 /*init_const_expr_p=*/false,
24386 asm_specification,
24387 LOOKUP_ONLYCONVERTING);
24388 }
24389
24390 if (pushed_scope)
24391 pop_scope (pushed_scope);
24392 }
24393 }
24394 else
24395 {
24396 cp_id_kind idk;
24397 /* If parsing a type specifier sequence failed, then
24398 this MUST be a simple expression. */
24399 cp_parser_parse_tentatively (parser);
24400 decl = cp_parser_primary_expression (parser, false, false,
24401 false, &idk);
24402 if (!cp_parser_error_occurred (parser)
24403 && decl
24404 && DECL_P (decl)
24405 && CLASS_TYPE_P (TREE_TYPE (decl)))
24406 {
24407 tree rhs;
24408
24409 cp_parser_parse_definitely (parser);
24410 cp_parser_require (parser, CPP_EQ, RT_EQ);
24411 rhs = cp_parser_assignment_expression (parser, false, NULL);
24412 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24413 rhs,
24414 tf_warning_or_error));
24415 add_private_clause = true;
24416 }
24417 else
24418 {
24419 decl = NULL;
24420 cp_parser_abort_tentative_parse (parser);
24421 init = cp_parser_expression (parser, false, NULL);
24422 if (init)
24423 {
24424 if (TREE_CODE (init) == MODIFY_EXPR
24425 || TREE_CODE (init) == MODOP_EXPR)
24426 real_decl = TREE_OPERAND (init, 0);
24427 }
24428 }
24429 }
24430 }
24431 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24432 if (this_pre_body)
24433 {
24434 this_pre_body = pop_stmt_list (this_pre_body);
24435 if (pre_body)
24436 {
24437 tree t = pre_body;
24438 pre_body = push_stmt_list ();
24439 add_stmt (t);
24440 add_stmt (this_pre_body);
24441 pre_body = pop_stmt_list (pre_body);
24442 }
24443 else
24444 pre_body = this_pre_body;
24445 }
24446
24447 if (decl)
24448 real_decl = decl;
24449 if (par_clauses != NULL && real_decl != NULL_TREE)
24450 {
24451 tree *c;
24452 for (c = par_clauses; *c ; )
24453 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24454 && OMP_CLAUSE_DECL (*c) == real_decl)
24455 {
24456 error_at (loc, "iteration variable %qD"
24457 " should not be firstprivate", real_decl);
24458 *c = OMP_CLAUSE_CHAIN (*c);
24459 }
24460 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24461 && OMP_CLAUSE_DECL (*c) == real_decl)
24462 {
24463 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24464 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24465 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24466 OMP_CLAUSE_DECL (l) = real_decl;
24467 OMP_CLAUSE_CHAIN (l) = clauses;
24468 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24469 clauses = l;
24470 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24471 CP_OMP_CLAUSE_INFO (*c) = NULL;
24472 add_private_clause = false;
24473 }
24474 else
24475 {
24476 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24477 && OMP_CLAUSE_DECL (*c) == real_decl)
24478 add_private_clause = false;
24479 c = &OMP_CLAUSE_CHAIN (*c);
24480 }
24481 }
24482
24483 if (add_private_clause)
24484 {
24485 tree c;
24486 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24487 {
24488 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24489 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24490 && OMP_CLAUSE_DECL (c) == decl)
24491 break;
24492 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24493 && OMP_CLAUSE_DECL (c) == decl)
24494 error_at (loc, "iteration variable %qD "
24495 "should not be firstprivate",
24496 decl);
24497 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24498 && OMP_CLAUSE_DECL (c) == decl)
24499 error_at (loc, "iteration variable %qD should not be reduction",
24500 decl);
24501 }
24502 if (c == NULL)
24503 {
24504 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24505 OMP_CLAUSE_DECL (c) = decl;
24506 c = finish_omp_clauses (c);
24507 if (c)
24508 {
24509 OMP_CLAUSE_CHAIN (c) = clauses;
24510 clauses = c;
24511 }
24512 }
24513 }
24514
24515 cond = NULL;
24516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24517 cond = cp_parser_omp_for_cond (parser, decl);
24518 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24519
24520 incr = NULL;
24521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24522 {
24523 /* If decl is an iterator, preserve the operator on decl
24524 until finish_omp_for. */
24525 if (decl
24526 && ((type_dependent_expression_p (decl)
24527 && !POINTER_TYPE_P (TREE_TYPE (decl)))
24528 || CLASS_TYPE_P (TREE_TYPE (decl))))
24529 incr = cp_parser_omp_for_incr (parser, decl);
24530 else
24531 incr = cp_parser_expression (parser, false, NULL);
24532 }
24533
24534 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24535 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24536 /*or_comma=*/false,
24537 /*consume_paren=*/true);
24538
24539 TREE_VEC_ELT (declv, i) = decl;
24540 TREE_VEC_ELT (initv, i) = init;
24541 TREE_VEC_ELT (condv, i) = cond;
24542 TREE_VEC_ELT (incrv, i) = incr;
24543
24544 if (i == collapse - 1)
24545 break;
24546
24547 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24548 in between the collapsed for loops to be still considered perfectly
24549 nested. Hopefully the final version clarifies this.
24550 For now handle (multiple) {'s and empty statements. */
24551 cp_parser_parse_tentatively (parser);
24552 do
24553 {
24554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24555 break;
24556 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24557 {
24558 cp_lexer_consume_token (parser->lexer);
24559 bracecount++;
24560 }
24561 else if (bracecount
24562 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24563 cp_lexer_consume_token (parser->lexer);
24564 else
24565 {
24566 loc = cp_lexer_peek_token (parser->lexer)->location;
24567 error_at (loc, "not enough collapsed for loops");
24568 collapse_err = true;
24569 cp_parser_abort_tentative_parse (parser);
24570 declv = NULL_TREE;
24571 break;
24572 }
24573 }
24574 while (1);
24575
24576 if (declv)
24577 {
24578 cp_parser_parse_definitely (parser);
24579 nbraces += bracecount;
24580 }
24581 }
24582
24583 /* Note that we saved the original contents of this flag when we entered
24584 the structured block, and so we don't need to re-save it here. */
24585 parser->in_statement = IN_OMP_FOR;
24586
24587 /* Note that the grammar doesn't call for a structured block here,
24588 though the loop as a whole is a structured block. */
24589 body = push_stmt_list ();
24590 cp_parser_statement (parser, NULL_TREE, false, NULL);
24591 body = pop_stmt_list (body);
24592
24593 if (declv == NULL_TREE)
24594 ret = NULL_TREE;
24595 else
24596 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24597 pre_body, clauses);
24598
24599 while (nbraces)
24600 {
24601 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24602 {
24603 cp_lexer_consume_token (parser->lexer);
24604 nbraces--;
24605 }
24606 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24607 cp_lexer_consume_token (parser->lexer);
24608 else
24609 {
24610 if (!collapse_err)
24611 {
24612 error_at (cp_lexer_peek_token (parser->lexer)->location,
24613 "collapsed loops not perfectly nested");
24614 }
24615 collapse_err = true;
24616 cp_parser_statement_seq_opt (parser, NULL);
24617 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24618 break;
24619 }
24620 }
24621
24622 while (!VEC_empty (tree, for_block))
24623 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24624 release_tree_vector (for_block);
24625
24626 return ret;
24627 }
24628
24629 /* OpenMP 2.5:
24630 #pragma omp for for-clause[optseq] new-line
24631 for-loop */
24632
24633 #define OMP_FOR_CLAUSE_MASK \
24634 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24635 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24636 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24637 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24638 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24639 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24640 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24641 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24642
24643 static tree
24644 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24645 {
24646 tree clauses, sb, ret;
24647 unsigned int save;
24648
24649 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24650 "#pragma omp for", pragma_tok);
24651
24652 sb = begin_omp_structured_block ();
24653 save = cp_parser_begin_omp_structured_block (parser);
24654
24655 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24656
24657 cp_parser_end_omp_structured_block (parser, save);
24658 add_stmt (finish_omp_structured_block (sb));
24659
24660 return ret;
24661 }
24662
24663 /* OpenMP 2.5:
24664 # pragma omp master new-line
24665 structured-block */
24666
24667 static tree
24668 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24669 {
24670 cp_parser_require_pragma_eol (parser, pragma_tok);
24671 return c_finish_omp_master (input_location,
24672 cp_parser_omp_structured_block (parser));
24673 }
24674
24675 /* OpenMP 2.5:
24676 # pragma omp ordered new-line
24677 structured-block */
24678
24679 static tree
24680 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24681 {
24682 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24683 cp_parser_require_pragma_eol (parser, pragma_tok);
24684 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24685 }
24686
24687 /* OpenMP 2.5:
24688
24689 section-scope:
24690 { section-sequence }
24691
24692 section-sequence:
24693 section-directive[opt] structured-block
24694 section-sequence section-directive structured-block */
24695
24696 static tree
24697 cp_parser_omp_sections_scope (cp_parser *parser)
24698 {
24699 tree stmt, substmt;
24700 bool error_suppress = false;
24701 cp_token *tok;
24702
24703 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24704 return NULL_TREE;
24705
24706 stmt = push_stmt_list ();
24707
24708 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24709 {
24710 unsigned save;
24711
24712 substmt = begin_omp_structured_block ();
24713 save = cp_parser_begin_omp_structured_block (parser);
24714
24715 while (1)
24716 {
24717 cp_parser_statement (parser, NULL_TREE, false, NULL);
24718
24719 tok = cp_lexer_peek_token (parser->lexer);
24720 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24721 break;
24722 if (tok->type == CPP_CLOSE_BRACE)
24723 break;
24724 if (tok->type == CPP_EOF)
24725 break;
24726 }
24727
24728 cp_parser_end_omp_structured_block (parser, save);
24729 substmt = finish_omp_structured_block (substmt);
24730 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24731 add_stmt (substmt);
24732 }
24733
24734 while (1)
24735 {
24736 tok = cp_lexer_peek_token (parser->lexer);
24737 if (tok->type == CPP_CLOSE_BRACE)
24738 break;
24739 if (tok->type == CPP_EOF)
24740 break;
24741
24742 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24743 {
24744 cp_lexer_consume_token (parser->lexer);
24745 cp_parser_require_pragma_eol (parser, tok);
24746 error_suppress = false;
24747 }
24748 else if (!error_suppress)
24749 {
24750 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24751 error_suppress = true;
24752 }
24753
24754 substmt = cp_parser_omp_structured_block (parser);
24755 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24756 add_stmt (substmt);
24757 }
24758 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24759
24760 substmt = pop_stmt_list (stmt);
24761
24762 stmt = make_node (OMP_SECTIONS);
24763 TREE_TYPE (stmt) = void_type_node;
24764 OMP_SECTIONS_BODY (stmt) = substmt;
24765
24766 add_stmt (stmt);
24767 return stmt;
24768 }
24769
24770 /* OpenMP 2.5:
24771 # pragma omp sections sections-clause[optseq] newline
24772 sections-scope */
24773
24774 #define OMP_SECTIONS_CLAUSE_MASK \
24775 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24776 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24777 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24778 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24779 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24780
24781 static tree
24782 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24783 {
24784 tree clauses, ret;
24785
24786 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24787 "#pragma omp sections", pragma_tok);
24788
24789 ret = cp_parser_omp_sections_scope (parser);
24790 if (ret)
24791 OMP_SECTIONS_CLAUSES (ret) = clauses;
24792
24793 return ret;
24794 }
24795
24796 /* OpenMP 2.5:
24797 # pragma parallel parallel-clause new-line
24798 # pragma parallel for parallel-for-clause new-line
24799 # pragma parallel sections parallel-sections-clause new-line */
24800
24801 #define OMP_PARALLEL_CLAUSE_MASK \
24802 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24803 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24804 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24805 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24806 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24807 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24808 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24809 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24810
24811 static tree
24812 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24813 {
24814 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24815 const char *p_name = "#pragma omp parallel";
24816 tree stmt, clauses, par_clause, ws_clause, block;
24817 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24818 unsigned int save;
24819 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24820
24821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24822 {
24823 cp_lexer_consume_token (parser->lexer);
24824 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24825 p_name = "#pragma omp parallel for";
24826 mask |= OMP_FOR_CLAUSE_MASK;
24827 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24828 }
24829 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24830 {
24831 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24832 const char *p = IDENTIFIER_POINTER (id);
24833 if (strcmp (p, "sections") == 0)
24834 {
24835 cp_lexer_consume_token (parser->lexer);
24836 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24837 p_name = "#pragma omp parallel sections";
24838 mask |= OMP_SECTIONS_CLAUSE_MASK;
24839 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24840 }
24841 }
24842
24843 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24844 block = begin_omp_parallel ();
24845 save = cp_parser_begin_omp_structured_block (parser);
24846
24847 switch (p_kind)
24848 {
24849 case PRAGMA_OMP_PARALLEL:
24850 cp_parser_statement (parser, NULL_TREE, false, NULL);
24851 par_clause = clauses;
24852 break;
24853
24854 case PRAGMA_OMP_PARALLEL_FOR:
24855 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24856 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24857 break;
24858
24859 case PRAGMA_OMP_PARALLEL_SECTIONS:
24860 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24861 stmt = cp_parser_omp_sections_scope (parser);
24862 if (stmt)
24863 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24864 break;
24865
24866 default:
24867 gcc_unreachable ();
24868 }
24869
24870 cp_parser_end_omp_structured_block (parser, save);
24871 stmt = finish_omp_parallel (par_clause, block);
24872 if (p_kind != PRAGMA_OMP_PARALLEL)
24873 OMP_PARALLEL_COMBINED (stmt) = 1;
24874 return stmt;
24875 }
24876
24877 /* OpenMP 2.5:
24878 # pragma omp single single-clause[optseq] new-line
24879 structured-block */
24880
24881 #define OMP_SINGLE_CLAUSE_MASK \
24882 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24883 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24884 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24885 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24886
24887 static tree
24888 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24889 {
24890 tree stmt = make_node (OMP_SINGLE);
24891 TREE_TYPE (stmt) = void_type_node;
24892
24893 OMP_SINGLE_CLAUSES (stmt)
24894 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24895 "#pragma omp single", pragma_tok);
24896 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24897
24898 return add_stmt (stmt);
24899 }
24900
24901 /* OpenMP 3.0:
24902 # pragma omp task task-clause[optseq] new-line
24903 structured-block */
24904
24905 #define OMP_TASK_CLAUSE_MASK \
24906 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24907 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24908 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24909 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24910 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24911 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24912
24913 static tree
24914 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24915 {
24916 tree clauses, block;
24917 unsigned int save;
24918
24919 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24920 "#pragma omp task", pragma_tok);
24921 block = begin_omp_task ();
24922 save = cp_parser_begin_omp_structured_block (parser);
24923 cp_parser_statement (parser, NULL_TREE, false, NULL);
24924 cp_parser_end_omp_structured_block (parser, save);
24925 return finish_omp_task (clauses, block);
24926 }
24927
24928 /* OpenMP 3.0:
24929 # pragma omp taskwait new-line */
24930
24931 static void
24932 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24933 {
24934 cp_parser_require_pragma_eol (parser, pragma_tok);
24935 finish_omp_taskwait ();
24936 }
24937
24938 /* OpenMP 2.5:
24939 # pragma omp threadprivate (variable-list) */
24940
24941 static void
24942 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24943 {
24944 tree vars;
24945
24946 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24947 cp_parser_require_pragma_eol (parser, pragma_tok);
24948
24949 finish_omp_threadprivate (vars);
24950 }
24951
24952 /* Main entry point to OpenMP statement pragmas. */
24953
24954 static void
24955 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24956 {
24957 tree stmt;
24958
24959 switch (pragma_tok->pragma_kind)
24960 {
24961 case PRAGMA_OMP_ATOMIC:
24962 cp_parser_omp_atomic (parser, pragma_tok);
24963 return;
24964 case PRAGMA_OMP_CRITICAL:
24965 stmt = cp_parser_omp_critical (parser, pragma_tok);
24966 break;
24967 case PRAGMA_OMP_FOR:
24968 stmt = cp_parser_omp_for (parser, pragma_tok);
24969 break;
24970 case PRAGMA_OMP_MASTER:
24971 stmt = cp_parser_omp_master (parser, pragma_tok);
24972 break;
24973 case PRAGMA_OMP_ORDERED:
24974 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24975 break;
24976 case PRAGMA_OMP_PARALLEL:
24977 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24978 break;
24979 case PRAGMA_OMP_SECTIONS:
24980 stmt = cp_parser_omp_sections (parser, pragma_tok);
24981 break;
24982 case PRAGMA_OMP_SINGLE:
24983 stmt = cp_parser_omp_single (parser, pragma_tok);
24984 break;
24985 case PRAGMA_OMP_TASK:
24986 stmt = cp_parser_omp_task (parser, pragma_tok);
24987 break;
24988 default:
24989 gcc_unreachable ();
24990 }
24991
24992 if (stmt)
24993 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24994 }
24995 \f
24996 /* The parser. */
24997
24998 static GTY (()) cp_parser *the_parser;
24999
25000 \f
25001 /* Special handling for the first token or line in the file. The first
25002 thing in the file might be #pragma GCC pch_preprocess, which loads a
25003 PCH file, which is a GC collection point. So we need to handle this
25004 first pragma without benefit of an existing lexer structure.
25005
25006 Always returns one token to the caller in *FIRST_TOKEN. This is
25007 either the true first token of the file, or the first token after
25008 the initial pragma. */
25009
25010 static void
25011 cp_parser_initial_pragma (cp_token *first_token)
25012 {
25013 tree name = NULL;
25014
25015 cp_lexer_get_preprocessor_token (NULL, first_token);
25016 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25017 return;
25018
25019 cp_lexer_get_preprocessor_token (NULL, first_token);
25020 if (first_token->type == CPP_STRING)
25021 {
25022 name = first_token->u.value;
25023
25024 cp_lexer_get_preprocessor_token (NULL, first_token);
25025 if (first_token->type != CPP_PRAGMA_EOL)
25026 error_at (first_token->location,
25027 "junk at end of %<#pragma GCC pch_preprocess%>");
25028 }
25029 else
25030 error_at (first_token->location, "expected string literal");
25031
25032 /* Skip to the end of the pragma. */
25033 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25034 cp_lexer_get_preprocessor_token (NULL, first_token);
25035
25036 /* Now actually load the PCH file. */
25037 if (name)
25038 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25039
25040 /* Read one more token to return to our caller. We have to do this
25041 after reading the PCH file in, since its pointers have to be
25042 live. */
25043 cp_lexer_get_preprocessor_token (NULL, first_token);
25044 }
25045
25046 /* Normal parsing of a pragma token. Here we can (and must) use the
25047 regular lexer. */
25048
25049 static bool
25050 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25051 {
25052 cp_token *pragma_tok;
25053 unsigned int id;
25054
25055 pragma_tok = cp_lexer_consume_token (parser->lexer);
25056 gcc_assert (pragma_tok->type == CPP_PRAGMA);
25057 parser->lexer->in_pragma = true;
25058
25059 id = pragma_tok->pragma_kind;
25060 switch (id)
25061 {
25062 case PRAGMA_GCC_PCH_PREPROCESS:
25063 error_at (pragma_tok->location,
25064 "%<#pragma GCC pch_preprocess%> must be first");
25065 break;
25066
25067 case PRAGMA_OMP_BARRIER:
25068 switch (context)
25069 {
25070 case pragma_compound:
25071 cp_parser_omp_barrier (parser, pragma_tok);
25072 return false;
25073 case pragma_stmt:
25074 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25075 "used in compound statements");
25076 break;
25077 default:
25078 goto bad_stmt;
25079 }
25080 break;
25081
25082 case PRAGMA_OMP_FLUSH:
25083 switch (context)
25084 {
25085 case pragma_compound:
25086 cp_parser_omp_flush (parser, pragma_tok);
25087 return false;
25088 case pragma_stmt:
25089 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25090 "used in compound statements");
25091 break;
25092 default:
25093 goto bad_stmt;
25094 }
25095 break;
25096
25097 case PRAGMA_OMP_TASKWAIT:
25098 switch (context)
25099 {
25100 case pragma_compound:
25101 cp_parser_omp_taskwait (parser, pragma_tok);
25102 return false;
25103 case pragma_stmt:
25104 error_at (pragma_tok->location,
25105 "%<#pragma omp taskwait%> may only be "
25106 "used in compound statements");
25107 break;
25108 default:
25109 goto bad_stmt;
25110 }
25111 break;
25112
25113 case PRAGMA_OMP_THREADPRIVATE:
25114 cp_parser_omp_threadprivate (parser, pragma_tok);
25115 return false;
25116
25117 case PRAGMA_OMP_ATOMIC:
25118 case PRAGMA_OMP_CRITICAL:
25119 case PRAGMA_OMP_FOR:
25120 case PRAGMA_OMP_MASTER:
25121 case PRAGMA_OMP_ORDERED:
25122 case PRAGMA_OMP_PARALLEL:
25123 case PRAGMA_OMP_SECTIONS:
25124 case PRAGMA_OMP_SINGLE:
25125 case PRAGMA_OMP_TASK:
25126 if (context == pragma_external)
25127 goto bad_stmt;
25128 cp_parser_omp_construct (parser, pragma_tok);
25129 return true;
25130
25131 case PRAGMA_OMP_SECTION:
25132 error_at (pragma_tok->location,
25133 "%<#pragma omp section%> may only be used in "
25134 "%<#pragma omp sections%> construct");
25135 break;
25136
25137 default:
25138 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25139 c_invoke_pragma_handler (id);
25140 break;
25141
25142 bad_stmt:
25143 cp_parser_error (parser, "expected declaration specifiers");
25144 break;
25145 }
25146
25147 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25148 return false;
25149 }
25150
25151 /* The interface the pragma parsers have to the lexer. */
25152
25153 enum cpp_ttype
25154 pragma_lex (tree *value)
25155 {
25156 cp_token *tok;
25157 enum cpp_ttype ret;
25158
25159 tok = cp_lexer_peek_token (the_parser->lexer);
25160
25161 ret = tok->type;
25162 *value = tok->u.value;
25163
25164 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25165 ret = CPP_EOF;
25166 else if (ret == CPP_STRING)
25167 *value = cp_parser_string_literal (the_parser, false, false);
25168 else
25169 {
25170 cp_lexer_consume_token (the_parser->lexer);
25171 if (ret == CPP_KEYWORD)
25172 ret = CPP_NAME;
25173 }
25174
25175 return ret;
25176 }
25177
25178 \f
25179 /* External interface. */
25180
25181 /* Parse one entire translation unit. */
25182
25183 void
25184 c_parse_file (void)
25185 {
25186 static bool already_called = false;
25187
25188 if (already_called)
25189 {
25190 sorry ("inter-module optimizations not implemented for C++");
25191 return;
25192 }
25193 already_called = true;
25194
25195 the_parser = cp_parser_new ();
25196 push_deferring_access_checks (flag_access_control
25197 ? dk_no_deferred : dk_no_check);
25198 cp_parser_translation_unit (the_parser);
25199 the_parser = NULL;
25200 }
25201
25202 #include "gt-cp-parser.h"