4e085abdf84ea622f356c459ffa555a1e6534cc0
[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 "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer. */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
49
50 static cp_token eof_token =
51 {
52 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57 NIC_NONE,
58 /* floating-point literal */
59 NIC_FLOAT,
60 /* %<this%> */
61 NIC_THIS,
62 /* %<__FUNCTION__%> */
63 NIC_FUNC_NAME,
64 /* %<__PRETTY_FUNCTION__%> */
65 NIC_PRETTY_FUNC,
66 /* %<__func__%> */
67 NIC_C99_FUNC,
68 /* "%<va_arg%> */
69 NIC_VA_ARG,
70 /* a cast */
71 NIC_CAST,
72 /* %<typeid%> operator */
73 NIC_TYPEID,
74 /* non-constant compound literals */
75 NIC_NCC,
76 /* a function call */
77 NIC_FUNC_CALL,
78 /* an increment */
79 NIC_INC,
80 /* an decrement */
81 NIC_DEC,
82 /* an array reference */
83 NIC_ARRAY_REF,
84 /* %<->%> */
85 NIC_ARROW,
86 /* %<.%> */
87 NIC_POINT,
88 /* the address of a label */
89 NIC_ADDR_LABEL,
90 /* %<*%> */
91 NIC_STAR,
92 /* %<&%> */
93 NIC_ADDR,
94 /* %<++%> */
95 NIC_PREINCREMENT,
96 /* %<--%> */
97 NIC_PREDECREMENT,
98 /* %<new%> */
99 NIC_NEW,
100 /* %<delete%> */
101 NIC_DEL,
102 /* calls to overloaded operators */
103 NIC_OVERLOADED,
104 /* an assignment */
105 NIC_ASSIGNMENT,
106 /* a comma operator */
107 NIC_COMMA,
108 /* a call to a constructor */
109 NIC_CONSTRUCTOR
110 } non_integral_constant;
111
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114 /* NULL */
115 NLE_NULL,
116 /* is not a type */
117 NLE_TYPE,
118 /* is not a class or namespace */
119 NLE_CXX98,
120 /* is not a class, namespace, or enumeration */
121 NLE_NOT_CXX98
122 } name_lookup_error;
123
124 /* The various kinds of required token */
125 typedef enum required_token {
126 RT_NONE,
127 RT_SEMICOLON, /* ';' */
128 RT_OPEN_PAREN, /* '(' */
129 RT_CLOSE_BRACE, /* '}' */
130 RT_OPEN_BRACE, /* '{' */
131 RT_CLOSE_SQUARE, /* ']' */
132 RT_OPEN_SQUARE, /* '[' */
133 RT_COMMA, /* ',' */
134 RT_SCOPE, /* '::' */
135 RT_LESS, /* '<' */
136 RT_GREATER, /* '>' */
137 RT_EQ, /* '=' */
138 RT_ELLIPSIS, /* '...' */
139 RT_MULT, /* '*' */
140 RT_COMPL, /* '~' */
141 RT_COLON, /* ':' */
142 RT_COLON_SCOPE, /* ':' or '::' */
143 RT_CLOSE_PAREN, /* ')' */
144 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145 RT_PRAGMA_EOL, /* end of line */
146 RT_NAME, /* identifier */
147
148 /* The type is CPP_KEYWORD */
149 RT_NEW, /* new */
150 RT_DELETE, /* delete */
151 RT_RETURN, /* return */
152 RT_WHILE, /* while */
153 RT_EXTERN, /* extern */
154 RT_STATIC_ASSERT, /* static_assert */
155 RT_DECLTYPE, /* decltype */
156 RT_OPERATOR, /* operator */
157 RT_CLASS, /* class */
158 RT_TEMPLATE, /* template */
159 RT_NAMESPACE, /* namespace */
160 RT_USING, /* using */
161 RT_ASM, /* asm */
162 RT_TRY, /* try */
163 RT_CATCH, /* catch */
164 RT_THROW, /* throw */
165 RT_LABEL, /* __label__ */
166 RT_AT_TRY, /* @try */
167 RT_AT_SYNCHRONIZED, /* @synchronized */
168 RT_AT_THROW, /* @throw */
169
170 RT_SELECT, /* selection-statement */
171 RT_INTERATION, /* iteration-statement */
172 RT_JUMP, /* jump-statement */
173 RT_CLASS_KEY, /* class-key */
174 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
175 } required_token;
176
177 /* Prototypes. */
178
179 static cp_lexer *cp_lexer_new_main
180 (void);
181 static cp_lexer *cp_lexer_new_from_tokens
182 (cp_token_cache *tokens);
183 static void cp_lexer_destroy
184 (cp_lexer *);
185 static int cp_lexer_saving_tokens
186 (const cp_lexer *);
187 static cp_token *cp_lexer_token_at
188 (cp_lexer *, cp_token_position);
189 static void cp_lexer_get_preprocessor_token
190 (cp_lexer *, cp_token *);
191 static inline cp_token *cp_lexer_peek_token
192 (cp_lexer *);
193 static cp_token *cp_lexer_peek_nth_token
194 (cp_lexer *, size_t);
195 static inline bool cp_lexer_next_token_is
196 (cp_lexer *, enum cpp_ttype);
197 static bool cp_lexer_next_token_is_not
198 (cp_lexer *, enum cpp_ttype);
199 static bool cp_lexer_next_token_is_keyword
200 (cp_lexer *, enum rid);
201 static cp_token *cp_lexer_consume_token
202 (cp_lexer *);
203 static void cp_lexer_purge_token
204 (cp_lexer *);
205 static void cp_lexer_purge_tokens_after
206 (cp_lexer *, cp_token_position);
207 static void cp_lexer_save_tokens
208 (cp_lexer *);
209 static void cp_lexer_commit_tokens
210 (cp_lexer *);
211 static void cp_lexer_rollback_tokens
212 (cp_lexer *);
213 #ifdef ENABLE_CHECKING
214 static void cp_lexer_print_token
215 (FILE *, cp_token *);
216 static inline bool cp_lexer_debugging_p
217 (cp_lexer *);
218 static void cp_lexer_start_debugging
219 (cp_lexer *) ATTRIBUTE_UNUSED;
220 static void cp_lexer_stop_debugging
221 (cp_lexer *) ATTRIBUTE_UNUSED;
222 #else
223 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
224 about passing NULL to functions that require non-NULL arguments
225 (fputs, fprintf). It will never be used, so all we need is a value
226 of the right type that's guaranteed not to be NULL. */
227 #define cp_lexer_debug_stream stdout
228 #define cp_lexer_print_token(str, tok) (void) 0
229 #define cp_lexer_debugging_p(lexer) 0
230 #endif /* ENABLE_CHECKING */
231
232 static cp_token_cache *cp_token_cache_new
233 (cp_token *, cp_token *);
234
235 static void cp_parser_initial_pragma
236 (cp_token *);
237
238 /* Manifest constants. */
239 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
240 #define CP_SAVED_TOKEN_STACK 5
241
242 /* Variables. */
243
244 #ifdef ENABLE_CHECKING
245 /* The stream to which debugging output should be written. */
246 static FILE *cp_lexer_debug_stream;
247 #endif /* ENABLE_CHECKING */
248
249 /* Nonzero if we are parsing an unevaluated operand: an operand to
250 sizeof, typeof, or alignof. */
251 int cp_unevaluated_operand;
252
253 #ifdef ENABLE_CHECKING
254 /* Dump up to NUM tokens in BUFFER to FILE. If NUM is 0, dump all the
255 tokens. */
256
257 void
258 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
259 {
260 unsigned i;
261 cp_token *token;
262
263 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264
265 if (num == 0)
266 num = VEC_length (cp_token, buffer);
267
268 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
269 {
270 cp_lexer_print_token (file, token);
271 switch (token->type)
272 {
273 case CPP_SEMICOLON:
274 case CPP_OPEN_BRACE:
275 case CPP_CLOSE_BRACE:
276 case CPP_EOF:
277 fputc ('\n', file);
278 break;
279
280 default:
281 fputc (' ', file);
282 }
283 }
284
285 if (i == num && i < VEC_length (cp_token, buffer))
286 {
287 fprintf (file, " ... ");
288 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
289 VEC_length (cp_token, buffer) - 1));
290 }
291
292 fprintf (file, "\n");
293 }
294
295
296 /* Dump all tokens in BUFFER to stderr. */
297
298 void
299 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
300 {
301 cp_lexer_dump_tokens (stderr, buffer, 0);
302 }
303 #endif
304
305
306 /* Allocate memory for a new lexer object and return it. */
307
308 static cp_lexer *
309 cp_lexer_alloc (void)
310 {
311 cp_lexer *lexer;
312
313 c_common_no_more_pch ();
314
315 /* Allocate the memory. */
316 lexer = ggc_alloc_cleared_cp_lexer ();
317
318 #ifdef ENABLE_CHECKING
319 /* Initially we are not debugging. */
320 lexer->debugging_p = false;
321 #endif /* ENABLE_CHECKING */
322 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
323 CP_SAVED_TOKEN_STACK);
324
325 /* Create the buffer. */
326 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
327
328 return lexer;
329 }
330
331
332 /* Create a new main C++ lexer, the lexer that gets tokens from the
333 preprocessor. */
334
335 static cp_lexer *
336 cp_lexer_new_main (void)
337 {
338 cp_lexer *lexer;
339 cp_token token;
340
341 /* It's possible that parsing the first pragma will load a PCH file,
342 which is a GC collection point. So we have to do that before
343 allocating any memory. */
344 cp_parser_initial_pragma (&token);
345
346 lexer = cp_lexer_alloc ();
347
348 /* Put the first token in the buffer. */
349 VEC_quick_push (cp_token, lexer->buffer, &token);
350
351 /* Get the remaining tokens from the preprocessor. */
352 while (token.type != CPP_EOF)
353 {
354 cp_lexer_get_preprocessor_token (lexer, &token);
355 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
356 }
357
358 lexer->last_token = VEC_address (cp_token, lexer->buffer)
359 + VEC_length (cp_token, lexer->buffer)
360 - 1;
361 lexer->next_token = VEC_length (cp_token, lexer->buffer)
362 ? VEC_address (cp_token, lexer->buffer)
363 : &eof_token;
364
365 /* Subsequent preprocessor diagnostics should use compiler
366 diagnostic functions to get the compiler source location. */
367 done_lexing = true;
368
369 gcc_assert (!lexer->next_token->purged_p);
370 return lexer;
371 }
372
373 /* Create a new lexer whose token stream is primed with the tokens in
374 CACHE. When these tokens are exhausted, no new tokens will be read. */
375
376 static cp_lexer *
377 cp_lexer_new_from_tokens (cp_token_cache *cache)
378 {
379 cp_token *first = cache->first;
380 cp_token *last = cache->last;
381 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
382
383 /* We do not own the buffer. */
384 lexer->buffer = NULL;
385 lexer->next_token = first == last ? &eof_token : first;
386 lexer->last_token = last;
387
388 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
389 CP_SAVED_TOKEN_STACK);
390
391 #ifdef ENABLE_CHECKING
392 /* Initially we are not debugging. */
393 lexer->debugging_p = false;
394 #endif
395
396 gcc_assert (!lexer->next_token->purged_p);
397 return lexer;
398 }
399
400 /* Frees all resources associated with LEXER. */
401
402 static void
403 cp_lexer_destroy (cp_lexer *lexer)
404 {
405 VEC_free (cp_token, gc, lexer->buffer);
406 VEC_free (cp_token_position, heap, lexer->saved_tokens);
407 ggc_free (lexer);
408 }
409
410 /* Returns nonzero if debugging information should be output. */
411
412 #ifdef ENABLE_CHECKING
413
414 static inline bool
415 cp_lexer_debugging_p (cp_lexer *lexer)
416 {
417 return lexer->debugging_p;
418 }
419
420 #endif /* ENABLE_CHECKING */
421
422 static inline cp_token_position
423 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
424 {
425 gcc_assert (!previous_p || lexer->next_token != &eof_token);
426
427 return lexer->next_token - previous_p;
428 }
429
430 static inline cp_token *
431 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
432 {
433 return pos;
434 }
435
436 static inline void
437 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
438 {
439 lexer->next_token = cp_lexer_token_at (lexer, pos);
440 }
441
442 static inline cp_token_position
443 cp_lexer_previous_token_position (cp_lexer *lexer)
444 {
445 if (lexer->next_token == &eof_token)
446 return lexer->last_token - 1;
447 else
448 return cp_lexer_token_position (lexer, true);
449 }
450
451 static inline cp_token *
452 cp_lexer_previous_token (cp_lexer *lexer)
453 {
454 cp_token_position tp = cp_lexer_previous_token_position (lexer);
455
456 return cp_lexer_token_at (lexer, tp);
457 }
458
459 /* nonzero if we are presently saving tokens. */
460
461 static inline int
462 cp_lexer_saving_tokens (const cp_lexer* lexer)
463 {
464 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
465 }
466
467 /* Store the next token from the preprocessor in *TOKEN. Return true
468 if we reach EOF. If LEXER is NULL, assume we are handling an
469 initial #pragma pch_preprocess, and thus want the lexer to return
470 processed strings. */
471
472 static void
473 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
474 {
475 static int is_extern_c = 0;
476
477 /* Get a new token from the preprocessor. */
478 token->type
479 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
480 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
481 token->keyword = RID_MAX;
482 token->pragma_kind = PRAGMA_NONE;
483 token->purged_p = false;
484
485 /* On some systems, some header files are surrounded by an
486 implicit extern "C" block. Set a flag in the token if it
487 comes from such a header. */
488 is_extern_c += pending_lang_change;
489 pending_lang_change = 0;
490 token->implicit_extern_c = is_extern_c > 0;
491
492 /* Check to see if this token is a keyword. */
493 if (token->type == CPP_NAME)
494 {
495 if (C_IS_RESERVED_WORD (token->u.value))
496 {
497 /* Mark this token as a keyword. */
498 token->type = CPP_KEYWORD;
499 /* Record which keyword. */
500 token->keyword = C_RID_CODE (token->u.value);
501 }
502 else
503 {
504 if (warn_cxx0x_compat
505 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
506 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
507 {
508 /* Warn about the C++0x keyword (but still treat it as
509 an identifier). */
510 warning (OPT_Wc__0x_compat,
511 "identifier %qE will become a keyword in C++0x",
512 token->u.value);
513
514 /* Clear out the C_RID_CODE so we don't warn about this
515 particular identifier-turned-keyword again. */
516 C_SET_RID_CODE (token->u.value, RID_MAX);
517 }
518
519 token->ambiguous_p = false;
520 token->keyword = RID_MAX;
521 }
522 }
523 else if (token->type == CPP_AT_NAME)
524 {
525 /* This only happens in Objective-C++; it must be a keyword. */
526 token->type = CPP_KEYWORD;
527 switch (C_RID_CODE (token->u.value))
528 {
529 /* Replace 'class' with '@class', 'private' with '@private',
530 etc. This prevents confusion with the C++ keyword
531 'class', and makes the tokens consistent with other
532 Objective-C 'AT' keywords. For example '@class' is
533 reported as RID_AT_CLASS which is consistent with
534 '@synchronized', which is reported as
535 RID_AT_SYNCHRONIZED.
536 */
537 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
538 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
539 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
540 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
541 case RID_THROW: token->keyword = RID_AT_THROW; break;
542 case RID_TRY: token->keyword = RID_AT_TRY; break;
543 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
544 default: token->keyword = C_RID_CODE (token->u.value);
545 }
546 }
547 else if (token->type == CPP_PRAGMA)
548 {
549 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
550 token->pragma_kind = ((enum pragma_kind)
551 TREE_INT_CST_LOW (token->u.value));
552 token->u.value = NULL_TREE;
553 }
554 }
555
556 /* Update the globals input_location and the input file stack from TOKEN. */
557 static inline void
558 cp_lexer_set_source_position_from_token (cp_token *token)
559 {
560 if (token->type != CPP_EOF)
561 {
562 input_location = token->location;
563 }
564 }
565
566 /* Return a pointer to the next token in the token stream, but do not
567 consume it. */
568
569 static inline cp_token *
570 cp_lexer_peek_token (cp_lexer *lexer)
571 {
572 if (cp_lexer_debugging_p (lexer))
573 {
574 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
576 putc ('\n', cp_lexer_debug_stream);
577 }
578 return lexer->next_token;
579 }
580
581 /* Return true if the next token has the indicated TYPE. */
582
583 static inline bool
584 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
585 {
586 return cp_lexer_peek_token (lexer)->type == type;
587 }
588
589 /* Return true if the next token does not have the indicated TYPE. */
590
591 static inline bool
592 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
593 {
594 return !cp_lexer_next_token_is (lexer, type);
595 }
596
597 /* Return true if the next token is the indicated KEYWORD. */
598
599 static inline bool
600 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
601 {
602 return cp_lexer_peek_token (lexer)->keyword == keyword;
603 }
604
605 /* Return true if the next token is not the indicated KEYWORD. */
606
607 static inline bool
608 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
609 {
610 return cp_lexer_peek_token (lexer)->keyword != keyword;
611 }
612
613 /* Return true if the next token is a keyword for a decl-specifier. */
614
615 static bool
616 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
617 {
618 cp_token *token;
619
620 token = cp_lexer_peek_token (lexer);
621 switch (token->keyword)
622 {
623 /* auto specifier: storage-class-specifier in C++,
624 simple-type-specifier in C++0x. */
625 case RID_AUTO:
626 /* Storage classes. */
627 case RID_REGISTER:
628 case RID_STATIC:
629 case RID_EXTERN:
630 case RID_MUTABLE:
631 case RID_THREAD:
632 /* Elaborated type specifiers. */
633 case RID_ENUM:
634 case RID_CLASS:
635 case RID_STRUCT:
636 case RID_UNION:
637 case RID_TYPENAME:
638 /* Simple type specifiers. */
639 case RID_CHAR:
640 case RID_CHAR16:
641 case RID_CHAR32:
642 case RID_WCHAR:
643 case RID_BOOL:
644 case RID_SHORT:
645 case RID_INT:
646 case RID_LONG:
647 case RID_INT128:
648 case RID_SIGNED:
649 case RID_UNSIGNED:
650 case RID_FLOAT:
651 case RID_DOUBLE:
652 case RID_VOID:
653 /* GNU extensions. */
654 case RID_ATTRIBUTE:
655 case RID_TYPEOF:
656 /* C++0x extensions. */
657 case RID_DECLTYPE:
658 case RID_UNDERLYING_TYPE:
659 return true;
660
661 default:
662 return false;
663 }
664 }
665
666 /* Return a pointer to the Nth token in the token stream. If N is 1,
667 then this is precisely equivalent to cp_lexer_peek_token (except
668 that it is not inline). One would like to disallow that case, but
669 there is one case (cp_parser_nth_token_starts_template_id) where
670 the caller passes a variable for N and it might be 1. */
671
672 static cp_token *
673 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
674 {
675 cp_token *token;
676
677 /* N is 1-based, not zero-based. */
678 gcc_assert (n > 0);
679
680 if (cp_lexer_debugging_p (lexer))
681 fprintf (cp_lexer_debug_stream,
682 "cp_lexer: peeking ahead %ld at token: ", (long)n);
683
684 --n;
685 token = lexer->next_token;
686 gcc_assert (!n || token != &eof_token);
687 while (n != 0)
688 {
689 ++token;
690 if (token == lexer->last_token)
691 {
692 token = &eof_token;
693 break;
694 }
695
696 if (!token->purged_p)
697 --n;
698 }
699
700 if (cp_lexer_debugging_p (lexer))
701 {
702 cp_lexer_print_token (cp_lexer_debug_stream, token);
703 putc ('\n', cp_lexer_debug_stream);
704 }
705
706 return token;
707 }
708
709 /* Return the next token, and advance the lexer's next_token pointer
710 to point to the next non-purged token. */
711
712 static cp_token *
713 cp_lexer_consume_token (cp_lexer* lexer)
714 {
715 cp_token *token = lexer->next_token;
716
717 gcc_assert (token != &eof_token);
718 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
719
720 do
721 {
722 lexer->next_token++;
723 if (lexer->next_token == lexer->last_token)
724 {
725 lexer->next_token = &eof_token;
726 break;
727 }
728
729 }
730 while (lexer->next_token->purged_p);
731
732 cp_lexer_set_source_position_from_token (token);
733
734 /* Provide debugging output. */
735 if (cp_lexer_debugging_p (lexer))
736 {
737 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
738 cp_lexer_print_token (cp_lexer_debug_stream, token);
739 putc ('\n', cp_lexer_debug_stream);
740 }
741
742 return token;
743 }
744
745 /* Permanently remove the next token from the token stream, and
746 advance the next_token pointer to refer to the next non-purged
747 token. */
748
749 static void
750 cp_lexer_purge_token (cp_lexer *lexer)
751 {
752 cp_token *tok = lexer->next_token;
753
754 gcc_assert (tok != &eof_token);
755 tok->purged_p = true;
756 tok->location = UNKNOWN_LOCATION;
757 tok->u.value = NULL_TREE;
758 tok->keyword = RID_MAX;
759
760 do
761 {
762 tok++;
763 if (tok == lexer->last_token)
764 {
765 tok = &eof_token;
766 break;
767 }
768 }
769 while (tok->purged_p);
770 lexer->next_token = tok;
771 }
772
773 /* Permanently remove all tokens after TOK, up to, but not
774 including, the token that will be returned next by
775 cp_lexer_peek_token. */
776
777 static void
778 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
779 {
780 cp_token *peek = lexer->next_token;
781
782 if (peek == &eof_token)
783 peek = lexer->last_token;
784
785 gcc_assert (tok < peek);
786
787 for ( tok += 1; tok != peek; tok += 1)
788 {
789 tok->purged_p = true;
790 tok->location = UNKNOWN_LOCATION;
791 tok->u.value = NULL_TREE;
792 tok->keyword = RID_MAX;
793 }
794 }
795
796 /* Begin saving tokens. All tokens consumed after this point will be
797 preserved. */
798
799 static void
800 cp_lexer_save_tokens (cp_lexer* lexer)
801 {
802 /* Provide debugging output. */
803 if (cp_lexer_debugging_p (lexer))
804 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
805
806 VEC_safe_push (cp_token_position, heap,
807 lexer->saved_tokens, lexer->next_token);
808 }
809
810 /* Commit to the portion of the token stream most recently saved. */
811
812 static void
813 cp_lexer_commit_tokens (cp_lexer* lexer)
814 {
815 /* Provide debugging output. */
816 if (cp_lexer_debugging_p (lexer))
817 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
818
819 VEC_pop (cp_token_position, lexer->saved_tokens);
820 }
821
822 /* Return all tokens saved since the last call to cp_lexer_save_tokens
823 to the token stream. Stop saving tokens. */
824
825 static void
826 cp_lexer_rollback_tokens (cp_lexer* lexer)
827 {
828 /* Provide debugging output. */
829 if (cp_lexer_debugging_p (lexer))
830 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
831
832 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
833 }
834
835 /* Print a representation of the TOKEN on the STREAM. */
836
837 #ifdef ENABLE_CHECKING
838
839 static void
840 cp_lexer_print_token (FILE * stream, cp_token *token)
841 {
842 /* We don't use cpp_type2name here because the parser defines
843 a few tokens of its own. */
844 static const char *const token_names[] = {
845 /* cpplib-defined token types */
846 #define OP(e, s) #e,
847 #define TK(e, s) #e,
848 TTYPE_TABLE
849 #undef OP
850 #undef TK
851 /* C++ parser token types - see "Manifest constants", above. */
852 "KEYWORD",
853 "TEMPLATE_ID",
854 "NESTED_NAME_SPECIFIER",
855 };
856
857 /* For some tokens, print the associated data. */
858 switch (token->type)
859 {
860 case CPP_KEYWORD:
861 /* Some keywords have a value that is not an IDENTIFIER_NODE.
862 For example, `struct' is mapped to an INTEGER_CST. */
863 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
864 break;
865 /* else fall through */
866 case CPP_NAME:
867 fputs (IDENTIFIER_POINTER (token->u.value), stream);
868 break;
869
870 case CPP_STRING:
871 case CPP_STRING16:
872 case CPP_STRING32:
873 case CPP_WSTRING:
874 case CPP_UTF8STRING:
875 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
876 break;
877
878 case CPP_NUMBER:
879 print_generic_expr (stream, token->u.value, 0);
880 break;
881
882 default:
883 /* If we have a name for the token, print it out. Otherwise, we
884 simply give the numeric code. */
885 if (token->type < ARRAY_SIZE(token_names))
886 fputs (token_names[token->type], stream);
887 else
888 fprintf (stream, "[%d]", token->type);
889 break;
890 }
891 }
892
893 /* Start emitting debugging information. */
894
895 static void
896 cp_lexer_start_debugging (cp_lexer* lexer)
897 {
898 lexer->debugging_p = true;
899 }
900
901 /* Stop emitting debugging information. */
902
903 static void
904 cp_lexer_stop_debugging (cp_lexer* lexer)
905 {
906 lexer->debugging_p = false;
907 }
908
909 #endif /* ENABLE_CHECKING */
910
911 /* Create a new cp_token_cache, representing a range of tokens. */
912
913 static cp_token_cache *
914 cp_token_cache_new (cp_token *first, cp_token *last)
915 {
916 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
917 cache->first = first;
918 cache->last = last;
919 return cache;
920 }
921
922 \f
923 /* Decl-specifiers. */
924
925 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
926
927 static void
928 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
929 {
930 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
931 }
932
933 /* Declarators. */
934
935 /* Nothing other than the parser should be creating declarators;
936 declarators are a semi-syntactic representation of C++ entities.
937 Other parts of the front end that need to create entities (like
938 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
939
940 static cp_declarator *make_call_declarator
941 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
942 static cp_declarator *make_array_declarator
943 (cp_declarator *, tree);
944 static cp_declarator *make_pointer_declarator
945 (cp_cv_quals, cp_declarator *);
946 static cp_declarator *make_reference_declarator
947 (cp_cv_quals, cp_declarator *, bool);
948 static cp_parameter_declarator *make_parameter_declarator
949 (cp_decl_specifier_seq *, cp_declarator *, tree);
950 static cp_declarator *make_ptrmem_declarator
951 (cp_cv_quals, tree, cp_declarator *);
952
953 /* An erroneous declarator. */
954 static cp_declarator *cp_error_declarator;
955
956 /* The obstack on which declarators and related data structures are
957 allocated. */
958 static struct obstack declarator_obstack;
959
960 /* Alloc BYTES from the declarator memory pool. */
961
962 static inline void *
963 alloc_declarator (size_t bytes)
964 {
965 return obstack_alloc (&declarator_obstack, bytes);
966 }
967
968 /* Allocate a declarator of the indicated KIND. Clear fields that are
969 common to all declarators. */
970
971 static cp_declarator *
972 make_declarator (cp_declarator_kind kind)
973 {
974 cp_declarator *declarator;
975
976 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
977 declarator->kind = kind;
978 declarator->attributes = NULL_TREE;
979 declarator->declarator = NULL;
980 declarator->parameter_pack_p = false;
981 declarator->id_loc = UNKNOWN_LOCATION;
982
983 return declarator;
984 }
985
986 /* Make a declarator for a generalized identifier. If
987 QUALIFYING_SCOPE is non-NULL, the identifier is
988 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
989 UNQUALIFIED_NAME. SFK indicates the kind of special function this
990 is, if any. */
991
992 static cp_declarator *
993 make_id_declarator (tree qualifying_scope, tree unqualified_name,
994 special_function_kind sfk)
995 {
996 cp_declarator *declarator;
997
998 /* It is valid to write:
999
1000 class C { void f(); };
1001 typedef C D;
1002 void D::f();
1003
1004 The standard is not clear about whether `typedef const C D' is
1005 legal; as of 2002-09-15 the committee is considering that
1006 question. EDG 3.0 allows that syntax. Therefore, we do as
1007 well. */
1008 if (qualifying_scope && TYPE_P (qualifying_scope))
1009 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1010
1011 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1012 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1013 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1014
1015 declarator = make_declarator (cdk_id);
1016 declarator->u.id.qualifying_scope = qualifying_scope;
1017 declarator->u.id.unqualified_name = unqualified_name;
1018 declarator->u.id.sfk = sfk;
1019
1020 return declarator;
1021 }
1022
1023 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1024 of modifiers such as const or volatile to apply to the pointer
1025 type, represented as identifiers. */
1026
1027 cp_declarator *
1028 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1029 {
1030 cp_declarator *declarator;
1031
1032 declarator = make_declarator (cdk_pointer);
1033 declarator->declarator = target;
1034 declarator->u.pointer.qualifiers = cv_qualifiers;
1035 declarator->u.pointer.class_type = NULL_TREE;
1036 if (target)
1037 {
1038 declarator->id_loc = target->id_loc;
1039 declarator->parameter_pack_p = target->parameter_pack_p;
1040 target->parameter_pack_p = false;
1041 }
1042 else
1043 declarator->parameter_pack_p = false;
1044
1045 return declarator;
1046 }
1047
1048 /* Like make_pointer_declarator -- but for references. */
1049
1050 cp_declarator *
1051 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1052 bool rvalue_ref)
1053 {
1054 cp_declarator *declarator;
1055
1056 declarator = make_declarator (cdk_reference);
1057 declarator->declarator = target;
1058 declarator->u.reference.qualifiers = cv_qualifiers;
1059 declarator->u.reference.rvalue_ref = rvalue_ref;
1060 if (target)
1061 {
1062 declarator->id_loc = target->id_loc;
1063 declarator->parameter_pack_p = target->parameter_pack_p;
1064 target->parameter_pack_p = false;
1065 }
1066 else
1067 declarator->parameter_pack_p = false;
1068
1069 return declarator;
1070 }
1071
1072 /* Like make_pointer_declarator -- but for a pointer to a non-static
1073 member of CLASS_TYPE. */
1074
1075 cp_declarator *
1076 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1077 cp_declarator *pointee)
1078 {
1079 cp_declarator *declarator;
1080
1081 declarator = make_declarator (cdk_ptrmem);
1082 declarator->declarator = pointee;
1083 declarator->u.pointer.qualifiers = cv_qualifiers;
1084 declarator->u.pointer.class_type = class_type;
1085
1086 if (pointee)
1087 {
1088 declarator->parameter_pack_p = pointee->parameter_pack_p;
1089 pointee->parameter_pack_p = false;
1090 }
1091 else
1092 declarator->parameter_pack_p = false;
1093
1094 return declarator;
1095 }
1096
1097 /* Make a declarator for the function given by TARGET, with the
1098 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1099 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1100 indicates what exceptions can be thrown. */
1101
1102 cp_declarator *
1103 make_call_declarator (cp_declarator *target,
1104 tree parms,
1105 cp_cv_quals cv_qualifiers,
1106 cp_virt_specifiers virt_specifiers,
1107 tree exception_specification,
1108 tree late_return_type)
1109 {
1110 cp_declarator *declarator;
1111
1112 declarator = make_declarator (cdk_function);
1113 declarator->declarator = target;
1114 declarator->u.function.parameters = parms;
1115 declarator->u.function.qualifiers = cv_qualifiers;
1116 declarator->u.function.virt_specifiers = virt_specifiers;
1117 declarator->u.function.exception_specification = exception_specification;
1118 declarator->u.function.late_return_type = late_return_type;
1119 if (target)
1120 {
1121 declarator->id_loc = target->id_loc;
1122 declarator->parameter_pack_p = target->parameter_pack_p;
1123 target->parameter_pack_p = false;
1124 }
1125 else
1126 declarator->parameter_pack_p = false;
1127
1128 return declarator;
1129 }
1130
1131 /* Make a declarator for an array of BOUNDS elements, each of which is
1132 defined by ELEMENT. */
1133
1134 cp_declarator *
1135 make_array_declarator (cp_declarator *element, tree bounds)
1136 {
1137 cp_declarator *declarator;
1138
1139 declarator = make_declarator (cdk_array);
1140 declarator->declarator = element;
1141 declarator->u.array.bounds = bounds;
1142 if (element)
1143 {
1144 declarator->id_loc = element->id_loc;
1145 declarator->parameter_pack_p = element->parameter_pack_p;
1146 element->parameter_pack_p = false;
1147 }
1148 else
1149 declarator->parameter_pack_p = false;
1150
1151 return declarator;
1152 }
1153
1154 /* Determine whether the declarator we've seen so far can be a
1155 parameter pack, when followed by an ellipsis. */
1156 static bool
1157 declarator_can_be_parameter_pack (cp_declarator *declarator)
1158 {
1159 /* Search for a declarator name, or any other declarator that goes
1160 after the point where the ellipsis could appear in a parameter
1161 pack. If we find any of these, then this declarator can not be
1162 made into a parameter pack. */
1163 bool found = false;
1164 while (declarator && !found)
1165 {
1166 switch ((int)declarator->kind)
1167 {
1168 case cdk_id:
1169 case cdk_array:
1170 found = true;
1171 break;
1172
1173 case cdk_error:
1174 return true;
1175
1176 default:
1177 declarator = declarator->declarator;
1178 break;
1179 }
1180 }
1181
1182 return !found;
1183 }
1184
1185 cp_parameter_declarator *no_parameters;
1186
1187 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1188 DECLARATOR and DEFAULT_ARGUMENT. */
1189
1190 cp_parameter_declarator *
1191 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1192 cp_declarator *declarator,
1193 tree default_argument)
1194 {
1195 cp_parameter_declarator *parameter;
1196
1197 parameter = ((cp_parameter_declarator *)
1198 alloc_declarator (sizeof (cp_parameter_declarator)));
1199 parameter->next = NULL;
1200 if (decl_specifiers)
1201 parameter->decl_specifiers = *decl_specifiers;
1202 else
1203 clear_decl_specs (&parameter->decl_specifiers);
1204 parameter->declarator = declarator;
1205 parameter->default_argument = default_argument;
1206 parameter->ellipsis_p = false;
1207
1208 return parameter;
1209 }
1210
1211 /* Returns true iff DECLARATOR is a declaration for a function. */
1212
1213 static bool
1214 function_declarator_p (const cp_declarator *declarator)
1215 {
1216 while (declarator)
1217 {
1218 if (declarator->kind == cdk_function
1219 && declarator->declarator->kind == cdk_id)
1220 return true;
1221 if (declarator->kind == cdk_id
1222 || declarator->kind == cdk_error)
1223 return false;
1224 declarator = declarator->declarator;
1225 }
1226 return false;
1227 }
1228
1229 /* The parser. */
1230
1231 /* Overview
1232 --------
1233
1234 A cp_parser parses the token stream as specified by the C++
1235 grammar. Its job is purely parsing, not semantic analysis. For
1236 example, the parser breaks the token stream into declarators,
1237 expressions, statements, and other similar syntactic constructs.
1238 It does not check that the types of the expressions on either side
1239 of an assignment-statement are compatible, or that a function is
1240 not declared with a parameter of type `void'.
1241
1242 The parser invokes routines elsewhere in the compiler to perform
1243 semantic analysis and to build up the abstract syntax tree for the
1244 code processed.
1245
1246 The parser (and the template instantiation code, which is, in a
1247 way, a close relative of parsing) are the only parts of the
1248 compiler that should be calling push_scope and pop_scope, or
1249 related functions. The parser (and template instantiation code)
1250 keeps track of what scope is presently active; everything else
1251 should simply honor that. (The code that generates static
1252 initializers may also need to set the scope, in order to check
1253 access control correctly when emitting the initializers.)
1254
1255 Methodology
1256 -----------
1257
1258 The parser is of the standard recursive-descent variety. Upcoming
1259 tokens in the token stream are examined in order to determine which
1260 production to use when parsing a non-terminal. Some C++ constructs
1261 require arbitrary look ahead to disambiguate. For example, it is
1262 impossible, in the general case, to tell whether a statement is an
1263 expression or declaration without scanning the entire statement.
1264 Therefore, the parser is capable of "parsing tentatively." When the
1265 parser is not sure what construct comes next, it enters this mode.
1266 Then, while we attempt to parse the construct, the parser queues up
1267 error messages, rather than issuing them immediately, and saves the
1268 tokens it consumes. If the construct is parsed successfully, the
1269 parser "commits", i.e., it issues any queued error messages and
1270 the tokens that were being preserved are permanently discarded.
1271 If, however, the construct is not parsed successfully, the parser
1272 rolls back its state completely so that it can resume parsing using
1273 a different alternative.
1274
1275 Future Improvements
1276 -------------------
1277
1278 The performance of the parser could probably be improved substantially.
1279 We could often eliminate the need to parse tentatively by looking ahead
1280 a little bit. In some places, this approach might not entirely eliminate
1281 the need to parse tentatively, but it might still speed up the average
1282 case. */
1283
1284 /* Flags that are passed to some parsing functions. These values can
1285 be bitwise-ored together. */
1286
1287 enum
1288 {
1289 /* No flags. */
1290 CP_PARSER_FLAGS_NONE = 0x0,
1291 /* The construct is optional. If it is not present, then no error
1292 should be issued. */
1293 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1294 /* When parsing a type-specifier, treat user-defined type-names
1295 as non-type identifiers. */
1296 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1297 /* When parsing a type-specifier, do not try to parse a class-specifier
1298 or enum-specifier. */
1299 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1300 /* When parsing a decl-specifier-seq, only allow type-specifier or
1301 constexpr. */
1302 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1303 };
1304
1305 /* This type is used for parameters and variables which hold
1306 combinations of the above flags. */
1307 typedef int cp_parser_flags;
1308
1309 /* The different kinds of declarators we want to parse. */
1310
1311 typedef enum cp_parser_declarator_kind
1312 {
1313 /* We want an abstract declarator. */
1314 CP_PARSER_DECLARATOR_ABSTRACT,
1315 /* We want a named declarator. */
1316 CP_PARSER_DECLARATOR_NAMED,
1317 /* We don't mind, but the name must be an unqualified-id. */
1318 CP_PARSER_DECLARATOR_EITHER
1319 } cp_parser_declarator_kind;
1320
1321 /* The precedence values used to parse binary expressions. The minimum value
1322 of PREC must be 1, because zero is reserved to quickly discriminate
1323 binary operators from other tokens. */
1324
1325 enum cp_parser_prec
1326 {
1327 PREC_NOT_OPERATOR,
1328 PREC_LOGICAL_OR_EXPRESSION,
1329 PREC_LOGICAL_AND_EXPRESSION,
1330 PREC_INCLUSIVE_OR_EXPRESSION,
1331 PREC_EXCLUSIVE_OR_EXPRESSION,
1332 PREC_AND_EXPRESSION,
1333 PREC_EQUALITY_EXPRESSION,
1334 PREC_RELATIONAL_EXPRESSION,
1335 PREC_SHIFT_EXPRESSION,
1336 PREC_ADDITIVE_EXPRESSION,
1337 PREC_MULTIPLICATIVE_EXPRESSION,
1338 PREC_PM_EXPRESSION,
1339 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1340 };
1341
1342 /* A mapping from a token type to a corresponding tree node type, with a
1343 precedence value. */
1344
1345 typedef struct cp_parser_binary_operations_map_node
1346 {
1347 /* The token type. */
1348 enum cpp_ttype token_type;
1349 /* The corresponding tree code. */
1350 enum tree_code tree_type;
1351 /* The precedence of this operator. */
1352 enum cp_parser_prec prec;
1353 } cp_parser_binary_operations_map_node;
1354
1355 typedef struct cp_parser_expression_stack_entry
1356 {
1357 /* Left hand side of the binary operation we are currently
1358 parsing. */
1359 tree lhs;
1360 /* Original tree code for left hand side, if it was a binary
1361 expression itself (used for -Wparentheses). */
1362 enum tree_code lhs_type;
1363 /* Tree code for the binary operation we are parsing. */
1364 enum tree_code tree_type;
1365 /* Precedence of the binary operation we are parsing. */
1366 enum cp_parser_prec prec;
1367 } cp_parser_expression_stack_entry;
1368
1369 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1370 entries because precedence levels on the stack are monotonically
1371 increasing. */
1372 typedef struct cp_parser_expression_stack_entry
1373 cp_parser_expression_stack[NUM_PREC_VALUES];
1374
1375 /* Prototypes. */
1376
1377 /* Constructors and destructors. */
1378
1379 static cp_parser_context *cp_parser_context_new
1380 (cp_parser_context *);
1381
1382 /* Class variables. */
1383
1384 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1385
1386 /* The operator-precedence table used by cp_parser_binary_expression.
1387 Transformed into an associative array (binops_by_token) by
1388 cp_parser_new. */
1389
1390 static const cp_parser_binary_operations_map_node binops[] = {
1391 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1392 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1393
1394 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1395 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1396 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1397
1398 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1399 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1400
1401 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1402 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1403
1404 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1405 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1406 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1407 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1408
1409 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1410 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1411
1412 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1413
1414 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1415
1416 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1417
1418 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1419
1420 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1421 };
1422
1423 /* The same as binops, but initialized by cp_parser_new so that
1424 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1425 for speed. */
1426 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1427
1428 /* Constructors and destructors. */
1429
1430 /* Construct a new context. The context below this one on the stack
1431 is given by NEXT. */
1432
1433 static cp_parser_context *
1434 cp_parser_context_new (cp_parser_context* next)
1435 {
1436 cp_parser_context *context;
1437
1438 /* Allocate the storage. */
1439 if (cp_parser_context_free_list != NULL)
1440 {
1441 /* Pull the first entry from the free list. */
1442 context = cp_parser_context_free_list;
1443 cp_parser_context_free_list = context->next;
1444 memset (context, 0, sizeof (*context));
1445 }
1446 else
1447 context = ggc_alloc_cleared_cp_parser_context ();
1448
1449 /* No errors have occurred yet in this context. */
1450 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1451 /* If this is not the bottommost context, copy information that we
1452 need from the previous context. */
1453 if (next)
1454 {
1455 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1456 expression, then we are parsing one in this context, too. */
1457 context->object_type = next->object_type;
1458 /* Thread the stack. */
1459 context->next = next;
1460 }
1461
1462 return context;
1463 }
1464
1465 /* Managing the unparsed function queues. */
1466
1467 #define unparsed_funs_with_default_args \
1468 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1469 #define unparsed_funs_with_definitions \
1470 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1471
1472 static void
1473 push_unparsed_function_queues (cp_parser *parser)
1474 {
1475 VEC_safe_push (cp_unparsed_functions_entry, gc,
1476 parser->unparsed_queues, NULL);
1477 unparsed_funs_with_default_args = NULL;
1478 unparsed_funs_with_definitions = make_tree_vector ();
1479 }
1480
1481 static void
1482 pop_unparsed_function_queues (cp_parser *parser)
1483 {
1484 release_tree_vector (unparsed_funs_with_definitions);
1485 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1486 }
1487
1488 /* Prototypes. */
1489
1490 /* Constructors and destructors. */
1491
1492 static cp_parser *cp_parser_new
1493 (void);
1494
1495 /* Routines to parse various constructs.
1496
1497 Those that return `tree' will return the error_mark_node (rather
1498 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1499 Sometimes, they will return an ordinary node if error-recovery was
1500 attempted, even though a parse error occurred. So, to check
1501 whether or not a parse error occurred, you should always use
1502 cp_parser_error_occurred. If the construct is optional (indicated
1503 either by an `_opt' in the name of the function that does the
1504 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1505 the construct is not present. */
1506
1507 /* Lexical conventions [gram.lex] */
1508
1509 static tree cp_parser_identifier
1510 (cp_parser *);
1511 static tree cp_parser_string_literal
1512 (cp_parser *, bool, bool);
1513
1514 /* Basic concepts [gram.basic] */
1515
1516 static bool cp_parser_translation_unit
1517 (cp_parser *);
1518
1519 /* Expressions [gram.expr] */
1520
1521 static tree cp_parser_primary_expression
1522 (cp_parser *, bool, bool, bool, cp_id_kind *);
1523 static tree cp_parser_id_expression
1524 (cp_parser *, bool, bool, bool *, bool, bool);
1525 static tree cp_parser_unqualified_id
1526 (cp_parser *, bool, bool, bool, bool);
1527 static tree cp_parser_nested_name_specifier_opt
1528 (cp_parser *, bool, bool, bool, bool);
1529 static tree cp_parser_nested_name_specifier
1530 (cp_parser *, bool, bool, bool, bool);
1531 static tree cp_parser_qualifying_entity
1532 (cp_parser *, bool, bool, bool, bool, bool);
1533 static tree cp_parser_postfix_expression
1534 (cp_parser *, bool, bool, bool, cp_id_kind *);
1535 static tree cp_parser_postfix_open_square_expression
1536 (cp_parser *, tree, bool);
1537 static tree cp_parser_postfix_dot_deref_expression
1538 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1539 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1540 (cp_parser *, int, bool, bool, bool *);
1541 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1542 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1543 static void cp_parser_pseudo_destructor_name
1544 (cp_parser *, tree *, tree *);
1545 static tree cp_parser_unary_expression
1546 (cp_parser *, bool, bool, cp_id_kind *);
1547 static enum tree_code cp_parser_unary_operator
1548 (cp_token *);
1549 static tree cp_parser_new_expression
1550 (cp_parser *);
1551 static VEC(tree,gc) *cp_parser_new_placement
1552 (cp_parser *);
1553 static tree cp_parser_new_type_id
1554 (cp_parser *, tree *);
1555 static cp_declarator *cp_parser_new_declarator_opt
1556 (cp_parser *);
1557 static cp_declarator *cp_parser_direct_new_declarator
1558 (cp_parser *);
1559 static VEC(tree,gc) *cp_parser_new_initializer
1560 (cp_parser *);
1561 static tree cp_parser_delete_expression
1562 (cp_parser *);
1563 static tree cp_parser_cast_expression
1564 (cp_parser *, bool, bool, cp_id_kind *);
1565 static tree cp_parser_binary_expression
1566 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1567 static tree cp_parser_question_colon_clause
1568 (cp_parser *, tree);
1569 static tree cp_parser_assignment_expression
1570 (cp_parser *, bool, cp_id_kind *);
1571 static enum tree_code cp_parser_assignment_operator_opt
1572 (cp_parser *);
1573 static tree cp_parser_expression
1574 (cp_parser *, bool, cp_id_kind *);
1575 static tree cp_parser_constant_expression
1576 (cp_parser *, bool, bool *);
1577 static tree cp_parser_builtin_offsetof
1578 (cp_parser *);
1579 static tree cp_parser_lambda_expression
1580 (cp_parser *);
1581 static void cp_parser_lambda_introducer
1582 (cp_parser *, tree);
1583 static void cp_parser_lambda_declarator_opt
1584 (cp_parser *, tree);
1585 static void cp_parser_lambda_body
1586 (cp_parser *, tree);
1587
1588 /* Statements [gram.stmt.stmt] */
1589
1590 static void cp_parser_statement
1591 (cp_parser *, tree, bool, bool *);
1592 static void cp_parser_label_for_labeled_statement
1593 (cp_parser *);
1594 static tree cp_parser_expression_statement
1595 (cp_parser *, tree);
1596 static tree cp_parser_compound_statement
1597 (cp_parser *, tree, bool, bool);
1598 static void cp_parser_statement_seq_opt
1599 (cp_parser *, tree);
1600 static tree cp_parser_selection_statement
1601 (cp_parser *, bool *);
1602 static tree cp_parser_condition
1603 (cp_parser *);
1604 static tree cp_parser_iteration_statement
1605 (cp_parser *);
1606 static bool cp_parser_for_init_statement
1607 (cp_parser *, tree *decl);
1608 static tree cp_parser_for
1609 (cp_parser *);
1610 static tree cp_parser_c_for
1611 (cp_parser *, tree, tree);
1612 static tree cp_parser_range_for
1613 (cp_parser *, tree, tree, tree);
1614 static tree cp_parser_perform_range_for_lookup
1615 (tree, tree *, tree *);
1616 static tree cp_parser_range_for_member_function
1617 (tree, tree);
1618 static tree cp_parser_jump_statement
1619 (cp_parser *);
1620 static void cp_parser_declaration_statement
1621 (cp_parser *);
1622
1623 static tree cp_parser_implicitly_scoped_statement
1624 (cp_parser *, bool *);
1625 static void cp_parser_already_scoped_statement
1626 (cp_parser *);
1627
1628 /* Declarations [gram.dcl.dcl] */
1629
1630 static void cp_parser_declaration_seq_opt
1631 (cp_parser *);
1632 static void cp_parser_declaration
1633 (cp_parser *);
1634 static void cp_parser_block_declaration
1635 (cp_parser *, bool);
1636 static void cp_parser_simple_declaration
1637 (cp_parser *, bool, tree *);
1638 static void cp_parser_decl_specifier_seq
1639 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1640 static tree cp_parser_storage_class_specifier_opt
1641 (cp_parser *);
1642 static tree cp_parser_function_specifier_opt
1643 (cp_parser *, cp_decl_specifier_seq *);
1644 static tree cp_parser_type_specifier
1645 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1646 int *, bool *);
1647 static tree cp_parser_simple_type_specifier
1648 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1649 static tree cp_parser_type_name
1650 (cp_parser *);
1651 static tree cp_parser_nonclass_name
1652 (cp_parser* parser);
1653 static tree cp_parser_elaborated_type_specifier
1654 (cp_parser *, bool, bool);
1655 static tree cp_parser_enum_specifier
1656 (cp_parser *);
1657 static void cp_parser_enumerator_list
1658 (cp_parser *, tree);
1659 static void cp_parser_enumerator_definition
1660 (cp_parser *, tree);
1661 static tree cp_parser_namespace_name
1662 (cp_parser *);
1663 static void cp_parser_namespace_definition
1664 (cp_parser *);
1665 static void cp_parser_namespace_body
1666 (cp_parser *);
1667 static tree cp_parser_qualified_namespace_specifier
1668 (cp_parser *);
1669 static void cp_parser_namespace_alias_definition
1670 (cp_parser *);
1671 static bool cp_parser_using_declaration
1672 (cp_parser *, bool);
1673 static void cp_parser_using_directive
1674 (cp_parser *);
1675 static void cp_parser_asm_definition
1676 (cp_parser *);
1677 static void cp_parser_linkage_specification
1678 (cp_parser *);
1679 static void cp_parser_static_assert
1680 (cp_parser *, bool);
1681 static tree cp_parser_decltype
1682 (cp_parser *);
1683
1684 /* Declarators [gram.dcl.decl] */
1685
1686 static tree cp_parser_init_declarator
1687 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1688 static cp_declarator *cp_parser_declarator
1689 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1690 static cp_declarator *cp_parser_direct_declarator
1691 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1692 static enum tree_code cp_parser_ptr_operator
1693 (cp_parser *, tree *, cp_cv_quals *);
1694 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1695 (cp_parser *);
1696 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1697 (cp_parser *);
1698 static tree cp_parser_late_return_type_opt
1699 (cp_parser *);
1700 static tree cp_parser_declarator_id
1701 (cp_parser *, bool);
1702 static tree cp_parser_type_id
1703 (cp_parser *);
1704 static tree cp_parser_template_type_arg
1705 (cp_parser *);
1706 static tree cp_parser_trailing_type_id (cp_parser *);
1707 static tree cp_parser_type_id_1
1708 (cp_parser *, bool, bool);
1709 static void cp_parser_type_specifier_seq
1710 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1711 static tree cp_parser_parameter_declaration_clause
1712 (cp_parser *);
1713 static tree cp_parser_parameter_declaration_list
1714 (cp_parser *, bool *);
1715 static cp_parameter_declarator *cp_parser_parameter_declaration
1716 (cp_parser *, bool, bool *);
1717 static tree cp_parser_default_argument
1718 (cp_parser *, bool);
1719 static void cp_parser_function_body
1720 (cp_parser *);
1721 static tree cp_parser_initializer
1722 (cp_parser *, bool *, bool *);
1723 static tree cp_parser_initializer_clause
1724 (cp_parser *, bool *);
1725 static tree cp_parser_braced_list
1726 (cp_parser*, bool*);
1727 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1728 (cp_parser *, bool *);
1729
1730 static bool cp_parser_ctor_initializer_opt_and_function_body
1731 (cp_parser *);
1732
1733 /* Classes [gram.class] */
1734
1735 static tree cp_parser_class_name
1736 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1737 static tree cp_parser_class_specifier
1738 (cp_parser *);
1739 static tree cp_parser_class_head
1740 (cp_parser *, bool *, tree *, tree *);
1741 static enum tag_types cp_parser_class_key
1742 (cp_parser *);
1743 static void cp_parser_member_specification_opt
1744 (cp_parser *);
1745 static void cp_parser_member_declaration
1746 (cp_parser *);
1747 static tree cp_parser_pure_specifier
1748 (cp_parser *);
1749 static tree cp_parser_constant_initializer
1750 (cp_parser *);
1751
1752 /* Derived classes [gram.class.derived] */
1753
1754 static tree cp_parser_base_clause
1755 (cp_parser *);
1756 static tree cp_parser_base_specifier
1757 (cp_parser *);
1758
1759 /* Special member functions [gram.special] */
1760
1761 static tree cp_parser_conversion_function_id
1762 (cp_parser *);
1763 static tree cp_parser_conversion_type_id
1764 (cp_parser *);
1765 static cp_declarator *cp_parser_conversion_declarator_opt
1766 (cp_parser *);
1767 static bool cp_parser_ctor_initializer_opt
1768 (cp_parser *);
1769 static void cp_parser_mem_initializer_list
1770 (cp_parser *);
1771 static tree cp_parser_mem_initializer
1772 (cp_parser *);
1773 static tree cp_parser_mem_initializer_id
1774 (cp_parser *);
1775
1776 /* Overloading [gram.over] */
1777
1778 static tree cp_parser_operator_function_id
1779 (cp_parser *);
1780 static tree cp_parser_operator
1781 (cp_parser *);
1782
1783 /* Templates [gram.temp] */
1784
1785 static void cp_parser_template_declaration
1786 (cp_parser *, bool);
1787 static tree cp_parser_template_parameter_list
1788 (cp_parser *);
1789 static tree cp_parser_template_parameter
1790 (cp_parser *, bool *, bool *);
1791 static tree cp_parser_type_parameter
1792 (cp_parser *, bool *);
1793 static tree cp_parser_template_id
1794 (cp_parser *, bool, bool, bool);
1795 static tree cp_parser_template_name
1796 (cp_parser *, bool, bool, bool, bool *);
1797 static tree cp_parser_template_argument_list
1798 (cp_parser *);
1799 static tree cp_parser_template_argument
1800 (cp_parser *);
1801 static void cp_parser_explicit_instantiation
1802 (cp_parser *);
1803 static void cp_parser_explicit_specialization
1804 (cp_parser *);
1805
1806 /* Exception handling [gram.exception] */
1807
1808 static tree cp_parser_try_block
1809 (cp_parser *);
1810 static bool cp_parser_function_try_block
1811 (cp_parser *);
1812 static void cp_parser_handler_seq
1813 (cp_parser *);
1814 static void cp_parser_handler
1815 (cp_parser *);
1816 static tree cp_parser_exception_declaration
1817 (cp_parser *);
1818 static tree cp_parser_throw_expression
1819 (cp_parser *);
1820 static tree cp_parser_exception_specification_opt
1821 (cp_parser *);
1822 static tree cp_parser_type_id_list
1823 (cp_parser *);
1824
1825 /* GNU Extensions */
1826
1827 static tree cp_parser_asm_specification_opt
1828 (cp_parser *);
1829 static tree cp_parser_asm_operand_list
1830 (cp_parser *);
1831 static tree cp_parser_asm_clobber_list
1832 (cp_parser *);
1833 static tree cp_parser_asm_label_list
1834 (cp_parser *);
1835 static tree cp_parser_attributes_opt
1836 (cp_parser *);
1837 static tree cp_parser_attribute_list
1838 (cp_parser *);
1839 static bool cp_parser_extension_opt
1840 (cp_parser *, int *);
1841 static void cp_parser_label_declaration
1842 (cp_parser *);
1843
1844 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1845 static bool cp_parser_pragma
1846 (cp_parser *, enum pragma_context);
1847
1848 /* Objective-C++ Productions */
1849
1850 static tree cp_parser_objc_message_receiver
1851 (cp_parser *);
1852 static tree cp_parser_objc_message_args
1853 (cp_parser *);
1854 static tree cp_parser_objc_message_expression
1855 (cp_parser *);
1856 static tree cp_parser_objc_encode_expression
1857 (cp_parser *);
1858 static tree cp_parser_objc_defs_expression
1859 (cp_parser *);
1860 static tree cp_parser_objc_protocol_expression
1861 (cp_parser *);
1862 static tree cp_parser_objc_selector_expression
1863 (cp_parser *);
1864 static tree cp_parser_objc_expression
1865 (cp_parser *);
1866 static bool cp_parser_objc_selector_p
1867 (enum cpp_ttype);
1868 static tree cp_parser_objc_selector
1869 (cp_parser *);
1870 static tree cp_parser_objc_protocol_refs_opt
1871 (cp_parser *);
1872 static void cp_parser_objc_declaration
1873 (cp_parser *, tree);
1874 static tree cp_parser_objc_statement
1875 (cp_parser *);
1876 static bool cp_parser_objc_valid_prefix_attributes
1877 (cp_parser *, tree *);
1878 static void cp_parser_objc_at_property_declaration
1879 (cp_parser *) ;
1880 static void cp_parser_objc_at_synthesize_declaration
1881 (cp_parser *) ;
1882 static void cp_parser_objc_at_dynamic_declaration
1883 (cp_parser *) ;
1884 static tree cp_parser_objc_struct_declaration
1885 (cp_parser *) ;
1886
1887 /* Utility Routines */
1888
1889 static tree cp_parser_lookup_name
1890 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1891 static tree cp_parser_lookup_name_simple
1892 (cp_parser *, tree, location_t);
1893 static tree cp_parser_maybe_treat_template_as_class
1894 (tree, bool);
1895 static bool cp_parser_check_declarator_template_parameters
1896 (cp_parser *, cp_declarator *, location_t);
1897 static bool cp_parser_check_template_parameters
1898 (cp_parser *, unsigned, location_t, cp_declarator *);
1899 static tree cp_parser_simple_cast_expression
1900 (cp_parser *);
1901 static tree cp_parser_global_scope_opt
1902 (cp_parser *, bool);
1903 static bool cp_parser_constructor_declarator_p
1904 (cp_parser *, bool);
1905 static tree cp_parser_function_definition_from_specifiers_and_declarator
1906 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1907 static tree cp_parser_function_definition_after_declarator
1908 (cp_parser *, bool);
1909 static void cp_parser_template_declaration_after_export
1910 (cp_parser *, bool);
1911 static void cp_parser_perform_template_parameter_access_checks
1912 (VEC (deferred_access_check,gc)*);
1913 static tree cp_parser_single_declaration
1914 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1915 static tree cp_parser_functional_cast
1916 (cp_parser *, tree);
1917 static tree cp_parser_save_member_function_body
1918 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1919 static tree cp_parser_enclosed_template_argument_list
1920 (cp_parser *);
1921 static void cp_parser_save_default_args
1922 (cp_parser *, tree);
1923 static void cp_parser_late_parsing_for_member
1924 (cp_parser *, tree);
1925 static void cp_parser_late_parsing_default_args
1926 (cp_parser *, tree);
1927 static tree cp_parser_sizeof_operand
1928 (cp_parser *, enum rid);
1929 static tree cp_parser_trait_expr
1930 (cp_parser *, enum rid);
1931 static bool cp_parser_declares_only_class_p
1932 (cp_parser *);
1933 static void cp_parser_set_storage_class
1934 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1935 static void cp_parser_set_decl_spec_type
1936 (cp_decl_specifier_seq *, tree, location_t, bool);
1937 static bool cp_parser_friend_p
1938 (const cp_decl_specifier_seq *);
1939 static void cp_parser_required_error
1940 (cp_parser *, required_token, bool);
1941 static cp_token *cp_parser_require
1942 (cp_parser *, enum cpp_ttype, required_token);
1943 static cp_token *cp_parser_require_keyword
1944 (cp_parser *, enum rid, required_token);
1945 static bool cp_parser_token_starts_function_definition_p
1946 (cp_token *);
1947 static bool cp_parser_next_token_starts_class_definition_p
1948 (cp_parser *);
1949 static bool cp_parser_next_token_ends_template_argument_p
1950 (cp_parser *);
1951 static bool cp_parser_nth_token_starts_template_argument_list_p
1952 (cp_parser *, size_t);
1953 static enum tag_types cp_parser_token_is_class_key
1954 (cp_token *);
1955 static void cp_parser_check_class_key
1956 (enum tag_types, tree type);
1957 static void cp_parser_check_access_in_redeclaration
1958 (tree type, location_t location);
1959 static bool cp_parser_optional_template_keyword
1960 (cp_parser *);
1961 static void cp_parser_pre_parsed_nested_name_specifier
1962 (cp_parser *);
1963 static bool cp_parser_cache_group
1964 (cp_parser *, enum cpp_ttype, unsigned);
1965 static void cp_parser_parse_tentatively
1966 (cp_parser *);
1967 static void cp_parser_commit_to_tentative_parse
1968 (cp_parser *);
1969 static void cp_parser_abort_tentative_parse
1970 (cp_parser *);
1971 static bool cp_parser_parse_definitely
1972 (cp_parser *);
1973 static inline bool cp_parser_parsing_tentatively
1974 (cp_parser *);
1975 static bool cp_parser_uncommitted_to_tentative_parse_p
1976 (cp_parser *);
1977 static void cp_parser_error
1978 (cp_parser *, const char *);
1979 static void cp_parser_name_lookup_error
1980 (cp_parser *, tree, tree, name_lookup_error, location_t);
1981 static bool cp_parser_simulate_error
1982 (cp_parser *);
1983 static bool cp_parser_check_type_definition
1984 (cp_parser *);
1985 static void cp_parser_check_for_definition_in_return_type
1986 (cp_declarator *, tree, location_t type_location);
1987 static void cp_parser_check_for_invalid_template_id
1988 (cp_parser *, tree, location_t location);
1989 static bool cp_parser_non_integral_constant_expression
1990 (cp_parser *, non_integral_constant);
1991 static void cp_parser_diagnose_invalid_type_name
1992 (cp_parser *, tree, tree, location_t);
1993 static bool cp_parser_parse_and_diagnose_invalid_type_name
1994 (cp_parser *);
1995 static int cp_parser_skip_to_closing_parenthesis
1996 (cp_parser *, bool, bool, bool);
1997 static void cp_parser_skip_to_end_of_statement
1998 (cp_parser *);
1999 static void cp_parser_consume_semicolon_at_end_of_statement
2000 (cp_parser *);
2001 static void cp_parser_skip_to_end_of_block_or_statement
2002 (cp_parser *);
2003 static bool cp_parser_skip_to_closing_brace
2004 (cp_parser *);
2005 static void cp_parser_skip_to_end_of_template_parameter_list
2006 (cp_parser *);
2007 static void cp_parser_skip_to_pragma_eol
2008 (cp_parser*, cp_token *);
2009 static bool cp_parser_error_occurred
2010 (cp_parser *);
2011 static bool cp_parser_allow_gnu_extensions_p
2012 (cp_parser *);
2013 static bool cp_parser_is_string_literal
2014 (cp_token *);
2015 static bool cp_parser_is_keyword
2016 (cp_token *, enum rid);
2017 static tree cp_parser_make_typename_type
2018 (cp_parser *, tree, tree, location_t location);
2019 static cp_declarator * cp_parser_make_indirect_declarator
2020 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2021
2022 /* Returns nonzero if we are parsing tentatively. */
2023
2024 static inline bool
2025 cp_parser_parsing_tentatively (cp_parser* parser)
2026 {
2027 return parser->context->next != NULL;
2028 }
2029
2030 /* Returns nonzero if TOKEN is a string literal. */
2031
2032 static bool
2033 cp_parser_is_string_literal (cp_token* token)
2034 {
2035 return (token->type == CPP_STRING ||
2036 token->type == CPP_STRING16 ||
2037 token->type == CPP_STRING32 ||
2038 token->type == CPP_WSTRING ||
2039 token->type == CPP_UTF8STRING);
2040 }
2041
2042 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2043
2044 static bool
2045 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2046 {
2047 return token->keyword == keyword;
2048 }
2049
2050 /* If not parsing tentatively, issue a diagnostic of the form
2051 FILE:LINE: MESSAGE before TOKEN
2052 where TOKEN is the next token in the input stream. MESSAGE
2053 (specified by the caller) is usually of the form "expected
2054 OTHER-TOKEN". */
2055
2056 static void
2057 cp_parser_error (cp_parser* parser, const char* gmsgid)
2058 {
2059 if (!cp_parser_simulate_error (parser))
2060 {
2061 cp_token *token = cp_lexer_peek_token (parser->lexer);
2062 /* This diagnostic makes more sense if it is tagged to the line
2063 of the token we just peeked at. */
2064 cp_lexer_set_source_position_from_token (token);
2065
2066 if (token->type == CPP_PRAGMA)
2067 {
2068 error_at (token->location,
2069 "%<#pragma%> is not allowed here");
2070 cp_parser_skip_to_pragma_eol (parser, token);
2071 return;
2072 }
2073
2074 c_parse_error (gmsgid,
2075 /* Because c_parser_error does not understand
2076 CPP_KEYWORD, keywords are treated like
2077 identifiers. */
2078 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2079 token->u.value, token->flags);
2080 }
2081 }
2082
2083 /* Issue an error about name-lookup failing. NAME is the
2084 IDENTIFIER_NODE DECL is the result of
2085 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2086 the thing that we hoped to find. */
2087
2088 static void
2089 cp_parser_name_lookup_error (cp_parser* parser,
2090 tree name,
2091 tree decl,
2092 name_lookup_error desired,
2093 location_t location)
2094 {
2095 /* If name lookup completely failed, tell the user that NAME was not
2096 declared. */
2097 if (decl == error_mark_node)
2098 {
2099 if (parser->scope && parser->scope != global_namespace)
2100 error_at (location, "%<%E::%E%> has not been declared",
2101 parser->scope, name);
2102 else if (parser->scope == global_namespace)
2103 error_at (location, "%<::%E%> has not been declared", name);
2104 else if (parser->object_scope
2105 && !CLASS_TYPE_P (parser->object_scope))
2106 error_at (location, "request for member %qE in non-class type %qT",
2107 name, parser->object_scope);
2108 else if (parser->object_scope)
2109 error_at (location, "%<%T::%E%> has not been declared",
2110 parser->object_scope, name);
2111 else
2112 error_at (location, "%qE has not been declared", name);
2113 }
2114 else if (parser->scope && parser->scope != global_namespace)
2115 {
2116 switch (desired)
2117 {
2118 case NLE_TYPE:
2119 error_at (location, "%<%E::%E%> is not a type",
2120 parser->scope, name);
2121 break;
2122 case NLE_CXX98:
2123 error_at (location, "%<%E::%E%> is not a class or namespace",
2124 parser->scope, name);
2125 break;
2126 case NLE_NOT_CXX98:
2127 error_at (location,
2128 "%<%E::%E%> is not a class, namespace, or enumeration",
2129 parser->scope, name);
2130 break;
2131 default:
2132 gcc_unreachable ();
2133
2134 }
2135 }
2136 else if (parser->scope == global_namespace)
2137 {
2138 switch (desired)
2139 {
2140 case NLE_TYPE:
2141 error_at (location, "%<::%E%> is not a type", name);
2142 break;
2143 case NLE_CXX98:
2144 error_at (location, "%<::%E%> is not a class or namespace", name);
2145 break;
2146 case NLE_NOT_CXX98:
2147 error_at (location,
2148 "%<::%E%> is not a class, namespace, or enumeration",
2149 name);
2150 break;
2151 default:
2152 gcc_unreachable ();
2153 }
2154 }
2155 else
2156 {
2157 switch (desired)
2158 {
2159 case NLE_TYPE:
2160 error_at (location, "%qE is not a type", name);
2161 break;
2162 case NLE_CXX98:
2163 error_at (location, "%qE is not a class or namespace", name);
2164 break;
2165 case NLE_NOT_CXX98:
2166 error_at (location,
2167 "%qE is not a class, namespace, or enumeration", name);
2168 break;
2169 default:
2170 gcc_unreachable ();
2171 }
2172 }
2173 }
2174
2175 /* If we are parsing tentatively, remember that an error has occurred
2176 during this tentative parse. Returns true if the error was
2177 simulated; false if a message should be issued by the caller. */
2178
2179 static bool
2180 cp_parser_simulate_error (cp_parser* parser)
2181 {
2182 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2183 {
2184 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2185 return true;
2186 }
2187 return false;
2188 }
2189
2190 /* Check for repeated decl-specifiers. */
2191
2192 static void
2193 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2194 location_t location)
2195 {
2196 int ds;
2197
2198 for (ds = ds_first; ds != ds_last; ++ds)
2199 {
2200 unsigned count = decl_specs->specs[ds];
2201 if (count < 2)
2202 continue;
2203 /* The "long" specifier is a special case because of "long long". */
2204 if (ds == ds_long)
2205 {
2206 if (count > 2)
2207 error_at (location, "%<long long long%> is too long for GCC");
2208 else
2209 pedwarn_cxx98 (location, OPT_Wlong_long,
2210 "ISO C++ 1998 does not support %<long long%>");
2211 }
2212 else if (count > 1)
2213 {
2214 static const char *const decl_spec_names[] = {
2215 "signed",
2216 "unsigned",
2217 "short",
2218 "long",
2219 "const",
2220 "volatile",
2221 "restrict",
2222 "inline",
2223 "virtual",
2224 "explicit",
2225 "friend",
2226 "typedef",
2227 "constexpr",
2228 "__complex",
2229 "__thread"
2230 };
2231 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2232 }
2233 }
2234 }
2235
2236 /* This function is called when a type is defined. If type
2237 definitions are forbidden at this point, an error message is
2238 issued. */
2239
2240 static bool
2241 cp_parser_check_type_definition (cp_parser* parser)
2242 {
2243 /* If types are forbidden here, issue a message. */
2244 if (parser->type_definition_forbidden_message)
2245 {
2246 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2247 in the message need to be interpreted. */
2248 error (parser->type_definition_forbidden_message);
2249 return false;
2250 }
2251 return true;
2252 }
2253
2254 /* This function is called when the DECLARATOR is processed. The TYPE
2255 was a type defined in the decl-specifiers. If it is invalid to
2256 define a type in the decl-specifiers for DECLARATOR, an error is
2257 issued. TYPE_LOCATION is the location of TYPE and is used
2258 for error reporting. */
2259
2260 static void
2261 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2262 tree type, location_t type_location)
2263 {
2264 /* [dcl.fct] forbids type definitions in return types.
2265 Unfortunately, it's not easy to know whether or not we are
2266 processing a return type until after the fact. */
2267 while (declarator
2268 && (declarator->kind == cdk_pointer
2269 || declarator->kind == cdk_reference
2270 || declarator->kind == cdk_ptrmem))
2271 declarator = declarator->declarator;
2272 if (declarator
2273 && declarator->kind == cdk_function)
2274 {
2275 error_at (type_location,
2276 "new types may not be defined in a return type");
2277 inform (type_location,
2278 "(perhaps a semicolon is missing after the definition of %qT)",
2279 type);
2280 }
2281 }
2282
2283 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2284 "<" in any valid C++ program. If the next token is indeed "<",
2285 issue a message warning the user about what appears to be an
2286 invalid attempt to form a template-id. LOCATION is the location
2287 of the type-specifier (TYPE) */
2288
2289 static void
2290 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2291 tree type, location_t location)
2292 {
2293 cp_token_position start = 0;
2294
2295 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2296 {
2297 if (TYPE_P (type))
2298 error_at (location, "%qT is not a template", type);
2299 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2300 error_at (location, "%qE is not a template", type);
2301 else
2302 error_at (location, "invalid template-id");
2303 /* Remember the location of the invalid "<". */
2304 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2305 start = cp_lexer_token_position (parser->lexer, true);
2306 /* Consume the "<". */
2307 cp_lexer_consume_token (parser->lexer);
2308 /* Parse the template arguments. */
2309 cp_parser_enclosed_template_argument_list (parser);
2310 /* Permanently remove the invalid template arguments so that
2311 this error message is not issued again. */
2312 if (start)
2313 cp_lexer_purge_tokens_after (parser->lexer, start);
2314 }
2315 }
2316
2317 /* If parsing an integral constant-expression, issue an error message
2318 about the fact that THING appeared and return true. Otherwise,
2319 return false. In either case, set
2320 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2321
2322 static bool
2323 cp_parser_non_integral_constant_expression (cp_parser *parser,
2324 non_integral_constant thing)
2325 {
2326 parser->non_integral_constant_expression_p = true;
2327 if (parser->integral_constant_expression_p)
2328 {
2329 if (!parser->allow_non_integral_constant_expression_p)
2330 {
2331 const char *msg = NULL;
2332 switch (thing)
2333 {
2334 case NIC_FLOAT:
2335 error ("floating-point literal "
2336 "cannot appear in a constant-expression");
2337 return true;
2338 case NIC_CAST:
2339 error ("a cast to a type other than an integral or "
2340 "enumeration type cannot appear in a "
2341 "constant-expression");
2342 return true;
2343 case NIC_TYPEID:
2344 error ("%<typeid%> operator "
2345 "cannot appear in a constant-expression");
2346 return true;
2347 case NIC_NCC:
2348 error ("non-constant compound literals "
2349 "cannot appear in a constant-expression");
2350 return true;
2351 case NIC_FUNC_CALL:
2352 error ("a function call "
2353 "cannot appear in a constant-expression");
2354 return true;
2355 case NIC_INC:
2356 error ("an increment "
2357 "cannot appear in a constant-expression");
2358 return true;
2359 case NIC_DEC:
2360 error ("an decrement "
2361 "cannot appear in a constant-expression");
2362 return true;
2363 case NIC_ARRAY_REF:
2364 error ("an array reference "
2365 "cannot appear in a constant-expression");
2366 return true;
2367 case NIC_ADDR_LABEL:
2368 error ("the address of a label "
2369 "cannot appear in a constant-expression");
2370 return true;
2371 case NIC_OVERLOADED:
2372 error ("calls to overloaded operators "
2373 "cannot appear in a constant-expression");
2374 return true;
2375 case NIC_ASSIGNMENT:
2376 error ("an assignment cannot appear in a constant-expression");
2377 return true;
2378 case NIC_COMMA:
2379 error ("a comma operator "
2380 "cannot appear in a constant-expression");
2381 return true;
2382 case NIC_CONSTRUCTOR:
2383 error ("a call to a constructor "
2384 "cannot appear in a constant-expression");
2385 return true;
2386 case NIC_THIS:
2387 msg = "this";
2388 break;
2389 case NIC_FUNC_NAME:
2390 msg = "__FUNCTION__";
2391 break;
2392 case NIC_PRETTY_FUNC:
2393 msg = "__PRETTY_FUNCTION__";
2394 break;
2395 case NIC_C99_FUNC:
2396 msg = "__func__";
2397 break;
2398 case NIC_VA_ARG:
2399 msg = "va_arg";
2400 break;
2401 case NIC_ARROW:
2402 msg = "->";
2403 break;
2404 case NIC_POINT:
2405 msg = ".";
2406 break;
2407 case NIC_STAR:
2408 msg = "*";
2409 break;
2410 case NIC_ADDR:
2411 msg = "&";
2412 break;
2413 case NIC_PREINCREMENT:
2414 msg = "++";
2415 break;
2416 case NIC_PREDECREMENT:
2417 msg = "--";
2418 break;
2419 case NIC_NEW:
2420 msg = "new";
2421 break;
2422 case NIC_DEL:
2423 msg = "delete";
2424 break;
2425 default:
2426 gcc_unreachable ();
2427 }
2428 if (msg)
2429 error ("%qs cannot appear in a constant-expression", msg);
2430 return true;
2431 }
2432 }
2433 return false;
2434 }
2435
2436 /* Emit a diagnostic for an invalid type name. SCOPE is the
2437 qualifying scope (or NULL, if none) for ID. This function commits
2438 to the current active tentative parse, if any. (Otherwise, the
2439 problematic construct might be encountered again later, resulting
2440 in duplicate error messages.) LOCATION is the location of ID. */
2441
2442 static void
2443 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2444 tree scope, tree id,
2445 location_t location)
2446 {
2447 tree decl, old_scope;
2448 cp_parser_commit_to_tentative_parse (parser);
2449 /* Try to lookup the identifier. */
2450 old_scope = parser->scope;
2451 parser->scope = scope;
2452 decl = cp_parser_lookup_name_simple (parser, id, location);
2453 parser->scope = old_scope;
2454 /* If the lookup found a template-name, it means that the user forgot
2455 to specify an argument list. Emit a useful error message. */
2456 if (TREE_CODE (decl) == TEMPLATE_DECL)
2457 error_at (location,
2458 "invalid use of template-name %qE without an argument list",
2459 decl);
2460 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2461 error_at (location, "invalid use of destructor %qD as a type", id);
2462 else if (TREE_CODE (decl) == TYPE_DECL)
2463 /* Something like 'unsigned A a;' */
2464 error_at (location, "invalid combination of multiple type-specifiers");
2465 else if (!parser->scope)
2466 {
2467 /* Issue an error message. */
2468 error_at (location, "%qE does not name a type", id);
2469 /* If we're in a template class, it's possible that the user was
2470 referring to a type from a base class. For example:
2471
2472 template <typename T> struct A { typedef T X; };
2473 template <typename T> struct B : public A<T> { X x; };
2474
2475 The user should have said "typename A<T>::X". */
2476 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2477 inform (location, "C++0x %<constexpr%> only available with "
2478 "-std=c++0x or -std=gnu++0x");
2479 else if (processing_template_decl && current_class_type
2480 && TYPE_BINFO (current_class_type))
2481 {
2482 tree b;
2483
2484 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2485 b;
2486 b = TREE_CHAIN (b))
2487 {
2488 tree base_type = BINFO_TYPE (b);
2489 if (CLASS_TYPE_P (base_type)
2490 && dependent_type_p (base_type))
2491 {
2492 tree field;
2493 /* Go from a particular instantiation of the
2494 template (which will have an empty TYPE_FIELDs),
2495 to the main version. */
2496 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2497 for (field = TYPE_FIELDS (base_type);
2498 field;
2499 field = DECL_CHAIN (field))
2500 if (TREE_CODE (field) == TYPE_DECL
2501 && DECL_NAME (field) == id)
2502 {
2503 inform (location,
2504 "(perhaps %<typename %T::%E%> was intended)",
2505 BINFO_TYPE (b), id);
2506 break;
2507 }
2508 if (field)
2509 break;
2510 }
2511 }
2512 }
2513 }
2514 /* Here we diagnose qualified-ids where the scope is actually correct,
2515 but the identifier does not resolve to a valid type name. */
2516 else if (parser->scope != error_mark_node)
2517 {
2518 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2519 error_at (location, "%qE in namespace %qE does not name a type",
2520 id, parser->scope);
2521 else if (CLASS_TYPE_P (parser->scope)
2522 && constructor_name_p (id, parser->scope))
2523 {
2524 /* A<T>::A<T>() */
2525 error_at (location, "%<%T::%E%> names the constructor, not"
2526 " the type", parser->scope, id);
2527 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2528 error_at (location, "and %qT has no template constructors",
2529 parser->scope);
2530 }
2531 else if (TYPE_P (parser->scope)
2532 && dependent_scope_p (parser->scope))
2533 error_at (location, "need %<typename%> before %<%T::%E%> because "
2534 "%qT is a dependent scope",
2535 parser->scope, id, parser->scope);
2536 else if (TYPE_P (parser->scope))
2537 error_at (location, "%qE in class %qT does not name a type",
2538 id, parser->scope);
2539 else
2540 gcc_unreachable ();
2541 }
2542 }
2543
2544 /* Check for a common situation where a type-name should be present,
2545 but is not, and issue a sensible error message. Returns true if an
2546 invalid type-name was detected.
2547
2548 The situation handled by this function are variable declarations of the
2549 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2550 Usually, `ID' should name a type, but if we got here it means that it
2551 does not. We try to emit the best possible error message depending on
2552 how exactly the id-expression looks like. */
2553
2554 static bool
2555 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2556 {
2557 tree id;
2558 cp_token *token = cp_lexer_peek_token (parser->lexer);
2559
2560 /* Avoid duplicate error about ambiguous lookup. */
2561 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2562 {
2563 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2564 if (next->type == CPP_NAME && next->ambiguous_p)
2565 goto out;
2566 }
2567
2568 cp_parser_parse_tentatively (parser);
2569 id = cp_parser_id_expression (parser,
2570 /*template_keyword_p=*/false,
2571 /*check_dependency_p=*/true,
2572 /*template_p=*/NULL,
2573 /*declarator_p=*/true,
2574 /*optional_p=*/false);
2575 /* If the next token is a (, this is a function with no explicit return
2576 type, i.e. constructor, destructor or conversion op. */
2577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2578 || TREE_CODE (id) == TYPE_DECL)
2579 {
2580 cp_parser_abort_tentative_parse (parser);
2581 return false;
2582 }
2583 if (!cp_parser_parse_definitely (parser))
2584 return false;
2585
2586 /* Emit a diagnostic for the invalid type. */
2587 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2588 id, token->location);
2589 out:
2590 /* If we aren't in the middle of a declarator (i.e. in a
2591 parameter-declaration-clause), skip to the end of the declaration;
2592 there's no point in trying to process it. */
2593 if (!parser->in_declarator_p)
2594 cp_parser_skip_to_end_of_block_or_statement (parser);
2595 return true;
2596 }
2597
2598 /* Consume tokens up to, and including, the next non-nested closing `)'.
2599 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2600 are doing error recovery. Returns -1 if OR_COMMA is true and we
2601 found an unnested comma. */
2602
2603 static int
2604 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2605 bool recovering,
2606 bool or_comma,
2607 bool consume_paren)
2608 {
2609 unsigned paren_depth = 0;
2610 unsigned brace_depth = 0;
2611 unsigned square_depth = 0;
2612
2613 if (recovering && !or_comma
2614 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2615 return 0;
2616
2617 while (true)
2618 {
2619 cp_token * token = cp_lexer_peek_token (parser->lexer);
2620
2621 switch (token->type)
2622 {
2623 case CPP_EOF:
2624 case CPP_PRAGMA_EOL:
2625 /* If we've run out of tokens, then there is no closing `)'. */
2626 return 0;
2627
2628 /* This is good for lambda expression capture-lists. */
2629 case CPP_OPEN_SQUARE:
2630 ++square_depth;
2631 break;
2632 case CPP_CLOSE_SQUARE:
2633 if (!square_depth--)
2634 return 0;
2635 break;
2636
2637 case CPP_SEMICOLON:
2638 /* This matches the processing in skip_to_end_of_statement. */
2639 if (!brace_depth)
2640 return 0;
2641 break;
2642
2643 case CPP_OPEN_BRACE:
2644 ++brace_depth;
2645 break;
2646 case CPP_CLOSE_BRACE:
2647 if (!brace_depth--)
2648 return 0;
2649 break;
2650
2651 case CPP_COMMA:
2652 if (recovering && or_comma && !brace_depth && !paren_depth
2653 && !square_depth)
2654 return -1;
2655 break;
2656
2657 case CPP_OPEN_PAREN:
2658 if (!brace_depth)
2659 ++paren_depth;
2660 break;
2661
2662 case CPP_CLOSE_PAREN:
2663 if (!brace_depth && !paren_depth--)
2664 {
2665 if (consume_paren)
2666 cp_lexer_consume_token (parser->lexer);
2667 return 1;
2668 }
2669 break;
2670
2671 default:
2672 break;
2673 }
2674
2675 /* Consume the token. */
2676 cp_lexer_consume_token (parser->lexer);
2677 }
2678 }
2679
2680 /* Consume tokens until we reach the end of the current statement.
2681 Normally, that will be just before consuming a `;'. However, if a
2682 non-nested `}' comes first, then we stop before consuming that. */
2683
2684 static void
2685 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2686 {
2687 unsigned nesting_depth = 0;
2688
2689 while (true)
2690 {
2691 cp_token *token = cp_lexer_peek_token (parser->lexer);
2692
2693 switch (token->type)
2694 {
2695 case CPP_EOF:
2696 case CPP_PRAGMA_EOL:
2697 /* If we've run out of tokens, stop. */
2698 return;
2699
2700 case CPP_SEMICOLON:
2701 /* If the next token is a `;', we have reached the end of the
2702 statement. */
2703 if (!nesting_depth)
2704 return;
2705 break;
2706
2707 case CPP_CLOSE_BRACE:
2708 /* If this is a non-nested '}', stop before consuming it.
2709 That way, when confronted with something like:
2710
2711 { 3 + }
2712
2713 we stop before consuming the closing '}', even though we
2714 have not yet reached a `;'. */
2715 if (nesting_depth == 0)
2716 return;
2717
2718 /* If it is the closing '}' for a block that we have
2719 scanned, stop -- but only after consuming the token.
2720 That way given:
2721
2722 void f g () { ... }
2723 typedef int I;
2724
2725 we will stop after the body of the erroneously declared
2726 function, but before consuming the following `typedef'
2727 declaration. */
2728 if (--nesting_depth == 0)
2729 {
2730 cp_lexer_consume_token (parser->lexer);
2731 return;
2732 }
2733
2734 case CPP_OPEN_BRACE:
2735 ++nesting_depth;
2736 break;
2737
2738 default:
2739 break;
2740 }
2741
2742 /* Consume the token. */
2743 cp_lexer_consume_token (parser->lexer);
2744 }
2745 }
2746
2747 /* This function is called at the end of a statement or declaration.
2748 If the next token is a semicolon, it is consumed; otherwise, error
2749 recovery is attempted. */
2750
2751 static void
2752 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2753 {
2754 /* Look for the trailing `;'. */
2755 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2756 {
2757 /* If there is additional (erroneous) input, skip to the end of
2758 the statement. */
2759 cp_parser_skip_to_end_of_statement (parser);
2760 /* If the next token is now a `;', consume it. */
2761 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2762 cp_lexer_consume_token (parser->lexer);
2763 }
2764 }
2765
2766 /* Skip tokens until we have consumed an entire block, or until we
2767 have consumed a non-nested `;'. */
2768
2769 static void
2770 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2771 {
2772 int nesting_depth = 0;
2773
2774 while (nesting_depth >= 0)
2775 {
2776 cp_token *token = cp_lexer_peek_token (parser->lexer);
2777
2778 switch (token->type)
2779 {
2780 case CPP_EOF:
2781 case CPP_PRAGMA_EOL:
2782 /* If we've run out of tokens, stop. */
2783 return;
2784
2785 case CPP_SEMICOLON:
2786 /* Stop if this is an unnested ';'. */
2787 if (!nesting_depth)
2788 nesting_depth = -1;
2789 break;
2790
2791 case CPP_CLOSE_BRACE:
2792 /* Stop if this is an unnested '}', or closes the outermost
2793 nesting level. */
2794 nesting_depth--;
2795 if (nesting_depth < 0)
2796 return;
2797 if (!nesting_depth)
2798 nesting_depth = -1;
2799 break;
2800
2801 case CPP_OPEN_BRACE:
2802 /* Nest. */
2803 nesting_depth++;
2804 break;
2805
2806 default:
2807 break;
2808 }
2809
2810 /* Consume the token. */
2811 cp_lexer_consume_token (parser->lexer);
2812 }
2813 }
2814
2815 /* Skip tokens until a non-nested closing curly brace is the next
2816 token, or there are no more tokens. Return true in the first case,
2817 false otherwise. */
2818
2819 static bool
2820 cp_parser_skip_to_closing_brace (cp_parser *parser)
2821 {
2822 unsigned nesting_depth = 0;
2823
2824 while (true)
2825 {
2826 cp_token *token = cp_lexer_peek_token (parser->lexer);
2827
2828 switch (token->type)
2829 {
2830 case CPP_EOF:
2831 case CPP_PRAGMA_EOL:
2832 /* If we've run out of tokens, stop. */
2833 return false;
2834
2835 case CPP_CLOSE_BRACE:
2836 /* If the next token is a non-nested `}', then we have reached
2837 the end of the current block. */
2838 if (nesting_depth-- == 0)
2839 return true;
2840 break;
2841
2842 case CPP_OPEN_BRACE:
2843 /* If it the next token is a `{', then we are entering a new
2844 block. Consume the entire block. */
2845 ++nesting_depth;
2846 break;
2847
2848 default:
2849 break;
2850 }
2851
2852 /* Consume the token. */
2853 cp_lexer_consume_token (parser->lexer);
2854 }
2855 }
2856
2857 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2858 parameter is the PRAGMA token, allowing us to purge the entire pragma
2859 sequence. */
2860
2861 static void
2862 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2863 {
2864 cp_token *token;
2865
2866 parser->lexer->in_pragma = false;
2867
2868 do
2869 token = cp_lexer_consume_token (parser->lexer);
2870 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2871
2872 /* Ensure that the pragma is not parsed again. */
2873 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2874 }
2875
2876 /* Require pragma end of line, resyncing with it as necessary. The
2877 arguments are as for cp_parser_skip_to_pragma_eol. */
2878
2879 static void
2880 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2881 {
2882 parser->lexer->in_pragma = false;
2883 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2884 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2885 }
2886
2887 /* This is a simple wrapper around make_typename_type. When the id is
2888 an unresolved identifier node, we can provide a superior diagnostic
2889 using cp_parser_diagnose_invalid_type_name. */
2890
2891 static tree
2892 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2893 tree id, location_t id_location)
2894 {
2895 tree result;
2896 if (TREE_CODE (id) == IDENTIFIER_NODE)
2897 {
2898 result = make_typename_type (scope, id, typename_type,
2899 /*complain=*/tf_none);
2900 if (result == error_mark_node)
2901 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2902 return result;
2903 }
2904 return make_typename_type (scope, id, typename_type, tf_error);
2905 }
2906
2907 /* This is a wrapper around the
2908 make_{pointer,ptrmem,reference}_declarator functions that decides
2909 which one to call based on the CODE and CLASS_TYPE arguments. The
2910 CODE argument should be one of the values returned by
2911 cp_parser_ptr_operator. */
2912 static cp_declarator *
2913 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2914 cp_cv_quals cv_qualifiers,
2915 cp_declarator *target)
2916 {
2917 if (code == ERROR_MARK)
2918 return cp_error_declarator;
2919
2920 if (code == INDIRECT_REF)
2921 if (class_type == NULL_TREE)
2922 return make_pointer_declarator (cv_qualifiers, target);
2923 else
2924 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2925 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2926 return make_reference_declarator (cv_qualifiers, target, false);
2927 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2928 return make_reference_declarator (cv_qualifiers, target, true);
2929 gcc_unreachable ();
2930 }
2931
2932 /* Create a new C++ parser. */
2933
2934 static cp_parser *
2935 cp_parser_new (void)
2936 {
2937 cp_parser *parser;
2938 cp_lexer *lexer;
2939 unsigned i;
2940
2941 /* cp_lexer_new_main is called before doing GC allocation because
2942 cp_lexer_new_main might load a PCH file. */
2943 lexer = cp_lexer_new_main ();
2944
2945 /* Initialize the binops_by_token so that we can get the tree
2946 directly from the token. */
2947 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2948 binops_by_token[binops[i].token_type] = binops[i];
2949
2950 parser = ggc_alloc_cleared_cp_parser ();
2951 parser->lexer = lexer;
2952 parser->context = cp_parser_context_new (NULL);
2953
2954 /* For now, we always accept GNU extensions. */
2955 parser->allow_gnu_extensions_p = 1;
2956
2957 /* The `>' token is a greater-than operator, not the end of a
2958 template-id. */
2959 parser->greater_than_is_operator_p = true;
2960
2961 parser->default_arg_ok_p = true;
2962
2963 /* We are not parsing a constant-expression. */
2964 parser->integral_constant_expression_p = false;
2965 parser->allow_non_integral_constant_expression_p = false;
2966 parser->non_integral_constant_expression_p = false;
2967
2968 /* Local variable names are not forbidden. */
2969 parser->local_variables_forbidden_p = false;
2970
2971 /* We are not processing an `extern "C"' declaration. */
2972 parser->in_unbraced_linkage_specification_p = false;
2973
2974 /* We are not processing a declarator. */
2975 parser->in_declarator_p = false;
2976
2977 /* We are not processing a template-argument-list. */
2978 parser->in_template_argument_list_p = false;
2979
2980 /* We are not in an iteration statement. */
2981 parser->in_statement = 0;
2982
2983 /* We are not in a switch statement. */
2984 parser->in_switch_statement_p = false;
2985
2986 /* We are not parsing a type-id inside an expression. */
2987 parser->in_type_id_in_expr_p = false;
2988
2989 /* Declarations aren't implicitly extern "C". */
2990 parser->implicit_extern_c = false;
2991
2992 /* String literals should be translated to the execution character set. */
2993 parser->translate_strings_p = true;
2994
2995 /* We are not parsing a function body. */
2996 parser->in_function_body = false;
2997
2998 /* We can correct until told otherwise. */
2999 parser->colon_corrects_to_scope_p = true;
3000
3001 /* The unparsed function queue is empty. */
3002 push_unparsed_function_queues (parser);
3003
3004 /* There are no classes being defined. */
3005 parser->num_classes_being_defined = 0;
3006
3007 /* No template parameters apply. */
3008 parser->num_template_parameter_lists = 0;
3009
3010 return parser;
3011 }
3012
3013 /* Create a cp_lexer structure which will emit the tokens in CACHE
3014 and push it onto the parser's lexer stack. This is used for delayed
3015 parsing of in-class method bodies and default arguments, and should
3016 not be confused with tentative parsing. */
3017 static void
3018 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3019 {
3020 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3021 lexer->next = parser->lexer;
3022 parser->lexer = lexer;
3023
3024 /* Move the current source position to that of the first token in the
3025 new lexer. */
3026 cp_lexer_set_source_position_from_token (lexer->next_token);
3027 }
3028
3029 /* Pop the top lexer off the parser stack. This is never used for the
3030 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3031 static void
3032 cp_parser_pop_lexer (cp_parser *parser)
3033 {
3034 cp_lexer *lexer = parser->lexer;
3035 parser->lexer = lexer->next;
3036 cp_lexer_destroy (lexer);
3037
3038 /* Put the current source position back where it was before this
3039 lexer was pushed. */
3040 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3041 }
3042
3043 /* Lexical conventions [gram.lex] */
3044
3045 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3046 identifier. */
3047
3048 static tree
3049 cp_parser_identifier (cp_parser* parser)
3050 {
3051 cp_token *token;
3052
3053 /* Look for the identifier. */
3054 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3055 /* Return the value. */
3056 return token ? token->u.value : error_mark_node;
3057 }
3058
3059 /* Parse a sequence of adjacent string constants. Returns a
3060 TREE_STRING representing the combined, nul-terminated string
3061 constant. If TRANSLATE is true, translate the string to the
3062 execution character set. If WIDE_OK is true, a wide string is
3063 invalid here.
3064
3065 C++98 [lex.string] says that if a narrow string literal token is
3066 adjacent to a wide string literal token, the behavior is undefined.
3067 However, C99 6.4.5p4 says that this results in a wide string literal.
3068 We follow C99 here, for consistency with the C front end.
3069
3070 This code is largely lifted from lex_string() in c-lex.c.
3071
3072 FUTURE: ObjC++ will need to handle @-strings here. */
3073 static tree
3074 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3075 {
3076 tree value;
3077 size_t count;
3078 struct obstack str_ob;
3079 cpp_string str, istr, *strs;
3080 cp_token *tok;
3081 enum cpp_ttype type;
3082
3083 tok = cp_lexer_peek_token (parser->lexer);
3084 if (!cp_parser_is_string_literal (tok))
3085 {
3086 cp_parser_error (parser, "expected string-literal");
3087 return error_mark_node;
3088 }
3089
3090 type = tok->type;
3091
3092 /* Try to avoid the overhead of creating and destroying an obstack
3093 for the common case of just one string. */
3094 if (!cp_parser_is_string_literal
3095 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3096 {
3097 cp_lexer_consume_token (parser->lexer);
3098
3099 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3100 str.len = TREE_STRING_LENGTH (tok->u.value);
3101 count = 1;
3102
3103 strs = &str;
3104 }
3105 else
3106 {
3107 gcc_obstack_init (&str_ob);
3108 count = 0;
3109
3110 do
3111 {
3112 cp_lexer_consume_token (parser->lexer);
3113 count++;
3114 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3115 str.len = TREE_STRING_LENGTH (tok->u.value);
3116
3117 if (type != tok->type)
3118 {
3119 if (type == CPP_STRING)
3120 type = tok->type;
3121 else if (tok->type != CPP_STRING)
3122 error_at (tok->location,
3123 "unsupported non-standard concatenation "
3124 "of string literals");
3125 }
3126
3127 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3128
3129 tok = cp_lexer_peek_token (parser->lexer);
3130 }
3131 while (cp_parser_is_string_literal (tok));
3132
3133 strs = (cpp_string *) obstack_finish (&str_ob);
3134 }
3135
3136 if (type != CPP_STRING && !wide_ok)
3137 {
3138 cp_parser_error (parser, "a wide string is invalid in this context");
3139 type = CPP_STRING;
3140 }
3141
3142 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3143 (parse_in, strs, count, &istr, type))
3144 {
3145 value = build_string (istr.len, (const char *)istr.text);
3146 free (CONST_CAST (unsigned char *, istr.text));
3147
3148 switch (type)
3149 {
3150 default:
3151 case CPP_STRING:
3152 case CPP_UTF8STRING:
3153 TREE_TYPE (value) = char_array_type_node;
3154 break;
3155 case CPP_STRING16:
3156 TREE_TYPE (value) = char16_array_type_node;
3157 break;
3158 case CPP_STRING32:
3159 TREE_TYPE (value) = char32_array_type_node;
3160 break;
3161 case CPP_WSTRING:
3162 TREE_TYPE (value) = wchar_array_type_node;
3163 break;
3164 }
3165
3166 value = fix_string_type (value);
3167 }
3168 else
3169 /* cpp_interpret_string has issued an error. */
3170 value = error_mark_node;
3171
3172 if (count > 1)
3173 obstack_free (&str_ob, 0);
3174
3175 return value;
3176 }
3177
3178
3179 /* Basic concepts [gram.basic] */
3180
3181 /* Parse a translation-unit.
3182
3183 translation-unit:
3184 declaration-seq [opt]
3185
3186 Returns TRUE if all went well. */
3187
3188 static bool
3189 cp_parser_translation_unit (cp_parser* parser)
3190 {
3191 /* The address of the first non-permanent object on the declarator
3192 obstack. */
3193 static void *declarator_obstack_base;
3194
3195 bool success;
3196
3197 /* Create the declarator obstack, if necessary. */
3198 if (!cp_error_declarator)
3199 {
3200 gcc_obstack_init (&declarator_obstack);
3201 /* Create the error declarator. */
3202 cp_error_declarator = make_declarator (cdk_error);
3203 /* Create the empty parameter list. */
3204 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3205 /* Remember where the base of the declarator obstack lies. */
3206 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3207 }
3208
3209 cp_parser_declaration_seq_opt (parser);
3210
3211 /* If there are no tokens left then all went well. */
3212 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3213 {
3214 /* Get rid of the token array; we don't need it any more. */
3215 cp_lexer_destroy (parser->lexer);
3216 parser->lexer = NULL;
3217
3218 /* This file might have been a context that's implicitly extern
3219 "C". If so, pop the lang context. (Only relevant for PCH.) */
3220 if (parser->implicit_extern_c)
3221 {
3222 pop_lang_context ();
3223 parser->implicit_extern_c = false;
3224 }
3225
3226 /* Finish up. */
3227 finish_translation_unit ();
3228
3229 success = true;
3230 }
3231 else
3232 {
3233 cp_parser_error (parser, "expected declaration");
3234 success = false;
3235 }
3236
3237 /* Make sure the declarator obstack was fully cleaned up. */
3238 gcc_assert (obstack_next_free (&declarator_obstack)
3239 == declarator_obstack_base);
3240
3241 /* All went well. */
3242 return success;
3243 }
3244
3245 /* Expressions [gram.expr] */
3246
3247 /* Parse a primary-expression.
3248
3249 primary-expression:
3250 literal
3251 this
3252 ( expression )
3253 id-expression
3254
3255 GNU Extensions:
3256
3257 primary-expression:
3258 ( compound-statement )
3259 __builtin_va_arg ( assignment-expression , type-id )
3260 __builtin_offsetof ( type-id , offsetof-expression )
3261
3262 C++ Extensions:
3263 __has_nothrow_assign ( type-id )
3264 __has_nothrow_constructor ( type-id )
3265 __has_nothrow_copy ( type-id )
3266 __has_trivial_assign ( type-id )
3267 __has_trivial_constructor ( type-id )
3268 __has_trivial_copy ( type-id )
3269 __has_trivial_destructor ( type-id )
3270 __has_virtual_destructor ( type-id )
3271 __is_abstract ( type-id )
3272 __is_base_of ( type-id , type-id )
3273 __is_class ( type-id )
3274 __is_convertible_to ( type-id , type-id )
3275 __is_empty ( type-id )
3276 __is_enum ( type-id )
3277 __is_pod ( type-id )
3278 __is_polymorphic ( type-id )
3279 __is_union ( type-id )
3280
3281 Objective-C++ Extension:
3282
3283 primary-expression:
3284 objc-expression
3285
3286 literal:
3287 __null
3288
3289 ADDRESS_P is true iff this expression was immediately preceded by
3290 "&" and therefore might denote a pointer-to-member. CAST_P is true
3291 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3292 true iff this expression is a template argument.
3293
3294 Returns a representation of the expression. Upon return, *IDK
3295 indicates what kind of id-expression (if any) was present. */
3296
3297 static tree
3298 cp_parser_primary_expression (cp_parser *parser,
3299 bool address_p,
3300 bool cast_p,
3301 bool template_arg_p,
3302 cp_id_kind *idk)
3303 {
3304 cp_token *token = NULL;
3305
3306 /* Assume the primary expression is not an id-expression. */
3307 *idk = CP_ID_KIND_NONE;
3308
3309 /* Peek at the next token. */
3310 token = cp_lexer_peek_token (parser->lexer);
3311 switch (token->type)
3312 {
3313 /* literal:
3314 integer-literal
3315 character-literal
3316 floating-literal
3317 string-literal
3318 boolean-literal */
3319 case CPP_CHAR:
3320 case CPP_CHAR16:
3321 case CPP_CHAR32:
3322 case CPP_WCHAR:
3323 case CPP_NUMBER:
3324 token = cp_lexer_consume_token (parser->lexer);
3325 if (TREE_CODE (token->u.value) == FIXED_CST)
3326 {
3327 error_at (token->location,
3328 "fixed-point types not supported in C++");
3329 return error_mark_node;
3330 }
3331 /* Floating-point literals are only allowed in an integral
3332 constant expression if they are cast to an integral or
3333 enumeration type. */
3334 if (TREE_CODE (token->u.value) == REAL_CST
3335 && parser->integral_constant_expression_p
3336 && pedantic)
3337 {
3338 /* CAST_P will be set even in invalid code like "int(2.7 +
3339 ...)". Therefore, we have to check that the next token
3340 is sure to end the cast. */
3341 if (cast_p)
3342 {
3343 cp_token *next_token;
3344
3345 next_token = cp_lexer_peek_token (parser->lexer);
3346 if (/* The comma at the end of an
3347 enumerator-definition. */
3348 next_token->type != CPP_COMMA
3349 /* The curly brace at the end of an enum-specifier. */
3350 && next_token->type != CPP_CLOSE_BRACE
3351 /* The end of a statement. */
3352 && next_token->type != CPP_SEMICOLON
3353 /* The end of the cast-expression. */
3354 && next_token->type != CPP_CLOSE_PAREN
3355 /* The end of an array bound. */
3356 && next_token->type != CPP_CLOSE_SQUARE
3357 /* The closing ">" in a template-argument-list. */
3358 && (next_token->type != CPP_GREATER
3359 || parser->greater_than_is_operator_p)
3360 /* C++0x only: A ">>" treated like two ">" tokens,
3361 in a template-argument-list. */
3362 && (next_token->type != CPP_RSHIFT
3363 || (cxx_dialect == cxx98)
3364 || parser->greater_than_is_operator_p))
3365 cast_p = false;
3366 }
3367
3368 /* If we are within a cast, then the constraint that the
3369 cast is to an integral or enumeration type will be
3370 checked at that point. If we are not within a cast, then
3371 this code is invalid. */
3372 if (!cast_p)
3373 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3374 }
3375 return token->u.value;
3376
3377 case CPP_STRING:
3378 case CPP_STRING16:
3379 case CPP_STRING32:
3380 case CPP_WSTRING:
3381 case CPP_UTF8STRING:
3382 /* ??? Should wide strings be allowed when parser->translate_strings_p
3383 is false (i.e. in attributes)? If not, we can kill the third
3384 argument to cp_parser_string_literal. */
3385 return cp_parser_string_literal (parser,
3386 parser->translate_strings_p,
3387 true);
3388
3389 case CPP_OPEN_PAREN:
3390 {
3391 tree expr;
3392 bool saved_greater_than_is_operator_p;
3393
3394 /* Consume the `('. */
3395 cp_lexer_consume_token (parser->lexer);
3396 /* Within a parenthesized expression, a `>' token is always
3397 the greater-than operator. */
3398 saved_greater_than_is_operator_p
3399 = parser->greater_than_is_operator_p;
3400 parser->greater_than_is_operator_p = true;
3401 /* If we see `( { ' then we are looking at the beginning of
3402 a GNU statement-expression. */
3403 if (cp_parser_allow_gnu_extensions_p (parser)
3404 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3405 {
3406 /* Statement-expressions are not allowed by the standard. */
3407 pedwarn (token->location, OPT_pedantic,
3408 "ISO C++ forbids braced-groups within expressions");
3409
3410 /* And they're not allowed outside of a function-body; you
3411 cannot, for example, write:
3412
3413 int i = ({ int j = 3; j + 1; });
3414
3415 at class or namespace scope. */
3416 if (!parser->in_function_body
3417 || parser->in_template_argument_list_p)
3418 {
3419 error_at (token->location,
3420 "statement-expressions are not allowed outside "
3421 "functions nor in template-argument lists");
3422 cp_parser_skip_to_end_of_block_or_statement (parser);
3423 expr = error_mark_node;
3424 }
3425 else
3426 {
3427 /* Start the statement-expression. */
3428 expr = begin_stmt_expr ();
3429 /* Parse the compound-statement. */
3430 cp_parser_compound_statement (parser, expr, false, false);
3431 /* Finish up. */
3432 expr = finish_stmt_expr (expr, false);
3433 }
3434 }
3435 else
3436 {
3437 /* Parse the parenthesized expression. */
3438 expr = cp_parser_expression (parser, cast_p, idk);
3439 /* Let the front end know that this expression was
3440 enclosed in parentheses. This matters in case, for
3441 example, the expression is of the form `A::B', since
3442 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3443 not. */
3444 finish_parenthesized_expr (expr);
3445 /* DR 705: Wrapping an unqualified name in parentheses
3446 suppresses arg-dependent lookup. We want to pass back
3447 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3448 (c++/37862), but none of the others. */
3449 if (*idk != CP_ID_KIND_QUALIFIED)
3450 *idk = CP_ID_KIND_NONE;
3451 }
3452 /* The `>' token might be the end of a template-id or
3453 template-parameter-list now. */
3454 parser->greater_than_is_operator_p
3455 = saved_greater_than_is_operator_p;
3456 /* Consume the `)'. */
3457 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3458 cp_parser_skip_to_end_of_statement (parser);
3459
3460 return expr;
3461 }
3462
3463 case CPP_OPEN_SQUARE:
3464 if (c_dialect_objc ())
3465 /* We have an Objective-C++ message. */
3466 return cp_parser_objc_expression (parser);
3467 {
3468 tree lam = cp_parser_lambda_expression (parser);
3469 /* Don't warn about a failed tentative parse. */
3470 if (cp_parser_error_occurred (parser))
3471 return error_mark_node;
3472 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3473 return lam;
3474 }
3475
3476 case CPP_OBJC_STRING:
3477 if (c_dialect_objc ())
3478 /* We have an Objective-C++ string literal. */
3479 return cp_parser_objc_expression (parser);
3480 cp_parser_error (parser, "expected primary-expression");
3481 return error_mark_node;
3482
3483 case CPP_KEYWORD:
3484 switch (token->keyword)
3485 {
3486 /* These two are the boolean literals. */
3487 case RID_TRUE:
3488 cp_lexer_consume_token (parser->lexer);
3489 return boolean_true_node;
3490 case RID_FALSE:
3491 cp_lexer_consume_token (parser->lexer);
3492 return boolean_false_node;
3493
3494 /* The `__null' literal. */
3495 case RID_NULL:
3496 cp_lexer_consume_token (parser->lexer);
3497 return null_node;
3498
3499 /* The `nullptr' literal. */
3500 case RID_NULLPTR:
3501 cp_lexer_consume_token (parser->lexer);
3502 return nullptr_node;
3503
3504 /* Recognize the `this' keyword. */
3505 case RID_THIS:
3506 cp_lexer_consume_token (parser->lexer);
3507 if (parser->local_variables_forbidden_p)
3508 {
3509 error_at (token->location,
3510 "%<this%> may not be used in this context");
3511 return error_mark_node;
3512 }
3513 /* Pointers cannot appear in constant-expressions. */
3514 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3515 return error_mark_node;
3516 return finish_this_expr ();
3517
3518 /* The `operator' keyword can be the beginning of an
3519 id-expression. */
3520 case RID_OPERATOR:
3521 goto id_expression;
3522
3523 case RID_FUNCTION_NAME:
3524 case RID_PRETTY_FUNCTION_NAME:
3525 case RID_C99_FUNCTION_NAME:
3526 {
3527 non_integral_constant name;
3528
3529 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3530 __func__ are the names of variables -- but they are
3531 treated specially. Therefore, they are handled here,
3532 rather than relying on the generic id-expression logic
3533 below. Grammatically, these names are id-expressions.
3534
3535 Consume the token. */
3536 token = cp_lexer_consume_token (parser->lexer);
3537
3538 switch (token->keyword)
3539 {
3540 case RID_FUNCTION_NAME:
3541 name = NIC_FUNC_NAME;
3542 break;
3543 case RID_PRETTY_FUNCTION_NAME:
3544 name = NIC_PRETTY_FUNC;
3545 break;
3546 case RID_C99_FUNCTION_NAME:
3547 name = NIC_C99_FUNC;
3548 break;
3549 default:
3550 gcc_unreachable ();
3551 }
3552
3553 if (cp_parser_non_integral_constant_expression (parser, name))
3554 return error_mark_node;
3555
3556 /* Look up the name. */
3557 return finish_fname (token->u.value);
3558 }
3559
3560 case RID_VA_ARG:
3561 {
3562 tree expression;
3563 tree type;
3564
3565 /* The `__builtin_va_arg' construct is used to handle
3566 `va_arg'. Consume the `__builtin_va_arg' token. */
3567 cp_lexer_consume_token (parser->lexer);
3568 /* Look for the opening `('. */
3569 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3570 /* Now, parse the assignment-expression. */
3571 expression = cp_parser_assignment_expression (parser,
3572 /*cast_p=*/false, NULL);
3573 /* Look for the `,'. */
3574 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3575 /* Parse the type-id. */
3576 type = cp_parser_type_id (parser);
3577 /* Look for the closing `)'. */
3578 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3579 /* Using `va_arg' in a constant-expression is not
3580 allowed. */
3581 if (cp_parser_non_integral_constant_expression (parser,
3582 NIC_VA_ARG))
3583 return error_mark_node;
3584 return build_x_va_arg (expression, type);
3585 }
3586
3587 case RID_OFFSETOF:
3588 return cp_parser_builtin_offsetof (parser);
3589
3590 case RID_HAS_NOTHROW_ASSIGN:
3591 case RID_HAS_NOTHROW_CONSTRUCTOR:
3592 case RID_HAS_NOTHROW_COPY:
3593 case RID_HAS_TRIVIAL_ASSIGN:
3594 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3595 case RID_HAS_TRIVIAL_COPY:
3596 case RID_HAS_TRIVIAL_DESTRUCTOR:
3597 case RID_HAS_VIRTUAL_DESTRUCTOR:
3598 case RID_IS_ABSTRACT:
3599 case RID_IS_BASE_OF:
3600 case RID_IS_CLASS:
3601 case RID_IS_CONVERTIBLE_TO:
3602 case RID_IS_EMPTY:
3603 case RID_IS_ENUM:
3604 case RID_IS_POD:
3605 case RID_IS_POLYMORPHIC:
3606 case RID_IS_STD_LAYOUT:
3607 case RID_IS_TRIVIAL:
3608 case RID_IS_UNION:
3609 case RID_IS_LITERAL_TYPE:
3610 return cp_parser_trait_expr (parser, token->keyword);
3611
3612 /* Objective-C++ expressions. */
3613 case RID_AT_ENCODE:
3614 case RID_AT_PROTOCOL:
3615 case RID_AT_SELECTOR:
3616 return cp_parser_objc_expression (parser);
3617
3618 case RID_TEMPLATE:
3619 if (parser->in_function_body
3620 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3621 == CPP_LESS))
3622 {
3623 error_at (token->location,
3624 "a template declaration cannot appear at block scope");
3625 cp_parser_skip_to_end_of_block_or_statement (parser);
3626 return error_mark_node;
3627 }
3628 default:
3629 cp_parser_error (parser, "expected primary-expression");
3630 return error_mark_node;
3631 }
3632
3633 /* An id-expression can start with either an identifier, a
3634 `::' as the beginning of a qualified-id, or the "operator"
3635 keyword. */
3636 case CPP_NAME:
3637 case CPP_SCOPE:
3638 case CPP_TEMPLATE_ID:
3639 case CPP_NESTED_NAME_SPECIFIER:
3640 {
3641 tree id_expression;
3642 tree decl;
3643 const char *error_msg;
3644 bool template_p;
3645 bool done;
3646 cp_token *id_expr_token;
3647
3648 id_expression:
3649 /* Parse the id-expression. */
3650 id_expression
3651 = cp_parser_id_expression (parser,
3652 /*template_keyword_p=*/false,
3653 /*check_dependency_p=*/true,
3654 &template_p,
3655 /*declarator_p=*/false,
3656 /*optional_p=*/false);
3657 if (id_expression == error_mark_node)
3658 return error_mark_node;
3659 id_expr_token = token;
3660 token = cp_lexer_peek_token (parser->lexer);
3661 done = (token->type != CPP_OPEN_SQUARE
3662 && token->type != CPP_OPEN_PAREN
3663 && token->type != CPP_DOT
3664 && token->type != CPP_DEREF
3665 && token->type != CPP_PLUS_PLUS
3666 && token->type != CPP_MINUS_MINUS);
3667 /* If we have a template-id, then no further lookup is
3668 required. If the template-id was for a template-class, we
3669 will sometimes have a TYPE_DECL at this point. */
3670 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3671 || TREE_CODE (id_expression) == TYPE_DECL)
3672 decl = id_expression;
3673 /* Look up the name. */
3674 else
3675 {
3676 tree ambiguous_decls;
3677
3678 /* If we already know that this lookup is ambiguous, then
3679 we've already issued an error message; there's no reason
3680 to check again. */
3681 if (id_expr_token->type == CPP_NAME
3682 && id_expr_token->ambiguous_p)
3683 {
3684 cp_parser_simulate_error (parser);
3685 return error_mark_node;
3686 }
3687
3688 decl = cp_parser_lookup_name (parser, id_expression,
3689 none_type,
3690 template_p,
3691 /*is_namespace=*/false,
3692 /*check_dependency=*/true,
3693 &ambiguous_decls,
3694 id_expr_token->location);
3695 /* If the lookup was ambiguous, an error will already have
3696 been issued. */
3697 if (ambiguous_decls)
3698 return error_mark_node;
3699
3700 /* In Objective-C++, we may have an Objective-C 2.0
3701 dot-syntax for classes here. */
3702 if (c_dialect_objc ()
3703 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3704 && TREE_CODE (decl) == TYPE_DECL
3705 && objc_is_class_name (decl))
3706 {
3707 tree component;
3708 cp_lexer_consume_token (parser->lexer);
3709 component = cp_parser_identifier (parser);
3710 if (component == error_mark_node)
3711 return error_mark_node;
3712
3713 return objc_build_class_component_ref (id_expression, component);
3714 }
3715
3716 /* In Objective-C++, an instance variable (ivar) may be preferred
3717 to whatever cp_parser_lookup_name() found. */
3718 decl = objc_lookup_ivar (decl, id_expression);
3719
3720 /* If name lookup gives us a SCOPE_REF, then the
3721 qualifying scope was dependent. */
3722 if (TREE_CODE (decl) == SCOPE_REF)
3723 {
3724 /* At this point, we do not know if DECL is a valid
3725 integral constant expression. We assume that it is
3726 in fact such an expression, so that code like:
3727
3728 template <int N> struct A {
3729 int a[B<N>::i];
3730 };
3731
3732 is accepted. At template-instantiation time, we
3733 will check that B<N>::i is actually a constant. */
3734 return decl;
3735 }
3736 /* Check to see if DECL is a local variable in a context
3737 where that is forbidden. */
3738 if (parser->local_variables_forbidden_p
3739 && local_variable_p (decl))
3740 {
3741 /* It might be that we only found DECL because we are
3742 trying to be generous with pre-ISO scoping rules.
3743 For example, consider:
3744
3745 int i;
3746 void g() {
3747 for (int i = 0; i < 10; ++i) {}
3748 extern void f(int j = i);
3749 }
3750
3751 Here, name look up will originally find the out
3752 of scope `i'. We need to issue a warning message,
3753 but then use the global `i'. */
3754 decl = check_for_out_of_scope_variable (decl);
3755 if (local_variable_p (decl))
3756 {
3757 error_at (id_expr_token->location,
3758 "local variable %qD may not appear in this context",
3759 decl);
3760 return error_mark_node;
3761 }
3762 }
3763 }
3764
3765 decl = (finish_id_expression
3766 (id_expression, decl, parser->scope,
3767 idk,
3768 parser->integral_constant_expression_p,
3769 parser->allow_non_integral_constant_expression_p,
3770 &parser->non_integral_constant_expression_p,
3771 template_p, done, address_p,
3772 template_arg_p,
3773 &error_msg,
3774 id_expr_token->location));
3775 if (error_msg)
3776 cp_parser_error (parser, error_msg);
3777 return decl;
3778 }
3779
3780 /* Anything else is an error. */
3781 default:
3782 cp_parser_error (parser, "expected primary-expression");
3783 return error_mark_node;
3784 }
3785 }
3786
3787 /* Parse an id-expression.
3788
3789 id-expression:
3790 unqualified-id
3791 qualified-id
3792
3793 qualified-id:
3794 :: [opt] nested-name-specifier template [opt] unqualified-id
3795 :: identifier
3796 :: operator-function-id
3797 :: template-id
3798
3799 Return a representation of the unqualified portion of the
3800 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3801 a `::' or nested-name-specifier.
3802
3803 Often, if the id-expression was a qualified-id, the caller will
3804 want to make a SCOPE_REF to represent the qualified-id. This
3805 function does not do this in order to avoid wastefully creating
3806 SCOPE_REFs when they are not required.
3807
3808 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3809 `template' keyword.
3810
3811 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3812 uninstantiated templates.
3813
3814 If *TEMPLATE_P is non-NULL, it is set to true iff the
3815 `template' keyword is used to explicitly indicate that the entity
3816 named is a template.
3817
3818 If DECLARATOR_P is true, the id-expression is appearing as part of
3819 a declarator, rather than as part of an expression. */
3820
3821 static tree
3822 cp_parser_id_expression (cp_parser *parser,
3823 bool template_keyword_p,
3824 bool check_dependency_p,
3825 bool *template_p,
3826 bool declarator_p,
3827 bool optional_p)
3828 {
3829 bool global_scope_p;
3830 bool nested_name_specifier_p;
3831
3832 /* Assume the `template' keyword was not used. */
3833 if (template_p)
3834 *template_p = template_keyword_p;
3835
3836 /* Look for the optional `::' operator. */
3837 global_scope_p
3838 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3839 != NULL_TREE);
3840 /* Look for the optional nested-name-specifier. */
3841 nested_name_specifier_p
3842 = (cp_parser_nested_name_specifier_opt (parser,
3843 /*typename_keyword_p=*/false,
3844 check_dependency_p,
3845 /*type_p=*/false,
3846 declarator_p)
3847 != NULL_TREE);
3848 /* If there is a nested-name-specifier, then we are looking at
3849 the first qualified-id production. */
3850 if (nested_name_specifier_p)
3851 {
3852 tree saved_scope;
3853 tree saved_object_scope;
3854 tree saved_qualifying_scope;
3855 tree unqualified_id;
3856 bool is_template;
3857
3858 /* See if the next token is the `template' keyword. */
3859 if (!template_p)
3860 template_p = &is_template;
3861 *template_p = cp_parser_optional_template_keyword (parser);
3862 /* Name lookup we do during the processing of the
3863 unqualified-id might obliterate SCOPE. */
3864 saved_scope = parser->scope;
3865 saved_object_scope = parser->object_scope;
3866 saved_qualifying_scope = parser->qualifying_scope;
3867 /* Process the final unqualified-id. */
3868 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3869 check_dependency_p,
3870 declarator_p,
3871 /*optional_p=*/false);
3872 /* Restore the SAVED_SCOPE for our caller. */
3873 parser->scope = saved_scope;
3874 parser->object_scope = saved_object_scope;
3875 parser->qualifying_scope = saved_qualifying_scope;
3876
3877 return unqualified_id;
3878 }
3879 /* Otherwise, if we are in global scope, then we are looking at one
3880 of the other qualified-id productions. */
3881 else if (global_scope_p)
3882 {
3883 cp_token *token;
3884 tree id;
3885
3886 /* Peek at the next token. */
3887 token = cp_lexer_peek_token (parser->lexer);
3888
3889 /* If it's an identifier, and the next token is not a "<", then
3890 we can avoid the template-id case. This is an optimization
3891 for this common case. */
3892 if (token->type == CPP_NAME
3893 && !cp_parser_nth_token_starts_template_argument_list_p
3894 (parser, 2))
3895 return cp_parser_identifier (parser);
3896
3897 cp_parser_parse_tentatively (parser);
3898 /* Try a template-id. */
3899 id = cp_parser_template_id (parser,
3900 /*template_keyword_p=*/false,
3901 /*check_dependency_p=*/true,
3902 declarator_p);
3903 /* If that worked, we're done. */
3904 if (cp_parser_parse_definitely (parser))
3905 return id;
3906
3907 /* Peek at the next token. (Changes in the token buffer may
3908 have invalidated the pointer obtained above.) */
3909 token = cp_lexer_peek_token (parser->lexer);
3910
3911 switch (token->type)
3912 {
3913 case CPP_NAME:
3914 return cp_parser_identifier (parser);
3915
3916 case CPP_KEYWORD:
3917 if (token->keyword == RID_OPERATOR)
3918 return cp_parser_operator_function_id (parser);
3919 /* Fall through. */
3920
3921 default:
3922 cp_parser_error (parser, "expected id-expression");
3923 return error_mark_node;
3924 }
3925 }
3926 else
3927 return cp_parser_unqualified_id (parser, template_keyword_p,
3928 /*check_dependency_p=*/true,
3929 declarator_p,
3930 optional_p);
3931 }
3932
3933 /* Parse an unqualified-id.
3934
3935 unqualified-id:
3936 identifier
3937 operator-function-id
3938 conversion-function-id
3939 ~ class-name
3940 template-id
3941
3942 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3943 keyword, in a construct like `A::template ...'.
3944
3945 Returns a representation of unqualified-id. For the `identifier'
3946 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3947 production a BIT_NOT_EXPR is returned; the operand of the
3948 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3949 other productions, see the documentation accompanying the
3950 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3951 names are looked up in uninstantiated templates. If DECLARATOR_P
3952 is true, the unqualified-id is appearing as part of a declarator,
3953 rather than as part of an expression. */
3954
3955 static tree
3956 cp_parser_unqualified_id (cp_parser* parser,
3957 bool template_keyword_p,
3958 bool check_dependency_p,
3959 bool declarator_p,
3960 bool optional_p)
3961 {
3962 cp_token *token;
3963
3964 /* Peek at the next token. */
3965 token = cp_lexer_peek_token (parser->lexer);
3966
3967 switch (token->type)
3968 {
3969 case CPP_NAME:
3970 {
3971 tree id;
3972
3973 /* We don't know yet whether or not this will be a
3974 template-id. */
3975 cp_parser_parse_tentatively (parser);
3976 /* Try a template-id. */
3977 id = cp_parser_template_id (parser, template_keyword_p,
3978 check_dependency_p,
3979 declarator_p);
3980 /* If it worked, we're done. */
3981 if (cp_parser_parse_definitely (parser))
3982 return id;
3983 /* Otherwise, it's an ordinary identifier. */
3984 return cp_parser_identifier (parser);
3985 }
3986
3987 case CPP_TEMPLATE_ID:
3988 return cp_parser_template_id (parser, template_keyword_p,
3989 check_dependency_p,
3990 declarator_p);
3991
3992 case CPP_COMPL:
3993 {
3994 tree type_decl;
3995 tree qualifying_scope;
3996 tree object_scope;
3997 tree scope;
3998 bool done;
3999
4000 /* Consume the `~' token. */
4001 cp_lexer_consume_token (parser->lexer);
4002 /* Parse the class-name. The standard, as written, seems to
4003 say that:
4004
4005 template <typename T> struct S { ~S (); };
4006 template <typename T> S<T>::~S() {}
4007
4008 is invalid, since `~' must be followed by a class-name, but
4009 `S<T>' is dependent, and so not known to be a class.
4010 That's not right; we need to look in uninstantiated
4011 templates. A further complication arises from:
4012
4013 template <typename T> void f(T t) {
4014 t.T::~T();
4015 }
4016
4017 Here, it is not possible to look up `T' in the scope of `T'
4018 itself. We must look in both the current scope, and the
4019 scope of the containing complete expression.
4020
4021 Yet another issue is:
4022
4023 struct S {
4024 int S;
4025 ~S();
4026 };
4027
4028 S::~S() {}
4029
4030 The standard does not seem to say that the `S' in `~S'
4031 should refer to the type `S' and not the data member
4032 `S::S'. */
4033
4034 /* DR 244 says that we look up the name after the "~" in the
4035 same scope as we looked up the qualifying name. That idea
4036 isn't fully worked out; it's more complicated than that. */
4037 scope = parser->scope;
4038 object_scope = parser->object_scope;
4039 qualifying_scope = parser->qualifying_scope;
4040
4041 /* Check for invalid scopes. */
4042 if (scope == error_mark_node)
4043 {
4044 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4045 cp_lexer_consume_token (parser->lexer);
4046 return error_mark_node;
4047 }
4048 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4049 {
4050 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4051 error_at (token->location,
4052 "scope %qT before %<~%> is not a class-name",
4053 scope);
4054 cp_parser_simulate_error (parser);
4055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4056 cp_lexer_consume_token (parser->lexer);
4057 return error_mark_node;
4058 }
4059 gcc_assert (!scope || TYPE_P (scope));
4060
4061 /* If the name is of the form "X::~X" it's OK even if X is a
4062 typedef. */
4063 token = cp_lexer_peek_token (parser->lexer);
4064 if (scope
4065 && token->type == CPP_NAME
4066 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4067 != CPP_LESS)
4068 && (token->u.value == TYPE_IDENTIFIER (scope)
4069 || constructor_name_p (token->u.value, scope)))
4070 {
4071 cp_lexer_consume_token (parser->lexer);
4072 return build_nt (BIT_NOT_EXPR, scope);
4073 }
4074
4075 /* If there was an explicit qualification (S::~T), first look
4076 in the scope given by the qualification (i.e., S).
4077
4078 Note: in the calls to cp_parser_class_name below we pass
4079 typename_type so that lookup finds the injected-class-name
4080 rather than the constructor. */
4081 done = false;
4082 type_decl = NULL_TREE;
4083 if (scope)
4084 {
4085 cp_parser_parse_tentatively (parser);
4086 type_decl = cp_parser_class_name (parser,
4087 /*typename_keyword_p=*/false,
4088 /*template_keyword_p=*/false,
4089 typename_type,
4090 /*check_dependency=*/false,
4091 /*class_head_p=*/false,
4092 declarator_p);
4093 if (cp_parser_parse_definitely (parser))
4094 done = true;
4095 }
4096 /* In "N::S::~S", look in "N" as well. */
4097 if (!done && scope && qualifying_scope)
4098 {
4099 cp_parser_parse_tentatively (parser);
4100 parser->scope = qualifying_scope;
4101 parser->object_scope = NULL_TREE;
4102 parser->qualifying_scope = NULL_TREE;
4103 type_decl
4104 = cp_parser_class_name (parser,
4105 /*typename_keyword_p=*/false,
4106 /*template_keyword_p=*/false,
4107 typename_type,
4108 /*check_dependency=*/false,
4109 /*class_head_p=*/false,
4110 declarator_p);
4111 if (cp_parser_parse_definitely (parser))
4112 done = true;
4113 }
4114 /* In "p->S::~T", look in the scope given by "*p" as well. */
4115 else if (!done && object_scope)
4116 {
4117 cp_parser_parse_tentatively (parser);
4118 parser->scope = object_scope;
4119 parser->object_scope = NULL_TREE;
4120 parser->qualifying_scope = NULL_TREE;
4121 type_decl
4122 = cp_parser_class_name (parser,
4123 /*typename_keyword_p=*/false,
4124 /*template_keyword_p=*/false,
4125 typename_type,
4126 /*check_dependency=*/false,
4127 /*class_head_p=*/false,
4128 declarator_p);
4129 if (cp_parser_parse_definitely (parser))
4130 done = true;
4131 }
4132 /* Look in the surrounding context. */
4133 if (!done)
4134 {
4135 parser->scope = NULL_TREE;
4136 parser->object_scope = NULL_TREE;
4137 parser->qualifying_scope = NULL_TREE;
4138 if (processing_template_decl)
4139 cp_parser_parse_tentatively (parser);
4140 type_decl
4141 = cp_parser_class_name (parser,
4142 /*typename_keyword_p=*/false,
4143 /*template_keyword_p=*/false,
4144 typename_type,
4145 /*check_dependency=*/false,
4146 /*class_head_p=*/false,
4147 declarator_p);
4148 if (processing_template_decl
4149 && ! cp_parser_parse_definitely (parser))
4150 {
4151 /* We couldn't find a type with this name, so just accept
4152 it and check for a match at instantiation time. */
4153 type_decl = cp_parser_identifier (parser);
4154 if (type_decl != error_mark_node)
4155 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4156 return type_decl;
4157 }
4158 }
4159 /* If an error occurred, assume that the name of the
4160 destructor is the same as the name of the qualifying
4161 class. That allows us to keep parsing after running
4162 into ill-formed destructor names. */
4163 if (type_decl == error_mark_node && scope)
4164 return build_nt (BIT_NOT_EXPR, scope);
4165 else if (type_decl == error_mark_node)
4166 return error_mark_node;
4167
4168 /* Check that destructor name and scope match. */
4169 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4170 {
4171 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4172 error_at (token->location,
4173 "declaration of %<~%T%> as member of %qT",
4174 type_decl, scope);
4175 cp_parser_simulate_error (parser);
4176 return error_mark_node;
4177 }
4178
4179 /* [class.dtor]
4180
4181 A typedef-name that names a class shall not be used as the
4182 identifier in the declarator for a destructor declaration. */
4183 if (declarator_p
4184 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4185 && !DECL_SELF_REFERENCE_P (type_decl)
4186 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4187 error_at (token->location,
4188 "typedef-name %qD used as destructor declarator",
4189 type_decl);
4190
4191 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4192 }
4193
4194 case CPP_KEYWORD:
4195 if (token->keyword == RID_OPERATOR)
4196 {
4197 tree id;
4198
4199 /* This could be a template-id, so we try that first. */
4200 cp_parser_parse_tentatively (parser);
4201 /* Try a template-id. */
4202 id = cp_parser_template_id (parser, template_keyword_p,
4203 /*check_dependency_p=*/true,
4204 declarator_p);
4205 /* If that worked, we're done. */
4206 if (cp_parser_parse_definitely (parser))
4207 return id;
4208 /* We still don't know whether we're looking at an
4209 operator-function-id or a conversion-function-id. */
4210 cp_parser_parse_tentatively (parser);
4211 /* Try an operator-function-id. */
4212 id = cp_parser_operator_function_id (parser);
4213 /* If that didn't work, try a conversion-function-id. */
4214 if (!cp_parser_parse_definitely (parser))
4215 id = cp_parser_conversion_function_id (parser);
4216
4217 return id;
4218 }
4219 /* Fall through. */
4220
4221 default:
4222 if (optional_p)
4223 return NULL_TREE;
4224 cp_parser_error (parser, "expected unqualified-id");
4225 return error_mark_node;
4226 }
4227 }
4228
4229 /* Parse an (optional) nested-name-specifier.
4230
4231 nested-name-specifier: [C++98]
4232 class-or-namespace-name :: nested-name-specifier [opt]
4233 class-or-namespace-name :: template nested-name-specifier [opt]
4234
4235 nested-name-specifier: [C++0x]
4236 type-name ::
4237 namespace-name ::
4238 nested-name-specifier identifier ::
4239 nested-name-specifier template [opt] simple-template-id ::
4240
4241 PARSER->SCOPE should be set appropriately before this function is
4242 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4243 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4244 in name lookups.
4245
4246 Sets PARSER->SCOPE to the class (TYPE) or namespace
4247 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4248 it unchanged if there is no nested-name-specifier. Returns the new
4249 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4250
4251 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4252 part of a declaration and/or decl-specifier. */
4253
4254 static tree
4255 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4256 bool typename_keyword_p,
4257 bool check_dependency_p,
4258 bool type_p,
4259 bool is_declaration)
4260 {
4261 bool success = false;
4262 cp_token_position start = 0;
4263 cp_token *token;
4264
4265 /* Remember where the nested-name-specifier starts. */
4266 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4267 {
4268 start = cp_lexer_token_position (parser->lexer, false);
4269 push_deferring_access_checks (dk_deferred);
4270 }
4271
4272 while (true)
4273 {
4274 tree new_scope;
4275 tree old_scope;
4276 tree saved_qualifying_scope;
4277 bool template_keyword_p;
4278
4279 /* Spot cases that cannot be the beginning of a
4280 nested-name-specifier. */
4281 token = cp_lexer_peek_token (parser->lexer);
4282
4283 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4284 the already parsed nested-name-specifier. */
4285 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4286 {
4287 /* Grab the nested-name-specifier and continue the loop. */
4288 cp_parser_pre_parsed_nested_name_specifier (parser);
4289 /* If we originally encountered this nested-name-specifier
4290 with IS_DECLARATION set to false, we will not have
4291 resolved TYPENAME_TYPEs, so we must do so here. */
4292 if (is_declaration
4293 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4294 {
4295 new_scope = resolve_typename_type (parser->scope,
4296 /*only_current_p=*/false);
4297 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4298 parser->scope = new_scope;
4299 }
4300 success = true;
4301 continue;
4302 }
4303
4304 /* Spot cases that cannot be the beginning of a
4305 nested-name-specifier. On the second and subsequent times
4306 through the loop, we look for the `template' keyword. */
4307 if (success && token->keyword == RID_TEMPLATE)
4308 ;
4309 /* A template-id can start a nested-name-specifier. */
4310 else if (token->type == CPP_TEMPLATE_ID)
4311 ;
4312 else
4313 {
4314 /* If the next token is not an identifier, then it is
4315 definitely not a type-name or namespace-name. */
4316 if (token->type != CPP_NAME)
4317 break;
4318 /* If the following token is neither a `<' (to begin a
4319 template-id), nor a `::', then we are not looking at a
4320 nested-name-specifier. */
4321 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4322
4323 if (token->type == CPP_COLON
4324 && parser->colon_corrects_to_scope_p
4325 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4326 {
4327 error_at (token->location,
4328 "found %<:%> in nested-name-specifier, expected %<::%>");
4329 token->type = CPP_SCOPE;
4330 }
4331
4332 if (token->type != CPP_SCOPE
4333 && !cp_parser_nth_token_starts_template_argument_list_p
4334 (parser, 2))
4335 break;
4336 }
4337
4338 /* The nested-name-specifier is optional, so we parse
4339 tentatively. */
4340 cp_parser_parse_tentatively (parser);
4341
4342 /* Look for the optional `template' keyword, if this isn't the
4343 first time through the loop. */
4344 if (success)
4345 template_keyword_p = cp_parser_optional_template_keyword (parser);
4346 else
4347 template_keyword_p = false;
4348
4349 /* Save the old scope since the name lookup we are about to do
4350 might destroy it. */
4351 old_scope = parser->scope;
4352 saved_qualifying_scope = parser->qualifying_scope;
4353 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4354 look up names in "X<T>::I" in order to determine that "Y" is
4355 a template. So, if we have a typename at this point, we make
4356 an effort to look through it. */
4357 if (is_declaration
4358 && !typename_keyword_p
4359 && parser->scope
4360 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4361 parser->scope = resolve_typename_type (parser->scope,
4362 /*only_current_p=*/false);
4363 /* Parse the qualifying entity. */
4364 new_scope
4365 = cp_parser_qualifying_entity (parser,
4366 typename_keyword_p,
4367 template_keyword_p,
4368 check_dependency_p,
4369 type_p,
4370 is_declaration);
4371 /* Look for the `::' token. */
4372 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4373
4374 /* If we found what we wanted, we keep going; otherwise, we're
4375 done. */
4376 if (!cp_parser_parse_definitely (parser))
4377 {
4378 bool error_p = false;
4379
4380 /* Restore the OLD_SCOPE since it was valid before the
4381 failed attempt at finding the last
4382 class-or-namespace-name. */
4383 parser->scope = old_scope;
4384 parser->qualifying_scope = saved_qualifying_scope;
4385 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4386 break;
4387 /* If the next token is an identifier, and the one after
4388 that is a `::', then any valid interpretation would have
4389 found a class-or-namespace-name. */
4390 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4391 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4392 == CPP_SCOPE)
4393 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4394 != CPP_COMPL))
4395 {
4396 token = cp_lexer_consume_token (parser->lexer);
4397 if (!error_p)
4398 {
4399 if (!token->ambiguous_p)
4400 {
4401 tree decl;
4402 tree ambiguous_decls;
4403
4404 decl = cp_parser_lookup_name (parser, token->u.value,
4405 none_type,
4406 /*is_template=*/false,
4407 /*is_namespace=*/false,
4408 /*check_dependency=*/true,
4409 &ambiguous_decls,
4410 token->location);
4411 if (TREE_CODE (decl) == TEMPLATE_DECL)
4412 error_at (token->location,
4413 "%qD used without template parameters",
4414 decl);
4415 else if (ambiguous_decls)
4416 {
4417 error_at (token->location,
4418 "reference to %qD is ambiguous",
4419 token->u.value);
4420 print_candidates (ambiguous_decls);
4421 decl = error_mark_node;
4422 }
4423 else
4424 {
4425 if (cxx_dialect != cxx98)
4426 cp_parser_name_lookup_error
4427 (parser, token->u.value, decl, NLE_NOT_CXX98,
4428 token->location);
4429 else
4430 cp_parser_name_lookup_error
4431 (parser, token->u.value, decl, NLE_CXX98,
4432 token->location);
4433 }
4434 }
4435 parser->scope = error_mark_node;
4436 error_p = true;
4437 /* Treat this as a successful nested-name-specifier
4438 due to:
4439
4440 [basic.lookup.qual]
4441
4442 If the name found is not a class-name (clause
4443 _class_) or namespace-name (_namespace.def_), the
4444 program is ill-formed. */
4445 success = true;
4446 }
4447 cp_lexer_consume_token (parser->lexer);
4448 }
4449 break;
4450 }
4451 /* We've found one valid nested-name-specifier. */
4452 success = true;
4453 /* Name lookup always gives us a DECL. */
4454 if (TREE_CODE (new_scope) == TYPE_DECL)
4455 new_scope = TREE_TYPE (new_scope);
4456 /* Uses of "template" must be followed by actual templates. */
4457 if (template_keyword_p
4458 && !(CLASS_TYPE_P (new_scope)
4459 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4460 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4461 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4462 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4463 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4464 == TEMPLATE_ID_EXPR)))
4465 permerror (input_location, TYPE_P (new_scope)
4466 ? "%qT is not a template"
4467 : "%qD is not a template",
4468 new_scope);
4469 /* If it is a class scope, try to complete it; we are about to
4470 be looking up names inside the class. */
4471 if (TYPE_P (new_scope)
4472 /* Since checking types for dependency can be expensive,
4473 avoid doing it if the type is already complete. */
4474 && !COMPLETE_TYPE_P (new_scope)
4475 /* Do not try to complete dependent types. */
4476 && !dependent_type_p (new_scope))
4477 {
4478 new_scope = complete_type (new_scope);
4479 /* If it is a typedef to current class, use the current
4480 class instead, as the typedef won't have any names inside
4481 it yet. */
4482 if (!COMPLETE_TYPE_P (new_scope)
4483 && currently_open_class (new_scope))
4484 new_scope = TYPE_MAIN_VARIANT (new_scope);
4485 }
4486 /* Make sure we look in the right scope the next time through
4487 the loop. */
4488 parser->scope = new_scope;
4489 }
4490
4491 /* If parsing tentatively, replace the sequence of tokens that makes
4492 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4493 token. That way, should we re-parse the token stream, we will
4494 not have to repeat the effort required to do the parse, nor will
4495 we issue duplicate error messages. */
4496 if (success && start)
4497 {
4498 cp_token *token;
4499
4500 token = cp_lexer_token_at (parser->lexer, start);
4501 /* Reset the contents of the START token. */
4502 token->type = CPP_NESTED_NAME_SPECIFIER;
4503 /* Retrieve any deferred checks. Do not pop this access checks yet
4504 so the memory will not be reclaimed during token replacing below. */
4505 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4506 token->u.tree_check_value->value = parser->scope;
4507 token->u.tree_check_value->checks = get_deferred_access_checks ();
4508 token->u.tree_check_value->qualifying_scope =
4509 parser->qualifying_scope;
4510 token->keyword = RID_MAX;
4511
4512 /* Purge all subsequent tokens. */
4513 cp_lexer_purge_tokens_after (parser->lexer, start);
4514 }
4515
4516 if (start)
4517 pop_to_parent_deferring_access_checks ();
4518
4519 return success ? parser->scope : NULL_TREE;
4520 }
4521
4522 /* Parse a nested-name-specifier. See
4523 cp_parser_nested_name_specifier_opt for details. This function
4524 behaves identically, except that it will an issue an error if no
4525 nested-name-specifier is present. */
4526
4527 static tree
4528 cp_parser_nested_name_specifier (cp_parser *parser,
4529 bool typename_keyword_p,
4530 bool check_dependency_p,
4531 bool type_p,
4532 bool is_declaration)
4533 {
4534 tree scope;
4535
4536 /* Look for the nested-name-specifier. */
4537 scope = cp_parser_nested_name_specifier_opt (parser,
4538 typename_keyword_p,
4539 check_dependency_p,
4540 type_p,
4541 is_declaration);
4542 /* If it was not present, issue an error message. */
4543 if (!scope)
4544 {
4545 cp_parser_error (parser, "expected nested-name-specifier");
4546 parser->scope = NULL_TREE;
4547 }
4548
4549 return scope;
4550 }
4551
4552 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4553 this is either a class-name or a namespace-name (which corresponds
4554 to the class-or-namespace-name production in the grammar). For
4555 C++0x, it can also be a type-name that refers to an enumeration
4556 type.
4557
4558 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4559 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4560 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4561 TYPE_P is TRUE iff the next name should be taken as a class-name,
4562 even the same name is declared to be another entity in the same
4563 scope.
4564
4565 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4566 specified by the class-or-namespace-name. If neither is found the
4567 ERROR_MARK_NODE is returned. */
4568
4569 static tree
4570 cp_parser_qualifying_entity (cp_parser *parser,
4571 bool typename_keyword_p,
4572 bool template_keyword_p,
4573 bool check_dependency_p,
4574 bool type_p,
4575 bool is_declaration)
4576 {
4577 tree saved_scope;
4578 tree saved_qualifying_scope;
4579 tree saved_object_scope;
4580 tree scope;
4581 bool only_class_p;
4582 bool successful_parse_p;
4583
4584 /* Before we try to parse the class-name, we must save away the
4585 current PARSER->SCOPE since cp_parser_class_name will destroy
4586 it. */
4587 saved_scope = parser->scope;
4588 saved_qualifying_scope = parser->qualifying_scope;
4589 saved_object_scope = parser->object_scope;
4590 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4591 there is no need to look for a namespace-name. */
4592 only_class_p = template_keyword_p
4593 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4594 if (!only_class_p)
4595 cp_parser_parse_tentatively (parser);
4596 scope = cp_parser_class_name (parser,
4597 typename_keyword_p,
4598 template_keyword_p,
4599 type_p ? class_type : none_type,
4600 check_dependency_p,
4601 /*class_head_p=*/false,
4602 is_declaration);
4603 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4604 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4605 if (!only_class_p
4606 && cxx_dialect != cxx98
4607 && !successful_parse_p)
4608 {
4609 /* Restore the saved scope. */
4610 parser->scope = saved_scope;
4611 parser->qualifying_scope = saved_qualifying_scope;
4612 parser->object_scope = saved_object_scope;
4613
4614 /* Parse tentatively. */
4615 cp_parser_parse_tentatively (parser);
4616
4617 /* Parse a typedef-name or enum-name. */
4618 scope = cp_parser_nonclass_name (parser);
4619
4620 /* "If the name found does not designate a namespace or a class,
4621 enumeration, or dependent type, the program is ill-formed."
4622
4623 We cover classes and dependent types above and namespaces below,
4624 so this code is only looking for enums. */
4625 if (!scope || TREE_CODE (scope) != TYPE_DECL
4626 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4627 cp_parser_simulate_error (parser);
4628
4629 successful_parse_p = cp_parser_parse_definitely (parser);
4630 }
4631 /* If that didn't work, try for a namespace-name. */
4632 if (!only_class_p && !successful_parse_p)
4633 {
4634 /* Restore the saved scope. */
4635 parser->scope = saved_scope;
4636 parser->qualifying_scope = saved_qualifying_scope;
4637 parser->object_scope = saved_object_scope;
4638 /* If we are not looking at an identifier followed by the scope
4639 resolution operator, then this is not part of a
4640 nested-name-specifier. (Note that this function is only used
4641 to parse the components of a nested-name-specifier.) */
4642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4643 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4644 return error_mark_node;
4645 scope = cp_parser_namespace_name (parser);
4646 }
4647
4648 return scope;
4649 }
4650
4651 /* Parse a postfix-expression.
4652
4653 postfix-expression:
4654 primary-expression
4655 postfix-expression [ expression ]
4656 postfix-expression ( expression-list [opt] )
4657 simple-type-specifier ( expression-list [opt] )
4658 typename :: [opt] nested-name-specifier identifier
4659 ( expression-list [opt] )
4660 typename :: [opt] nested-name-specifier template [opt] template-id
4661 ( expression-list [opt] )
4662 postfix-expression . template [opt] id-expression
4663 postfix-expression -> template [opt] id-expression
4664 postfix-expression . pseudo-destructor-name
4665 postfix-expression -> pseudo-destructor-name
4666 postfix-expression ++
4667 postfix-expression --
4668 dynamic_cast < type-id > ( expression )
4669 static_cast < type-id > ( expression )
4670 reinterpret_cast < type-id > ( expression )
4671 const_cast < type-id > ( expression )
4672 typeid ( expression )
4673 typeid ( type-id )
4674
4675 GNU Extension:
4676
4677 postfix-expression:
4678 ( type-id ) { initializer-list , [opt] }
4679
4680 This extension is a GNU version of the C99 compound-literal
4681 construct. (The C99 grammar uses `type-name' instead of `type-id',
4682 but they are essentially the same concept.)
4683
4684 If ADDRESS_P is true, the postfix expression is the operand of the
4685 `&' operator. CAST_P is true if this expression is the target of a
4686 cast.
4687
4688 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4689 class member access expressions [expr.ref].
4690
4691 Returns a representation of the expression. */
4692
4693 static tree
4694 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4695 bool member_access_only_p,
4696 cp_id_kind * pidk_return)
4697 {
4698 cp_token *token;
4699 enum rid keyword;
4700 cp_id_kind idk = CP_ID_KIND_NONE;
4701 tree postfix_expression = NULL_TREE;
4702 bool is_member_access = false;
4703
4704 /* Peek at the next token. */
4705 token = cp_lexer_peek_token (parser->lexer);
4706 /* Some of the productions are determined by keywords. */
4707 keyword = token->keyword;
4708 switch (keyword)
4709 {
4710 case RID_DYNCAST:
4711 case RID_STATCAST:
4712 case RID_REINTCAST:
4713 case RID_CONSTCAST:
4714 {
4715 tree type;
4716 tree expression;
4717 const char *saved_message;
4718
4719 /* All of these can be handled in the same way from the point
4720 of view of parsing. Begin by consuming the token
4721 identifying the cast. */
4722 cp_lexer_consume_token (parser->lexer);
4723
4724 /* New types cannot be defined in the cast. */
4725 saved_message = parser->type_definition_forbidden_message;
4726 parser->type_definition_forbidden_message
4727 = G_("types may not be defined in casts");
4728
4729 /* Look for the opening `<'. */
4730 cp_parser_require (parser, CPP_LESS, RT_LESS);
4731 /* Parse the type to which we are casting. */
4732 type = cp_parser_type_id (parser);
4733 /* Look for the closing `>'. */
4734 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4735 /* Restore the old message. */
4736 parser->type_definition_forbidden_message = saved_message;
4737
4738 /* And the expression which is being cast. */
4739 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4740 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4741 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4742
4743 /* Only type conversions to integral or enumeration types
4744 can be used in constant-expressions. */
4745 if (!cast_valid_in_integral_constant_expression_p (type)
4746 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4747 return error_mark_node;
4748
4749 switch (keyword)
4750 {
4751 case RID_DYNCAST:
4752 postfix_expression
4753 = build_dynamic_cast (type, expression, tf_warning_or_error);
4754 break;
4755 case RID_STATCAST:
4756 postfix_expression
4757 = build_static_cast (type, expression, tf_warning_or_error);
4758 break;
4759 case RID_REINTCAST:
4760 postfix_expression
4761 = build_reinterpret_cast (type, expression,
4762 tf_warning_or_error);
4763 break;
4764 case RID_CONSTCAST:
4765 postfix_expression
4766 = build_const_cast (type, expression, tf_warning_or_error);
4767 break;
4768 default:
4769 gcc_unreachable ();
4770 }
4771 }
4772 break;
4773
4774 case RID_TYPEID:
4775 {
4776 tree type;
4777 const char *saved_message;
4778 bool saved_in_type_id_in_expr_p;
4779
4780 /* Consume the `typeid' token. */
4781 cp_lexer_consume_token (parser->lexer);
4782 /* Look for the `(' token. */
4783 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4784 /* Types cannot be defined in a `typeid' expression. */
4785 saved_message = parser->type_definition_forbidden_message;
4786 parser->type_definition_forbidden_message
4787 = G_("types may not be defined in a %<typeid%> expression");
4788 /* We can't be sure yet whether we're looking at a type-id or an
4789 expression. */
4790 cp_parser_parse_tentatively (parser);
4791 /* Try a type-id first. */
4792 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4793 parser->in_type_id_in_expr_p = true;
4794 type = cp_parser_type_id (parser);
4795 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4796 /* Look for the `)' token. Otherwise, we can't be sure that
4797 we're not looking at an expression: consider `typeid (int
4798 (3))', for example. */
4799 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4800 /* If all went well, simply lookup the type-id. */
4801 if (cp_parser_parse_definitely (parser))
4802 postfix_expression = get_typeid (type);
4803 /* Otherwise, fall back to the expression variant. */
4804 else
4805 {
4806 tree expression;
4807
4808 /* Look for an expression. */
4809 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4810 /* Compute its typeid. */
4811 postfix_expression = build_typeid (expression);
4812 /* Look for the `)' token. */
4813 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4814 }
4815 /* Restore the saved message. */
4816 parser->type_definition_forbidden_message = saved_message;
4817 /* `typeid' may not appear in an integral constant expression. */
4818 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4819 return error_mark_node;
4820 }
4821 break;
4822
4823 case RID_TYPENAME:
4824 {
4825 tree type;
4826 /* The syntax permitted here is the same permitted for an
4827 elaborated-type-specifier. */
4828 type = cp_parser_elaborated_type_specifier (parser,
4829 /*is_friend=*/false,
4830 /*is_declaration=*/false);
4831 postfix_expression = cp_parser_functional_cast (parser, type);
4832 }
4833 break;
4834
4835 default:
4836 {
4837 tree type;
4838
4839 /* If the next thing is a simple-type-specifier, we may be
4840 looking at a functional cast. We could also be looking at
4841 an id-expression. So, we try the functional cast, and if
4842 that doesn't work we fall back to the primary-expression. */
4843 cp_parser_parse_tentatively (parser);
4844 /* Look for the simple-type-specifier. */
4845 type = cp_parser_simple_type_specifier (parser,
4846 /*decl_specs=*/NULL,
4847 CP_PARSER_FLAGS_NONE);
4848 /* Parse the cast itself. */
4849 if (!cp_parser_error_occurred (parser))
4850 postfix_expression
4851 = cp_parser_functional_cast (parser, type);
4852 /* If that worked, we're done. */
4853 if (cp_parser_parse_definitely (parser))
4854 break;
4855
4856 /* If the functional-cast didn't work out, try a
4857 compound-literal. */
4858 if (cp_parser_allow_gnu_extensions_p (parser)
4859 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4860 {
4861 VEC(constructor_elt,gc) *initializer_list = NULL;
4862 bool saved_in_type_id_in_expr_p;
4863
4864 cp_parser_parse_tentatively (parser);
4865 /* Consume the `('. */
4866 cp_lexer_consume_token (parser->lexer);
4867 /* Parse the type. */
4868 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4869 parser->in_type_id_in_expr_p = true;
4870 type = cp_parser_type_id (parser);
4871 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4872 /* Look for the `)'. */
4873 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4874 /* Look for the `{'. */
4875 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4876 /* If things aren't going well, there's no need to
4877 keep going. */
4878 if (!cp_parser_error_occurred (parser))
4879 {
4880 bool non_constant_p;
4881 /* Parse the initializer-list. */
4882 initializer_list
4883 = cp_parser_initializer_list (parser, &non_constant_p);
4884 /* Allow a trailing `,'. */
4885 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4886 cp_lexer_consume_token (parser->lexer);
4887 /* Look for the final `}'. */
4888 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4889 }
4890 /* If that worked, we're definitely looking at a
4891 compound-literal expression. */
4892 if (cp_parser_parse_definitely (parser))
4893 {
4894 /* Warn the user that a compound literal is not
4895 allowed in standard C++. */
4896 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4897 /* For simplicity, we disallow compound literals in
4898 constant-expressions. We could
4899 allow compound literals of integer type, whose
4900 initializer was a constant, in constant
4901 expressions. Permitting that usage, as a further
4902 extension, would not change the meaning of any
4903 currently accepted programs. (Of course, as
4904 compound literals are not part of ISO C++, the
4905 standard has nothing to say.) */
4906 if (cp_parser_non_integral_constant_expression (parser,
4907 NIC_NCC))
4908 {
4909 postfix_expression = error_mark_node;
4910 break;
4911 }
4912 /* Form the representation of the compound-literal. */
4913 postfix_expression
4914 = (finish_compound_literal
4915 (type, build_constructor (init_list_type_node,
4916 initializer_list),
4917 tf_warning_or_error));
4918 break;
4919 }
4920 }
4921
4922 /* It must be a primary-expression. */
4923 postfix_expression
4924 = cp_parser_primary_expression (parser, address_p, cast_p,
4925 /*template_arg_p=*/false,
4926 &idk);
4927 }
4928 break;
4929 }
4930
4931 /* Keep looping until the postfix-expression is complete. */
4932 while (true)
4933 {
4934 if (idk == CP_ID_KIND_UNQUALIFIED
4935 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4936 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4937 /* It is not a Koenig lookup function call. */
4938 postfix_expression
4939 = unqualified_name_lookup_error (postfix_expression);
4940
4941 /* Peek at the next token. */
4942 token = cp_lexer_peek_token (parser->lexer);
4943
4944 switch (token->type)
4945 {
4946 case CPP_OPEN_SQUARE:
4947 postfix_expression
4948 = cp_parser_postfix_open_square_expression (parser,
4949 postfix_expression,
4950 false);
4951 idk = CP_ID_KIND_NONE;
4952 is_member_access = false;
4953 break;
4954
4955 case CPP_OPEN_PAREN:
4956 /* postfix-expression ( expression-list [opt] ) */
4957 {
4958 bool koenig_p;
4959 bool is_builtin_constant_p;
4960 bool saved_integral_constant_expression_p = false;
4961 bool saved_non_integral_constant_expression_p = false;
4962 VEC(tree,gc) *args;
4963
4964 is_member_access = false;
4965
4966 is_builtin_constant_p
4967 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4968 if (is_builtin_constant_p)
4969 {
4970 /* The whole point of __builtin_constant_p is to allow
4971 non-constant expressions to appear as arguments. */
4972 saved_integral_constant_expression_p
4973 = parser->integral_constant_expression_p;
4974 saved_non_integral_constant_expression_p
4975 = parser->non_integral_constant_expression_p;
4976 parser->integral_constant_expression_p = false;
4977 }
4978 args = (cp_parser_parenthesized_expression_list
4979 (parser, non_attr,
4980 /*cast_p=*/false, /*allow_expansion_p=*/true,
4981 /*non_constant_p=*/NULL));
4982 if (is_builtin_constant_p)
4983 {
4984 parser->integral_constant_expression_p
4985 = saved_integral_constant_expression_p;
4986 parser->non_integral_constant_expression_p
4987 = saved_non_integral_constant_expression_p;
4988 }
4989
4990 if (args == NULL)
4991 {
4992 postfix_expression = error_mark_node;
4993 break;
4994 }
4995
4996 /* Function calls are not permitted in
4997 constant-expressions. */
4998 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4999 && cp_parser_non_integral_constant_expression (parser,
5000 NIC_FUNC_CALL))
5001 {
5002 postfix_expression = error_mark_node;
5003 release_tree_vector (args);
5004 break;
5005 }
5006
5007 koenig_p = false;
5008 if (idk == CP_ID_KIND_UNQUALIFIED
5009 || idk == CP_ID_KIND_TEMPLATE_ID)
5010 {
5011 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5012 {
5013 if (!VEC_empty (tree, args))
5014 {
5015 koenig_p = true;
5016 if (!any_type_dependent_arguments_p (args))
5017 postfix_expression
5018 = perform_koenig_lookup (postfix_expression, args,
5019 /*include_std=*/false);
5020 }
5021 else
5022 postfix_expression
5023 = unqualified_fn_lookup_error (postfix_expression);
5024 }
5025 /* We do not perform argument-dependent lookup if
5026 normal lookup finds a non-function, in accordance
5027 with the expected resolution of DR 218. */
5028 else if (!VEC_empty (tree, args)
5029 && is_overloaded_fn (postfix_expression))
5030 {
5031 tree fn = get_first_fn (postfix_expression);
5032 fn = STRIP_TEMPLATE (fn);
5033
5034 /* Do not do argument dependent lookup if regular
5035 lookup finds a member function or a block-scope
5036 function declaration. [basic.lookup.argdep]/3 */
5037 if (!DECL_FUNCTION_MEMBER_P (fn)
5038 && !DECL_LOCAL_FUNCTION_P (fn))
5039 {
5040 koenig_p = true;
5041 if (!any_type_dependent_arguments_p (args))
5042 postfix_expression
5043 = perform_koenig_lookup (postfix_expression, args,
5044 /*include_std=*/false);
5045 }
5046 }
5047 }
5048
5049 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5050 {
5051 tree instance = TREE_OPERAND (postfix_expression, 0);
5052 tree fn = TREE_OPERAND (postfix_expression, 1);
5053
5054 if (processing_template_decl
5055 && (type_dependent_expression_p (instance)
5056 || (!BASELINK_P (fn)
5057 && TREE_CODE (fn) != FIELD_DECL)
5058 || type_dependent_expression_p (fn)
5059 || any_type_dependent_arguments_p (args)))
5060 {
5061 postfix_expression
5062 = build_nt_call_vec (postfix_expression, args);
5063 release_tree_vector (args);
5064 break;
5065 }
5066
5067 if (BASELINK_P (fn))
5068 {
5069 postfix_expression
5070 = (build_new_method_call
5071 (instance, fn, &args, NULL_TREE,
5072 (idk == CP_ID_KIND_QUALIFIED
5073 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5074 : LOOKUP_NORMAL),
5075 /*fn_p=*/NULL,
5076 tf_warning_or_error));
5077 }
5078 else
5079 postfix_expression
5080 = finish_call_expr (postfix_expression, &args,
5081 /*disallow_virtual=*/false,
5082 /*koenig_p=*/false,
5083 tf_warning_or_error);
5084 }
5085 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5086 || TREE_CODE (postfix_expression) == MEMBER_REF
5087 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5088 postfix_expression = (build_offset_ref_call_from_tree
5089 (postfix_expression, &args));
5090 else if (idk == CP_ID_KIND_QUALIFIED)
5091 /* A call to a static class member, or a namespace-scope
5092 function. */
5093 postfix_expression
5094 = finish_call_expr (postfix_expression, &args,
5095 /*disallow_virtual=*/true,
5096 koenig_p,
5097 tf_warning_or_error);
5098 else
5099 /* All other function calls. */
5100 postfix_expression
5101 = finish_call_expr (postfix_expression, &args,
5102 /*disallow_virtual=*/false,
5103 koenig_p,
5104 tf_warning_or_error);
5105
5106 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5107 idk = CP_ID_KIND_NONE;
5108
5109 release_tree_vector (args);
5110 }
5111 break;
5112
5113 case CPP_DOT:
5114 case CPP_DEREF:
5115 /* postfix-expression . template [opt] id-expression
5116 postfix-expression . pseudo-destructor-name
5117 postfix-expression -> template [opt] id-expression
5118 postfix-expression -> pseudo-destructor-name */
5119
5120 /* Consume the `.' or `->' operator. */
5121 cp_lexer_consume_token (parser->lexer);
5122
5123 postfix_expression
5124 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5125 postfix_expression,
5126 false, &idk,
5127 token->location);
5128
5129 is_member_access = true;
5130 break;
5131
5132 case CPP_PLUS_PLUS:
5133 /* postfix-expression ++ */
5134 /* Consume the `++' token. */
5135 cp_lexer_consume_token (parser->lexer);
5136 /* Generate a representation for the complete expression. */
5137 postfix_expression
5138 = finish_increment_expr (postfix_expression,
5139 POSTINCREMENT_EXPR);
5140 /* Increments may not appear in constant-expressions. */
5141 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5142 postfix_expression = error_mark_node;
5143 idk = CP_ID_KIND_NONE;
5144 is_member_access = false;
5145 break;
5146
5147 case CPP_MINUS_MINUS:
5148 /* postfix-expression -- */
5149 /* Consume the `--' token. */
5150 cp_lexer_consume_token (parser->lexer);
5151 /* Generate a representation for the complete expression. */
5152 postfix_expression
5153 = finish_increment_expr (postfix_expression,
5154 POSTDECREMENT_EXPR);
5155 /* Decrements may not appear in constant-expressions. */
5156 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5157 postfix_expression = error_mark_node;
5158 idk = CP_ID_KIND_NONE;
5159 is_member_access = false;
5160 break;
5161
5162 default:
5163 if (pidk_return != NULL)
5164 * pidk_return = idk;
5165 if (member_access_only_p)
5166 return is_member_access? postfix_expression : error_mark_node;
5167 else
5168 return postfix_expression;
5169 }
5170 }
5171
5172 /* We should never get here. */
5173 gcc_unreachable ();
5174 return error_mark_node;
5175 }
5176
5177 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5178 by cp_parser_builtin_offsetof. We're looking for
5179
5180 postfix-expression [ expression ]
5181
5182 FOR_OFFSETOF is set if we're being called in that context, which
5183 changes how we deal with integer constant expressions. */
5184
5185 static tree
5186 cp_parser_postfix_open_square_expression (cp_parser *parser,
5187 tree postfix_expression,
5188 bool for_offsetof)
5189 {
5190 tree index;
5191
5192 /* Consume the `[' token. */
5193 cp_lexer_consume_token (parser->lexer);
5194
5195 /* Parse the index expression. */
5196 /* ??? For offsetof, there is a question of what to allow here. If
5197 offsetof is not being used in an integral constant expression context,
5198 then we *could* get the right answer by computing the value at runtime.
5199 If we are in an integral constant expression context, then we might
5200 could accept any constant expression; hard to say without analysis.
5201 Rather than open the barn door too wide right away, allow only integer
5202 constant expressions here. */
5203 if (for_offsetof)
5204 index = cp_parser_constant_expression (parser, false, NULL);
5205 else
5206 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5207
5208 /* Look for the closing `]'. */
5209 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5210
5211 /* Build the ARRAY_REF. */
5212 postfix_expression = grok_array_decl (postfix_expression, index);
5213
5214 /* When not doing offsetof, array references are not permitted in
5215 constant-expressions. */
5216 if (!for_offsetof
5217 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5218 postfix_expression = error_mark_node;
5219
5220 return postfix_expression;
5221 }
5222
5223 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5224 by cp_parser_builtin_offsetof. We're looking for
5225
5226 postfix-expression . template [opt] id-expression
5227 postfix-expression . pseudo-destructor-name
5228 postfix-expression -> template [opt] id-expression
5229 postfix-expression -> pseudo-destructor-name
5230
5231 FOR_OFFSETOF is set if we're being called in that context. That sorta
5232 limits what of the above we'll actually accept, but nevermind.
5233 TOKEN_TYPE is the "." or "->" token, which will already have been
5234 removed from the stream. */
5235
5236 static tree
5237 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5238 enum cpp_ttype token_type,
5239 tree postfix_expression,
5240 bool for_offsetof, cp_id_kind *idk,
5241 location_t location)
5242 {
5243 tree name;
5244 bool dependent_p;
5245 bool pseudo_destructor_p;
5246 tree scope = NULL_TREE;
5247
5248 /* If this is a `->' operator, dereference the pointer. */
5249 if (token_type == CPP_DEREF)
5250 postfix_expression = build_x_arrow (postfix_expression);
5251 /* Check to see whether or not the expression is type-dependent. */
5252 dependent_p = type_dependent_expression_p (postfix_expression);
5253 /* The identifier following the `->' or `.' is not qualified. */
5254 parser->scope = NULL_TREE;
5255 parser->qualifying_scope = NULL_TREE;
5256 parser->object_scope = NULL_TREE;
5257 *idk = CP_ID_KIND_NONE;
5258
5259 /* Enter the scope corresponding to the type of the object
5260 given by the POSTFIX_EXPRESSION. */
5261 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5262 {
5263 scope = TREE_TYPE (postfix_expression);
5264 /* According to the standard, no expression should ever have
5265 reference type. Unfortunately, we do not currently match
5266 the standard in this respect in that our internal representation
5267 of an expression may have reference type even when the standard
5268 says it does not. Therefore, we have to manually obtain the
5269 underlying type here. */
5270 scope = non_reference (scope);
5271 /* The type of the POSTFIX_EXPRESSION must be complete. */
5272 if (scope == unknown_type_node)
5273 {
5274 error_at (location, "%qE does not have class type",
5275 postfix_expression);
5276 scope = NULL_TREE;
5277 }
5278 else
5279 scope = complete_type_or_else (scope, NULL_TREE);
5280 /* Let the name lookup machinery know that we are processing a
5281 class member access expression. */
5282 parser->context->object_type = scope;
5283 /* If something went wrong, we want to be able to discern that case,
5284 as opposed to the case where there was no SCOPE due to the type
5285 of expression being dependent. */
5286 if (!scope)
5287 scope = error_mark_node;
5288 /* If the SCOPE was erroneous, make the various semantic analysis
5289 functions exit quickly -- and without issuing additional error
5290 messages. */
5291 if (scope == error_mark_node)
5292 postfix_expression = error_mark_node;
5293 }
5294
5295 /* Assume this expression is not a pseudo-destructor access. */
5296 pseudo_destructor_p = false;
5297
5298 /* If the SCOPE is a scalar type, then, if this is a valid program,
5299 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5300 is type dependent, it can be pseudo-destructor-name or something else.
5301 Try to parse it as pseudo-destructor-name first. */
5302 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5303 {
5304 tree s;
5305 tree type;
5306
5307 cp_parser_parse_tentatively (parser);
5308 /* Parse the pseudo-destructor-name. */
5309 s = NULL_TREE;
5310 cp_parser_pseudo_destructor_name (parser, &s, &type);
5311 if (dependent_p
5312 && (cp_parser_error_occurred (parser)
5313 || TREE_CODE (type) != TYPE_DECL
5314 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5315 cp_parser_abort_tentative_parse (parser);
5316 else if (cp_parser_parse_definitely (parser))
5317 {
5318 pseudo_destructor_p = true;
5319 postfix_expression
5320 = finish_pseudo_destructor_expr (postfix_expression,
5321 s, TREE_TYPE (type));
5322 }
5323 }
5324
5325 if (!pseudo_destructor_p)
5326 {
5327 /* If the SCOPE is not a scalar type, we are looking at an
5328 ordinary class member access expression, rather than a
5329 pseudo-destructor-name. */
5330 bool template_p;
5331 cp_token *token = cp_lexer_peek_token (parser->lexer);
5332 /* Parse the id-expression. */
5333 name = (cp_parser_id_expression
5334 (parser,
5335 cp_parser_optional_template_keyword (parser),
5336 /*check_dependency_p=*/true,
5337 &template_p,
5338 /*declarator_p=*/false,
5339 /*optional_p=*/false));
5340 /* In general, build a SCOPE_REF if the member name is qualified.
5341 However, if the name was not dependent and has already been
5342 resolved; there is no need to build the SCOPE_REF. For example;
5343
5344 struct X { void f(); };
5345 template <typename T> void f(T* t) { t->X::f(); }
5346
5347 Even though "t" is dependent, "X::f" is not and has been resolved
5348 to a BASELINK; there is no need to include scope information. */
5349
5350 /* But we do need to remember that there was an explicit scope for
5351 virtual function calls. */
5352 if (parser->scope)
5353 *idk = CP_ID_KIND_QUALIFIED;
5354
5355 /* If the name is a template-id that names a type, we will get a
5356 TYPE_DECL here. That is invalid code. */
5357 if (TREE_CODE (name) == TYPE_DECL)
5358 {
5359 error_at (token->location, "invalid use of %qD", name);
5360 postfix_expression = error_mark_node;
5361 }
5362 else
5363 {
5364 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5365 {
5366 name = build_qualified_name (/*type=*/NULL_TREE,
5367 parser->scope,
5368 name,
5369 template_p);
5370 parser->scope = NULL_TREE;
5371 parser->qualifying_scope = NULL_TREE;
5372 parser->object_scope = NULL_TREE;
5373 }
5374 if (scope && name && BASELINK_P (name))
5375 adjust_result_of_qualified_name_lookup
5376 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5377 postfix_expression
5378 = finish_class_member_access_expr (postfix_expression, name,
5379 template_p,
5380 tf_warning_or_error);
5381 }
5382 }
5383
5384 /* We no longer need to look up names in the scope of the object on
5385 the left-hand side of the `.' or `->' operator. */
5386 parser->context->object_type = NULL_TREE;
5387
5388 /* Outside of offsetof, these operators may not appear in
5389 constant-expressions. */
5390 if (!for_offsetof
5391 && (cp_parser_non_integral_constant_expression
5392 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5393 postfix_expression = error_mark_node;
5394
5395 return postfix_expression;
5396 }
5397
5398 /* Parse a parenthesized expression-list.
5399
5400 expression-list:
5401 assignment-expression
5402 expression-list, assignment-expression
5403
5404 attribute-list:
5405 expression-list
5406 identifier
5407 identifier, expression-list
5408
5409 CAST_P is true if this expression is the target of a cast.
5410
5411 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5412 argument pack.
5413
5414 Returns a vector of trees. Each element is a representation of an
5415 assignment-expression. NULL is returned if the ( and or ) are
5416 missing. An empty, but allocated, vector is returned on no
5417 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5418 if we are parsing an attribute list for an attribute that wants a
5419 plain identifier argument, normal_attr for an attribute that wants
5420 an expression, or non_attr if we aren't parsing an attribute list. If
5421 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5422 not all of the expressions in the list were constant. */
5423
5424 static VEC(tree,gc) *
5425 cp_parser_parenthesized_expression_list (cp_parser* parser,
5426 int is_attribute_list,
5427 bool cast_p,
5428 bool allow_expansion_p,
5429 bool *non_constant_p)
5430 {
5431 VEC(tree,gc) *expression_list;
5432 bool fold_expr_p = is_attribute_list != non_attr;
5433 tree identifier = NULL_TREE;
5434 bool saved_greater_than_is_operator_p;
5435
5436 /* Assume all the expressions will be constant. */
5437 if (non_constant_p)
5438 *non_constant_p = false;
5439
5440 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5441 return NULL;
5442
5443 expression_list = make_tree_vector ();
5444
5445 /* Within a parenthesized expression, a `>' token is always
5446 the greater-than operator. */
5447 saved_greater_than_is_operator_p
5448 = parser->greater_than_is_operator_p;
5449 parser->greater_than_is_operator_p = true;
5450
5451 /* Consume expressions until there are no more. */
5452 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5453 while (true)
5454 {
5455 tree expr;
5456
5457 /* At the beginning of attribute lists, check to see if the
5458 next token is an identifier. */
5459 if (is_attribute_list == id_attr
5460 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5461 {
5462 cp_token *token;
5463
5464 /* Consume the identifier. */
5465 token = cp_lexer_consume_token (parser->lexer);
5466 /* Save the identifier. */
5467 identifier = token->u.value;
5468 }
5469 else
5470 {
5471 bool expr_non_constant_p;
5472
5473 /* Parse the next assignment-expression. */
5474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5475 {
5476 /* A braced-init-list. */
5477 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5478 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5479 if (non_constant_p && expr_non_constant_p)
5480 *non_constant_p = true;
5481 }
5482 else if (non_constant_p)
5483 {
5484 expr = (cp_parser_constant_expression
5485 (parser, /*allow_non_constant_p=*/true,
5486 &expr_non_constant_p));
5487 if (expr_non_constant_p)
5488 *non_constant_p = true;
5489 }
5490 else
5491 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5492
5493 if (fold_expr_p)
5494 expr = fold_non_dependent_expr (expr);
5495
5496 /* If we have an ellipsis, then this is an expression
5497 expansion. */
5498 if (allow_expansion_p
5499 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5500 {
5501 /* Consume the `...'. */
5502 cp_lexer_consume_token (parser->lexer);
5503
5504 /* Build the argument pack. */
5505 expr = make_pack_expansion (expr);
5506 }
5507
5508 /* Add it to the list. We add error_mark_node
5509 expressions to the list, so that we can still tell if
5510 the correct form for a parenthesized expression-list
5511 is found. That gives better errors. */
5512 VEC_safe_push (tree, gc, expression_list, expr);
5513
5514 if (expr == error_mark_node)
5515 goto skip_comma;
5516 }
5517
5518 /* After the first item, attribute lists look the same as
5519 expression lists. */
5520 is_attribute_list = non_attr;
5521
5522 get_comma:;
5523 /* If the next token isn't a `,', then we are done. */
5524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5525 break;
5526
5527 /* Otherwise, consume the `,' and keep going. */
5528 cp_lexer_consume_token (parser->lexer);
5529 }
5530
5531 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5532 {
5533 int ending;
5534
5535 skip_comma:;
5536 /* We try and resync to an unnested comma, as that will give the
5537 user better diagnostics. */
5538 ending = cp_parser_skip_to_closing_parenthesis (parser,
5539 /*recovering=*/true,
5540 /*or_comma=*/true,
5541 /*consume_paren=*/true);
5542 if (ending < 0)
5543 goto get_comma;
5544 if (!ending)
5545 {
5546 parser->greater_than_is_operator_p
5547 = saved_greater_than_is_operator_p;
5548 return NULL;
5549 }
5550 }
5551
5552 parser->greater_than_is_operator_p
5553 = saved_greater_than_is_operator_p;
5554
5555 if (identifier)
5556 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5557
5558 return expression_list;
5559 }
5560
5561 /* Parse a pseudo-destructor-name.
5562
5563 pseudo-destructor-name:
5564 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5565 :: [opt] nested-name-specifier template template-id :: ~ type-name
5566 :: [opt] nested-name-specifier [opt] ~ type-name
5567
5568 If either of the first two productions is used, sets *SCOPE to the
5569 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5570 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5571 or ERROR_MARK_NODE if the parse fails. */
5572
5573 static void
5574 cp_parser_pseudo_destructor_name (cp_parser* parser,
5575 tree* scope,
5576 tree* type)
5577 {
5578 bool nested_name_specifier_p;
5579
5580 /* Assume that things will not work out. */
5581 *type = error_mark_node;
5582
5583 /* Look for the optional `::' operator. */
5584 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5585 /* Look for the optional nested-name-specifier. */
5586 nested_name_specifier_p
5587 = (cp_parser_nested_name_specifier_opt (parser,
5588 /*typename_keyword_p=*/false,
5589 /*check_dependency_p=*/true,
5590 /*type_p=*/false,
5591 /*is_declaration=*/false)
5592 != NULL_TREE);
5593 /* Now, if we saw a nested-name-specifier, we might be doing the
5594 second production. */
5595 if (nested_name_specifier_p
5596 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5597 {
5598 /* Consume the `template' keyword. */
5599 cp_lexer_consume_token (parser->lexer);
5600 /* Parse the template-id. */
5601 cp_parser_template_id (parser,
5602 /*template_keyword_p=*/true,
5603 /*check_dependency_p=*/false,
5604 /*is_declaration=*/true);
5605 /* Look for the `::' token. */
5606 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5607 }
5608 /* If the next token is not a `~', then there might be some
5609 additional qualification. */
5610 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5611 {
5612 /* At this point, we're looking for "type-name :: ~". The type-name
5613 must not be a class-name, since this is a pseudo-destructor. So,
5614 it must be either an enum-name, or a typedef-name -- both of which
5615 are just identifiers. So, we peek ahead to check that the "::"
5616 and "~" tokens are present; if they are not, then we can avoid
5617 calling type_name. */
5618 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5619 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5620 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5621 {
5622 cp_parser_error (parser, "non-scalar type");
5623 return;
5624 }
5625
5626 /* Look for the type-name. */
5627 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5628 if (*scope == error_mark_node)
5629 return;
5630
5631 /* Look for the `::' token. */
5632 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5633 }
5634 else
5635 *scope = NULL_TREE;
5636
5637 /* Look for the `~'. */
5638 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5639 /* Look for the type-name again. We are not responsible for
5640 checking that it matches the first type-name. */
5641 *type = cp_parser_nonclass_name (parser);
5642 }
5643
5644 /* Parse a unary-expression.
5645
5646 unary-expression:
5647 postfix-expression
5648 ++ cast-expression
5649 -- cast-expression
5650 unary-operator cast-expression
5651 sizeof unary-expression
5652 sizeof ( type-id )
5653 alignof ( type-id ) [C++0x]
5654 new-expression
5655 delete-expression
5656
5657 GNU Extensions:
5658
5659 unary-expression:
5660 __extension__ cast-expression
5661 __alignof__ unary-expression
5662 __alignof__ ( type-id )
5663 alignof unary-expression [C++0x]
5664 __real__ cast-expression
5665 __imag__ cast-expression
5666 && identifier
5667
5668 ADDRESS_P is true iff the unary-expression is appearing as the
5669 operand of the `&' operator. CAST_P is true if this expression is
5670 the target of a cast.
5671
5672 Returns a representation of the expression. */
5673
5674 static tree
5675 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5676 cp_id_kind * pidk)
5677 {
5678 cp_token *token;
5679 enum tree_code unary_operator;
5680
5681 /* Peek at the next token. */
5682 token = cp_lexer_peek_token (parser->lexer);
5683 /* Some keywords give away the kind of expression. */
5684 if (token->type == CPP_KEYWORD)
5685 {
5686 enum rid keyword = token->keyword;
5687
5688 switch (keyword)
5689 {
5690 case RID_ALIGNOF:
5691 case RID_SIZEOF:
5692 {
5693 tree operand;
5694 enum tree_code op;
5695
5696 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5697 /* Consume the token. */
5698 cp_lexer_consume_token (parser->lexer);
5699 /* Parse the operand. */
5700 operand = cp_parser_sizeof_operand (parser, keyword);
5701
5702 if (TYPE_P (operand))
5703 return cxx_sizeof_or_alignof_type (operand, op, true);
5704 else
5705 {
5706 /* ISO C++ defines alignof only with types, not with
5707 expressions. So pedwarn if alignof is used with a non-
5708 type expression. However, __alignof__ is ok. */
5709 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5710 pedwarn (token->location, OPT_pedantic,
5711 "ISO C++ does not allow %<alignof%> "
5712 "with a non-type");
5713
5714 return cxx_sizeof_or_alignof_expr (operand, op, true);
5715 }
5716 }
5717
5718 case RID_NEW:
5719 return cp_parser_new_expression (parser);
5720
5721 case RID_DELETE:
5722 return cp_parser_delete_expression (parser);
5723
5724 case RID_EXTENSION:
5725 {
5726 /* The saved value of the PEDANTIC flag. */
5727 int saved_pedantic;
5728 tree expr;
5729
5730 /* Save away the PEDANTIC flag. */
5731 cp_parser_extension_opt (parser, &saved_pedantic);
5732 /* Parse the cast-expression. */
5733 expr = cp_parser_simple_cast_expression (parser);
5734 /* Restore the PEDANTIC flag. */
5735 pedantic = saved_pedantic;
5736
5737 return expr;
5738 }
5739
5740 case RID_REALPART:
5741 case RID_IMAGPART:
5742 {
5743 tree expression;
5744
5745 /* Consume the `__real__' or `__imag__' token. */
5746 cp_lexer_consume_token (parser->lexer);
5747 /* Parse the cast-expression. */
5748 expression = cp_parser_simple_cast_expression (parser);
5749 /* Create the complete representation. */
5750 return build_x_unary_op ((keyword == RID_REALPART
5751 ? REALPART_EXPR : IMAGPART_EXPR),
5752 expression,
5753 tf_warning_or_error);
5754 }
5755 break;
5756
5757 case RID_NOEXCEPT:
5758 {
5759 tree expr;
5760 const char *saved_message;
5761 bool saved_integral_constant_expression_p;
5762 bool saved_non_integral_constant_expression_p;
5763 bool saved_greater_than_is_operator_p;
5764
5765 cp_lexer_consume_token (parser->lexer);
5766 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5767
5768 saved_message = parser->type_definition_forbidden_message;
5769 parser->type_definition_forbidden_message
5770 = G_("types may not be defined in %<noexcept%> expressions");
5771
5772 saved_integral_constant_expression_p
5773 = parser->integral_constant_expression_p;
5774 saved_non_integral_constant_expression_p
5775 = parser->non_integral_constant_expression_p;
5776 parser->integral_constant_expression_p = false;
5777
5778 saved_greater_than_is_operator_p
5779 = parser->greater_than_is_operator_p;
5780 parser->greater_than_is_operator_p = true;
5781
5782 ++cp_unevaluated_operand;
5783 ++c_inhibit_evaluation_warnings;
5784 expr = cp_parser_expression (parser, false, NULL);
5785 --c_inhibit_evaluation_warnings;
5786 --cp_unevaluated_operand;
5787
5788 parser->greater_than_is_operator_p
5789 = saved_greater_than_is_operator_p;
5790
5791 parser->integral_constant_expression_p
5792 = saved_integral_constant_expression_p;
5793 parser->non_integral_constant_expression_p
5794 = saved_non_integral_constant_expression_p;
5795
5796 parser->type_definition_forbidden_message = saved_message;
5797
5798 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5799 return finish_noexcept_expr (expr, tf_warning_or_error);
5800 }
5801
5802 default:
5803 break;
5804 }
5805 }
5806
5807 /* Look for the `:: new' and `:: delete', which also signal the
5808 beginning of a new-expression, or delete-expression,
5809 respectively. If the next token is `::', then it might be one of
5810 these. */
5811 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5812 {
5813 enum rid keyword;
5814
5815 /* See if the token after the `::' is one of the keywords in
5816 which we're interested. */
5817 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5818 /* If it's `new', we have a new-expression. */
5819 if (keyword == RID_NEW)
5820 return cp_parser_new_expression (parser);
5821 /* Similarly, for `delete'. */
5822 else if (keyword == RID_DELETE)
5823 return cp_parser_delete_expression (parser);
5824 }
5825
5826 /* Look for a unary operator. */
5827 unary_operator = cp_parser_unary_operator (token);
5828 /* The `++' and `--' operators can be handled similarly, even though
5829 they are not technically unary-operators in the grammar. */
5830 if (unary_operator == ERROR_MARK)
5831 {
5832 if (token->type == CPP_PLUS_PLUS)
5833 unary_operator = PREINCREMENT_EXPR;
5834 else if (token->type == CPP_MINUS_MINUS)
5835 unary_operator = PREDECREMENT_EXPR;
5836 /* Handle the GNU address-of-label extension. */
5837 else if (cp_parser_allow_gnu_extensions_p (parser)
5838 && token->type == CPP_AND_AND)
5839 {
5840 tree identifier;
5841 tree expression;
5842 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5843
5844 /* Consume the '&&' token. */
5845 cp_lexer_consume_token (parser->lexer);
5846 /* Look for the identifier. */
5847 identifier = cp_parser_identifier (parser);
5848 /* Create an expression representing the address. */
5849 expression = finish_label_address_expr (identifier, loc);
5850 if (cp_parser_non_integral_constant_expression (parser,
5851 NIC_ADDR_LABEL))
5852 expression = error_mark_node;
5853 return expression;
5854 }
5855 }
5856 if (unary_operator != ERROR_MARK)
5857 {
5858 tree cast_expression;
5859 tree expression = error_mark_node;
5860 non_integral_constant non_constant_p = NIC_NONE;
5861
5862 /* Consume the operator token. */
5863 token = cp_lexer_consume_token (parser->lexer);
5864 /* Parse the cast-expression. */
5865 cast_expression
5866 = cp_parser_cast_expression (parser,
5867 unary_operator == ADDR_EXPR,
5868 /*cast_p=*/false, pidk);
5869 /* Now, build an appropriate representation. */
5870 switch (unary_operator)
5871 {
5872 case INDIRECT_REF:
5873 non_constant_p = NIC_STAR;
5874 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5875 tf_warning_or_error);
5876 break;
5877
5878 case ADDR_EXPR:
5879 non_constant_p = NIC_ADDR;
5880 /* Fall through. */
5881 case BIT_NOT_EXPR:
5882 expression = build_x_unary_op (unary_operator, cast_expression,
5883 tf_warning_or_error);
5884 break;
5885
5886 case PREINCREMENT_EXPR:
5887 case PREDECREMENT_EXPR:
5888 non_constant_p = unary_operator == PREINCREMENT_EXPR
5889 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5890 /* Fall through. */
5891 case UNARY_PLUS_EXPR:
5892 case NEGATE_EXPR:
5893 case TRUTH_NOT_EXPR:
5894 expression = finish_unary_op_expr (unary_operator, cast_expression);
5895 break;
5896
5897 default:
5898 gcc_unreachable ();
5899 }
5900
5901 if (non_constant_p != NIC_NONE
5902 && cp_parser_non_integral_constant_expression (parser,
5903 non_constant_p))
5904 expression = error_mark_node;
5905
5906 return expression;
5907 }
5908
5909 return cp_parser_postfix_expression (parser, address_p, cast_p,
5910 /*member_access_only_p=*/false,
5911 pidk);
5912 }
5913
5914 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5915 unary-operator, the corresponding tree code is returned. */
5916
5917 static enum tree_code
5918 cp_parser_unary_operator (cp_token* token)
5919 {
5920 switch (token->type)
5921 {
5922 case CPP_MULT:
5923 return INDIRECT_REF;
5924
5925 case CPP_AND:
5926 return ADDR_EXPR;
5927
5928 case CPP_PLUS:
5929 return UNARY_PLUS_EXPR;
5930
5931 case CPP_MINUS:
5932 return NEGATE_EXPR;
5933
5934 case CPP_NOT:
5935 return TRUTH_NOT_EXPR;
5936
5937 case CPP_COMPL:
5938 return BIT_NOT_EXPR;
5939
5940 default:
5941 return ERROR_MARK;
5942 }
5943 }
5944
5945 /* Parse a new-expression.
5946
5947 new-expression:
5948 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5949 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5950
5951 Returns a representation of the expression. */
5952
5953 static tree
5954 cp_parser_new_expression (cp_parser* parser)
5955 {
5956 bool global_scope_p;
5957 VEC(tree,gc) *placement;
5958 tree type;
5959 VEC(tree,gc) *initializer;
5960 tree nelts;
5961 tree ret;
5962
5963 /* Look for the optional `::' operator. */
5964 global_scope_p
5965 = (cp_parser_global_scope_opt (parser,
5966 /*current_scope_valid_p=*/false)
5967 != NULL_TREE);
5968 /* Look for the `new' operator. */
5969 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5970 /* There's no easy way to tell a new-placement from the
5971 `( type-id )' construct. */
5972 cp_parser_parse_tentatively (parser);
5973 /* Look for a new-placement. */
5974 placement = cp_parser_new_placement (parser);
5975 /* If that didn't work out, there's no new-placement. */
5976 if (!cp_parser_parse_definitely (parser))
5977 {
5978 if (placement != NULL)
5979 release_tree_vector (placement);
5980 placement = NULL;
5981 }
5982
5983 /* If the next token is a `(', then we have a parenthesized
5984 type-id. */
5985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5986 {
5987 cp_token *token;
5988 /* Consume the `('. */
5989 cp_lexer_consume_token (parser->lexer);
5990 /* Parse the type-id. */
5991 type = cp_parser_type_id (parser);
5992 /* Look for the closing `)'. */
5993 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5994 token = cp_lexer_peek_token (parser->lexer);
5995 /* There should not be a direct-new-declarator in this production,
5996 but GCC used to allowed this, so we check and emit a sensible error
5997 message for this case. */
5998 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5999 {
6000 error_at (token->location,
6001 "array bound forbidden after parenthesized type-id");
6002 inform (token->location,
6003 "try removing the parentheses around the type-id");
6004 cp_parser_direct_new_declarator (parser);
6005 }
6006 nelts = NULL_TREE;
6007 }
6008 /* Otherwise, there must be a new-type-id. */
6009 else
6010 type = cp_parser_new_type_id (parser, &nelts);
6011
6012 /* If the next token is a `(' or '{', then we have a new-initializer. */
6013 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6014 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6015 initializer = cp_parser_new_initializer (parser);
6016 else
6017 initializer = NULL;
6018
6019 /* A new-expression may not appear in an integral constant
6020 expression. */
6021 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6022 ret = error_mark_node;
6023 else
6024 {
6025 /* Create a representation of the new-expression. */
6026 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6027 tf_warning_or_error);
6028 }
6029
6030 if (placement != NULL)
6031 release_tree_vector (placement);
6032 if (initializer != NULL)
6033 release_tree_vector (initializer);
6034
6035 return ret;
6036 }
6037
6038 /* Parse a new-placement.
6039
6040 new-placement:
6041 ( expression-list )
6042
6043 Returns the same representation as for an expression-list. */
6044
6045 static VEC(tree,gc) *
6046 cp_parser_new_placement (cp_parser* parser)
6047 {
6048 VEC(tree,gc) *expression_list;
6049
6050 /* Parse the expression-list. */
6051 expression_list = (cp_parser_parenthesized_expression_list
6052 (parser, non_attr, /*cast_p=*/false,
6053 /*allow_expansion_p=*/true,
6054 /*non_constant_p=*/NULL));
6055
6056 return expression_list;
6057 }
6058
6059 /* Parse a new-type-id.
6060
6061 new-type-id:
6062 type-specifier-seq new-declarator [opt]
6063
6064 Returns the TYPE allocated. If the new-type-id indicates an array
6065 type, *NELTS is set to the number of elements in the last array
6066 bound; the TYPE will not include the last array bound. */
6067
6068 static tree
6069 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6070 {
6071 cp_decl_specifier_seq type_specifier_seq;
6072 cp_declarator *new_declarator;
6073 cp_declarator *declarator;
6074 cp_declarator *outer_declarator;
6075 const char *saved_message;
6076 tree type;
6077
6078 /* The type-specifier sequence must not contain type definitions.
6079 (It cannot contain declarations of new types either, but if they
6080 are not definitions we will catch that because they are not
6081 complete.) */
6082 saved_message = parser->type_definition_forbidden_message;
6083 parser->type_definition_forbidden_message
6084 = G_("types may not be defined in a new-type-id");
6085 /* Parse the type-specifier-seq. */
6086 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6087 /*is_trailing_return=*/false,
6088 &type_specifier_seq);
6089 /* Restore the old message. */
6090 parser->type_definition_forbidden_message = saved_message;
6091 /* Parse the new-declarator. */
6092 new_declarator = cp_parser_new_declarator_opt (parser);
6093
6094 /* Determine the number of elements in the last array dimension, if
6095 any. */
6096 *nelts = NULL_TREE;
6097 /* Skip down to the last array dimension. */
6098 declarator = new_declarator;
6099 outer_declarator = NULL;
6100 while (declarator && (declarator->kind == cdk_pointer
6101 || declarator->kind == cdk_ptrmem))
6102 {
6103 outer_declarator = declarator;
6104 declarator = declarator->declarator;
6105 }
6106 while (declarator
6107 && declarator->kind == cdk_array
6108 && declarator->declarator
6109 && declarator->declarator->kind == cdk_array)
6110 {
6111 outer_declarator = declarator;
6112 declarator = declarator->declarator;
6113 }
6114
6115 if (declarator && declarator->kind == cdk_array)
6116 {
6117 *nelts = declarator->u.array.bounds;
6118 if (*nelts == error_mark_node)
6119 *nelts = integer_one_node;
6120
6121 if (outer_declarator)
6122 outer_declarator->declarator = declarator->declarator;
6123 else
6124 new_declarator = NULL;
6125 }
6126
6127 type = groktypename (&type_specifier_seq, new_declarator, false);
6128 return type;
6129 }
6130
6131 /* Parse an (optional) new-declarator.
6132
6133 new-declarator:
6134 ptr-operator new-declarator [opt]
6135 direct-new-declarator
6136
6137 Returns the declarator. */
6138
6139 static cp_declarator *
6140 cp_parser_new_declarator_opt (cp_parser* parser)
6141 {
6142 enum tree_code code;
6143 tree type;
6144 cp_cv_quals cv_quals;
6145
6146 /* We don't know if there's a ptr-operator next, or not. */
6147 cp_parser_parse_tentatively (parser);
6148 /* Look for a ptr-operator. */
6149 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6150 /* If that worked, look for more new-declarators. */
6151 if (cp_parser_parse_definitely (parser))
6152 {
6153 cp_declarator *declarator;
6154
6155 /* Parse another optional declarator. */
6156 declarator = cp_parser_new_declarator_opt (parser);
6157
6158 return cp_parser_make_indirect_declarator
6159 (code, type, cv_quals, declarator);
6160 }
6161
6162 /* If the next token is a `[', there is a direct-new-declarator. */
6163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6164 return cp_parser_direct_new_declarator (parser);
6165
6166 return NULL;
6167 }
6168
6169 /* Parse a direct-new-declarator.
6170
6171 direct-new-declarator:
6172 [ expression ]
6173 direct-new-declarator [constant-expression]
6174
6175 */
6176
6177 static cp_declarator *
6178 cp_parser_direct_new_declarator (cp_parser* parser)
6179 {
6180 cp_declarator *declarator = NULL;
6181
6182 while (true)
6183 {
6184 tree expression;
6185
6186 /* Look for the opening `['. */
6187 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6188 /* The first expression is not required to be constant. */
6189 if (!declarator)
6190 {
6191 cp_token *token = cp_lexer_peek_token (parser->lexer);
6192 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6193 /* The standard requires that the expression have integral
6194 type. DR 74 adds enumeration types. We believe that the
6195 real intent is that these expressions be handled like the
6196 expression in a `switch' condition, which also allows
6197 classes with a single conversion to integral or
6198 enumeration type. */
6199 if (!processing_template_decl)
6200 {
6201 expression
6202 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6203 expression,
6204 /*complain=*/true);
6205 if (!expression)
6206 {
6207 error_at (token->location,
6208 "expression in new-declarator must have integral "
6209 "or enumeration type");
6210 expression = error_mark_node;
6211 }
6212 }
6213 }
6214 /* But all the other expressions must be. */
6215 else
6216 expression
6217 = cp_parser_constant_expression (parser,
6218 /*allow_non_constant=*/false,
6219 NULL);
6220 /* Look for the closing `]'. */
6221 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6222
6223 /* Add this bound to the declarator. */
6224 declarator = make_array_declarator (declarator, expression);
6225
6226 /* If the next token is not a `[', then there are no more
6227 bounds. */
6228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6229 break;
6230 }
6231
6232 return declarator;
6233 }
6234
6235 /* Parse a new-initializer.
6236
6237 new-initializer:
6238 ( expression-list [opt] )
6239 braced-init-list
6240
6241 Returns a representation of the expression-list. */
6242
6243 static VEC(tree,gc) *
6244 cp_parser_new_initializer (cp_parser* parser)
6245 {
6246 VEC(tree,gc) *expression_list;
6247
6248 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6249 {
6250 tree t;
6251 bool expr_non_constant_p;
6252 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6253 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6254 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6255 expression_list = make_tree_vector_single (t);
6256 }
6257 else
6258 expression_list = (cp_parser_parenthesized_expression_list
6259 (parser, non_attr, /*cast_p=*/false,
6260 /*allow_expansion_p=*/true,
6261 /*non_constant_p=*/NULL));
6262
6263 return expression_list;
6264 }
6265
6266 /* Parse a delete-expression.
6267
6268 delete-expression:
6269 :: [opt] delete cast-expression
6270 :: [opt] delete [ ] cast-expression
6271
6272 Returns a representation of the expression. */
6273
6274 static tree
6275 cp_parser_delete_expression (cp_parser* parser)
6276 {
6277 bool global_scope_p;
6278 bool array_p;
6279 tree expression;
6280
6281 /* Look for the optional `::' operator. */
6282 global_scope_p
6283 = (cp_parser_global_scope_opt (parser,
6284 /*current_scope_valid_p=*/false)
6285 != NULL_TREE);
6286 /* Look for the `delete' keyword. */
6287 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6288 /* See if the array syntax is in use. */
6289 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6290 {
6291 /* Consume the `[' token. */
6292 cp_lexer_consume_token (parser->lexer);
6293 /* Look for the `]' token. */
6294 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6295 /* Remember that this is the `[]' construct. */
6296 array_p = true;
6297 }
6298 else
6299 array_p = false;
6300
6301 /* Parse the cast-expression. */
6302 expression = cp_parser_simple_cast_expression (parser);
6303
6304 /* A delete-expression may not appear in an integral constant
6305 expression. */
6306 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6307 return error_mark_node;
6308
6309 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6310 tf_warning_or_error);
6311 }
6312
6313 /* Returns true if TOKEN may start a cast-expression and false
6314 otherwise. */
6315
6316 static bool
6317 cp_parser_token_starts_cast_expression (cp_token *token)
6318 {
6319 switch (token->type)
6320 {
6321 case CPP_COMMA:
6322 case CPP_SEMICOLON:
6323 case CPP_QUERY:
6324 case CPP_COLON:
6325 case CPP_CLOSE_SQUARE:
6326 case CPP_CLOSE_PAREN:
6327 case CPP_CLOSE_BRACE:
6328 case CPP_DOT:
6329 case CPP_DOT_STAR:
6330 case CPP_DEREF:
6331 case CPP_DEREF_STAR:
6332 case CPP_DIV:
6333 case CPP_MOD:
6334 case CPP_LSHIFT:
6335 case CPP_RSHIFT:
6336 case CPP_LESS:
6337 case CPP_GREATER:
6338 case CPP_LESS_EQ:
6339 case CPP_GREATER_EQ:
6340 case CPP_EQ_EQ:
6341 case CPP_NOT_EQ:
6342 case CPP_EQ:
6343 case CPP_MULT_EQ:
6344 case CPP_DIV_EQ:
6345 case CPP_MOD_EQ:
6346 case CPP_PLUS_EQ:
6347 case CPP_MINUS_EQ:
6348 case CPP_RSHIFT_EQ:
6349 case CPP_LSHIFT_EQ:
6350 case CPP_AND_EQ:
6351 case CPP_XOR_EQ:
6352 case CPP_OR_EQ:
6353 case CPP_XOR:
6354 case CPP_OR:
6355 case CPP_OR_OR:
6356 case CPP_EOF:
6357 return false;
6358
6359 /* '[' may start a primary-expression in obj-c++. */
6360 case CPP_OPEN_SQUARE:
6361 return c_dialect_objc ();
6362
6363 default:
6364 return true;
6365 }
6366 }
6367
6368 /* Parse a cast-expression.
6369
6370 cast-expression:
6371 unary-expression
6372 ( type-id ) cast-expression
6373
6374 ADDRESS_P is true iff the unary-expression is appearing as the
6375 operand of the `&' operator. CAST_P is true if this expression is
6376 the target of a cast.
6377
6378 Returns a representation of the expression. */
6379
6380 static tree
6381 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6382 cp_id_kind * pidk)
6383 {
6384 /* If it's a `(', then we might be looking at a cast. */
6385 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6386 {
6387 tree type = NULL_TREE;
6388 tree expr = NULL_TREE;
6389 bool compound_literal_p;
6390 const char *saved_message;
6391
6392 /* There's no way to know yet whether or not this is a cast.
6393 For example, `(int (3))' is a unary-expression, while `(int)
6394 3' is a cast. So, we resort to parsing tentatively. */
6395 cp_parser_parse_tentatively (parser);
6396 /* Types may not be defined in a cast. */
6397 saved_message = parser->type_definition_forbidden_message;
6398 parser->type_definition_forbidden_message
6399 = G_("types may not be defined in casts");
6400 /* Consume the `('. */
6401 cp_lexer_consume_token (parser->lexer);
6402 /* A very tricky bit is that `(struct S) { 3 }' is a
6403 compound-literal (which we permit in C++ as an extension).
6404 But, that construct is not a cast-expression -- it is a
6405 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6406 is legal; if the compound-literal were a cast-expression,
6407 you'd need an extra set of parentheses.) But, if we parse
6408 the type-id, and it happens to be a class-specifier, then we
6409 will commit to the parse at that point, because we cannot
6410 undo the action that is done when creating a new class. So,
6411 then we cannot back up and do a postfix-expression.
6412
6413 Therefore, we scan ahead to the closing `)', and check to see
6414 if the token after the `)' is a `{'. If so, we are not
6415 looking at a cast-expression.
6416
6417 Save tokens so that we can put them back. */
6418 cp_lexer_save_tokens (parser->lexer);
6419 /* Skip tokens until the next token is a closing parenthesis.
6420 If we find the closing `)', and the next token is a `{', then
6421 we are looking at a compound-literal. */
6422 compound_literal_p
6423 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6424 /*consume_paren=*/true)
6425 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6426 /* Roll back the tokens we skipped. */
6427 cp_lexer_rollback_tokens (parser->lexer);
6428 /* If we were looking at a compound-literal, simulate an error
6429 so that the call to cp_parser_parse_definitely below will
6430 fail. */
6431 if (compound_literal_p)
6432 cp_parser_simulate_error (parser);
6433 else
6434 {
6435 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6436 parser->in_type_id_in_expr_p = true;
6437 /* Look for the type-id. */
6438 type = cp_parser_type_id (parser);
6439 /* Look for the closing `)'. */
6440 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6441 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6442 }
6443
6444 /* Restore the saved message. */
6445 parser->type_definition_forbidden_message = saved_message;
6446
6447 /* At this point this can only be either a cast or a
6448 parenthesized ctor such as `(T ())' that looks like a cast to
6449 function returning T. */
6450 if (!cp_parser_error_occurred (parser)
6451 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6452 (parser->lexer)))
6453 {
6454 cp_parser_parse_definitely (parser);
6455 expr = cp_parser_cast_expression (parser,
6456 /*address_p=*/false,
6457 /*cast_p=*/true, pidk);
6458
6459 /* Warn about old-style casts, if so requested. */
6460 if (warn_old_style_cast
6461 && !in_system_header
6462 && !VOID_TYPE_P (type)
6463 && current_lang_name != lang_name_c)
6464 warning (OPT_Wold_style_cast, "use of old-style cast");
6465
6466 /* Only type conversions to integral or enumeration types
6467 can be used in constant-expressions. */
6468 if (!cast_valid_in_integral_constant_expression_p (type)
6469 && cp_parser_non_integral_constant_expression (parser,
6470 NIC_CAST))
6471 return error_mark_node;
6472
6473 /* Perform the cast. */
6474 expr = build_c_cast (input_location, type, expr);
6475 return expr;
6476 }
6477 else
6478 cp_parser_abort_tentative_parse (parser);
6479 }
6480
6481 /* If we get here, then it's not a cast, so it must be a
6482 unary-expression. */
6483 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6484 }
6485
6486 /* Parse a binary expression of the general form:
6487
6488 pm-expression:
6489 cast-expression
6490 pm-expression .* cast-expression
6491 pm-expression ->* cast-expression
6492
6493 multiplicative-expression:
6494 pm-expression
6495 multiplicative-expression * pm-expression
6496 multiplicative-expression / pm-expression
6497 multiplicative-expression % pm-expression
6498
6499 additive-expression:
6500 multiplicative-expression
6501 additive-expression + multiplicative-expression
6502 additive-expression - multiplicative-expression
6503
6504 shift-expression:
6505 additive-expression
6506 shift-expression << additive-expression
6507 shift-expression >> additive-expression
6508
6509 relational-expression:
6510 shift-expression
6511 relational-expression < shift-expression
6512 relational-expression > shift-expression
6513 relational-expression <= shift-expression
6514 relational-expression >= shift-expression
6515
6516 GNU Extension:
6517
6518 relational-expression:
6519 relational-expression <? shift-expression
6520 relational-expression >? shift-expression
6521
6522 equality-expression:
6523 relational-expression
6524 equality-expression == relational-expression
6525 equality-expression != relational-expression
6526
6527 and-expression:
6528 equality-expression
6529 and-expression & equality-expression
6530
6531 exclusive-or-expression:
6532 and-expression
6533 exclusive-or-expression ^ and-expression
6534
6535 inclusive-or-expression:
6536 exclusive-or-expression
6537 inclusive-or-expression | exclusive-or-expression
6538
6539 logical-and-expression:
6540 inclusive-or-expression
6541 logical-and-expression && inclusive-or-expression
6542
6543 logical-or-expression:
6544 logical-and-expression
6545 logical-or-expression || logical-and-expression
6546
6547 All these are implemented with a single function like:
6548
6549 binary-expression:
6550 simple-cast-expression
6551 binary-expression <token> binary-expression
6552
6553 CAST_P is true if this expression is the target of a cast.
6554
6555 The binops_by_token map is used to get the tree codes for each <token> type.
6556 binary-expressions are associated according to a precedence table. */
6557
6558 #define TOKEN_PRECEDENCE(token) \
6559 (((token->type == CPP_GREATER \
6560 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6561 && !parser->greater_than_is_operator_p) \
6562 ? PREC_NOT_OPERATOR \
6563 : binops_by_token[token->type].prec)
6564
6565 static tree
6566 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6567 bool no_toplevel_fold_p,
6568 enum cp_parser_prec prec,
6569 cp_id_kind * pidk)
6570 {
6571 cp_parser_expression_stack stack;
6572 cp_parser_expression_stack_entry *sp = &stack[0];
6573 tree lhs, rhs;
6574 cp_token *token;
6575 enum tree_code tree_type, lhs_type, rhs_type;
6576 enum cp_parser_prec new_prec, lookahead_prec;
6577 bool overloaded_p;
6578
6579 /* Parse the first expression. */
6580 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6581 lhs_type = ERROR_MARK;
6582
6583 for (;;)
6584 {
6585 /* Get an operator token. */
6586 token = cp_lexer_peek_token (parser->lexer);
6587
6588 if (warn_cxx0x_compat
6589 && token->type == CPP_RSHIFT
6590 && !parser->greater_than_is_operator_p)
6591 {
6592 if (warning_at (token->location, OPT_Wc__0x_compat,
6593 "%<>>%> operator will be treated as"
6594 " two right angle brackets in C++0x"))
6595 inform (token->location,
6596 "suggest parentheses around %<>>%> expression");
6597 }
6598
6599 new_prec = TOKEN_PRECEDENCE (token);
6600
6601 /* Popping an entry off the stack means we completed a subexpression:
6602 - either we found a token which is not an operator (`>' where it is not
6603 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6604 will happen repeatedly;
6605 - or, we found an operator which has lower priority. This is the case
6606 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6607 parsing `3 * 4'. */
6608 if (new_prec <= prec)
6609 {
6610 if (sp == stack)
6611 break;
6612 else
6613 goto pop;
6614 }
6615
6616 get_rhs:
6617 tree_type = binops_by_token[token->type].tree_type;
6618
6619 /* We used the operator token. */
6620 cp_lexer_consume_token (parser->lexer);
6621
6622 /* For "false && x" or "true || x", x will never be executed;
6623 disable warnings while evaluating it. */
6624 if (tree_type == TRUTH_ANDIF_EXPR)
6625 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6626 else if (tree_type == TRUTH_ORIF_EXPR)
6627 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6628
6629 /* Extract another operand. It may be the RHS of this expression
6630 or the LHS of a new, higher priority expression. */
6631 rhs = cp_parser_simple_cast_expression (parser);
6632 rhs_type = ERROR_MARK;
6633
6634 /* Get another operator token. Look up its precedence to avoid
6635 building a useless (immediately popped) stack entry for common
6636 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6637 token = cp_lexer_peek_token (parser->lexer);
6638 lookahead_prec = TOKEN_PRECEDENCE (token);
6639 if (lookahead_prec > new_prec)
6640 {
6641 /* ... and prepare to parse the RHS of the new, higher priority
6642 expression. Since precedence levels on the stack are
6643 monotonically increasing, we do not have to care about
6644 stack overflows. */
6645 sp->prec = prec;
6646 sp->tree_type = tree_type;
6647 sp->lhs = lhs;
6648 sp->lhs_type = lhs_type;
6649 sp++;
6650 lhs = rhs;
6651 lhs_type = rhs_type;
6652 prec = new_prec;
6653 new_prec = lookahead_prec;
6654 goto get_rhs;
6655
6656 pop:
6657 lookahead_prec = new_prec;
6658 /* If the stack is not empty, we have parsed into LHS the right side
6659 (`4' in the example above) of an expression we had suspended.
6660 We can use the information on the stack to recover the LHS (`3')
6661 from the stack together with the tree code (`MULT_EXPR'), and
6662 the precedence of the higher level subexpression
6663 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6664 which will be used to actually build the additive expression. */
6665 --sp;
6666 prec = sp->prec;
6667 tree_type = sp->tree_type;
6668 rhs = lhs;
6669 rhs_type = lhs_type;
6670 lhs = sp->lhs;
6671 lhs_type = sp->lhs_type;
6672 }
6673
6674 /* Undo the disabling of warnings done above. */
6675 if (tree_type == TRUTH_ANDIF_EXPR)
6676 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6677 else if (tree_type == TRUTH_ORIF_EXPR)
6678 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6679
6680 overloaded_p = false;
6681 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6682 ERROR_MARK for everything that is not a binary expression.
6683 This makes warn_about_parentheses miss some warnings that
6684 involve unary operators. For unary expressions we should
6685 pass the correct tree_code unless the unary expression was
6686 surrounded by parentheses.
6687 */
6688 if (no_toplevel_fold_p
6689 && lookahead_prec <= prec
6690 && sp == stack
6691 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6692 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6693 else
6694 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6695 &overloaded_p, tf_warning_or_error);
6696 lhs_type = tree_type;
6697
6698 /* If the binary operator required the use of an overloaded operator,
6699 then this expression cannot be an integral constant-expression.
6700 An overloaded operator can be used even if both operands are
6701 otherwise permissible in an integral constant-expression if at
6702 least one of the operands is of enumeration type. */
6703
6704 if (overloaded_p
6705 && cp_parser_non_integral_constant_expression (parser,
6706 NIC_OVERLOADED))
6707 return error_mark_node;
6708 }
6709
6710 return lhs;
6711 }
6712
6713
6714 /* Parse the `? expression : assignment-expression' part of a
6715 conditional-expression. The LOGICAL_OR_EXPR is the
6716 logical-or-expression that started the conditional-expression.
6717 Returns a representation of the entire conditional-expression.
6718
6719 This routine is used by cp_parser_assignment_expression.
6720
6721 ? expression : assignment-expression
6722
6723 GNU Extensions:
6724
6725 ? : assignment-expression */
6726
6727 static tree
6728 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6729 {
6730 tree expr;
6731 tree assignment_expr;
6732 struct cp_token *token;
6733
6734 /* Consume the `?' token. */
6735 cp_lexer_consume_token (parser->lexer);
6736 token = cp_lexer_peek_token (parser->lexer);
6737 if (cp_parser_allow_gnu_extensions_p (parser)
6738 && token->type == CPP_COLON)
6739 {
6740 pedwarn (token->location, OPT_pedantic,
6741 "ISO C++ does not allow ?: with omitted middle operand");
6742 /* Implicit true clause. */
6743 expr = NULL_TREE;
6744 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6745 warn_for_omitted_condop (token->location, logical_or_expr);
6746 }
6747 else
6748 {
6749 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6750 parser->colon_corrects_to_scope_p = false;
6751 /* Parse the expression. */
6752 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6753 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6754 c_inhibit_evaluation_warnings +=
6755 ((logical_or_expr == truthvalue_true_node)
6756 - (logical_or_expr == truthvalue_false_node));
6757 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6758 }
6759
6760 /* The next token should be a `:'. */
6761 cp_parser_require (parser, CPP_COLON, RT_COLON);
6762 /* Parse the assignment-expression. */
6763 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6764 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6765
6766 /* Build the conditional-expression. */
6767 return build_x_conditional_expr (logical_or_expr,
6768 expr,
6769 assignment_expr,
6770 tf_warning_or_error);
6771 }
6772
6773 /* Parse an assignment-expression.
6774
6775 assignment-expression:
6776 conditional-expression
6777 logical-or-expression assignment-operator assignment_expression
6778 throw-expression
6779
6780 CAST_P is true if this expression is the target of a cast.
6781
6782 Returns a representation for the expression. */
6783
6784 static tree
6785 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6786 cp_id_kind * pidk)
6787 {
6788 tree expr;
6789
6790 /* If the next token is the `throw' keyword, then we're looking at
6791 a throw-expression. */
6792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6793 expr = cp_parser_throw_expression (parser);
6794 /* Otherwise, it must be that we are looking at a
6795 logical-or-expression. */
6796 else
6797 {
6798 /* Parse the binary expressions (logical-or-expression). */
6799 expr = cp_parser_binary_expression (parser, cast_p, false,
6800 PREC_NOT_OPERATOR, pidk);
6801 /* If the next token is a `?' then we're actually looking at a
6802 conditional-expression. */
6803 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6804 return cp_parser_question_colon_clause (parser, expr);
6805 else
6806 {
6807 enum tree_code assignment_operator;
6808
6809 /* If it's an assignment-operator, we're using the second
6810 production. */
6811 assignment_operator
6812 = cp_parser_assignment_operator_opt (parser);
6813 if (assignment_operator != ERROR_MARK)
6814 {
6815 bool non_constant_p;
6816
6817 /* Parse the right-hand side of the assignment. */
6818 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6819
6820 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6821 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6822
6823 /* An assignment may not appear in a
6824 constant-expression. */
6825 if (cp_parser_non_integral_constant_expression (parser,
6826 NIC_ASSIGNMENT))
6827 return error_mark_node;
6828 /* Build the assignment expression. */
6829 expr = build_x_modify_expr (expr,
6830 assignment_operator,
6831 rhs,
6832 tf_warning_or_error);
6833 }
6834 }
6835 }
6836
6837 return expr;
6838 }
6839
6840 /* Parse an (optional) assignment-operator.
6841
6842 assignment-operator: one of
6843 = *= /= %= += -= >>= <<= &= ^= |=
6844
6845 GNU Extension:
6846
6847 assignment-operator: one of
6848 <?= >?=
6849
6850 If the next token is an assignment operator, the corresponding tree
6851 code is returned, and the token is consumed. For example, for
6852 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6853 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6854 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6855 operator, ERROR_MARK is returned. */
6856
6857 static enum tree_code
6858 cp_parser_assignment_operator_opt (cp_parser* parser)
6859 {
6860 enum tree_code op;
6861 cp_token *token;
6862
6863 /* Peek at the next token. */
6864 token = cp_lexer_peek_token (parser->lexer);
6865
6866 switch (token->type)
6867 {
6868 case CPP_EQ:
6869 op = NOP_EXPR;
6870 break;
6871
6872 case CPP_MULT_EQ:
6873 op = MULT_EXPR;
6874 break;
6875
6876 case CPP_DIV_EQ:
6877 op = TRUNC_DIV_EXPR;
6878 break;
6879
6880 case CPP_MOD_EQ:
6881 op = TRUNC_MOD_EXPR;
6882 break;
6883
6884 case CPP_PLUS_EQ:
6885 op = PLUS_EXPR;
6886 break;
6887
6888 case CPP_MINUS_EQ:
6889 op = MINUS_EXPR;
6890 break;
6891
6892 case CPP_RSHIFT_EQ:
6893 op = RSHIFT_EXPR;
6894 break;
6895
6896 case CPP_LSHIFT_EQ:
6897 op = LSHIFT_EXPR;
6898 break;
6899
6900 case CPP_AND_EQ:
6901 op = BIT_AND_EXPR;
6902 break;
6903
6904 case CPP_XOR_EQ:
6905 op = BIT_XOR_EXPR;
6906 break;
6907
6908 case CPP_OR_EQ:
6909 op = BIT_IOR_EXPR;
6910 break;
6911
6912 default:
6913 /* Nothing else is an assignment operator. */
6914 op = ERROR_MARK;
6915 }
6916
6917 /* If it was an assignment operator, consume it. */
6918 if (op != ERROR_MARK)
6919 cp_lexer_consume_token (parser->lexer);
6920
6921 return op;
6922 }
6923
6924 /* Parse an expression.
6925
6926 expression:
6927 assignment-expression
6928 expression , assignment-expression
6929
6930 CAST_P is true if this expression is the target of a cast.
6931
6932 Returns a representation of the expression. */
6933
6934 static tree
6935 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6936 {
6937 tree expression = NULL_TREE;
6938
6939 while (true)
6940 {
6941 tree assignment_expression;
6942
6943 /* Parse the next assignment-expression. */
6944 assignment_expression
6945 = cp_parser_assignment_expression (parser, cast_p, pidk);
6946 /* If this is the first assignment-expression, we can just
6947 save it away. */
6948 if (!expression)
6949 expression = assignment_expression;
6950 else
6951 expression = build_x_compound_expr (expression,
6952 assignment_expression,
6953 tf_warning_or_error);
6954 /* If the next token is not a comma, then we are done with the
6955 expression. */
6956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6957 break;
6958 /* Consume the `,'. */
6959 cp_lexer_consume_token (parser->lexer);
6960 /* A comma operator cannot appear in a constant-expression. */
6961 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6962 expression = error_mark_node;
6963 }
6964
6965 return expression;
6966 }
6967
6968 /* Parse a constant-expression.
6969
6970 constant-expression:
6971 conditional-expression
6972
6973 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6974 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6975 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6976 is false, NON_CONSTANT_P should be NULL. */
6977
6978 static tree
6979 cp_parser_constant_expression (cp_parser* parser,
6980 bool allow_non_constant_p,
6981 bool *non_constant_p)
6982 {
6983 bool saved_integral_constant_expression_p;
6984 bool saved_allow_non_integral_constant_expression_p;
6985 bool saved_non_integral_constant_expression_p;
6986 tree expression;
6987
6988 /* It might seem that we could simply parse the
6989 conditional-expression, and then check to see if it were
6990 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6991 one that the compiler can figure out is constant, possibly after
6992 doing some simplifications or optimizations. The standard has a
6993 precise definition of constant-expression, and we must honor
6994 that, even though it is somewhat more restrictive.
6995
6996 For example:
6997
6998 int i[(2, 3)];
6999
7000 is not a legal declaration, because `(2, 3)' is not a
7001 constant-expression. The `,' operator is forbidden in a
7002 constant-expression. However, GCC's constant-folding machinery
7003 will fold this operation to an INTEGER_CST for `3'. */
7004
7005 /* Save the old settings. */
7006 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7007 saved_allow_non_integral_constant_expression_p
7008 = parser->allow_non_integral_constant_expression_p;
7009 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7010 /* We are now parsing a constant-expression. */
7011 parser->integral_constant_expression_p = true;
7012 parser->allow_non_integral_constant_expression_p
7013 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7014 parser->non_integral_constant_expression_p = false;
7015 /* Although the grammar says "conditional-expression", we parse an
7016 "assignment-expression", which also permits "throw-expression"
7017 and the use of assignment operators. In the case that
7018 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7019 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7020 actually essential that we look for an assignment-expression.
7021 For example, cp_parser_initializer_clauses uses this function to
7022 determine whether a particular assignment-expression is in fact
7023 constant. */
7024 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7025 /* Restore the old settings. */
7026 parser->integral_constant_expression_p
7027 = saved_integral_constant_expression_p;
7028 parser->allow_non_integral_constant_expression_p
7029 = saved_allow_non_integral_constant_expression_p;
7030 if (cxx_dialect >= cxx0x)
7031 {
7032 /* Require an rvalue constant expression here; that's what our
7033 callers expect. Reference constant expressions are handled
7034 separately in e.g. cp_parser_template_argument. */
7035 bool is_const = potential_rvalue_constant_expression (expression);
7036 parser->non_integral_constant_expression_p = !is_const;
7037 if (!is_const && !allow_non_constant_p)
7038 require_potential_rvalue_constant_expression (expression);
7039 }
7040 if (allow_non_constant_p)
7041 *non_constant_p = parser->non_integral_constant_expression_p;
7042 else if (parser->non_integral_constant_expression_p)
7043 expression = error_mark_node;
7044 parser->non_integral_constant_expression_p
7045 = saved_non_integral_constant_expression_p;
7046
7047 return expression;
7048 }
7049
7050 /* Parse __builtin_offsetof.
7051
7052 offsetof-expression:
7053 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7054
7055 offsetof-member-designator:
7056 id-expression
7057 | offsetof-member-designator "." id-expression
7058 | offsetof-member-designator "[" expression "]"
7059 | offsetof-member-designator "->" id-expression */
7060
7061 static tree
7062 cp_parser_builtin_offsetof (cp_parser *parser)
7063 {
7064 int save_ice_p, save_non_ice_p;
7065 tree type, expr;
7066 cp_id_kind dummy;
7067 cp_token *token;
7068
7069 /* We're about to accept non-integral-constant things, but will
7070 definitely yield an integral constant expression. Save and
7071 restore these values around our local parsing. */
7072 save_ice_p = parser->integral_constant_expression_p;
7073 save_non_ice_p = parser->non_integral_constant_expression_p;
7074
7075 /* Consume the "__builtin_offsetof" token. */
7076 cp_lexer_consume_token (parser->lexer);
7077 /* Consume the opening `('. */
7078 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7079 /* Parse the type-id. */
7080 type = cp_parser_type_id (parser);
7081 /* Look for the `,'. */
7082 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7083 token = cp_lexer_peek_token (parser->lexer);
7084
7085 /* Build the (type *)null that begins the traditional offsetof macro. */
7086 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7087 tf_warning_or_error);
7088
7089 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7090 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7091 true, &dummy, token->location);
7092 while (true)
7093 {
7094 token = cp_lexer_peek_token (parser->lexer);
7095 switch (token->type)
7096 {
7097 case CPP_OPEN_SQUARE:
7098 /* offsetof-member-designator "[" expression "]" */
7099 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7100 break;
7101
7102 case CPP_DEREF:
7103 /* offsetof-member-designator "->" identifier */
7104 expr = grok_array_decl (expr, integer_zero_node);
7105 /* FALLTHRU */
7106
7107 case CPP_DOT:
7108 /* offsetof-member-designator "." identifier */
7109 cp_lexer_consume_token (parser->lexer);
7110 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7111 expr, true, &dummy,
7112 token->location);
7113 break;
7114
7115 case CPP_CLOSE_PAREN:
7116 /* Consume the ")" token. */
7117 cp_lexer_consume_token (parser->lexer);
7118 goto success;
7119
7120 default:
7121 /* Error. We know the following require will fail, but
7122 that gives the proper error message. */
7123 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7124 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7125 expr = error_mark_node;
7126 goto failure;
7127 }
7128 }
7129
7130 success:
7131 /* If we're processing a template, we can't finish the semantics yet.
7132 Otherwise we can fold the entire expression now. */
7133 if (processing_template_decl)
7134 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7135 else
7136 expr = finish_offsetof (expr);
7137
7138 failure:
7139 parser->integral_constant_expression_p = save_ice_p;
7140 parser->non_integral_constant_expression_p = save_non_ice_p;
7141
7142 return expr;
7143 }
7144
7145 /* Parse a trait expression.
7146
7147 Returns a representation of the expression, the underlying type
7148 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7149
7150 static tree
7151 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7152 {
7153 cp_trait_kind kind;
7154 tree type1, type2 = NULL_TREE;
7155 bool binary = false;
7156 cp_decl_specifier_seq decl_specs;
7157
7158 switch (keyword)
7159 {
7160 case RID_HAS_NOTHROW_ASSIGN:
7161 kind = CPTK_HAS_NOTHROW_ASSIGN;
7162 break;
7163 case RID_HAS_NOTHROW_CONSTRUCTOR:
7164 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7165 break;
7166 case RID_HAS_NOTHROW_COPY:
7167 kind = CPTK_HAS_NOTHROW_COPY;
7168 break;
7169 case RID_HAS_TRIVIAL_ASSIGN:
7170 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7171 break;
7172 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7173 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7174 break;
7175 case RID_HAS_TRIVIAL_COPY:
7176 kind = CPTK_HAS_TRIVIAL_COPY;
7177 break;
7178 case RID_HAS_TRIVIAL_DESTRUCTOR:
7179 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7180 break;
7181 case RID_HAS_VIRTUAL_DESTRUCTOR:
7182 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7183 break;
7184 case RID_IS_ABSTRACT:
7185 kind = CPTK_IS_ABSTRACT;
7186 break;
7187 case RID_IS_BASE_OF:
7188 kind = CPTK_IS_BASE_OF;
7189 binary = true;
7190 break;
7191 case RID_IS_CLASS:
7192 kind = CPTK_IS_CLASS;
7193 break;
7194 case RID_IS_CONVERTIBLE_TO:
7195 kind = CPTK_IS_CONVERTIBLE_TO;
7196 binary = true;
7197 break;
7198 case RID_IS_EMPTY:
7199 kind = CPTK_IS_EMPTY;
7200 break;
7201 case RID_IS_ENUM:
7202 kind = CPTK_IS_ENUM;
7203 break;
7204 case RID_IS_LITERAL_TYPE:
7205 kind = CPTK_IS_LITERAL_TYPE;
7206 break;
7207 case RID_IS_POD:
7208 kind = CPTK_IS_POD;
7209 break;
7210 case RID_IS_POLYMORPHIC:
7211 kind = CPTK_IS_POLYMORPHIC;
7212 break;
7213 case RID_IS_STD_LAYOUT:
7214 kind = CPTK_IS_STD_LAYOUT;
7215 break;
7216 case RID_IS_TRIVIAL:
7217 kind = CPTK_IS_TRIVIAL;
7218 break;
7219 case RID_IS_UNION:
7220 kind = CPTK_IS_UNION;
7221 break;
7222 case RID_UNDERLYING_TYPE:
7223 kind = CPTK_UNDERLYING_TYPE;
7224 break;
7225 default:
7226 gcc_unreachable ();
7227 }
7228
7229 /* Consume the token. */
7230 cp_lexer_consume_token (parser->lexer);
7231
7232 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7233
7234 type1 = cp_parser_type_id (parser);
7235
7236 if (type1 == error_mark_node)
7237 return error_mark_node;
7238
7239 /* Build a trivial decl-specifier-seq. */
7240 clear_decl_specs (&decl_specs);
7241 decl_specs.type = type1;
7242
7243 /* Call grokdeclarator to figure out what type this is. */
7244 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7245 /*initialized=*/0, /*attrlist=*/NULL);
7246
7247 if (binary)
7248 {
7249 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7250
7251 type2 = cp_parser_type_id (parser);
7252
7253 if (type2 == error_mark_node)
7254 return error_mark_node;
7255
7256 /* Build a trivial decl-specifier-seq. */
7257 clear_decl_specs (&decl_specs);
7258 decl_specs.type = type2;
7259
7260 /* Call grokdeclarator to figure out what type this is. */
7261 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7262 /*initialized=*/0, /*attrlist=*/NULL);
7263 }
7264
7265 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7266
7267 /* Complete the trait expression, which may mean either processing
7268 the trait expr now or saving it for template instantiation. */
7269 return kind != CPTK_UNDERLYING_TYPE
7270 ? finish_trait_expr (kind, type1, type2)
7271 : finish_underlying_type (type1);
7272 }
7273
7274 /* Lambdas that appear in variable initializer or default argument scope
7275 get that in their mangling, so we need to record it. We might as well
7276 use the count for function and namespace scopes as well. */
7277 static GTY(()) tree lambda_scope;
7278 static GTY(()) int lambda_count;
7279 typedef struct GTY(()) tree_int
7280 {
7281 tree t;
7282 int i;
7283 } tree_int;
7284 DEF_VEC_O(tree_int);
7285 DEF_VEC_ALLOC_O(tree_int,gc);
7286 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7287
7288 static void
7289 start_lambda_scope (tree decl)
7290 {
7291 tree_int ti;
7292 gcc_assert (decl);
7293 /* Once we're inside a function, we ignore other scopes and just push
7294 the function again so that popping works properly. */
7295 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7296 decl = current_function_decl;
7297 ti.t = lambda_scope;
7298 ti.i = lambda_count;
7299 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7300 if (lambda_scope != decl)
7301 {
7302 /* Don't reset the count if we're still in the same function. */
7303 lambda_scope = decl;
7304 lambda_count = 0;
7305 }
7306 }
7307
7308 static void
7309 record_lambda_scope (tree lambda)
7310 {
7311 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7312 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7313 }
7314
7315 static void
7316 finish_lambda_scope (void)
7317 {
7318 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7319 if (lambda_scope != p->t)
7320 {
7321 lambda_scope = p->t;
7322 lambda_count = p->i;
7323 }
7324 VEC_pop (tree_int, lambda_scope_stack);
7325 }
7326
7327 /* Parse a lambda expression.
7328
7329 lambda-expression:
7330 lambda-introducer lambda-declarator [opt] compound-statement
7331
7332 Returns a representation of the expression. */
7333
7334 static tree
7335 cp_parser_lambda_expression (cp_parser* parser)
7336 {
7337 tree lambda_expr = build_lambda_expr ();
7338 tree type;
7339
7340 LAMBDA_EXPR_LOCATION (lambda_expr)
7341 = cp_lexer_peek_token (parser->lexer)->location;
7342
7343 if (cp_unevaluated_operand)
7344 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7345 "lambda-expression in unevaluated context");
7346
7347 /* We may be in the middle of deferred access check. Disable
7348 it now. */
7349 push_deferring_access_checks (dk_no_deferred);
7350
7351 cp_parser_lambda_introducer (parser, lambda_expr);
7352
7353 type = begin_lambda_type (lambda_expr);
7354
7355 record_lambda_scope (lambda_expr);
7356
7357 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7358 determine_visibility (TYPE_NAME (type));
7359
7360 /* Now that we've started the type, add the capture fields for any
7361 explicit captures. */
7362 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7363
7364 {
7365 /* Inside the class, surrounding template-parameter-lists do not apply. */
7366 unsigned int saved_num_template_parameter_lists
7367 = parser->num_template_parameter_lists;
7368
7369 parser->num_template_parameter_lists = 0;
7370
7371 /* By virtue of defining a local class, a lambda expression has access to
7372 the private variables of enclosing classes. */
7373
7374 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7375
7376 cp_parser_lambda_body (parser, lambda_expr);
7377
7378 /* The capture list was built up in reverse order; fix that now. */
7379 {
7380 tree newlist = NULL_TREE;
7381 tree elt, next;
7382
7383 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7384 elt; elt = next)
7385 {
7386 tree field = TREE_PURPOSE (elt);
7387 char *buf;
7388
7389 next = TREE_CHAIN (elt);
7390 TREE_CHAIN (elt) = newlist;
7391 newlist = elt;
7392
7393 /* Also add __ to the beginning of the field name so that code
7394 outside the lambda body can't see the captured name. We could
7395 just remove the name entirely, but this is more useful for
7396 debugging. */
7397 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7398 /* The 'this' capture already starts with __. */
7399 continue;
7400
7401 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7402 buf[1] = buf[0] = '_';
7403 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7404 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7405 DECL_NAME (field) = get_identifier (buf);
7406 }
7407 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7408 }
7409
7410 maybe_add_lambda_conv_op (type);
7411
7412 type = finish_struct (type, /*attributes=*/NULL_TREE);
7413
7414 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7415 }
7416
7417 pop_deferring_access_checks ();
7418
7419 return build_lambda_object (lambda_expr);
7420 }
7421
7422 /* Parse the beginning of a lambda expression.
7423
7424 lambda-introducer:
7425 [ lambda-capture [opt] ]
7426
7427 LAMBDA_EXPR is the current representation of the lambda expression. */
7428
7429 static void
7430 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7431 {
7432 /* Need commas after the first capture. */
7433 bool first = true;
7434
7435 /* Eat the leading `['. */
7436 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7437
7438 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7439 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7440 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7441 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7442 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7443 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7444
7445 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7446 {
7447 cp_lexer_consume_token (parser->lexer);
7448 first = false;
7449 }
7450
7451 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7452 {
7453 cp_token* capture_token;
7454 tree capture_id;
7455 tree capture_init_expr;
7456 cp_id_kind idk = CP_ID_KIND_NONE;
7457 bool explicit_init_p = false;
7458
7459 enum capture_kind_type
7460 {
7461 BY_COPY,
7462 BY_REFERENCE
7463 };
7464 enum capture_kind_type capture_kind = BY_COPY;
7465
7466 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7467 {
7468 error ("expected end of capture-list");
7469 return;
7470 }
7471
7472 if (first)
7473 first = false;
7474 else
7475 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7476
7477 /* Possibly capture `this'. */
7478 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7479 {
7480 cp_lexer_consume_token (parser->lexer);
7481 add_capture (lambda_expr,
7482 /*id=*/get_identifier ("__this"),
7483 /*initializer=*/finish_this_expr(),
7484 /*by_reference_p=*/false,
7485 explicit_init_p);
7486 continue;
7487 }
7488
7489 /* Remember whether we want to capture as a reference or not. */
7490 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7491 {
7492 capture_kind = BY_REFERENCE;
7493 cp_lexer_consume_token (parser->lexer);
7494 }
7495
7496 /* Get the identifier. */
7497 capture_token = cp_lexer_peek_token (parser->lexer);
7498 capture_id = cp_parser_identifier (parser);
7499
7500 if (capture_id == error_mark_node)
7501 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7502 delimiters, but I modified this to stop on unnested ']' as well. It
7503 was already changed to stop on unnested '}', so the
7504 "closing_parenthesis" name is no more misleading with my change. */
7505 {
7506 cp_parser_skip_to_closing_parenthesis (parser,
7507 /*recovering=*/true,
7508 /*or_comma=*/true,
7509 /*consume_paren=*/true);
7510 break;
7511 }
7512
7513 /* Find the initializer for this capture. */
7514 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7515 {
7516 /* An explicit expression exists. */
7517 cp_lexer_consume_token (parser->lexer);
7518 pedwarn (input_location, OPT_pedantic,
7519 "ISO C++ does not allow initializers "
7520 "in lambda expression capture lists");
7521 capture_init_expr = cp_parser_assignment_expression (parser,
7522 /*cast_p=*/true,
7523 &idk);
7524 explicit_init_p = true;
7525 }
7526 else
7527 {
7528 const char* error_msg;
7529
7530 /* Turn the identifier into an id-expression. */
7531 capture_init_expr
7532 = cp_parser_lookup_name
7533 (parser,
7534 capture_id,
7535 none_type,
7536 /*is_template=*/false,
7537 /*is_namespace=*/false,
7538 /*check_dependency=*/true,
7539 /*ambiguous_decls=*/NULL,
7540 capture_token->location);
7541
7542 capture_init_expr
7543 = finish_id_expression
7544 (capture_id,
7545 capture_init_expr,
7546 parser->scope,
7547 &idk,
7548 /*integral_constant_expression_p=*/false,
7549 /*allow_non_integral_constant_expression_p=*/false,
7550 /*non_integral_constant_expression_p=*/NULL,
7551 /*template_p=*/false,
7552 /*done=*/true,
7553 /*address_p=*/false,
7554 /*template_arg_p=*/false,
7555 &error_msg,
7556 capture_token->location);
7557 }
7558
7559 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7560 capture_init_expr
7561 = unqualified_name_lookup_error (capture_init_expr);
7562
7563 add_capture (lambda_expr,
7564 capture_id,
7565 capture_init_expr,
7566 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7567 explicit_init_p);
7568 }
7569
7570 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7571 }
7572
7573 /* Parse the (optional) middle of a lambda expression.
7574
7575 lambda-declarator:
7576 ( parameter-declaration-clause [opt] )
7577 attribute-specifier [opt]
7578 mutable [opt]
7579 exception-specification [opt]
7580 lambda-return-type-clause [opt]
7581
7582 LAMBDA_EXPR is the current representation of the lambda expression. */
7583
7584 static void
7585 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7586 {
7587 /* 5.1.1.4 of the standard says:
7588 If a lambda-expression does not include a lambda-declarator, it is as if
7589 the lambda-declarator were ().
7590 This means an empty parameter list, no attributes, and no exception
7591 specification. */
7592 tree param_list = void_list_node;
7593 tree attributes = NULL_TREE;
7594 tree exception_spec = NULL_TREE;
7595 tree t;
7596
7597 /* The lambda-declarator is optional, but must begin with an opening
7598 parenthesis if present. */
7599 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7600 {
7601 cp_lexer_consume_token (parser->lexer);
7602
7603 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7604
7605 /* Parse parameters. */
7606 param_list = cp_parser_parameter_declaration_clause (parser);
7607
7608 /* Default arguments shall not be specified in the
7609 parameter-declaration-clause of a lambda-declarator. */
7610 for (t = param_list; t; t = TREE_CHAIN (t))
7611 if (TREE_PURPOSE (t))
7612 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7613 "default argument specified for lambda parameter");
7614
7615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7616
7617 attributes = cp_parser_attributes_opt (parser);
7618
7619 /* Parse optional `mutable' keyword. */
7620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7621 {
7622 cp_lexer_consume_token (parser->lexer);
7623 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7624 }
7625
7626 /* Parse optional exception specification. */
7627 exception_spec = cp_parser_exception_specification_opt (parser);
7628
7629 /* Parse optional trailing return type. */
7630 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7631 {
7632 cp_lexer_consume_token (parser->lexer);
7633 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7634 }
7635
7636 /* The function parameters must be in scope all the way until after the
7637 trailing-return-type in case of decltype. */
7638 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7639 pop_binding (DECL_NAME (t), t);
7640
7641 leave_scope ();
7642 }
7643
7644 /* Create the function call operator.
7645
7646 Messing with declarators like this is no uglier than building up the
7647 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7648 other code. */
7649 {
7650 cp_decl_specifier_seq return_type_specs;
7651 cp_declarator* declarator;
7652 tree fco;
7653 int quals;
7654 void *p;
7655
7656 clear_decl_specs (&return_type_specs);
7657 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7658 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7659 else
7660 /* Maybe we will deduce the return type later, but we can use void
7661 as a placeholder return type anyways. */
7662 return_type_specs.type = void_type_node;
7663
7664 p = obstack_alloc (&declarator_obstack, 0);
7665
7666 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7667 sfk_none);
7668
7669 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7670 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7671 declarator = make_call_declarator (declarator, param_list, quals,
7672 VIRT_SPEC_UNSPECIFIED,
7673 exception_spec,
7674 /*late_return_type=*/NULL_TREE);
7675 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7676
7677 fco = grokmethod (&return_type_specs,
7678 declarator,
7679 attributes);
7680 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7681 DECL_ARTIFICIAL (fco) = 1;
7682
7683 finish_member_declaration (fco);
7684
7685 obstack_free (&declarator_obstack, p);
7686 }
7687 }
7688
7689 /* Parse the body of a lambda expression, which is simply
7690
7691 compound-statement
7692
7693 but which requires special handling.
7694 LAMBDA_EXPR is the current representation of the lambda expression. */
7695
7696 static void
7697 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7698 {
7699 bool nested = (current_function_decl != NULL_TREE);
7700 if (nested)
7701 push_function_context ();
7702
7703 /* Finish the function call operator
7704 - class_specifier
7705 + late_parsing_for_member
7706 + function_definition_after_declarator
7707 + ctor_initializer_opt_and_function_body */
7708 {
7709 tree fco = lambda_function (lambda_expr);
7710 tree body;
7711 bool done = false;
7712
7713 /* Let the front end know that we are going to be defining this
7714 function. */
7715 start_preparsed_function (fco,
7716 NULL_TREE,
7717 SF_PRE_PARSED | SF_INCLASS_INLINE);
7718
7719 start_lambda_scope (fco);
7720 body = begin_function_body ();
7721
7722 /* 5.1.1.4 of the standard says:
7723 If a lambda-expression does not include a trailing-return-type, it
7724 is as if the trailing-return-type denotes the following type:
7725 * if the compound-statement is of the form
7726 { return attribute-specifier [opt] expression ; }
7727 the type of the returned expression after lvalue-to-rvalue
7728 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7729 (_conv.array_ 4.2), and function-to-pointer conversion
7730 (_conv.func_ 4.3);
7731 * otherwise, void. */
7732
7733 /* In a lambda that has neither a lambda-return-type-clause
7734 nor a deducible form, errors should be reported for return statements
7735 in the body. Since we used void as the placeholder return type, parsing
7736 the body as usual will give such desired behavior. */
7737 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7738 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7739 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7740 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7741 {
7742 tree compound_stmt;
7743 tree expr = NULL_TREE;
7744 cp_id_kind idk = CP_ID_KIND_NONE;
7745
7746 /* Parse tentatively in case there's more after the initial return
7747 statement. */
7748 cp_parser_parse_tentatively (parser);
7749
7750 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7751 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7752
7753 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7754
7755 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7756 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7757
7758 if (cp_parser_parse_definitely (parser))
7759 {
7760 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7761
7762 compound_stmt = begin_compound_stmt (0);
7763 /* Will get error here if type not deduced yet. */
7764 finish_return_stmt (expr);
7765 finish_compound_stmt (compound_stmt);
7766
7767 done = true;
7768 }
7769 }
7770
7771 if (!done)
7772 {
7773 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7774 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7775 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7776 cp_parser_compound_stmt does not pass it. */
7777 cp_parser_function_body (parser);
7778 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7779 }
7780
7781 finish_function_body (body);
7782 finish_lambda_scope ();
7783
7784 /* Finish the function and generate code for it if necessary. */
7785 expand_or_defer_fn (finish_function (/*inline*/2));
7786 }
7787
7788 if (nested)
7789 pop_function_context();
7790 }
7791
7792 /* Statements [gram.stmt.stmt] */
7793
7794 /* Parse a statement.
7795
7796 statement:
7797 labeled-statement
7798 expression-statement
7799 compound-statement
7800 selection-statement
7801 iteration-statement
7802 jump-statement
7803 declaration-statement
7804 try-block
7805
7806 IN_COMPOUND is true when the statement is nested inside a
7807 cp_parser_compound_statement; this matters for certain pragmas.
7808
7809 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7810 is a (possibly labeled) if statement which is not enclosed in braces
7811 and has an else clause. This is used to implement -Wparentheses. */
7812
7813 static void
7814 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7815 bool in_compound, bool *if_p)
7816 {
7817 tree statement;
7818 cp_token *token;
7819 location_t statement_location;
7820
7821 restart:
7822 if (if_p != NULL)
7823 *if_p = false;
7824 /* There is no statement yet. */
7825 statement = NULL_TREE;
7826 /* Peek at the next token. */
7827 token = cp_lexer_peek_token (parser->lexer);
7828 /* Remember the location of the first token in the statement. */
7829 statement_location = token->location;
7830 /* If this is a keyword, then that will often determine what kind of
7831 statement we have. */
7832 if (token->type == CPP_KEYWORD)
7833 {
7834 enum rid keyword = token->keyword;
7835
7836 switch (keyword)
7837 {
7838 case RID_CASE:
7839 case RID_DEFAULT:
7840 /* Looks like a labeled-statement with a case label.
7841 Parse the label, and then use tail recursion to parse
7842 the statement. */
7843 cp_parser_label_for_labeled_statement (parser);
7844 goto restart;
7845
7846 case RID_IF:
7847 case RID_SWITCH:
7848 statement = cp_parser_selection_statement (parser, if_p);
7849 break;
7850
7851 case RID_WHILE:
7852 case RID_DO:
7853 case RID_FOR:
7854 statement = cp_parser_iteration_statement (parser);
7855 break;
7856
7857 case RID_BREAK:
7858 case RID_CONTINUE:
7859 case RID_RETURN:
7860 case RID_GOTO:
7861 statement = cp_parser_jump_statement (parser);
7862 break;
7863
7864 /* Objective-C++ exception-handling constructs. */
7865 case RID_AT_TRY:
7866 case RID_AT_CATCH:
7867 case RID_AT_FINALLY:
7868 case RID_AT_SYNCHRONIZED:
7869 case RID_AT_THROW:
7870 statement = cp_parser_objc_statement (parser);
7871 break;
7872
7873 case RID_TRY:
7874 statement = cp_parser_try_block (parser);
7875 break;
7876
7877 case RID_NAMESPACE:
7878 /* This must be a namespace alias definition. */
7879 cp_parser_declaration_statement (parser);
7880 return;
7881
7882 default:
7883 /* It might be a keyword like `int' that can start a
7884 declaration-statement. */
7885 break;
7886 }
7887 }
7888 else if (token->type == CPP_NAME)
7889 {
7890 /* If the next token is a `:', then we are looking at a
7891 labeled-statement. */
7892 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7893 if (token->type == CPP_COLON)
7894 {
7895 /* Looks like a labeled-statement with an ordinary label.
7896 Parse the label, and then use tail recursion to parse
7897 the statement. */
7898 cp_parser_label_for_labeled_statement (parser);
7899 goto restart;
7900 }
7901 }
7902 /* Anything that starts with a `{' must be a compound-statement. */
7903 else if (token->type == CPP_OPEN_BRACE)
7904 statement = cp_parser_compound_statement (parser, NULL, false, false);
7905 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7906 a statement all its own. */
7907 else if (token->type == CPP_PRAGMA)
7908 {
7909 /* Only certain OpenMP pragmas are attached to statements, and thus
7910 are considered statements themselves. All others are not. In
7911 the context of a compound, accept the pragma as a "statement" and
7912 return so that we can check for a close brace. Otherwise we
7913 require a real statement and must go back and read one. */
7914 if (in_compound)
7915 cp_parser_pragma (parser, pragma_compound);
7916 else if (!cp_parser_pragma (parser, pragma_stmt))
7917 goto restart;
7918 return;
7919 }
7920 else if (token->type == CPP_EOF)
7921 {
7922 cp_parser_error (parser, "expected statement");
7923 return;
7924 }
7925
7926 /* Everything else must be a declaration-statement or an
7927 expression-statement. Try for the declaration-statement
7928 first, unless we are looking at a `;', in which case we know that
7929 we have an expression-statement. */
7930 if (!statement)
7931 {
7932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7933 {
7934 cp_parser_parse_tentatively (parser);
7935 /* Try to parse the declaration-statement. */
7936 cp_parser_declaration_statement (parser);
7937 /* If that worked, we're done. */
7938 if (cp_parser_parse_definitely (parser))
7939 return;
7940 }
7941 /* Look for an expression-statement instead. */
7942 statement = cp_parser_expression_statement (parser, in_statement_expr);
7943 }
7944
7945 /* Set the line number for the statement. */
7946 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7947 SET_EXPR_LOCATION (statement, statement_location);
7948 }
7949
7950 /* Parse the label for a labeled-statement, i.e.
7951
7952 identifier :
7953 case constant-expression :
7954 default :
7955
7956 GNU Extension:
7957 case constant-expression ... constant-expression : statement
7958
7959 When a label is parsed without errors, the label is added to the
7960 parse tree by the finish_* functions, so this function doesn't
7961 have to return the label. */
7962
7963 static void
7964 cp_parser_label_for_labeled_statement (cp_parser* parser)
7965 {
7966 cp_token *token;
7967 tree label = NULL_TREE;
7968 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7969
7970 /* The next token should be an identifier. */
7971 token = cp_lexer_peek_token (parser->lexer);
7972 if (token->type != CPP_NAME
7973 && token->type != CPP_KEYWORD)
7974 {
7975 cp_parser_error (parser, "expected labeled-statement");
7976 return;
7977 }
7978
7979 parser->colon_corrects_to_scope_p = false;
7980 switch (token->keyword)
7981 {
7982 case RID_CASE:
7983 {
7984 tree expr, expr_hi;
7985 cp_token *ellipsis;
7986
7987 /* Consume the `case' token. */
7988 cp_lexer_consume_token (parser->lexer);
7989 /* Parse the constant-expression. */
7990 expr = cp_parser_constant_expression (parser,
7991 /*allow_non_constant_p=*/false,
7992 NULL);
7993
7994 ellipsis = cp_lexer_peek_token (parser->lexer);
7995 if (ellipsis->type == CPP_ELLIPSIS)
7996 {
7997 /* Consume the `...' token. */
7998 cp_lexer_consume_token (parser->lexer);
7999 expr_hi =
8000 cp_parser_constant_expression (parser,
8001 /*allow_non_constant_p=*/false,
8002 NULL);
8003 /* We don't need to emit warnings here, as the common code
8004 will do this for us. */
8005 }
8006 else
8007 expr_hi = NULL_TREE;
8008
8009 if (parser->in_switch_statement_p)
8010 finish_case_label (token->location, expr, expr_hi);
8011 else
8012 error_at (token->location,
8013 "case label %qE not within a switch statement",
8014 expr);
8015 }
8016 break;
8017
8018 case RID_DEFAULT:
8019 /* Consume the `default' token. */
8020 cp_lexer_consume_token (parser->lexer);
8021
8022 if (parser->in_switch_statement_p)
8023 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8024 else
8025 error_at (token->location, "case label not within a switch statement");
8026 break;
8027
8028 default:
8029 /* Anything else must be an ordinary label. */
8030 label = finish_label_stmt (cp_parser_identifier (parser));
8031 break;
8032 }
8033
8034 /* Require the `:' token. */
8035 cp_parser_require (parser, CPP_COLON, RT_COLON);
8036
8037 /* An ordinary label may optionally be followed by attributes.
8038 However, this is only permitted if the attributes are then
8039 followed by a semicolon. This is because, for backward
8040 compatibility, when parsing
8041 lab: __attribute__ ((unused)) int i;
8042 we want the attribute to attach to "i", not "lab". */
8043 if (label != NULL_TREE
8044 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8045 {
8046 tree attrs;
8047
8048 cp_parser_parse_tentatively (parser);
8049 attrs = cp_parser_attributes_opt (parser);
8050 if (attrs == NULL_TREE
8051 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8052 cp_parser_abort_tentative_parse (parser);
8053 else if (!cp_parser_parse_definitely (parser))
8054 ;
8055 else
8056 cplus_decl_attributes (&label, attrs, 0);
8057 }
8058
8059 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8060 }
8061
8062 /* Parse an expression-statement.
8063
8064 expression-statement:
8065 expression [opt] ;
8066
8067 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8068 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8069 indicates whether this expression-statement is part of an
8070 expression statement. */
8071
8072 static tree
8073 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8074 {
8075 tree statement = NULL_TREE;
8076 cp_token *token = cp_lexer_peek_token (parser->lexer);
8077
8078 /* If the next token is a ';', then there is no expression
8079 statement. */
8080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8081 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8082
8083 /* Give a helpful message for "A<T>::type t;" and the like. */
8084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8085 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8086 {
8087 if (TREE_CODE (statement) == SCOPE_REF)
8088 error_at (token->location, "need %<typename%> before %qE because "
8089 "%qT is a dependent scope",
8090 statement, TREE_OPERAND (statement, 0));
8091 else if (is_overloaded_fn (statement)
8092 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8093 {
8094 /* A::A a; */
8095 tree fn = get_first_fn (statement);
8096 error_at (token->location,
8097 "%<%T::%D%> names the constructor, not the type",
8098 DECL_CONTEXT (fn), DECL_NAME (fn));
8099 }
8100 }
8101
8102 /* Consume the final `;'. */
8103 cp_parser_consume_semicolon_at_end_of_statement (parser);
8104
8105 if (in_statement_expr
8106 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8107 /* This is the final expression statement of a statement
8108 expression. */
8109 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8110 else if (statement)
8111 statement = finish_expr_stmt (statement);
8112 else
8113 finish_stmt ();
8114
8115 return statement;
8116 }
8117
8118 /* Parse a compound-statement.
8119
8120 compound-statement:
8121 { statement-seq [opt] }
8122
8123 GNU extension:
8124
8125 compound-statement:
8126 { label-declaration-seq [opt] statement-seq [opt] }
8127
8128 label-declaration-seq:
8129 label-declaration
8130 label-declaration-seq label-declaration
8131
8132 Returns a tree representing the statement. */
8133
8134 static tree
8135 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8136 bool in_try, bool function_body)
8137 {
8138 tree compound_stmt;
8139
8140 /* Consume the `{'. */
8141 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8142 return error_mark_node;
8143 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8144 && !function_body)
8145 pedwarn (input_location, OPT_pedantic,
8146 "compound-statement in constexpr function");
8147 /* Begin the compound-statement. */
8148 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8149 /* If the next keyword is `__label__' we have a label declaration. */
8150 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8151 cp_parser_label_declaration (parser);
8152 /* Parse an (optional) statement-seq. */
8153 cp_parser_statement_seq_opt (parser, in_statement_expr);
8154 /* Finish the compound-statement. */
8155 finish_compound_stmt (compound_stmt);
8156 /* Consume the `}'. */
8157 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8158
8159 return compound_stmt;
8160 }
8161
8162 /* Parse an (optional) statement-seq.
8163
8164 statement-seq:
8165 statement
8166 statement-seq [opt] statement */
8167
8168 static void
8169 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8170 {
8171 /* Scan statements until there aren't any more. */
8172 while (true)
8173 {
8174 cp_token *token = cp_lexer_peek_token (parser->lexer);
8175
8176 /* If we are looking at a `}', then we have run out of
8177 statements; the same is true if we have reached the end
8178 of file, or have stumbled upon a stray '@end'. */
8179 if (token->type == CPP_CLOSE_BRACE
8180 || token->type == CPP_EOF
8181 || token->type == CPP_PRAGMA_EOL
8182 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8183 break;
8184
8185 /* If we are in a compound statement and find 'else' then
8186 something went wrong. */
8187 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8188 {
8189 if (parser->in_statement & IN_IF_STMT)
8190 break;
8191 else
8192 {
8193 token = cp_lexer_consume_token (parser->lexer);
8194 error_at (token->location, "%<else%> without a previous %<if%>");
8195 }
8196 }
8197
8198 /* Parse the statement. */
8199 cp_parser_statement (parser, in_statement_expr, true, NULL);
8200 }
8201 }
8202
8203 /* Parse a selection-statement.
8204
8205 selection-statement:
8206 if ( condition ) statement
8207 if ( condition ) statement else statement
8208 switch ( condition ) statement
8209
8210 Returns the new IF_STMT or SWITCH_STMT.
8211
8212 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8213 is a (possibly labeled) if statement which is not enclosed in
8214 braces and has an else clause. This is used to implement
8215 -Wparentheses. */
8216
8217 static tree
8218 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8219 {
8220 cp_token *token;
8221 enum rid keyword;
8222
8223 if (if_p != NULL)
8224 *if_p = false;
8225
8226 /* Peek at the next token. */
8227 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8228
8229 /* See what kind of keyword it is. */
8230 keyword = token->keyword;
8231 switch (keyword)
8232 {
8233 case RID_IF:
8234 case RID_SWITCH:
8235 {
8236 tree statement;
8237 tree condition;
8238
8239 /* Look for the `('. */
8240 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8241 {
8242 cp_parser_skip_to_end_of_statement (parser);
8243 return error_mark_node;
8244 }
8245
8246 /* Begin the selection-statement. */
8247 if (keyword == RID_IF)
8248 statement = begin_if_stmt ();
8249 else
8250 statement = begin_switch_stmt ();
8251
8252 /* Parse the condition. */
8253 condition = cp_parser_condition (parser);
8254 /* Look for the `)'. */
8255 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8256 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8257 /*consume_paren=*/true);
8258
8259 if (keyword == RID_IF)
8260 {
8261 bool nested_if;
8262 unsigned char in_statement;
8263
8264 /* Add the condition. */
8265 finish_if_stmt_cond (condition, statement);
8266
8267 /* Parse the then-clause. */
8268 in_statement = parser->in_statement;
8269 parser->in_statement |= IN_IF_STMT;
8270 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8271 {
8272 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8273 add_stmt (build_empty_stmt (loc));
8274 cp_lexer_consume_token (parser->lexer);
8275 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8276 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8277 "empty body in an %<if%> statement");
8278 nested_if = false;
8279 }
8280 else
8281 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8282 parser->in_statement = in_statement;
8283
8284 finish_then_clause (statement);
8285
8286 /* If the next token is `else', parse the else-clause. */
8287 if (cp_lexer_next_token_is_keyword (parser->lexer,
8288 RID_ELSE))
8289 {
8290 /* Consume the `else' keyword. */
8291 cp_lexer_consume_token (parser->lexer);
8292 begin_else_clause (statement);
8293 /* Parse the else-clause. */
8294 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8295 {
8296 location_t loc;
8297 loc = cp_lexer_peek_token (parser->lexer)->location;
8298 warning_at (loc,
8299 OPT_Wempty_body, "suggest braces around "
8300 "empty body in an %<else%> statement");
8301 add_stmt (build_empty_stmt (loc));
8302 cp_lexer_consume_token (parser->lexer);
8303 }
8304 else
8305 cp_parser_implicitly_scoped_statement (parser, NULL);
8306
8307 finish_else_clause (statement);
8308
8309 /* If we are currently parsing a then-clause, then
8310 IF_P will not be NULL. We set it to true to
8311 indicate that this if statement has an else clause.
8312 This may trigger the Wparentheses warning below
8313 when we get back up to the parent if statement. */
8314 if (if_p != NULL)
8315 *if_p = true;
8316 }
8317 else
8318 {
8319 /* This if statement does not have an else clause. If
8320 NESTED_IF is true, then the then-clause is an if
8321 statement which does have an else clause. We warn
8322 about the potential ambiguity. */
8323 if (nested_if)
8324 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8325 "suggest explicit braces to avoid ambiguous"
8326 " %<else%>");
8327 }
8328
8329 /* Now we're all done with the if-statement. */
8330 finish_if_stmt (statement);
8331 }
8332 else
8333 {
8334 bool in_switch_statement_p;
8335 unsigned char in_statement;
8336
8337 /* Add the condition. */
8338 finish_switch_cond (condition, statement);
8339
8340 /* Parse the body of the switch-statement. */
8341 in_switch_statement_p = parser->in_switch_statement_p;
8342 in_statement = parser->in_statement;
8343 parser->in_switch_statement_p = true;
8344 parser->in_statement |= IN_SWITCH_STMT;
8345 cp_parser_implicitly_scoped_statement (parser, NULL);
8346 parser->in_switch_statement_p = in_switch_statement_p;
8347 parser->in_statement = in_statement;
8348
8349 /* Now we're all done with the switch-statement. */
8350 finish_switch_stmt (statement);
8351 }
8352
8353 return statement;
8354 }
8355 break;
8356
8357 default:
8358 cp_parser_error (parser, "expected selection-statement");
8359 return error_mark_node;
8360 }
8361 }
8362
8363 /* Parse a condition.
8364
8365 condition:
8366 expression
8367 type-specifier-seq declarator = initializer-clause
8368 type-specifier-seq declarator braced-init-list
8369
8370 GNU Extension:
8371
8372 condition:
8373 type-specifier-seq declarator asm-specification [opt]
8374 attributes [opt] = assignment-expression
8375
8376 Returns the expression that should be tested. */
8377
8378 static tree
8379 cp_parser_condition (cp_parser* parser)
8380 {
8381 cp_decl_specifier_seq type_specifiers;
8382 const char *saved_message;
8383 int declares_class_or_enum;
8384
8385 /* Try the declaration first. */
8386 cp_parser_parse_tentatively (parser);
8387 /* New types are not allowed in the type-specifier-seq for a
8388 condition. */
8389 saved_message = parser->type_definition_forbidden_message;
8390 parser->type_definition_forbidden_message
8391 = G_("types may not be defined in conditions");
8392 /* Parse the type-specifier-seq. */
8393 cp_parser_decl_specifier_seq (parser,
8394 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8395 &type_specifiers,
8396 &declares_class_or_enum);
8397 /* Restore the saved message. */
8398 parser->type_definition_forbidden_message = saved_message;
8399 /* If all is well, we might be looking at a declaration. */
8400 if (!cp_parser_error_occurred (parser))
8401 {
8402 tree decl;
8403 tree asm_specification;
8404 tree attributes;
8405 cp_declarator *declarator;
8406 tree initializer = NULL_TREE;
8407
8408 /* Parse the declarator. */
8409 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8410 /*ctor_dtor_or_conv_p=*/NULL,
8411 /*parenthesized_p=*/NULL,
8412 /*member_p=*/false);
8413 /* Parse the attributes. */
8414 attributes = cp_parser_attributes_opt (parser);
8415 /* Parse the asm-specification. */
8416 asm_specification = cp_parser_asm_specification_opt (parser);
8417 /* If the next token is not an `=' or '{', then we might still be
8418 looking at an expression. For example:
8419
8420 if (A(a).x)
8421
8422 looks like a decl-specifier-seq and a declarator -- but then
8423 there is no `=', so this is an expression. */
8424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8425 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8426 cp_parser_simulate_error (parser);
8427
8428 /* If we did see an `=' or '{', then we are looking at a declaration
8429 for sure. */
8430 if (cp_parser_parse_definitely (parser))
8431 {
8432 tree pushed_scope;
8433 bool non_constant_p;
8434 bool flags = LOOKUP_ONLYCONVERTING;
8435
8436 /* Create the declaration. */
8437 decl = start_decl (declarator, &type_specifiers,
8438 /*initialized_p=*/true,
8439 attributes, /*prefix_attributes=*/NULL_TREE,
8440 &pushed_scope);
8441
8442 /* Parse the initializer. */
8443 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8444 {
8445 initializer = cp_parser_braced_list (parser, &non_constant_p);
8446 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8447 flags = 0;
8448 }
8449 else
8450 {
8451 /* Consume the `='. */
8452 cp_parser_require (parser, CPP_EQ, RT_EQ);
8453 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8454 }
8455 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8456 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8457
8458 /* Process the initializer. */
8459 cp_finish_decl (decl,
8460 initializer, !non_constant_p,
8461 asm_specification,
8462 flags);
8463
8464 if (pushed_scope)
8465 pop_scope (pushed_scope);
8466
8467 return convert_from_reference (decl);
8468 }
8469 }
8470 /* If we didn't even get past the declarator successfully, we are
8471 definitely not looking at a declaration. */
8472 else
8473 cp_parser_abort_tentative_parse (parser);
8474
8475 /* Otherwise, we are looking at an expression. */
8476 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8477 }
8478
8479 /* Parses a for-statement or range-for-statement until the closing ')',
8480 not included. */
8481
8482 static tree
8483 cp_parser_for (cp_parser *parser)
8484 {
8485 tree init, scope, decl;
8486 bool is_range_for;
8487
8488 /* Begin the for-statement. */
8489 scope = begin_for_scope (&init);
8490
8491 /* Parse the initialization. */
8492 is_range_for = cp_parser_for_init_statement (parser, &decl);
8493
8494 if (is_range_for)
8495 return cp_parser_range_for (parser, scope, init, decl);
8496 else
8497 return cp_parser_c_for (parser, scope, init);
8498 }
8499
8500 static tree
8501 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8502 {
8503 /* Normal for loop */
8504 tree condition = NULL_TREE;
8505 tree expression = NULL_TREE;
8506 tree stmt;
8507
8508 stmt = begin_for_stmt (scope, init);
8509 /* The for-init-statement has already been parsed in
8510 cp_parser_for_init_statement, so no work is needed here. */
8511 finish_for_init_stmt (stmt);
8512
8513 /* If there's a condition, process it. */
8514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8515 condition = cp_parser_condition (parser);
8516 finish_for_cond (condition, stmt);
8517 /* Look for the `;'. */
8518 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8519
8520 /* If there's an expression, process it. */
8521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8522 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8523 finish_for_expr (expression, stmt);
8524
8525 return stmt;
8526 }
8527
8528 /* Tries to parse a range-based for-statement:
8529
8530 range-based-for:
8531 decl-specifier-seq declarator : expression
8532
8533 The decl-specifier-seq declarator and the `:' are already parsed by
8534 cp_parser_for_init_statement. If processing_template_decl it returns a
8535 newly created RANGE_FOR_STMT; if not, it is converted to a
8536 regular FOR_STMT. */
8537
8538 static tree
8539 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8540 {
8541 tree stmt, range_expr;
8542
8543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8544 {
8545 bool expr_non_constant_p;
8546 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8547 }
8548 else
8549 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8550
8551 /* If in template, STMT is converted to a normal for-statement
8552 at instantiation. If not, it is done just ahead. */
8553 if (processing_template_decl)
8554 {
8555 stmt = begin_range_for_stmt (scope, init);
8556 finish_range_for_decl (stmt, range_decl, range_expr);
8557 }
8558 else
8559 {
8560 stmt = begin_for_stmt (scope, init);
8561 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8562 }
8563 return stmt;
8564 }
8565
8566 /* Converts a range-based for-statement into a normal
8567 for-statement, as per the definition.
8568
8569 for (RANGE_DECL : RANGE_EXPR)
8570 BLOCK
8571
8572 should be equivalent to:
8573
8574 {
8575 auto &&__range = RANGE_EXPR;
8576 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8577 __begin != __end;
8578 ++__begin)
8579 {
8580 RANGE_DECL = *__begin;
8581 BLOCK
8582 }
8583 }
8584
8585 If RANGE_EXPR is an array:
8586 BEGIN_EXPR = __range
8587 END_EXPR = __range + ARRAY_SIZE(__range)
8588 Else if RANGE_EXPR has a member 'begin' or 'end':
8589 BEGIN_EXPR = __range.begin()
8590 END_EXPR = __range.end()
8591 Else:
8592 BEGIN_EXPR = begin(__range)
8593 END_EXPR = end(__range);
8594
8595 If __range has a member 'begin' but not 'end', or vice versa, we must
8596 still use the second alternative (it will surely fail, however).
8597 When calling begin()/end() in the third alternative we must use
8598 argument dependent lookup, but always considering 'std' as an associated
8599 namespace. */
8600
8601 tree
8602 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8603 {
8604 tree range_type, range_temp;
8605 tree begin, end;
8606 tree iter_type, begin_expr, end_expr;
8607 tree condition, expression;
8608
8609 if (range_decl == error_mark_node || range_expr == error_mark_node)
8610 /* If an error happened previously do nothing or else a lot of
8611 unhelpful errors would be issued. */
8612 begin_expr = end_expr = iter_type = error_mark_node;
8613 else
8614 {
8615 /* Find out the type deduced by the declaration
8616 `auto &&__range = range_expr'. */
8617 range_type = cp_build_reference_type (make_auto (), true);
8618 range_type = do_auto_deduction (range_type, range_expr,
8619 type_uses_auto (range_type));
8620
8621 /* Create the __range variable. */
8622 range_temp = build_decl (input_location, VAR_DECL,
8623 get_identifier ("__for_range"), range_type);
8624 TREE_USED (range_temp) = 1;
8625 DECL_ARTIFICIAL (range_temp) = 1;
8626 pushdecl (range_temp);
8627 cp_finish_decl (range_temp, range_expr,
8628 /*is_constant_init*/false, NULL_TREE,
8629 LOOKUP_ONLYCONVERTING);
8630
8631 range_temp = convert_from_reference (range_temp);
8632 iter_type = cp_parser_perform_range_for_lookup (range_temp,
8633 &begin_expr, &end_expr);
8634 }
8635
8636 /* The new for initialization statement. */
8637 begin = build_decl (input_location, VAR_DECL,
8638 get_identifier ("__for_begin"), iter_type);
8639 TREE_USED (begin) = 1;
8640 DECL_ARTIFICIAL (begin) = 1;
8641 pushdecl (begin);
8642 cp_finish_decl (begin, begin_expr,
8643 /*is_constant_init*/false, NULL_TREE,
8644 LOOKUP_ONLYCONVERTING);
8645
8646 end = build_decl (input_location, VAR_DECL,
8647 get_identifier ("__for_end"), iter_type);
8648 TREE_USED (end) = 1;
8649 DECL_ARTIFICIAL (end) = 1;
8650 pushdecl (end);
8651 cp_finish_decl (end, end_expr,
8652 /*is_constant_init*/false, NULL_TREE,
8653 LOOKUP_ONLYCONVERTING);
8654
8655 finish_for_init_stmt (statement);
8656
8657 /* The new for condition. */
8658 condition = build_x_binary_op (NE_EXPR,
8659 begin, ERROR_MARK,
8660 end, ERROR_MARK,
8661 NULL, tf_warning_or_error);
8662 finish_for_cond (condition, statement);
8663
8664 /* The new increment expression. */
8665 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8666 finish_for_expr (expression, statement);
8667
8668 /* The declaration is initialized with *__begin inside the loop body. */
8669 cp_finish_decl (range_decl,
8670 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8671 /*is_constant_init*/false, NULL_TREE,
8672 LOOKUP_ONLYCONVERTING);
8673
8674 return statement;
8675 }
8676
8677 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8678 We need to solve both at the same time because the method used
8679 depends on the existence of members begin or end.
8680 Returns the type deduced for the iterator expression. */
8681
8682 static tree
8683 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8684 {
8685 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8686 {
8687 error ("range-based %<for%> expression of type %qT "
8688 "has incomplete type", TREE_TYPE (range));
8689 *begin = *end = error_mark_node;
8690 return error_mark_node;
8691 }
8692 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8693 {
8694 /* If RANGE is an array, we will use pointer arithmetic. */
8695 *begin = range;
8696 *end = build_binary_op (input_location, PLUS_EXPR,
8697 range,
8698 array_type_nelts_top (TREE_TYPE (range)),
8699 0);
8700 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8701 }
8702 else
8703 {
8704 /* If it is not an array, we must do a bit of magic. */
8705 tree id_begin, id_end;
8706 tree member_begin, member_end;
8707
8708 *begin = *end = error_mark_node;
8709
8710 id_begin = get_identifier ("begin");
8711 id_end = get_identifier ("end");
8712 member_begin = lookup_member (TREE_TYPE (range), id_begin,
8713 /*protect=*/2, /*want_type=*/false);
8714 member_end = lookup_member (TREE_TYPE (range), id_end,
8715 /*protect=*/2, /*want_type=*/false);
8716
8717 if (member_begin != NULL_TREE || member_end != NULL_TREE)
8718 {
8719 /* Use the member functions. */
8720 if (member_begin != NULL_TREE)
8721 *begin = cp_parser_range_for_member_function (range, id_begin);
8722 else
8723 error ("range-based %<for%> expression of type %qT has an "
8724 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8725
8726 if (member_end != NULL_TREE)
8727 *end = cp_parser_range_for_member_function (range, id_end);
8728 else
8729 error ("range-based %<for%> expression of type %qT has a "
8730 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8731 }
8732 else
8733 {
8734 /* Use global functions with ADL. */
8735 VEC(tree,gc) *vec;
8736 vec = make_tree_vector ();
8737
8738 VEC_safe_push (tree, gc, vec, range);
8739
8740 member_begin = perform_koenig_lookup (id_begin, vec,
8741 /*include_std=*/true);
8742 *begin = finish_call_expr (member_begin, &vec, false, true,
8743 tf_warning_or_error);
8744 member_end = perform_koenig_lookup (id_end, vec,
8745 /*include_std=*/true);
8746 *end = finish_call_expr (member_end, &vec, false, true,
8747 tf_warning_or_error);
8748
8749 release_tree_vector (vec);
8750 }
8751
8752 /* Last common checks. */
8753 if (*begin == error_mark_node || *end == error_mark_node)
8754 {
8755 /* If one of the expressions is an error do no more checks. */
8756 *begin = *end = error_mark_node;
8757 return error_mark_node;
8758 }
8759 else
8760 {
8761 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8762 /* The unqualified type of the __begin and __end temporaries should
8763 be the same, as required by the multiple auto declaration. */
8764 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8765 error ("inconsistent begin/end types in range-based %<for%> "
8766 "statement: %qT and %qT",
8767 TREE_TYPE (*begin), TREE_TYPE (*end));
8768 return iter_type;
8769 }
8770 }
8771 }
8772
8773 /* Helper function for cp_parser_perform_range_for_lookup.
8774 Builds a tree for RANGE.IDENTIFIER(). */
8775
8776 static tree
8777 cp_parser_range_for_member_function (tree range, tree identifier)
8778 {
8779 tree member, res;
8780 VEC(tree,gc) *vec;
8781
8782 member = finish_class_member_access_expr (range, identifier,
8783 false, tf_warning_or_error);
8784 if (member == error_mark_node)
8785 return error_mark_node;
8786
8787 vec = make_tree_vector ();
8788 res = finish_call_expr (member, &vec,
8789 /*disallow_virtual=*/false,
8790 /*koenig_p=*/false,
8791 tf_warning_or_error);
8792 release_tree_vector (vec);
8793 return res;
8794 }
8795
8796 /* Parse an iteration-statement.
8797
8798 iteration-statement:
8799 while ( condition ) statement
8800 do statement while ( expression ) ;
8801 for ( for-init-statement condition [opt] ; expression [opt] )
8802 statement
8803
8804 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8805
8806 static tree
8807 cp_parser_iteration_statement (cp_parser* parser)
8808 {
8809 cp_token *token;
8810 enum rid keyword;
8811 tree statement;
8812 unsigned char in_statement;
8813
8814 /* Peek at the next token. */
8815 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8816 if (!token)
8817 return error_mark_node;
8818
8819 /* Remember whether or not we are already within an iteration
8820 statement. */
8821 in_statement = parser->in_statement;
8822
8823 /* See what kind of keyword it is. */
8824 keyword = token->keyword;
8825 switch (keyword)
8826 {
8827 case RID_WHILE:
8828 {
8829 tree condition;
8830
8831 /* Begin the while-statement. */
8832 statement = begin_while_stmt ();
8833 /* Look for the `('. */
8834 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8835 /* Parse the condition. */
8836 condition = cp_parser_condition (parser);
8837 finish_while_stmt_cond (condition, statement);
8838 /* Look for the `)'. */
8839 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8840 /* Parse the dependent statement. */
8841 parser->in_statement = IN_ITERATION_STMT;
8842 cp_parser_already_scoped_statement (parser);
8843 parser->in_statement = in_statement;
8844 /* We're done with the while-statement. */
8845 finish_while_stmt (statement);
8846 }
8847 break;
8848
8849 case RID_DO:
8850 {
8851 tree expression;
8852
8853 /* Begin the do-statement. */
8854 statement = begin_do_stmt ();
8855 /* Parse the body of the do-statement. */
8856 parser->in_statement = IN_ITERATION_STMT;
8857 cp_parser_implicitly_scoped_statement (parser, NULL);
8858 parser->in_statement = in_statement;
8859 finish_do_body (statement);
8860 /* Look for the `while' keyword. */
8861 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8862 /* Look for the `('. */
8863 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8864 /* Parse the expression. */
8865 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8866 /* We're done with the do-statement. */
8867 finish_do_stmt (expression, statement);
8868 /* Look for the `)'. */
8869 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8870 /* Look for the `;'. */
8871 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8872 }
8873 break;
8874
8875 case RID_FOR:
8876 {
8877 /* Look for the `('. */
8878 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8879
8880 statement = cp_parser_for (parser);
8881
8882 /* Look for the `)'. */
8883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8884
8885 /* Parse the body of the for-statement. */
8886 parser->in_statement = IN_ITERATION_STMT;
8887 cp_parser_already_scoped_statement (parser);
8888 parser->in_statement = in_statement;
8889
8890 /* We're done with the for-statement. */
8891 finish_for_stmt (statement);
8892 }
8893 break;
8894
8895 default:
8896 cp_parser_error (parser, "expected iteration-statement");
8897 statement = error_mark_node;
8898 break;
8899 }
8900
8901 return statement;
8902 }
8903
8904 /* Parse a for-init-statement or the declarator of a range-based-for.
8905 Returns true if a range-based-for declaration is seen.
8906
8907 for-init-statement:
8908 expression-statement
8909 simple-declaration */
8910
8911 static bool
8912 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8913 {
8914 /* If the next token is a `;', then we have an empty
8915 expression-statement. Grammatically, this is also a
8916 simple-declaration, but an invalid one, because it does not
8917 declare anything. Therefore, if we did not handle this case
8918 specially, we would issue an error message about an invalid
8919 declaration. */
8920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8921 {
8922 bool is_range_for = false;
8923 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8924
8925 parser->colon_corrects_to_scope_p = false;
8926
8927 /* We're going to speculatively look for a declaration, falling back
8928 to an expression, if necessary. */
8929 cp_parser_parse_tentatively (parser);
8930 /* Parse the declaration. */
8931 cp_parser_simple_declaration (parser,
8932 /*function_definition_allowed_p=*/false,
8933 decl);
8934 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8935 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8936 {
8937 /* It is a range-for, consume the ':' */
8938 cp_lexer_consume_token (parser->lexer);
8939 is_range_for = true;
8940 if (cxx_dialect < cxx0x)
8941 {
8942 error_at (cp_lexer_peek_token (parser->lexer)->location,
8943 "range-based %<for%> loops are not allowed "
8944 "in C++98 mode");
8945 *decl = error_mark_node;
8946 }
8947 }
8948 else
8949 /* The ';' is not consumed yet because we told
8950 cp_parser_simple_declaration not to. */
8951 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8952
8953 if (cp_parser_parse_definitely (parser))
8954 return is_range_for;
8955 /* If the tentative parse failed, then we shall need to look for an
8956 expression-statement. */
8957 }
8958 /* If we are here, it is an expression-statement. */
8959 cp_parser_expression_statement (parser, NULL_TREE);
8960 return false;
8961 }
8962
8963 /* Parse a jump-statement.
8964
8965 jump-statement:
8966 break ;
8967 continue ;
8968 return expression [opt] ;
8969 return braced-init-list ;
8970 goto identifier ;
8971
8972 GNU extension:
8973
8974 jump-statement:
8975 goto * expression ;
8976
8977 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8978
8979 static tree
8980 cp_parser_jump_statement (cp_parser* parser)
8981 {
8982 tree statement = error_mark_node;
8983 cp_token *token;
8984 enum rid keyword;
8985 unsigned char in_statement;
8986
8987 /* Peek at the next token. */
8988 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8989 if (!token)
8990 return error_mark_node;
8991
8992 /* See what kind of keyword it is. */
8993 keyword = token->keyword;
8994 switch (keyword)
8995 {
8996 case RID_BREAK:
8997 in_statement = parser->in_statement & ~IN_IF_STMT;
8998 switch (in_statement)
8999 {
9000 case 0:
9001 error_at (token->location, "break statement not within loop or switch");
9002 break;
9003 default:
9004 gcc_assert ((in_statement & IN_SWITCH_STMT)
9005 || in_statement == IN_ITERATION_STMT);
9006 statement = finish_break_stmt ();
9007 break;
9008 case IN_OMP_BLOCK:
9009 error_at (token->location, "invalid exit from OpenMP structured block");
9010 break;
9011 case IN_OMP_FOR:
9012 error_at (token->location, "break statement used with OpenMP for loop");
9013 break;
9014 }
9015 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9016 break;
9017
9018 case RID_CONTINUE:
9019 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9020 {
9021 case 0:
9022 error_at (token->location, "continue statement not within a loop");
9023 break;
9024 case IN_ITERATION_STMT:
9025 case IN_OMP_FOR:
9026 statement = finish_continue_stmt ();
9027 break;
9028 case IN_OMP_BLOCK:
9029 error_at (token->location, "invalid exit from OpenMP structured block");
9030 break;
9031 default:
9032 gcc_unreachable ();
9033 }
9034 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9035 break;
9036
9037 case RID_RETURN:
9038 {
9039 tree expr;
9040 bool expr_non_constant_p;
9041
9042 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9043 {
9044 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9045 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9046 }
9047 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9048 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9049 else
9050 /* If the next token is a `;', then there is no
9051 expression. */
9052 expr = NULL_TREE;
9053 /* Build the return-statement. */
9054 statement = finish_return_stmt (expr);
9055 /* Look for the final `;'. */
9056 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9057 }
9058 break;
9059
9060 case RID_GOTO:
9061 /* Create the goto-statement. */
9062 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9063 {
9064 /* Issue a warning about this use of a GNU extension. */
9065 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9066 /* Consume the '*' token. */
9067 cp_lexer_consume_token (parser->lexer);
9068 /* Parse the dependent expression. */
9069 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9070 }
9071 else
9072 finish_goto_stmt (cp_parser_identifier (parser));
9073 /* Look for the final `;'. */
9074 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9075 break;
9076
9077 default:
9078 cp_parser_error (parser, "expected jump-statement");
9079 break;
9080 }
9081
9082 return statement;
9083 }
9084
9085 /* Parse a declaration-statement.
9086
9087 declaration-statement:
9088 block-declaration */
9089
9090 static void
9091 cp_parser_declaration_statement (cp_parser* parser)
9092 {
9093 void *p;
9094
9095 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9096 p = obstack_alloc (&declarator_obstack, 0);
9097
9098 /* Parse the block-declaration. */
9099 cp_parser_block_declaration (parser, /*statement_p=*/true);
9100
9101 /* Free any declarators allocated. */
9102 obstack_free (&declarator_obstack, p);
9103
9104 /* Finish off the statement. */
9105 finish_stmt ();
9106 }
9107
9108 /* Some dependent statements (like `if (cond) statement'), are
9109 implicitly in their own scope. In other words, if the statement is
9110 a single statement (as opposed to a compound-statement), it is
9111 none-the-less treated as if it were enclosed in braces. Any
9112 declarations appearing in the dependent statement are out of scope
9113 after control passes that point. This function parses a statement,
9114 but ensures that is in its own scope, even if it is not a
9115 compound-statement.
9116
9117 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9118 is a (possibly labeled) if statement which is not enclosed in
9119 braces and has an else clause. This is used to implement
9120 -Wparentheses.
9121
9122 Returns the new statement. */
9123
9124 static tree
9125 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9126 {
9127 tree statement;
9128
9129 if (if_p != NULL)
9130 *if_p = false;
9131
9132 /* Mark if () ; with a special NOP_EXPR. */
9133 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9134 {
9135 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9136 cp_lexer_consume_token (parser->lexer);
9137 statement = add_stmt (build_empty_stmt (loc));
9138 }
9139 /* if a compound is opened, we simply parse the statement directly. */
9140 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9141 statement = cp_parser_compound_statement (parser, NULL, false, false);
9142 /* If the token is not a `{', then we must take special action. */
9143 else
9144 {
9145 /* Create a compound-statement. */
9146 statement = begin_compound_stmt (0);
9147 /* Parse the dependent-statement. */
9148 cp_parser_statement (parser, NULL_TREE, false, if_p);
9149 /* Finish the dummy compound-statement. */
9150 finish_compound_stmt (statement);
9151 }
9152
9153 /* Return the statement. */
9154 return statement;
9155 }
9156
9157 /* For some dependent statements (like `while (cond) statement'), we
9158 have already created a scope. Therefore, even if the dependent
9159 statement is a compound-statement, we do not want to create another
9160 scope. */
9161
9162 static void
9163 cp_parser_already_scoped_statement (cp_parser* parser)
9164 {
9165 /* If the token is a `{', then we must take special action. */
9166 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9167 cp_parser_statement (parser, NULL_TREE, false, NULL);
9168 else
9169 {
9170 /* Avoid calling cp_parser_compound_statement, so that we
9171 don't create a new scope. Do everything else by hand. */
9172 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9173 /* If the next keyword is `__label__' we have a label declaration. */
9174 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9175 cp_parser_label_declaration (parser);
9176 /* Parse an (optional) statement-seq. */
9177 cp_parser_statement_seq_opt (parser, NULL_TREE);
9178 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9179 }
9180 }
9181
9182 /* Declarations [gram.dcl.dcl] */
9183
9184 /* Parse an optional declaration-sequence.
9185
9186 declaration-seq:
9187 declaration
9188 declaration-seq declaration */
9189
9190 static void
9191 cp_parser_declaration_seq_opt (cp_parser* parser)
9192 {
9193 while (true)
9194 {
9195 cp_token *token;
9196
9197 token = cp_lexer_peek_token (parser->lexer);
9198
9199 if (token->type == CPP_CLOSE_BRACE
9200 || token->type == CPP_EOF
9201 || token->type == CPP_PRAGMA_EOL)
9202 break;
9203
9204 if (token->type == CPP_SEMICOLON)
9205 {
9206 /* A declaration consisting of a single semicolon is
9207 invalid. Allow it unless we're being pedantic. */
9208 cp_lexer_consume_token (parser->lexer);
9209 if (!in_system_header)
9210 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9211 continue;
9212 }
9213
9214 /* If we're entering or exiting a region that's implicitly
9215 extern "C", modify the lang context appropriately. */
9216 if (!parser->implicit_extern_c && token->implicit_extern_c)
9217 {
9218 push_lang_context (lang_name_c);
9219 parser->implicit_extern_c = true;
9220 }
9221 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9222 {
9223 pop_lang_context ();
9224 parser->implicit_extern_c = false;
9225 }
9226
9227 if (token->type == CPP_PRAGMA)
9228 {
9229 /* A top-level declaration can consist solely of a #pragma.
9230 A nested declaration cannot, so this is done here and not
9231 in cp_parser_declaration. (A #pragma at block scope is
9232 handled in cp_parser_statement.) */
9233 cp_parser_pragma (parser, pragma_external);
9234 continue;
9235 }
9236
9237 /* Parse the declaration itself. */
9238 cp_parser_declaration (parser);
9239 }
9240 }
9241
9242 /* Parse a declaration.
9243
9244 declaration:
9245 block-declaration
9246 function-definition
9247 template-declaration
9248 explicit-instantiation
9249 explicit-specialization
9250 linkage-specification
9251 namespace-definition
9252
9253 GNU extension:
9254
9255 declaration:
9256 __extension__ declaration */
9257
9258 static void
9259 cp_parser_declaration (cp_parser* parser)
9260 {
9261 cp_token token1;
9262 cp_token token2;
9263 int saved_pedantic;
9264 void *p;
9265 tree attributes = NULL_TREE;
9266
9267 /* Check for the `__extension__' keyword. */
9268 if (cp_parser_extension_opt (parser, &saved_pedantic))
9269 {
9270 /* Parse the qualified declaration. */
9271 cp_parser_declaration (parser);
9272 /* Restore the PEDANTIC flag. */
9273 pedantic = saved_pedantic;
9274
9275 return;
9276 }
9277
9278 /* Try to figure out what kind of declaration is present. */
9279 token1 = *cp_lexer_peek_token (parser->lexer);
9280
9281 if (token1.type != CPP_EOF)
9282 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9283 else
9284 {
9285 token2.type = CPP_EOF;
9286 token2.keyword = RID_MAX;
9287 }
9288
9289 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9290 p = obstack_alloc (&declarator_obstack, 0);
9291
9292 /* If the next token is `extern' and the following token is a string
9293 literal, then we have a linkage specification. */
9294 if (token1.keyword == RID_EXTERN
9295 && cp_parser_is_string_literal (&token2))
9296 cp_parser_linkage_specification (parser);
9297 /* If the next token is `template', then we have either a template
9298 declaration, an explicit instantiation, or an explicit
9299 specialization. */
9300 else if (token1.keyword == RID_TEMPLATE)
9301 {
9302 /* `template <>' indicates a template specialization. */
9303 if (token2.type == CPP_LESS
9304 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9305 cp_parser_explicit_specialization (parser);
9306 /* `template <' indicates a template declaration. */
9307 else if (token2.type == CPP_LESS)
9308 cp_parser_template_declaration (parser, /*member_p=*/false);
9309 /* Anything else must be an explicit instantiation. */
9310 else
9311 cp_parser_explicit_instantiation (parser);
9312 }
9313 /* If the next token is `export', then we have a template
9314 declaration. */
9315 else if (token1.keyword == RID_EXPORT)
9316 cp_parser_template_declaration (parser, /*member_p=*/false);
9317 /* If the next token is `extern', 'static' or 'inline' and the one
9318 after that is `template', we have a GNU extended explicit
9319 instantiation directive. */
9320 else if (cp_parser_allow_gnu_extensions_p (parser)
9321 && (token1.keyword == RID_EXTERN
9322 || token1.keyword == RID_STATIC
9323 || token1.keyword == RID_INLINE)
9324 && token2.keyword == RID_TEMPLATE)
9325 cp_parser_explicit_instantiation (parser);
9326 /* If the next token is `namespace', check for a named or unnamed
9327 namespace definition. */
9328 else if (token1.keyword == RID_NAMESPACE
9329 && (/* A named namespace definition. */
9330 (token2.type == CPP_NAME
9331 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9332 != CPP_EQ))
9333 /* An unnamed namespace definition. */
9334 || token2.type == CPP_OPEN_BRACE
9335 || token2.keyword == RID_ATTRIBUTE))
9336 cp_parser_namespace_definition (parser);
9337 /* An inline (associated) namespace definition. */
9338 else if (token1.keyword == RID_INLINE
9339 && token2.keyword == RID_NAMESPACE)
9340 cp_parser_namespace_definition (parser);
9341 /* Objective-C++ declaration/definition. */
9342 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9343 cp_parser_objc_declaration (parser, NULL_TREE);
9344 else if (c_dialect_objc ()
9345 && token1.keyword == RID_ATTRIBUTE
9346 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9347 cp_parser_objc_declaration (parser, attributes);
9348 /* We must have either a block declaration or a function
9349 definition. */
9350 else
9351 /* Try to parse a block-declaration, or a function-definition. */
9352 cp_parser_block_declaration (parser, /*statement_p=*/false);
9353
9354 /* Free any declarators allocated. */
9355 obstack_free (&declarator_obstack, p);
9356 }
9357
9358 /* Parse a block-declaration.
9359
9360 block-declaration:
9361 simple-declaration
9362 asm-definition
9363 namespace-alias-definition
9364 using-declaration
9365 using-directive
9366
9367 GNU Extension:
9368
9369 block-declaration:
9370 __extension__ block-declaration
9371
9372 C++0x Extension:
9373
9374 block-declaration:
9375 static_assert-declaration
9376
9377 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9378 part of a declaration-statement. */
9379
9380 static void
9381 cp_parser_block_declaration (cp_parser *parser,
9382 bool statement_p)
9383 {
9384 cp_token *token1;
9385 int saved_pedantic;
9386
9387 /* Check for the `__extension__' keyword. */
9388 if (cp_parser_extension_opt (parser, &saved_pedantic))
9389 {
9390 /* Parse the qualified declaration. */
9391 cp_parser_block_declaration (parser, statement_p);
9392 /* Restore the PEDANTIC flag. */
9393 pedantic = saved_pedantic;
9394
9395 return;
9396 }
9397
9398 /* Peek at the next token to figure out which kind of declaration is
9399 present. */
9400 token1 = cp_lexer_peek_token (parser->lexer);
9401
9402 /* If the next keyword is `asm', we have an asm-definition. */
9403 if (token1->keyword == RID_ASM)
9404 {
9405 if (statement_p)
9406 cp_parser_commit_to_tentative_parse (parser);
9407 cp_parser_asm_definition (parser);
9408 }
9409 /* If the next keyword is `namespace', we have a
9410 namespace-alias-definition. */
9411 else if (token1->keyword == RID_NAMESPACE)
9412 cp_parser_namespace_alias_definition (parser);
9413 /* If the next keyword is `using', we have either a
9414 using-declaration or a using-directive. */
9415 else if (token1->keyword == RID_USING)
9416 {
9417 cp_token *token2;
9418
9419 if (statement_p)
9420 cp_parser_commit_to_tentative_parse (parser);
9421 /* If the token after `using' is `namespace', then we have a
9422 using-directive. */
9423 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9424 if (token2->keyword == RID_NAMESPACE)
9425 cp_parser_using_directive (parser);
9426 /* Otherwise, it's a using-declaration. */
9427 else
9428 cp_parser_using_declaration (parser,
9429 /*access_declaration_p=*/false);
9430 }
9431 /* If the next keyword is `__label__' we have a misplaced label
9432 declaration. */
9433 else if (token1->keyword == RID_LABEL)
9434 {
9435 cp_lexer_consume_token (parser->lexer);
9436 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9437 cp_parser_skip_to_end_of_statement (parser);
9438 /* If the next token is now a `;', consume it. */
9439 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9440 cp_lexer_consume_token (parser->lexer);
9441 }
9442 /* If the next token is `static_assert' we have a static assertion. */
9443 else if (token1->keyword == RID_STATIC_ASSERT)
9444 cp_parser_static_assert (parser, /*member_p=*/false);
9445 /* Anything else must be a simple-declaration. */
9446 else
9447 cp_parser_simple_declaration (parser, !statement_p,
9448 /*maybe_range_for_decl*/NULL);
9449 }
9450
9451 /* Parse a simple-declaration.
9452
9453 simple-declaration:
9454 decl-specifier-seq [opt] init-declarator-list [opt] ;
9455
9456 init-declarator-list:
9457 init-declarator
9458 init-declarator-list , init-declarator
9459
9460 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9461 function-definition as a simple-declaration.
9462
9463 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9464 parsed declaration if it is an uninitialized single declarator not followed
9465 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9466 if present, will not be consumed. */
9467
9468 static void
9469 cp_parser_simple_declaration (cp_parser* parser,
9470 bool function_definition_allowed_p,
9471 tree *maybe_range_for_decl)
9472 {
9473 cp_decl_specifier_seq decl_specifiers;
9474 int declares_class_or_enum;
9475 bool saw_declarator;
9476
9477 if (maybe_range_for_decl)
9478 *maybe_range_for_decl = NULL_TREE;
9479
9480 /* Defer access checks until we know what is being declared; the
9481 checks for names appearing in the decl-specifier-seq should be
9482 done as if we were in the scope of the thing being declared. */
9483 push_deferring_access_checks (dk_deferred);
9484
9485 /* Parse the decl-specifier-seq. We have to keep track of whether
9486 or not the decl-specifier-seq declares a named class or
9487 enumeration type, since that is the only case in which the
9488 init-declarator-list is allowed to be empty.
9489
9490 [dcl.dcl]
9491
9492 In a simple-declaration, the optional init-declarator-list can be
9493 omitted only when declaring a class or enumeration, that is when
9494 the decl-specifier-seq contains either a class-specifier, an
9495 elaborated-type-specifier, or an enum-specifier. */
9496 cp_parser_decl_specifier_seq (parser,
9497 CP_PARSER_FLAGS_OPTIONAL,
9498 &decl_specifiers,
9499 &declares_class_or_enum);
9500 /* We no longer need to defer access checks. */
9501 stop_deferring_access_checks ();
9502
9503 /* In a block scope, a valid declaration must always have a
9504 decl-specifier-seq. By not trying to parse declarators, we can
9505 resolve the declaration/expression ambiguity more quickly. */
9506 if (!function_definition_allowed_p
9507 && !decl_specifiers.any_specifiers_p)
9508 {
9509 cp_parser_error (parser, "expected declaration");
9510 goto done;
9511 }
9512
9513 /* If the next two tokens are both identifiers, the code is
9514 erroneous. The usual cause of this situation is code like:
9515
9516 T t;
9517
9518 where "T" should name a type -- but does not. */
9519 if (!decl_specifiers.any_type_specifiers_p
9520 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9521 {
9522 /* If parsing tentatively, we should commit; we really are
9523 looking at a declaration. */
9524 cp_parser_commit_to_tentative_parse (parser);
9525 /* Give up. */
9526 goto done;
9527 }
9528
9529 /* If we have seen at least one decl-specifier, and the next token
9530 is not a parenthesis, then we must be looking at a declaration.
9531 (After "int (" we might be looking at a functional cast.) */
9532 if (decl_specifiers.any_specifiers_p
9533 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9534 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9535 && !cp_parser_error_occurred (parser))
9536 cp_parser_commit_to_tentative_parse (parser);
9537
9538 /* Keep going until we hit the `;' at the end of the simple
9539 declaration. */
9540 saw_declarator = false;
9541 while (cp_lexer_next_token_is_not (parser->lexer,
9542 CPP_SEMICOLON))
9543 {
9544 cp_token *token;
9545 bool function_definition_p;
9546 tree decl;
9547
9548 if (saw_declarator)
9549 {
9550 /* If we are processing next declarator, coma is expected */
9551 token = cp_lexer_peek_token (parser->lexer);
9552 gcc_assert (token->type == CPP_COMMA);
9553 cp_lexer_consume_token (parser->lexer);
9554 if (maybe_range_for_decl)
9555 *maybe_range_for_decl = error_mark_node;
9556 }
9557 else
9558 saw_declarator = true;
9559
9560 /* Parse the init-declarator. */
9561 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9562 /*checks=*/NULL,
9563 function_definition_allowed_p,
9564 /*member_p=*/false,
9565 declares_class_or_enum,
9566 &function_definition_p,
9567 maybe_range_for_decl);
9568 /* If an error occurred while parsing tentatively, exit quickly.
9569 (That usually happens when in the body of a function; each
9570 statement is treated as a declaration-statement until proven
9571 otherwise.) */
9572 if (cp_parser_error_occurred (parser))
9573 goto done;
9574 /* Handle function definitions specially. */
9575 if (function_definition_p)
9576 {
9577 /* If the next token is a `,', then we are probably
9578 processing something like:
9579
9580 void f() {}, *p;
9581
9582 which is erroneous. */
9583 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9584 {
9585 cp_token *token = cp_lexer_peek_token (parser->lexer);
9586 error_at (token->location,
9587 "mixing"
9588 " declarations and function-definitions is forbidden");
9589 }
9590 /* Otherwise, we're done with the list of declarators. */
9591 else
9592 {
9593 pop_deferring_access_checks ();
9594 return;
9595 }
9596 }
9597 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9598 *maybe_range_for_decl = decl;
9599 /* The next token should be either a `,' or a `;'. */
9600 token = cp_lexer_peek_token (parser->lexer);
9601 /* If it's a `,', there are more declarators to come. */
9602 if (token->type == CPP_COMMA)
9603 /* will be consumed next time around */;
9604 /* If it's a `;', we are done. */
9605 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9606 break;
9607 /* Anything else is an error. */
9608 else
9609 {
9610 /* If we have already issued an error message we don't need
9611 to issue another one. */
9612 if (decl != error_mark_node
9613 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9614 cp_parser_error (parser, "expected %<,%> or %<;%>");
9615 /* Skip tokens until we reach the end of the statement. */
9616 cp_parser_skip_to_end_of_statement (parser);
9617 /* If the next token is now a `;', consume it. */
9618 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9619 cp_lexer_consume_token (parser->lexer);
9620 goto done;
9621 }
9622 /* After the first time around, a function-definition is not
9623 allowed -- even if it was OK at first. For example:
9624
9625 int i, f() {}
9626
9627 is not valid. */
9628 function_definition_allowed_p = false;
9629 }
9630
9631 /* Issue an error message if no declarators are present, and the
9632 decl-specifier-seq does not itself declare a class or
9633 enumeration. */
9634 if (!saw_declarator)
9635 {
9636 if (cp_parser_declares_only_class_p (parser))
9637 shadow_tag (&decl_specifiers);
9638 /* Perform any deferred access checks. */
9639 perform_deferred_access_checks ();
9640 }
9641
9642 /* Consume the `;'. */
9643 if (!maybe_range_for_decl)
9644 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9645
9646 done:
9647 pop_deferring_access_checks ();
9648 }
9649
9650 /* Parse a decl-specifier-seq.
9651
9652 decl-specifier-seq:
9653 decl-specifier-seq [opt] decl-specifier
9654
9655 decl-specifier:
9656 storage-class-specifier
9657 type-specifier
9658 function-specifier
9659 friend
9660 typedef
9661
9662 GNU Extension:
9663
9664 decl-specifier:
9665 attributes
9666
9667 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9668
9669 The parser flags FLAGS is used to control type-specifier parsing.
9670
9671 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9672 flags:
9673
9674 1: one of the decl-specifiers is an elaborated-type-specifier
9675 (i.e., a type declaration)
9676 2: one of the decl-specifiers is an enum-specifier or a
9677 class-specifier (i.e., a type definition)
9678
9679 */
9680
9681 static void
9682 cp_parser_decl_specifier_seq (cp_parser* parser,
9683 cp_parser_flags flags,
9684 cp_decl_specifier_seq *decl_specs,
9685 int* declares_class_or_enum)
9686 {
9687 bool constructor_possible_p = !parser->in_declarator_p;
9688 cp_token *start_token = NULL;
9689
9690 /* Clear DECL_SPECS. */
9691 clear_decl_specs (decl_specs);
9692
9693 /* Assume no class or enumeration type is declared. */
9694 *declares_class_or_enum = 0;
9695
9696 /* Keep reading specifiers until there are no more to read. */
9697 while (true)
9698 {
9699 bool constructor_p;
9700 bool found_decl_spec;
9701 cp_token *token;
9702
9703 /* Peek at the next token. */
9704 token = cp_lexer_peek_token (parser->lexer);
9705
9706 /* Save the first token of the decl spec list for error
9707 reporting. */
9708 if (!start_token)
9709 start_token = token;
9710 /* Handle attributes. */
9711 if (token->keyword == RID_ATTRIBUTE)
9712 {
9713 /* Parse the attributes. */
9714 decl_specs->attributes
9715 = chainon (decl_specs->attributes,
9716 cp_parser_attributes_opt (parser));
9717 continue;
9718 }
9719 /* Assume we will find a decl-specifier keyword. */
9720 found_decl_spec = true;
9721 /* If the next token is an appropriate keyword, we can simply
9722 add it to the list. */
9723 switch (token->keyword)
9724 {
9725 /* decl-specifier:
9726 friend
9727 constexpr */
9728 case RID_FRIEND:
9729 if (!at_class_scope_p ())
9730 {
9731 error_at (token->location, "%<friend%> used outside of class");
9732 cp_lexer_purge_token (parser->lexer);
9733 }
9734 else
9735 {
9736 ++decl_specs->specs[(int) ds_friend];
9737 /* Consume the token. */
9738 cp_lexer_consume_token (parser->lexer);
9739 }
9740 break;
9741
9742 case RID_CONSTEXPR:
9743 ++decl_specs->specs[(int) ds_constexpr];
9744 cp_lexer_consume_token (parser->lexer);
9745 break;
9746
9747 /* function-specifier:
9748 inline
9749 virtual
9750 explicit */
9751 case RID_INLINE:
9752 case RID_VIRTUAL:
9753 case RID_EXPLICIT:
9754 cp_parser_function_specifier_opt (parser, decl_specs);
9755 break;
9756
9757 /* decl-specifier:
9758 typedef */
9759 case RID_TYPEDEF:
9760 ++decl_specs->specs[(int) ds_typedef];
9761 /* Consume the token. */
9762 cp_lexer_consume_token (parser->lexer);
9763 /* A constructor declarator cannot appear in a typedef. */
9764 constructor_possible_p = false;
9765 /* The "typedef" keyword can only occur in a declaration; we
9766 may as well commit at this point. */
9767 cp_parser_commit_to_tentative_parse (parser);
9768
9769 if (decl_specs->storage_class != sc_none)
9770 decl_specs->conflicting_specifiers_p = true;
9771 break;
9772
9773 /* storage-class-specifier:
9774 auto
9775 register
9776 static
9777 extern
9778 mutable
9779
9780 GNU Extension:
9781 thread */
9782 case RID_AUTO:
9783 if (cxx_dialect == cxx98)
9784 {
9785 /* Consume the token. */
9786 cp_lexer_consume_token (parser->lexer);
9787
9788 /* Complain about `auto' as a storage specifier, if
9789 we're complaining about C++0x compatibility. */
9790 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9791 " will change meaning in C++0x; please remove it");
9792
9793 /* Set the storage class anyway. */
9794 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9795 token->location);
9796 }
9797 else
9798 /* C++0x auto type-specifier. */
9799 found_decl_spec = false;
9800 break;
9801
9802 case RID_REGISTER:
9803 case RID_STATIC:
9804 case RID_EXTERN:
9805 case RID_MUTABLE:
9806 /* Consume the token. */
9807 cp_lexer_consume_token (parser->lexer);
9808 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9809 token->location);
9810 break;
9811 case RID_THREAD:
9812 /* Consume the token. */
9813 cp_lexer_consume_token (parser->lexer);
9814 ++decl_specs->specs[(int) ds_thread];
9815 break;
9816
9817 default:
9818 /* We did not yet find a decl-specifier yet. */
9819 found_decl_spec = false;
9820 break;
9821 }
9822
9823 if (found_decl_spec
9824 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9825 && token->keyword != RID_CONSTEXPR)
9826 error ("decl-specifier invalid in condition");
9827
9828 /* Constructors are a special case. The `S' in `S()' is not a
9829 decl-specifier; it is the beginning of the declarator. */
9830 constructor_p
9831 = (!found_decl_spec
9832 && constructor_possible_p
9833 && (cp_parser_constructor_declarator_p
9834 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9835
9836 /* If we don't have a DECL_SPEC yet, then we must be looking at
9837 a type-specifier. */
9838 if (!found_decl_spec && !constructor_p)
9839 {
9840 int decl_spec_declares_class_or_enum;
9841 bool is_cv_qualifier;
9842 tree type_spec;
9843
9844 type_spec
9845 = cp_parser_type_specifier (parser, flags,
9846 decl_specs,
9847 /*is_declaration=*/true,
9848 &decl_spec_declares_class_or_enum,
9849 &is_cv_qualifier);
9850 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9851
9852 /* If this type-specifier referenced a user-defined type
9853 (a typedef, class-name, etc.), then we can't allow any
9854 more such type-specifiers henceforth.
9855
9856 [dcl.spec]
9857
9858 The longest sequence of decl-specifiers that could
9859 possibly be a type name is taken as the
9860 decl-specifier-seq of a declaration. The sequence shall
9861 be self-consistent as described below.
9862
9863 [dcl.type]
9864
9865 As a general rule, at most one type-specifier is allowed
9866 in the complete decl-specifier-seq of a declaration. The
9867 only exceptions are the following:
9868
9869 -- const or volatile can be combined with any other
9870 type-specifier.
9871
9872 -- signed or unsigned can be combined with char, long,
9873 short, or int.
9874
9875 -- ..
9876
9877 Example:
9878
9879 typedef char* Pc;
9880 void g (const int Pc);
9881
9882 Here, Pc is *not* part of the decl-specifier seq; it's
9883 the declarator. Therefore, once we see a type-specifier
9884 (other than a cv-qualifier), we forbid any additional
9885 user-defined types. We *do* still allow things like `int
9886 int' to be considered a decl-specifier-seq, and issue the
9887 error message later. */
9888 if (type_spec && !is_cv_qualifier)
9889 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9890 /* A constructor declarator cannot follow a type-specifier. */
9891 if (type_spec)
9892 {
9893 constructor_possible_p = false;
9894 found_decl_spec = true;
9895 if (!is_cv_qualifier)
9896 decl_specs->any_type_specifiers_p = true;
9897 }
9898 }
9899
9900 /* If we still do not have a DECL_SPEC, then there are no more
9901 decl-specifiers. */
9902 if (!found_decl_spec)
9903 break;
9904
9905 decl_specs->any_specifiers_p = true;
9906 /* After we see one decl-specifier, further decl-specifiers are
9907 always optional. */
9908 flags |= CP_PARSER_FLAGS_OPTIONAL;
9909 }
9910
9911 cp_parser_check_decl_spec (decl_specs, start_token->location);
9912
9913 /* Don't allow a friend specifier with a class definition. */
9914 if (decl_specs->specs[(int) ds_friend] != 0
9915 && (*declares_class_or_enum & 2))
9916 error_at (start_token->location,
9917 "class definition may not be declared a friend");
9918 }
9919
9920 /* Parse an (optional) storage-class-specifier.
9921
9922 storage-class-specifier:
9923 auto
9924 register
9925 static
9926 extern
9927 mutable
9928
9929 GNU Extension:
9930
9931 storage-class-specifier:
9932 thread
9933
9934 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9935
9936 static tree
9937 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9938 {
9939 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9940 {
9941 case RID_AUTO:
9942 if (cxx_dialect != cxx98)
9943 return NULL_TREE;
9944 /* Fall through for C++98. */
9945
9946 case RID_REGISTER:
9947 case RID_STATIC:
9948 case RID_EXTERN:
9949 case RID_MUTABLE:
9950 case RID_THREAD:
9951 /* Consume the token. */
9952 return cp_lexer_consume_token (parser->lexer)->u.value;
9953
9954 default:
9955 return NULL_TREE;
9956 }
9957 }
9958
9959 /* Parse an (optional) function-specifier.
9960
9961 function-specifier:
9962 inline
9963 virtual
9964 explicit
9965
9966 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9967 Updates DECL_SPECS, if it is non-NULL. */
9968
9969 static tree
9970 cp_parser_function_specifier_opt (cp_parser* parser,
9971 cp_decl_specifier_seq *decl_specs)
9972 {
9973 cp_token *token = cp_lexer_peek_token (parser->lexer);
9974 switch (token->keyword)
9975 {
9976 case RID_INLINE:
9977 if (decl_specs)
9978 ++decl_specs->specs[(int) ds_inline];
9979 break;
9980
9981 case RID_VIRTUAL:
9982 /* 14.5.2.3 [temp.mem]
9983
9984 A member function template shall not be virtual. */
9985 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9986 error_at (token->location, "templates may not be %<virtual%>");
9987 else if (decl_specs)
9988 ++decl_specs->specs[(int) ds_virtual];
9989 break;
9990
9991 case RID_EXPLICIT:
9992 if (decl_specs)
9993 ++decl_specs->specs[(int) ds_explicit];
9994 break;
9995
9996 default:
9997 return NULL_TREE;
9998 }
9999
10000 /* Consume the token. */
10001 return cp_lexer_consume_token (parser->lexer)->u.value;
10002 }
10003
10004 /* Parse a linkage-specification.
10005
10006 linkage-specification:
10007 extern string-literal { declaration-seq [opt] }
10008 extern string-literal declaration */
10009
10010 static void
10011 cp_parser_linkage_specification (cp_parser* parser)
10012 {
10013 tree linkage;
10014
10015 /* Look for the `extern' keyword. */
10016 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10017
10018 /* Look for the string-literal. */
10019 linkage = cp_parser_string_literal (parser, false, false);
10020
10021 /* Transform the literal into an identifier. If the literal is a
10022 wide-character string, or contains embedded NULs, then we can't
10023 handle it as the user wants. */
10024 if (strlen (TREE_STRING_POINTER (linkage))
10025 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10026 {
10027 cp_parser_error (parser, "invalid linkage-specification");
10028 /* Assume C++ linkage. */
10029 linkage = lang_name_cplusplus;
10030 }
10031 else
10032 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10033
10034 /* We're now using the new linkage. */
10035 push_lang_context (linkage);
10036
10037 /* If the next token is a `{', then we're using the first
10038 production. */
10039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10040 {
10041 /* Consume the `{' token. */
10042 cp_lexer_consume_token (parser->lexer);
10043 /* Parse the declarations. */
10044 cp_parser_declaration_seq_opt (parser);
10045 /* Look for the closing `}'. */
10046 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10047 }
10048 /* Otherwise, there's just one declaration. */
10049 else
10050 {
10051 bool saved_in_unbraced_linkage_specification_p;
10052
10053 saved_in_unbraced_linkage_specification_p
10054 = parser->in_unbraced_linkage_specification_p;
10055 parser->in_unbraced_linkage_specification_p = true;
10056 cp_parser_declaration (parser);
10057 parser->in_unbraced_linkage_specification_p
10058 = saved_in_unbraced_linkage_specification_p;
10059 }
10060
10061 /* We're done with the linkage-specification. */
10062 pop_lang_context ();
10063 }
10064
10065 /* Parse a static_assert-declaration.
10066
10067 static_assert-declaration:
10068 static_assert ( constant-expression , string-literal ) ;
10069
10070 If MEMBER_P, this static_assert is a class member. */
10071
10072 static void
10073 cp_parser_static_assert(cp_parser *parser, bool member_p)
10074 {
10075 tree condition;
10076 tree message;
10077 cp_token *token;
10078 location_t saved_loc;
10079 bool dummy;
10080
10081 /* Peek at the `static_assert' token so we can keep track of exactly
10082 where the static assertion started. */
10083 token = cp_lexer_peek_token (parser->lexer);
10084 saved_loc = token->location;
10085
10086 /* Look for the `static_assert' keyword. */
10087 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10088 RT_STATIC_ASSERT))
10089 return;
10090
10091 /* We know we are in a static assertion; commit to any tentative
10092 parse. */
10093 if (cp_parser_parsing_tentatively (parser))
10094 cp_parser_commit_to_tentative_parse (parser);
10095
10096 /* Parse the `(' starting the static assertion condition. */
10097 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10098
10099 /* Parse the constant-expression. Allow a non-constant expression
10100 here in order to give better diagnostics in finish_static_assert. */
10101 condition =
10102 cp_parser_constant_expression (parser,
10103 /*allow_non_constant_p=*/true,
10104 /*non_constant_p=*/&dummy);
10105
10106 /* Parse the separating `,'. */
10107 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10108
10109 /* Parse the string-literal message. */
10110 message = cp_parser_string_literal (parser,
10111 /*translate=*/false,
10112 /*wide_ok=*/true);
10113
10114 /* A `)' completes the static assertion. */
10115 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10116 cp_parser_skip_to_closing_parenthesis (parser,
10117 /*recovering=*/true,
10118 /*or_comma=*/false,
10119 /*consume_paren=*/true);
10120
10121 /* A semicolon terminates the declaration. */
10122 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10123
10124 /* Complete the static assertion, which may mean either processing
10125 the static assert now or saving it for template instantiation. */
10126 finish_static_assert (condition, message, saved_loc, member_p);
10127 }
10128
10129 /* Parse a `decltype' type. Returns the type.
10130
10131 simple-type-specifier:
10132 decltype ( expression ) */
10133
10134 static tree
10135 cp_parser_decltype (cp_parser *parser)
10136 {
10137 tree expr;
10138 bool id_expression_or_member_access_p = false;
10139 const char *saved_message;
10140 bool saved_integral_constant_expression_p;
10141 bool saved_non_integral_constant_expression_p;
10142 cp_token *id_expr_start_token;
10143
10144 /* Look for the `decltype' token. */
10145 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10146 return error_mark_node;
10147
10148 /* Types cannot be defined in a `decltype' expression. Save away the
10149 old message. */
10150 saved_message = parser->type_definition_forbidden_message;
10151
10152 /* And create the new one. */
10153 parser->type_definition_forbidden_message
10154 = G_("types may not be defined in %<decltype%> expressions");
10155
10156 /* The restrictions on constant-expressions do not apply inside
10157 decltype expressions. */
10158 saved_integral_constant_expression_p
10159 = parser->integral_constant_expression_p;
10160 saved_non_integral_constant_expression_p
10161 = parser->non_integral_constant_expression_p;
10162 parser->integral_constant_expression_p = false;
10163
10164 /* Do not actually evaluate the expression. */
10165 ++cp_unevaluated_operand;
10166
10167 /* Do not warn about problems with the expression. */
10168 ++c_inhibit_evaluation_warnings;
10169
10170 /* Parse the opening `('. */
10171 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10172 return error_mark_node;
10173
10174 /* First, try parsing an id-expression. */
10175 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10176 cp_parser_parse_tentatively (parser);
10177 expr = cp_parser_id_expression (parser,
10178 /*template_keyword_p=*/false,
10179 /*check_dependency_p=*/true,
10180 /*template_p=*/NULL,
10181 /*declarator_p=*/false,
10182 /*optional_p=*/false);
10183
10184 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10185 {
10186 bool non_integral_constant_expression_p = false;
10187 tree id_expression = expr;
10188 cp_id_kind idk;
10189 const char *error_msg;
10190
10191 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10192 /* Lookup the name we got back from the id-expression. */
10193 expr = cp_parser_lookup_name (parser, expr,
10194 none_type,
10195 /*is_template=*/false,
10196 /*is_namespace=*/false,
10197 /*check_dependency=*/true,
10198 /*ambiguous_decls=*/NULL,
10199 id_expr_start_token->location);
10200
10201 if (expr
10202 && expr != error_mark_node
10203 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10204 && TREE_CODE (expr) != TYPE_DECL
10205 && (TREE_CODE (expr) != BIT_NOT_EXPR
10206 || !TYPE_P (TREE_OPERAND (expr, 0)))
10207 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10208 {
10209 /* Complete lookup of the id-expression. */
10210 expr = (finish_id_expression
10211 (id_expression, expr, parser->scope, &idk,
10212 /*integral_constant_expression_p=*/false,
10213 /*allow_non_integral_constant_expression_p=*/true,
10214 &non_integral_constant_expression_p,
10215 /*template_p=*/false,
10216 /*done=*/true,
10217 /*address_p=*/false,
10218 /*template_arg_p=*/false,
10219 &error_msg,
10220 id_expr_start_token->location));
10221
10222 if (expr == error_mark_node)
10223 /* We found an id-expression, but it was something that we
10224 should not have found. This is an error, not something
10225 we can recover from, so note that we found an
10226 id-expression and we'll recover as gracefully as
10227 possible. */
10228 id_expression_or_member_access_p = true;
10229 }
10230
10231 if (expr
10232 && expr != error_mark_node
10233 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10234 /* We have an id-expression. */
10235 id_expression_or_member_access_p = true;
10236 }
10237
10238 if (!id_expression_or_member_access_p)
10239 {
10240 /* Abort the id-expression parse. */
10241 cp_parser_abort_tentative_parse (parser);
10242
10243 /* Parsing tentatively, again. */
10244 cp_parser_parse_tentatively (parser);
10245
10246 /* Parse a class member access. */
10247 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10248 /*cast_p=*/false,
10249 /*member_access_only_p=*/true, NULL);
10250
10251 if (expr
10252 && expr != error_mark_node
10253 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10254 /* We have an id-expression. */
10255 id_expression_or_member_access_p = true;
10256 }
10257
10258 if (id_expression_or_member_access_p)
10259 /* We have parsed the complete id-expression or member access. */
10260 cp_parser_parse_definitely (parser);
10261 else
10262 {
10263 bool saved_greater_than_is_operator_p;
10264
10265 /* Abort our attempt to parse an id-expression or member access
10266 expression. */
10267 cp_parser_abort_tentative_parse (parser);
10268
10269 /* Within a parenthesized expression, a `>' token is always
10270 the greater-than operator. */
10271 saved_greater_than_is_operator_p
10272 = parser->greater_than_is_operator_p;
10273 parser->greater_than_is_operator_p = true;
10274
10275 /* Parse a full expression. */
10276 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10277
10278 /* The `>' token might be the end of a template-id or
10279 template-parameter-list now. */
10280 parser->greater_than_is_operator_p
10281 = saved_greater_than_is_operator_p;
10282 }
10283
10284 /* Go back to evaluating expressions. */
10285 --cp_unevaluated_operand;
10286 --c_inhibit_evaluation_warnings;
10287
10288 /* Restore the old message and the integral constant expression
10289 flags. */
10290 parser->type_definition_forbidden_message = saved_message;
10291 parser->integral_constant_expression_p
10292 = saved_integral_constant_expression_p;
10293 parser->non_integral_constant_expression_p
10294 = saved_non_integral_constant_expression_p;
10295
10296 if (expr == error_mark_node)
10297 {
10298 /* Skip everything up to the closing `)'. */
10299 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10300 /*consume_paren=*/true);
10301 return error_mark_node;
10302 }
10303
10304 /* Parse to the closing `)'. */
10305 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10306 {
10307 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10308 /*consume_paren=*/true);
10309 return error_mark_node;
10310 }
10311
10312 return finish_decltype_type (expr, id_expression_or_member_access_p,
10313 tf_warning_or_error);
10314 }
10315
10316 /* Special member functions [gram.special] */
10317
10318 /* Parse a conversion-function-id.
10319
10320 conversion-function-id:
10321 operator conversion-type-id
10322
10323 Returns an IDENTIFIER_NODE representing the operator. */
10324
10325 static tree
10326 cp_parser_conversion_function_id (cp_parser* parser)
10327 {
10328 tree type;
10329 tree saved_scope;
10330 tree saved_qualifying_scope;
10331 tree saved_object_scope;
10332 tree pushed_scope = NULL_TREE;
10333
10334 /* Look for the `operator' token. */
10335 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10336 return error_mark_node;
10337 /* When we parse the conversion-type-id, the current scope will be
10338 reset. However, we need that information in able to look up the
10339 conversion function later, so we save it here. */
10340 saved_scope = parser->scope;
10341 saved_qualifying_scope = parser->qualifying_scope;
10342 saved_object_scope = parser->object_scope;
10343 /* We must enter the scope of the class so that the names of
10344 entities declared within the class are available in the
10345 conversion-type-id. For example, consider:
10346
10347 struct S {
10348 typedef int I;
10349 operator I();
10350 };
10351
10352 S::operator I() { ... }
10353
10354 In order to see that `I' is a type-name in the definition, we
10355 must be in the scope of `S'. */
10356 if (saved_scope)
10357 pushed_scope = push_scope (saved_scope);
10358 /* Parse the conversion-type-id. */
10359 type = cp_parser_conversion_type_id (parser);
10360 /* Leave the scope of the class, if any. */
10361 if (pushed_scope)
10362 pop_scope (pushed_scope);
10363 /* Restore the saved scope. */
10364 parser->scope = saved_scope;
10365 parser->qualifying_scope = saved_qualifying_scope;
10366 parser->object_scope = saved_object_scope;
10367 /* If the TYPE is invalid, indicate failure. */
10368 if (type == error_mark_node)
10369 return error_mark_node;
10370 return mangle_conv_op_name_for_type (type);
10371 }
10372
10373 /* Parse a conversion-type-id:
10374
10375 conversion-type-id:
10376 type-specifier-seq conversion-declarator [opt]
10377
10378 Returns the TYPE specified. */
10379
10380 static tree
10381 cp_parser_conversion_type_id (cp_parser* parser)
10382 {
10383 tree attributes;
10384 cp_decl_specifier_seq type_specifiers;
10385 cp_declarator *declarator;
10386 tree type_specified;
10387
10388 /* Parse the attributes. */
10389 attributes = cp_parser_attributes_opt (parser);
10390 /* Parse the type-specifiers. */
10391 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10392 /*is_trailing_return=*/false,
10393 &type_specifiers);
10394 /* If that didn't work, stop. */
10395 if (type_specifiers.type == error_mark_node)
10396 return error_mark_node;
10397 /* Parse the conversion-declarator. */
10398 declarator = cp_parser_conversion_declarator_opt (parser);
10399
10400 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10401 /*initialized=*/0, &attributes);
10402 if (attributes)
10403 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10404
10405 /* Don't give this error when parsing tentatively. This happens to
10406 work because we always parse this definitively once. */
10407 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10408 && type_uses_auto (type_specified))
10409 {
10410 error ("invalid use of %<auto%> in conversion operator");
10411 return error_mark_node;
10412 }
10413
10414 return type_specified;
10415 }
10416
10417 /* Parse an (optional) conversion-declarator.
10418
10419 conversion-declarator:
10420 ptr-operator conversion-declarator [opt]
10421
10422 */
10423
10424 static cp_declarator *
10425 cp_parser_conversion_declarator_opt (cp_parser* parser)
10426 {
10427 enum tree_code code;
10428 tree class_type;
10429 cp_cv_quals cv_quals;
10430
10431 /* We don't know if there's a ptr-operator next, or not. */
10432 cp_parser_parse_tentatively (parser);
10433 /* Try the ptr-operator. */
10434 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10435 /* If it worked, look for more conversion-declarators. */
10436 if (cp_parser_parse_definitely (parser))
10437 {
10438 cp_declarator *declarator;
10439
10440 /* Parse another optional declarator. */
10441 declarator = cp_parser_conversion_declarator_opt (parser);
10442
10443 return cp_parser_make_indirect_declarator
10444 (code, class_type, cv_quals, declarator);
10445 }
10446
10447 return NULL;
10448 }
10449
10450 /* Parse an (optional) ctor-initializer.
10451
10452 ctor-initializer:
10453 : mem-initializer-list
10454
10455 Returns TRUE iff the ctor-initializer was actually present. */
10456
10457 static bool
10458 cp_parser_ctor_initializer_opt (cp_parser* parser)
10459 {
10460 /* If the next token is not a `:', then there is no
10461 ctor-initializer. */
10462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10463 {
10464 /* Do default initialization of any bases and members. */
10465 if (DECL_CONSTRUCTOR_P (current_function_decl))
10466 finish_mem_initializers (NULL_TREE);
10467
10468 return false;
10469 }
10470
10471 /* Consume the `:' token. */
10472 cp_lexer_consume_token (parser->lexer);
10473 /* And the mem-initializer-list. */
10474 cp_parser_mem_initializer_list (parser);
10475
10476 return true;
10477 }
10478
10479 /* Parse a mem-initializer-list.
10480
10481 mem-initializer-list:
10482 mem-initializer ... [opt]
10483 mem-initializer ... [opt] , mem-initializer-list */
10484
10485 static void
10486 cp_parser_mem_initializer_list (cp_parser* parser)
10487 {
10488 tree mem_initializer_list = NULL_TREE;
10489 cp_token *token = cp_lexer_peek_token (parser->lexer);
10490
10491 /* Let the semantic analysis code know that we are starting the
10492 mem-initializer-list. */
10493 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10494 error_at (token->location,
10495 "only constructors take member initializers");
10496
10497 /* Loop through the list. */
10498 while (true)
10499 {
10500 tree mem_initializer;
10501
10502 token = cp_lexer_peek_token (parser->lexer);
10503 /* Parse the mem-initializer. */
10504 mem_initializer = cp_parser_mem_initializer (parser);
10505 /* If the next token is a `...', we're expanding member initializers. */
10506 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10507 {
10508 /* Consume the `...'. */
10509 cp_lexer_consume_token (parser->lexer);
10510
10511 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10512 can be expanded but members cannot. */
10513 if (mem_initializer != error_mark_node
10514 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10515 {
10516 error_at (token->location,
10517 "cannot expand initializer for member %<%D%>",
10518 TREE_PURPOSE (mem_initializer));
10519 mem_initializer = error_mark_node;
10520 }
10521
10522 /* Construct the pack expansion type. */
10523 if (mem_initializer != error_mark_node)
10524 mem_initializer = make_pack_expansion (mem_initializer);
10525 }
10526 /* Add it to the list, unless it was erroneous. */
10527 if (mem_initializer != error_mark_node)
10528 {
10529 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10530 mem_initializer_list = mem_initializer;
10531 }
10532 /* If the next token is not a `,', we're done. */
10533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10534 break;
10535 /* Consume the `,' token. */
10536 cp_lexer_consume_token (parser->lexer);
10537 }
10538
10539 /* Perform semantic analysis. */
10540 if (DECL_CONSTRUCTOR_P (current_function_decl))
10541 finish_mem_initializers (mem_initializer_list);
10542 }
10543
10544 /* Parse a mem-initializer.
10545
10546 mem-initializer:
10547 mem-initializer-id ( expression-list [opt] )
10548 mem-initializer-id braced-init-list
10549
10550 GNU extension:
10551
10552 mem-initializer:
10553 ( expression-list [opt] )
10554
10555 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10556 class) or FIELD_DECL (for a non-static data member) to initialize;
10557 the TREE_VALUE is the expression-list. An empty initialization
10558 list is represented by void_list_node. */
10559
10560 static tree
10561 cp_parser_mem_initializer (cp_parser* parser)
10562 {
10563 tree mem_initializer_id;
10564 tree expression_list;
10565 tree member;
10566 cp_token *token = cp_lexer_peek_token (parser->lexer);
10567
10568 /* Find out what is being initialized. */
10569 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10570 {
10571 permerror (token->location,
10572 "anachronistic old-style base class initializer");
10573 mem_initializer_id = NULL_TREE;
10574 }
10575 else
10576 {
10577 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10578 if (mem_initializer_id == error_mark_node)
10579 return mem_initializer_id;
10580 }
10581 member = expand_member_init (mem_initializer_id);
10582 if (member && !DECL_P (member))
10583 in_base_initializer = 1;
10584
10585 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10586 {
10587 bool expr_non_constant_p;
10588 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10589 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10590 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10591 expression_list = build_tree_list (NULL_TREE, expression_list);
10592 }
10593 else
10594 {
10595 VEC(tree,gc)* vec;
10596 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10597 /*cast_p=*/false,
10598 /*allow_expansion_p=*/true,
10599 /*non_constant_p=*/NULL);
10600 if (vec == NULL)
10601 return error_mark_node;
10602 expression_list = build_tree_list_vec (vec);
10603 release_tree_vector (vec);
10604 }
10605
10606 if (expression_list == error_mark_node)
10607 return error_mark_node;
10608 if (!expression_list)
10609 expression_list = void_type_node;
10610
10611 in_base_initializer = 0;
10612
10613 return member ? build_tree_list (member, expression_list) : error_mark_node;
10614 }
10615
10616 /* Parse a mem-initializer-id.
10617
10618 mem-initializer-id:
10619 :: [opt] nested-name-specifier [opt] class-name
10620 identifier
10621
10622 Returns a TYPE indicating the class to be initializer for the first
10623 production. Returns an IDENTIFIER_NODE indicating the data member
10624 to be initialized for the second production. */
10625
10626 static tree
10627 cp_parser_mem_initializer_id (cp_parser* parser)
10628 {
10629 bool global_scope_p;
10630 bool nested_name_specifier_p;
10631 bool template_p = false;
10632 tree id;
10633
10634 cp_token *token = cp_lexer_peek_token (parser->lexer);
10635
10636 /* `typename' is not allowed in this context ([temp.res]). */
10637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10638 {
10639 error_at (token->location,
10640 "keyword %<typename%> not allowed in this context (a qualified "
10641 "member initializer is implicitly a type)");
10642 cp_lexer_consume_token (parser->lexer);
10643 }
10644 /* Look for the optional `::' operator. */
10645 global_scope_p
10646 = (cp_parser_global_scope_opt (parser,
10647 /*current_scope_valid_p=*/false)
10648 != NULL_TREE);
10649 /* Look for the optional nested-name-specifier. The simplest way to
10650 implement:
10651
10652 [temp.res]
10653
10654 The keyword `typename' is not permitted in a base-specifier or
10655 mem-initializer; in these contexts a qualified name that
10656 depends on a template-parameter is implicitly assumed to be a
10657 type name.
10658
10659 is to assume that we have seen the `typename' keyword at this
10660 point. */
10661 nested_name_specifier_p
10662 = (cp_parser_nested_name_specifier_opt (parser,
10663 /*typename_keyword_p=*/true,
10664 /*check_dependency_p=*/true,
10665 /*type_p=*/true,
10666 /*is_declaration=*/true)
10667 != NULL_TREE);
10668 if (nested_name_specifier_p)
10669 template_p = cp_parser_optional_template_keyword (parser);
10670 /* If there is a `::' operator or a nested-name-specifier, then we
10671 are definitely looking for a class-name. */
10672 if (global_scope_p || nested_name_specifier_p)
10673 return cp_parser_class_name (parser,
10674 /*typename_keyword_p=*/true,
10675 /*template_keyword_p=*/template_p,
10676 typename_type,
10677 /*check_dependency_p=*/true,
10678 /*class_head_p=*/false,
10679 /*is_declaration=*/true);
10680 /* Otherwise, we could also be looking for an ordinary identifier. */
10681 cp_parser_parse_tentatively (parser);
10682 /* Try a class-name. */
10683 id = cp_parser_class_name (parser,
10684 /*typename_keyword_p=*/true,
10685 /*template_keyword_p=*/false,
10686 none_type,
10687 /*check_dependency_p=*/true,
10688 /*class_head_p=*/false,
10689 /*is_declaration=*/true);
10690 /* If we found one, we're done. */
10691 if (cp_parser_parse_definitely (parser))
10692 return id;
10693 /* Otherwise, look for an ordinary identifier. */
10694 return cp_parser_identifier (parser);
10695 }
10696
10697 /* Overloading [gram.over] */
10698
10699 /* Parse an operator-function-id.
10700
10701 operator-function-id:
10702 operator operator
10703
10704 Returns an IDENTIFIER_NODE for the operator which is a
10705 human-readable spelling of the identifier, e.g., `operator +'. */
10706
10707 static tree
10708 cp_parser_operator_function_id (cp_parser* parser)
10709 {
10710 /* Look for the `operator' keyword. */
10711 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10712 return error_mark_node;
10713 /* And then the name of the operator itself. */
10714 return cp_parser_operator (parser);
10715 }
10716
10717 /* Parse an operator.
10718
10719 operator:
10720 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10721 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10722 || ++ -- , ->* -> () []
10723
10724 GNU Extensions:
10725
10726 operator:
10727 <? >? <?= >?=
10728
10729 Returns an IDENTIFIER_NODE for the operator which is a
10730 human-readable spelling of the identifier, e.g., `operator +'. */
10731
10732 static tree
10733 cp_parser_operator (cp_parser* parser)
10734 {
10735 tree id = NULL_TREE;
10736 cp_token *token;
10737
10738 /* Peek at the next token. */
10739 token = cp_lexer_peek_token (parser->lexer);
10740 /* Figure out which operator we have. */
10741 switch (token->type)
10742 {
10743 case CPP_KEYWORD:
10744 {
10745 enum tree_code op;
10746
10747 /* The keyword should be either `new' or `delete'. */
10748 if (token->keyword == RID_NEW)
10749 op = NEW_EXPR;
10750 else if (token->keyword == RID_DELETE)
10751 op = DELETE_EXPR;
10752 else
10753 break;
10754
10755 /* Consume the `new' or `delete' token. */
10756 cp_lexer_consume_token (parser->lexer);
10757
10758 /* Peek at the next token. */
10759 token = cp_lexer_peek_token (parser->lexer);
10760 /* If it's a `[' token then this is the array variant of the
10761 operator. */
10762 if (token->type == CPP_OPEN_SQUARE)
10763 {
10764 /* Consume the `[' token. */
10765 cp_lexer_consume_token (parser->lexer);
10766 /* Look for the `]' token. */
10767 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10768 id = ansi_opname (op == NEW_EXPR
10769 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10770 }
10771 /* Otherwise, we have the non-array variant. */
10772 else
10773 id = ansi_opname (op);
10774
10775 return id;
10776 }
10777
10778 case CPP_PLUS:
10779 id = ansi_opname (PLUS_EXPR);
10780 break;
10781
10782 case CPP_MINUS:
10783 id = ansi_opname (MINUS_EXPR);
10784 break;
10785
10786 case CPP_MULT:
10787 id = ansi_opname (MULT_EXPR);
10788 break;
10789
10790 case CPP_DIV:
10791 id = ansi_opname (TRUNC_DIV_EXPR);
10792 break;
10793
10794 case CPP_MOD:
10795 id = ansi_opname (TRUNC_MOD_EXPR);
10796 break;
10797
10798 case CPP_XOR:
10799 id = ansi_opname (BIT_XOR_EXPR);
10800 break;
10801
10802 case CPP_AND:
10803 id = ansi_opname (BIT_AND_EXPR);
10804 break;
10805
10806 case CPP_OR:
10807 id = ansi_opname (BIT_IOR_EXPR);
10808 break;
10809
10810 case CPP_COMPL:
10811 id = ansi_opname (BIT_NOT_EXPR);
10812 break;
10813
10814 case CPP_NOT:
10815 id = ansi_opname (TRUTH_NOT_EXPR);
10816 break;
10817
10818 case CPP_EQ:
10819 id = ansi_assopname (NOP_EXPR);
10820 break;
10821
10822 case CPP_LESS:
10823 id = ansi_opname (LT_EXPR);
10824 break;
10825
10826 case CPP_GREATER:
10827 id = ansi_opname (GT_EXPR);
10828 break;
10829
10830 case CPP_PLUS_EQ:
10831 id = ansi_assopname (PLUS_EXPR);
10832 break;
10833
10834 case CPP_MINUS_EQ:
10835 id = ansi_assopname (MINUS_EXPR);
10836 break;
10837
10838 case CPP_MULT_EQ:
10839 id = ansi_assopname (MULT_EXPR);
10840 break;
10841
10842 case CPP_DIV_EQ:
10843 id = ansi_assopname (TRUNC_DIV_EXPR);
10844 break;
10845
10846 case CPP_MOD_EQ:
10847 id = ansi_assopname (TRUNC_MOD_EXPR);
10848 break;
10849
10850 case CPP_XOR_EQ:
10851 id = ansi_assopname (BIT_XOR_EXPR);
10852 break;
10853
10854 case CPP_AND_EQ:
10855 id = ansi_assopname (BIT_AND_EXPR);
10856 break;
10857
10858 case CPP_OR_EQ:
10859 id = ansi_assopname (BIT_IOR_EXPR);
10860 break;
10861
10862 case CPP_LSHIFT:
10863 id = ansi_opname (LSHIFT_EXPR);
10864 break;
10865
10866 case CPP_RSHIFT:
10867 id = ansi_opname (RSHIFT_EXPR);
10868 break;
10869
10870 case CPP_LSHIFT_EQ:
10871 id = ansi_assopname (LSHIFT_EXPR);
10872 break;
10873
10874 case CPP_RSHIFT_EQ:
10875 id = ansi_assopname (RSHIFT_EXPR);
10876 break;
10877
10878 case CPP_EQ_EQ:
10879 id = ansi_opname (EQ_EXPR);
10880 break;
10881
10882 case CPP_NOT_EQ:
10883 id = ansi_opname (NE_EXPR);
10884 break;
10885
10886 case CPP_LESS_EQ:
10887 id = ansi_opname (LE_EXPR);
10888 break;
10889
10890 case CPP_GREATER_EQ:
10891 id = ansi_opname (GE_EXPR);
10892 break;
10893
10894 case CPP_AND_AND:
10895 id = ansi_opname (TRUTH_ANDIF_EXPR);
10896 break;
10897
10898 case CPP_OR_OR:
10899 id = ansi_opname (TRUTH_ORIF_EXPR);
10900 break;
10901
10902 case CPP_PLUS_PLUS:
10903 id = ansi_opname (POSTINCREMENT_EXPR);
10904 break;
10905
10906 case CPP_MINUS_MINUS:
10907 id = ansi_opname (PREDECREMENT_EXPR);
10908 break;
10909
10910 case CPP_COMMA:
10911 id = ansi_opname (COMPOUND_EXPR);
10912 break;
10913
10914 case CPP_DEREF_STAR:
10915 id = ansi_opname (MEMBER_REF);
10916 break;
10917
10918 case CPP_DEREF:
10919 id = ansi_opname (COMPONENT_REF);
10920 break;
10921
10922 case CPP_OPEN_PAREN:
10923 /* Consume the `('. */
10924 cp_lexer_consume_token (parser->lexer);
10925 /* Look for the matching `)'. */
10926 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10927 return ansi_opname (CALL_EXPR);
10928
10929 case CPP_OPEN_SQUARE:
10930 /* Consume the `['. */
10931 cp_lexer_consume_token (parser->lexer);
10932 /* Look for the matching `]'. */
10933 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10934 return ansi_opname (ARRAY_REF);
10935
10936 default:
10937 /* Anything else is an error. */
10938 break;
10939 }
10940
10941 /* If we have selected an identifier, we need to consume the
10942 operator token. */
10943 if (id)
10944 cp_lexer_consume_token (parser->lexer);
10945 /* Otherwise, no valid operator name was present. */
10946 else
10947 {
10948 cp_parser_error (parser, "expected operator");
10949 id = error_mark_node;
10950 }
10951
10952 return id;
10953 }
10954
10955 /* Parse a template-declaration.
10956
10957 template-declaration:
10958 export [opt] template < template-parameter-list > declaration
10959
10960 If MEMBER_P is TRUE, this template-declaration occurs within a
10961 class-specifier.
10962
10963 The grammar rule given by the standard isn't correct. What
10964 is really meant is:
10965
10966 template-declaration:
10967 export [opt] template-parameter-list-seq
10968 decl-specifier-seq [opt] init-declarator [opt] ;
10969 export [opt] template-parameter-list-seq
10970 function-definition
10971
10972 template-parameter-list-seq:
10973 template-parameter-list-seq [opt]
10974 template < template-parameter-list > */
10975
10976 static void
10977 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10978 {
10979 /* Check for `export'. */
10980 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10981 {
10982 /* Consume the `export' token. */
10983 cp_lexer_consume_token (parser->lexer);
10984 /* Warn that we do not support `export'. */
10985 warning (0, "keyword %<export%> not implemented, and will be ignored");
10986 }
10987
10988 cp_parser_template_declaration_after_export (parser, member_p);
10989 }
10990
10991 /* Parse a template-parameter-list.
10992
10993 template-parameter-list:
10994 template-parameter
10995 template-parameter-list , template-parameter
10996
10997 Returns a TREE_LIST. Each node represents a template parameter.
10998 The nodes are connected via their TREE_CHAINs. */
10999
11000 static tree
11001 cp_parser_template_parameter_list (cp_parser* parser)
11002 {
11003 tree parameter_list = NULL_TREE;
11004
11005 begin_template_parm_list ();
11006
11007 /* The loop below parses the template parms. We first need to know
11008 the total number of template parms to be able to compute proper
11009 canonical types of each dependent type. So after the loop, when
11010 we know the total number of template parms,
11011 end_template_parm_list computes the proper canonical types and
11012 fixes up the dependent types accordingly. */
11013 while (true)
11014 {
11015 tree parameter;
11016 bool is_non_type;
11017 bool is_parameter_pack;
11018 location_t parm_loc;
11019
11020 /* Parse the template-parameter. */
11021 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11022 parameter = cp_parser_template_parameter (parser,
11023 &is_non_type,
11024 &is_parameter_pack);
11025 /* Add it to the list. */
11026 if (parameter != error_mark_node)
11027 parameter_list = process_template_parm (parameter_list,
11028 parm_loc,
11029 parameter,
11030 is_non_type,
11031 is_parameter_pack,
11032 0);
11033 else
11034 {
11035 tree err_parm = build_tree_list (parameter, parameter);
11036 parameter_list = chainon (parameter_list, err_parm);
11037 }
11038
11039 /* If the next token is not a `,', we're done. */
11040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11041 break;
11042 /* Otherwise, consume the `,' token. */
11043 cp_lexer_consume_token (parser->lexer);
11044 }
11045
11046 return end_template_parm_list (parameter_list);
11047 }
11048
11049 /* Parse a template-parameter.
11050
11051 template-parameter:
11052 type-parameter
11053 parameter-declaration
11054
11055 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11056 the parameter. The TREE_PURPOSE is the default value, if any.
11057 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11058 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11059 set to true iff this parameter is a parameter pack. */
11060
11061 static tree
11062 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11063 bool *is_parameter_pack)
11064 {
11065 cp_token *token;
11066 cp_parameter_declarator *parameter_declarator;
11067 cp_declarator *id_declarator;
11068 tree parm;
11069
11070 /* Assume it is a type parameter or a template parameter. */
11071 *is_non_type = false;
11072 /* Assume it not a parameter pack. */
11073 *is_parameter_pack = false;
11074 /* Peek at the next token. */
11075 token = cp_lexer_peek_token (parser->lexer);
11076 /* If it is `class' or `template', we have a type-parameter. */
11077 if (token->keyword == RID_TEMPLATE)
11078 return cp_parser_type_parameter (parser, is_parameter_pack);
11079 /* If it is `class' or `typename' we do not know yet whether it is a
11080 type parameter or a non-type parameter. Consider:
11081
11082 template <typename T, typename T::X X> ...
11083
11084 or:
11085
11086 template <class C, class D*> ...
11087
11088 Here, the first parameter is a type parameter, and the second is
11089 a non-type parameter. We can tell by looking at the token after
11090 the identifier -- if it is a `,', `=', or `>' then we have a type
11091 parameter. */
11092 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11093 {
11094 /* Peek at the token after `class' or `typename'. */
11095 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11096 /* If it's an ellipsis, we have a template type parameter
11097 pack. */
11098 if (token->type == CPP_ELLIPSIS)
11099 return cp_parser_type_parameter (parser, is_parameter_pack);
11100 /* If it's an identifier, skip it. */
11101 if (token->type == CPP_NAME)
11102 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11103 /* Now, see if the token looks like the end of a template
11104 parameter. */
11105 if (token->type == CPP_COMMA
11106 || token->type == CPP_EQ
11107 || token->type == CPP_GREATER)
11108 return cp_parser_type_parameter (parser, is_parameter_pack);
11109 }
11110
11111 /* Otherwise, it is a non-type parameter.
11112
11113 [temp.param]
11114
11115 When parsing a default template-argument for a non-type
11116 template-parameter, the first non-nested `>' is taken as the end
11117 of the template parameter-list rather than a greater-than
11118 operator. */
11119 *is_non_type = true;
11120 parameter_declarator
11121 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11122 /*parenthesized_p=*/NULL);
11123
11124 /* If the parameter declaration is marked as a parameter pack, set
11125 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11126 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11127 grokdeclarator. */
11128 if (parameter_declarator
11129 && parameter_declarator->declarator
11130 && parameter_declarator->declarator->parameter_pack_p)
11131 {
11132 *is_parameter_pack = true;
11133 parameter_declarator->declarator->parameter_pack_p = false;
11134 }
11135
11136 /* If the next token is an ellipsis, and we don't already have it
11137 marked as a parameter pack, then we have a parameter pack (that
11138 has no declarator). */
11139 if (!*is_parameter_pack
11140 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11141 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11142 {
11143 /* Consume the `...'. */
11144 cp_lexer_consume_token (parser->lexer);
11145 maybe_warn_variadic_templates ();
11146
11147 *is_parameter_pack = true;
11148 }
11149 /* We might end up with a pack expansion as the type of the non-type
11150 template parameter, in which case this is a non-type template
11151 parameter pack. */
11152 else if (parameter_declarator
11153 && parameter_declarator->decl_specifiers.type
11154 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11155 {
11156 *is_parameter_pack = true;
11157 parameter_declarator->decl_specifiers.type =
11158 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11159 }
11160
11161 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11162 {
11163 /* Parameter packs cannot have default arguments. However, a
11164 user may try to do so, so we'll parse them and give an
11165 appropriate diagnostic here. */
11166
11167 /* Consume the `='. */
11168 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11169 cp_lexer_consume_token (parser->lexer);
11170
11171 /* Find the name of the parameter pack. */
11172 id_declarator = parameter_declarator->declarator;
11173 while (id_declarator && id_declarator->kind != cdk_id)
11174 id_declarator = id_declarator->declarator;
11175
11176 if (id_declarator && id_declarator->kind == cdk_id)
11177 error_at (start_token->location,
11178 "template parameter pack %qD cannot have a default argument",
11179 id_declarator->u.id.unqualified_name);
11180 else
11181 error_at (start_token->location,
11182 "template parameter pack cannot have a default argument");
11183
11184 /* Parse the default argument, but throw away the result. */
11185 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11186 }
11187
11188 parm = grokdeclarator (parameter_declarator->declarator,
11189 &parameter_declarator->decl_specifiers,
11190 TPARM, /*initialized=*/0,
11191 /*attrlist=*/NULL);
11192 if (parm == error_mark_node)
11193 return error_mark_node;
11194
11195 return build_tree_list (parameter_declarator->default_argument, parm);
11196 }
11197
11198 /* Parse a type-parameter.
11199
11200 type-parameter:
11201 class identifier [opt]
11202 class identifier [opt] = type-id
11203 typename identifier [opt]
11204 typename identifier [opt] = type-id
11205 template < template-parameter-list > class identifier [opt]
11206 template < template-parameter-list > class identifier [opt]
11207 = id-expression
11208
11209 GNU Extension (variadic templates):
11210
11211 type-parameter:
11212 class ... identifier [opt]
11213 typename ... identifier [opt]
11214
11215 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11216 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11217 the declaration of the parameter.
11218
11219 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11220
11221 static tree
11222 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11223 {
11224 cp_token *token;
11225 tree parameter;
11226
11227 /* Look for a keyword to tell us what kind of parameter this is. */
11228 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11229 if (!token)
11230 return error_mark_node;
11231
11232 switch (token->keyword)
11233 {
11234 case RID_CLASS:
11235 case RID_TYPENAME:
11236 {
11237 tree identifier;
11238 tree default_argument;
11239
11240 /* If the next token is an ellipsis, we have a template
11241 argument pack. */
11242 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11243 {
11244 /* Consume the `...' token. */
11245 cp_lexer_consume_token (parser->lexer);
11246 maybe_warn_variadic_templates ();
11247
11248 *is_parameter_pack = true;
11249 }
11250
11251 /* If the next token is an identifier, then it names the
11252 parameter. */
11253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11254 identifier = cp_parser_identifier (parser);
11255 else
11256 identifier = NULL_TREE;
11257
11258 /* Create the parameter. */
11259 parameter = finish_template_type_parm (class_type_node, identifier);
11260
11261 /* If the next token is an `=', we have a default argument. */
11262 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11263 {
11264 /* Consume the `=' token. */
11265 cp_lexer_consume_token (parser->lexer);
11266 /* Parse the default-argument. */
11267 push_deferring_access_checks (dk_no_deferred);
11268 default_argument = cp_parser_type_id (parser);
11269
11270 /* Template parameter packs cannot have default
11271 arguments. */
11272 if (*is_parameter_pack)
11273 {
11274 if (identifier)
11275 error_at (token->location,
11276 "template parameter pack %qD cannot have a "
11277 "default argument", identifier);
11278 else
11279 error_at (token->location,
11280 "template parameter packs cannot have "
11281 "default arguments");
11282 default_argument = NULL_TREE;
11283 }
11284 pop_deferring_access_checks ();
11285 }
11286 else
11287 default_argument = NULL_TREE;
11288
11289 /* Create the combined representation of the parameter and the
11290 default argument. */
11291 parameter = build_tree_list (default_argument, parameter);
11292 }
11293 break;
11294
11295 case RID_TEMPLATE:
11296 {
11297 tree identifier;
11298 tree default_argument;
11299
11300 /* Look for the `<'. */
11301 cp_parser_require (parser, CPP_LESS, RT_LESS);
11302 /* Parse the template-parameter-list. */
11303 cp_parser_template_parameter_list (parser);
11304 /* Look for the `>'. */
11305 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11306 /* Look for the `class' keyword. */
11307 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11308 /* If the next token is an ellipsis, we have a template
11309 argument pack. */
11310 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11311 {
11312 /* Consume the `...' token. */
11313 cp_lexer_consume_token (parser->lexer);
11314 maybe_warn_variadic_templates ();
11315
11316 *is_parameter_pack = true;
11317 }
11318 /* If the next token is an `=', then there is a
11319 default-argument. If the next token is a `>', we are at
11320 the end of the parameter-list. If the next token is a `,',
11321 then we are at the end of this parameter. */
11322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11323 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11324 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11325 {
11326 identifier = cp_parser_identifier (parser);
11327 /* Treat invalid names as if the parameter were nameless. */
11328 if (identifier == error_mark_node)
11329 identifier = NULL_TREE;
11330 }
11331 else
11332 identifier = NULL_TREE;
11333
11334 /* Create the template parameter. */
11335 parameter = finish_template_template_parm (class_type_node,
11336 identifier);
11337
11338 /* If the next token is an `=', then there is a
11339 default-argument. */
11340 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11341 {
11342 bool is_template;
11343
11344 /* Consume the `='. */
11345 cp_lexer_consume_token (parser->lexer);
11346 /* Parse the id-expression. */
11347 push_deferring_access_checks (dk_no_deferred);
11348 /* save token before parsing the id-expression, for error
11349 reporting */
11350 token = cp_lexer_peek_token (parser->lexer);
11351 default_argument
11352 = cp_parser_id_expression (parser,
11353 /*template_keyword_p=*/false,
11354 /*check_dependency_p=*/true,
11355 /*template_p=*/&is_template,
11356 /*declarator_p=*/false,
11357 /*optional_p=*/false);
11358 if (TREE_CODE (default_argument) == TYPE_DECL)
11359 /* If the id-expression was a template-id that refers to
11360 a template-class, we already have the declaration here,
11361 so no further lookup is needed. */
11362 ;
11363 else
11364 /* Look up the name. */
11365 default_argument
11366 = cp_parser_lookup_name (parser, default_argument,
11367 none_type,
11368 /*is_template=*/is_template,
11369 /*is_namespace=*/false,
11370 /*check_dependency=*/true,
11371 /*ambiguous_decls=*/NULL,
11372 token->location);
11373 /* See if the default argument is valid. */
11374 default_argument
11375 = check_template_template_default_arg (default_argument);
11376
11377 /* Template parameter packs cannot have default
11378 arguments. */
11379 if (*is_parameter_pack)
11380 {
11381 if (identifier)
11382 error_at (token->location,
11383 "template parameter pack %qD cannot "
11384 "have a default argument",
11385 identifier);
11386 else
11387 error_at (token->location, "template parameter packs cannot "
11388 "have default arguments");
11389 default_argument = NULL_TREE;
11390 }
11391 pop_deferring_access_checks ();
11392 }
11393 else
11394 default_argument = NULL_TREE;
11395
11396 /* Create the combined representation of the parameter and the
11397 default argument. */
11398 parameter = build_tree_list (default_argument, parameter);
11399 }
11400 break;
11401
11402 default:
11403 gcc_unreachable ();
11404 break;
11405 }
11406
11407 return parameter;
11408 }
11409
11410 /* Parse a template-id.
11411
11412 template-id:
11413 template-name < template-argument-list [opt] >
11414
11415 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11416 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11417 returned. Otherwise, if the template-name names a function, or set
11418 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11419 names a class, returns a TYPE_DECL for the specialization.
11420
11421 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11422 uninstantiated templates. */
11423
11424 static tree
11425 cp_parser_template_id (cp_parser *parser,
11426 bool template_keyword_p,
11427 bool check_dependency_p,
11428 bool is_declaration)
11429 {
11430 int i;
11431 tree templ;
11432 tree arguments;
11433 tree template_id;
11434 cp_token_position start_of_id = 0;
11435 deferred_access_check *chk;
11436 VEC (deferred_access_check,gc) *access_check;
11437 cp_token *next_token = NULL, *next_token_2 = NULL;
11438 bool is_identifier;
11439
11440 /* If the next token corresponds to a template-id, there is no need
11441 to reparse it. */
11442 next_token = cp_lexer_peek_token (parser->lexer);
11443 if (next_token->type == CPP_TEMPLATE_ID)
11444 {
11445 struct tree_check *check_value;
11446
11447 /* Get the stored value. */
11448 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11449 /* Perform any access checks that were deferred. */
11450 access_check = check_value->checks;
11451 if (access_check)
11452 {
11453 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11454 perform_or_defer_access_check (chk->binfo,
11455 chk->decl,
11456 chk->diag_decl);
11457 }
11458 /* Return the stored value. */
11459 return check_value->value;
11460 }
11461
11462 /* Avoid performing name lookup if there is no possibility of
11463 finding a template-id. */
11464 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11465 || (next_token->type == CPP_NAME
11466 && !cp_parser_nth_token_starts_template_argument_list_p
11467 (parser, 2)))
11468 {
11469 cp_parser_error (parser, "expected template-id");
11470 return error_mark_node;
11471 }
11472
11473 /* Remember where the template-id starts. */
11474 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11475 start_of_id = cp_lexer_token_position (parser->lexer, false);
11476
11477 push_deferring_access_checks (dk_deferred);
11478
11479 /* Parse the template-name. */
11480 is_identifier = false;
11481 templ = cp_parser_template_name (parser, template_keyword_p,
11482 check_dependency_p,
11483 is_declaration,
11484 &is_identifier);
11485 if (templ == error_mark_node || is_identifier)
11486 {
11487 pop_deferring_access_checks ();
11488 return templ;
11489 }
11490
11491 /* If we find the sequence `[:' after a template-name, it's probably
11492 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11493 parse correctly the argument list. */
11494 next_token = cp_lexer_peek_token (parser->lexer);
11495 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11496 if (next_token->type == CPP_OPEN_SQUARE
11497 && next_token->flags & DIGRAPH
11498 && next_token_2->type == CPP_COLON
11499 && !(next_token_2->flags & PREV_WHITE))
11500 {
11501 cp_parser_parse_tentatively (parser);
11502 /* Change `:' into `::'. */
11503 next_token_2->type = CPP_SCOPE;
11504 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11505 CPP_LESS. */
11506 cp_lexer_consume_token (parser->lexer);
11507
11508 /* Parse the arguments. */
11509 arguments = cp_parser_enclosed_template_argument_list (parser);
11510 if (!cp_parser_parse_definitely (parser))
11511 {
11512 /* If we couldn't parse an argument list, then we revert our changes
11513 and return simply an error. Maybe this is not a template-id
11514 after all. */
11515 next_token_2->type = CPP_COLON;
11516 cp_parser_error (parser, "expected %<<%>");
11517 pop_deferring_access_checks ();
11518 return error_mark_node;
11519 }
11520 /* Otherwise, emit an error about the invalid digraph, but continue
11521 parsing because we got our argument list. */
11522 if (permerror (next_token->location,
11523 "%<<::%> cannot begin a template-argument list"))
11524 {
11525 static bool hint = false;
11526 inform (next_token->location,
11527 "%<<:%> is an alternate spelling for %<[%>."
11528 " Insert whitespace between %<<%> and %<::%>");
11529 if (!hint && !flag_permissive)
11530 {
11531 inform (next_token->location, "(if you use %<-fpermissive%>"
11532 " G++ will accept your code)");
11533 hint = true;
11534 }
11535 }
11536 }
11537 else
11538 {
11539 /* Look for the `<' that starts the template-argument-list. */
11540 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11541 {
11542 pop_deferring_access_checks ();
11543 return error_mark_node;
11544 }
11545 /* Parse the arguments. */
11546 arguments = cp_parser_enclosed_template_argument_list (parser);
11547 }
11548
11549 /* Build a representation of the specialization. */
11550 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11551 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11552 else if (DECL_CLASS_TEMPLATE_P (templ)
11553 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11554 {
11555 bool entering_scope;
11556 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11557 template (rather than some instantiation thereof) only if
11558 is not nested within some other construct. For example, in
11559 "template <typename T> void f(T) { A<T>::", A<T> is just an
11560 instantiation of A. */
11561 entering_scope = (template_parm_scope_p ()
11562 && cp_lexer_next_token_is (parser->lexer,
11563 CPP_SCOPE));
11564 template_id
11565 = finish_template_type (templ, arguments, entering_scope);
11566 }
11567 else
11568 {
11569 /* If it's not a class-template or a template-template, it should be
11570 a function-template. */
11571 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11572 || TREE_CODE (templ) == OVERLOAD
11573 || BASELINK_P (templ)));
11574
11575 template_id = lookup_template_function (templ, arguments);
11576 }
11577
11578 /* If parsing tentatively, replace the sequence of tokens that makes
11579 up the template-id with a CPP_TEMPLATE_ID token. That way,
11580 should we re-parse the token stream, we will not have to repeat
11581 the effort required to do the parse, nor will we issue duplicate
11582 error messages about problems during instantiation of the
11583 template. */
11584 if (start_of_id)
11585 {
11586 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11587
11588 /* Reset the contents of the START_OF_ID token. */
11589 token->type = CPP_TEMPLATE_ID;
11590 /* Retrieve any deferred checks. Do not pop this access checks yet
11591 so the memory will not be reclaimed during token replacing below. */
11592 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11593 token->u.tree_check_value->value = template_id;
11594 token->u.tree_check_value->checks = get_deferred_access_checks ();
11595 token->keyword = RID_MAX;
11596
11597 /* Purge all subsequent tokens. */
11598 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11599
11600 /* ??? Can we actually assume that, if template_id ==
11601 error_mark_node, we will have issued a diagnostic to the
11602 user, as opposed to simply marking the tentative parse as
11603 failed? */
11604 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11605 error_at (token->location, "parse error in template argument list");
11606 }
11607
11608 pop_deferring_access_checks ();
11609 return template_id;
11610 }
11611
11612 /* Parse a template-name.
11613
11614 template-name:
11615 identifier
11616
11617 The standard should actually say:
11618
11619 template-name:
11620 identifier
11621 operator-function-id
11622
11623 A defect report has been filed about this issue.
11624
11625 A conversion-function-id cannot be a template name because they cannot
11626 be part of a template-id. In fact, looking at this code:
11627
11628 a.operator K<int>()
11629
11630 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11631 It is impossible to call a templated conversion-function-id with an
11632 explicit argument list, since the only allowed template parameter is
11633 the type to which it is converting.
11634
11635 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11636 `template' keyword, in a construction like:
11637
11638 T::template f<3>()
11639
11640 In that case `f' is taken to be a template-name, even though there
11641 is no way of knowing for sure.
11642
11643 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11644 name refers to a set of overloaded functions, at least one of which
11645 is a template, or an IDENTIFIER_NODE with the name of the template,
11646 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11647 names are looked up inside uninstantiated templates. */
11648
11649 static tree
11650 cp_parser_template_name (cp_parser* parser,
11651 bool template_keyword_p,
11652 bool check_dependency_p,
11653 bool is_declaration,
11654 bool *is_identifier)
11655 {
11656 tree identifier;
11657 tree decl;
11658 tree fns;
11659 cp_token *token = cp_lexer_peek_token (parser->lexer);
11660
11661 /* If the next token is `operator', then we have either an
11662 operator-function-id or a conversion-function-id. */
11663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11664 {
11665 /* We don't know whether we're looking at an
11666 operator-function-id or a conversion-function-id. */
11667 cp_parser_parse_tentatively (parser);
11668 /* Try an operator-function-id. */
11669 identifier = cp_parser_operator_function_id (parser);
11670 /* If that didn't work, try a conversion-function-id. */
11671 if (!cp_parser_parse_definitely (parser))
11672 {
11673 cp_parser_error (parser, "expected template-name");
11674 return error_mark_node;
11675 }
11676 }
11677 /* Look for the identifier. */
11678 else
11679 identifier = cp_parser_identifier (parser);
11680
11681 /* If we didn't find an identifier, we don't have a template-id. */
11682 if (identifier == error_mark_node)
11683 return error_mark_node;
11684
11685 /* If the name immediately followed the `template' keyword, then it
11686 is a template-name. However, if the next token is not `<', then
11687 we do not treat it as a template-name, since it is not being used
11688 as part of a template-id. This enables us to handle constructs
11689 like:
11690
11691 template <typename T> struct S { S(); };
11692 template <typename T> S<T>::S();
11693
11694 correctly. We would treat `S' as a template -- if it were `S<T>'
11695 -- but we do not if there is no `<'. */
11696
11697 if (processing_template_decl
11698 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11699 {
11700 /* In a declaration, in a dependent context, we pretend that the
11701 "template" keyword was present in order to improve error
11702 recovery. For example, given:
11703
11704 template <typename T> void f(T::X<int>);
11705
11706 we want to treat "X<int>" as a template-id. */
11707 if (is_declaration
11708 && !template_keyword_p
11709 && parser->scope && TYPE_P (parser->scope)
11710 && check_dependency_p
11711 && dependent_scope_p (parser->scope)
11712 /* Do not do this for dtors (or ctors), since they never
11713 need the template keyword before their name. */
11714 && !constructor_name_p (identifier, parser->scope))
11715 {
11716 cp_token_position start = 0;
11717
11718 /* Explain what went wrong. */
11719 error_at (token->location, "non-template %qD used as template",
11720 identifier);
11721 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11722 parser->scope, identifier);
11723 /* If parsing tentatively, find the location of the "<" token. */
11724 if (cp_parser_simulate_error (parser))
11725 start = cp_lexer_token_position (parser->lexer, true);
11726 /* Parse the template arguments so that we can issue error
11727 messages about them. */
11728 cp_lexer_consume_token (parser->lexer);
11729 cp_parser_enclosed_template_argument_list (parser);
11730 /* Skip tokens until we find a good place from which to
11731 continue parsing. */
11732 cp_parser_skip_to_closing_parenthesis (parser,
11733 /*recovering=*/true,
11734 /*or_comma=*/true,
11735 /*consume_paren=*/false);
11736 /* If parsing tentatively, permanently remove the
11737 template argument list. That will prevent duplicate
11738 error messages from being issued about the missing
11739 "template" keyword. */
11740 if (start)
11741 cp_lexer_purge_tokens_after (parser->lexer, start);
11742 if (is_identifier)
11743 *is_identifier = true;
11744 return identifier;
11745 }
11746
11747 /* If the "template" keyword is present, then there is generally
11748 no point in doing name-lookup, so we just return IDENTIFIER.
11749 But, if the qualifying scope is non-dependent then we can
11750 (and must) do name-lookup normally. */
11751 if (template_keyword_p
11752 && (!parser->scope
11753 || (TYPE_P (parser->scope)
11754 && dependent_type_p (parser->scope))))
11755 return identifier;
11756 }
11757
11758 /* Look up the name. */
11759 decl = cp_parser_lookup_name (parser, identifier,
11760 none_type,
11761 /*is_template=*/true,
11762 /*is_namespace=*/false,
11763 check_dependency_p,
11764 /*ambiguous_decls=*/NULL,
11765 token->location);
11766
11767 /* If DECL is a template, then the name was a template-name. */
11768 if (TREE_CODE (decl) == TEMPLATE_DECL)
11769 ;
11770 else
11771 {
11772 tree fn = NULL_TREE;
11773
11774 /* The standard does not explicitly indicate whether a name that
11775 names a set of overloaded declarations, some of which are
11776 templates, is a template-name. However, such a name should
11777 be a template-name; otherwise, there is no way to form a
11778 template-id for the overloaded templates. */
11779 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11780 if (TREE_CODE (fns) == OVERLOAD)
11781 for (fn = fns; fn; fn = OVL_NEXT (fn))
11782 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11783 break;
11784
11785 if (!fn)
11786 {
11787 /* The name does not name a template. */
11788 cp_parser_error (parser, "expected template-name");
11789 return error_mark_node;
11790 }
11791 }
11792
11793 /* If DECL is dependent, and refers to a function, then just return
11794 its name; we will look it up again during template instantiation. */
11795 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11796 {
11797 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11798 if (TYPE_P (scope) && dependent_type_p (scope))
11799 return identifier;
11800 }
11801
11802 return decl;
11803 }
11804
11805 /* Parse a template-argument-list.
11806
11807 template-argument-list:
11808 template-argument ... [opt]
11809 template-argument-list , template-argument ... [opt]
11810
11811 Returns a TREE_VEC containing the arguments. */
11812
11813 static tree
11814 cp_parser_template_argument_list (cp_parser* parser)
11815 {
11816 tree fixed_args[10];
11817 unsigned n_args = 0;
11818 unsigned alloced = 10;
11819 tree *arg_ary = fixed_args;
11820 tree vec;
11821 bool saved_in_template_argument_list_p;
11822 bool saved_ice_p;
11823 bool saved_non_ice_p;
11824
11825 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11826 parser->in_template_argument_list_p = true;
11827 /* Even if the template-id appears in an integral
11828 constant-expression, the contents of the argument list do
11829 not. */
11830 saved_ice_p = parser->integral_constant_expression_p;
11831 parser->integral_constant_expression_p = false;
11832 saved_non_ice_p = parser->non_integral_constant_expression_p;
11833 parser->non_integral_constant_expression_p = false;
11834 /* Parse the arguments. */
11835 do
11836 {
11837 tree argument;
11838
11839 if (n_args)
11840 /* Consume the comma. */
11841 cp_lexer_consume_token (parser->lexer);
11842
11843 /* Parse the template-argument. */
11844 argument = cp_parser_template_argument (parser);
11845
11846 /* If the next token is an ellipsis, we're expanding a template
11847 argument pack. */
11848 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11849 {
11850 if (argument == error_mark_node)
11851 {
11852 cp_token *token = cp_lexer_peek_token (parser->lexer);
11853 error_at (token->location,
11854 "expected parameter pack before %<...%>");
11855 }
11856 /* Consume the `...' token. */
11857 cp_lexer_consume_token (parser->lexer);
11858
11859 /* Make the argument into a TYPE_PACK_EXPANSION or
11860 EXPR_PACK_EXPANSION. */
11861 argument = make_pack_expansion (argument);
11862 }
11863
11864 if (n_args == alloced)
11865 {
11866 alloced *= 2;
11867
11868 if (arg_ary == fixed_args)
11869 {
11870 arg_ary = XNEWVEC (tree, alloced);
11871 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11872 }
11873 else
11874 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11875 }
11876 arg_ary[n_args++] = argument;
11877 }
11878 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11879
11880 vec = make_tree_vec (n_args);
11881
11882 while (n_args--)
11883 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11884
11885 if (arg_ary != fixed_args)
11886 free (arg_ary);
11887 parser->non_integral_constant_expression_p = saved_non_ice_p;
11888 parser->integral_constant_expression_p = saved_ice_p;
11889 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11890 #ifdef ENABLE_CHECKING
11891 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11892 #endif
11893 return vec;
11894 }
11895
11896 /* Parse a template-argument.
11897
11898 template-argument:
11899 assignment-expression
11900 type-id
11901 id-expression
11902
11903 The representation is that of an assignment-expression, type-id, or
11904 id-expression -- except that the qualified id-expression is
11905 evaluated, so that the value returned is either a DECL or an
11906 OVERLOAD.
11907
11908 Although the standard says "assignment-expression", it forbids
11909 throw-expressions or assignments in the template argument.
11910 Therefore, we use "conditional-expression" instead. */
11911
11912 static tree
11913 cp_parser_template_argument (cp_parser* parser)
11914 {
11915 tree argument;
11916 bool template_p;
11917 bool address_p;
11918 bool maybe_type_id = false;
11919 cp_token *token = NULL, *argument_start_token = NULL;
11920 cp_id_kind idk;
11921
11922 /* There's really no way to know what we're looking at, so we just
11923 try each alternative in order.
11924
11925 [temp.arg]
11926
11927 In a template-argument, an ambiguity between a type-id and an
11928 expression is resolved to a type-id, regardless of the form of
11929 the corresponding template-parameter.
11930
11931 Therefore, we try a type-id first. */
11932 cp_parser_parse_tentatively (parser);
11933 argument = cp_parser_template_type_arg (parser);
11934 /* If there was no error parsing the type-id but the next token is a
11935 '>>', our behavior depends on which dialect of C++ we're
11936 parsing. In C++98, we probably found a typo for '> >'. But there
11937 are type-id which are also valid expressions. For instance:
11938
11939 struct X { int operator >> (int); };
11940 template <int V> struct Foo {};
11941 Foo<X () >> 5> r;
11942
11943 Here 'X()' is a valid type-id of a function type, but the user just
11944 wanted to write the expression "X() >> 5". Thus, we remember that we
11945 found a valid type-id, but we still try to parse the argument as an
11946 expression to see what happens.
11947
11948 In C++0x, the '>>' will be considered two separate '>'
11949 tokens. */
11950 if (!cp_parser_error_occurred (parser)
11951 && cxx_dialect == cxx98
11952 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11953 {
11954 maybe_type_id = true;
11955 cp_parser_abort_tentative_parse (parser);
11956 }
11957 else
11958 {
11959 /* If the next token isn't a `,' or a `>', then this argument wasn't
11960 really finished. This means that the argument is not a valid
11961 type-id. */
11962 if (!cp_parser_next_token_ends_template_argument_p (parser))
11963 cp_parser_error (parser, "expected template-argument");
11964 /* If that worked, we're done. */
11965 if (cp_parser_parse_definitely (parser))
11966 return argument;
11967 }
11968 /* We're still not sure what the argument will be. */
11969 cp_parser_parse_tentatively (parser);
11970 /* Try a template. */
11971 argument_start_token = cp_lexer_peek_token (parser->lexer);
11972 argument = cp_parser_id_expression (parser,
11973 /*template_keyword_p=*/false,
11974 /*check_dependency_p=*/true,
11975 &template_p,
11976 /*declarator_p=*/false,
11977 /*optional_p=*/false);
11978 /* If the next token isn't a `,' or a `>', then this argument wasn't
11979 really finished. */
11980 if (!cp_parser_next_token_ends_template_argument_p (parser))
11981 cp_parser_error (parser, "expected template-argument");
11982 if (!cp_parser_error_occurred (parser))
11983 {
11984 /* Figure out what is being referred to. If the id-expression
11985 was for a class template specialization, then we will have a
11986 TYPE_DECL at this point. There is no need to do name lookup
11987 at this point in that case. */
11988 if (TREE_CODE (argument) != TYPE_DECL)
11989 argument = cp_parser_lookup_name (parser, argument,
11990 none_type,
11991 /*is_template=*/template_p,
11992 /*is_namespace=*/false,
11993 /*check_dependency=*/true,
11994 /*ambiguous_decls=*/NULL,
11995 argument_start_token->location);
11996 if (TREE_CODE (argument) != TEMPLATE_DECL
11997 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11998 cp_parser_error (parser, "expected template-name");
11999 }
12000 if (cp_parser_parse_definitely (parser))
12001 return argument;
12002 /* It must be a non-type argument. There permitted cases are given
12003 in [temp.arg.nontype]:
12004
12005 -- an integral constant-expression of integral or enumeration
12006 type; or
12007
12008 -- the name of a non-type template-parameter; or
12009
12010 -- the name of an object or function with external linkage...
12011
12012 -- the address of an object or function with external linkage...
12013
12014 -- a pointer to member... */
12015 /* Look for a non-type template parameter. */
12016 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12017 {
12018 cp_parser_parse_tentatively (parser);
12019 argument = cp_parser_primary_expression (parser,
12020 /*address_p=*/false,
12021 /*cast_p=*/false,
12022 /*template_arg_p=*/true,
12023 &idk);
12024 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12025 || !cp_parser_next_token_ends_template_argument_p (parser))
12026 cp_parser_simulate_error (parser);
12027 if (cp_parser_parse_definitely (parser))
12028 return argument;
12029 }
12030
12031 /* If the next token is "&", the argument must be the address of an
12032 object or function with external linkage. */
12033 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12034 if (address_p)
12035 cp_lexer_consume_token (parser->lexer);
12036 /* See if we might have an id-expression. */
12037 token = cp_lexer_peek_token (parser->lexer);
12038 if (token->type == CPP_NAME
12039 || token->keyword == RID_OPERATOR
12040 || token->type == CPP_SCOPE
12041 || token->type == CPP_TEMPLATE_ID
12042 || token->type == CPP_NESTED_NAME_SPECIFIER)
12043 {
12044 cp_parser_parse_tentatively (parser);
12045 argument = cp_parser_primary_expression (parser,
12046 address_p,
12047 /*cast_p=*/false,
12048 /*template_arg_p=*/true,
12049 &idk);
12050 if (cp_parser_error_occurred (parser)
12051 || !cp_parser_next_token_ends_template_argument_p (parser))
12052 cp_parser_abort_tentative_parse (parser);
12053 else
12054 {
12055 tree probe;
12056
12057 if (TREE_CODE (argument) == INDIRECT_REF)
12058 {
12059 gcc_assert (REFERENCE_REF_P (argument));
12060 argument = TREE_OPERAND (argument, 0);
12061 }
12062
12063 /* If we're in a template, we represent a qualified-id referring
12064 to a static data member as a SCOPE_REF even if the scope isn't
12065 dependent so that we can check access control later. */
12066 probe = argument;
12067 if (TREE_CODE (probe) == SCOPE_REF)
12068 probe = TREE_OPERAND (probe, 1);
12069 if (TREE_CODE (probe) == VAR_DECL)
12070 {
12071 /* A variable without external linkage might still be a
12072 valid constant-expression, so no error is issued here
12073 if the external-linkage check fails. */
12074 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12075 cp_parser_simulate_error (parser);
12076 }
12077 else if (is_overloaded_fn (argument))
12078 /* All overloaded functions are allowed; if the external
12079 linkage test does not pass, an error will be issued
12080 later. */
12081 ;
12082 else if (address_p
12083 && (TREE_CODE (argument) == OFFSET_REF
12084 || TREE_CODE (argument) == SCOPE_REF))
12085 /* A pointer-to-member. */
12086 ;
12087 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12088 ;
12089 else
12090 cp_parser_simulate_error (parser);
12091
12092 if (cp_parser_parse_definitely (parser))
12093 {
12094 if (address_p)
12095 argument = build_x_unary_op (ADDR_EXPR, argument,
12096 tf_warning_or_error);
12097 return argument;
12098 }
12099 }
12100 }
12101 /* If the argument started with "&", there are no other valid
12102 alternatives at this point. */
12103 if (address_p)
12104 {
12105 cp_parser_error (parser, "invalid non-type template argument");
12106 return error_mark_node;
12107 }
12108
12109 /* If the argument wasn't successfully parsed as a type-id followed
12110 by '>>', the argument can only be a constant expression now.
12111 Otherwise, we try parsing the constant-expression tentatively,
12112 because the argument could really be a type-id. */
12113 if (maybe_type_id)
12114 cp_parser_parse_tentatively (parser);
12115 argument = cp_parser_constant_expression (parser,
12116 /*allow_non_constant_p=*/false,
12117 /*non_constant_p=*/NULL);
12118 argument = fold_non_dependent_expr (argument);
12119 if (!maybe_type_id)
12120 return argument;
12121 if (!cp_parser_next_token_ends_template_argument_p (parser))
12122 cp_parser_error (parser, "expected template-argument");
12123 if (cp_parser_parse_definitely (parser))
12124 return argument;
12125 /* We did our best to parse the argument as a non type-id, but that
12126 was the only alternative that matched (albeit with a '>' after
12127 it). We can assume it's just a typo from the user, and a
12128 diagnostic will then be issued. */
12129 return cp_parser_template_type_arg (parser);
12130 }
12131
12132 /* Parse an explicit-instantiation.
12133
12134 explicit-instantiation:
12135 template declaration
12136
12137 Although the standard says `declaration', what it really means is:
12138
12139 explicit-instantiation:
12140 template decl-specifier-seq [opt] declarator [opt] ;
12141
12142 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12143 supposed to be allowed. A defect report has been filed about this
12144 issue.
12145
12146 GNU Extension:
12147
12148 explicit-instantiation:
12149 storage-class-specifier template
12150 decl-specifier-seq [opt] declarator [opt] ;
12151 function-specifier template
12152 decl-specifier-seq [opt] declarator [opt] ; */
12153
12154 static void
12155 cp_parser_explicit_instantiation (cp_parser* parser)
12156 {
12157 int declares_class_or_enum;
12158 cp_decl_specifier_seq decl_specifiers;
12159 tree extension_specifier = NULL_TREE;
12160
12161 timevar_push (TV_TEMPLATE_INST);
12162
12163 /* Look for an (optional) storage-class-specifier or
12164 function-specifier. */
12165 if (cp_parser_allow_gnu_extensions_p (parser))
12166 {
12167 extension_specifier
12168 = cp_parser_storage_class_specifier_opt (parser);
12169 if (!extension_specifier)
12170 extension_specifier
12171 = cp_parser_function_specifier_opt (parser,
12172 /*decl_specs=*/NULL);
12173 }
12174
12175 /* Look for the `template' keyword. */
12176 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12177 /* Let the front end know that we are processing an explicit
12178 instantiation. */
12179 begin_explicit_instantiation ();
12180 /* [temp.explicit] says that we are supposed to ignore access
12181 control while processing explicit instantiation directives. */
12182 push_deferring_access_checks (dk_no_check);
12183 /* Parse a decl-specifier-seq. */
12184 cp_parser_decl_specifier_seq (parser,
12185 CP_PARSER_FLAGS_OPTIONAL,
12186 &decl_specifiers,
12187 &declares_class_or_enum);
12188 /* If there was exactly one decl-specifier, and it declared a class,
12189 and there's no declarator, then we have an explicit type
12190 instantiation. */
12191 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12192 {
12193 tree type;
12194
12195 type = check_tag_decl (&decl_specifiers);
12196 /* Turn access control back on for names used during
12197 template instantiation. */
12198 pop_deferring_access_checks ();
12199 if (type)
12200 do_type_instantiation (type, extension_specifier,
12201 /*complain=*/tf_error);
12202 }
12203 else
12204 {
12205 cp_declarator *declarator;
12206 tree decl;
12207
12208 /* Parse the declarator. */
12209 declarator
12210 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12211 /*ctor_dtor_or_conv_p=*/NULL,
12212 /*parenthesized_p=*/NULL,
12213 /*member_p=*/false);
12214 if (declares_class_or_enum & 2)
12215 cp_parser_check_for_definition_in_return_type (declarator,
12216 decl_specifiers.type,
12217 decl_specifiers.type_location);
12218 if (declarator != cp_error_declarator)
12219 {
12220 if (decl_specifiers.specs[(int)ds_inline])
12221 permerror (input_location, "explicit instantiation shall not use"
12222 " %<inline%> specifier");
12223 if (decl_specifiers.specs[(int)ds_constexpr])
12224 permerror (input_location, "explicit instantiation shall not use"
12225 " %<constexpr%> specifier");
12226
12227 decl = grokdeclarator (declarator, &decl_specifiers,
12228 NORMAL, 0, &decl_specifiers.attributes);
12229 /* Turn access control back on for names used during
12230 template instantiation. */
12231 pop_deferring_access_checks ();
12232 /* Do the explicit instantiation. */
12233 do_decl_instantiation (decl, extension_specifier);
12234 }
12235 else
12236 {
12237 pop_deferring_access_checks ();
12238 /* Skip the body of the explicit instantiation. */
12239 cp_parser_skip_to_end_of_statement (parser);
12240 }
12241 }
12242 /* We're done with the instantiation. */
12243 end_explicit_instantiation ();
12244
12245 cp_parser_consume_semicolon_at_end_of_statement (parser);
12246
12247 timevar_pop (TV_TEMPLATE_INST);
12248 }
12249
12250 /* Parse an explicit-specialization.
12251
12252 explicit-specialization:
12253 template < > declaration
12254
12255 Although the standard says `declaration', what it really means is:
12256
12257 explicit-specialization:
12258 template <> decl-specifier [opt] init-declarator [opt] ;
12259 template <> function-definition
12260 template <> explicit-specialization
12261 template <> template-declaration */
12262
12263 static void
12264 cp_parser_explicit_specialization (cp_parser* parser)
12265 {
12266 bool need_lang_pop;
12267 cp_token *token = cp_lexer_peek_token (parser->lexer);
12268
12269 /* Look for the `template' keyword. */
12270 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12271 /* Look for the `<'. */
12272 cp_parser_require (parser, CPP_LESS, RT_LESS);
12273 /* Look for the `>'. */
12274 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12275 /* We have processed another parameter list. */
12276 ++parser->num_template_parameter_lists;
12277 /* [temp]
12278
12279 A template ... explicit specialization ... shall not have C
12280 linkage. */
12281 if (current_lang_name == lang_name_c)
12282 {
12283 error_at (token->location, "template specialization with C linkage");
12284 /* Give it C++ linkage to avoid confusing other parts of the
12285 front end. */
12286 push_lang_context (lang_name_cplusplus);
12287 need_lang_pop = true;
12288 }
12289 else
12290 need_lang_pop = false;
12291 /* Let the front end know that we are beginning a specialization. */
12292 if (!begin_specialization ())
12293 {
12294 end_specialization ();
12295 return;
12296 }
12297
12298 /* If the next keyword is `template', we need to figure out whether
12299 or not we're looking a template-declaration. */
12300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12301 {
12302 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12303 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12304 cp_parser_template_declaration_after_export (parser,
12305 /*member_p=*/false);
12306 else
12307 cp_parser_explicit_specialization (parser);
12308 }
12309 else
12310 /* Parse the dependent declaration. */
12311 cp_parser_single_declaration (parser,
12312 /*checks=*/NULL,
12313 /*member_p=*/false,
12314 /*explicit_specialization_p=*/true,
12315 /*friend_p=*/NULL);
12316 /* We're done with the specialization. */
12317 end_specialization ();
12318 /* For the erroneous case of a template with C linkage, we pushed an
12319 implicit C++ linkage scope; exit that scope now. */
12320 if (need_lang_pop)
12321 pop_lang_context ();
12322 /* We're done with this parameter list. */
12323 --parser->num_template_parameter_lists;
12324 }
12325
12326 /* Parse a type-specifier.
12327
12328 type-specifier:
12329 simple-type-specifier
12330 class-specifier
12331 enum-specifier
12332 elaborated-type-specifier
12333 cv-qualifier
12334
12335 GNU Extension:
12336
12337 type-specifier:
12338 __complex__
12339
12340 Returns a representation of the type-specifier. For a
12341 class-specifier, enum-specifier, or elaborated-type-specifier, a
12342 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12343
12344 The parser flags FLAGS is used to control type-specifier parsing.
12345
12346 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12347 in a decl-specifier-seq.
12348
12349 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12350 class-specifier, enum-specifier, or elaborated-type-specifier, then
12351 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12352 if a type is declared; 2 if it is defined. Otherwise, it is set to
12353 zero.
12354
12355 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12356 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12357 is set to FALSE. */
12358
12359 static tree
12360 cp_parser_type_specifier (cp_parser* parser,
12361 cp_parser_flags flags,
12362 cp_decl_specifier_seq *decl_specs,
12363 bool is_declaration,
12364 int* declares_class_or_enum,
12365 bool* is_cv_qualifier)
12366 {
12367 tree type_spec = NULL_TREE;
12368 cp_token *token;
12369 enum rid keyword;
12370 cp_decl_spec ds = ds_last;
12371
12372 /* Assume this type-specifier does not declare a new type. */
12373 if (declares_class_or_enum)
12374 *declares_class_or_enum = 0;
12375 /* And that it does not specify a cv-qualifier. */
12376 if (is_cv_qualifier)
12377 *is_cv_qualifier = false;
12378 /* Peek at the next token. */
12379 token = cp_lexer_peek_token (parser->lexer);
12380
12381 /* If we're looking at a keyword, we can use that to guide the
12382 production we choose. */
12383 keyword = token->keyword;
12384 switch (keyword)
12385 {
12386 case RID_ENUM:
12387 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12388 goto elaborated_type_specifier;
12389
12390 /* Look for the enum-specifier. */
12391 type_spec = cp_parser_enum_specifier (parser);
12392 /* If that worked, we're done. */
12393 if (type_spec)
12394 {
12395 if (declares_class_or_enum)
12396 *declares_class_or_enum = 2;
12397 if (decl_specs)
12398 cp_parser_set_decl_spec_type (decl_specs,
12399 type_spec,
12400 token->location,
12401 /*user_defined_p=*/true);
12402 return type_spec;
12403 }
12404 else
12405 goto elaborated_type_specifier;
12406
12407 /* Any of these indicate either a class-specifier, or an
12408 elaborated-type-specifier. */
12409 case RID_CLASS:
12410 case RID_STRUCT:
12411 case RID_UNION:
12412 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12413 goto elaborated_type_specifier;
12414
12415 /* Parse tentatively so that we can back up if we don't find a
12416 class-specifier. */
12417 cp_parser_parse_tentatively (parser);
12418 /* Look for the class-specifier. */
12419 type_spec = cp_parser_class_specifier (parser);
12420 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12421 /* If that worked, we're done. */
12422 if (cp_parser_parse_definitely (parser))
12423 {
12424 if (declares_class_or_enum)
12425 *declares_class_or_enum = 2;
12426 if (decl_specs)
12427 cp_parser_set_decl_spec_type (decl_specs,
12428 type_spec,
12429 token->location,
12430 /*user_defined_p=*/true);
12431 return type_spec;
12432 }
12433
12434 /* Fall through. */
12435 elaborated_type_specifier:
12436 /* We're declaring (not defining) a class or enum. */
12437 if (declares_class_or_enum)
12438 *declares_class_or_enum = 1;
12439
12440 /* Fall through. */
12441 case RID_TYPENAME:
12442 /* Look for an elaborated-type-specifier. */
12443 type_spec
12444 = (cp_parser_elaborated_type_specifier
12445 (parser,
12446 decl_specs && decl_specs->specs[(int) ds_friend],
12447 is_declaration));
12448 if (decl_specs)
12449 cp_parser_set_decl_spec_type (decl_specs,
12450 type_spec,
12451 token->location,
12452 /*user_defined_p=*/true);
12453 return type_spec;
12454
12455 case RID_CONST:
12456 ds = ds_const;
12457 if (is_cv_qualifier)
12458 *is_cv_qualifier = true;
12459 break;
12460
12461 case RID_VOLATILE:
12462 ds = ds_volatile;
12463 if (is_cv_qualifier)
12464 *is_cv_qualifier = true;
12465 break;
12466
12467 case RID_RESTRICT:
12468 ds = ds_restrict;
12469 if (is_cv_qualifier)
12470 *is_cv_qualifier = true;
12471 break;
12472
12473 case RID_COMPLEX:
12474 /* The `__complex__' keyword is a GNU extension. */
12475 ds = ds_complex;
12476 break;
12477
12478 default:
12479 break;
12480 }
12481
12482 /* Handle simple keywords. */
12483 if (ds != ds_last)
12484 {
12485 if (decl_specs)
12486 {
12487 ++decl_specs->specs[(int)ds];
12488 decl_specs->any_specifiers_p = true;
12489 }
12490 return cp_lexer_consume_token (parser->lexer)->u.value;
12491 }
12492
12493 /* If we do not already have a type-specifier, assume we are looking
12494 at a simple-type-specifier. */
12495 type_spec = cp_parser_simple_type_specifier (parser,
12496 decl_specs,
12497 flags);
12498
12499 /* If we didn't find a type-specifier, and a type-specifier was not
12500 optional in this context, issue an error message. */
12501 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12502 {
12503 cp_parser_error (parser, "expected type specifier");
12504 return error_mark_node;
12505 }
12506
12507 return type_spec;
12508 }
12509
12510 /* Parse a simple-type-specifier.
12511
12512 simple-type-specifier:
12513 :: [opt] nested-name-specifier [opt] type-name
12514 :: [opt] nested-name-specifier template template-id
12515 char
12516 wchar_t
12517 bool
12518 short
12519 int
12520 long
12521 signed
12522 unsigned
12523 float
12524 double
12525 void
12526
12527 C++0x Extension:
12528
12529 simple-type-specifier:
12530 auto
12531 decltype ( expression )
12532 char16_t
12533 char32_t
12534 __underlying_type ( type-id )
12535
12536 GNU Extension:
12537
12538 simple-type-specifier:
12539 __int128
12540 __typeof__ unary-expression
12541 __typeof__ ( type-id )
12542
12543 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12544 appropriately updated. */
12545
12546 static tree
12547 cp_parser_simple_type_specifier (cp_parser* parser,
12548 cp_decl_specifier_seq *decl_specs,
12549 cp_parser_flags flags)
12550 {
12551 tree type = NULL_TREE;
12552 cp_token *token;
12553
12554 /* Peek at the next token. */
12555 token = cp_lexer_peek_token (parser->lexer);
12556
12557 /* If we're looking at a keyword, things are easy. */
12558 switch (token->keyword)
12559 {
12560 case RID_CHAR:
12561 if (decl_specs)
12562 decl_specs->explicit_char_p = true;
12563 type = char_type_node;
12564 break;
12565 case RID_CHAR16:
12566 type = char16_type_node;
12567 break;
12568 case RID_CHAR32:
12569 type = char32_type_node;
12570 break;
12571 case RID_WCHAR:
12572 type = wchar_type_node;
12573 break;
12574 case RID_BOOL:
12575 type = boolean_type_node;
12576 break;
12577 case RID_SHORT:
12578 if (decl_specs)
12579 ++decl_specs->specs[(int) ds_short];
12580 type = short_integer_type_node;
12581 break;
12582 case RID_INT:
12583 if (decl_specs)
12584 decl_specs->explicit_int_p = true;
12585 type = integer_type_node;
12586 break;
12587 case RID_INT128:
12588 if (!int128_integer_type_node)
12589 break;
12590 if (decl_specs)
12591 decl_specs->explicit_int128_p = true;
12592 type = int128_integer_type_node;
12593 break;
12594 case RID_LONG:
12595 if (decl_specs)
12596 ++decl_specs->specs[(int) ds_long];
12597 type = long_integer_type_node;
12598 break;
12599 case RID_SIGNED:
12600 if (decl_specs)
12601 ++decl_specs->specs[(int) ds_signed];
12602 type = integer_type_node;
12603 break;
12604 case RID_UNSIGNED:
12605 if (decl_specs)
12606 ++decl_specs->specs[(int) ds_unsigned];
12607 type = unsigned_type_node;
12608 break;
12609 case RID_FLOAT:
12610 type = float_type_node;
12611 break;
12612 case RID_DOUBLE:
12613 type = double_type_node;
12614 break;
12615 case RID_VOID:
12616 type = void_type_node;
12617 break;
12618
12619 case RID_AUTO:
12620 maybe_warn_cpp0x (CPP0X_AUTO);
12621 type = make_auto ();
12622 break;
12623
12624 case RID_DECLTYPE:
12625 /* Parse the `decltype' type. */
12626 type = cp_parser_decltype (parser);
12627
12628 if (decl_specs)
12629 cp_parser_set_decl_spec_type (decl_specs, type,
12630 token->location,
12631 /*user_defined_p=*/true);
12632
12633 return type;
12634
12635 case RID_TYPEOF:
12636 /* Consume the `typeof' token. */
12637 cp_lexer_consume_token (parser->lexer);
12638 /* Parse the operand to `typeof'. */
12639 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12640 /* If it is not already a TYPE, take its type. */
12641 if (!TYPE_P (type))
12642 type = finish_typeof (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 case RID_UNDERLYING_TYPE:
12652 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12653
12654 if (decl_specs)
12655 cp_parser_set_decl_spec_type (decl_specs, type,
12656 token->location,
12657 /*user_defined_p=*/true);
12658
12659 return type;
12660
12661 default:
12662 break;
12663 }
12664
12665 /* If the type-specifier was for a built-in type, we're done. */
12666 if (type)
12667 {
12668 /* Record the type. */
12669 if (decl_specs
12670 && (token->keyword != RID_SIGNED
12671 && token->keyword != RID_UNSIGNED
12672 && token->keyword != RID_SHORT
12673 && token->keyword != RID_LONG))
12674 cp_parser_set_decl_spec_type (decl_specs,
12675 type,
12676 token->location,
12677 /*user_defined=*/false);
12678 if (decl_specs)
12679 decl_specs->any_specifiers_p = true;
12680
12681 /* Consume the token. */
12682 cp_lexer_consume_token (parser->lexer);
12683
12684 /* There is no valid C++ program where a non-template type is
12685 followed by a "<". That usually indicates that the user thought
12686 that the type was a template. */
12687 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12688
12689 return TYPE_NAME (type);
12690 }
12691
12692 /* The type-specifier must be a user-defined type. */
12693 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12694 {
12695 bool qualified_p;
12696 bool global_p;
12697
12698 /* Don't gobble tokens or issue error messages if this is an
12699 optional type-specifier. */
12700 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12701 cp_parser_parse_tentatively (parser);
12702
12703 /* Look for the optional `::' operator. */
12704 global_p
12705 = (cp_parser_global_scope_opt (parser,
12706 /*current_scope_valid_p=*/false)
12707 != NULL_TREE);
12708 /* Look for the nested-name specifier. */
12709 qualified_p
12710 = (cp_parser_nested_name_specifier_opt (parser,
12711 /*typename_keyword_p=*/false,
12712 /*check_dependency_p=*/true,
12713 /*type_p=*/false,
12714 /*is_declaration=*/false)
12715 != NULL_TREE);
12716 token = cp_lexer_peek_token (parser->lexer);
12717 /* If we have seen a nested-name-specifier, and the next token
12718 is `template', then we are using the template-id production. */
12719 if (parser->scope
12720 && cp_parser_optional_template_keyword (parser))
12721 {
12722 /* Look for the template-id. */
12723 type = cp_parser_template_id (parser,
12724 /*template_keyword_p=*/true,
12725 /*check_dependency_p=*/true,
12726 /*is_declaration=*/false);
12727 /* If the template-id did not name a type, we are out of
12728 luck. */
12729 if (TREE_CODE (type) != TYPE_DECL)
12730 {
12731 cp_parser_error (parser, "expected template-id for type");
12732 type = NULL_TREE;
12733 }
12734 }
12735 /* Otherwise, look for a type-name. */
12736 else
12737 type = cp_parser_type_name (parser);
12738 /* Keep track of all name-lookups performed in class scopes. */
12739 if (type
12740 && !global_p
12741 && !qualified_p
12742 && TREE_CODE (type) == TYPE_DECL
12743 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12744 maybe_note_name_used_in_class (DECL_NAME (type), type);
12745 /* If it didn't work out, we don't have a TYPE. */
12746 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12747 && !cp_parser_parse_definitely (parser))
12748 type = NULL_TREE;
12749 if (type && decl_specs)
12750 cp_parser_set_decl_spec_type (decl_specs, type,
12751 token->location,
12752 /*user_defined=*/true);
12753 }
12754
12755 /* If we didn't get a type-name, issue an error message. */
12756 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12757 {
12758 cp_parser_error (parser, "expected type-name");
12759 return error_mark_node;
12760 }
12761
12762 if (type && type != error_mark_node)
12763 {
12764 /* See if TYPE is an Objective-C type, and if so, parse and
12765 accept any protocol references following it. Do this before
12766 the cp_parser_check_for_invalid_template_id() call, because
12767 Objective-C types can be followed by '<...>' which would
12768 enclose protocol names rather than template arguments, and so
12769 everything is fine. */
12770 if (c_dialect_objc () && !parser->scope
12771 && (objc_is_id (type) || objc_is_class_name (type)))
12772 {
12773 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12774 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12775
12776 /* Clobber the "unqualified" type previously entered into
12777 DECL_SPECS with the new, improved protocol-qualified version. */
12778 if (decl_specs)
12779 decl_specs->type = qual_type;
12780
12781 return qual_type;
12782 }
12783
12784 /* There is no valid C++ program where a non-template type is
12785 followed by a "<". That usually indicates that the user
12786 thought that the type was a template. */
12787 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12788 token->location);
12789 }
12790
12791 return type;
12792 }
12793
12794 /* Parse a type-name.
12795
12796 type-name:
12797 class-name
12798 enum-name
12799 typedef-name
12800
12801 enum-name:
12802 identifier
12803
12804 typedef-name:
12805 identifier
12806
12807 Returns a TYPE_DECL for the type. */
12808
12809 static tree
12810 cp_parser_type_name (cp_parser* parser)
12811 {
12812 tree type_decl;
12813
12814 /* We can't know yet whether it is a class-name or not. */
12815 cp_parser_parse_tentatively (parser);
12816 /* Try a class-name. */
12817 type_decl = cp_parser_class_name (parser,
12818 /*typename_keyword_p=*/false,
12819 /*template_keyword_p=*/false,
12820 none_type,
12821 /*check_dependency_p=*/true,
12822 /*class_head_p=*/false,
12823 /*is_declaration=*/false);
12824 /* If it's not a class-name, keep looking. */
12825 if (!cp_parser_parse_definitely (parser))
12826 {
12827 /* It must be a typedef-name or an enum-name. */
12828 return cp_parser_nonclass_name (parser);
12829 }
12830
12831 return type_decl;
12832 }
12833
12834 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12835
12836 enum-name:
12837 identifier
12838
12839 typedef-name:
12840 identifier
12841
12842 Returns a TYPE_DECL for the type. */
12843
12844 static tree
12845 cp_parser_nonclass_name (cp_parser* parser)
12846 {
12847 tree type_decl;
12848 tree identifier;
12849
12850 cp_token *token = cp_lexer_peek_token (parser->lexer);
12851 identifier = cp_parser_identifier (parser);
12852 if (identifier == error_mark_node)
12853 return error_mark_node;
12854
12855 /* Look up the type-name. */
12856 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12857
12858 if (TREE_CODE (type_decl) != TYPE_DECL
12859 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12860 {
12861 /* See if this is an Objective-C type. */
12862 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12863 tree type = objc_get_protocol_qualified_type (identifier, protos);
12864 if (type)
12865 type_decl = TYPE_NAME (type);
12866 }
12867
12868 /* Issue an error if we did not find a type-name. */
12869 if (TREE_CODE (type_decl) != TYPE_DECL
12870 /* In Objective-C, we have the complication that class names are
12871 normally type names and start declarations (eg, the
12872 "NSObject" in "NSObject *object;"), but can be used in an
12873 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12874 is an expression. So, a classname followed by a dot is not a
12875 valid type-name. */
12876 || (objc_is_class_name (TREE_TYPE (type_decl))
12877 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12878 {
12879 if (!cp_parser_simulate_error (parser))
12880 cp_parser_name_lookup_error (parser, identifier, type_decl,
12881 NLE_TYPE, token->location);
12882 return error_mark_node;
12883 }
12884 /* Remember that the name was used in the definition of the
12885 current class so that we can check later to see if the
12886 meaning would have been different after the class was
12887 entirely defined. */
12888 else if (type_decl != error_mark_node
12889 && !parser->scope)
12890 maybe_note_name_used_in_class (identifier, type_decl);
12891
12892 return type_decl;
12893 }
12894
12895 /* Parse an elaborated-type-specifier. Note that the grammar given
12896 here incorporates the resolution to DR68.
12897
12898 elaborated-type-specifier:
12899 class-key :: [opt] nested-name-specifier [opt] identifier
12900 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12901 enum-key :: [opt] nested-name-specifier [opt] identifier
12902 typename :: [opt] nested-name-specifier identifier
12903 typename :: [opt] nested-name-specifier template [opt]
12904 template-id
12905
12906 GNU extension:
12907
12908 elaborated-type-specifier:
12909 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12910 class-key attributes :: [opt] nested-name-specifier [opt]
12911 template [opt] template-id
12912 enum attributes :: [opt] nested-name-specifier [opt] identifier
12913
12914 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12915 declared `friend'. If IS_DECLARATION is TRUE, then this
12916 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12917 something is being declared.
12918
12919 Returns the TYPE specified. */
12920
12921 static tree
12922 cp_parser_elaborated_type_specifier (cp_parser* parser,
12923 bool is_friend,
12924 bool is_declaration)
12925 {
12926 enum tag_types tag_type;
12927 tree identifier;
12928 tree type = NULL_TREE;
12929 tree attributes = NULL_TREE;
12930 tree globalscope;
12931 cp_token *token = NULL;
12932
12933 /* See if we're looking at the `enum' keyword. */
12934 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12935 {
12936 /* Consume the `enum' token. */
12937 cp_lexer_consume_token (parser->lexer);
12938 /* Remember that it's an enumeration type. */
12939 tag_type = enum_type;
12940 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12941 enums) is used here. */
12942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12943 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12944 {
12945 pedwarn (input_location, 0, "elaborated-type-specifier "
12946 "for a scoped enum must not use the %<%D%> keyword",
12947 cp_lexer_peek_token (parser->lexer)->u.value);
12948 /* Consume the `struct' or `class' and parse it anyway. */
12949 cp_lexer_consume_token (parser->lexer);
12950 }
12951 /* Parse the attributes. */
12952 attributes = cp_parser_attributes_opt (parser);
12953 }
12954 /* Or, it might be `typename'. */
12955 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12956 RID_TYPENAME))
12957 {
12958 /* Consume the `typename' token. */
12959 cp_lexer_consume_token (parser->lexer);
12960 /* Remember that it's a `typename' type. */
12961 tag_type = typename_type;
12962 }
12963 /* Otherwise it must be a class-key. */
12964 else
12965 {
12966 tag_type = cp_parser_class_key (parser);
12967 if (tag_type == none_type)
12968 return error_mark_node;
12969 /* Parse the attributes. */
12970 attributes = cp_parser_attributes_opt (parser);
12971 }
12972
12973 /* Look for the `::' operator. */
12974 globalscope = cp_parser_global_scope_opt (parser,
12975 /*current_scope_valid_p=*/false);
12976 /* Look for the nested-name-specifier. */
12977 if (tag_type == typename_type && !globalscope)
12978 {
12979 if (!cp_parser_nested_name_specifier (parser,
12980 /*typename_keyword_p=*/true,
12981 /*check_dependency_p=*/true,
12982 /*type_p=*/true,
12983 is_declaration))
12984 return error_mark_node;
12985 }
12986 else
12987 /* Even though `typename' is not present, the proposed resolution
12988 to Core Issue 180 says that in `class A<T>::B', `B' should be
12989 considered a type-name, even if `A<T>' is dependent. */
12990 cp_parser_nested_name_specifier_opt (parser,
12991 /*typename_keyword_p=*/true,
12992 /*check_dependency_p=*/true,
12993 /*type_p=*/true,
12994 is_declaration);
12995 /* For everything but enumeration types, consider a template-id.
12996 For an enumeration type, consider only a plain identifier. */
12997 if (tag_type != enum_type)
12998 {
12999 bool template_p = false;
13000 tree decl;
13001
13002 /* Allow the `template' keyword. */
13003 template_p = cp_parser_optional_template_keyword (parser);
13004 /* If we didn't see `template', we don't know if there's a
13005 template-id or not. */
13006 if (!template_p)
13007 cp_parser_parse_tentatively (parser);
13008 /* Parse the template-id. */
13009 token = cp_lexer_peek_token (parser->lexer);
13010 decl = cp_parser_template_id (parser, template_p,
13011 /*check_dependency_p=*/true,
13012 is_declaration);
13013 /* If we didn't find a template-id, look for an ordinary
13014 identifier. */
13015 if (!template_p && !cp_parser_parse_definitely (parser))
13016 ;
13017 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13018 in effect, then we must assume that, upon instantiation, the
13019 template will correspond to a class. */
13020 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13021 && tag_type == typename_type)
13022 type = make_typename_type (parser->scope, decl,
13023 typename_type,
13024 /*complain=*/tf_error);
13025 /* If the `typename' keyword is in effect and DECL is not a type
13026 decl. Then type is non existant. */
13027 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13028 type = NULL_TREE;
13029 else
13030 type = TREE_TYPE (decl);
13031 }
13032
13033 if (!type)
13034 {
13035 token = cp_lexer_peek_token (parser->lexer);
13036 identifier = cp_parser_identifier (parser);
13037
13038 if (identifier == error_mark_node)
13039 {
13040 parser->scope = NULL_TREE;
13041 return error_mark_node;
13042 }
13043
13044 /* For a `typename', we needn't call xref_tag. */
13045 if (tag_type == typename_type
13046 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13047 return cp_parser_make_typename_type (parser, parser->scope,
13048 identifier,
13049 token->location);
13050 /* Look up a qualified name in the usual way. */
13051 if (parser->scope)
13052 {
13053 tree decl;
13054 tree ambiguous_decls;
13055
13056 decl = cp_parser_lookup_name (parser, identifier,
13057 tag_type,
13058 /*is_template=*/false,
13059 /*is_namespace=*/false,
13060 /*check_dependency=*/true,
13061 &ambiguous_decls,
13062 token->location);
13063
13064 /* If the lookup was ambiguous, an error will already have been
13065 issued. */
13066 if (ambiguous_decls)
13067 return error_mark_node;
13068
13069 /* If we are parsing friend declaration, DECL may be a
13070 TEMPLATE_DECL tree node here. However, we need to check
13071 whether this TEMPLATE_DECL results in valid code. Consider
13072 the following example:
13073
13074 namespace N {
13075 template <class T> class C {};
13076 }
13077 class X {
13078 template <class T> friend class N::C; // #1, valid code
13079 };
13080 template <class T> class Y {
13081 friend class N::C; // #2, invalid code
13082 };
13083
13084 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13085 name lookup of `N::C'. We see that friend declaration must
13086 be template for the code to be valid. Note that
13087 processing_template_decl does not work here since it is
13088 always 1 for the above two cases. */
13089
13090 decl = (cp_parser_maybe_treat_template_as_class
13091 (decl, /*tag_name_p=*/is_friend
13092 && parser->num_template_parameter_lists));
13093
13094 if (TREE_CODE (decl) != TYPE_DECL)
13095 {
13096 cp_parser_diagnose_invalid_type_name (parser,
13097 parser->scope,
13098 identifier,
13099 token->location);
13100 return error_mark_node;
13101 }
13102
13103 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13104 {
13105 bool allow_template = (parser->num_template_parameter_lists
13106 || DECL_SELF_REFERENCE_P (decl));
13107 type = check_elaborated_type_specifier (tag_type, decl,
13108 allow_template);
13109
13110 if (type == error_mark_node)
13111 return error_mark_node;
13112 }
13113
13114 /* Forward declarations of nested types, such as
13115
13116 class C1::C2;
13117 class C1::C2::C3;
13118
13119 are invalid unless all components preceding the final '::'
13120 are complete. If all enclosing types are complete, these
13121 declarations become merely pointless.
13122
13123 Invalid forward declarations of nested types are errors
13124 caught elsewhere in parsing. Those that are pointless arrive
13125 here. */
13126
13127 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13128 && !is_friend && !processing_explicit_instantiation)
13129 warning (0, "declaration %qD does not declare anything", decl);
13130
13131 type = TREE_TYPE (decl);
13132 }
13133 else
13134 {
13135 /* An elaborated-type-specifier sometimes introduces a new type and
13136 sometimes names an existing type. Normally, the rule is that it
13137 introduces a new type only if there is not an existing type of
13138 the same name already in scope. For example, given:
13139
13140 struct S {};
13141 void f() { struct S s; }
13142
13143 the `struct S' in the body of `f' is the same `struct S' as in
13144 the global scope; the existing definition is used. However, if
13145 there were no global declaration, this would introduce a new
13146 local class named `S'.
13147
13148 An exception to this rule applies to the following code:
13149
13150 namespace N { struct S; }
13151
13152 Here, the elaborated-type-specifier names a new type
13153 unconditionally; even if there is already an `S' in the
13154 containing scope this declaration names a new type.
13155 This exception only applies if the elaborated-type-specifier
13156 forms the complete declaration:
13157
13158 [class.name]
13159
13160 A declaration consisting solely of `class-key identifier ;' is
13161 either a redeclaration of the name in the current scope or a
13162 forward declaration of the identifier as a class name. It
13163 introduces the name into the current scope.
13164
13165 We are in this situation precisely when the next token is a `;'.
13166
13167 An exception to the exception is that a `friend' declaration does
13168 *not* name a new type; i.e., given:
13169
13170 struct S { friend struct T; };
13171
13172 `T' is not a new type in the scope of `S'.
13173
13174 Also, `new struct S' or `sizeof (struct S)' never results in the
13175 definition of a new type; a new type can only be declared in a
13176 declaration context. */
13177
13178 tag_scope ts;
13179 bool template_p;
13180
13181 if (is_friend)
13182 /* Friends have special name lookup rules. */
13183 ts = ts_within_enclosing_non_class;
13184 else if (is_declaration
13185 && cp_lexer_next_token_is (parser->lexer,
13186 CPP_SEMICOLON))
13187 /* This is a `class-key identifier ;' */
13188 ts = ts_current;
13189 else
13190 ts = ts_global;
13191
13192 template_p =
13193 (parser->num_template_parameter_lists
13194 && (cp_parser_next_token_starts_class_definition_p (parser)
13195 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13196 /* An unqualified name was used to reference this type, so
13197 there were no qualifying templates. */
13198 if (!cp_parser_check_template_parameters (parser,
13199 /*num_templates=*/0,
13200 token->location,
13201 /*declarator=*/NULL))
13202 return error_mark_node;
13203 type = xref_tag (tag_type, identifier, ts, template_p);
13204 }
13205 }
13206
13207 if (type == error_mark_node)
13208 return error_mark_node;
13209
13210 /* Allow attributes on forward declarations of classes. */
13211 if (attributes)
13212 {
13213 if (TREE_CODE (type) == TYPENAME_TYPE)
13214 warning (OPT_Wattributes,
13215 "attributes ignored on uninstantiated type");
13216 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13217 && ! processing_explicit_instantiation)
13218 warning (OPT_Wattributes,
13219 "attributes ignored on template instantiation");
13220 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13221 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13222 else
13223 warning (OPT_Wattributes,
13224 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13225 }
13226
13227 if (tag_type != enum_type)
13228 cp_parser_check_class_key (tag_type, type);
13229
13230 /* A "<" cannot follow an elaborated type specifier. If that
13231 happens, the user was probably trying to form a template-id. */
13232 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13233
13234 return type;
13235 }
13236
13237 /* Parse an enum-specifier.
13238
13239 enum-specifier:
13240 enum-head { enumerator-list [opt] }
13241
13242 enum-head:
13243 enum-key identifier [opt] enum-base [opt]
13244 enum-key nested-name-specifier identifier enum-base [opt]
13245
13246 enum-key:
13247 enum
13248 enum class [C++0x]
13249 enum struct [C++0x]
13250
13251 enum-base: [C++0x]
13252 : type-specifier-seq
13253
13254 opaque-enum-specifier:
13255 enum-key identifier enum-base [opt] ;
13256
13257 GNU Extensions:
13258 enum-key attributes[opt] identifier [opt] enum-base [opt]
13259 { enumerator-list [opt] }attributes[opt]
13260
13261 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13262 if the token stream isn't an enum-specifier after all. */
13263
13264 static tree
13265 cp_parser_enum_specifier (cp_parser* parser)
13266 {
13267 tree identifier;
13268 tree type = NULL_TREE;
13269 tree prev_scope;
13270 tree nested_name_specifier = NULL_TREE;
13271 tree attributes;
13272 bool scoped_enum_p = false;
13273 bool has_underlying_type = false;
13274 bool nested_being_defined = false;
13275 bool new_value_list = false;
13276 bool is_new_type = false;
13277 bool is_anonymous = false;
13278 tree underlying_type = NULL_TREE;
13279 cp_token *type_start_token = NULL;
13280 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13281
13282 parser->colon_corrects_to_scope_p = false;
13283
13284 /* Parse tentatively so that we can back up if we don't find a
13285 enum-specifier. */
13286 cp_parser_parse_tentatively (parser);
13287
13288 /* Caller guarantees that the current token is 'enum', an identifier
13289 possibly follows, and the token after that is an opening brace.
13290 If we don't have an identifier, fabricate an anonymous name for
13291 the enumeration being defined. */
13292 cp_lexer_consume_token (parser->lexer);
13293
13294 /* Parse the "class" or "struct", which indicates a scoped
13295 enumeration type in C++0x. */
13296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13297 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13298 {
13299 if (cxx_dialect < cxx0x)
13300 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13301
13302 /* Consume the `struct' or `class' token. */
13303 cp_lexer_consume_token (parser->lexer);
13304
13305 scoped_enum_p = true;
13306 }
13307
13308 attributes = cp_parser_attributes_opt (parser);
13309
13310 /* Clear the qualification. */
13311 parser->scope = NULL_TREE;
13312 parser->qualifying_scope = NULL_TREE;
13313 parser->object_scope = NULL_TREE;
13314
13315 /* Figure out in what scope the declaration is being placed. */
13316 prev_scope = current_scope ();
13317
13318 type_start_token = cp_lexer_peek_token (parser->lexer);
13319
13320 push_deferring_access_checks (dk_no_check);
13321 nested_name_specifier
13322 = cp_parser_nested_name_specifier_opt (parser,
13323 /*typename_keyword_p=*/true,
13324 /*check_dependency_p=*/false,
13325 /*type_p=*/false,
13326 /*is_declaration=*/false);
13327
13328 if (nested_name_specifier)
13329 {
13330 tree name;
13331
13332 identifier = cp_parser_identifier (parser);
13333 name = cp_parser_lookup_name (parser, identifier,
13334 enum_type,
13335 /*is_template=*/false,
13336 /*is_namespace=*/false,
13337 /*check_dependency=*/true,
13338 /*ambiguous_decls=*/NULL,
13339 input_location);
13340 if (name)
13341 {
13342 type = TREE_TYPE (name);
13343 if (TREE_CODE (type) == TYPENAME_TYPE)
13344 {
13345 /* Are template enums allowed in ISO? */
13346 if (template_parm_scope_p ())
13347 pedwarn (type_start_token->location, OPT_pedantic,
13348 "%qD is an enumeration template", name);
13349 /* ignore a typename reference, for it will be solved by name
13350 in start_enum. */
13351 type = NULL_TREE;
13352 }
13353 }
13354 else
13355 error_at (type_start_token->location,
13356 "%qD is not an enumerator-name", identifier);
13357 }
13358 else
13359 {
13360 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13361 identifier = cp_parser_identifier (parser);
13362 else
13363 {
13364 identifier = make_anon_name ();
13365 is_anonymous = true;
13366 }
13367 }
13368 pop_deferring_access_checks ();
13369
13370 /* Check for the `:' that denotes a specified underlying type in C++0x.
13371 Note that a ':' could also indicate a bitfield width, however. */
13372 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13373 {
13374 cp_decl_specifier_seq type_specifiers;
13375
13376 /* Consume the `:'. */
13377 cp_lexer_consume_token (parser->lexer);
13378
13379 /* Parse the type-specifier-seq. */
13380 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13381 /*is_trailing_return=*/false,
13382 &type_specifiers);
13383
13384 /* At this point this is surely not elaborated type specifier. */
13385 if (!cp_parser_parse_definitely (parser))
13386 return NULL_TREE;
13387
13388 if (cxx_dialect < cxx0x)
13389 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13390
13391 has_underlying_type = true;
13392
13393 /* If that didn't work, stop. */
13394 if (type_specifiers.type != error_mark_node)
13395 {
13396 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13397 /*initialized=*/0, NULL);
13398 if (underlying_type == error_mark_node)
13399 underlying_type = NULL_TREE;
13400 }
13401 }
13402
13403 /* Look for the `{' but don't consume it yet. */
13404 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13405 {
13406 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13407 {
13408 cp_parser_error (parser, "expected %<{%>");
13409 if (has_underlying_type)
13410 {
13411 type = NULL_TREE;
13412 goto out;
13413 }
13414 }
13415 /* An opaque-enum-specifier must have a ';' here. */
13416 if ((scoped_enum_p || underlying_type)
13417 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13418 {
13419 cp_parser_error (parser, "expected %<;%> or %<{%>");
13420 if (has_underlying_type)
13421 {
13422 type = NULL_TREE;
13423 goto out;
13424 }
13425 }
13426 }
13427
13428 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13429 return NULL_TREE;
13430
13431 if (nested_name_specifier)
13432 {
13433 if (CLASS_TYPE_P (nested_name_specifier))
13434 {
13435 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13436 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13437 push_scope (nested_name_specifier);
13438 }
13439 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13440 {
13441 push_nested_namespace (nested_name_specifier);
13442 }
13443 }
13444
13445 /* Issue an error message if type-definitions are forbidden here. */
13446 if (!cp_parser_check_type_definition (parser))
13447 type = error_mark_node;
13448 else
13449 /* Create the new type. We do this before consuming the opening
13450 brace so the enum will be recorded as being on the line of its
13451 tag (or the 'enum' keyword, if there is no tag). */
13452 type = start_enum (identifier, type, underlying_type,
13453 scoped_enum_p, &is_new_type);
13454
13455 /* If the next token is not '{' it is an opaque-enum-specifier or an
13456 elaborated-type-specifier. */
13457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13458 {
13459 timevar_push (TV_PARSE_ENUM);
13460 if (nested_name_specifier)
13461 {
13462 /* The following catches invalid code such as:
13463 enum class S<int>::E { A, B, C }; */
13464 if (!processing_specialization
13465 && CLASS_TYPE_P (nested_name_specifier)
13466 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13467 error_at (type_start_token->location, "cannot add an enumerator "
13468 "list to a template instantiation");
13469
13470 /* If that scope does not contain the scope in which the
13471 class was originally declared, the program is invalid. */
13472 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13473 {
13474 if (at_namespace_scope_p ())
13475 error_at (type_start_token->location,
13476 "declaration of %qD in namespace %qD which does not "
13477 "enclose %qD",
13478 type, prev_scope, nested_name_specifier);
13479 else
13480 error_at (type_start_token->location,
13481 "declaration of %qD in %qD which does not enclose %qD",
13482 type, prev_scope, nested_name_specifier);
13483 type = error_mark_node;
13484 }
13485 }
13486
13487 if (scoped_enum_p)
13488 begin_scope (sk_scoped_enum, type);
13489
13490 /* Consume the opening brace. */
13491 cp_lexer_consume_token (parser->lexer);
13492
13493 if (type == error_mark_node)
13494 ; /* Nothing to add */
13495 else if (OPAQUE_ENUM_P (type)
13496 || (cxx_dialect > cxx98 && processing_specialization))
13497 {
13498 new_value_list = true;
13499 SET_OPAQUE_ENUM_P (type, false);
13500 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13501 }
13502 else
13503 {
13504 error_at (type_start_token->location, "multiple definition of %q#T", type);
13505 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13506 "previous definition here");
13507 type = error_mark_node;
13508 }
13509
13510 if (type == error_mark_node)
13511 cp_parser_skip_to_end_of_block_or_statement (parser);
13512 /* If the next token is not '}', then there are some enumerators. */
13513 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13514 cp_parser_enumerator_list (parser, type);
13515
13516 /* Consume the final '}'. */
13517 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13518
13519 if (scoped_enum_p)
13520 finish_scope ();
13521 timevar_pop (TV_PARSE_ENUM);
13522 }
13523 else
13524 {
13525 /* If a ';' follows, then it is an opaque-enum-specifier
13526 and additional restrictions apply. */
13527 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13528 {
13529 if (is_anonymous)
13530 error_at (type_start_token->location,
13531 "opaque-enum-specifier without name");
13532 else if (nested_name_specifier)
13533 error_at (type_start_token->location,
13534 "opaque-enum-specifier must use a simple identifier");
13535 }
13536 }
13537
13538 /* Look for trailing attributes to apply to this enumeration, and
13539 apply them if appropriate. */
13540 if (cp_parser_allow_gnu_extensions_p (parser))
13541 {
13542 tree trailing_attr = cp_parser_attributes_opt (parser);
13543 trailing_attr = chainon (trailing_attr, attributes);
13544 cplus_decl_attributes (&type,
13545 trailing_attr,
13546 (int) ATTR_FLAG_TYPE_IN_PLACE);
13547 }
13548
13549 /* Finish up the enumeration. */
13550 if (type != error_mark_node)
13551 {
13552 if (new_value_list)
13553 finish_enum_value_list (type);
13554 if (is_new_type)
13555 finish_enum (type);
13556 }
13557
13558 if (nested_name_specifier)
13559 {
13560 if (CLASS_TYPE_P (nested_name_specifier))
13561 {
13562 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13563 pop_scope (nested_name_specifier);
13564 }
13565 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13566 {
13567 pop_nested_namespace (nested_name_specifier);
13568 }
13569 }
13570 out:
13571 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13572 return type;
13573 }
13574
13575 /* Parse an enumerator-list. The enumerators all have the indicated
13576 TYPE.
13577
13578 enumerator-list:
13579 enumerator-definition
13580 enumerator-list , enumerator-definition */
13581
13582 static void
13583 cp_parser_enumerator_list (cp_parser* parser, tree type)
13584 {
13585 while (true)
13586 {
13587 /* Parse an enumerator-definition. */
13588 cp_parser_enumerator_definition (parser, type);
13589
13590 /* If the next token is not a ',', we've reached the end of
13591 the list. */
13592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13593 break;
13594 /* Otherwise, consume the `,' and keep going. */
13595 cp_lexer_consume_token (parser->lexer);
13596 /* If the next token is a `}', there is a trailing comma. */
13597 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13598 {
13599 if (!in_system_header)
13600 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13601 break;
13602 }
13603 }
13604 }
13605
13606 /* Parse an enumerator-definition. The enumerator has the indicated
13607 TYPE.
13608
13609 enumerator-definition:
13610 enumerator
13611 enumerator = constant-expression
13612
13613 enumerator:
13614 identifier */
13615
13616 static void
13617 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13618 {
13619 tree identifier;
13620 tree value;
13621 location_t loc;
13622
13623 /* Save the input location because we are interested in the location
13624 of the identifier and not the location of the explicit value. */
13625 loc = cp_lexer_peek_token (parser->lexer)->location;
13626
13627 /* Look for the identifier. */
13628 identifier = cp_parser_identifier (parser);
13629 if (identifier == error_mark_node)
13630 return;
13631
13632 /* If the next token is an '=', then there is an explicit value. */
13633 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13634 {
13635 /* Consume the `=' token. */
13636 cp_lexer_consume_token (parser->lexer);
13637 /* Parse the value. */
13638 value = cp_parser_constant_expression (parser,
13639 /*allow_non_constant_p=*/false,
13640 NULL);
13641 }
13642 else
13643 value = NULL_TREE;
13644
13645 /* If we are processing a template, make sure the initializer of the
13646 enumerator doesn't contain any bare template parameter pack. */
13647 if (check_for_bare_parameter_packs (value))
13648 value = error_mark_node;
13649
13650 /* integral_constant_value will pull out this expression, so make sure
13651 it's folded as appropriate. */
13652 value = fold_non_dependent_expr (value);
13653
13654 /* Create the enumerator. */
13655 build_enumerator (identifier, value, type, loc);
13656 }
13657
13658 /* Parse a namespace-name.
13659
13660 namespace-name:
13661 original-namespace-name
13662 namespace-alias
13663
13664 Returns the NAMESPACE_DECL for the namespace. */
13665
13666 static tree
13667 cp_parser_namespace_name (cp_parser* parser)
13668 {
13669 tree identifier;
13670 tree namespace_decl;
13671
13672 cp_token *token = cp_lexer_peek_token (parser->lexer);
13673
13674 /* Get the name of the namespace. */
13675 identifier = cp_parser_identifier (parser);
13676 if (identifier == error_mark_node)
13677 return error_mark_node;
13678
13679 /* Look up the identifier in the currently active scope. Look only
13680 for namespaces, due to:
13681
13682 [basic.lookup.udir]
13683
13684 When looking up a namespace-name in a using-directive or alias
13685 definition, only namespace names are considered.
13686
13687 And:
13688
13689 [basic.lookup.qual]
13690
13691 During the lookup of a name preceding the :: scope resolution
13692 operator, object, function, and enumerator names are ignored.
13693
13694 (Note that cp_parser_qualifying_entity only calls this
13695 function if the token after the name is the scope resolution
13696 operator.) */
13697 namespace_decl = cp_parser_lookup_name (parser, identifier,
13698 none_type,
13699 /*is_template=*/false,
13700 /*is_namespace=*/true,
13701 /*check_dependency=*/true,
13702 /*ambiguous_decls=*/NULL,
13703 token->location);
13704 /* If it's not a namespace, issue an error. */
13705 if (namespace_decl == error_mark_node
13706 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13707 {
13708 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13709 error_at (token->location, "%qD is not a namespace-name", identifier);
13710 cp_parser_error (parser, "expected namespace-name");
13711 namespace_decl = error_mark_node;
13712 }
13713
13714 return namespace_decl;
13715 }
13716
13717 /* Parse a namespace-definition.
13718
13719 namespace-definition:
13720 named-namespace-definition
13721 unnamed-namespace-definition
13722
13723 named-namespace-definition:
13724 original-namespace-definition
13725 extension-namespace-definition
13726
13727 original-namespace-definition:
13728 namespace identifier { namespace-body }
13729
13730 extension-namespace-definition:
13731 namespace original-namespace-name { namespace-body }
13732
13733 unnamed-namespace-definition:
13734 namespace { namespace-body } */
13735
13736 static void
13737 cp_parser_namespace_definition (cp_parser* parser)
13738 {
13739 tree identifier, attribs;
13740 bool has_visibility;
13741 bool is_inline;
13742
13743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13744 {
13745 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13746 is_inline = true;
13747 cp_lexer_consume_token (parser->lexer);
13748 }
13749 else
13750 is_inline = false;
13751
13752 /* Look for the `namespace' keyword. */
13753 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13754
13755 /* Get the name of the namespace. We do not attempt to distinguish
13756 between an original-namespace-definition and an
13757 extension-namespace-definition at this point. The semantic
13758 analysis routines are responsible for that. */
13759 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13760 identifier = cp_parser_identifier (parser);
13761 else
13762 identifier = NULL_TREE;
13763
13764 /* Parse any specified attributes. */
13765 attribs = cp_parser_attributes_opt (parser);
13766
13767 /* Look for the `{' to start the namespace. */
13768 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13769 /* Start the namespace. */
13770 push_namespace (identifier);
13771
13772 /* "inline namespace" is equivalent to a stub namespace definition
13773 followed by a strong using directive. */
13774 if (is_inline)
13775 {
13776 tree name_space = current_namespace;
13777 /* Set up namespace association. */
13778 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13779 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13780 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13781 /* Import the contents of the inline namespace. */
13782 pop_namespace ();
13783 do_using_directive (name_space);
13784 push_namespace (identifier);
13785 }
13786
13787 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13788
13789 /* Parse the body of the namespace. */
13790 cp_parser_namespace_body (parser);
13791
13792 if (has_visibility)
13793 pop_visibility (1);
13794
13795 /* Finish the namespace. */
13796 pop_namespace ();
13797 /* Look for the final `}'. */
13798 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13799 }
13800
13801 /* Parse a namespace-body.
13802
13803 namespace-body:
13804 declaration-seq [opt] */
13805
13806 static void
13807 cp_parser_namespace_body (cp_parser* parser)
13808 {
13809 cp_parser_declaration_seq_opt (parser);
13810 }
13811
13812 /* Parse a namespace-alias-definition.
13813
13814 namespace-alias-definition:
13815 namespace identifier = qualified-namespace-specifier ; */
13816
13817 static void
13818 cp_parser_namespace_alias_definition (cp_parser* parser)
13819 {
13820 tree identifier;
13821 tree namespace_specifier;
13822
13823 cp_token *token = cp_lexer_peek_token (parser->lexer);
13824
13825 /* Look for the `namespace' keyword. */
13826 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13827 /* Look for the identifier. */
13828 identifier = cp_parser_identifier (parser);
13829 if (identifier == error_mark_node)
13830 return;
13831 /* Look for the `=' token. */
13832 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13833 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13834 {
13835 error_at (token->location, "%<namespace%> definition is not allowed here");
13836 /* Skip the definition. */
13837 cp_lexer_consume_token (parser->lexer);
13838 if (cp_parser_skip_to_closing_brace (parser))
13839 cp_lexer_consume_token (parser->lexer);
13840 return;
13841 }
13842 cp_parser_require (parser, CPP_EQ, RT_EQ);
13843 /* Look for the qualified-namespace-specifier. */
13844 namespace_specifier
13845 = cp_parser_qualified_namespace_specifier (parser);
13846 /* Look for the `;' token. */
13847 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13848
13849 /* Register the alias in the symbol table. */
13850 do_namespace_alias (identifier, namespace_specifier);
13851 }
13852
13853 /* Parse a qualified-namespace-specifier.
13854
13855 qualified-namespace-specifier:
13856 :: [opt] nested-name-specifier [opt] namespace-name
13857
13858 Returns a NAMESPACE_DECL corresponding to the specified
13859 namespace. */
13860
13861 static tree
13862 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13863 {
13864 /* Look for the optional `::'. */
13865 cp_parser_global_scope_opt (parser,
13866 /*current_scope_valid_p=*/false);
13867
13868 /* Look for the optional nested-name-specifier. */
13869 cp_parser_nested_name_specifier_opt (parser,
13870 /*typename_keyword_p=*/false,
13871 /*check_dependency_p=*/true,
13872 /*type_p=*/false,
13873 /*is_declaration=*/true);
13874
13875 return cp_parser_namespace_name (parser);
13876 }
13877
13878 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13879 access declaration.
13880
13881 using-declaration:
13882 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13883 using :: unqualified-id ;
13884
13885 access-declaration:
13886 qualified-id ;
13887
13888 */
13889
13890 static bool
13891 cp_parser_using_declaration (cp_parser* parser,
13892 bool access_declaration_p)
13893 {
13894 cp_token *token;
13895 bool typename_p = false;
13896 bool global_scope_p;
13897 tree decl;
13898 tree identifier;
13899 tree qscope;
13900
13901 if (access_declaration_p)
13902 cp_parser_parse_tentatively (parser);
13903 else
13904 {
13905 /* Look for the `using' keyword. */
13906 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13907
13908 /* Peek at the next token. */
13909 token = cp_lexer_peek_token (parser->lexer);
13910 /* See if it's `typename'. */
13911 if (token->keyword == RID_TYPENAME)
13912 {
13913 /* Remember that we've seen it. */
13914 typename_p = true;
13915 /* Consume the `typename' token. */
13916 cp_lexer_consume_token (parser->lexer);
13917 }
13918 }
13919
13920 /* Look for the optional global scope qualification. */
13921 global_scope_p
13922 = (cp_parser_global_scope_opt (parser,
13923 /*current_scope_valid_p=*/false)
13924 != NULL_TREE);
13925
13926 /* If we saw `typename', or didn't see `::', then there must be a
13927 nested-name-specifier present. */
13928 if (typename_p || !global_scope_p)
13929 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13930 /*check_dependency_p=*/true,
13931 /*type_p=*/false,
13932 /*is_declaration=*/true);
13933 /* Otherwise, we could be in either of the two productions. In that
13934 case, treat the nested-name-specifier as optional. */
13935 else
13936 qscope = cp_parser_nested_name_specifier_opt (parser,
13937 /*typename_keyword_p=*/false,
13938 /*check_dependency_p=*/true,
13939 /*type_p=*/false,
13940 /*is_declaration=*/true);
13941 if (!qscope)
13942 qscope = global_namespace;
13943
13944 if (access_declaration_p && cp_parser_error_occurred (parser))
13945 /* Something has already gone wrong; there's no need to parse
13946 further. Since an error has occurred, the return value of
13947 cp_parser_parse_definitely will be false, as required. */
13948 return cp_parser_parse_definitely (parser);
13949
13950 token = cp_lexer_peek_token (parser->lexer);
13951 /* Parse the unqualified-id. */
13952 identifier = cp_parser_unqualified_id (parser,
13953 /*template_keyword_p=*/false,
13954 /*check_dependency_p=*/true,
13955 /*declarator_p=*/true,
13956 /*optional_p=*/false);
13957
13958 if (access_declaration_p)
13959 {
13960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13961 cp_parser_simulate_error (parser);
13962 if (!cp_parser_parse_definitely (parser))
13963 return false;
13964 }
13965
13966 /* The function we call to handle a using-declaration is different
13967 depending on what scope we are in. */
13968 if (qscope == error_mark_node || identifier == error_mark_node)
13969 ;
13970 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13971 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13972 /* [namespace.udecl]
13973
13974 A using declaration shall not name a template-id. */
13975 error_at (token->location,
13976 "a template-id may not appear in a using-declaration");
13977 else
13978 {
13979 if (at_class_scope_p ())
13980 {
13981 /* Create the USING_DECL. */
13982 decl = do_class_using_decl (parser->scope, identifier);
13983
13984 if (check_for_bare_parameter_packs (decl))
13985 return false;
13986 else
13987 /* Add it to the list of members in this class. */
13988 finish_member_declaration (decl);
13989 }
13990 else
13991 {
13992 decl = cp_parser_lookup_name_simple (parser,
13993 identifier,
13994 token->location);
13995 if (decl == error_mark_node)
13996 cp_parser_name_lookup_error (parser, identifier,
13997 decl, NLE_NULL,
13998 token->location);
13999 else if (check_for_bare_parameter_packs (decl))
14000 return false;
14001 else if (!at_namespace_scope_p ())
14002 do_local_using_decl (decl, qscope, identifier);
14003 else
14004 do_toplevel_using_decl (decl, qscope, identifier);
14005 }
14006 }
14007
14008 /* Look for the final `;'. */
14009 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14010
14011 return true;
14012 }
14013
14014 /* Parse a using-directive.
14015
14016 using-directive:
14017 using namespace :: [opt] nested-name-specifier [opt]
14018 namespace-name ; */
14019
14020 static void
14021 cp_parser_using_directive (cp_parser* parser)
14022 {
14023 tree namespace_decl;
14024 tree attribs;
14025
14026 /* Look for the `using' keyword. */
14027 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14028 /* And the `namespace' keyword. */
14029 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14030 /* Look for the optional `::' operator. */
14031 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14032 /* And the optional nested-name-specifier. */
14033 cp_parser_nested_name_specifier_opt (parser,
14034 /*typename_keyword_p=*/false,
14035 /*check_dependency_p=*/true,
14036 /*type_p=*/false,
14037 /*is_declaration=*/true);
14038 /* Get the namespace being used. */
14039 namespace_decl = cp_parser_namespace_name (parser);
14040 /* And any specified attributes. */
14041 attribs = cp_parser_attributes_opt (parser);
14042 /* Update the symbol table. */
14043 parse_using_directive (namespace_decl, attribs);
14044 /* Look for the final `;'. */
14045 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14046 }
14047
14048 /* Parse an asm-definition.
14049
14050 asm-definition:
14051 asm ( string-literal ) ;
14052
14053 GNU Extension:
14054
14055 asm-definition:
14056 asm volatile [opt] ( string-literal ) ;
14057 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14058 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14059 : asm-operand-list [opt] ) ;
14060 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14061 : asm-operand-list [opt]
14062 : asm-clobber-list [opt] ) ;
14063 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14064 : asm-clobber-list [opt]
14065 : asm-goto-list ) ; */
14066
14067 static void
14068 cp_parser_asm_definition (cp_parser* parser)
14069 {
14070 tree string;
14071 tree outputs = NULL_TREE;
14072 tree inputs = NULL_TREE;
14073 tree clobbers = NULL_TREE;
14074 tree labels = NULL_TREE;
14075 tree asm_stmt;
14076 bool volatile_p = false;
14077 bool extended_p = false;
14078 bool invalid_inputs_p = false;
14079 bool invalid_outputs_p = false;
14080 bool goto_p = false;
14081 required_token missing = RT_NONE;
14082
14083 /* Look for the `asm' keyword. */
14084 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14085 /* See if the next token is `volatile'. */
14086 if (cp_parser_allow_gnu_extensions_p (parser)
14087 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14088 {
14089 /* Remember that we saw the `volatile' keyword. */
14090 volatile_p = true;
14091 /* Consume the token. */
14092 cp_lexer_consume_token (parser->lexer);
14093 }
14094 if (cp_parser_allow_gnu_extensions_p (parser)
14095 && parser->in_function_body
14096 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14097 {
14098 /* Remember that we saw the `goto' keyword. */
14099 goto_p = true;
14100 /* Consume the token. */
14101 cp_lexer_consume_token (parser->lexer);
14102 }
14103 /* Look for the opening `('. */
14104 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14105 return;
14106 /* Look for the string. */
14107 string = cp_parser_string_literal (parser, false, false);
14108 if (string == error_mark_node)
14109 {
14110 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14111 /*consume_paren=*/true);
14112 return;
14113 }
14114
14115 /* If we're allowing GNU extensions, check for the extended assembly
14116 syntax. Unfortunately, the `:' tokens need not be separated by
14117 a space in C, and so, for compatibility, we tolerate that here
14118 too. Doing that means that we have to treat the `::' operator as
14119 two `:' tokens. */
14120 if (cp_parser_allow_gnu_extensions_p (parser)
14121 && parser->in_function_body
14122 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14123 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14124 {
14125 bool inputs_p = false;
14126 bool clobbers_p = false;
14127 bool labels_p = false;
14128
14129 /* The extended syntax was used. */
14130 extended_p = true;
14131
14132 /* Look for outputs. */
14133 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14134 {
14135 /* Consume the `:'. */
14136 cp_lexer_consume_token (parser->lexer);
14137 /* Parse the output-operands. */
14138 if (cp_lexer_next_token_is_not (parser->lexer,
14139 CPP_COLON)
14140 && cp_lexer_next_token_is_not (parser->lexer,
14141 CPP_SCOPE)
14142 && cp_lexer_next_token_is_not (parser->lexer,
14143 CPP_CLOSE_PAREN)
14144 && !goto_p)
14145 outputs = cp_parser_asm_operand_list (parser);
14146
14147 if (outputs == error_mark_node)
14148 invalid_outputs_p = true;
14149 }
14150 /* If the next token is `::', there are no outputs, and the
14151 next token is the beginning of the inputs. */
14152 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14153 /* The inputs are coming next. */
14154 inputs_p = true;
14155
14156 /* Look for inputs. */
14157 if (inputs_p
14158 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14159 {
14160 /* Consume the `:' or `::'. */
14161 cp_lexer_consume_token (parser->lexer);
14162 /* Parse the output-operands. */
14163 if (cp_lexer_next_token_is_not (parser->lexer,
14164 CPP_COLON)
14165 && cp_lexer_next_token_is_not (parser->lexer,
14166 CPP_SCOPE)
14167 && cp_lexer_next_token_is_not (parser->lexer,
14168 CPP_CLOSE_PAREN))
14169 inputs = cp_parser_asm_operand_list (parser);
14170
14171 if (inputs == error_mark_node)
14172 invalid_inputs_p = true;
14173 }
14174 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14175 /* The clobbers are coming next. */
14176 clobbers_p = true;
14177
14178 /* Look for clobbers. */
14179 if (clobbers_p
14180 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14181 {
14182 clobbers_p = true;
14183 /* Consume the `:' or `::'. */
14184 cp_lexer_consume_token (parser->lexer);
14185 /* Parse the clobbers. */
14186 if (cp_lexer_next_token_is_not (parser->lexer,
14187 CPP_COLON)
14188 && cp_lexer_next_token_is_not (parser->lexer,
14189 CPP_CLOSE_PAREN))
14190 clobbers = cp_parser_asm_clobber_list (parser);
14191 }
14192 else if (goto_p
14193 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14194 /* The labels are coming next. */
14195 labels_p = true;
14196
14197 /* Look for labels. */
14198 if (labels_p
14199 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14200 {
14201 labels_p = true;
14202 /* Consume the `:' or `::'. */
14203 cp_lexer_consume_token (parser->lexer);
14204 /* Parse the labels. */
14205 labels = cp_parser_asm_label_list (parser);
14206 }
14207
14208 if (goto_p && !labels_p)
14209 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14210 }
14211 else if (goto_p)
14212 missing = RT_COLON_SCOPE;
14213
14214 /* Look for the closing `)'. */
14215 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14216 missing ? missing : RT_CLOSE_PAREN))
14217 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14218 /*consume_paren=*/true);
14219 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14220
14221 if (!invalid_inputs_p && !invalid_outputs_p)
14222 {
14223 /* Create the ASM_EXPR. */
14224 if (parser->in_function_body)
14225 {
14226 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14227 inputs, clobbers, labels);
14228 /* If the extended syntax was not used, mark the ASM_EXPR. */
14229 if (!extended_p)
14230 {
14231 tree temp = asm_stmt;
14232 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14233 temp = TREE_OPERAND (temp, 0);
14234
14235 ASM_INPUT_P (temp) = 1;
14236 }
14237 }
14238 else
14239 cgraph_add_asm_node (string);
14240 }
14241 }
14242
14243 /* Declarators [gram.dcl.decl] */
14244
14245 /* Parse an init-declarator.
14246
14247 init-declarator:
14248 declarator initializer [opt]
14249
14250 GNU Extension:
14251
14252 init-declarator:
14253 declarator asm-specification [opt] attributes [opt] initializer [opt]
14254
14255 function-definition:
14256 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14257 function-body
14258 decl-specifier-seq [opt] declarator function-try-block
14259
14260 GNU Extension:
14261
14262 function-definition:
14263 __extension__ function-definition
14264
14265 The DECL_SPECIFIERS apply to this declarator. Returns a
14266 representation of the entity declared. If MEMBER_P is TRUE, then
14267 this declarator appears in a class scope. The new DECL created by
14268 this declarator is returned.
14269
14270 The CHECKS are access checks that should be performed once we know
14271 what entity is being declared (and, therefore, what classes have
14272 befriended it).
14273
14274 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14275 for a function-definition here as well. If the declarator is a
14276 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14277 be TRUE upon return. By that point, the function-definition will
14278 have been completely parsed.
14279
14280 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14281 is FALSE.
14282
14283 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14284 parsed declaration if it is an uninitialized single declarator not followed
14285 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14286 if present, will not be consumed. If returned, this declarator will be
14287 created with SD_INITIALIZED but will not call cp_finish_decl. */
14288
14289 static tree
14290 cp_parser_init_declarator (cp_parser* parser,
14291 cp_decl_specifier_seq *decl_specifiers,
14292 VEC (deferred_access_check,gc)* checks,
14293 bool function_definition_allowed_p,
14294 bool member_p,
14295 int declares_class_or_enum,
14296 bool* function_definition_p,
14297 tree* maybe_range_for_decl)
14298 {
14299 cp_token *token = NULL, *asm_spec_start_token = NULL,
14300 *attributes_start_token = NULL;
14301 cp_declarator *declarator;
14302 tree prefix_attributes;
14303 tree attributes;
14304 tree asm_specification;
14305 tree initializer;
14306 tree decl = NULL_TREE;
14307 tree scope;
14308 int is_initialized;
14309 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14310 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14311 "(...)". */
14312 enum cpp_ttype initialization_kind;
14313 bool is_direct_init = false;
14314 bool is_non_constant_init;
14315 int ctor_dtor_or_conv_p;
14316 bool friend_p;
14317 tree pushed_scope = NULL_TREE;
14318 bool range_for_decl_p = false;
14319
14320 /* Gather the attributes that were provided with the
14321 decl-specifiers. */
14322 prefix_attributes = decl_specifiers->attributes;
14323
14324 /* Assume that this is not the declarator for a function
14325 definition. */
14326 if (function_definition_p)
14327 *function_definition_p = false;
14328
14329 /* Defer access checks while parsing the declarator; we cannot know
14330 what names are accessible until we know what is being
14331 declared. */
14332 resume_deferring_access_checks ();
14333
14334 /* Parse the declarator. */
14335 token = cp_lexer_peek_token (parser->lexer);
14336 declarator
14337 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14338 &ctor_dtor_or_conv_p,
14339 /*parenthesized_p=*/NULL,
14340 /*member_p=*/false);
14341 /* Gather up the deferred checks. */
14342 stop_deferring_access_checks ();
14343
14344 /* If the DECLARATOR was erroneous, there's no need to go
14345 further. */
14346 if (declarator == cp_error_declarator)
14347 return error_mark_node;
14348
14349 /* Check that the number of template-parameter-lists is OK. */
14350 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14351 token->location))
14352 return error_mark_node;
14353
14354 if (declares_class_or_enum & 2)
14355 cp_parser_check_for_definition_in_return_type (declarator,
14356 decl_specifiers->type,
14357 decl_specifiers->type_location);
14358
14359 /* Figure out what scope the entity declared by the DECLARATOR is
14360 located in. `grokdeclarator' sometimes changes the scope, so
14361 we compute it now. */
14362 scope = get_scope_of_declarator (declarator);
14363
14364 /* Perform any lookups in the declared type which were thought to be
14365 dependent, but are not in the scope of the declarator. */
14366 decl_specifiers->type
14367 = maybe_update_decl_type (decl_specifiers->type, scope);
14368
14369 /* If we're allowing GNU extensions, look for an asm-specification
14370 and attributes. */
14371 if (cp_parser_allow_gnu_extensions_p (parser))
14372 {
14373 /* Look for an asm-specification. */
14374 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14375 asm_specification = cp_parser_asm_specification_opt (parser);
14376 /* And attributes. */
14377 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14378 attributes = cp_parser_attributes_opt (parser);
14379 }
14380 else
14381 {
14382 asm_specification = NULL_TREE;
14383 attributes = NULL_TREE;
14384 }
14385
14386 /* Peek at the next token. */
14387 token = cp_lexer_peek_token (parser->lexer);
14388 /* Check to see if the token indicates the start of a
14389 function-definition. */
14390 if (function_declarator_p (declarator)
14391 && cp_parser_token_starts_function_definition_p (token))
14392 {
14393 if (!function_definition_allowed_p)
14394 {
14395 /* If a function-definition should not appear here, issue an
14396 error message. */
14397 cp_parser_error (parser,
14398 "a function-definition is not allowed here");
14399 return error_mark_node;
14400 }
14401 else
14402 {
14403 location_t func_brace_location
14404 = cp_lexer_peek_token (parser->lexer)->location;
14405
14406 /* Neither attributes nor an asm-specification are allowed
14407 on a function-definition. */
14408 if (asm_specification)
14409 error_at (asm_spec_start_token->location,
14410 "an asm-specification is not allowed "
14411 "on a function-definition");
14412 if (attributes)
14413 error_at (attributes_start_token->location,
14414 "attributes are not allowed on a function-definition");
14415 /* This is a function-definition. */
14416 *function_definition_p = true;
14417
14418 /* Parse the function definition. */
14419 if (member_p)
14420 decl = cp_parser_save_member_function_body (parser,
14421 decl_specifiers,
14422 declarator,
14423 prefix_attributes);
14424 else
14425 decl
14426 = (cp_parser_function_definition_from_specifiers_and_declarator
14427 (parser, decl_specifiers, prefix_attributes, declarator));
14428
14429 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14430 {
14431 /* This is where the prologue starts... */
14432 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14433 = func_brace_location;
14434 }
14435
14436 return decl;
14437 }
14438 }
14439
14440 /* [dcl.dcl]
14441
14442 Only in function declarations for constructors, destructors, and
14443 type conversions can the decl-specifier-seq be omitted.
14444
14445 We explicitly postpone this check past the point where we handle
14446 function-definitions because we tolerate function-definitions
14447 that are missing their return types in some modes. */
14448 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14449 {
14450 cp_parser_error (parser,
14451 "expected constructor, destructor, or type conversion");
14452 return error_mark_node;
14453 }
14454
14455 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14456 if (token->type == CPP_EQ
14457 || token->type == CPP_OPEN_PAREN
14458 || token->type == CPP_OPEN_BRACE)
14459 {
14460 is_initialized = SD_INITIALIZED;
14461 initialization_kind = token->type;
14462 if (maybe_range_for_decl)
14463 *maybe_range_for_decl = error_mark_node;
14464
14465 if (token->type == CPP_EQ
14466 && function_declarator_p (declarator))
14467 {
14468 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14469 if (t2->keyword == RID_DEFAULT)
14470 is_initialized = SD_DEFAULTED;
14471 else if (t2->keyword == RID_DELETE)
14472 is_initialized = SD_DELETED;
14473 }
14474 }
14475 else
14476 {
14477 /* If the init-declarator isn't initialized and isn't followed by a
14478 `,' or `;', it's not a valid init-declarator. */
14479 if (token->type != CPP_COMMA
14480 && token->type != CPP_SEMICOLON)
14481 {
14482 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14483 range_for_decl_p = true;
14484 else
14485 {
14486 cp_parser_error (parser, "expected initializer");
14487 return error_mark_node;
14488 }
14489 }
14490 is_initialized = SD_UNINITIALIZED;
14491 initialization_kind = CPP_EOF;
14492 }
14493
14494 /* Because start_decl has side-effects, we should only call it if we
14495 know we're going ahead. By this point, we know that we cannot
14496 possibly be looking at any other construct. */
14497 cp_parser_commit_to_tentative_parse (parser);
14498
14499 /* If the decl specifiers were bad, issue an error now that we're
14500 sure this was intended to be a declarator. Then continue
14501 declaring the variable(s), as int, to try to cut down on further
14502 errors. */
14503 if (decl_specifiers->any_specifiers_p
14504 && decl_specifiers->type == error_mark_node)
14505 {
14506 cp_parser_error (parser, "invalid type in declaration");
14507 decl_specifiers->type = integer_type_node;
14508 }
14509
14510 /* Check to see whether or not this declaration is a friend. */
14511 friend_p = cp_parser_friend_p (decl_specifiers);
14512
14513 /* Enter the newly declared entry in the symbol table. If we're
14514 processing a declaration in a class-specifier, we wait until
14515 after processing the initializer. */
14516 if (!member_p)
14517 {
14518 if (parser->in_unbraced_linkage_specification_p)
14519 decl_specifiers->storage_class = sc_extern;
14520 decl = start_decl (declarator, decl_specifiers,
14521 range_for_decl_p? SD_INITIALIZED : is_initialized,
14522 attributes, prefix_attributes,
14523 &pushed_scope);
14524 /* Adjust location of decl if declarator->id_loc is more appropriate:
14525 set, and decl wasn't merged with another decl, in which case its
14526 location would be different from input_location, and more accurate. */
14527 if (DECL_P (decl)
14528 && declarator->id_loc != UNKNOWN_LOCATION
14529 && DECL_SOURCE_LOCATION (decl) == input_location)
14530 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14531 }
14532 else if (scope)
14533 /* Enter the SCOPE. That way unqualified names appearing in the
14534 initializer will be looked up in SCOPE. */
14535 pushed_scope = push_scope (scope);
14536
14537 /* Perform deferred access control checks, now that we know in which
14538 SCOPE the declared entity resides. */
14539 if (!member_p && decl)
14540 {
14541 tree saved_current_function_decl = NULL_TREE;
14542
14543 /* If the entity being declared is a function, pretend that we
14544 are in its scope. If it is a `friend', it may have access to
14545 things that would not otherwise be accessible. */
14546 if (TREE_CODE (decl) == FUNCTION_DECL)
14547 {
14548 saved_current_function_decl = current_function_decl;
14549 current_function_decl = decl;
14550 }
14551
14552 /* Perform access checks for template parameters. */
14553 cp_parser_perform_template_parameter_access_checks (checks);
14554
14555 /* Perform the access control checks for the declarator and the
14556 decl-specifiers. */
14557 perform_deferred_access_checks ();
14558
14559 /* Restore the saved value. */
14560 if (TREE_CODE (decl) == FUNCTION_DECL)
14561 current_function_decl = saved_current_function_decl;
14562 }
14563
14564 /* Parse the initializer. */
14565 initializer = NULL_TREE;
14566 is_direct_init = false;
14567 is_non_constant_init = true;
14568 if (is_initialized)
14569 {
14570 if (function_declarator_p (declarator))
14571 {
14572 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14573 if (initialization_kind == CPP_EQ)
14574 initializer = cp_parser_pure_specifier (parser);
14575 else
14576 {
14577 /* If the declaration was erroneous, we don't really
14578 know what the user intended, so just silently
14579 consume the initializer. */
14580 if (decl != error_mark_node)
14581 error_at (initializer_start_token->location,
14582 "initializer provided for function");
14583 cp_parser_skip_to_closing_parenthesis (parser,
14584 /*recovering=*/true,
14585 /*or_comma=*/false,
14586 /*consume_paren=*/true);
14587 }
14588 }
14589 else
14590 {
14591 /* We want to record the extra mangling scope for in-class
14592 initializers of class members and initializers of static data
14593 member templates. The former is a C++0x feature which isn't
14594 implemented yet, and I expect it will involve deferring
14595 parsing of the initializer until end of class as with default
14596 arguments. So right here we only handle the latter. */
14597 if (!member_p && processing_template_decl)
14598 start_lambda_scope (decl);
14599 initializer = cp_parser_initializer (parser,
14600 &is_direct_init,
14601 &is_non_constant_init);
14602 if (!member_p && processing_template_decl)
14603 finish_lambda_scope ();
14604 }
14605 }
14606
14607 /* The old parser allows attributes to appear after a parenthesized
14608 initializer. Mark Mitchell proposed removing this functionality
14609 on the GCC mailing lists on 2002-08-13. This parser accepts the
14610 attributes -- but ignores them. */
14611 if (cp_parser_allow_gnu_extensions_p (parser)
14612 && initialization_kind == CPP_OPEN_PAREN)
14613 if (cp_parser_attributes_opt (parser))
14614 warning (OPT_Wattributes,
14615 "attributes after parenthesized initializer ignored");
14616
14617 /* For an in-class declaration, use `grokfield' to create the
14618 declaration. */
14619 if (member_p)
14620 {
14621 if (pushed_scope)
14622 {
14623 pop_scope (pushed_scope);
14624 pushed_scope = NULL_TREE;
14625 }
14626 decl = grokfield (declarator, decl_specifiers,
14627 initializer, !is_non_constant_init,
14628 /*asmspec=*/NULL_TREE,
14629 prefix_attributes);
14630 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14631 cp_parser_save_default_args (parser, decl);
14632 }
14633
14634 /* Finish processing the declaration. But, skip member
14635 declarations. */
14636 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14637 {
14638 cp_finish_decl (decl,
14639 initializer, !is_non_constant_init,
14640 asm_specification,
14641 /* If the initializer is in parentheses, then this is
14642 a direct-initialization, which means that an
14643 `explicit' constructor is OK. Otherwise, an
14644 `explicit' constructor cannot be used. */
14645 ((is_direct_init || !is_initialized)
14646 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14647 }
14648 else if ((cxx_dialect != cxx98) && friend_p
14649 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14650 /* Core issue #226 (C++0x only): A default template-argument
14651 shall not be specified in a friend class template
14652 declaration. */
14653 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14654 /*is_partial=*/0, /*is_friend_decl=*/1);
14655
14656 if (!friend_p && pushed_scope)
14657 pop_scope (pushed_scope);
14658
14659 return decl;
14660 }
14661
14662 /* Parse a declarator.
14663
14664 declarator:
14665 direct-declarator
14666 ptr-operator declarator
14667
14668 abstract-declarator:
14669 ptr-operator abstract-declarator [opt]
14670 direct-abstract-declarator
14671
14672 GNU Extensions:
14673
14674 declarator:
14675 attributes [opt] direct-declarator
14676 attributes [opt] ptr-operator declarator
14677
14678 abstract-declarator:
14679 attributes [opt] ptr-operator abstract-declarator [opt]
14680 attributes [opt] direct-abstract-declarator
14681
14682 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14683 detect constructor, destructor or conversion operators. It is set
14684 to -1 if the declarator is a name, and +1 if it is a
14685 function. Otherwise it is set to zero. Usually you just want to
14686 test for >0, but internally the negative value is used.
14687
14688 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14689 a decl-specifier-seq unless it declares a constructor, destructor,
14690 or conversion. It might seem that we could check this condition in
14691 semantic analysis, rather than parsing, but that makes it difficult
14692 to handle something like `f()'. We want to notice that there are
14693 no decl-specifiers, and therefore realize that this is an
14694 expression, not a declaration.)
14695
14696 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14697 the declarator is a direct-declarator of the form "(...)".
14698
14699 MEMBER_P is true iff this declarator is a member-declarator. */
14700
14701 static cp_declarator *
14702 cp_parser_declarator (cp_parser* parser,
14703 cp_parser_declarator_kind dcl_kind,
14704 int* ctor_dtor_or_conv_p,
14705 bool* parenthesized_p,
14706 bool member_p)
14707 {
14708 cp_declarator *declarator;
14709 enum tree_code code;
14710 cp_cv_quals cv_quals;
14711 tree class_type;
14712 tree attributes = NULL_TREE;
14713
14714 /* Assume this is not a constructor, destructor, or type-conversion
14715 operator. */
14716 if (ctor_dtor_or_conv_p)
14717 *ctor_dtor_or_conv_p = 0;
14718
14719 if (cp_parser_allow_gnu_extensions_p (parser))
14720 attributes = cp_parser_attributes_opt (parser);
14721
14722 /* Check for the ptr-operator production. */
14723 cp_parser_parse_tentatively (parser);
14724 /* Parse the ptr-operator. */
14725 code = cp_parser_ptr_operator (parser,
14726 &class_type,
14727 &cv_quals);
14728 /* If that worked, then we have a ptr-operator. */
14729 if (cp_parser_parse_definitely (parser))
14730 {
14731 /* If a ptr-operator was found, then this declarator was not
14732 parenthesized. */
14733 if (parenthesized_p)
14734 *parenthesized_p = true;
14735 /* The dependent declarator is optional if we are parsing an
14736 abstract-declarator. */
14737 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14738 cp_parser_parse_tentatively (parser);
14739
14740 /* Parse the dependent declarator. */
14741 declarator = cp_parser_declarator (parser, dcl_kind,
14742 /*ctor_dtor_or_conv_p=*/NULL,
14743 /*parenthesized_p=*/NULL,
14744 /*member_p=*/false);
14745
14746 /* If we are parsing an abstract-declarator, we must handle the
14747 case where the dependent declarator is absent. */
14748 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14749 && !cp_parser_parse_definitely (parser))
14750 declarator = NULL;
14751
14752 declarator = cp_parser_make_indirect_declarator
14753 (code, class_type, cv_quals, declarator);
14754 }
14755 /* Everything else is a direct-declarator. */
14756 else
14757 {
14758 if (parenthesized_p)
14759 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14760 CPP_OPEN_PAREN);
14761 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14762 ctor_dtor_or_conv_p,
14763 member_p);
14764 }
14765
14766 if (attributes && declarator && declarator != cp_error_declarator)
14767 declarator->attributes = attributes;
14768
14769 return declarator;
14770 }
14771
14772 /* Parse a direct-declarator or direct-abstract-declarator.
14773
14774 direct-declarator:
14775 declarator-id
14776 direct-declarator ( parameter-declaration-clause )
14777 cv-qualifier-seq [opt]
14778 exception-specification [opt]
14779 direct-declarator [ constant-expression [opt] ]
14780 ( declarator )
14781
14782 direct-abstract-declarator:
14783 direct-abstract-declarator [opt]
14784 ( parameter-declaration-clause )
14785 cv-qualifier-seq [opt]
14786 exception-specification [opt]
14787 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14788 ( abstract-declarator )
14789
14790 Returns a representation of the declarator. DCL_KIND is
14791 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14792 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14793 we are parsing a direct-declarator. It is
14794 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14795 of ambiguity we prefer an abstract declarator, as per
14796 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14797 cp_parser_declarator. */
14798
14799 static cp_declarator *
14800 cp_parser_direct_declarator (cp_parser* parser,
14801 cp_parser_declarator_kind dcl_kind,
14802 int* ctor_dtor_or_conv_p,
14803 bool member_p)
14804 {
14805 cp_token *token;
14806 cp_declarator *declarator = NULL;
14807 tree scope = NULL_TREE;
14808 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14809 bool saved_in_declarator_p = parser->in_declarator_p;
14810 bool first = true;
14811 tree pushed_scope = NULL_TREE;
14812
14813 while (true)
14814 {
14815 /* Peek at the next token. */
14816 token = cp_lexer_peek_token (parser->lexer);
14817 if (token->type == CPP_OPEN_PAREN)
14818 {
14819 /* This is either a parameter-declaration-clause, or a
14820 parenthesized declarator. When we know we are parsing a
14821 named declarator, it must be a parenthesized declarator
14822 if FIRST is true. For instance, `(int)' is a
14823 parameter-declaration-clause, with an omitted
14824 direct-abstract-declarator. But `((*))', is a
14825 parenthesized abstract declarator. Finally, when T is a
14826 template parameter `(T)' is a
14827 parameter-declaration-clause, and not a parenthesized
14828 named declarator.
14829
14830 We first try and parse a parameter-declaration-clause,
14831 and then try a nested declarator (if FIRST is true).
14832
14833 It is not an error for it not to be a
14834 parameter-declaration-clause, even when FIRST is
14835 false. Consider,
14836
14837 int i (int);
14838 int i (3);
14839
14840 The first is the declaration of a function while the
14841 second is the definition of a variable, including its
14842 initializer.
14843
14844 Having seen only the parenthesis, we cannot know which of
14845 these two alternatives should be selected. Even more
14846 complex are examples like:
14847
14848 int i (int (a));
14849 int i (int (3));
14850
14851 The former is a function-declaration; the latter is a
14852 variable initialization.
14853
14854 Thus again, we try a parameter-declaration-clause, and if
14855 that fails, we back out and return. */
14856
14857 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14858 {
14859 tree params;
14860 unsigned saved_num_template_parameter_lists;
14861 bool is_declarator = false;
14862 tree t;
14863
14864 /* In a member-declarator, the only valid interpretation
14865 of a parenthesis is the start of a
14866 parameter-declaration-clause. (It is invalid to
14867 initialize a static data member with a parenthesized
14868 initializer; only the "=" form of initialization is
14869 permitted.) */
14870 if (!member_p)
14871 cp_parser_parse_tentatively (parser);
14872
14873 /* Consume the `('. */
14874 cp_lexer_consume_token (parser->lexer);
14875 if (first)
14876 {
14877 /* If this is going to be an abstract declarator, we're
14878 in a declarator and we can't have default args. */
14879 parser->default_arg_ok_p = false;
14880 parser->in_declarator_p = true;
14881 }
14882
14883 /* Inside the function parameter list, surrounding
14884 template-parameter-lists do not apply. */
14885 saved_num_template_parameter_lists
14886 = parser->num_template_parameter_lists;
14887 parser->num_template_parameter_lists = 0;
14888
14889 begin_scope (sk_function_parms, NULL_TREE);
14890
14891 /* Parse the parameter-declaration-clause. */
14892 params = cp_parser_parameter_declaration_clause (parser);
14893
14894 parser->num_template_parameter_lists
14895 = saved_num_template_parameter_lists;
14896
14897 /* If all went well, parse the cv-qualifier-seq and the
14898 exception-specification. */
14899 if (member_p || cp_parser_parse_definitely (parser))
14900 {
14901 cp_cv_quals cv_quals;
14902 cp_virt_specifiers virt_specifiers;
14903 tree exception_specification;
14904 tree late_return;
14905
14906 is_declarator = true;
14907
14908 if (ctor_dtor_or_conv_p)
14909 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14910 first = false;
14911 /* Consume the `)'. */
14912 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14913
14914 /* Parse the cv-qualifier-seq. */
14915 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14916 /* And the exception-specification. */
14917 exception_specification
14918 = cp_parser_exception_specification_opt (parser);
14919 /* Parse the virt-specifier-seq. */
14920 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
14921
14922 late_return
14923 = cp_parser_late_return_type_opt (parser);
14924
14925 /* Create the function-declarator. */
14926 declarator = make_call_declarator (declarator,
14927 params,
14928 cv_quals,
14929 virt_specifiers,
14930 exception_specification,
14931 late_return);
14932 /* Any subsequent parameter lists are to do with
14933 return type, so are not those of the declared
14934 function. */
14935 parser->default_arg_ok_p = false;
14936 }
14937
14938 /* Remove the function parms from scope. */
14939 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14940 pop_binding (DECL_NAME (t), t);
14941 leave_scope();
14942
14943 if (is_declarator)
14944 /* Repeat the main loop. */
14945 continue;
14946 }
14947
14948 /* If this is the first, we can try a parenthesized
14949 declarator. */
14950 if (first)
14951 {
14952 bool saved_in_type_id_in_expr_p;
14953
14954 parser->default_arg_ok_p = saved_default_arg_ok_p;
14955 parser->in_declarator_p = saved_in_declarator_p;
14956
14957 /* Consume the `('. */
14958 cp_lexer_consume_token (parser->lexer);
14959 /* Parse the nested declarator. */
14960 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14961 parser->in_type_id_in_expr_p = true;
14962 declarator
14963 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14964 /*parenthesized_p=*/NULL,
14965 member_p);
14966 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14967 first = false;
14968 /* Expect a `)'. */
14969 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14970 declarator = cp_error_declarator;
14971 if (declarator == cp_error_declarator)
14972 break;
14973
14974 goto handle_declarator;
14975 }
14976 /* Otherwise, we must be done. */
14977 else
14978 break;
14979 }
14980 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14981 && token->type == CPP_OPEN_SQUARE)
14982 {
14983 /* Parse an array-declarator. */
14984 tree bounds;
14985
14986 if (ctor_dtor_or_conv_p)
14987 *ctor_dtor_or_conv_p = 0;
14988
14989 first = false;
14990 parser->default_arg_ok_p = false;
14991 parser->in_declarator_p = true;
14992 /* Consume the `['. */
14993 cp_lexer_consume_token (parser->lexer);
14994 /* Peek at the next token. */
14995 token = cp_lexer_peek_token (parser->lexer);
14996 /* If the next token is `]', then there is no
14997 constant-expression. */
14998 if (token->type != CPP_CLOSE_SQUARE)
14999 {
15000 bool non_constant_p;
15001
15002 bounds
15003 = cp_parser_constant_expression (parser,
15004 /*allow_non_constant=*/true,
15005 &non_constant_p);
15006 if (!non_constant_p)
15007 /* OK */;
15008 /* Normally, the array bound must be an integral constant
15009 expression. However, as an extension, we allow VLAs
15010 in function scopes as long as they aren't part of a
15011 parameter declaration. */
15012 else if (!parser->in_function_body
15013 || current_binding_level->kind == sk_function_parms)
15014 {
15015 cp_parser_error (parser,
15016 "array bound is not an integer constant");
15017 bounds = error_mark_node;
15018 }
15019 else if (processing_template_decl && !error_operand_p (bounds))
15020 {
15021 /* Remember this wasn't a constant-expression. */
15022 bounds = build_nop (TREE_TYPE (bounds), bounds);
15023 TREE_SIDE_EFFECTS (bounds) = 1;
15024 }
15025 }
15026 else
15027 bounds = NULL_TREE;
15028 /* Look for the closing `]'. */
15029 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15030 {
15031 declarator = cp_error_declarator;
15032 break;
15033 }
15034
15035 declarator = make_array_declarator (declarator, bounds);
15036 }
15037 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15038 {
15039 {
15040 tree qualifying_scope;
15041 tree unqualified_name;
15042 special_function_kind sfk;
15043 bool abstract_ok;
15044 bool pack_expansion_p = false;
15045 cp_token *declarator_id_start_token;
15046
15047 /* Parse a declarator-id */
15048 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15049 if (abstract_ok)
15050 {
15051 cp_parser_parse_tentatively (parser);
15052
15053 /* If we see an ellipsis, we should be looking at a
15054 parameter pack. */
15055 if (token->type == CPP_ELLIPSIS)
15056 {
15057 /* Consume the `...' */
15058 cp_lexer_consume_token (parser->lexer);
15059
15060 pack_expansion_p = true;
15061 }
15062 }
15063
15064 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15065 unqualified_name
15066 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15067 qualifying_scope = parser->scope;
15068 if (abstract_ok)
15069 {
15070 bool okay = false;
15071
15072 if (!unqualified_name && pack_expansion_p)
15073 {
15074 /* Check whether an error occurred. */
15075 okay = !cp_parser_error_occurred (parser);
15076
15077 /* We already consumed the ellipsis to mark a
15078 parameter pack, but we have no way to report it,
15079 so abort the tentative parse. We will be exiting
15080 immediately anyway. */
15081 cp_parser_abort_tentative_parse (parser);
15082 }
15083 else
15084 okay = cp_parser_parse_definitely (parser);
15085
15086 if (!okay)
15087 unqualified_name = error_mark_node;
15088 else if (unqualified_name
15089 && (qualifying_scope
15090 || (TREE_CODE (unqualified_name)
15091 != IDENTIFIER_NODE)))
15092 {
15093 cp_parser_error (parser, "expected unqualified-id");
15094 unqualified_name = error_mark_node;
15095 }
15096 }
15097
15098 if (!unqualified_name)
15099 return NULL;
15100 if (unqualified_name == error_mark_node)
15101 {
15102 declarator = cp_error_declarator;
15103 pack_expansion_p = false;
15104 declarator->parameter_pack_p = false;
15105 break;
15106 }
15107
15108 if (qualifying_scope && at_namespace_scope_p ()
15109 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15110 {
15111 /* In the declaration of a member of a template class
15112 outside of the class itself, the SCOPE will sometimes
15113 be a TYPENAME_TYPE. For example, given:
15114
15115 template <typename T>
15116 int S<T>::R::i = 3;
15117
15118 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15119 this context, we must resolve S<T>::R to an ordinary
15120 type, rather than a typename type.
15121
15122 The reason we normally avoid resolving TYPENAME_TYPEs
15123 is that a specialization of `S' might render
15124 `S<T>::R' not a type. However, if `S' is
15125 specialized, then this `i' will not be used, so there
15126 is no harm in resolving the types here. */
15127 tree type;
15128
15129 /* Resolve the TYPENAME_TYPE. */
15130 type = resolve_typename_type (qualifying_scope,
15131 /*only_current_p=*/false);
15132 /* If that failed, the declarator is invalid. */
15133 if (TREE_CODE (type) == TYPENAME_TYPE)
15134 {
15135 if (typedef_variant_p (type))
15136 error_at (declarator_id_start_token->location,
15137 "cannot define member of dependent typedef "
15138 "%qT", type);
15139 else
15140 error_at (declarator_id_start_token->location,
15141 "%<%T::%E%> is not a type",
15142 TYPE_CONTEXT (qualifying_scope),
15143 TYPE_IDENTIFIER (qualifying_scope));
15144 }
15145 qualifying_scope = type;
15146 }
15147
15148 sfk = sfk_none;
15149
15150 if (unqualified_name)
15151 {
15152 tree class_type;
15153
15154 if (qualifying_scope
15155 && CLASS_TYPE_P (qualifying_scope))
15156 class_type = qualifying_scope;
15157 else
15158 class_type = current_class_type;
15159
15160 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15161 {
15162 tree name_type = TREE_TYPE (unqualified_name);
15163 if (class_type && same_type_p (name_type, class_type))
15164 {
15165 if (qualifying_scope
15166 && CLASSTYPE_USE_TEMPLATE (name_type))
15167 {
15168 error_at (declarator_id_start_token->location,
15169 "invalid use of constructor as a template");
15170 inform (declarator_id_start_token->location,
15171 "use %<%T::%D%> instead of %<%T::%D%> to "
15172 "name the constructor in a qualified name",
15173 class_type,
15174 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15175 class_type, name_type);
15176 declarator = cp_error_declarator;
15177 break;
15178 }
15179 else
15180 unqualified_name = constructor_name (class_type);
15181 }
15182 else
15183 {
15184 /* We do not attempt to print the declarator
15185 here because we do not have enough
15186 information about its original syntactic
15187 form. */
15188 cp_parser_error (parser, "invalid declarator");
15189 declarator = cp_error_declarator;
15190 break;
15191 }
15192 }
15193
15194 if (class_type)
15195 {
15196 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15197 sfk = sfk_destructor;
15198 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15199 sfk = sfk_conversion;
15200 else if (/* There's no way to declare a constructor
15201 for an anonymous type, even if the type
15202 got a name for linkage purposes. */
15203 !TYPE_WAS_ANONYMOUS (class_type)
15204 && constructor_name_p (unqualified_name,
15205 class_type))
15206 {
15207 unqualified_name = constructor_name (class_type);
15208 sfk = sfk_constructor;
15209 }
15210 else if (is_overloaded_fn (unqualified_name)
15211 && DECL_CONSTRUCTOR_P (get_first_fn
15212 (unqualified_name)))
15213 sfk = sfk_constructor;
15214
15215 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15216 *ctor_dtor_or_conv_p = -1;
15217 }
15218 }
15219 declarator = make_id_declarator (qualifying_scope,
15220 unqualified_name,
15221 sfk);
15222 declarator->id_loc = token->location;
15223 declarator->parameter_pack_p = pack_expansion_p;
15224
15225 if (pack_expansion_p)
15226 maybe_warn_variadic_templates ();
15227 }
15228
15229 handle_declarator:;
15230 scope = get_scope_of_declarator (declarator);
15231 if (scope)
15232 /* Any names that appear after the declarator-id for a
15233 member are looked up in the containing scope. */
15234 pushed_scope = push_scope (scope);
15235 parser->in_declarator_p = true;
15236 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15237 || (declarator && declarator->kind == cdk_id))
15238 /* Default args are only allowed on function
15239 declarations. */
15240 parser->default_arg_ok_p = saved_default_arg_ok_p;
15241 else
15242 parser->default_arg_ok_p = false;
15243
15244 first = false;
15245 }
15246 /* We're done. */
15247 else
15248 break;
15249 }
15250
15251 /* For an abstract declarator, we might wind up with nothing at this
15252 point. That's an error; the declarator is not optional. */
15253 if (!declarator)
15254 cp_parser_error (parser, "expected declarator");
15255
15256 /* If we entered a scope, we must exit it now. */
15257 if (pushed_scope)
15258 pop_scope (pushed_scope);
15259
15260 parser->default_arg_ok_p = saved_default_arg_ok_p;
15261 parser->in_declarator_p = saved_in_declarator_p;
15262
15263 return declarator;
15264 }
15265
15266 /* Parse a ptr-operator.
15267
15268 ptr-operator:
15269 * cv-qualifier-seq [opt]
15270 &
15271 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15272
15273 GNU Extension:
15274
15275 ptr-operator:
15276 & cv-qualifier-seq [opt]
15277
15278 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15279 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15280 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15281 filled in with the TYPE containing the member. *CV_QUALS is
15282 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15283 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15284 Note that the tree codes returned by this function have nothing
15285 to do with the types of trees that will be eventually be created
15286 to represent the pointer or reference type being parsed. They are
15287 just constants with suggestive names. */
15288 static enum tree_code
15289 cp_parser_ptr_operator (cp_parser* parser,
15290 tree* type,
15291 cp_cv_quals *cv_quals)
15292 {
15293 enum tree_code code = ERROR_MARK;
15294 cp_token *token;
15295
15296 /* Assume that it's not a pointer-to-member. */
15297 *type = NULL_TREE;
15298 /* And that there are no cv-qualifiers. */
15299 *cv_quals = TYPE_UNQUALIFIED;
15300
15301 /* Peek at the next token. */
15302 token = cp_lexer_peek_token (parser->lexer);
15303
15304 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15305 if (token->type == CPP_MULT)
15306 code = INDIRECT_REF;
15307 else if (token->type == CPP_AND)
15308 code = ADDR_EXPR;
15309 else if ((cxx_dialect != cxx98) &&
15310 token->type == CPP_AND_AND) /* C++0x only */
15311 code = NON_LVALUE_EXPR;
15312
15313 if (code != ERROR_MARK)
15314 {
15315 /* Consume the `*', `&' or `&&'. */
15316 cp_lexer_consume_token (parser->lexer);
15317
15318 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15319 `&', if we are allowing GNU extensions. (The only qualifier
15320 that can legally appear after `&' is `restrict', but that is
15321 enforced during semantic analysis. */
15322 if (code == INDIRECT_REF
15323 || cp_parser_allow_gnu_extensions_p (parser))
15324 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15325 }
15326 else
15327 {
15328 /* Try the pointer-to-member case. */
15329 cp_parser_parse_tentatively (parser);
15330 /* Look for the optional `::' operator. */
15331 cp_parser_global_scope_opt (parser,
15332 /*current_scope_valid_p=*/false);
15333 /* Look for the nested-name specifier. */
15334 token = cp_lexer_peek_token (parser->lexer);
15335 cp_parser_nested_name_specifier (parser,
15336 /*typename_keyword_p=*/false,
15337 /*check_dependency_p=*/true,
15338 /*type_p=*/false,
15339 /*is_declaration=*/false);
15340 /* If we found it, and the next token is a `*', then we are
15341 indeed looking at a pointer-to-member operator. */
15342 if (!cp_parser_error_occurred (parser)
15343 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15344 {
15345 /* Indicate that the `*' operator was used. */
15346 code = INDIRECT_REF;
15347
15348 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15349 error_at (token->location, "%qD is a namespace", parser->scope);
15350 else
15351 {
15352 /* The type of which the member is a member is given by the
15353 current SCOPE. */
15354 *type = parser->scope;
15355 /* The next name will not be qualified. */
15356 parser->scope = NULL_TREE;
15357 parser->qualifying_scope = NULL_TREE;
15358 parser->object_scope = NULL_TREE;
15359 /* Look for the optional cv-qualifier-seq. */
15360 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15361 }
15362 }
15363 /* If that didn't work we don't have a ptr-operator. */
15364 if (!cp_parser_parse_definitely (parser))
15365 cp_parser_error (parser, "expected ptr-operator");
15366 }
15367
15368 return code;
15369 }
15370
15371 /* Parse an (optional) cv-qualifier-seq.
15372
15373 cv-qualifier-seq:
15374 cv-qualifier cv-qualifier-seq [opt]
15375
15376 cv-qualifier:
15377 const
15378 volatile
15379
15380 GNU Extension:
15381
15382 cv-qualifier:
15383 __restrict__
15384
15385 Returns a bitmask representing the cv-qualifiers. */
15386
15387 static cp_cv_quals
15388 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15389 {
15390 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15391
15392 while (true)
15393 {
15394 cp_token *token;
15395 cp_cv_quals cv_qualifier;
15396
15397 /* Peek at the next token. */
15398 token = cp_lexer_peek_token (parser->lexer);
15399 /* See if it's a cv-qualifier. */
15400 switch (token->keyword)
15401 {
15402 case RID_CONST:
15403 cv_qualifier = TYPE_QUAL_CONST;
15404 break;
15405
15406 case RID_VOLATILE:
15407 cv_qualifier = TYPE_QUAL_VOLATILE;
15408 break;
15409
15410 case RID_RESTRICT:
15411 cv_qualifier = TYPE_QUAL_RESTRICT;
15412 break;
15413
15414 default:
15415 cv_qualifier = TYPE_UNQUALIFIED;
15416 break;
15417 }
15418
15419 if (!cv_qualifier)
15420 break;
15421
15422 if (cv_quals & cv_qualifier)
15423 {
15424 error_at (token->location, "duplicate cv-qualifier");
15425 cp_lexer_purge_token (parser->lexer);
15426 }
15427 else
15428 {
15429 cp_lexer_consume_token (parser->lexer);
15430 cv_quals |= cv_qualifier;
15431 }
15432 }
15433
15434 return cv_quals;
15435 }
15436
15437 /* Parse an (optional) virt-specifier-seq.
15438
15439 virt-specifier-seq:
15440 virt-specifier virt-specifier-seq [opt]
15441
15442 virt-specifier:
15443 override
15444 final
15445
15446 Returns a bitmask representing the virt-specifiers. */
15447
15448 static cp_virt_specifiers
15449 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15450 {
15451 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15452
15453 while (true)
15454 {
15455 cp_token *token;
15456 cp_virt_specifiers virt_specifier;
15457
15458 /* Peek at the next token. */
15459 token = cp_lexer_peek_token (parser->lexer);
15460 /* See if it's a virt-specifier-qualifier. */
15461 if (token->type != CPP_NAME)
15462 break;
15463 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15464 virt_specifier = VIRT_SPEC_OVERRIDE;
15465 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15466 virt_specifier = VIRT_SPEC_FINAL;
15467 else
15468 break;
15469
15470 if (virt_specifiers & virt_specifier)
15471 {
15472 error_at (token->location, "duplicate virt-specifier");
15473 cp_lexer_purge_token (parser->lexer);
15474 }
15475 else
15476 {
15477 cp_lexer_consume_token (parser->lexer);
15478 virt_specifiers |= virt_specifier;
15479 }
15480 }
15481 return virt_specifiers;
15482 }
15483
15484 /* Parse a late-specified return type, if any. This is not a separate
15485 non-terminal, but part of a function declarator, which looks like
15486
15487 -> trailing-type-specifier-seq abstract-declarator(opt)
15488
15489 Returns the type indicated by the type-id. */
15490
15491 static tree
15492 cp_parser_late_return_type_opt (cp_parser* parser)
15493 {
15494 cp_token *token;
15495
15496 /* Peek at the next token. */
15497 token = cp_lexer_peek_token (parser->lexer);
15498 /* A late-specified return type is indicated by an initial '->'. */
15499 if (token->type != CPP_DEREF)
15500 return NULL_TREE;
15501
15502 /* Consume the ->. */
15503 cp_lexer_consume_token (parser->lexer);
15504
15505 return cp_parser_trailing_type_id (parser);
15506 }
15507
15508 /* Parse a declarator-id.
15509
15510 declarator-id:
15511 id-expression
15512 :: [opt] nested-name-specifier [opt] type-name
15513
15514 In the `id-expression' case, the value returned is as for
15515 cp_parser_id_expression if the id-expression was an unqualified-id.
15516 If the id-expression was a qualified-id, then a SCOPE_REF is
15517 returned. The first operand is the scope (either a NAMESPACE_DECL
15518 or TREE_TYPE), but the second is still just a representation of an
15519 unqualified-id. */
15520
15521 static tree
15522 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15523 {
15524 tree id;
15525 /* The expression must be an id-expression. Assume that qualified
15526 names are the names of types so that:
15527
15528 template <class T>
15529 int S<T>::R::i = 3;
15530
15531 will work; we must treat `S<T>::R' as the name of a type.
15532 Similarly, assume that qualified names are templates, where
15533 required, so that:
15534
15535 template <class T>
15536 int S<T>::R<T>::i = 3;
15537
15538 will work, too. */
15539 id = cp_parser_id_expression (parser,
15540 /*template_keyword_p=*/false,
15541 /*check_dependency_p=*/false,
15542 /*template_p=*/NULL,
15543 /*declarator_p=*/true,
15544 optional_p);
15545 if (id && BASELINK_P (id))
15546 id = BASELINK_FUNCTIONS (id);
15547 return id;
15548 }
15549
15550 /* Parse a type-id.
15551
15552 type-id:
15553 type-specifier-seq abstract-declarator [opt]
15554
15555 Returns the TYPE specified. */
15556
15557 static tree
15558 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15559 bool is_trailing_return)
15560 {
15561 cp_decl_specifier_seq type_specifier_seq;
15562 cp_declarator *abstract_declarator;
15563
15564 /* Parse the type-specifier-seq. */
15565 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15566 is_trailing_return,
15567 &type_specifier_seq);
15568 if (type_specifier_seq.type == error_mark_node)
15569 return error_mark_node;
15570
15571 /* There might or might not be an abstract declarator. */
15572 cp_parser_parse_tentatively (parser);
15573 /* Look for the declarator. */
15574 abstract_declarator
15575 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15576 /*parenthesized_p=*/NULL,
15577 /*member_p=*/false);
15578 /* Check to see if there really was a declarator. */
15579 if (!cp_parser_parse_definitely (parser))
15580 abstract_declarator = NULL;
15581
15582 if (type_specifier_seq.type
15583 && type_uses_auto (type_specifier_seq.type))
15584 {
15585 /* A type-id with type 'auto' is only ok if the abstract declarator
15586 is a function declarator with a late-specified return type. */
15587 if (abstract_declarator
15588 && abstract_declarator->kind == cdk_function
15589 && abstract_declarator->u.function.late_return_type)
15590 /* OK */;
15591 else
15592 {
15593 error ("invalid use of %<auto%>");
15594 return error_mark_node;
15595 }
15596 }
15597
15598 return groktypename (&type_specifier_seq, abstract_declarator,
15599 is_template_arg);
15600 }
15601
15602 static tree cp_parser_type_id (cp_parser *parser)
15603 {
15604 return cp_parser_type_id_1 (parser, false, false);
15605 }
15606
15607 static tree cp_parser_template_type_arg (cp_parser *parser)
15608 {
15609 tree r;
15610 const char *saved_message = parser->type_definition_forbidden_message;
15611 parser->type_definition_forbidden_message
15612 = G_("types may not be defined in template arguments");
15613 r = cp_parser_type_id_1 (parser, true, false);
15614 parser->type_definition_forbidden_message = saved_message;
15615 return r;
15616 }
15617
15618 static tree cp_parser_trailing_type_id (cp_parser *parser)
15619 {
15620 return cp_parser_type_id_1 (parser, false, true);
15621 }
15622
15623 /* Parse a type-specifier-seq.
15624
15625 type-specifier-seq:
15626 type-specifier type-specifier-seq [opt]
15627
15628 GNU extension:
15629
15630 type-specifier-seq:
15631 attributes type-specifier-seq [opt]
15632
15633 If IS_DECLARATION is true, we are at the start of a "condition" or
15634 exception-declaration, so we might be followed by a declarator-id.
15635
15636 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15637 i.e. we've just seen "->".
15638
15639 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15640
15641 static void
15642 cp_parser_type_specifier_seq (cp_parser* parser,
15643 bool is_declaration,
15644 bool is_trailing_return,
15645 cp_decl_specifier_seq *type_specifier_seq)
15646 {
15647 bool seen_type_specifier = false;
15648 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15649 cp_token *start_token = NULL;
15650
15651 /* Clear the TYPE_SPECIFIER_SEQ. */
15652 clear_decl_specs (type_specifier_seq);
15653
15654 /* In the context of a trailing return type, enum E { } is an
15655 elaborated-type-specifier followed by a function-body, not an
15656 enum-specifier. */
15657 if (is_trailing_return)
15658 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15659
15660 /* Parse the type-specifiers and attributes. */
15661 while (true)
15662 {
15663 tree type_specifier;
15664 bool is_cv_qualifier;
15665
15666 /* Check for attributes first. */
15667 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15668 {
15669 type_specifier_seq->attributes =
15670 chainon (type_specifier_seq->attributes,
15671 cp_parser_attributes_opt (parser));
15672 continue;
15673 }
15674
15675 /* record the token of the beginning of the type specifier seq,
15676 for error reporting purposes*/
15677 if (!start_token)
15678 start_token = cp_lexer_peek_token (parser->lexer);
15679
15680 /* Look for the type-specifier. */
15681 type_specifier = cp_parser_type_specifier (parser,
15682 flags,
15683 type_specifier_seq,
15684 /*is_declaration=*/false,
15685 NULL,
15686 &is_cv_qualifier);
15687 if (!type_specifier)
15688 {
15689 /* If the first type-specifier could not be found, this is not a
15690 type-specifier-seq at all. */
15691 if (!seen_type_specifier)
15692 {
15693 cp_parser_error (parser, "expected type-specifier");
15694 type_specifier_seq->type = error_mark_node;
15695 return;
15696 }
15697 /* If subsequent type-specifiers could not be found, the
15698 type-specifier-seq is complete. */
15699 break;
15700 }
15701
15702 seen_type_specifier = true;
15703 /* The standard says that a condition can be:
15704
15705 type-specifier-seq declarator = assignment-expression
15706
15707 However, given:
15708
15709 struct S {};
15710 if (int S = ...)
15711
15712 we should treat the "S" as a declarator, not as a
15713 type-specifier. The standard doesn't say that explicitly for
15714 type-specifier-seq, but it does say that for
15715 decl-specifier-seq in an ordinary declaration. Perhaps it
15716 would be clearer just to allow a decl-specifier-seq here, and
15717 then add a semantic restriction that if any decl-specifiers
15718 that are not type-specifiers appear, the program is invalid. */
15719 if (is_declaration && !is_cv_qualifier)
15720 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15721 }
15722
15723 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15724 }
15725
15726 /* Parse a parameter-declaration-clause.
15727
15728 parameter-declaration-clause:
15729 parameter-declaration-list [opt] ... [opt]
15730 parameter-declaration-list , ...
15731
15732 Returns a representation for the parameter declarations. A return
15733 value of NULL indicates a parameter-declaration-clause consisting
15734 only of an ellipsis. */
15735
15736 static tree
15737 cp_parser_parameter_declaration_clause (cp_parser* parser)
15738 {
15739 tree parameters;
15740 cp_token *token;
15741 bool ellipsis_p;
15742 bool is_error;
15743
15744 /* Peek at the next token. */
15745 token = cp_lexer_peek_token (parser->lexer);
15746 /* Check for trivial parameter-declaration-clauses. */
15747 if (token->type == CPP_ELLIPSIS)
15748 {
15749 /* Consume the `...' token. */
15750 cp_lexer_consume_token (parser->lexer);
15751 return NULL_TREE;
15752 }
15753 else if (token->type == CPP_CLOSE_PAREN)
15754 /* There are no parameters. */
15755 {
15756 #ifndef NO_IMPLICIT_EXTERN_C
15757 if (in_system_header && current_class_type == NULL
15758 && current_lang_name == lang_name_c)
15759 return NULL_TREE;
15760 else
15761 #endif
15762 return void_list_node;
15763 }
15764 /* Check for `(void)', too, which is a special case. */
15765 else if (token->keyword == RID_VOID
15766 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15767 == CPP_CLOSE_PAREN))
15768 {
15769 /* Consume the `void' token. */
15770 cp_lexer_consume_token (parser->lexer);
15771 /* There are no parameters. */
15772 return void_list_node;
15773 }
15774
15775 /* Parse the parameter-declaration-list. */
15776 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15777 /* If a parse error occurred while parsing the
15778 parameter-declaration-list, then the entire
15779 parameter-declaration-clause is erroneous. */
15780 if (is_error)
15781 return NULL;
15782
15783 /* Peek at the next token. */
15784 token = cp_lexer_peek_token (parser->lexer);
15785 /* If it's a `,', the clause should terminate with an ellipsis. */
15786 if (token->type == CPP_COMMA)
15787 {
15788 /* Consume the `,'. */
15789 cp_lexer_consume_token (parser->lexer);
15790 /* Expect an ellipsis. */
15791 ellipsis_p
15792 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15793 }
15794 /* It might also be `...' if the optional trailing `,' was
15795 omitted. */
15796 else if (token->type == CPP_ELLIPSIS)
15797 {
15798 /* Consume the `...' token. */
15799 cp_lexer_consume_token (parser->lexer);
15800 /* And remember that we saw it. */
15801 ellipsis_p = true;
15802 }
15803 else
15804 ellipsis_p = false;
15805
15806 /* Finish the parameter list. */
15807 if (!ellipsis_p)
15808 parameters = chainon (parameters, void_list_node);
15809
15810 return parameters;
15811 }
15812
15813 /* Parse a parameter-declaration-list.
15814
15815 parameter-declaration-list:
15816 parameter-declaration
15817 parameter-declaration-list , parameter-declaration
15818
15819 Returns a representation of the parameter-declaration-list, as for
15820 cp_parser_parameter_declaration_clause. However, the
15821 `void_list_node' is never appended to the list. Upon return,
15822 *IS_ERROR will be true iff an error occurred. */
15823
15824 static tree
15825 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15826 {
15827 tree parameters = NULL_TREE;
15828 tree *tail = &parameters;
15829 bool saved_in_unbraced_linkage_specification_p;
15830 int index = 0;
15831
15832 /* Assume all will go well. */
15833 *is_error = false;
15834 /* The special considerations that apply to a function within an
15835 unbraced linkage specifications do not apply to the parameters
15836 to the function. */
15837 saved_in_unbraced_linkage_specification_p
15838 = parser->in_unbraced_linkage_specification_p;
15839 parser->in_unbraced_linkage_specification_p = false;
15840
15841 /* Look for more parameters. */
15842 while (true)
15843 {
15844 cp_parameter_declarator *parameter;
15845 tree decl = error_mark_node;
15846 bool parenthesized_p;
15847 /* Parse the parameter. */
15848 parameter
15849 = cp_parser_parameter_declaration (parser,
15850 /*template_parm_p=*/false,
15851 &parenthesized_p);
15852
15853 /* We don't know yet if the enclosing context is deprecated, so wait
15854 and warn in grokparms if appropriate. */
15855 deprecated_state = DEPRECATED_SUPPRESS;
15856
15857 if (parameter)
15858 decl = grokdeclarator (parameter->declarator,
15859 &parameter->decl_specifiers,
15860 PARM,
15861 parameter->default_argument != NULL_TREE,
15862 &parameter->decl_specifiers.attributes);
15863
15864 deprecated_state = DEPRECATED_NORMAL;
15865
15866 /* If a parse error occurred parsing the parameter declaration,
15867 then the entire parameter-declaration-list is erroneous. */
15868 if (decl == error_mark_node)
15869 {
15870 *is_error = true;
15871 parameters = error_mark_node;
15872 break;
15873 }
15874
15875 if (parameter->decl_specifiers.attributes)
15876 cplus_decl_attributes (&decl,
15877 parameter->decl_specifiers.attributes,
15878 0);
15879 if (DECL_NAME (decl))
15880 decl = pushdecl (decl);
15881
15882 if (decl != error_mark_node)
15883 {
15884 retrofit_lang_decl (decl);
15885 DECL_PARM_INDEX (decl) = ++index;
15886 DECL_PARM_LEVEL (decl) = function_parm_depth ();
15887 }
15888
15889 /* Add the new parameter to the list. */
15890 *tail = build_tree_list (parameter->default_argument, decl);
15891 tail = &TREE_CHAIN (*tail);
15892
15893 /* Peek at the next token. */
15894 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15895 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15896 /* These are for Objective-C++ */
15897 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15898 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15899 /* The parameter-declaration-list is complete. */
15900 break;
15901 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15902 {
15903 cp_token *token;
15904
15905 /* Peek at the next token. */
15906 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15907 /* If it's an ellipsis, then the list is complete. */
15908 if (token->type == CPP_ELLIPSIS)
15909 break;
15910 /* Otherwise, there must be more parameters. Consume the
15911 `,'. */
15912 cp_lexer_consume_token (parser->lexer);
15913 /* When parsing something like:
15914
15915 int i(float f, double d)
15916
15917 we can tell after seeing the declaration for "f" that we
15918 are not looking at an initialization of a variable "i",
15919 but rather at the declaration of a function "i".
15920
15921 Due to the fact that the parsing of template arguments
15922 (as specified to a template-id) requires backtracking we
15923 cannot use this technique when inside a template argument
15924 list. */
15925 if (!parser->in_template_argument_list_p
15926 && !parser->in_type_id_in_expr_p
15927 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15928 /* However, a parameter-declaration of the form
15929 "foat(f)" (which is a valid declaration of a
15930 parameter "f") can also be interpreted as an
15931 expression (the conversion of "f" to "float"). */
15932 && !parenthesized_p)
15933 cp_parser_commit_to_tentative_parse (parser);
15934 }
15935 else
15936 {
15937 cp_parser_error (parser, "expected %<,%> or %<...%>");
15938 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15939 cp_parser_skip_to_closing_parenthesis (parser,
15940 /*recovering=*/true,
15941 /*or_comma=*/false,
15942 /*consume_paren=*/false);
15943 break;
15944 }
15945 }
15946
15947 parser->in_unbraced_linkage_specification_p
15948 = saved_in_unbraced_linkage_specification_p;
15949
15950 return parameters;
15951 }
15952
15953 /* Parse a parameter declaration.
15954
15955 parameter-declaration:
15956 decl-specifier-seq ... [opt] declarator
15957 decl-specifier-seq declarator = assignment-expression
15958 decl-specifier-seq ... [opt] abstract-declarator [opt]
15959 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15960
15961 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15962 declares a template parameter. (In that case, a non-nested `>'
15963 token encountered during the parsing of the assignment-expression
15964 is not interpreted as a greater-than operator.)
15965
15966 Returns a representation of the parameter, or NULL if an error
15967 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15968 true iff the declarator is of the form "(p)". */
15969
15970 static cp_parameter_declarator *
15971 cp_parser_parameter_declaration (cp_parser *parser,
15972 bool template_parm_p,
15973 bool *parenthesized_p)
15974 {
15975 int declares_class_or_enum;
15976 cp_decl_specifier_seq decl_specifiers;
15977 cp_declarator *declarator;
15978 tree default_argument;
15979 cp_token *token = NULL, *declarator_token_start = NULL;
15980 const char *saved_message;
15981
15982 /* In a template parameter, `>' is not an operator.
15983
15984 [temp.param]
15985
15986 When parsing a default template-argument for a non-type
15987 template-parameter, the first non-nested `>' is taken as the end
15988 of the template parameter-list rather than a greater-than
15989 operator. */
15990
15991 /* Type definitions may not appear in parameter types. */
15992 saved_message = parser->type_definition_forbidden_message;
15993 parser->type_definition_forbidden_message
15994 = G_("types may not be defined in parameter types");
15995
15996 /* Parse the declaration-specifiers. */
15997 cp_parser_decl_specifier_seq (parser,
15998 CP_PARSER_FLAGS_NONE,
15999 &decl_specifiers,
16000 &declares_class_or_enum);
16001
16002 /* Complain about missing 'typename' or other invalid type names. */
16003 if (!decl_specifiers.any_type_specifiers_p)
16004 cp_parser_parse_and_diagnose_invalid_type_name (parser);
16005
16006 /* If an error occurred, there's no reason to attempt to parse the
16007 rest of the declaration. */
16008 if (cp_parser_error_occurred (parser))
16009 {
16010 parser->type_definition_forbidden_message = saved_message;
16011 return NULL;
16012 }
16013
16014 /* Peek at the next token. */
16015 token = cp_lexer_peek_token (parser->lexer);
16016
16017 /* If the next token is a `)', `,', `=', `>', or `...', then there
16018 is no declarator. However, when variadic templates are enabled,
16019 there may be a declarator following `...'. */
16020 if (token->type == CPP_CLOSE_PAREN
16021 || token->type == CPP_COMMA
16022 || token->type == CPP_EQ
16023 || token->type == CPP_GREATER)
16024 {
16025 declarator = NULL;
16026 if (parenthesized_p)
16027 *parenthesized_p = false;
16028 }
16029 /* Otherwise, there should be a declarator. */
16030 else
16031 {
16032 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16033 parser->default_arg_ok_p = false;
16034
16035 /* After seeing a decl-specifier-seq, if the next token is not a
16036 "(", there is no possibility that the code is a valid
16037 expression. Therefore, if parsing tentatively, we commit at
16038 this point. */
16039 if (!parser->in_template_argument_list_p
16040 /* In an expression context, having seen:
16041
16042 (int((char ...
16043
16044 we cannot be sure whether we are looking at a
16045 function-type (taking a "char" as a parameter) or a cast
16046 of some object of type "char" to "int". */
16047 && !parser->in_type_id_in_expr_p
16048 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16049 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16050 cp_parser_commit_to_tentative_parse (parser);
16051 /* Parse the declarator. */
16052 declarator_token_start = token;
16053 declarator = cp_parser_declarator (parser,
16054 CP_PARSER_DECLARATOR_EITHER,
16055 /*ctor_dtor_or_conv_p=*/NULL,
16056 parenthesized_p,
16057 /*member_p=*/false);
16058 parser->default_arg_ok_p = saved_default_arg_ok_p;
16059 /* After the declarator, allow more attributes. */
16060 decl_specifiers.attributes
16061 = chainon (decl_specifiers.attributes,
16062 cp_parser_attributes_opt (parser));
16063 }
16064
16065 /* If the next token is an ellipsis, and we have not seen a
16066 declarator name, and the type of the declarator contains parameter
16067 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16068 a parameter pack expansion expression. Otherwise, leave the
16069 ellipsis for a C-style variadic function. */
16070 token = cp_lexer_peek_token (parser->lexer);
16071 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16072 {
16073 tree type = decl_specifiers.type;
16074
16075 if (type && DECL_P (type))
16076 type = TREE_TYPE (type);
16077
16078 if (type
16079 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16080 && declarator_can_be_parameter_pack (declarator)
16081 && (!declarator || !declarator->parameter_pack_p)
16082 && uses_parameter_packs (type))
16083 {
16084 /* Consume the `...'. */
16085 cp_lexer_consume_token (parser->lexer);
16086 maybe_warn_variadic_templates ();
16087
16088 /* Build a pack expansion type */
16089 if (declarator)
16090 declarator->parameter_pack_p = true;
16091 else
16092 decl_specifiers.type = make_pack_expansion (type);
16093 }
16094 }
16095
16096 /* The restriction on defining new types applies only to the type
16097 of the parameter, not to the default argument. */
16098 parser->type_definition_forbidden_message = saved_message;
16099
16100 /* If the next token is `=', then process a default argument. */
16101 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16102 {
16103 /* Consume the `='. */
16104 cp_lexer_consume_token (parser->lexer);
16105
16106 /* If we are defining a class, then the tokens that make up the
16107 default argument must be saved and processed later. */
16108 if (!template_parm_p && at_class_scope_p ()
16109 && TYPE_BEING_DEFINED (current_class_type)
16110 && !LAMBDA_TYPE_P (current_class_type))
16111 {
16112 unsigned depth = 0;
16113 int maybe_template_id = 0;
16114 cp_token *first_token;
16115 cp_token *token;
16116
16117 /* Add tokens until we have processed the entire default
16118 argument. We add the range [first_token, token). */
16119 first_token = cp_lexer_peek_token (parser->lexer);
16120 while (true)
16121 {
16122 bool done = false;
16123
16124 /* Peek at the next token. */
16125 token = cp_lexer_peek_token (parser->lexer);
16126 /* What we do depends on what token we have. */
16127 switch (token->type)
16128 {
16129 /* In valid code, a default argument must be
16130 immediately followed by a `,' `)', or `...'. */
16131 case CPP_COMMA:
16132 if (depth == 0 && maybe_template_id)
16133 {
16134 /* If we've seen a '<', we might be in a
16135 template-argument-list. Until Core issue 325 is
16136 resolved, we don't know how this situation ought
16137 to be handled, so try to DTRT. We check whether
16138 what comes after the comma is a valid parameter
16139 declaration list. If it is, then the comma ends
16140 the default argument; otherwise the default
16141 argument continues. */
16142 bool error = false;
16143 tree t;
16144
16145 /* Set ITALP so cp_parser_parameter_declaration_list
16146 doesn't decide to commit to this parse. */
16147 bool saved_italp = parser->in_template_argument_list_p;
16148 parser->in_template_argument_list_p = true;
16149
16150 cp_parser_parse_tentatively (parser);
16151 cp_lexer_consume_token (parser->lexer);
16152 begin_scope (sk_function_parms, NULL_TREE);
16153 cp_parser_parameter_declaration_list (parser, &error);
16154 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16155 pop_binding (DECL_NAME (t), t);
16156 leave_scope ();
16157 if (!cp_parser_error_occurred (parser) && !error)
16158 done = true;
16159 cp_parser_abort_tentative_parse (parser);
16160
16161 parser->in_template_argument_list_p = saved_italp;
16162 break;
16163 }
16164 case CPP_CLOSE_PAREN:
16165 case CPP_ELLIPSIS:
16166 /* If we run into a non-nested `;', `}', or `]',
16167 then the code is invalid -- but the default
16168 argument is certainly over. */
16169 case CPP_SEMICOLON:
16170 case CPP_CLOSE_BRACE:
16171 case CPP_CLOSE_SQUARE:
16172 if (depth == 0)
16173 done = true;
16174 /* Update DEPTH, if necessary. */
16175 else if (token->type == CPP_CLOSE_PAREN
16176 || token->type == CPP_CLOSE_BRACE
16177 || token->type == CPP_CLOSE_SQUARE)
16178 --depth;
16179 break;
16180
16181 case CPP_OPEN_PAREN:
16182 case CPP_OPEN_SQUARE:
16183 case CPP_OPEN_BRACE:
16184 ++depth;
16185 break;
16186
16187 case CPP_LESS:
16188 if (depth == 0)
16189 /* This might be the comparison operator, or it might
16190 start a template argument list. */
16191 ++maybe_template_id;
16192 break;
16193
16194 case CPP_RSHIFT:
16195 if (cxx_dialect == cxx98)
16196 break;
16197 /* Fall through for C++0x, which treats the `>>'
16198 operator like two `>' tokens in certain
16199 cases. */
16200
16201 case CPP_GREATER:
16202 if (depth == 0)
16203 {
16204 /* This might be an operator, or it might close a
16205 template argument list. But if a previous '<'
16206 started a template argument list, this will have
16207 closed it, so we can't be in one anymore. */
16208 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16209 if (maybe_template_id < 0)
16210 maybe_template_id = 0;
16211 }
16212 break;
16213
16214 /* If we run out of tokens, issue an error message. */
16215 case CPP_EOF:
16216 case CPP_PRAGMA_EOL:
16217 error_at (token->location, "file ends in default argument");
16218 done = true;
16219 break;
16220
16221 case CPP_NAME:
16222 case CPP_SCOPE:
16223 /* In these cases, we should look for template-ids.
16224 For example, if the default argument is
16225 `X<int, double>()', we need to do name lookup to
16226 figure out whether or not `X' is a template; if
16227 so, the `,' does not end the default argument.
16228
16229 That is not yet done. */
16230 break;
16231
16232 default:
16233 break;
16234 }
16235
16236 /* If we've reached the end, stop. */
16237 if (done)
16238 break;
16239
16240 /* Add the token to the token block. */
16241 token = cp_lexer_consume_token (parser->lexer);
16242 }
16243
16244 /* Create a DEFAULT_ARG to represent the unparsed default
16245 argument. */
16246 default_argument = make_node (DEFAULT_ARG);
16247 DEFARG_TOKENS (default_argument)
16248 = cp_token_cache_new (first_token, token);
16249 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16250 }
16251 /* Outside of a class definition, we can just parse the
16252 assignment-expression. */
16253 else
16254 {
16255 token = cp_lexer_peek_token (parser->lexer);
16256 default_argument
16257 = cp_parser_default_argument (parser, template_parm_p);
16258 }
16259
16260 if (!parser->default_arg_ok_p)
16261 {
16262 if (flag_permissive)
16263 warning (0, "deprecated use of default argument for parameter of non-function");
16264 else
16265 {
16266 error_at (token->location,
16267 "default arguments are only "
16268 "permitted for function parameters");
16269 default_argument = NULL_TREE;
16270 }
16271 }
16272 else if ((declarator && declarator->parameter_pack_p)
16273 || (decl_specifiers.type
16274 && PACK_EXPANSION_P (decl_specifiers.type)))
16275 {
16276 /* Find the name of the parameter pack. */
16277 cp_declarator *id_declarator = declarator;
16278 while (id_declarator && id_declarator->kind != cdk_id)
16279 id_declarator = id_declarator->declarator;
16280
16281 if (id_declarator && id_declarator->kind == cdk_id)
16282 error_at (declarator_token_start->location,
16283 template_parm_p
16284 ? "template parameter pack %qD"
16285 " cannot have a default argument"
16286 : "parameter pack %qD cannot have a default argument",
16287 id_declarator->u.id.unqualified_name);
16288 else
16289 error_at (declarator_token_start->location,
16290 template_parm_p
16291 ? "template parameter pack cannot have a default argument"
16292 : "parameter pack cannot have a default argument");
16293
16294 default_argument = NULL_TREE;
16295 }
16296 }
16297 else
16298 default_argument = NULL_TREE;
16299
16300 return make_parameter_declarator (&decl_specifiers,
16301 declarator,
16302 default_argument);
16303 }
16304
16305 /* Parse a default argument and return it.
16306
16307 TEMPLATE_PARM_P is true if this is a default argument for a
16308 non-type template parameter. */
16309 static tree
16310 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16311 {
16312 tree default_argument = NULL_TREE;
16313 bool saved_greater_than_is_operator_p;
16314 bool saved_local_variables_forbidden_p;
16315
16316 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16317 set correctly. */
16318 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16319 parser->greater_than_is_operator_p = !template_parm_p;
16320 /* Local variable names (and the `this' keyword) may not
16321 appear in a default argument. */
16322 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16323 parser->local_variables_forbidden_p = true;
16324 /* Parse the assignment-expression. */
16325 if (template_parm_p)
16326 push_deferring_access_checks (dk_no_deferred);
16327 default_argument
16328 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16329 if (template_parm_p)
16330 pop_deferring_access_checks ();
16331 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16332 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16333
16334 return default_argument;
16335 }
16336
16337 /* Parse a function-body.
16338
16339 function-body:
16340 compound_statement */
16341
16342 static void
16343 cp_parser_function_body (cp_parser *parser)
16344 {
16345 cp_parser_compound_statement (parser, NULL, false, true);
16346 }
16347
16348 /* Parse a ctor-initializer-opt followed by a function-body. Return
16349 true if a ctor-initializer was present. */
16350
16351 static bool
16352 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16353 {
16354 tree body, list;
16355 bool ctor_initializer_p;
16356 const bool check_body_p =
16357 DECL_CONSTRUCTOR_P (current_function_decl)
16358 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16359 tree last = NULL;
16360
16361 /* Begin the function body. */
16362 body = begin_function_body ();
16363 /* Parse the optional ctor-initializer. */
16364 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16365
16366 /* If we're parsing a constexpr constructor definition, we need
16367 to check that the constructor body is indeed empty. However,
16368 before we get to cp_parser_function_body lot of junk has been
16369 generated, so we can't just check that we have an empty block.
16370 Rather we take a snapshot of the outermost block, and check whether
16371 cp_parser_function_body changed its state. */
16372 if (check_body_p)
16373 {
16374 list = body;
16375 if (TREE_CODE (list) == BIND_EXPR)
16376 list = BIND_EXPR_BODY (list);
16377 if (TREE_CODE (list) == STATEMENT_LIST
16378 && STATEMENT_LIST_TAIL (list) != NULL)
16379 last = STATEMENT_LIST_TAIL (list)->stmt;
16380 }
16381 /* Parse the function-body. */
16382 cp_parser_function_body (parser);
16383 if (check_body_p)
16384 check_constexpr_ctor_body (last, list);
16385 /* Finish the function body. */
16386 finish_function_body (body);
16387
16388 return ctor_initializer_p;
16389 }
16390
16391 /* Parse an initializer.
16392
16393 initializer:
16394 = initializer-clause
16395 ( expression-list )
16396
16397 Returns an expression representing the initializer. If no
16398 initializer is present, NULL_TREE is returned.
16399
16400 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16401 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16402 set to TRUE if there is no initializer present. If there is an
16403 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16404 is set to true; otherwise it is set to false. */
16405
16406 static tree
16407 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16408 bool* non_constant_p)
16409 {
16410 cp_token *token;
16411 tree init;
16412
16413 /* Peek at the next token. */
16414 token = cp_lexer_peek_token (parser->lexer);
16415
16416 /* Let our caller know whether or not this initializer was
16417 parenthesized. */
16418 *is_direct_init = (token->type != CPP_EQ);
16419 /* Assume that the initializer is constant. */
16420 *non_constant_p = false;
16421
16422 if (token->type == CPP_EQ)
16423 {
16424 /* Consume the `='. */
16425 cp_lexer_consume_token (parser->lexer);
16426 /* Parse the initializer-clause. */
16427 init = cp_parser_initializer_clause (parser, non_constant_p);
16428 }
16429 else if (token->type == CPP_OPEN_PAREN)
16430 {
16431 VEC(tree,gc) *vec;
16432 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16433 /*cast_p=*/false,
16434 /*allow_expansion_p=*/true,
16435 non_constant_p);
16436 if (vec == NULL)
16437 return error_mark_node;
16438 init = build_tree_list_vec (vec);
16439 release_tree_vector (vec);
16440 }
16441 else if (token->type == CPP_OPEN_BRACE)
16442 {
16443 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16444 init = cp_parser_braced_list (parser, non_constant_p);
16445 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16446 }
16447 else
16448 {
16449 /* Anything else is an error. */
16450 cp_parser_error (parser, "expected initializer");
16451 init = error_mark_node;
16452 }
16453
16454 return init;
16455 }
16456
16457 /* Parse an initializer-clause.
16458
16459 initializer-clause:
16460 assignment-expression
16461 braced-init-list
16462
16463 Returns an expression representing the initializer.
16464
16465 If the `assignment-expression' production is used the value
16466 returned is simply a representation for the expression.
16467
16468 Otherwise, calls cp_parser_braced_list. */
16469
16470 static tree
16471 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16472 {
16473 tree initializer;
16474
16475 /* Assume the expression is constant. */
16476 *non_constant_p = false;
16477
16478 /* If it is not a `{', then we are looking at an
16479 assignment-expression. */
16480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16481 {
16482 initializer
16483 = cp_parser_constant_expression (parser,
16484 /*allow_non_constant_p=*/true,
16485 non_constant_p);
16486 if (!*non_constant_p)
16487 {
16488 /* We only want to fold if this is really a constant
16489 expression. FIXME Actually, we don't want to fold here, but in
16490 cp_finish_decl. */
16491 tree folded = fold_non_dependent_expr (initializer);
16492 folded = maybe_constant_value (folded);
16493 if (TREE_CONSTANT (folded))
16494 initializer = folded;
16495 }
16496 }
16497 else
16498 initializer = cp_parser_braced_list (parser, non_constant_p);
16499
16500 return initializer;
16501 }
16502
16503 /* Parse a brace-enclosed initializer list.
16504
16505 braced-init-list:
16506 { initializer-list , [opt] }
16507 { }
16508
16509 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16510 the elements of the initializer-list (or NULL, if the last
16511 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16512 NULL_TREE. There is no way to detect whether or not the optional
16513 trailing `,' was provided. NON_CONSTANT_P is as for
16514 cp_parser_initializer. */
16515
16516 static tree
16517 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16518 {
16519 tree initializer;
16520
16521 /* Consume the `{' token. */
16522 cp_lexer_consume_token (parser->lexer);
16523 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16524 initializer = make_node (CONSTRUCTOR);
16525 /* If it's not a `}', then there is a non-trivial initializer. */
16526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16527 {
16528 /* Parse the initializer list. */
16529 CONSTRUCTOR_ELTS (initializer)
16530 = cp_parser_initializer_list (parser, non_constant_p);
16531 /* A trailing `,' token is allowed. */
16532 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16533 cp_lexer_consume_token (parser->lexer);
16534 }
16535 /* Now, there should be a trailing `}'. */
16536 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16537 TREE_TYPE (initializer) = init_list_type_node;
16538 return initializer;
16539 }
16540
16541 /* Parse an initializer-list.
16542
16543 initializer-list:
16544 initializer-clause ... [opt]
16545 initializer-list , initializer-clause ... [opt]
16546
16547 GNU Extension:
16548
16549 initializer-list:
16550 identifier : initializer-clause
16551 initializer-list, identifier : initializer-clause
16552
16553 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16554 for the initializer. If the INDEX of the elt is non-NULL, it is the
16555 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16556 as for cp_parser_initializer. */
16557
16558 static VEC(constructor_elt,gc) *
16559 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16560 {
16561 VEC(constructor_elt,gc) *v = NULL;
16562
16563 /* Assume all of the expressions are constant. */
16564 *non_constant_p = false;
16565
16566 /* Parse the rest of the list. */
16567 while (true)
16568 {
16569 cp_token *token;
16570 tree identifier;
16571 tree initializer;
16572 bool clause_non_constant_p;
16573
16574 /* If the next token is an identifier and the following one is a
16575 colon, we are looking at the GNU designated-initializer
16576 syntax. */
16577 if (cp_parser_allow_gnu_extensions_p (parser)
16578 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16579 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16580 {
16581 /* Warn the user that they are using an extension. */
16582 pedwarn (input_location, OPT_pedantic,
16583 "ISO C++ does not allow designated initializers");
16584 /* Consume the identifier. */
16585 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16586 /* Consume the `:'. */
16587 cp_lexer_consume_token (parser->lexer);
16588 }
16589 else
16590 identifier = NULL_TREE;
16591
16592 /* Parse the initializer. */
16593 initializer = cp_parser_initializer_clause (parser,
16594 &clause_non_constant_p);
16595 /* If any clause is non-constant, so is the entire initializer. */
16596 if (clause_non_constant_p)
16597 *non_constant_p = true;
16598
16599 /* If we have an ellipsis, this is an initializer pack
16600 expansion. */
16601 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16602 {
16603 /* Consume the `...'. */
16604 cp_lexer_consume_token (parser->lexer);
16605
16606 /* Turn the initializer into an initializer expansion. */
16607 initializer = make_pack_expansion (initializer);
16608 }
16609
16610 /* Add it to the vector. */
16611 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16612
16613 /* If the next token is not a comma, we have reached the end of
16614 the list. */
16615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16616 break;
16617
16618 /* Peek at the next token. */
16619 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16620 /* If the next token is a `}', then we're still done. An
16621 initializer-clause can have a trailing `,' after the
16622 initializer-list and before the closing `}'. */
16623 if (token->type == CPP_CLOSE_BRACE)
16624 break;
16625
16626 /* Consume the `,' token. */
16627 cp_lexer_consume_token (parser->lexer);
16628 }
16629
16630 return v;
16631 }
16632
16633 /* Classes [gram.class] */
16634
16635 /* Parse a class-name.
16636
16637 class-name:
16638 identifier
16639 template-id
16640
16641 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16642 to indicate that names looked up in dependent types should be
16643 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16644 keyword has been used to indicate that the name that appears next
16645 is a template. TAG_TYPE indicates the explicit tag given before
16646 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16647 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16648 is the class being defined in a class-head.
16649
16650 Returns the TYPE_DECL representing the class. */
16651
16652 static tree
16653 cp_parser_class_name (cp_parser *parser,
16654 bool typename_keyword_p,
16655 bool template_keyword_p,
16656 enum tag_types tag_type,
16657 bool check_dependency_p,
16658 bool class_head_p,
16659 bool is_declaration)
16660 {
16661 tree decl;
16662 tree scope;
16663 bool typename_p;
16664 cp_token *token;
16665 tree identifier = NULL_TREE;
16666
16667 /* All class-names start with an identifier. */
16668 token = cp_lexer_peek_token (parser->lexer);
16669 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16670 {
16671 cp_parser_error (parser, "expected class-name");
16672 return error_mark_node;
16673 }
16674
16675 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16676 to a template-id, so we save it here. */
16677 scope = parser->scope;
16678 if (scope == error_mark_node)
16679 return error_mark_node;
16680
16681 /* Any name names a type if we're following the `typename' keyword
16682 in a qualified name where the enclosing scope is type-dependent. */
16683 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16684 && dependent_type_p (scope));
16685 /* Handle the common case (an identifier, but not a template-id)
16686 efficiently. */
16687 if (token->type == CPP_NAME
16688 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16689 {
16690 cp_token *identifier_token;
16691 bool ambiguous_p;
16692
16693 /* Look for the identifier. */
16694 identifier_token = cp_lexer_peek_token (parser->lexer);
16695 ambiguous_p = identifier_token->ambiguous_p;
16696 identifier = cp_parser_identifier (parser);
16697 /* If the next token isn't an identifier, we are certainly not
16698 looking at a class-name. */
16699 if (identifier == error_mark_node)
16700 decl = error_mark_node;
16701 /* If we know this is a type-name, there's no need to look it
16702 up. */
16703 else if (typename_p)
16704 decl = identifier;
16705 else
16706 {
16707 tree ambiguous_decls;
16708 /* If we already know that this lookup is ambiguous, then
16709 we've already issued an error message; there's no reason
16710 to check again. */
16711 if (ambiguous_p)
16712 {
16713 cp_parser_simulate_error (parser);
16714 return error_mark_node;
16715 }
16716 /* If the next token is a `::', then the name must be a type
16717 name.
16718
16719 [basic.lookup.qual]
16720
16721 During the lookup for a name preceding the :: scope
16722 resolution operator, object, function, and enumerator
16723 names are ignored. */
16724 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16725 tag_type = typename_type;
16726 /* Look up the name. */
16727 decl = cp_parser_lookup_name (parser, identifier,
16728 tag_type,
16729 /*is_template=*/false,
16730 /*is_namespace=*/false,
16731 check_dependency_p,
16732 &ambiguous_decls,
16733 identifier_token->location);
16734 if (ambiguous_decls)
16735 {
16736 if (cp_parser_parsing_tentatively (parser))
16737 cp_parser_simulate_error (parser);
16738 return error_mark_node;
16739 }
16740 }
16741 }
16742 else
16743 {
16744 /* Try a template-id. */
16745 decl = cp_parser_template_id (parser, template_keyword_p,
16746 check_dependency_p,
16747 is_declaration);
16748 if (decl == error_mark_node)
16749 return error_mark_node;
16750 }
16751
16752 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16753
16754 /* If this is a typename, create a TYPENAME_TYPE. */
16755 if (typename_p && decl != error_mark_node)
16756 {
16757 decl = make_typename_type (scope, decl, typename_type,
16758 /*complain=*/tf_error);
16759 if (decl != error_mark_node)
16760 decl = TYPE_NAME (decl);
16761 }
16762
16763 /* Check to see that it is really the name of a class. */
16764 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16765 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16766 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16767 /* Situations like this:
16768
16769 template <typename T> struct A {
16770 typename T::template X<int>::I i;
16771 };
16772
16773 are problematic. Is `T::template X<int>' a class-name? The
16774 standard does not seem to be definitive, but there is no other
16775 valid interpretation of the following `::'. Therefore, those
16776 names are considered class-names. */
16777 {
16778 decl = make_typename_type (scope, decl, tag_type, tf_error);
16779 if (decl != error_mark_node)
16780 decl = TYPE_NAME (decl);
16781 }
16782 else if (TREE_CODE (decl) != TYPE_DECL
16783 || TREE_TYPE (decl) == error_mark_node
16784 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16785 /* In Objective-C 2.0, a classname followed by '.' starts a
16786 dot-syntax expression, and it's not a type-name. */
16787 || (c_dialect_objc ()
16788 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16789 && objc_is_class_name (decl)))
16790 decl = error_mark_node;
16791
16792 if (decl == error_mark_node)
16793 cp_parser_error (parser, "expected class-name");
16794 else if (identifier && !parser->scope)
16795 maybe_note_name_used_in_class (identifier, decl);
16796
16797 return decl;
16798 }
16799
16800 /* Parse a class-specifier.
16801
16802 class-specifier:
16803 class-head { member-specification [opt] }
16804
16805 Returns the TREE_TYPE representing the class. */
16806
16807 static tree
16808 cp_parser_class_specifier_1 (cp_parser* parser)
16809 {
16810 tree type;
16811 tree attributes = NULL_TREE;
16812 bool nested_name_specifier_p;
16813 unsigned saved_num_template_parameter_lists;
16814 bool saved_in_function_body;
16815 bool saved_in_unbraced_linkage_specification_p;
16816 tree old_scope = NULL_TREE;
16817 tree scope = NULL_TREE;
16818 tree bases;
16819 cp_token *closing_brace;
16820
16821 push_deferring_access_checks (dk_no_deferred);
16822
16823 /* Parse the class-head. */
16824 type = cp_parser_class_head (parser,
16825 &nested_name_specifier_p,
16826 &attributes,
16827 &bases);
16828 /* If the class-head was a semantic disaster, skip the entire body
16829 of the class. */
16830 if (!type)
16831 {
16832 cp_parser_skip_to_end_of_block_or_statement (parser);
16833 pop_deferring_access_checks ();
16834 return error_mark_node;
16835 }
16836
16837 /* Look for the `{'. */
16838 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16839 {
16840 pop_deferring_access_checks ();
16841 return error_mark_node;
16842 }
16843
16844 /* Process the base classes. If they're invalid, skip the
16845 entire class body. */
16846 if (!xref_basetypes (type, bases))
16847 {
16848 /* Consuming the closing brace yields better error messages
16849 later on. */
16850 if (cp_parser_skip_to_closing_brace (parser))
16851 cp_lexer_consume_token (parser->lexer);
16852 pop_deferring_access_checks ();
16853 return error_mark_node;
16854 }
16855
16856 /* Issue an error message if type-definitions are forbidden here. */
16857 cp_parser_check_type_definition (parser);
16858 /* Remember that we are defining one more class. */
16859 ++parser->num_classes_being_defined;
16860 /* Inside the class, surrounding template-parameter-lists do not
16861 apply. */
16862 saved_num_template_parameter_lists
16863 = parser->num_template_parameter_lists;
16864 parser->num_template_parameter_lists = 0;
16865 /* We are not in a function body. */
16866 saved_in_function_body = parser->in_function_body;
16867 parser->in_function_body = false;
16868 /* We are not immediately inside an extern "lang" block. */
16869 saved_in_unbraced_linkage_specification_p
16870 = parser->in_unbraced_linkage_specification_p;
16871 parser->in_unbraced_linkage_specification_p = false;
16872
16873 /* Start the class. */
16874 if (nested_name_specifier_p)
16875 {
16876 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16877 old_scope = push_inner_scope (scope);
16878 }
16879 type = begin_class_definition (type, attributes);
16880
16881 if (type == error_mark_node)
16882 /* If the type is erroneous, skip the entire body of the class. */
16883 cp_parser_skip_to_closing_brace (parser);
16884 else
16885 /* Parse the member-specification. */
16886 cp_parser_member_specification_opt (parser);
16887
16888 /* Look for the trailing `}'. */
16889 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16890 /* Look for trailing attributes to apply to this class. */
16891 if (cp_parser_allow_gnu_extensions_p (parser))
16892 attributes = cp_parser_attributes_opt (parser);
16893 if (type != error_mark_node)
16894 type = finish_struct (type, attributes);
16895 if (nested_name_specifier_p)
16896 pop_inner_scope (old_scope, scope);
16897
16898 /* We've finished a type definition. Check for the common syntax
16899 error of forgetting a semicolon after the definition. We need to
16900 be careful, as we can't just check for not-a-semicolon and be done
16901 with it; the user might have typed:
16902
16903 class X { } c = ...;
16904 class X { } *p = ...;
16905
16906 and so forth. Instead, enumerate all the possible tokens that
16907 might follow this production; if we don't see one of them, then
16908 complain and silently insert the semicolon. */
16909 {
16910 cp_token *token = cp_lexer_peek_token (parser->lexer);
16911 bool want_semicolon = true;
16912
16913 switch (token->type)
16914 {
16915 case CPP_NAME:
16916 case CPP_SEMICOLON:
16917 case CPP_MULT:
16918 case CPP_AND:
16919 case CPP_OPEN_PAREN:
16920 case CPP_CLOSE_PAREN:
16921 case CPP_COMMA:
16922 want_semicolon = false;
16923 break;
16924
16925 /* While it's legal for type qualifiers and storage class
16926 specifiers to follow type definitions in the grammar, only
16927 compiler testsuites contain code like that. Assume that if
16928 we see such code, then what we're really seeing is a case
16929 like:
16930
16931 class X { }
16932 const <type> var = ...;
16933
16934 or
16935
16936 class Y { }
16937 static <type> func (...) ...
16938
16939 i.e. the qualifier or specifier applies to the next
16940 declaration. To do so, however, we need to look ahead one
16941 more token to see if *that* token is a type specifier.
16942
16943 This code could be improved to handle:
16944
16945 class Z { }
16946 static const <type> var = ...; */
16947 case CPP_KEYWORD:
16948 if (keyword_is_decl_specifier (token->keyword))
16949 {
16950 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16951
16952 /* Handling user-defined types here would be nice, but very
16953 tricky. */
16954 want_semicolon
16955 = (lookahead->type == CPP_KEYWORD
16956 && keyword_begins_type_specifier (lookahead->keyword));
16957 }
16958 break;
16959 default:
16960 break;
16961 }
16962
16963 /* If we don't have a type, then something is very wrong and we
16964 shouldn't try to do anything clever. Likewise for not seeing the
16965 closing brace. */
16966 if (closing_brace && TYPE_P (type) && want_semicolon)
16967 {
16968 cp_token_position prev
16969 = cp_lexer_previous_token_position (parser->lexer);
16970 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16971 location_t loc = prev_token->location;
16972
16973 if (CLASSTYPE_DECLARED_CLASS (type))
16974 error_at (loc, "expected %<;%> after class definition");
16975 else if (TREE_CODE (type) == RECORD_TYPE)
16976 error_at (loc, "expected %<;%> after struct definition");
16977 else if (TREE_CODE (type) == UNION_TYPE)
16978 error_at (loc, "expected %<;%> after union definition");
16979 else
16980 gcc_unreachable ();
16981
16982 /* Unget one token and smash it to look as though we encountered
16983 a semicolon in the input stream. */
16984 cp_lexer_set_token_position (parser->lexer, prev);
16985 token = cp_lexer_peek_token (parser->lexer);
16986 token->type = CPP_SEMICOLON;
16987 token->keyword = RID_MAX;
16988 }
16989 }
16990
16991 /* If this class is not itself within the scope of another class,
16992 then we need to parse the bodies of all of the queued function
16993 definitions. Note that the queued functions defined in a class
16994 are not always processed immediately following the
16995 class-specifier for that class. Consider:
16996
16997 struct A {
16998 struct B { void f() { sizeof (A); } };
16999 };
17000
17001 If `f' were processed before the processing of `A' were
17002 completed, there would be no way to compute the size of `A'.
17003 Note that the nesting we are interested in here is lexical --
17004 not the semantic nesting given by TYPE_CONTEXT. In particular,
17005 for:
17006
17007 struct A { struct B; };
17008 struct A::B { void f() { } };
17009
17010 there is no need to delay the parsing of `A::B::f'. */
17011 if (--parser->num_classes_being_defined == 0)
17012 {
17013 tree fn;
17014 tree class_type = NULL_TREE;
17015 tree pushed_scope = NULL_TREE;
17016 unsigned ix;
17017 cp_default_arg_entry *e;
17018
17019 /* In a first pass, parse default arguments to the functions.
17020 Then, in a second pass, parse the bodies of the functions.
17021 This two-phased approach handles cases like:
17022
17023 struct S {
17024 void f() { g(); }
17025 void g(int i = 3);
17026 };
17027
17028 */
17029 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17030 ix, e)
17031 {
17032 fn = e->decl;
17033 /* If there are default arguments that have not yet been processed,
17034 take care of them now. */
17035 if (class_type != e->class_type)
17036 {
17037 if (pushed_scope)
17038 pop_scope (pushed_scope);
17039 class_type = e->class_type;
17040 pushed_scope = push_scope (class_type);
17041 }
17042 /* Make sure that any template parameters are in scope. */
17043 maybe_begin_member_template_processing (fn);
17044 /* Parse the default argument expressions. */
17045 cp_parser_late_parsing_default_args (parser, fn);
17046 /* Remove any template parameters from the symbol table. */
17047 maybe_end_member_template_processing ();
17048 }
17049 if (pushed_scope)
17050 pop_scope (pushed_scope);
17051 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17052 /* Now parse the body of the functions. */
17053 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17054 cp_parser_late_parsing_for_member (parser, fn);
17055 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17056 }
17057
17058 /* Put back any saved access checks. */
17059 pop_deferring_access_checks ();
17060
17061 /* Restore saved state. */
17062 parser->in_function_body = saved_in_function_body;
17063 parser->num_template_parameter_lists
17064 = saved_num_template_parameter_lists;
17065 parser->in_unbraced_linkage_specification_p
17066 = saved_in_unbraced_linkage_specification_p;
17067
17068 return type;
17069 }
17070
17071 static tree
17072 cp_parser_class_specifier (cp_parser* parser)
17073 {
17074 tree ret;
17075 timevar_push (TV_PARSE_STRUCT);
17076 ret = cp_parser_class_specifier_1 (parser);
17077 timevar_pop (TV_PARSE_STRUCT);
17078 return ret;
17079 }
17080
17081 /* Parse a class-head.
17082
17083 class-head:
17084 class-key identifier [opt] base-clause [opt]
17085 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17086 class-key nested-name-specifier [opt] template-id
17087 base-clause [opt]
17088
17089 class-virt-specifier:
17090 final
17091
17092 GNU Extensions:
17093 class-key attributes identifier [opt] base-clause [opt]
17094 class-key attributes nested-name-specifier identifier base-clause [opt]
17095 class-key attributes nested-name-specifier [opt] template-id
17096 base-clause [opt]
17097
17098 Upon return BASES is initialized to the list of base classes (or
17099 NULL, if there are none) in the same form returned by
17100 cp_parser_base_clause.
17101
17102 Returns the TYPE of the indicated class. Sets
17103 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17104 involving a nested-name-specifier was used, and FALSE otherwise.
17105
17106 Returns error_mark_node if this is not a class-head.
17107
17108 Returns NULL_TREE if the class-head is syntactically valid, but
17109 semantically invalid in a way that means we should skip the entire
17110 body of the class. */
17111
17112 static tree
17113 cp_parser_class_head (cp_parser* parser,
17114 bool* nested_name_specifier_p,
17115 tree *attributes_p,
17116 tree *bases)
17117 {
17118 tree nested_name_specifier;
17119 enum tag_types class_key;
17120 tree id = NULL_TREE;
17121 tree type = NULL_TREE;
17122 tree attributes;
17123 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17124 bool template_id_p = false;
17125 bool qualified_p = false;
17126 bool invalid_nested_name_p = false;
17127 bool invalid_explicit_specialization_p = false;
17128 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17129 tree pushed_scope = NULL_TREE;
17130 unsigned num_templates;
17131 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17132 /* Assume no nested-name-specifier will be present. */
17133 *nested_name_specifier_p = false;
17134 /* Assume no template parameter lists will be used in defining the
17135 type. */
17136 num_templates = 0;
17137 parser->colon_corrects_to_scope_p = false;
17138
17139 *bases = NULL_TREE;
17140
17141 /* Look for the class-key. */
17142 class_key = cp_parser_class_key (parser);
17143 if (class_key == none_type)
17144 return error_mark_node;
17145
17146 /* Parse the attributes. */
17147 attributes = cp_parser_attributes_opt (parser);
17148
17149 /* If the next token is `::', that is invalid -- but sometimes
17150 people do try to write:
17151
17152 struct ::S {};
17153
17154 Handle this gracefully by accepting the extra qualifier, and then
17155 issuing an error about it later if this really is a
17156 class-head. If it turns out just to be an elaborated type
17157 specifier, remain silent. */
17158 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17159 qualified_p = true;
17160
17161 push_deferring_access_checks (dk_no_check);
17162
17163 /* Determine the name of the class. Begin by looking for an
17164 optional nested-name-specifier. */
17165 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17166 nested_name_specifier
17167 = cp_parser_nested_name_specifier_opt (parser,
17168 /*typename_keyword_p=*/false,
17169 /*check_dependency_p=*/false,
17170 /*type_p=*/false,
17171 /*is_declaration=*/false);
17172 /* If there was a nested-name-specifier, then there *must* be an
17173 identifier. */
17174 if (nested_name_specifier)
17175 {
17176 type_start_token = cp_lexer_peek_token (parser->lexer);
17177 /* Although the grammar says `identifier', it really means
17178 `class-name' or `template-name'. You are only allowed to
17179 define a class that has already been declared with this
17180 syntax.
17181
17182 The proposed resolution for Core Issue 180 says that wherever
17183 you see `class T::X' you should treat `X' as a type-name.
17184
17185 It is OK to define an inaccessible class; for example:
17186
17187 class A { class B; };
17188 class A::B {};
17189
17190 We do not know if we will see a class-name, or a
17191 template-name. We look for a class-name first, in case the
17192 class-name is a template-id; if we looked for the
17193 template-name first we would stop after the template-name. */
17194 cp_parser_parse_tentatively (parser);
17195 type = cp_parser_class_name (parser,
17196 /*typename_keyword_p=*/false,
17197 /*template_keyword_p=*/false,
17198 class_type,
17199 /*check_dependency_p=*/false,
17200 /*class_head_p=*/true,
17201 /*is_declaration=*/false);
17202 /* If that didn't work, ignore the nested-name-specifier. */
17203 if (!cp_parser_parse_definitely (parser))
17204 {
17205 invalid_nested_name_p = true;
17206 type_start_token = cp_lexer_peek_token (parser->lexer);
17207 id = cp_parser_identifier (parser);
17208 if (id == error_mark_node)
17209 id = NULL_TREE;
17210 }
17211 /* If we could not find a corresponding TYPE, treat this
17212 declaration like an unqualified declaration. */
17213 if (type == error_mark_node)
17214 nested_name_specifier = NULL_TREE;
17215 /* Otherwise, count the number of templates used in TYPE and its
17216 containing scopes. */
17217 else
17218 {
17219 tree scope;
17220
17221 for (scope = TREE_TYPE (type);
17222 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17223 scope = (TYPE_P (scope)
17224 ? TYPE_CONTEXT (scope)
17225 : DECL_CONTEXT (scope)))
17226 if (TYPE_P (scope)
17227 && CLASS_TYPE_P (scope)
17228 && CLASSTYPE_TEMPLATE_INFO (scope)
17229 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17230 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17231 ++num_templates;
17232 }
17233 }
17234 /* Otherwise, the identifier is optional. */
17235 else
17236 {
17237 /* We don't know whether what comes next is a template-id,
17238 an identifier, or nothing at all. */
17239 cp_parser_parse_tentatively (parser);
17240 /* Check for a template-id. */
17241 type_start_token = cp_lexer_peek_token (parser->lexer);
17242 id = cp_parser_template_id (parser,
17243 /*template_keyword_p=*/false,
17244 /*check_dependency_p=*/true,
17245 /*is_declaration=*/true);
17246 /* If that didn't work, it could still be an identifier. */
17247 if (!cp_parser_parse_definitely (parser))
17248 {
17249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17250 {
17251 type_start_token = cp_lexer_peek_token (parser->lexer);
17252 id = cp_parser_identifier (parser);
17253 }
17254 else
17255 id = NULL_TREE;
17256 }
17257 else
17258 {
17259 template_id_p = true;
17260 ++num_templates;
17261 }
17262 }
17263
17264 pop_deferring_access_checks ();
17265
17266 if (id)
17267 {
17268 cp_parser_check_for_invalid_template_id (parser, id,
17269 type_start_token->location);
17270 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17271 }
17272
17273 /* If it's not a `:' or a `{' then we can't really be looking at a
17274 class-head, since a class-head only appears as part of a
17275 class-specifier. We have to detect this situation before calling
17276 xref_tag, since that has irreversible side-effects. */
17277 if (!cp_parser_next_token_starts_class_definition_p (parser))
17278 {
17279 cp_parser_error (parser, "expected %<{%> or %<:%>");
17280 type = error_mark_node;
17281 goto out;
17282 }
17283
17284 /* At this point, we're going ahead with the class-specifier, even
17285 if some other problem occurs. */
17286 cp_parser_commit_to_tentative_parse (parser);
17287 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17288 {
17289 cp_parser_error (parser,
17290 "cannot specify %<override%> for a class");
17291 type = error_mark_node;
17292 goto out;
17293 }
17294 /* Issue the error about the overly-qualified name now. */
17295 if (qualified_p)
17296 {
17297 cp_parser_error (parser,
17298 "global qualification of class name is invalid");
17299 type = error_mark_node;
17300 goto out;
17301 }
17302 else if (invalid_nested_name_p)
17303 {
17304 cp_parser_error (parser,
17305 "qualified name does not name a class");
17306 type = error_mark_node;
17307 goto out;
17308 }
17309 else if (nested_name_specifier)
17310 {
17311 tree scope;
17312
17313 /* Reject typedef-names in class heads. */
17314 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17315 {
17316 error_at (type_start_token->location,
17317 "invalid class name in declaration of %qD",
17318 type);
17319 type = NULL_TREE;
17320 goto done;
17321 }
17322
17323 /* Figure out in what scope the declaration is being placed. */
17324 scope = current_scope ();
17325 /* If that scope does not contain the scope in which the
17326 class was originally declared, the program is invalid. */
17327 if (scope && !is_ancestor (scope, nested_name_specifier))
17328 {
17329 if (at_namespace_scope_p ())
17330 error_at (type_start_token->location,
17331 "declaration of %qD in namespace %qD which does not "
17332 "enclose %qD",
17333 type, scope, nested_name_specifier);
17334 else
17335 error_at (type_start_token->location,
17336 "declaration of %qD in %qD which does not enclose %qD",
17337 type, scope, nested_name_specifier);
17338 type = NULL_TREE;
17339 goto done;
17340 }
17341 /* [dcl.meaning]
17342
17343 A declarator-id shall not be qualified except for the
17344 definition of a ... nested class outside of its class
17345 ... [or] the definition or explicit instantiation of a
17346 class member of a namespace outside of its namespace. */
17347 if (scope == nested_name_specifier)
17348 {
17349 permerror (nested_name_specifier_token_start->location,
17350 "extra qualification not allowed");
17351 nested_name_specifier = NULL_TREE;
17352 num_templates = 0;
17353 }
17354 }
17355 /* An explicit-specialization must be preceded by "template <>". If
17356 it is not, try to recover gracefully. */
17357 if (at_namespace_scope_p ()
17358 && parser->num_template_parameter_lists == 0
17359 && template_id_p)
17360 {
17361 error_at (type_start_token->location,
17362 "an explicit specialization must be preceded by %<template <>%>");
17363 invalid_explicit_specialization_p = true;
17364 /* Take the same action that would have been taken by
17365 cp_parser_explicit_specialization. */
17366 ++parser->num_template_parameter_lists;
17367 begin_specialization ();
17368 }
17369 /* There must be no "return" statements between this point and the
17370 end of this function; set "type "to the correct return value and
17371 use "goto done;" to return. */
17372 /* Make sure that the right number of template parameters were
17373 present. */
17374 if (!cp_parser_check_template_parameters (parser, num_templates,
17375 type_start_token->location,
17376 /*declarator=*/NULL))
17377 {
17378 /* If something went wrong, there is no point in even trying to
17379 process the class-definition. */
17380 type = NULL_TREE;
17381 goto done;
17382 }
17383
17384 /* Look up the type. */
17385 if (template_id_p)
17386 {
17387 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17388 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17389 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17390 {
17391 error_at (type_start_token->location,
17392 "function template %qD redeclared as a class template", id);
17393 type = error_mark_node;
17394 }
17395 else
17396 {
17397 type = TREE_TYPE (id);
17398 type = maybe_process_partial_specialization (type);
17399 }
17400 if (nested_name_specifier)
17401 pushed_scope = push_scope (nested_name_specifier);
17402 }
17403 else if (nested_name_specifier)
17404 {
17405 tree class_type;
17406
17407 /* Given:
17408
17409 template <typename T> struct S { struct T };
17410 template <typename T> struct S<T>::T { };
17411
17412 we will get a TYPENAME_TYPE when processing the definition of
17413 `S::T'. We need to resolve it to the actual type before we
17414 try to define it. */
17415 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17416 {
17417 class_type = resolve_typename_type (TREE_TYPE (type),
17418 /*only_current_p=*/false);
17419 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17420 type = TYPE_NAME (class_type);
17421 else
17422 {
17423 cp_parser_error (parser, "could not resolve typename type");
17424 type = error_mark_node;
17425 }
17426 }
17427
17428 if (maybe_process_partial_specialization (TREE_TYPE (type))
17429 == error_mark_node)
17430 {
17431 type = NULL_TREE;
17432 goto done;
17433 }
17434
17435 class_type = current_class_type;
17436 /* Enter the scope indicated by the nested-name-specifier. */
17437 pushed_scope = push_scope (nested_name_specifier);
17438 /* Get the canonical version of this type. */
17439 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17440 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17441 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17442 {
17443 type = push_template_decl (type);
17444 if (type == error_mark_node)
17445 {
17446 type = NULL_TREE;
17447 goto done;
17448 }
17449 }
17450
17451 type = TREE_TYPE (type);
17452 *nested_name_specifier_p = true;
17453 }
17454 else /* The name is not a nested name. */
17455 {
17456 /* If the class was unnamed, create a dummy name. */
17457 if (!id)
17458 id = make_anon_name ();
17459 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17460 parser->num_template_parameter_lists);
17461 }
17462
17463 /* Indicate whether this class was declared as a `class' or as a
17464 `struct'. */
17465 if (TREE_CODE (type) == RECORD_TYPE)
17466 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17467 cp_parser_check_class_key (class_key, type);
17468
17469 /* If this type was already complete, and we see another definition,
17470 that's an error. */
17471 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17472 {
17473 error_at (type_start_token->location, "redefinition of %q#T",
17474 type);
17475 error_at (type_start_token->location, "previous definition of %q+#T",
17476 type);
17477 type = NULL_TREE;
17478 goto done;
17479 }
17480 else if (type == error_mark_node)
17481 type = NULL_TREE;
17482
17483 /* We will have entered the scope containing the class; the names of
17484 base classes should be looked up in that context. For example:
17485
17486 struct A { struct B {}; struct C; };
17487 struct A::C : B {};
17488
17489 is valid. */
17490
17491 /* Get the list of base-classes, if there is one. */
17492 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17493 *bases = cp_parser_base_clause (parser);
17494
17495 done:
17496 /* Leave the scope given by the nested-name-specifier. We will
17497 enter the class scope itself while processing the members. */
17498 if (pushed_scope)
17499 pop_scope (pushed_scope);
17500
17501 if (invalid_explicit_specialization_p)
17502 {
17503 end_specialization ();
17504 --parser->num_template_parameter_lists;
17505 }
17506
17507 if (type)
17508 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17509 *attributes_p = attributes;
17510 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17511 CLASSTYPE_FINAL (type) = 1;
17512 out:
17513 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17514 return type;
17515 }
17516
17517 /* Parse a class-key.
17518
17519 class-key:
17520 class
17521 struct
17522 union
17523
17524 Returns the kind of class-key specified, or none_type to indicate
17525 error. */
17526
17527 static enum tag_types
17528 cp_parser_class_key (cp_parser* parser)
17529 {
17530 cp_token *token;
17531 enum tag_types tag_type;
17532
17533 /* Look for the class-key. */
17534 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17535 if (!token)
17536 return none_type;
17537
17538 /* Check to see if the TOKEN is a class-key. */
17539 tag_type = cp_parser_token_is_class_key (token);
17540 if (!tag_type)
17541 cp_parser_error (parser, "expected class-key");
17542 return tag_type;
17543 }
17544
17545 /* Parse an (optional) member-specification.
17546
17547 member-specification:
17548 member-declaration member-specification [opt]
17549 access-specifier : member-specification [opt] */
17550
17551 static void
17552 cp_parser_member_specification_opt (cp_parser* parser)
17553 {
17554 while (true)
17555 {
17556 cp_token *token;
17557 enum rid keyword;
17558
17559 /* Peek at the next token. */
17560 token = cp_lexer_peek_token (parser->lexer);
17561 /* If it's a `}', or EOF then we've seen all the members. */
17562 if (token->type == CPP_CLOSE_BRACE
17563 || token->type == CPP_EOF
17564 || token->type == CPP_PRAGMA_EOL)
17565 break;
17566
17567 /* See if this token is a keyword. */
17568 keyword = token->keyword;
17569 switch (keyword)
17570 {
17571 case RID_PUBLIC:
17572 case RID_PROTECTED:
17573 case RID_PRIVATE:
17574 /* Consume the access-specifier. */
17575 cp_lexer_consume_token (parser->lexer);
17576 /* Remember which access-specifier is active. */
17577 current_access_specifier = token->u.value;
17578 /* Look for the `:'. */
17579 cp_parser_require (parser, CPP_COLON, RT_COLON);
17580 break;
17581
17582 default:
17583 /* Accept #pragmas at class scope. */
17584 if (token->type == CPP_PRAGMA)
17585 {
17586 cp_parser_pragma (parser, pragma_external);
17587 break;
17588 }
17589
17590 /* Otherwise, the next construction must be a
17591 member-declaration. */
17592 cp_parser_member_declaration (parser);
17593 }
17594 }
17595 }
17596
17597 /* Parse a member-declaration.
17598
17599 member-declaration:
17600 decl-specifier-seq [opt] member-declarator-list [opt] ;
17601 function-definition ; [opt]
17602 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17603 using-declaration
17604 template-declaration
17605
17606 member-declarator-list:
17607 member-declarator
17608 member-declarator-list , member-declarator
17609
17610 member-declarator:
17611 declarator pure-specifier [opt]
17612 declarator constant-initializer [opt]
17613 identifier [opt] : constant-expression
17614
17615 GNU Extensions:
17616
17617 member-declaration:
17618 __extension__ member-declaration
17619
17620 member-declarator:
17621 declarator attributes [opt] pure-specifier [opt]
17622 declarator attributes [opt] constant-initializer [opt]
17623 identifier [opt] attributes [opt] : constant-expression
17624
17625 C++0x Extensions:
17626
17627 member-declaration:
17628 static_assert-declaration */
17629
17630 static void
17631 cp_parser_member_declaration (cp_parser* parser)
17632 {
17633 cp_decl_specifier_seq decl_specifiers;
17634 tree prefix_attributes;
17635 tree decl;
17636 int declares_class_or_enum;
17637 bool friend_p;
17638 cp_token *token = NULL;
17639 cp_token *decl_spec_token_start = NULL;
17640 cp_token *initializer_token_start = NULL;
17641 int saved_pedantic;
17642 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17643
17644 /* Check for the `__extension__' keyword. */
17645 if (cp_parser_extension_opt (parser, &saved_pedantic))
17646 {
17647 /* Recurse. */
17648 cp_parser_member_declaration (parser);
17649 /* Restore the old value of the PEDANTIC flag. */
17650 pedantic = saved_pedantic;
17651
17652 return;
17653 }
17654
17655 /* Check for a template-declaration. */
17656 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17657 {
17658 /* An explicit specialization here is an error condition, and we
17659 expect the specialization handler to detect and report this. */
17660 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17661 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17662 cp_parser_explicit_specialization (parser);
17663 else
17664 cp_parser_template_declaration (parser, /*member_p=*/true);
17665
17666 return;
17667 }
17668
17669 /* Check for a using-declaration. */
17670 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17671 {
17672 /* Parse the using-declaration. */
17673 cp_parser_using_declaration (parser,
17674 /*access_declaration_p=*/false);
17675 return;
17676 }
17677
17678 /* Check for @defs. */
17679 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17680 {
17681 tree ivar, member;
17682 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17683 ivar = ivar_chains;
17684 while (ivar)
17685 {
17686 member = ivar;
17687 ivar = TREE_CHAIN (member);
17688 TREE_CHAIN (member) = NULL_TREE;
17689 finish_member_declaration (member);
17690 }
17691 return;
17692 }
17693
17694 /* If the next token is `static_assert' we have a static assertion. */
17695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17696 {
17697 cp_parser_static_assert (parser, /*member_p=*/true);
17698 return;
17699 }
17700
17701 parser->colon_corrects_to_scope_p = false;
17702
17703 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17704 goto out;
17705
17706 /* Parse the decl-specifier-seq. */
17707 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17708 cp_parser_decl_specifier_seq (parser,
17709 CP_PARSER_FLAGS_OPTIONAL,
17710 &decl_specifiers,
17711 &declares_class_or_enum);
17712 prefix_attributes = decl_specifiers.attributes;
17713 decl_specifiers.attributes = NULL_TREE;
17714 /* Check for an invalid type-name. */
17715 if (!decl_specifiers.any_type_specifiers_p
17716 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17717 goto out;
17718 /* If there is no declarator, then the decl-specifier-seq should
17719 specify a type. */
17720 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17721 {
17722 /* If there was no decl-specifier-seq, and the next token is a
17723 `;', then we have something like:
17724
17725 struct S { ; };
17726
17727 [class.mem]
17728
17729 Each member-declaration shall declare at least one member
17730 name of the class. */
17731 if (!decl_specifiers.any_specifiers_p)
17732 {
17733 cp_token *token = cp_lexer_peek_token (parser->lexer);
17734 if (!in_system_header_at (token->location))
17735 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17736 }
17737 else
17738 {
17739 tree type;
17740
17741 /* See if this declaration is a friend. */
17742 friend_p = cp_parser_friend_p (&decl_specifiers);
17743 /* If there were decl-specifiers, check to see if there was
17744 a class-declaration. */
17745 type = check_tag_decl (&decl_specifiers);
17746 /* Nested classes have already been added to the class, but
17747 a `friend' needs to be explicitly registered. */
17748 if (friend_p)
17749 {
17750 /* If the `friend' keyword was present, the friend must
17751 be introduced with a class-key. */
17752 if (!declares_class_or_enum)
17753 error_at (decl_spec_token_start->location,
17754 "a class-key must be used when declaring a friend");
17755 /* In this case:
17756
17757 template <typename T> struct A {
17758 friend struct A<T>::B;
17759 };
17760
17761 A<T>::B will be represented by a TYPENAME_TYPE, and
17762 therefore not recognized by check_tag_decl. */
17763 if (!type
17764 && decl_specifiers.type
17765 && TYPE_P (decl_specifiers.type))
17766 type = decl_specifiers.type;
17767 if (!type || !TYPE_P (type))
17768 error_at (decl_spec_token_start->location,
17769 "friend declaration does not name a class or "
17770 "function");
17771 else
17772 make_friend_class (current_class_type, type,
17773 /*complain=*/true);
17774 }
17775 /* If there is no TYPE, an error message will already have
17776 been issued. */
17777 else if (!type || type == error_mark_node)
17778 ;
17779 /* An anonymous aggregate has to be handled specially; such
17780 a declaration really declares a data member (with a
17781 particular type), as opposed to a nested class. */
17782 else if (ANON_AGGR_TYPE_P (type))
17783 {
17784 /* Remove constructors and such from TYPE, now that we
17785 know it is an anonymous aggregate. */
17786 fixup_anonymous_aggr (type);
17787 /* And make the corresponding data member. */
17788 decl = build_decl (decl_spec_token_start->location,
17789 FIELD_DECL, NULL_TREE, type);
17790 /* Add it to the class. */
17791 finish_member_declaration (decl);
17792 }
17793 else
17794 cp_parser_check_access_in_redeclaration
17795 (TYPE_NAME (type),
17796 decl_spec_token_start->location);
17797 }
17798 }
17799 else
17800 {
17801 bool assume_semicolon = false;
17802
17803 /* See if these declarations will be friends. */
17804 friend_p = cp_parser_friend_p (&decl_specifiers);
17805
17806 /* Keep going until we hit the `;' at the end of the
17807 declaration. */
17808 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17809 {
17810 tree attributes = NULL_TREE;
17811 tree first_attribute;
17812
17813 /* Peek at the next token. */
17814 token = cp_lexer_peek_token (parser->lexer);
17815
17816 /* Check for a bitfield declaration. */
17817 if (token->type == CPP_COLON
17818 || (token->type == CPP_NAME
17819 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17820 == CPP_COLON))
17821 {
17822 tree identifier;
17823 tree width;
17824
17825 /* Get the name of the bitfield. Note that we cannot just
17826 check TOKEN here because it may have been invalidated by
17827 the call to cp_lexer_peek_nth_token above. */
17828 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17829 identifier = cp_parser_identifier (parser);
17830 else
17831 identifier = NULL_TREE;
17832
17833 /* Consume the `:' token. */
17834 cp_lexer_consume_token (parser->lexer);
17835 /* Get the width of the bitfield. */
17836 width
17837 = cp_parser_constant_expression (parser,
17838 /*allow_non_constant=*/false,
17839 NULL);
17840
17841 /* Look for attributes that apply to the bitfield. */
17842 attributes = cp_parser_attributes_opt (parser);
17843 /* Remember which attributes are prefix attributes and
17844 which are not. */
17845 first_attribute = attributes;
17846 /* Combine the attributes. */
17847 attributes = chainon (prefix_attributes, attributes);
17848
17849 /* Create the bitfield declaration. */
17850 decl = grokbitfield (identifier
17851 ? make_id_declarator (NULL_TREE,
17852 identifier,
17853 sfk_none)
17854 : NULL,
17855 &decl_specifiers,
17856 width,
17857 attributes);
17858 }
17859 else
17860 {
17861 cp_declarator *declarator;
17862 tree initializer;
17863 tree asm_specification;
17864 int ctor_dtor_or_conv_p;
17865
17866 /* Parse the declarator. */
17867 declarator
17868 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17869 &ctor_dtor_or_conv_p,
17870 /*parenthesized_p=*/NULL,
17871 /*member_p=*/true);
17872
17873 /* If something went wrong parsing the declarator, make sure
17874 that we at least consume some tokens. */
17875 if (declarator == cp_error_declarator)
17876 {
17877 /* Skip to the end of the statement. */
17878 cp_parser_skip_to_end_of_statement (parser);
17879 /* If the next token is not a semicolon, that is
17880 probably because we just skipped over the body of
17881 a function. So, we consume a semicolon if
17882 present, but do not issue an error message if it
17883 is not present. */
17884 if (cp_lexer_next_token_is (parser->lexer,
17885 CPP_SEMICOLON))
17886 cp_lexer_consume_token (parser->lexer);
17887 goto out;
17888 }
17889
17890 if (declares_class_or_enum & 2)
17891 cp_parser_check_for_definition_in_return_type
17892 (declarator, decl_specifiers.type,
17893 decl_specifiers.type_location);
17894
17895 /* Look for an asm-specification. */
17896 asm_specification = cp_parser_asm_specification_opt (parser);
17897 /* Look for attributes that apply to the declaration. */
17898 attributes = cp_parser_attributes_opt (parser);
17899 /* Remember which attributes are prefix attributes and
17900 which are not. */
17901 first_attribute = attributes;
17902 /* Combine the attributes. */
17903 attributes = chainon (prefix_attributes, attributes);
17904
17905 /* If it's an `=', then we have a constant-initializer or a
17906 pure-specifier. It is not correct to parse the
17907 initializer before registering the member declaration
17908 since the member declaration should be in scope while
17909 its initializer is processed. However, the rest of the
17910 front end does not yet provide an interface that allows
17911 us to handle this correctly. */
17912 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17913 {
17914 /* In [class.mem]:
17915
17916 A pure-specifier shall be used only in the declaration of
17917 a virtual function.
17918
17919 A member-declarator can contain a constant-initializer
17920 only if it declares a static member of integral or
17921 enumeration type.
17922
17923 Therefore, if the DECLARATOR is for a function, we look
17924 for a pure-specifier; otherwise, we look for a
17925 constant-initializer. When we call `grokfield', it will
17926 perform more stringent semantics checks. */
17927 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17928 if (function_declarator_p (declarator))
17929 initializer = cp_parser_pure_specifier (parser);
17930 else
17931 /* Parse the initializer. */
17932 initializer = cp_parser_constant_initializer (parser);
17933 }
17934 /* Otherwise, there is no initializer. */
17935 else
17936 initializer = NULL_TREE;
17937
17938 /* See if we are probably looking at a function
17939 definition. We are certainly not looking at a
17940 member-declarator. Calling `grokfield' has
17941 side-effects, so we must not do it unless we are sure
17942 that we are looking at a member-declarator. */
17943 if (cp_parser_token_starts_function_definition_p
17944 (cp_lexer_peek_token (parser->lexer)))
17945 {
17946 /* The grammar does not allow a pure-specifier to be
17947 used when a member function is defined. (It is
17948 possible that this fact is an oversight in the
17949 standard, since a pure function may be defined
17950 outside of the class-specifier. */
17951 if (initializer)
17952 error_at (initializer_token_start->location,
17953 "pure-specifier on function-definition");
17954 decl = cp_parser_save_member_function_body (parser,
17955 &decl_specifiers,
17956 declarator,
17957 attributes);
17958 /* If the member was not a friend, declare it here. */
17959 if (!friend_p)
17960 finish_member_declaration (decl);
17961 /* Peek at the next token. */
17962 token = cp_lexer_peek_token (parser->lexer);
17963 /* If the next token is a semicolon, consume it. */
17964 if (token->type == CPP_SEMICOLON)
17965 cp_lexer_consume_token (parser->lexer);
17966 goto out;
17967 }
17968 else
17969 if (declarator->kind == cdk_function)
17970 declarator->id_loc = token->location;
17971 /* Create the declaration. */
17972 decl = grokfield (declarator, &decl_specifiers,
17973 initializer, /*init_const_expr_p=*/true,
17974 asm_specification,
17975 attributes);
17976 }
17977
17978 /* Reset PREFIX_ATTRIBUTES. */
17979 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17980 attributes = TREE_CHAIN (attributes);
17981 if (attributes)
17982 TREE_CHAIN (attributes) = NULL_TREE;
17983
17984 /* If there is any qualification still in effect, clear it
17985 now; we will be starting fresh with the next declarator. */
17986 parser->scope = NULL_TREE;
17987 parser->qualifying_scope = NULL_TREE;
17988 parser->object_scope = NULL_TREE;
17989 /* If it's a `,', then there are more declarators. */
17990 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17991 cp_lexer_consume_token (parser->lexer);
17992 /* If the next token isn't a `;', then we have a parse error. */
17993 else if (cp_lexer_next_token_is_not (parser->lexer,
17994 CPP_SEMICOLON))
17995 {
17996 /* The next token might be a ways away from where the
17997 actual semicolon is missing. Find the previous token
17998 and use that for our error position. */
17999 cp_token *token = cp_lexer_previous_token (parser->lexer);
18000 error_at (token->location,
18001 "expected %<;%> at end of member declaration");
18002
18003 /* Assume that the user meant to provide a semicolon. If
18004 we were to cp_parser_skip_to_end_of_statement, we might
18005 skip to a semicolon inside a member function definition
18006 and issue nonsensical error messages. */
18007 assume_semicolon = true;
18008 }
18009
18010 if (decl)
18011 {
18012 /* Add DECL to the list of members. */
18013 if (!friend_p)
18014 finish_member_declaration (decl);
18015
18016 if (TREE_CODE (decl) == FUNCTION_DECL)
18017 cp_parser_save_default_args (parser, decl);
18018 }
18019
18020 if (assume_semicolon)
18021 goto out;
18022 }
18023 }
18024
18025 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18026 out:
18027 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18028 }
18029
18030 /* Parse a pure-specifier.
18031
18032 pure-specifier:
18033 = 0
18034
18035 Returns INTEGER_ZERO_NODE if a pure specifier is found.
18036 Otherwise, ERROR_MARK_NODE is returned. */
18037
18038 static tree
18039 cp_parser_pure_specifier (cp_parser* parser)
18040 {
18041 cp_token *token;
18042
18043 /* Look for the `=' token. */
18044 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18045 return error_mark_node;
18046 /* Look for the `0' token. */
18047 token = cp_lexer_peek_token (parser->lexer);
18048
18049 if (token->type == CPP_EOF
18050 || token->type == CPP_PRAGMA_EOL)
18051 return error_mark_node;
18052
18053 cp_lexer_consume_token (parser->lexer);
18054
18055 /* Accept = default or = delete in c++0x mode. */
18056 if (token->keyword == RID_DEFAULT
18057 || token->keyword == RID_DELETE)
18058 {
18059 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18060 return token->u.value;
18061 }
18062
18063 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
18064 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18065 {
18066 cp_parser_error (parser,
18067 "invalid pure specifier (only %<= 0%> is allowed)");
18068 cp_parser_skip_to_end_of_statement (parser);
18069 return error_mark_node;
18070 }
18071 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18072 {
18073 error_at (token->location, "templates may not be %<virtual%>");
18074 return error_mark_node;
18075 }
18076
18077 return integer_zero_node;
18078 }
18079
18080 /* Parse a constant-initializer.
18081
18082 constant-initializer:
18083 = constant-expression
18084
18085 Returns a representation of the constant-expression. */
18086
18087 static tree
18088 cp_parser_constant_initializer (cp_parser* parser)
18089 {
18090 /* Look for the `=' token. */
18091 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18092 return error_mark_node;
18093
18094 /* It is invalid to write:
18095
18096 struct S { static const int i = { 7 }; };
18097
18098 */
18099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18100 {
18101 cp_parser_error (parser,
18102 "a brace-enclosed initializer is not allowed here");
18103 /* Consume the opening brace. */
18104 cp_lexer_consume_token (parser->lexer);
18105 /* Skip the initializer. */
18106 cp_parser_skip_to_closing_brace (parser);
18107 /* Look for the trailing `}'. */
18108 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18109
18110 return error_mark_node;
18111 }
18112
18113 return cp_parser_constant_expression (parser,
18114 /*allow_non_constant=*/false,
18115 NULL);
18116 }
18117
18118 /* Derived classes [gram.class.derived] */
18119
18120 /* Parse a base-clause.
18121
18122 base-clause:
18123 : base-specifier-list
18124
18125 base-specifier-list:
18126 base-specifier ... [opt]
18127 base-specifier-list , base-specifier ... [opt]
18128
18129 Returns a TREE_LIST representing the base-classes, in the order in
18130 which they were declared. The representation of each node is as
18131 described by cp_parser_base_specifier.
18132
18133 In the case that no bases are specified, this function will return
18134 NULL_TREE, not ERROR_MARK_NODE. */
18135
18136 static tree
18137 cp_parser_base_clause (cp_parser* parser)
18138 {
18139 tree bases = NULL_TREE;
18140
18141 /* Look for the `:' that begins the list. */
18142 cp_parser_require (parser, CPP_COLON, RT_COLON);
18143
18144 /* Scan the base-specifier-list. */
18145 while (true)
18146 {
18147 cp_token *token;
18148 tree base;
18149 bool pack_expansion_p = false;
18150
18151 /* Look for the base-specifier. */
18152 base = cp_parser_base_specifier (parser);
18153 /* Look for the (optional) ellipsis. */
18154 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18155 {
18156 /* Consume the `...'. */
18157 cp_lexer_consume_token (parser->lexer);
18158
18159 pack_expansion_p = true;
18160 }
18161
18162 /* Add BASE to the front of the list. */
18163 if (base != error_mark_node)
18164 {
18165 if (pack_expansion_p)
18166 /* Make this a pack expansion type. */
18167 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18168
18169
18170 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18171 {
18172 TREE_CHAIN (base) = bases;
18173 bases = base;
18174 }
18175 }
18176 /* Peek at the next token. */
18177 token = cp_lexer_peek_token (parser->lexer);
18178 /* If it's not a comma, then the list is complete. */
18179 if (token->type != CPP_COMMA)
18180 break;
18181 /* Consume the `,'. */
18182 cp_lexer_consume_token (parser->lexer);
18183 }
18184
18185 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18186 base class had a qualified name. However, the next name that
18187 appears is certainly not qualified. */
18188 parser->scope = NULL_TREE;
18189 parser->qualifying_scope = NULL_TREE;
18190 parser->object_scope = NULL_TREE;
18191
18192 return nreverse (bases);
18193 }
18194
18195 /* Parse a base-specifier.
18196
18197 base-specifier:
18198 :: [opt] nested-name-specifier [opt] class-name
18199 virtual access-specifier [opt] :: [opt] nested-name-specifier
18200 [opt] class-name
18201 access-specifier virtual [opt] :: [opt] nested-name-specifier
18202 [opt] class-name
18203
18204 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18205 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18206 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18207 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18208
18209 static tree
18210 cp_parser_base_specifier (cp_parser* parser)
18211 {
18212 cp_token *token;
18213 bool done = false;
18214 bool virtual_p = false;
18215 bool duplicate_virtual_error_issued_p = false;
18216 bool duplicate_access_error_issued_p = false;
18217 bool class_scope_p, template_p;
18218 tree access = access_default_node;
18219 tree type;
18220
18221 /* Process the optional `virtual' and `access-specifier'. */
18222 while (!done)
18223 {
18224 /* Peek at the next token. */
18225 token = cp_lexer_peek_token (parser->lexer);
18226 /* Process `virtual'. */
18227 switch (token->keyword)
18228 {
18229 case RID_VIRTUAL:
18230 /* If `virtual' appears more than once, issue an error. */
18231 if (virtual_p && !duplicate_virtual_error_issued_p)
18232 {
18233 cp_parser_error (parser,
18234 "%<virtual%> specified more than once in base-specified");
18235 duplicate_virtual_error_issued_p = true;
18236 }
18237
18238 virtual_p = true;
18239
18240 /* Consume the `virtual' token. */
18241 cp_lexer_consume_token (parser->lexer);
18242
18243 break;
18244
18245 case RID_PUBLIC:
18246 case RID_PROTECTED:
18247 case RID_PRIVATE:
18248 /* If more than one access specifier appears, issue an
18249 error. */
18250 if (access != access_default_node
18251 && !duplicate_access_error_issued_p)
18252 {
18253 cp_parser_error (parser,
18254 "more than one access specifier in base-specified");
18255 duplicate_access_error_issued_p = true;
18256 }
18257
18258 access = ridpointers[(int) token->keyword];
18259
18260 /* Consume the access-specifier. */
18261 cp_lexer_consume_token (parser->lexer);
18262
18263 break;
18264
18265 default:
18266 done = true;
18267 break;
18268 }
18269 }
18270 /* It is not uncommon to see programs mechanically, erroneously, use
18271 the 'typename' keyword to denote (dependent) qualified types
18272 as base classes. */
18273 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18274 {
18275 token = cp_lexer_peek_token (parser->lexer);
18276 if (!processing_template_decl)
18277 error_at (token->location,
18278 "keyword %<typename%> not allowed outside of templates");
18279 else
18280 error_at (token->location,
18281 "keyword %<typename%> not allowed in this context "
18282 "(the base class is implicitly a type)");
18283 cp_lexer_consume_token (parser->lexer);
18284 }
18285
18286 /* Look for the optional `::' operator. */
18287 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18288 /* Look for the nested-name-specifier. The simplest way to
18289 implement:
18290
18291 [temp.res]
18292
18293 The keyword `typename' is not permitted in a base-specifier or
18294 mem-initializer; in these contexts a qualified name that
18295 depends on a template-parameter is implicitly assumed to be a
18296 type name.
18297
18298 is to pretend that we have seen the `typename' keyword at this
18299 point. */
18300 cp_parser_nested_name_specifier_opt (parser,
18301 /*typename_keyword_p=*/true,
18302 /*check_dependency_p=*/true,
18303 typename_type,
18304 /*is_declaration=*/true);
18305 /* If the base class is given by a qualified name, assume that names
18306 we see are type names or templates, as appropriate. */
18307 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18308 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18309
18310 /* Finally, look for the class-name. */
18311 type = cp_parser_class_name (parser,
18312 class_scope_p,
18313 template_p,
18314 typename_type,
18315 /*check_dependency_p=*/true,
18316 /*class_head_p=*/false,
18317 /*is_declaration=*/true);
18318
18319 if (type == error_mark_node)
18320 return error_mark_node;
18321
18322 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18323 }
18324
18325 /* Exception handling [gram.exception] */
18326
18327 /* Parse an (optional) exception-specification.
18328
18329 exception-specification:
18330 throw ( type-id-list [opt] )
18331
18332 Returns a TREE_LIST representing the exception-specification. The
18333 TREE_VALUE of each node is a type. */
18334
18335 static tree
18336 cp_parser_exception_specification_opt (cp_parser* parser)
18337 {
18338 cp_token *token;
18339 tree type_id_list;
18340 const char *saved_message;
18341
18342 /* Peek at the next token. */
18343 token = cp_lexer_peek_token (parser->lexer);
18344
18345 /* Is it a noexcept-specification? */
18346 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18347 {
18348 tree expr;
18349 cp_lexer_consume_token (parser->lexer);
18350
18351 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18352 {
18353 cp_lexer_consume_token (parser->lexer);
18354
18355 /* Types may not be defined in an exception-specification. */
18356 saved_message = parser->type_definition_forbidden_message;
18357 parser->type_definition_forbidden_message
18358 = G_("types may not be defined in an exception-specification");
18359
18360 expr = cp_parser_constant_expression (parser, false, NULL);
18361
18362 /* Restore the saved message. */
18363 parser->type_definition_forbidden_message = saved_message;
18364
18365 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18366 }
18367 else
18368 expr = boolean_true_node;
18369
18370 return build_noexcept_spec (expr, tf_warning_or_error);
18371 }
18372
18373 /* If it's not `throw', then there's no exception-specification. */
18374 if (!cp_parser_is_keyword (token, RID_THROW))
18375 return NULL_TREE;
18376
18377 #if 0
18378 /* Enable this once a lot of code has transitioned to noexcept? */
18379 if (cxx_dialect == cxx0x && !in_system_header)
18380 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18381 "deprecated in C++0x; use %<noexcept%> instead");
18382 #endif
18383
18384 /* Consume the `throw'. */
18385 cp_lexer_consume_token (parser->lexer);
18386
18387 /* Look for the `('. */
18388 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18389
18390 /* Peek at the next token. */
18391 token = cp_lexer_peek_token (parser->lexer);
18392 /* If it's not a `)', then there is a type-id-list. */
18393 if (token->type != CPP_CLOSE_PAREN)
18394 {
18395 /* Types may not be defined in an exception-specification. */
18396 saved_message = parser->type_definition_forbidden_message;
18397 parser->type_definition_forbidden_message
18398 = G_("types may not be defined in an exception-specification");
18399 /* Parse the type-id-list. */
18400 type_id_list = cp_parser_type_id_list (parser);
18401 /* Restore the saved message. */
18402 parser->type_definition_forbidden_message = saved_message;
18403 }
18404 else
18405 type_id_list = empty_except_spec;
18406
18407 /* Look for the `)'. */
18408 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18409
18410 return type_id_list;
18411 }
18412
18413 /* Parse an (optional) type-id-list.
18414
18415 type-id-list:
18416 type-id ... [opt]
18417 type-id-list , type-id ... [opt]
18418
18419 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18420 in the order that the types were presented. */
18421
18422 static tree
18423 cp_parser_type_id_list (cp_parser* parser)
18424 {
18425 tree types = NULL_TREE;
18426
18427 while (true)
18428 {
18429 cp_token *token;
18430 tree type;
18431
18432 /* Get the next type-id. */
18433 type = cp_parser_type_id (parser);
18434 /* Parse the optional ellipsis. */
18435 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18436 {
18437 /* Consume the `...'. */
18438 cp_lexer_consume_token (parser->lexer);
18439
18440 /* Turn the type into a pack expansion expression. */
18441 type = make_pack_expansion (type);
18442 }
18443 /* Add it to the list. */
18444 types = add_exception_specifier (types, type, /*complain=*/1);
18445 /* Peek at the next token. */
18446 token = cp_lexer_peek_token (parser->lexer);
18447 /* If it is not a `,', we are done. */
18448 if (token->type != CPP_COMMA)
18449 break;
18450 /* Consume the `,'. */
18451 cp_lexer_consume_token (parser->lexer);
18452 }
18453
18454 return nreverse (types);
18455 }
18456
18457 /* Parse a try-block.
18458
18459 try-block:
18460 try compound-statement handler-seq */
18461
18462 static tree
18463 cp_parser_try_block (cp_parser* parser)
18464 {
18465 tree try_block;
18466
18467 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18468 try_block = begin_try_block ();
18469 cp_parser_compound_statement (parser, NULL, true, false);
18470 finish_try_block (try_block);
18471 cp_parser_handler_seq (parser);
18472 finish_handler_sequence (try_block);
18473
18474 return try_block;
18475 }
18476
18477 /* Parse a function-try-block.
18478
18479 function-try-block:
18480 try ctor-initializer [opt] function-body handler-seq */
18481
18482 static bool
18483 cp_parser_function_try_block (cp_parser* parser)
18484 {
18485 tree compound_stmt;
18486 tree try_block;
18487 bool ctor_initializer_p;
18488
18489 /* Look for the `try' keyword. */
18490 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18491 return false;
18492 /* Let the rest of the front end know where we are. */
18493 try_block = begin_function_try_block (&compound_stmt);
18494 /* Parse the function-body. */
18495 ctor_initializer_p
18496 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18497 /* We're done with the `try' part. */
18498 finish_function_try_block (try_block);
18499 /* Parse the handlers. */
18500 cp_parser_handler_seq (parser);
18501 /* We're done with the handlers. */
18502 finish_function_handler_sequence (try_block, compound_stmt);
18503
18504 return ctor_initializer_p;
18505 }
18506
18507 /* Parse a handler-seq.
18508
18509 handler-seq:
18510 handler handler-seq [opt] */
18511
18512 static void
18513 cp_parser_handler_seq (cp_parser* parser)
18514 {
18515 while (true)
18516 {
18517 cp_token *token;
18518
18519 /* Parse the handler. */
18520 cp_parser_handler (parser);
18521 /* Peek at the next token. */
18522 token = cp_lexer_peek_token (parser->lexer);
18523 /* If it's not `catch' then there are no more handlers. */
18524 if (!cp_parser_is_keyword (token, RID_CATCH))
18525 break;
18526 }
18527 }
18528
18529 /* Parse a handler.
18530
18531 handler:
18532 catch ( exception-declaration ) compound-statement */
18533
18534 static void
18535 cp_parser_handler (cp_parser* parser)
18536 {
18537 tree handler;
18538 tree declaration;
18539
18540 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18541 handler = begin_handler ();
18542 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18543 declaration = cp_parser_exception_declaration (parser);
18544 finish_handler_parms (declaration, handler);
18545 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18546 cp_parser_compound_statement (parser, NULL, false, false);
18547 finish_handler (handler);
18548 }
18549
18550 /* Parse an exception-declaration.
18551
18552 exception-declaration:
18553 type-specifier-seq declarator
18554 type-specifier-seq abstract-declarator
18555 type-specifier-seq
18556 ...
18557
18558 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18559 ellipsis variant is used. */
18560
18561 static tree
18562 cp_parser_exception_declaration (cp_parser* parser)
18563 {
18564 cp_decl_specifier_seq type_specifiers;
18565 cp_declarator *declarator;
18566 const char *saved_message;
18567
18568 /* If it's an ellipsis, it's easy to handle. */
18569 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18570 {
18571 /* Consume the `...' token. */
18572 cp_lexer_consume_token (parser->lexer);
18573 return NULL_TREE;
18574 }
18575
18576 /* Types may not be defined in exception-declarations. */
18577 saved_message = parser->type_definition_forbidden_message;
18578 parser->type_definition_forbidden_message
18579 = G_("types may not be defined in exception-declarations");
18580
18581 /* Parse the type-specifier-seq. */
18582 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18583 /*is_trailing_return=*/false,
18584 &type_specifiers);
18585 /* If it's a `)', then there is no declarator. */
18586 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18587 declarator = NULL;
18588 else
18589 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18590 /*ctor_dtor_or_conv_p=*/NULL,
18591 /*parenthesized_p=*/NULL,
18592 /*member_p=*/false);
18593
18594 /* Restore the saved message. */
18595 parser->type_definition_forbidden_message = saved_message;
18596
18597 if (!type_specifiers.any_specifiers_p)
18598 return error_mark_node;
18599
18600 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18601 }
18602
18603 /* Parse a throw-expression.
18604
18605 throw-expression:
18606 throw assignment-expression [opt]
18607
18608 Returns a THROW_EXPR representing the throw-expression. */
18609
18610 static tree
18611 cp_parser_throw_expression (cp_parser* parser)
18612 {
18613 tree expression;
18614 cp_token* token;
18615
18616 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18617 token = cp_lexer_peek_token (parser->lexer);
18618 /* Figure out whether or not there is an assignment-expression
18619 following the "throw" keyword. */
18620 if (token->type == CPP_COMMA
18621 || token->type == CPP_SEMICOLON
18622 || token->type == CPP_CLOSE_PAREN
18623 || token->type == CPP_CLOSE_SQUARE
18624 || token->type == CPP_CLOSE_BRACE
18625 || token->type == CPP_COLON)
18626 expression = NULL_TREE;
18627 else
18628 expression = cp_parser_assignment_expression (parser,
18629 /*cast_p=*/false, NULL);
18630
18631 return build_throw (expression);
18632 }
18633
18634 /* GNU Extensions */
18635
18636 /* Parse an (optional) asm-specification.
18637
18638 asm-specification:
18639 asm ( string-literal )
18640
18641 If the asm-specification is present, returns a STRING_CST
18642 corresponding to the string-literal. Otherwise, returns
18643 NULL_TREE. */
18644
18645 static tree
18646 cp_parser_asm_specification_opt (cp_parser* parser)
18647 {
18648 cp_token *token;
18649 tree asm_specification;
18650
18651 /* Peek at the next token. */
18652 token = cp_lexer_peek_token (parser->lexer);
18653 /* If the next token isn't the `asm' keyword, then there's no
18654 asm-specification. */
18655 if (!cp_parser_is_keyword (token, RID_ASM))
18656 return NULL_TREE;
18657
18658 /* Consume the `asm' token. */
18659 cp_lexer_consume_token (parser->lexer);
18660 /* Look for the `('. */
18661 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18662
18663 /* Look for the string-literal. */
18664 asm_specification = cp_parser_string_literal (parser, false, false);
18665
18666 /* Look for the `)'. */
18667 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18668
18669 return asm_specification;
18670 }
18671
18672 /* Parse an asm-operand-list.
18673
18674 asm-operand-list:
18675 asm-operand
18676 asm-operand-list , asm-operand
18677
18678 asm-operand:
18679 string-literal ( expression )
18680 [ string-literal ] string-literal ( expression )
18681
18682 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18683 each node is the expression. The TREE_PURPOSE is itself a
18684 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18685 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18686 is a STRING_CST for the string literal before the parenthesis. Returns
18687 ERROR_MARK_NODE if any of the operands are invalid. */
18688
18689 static tree
18690 cp_parser_asm_operand_list (cp_parser* parser)
18691 {
18692 tree asm_operands = NULL_TREE;
18693 bool invalid_operands = false;
18694
18695 while (true)
18696 {
18697 tree string_literal;
18698 tree expression;
18699 tree name;
18700
18701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18702 {
18703 /* Consume the `[' token. */
18704 cp_lexer_consume_token (parser->lexer);
18705 /* Read the operand name. */
18706 name = cp_parser_identifier (parser);
18707 if (name != error_mark_node)
18708 name = build_string (IDENTIFIER_LENGTH (name),
18709 IDENTIFIER_POINTER (name));
18710 /* Look for the closing `]'. */
18711 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18712 }
18713 else
18714 name = NULL_TREE;
18715 /* Look for the string-literal. */
18716 string_literal = cp_parser_string_literal (parser, false, false);
18717
18718 /* Look for the `('. */
18719 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18720 /* Parse the expression. */
18721 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18722 /* Look for the `)'. */
18723 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18724
18725 if (name == error_mark_node
18726 || string_literal == error_mark_node
18727 || expression == error_mark_node)
18728 invalid_operands = true;
18729
18730 /* Add this operand to the list. */
18731 asm_operands = tree_cons (build_tree_list (name, string_literal),
18732 expression,
18733 asm_operands);
18734 /* If the next token is not a `,', there are no more
18735 operands. */
18736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18737 break;
18738 /* Consume the `,'. */
18739 cp_lexer_consume_token (parser->lexer);
18740 }
18741
18742 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18743 }
18744
18745 /* Parse an asm-clobber-list.
18746
18747 asm-clobber-list:
18748 string-literal
18749 asm-clobber-list , string-literal
18750
18751 Returns a TREE_LIST, indicating the clobbers in the order that they
18752 appeared. The TREE_VALUE of each node is a STRING_CST. */
18753
18754 static tree
18755 cp_parser_asm_clobber_list (cp_parser* parser)
18756 {
18757 tree clobbers = NULL_TREE;
18758
18759 while (true)
18760 {
18761 tree string_literal;
18762
18763 /* Look for the string literal. */
18764 string_literal = cp_parser_string_literal (parser, false, false);
18765 /* Add it to the list. */
18766 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18767 /* If the next token is not a `,', then the list is
18768 complete. */
18769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18770 break;
18771 /* Consume the `,' token. */
18772 cp_lexer_consume_token (parser->lexer);
18773 }
18774
18775 return clobbers;
18776 }
18777
18778 /* Parse an asm-label-list.
18779
18780 asm-label-list:
18781 identifier
18782 asm-label-list , identifier
18783
18784 Returns a TREE_LIST, indicating the labels in the order that they
18785 appeared. The TREE_VALUE of each node is a label. */
18786
18787 static tree
18788 cp_parser_asm_label_list (cp_parser* parser)
18789 {
18790 tree labels = NULL_TREE;
18791
18792 while (true)
18793 {
18794 tree identifier, label, name;
18795
18796 /* Look for the identifier. */
18797 identifier = cp_parser_identifier (parser);
18798 if (!error_operand_p (identifier))
18799 {
18800 label = lookup_label (identifier);
18801 if (TREE_CODE (label) == LABEL_DECL)
18802 {
18803 TREE_USED (label) = 1;
18804 check_goto (label);
18805 name = build_string (IDENTIFIER_LENGTH (identifier),
18806 IDENTIFIER_POINTER (identifier));
18807 labels = tree_cons (name, label, labels);
18808 }
18809 }
18810 /* If the next token is not a `,', then the list is
18811 complete. */
18812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18813 break;
18814 /* Consume the `,' token. */
18815 cp_lexer_consume_token (parser->lexer);
18816 }
18817
18818 return nreverse (labels);
18819 }
18820
18821 /* Parse an (optional) series of attributes.
18822
18823 attributes:
18824 attributes attribute
18825
18826 attribute:
18827 __attribute__ (( attribute-list [opt] ))
18828
18829 The return value is as for cp_parser_attribute_list. */
18830
18831 static tree
18832 cp_parser_attributes_opt (cp_parser* parser)
18833 {
18834 tree attributes = NULL_TREE;
18835
18836 while (true)
18837 {
18838 cp_token *token;
18839 tree attribute_list;
18840
18841 /* Peek at the next token. */
18842 token = cp_lexer_peek_token (parser->lexer);
18843 /* If it's not `__attribute__', then we're done. */
18844 if (token->keyword != RID_ATTRIBUTE)
18845 break;
18846
18847 /* Consume the `__attribute__' keyword. */
18848 cp_lexer_consume_token (parser->lexer);
18849 /* Look for the two `(' tokens. */
18850 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18851 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18852
18853 /* Peek at the next token. */
18854 token = cp_lexer_peek_token (parser->lexer);
18855 if (token->type != CPP_CLOSE_PAREN)
18856 /* Parse the attribute-list. */
18857 attribute_list = cp_parser_attribute_list (parser);
18858 else
18859 /* If the next token is a `)', then there is no attribute
18860 list. */
18861 attribute_list = NULL;
18862
18863 /* Look for the two `)' tokens. */
18864 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18866
18867 /* Add these new attributes to the list. */
18868 attributes = chainon (attributes, attribute_list);
18869 }
18870
18871 return attributes;
18872 }
18873
18874 /* Parse an attribute-list.
18875
18876 attribute-list:
18877 attribute
18878 attribute-list , attribute
18879
18880 attribute:
18881 identifier
18882 identifier ( identifier )
18883 identifier ( identifier , expression-list )
18884 identifier ( expression-list )
18885
18886 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18887 to an attribute. The TREE_PURPOSE of each node is the identifier
18888 indicating which attribute is in use. The TREE_VALUE represents
18889 the arguments, if any. */
18890
18891 static tree
18892 cp_parser_attribute_list (cp_parser* parser)
18893 {
18894 tree attribute_list = NULL_TREE;
18895 bool save_translate_strings_p = parser->translate_strings_p;
18896
18897 parser->translate_strings_p = false;
18898 while (true)
18899 {
18900 cp_token *token;
18901 tree identifier;
18902 tree attribute;
18903
18904 /* Look for the identifier. We also allow keywords here; for
18905 example `__attribute__ ((const))' is legal. */
18906 token = cp_lexer_peek_token (parser->lexer);
18907 if (token->type == CPP_NAME
18908 || token->type == CPP_KEYWORD)
18909 {
18910 tree arguments = NULL_TREE;
18911
18912 /* Consume the token. */
18913 token = cp_lexer_consume_token (parser->lexer);
18914
18915 /* Save away the identifier that indicates which attribute
18916 this is. */
18917 identifier = (token->type == CPP_KEYWORD)
18918 /* For keywords, use the canonical spelling, not the
18919 parsed identifier. */
18920 ? ridpointers[(int) token->keyword]
18921 : token->u.value;
18922
18923 attribute = build_tree_list (identifier, NULL_TREE);
18924
18925 /* Peek at the next token. */
18926 token = cp_lexer_peek_token (parser->lexer);
18927 /* If it's an `(', then parse the attribute arguments. */
18928 if (token->type == CPP_OPEN_PAREN)
18929 {
18930 VEC(tree,gc) *vec;
18931 int attr_flag = (attribute_takes_identifier_p (identifier)
18932 ? id_attr : normal_attr);
18933 vec = cp_parser_parenthesized_expression_list
18934 (parser, attr_flag, /*cast_p=*/false,
18935 /*allow_expansion_p=*/false,
18936 /*non_constant_p=*/NULL);
18937 if (vec == NULL)
18938 arguments = error_mark_node;
18939 else
18940 {
18941 arguments = build_tree_list_vec (vec);
18942 release_tree_vector (vec);
18943 }
18944 /* Save the arguments away. */
18945 TREE_VALUE (attribute) = arguments;
18946 }
18947
18948 if (arguments != error_mark_node)
18949 {
18950 /* Add this attribute to the list. */
18951 TREE_CHAIN (attribute) = attribute_list;
18952 attribute_list = attribute;
18953 }
18954
18955 token = cp_lexer_peek_token (parser->lexer);
18956 }
18957 /* Now, look for more attributes. If the next token isn't a
18958 `,', we're done. */
18959 if (token->type != CPP_COMMA)
18960 break;
18961
18962 /* Consume the comma and keep going. */
18963 cp_lexer_consume_token (parser->lexer);
18964 }
18965 parser->translate_strings_p = save_translate_strings_p;
18966
18967 /* We built up the list in reverse order. */
18968 return nreverse (attribute_list);
18969 }
18970
18971 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18972 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18973 current value of the PEDANTIC flag, regardless of whether or not
18974 the `__extension__' keyword is present. The caller is responsible
18975 for restoring the value of the PEDANTIC flag. */
18976
18977 static bool
18978 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18979 {
18980 /* Save the old value of the PEDANTIC flag. */
18981 *saved_pedantic = pedantic;
18982
18983 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18984 {
18985 /* Consume the `__extension__' token. */
18986 cp_lexer_consume_token (parser->lexer);
18987 /* We're not being pedantic while the `__extension__' keyword is
18988 in effect. */
18989 pedantic = 0;
18990
18991 return true;
18992 }
18993
18994 return false;
18995 }
18996
18997 /* Parse a label declaration.
18998
18999 label-declaration:
19000 __label__ label-declarator-seq ;
19001
19002 label-declarator-seq:
19003 identifier , label-declarator-seq
19004 identifier */
19005
19006 static void
19007 cp_parser_label_declaration (cp_parser* parser)
19008 {
19009 /* Look for the `__label__' keyword. */
19010 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19011
19012 while (true)
19013 {
19014 tree identifier;
19015
19016 /* Look for an identifier. */
19017 identifier = cp_parser_identifier (parser);
19018 /* If we failed, stop. */
19019 if (identifier == error_mark_node)
19020 break;
19021 /* Declare it as a label. */
19022 finish_label_decl (identifier);
19023 /* If the next token is a `;', stop. */
19024 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19025 break;
19026 /* Look for the `,' separating the label declarations. */
19027 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19028 }
19029
19030 /* Look for the final `;'. */
19031 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19032 }
19033
19034 /* Support Functions */
19035
19036 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19037 NAME should have one of the representations used for an
19038 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19039 is returned. If PARSER->SCOPE is a dependent type, then a
19040 SCOPE_REF is returned.
19041
19042 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19043 returned; the name was already resolved when the TEMPLATE_ID_EXPR
19044 was formed. Abstractly, such entities should not be passed to this
19045 function, because they do not need to be looked up, but it is
19046 simpler to check for this special case here, rather than at the
19047 call-sites.
19048
19049 In cases not explicitly covered above, this function returns a
19050 DECL, OVERLOAD, or baselink representing the result of the lookup.
19051 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19052 is returned.
19053
19054 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19055 (e.g., "struct") that was used. In that case bindings that do not
19056 refer to types are ignored.
19057
19058 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19059 ignored.
19060
19061 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19062 are ignored.
19063
19064 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19065 types.
19066
19067 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19068 TREE_LIST of candidates if name-lookup results in an ambiguity, and
19069 NULL_TREE otherwise. */
19070
19071 static tree
19072 cp_parser_lookup_name (cp_parser *parser, tree name,
19073 enum tag_types tag_type,
19074 bool is_template,
19075 bool is_namespace,
19076 bool check_dependency,
19077 tree *ambiguous_decls,
19078 location_t name_location)
19079 {
19080 int flags = 0;
19081 tree decl;
19082 tree object_type = parser->context->object_type;
19083
19084 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19085 flags |= LOOKUP_COMPLAIN;
19086
19087 /* Assume that the lookup will be unambiguous. */
19088 if (ambiguous_decls)
19089 *ambiguous_decls = NULL_TREE;
19090
19091 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19092 no longer valid. Note that if we are parsing tentatively, and
19093 the parse fails, OBJECT_TYPE will be automatically restored. */
19094 parser->context->object_type = NULL_TREE;
19095
19096 if (name == error_mark_node)
19097 return error_mark_node;
19098
19099 /* A template-id has already been resolved; there is no lookup to
19100 do. */
19101 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19102 return name;
19103 if (BASELINK_P (name))
19104 {
19105 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19106 == TEMPLATE_ID_EXPR);
19107 return name;
19108 }
19109
19110 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19111 it should already have been checked to make sure that the name
19112 used matches the type being destroyed. */
19113 if (TREE_CODE (name) == BIT_NOT_EXPR)
19114 {
19115 tree type;
19116
19117 /* Figure out to which type this destructor applies. */
19118 if (parser->scope)
19119 type = parser->scope;
19120 else if (object_type)
19121 type = object_type;
19122 else
19123 type = current_class_type;
19124 /* If that's not a class type, there is no destructor. */
19125 if (!type || !CLASS_TYPE_P (type))
19126 return error_mark_node;
19127 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19128 lazily_declare_fn (sfk_destructor, type);
19129 if (!CLASSTYPE_DESTRUCTORS (type))
19130 return error_mark_node;
19131 /* If it was a class type, return the destructor. */
19132 return CLASSTYPE_DESTRUCTORS (type);
19133 }
19134
19135 /* By this point, the NAME should be an ordinary identifier. If
19136 the id-expression was a qualified name, the qualifying scope is
19137 stored in PARSER->SCOPE at this point. */
19138 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19139
19140 /* Perform the lookup. */
19141 if (parser->scope)
19142 {
19143 bool dependent_p;
19144
19145 if (parser->scope == error_mark_node)
19146 return error_mark_node;
19147
19148 /* If the SCOPE is dependent, the lookup must be deferred until
19149 the template is instantiated -- unless we are explicitly
19150 looking up names in uninstantiated templates. Even then, we
19151 cannot look up the name if the scope is not a class type; it
19152 might, for example, be a template type parameter. */
19153 dependent_p = (TYPE_P (parser->scope)
19154 && dependent_scope_p (parser->scope));
19155 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19156 && dependent_p)
19157 /* Defer lookup. */
19158 decl = error_mark_node;
19159 else
19160 {
19161 tree pushed_scope = NULL_TREE;
19162
19163 /* If PARSER->SCOPE is a dependent type, then it must be a
19164 class type, and we must not be checking dependencies;
19165 otherwise, we would have processed this lookup above. So
19166 that PARSER->SCOPE is not considered a dependent base by
19167 lookup_member, we must enter the scope here. */
19168 if (dependent_p)
19169 pushed_scope = push_scope (parser->scope);
19170
19171 /* If the PARSER->SCOPE is a template specialization, it
19172 may be instantiated during name lookup. In that case,
19173 errors may be issued. Even if we rollback the current
19174 tentative parse, those errors are valid. */
19175 decl = lookup_qualified_name (parser->scope, name,
19176 tag_type != none_type,
19177 /*complain=*/true);
19178
19179 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19180 lookup result and the nested-name-specifier nominates a class C:
19181 * if the name specified after the nested-name-specifier, when
19182 looked up in C, is the injected-class-name of C (Clause 9), or
19183 * if the name specified after the nested-name-specifier is the
19184 same as the identifier or the simple-template-id's template-
19185 name in the last component of the nested-name-specifier,
19186 the name is instead considered to name the constructor of
19187 class C. [ Note: for example, the constructor is not an
19188 acceptable lookup result in an elaborated-type-specifier so
19189 the constructor would not be used in place of the
19190 injected-class-name. --end note ] Such a constructor name
19191 shall be used only in the declarator-id of a declaration that
19192 names a constructor or in a using-declaration. */
19193 if (tag_type == none_type
19194 && DECL_SELF_REFERENCE_P (decl)
19195 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19196 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19197 tag_type != none_type,
19198 /*complain=*/true);
19199
19200 /* If we have a single function from a using decl, pull it out. */
19201 if (TREE_CODE (decl) == OVERLOAD
19202 && !really_overloaded_fn (decl))
19203 decl = OVL_FUNCTION (decl);
19204
19205 if (pushed_scope)
19206 pop_scope (pushed_scope);
19207 }
19208
19209 /* If the scope is a dependent type and either we deferred lookup or
19210 we did lookup but didn't find the name, rememeber the name. */
19211 if (decl == error_mark_node && TYPE_P (parser->scope)
19212 && dependent_type_p (parser->scope))
19213 {
19214 if (tag_type)
19215 {
19216 tree type;
19217
19218 /* The resolution to Core Issue 180 says that `struct
19219 A::B' should be considered a type-name, even if `A'
19220 is dependent. */
19221 type = make_typename_type (parser->scope, name, tag_type,
19222 /*complain=*/tf_error);
19223 decl = TYPE_NAME (type);
19224 }
19225 else if (is_template
19226 && (cp_parser_next_token_ends_template_argument_p (parser)
19227 || cp_lexer_next_token_is (parser->lexer,
19228 CPP_CLOSE_PAREN)))
19229 decl = make_unbound_class_template (parser->scope,
19230 name, NULL_TREE,
19231 /*complain=*/tf_error);
19232 else
19233 decl = build_qualified_name (/*type=*/NULL_TREE,
19234 parser->scope, name,
19235 is_template);
19236 }
19237 parser->qualifying_scope = parser->scope;
19238 parser->object_scope = NULL_TREE;
19239 }
19240 else if (object_type)
19241 {
19242 tree object_decl = NULL_TREE;
19243 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19244 OBJECT_TYPE is not a class. */
19245 if (CLASS_TYPE_P (object_type))
19246 /* If the OBJECT_TYPE is a template specialization, it may
19247 be instantiated during name lookup. In that case, errors
19248 may be issued. Even if we rollback the current tentative
19249 parse, those errors are valid. */
19250 object_decl = lookup_member (object_type,
19251 name,
19252 /*protect=*/0,
19253 tag_type != none_type);
19254 /* Look it up in the enclosing context, too. */
19255 decl = lookup_name_real (name, tag_type != none_type,
19256 /*nonclass=*/0,
19257 /*block_p=*/true, is_namespace, flags);
19258 parser->object_scope = object_type;
19259 parser->qualifying_scope = NULL_TREE;
19260 if (object_decl)
19261 decl = object_decl;
19262 }
19263 else
19264 {
19265 decl = lookup_name_real (name, tag_type != none_type,
19266 /*nonclass=*/0,
19267 /*block_p=*/true, is_namespace, flags);
19268 parser->qualifying_scope = NULL_TREE;
19269 parser->object_scope = NULL_TREE;
19270 }
19271
19272 /* If the lookup failed, let our caller know. */
19273 if (!decl || decl == error_mark_node)
19274 return error_mark_node;
19275
19276 /* Pull out the template from an injected-class-name (or multiple). */
19277 if (is_template)
19278 decl = maybe_get_template_decl_from_type_decl (decl);
19279
19280 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19281 if (TREE_CODE (decl) == TREE_LIST)
19282 {
19283 if (ambiguous_decls)
19284 *ambiguous_decls = decl;
19285 /* The error message we have to print is too complicated for
19286 cp_parser_error, so we incorporate its actions directly. */
19287 if (!cp_parser_simulate_error (parser))
19288 {
19289 error_at (name_location, "reference to %qD is ambiguous",
19290 name);
19291 print_candidates (decl);
19292 }
19293 return error_mark_node;
19294 }
19295
19296 gcc_assert (DECL_P (decl)
19297 || TREE_CODE (decl) == OVERLOAD
19298 || TREE_CODE (decl) == SCOPE_REF
19299 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19300 || BASELINK_P (decl));
19301
19302 /* If we have resolved the name of a member declaration, check to
19303 see if the declaration is accessible. When the name resolves to
19304 set of overloaded functions, accessibility is checked when
19305 overload resolution is done.
19306
19307 During an explicit instantiation, access is not checked at all,
19308 as per [temp.explicit]. */
19309 if (DECL_P (decl))
19310 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19311
19312 return decl;
19313 }
19314
19315 /* Like cp_parser_lookup_name, but for use in the typical case where
19316 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19317 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19318
19319 static tree
19320 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19321 {
19322 return cp_parser_lookup_name (parser, name,
19323 none_type,
19324 /*is_template=*/false,
19325 /*is_namespace=*/false,
19326 /*check_dependency=*/true,
19327 /*ambiguous_decls=*/NULL,
19328 location);
19329 }
19330
19331 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19332 the current context, return the TYPE_DECL. If TAG_NAME_P is
19333 true, the DECL indicates the class being defined in a class-head,
19334 or declared in an elaborated-type-specifier.
19335
19336 Otherwise, return DECL. */
19337
19338 static tree
19339 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19340 {
19341 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19342 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19343
19344 struct A {
19345 template <typename T> struct B;
19346 };
19347
19348 template <typename T> struct A::B {};
19349
19350 Similarly, in an elaborated-type-specifier:
19351
19352 namespace N { struct X{}; }
19353
19354 struct A {
19355 template <typename T> friend struct N::X;
19356 };
19357
19358 However, if the DECL refers to a class type, and we are in
19359 the scope of the class, then the name lookup automatically
19360 finds the TYPE_DECL created by build_self_reference rather
19361 than a TEMPLATE_DECL. For example, in:
19362
19363 template <class T> struct S {
19364 S s;
19365 };
19366
19367 there is no need to handle such case. */
19368
19369 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19370 return DECL_TEMPLATE_RESULT (decl);
19371
19372 return decl;
19373 }
19374
19375 /* If too many, or too few, template-parameter lists apply to the
19376 declarator, issue an error message. Returns TRUE if all went well,
19377 and FALSE otherwise. */
19378
19379 static bool
19380 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19381 cp_declarator *declarator,
19382 location_t declarator_location)
19383 {
19384 unsigned num_templates;
19385
19386 /* We haven't seen any classes that involve template parameters yet. */
19387 num_templates = 0;
19388
19389 switch (declarator->kind)
19390 {
19391 case cdk_id:
19392 if (declarator->u.id.qualifying_scope)
19393 {
19394 tree scope;
19395
19396 scope = declarator->u.id.qualifying_scope;
19397
19398 while (scope && CLASS_TYPE_P (scope))
19399 {
19400 /* You're supposed to have one `template <...>'
19401 for every template class, but you don't need one
19402 for a full specialization. For example:
19403
19404 template <class T> struct S{};
19405 template <> struct S<int> { void f(); };
19406 void S<int>::f () {}
19407
19408 is correct; there shouldn't be a `template <>' for
19409 the definition of `S<int>::f'. */
19410 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19411 /* If SCOPE does not have template information of any
19412 kind, then it is not a template, nor is it nested
19413 within a template. */
19414 break;
19415 if (explicit_class_specialization_p (scope))
19416 break;
19417 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19418 ++num_templates;
19419
19420 scope = TYPE_CONTEXT (scope);
19421 }
19422 }
19423 else if (TREE_CODE (declarator->u.id.unqualified_name)
19424 == TEMPLATE_ID_EXPR)
19425 /* If the DECLARATOR has the form `X<y>' then it uses one
19426 additional level of template parameters. */
19427 ++num_templates;
19428
19429 return cp_parser_check_template_parameters
19430 (parser, num_templates, declarator_location, declarator);
19431
19432
19433 case cdk_function:
19434 case cdk_array:
19435 case cdk_pointer:
19436 case cdk_reference:
19437 case cdk_ptrmem:
19438 return (cp_parser_check_declarator_template_parameters
19439 (parser, declarator->declarator, declarator_location));
19440
19441 case cdk_error:
19442 return true;
19443
19444 default:
19445 gcc_unreachable ();
19446 }
19447 return false;
19448 }
19449
19450 /* NUM_TEMPLATES were used in the current declaration. If that is
19451 invalid, return FALSE and issue an error messages. Otherwise,
19452 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19453 declarator and we can print more accurate diagnostics. */
19454
19455 static bool
19456 cp_parser_check_template_parameters (cp_parser* parser,
19457 unsigned num_templates,
19458 location_t location,
19459 cp_declarator *declarator)
19460 {
19461 /* If there are the same number of template classes and parameter
19462 lists, that's OK. */
19463 if (parser->num_template_parameter_lists == num_templates)
19464 return true;
19465 /* If there are more, but only one more, then we are referring to a
19466 member template. That's OK too. */
19467 if (parser->num_template_parameter_lists == num_templates + 1)
19468 return true;
19469 /* If there are more template classes than parameter lists, we have
19470 something like:
19471
19472 template <class T> void S<T>::R<T>::f (); */
19473 if (parser->num_template_parameter_lists < num_templates)
19474 {
19475 if (declarator && !current_function_decl)
19476 error_at (location, "specializing member %<%T::%E%> "
19477 "requires %<template<>%> syntax",
19478 declarator->u.id.qualifying_scope,
19479 declarator->u.id.unqualified_name);
19480 else if (declarator)
19481 error_at (location, "invalid declaration of %<%T::%E%>",
19482 declarator->u.id.qualifying_scope,
19483 declarator->u.id.unqualified_name);
19484 else
19485 error_at (location, "too few template-parameter-lists");
19486 return false;
19487 }
19488 /* Otherwise, there are too many template parameter lists. We have
19489 something like:
19490
19491 template <class T> template <class U> void S::f(); */
19492 error_at (location, "too many template-parameter-lists");
19493 return false;
19494 }
19495
19496 /* Parse an optional `::' token indicating that the following name is
19497 from the global namespace. If so, PARSER->SCOPE is set to the
19498 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19499 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19500 Returns the new value of PARSER->SCOPE, if the `::' token is
19501 present, and NULL_TREE otherwise. */
19502
19503 static tree
19504 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19505 {
19506 cp_token *token;
19507
19508 /* Peek at the next token. */
19509 token = cp_lexer_peek_token (parser->lexer);
19510 /* If we're looking at a `::' token then we're starting from the
19511 global namespace, not our current location. */
19512 if (token->type == CPP_SCOPE)
19513 {
19514 /* Consume the `::' token. */
19515 cp_lexer_consume_token (parser->lexer);
19516 /* Set the SCOPE so that we know where to start the lookup. */
19517 parser->scope = global_namespace;
19518 parser->qualifying_scope = global_namespace;
19519 parser->object_scope = NULL_TREE;
19520
19521 return parser->scope;
19522 }
19523 else if (!current_scope_valid_p)
19524 {
19525 parser->scope = NULL_TREE;
19526 parser->qualifying_scope = NULL_TREE;
19527 parser->object_scope = NULL_TREE;
19528 }
19529
19530 return NULL_TREE;
19531 }
19532
19533 /* Returns TRUE if the upcoming token sequence is the start of a
19534 constructor declarator. If FRIEND_P is true, the declarator is
19535 preceded by the `friend' specifier. */
19536
19537 static bool
19538 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19539 {
19540 bool constructor_p;
19541 tree nested_name_specifier;
19542 cp_token *next_token;
19543
19544 /* The common case is that this is not a constructor declarator, so
19545 try to avoid doing lots of work if at all possible. It's not
19546 valid declare a constructor at function scope. */
19547 if (parser->in_function_body)
19548 return false;
19549 /* And only certain tokens can begin a constructor declarator. */
19550 next_token = cp_lexer_peek_token (parser->lexer);
19551 if (next_token->type != CPP_NAME
19552 && next_token->type != CPP_SCOPE
19553 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19554 && next_token->type != CPP_TEMPLATE_ID)
19555 return false;
19556
19557 /* Parse tentatively; we are going to roll back all of the tokens
19558 consumed here. */
19559 cp_parser_parse_tentatively (parser);
19560 /* Assume that we are looking at a constructor declarator. */
19561 constructor_p = true;
19562
19563 /* Look for the optional `::' operator. */
19564 cp_parser_global_scope_opt (parser,
19565 /*current_scope_valid_p=*/false);
19566 /* Look for the nested-name-specifier. */
19567 nested_name_specifier
19568 = (cp_parser_nested_name_specifier_opt (parser,
19569 /*typename_keyword_p=*/false,
19570 /*check_dependency_p=*/false,
19571 /*type_p=*/false,
19572 /*is_declaration=*/false));
19573 /* Outside of a class-specifier, there must be a
19574 nested-name-specifier. */
19575 if (!nested_name_specifier &&
19576 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19577 || friend_p))
19578 constructor_p = false;
19579 else if (nested_name_specifier == error_mark_node)
19580 constructor_p = false;
19581
19582 /* If we have a class scope, this is easy; DR 147 says that S::S always
19583 names the constructor, and no other qualified name could. */
19584 if (constructor_p && nested_name_specifier
19585 && TYPE_P (nested_name_specifier))
19586 {
19587 tree id = cp_parser_unqualified_id (parser,
19588 /*template_keyword_p=*/false,
19589 /*check_dependency_p=*/false,
19590 /*declarator_p=*/true,
19591 /*optional_p=*/false);
19592 if (is_overloaded_fn (id))
19593 id = DECL_NAME (get_first_fn (id));
19594 if (!constructor_name_p (id, nested_name_specifier))
19595 constructor_p = false;
19596 }
19597 /* If we still think that this might be a constructor-declarator,
19598 look for a class-name. */
19599 else if (constructor_p)
19600 {
19601 /* If we have:
19602
19603 template <typename T> struct S {
19604 S();
19605 };
19606
19607 we must recognize that the nested `S' names a class. */
19608 tree type_decl;
19609 type_decl = cp_parser_class_name (parser,
19610 /*typename_keyword_p=*/false,
19611 /*template_keyword_p=*/false,
19612 none_type,
19613 /*check_dependency_p=*/false,
19614 /*class_head_p=*/false,
19615 /*is_declaration=*/false);
19616 /* If there was no class-name, then this is not a constructor. */
19617 constructor_p = !cp_parser_error_occurred (parser);
19618
19619 /* If we're still considering a constructor, we have to see a `(',
19620 to begin the parameter-declaration-clause, followed by either a
19621 `)', an `...', or a decl-specifier. We need to check for a
19622 type-specifier to avoid being fooled into thinking that:
19623
19624 S (f) (int);
19625
19626 is a constructor. (It is actually a function named `f' that
19627 takes one parameter (of type `int') and returns a value of type
19628 `S'. */
19629 if (constructor_p
19630 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19631 constructor_p = false;
19632
19633 if (constructor_p
19634 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19635 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19636 /* A parameter declaration begins with a decl-specifier,
19637 which is either the "attribute" keyword, a storage class
19638 specifier, or (usually) a type-specifier. */
19639 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19640 {
19641 tree type;
19642 tree pushed_scope = NULL_TREE;
19643 unsigned saved_num_template_parameter_lists;
19644
19645 /* Names appearing in the type-specifier should be looked up
19646 in the scope of the class. */
19647 if (current_class_type)
19648 type = NULL_TREE;
19649 else
19650 {
19651 type = TREE_TYPE (type_decl);
19652 if (TREE_CODE (type) == TYPENAME_TYPE)
19653 {
19654 type = resolve_typename_type (type,
19655 /*only_current_p=*/false);
19656 if (TREE_CODE (type) == TYPENAME_TYPE)
19657 {
19658 cp_parser_abort_tentative_parse (parser);
19659 return false;
19660 }
19661 }
19662 pushed_scope = push_scope (type);
19663 }
19664
19665 /* Inside the constructor parameter list, surrounding
19666 template-parameter-lists do not apply. */
19667 saved_num_template_parameter_lists
19668 = parser->num_template_parameter_lists;
19669 parser->num_template_parameter_lists = 0;
19670
19671 /* Look for the type-specifier. */
19672 cp_parser_type_specifier (parser,
19673 CP_PARSER_FLAGS_NONE,
19674 /*decl_specs=*/NULL,
19675 /*is_declarator=*/true,
19676 /*declares_class_or_enum=*/NULL,
19677 /*is_cv_qualifier=*/NULL);
19678
19679 parser->num_template_parameter_lists
19680 = saved_num_template_parameter_lists;
19681
19682 /* Leave the scope of the class. */
19683 if (pushed_scope)
19684 pop_scope (pushed_scope);
19685
19686 constructor_p = !cp_parser_error_occurred (parser);
19687 }
19688 }
19689
19690 /* We did not really want to consume any tokens. */
19691 cp_parser_abort_tentative_parse (parser);
19692
19693 return constructor_p;
19694 }
19695
19696 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19697 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19698 they must be performed once we are in the scope of the function.
19699
19700 Returns the function defined. */
19701
19702 static tree
19703 cp_parser_function_definition_from_specifiers_and_declarator
19704 (cp_parser* parser,
19705 cp_decl_specifier_seq *decl_specifiers,
19706 tree attributes,
19707 const cp_declarator *declarator)
19708 {
19709 tree fn;
19710 bool success_p;
19711
19712 /* Begin the function-definition. */
19713 success_p = start_function (decl_specifiers, declarator, attributes);
19714
19715 /* The things we're about to see are not directly qualified by any
19716 template headers we've seen thus far. */
19717 reset_specialization ();
19718
19719 /* If there were names looked up in the decl-specifier-seq that we
19720 did not check, check them now. We must wait until we are in the
19721 scope of the function to perform the checks, since the function
19722 might be a friend. */
19723 perform_deferred_access_checks ();
19724
19725 if (!success_p)
19726 {
19727 /* Skip the entire function. */
19728 cp_parser_skip_to_end_of_block_or_statement (parser);
19729 fn = error_mark_node;
19730 }
19731 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19732 {
19733 /* Seen already, skip it. An error message has already been output. */
19734 cp_parser_skip_to_end_of_block_or_statement (parser);
19735 fn = current_function_decl;
19736 current_function_decl = NULL_TREE;
19737 /* If this is a function from a class, pop the nested class. */
19738 if (current_class_name)
19739 pop_nested_class ();
19740 }
19741 else
19742 {
19743 timevar_id_t tv;
19744 if (DECL_DECLARED_INLINE_P (current_function_decl))
19745 tv = TV_PARSE_INLINE;
19746 else
19747 tv = TV_PARSE_FUNC;
19748 timevar_push (tv);
19749 fn = cp_parser_function_definition_after_declarator (parser,
19750 /*inline_p=*/false);
19751 timevar_pop (tv);
19752 }
19753
19754 return fn;
19755 }
19756
19757 /* Parse the part of a function-definition that follows the
19758 declarator. INLINE_P is TRUE iff this function is an inline
19759 function defined within a class-specifier.
19760
19761 Returns the function defined. */
19762
19763 static tree
19764 cp_parser_function_definition_after_declarator (cp_parser* parser,
19765 bool inline_p)
19766 {
19767 tree fn;
19768 bool ctor_initializer_p = false;
19769 bool saved_in_unbraced_linkage_specification_p;
19770 bool saved_in_function_body;
19771 unsigned saved_num_template_parameter_lists;
19772 cp_token *token;
19773
19774 saved_in_function_body = parser->in_function_body;
19775 parser->in_function_body = true;
19776 /* If the next token is `return', then the code may be trying to
19777 make use of the "named return value" extension that G++ used to
19778 support. */
19779 token = cp_lexer_peek_token (parser->lexer);
19780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19781 {
19782 /* Consume the `return' keyword. */
19783 cp_lexer_consume_token (parser->lexer);
19784 /* Look for the identifier that indicates what value is to be
19785 returned. */
19786 cp_parser_identifier (parser);
19787 /* Issue an error message. */
19788 error_at (token->location,
19789 "named return values are no longer supported");
19790 /* Skip tokens until we reach the start of the function body. */
19791 while (true)
19792 {
19793 cp_token *token = cp_lexer_peek_token (parser->lexer);
19794 if (token->type == CPP_OPEN_BRACE
19795 || token->type == CPP_EOF
19796 || token->type == CPP_PRAGMA_EOL)
19797 break;
19798 cp_lexer_consume_token (parser->lexer);
19799 }
19800 }
19801 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19802 anything declared inside `f'. */
19803 saved_in_unbraced_linkage_specification_p
19804 = parser->in_unbraced_linkage_specification_p;
19805 parser->in_unbraced_linkage_specification_p = false;
19806 /* Inside the function, surrounding template-parameter-lists do not
19807 apply. */
19808 saved_num_template_parameter_lists
19809 = parser->num_template_parameter_lists;
19810 parser->num_template_parameter_lists = 0;
19811
19812 start_lambda_scope (current_function_decl);
19813
19814 /* If the next token is `try', then we are looking at a
19815 function-try-block. */
19816 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19817 ctor_initializer_p = cp_parser_function_try_block (parser);
19818 /* A function-try-block includes the function-body, so we only do
19819 this next part if we're not processing a function-try-block. */
19820 else
19821 ctor_initializer_p
19822 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19823
19824 finish_lambda_scope ();
19825
19826 /* Finish the function. */
19827 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19828 (inline_p ? 2 : 0));
19829 /* Generate code for it, if necessary. */
19830 expand_or_defer_fn (fn);
19831 /* Restore the saved values. */
19832 parser->in_unbraced_linkage_specification_p
19833 = saved_in_unbraced_linkage_specification_p;
19834 parser->num_template_parameter_lists
19835 = saved_num_template_parameter_lists;
19836 parser->in_function_body = saved_in_function_body;
19837
19838 return fn;
19839 }
19840
19841 /* Parse a template-declaration, assuming that the `export' (and
19842 `extern') keywords, if present, has already been scanned. MEMBER_P
19843 is as for cp_parser_template_declaration. */
19844
19845 static void
19846 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19847 {
19848 tree decl = NULL_TREE;
19849 VEC (deferred_access_check,gc) *checks;
19850 tree parameter_list;
19851 bool friend_p = false;
19852 bool need_lang_pop;
19853 cp_token *token;
19854
19855 /* Look for the `template' keyword. */
19856 token = cp_lexer_peek_token (parser->lexer);
19857 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19858 return;
19859
19860 /* And the `<'. */
19861 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19862 return;
19863 if (at_class_scope_p () && current_function_decl)
19864 {
19865 /* 14.5.2.2 [temp.mem]
19866
19867 A local class shall not have member templates. */
19868 error_at (token->location,
19869 "invalid declaration of member template in local class");
19870 cp_parser_skip_to_end_of_block_or_statement (parser);
19871 return;
19872 }
19873 /* [temp]
19874
19875 A template ... shall not have C linkage. */
19876 if (current_lang_name == lang_name_c)
19877 {
19878 error_at (token->location, "template with C linkage");
19879 /* Give it C++ linkage to avoid confusing other parts of the
19880 front end. */
19881 push_lang_context (lang_name_cplusplus);
19882 need_lang_pop = true;
19883 }
19884 else
19885 need_lang_pop = false;
19886
19887 /* We cannot perform access checks on the template parameter
19888 declarations until we know what is being declared, just as we
19889 cannot check the decl-specifier list. */
19890 push_deferring_access_checks (dk_deferred);
19891
19892 /* If the next token is `>', then we have an invalid
19893 specialization. Rather than complain about an invalid template
19894 parameter, issue an error message here. */
19895 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19896 {
19897 cp_parser_error (parser, "invalid explicit specialization");
19898 begin_specialization ();
19899 parameter_list = NULL_TREE;
19900 }
19901 else
19902 {
19903 /* Parse the template parameters. */
19904 parameter_list = cp_parser_template_parameter_list (parser);
19905 fixup_template_parms ();
19906 }
19907
19908 /* Get the deferred access checks from the parameter list. These
19909 will be checked once we know what is being declared, as for a
19910 member template the checks must be performed in the scope of the
19911 class containing the member. */
19912 checks = get_deferred_access_checks ();
19913
19914 /* Look for the `>'. */
19915 cp_parser_skip_to_end_of_template_parameter_list (parser);
19916 /* We just processed one more parameter list. */
19917 ++parser->num_template_parameter_lists;
19918 /* If the next token is `template', there are more template
19919 parameters. */
19920 if (cp_lexer_next_token_is_keyword (parser->lexer,
19921 RID_TEMPLATE))
19922 cp_parser_template_declaration_after_export (parser, member_p);
19923 else
19924 {
19925 /* There are no access checks when parsing a template, as we do not
19926 know if a specialization will be a friend. */
19927 push_deferring_access_checks (dk_no_check);
19928 token = cp_lexer_peek_token (parser->lexer);
19929 decl = cp_parser_single_declaration (parser,
19930 checks,
19931 member_p,
19932 /*explicit_specialization_p=*/false,
19933 &friend_p);
19934 pop_deferring_access_checks ();
19935
19936 /* If this is a member template declaration, let the front
19937 end know. */
19938 if (member_p && !friend_p && decl)
19939 {
19940 if (TREE_CODE (decl) == TYPE_DECL)
19941 cp_parser_check_access_in_redeclaration (decl, token->location);
19942
19943 decl = finish_member_template_decl (decl);
19944 }
19945 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19946 make_friend_class (current_class_type, TREE_TYPE (decl),
19947 /*complain=*/true);
19948 }
19949 /* We are done with the current parameter list. */
19950 --parser->num_template_parameter_lists;
19951
19952 pop_deferring_access_checks ();
19953
19954 /* Finish up. */
19955 finish_template_decl (parameter_list);
19956
19957 /* Register member declarations. */
19958 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19959 finish_member_declaration (decl);
19960 /* For the erroneous case of a template with C linkage, we pushed an
19961 implicit C++ linkage scope; exit that scope now. */
19962 if (need_lang_pop)
19963 pop_lang_context ();
19964 /* If DECL is a function template, we must return to parse it later.
19965 (Even though there is no definition, there might be default
19966 arguments that need handling.) */
19967 if (member_p && decl
19968 && (TREE_CODE (decl) == FUNCTION_DECL
19969 || DECL_FUNCTION_TEMPLATE_P (decl)))
19970 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19971 }
19972
19973 /* Perform the deferred access checks from a template-parameter-list.
19974 CHECKS is a TREE_LIST of access checks, as returned by
19975 get_deferred_access_checks. */
19976
19977 static void
19978 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19979 {
19980 ++processing_template_parmlist;
19981 perform_access_checks (checks);
19982 --processing_template_parmlist;
19983 }
19984
19985 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19986 `function-definition' sequence. MEMBER_P is true, this declaration
19987 appears in a class scope.
19988
19989 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19990 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19991
19992 static tree
19993 cp_parser_single_declaration (cp_parser* parser,
19994 VEC (deferred_access_check,gc)* checks,
19995 bool member_p,
19996 bool explicit_specialization_p,
19997 bool* friend_p)
19998 {
19999 int declares_class_or_enum;
20000 tree decl = NULL_TREE;
20001 cp_decl_specifier_seq decl_specifiers;
20002 bool function_definition_p = false;
20003 cp_token *decl_spec_token_start;
20004
20005 /* This function is only used when processing a template
20006 declaration. */
20007 gcc_assert (innermost_scope_kind () == sk_template_parms
20008 || innermost_scope_kind () == sk_template_spec);
20009
20010 /* Defer access checks until we know what is being declared. */
20011 push_deferring_access_checks (dk_deferred);
20012
20013 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20014 alternative. */
20015 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20016 cp_parser_decl_specifier_seq (parser,
20017 CP_PARSER_FLAGS_OPTIONAL,
20018 &decl_specifiers,
20019 &declares_class_or_enum);
20020 if (friend_p)
20021 *friend_p = cp_parser_friend_p (&decl_specifiers);
20022
20023 /* There are no template typedefs. */
20024 if (decl_specifiers.specs[(int) ds_typedef])
20025 {
20026 error_at (decl_spec_token_start->location,
20027 "template declaration of %<typedef%>");
20028 decl = error_mark_node;
20029 }
20030
20031 /* Gather up the access checks that occurred the
20032 decl-specifier-seq. */
20033 stop_deferring_access_checks ();
20034
20035 /* Check for the declaration of a template class. */
20036 if (declares_class_or_enum)
20037 {
20038 if (cp_parser_declares_only_class_p (parser))
20039 {
20040 decl = shadow_tag (&decl_specifiers);
20041
20042 /* In this case:
20043
20044 struct C {
20045 friend template <typename T> struct A<T>::B;
20046 };
20047
20048 A<T>::B will be represented by a TYPENAME_TYPE, and
20049 therefore not recognized by shadow_tag. */
20050 if (friend_p && *friend_p
20051 && !decl
20052 && decl_specifiers.type
20053 && TYPE_P (decl_specifiers.type))
20054 decl = decl_specifiers.type;
20055
20056 if (decl && decl != error_mark_node)
20057 decl = TYPE_NAME (decl);
20058 else
20059 decl = error_mark_node;
20060
20061 /* Perform access checks for template parameters. */
20062 cp_parser_perform_template_parameter_access_checks (checks);
20063 }
20064 }
20065
20066 /* Complain about missing 'typename' or other invalid type names. */
20067 if (!decl_specifiers.any_type_specifiers_p
20068 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20069 {
20070 /* cp_parser_parse_and_diagnose_invalid_type_name calls
20071 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20072 the rest of this declaration. */
20073 decl = error_mark_node;
20074 goto out;
20075 }
20076
20077 /* If it's not a template class, try for a template function. If
20078 the next token is a `;', then this declaration does not declare
20079 anything. But, if there were errors in the decl-specifiers, then
20080 the error might well have come from an attempted class-specifier.
20081 In that case, there's no need to warn about a missing declarator. */
20082 if (!decl
20083 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20084 || decl_specifiers.type != error_mark_node))
20085 {
20086 decl = cp_parser_init_declarator (parser,
20087 &decl_specifiers,
20088 checks,
20089 /*function_definition_allowed_p=*/true,
20090 member_p,
20091 declares_class_or_enum,
20092 &function_definition_p,
20093 NULL);
20094
20095 /* 7.1.1-1 [dcl.stc]
20096
20097 A storage-class-specifier shall not be specified in an explicit
20098 specialization... */
20099 if (decl
20100 && explicit_specialization_p
20101 && decl_specifiers.storage_class != sc_none)
20102 {
20103 error_at (decl_spec_token_start->location,
20104 "explicit template specialization cannot have a storage class");
20105 decl = error_mark_node;
20106 }
20107 }
20108
20109 /* Look for a trailing `;' after the declaration. */
20110 if (!function_definition_p
20111 && (decl == error_mark_node
20112 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20113 cp_parser_skip_to_end_of_block_or_statement (parser);
20114
20115 out:
20116 pop_deferring_access_checks ();
20117
20118 /* Clear any current qualification; whatever comes next is the start
20119 of something new. */
20120 parser->scope = NULL_TREE;
20121 parser->qualifying_scope = NULL_TREE;
20122 parser->object_scope = NULL_TREE;
20123
20124 return decl;
20125 }
20126
20127 /* Parse a cast-expression that is not the operand of a unary "&". */
20128
20129 static tree
20130 cp_parser_simple_cast_expression (cp_parser *parser)
20131 {
20132 return cp_parser_cast_expression (parser, /*address_p=*/false,
20133 /*cast_p=*/false, NULL);
20134 }
20135
20136 /* Parse a functional cast to TYPE. Returns an expression
20137 representing the cast. */
20138
20139 static tree
20140 cp_parser_functional_cast (cp_parser* parser, tree type)
20141 {
20142 VEC(tree,gc) *vec;
20143 tree expression_list;
20144 tree cast;
20145 bool nonconst_p;
20146
20147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20148 {
20149 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20150 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20151 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20152 if (TREE_CODE (type) == TYPE_DECL)
20153 type = TREE_TYPE (type);
20154 return finish_compound_literal (type, expression_list,
20155 tf_warning_or_error);
20156 }
20157
20158
20159 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20160 /*cast_p=*/true,
20161 /*allow_expansion_p=*/true,
20162 /*non_constant_p=*/NULL);
20163 if (vec == NULL)
20164 expression_list = error_mark_node;
20165 else
20166 {
20167 expression_list = build_tree_list_vec (vec);
20168 release_tree_vector (vec);
20169 }
20170
20171 cast = build_functional_cast (type, expression_list,
20172 tf_warning_or_error);
20173 /* [expr.const]/1: In an integral constant expression "only type
20174 conversions to integral or enumeration type can be used". */
20175 if (TREE_CODE (type) == TYPE_DECL)
20176 type = TREE_TYPE (type);
20177 if (cast != error_mark_node
20178 && !cast_valid_in_integral_constant_expression_p (type)
20179 && cp_parser_non_integral_constant_expression (parser,
20180 NIC_CONSTRUCTOR))
20181 return error_mark_node;
20182 return cast;
20183 }
20184
20185 /* Save the tokens that make up the body of a member function defined
20186 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20187 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20188 specifiers applied to the declaration. Returns the FUNCTION_DECL
20189 for the member function. */
20190
20191 static tree
20192 cp_parser_save_member_function_body (cp_parser* parser,
20193 cp_decl_specifier_seq *decl_specifiers,
20194 cp_declarator *declarator,
20195 tree attributes)
20196 {
20197 cp_token *first;
20198 cp_token *last;
20199 tree fn;
20200
20201 /* Create the FUNCTION_DECL. */
20202 fn = grokmethod (decl_specifiers, declarator, attributes);
20203 /* If something went badly wrong, bail out now. */
20204 if (fn == error_mark_node)
20205 {
20206 /* If there's a function-body, skip it. */
20207 if (cp_parser_token_starts_function_definition_p
20208 (cp_lexer_peek_token (parser->lexer)))
20209 cp_parser_skip_to_end_of_block_or_statement (parser);
20210 return error_mark_node;
20211 }
20212
20213 /* Remember it, if there default args to post process. */
20214 cp_parser_save_default_args (parser, fn);
20215
20216 /* Save away the tokens that make up the body of the
20217 function. */
20218 first = parser->lexer->next_token;
20219 /* We can have braced-init-list mem-initializers before the fn body. */
20220 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20221 {
20222 cp_lexer_consume_token (parser->lexer);
20223 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20224 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20225 {
20226 /* cache_group will stop after an un-nested { } pair, too. */
20227 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20228 break;
20229
20230 /* variadic mem-inits have ... after the ')'. */
20231 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20232 cp_lexer_consume_token (parser->lexer);
20233 }
20234 }
20235 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20236 /* Handle function try blocks. */
20237 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20238 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20239 last = parser->lexer->next_token;
20240
20241 /* Save away the inline definition; we will process it when the
20242 class is complete. */
20243 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20244 DECL_PENDING_INLINE_P (fn) = 1;
20245
20246 /* We need to know that this was defined in the class, so that
20247 friend templates are handled correctly. */
20248 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20249
20250 /* Add FN to the queue of functions to be parsed later. */
20251 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20252
20253 return fn;
20254 }
20255
20256 /* Parse a template-argument-list, as well as the trailing ">" (but
20257 not the opening ">"). See cp_parser_template_argument_list for the
20258 return value. */
20259
20260 static tree
20261 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20262 {
20263 tree arguments;
20264 tree saved_scope;
20265 tree saved_qualifying_scope;
20266 tree saved_object_scope;
20267 bool saved_greater_than_is_operator_p;
20268 int saved_unevaluated_operand;
20269 int saved_inhibit_evaluation_warnings;
20270
20271 /* [temp.names]
20272
20273 When parsing a template-id, the first non-nested `>' is taken as
20274 the end of the template-argument-list rather than a greater-than
20275 operator. */
20276 saved_greater_than_is_operator_p
20277 = parser->greater_than_is_operator_p;
20278 parser->greater_than_is_operator_p = false;
20279 /* Parsing the argument list may modify SCOPE, so we save it
20280 here. */
20281 saved_scope = parser->scope;
20282 saved_qualifying_scope = parser->qualifying_scope;
20283 saved_object_scope = parser->object_scope;
20284 /* We need to evaluate the template arguments, even though this
20285 template-id may be nested within a "sizeof". */
20286 saved_unevaluated_operand = cp_unevaluated_operand;
20287 cp_unevaluated_operand = 0;
20288 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20289 c_inhibit_evaluation_warnings = 0;
20290 /* Parse the template-argument-list itself. */
20291 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20292 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20293 arguments = NULL_TREE;
20294 else
20295 arguments = cp_parser_template_argument_list (parser);
20296 /* Look for the `>' that ends the template-argument-list. If we find
20297 a '>>' instead, it's probably just a typo. */
20298 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20299 {
20300 if (cxx_dialect != cxx98)
20301 {
20302 /* In C++0x, a `>>' in a template argument list or cast
20303 expression is considered to be two separate `>'
20304 tokens. So, change the current token to a `>', but don't
20305 consume it: it will be consumed later when the outer
20306 template argument list (or cast expression) is parsed.
20307 Note that this replacement of `>' for `>>' is necessary
20308 even if we are parsing tentatively: in the tentative
20309 case, after calling
20310 cp_parser_enclosed_template_argument_list we will always
20311 throw away all of the template arguments and the first
20312 closing `>', either because the template argument list
20313 was erroneous or because we are replacing those tokens
20314 with a CPP_TEMPLATE_ID token. The second `>' (which will
20315 not have been thrown away) is needed either to close an
20316 outer template argument list or to complete a new-style
20317 cast. */
20318 cp_token *token = cp_lexer_peek_token (parser->lexer);
20319 token->type = CPP_GREATER;
20320 }
20321 else if (!saved_greater_than_is_operator_p)
20322 {
20323 /* If we're in a nested template argument list, the '>>' has
20324 to be a typo for '> >'. We emit the error message, but we
20325 continue parsing and we push a '>' as next token, so that
20326 the argument list will be parsed correctly. Note that the
20327 global source location is still on the token before the
20328 '>>', so we need to say explicitly where we want it. */
20329 cp_token *token = cp_lexer_peek_token (parser->lexer);
20330 error_at (token->location, "%<>>%> should be %<> >%> "
20331 "within a nested template argument list");
20332
20333 token->type = CPP_GREATER;
20334 }
20335 else
20336 {
20337 /* If this is not a nested template argument list, the '>>'
20338 is a typo for '>'. Emit an error message and continue.
20339 Same deal about the token location, but here we can get it
20340 right by consuming the '>>' before issuing the diagnostic. */
20341 cp_token *token = cp_lexer_consume_token (parser->lexer);
20342 error_at (token->location,
20343 "spurious %<>>%>, use %<>%> to terminate "
20344 "a template argument list");
20345 }
20346 }
20347 else
20348 cp_parser_skip_to_end_of_template_parameter_list (parser);
20349 /* The `>' token might be a greater-than operator again now. */
20350 parser->greater_than_is_operator_p
20351 = saved_greater_than_is_operator_p;
20352 /* Restore the SAVED_SCOPE. */
20353 parser->scope = saved_scope;
20354 parser->qualifying_scope = saved_qualifying_scope;
20355 parser->object_scope = saved_object_scope;
20356 cp_unevaluated_operand = saved_unevaluated_operand;
20357 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20358
20359 return arguments;
20360 }
20361
20362 /* MEMBER_FUNCTION is a member function, or a friend. If default
20363 arguments, or the body of the function have not yet been parsed,
20364 parse them now. */
20365
20366 static void
20367 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20368 {
20369 timevar_push (TV_PARSE_INMETH);
20370 /* If this member is a template, get the underlying
20371 FUNCTION_DECL. */
20372 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20373 member_function = DECL_TEMPLATE_RESULT (member_function);
20374
20375 /* There should not be any class definitions in progress at this
20376 point; the bodies of members are only parsed outside of all class
20377 definitions. */
20378 gcc_assert (parser->num_classes_being_defined == 0);
20379 /* While we're parsing the member functions we might encounter more
20380 classes. We want to handle them right away, but we don't want
20381 them getting mixed up with functions that are currently in the
20382 queue. */
20383 push_unparsed_function_queues (parser);
20384
20385 /* Make sure that any template parameters are in scope. */
20386 maybe_begin_member_template_processing (member_function);
20387
20388 /* If the body of the function has not yet been parsed, parse it
20389 now. */
20390 if (DECL_PENDING_INLINE_P (member_function))
20391 {
20392 tree function_scope;
20393 cp_token_cache *tokens;
20394
20395 /* The function is no longer pending; we are processing it. */
20396 tokens = DECL_PENDING_INLINE_INFO (member_function);
20397 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20398 DECL_PENDING_INLINE_P (member_function) = 0;
20399
20400 /* If this is a local class, enter the scope of the containing
20401 function. */
20402 function_scope = current_function_decl;
20403 if (function_scope)
20404 push_function_context ();
20405
20406 /* Push the body of the function onto the lexer stack. */
20407 cp_parser_push_lexer_for_tokens (parser, tokens);
20408
20409 /* Let the front end know that we going to be defining this
20410 function. */
20411 start_preparsed_function (member_function, NULL_TREE,
20412 SF_PRE_PARSED | SF_INCLASS_INLINE);
20413
20414 /* Don't do access checking if it is a templated function. */
20415 if (processing_template_decl)
20416 push_deferring_access_checks (dk_no_check);
20417
20418 /* Now, parse the body of the function. */
20419 cp_parser_function_definition_after_declarator (parser,
20420 /*inline_p=*/true);
20421
20422 if (processing_template_decl)
20423 pop_deferring_access_checks ();
20424
20425 /* Leave the scope of the containing function. */
20426 if (function_scope)
20427 pop_function_context ();
20428 cp_parser_pop_lexer (parser);
20429 }
20430
20431 /* Remove any template parameters from the symbol table. */
20432 maybe_end_member_template_processing ();
20433
20434 /* Restore the queue. */
20435 pop_unparsed_function_queues (parser);
20436 timevar_pop (TV_PARSE_INMETH);
20437 }
20438
20439 /* If DECL contains any default args, remember it on the unparsed
20440 functions queue. */
20441
20442 static void
20443 cp_parser_save_default_args (cp_parser* parser, tree decl)
20444 {
20445 tree probe;
20446
20447 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20448 probe;
20449 probe = TREE_CHAIN (probe))
20450 if (TREE_PURPOSE (probe))
20451 {
20452 cp_default_arg_entry *entry
20453 = VEC_safe_push (cp_default_arg_entry, gc,
20454 unparsed_funs_with_default_args, NULL);
20455 entry->class_type = current_class_type;
20456 entry->decl = decl;
20457 break;
20458 }
20459 }
20460
20461 /* FN is a FUNCTION_DECL which may contains a parameter with an
20462 unparsed DEFAULT_ARG. Parse the default args now. This function
20463 assumes that the current scope is the scope in which the default
20464 argument should be processed. */
20465
20466 static void
20467 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20468 {
20469 bool saved_local_variables_forbidden_p;
20470 tree parm, parmdecl;
20471
20472 /* While we're parsing the default args, we might (due to the
20473 statement expression extension) encounter more classes. We want
20474 to handle them right away, but we don't want them getting mixed
20475 up with default args that are currently in the queue. */
20476 push_unparsed_function_queues (parser);
20477
20478 /* Local variable names (and the `this' keyword) may not appear
20479 in a default argument. */
20480 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20481 parser->local_variables_forbidden_p = true;
20482
20483 push_defarg_context (fn);
20484
20485 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20486 parmdecl = DECL_ARGUMENTS (fn);
20487 parm && parm != void_list_node;
20488 parm = TREE_CHAIN (parm),
20489 parmdecl = DECL_CHAIN (parmdecl))
20490 {
20491 cp_token_cache *tokens;
20492 tree default_arg = TREE_PURPOSE (parm);
20493 tree parsed_arg;
20494 VEC(tree,gc) *insts;
20495 tree copy;
20496 unsigned ix;
20497
20498 if (!default_arg)
20499 continue;
20500
20501 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20502 /* This can happen for a friend declaration for a function
20503 already declared with default arguments. */
20504 continue;
20505
20506 /* Push the saved tokens for the default argument onto the parser's
20507 lexer stack. */
20508 tokens = DEFARG_TOKENS (default_arg);
20509 cp_parser_push_lexer_for_tokens (parser, tokens);
20510
20511 start_lambda_scope (parmdecl);
20512
20513 /* Parse the assignment-expression. */
20514 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20515 if (parsed_arg == error_mark_node)
20516 {
20517 cp_parser_pop_lexer (parser);
20518 continue;
20519 }
20520
20521 if (!processing_template_decl)
20522 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20523
20524 TREE_PURPOSE (parm) = parsed_arg;
20525
20526 /* Update any instantiations we've already created. */
20527 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20528 VEC_iterate (tree, insts, ix, copy); ix++)
20529 TREE_PURPOSE (copy) = parsed_arg;
20530
20531 finish_lambda_scope ();
20532
20533 /* If the token stream has not been completely used up, then
20534 there was extra junk after the end of the default
20535 argument. */
20536 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20537 cp_parser_error (parser, "expected %<,%>");
20538
20539 /* Revert to the main lexer. */
20540 cp_parser_pop_lexer (parser);
20541 }
20542
20543 pop_defarg_context ();
20544
20545 /* Make sure no default arg is missing. */
20546 check_default_args (fn);
20547
20548 /* Restore the state of local_variables_forbidden_p. */
20549 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20550
20551 /* Restore the queue. */
20552 pop_unparsed_function_queues (parser);
20553 }
20554
20555 /* Parse the operand of `sizeof' (or a similar operator). Returns
20556 either a TYPE or an expression, depending on the form of the
20557 input. The KEYWORD indicates which kind of expression we have
20558 encountered. */
20559
20560 static tree
20561 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20562 {
20563 tree expr = NULL_TREE;
20564 const char *saved_message;
20565 char *tmp;
20566 bool saved_integral_constant_expression_p;
20567 bool saved_non_integral_constant_expression_p;
20568 bool pack_expansion_p = false;
20569
20570 /* Types cannot be defined in a `sizeof' expression. Save away the
20571 old message. */
20572 saved_message = parser->type_definition_forbidden_message;
20573 /* And create the new one. */
20574 tmp = concat ("types may not be defined in %<",
20575 IDENTIFIER_POINTER (ridpointers[keyword]),
20576 "%> expressions", NULL);
20577 parser->type_definition_forbidden_message = tmp;
20578
20579 /* The restrictions on constant-expressions do not apply inside
20580 sizeof expressions. */
20581 saved_integral_constant_expression_p
20582 = parser->integral_constant_expression_p;
20583 saved_non_integral_constant_expression_p
20584 = parser->non_integral_constant_expression_p;
20585 parser->integral_constant_expression_p = false;
20586
20587 /* If it's a `...', then we are computing the length of a parameter
20588 pack. */
20589 if (keyword == RID_SIZEOF
20590 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20591 {
20592 /* Consume the `...'. */
20593 cp_lexer_consume_token (parser->lexer);
20594 maybe_warn_variadic_templates ();
20595
20596 /* Note that this is an expansion. */
20597 pack_expansion_p = true;
20598 }
20599
20600 /* Do not actually evaluate the expression. */
20601 ++cp_unevaluated_operand;
20602 ++c_inhibit_evaluation_warnings;
20603 /* If it's a `(', then we might be looking at the type-id
20604 construction. */
20605 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20606 {
20607 tree type;
20608 bool saved_in_type_id_in_expr_p;
20609
20610 /* We can't be sure yet whether we're looking at a type-id or an
20611 expression. */
20612 cp_parser_parse_tentatively (parser);
20613 /* Consume the `('. */
20614 cp_lexer_consume_token (parser->lexer);
20615 /* Parse the type-id. */
20616 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20617 parser->in_type_id_in_expr_p = true;
20618 type = cp_parser_type_id (parser);
20619 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20620 /* Now, look for the trailing `)'. */
20621 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20622 /* If all went well, then we're done. */
20623 if (cp_parser_parse_definitely (parser))
20624 {
20625 cp_decl_specifier_seq decl_specs;
20626
20627 /* Build a trivial decl-specifier-seq. */
20628 clear_decl_specs (&decl_specs);
20629 decl_specs.type = type;
20630
20631 /* Call grokdeclarator to figure out what type this is. */
20632 expr = grokdeclarator (NULL,
20633 &decl_specs,
20634 TYPENAME,
20635 /*initialized=*/0,
20636 /*attrlist=*/NULL);
20637 }
20638 }
20639
20640 /* If the type-id production did not work out, then we must be
20641 looking at the unary-expression production. */
20642 if (!expr)
20643 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20644 /*cast_p=*/false, NULL);
20645
20646 if (pack_expansion_p)
20647 /* Build a pack expansion. */
20648 expr = make_pack_expansion (expr);
20649
20650 /* Go back to evaluating expressions. */
20651 --cp_unevaluated_operand;
20652 --c_inhibit_evaluation_warnings;
20653
20654 /* Free the message we created. */
20655 free (tmp);
20656 /* And restore the old one. */
20657 parser->type_definition_forbidden_message = saved_message;
20658 parser->integral_constant_expression_p
20659 = saved_integral_constant_expression_p;
20660 parser->non_integral_constant_expression_p
20661 = saved_non_integral_constant_expression_p;
20662
20663 return expr;
20664 }
20665
20666 /* If the current declaration has no declarator, return true. */
20667
20668 static bool
20669 cp_parser_declares_only_class_p (cp_parser *parser)
20670 {
20671 /* If the next token is a `;' or a `,' then there is no
20672 declarator. */
20673 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20674 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20675 }
20676
20677 /* Update the DECL_SPECS to reflect the storage class indicated by
20678 KEYWORD. */
20679
20680 static void
20681 cp_parser_set_storage_class (cp_parser *parser,
20682 cp_decl_specifier_seq *decl_specs,
20683 enum rid keyword,
20684 location_t location)
20685 {
20686 cp_storage_class storage_class;
20687
20688 if (parser->in_unbraced_linkage_specification_p)
20689 {
20690 error_at (location, "invalid use of %qD in linkage specification",
20691 ridpointers[keyword]);
20692 return;
20693 }
20694 else if (decl_specs->storage_class != sc_none)
20695 {
20696 decl_specs->conflicting_specifiers_p = true;
20697 return;
20698 }
20699
20700 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20701 && decl_specs->specs[(int) ds_thread])
20702 {
20703 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20704 decl_specs->specs[(int) ds_thread] = 0;
20705 }
20706
20707 switch (keyword)
20708 {
20709 case RID_AUTO:
20710 storage_class = sc_auto;
20711 break;
20712 case RID_REGISTER:
20713 storage_class = sc_register;
20714 break;
20715 case RID_STATIC:
20716 storage_class = sc_static;
20717 break;
20718 case RID_EXTERN:
20719 storage_class = sc_extern;
20720 break;
20721 case RID_MUTABLE:
20722 storage_class = sc_mutable;
20723 break;
20724 default:
20725 gcc_unreachable ();
20726 }
20727 decl_specs->storage_class = storage_class;
20728
20729 /* A storage class specifier cannot be applied alongside a typedef
20730 specifier. If there is a typedef specifier present then set
20731 conflicting_specifiers_p which will trigger an error later
20732 on in grokdeclarator. */
20733 if (decl_specs->specs[(int)ds_typedef])
20734 decl_specs->conflicting_specifiers_p = true;
20735 }
20736
20737 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20738 is true, the type is a user-defined type; otherwise it is a
20739 built-in type specified by a keyword. */
20740
20741 static void
20742 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20743 tree type_spec,
20744 location_t location,
20745 bool user_defined_p)
20746 {
20747 decl_specs->any_specifiers_p = true;
20748
20749 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20750 (with, for example, in "typedef int wchar_t;") we remember that
20751 this is what happened. In system headers, we ignore these
20752 declarations so that G++ can work with system headers that are not
20753 C++-safe. */
20754 if (decl_specs->specs[(int) ds_typedef]
20755 && !user_defined_p
20756 && (type_spec == boolean_type_node
20757 || type_spec == char16_type_node
20758 || type_spec == char32_type_node
20759 || type_spec == wchar_type_node)
20760 && (decl_specs->type
20761 || decl_specs->specs[(int) ds_long]
20762 || decl_specs->specs[(int) ds_short]
20763 || decl_specs->specs[(int) ds_unsigned]
20764 || decl_specs->specs[(int) ds_signed]))
20765 {
20766 decl_specs->redefined_builtin_type = type_spec;
20767 if (!decl_specs->type)
20768 {
20769 decl_specs->type = type_spec;
20770 decl_specs->user_defined_type_p = false;
20771 decl_specs->type_location = location;
20772 }
20773 }
20774 else if (decl_specs->type)
20775 decl_specs->multiple_types_p = true;
20776 else
20777 {
20778 decl_specs->type = type_spec;
20779 decl_specs->user_defined_type_p = user_defined_p;
20780 decl_specs->redefined_builtin_type = NULL_TREE;
20781 decl_specs->type_location = location;
20782 }
20783 }
20784
20785 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20786 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20787
20788 static bool
20789 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20790 {
20791 return decl_specifiers->specs[(int) ds_friend] != 0;
20792 }
20793
20794 /* Issue an error message indicating that TOKEN_DESC was expected.
20795 If KEYWORD is true, it indicated this function is called by
20796 cp_parser_require_keword and the required token can only be
20797 a indicated keyword. */
20798
20799 static void
20800 cp_parser_required_error (cp_parser *parser,
20801 required_token token_desc,
20802 bool keyword)
20803 {
20804 switch (token_desc)
20805 {
20806 case RT_NEW:
20807 cp_parser_error (parser, "expected %<new%>");
20808 return;
20809 case RT_DELETE:
20810 cp_parser_error (parser, "expected %<delete%>");
20811 return;
20812 case RT_RETURN:
20813 cp_parser_error (parser, "expected %<return%>");
20814 return;
20815 case RT_WHILE:
20816 cp_parser_error (parser, "expected %<while%>");
20817 return;
20818 case RT_EXTERN:
20819 cp_parser_error (parser, "expected %<extern%>");
20820 return;
20821 case RT_STATIC_ASSERT:
20822 cp_parser_error (parser, "expected %<static_assert%>");
20823 return;
20824 case RT_DECLTYPE:
20825 cp_parser_error (parser, "expected %<decltype%>");
20826 return;
20827 case RT_OPERATOR:
20828 cp_parser_error (parser, "expected %<operator%>");
20829 return;
20830 case RT_CLASS:
20831 cp_parser_error (parser, "expected %<class%>");
20832 return;
20833 case RT_TEMPLATE:
20834 cp_parser_error (parser, "expected %<template%>");
20835 return;
20836 case RT_NAMESPACE:
20837 cp_parser_error (parser, "expected %<namespace%>");
20838 return;
20839 case RT_USING:
20840 cp_parser_error (parser, "expected %<using%>");
20841 return;
20842 case RT_ASM:
20843 cp_parser_error (parser, "expected %<asm%>");
20844 return;
20845 case RT_TRY:
20846 cp_parser_error (parser, "expected %<try%>");
20847 return;
20848 case RT_CATCH:
20849 cp_parser_error (parser, "expected %<catch%>");
20850 return;
20851 case RT_THROW:
20852 cp_parser_error (parser, "expected %<throw%>");
20853 return;
20854 case RT_LABEL:
20855 cp_parser_error (parser, "expected %<__label__%>");
20856 return;
20857 case RT_AT_TRY:
20858 cp_parser_error (parser, "expected %<@try%>");
20859 return;
20860 case RT_AT_SYNCHRONIZED:
20861 cp_parser_error (parser, "expected %<@synchronized%>");
20862 return;
20863 case RT_AT_THROW:
20864 cp_parser_error (parser, "expected %<@throw%>");
20865 return;
20866 default:
20867 break;
20868 }
20869 if (!keyword)
20870 {
20871 switch (token_desc)
20872 {
20873 case RT_SEMICOLON:
20874 cp_parser_error (parser, "expected %<;%>");
20875 return;
20876 case RT_OPEN_PAREN:
20877 cp_parser_error (parser, "expected %<(%>");
20878 return;
20879 case RT_CLOSE_BRACE:
20880 cp_parser_error (parser, "expected %<}%>");
20881 return;
20882 case RT_OPEN_BRACE:
20883 cp_parser_error (parser, "expected %<{%>");
20884 return;
20885 case RT_CLOSE_SQUARE:
20886 cp_parser_error (parser, "expected %<]%>");
20887 return;
20888 case RT_OPEN_SQUARE:
20889 cp_parser_error (parser, "expected %<[%>");
20890 return;
20891 case RT_COMMA:
20892 cp_parser_error (parser, "expected %<,%>");
20893 return;
20894 case RT_SCOPE:
20895 cp_parser_error (parser, "expected %<::%>");
20896 return;
20897 case RT_LESS:
20898 cp_parser_error (parser, "expected %<<%>");
20899 return;
20900 case RT_GREATER:
20901 cp_parser_error (parser, "expected %<>%>");
20902 return;
20903 case RT_EQ:
20904 cp_parser_error (parser, "expected %<=%>");
20905 return;
20906 case RT_ELLIPSIS:
20907 cp_parser_error (parser, "expected %<...%>");
20908 return;
20909 case RT_MULT:
20910 cp_parser_error (parser, "expected %<*%>");
20911 return;
20912 case RT_COMPL:
20913 cp_parser_error (parser, "expected %<~%>");
20914 return;
20915 case RT_COLON:
20916 cp_parser_error (parser, "expected %<:%>");
20917 return;
20918 case RT_COLON_SCOPE:
20919 cp_parser_error (parser, "expected %<:%> or %<::%>");
20920 return;
20921 case RT_CLOSE_PAREN:
20922 cp_parser_error (parser, "expected %<)%>");
20923 return;
20924 case RT_COMMA_CLOSE_PAREN:
20925 cp_parser_error (parser, "expected %<,%> or %<)%>");
20926 return;
20927 case RT_PRAGMA_EOL:
20928 cp_parser_error (parser, "expected end of line");
20929 return;
20930 case RT_NAME:
20931 cp_parser_error (parser, "expected identifier");
20932 return;
20933 case RT_SELECT:
20934 cp_parser_error (parser, "expected selection-statement");
20935 return;
20936 case RT_INTERATION:
20937 cp_parser_error (parser, "expected iteration-statement");
20938 return;
20939 case RT_JUMP:
20940 cp_parser_error (parser, "expected jump-statement");
20941 return;
20942 case RT_CLASS_KEY:
20943 cp_parser_error (parser, "expected class-key");
20944 return;
20945 case RT_CLASS_TYPENAME_TEMPLATE:
20946 cp_parser_error (parser,
20947 "expected %<class%>, %<typename%>, or %<template%>");
20948 return;
20949 default:
20950 gcc_unreachable ();
20951 }
20952 }
20953 else
20954 gcc_unreachable ();
20955 }
20956
20957
20958
20959 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20960 issue an error message indicating that TOKEN_DESC was expected.
20961
20962 Returns the token consumed, if the token had the appropriate type.
20963 Otherwise, returns NULL. */
20964
20965 static cp_token *
20966 cp_parser_require (cp_parser* parser,
20967 enum cpp_ttype type,
20968 required_token token_desc)
20969 {
20970 if (cp_lexer_next_token_is (parser->lexer, type))
20971 return cp_lexer_consume_token (parser->lexer);
20972 else
20973 {
20974 /* Output the MESSAGE -- unless we're parsing tentatively. */
20975 if (!cp_parser_simulate_error (parser))
20976 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20977 return NULL;
20978 }
20979 }
20980
20981 /* An error message is produced if the next token is not '>'.
20982 All further tokens are skipped until the desired token is
20983 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20984
20985 static void
20986 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20987 {
20988 /* Current level of '< ... >'. */
20989 unsigned level = 0;
20990 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20991 unsigned nesting_depth = 0;
20992
20993 /* Are we ready, yet? If not, issue error message. */
20994 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20995 return;
20996
20997 /* Skip tokens until the desired token is found. */
20998 while (true)
20999 {
21000 /* Peek at the next token. */
21001 switch (cp_lexer_peek_token (parser->lexer)->type)
21002 {
21003 case CPP_LESS:
21004 if (!nesting_depth)
21005 ++level;
21006 break;
21007
21008 case CPP_RSHIFT:
21009 if (cxx_dialect == cxx98)
21010 /* C++0x views the `>>' operator as two `>' tokens, but
21011 C++98 does not. */
21012 break;
21013 else if (!nesting_depth && level-- == 0)
21014 {
21015 /* We've hit a `>>' where the first `>' closes the
21016 template argument list, and the second `>' is
21017 spurious. Just consume the `>>' and stop; we've
21018 already produced at least one error. */
21019 cp_lexer_consume_token (parser->lexer);
21020 return;
21021 }
21022 /* Fall through for C++0x, so we handle the second `>' in
21023 the `>>'. */
21024
21025 case CPP_GREATER:
21026 if (!nesting_depth && level-- == 0)
21027 {
21028 /* We've reached the token we want, consume it and stop. */
21029 cp_lexer_consume_token (parser->lexer);
21030 return;
21031 }
21032 break;
21033
21034 case CPP_OPEN_PAREN:
21035 case CPP_OPEN_SQUARE:
21036 ++nesting_depth;
21037 break;
21038
21039 case CPP_CLOSE_PAREN:
21040 case CPP_CLOSE_SQUARE:
21041 if (nesting_depth-- == 0)
21042 return;
21043 break;
21044
21045 case CPP_EOF:
21046 case CPP_PRAGMA_EOL:
21047 case CPP_SEMICOLON:
21048 case CPP_OPEN_BRACE:
21049 case CPP_CLOSE_BRACE:
21050 /* The '>' was probably forgotten, don't look further. */
21051 return;
21052
21053 default:
21054 break;
21055 }
21056
21057 /* Consume this token. */
21058 cp_lexer_consume_token (parser->lexer);
21059 }
21060 }
21061
21062 /* If the next token is the indicated keyword, consume it. Otherwise,
21063 issue an error message indicating that TOKEN_DESC was expected.
21064
21065 Returns the token consumed, if the token had the appropriate type.
21066 Otherwise, returns NULL. */
21067
21068 static cp_token *
21069 cp_parser_require_keyword (cp_parser* parser,
21070 enum rid keyword,
21071 required_token token_desc)
21072 {
21073 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21074
21075 if (token && token->keyword != keyword)
21076 {
21077 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
21078 return NULL;
21079 }
21080
21081 return token;
21082 }
21083
21084 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21085 function-definition. */
21086
21087 static bool
21088 cp_parser_token_starts_function_definition_p (cp_token* token)
21089 {
21090 return (/* An ordinary function-body begins with an `{'. */
21091 token->type == CPP_OPEN_BRACE
21092 /* A ctor-initializer begins with a `:'. */
21093 || token->type == CPP_COLON
21094 /* A function-try-block begins with `try'. */
21095 || token->keyword == RID_TRY
21096 /* The named return value extension begins with `return'. */
21097 || token->keyword == RID_RETURN);
21098 }
21099
21100 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21101 definition. */
21102
21103 static bool
21104 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21105 {
21106 cp_token *token;
21107
21108 token = cp_lexer_peek_token (parser->lexer);
21109 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21110 }
21111
21112 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21113 C++0x) ending a template-argument. */
21114
21115 static bool
21116 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21117 {
21118 cp_token *token;
21119
21120 token = cp_lexer_peek_token (parser->lexer);
21121 return (token->type == CPP_COMMA
21122 || token->type == CPP_GREATER
21123 || token->type == CPP_ELLIPSIS
21124 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21125 }
21126
21127 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21128 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21129
21130 static bool
21131 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21132 size_t n)
21133 {
21134 cp_token *token;
21135
21136 token = cp_lexer_peek_nth_token (parser->lexer, n);
21137 if (token->type == CPP_LESS)
21138 return true;
21139 /* Check for the sequence `<::' in the original code. It would be lexed as
21140 `[:', where `[' is a digraph, and there is no whitespace before
21141 `:'. */
21142 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21143 {
21144 cp_token *token2;
21145 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21146 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21147 return true;
21148 }
21149 return false;
21150 }
21151
21152 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21153 or none_type otherwise. */
21154
21155 static enum tag_types
21156 cp_parser_token_is_class_key (cp_token* token)
21157 {
21158 switch (token->keyword)
21159 {
21160 case RID_CLASS:
21161 return class_type;
21162 case RID_STRUCT:
21163 return record_type;
21164 case RID_UNION:
21165 return union_type;
21166
21167 default:
21168 return none_type;
21169 }
21170 }
21171
21172 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21173
21174 static void
21175 cp_parser_check_class_key (enum tag_types class_key, tree type)
21176 {
21177 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21178 permerror (input_location, "%qs tag used in naming %q#T",
21179 class_key == union_type ? "union"
21180 : class_key == record_type ? "struct" : "class",
21181 type);
21182 }
21183
21184 /* Issue an error message if DECL is redeclared with different
21185 access than its original declaration [class.access.spec/3].
21186 This applies to nested classes and nested class templates.
21187 [class.mem/1]. */
21188
21189 static void
21190 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21191 {
21192 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21193 return;
21194
21195 if ((TREE_PRIVATE (decl)
21196 != (current_access_specifier == access_private_node))
21197 || (TREE_PROTECTED (decl)
21198 != (current_access_specifier == access_protected_node)))
21199 error_at (location, "%qD redeclared with different access", decl);
21200 }
21201
21202 /* Look for the `template' keyword, as a syntactic disambiguator.
21203 Return TRUE iff it is present, in which case it will be
21204 consumed. */
21205
21206 static bool
21207 cp_parser_optional_template_keyword (cp_parser *parser)
21208 {
21209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21210 {
21211 /* The `template' keyword can only be used within templates;
21212 outside templates the parser can always figure out what is a
21213 template and what is not. */
21214 if (!processing_template_decl)
21215 {
21216 cp_token *token = cp_lexer_peek_token (parser->lexer);
21217 error_at (token->location,
21218 "%<template%> (as a disambiguator) is only allowed "
21219 "within templates");
21220 /* If this part of the token stream is rescanned, the same
21221 error message would be generated. So, we purge the token
21222 from the stream. */
21223 cp_lexer_purge_token (parser->lexer);
21224 return false;
21225 }
21226 else
21227 {
21228 /* Consume the `template' keyword. */
21229 cp_lexer_consume_token (parser->lexer);
21230 return true;
21231 }
21232 }
21233
21234 return false;
21235 }
21236
21237 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21238 set PARSER->SCOPE, and perform other related actions. */
21239
21240 static void
21241 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21242 {
21243 int i;
21244 struct tree_check *check_value;
21245 deferred_access_check *chk;
21246 VEC (deferred_access_check,gc) *checks;
21247
21248 /* Get the stored value. */
21249 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21250 /* Perform any access checks that were deferred. */
21251 checks = check_value->checks;
21252 if (checks)
21253 {
21254 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21255 perform_or_defer_access_check (chk->binfo,
21256 chk->decl,
21257 chk->diag_decl);
21258 }
21259 /* Set the scope from the stored value. */
21260 parser->scope = check_value->value;
21261 parser->qualifying_scope = check_value->qualifying_scope;
21262 parser->object_scope = NULL_TREE;
21263 }
21264
21265 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21266 encounter the end of a block before what we were looking for. */
21267
21268 static bool
21269 cp_parser_cache_group (cp_parser *parser,
21270 enum cpp_ttype end,
21271 unsigned depth)
21272 {
21273 while (true)
21274 {
21275 cp_token *token = cp_lexer_peek_token (parser->lexer);
21276
21277 /* Abort a parenthesized expression if we encounter a semicolon. */
21278 if ((end == CPP_CLOSE_PAREN || depth == 0)
21279 && token->type == CPP_SEMICOLON)
21280 return true;
21281 /* If we've reached the end of the file, stop. */
21282 if (token->type == CPP_EOF
21283 || (end != CPP_PRAGMA_EOL
21284 && token->type == CPP_PRAGMA_EOL))
21285 return true;
21286 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21287 /* We've hit the end of an enclosing block, so there's been some
21288 kind of syntax error. */
21289 return true;
21290
21291 /* Consume the token. */
21292 cp_lexer_consume_token (parser->lexer);
21293 /* See if it starts a new group. */
21294 if (token->type == CPP_OPEN_BRACE)
21295 {
21296 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21297 /* In theory this should probably check end == '}', but
21298 cp_parser_save_member_function_body needs it to exit
21299 after either '}' or ')' when called with ')'. */
21300 if (depth == 0)
21301 return false;
21302 }
21303 else if (token->type == CPP_OPEN_PAREN)
21304 {
21305 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21306 if (depth == 0 && end == CPP_CLOSE_PAREN)
21307 return false;
21308 }
21309 else if (token->type == CPP_PRAGMA)
21310 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21311 else if (token->type == end)
21312 return false;
21313 }
21314 }
21315
21316 /* Begin parsing tentatively. We always save tokens while parsing
21317 tentatively so that if the tentative parsing fails we can restore the
21318 tokens. */
21319
21320 static void
21321 cp_parser_parse_tentatively (cp_parser* parser)
21322 {
21323 /* Enter a new parsing context. */
21324 parser->context = cp_parser_context_new (parser->context);
21325 /* Begin saving tokens. */
21326 cp_lexer_save_tokens (parser->lexer);
21327 /* In order to avoid repetitive access control error messages,
21328 access checks are queued up until we are no longer parsing
21329 tentatively. */
21330 push_deferring_access_checks (dk_deferred);
21331 }
21332
21333 /* Commit to the currently active tentative parse. */
21334
21335 static void
21336 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21337 {
21338 cp_parser_context *context;
21339 cp_lexer *lexer;
21340
21341 /* Mark all of the levels as committed. */
21342 lexer = parser->lexer;
21343 for (context = parser->context; context->next; context = context->next)
21344 {
21345 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21346 break;
21347 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21348 while (!cp_lexer_saving_tokens (lexer))
21349 lexer = lexer->next;
21350 cp_lexer_commit_tokens (lexer);
21351 }
21352 }
21353
21354 /* Abort the currently active tentative parse. All consumed tokens
21355 will be rolled back, and no diagnostics will be issued. */
21356
21357 static void
21358 cp_parser_abort_tentative_parse (cp_parser* parser)
21359 {
21360 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21361 || errorcount > 0);
21362 cp_parser_simulate_error (parser);
21363 /* Now, pretend that we want to see if the construct was
21364 successfully parsed. */
21365 cp_parser_parse_definitely (parser);
21366 }
21367
21368 /* Stop parsing tentatively. If a parse error has occurred, restore the
21369 token stream. Otherwise, commit to the tokens we have consumed.
21370 Returns true if no error occurred; false otherwise. */
21371
21372 static bool
21373 cp_parser_parse_definitely (cp_parser* parser)
21374 {
21375 bool error_occurred;
21376 cp_parser_context *context;
21377
21378 /* Remember whether or not an error occurred, since we are about to
21379 destroy that information. */
21380 error_occurred = cp_parser_error_occurred (parser);
21381 /* Remove the topmost context from the stack. */
21382 context = parser->context;
21383 parser->context = context->next;
21384 /* If no parse errors occurred, commit to the tentative parse. */
21385 if (!error_occurred)
21386 {
21387 /* Commit to the tokens read tentatively, unless that was
21388 already done. */
21389 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21390 cp_lexer_commit_tokens (parser->lexer);
21391
21392 pop_to_parent_deferring_access_checks ();
21393 }
21394 /* Otherwise, if errors occurred, roll back our state so that things
21395 are just as they were before we began the tentative parse. */
21396 else
21397 {
21398 cp_lexer_rollback_tokens (parser->lexer);
21399 pop_deferring_access_checks ();
21400 }
21401 /* Add the context to the front of the free list. */
21402 context->next = cp_parser_context_free_list;
21403 cp_parser_context_free_list = context;
21404
21405 return !error_occurred;
21406 }
21407
21408 /* Returns true if we are parsing tentatively and are not committed to
21409 this tentative parse. */
21410
21411 static bool
21412 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21413 {
21414 return (cp_parser_parsing_tentatively (parser)
21415 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21416 }
21417
21418 /* Returns nonzero iff an error has occurred during the most recent
21419 tentative parse. */
21420
21421 static bool
21422 cp_parser_error_occurred (cp_parser* parser)
21423 {
21424 return (cp_parser_parsing_tentatively (parser)
21425 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21426 }
21427
21428 /* Returns nonzero if GNU extensions are allowed. */
21429
21430 static bool
21431 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21432 {
21433 return parser->allow_gnu_extensions_p;
21434 }
21435 \f
21436 /* Objective-C++ Productions */
21437
21438
21439 /* Parse an Objective-C expression, which feeds into a primary-expression
21440 above.
21441
21442 objc-expression:
21443 objc-message-expression
21444 objc-string-literal
21445 objc-encode-expression
21446 objc-protocol-expression
21447 objc-selector-expression
21448
21449 Returns a tree representation of the expression. */
21450
21451 static tree
21452 cp_parser_objc_expression (cp_parser* parser)
21453 {
21454 /* Try to figure out what kind of declaration is present. */
21455 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21456
21457 switch (kwd->type)
21458 {
21459 case CPP_OPEN_SQUARE:
21460 return cp_parser_objc_message_expression (parser);
21461
21462 case CPP_OBJC_STRING:
21463 kwd = cp_lexer_consume_token (parser->lexer);
21464 return objc_build_string_object (kwd->u.value);
21465
21466 case CPP_KEYWORD:
21467 switch (kwd->keyword)
21468 {
21469 case RID_AT_ENCODE:
21470 return cp_parser_objc_encode_expression (parser);
21471
21472 case RID_AT_PROTOCOL:
21473 return cp_parser_objc_protocol_expression (parser);
21474
21475 case RID_AT_SELECTOR:
21476 return cp_parser_objc_selector_expression (parser);
21477
21478 default:
21479 break;
21480 }
21481 default:
21482 error_at (kwd->location,
21483 "misplaced %<@%D%> Objective-C++ construct",
21484 kwd->u.value);
21485 cp_parser_skip_to_end_of_block_or_statement (parser);
21486 }
21487
21488 return error_mark_node;
21489 }
21490
21491 /* Parse an Objective-C message expression.
21492
21493 objc-message-expression:
21494 [ objc-message-receiver objc-message-args ]
21495
21496 Returns a representation of an Objective-C message. */
21497
21498 static tree
21499 cp_parser_objc_message_expression (cp_parser* parser)
21500 {
21501 tree receiver, messageargs;
21502
21503 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21504 receiver = cp_parser_objc_message_receiver (parser);
21505 messageargs = cp_parser_objc_message_args (parser);
21506 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21507
21508 return objc_build_message_expr (receiver, messageargs);
21509 }
21510
21511 /* Parse an objc-message-receiver.
21512
21513 objc-message-receiver:
21514 expression
21515 simple-type-specifier
21516
21517 Returns a representation of the type or expression. */
21518
21519 static tree
21520 cp_parser_objc_message_receiver (cp_parser* parser)
21521 {
21522 tree rcv;
21523
21524 /* An Objective-C message receiver may be either (1) a type
21525 or (2) an expression. */
21526 cp_parser_parse_tentatively (parser);
21527 rcv = cp_parser_expression (parser, false, NULL);
21528
21529 if (cp_parser_parse_definitely (parser))
21530 return rcv;
21531
21532 rcv = cp_parser_simple_type_specifier (parser,
21533 /*decl_specs=*/NULL,
21534 CP_PARSER_FLAGS_NONE);
21535
21536 return objc_get_class_reference (rcv);
21537 }
21538
21539 /* Parse the arguments and selectors comprising an Objective-C message.
21540
21541 objc-message-args:
21542 objc-selector
21543 objc-selector-args
21544 objc-selector-args , objc-comma-args
21545
21546 objc-selector-args:
21547 objc-selector [opt] : assignment-expression
21548 objc-selector-args objc-selector [opt] : assignment-expression
21549
21550 objc-comma-args:
21551 assignment-expression
21552 objc-comma-args , assignment-expression
21553
21554 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21555 selector arguments and TREE_VALUE containing a list of comma
21556 arguments. */
21557
21558 static tree
21559 cp_parser_objc_message_args (cp_parser* parser)
21560 {
21561 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21562 bool maybe_unary_selector_p = true;
21563 cp_token *token = cp_lexer_peek_token (parser->lexer);
21564
21565 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21566 {
21567 tree selector = NULL_TREE, arg;
21568
21569 if (token->type != CPP_COLON)
21570 selector = cp_parser_objc_selector (parser);
21571
21572 /* Detect if we have a unary selector. */
21573 if (maybe_unary_selector_p
21574 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21575 return build_tree_list (selector, NULL_TREE);
21576
21577 maybe_unary_selector_p = false;
21578 cp_parser_require (parser, CPP_COLON, RT_COLON);
21579 arg = cp_parser_assignment_expression (parser, false, NULL);
21580
21581 sel_args
21582 = chainon (sel_args,
21583 build_tree_list (selector, arg));
21584
21585 token = cp_lexer_peek_token (parser->lexer);
21586 }
21587
21588 /* Handle non-selector arguments, if any. */
21589 while (token->type == CPP_COMMA)
21590 {
21591 tree arg;
21592
21593 cp_lexer_consume_token (parser->lexer);
21594 arg = cp_parser_assignment_expression (parser, false, NULL);
21595
21596 addl_args
21597 = chainon (addl_args,
21598 build_tree_list (NULL_TREE, arg));
21599
21600 token = cp_lexer_peek_token (parser->lexer);
21601 }
21602
21603 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21604 {
21605 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21606 return build_tree_list (error_mark_node, error_mark_node);
21607 }
21608
21609 return build_tree_list (sel_args, addl_args);
21610 }
21611
21612 /* Parse an Objective-C encode expression.
21613
21614 objc-encode-expression:
21615 @encode objc-typename
21616
21617 Returns an encoded representation of the type argument. */
21618
21619 static tree
21620 cp_parser_objc_encode_expression (cp_parser* parser)
21621 {
21622 tree type;
21623 cp_token *token;
21624
21625 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21626 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21627 token = cp_lexer_peek_token (parser->lexer);
21628 type = complete_type (cp_parser_type_id (parser));
21629 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21630
21631 if (!type)
21632 {
21633 error_at (token->location,
21634 "%<@encode%> must specify a type as an argument");
21635 return error_mark_node;
21636 }
21637
21638 /* This happens if we find @encode(T) (where T is a template
21639 typename or something dependent on a template typename) when
21640 parsing a template. In that case, we can't compile it
21641 immediately, but we rather create an AT_ENCODE_EXPR which will
21642 need to be instantiated when the template is used.
21643 */
21644 if (dependent_type_p (type))
21645 {
21646 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21647 TREE_READONLY (value) = 1;
21648 return value;
21649 }
21650
21651 return objc_build_encode_expr (type);
21652 }
21653
21654 /* Parse an Objective-C @defs expression. */
21655
21656 static tree
21657 cp_parser_objc_defs_expression (cp_parser *parser)
21658 {
21659 tree name;
21660
21661 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21662 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21663 name = cp_parser_identifier (parser);
21664 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21665
21666 return objc_get_class_ivars (name);
21667 }
21668
21669 /* Parse an Objective-C protocol expression.
21670
21671 objc-protocol-expression:
21672 @protocol ( identifier )
21673
21674 Returns a representation of the protocol expression. */
21675
21676 static tree
21677 cp_parser_objc_protocol_expression (cp_parser* parser)
21678 {
21679 tree proto;
21680
21681 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21682 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21683 proto = cp_parser_identifier (parser);
21684 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21685
21686 return objc_build_protocol_expr (proto);
21687 }
21688
21689 /* Parse an Objective-C selector expression.
21690
21691 objc-selector-expression:
21692 @selector ( objc-method-signature )
21693
21694 objc-method-signature:
21695 objc-selector
21696 objc-selector-seq
21697
21698 objc-selector-seq:
21699 objc-selector :
21700 objc-selector-seq objc-selector :
21701
21702 Returns a representation of the method selector. */
21703
21704 static tree
21705 cp_parser_objc_selector_expression (cp_parser* parser)
21706 {
21707 tree sel_seq = NULL_TREE;
21708 bool maybe_unary_selector_p = true;
21709 cp_token *token;
21710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21711
21712 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21713 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21714 token = cp_lexer_peek_token (parser->lexer);
21715
21716 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21717 || token->type == CPP_SCOPE)
21718 {
21719 tree selector = NULL_TREE;
21720
21721 if (token->type != CPP_COLON
21722 || token->type == CPP_SCOPE)
21723 selector = cp_parser_objc_selector (parser);
21724
21725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21726 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21727 {
21728 /* Detect if we have a unary selector. */
21729 if (maybe_unary_selector_p)
21730 {
21731 sel_seq = selector;
21732 goto finish_selector;
21733 }
21734 else
21735 {
21736 cp_parser_error (parser, "expected %<:%>");
21737 }
21738 }
21739 maybe_unary_selector_p = false;
21740 token = cp_lexer_consume_token (parser->lexer);
21741
21742 if (token->type == CPP_SCOPE)
21743 {
21744 sel_seq
21745 = chainon (sel_seq,
21746 build_tree_list (selector, NULL_TREE));
21747 sel_seq
21748 = chainon (sel_seq,
21749 build_tree_list (NULL_TREE, NULL_TREE));
21750 }
21751 else
21752 sel_seq
21753 = chainon (sel_seq,
21754 build_tree_list (selector, NULL_TREE));
21755
21756 token = cp_lexer_peek_token (parser->lexer);
21757 }
21758
21759 finish_selector:
21760 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21761
21762 return objc_build_selector_expr (loc, sel_seq);
21763 }
21764
21765 /* Parse a list of identifiers.
21766
21767 objc-identifier-list:
21768 identifier
21769 objc-identifier-list , identifier
21770
21771 Returns a TREE_LIST of identifier nodes. */
21772
21773 static tree
21774 cp_parser_objc_identifier_list (cp_parser* parser)
21775 {
21776 tree identifier;
21777 tree list;
21778 cp_token *sep;
21779
21780 identifier = cp_parser_identifier (parser);
21781 if (identifier == error_mark_node)
21782 return error_mark_node;
21783
21784 list = build_tree_list (NULL_TREE, identifier);
21785 sep = cp_lexer_peek_token (parser->lexer);
21786
21787 while (sep->type == CPP_COMMA)
21788 {
21789 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21790 identifier = cp_parser_identifier (parser);
21791 if (identifier == error_mark_node)
21792 return list;
21793
21794 list = chainon (list, build_tree_list (NULL_TREE,
21795 identifier));
21796 sep = cp_lexer_peek_token (parser->lexer);
21797 }
21798
21799 return list;
21800 }
21801
21802 /* Parse an Objective-C alias declaration.
21803
21804 objc-alias-declaration:
21805 @compatibility_alias identifier identifier ;
21806
21807 This function registers the alias mapping with the Objective-C front end.
21808 It returns nothing. */
21809
21810 static void
21811 cp_parser_objc_alias_declaration (cp_parser* parser)
21812 {
21813 tree alias, orig;
21814
21815 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21816 alias = cp_parser_identifier (parser);
21817 orig = cp_parser_identifier (parser);
21818 objc_declare_alias (alias, orig);
21819 cp_parser_consume_semicolon_at_end_of_statement (parser);
21820 }
21821
21822 /* Parse an Objective-C class forward-declaration.
21823
21824 objc-class-declaration:
21825 @class objc-identifier-list ;
21826
21827 The function registers the forward declarations with the Objective-C
21828 front end. It returns nothing. */
21829
21830 static void
21831 cp_parser_objc_class_declaration (cp_parser* parser)
21832 {
21833 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21834 while (true)
21835 {
21836 tree id;
21837
21838 id = cp_parser_identifier (parser);
21839 if (id == error_mark_node)
21840 break;
21841
21842 objc_declare_class (id);
21843
21844 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21845 cp_lexer_consume_token (parser->lexer);
21846 else
21847 break;
21848 }
21849 cp_parser_consume_semicolon_at_end_of_statement (parser);
21850 }
21851
21852 /* Parse a list of Objective-C protocol references.
21853
21854 objc-protocol-refs-opt:
21855 objc-protocol-refs [opt]
21856
21857 objc-protocol-refs:
21858 < objc-identifier-list >
21859
21860 Returns a TREE_LIST of identifiers, if any. */
21861
21862 static tree
21863 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21864 {
21865 tree protorefs = NULL_TREE;
21866
21867 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21868 {
21869 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21870 protorefs = cp_parser_objc_identifier_list (parser);
21871 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21872 }
21873
21874 return protorefs;
21875 }
21876
21877 /* Parse a Objective-C visibility specification. */
21878
21879 static void
21880 cp_parser_objc_visibility_spec (cp_parser* parser)
21881 {
21882 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21883
21884 switch (vis->keyword)
21885 {
21886 case RID_AT_PRIVATE:
21887 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21888 break;
21889 case RID_AT_PROTECTED:
21890 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21891 break;
21892 case RID_AT_PUBLIC:
21893 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21894 break;
21895 case RID_AT_PACKAGE:
21896 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21897 break;
21898 default:
21899 return;
21900 }
21901
21902 /* Eat '@private'/'@protected'/'@public'. */
21903 cp_lexer_consume_token (parser->lexer);
21904 }
21905
21906 /* Parse an Objective-C method type. Return 'true' if it is a class
21907 (+) method, and 'false' if it is an instance (-) method. */
21908
21909 static inline bool
21910 cp_parser_objc_method_type (cp_parser* parser)
21911 {
21912 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21913 return true;
21914 else
21915 return false;
21916 }
21917
21918 /* Parse an Objective-C protocol qualifier. */
21919
21920 static tree
21921 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21922 {
21923 tree quals = NULL_TREE, node;
21924 cp_token *token = cp_lexer_peek_token (parser->lexer);
21925
21926 node = token->u.value;
21927
21928 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21929 && (node == ridpointers [(int) RID_IN]
21930 || node == ridpointers [(int) RID_OUT]
21931 || node == ridpointers [(int) RID_INOUT]
21932 || node == ridpointers [(int) RID_BYCOPY]
21933 || node == ridpointers [(int) RID_BYREF]
21934 || node == ridpointers [(int) RID_ONEWAY]))
21935 {
21936 quals = tree_cons (NULL_TREE, node, quals);
21937 cp_lexer_consume_token (parser->lexer);
21938 token = cp_lexer_peek_token (parser->lexer);
21939 node = token->u.value;
21940 }
21941
21942 return quals;
21943 }
21944
21945 /* Parse an Objective-C typename. */
21946
21947 static tree
21948 cp_parser_objc_typename (cp_parser* parser)
21949 {
21950 tree type_name = NULL_TREE;
21951
21952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21953 {
21954 tree proto_quals, cp_type = NULL_TREE;
21955
21956 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21957 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21958
21959 /* An ObjC type name may consist of just protocol qualifiers, in which
21960 case the type shall default to 'id'. */
21961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21962 {
21963 cp_type = cp_parser_type_id (parser);
21964
21965 /* If the type could not be parsed, an error has already
21966 been produced. For error recovery, behave as if it had
21967 not been specified, which will use the default type
21968 'id'. */
21969 if (cp_type == error_mark_node)
21970 {
21971 cp_type = NULL_TREE;
21972 /* We need to skip to the closing parenthesis as
21973 cp_parser_type_id() does not seem to do it for
21974 us. */
21975 cp_parser_skip_to_closing_parenthesis (parser,
21976 /*recovering=*/true,
21977 /*or_comma=*/false,
21978 /*consume_paren=*/false);
21979 }
21980 }
21981
21982 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21983 type_name = build_tree_list (proto_quals, cp_type);
21984 }
21985
21986 return type_name;
21987 }
21988
21989 /* Check to see if TYPE refers to an Objective-C selector name. */
21990
21991 static bool
21992 cp_parser_objc_selector_p (enum cpp_ttype type)
21993 {
21994 return (type == CPP_NAME || type == CPP_KEYWORD
21995 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21996 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21997 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21998 || type == CPP_XOR || type == CPP_XOR_EQ);
21999 }
22000
22001 /* Parse an Objective-C selector. */
22002
22003 static tree
22004 cp_parser_objc_selector (cp_parser* parser)
22005 {
22006 cp_token *token = cp_lexer_consume_token (parser->lexer);
22007
22008 if (!cp_parser_objc_selector_p (token->type))
22009 {
22010 error_at (token->location, "invalid Objective-C++ selector name");
22011 return error_mark_node;
22012 }
22013
22014 /* C++ operator names are allowed to appear in ObjC selectors. */
22015 switch (token->type)
22016 {
22017 case CPP_AND_AND: return get_identifier ("and");
22018 case CPP_AND_EQ: return get_identifier ("and_eq");
22019 case CPP_AND: return get_identifier ("bitand");
22020 case CPP_OR: return get_identifier ("bitor");
22021 case CPP_COMPL: return get_identifier ("compl");
22022 case CPP_NOT: return get_identifier ("not");
22023 case CPP_NOT_EQ: return get_identifier ("not_eq");
22024 case CPP_OR_OR: return get_identifier ("or");
22025 case CPP_OR_EQ: return get_identifier ("or_eq");
22026 case CPP_XOR: return get_identifier ("xor");
22027 case CPP_XOR_EQ: return get_identifier ("xor_eq");
22028 default: return token->u.value;
22029 }
22030 }
22031
22032 /* Parse an Objective-C params list. */
22033
22034 static tree
22035 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22036 {
22037 tree params = NULL_TREE;
22038 bool maybe_unary_selector_p = true;
22039 cp_token *token = cp_lexer_peek_token (parser->lexer);
22040
22041 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22042 {
22043 tree selector = NULL_TREE, type_name, identifier;
22044 tree parm_attr = NULL_TREE;
22045
22046 if (token->keyword == RID_ATTRIBUTE)
22047 break;
22048
22049 if (token->type != CPP_COLON)
22050 selector = cp_parser_objc_selector (parser);
22051
22052 /* Detect if we have a unary selector. */
22053 if (maybe_unary_selector_p
22054 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22055 {
22056 params = selector; /* Might be followed by attributes. */
22057 break;
22058 }
22059
22060 maybe_unary_selector_p = false;
22061 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22062 {
22063 /* Something went quite wrong. There should be a colon
22064 here, but there is not. Stop parsing parameters. */
22065 break;
22066 }
22067 type_name = cp_parser_objc_typename (parser);
22068 /* New ObjC allows attributes on parameters too. */
22069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22070 parm_attr = cp_parser_attributes_opt (parser);
22071 identifier = cp_parser_identifier (parser);
22072
22073 params
22074 = chainon (params,
22075 objc_build_keyword_decl (selector,
22076 type_name,
22077 identifier,
22078 parm_attr));
22079
22080 token = cp_lexer_peek_token (parser->lexer);
22081 }
22082
22083 if (params == NULL_TREE)
22084 {
22085 cp_parser_error (parser, "objective-c++ method declaration is expected");
22086 return error_mark_node;
22087 }
22088
22089 /* We allow tail attributes for the method. */
22090 if (token->keyword == RID_ATTRIBUTE)
22091 {
22092 *attributes = cp_parser_attributes_opt (parser);
22093 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22094 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22095 return params;
22096 cp_parser_error (parser,
22097 "method attributes must be specified at the end");
22098 return error_mark_node;
22099 }
22100
22101 if (params == NULL_TREE)
22102 {
22103 cp_parser_error (parser, "objective-c++ method declaration is expected");
22104 return error_mark_node;
22105 }
22106 return params;
22107 }
22108
22109 /* Parse the non-keyword Objective-C params. */
22110
22111 static tree
22112 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
22113 tree* attributes)
22114 {
22115 tree params = make_node (TREE_LIST);
22116 cp_token *token = cp_lexer_peek_token (parser->lexer);
22117 *ellipsisp = false; /* Initially, assume no ellipsis. */
22118
22119 while (token->type == CPP_COMMA)
22120 {
22121 cp_parameter_declarator *parmdecl;
22122 tree parm;
22123
22124 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22125 token = cp_lexer_peek_token (parser->lexer);
22126
22127 if (token->type == CPP_ELLIPSIS)
22128 {
22129 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22130 *ellipsisp = true;
22131 token = cp_lexer_peek_token (parser->lexer);
22132 break;
22133 }
22134
22135 /* TODO: parse attributes for tail parameters. */
22136 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22137 parm = grokdeclarator (parmdecl->declarator,
22138 &parmdecl->decl_specifiers,
22139 PARM, /*initialized=*/0,
22140 /*attrlist=*/NULL);
22141
22142 chainon (params, build_tree_list (NULL_TREE, parm));
22143 token = cp_lexer_peek_token (parser->lexer);
22144 }
22145
22146 /* We allow tail attributes for the method. */
22147 if (token->keyword == RID_ATTRIBUTE)
22148 {
22149 if (*attributes == NULL_TREE)
22150 {
22151 *attributes = cp_parser_attributes_opt (parser);
22152 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22153 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22154 return params;
22155 }
22156 else
22157 /* We have an error, but parse the attributes, so that we can
22158 carry on. */
22159 *attributes = cp_parser_attributes_opt (parser);
22160
22161 cp_parser_error (parser,
22162 "method attributes must be specified at the end");
22163 return error_mark_node;
22164 }
22165
22166 return params;
22167 }
22168
22169 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22170
22171 static void
22172 cp_parser_objc_interstitial_code (cp_parser* parser)
22173 {
22174 cp_token *token = cp_lexer_peek_token (parser->lexer);
22175
22176 /* If the next token is `extern' and the following token is a string
22177 literal, then we have a linkage specification. */
22178 if (token->keyword == RID_EXTERN
22179 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22180 cp_parser_linkage_specification (parser);
22181 /* Handle #pragma, if any. */
22182 else if (token->type == CPP_PRAGMA)
22183 cp_parser_pragma (parser, pragma_external);
22184 /* Allow stray semicolons. */
22185 else if (token->type == CPP_SEMICOLON)
22186 cp_lexer_consume_token (parser->lexer);
22187 /* Mark methods as optional or required, when building protocols. */
22188 else if (token->keyword == RID_AT_OPTIONAL)
22189 {
22190 cp_lexer_consume_token (parser->lexer);
22191 objc_set_method_opt (true);
22192 }
22193 else if (token->keyword == RID_AT_REQUIRED)
22194 {
22195 cp_lexer_consume_token (parser->lexer);
22196 objc_set_method_opt (false);
22197 }
22198 else if (token->keyword == RID_NAMESPACE)
22199 cp_parser_namespace_definition (parser);
22200 /* Other stray characters must generate errors. */
22201 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22202 {
22203 cp_lexer_consume_token (parser->lexer);
22204 error ("stray %qs between Objective-C++ methods",
22205 token->type == CPP_OPEN_BRACE ? "{" : "}");
22206 }
22207 /* Finally, try to parse a block-declaration, or a function-definition. */
22208 else
22209 cp_parser_block_declaration (parser, /*statement_p=*/false);
22210 }
22211
22212 /* Parse a method signature. */
22213
22214 static tree
22215 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22216 {
22217 tree rettype, kwdparms, optparms;
22218 bool ellipsis = false;
22219 bool is_class_method;
22220
22221 is_class_method = cp_parser_objc_method_type (parser);
22222 rettype = cp_parser_objc_typename (parser);
22223 *attributes = NULL_TREE;
22224 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22225 if (kwdparms == error_mark_node)
22226 return error_mark_node;
22227 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22228 if (optparms == error_mark_node)
22229 return error_mark_node;
22230
22231 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22232 }
22233
22234 static bool
22235 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22236 {
22237 tree tattr;
22238 cp_lexer_save_tokens (parser->lexer);
22239 tattr = cp_parser_attributes_opt (parser);
22240 gcc_assert (tattr) ;
22241
22242 /* If the attributes are followed by a method introducer, this is not allowed.
22243 Dump the attributes and flag the situation. */
22244 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22245 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22246 return true;
22247
22248 /* Otherwise, the attributes introduce some interstitial code, possibly so
22249 rewind to allow that check. */
22250 cp_lexer_rollback_tokens (parser->lexer);
22251 return false;
22252 }
22253
22254 /* Parse an Objective-C method prototype list. */
22255
22256 static void
22257 cp_parser_objc_method_prototype_list (cp_parser* parser)
22258 {
22259 cp_token *token = cp_lexer_peek_token (parser->lexer);
22260
22261 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22262 {
22263 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22264 {
22265 tree attributes, sig;
22266 bool is_class_method;
22267 if (token->type == CPP_PLUS)
22268 is_class_method = true;
22269 else
22270 is_class_method = false;
22271 sig = cp_parser_objc_method_signature (parser, &attributes);
22272 if (sig == error_mark_node)
22273 {
22274 cp_parser_skip_to_end_of_block_or_statement (parser);
22275 token = cp_lexer_peek_token (parser->lexer);
22276 continue;
22277 }
22278 objc_add_method_declaration (is_class_method, sig, attributes);
22279 cp_parser_consume_semicolon_at_end_of_statement (parser);
22280 }
22281 else if (token->keyword == RID_AT_PROPERTY)
22282 cp_parser_objc_at_property_declaration (parser);
22283 else if (token->keyword == RID_ATTRIBUTE
22284 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22285 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22286 OPT_Wattributes,
22287 "prefix attributes are ignored for methods");
22288 else
22289 /* Allow for interspersed non-ObjC++ code. */
22290 cp_parser_objc_interstitial_code (parser);
22291
22292 token = cp_lexer_peek_token (parser->lexer);
22293 }
22294
22295 if (token->type != CPP_EOF)
22296 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22297 else
22298 cp_parser_error (parser, "expected %<@end%>");
22299
22300 objc_finish_interface ();
22301 }
22302
22303 /* Parse an Objective-C method definition list. */
22304
22305 static void
22306 cp_parser_objc_method_definition_list (cp_parser* parser)
22307 {
22308 cp_token *token = cp_lexer_peek_token (parser->lexer);
22309
22310 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22311 {
22312 tree meth;
22313
22314 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22315 {
22316 cp_token *ptk;
22317 tree sig, attribute;
22318 bool is_class_method;
22319 if (token->type == CPP_PLUS)
22320 is_class_method = true;
22321 else
22322 is_class_method = false;
22323 push_deferring_access_checks (dk_deferred);
22324 sig = cp_parser_objc_method_signature (parser, &attribute);
22325 if (sig == error_mark_node)
22326 {
22327 cp_parser_skip_to_end_of_block_or_statement (parser);
22328 token = cp_lexer_peek_token (parser->lexer);
22329 continue;
22330 }
22331 objc_start_method_definition (is_class_method, sig, attribute,
22332 NULL_TREE);
22333
22334 /* For historical reasons, we accept an optional semicolon. */
22335 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22336 cp_lexer_consume_token (parser->lexer);
22337
22338 ptk = cp_lexer_peek_token (parser->lexer);
22339 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22340 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22341 {
22342 perform_deferred_access_checks ();
22343 stop_deferring_access_checks ();
22344 meth = cp_parser_function_definition_after_declarator (parser,
22345 false);
22346 pop_deferring_access_checks ();
22347 objc_finish_method_definition (meth);
22348 }
22349 }
22350 /* The following case will be removed once @synthesize is
22351 completely implemented. */
22352 else if (token->keyword == RID_AT_PROPERTY)
22353 cp_parser_objc_at_property_declaration (parser);
22354 else if (token->keyword == RID_AT_SYNTHESIZE)
22355 cp_parser_objc_at_synthesize_declaration (parser);
22356 else if (token->keyword == RID_AT_DYNAMIC)
22357 cp_parser_objc_at_dynamic_declaration (parser);
22358 else if (token->keyword == RID_ATTRIBUTE
22359 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22360 warning_at (token->location, OPT_Wattributes,
22361 "prefix attributes are ignored for methods");
22362 else
22363 /* Allow for interspersed non-ObjC++ code. */
22364 cp_parser_objc_interstitial_code (parser);
22365
22366 token = cp_lexer_peek_token (parser->lexer);
22367 }
22368
22369 if (token->type != CPP_EOF)
22370 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22371 else
22372 cp_parser_error (parser, "expected %<@end%>");
22373
22374 objc_finish_implementation ();
22375 }
22376
22377 /* Parse Objective-C ivars. */
22378
22379 static void
22380 cp_parser_objc_class_ivars (cp_parser* parser)
22381 {
22382 cp_token *token = cp_lexer_peek_token (parser->lexer);
22383
22384 if (token->type != CPP_OPEN_BRACE)
22385 return; /* No ivars specified. */
22386
22387 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22388 token = cp_lexer_peek_token (parser->lexer);
22389
22390 while (token->type != CPP_CLOSE_BRACE
22391 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22392 {
22393 cp_decl_specifier_seq declspecs;
22394 int decl_class_or_enum_p;
22395 tree prefix_attributes;
22396
22397 cp_parser_objc_visibility_spec (parser);
22398
22399 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22400 break;
22401
22402 cp_parser_decl_specifier_seq (parser,
22403 CP_PARSER_FLAGS_OPTIONAL,
22404 &declspecs,
22405 &decl_class_or_enum_p);
22406
22407 /* auto, register, static, extern, mutable. */
22408 if (declspecs.storage_class != sc_none)
22409 {
22410 cp_parser_error (parser, "invalid type for instance variable");
22411 declspecs.storage_class = sc_none;
22412 }
22413
22414 /* __thread. */
22415 if (declspecs.specs[(int) ds_thread])
22416 {
22417 cp_parser_error (parser, "invalid type for instance variable");
22418 declspecs.specs[(int) ds_thread] = 0;
22419 }
22420
22421 /* typedef. */
22422 if (declspecs.specs[(int) ds_typedef])
22423 {
22424 cp_parser_error (parser, "invalid type for instance variable");
22425 declspecs.specs[(int) ds_typedef] = 0;
22426 }
22427
22428 prefix_attributes = declspecs.attributes;
22429 declspecs.attributes = NULL_TREE;
22430
22431 /* Keep going until we hit the `;' at the end of the
22432 declaration. */
22433 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22434 {
22435 tree width = NULL_TREE, attributes, first_attribute, decl;
22436 cp_declarator *declarator = NULL;
22437 int ctor_dtor_or_conv_p;
22438
22439 /* Check for a (possibly unnamed) bitfield declaration. */
22440 token = cp_lexer_peek_token (parser->lexer);
22441 if (token->type == CPP_COLON)
22442 goto eat_colon;
22443
22444 if (token->type == CPP_NAME
22445 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22446 == CPP_COLON))
22447 {
22448 /* Get the name of the bitfield. */
22449 declarator = make_id_declarator (NULL_TREE,
22450 cp_parser_identifier (parser),
22451 sfk_none);
22452
22453 eat_colon:
22454 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22455 /* Get the width of the bitfield. */
22456 width
22457 = cp_parser_constant_expression (parser,
22458 /*allow_non_constant=*/false,
22459 NULL);
22460 }
22461 else
22462 {
22463 /* Parse the declarator. */
22464 declarator
22465 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22466 &ctor_dtor_or_conv_p,
22467 /*parenthesized_p=*/NULL,
22468 /*member_p=*/false);
22469 }
22470
22471 /* Look for attributes that apply to the ivar. */
22472 attributes = cp_parser_attributes_opt (parser);
22473 /* Remember which attributes are prefix attributes and
22474 which are not. */
22475 first_attribute = attributes;
22476 /* Combine the attributes. */
22477 attributes = chainon (prefix_attributes, attributes);
22478
22479 if (width)
22480 /* Create the bitfield declaration. */
22481 decl = grokbitfield (declarator, &declspecs,
22482 width,
22483 attributes);
22484 else
22485 decl = grokfield (declarator, &declspecs,
22486 NULL_TREE, /*init_const_expr_p=*/false,
22487 NULL_TREE, attributes);
22488
22489 /* Add the instance variable. */
22490 objc_add_instance_variable (decl);
22491
22492 /* Reset PREFIX_ATTRIBUTES. */
22493 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22494 attributes = TREE_CHAIN (attributes);
22495 if (attributes)
22496 TREE_CHAIN (attributes) = NULL_TREE;
22497
22498 token = cp_lexer_peek_token (parser->lexer);
22499
22500 if (token->type == CPP_COMMA)
22501 {
22502 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22503 continue;
22504 }
22505 break;
22506 }
22507
22508 cp_parser_consume_semicolon_at_end_of_statement (parser);
22509 token = cp_lexer_peek_token (parser->lexer);
22510 }
22511
22512 if (token->keyword == RID_AT_END)
22513 cp_parser_error (parser, "expected %<}%>");
22514
22515 /* Do not consume the RID_AT_END, so it will be read again as terminating
22516 the @interface of @implementation. */
22517 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22518 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22519
22520 /* For historical reasons, we accept an optional semicolon. */
22521 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22522 cp_lexer_consume_token (parser->lexer);
22523 }
22524
22525 /* Parse an Objective-C protocol declaration. */
22526
22527 static void
22528 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22529 {
22530 tree proto, protorefs;
22531 cp_token *tok;
22532
22533 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22535 {
22536 tok = cp_lexer_peek_token (parser->lexer);
22537 error_at (tok->location, "identifier expected after %<@protocol%>");
22538 cp_parser_consume_semicolon_at_end_of_statement (parser);
22539 return;
22540 }
22541
22542 /* See if we have a forward declaration or a definition. */
22543 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22544
22545 /* Try a forward declaration first. */
22546 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22547 {
22548 while (true)
22549 {
22550 tree id;
22551
22552 id = cp_parser_identifier (parser);
22553 if (id == error_mark_node)
22554 break;
22555
22556 objc_declare_protocol (id, attributes);
22557
22558 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22559 cp_lexer_consume_token (parser->lexer);
22560 else
22561 break;
22562 }
22563 cp_parser_consume_semicolon_at_end_of_statement (parser);
22564 }
22565
22566 /* Ok, we got a full-fledged definition (or at least should). */
22567 else
22568 {
22569 proto = cp_parser_identifier (parser);
22570 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22571 objc_start_protocol (proto, protorefs, attributes);
22572 cp_parser_objc_method_prototype_list (parser);
22573 }
22574 }
22575
22576 /* Parse an Objective-C superclass or category. */
22577
22578 static void
22579 cp_parser_objc_superclass_or_category (cp_parser *parser,
22580 bool iface_p,
22581 tree *super,
22582 tree *categ, bool *is_class_extension)
22583 {
22584 cp_token *next = cp_lexer_peek_token (parser->lexer);
22585
22586 *super = *categ = NULL_TREE;
22587 *is_class_extension = false;
22588 if (next->type == CPP_COLON)
22589 {
22590 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22591 *super = cp_parser_identifier (parser);
22592 }
22593 else if (next->type == CPP_OPEN_PAREN)
22594 {
22595 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22596
22597 /* If there is no category name, and this is an @interface, we
22598 have a class extension. */
22599 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22600 {
22601 *categ = NULL_TREE;
22602 *is_class_extension = true;
22603 }
22604 else
22605 *categ = cp_parser_identifier (parser);
22606
22607 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22608 }
22609 }
22610
22611 /* Parse an Objective-C class interface. */
22612
22613 static void
22614 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22615 {
22616 tree name, super, categ, protos;
22617 bool is_class_extension;
22618
22619 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22620 name = cp_parser_identifier (parser);
22621 if (name == error_mark_node)
22622 {
22623 /* It's hard to recover because even if valid @interface stuff
22624 is to follow, we can't compile it (or validate it) if we
22625 don't even know which class it refers to. Let's assume this
22626 was a stray '@interface' token in the stream and skip it.
22627 */
22628 return;
22629 }
22630 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22631 &is_class_extension);
22632 protos = cp_parser_objc_protocol_refs_opt (parser);
22633
22634 /* We have either a class or a category on our hands. */
22635 if (categ || is_class_extension)
22636 objc_start_category_interface (name, categ, protos, attributes);
22637 else
22638 {
22639 objc_start_class_interface (name, super, protos, attributes);
22640 /* Handle instance variable declarations, if any. */
22641 cp_parser_objc_class_ivars (parser);
22642 objc_continue_interface ();
22643 }
22644
22645 cp_parser_objc_method_prototype_list (parser);
22646 }
22647
22648 /* Parse an Objective-C class implementation. */
22649
22650 static void
22651 cp_parser_objc_class_implementation (cp_parser* parser)
22652 {
22653 tree name, super, categ;
22654 bool is_class_extension;
22655
22656 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22657 name = cp_parser_identifier (parser);
22658 if (name == error_mark_node)
22659 {
22660 /* It's hard to recover because even if valid @implementation
22661 stuff is to follow, we can't compile it (or validate it) if
22662 we don't even know which class it refers to. Let's assume
22663 this was a stray '@implementation' token in the stream and
22664 skip it.
22665 */
22666 return;
22667 }
22668 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22669 &is_class_extension);
22670
22671 /* We have either a class or a category on our hands. */
22672 if (categ)
22673 objc_start_category_implementation (name, categ);
22674 else
22675 {
22676 objc_start_class_implementation (name, super);
22677 /* Handle instance variable declarations, if any. */
22678 cp_parser_objc_class_ivars (parser);
22679 objc_continue_implementation ();
22680 }
22681
22682 cp_parser_objc_method_definition_list (parser);
22683 }
22684
22685 /* Consume the @end token and finish off the implementation. */
22686
22687 static void
22688 cp_parser_objc_end_implementation (cp_parser* parser)
22689 {
22690 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22691 objc_finish_implementation ();
22692 }
22693
22694 /* Parse an Objective-C declaration. */
22695
22696 static void
22697 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22698 {
22699 /* Try to figure out what kind of declaration is present. */
22700 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22701
22702 if (attributes)
22703 switch (kwd->keyword)
22704 {
22705 case RID_AT_ALIAS:
22706 case RID_AT_CLASS:
22707 case RID_AT_END:
22708 error_at (kwd->location, "attributes may not be specified before"
22709 " the %<@%D%> Objective-C++ keyword",
22710 kwd->u.value);
22711 attributes = NULL;
22712 break;
22713 case RID_AT_IMPLEMENTATION:
22714 warning_at (kwd->location, OPT_Wattributes,
22715 "prefix attributes are ignored before %<@%D%>",
22716 kwd->u.value);
22717 attributes = NULL;
22718 default:
22719 break;
22720 }
22721
22722 switch (kwd->keyword)
22723 {
22724 case RID_AT_ALIAS:
22725 cp_parser_objc_alias_declaration (parser);
22726 break;
22727 case RID_AT_CLASS:
22728 cp_parser_objc_class_declaration (parser);
22729 break;
22730 case RID_AT_PROTOCOL:
22731 cp_parser_objc_protocol_declaration (parser, attributes);
22732 break;
22733 case RID_AT_INTERFACE:
22734 cp_parser_objc_class_interface (parser, attributes);
22735 break;
22736 case RID_AT_IMPLEMENTATION:
22737 cp_parser_objc_class_implementation (parser);
22738 break;
22739 case RID_AT_END:
22740 cp_parser_objc_end_implementation (parser);
22741 break;
22742 default:
22743 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22744 kwd->u.value);
22745 cp_parser_skip_to_end_of_block_or_statement (parser);
22746 }
22747 }
22748
22749 /* Parse an Objective-C try-catch-finally statement.
22750
22751 objc-try-catch-finally-stmt:
22752 @try compound-statement objc-catch-clause-seq [opt]
22753 objc-finally-clause [opt]
22754
22755 objc-catch-clause-seq:
22756 objc-catch-clause objc-catch-clause-seq [opt]
22757
22758 objc-catch-clause:
22759 @catch ( objc-exception-declaration ) compound-statement
22760
22761 objc-finally-clause:
22762 @finally compound-statement
22763
22764 objc-exception-declaration:
22765 parameter-declaration
22766 '...'
22767
22768 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22769
22770 Returns NULL_TREE.
22771
22772 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22773 for C. Keep them in sync. */
22774
22775 static tree
22776 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22777 {
22778 location_t location;
22779 tree stmt;
22780
22781 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22782 location = cp_lexer_peek_token (parser->lexer)->location;
22783 objc_maybe_warn_exceptions (location);
22784 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22785 node, lest it get absorbed into the surrounding block. */
22786 stmt = push_stmt_list ();
22787 cp_parser_compound_statement (parser, NULL, false, false);
22788 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22789
22790 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22791 {
22792 cp_parameter_declarator *parm;
22793 tree parameter_declaration = error_mark_node;
22794 bool seen_open_paren = false;
22795
22796 cp_lexer_consume_token (parser->lexer);
22797 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22798 seen_open_paren = true;
22799 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22800 {
22801 /* We have "@catch (...)" (where the '...' are literally
22802 what is in the code). Skip the '...'.
22803 parameter_declaration is set to NULL_TREE, and
22804 objc_being_catch_clauses() knows that that means
22805 '...'. */
22806 cp_lexer_consume_token (parser->lexer);
22807 parameter_declaration = NULL_TREE;
22808 }
22809 else
22810 {
22811 /* We have "@catch (NSException *exception)" or something
22812 like that. Parse the parameter declaration. */
22813 parm = cp_parser_parameter_declaration (parser, false, NULL);
22814 if (parm == NULL)
22815 parameter_declaration = error_mark_node;
22816 else
22817 parameter_declaration = grokdeclarator (parm->declarator,
22818 &parm->decl_specifiers,
22819 PARM, /*initialized=*/0,
22820 /*attrlist=*/NULL);
22821 }
22822 if (seen_open_paren)
22823 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22824 else
22825 {
22826 /* If there was no open parenthesis, we are recovering from
22827 an error, and we are trying to figure out what mistake
22828 the user has made. */
22829
22830 /* If there is an immediate closing parenthesis, the user
22831 probably forgot the opening one (ie, they typed "@catch
22832 NSException *e)". Parse the closing parenthesis and keep
22833 going. */
22834 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22835 cp_lexer_consume_token (parser->lexer);
22836
22837 /* If these is no immediate closing parenthesis, the user
22838 probably doesn't know that parenthesis are required at
22839 all (ie, they typed "@catch NSException *e"). So, just
22840 forget about the closing parenthesis and keep going. */
22841 }
22842 objc_begin_catch_clause (parameter_declaration);
22843 cp_parser_compound_statement (parser, NULL, false, false);
22844 objc_finish_catch_clause ();
22845 }
22846 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22847 {
22848 cp_lexer_consume_token (parser->lexer);
22849 location = cp_lexer_peek_token (parser->lexer)->location;
22850 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22851 node, lest it get absorbed into the surrounding block. */
22852 stmt = push_stmt_list ();
22853 cp_parser_compound_statement (parser, NULL, false, false);
22854 objc_build_finally_clause (location, pop_stmt_list (stmt));
22855 }
22856
22857 return objc_finish_try_stmt ();
22858 }
22859
22860 /* Parse an Objective-C synchronized statement.
22861
22862 objc-synchronized-stmt:
22863 @synchronized ( expression ) compound-statement
22864
22865 Returns NULL_TREE. */
22866
22867 static tree
22868 cp_parser_objc_synchronized_statement (cp_parser *parser)
22869 {
22870 location_t location;
22871 tree lock, stmt;
22872
22873 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22874
22875 location = cp_lexer_peek_token (parser->lexer)->location;
22876 objc_maybe_warn_exceptions (location);
22877 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22878 lock = cp_parser_expression (parser, false, NULL);
22879 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22880
22881 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22882 node, lest it get absorbed into the surrounding block. */
22883 stmt = push_stmt_list ();
22884 cp_parser_compound_statement (parser, NULL, false, false);
22885
22886 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22887 }
22888
22889 /* Parse an Objective-C throw statement.
22890
22891 objc-throw-stmt:
22892 @throw assignment-expression [opt] ;
22893
22894 Returns a constructed '@throw' statement. */
22895
22896 static tree
22897 cp_parser_objc_throw_statement (cp_parser *parser)
22898 {
22899 tree expr = NULL_TREE;
22900 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22901
22902 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22903
22904 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22905 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22906
22907 cp_parser_consume_semicolon_at_end_of_statement (parser);
22908
22909 return objc_build_throw_stmt (loc, expr);
22910 }
22911
22912 /* Parse an Objective-C statement. */
22913
22914 static tree
22915 cp_parser_objc_statement (cp_parser * parser)
22916 {
22917 /* Try to figure out what kind of declaration is present. */
22918 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22919
22920 switch (kwd->keyword)
22921 {
22922 case RID_AT_TRY:
22923 return cp_parser_objc_try_catch_finally_statement (parser);
22924 case RID_AT_SYNCHRONIZED:
22925 return cp_parser_objc_synchronized_statement (parser);
22926 case RID_AT_THROW:
22927 return cp_parser_objc_throw_statement (parser);
22928 default:
22929 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22930 kwd->u.value);
22931 cp_parser_skip_to_end_of_block_or_statement (parser);
22932 }
22933
22934 return error_mark_node;
22935 }
22936
22937 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22938 look ahead to see if an objc keyword follows the attributes. This
22939 is to detect the use of prefix attributes on ObjC @interface and
22940 @protocol. */
22941
22942 static bool
22943 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22944 {
22945 cp_lexer_save_tokens (parser->lexer);
22946 *attrib = cp_parser_attributes_opt (parser);
22947 gcc_assert (*attrib);
22948 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22949 {
22950 cp_lexer_commit_tokens (parser->lexer);
22951 return true;
22952 }
22953 cp_lexer_rollback_tokens (parser->lexer);
22954 return false;
22955 }
22956
22957 /* This routine is a minimal replacement for
22958 c_parser_struct_declaration () used when parsing the list of
22959 types/names or ObjC++ properties. For example, when parsing the
22960 code
22961
22962 @property (readonly) int a, b, c;
22963
22964 this function is responsible for parsing "int a, int b, int c" and
22965 returning the declarations as CHAIN of DECLs.
22966
22967 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22968 similar parsing. */
22969 static tree
22970 cp_parser_objc_struct_declaration (cp_parser *parser)
22971 {
22972 tree decls = NULL_TREE;
22973 cp_decl_specifier_seq declspecs;
22974 int decl_class_or_enum_p;
22975 tree prefix_attributes;
22976
22977 cp_parser_decl_specifier_seq (parser,
22978 CP_PARSER_FLAGS_NONE,
22979 &declspecs,
22980 &decl_class_or_enum_p);
22981
22982 if (declspecs.type == error_mark_node)
22983 return error_mark_node;
22984
22985 /* auto, register, static, extern, mutable. */
22986 if (declspecs.storage_class != sc_none)
22987 {
22988 cp_parser_error (parser, "invalid type for property");
22989 declspecs.storage_class = sc_none;
22990 }
22991
22992 /* __thread. */
22993 if (declspecs.specs[(int) ds_thread])
22994 {
22995 cp_parser_error (parser, "invalid type for property");
22996 declspecs.specs[(int) ds_thread] = 0;
22997 }
22998
22999 /* typedef. */
23000 if (declspecs.specs[(int) ds_typedef])
23001 {
23002 cp_parser_error (parser, "invalid type for property");
23003 declspecs.specs[(int) ds_typedef] = 0;
23004 }
23005
23006 prefix_attributes = declspecs.attributes;
23007 declspecs.attributes = NULL_TREE;
23008
23009 /* Keep going until we hit the `;' at the end of the declaration. */
23010 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23011 {
23012 tree attributes, first_attribute, decl;
23013 cp_declarator *declarator;
23014 cp_token *token;
23015
23016 /* Parse the declarator. */
23017 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23018 NULL, NULL, false);
23019
23020 /* Look for attributes that apply to the ivar. */
23021 attributes = cp_parser_attributes_opt (parser);
23022 /* Remember which attributes are prefix attributes and
23023 which are not. */
23024 first_attribute = attributes;
23025 /* Combine the attributes. */
23026 attributes = chainon (prefix_attributes, attributes);
23027
23028 decl = grokfield (declarator, &declspecs,
23029 NULL_TREE, /*init_const_expr_p=*/false,
23030 NULL_TREE, attributes);
23031
23032 if (decl == error_mark_node || decl == NULL_TREE)
23033 return error_mark_node;
23034
23035 /* Reset PREFIX_ATTRIBUTES. */
23036 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23037 attributes = TREE_CHAIN (attributes);
23038 if (attributes)
23039 TREE_CHAIN (attributes) = NULL_TREE;
23040
23041 DECL_CHAIN (decl) = decls;
23042 decls = decl;
23043
23044 token = cp_lexer_peek_token (parser->lexer);
23045 if (token->type == CPP_COMMA)
23046 {
23047 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23048 continue;
23049 }
23050 else
23051 break;
23052 }
23053 return decls;
23054 }
23055
23056 /* Parse an Objective-C @property declaration. The syntax is:
23057
23058 objc-property-declaration:
23059 '@property' objc-property-attributes[opt] struct-declaration ;
23060
23061 objc-property-attributes:
23062 '(' objc-property-attribute-list ')'
23063
23064 objc-property-attribute-list:
23065 objc-property-attribute
23066 objc-property-attribute-list, objc-property-attribute
23067
23068 objc-property-attribute
23069 'getter' = identifier
23070 'setter' = identifier
23071 'readonly'
23072 'readwrite'
23073 'assign'
23074 'retain'
23075 'copy'
23076 'nonatomic'
23077
23078 For example:
23079 @property NSString *name;
23080 @property (readonly) id object;
23081 @property (retain, nonatomic, getter=getTheName) id name;
23082 @property int a, b, c;
23083
23084 PS: This function is identical to
23085 c_parser_objc_at_property_declaration for C. Keep them in sync. */
23086 static void
23087 cp_parser_objc_at_property_declaration (cp_parser *parser)
23088 {
23089 /* The following variables hold the attributes of the properties as
23090 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
23091 seen. When we see an attribute, we set them to 'true' (if they
23092 are boolean properties) or to the identifier (if they have an
23093 argument, ie, for getter and setter). Note that here we only
23094 parse the list of attributes, check the syntax and accumulate the
23095 attributes that we find. objc_add_property_declaration() will
23096 then process the information. */
23097 bool property_assign = false;
23098 bool property_copy = false;
23099 tree property_getter_ident = NULL_TREE;
23100 bool property_nonatomic = false;
23101 bool property_readonly = false;
23102 bool property_readwrite = false;
23103 bool property_retain = false;
23104 tree property_setter_ident = NULL_TREE;
23105
23106 /* 'properties' is the list of properties that we read. Usually a
23107 single one, but maybe more (eg, in "@property int a, b, c;" there
23108 are three). */
23109 tree properties;
23110 location_t loc;
23111
23112 loc = cp_lexer_peek_token (parser->lexer)->location;
23113
23114 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
23115
23116 /* Parse the optional attribute list... */
23117 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23118 {
23119 /* Eat the '('. */
23120 cp_lexer_consume_token (parser->lexer);
23121
23122 while (true)
23123 {
23124 bool syntax_error = false;
23125 cp_token *token = cp_lexer_peek_token (parser->lexer);
23126 enum rid keyword;
23127
23128 if (token->type != CPP_NAME)
23129 {
23130 cp_parser_error (parser, "expected identifier");
23131 break;
23132 }
23133 keyword = C_RID_CODE (token->u.value);
23134 cp_lexer_consume_token (parser->lexer);
23135 switch (keyword)
23136 {
23137 case RID_ASSIGN: property_assign = true; break;
23138 case RID_COPY: property_copy = true; break;
23139 case RID_NONATOMIC: property_nonatomic = true; break;
23140 case RID_READONLY: property_readonly = true; break;
23141 case RID_READWRITE: property_readwrite = true; break;
23142 case RID_RETAIN: property_retain = true; break;
23143
23144 case RID_GETTER:
23145 case RID_SETTER:
23146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23147 {
23148 if (keyword == RID_GETTER)
23149 cp_parser_error (parser,
23150 "missing %<=%> (after %<getter%> attribute)");
23151 else
23152 cp_parser_error (parser,
23153 "missing %<=%> (after %<setter%> attribute)");
23154 syntax_error = true;
23155 break;
23156 }
23157 cp_lexer_consume_token (parser->lexer); /* eat the = */
23158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23159 {
23160 cp_parser_error (parser, "expected identifier");
23161 syntax_error = true;
23162 break;
23163 }
23164 if (keyword == RID_SETTER)
23165 {
23166 if (property_setter_ident != NULL_TREE)
23167 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23168 else
23169 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23170 cp_lexer_consume_token (parser->lexer);
23171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23172 cp_parser_error (parser, "setter name must terminate with %<:%>");
23173 else
23174 cp_lexer_consume_token (parser->lexer);
23175 }
23176 else
23177 {
23178 if (property_getter_ident != NULL_TREE)
23179 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23180 else
23181 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23182 cp_lexer_consume_token (parser->lexer);
23183 }
23184 break;
23185 default:
23186 cp_parser_error (parser, "unknown property attribute");
23187 syntax_error = true;
23188 break;
23189 }
23190
23191 if (syntax_error)
23192 break;
23193
23194 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23195 cp_lexer_consume_token (parser->lexer);
23196 else
23197 break;
23198 }
23199
23200 /* FIXME: "@property (setter, assign);" will generate a spurious
23201 "error: expected ‘)’ before ‘,’ token". This is because
23202 cp_parser_require, unlike the C counterpart, will produce an
23203 error even if we are in error recovery. */
23204 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23205 {
23206 cp_parser_skip_to_closing_parenthesis (parser,
23207 /*recovering=*/true,
23208 /*or_comma=*/false,
23209 /*consume_paren=*/true);
23210 }
23211 }
23212
23213 /* ... and the property declaration(s). */
23214 properties = cp_parser_objc_struct_declaration (parser);
23215
23216 if (properties == error_mark_node)
23217 {
23218 cp_parser_skip_to_end_of_statement (parser);
23219 /* If the next token is now a `;', consume it. */
23220 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23221 cp_lexer_consume_token (parser->lexer);
23222 return;
23223 }
23224
23225 if (properties == NULL_TREE)
23226 cp_parser_error (parser, "expected identifier");
23227 else
23228 {
23229 /* Comma-separated properties are chained together in
23230 reverse order; add them one by one. */
23231 properties = nreverse (properties);
23232
23233 for (; properties; properties = TREE_CHAIN (properties))
23234 objc_add_property_declaration (loc, copy_node (properties),
23235 property_readonly, property_readwrite,
23236 property_assign, property_retain,
23237 property_copy, property_nonatomic,
23238 property_getter_ident, property_setter_ident);
23239 }
23240
23241 cp_parser_consume_semicolon_at_end_of_statement (parser);
23242 }
23243
23244 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23245
23246 objc-synthesize-declaration:
23247 @synthesize objc-synthesize-identifier-list ;
23248
23249 objc-synthesize-identifier-list:
23250 objc-synthesize-identifier
23251 objc-synthesize-identifier-list, objc-synthesize-identifier
23252
23253 objc-synthesize-identifier
23254 identifier
23255 identifier = identifier
23256
23257 For example:
23258 @synthesize MyProperty;
23259 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23260
23261 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23262 for C. Keep them in sync.
23263 */
23264 static void
23265 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23266 {
23267 tree list = NULL_TREE;
23268 location_t loc;
23269 loc = cp_lexer_peek_token (parser->lexer)->location;
23270
23271 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23272 while (true)
23273 {
23274 tree property, ivar;
23275 property = cp_parser_identifier (parser);
23276 if (property == error_mark_node)
23277 {
23278 cp_parser_consume_semicolon_at_end_of_statement (parser);
23279 return;
23280 }
23281 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23282 {
23283 cp_lexer_consume_token (parser->lexer);
23284 ivar = cp_parser_identifier (parser);
23285 if (ivar == error_mark_node)
23286 {
23287 cp_parser_consume_semicolon_at_end_of_statement (parser);
23288 return;
23289 }
23290 }
23291 else
23292 ivar = NULL_TREE;
23293 list = chainon (list, build_tree_list (ivar, property));
23294 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23295 cp_lexer_consume_token (parser->lexer);
23296 else
23297 break;
23298 }
23299 cp_parser_consume_semicolon_at_end_of_statement (parser);
23300 objc_add_synthesize_declaration (loc, list);
23301 }
23302
23303 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23304
23305 objc-dynamic-declaration:
23306 @dynamic identifier-list ;
23307
23308 For example:
23309 @dynamic MyProperty;
23310 @dynamic MyProperty, AnotherProperty;
23311
23312 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23313 for C. Keep them in sync.
23314 */
23315 static void
23316 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23317 {
23318 tree list = NULL_TREE;
23319 location_t loc;
23320 loc = cp_lexer_peek_token (parser->lexer)->location;
23321
23322 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23323 while (true)
23324 {
23325 tree property;
23326 property = cp_parser_identifier (parser);
23327 if (property == error_mark_node)
23328 {
23329 cp_parser_consume_semicolon_at_end_of_statement (parser);
23330 return;
23331 }
23332 list = chainon (list, build_tree_list (NULL, property));
23333 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23334 cp_lexer_consume_token (parser->lexer);
23335 else
23336 break;
23337 }
23338 cp_parser_consume_semicolon_at_end_of_statement (parser);
23339 objc_add_dynamic_declaration (loc, list);
23340 }
23341
23342 \f
23343 /* OpenMP 2.5 parsing routines. */
23344
23345 /* Returns name of the next clause.
23346 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23347 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23348 returned and the token is consumed. */
23349
23350 static pragma_omp_clause
23351 cp_parser_omp_clause_name (cp_parser *parser)
23352 {
23353 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23354
23355 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23356 result = PRAGMA_OMP_CLAUSE_IF;
23357 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23358 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23359 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23360 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23361 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23362 {
23363 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23364 const char *p = IDENTIFIER_POINTER (id);
23365
23366 switch (p[0])
23367 {
23368 case 'c':
23369 if (!strcmp ("collapse", p))
23370 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23371 else if (!strcmp ("copyin", p))
23372 result = PRAGMA_OMP_CLAUSE_COPYIN;
23373 else if (!strcmp ("copyprivate", p))
23374 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23375 break;
23376 case 'f':
23377 if (!strcmp ("firstprivate", p))
23378 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23379 break;
23380 case 'l':
23381 if (!strcmp ("lastprivate", p))
23382 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23383 break;
23384 case 'n':
23385 if (!strcmp ("nowait", p))
23386 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23387 else if (!strcmp ("num_threads", p))
23388 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23389 break;
23390 case 'o':
23391 if (!strcmp ("ordered", p))
23392 result = PRAGMA_OMP_CLAUSE_ORDERED;
23393 break;
23394 case 'r':
23395 if (!strcmp ("reduction", p))
23396 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23397 break;
23398 case 's':
23399 if (!strcmp ("schedule", p))
23400 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23401 else if (!strcmp ("shared", p))
23402 result = PRAGMA_OMP_CLAUSE_SHARED;
23403 break;
23404 case 'u':
23405 if (!strcmp ("untied", p))
23406 result = PRAGMA_OMP_CLAUSE_UNTIED;
23407 break;
23408 }
23409 }
23410
23411 if (result != PRAGMA_OMP_CLAUSE_NONE)
23412 cp_lexer_consume_token (parser->lexer);
23413
23414 return result;
23415 }
23416
23417 /* Validate that a clause of the given type does not already exist. */
23418
23419 static void
23420 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23421 const char *name, location_t location)
23422 {
23423 tree c;
23424
23425 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23426 if (OMP_CLAUSE_CODE (c) == code)
23427 {
23428 error_at (location, "too many %qs clauses", name);
23429 break;
23430 }
23431 }
23432
23433 /* OpenMP 2.5:
23434 variable-list:
23435 identifier
23436 variable-list , identifier
23437
23438 In addition, we match a closing parenthesis. An opening parenthesis
23439 will have been consumed by the caller.
23440
23441 If KIND is nonzero, create the appropriate node and install the decl
23442 in OMP_CLAUSE_DECL and add the node to the head of the list.
23443
23444 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23445 return the list created. */
23446
23447 static tree
23448 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23449 tree list)
23450 {
23451 cp_token *token;
23452 while (1)
23453 {
23454 tree name, decl;
23455
23456 token = cp_lexer_peek_token (parser->lexer);
23457 name = cp_parser_id_expression (parser, /*template_p=*/false,
23458 /*check_dependency_p=*/true,
23459 /*template_p=*/NULL,
23460 /*declarator_p=*/false,
23461 /*optional_p=*/false);
23462 if (name == error_mark_node)
23463 goto skip_comma;
23464
23465 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23466 if (decl == error_mark_node)
23467 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23468 token->location);
23469 else if (kind != 0)
23470 {
23471 tree u = build_omp_clause (token->location, kind);
23472 OMP_CLAUSE_DECL (u) = decl;
23473 OMP_CLAUSE_CHAIN (u) = list;
23474 list = u;
23475 }
23476 else
23477 list = tree_cons (decl, NULL_TREE, list);
23478
23479 get_comma:
23480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23481 break;
23482 cp_lexer_consume_token (parser->lexer);
23483 }
23484
23485 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23486 {
23487 int ending;
23488
23489 /* Try to resync to an unnested comma. Copied from
23490 cp_parser_parenthesized_expression_list. */
23491 skip_comma:
23492 ending = cp_parser_skip_to_closing_parenthesis (parser,
23493 /*recovering=*/true,
23494 /*or_comma=*/true,
23495 /*consume_paren=*/true);
23496 if (ending < 0)
23497 goto get_comma;
23498 }
23499
23500 return list;
23501 }
23502
23503 /* Similarly, but expect leading and trailing parenthesis. This is a very
23504 common case for omp clauses. */
23505
23506 static tree
23507 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23508 {
23509 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23510 return cp_parser_omp_var_list_no_open (parser, kind, list);
23511 return list;
23512 }
23513
23514 /* OpenMP 3.0:
23515 collapse ( constant-expression ) */
23516
23517 static tree
23518 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23519 {
23520 tree c, num;
23521 location_t loc;
23522 HOST_WIDE_INT n;
23523
23524 loc = cp_lexer_peek_token (parser->lexer)->location;
23525 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23526 return list;
23527
23528 num = cp_parser_constant_expression (parser, false, NULL);
23529
23530 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23532 /*or_comma=*/false,
23533 /*consume_paren=*/true);
23534
23535 if (num == error_mark_node)
23536 return list;
23537 num = fold_non_dependent_expr (num);
23538 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23539 || !host_integerp (num, 0)
23540 || (n = tree_low_cst (num, 0)) <= 0
23541 || (int) n != n)
23542 {
23543 error_at (loc, "collapse argument needs positive constant integer expression");
23544 return list;
23545 }
23546
23547 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23548 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23549 OMP_CLAUSE_CHAIN (c) = list;
23550 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23551
23552 return c;
23553 }
23554
23555 /* OpenMP 2.5:
23556 default ( shared | none ) */
23557
23558 static tree
23559 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23560 {
23561 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23562 tree c;
23563
23564 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23565 return list;
23566 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23567 {
23568 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23569 const char *p = IDENTIFIER_POINTER (id);
23570
23571 switch (p[0])
23572 {
23573 case 'n':
23574 if (strcmp ("none", p) != 0)
23575 goto invalid_kind;
23576 kind = OMP_CLAUSE_DEFAULT_NONE;
23577 break;
23578
23579 case 's':
23580 if (strcmp ("shared", p) != 0)
23581 goto invalid_kind;
23582 kind = OMP_CLAUSE_DEFAULT_SHARED;
23583 break;
23584
23585 default:
23586 goto invalid_kind;
23587 }
23588
23589 cp_lexer_consume_token (parser->lexer);
23590 }
23591 else
23592 {
23593 invalid_kind:
23594 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23595 }
23596
23597 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23598 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23599 /*or_comma=*/false,
23600 /*consume_paren=*/true);
23601
23602 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23603 return list;
23604
23605 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23606 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23607 OMP_CLAUSE_CHAIN (c) = list;
23608 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23609
23610 return c;
23611 }
23612
23613 /* OpenMP 2.5:
23614 if ( expression ) */
23615
23616 static tree
23617 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23618 {
23619 tree t, c;
23620
23621 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23622 return list;
23623
23624 t = cp_parser_condition (parser);
23625
23626 if (t == error_mark_node
23627 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23628 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23629 /*or_comma=*/false,
23630 /*consume_paren=*/true);
23631
23632 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23633
23634 c = build_omp_clause (location, OMP_CLAUSE_IF);
23635 OMP_CLAUSE_IF_EXPR (c) = t;
23636 OMP_CLAUSE_CHAIN (c) = list;
23637
23638 return c;
23639 }
23640
23641 /* OpenMP 2.5:
23642 nowait */
23643
23644 static tree
23645 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23646 tree list, location_t location)
23647 {
23648 tree c;
23649
23650 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23651
23652 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23653 OMP_CLAUSE_CHAIN (c) = list;
23654 return c;
23655 }
23656
23657 /* OpenMP 2.5:
23658 num_threads ( expression ) */
23659
23660 static tree
23661 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23662 location_t location)
23663 {
23664 tree t, c;
23665
23666 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23667 return list;
23668
23669 t = cp_parser_expression (parser, false, NULL);
23670
23671 if (t == error_mark_node
23672 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23673 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23674 /*or_comma=*/false,
23675 /*consume_paren=*/true);
23676
23677 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23678 "num_threads", location);
23679
23680 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23681 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23682 OMP_CLAUSE_CHAIN (c) = list;
23683
23684 return c;
23685 }
23686
23687 /* OpenMP 2.5:
23688 ordered */
23689
23690 static tree
23691 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23692 tree list, location_t location)
23693 {
23694 tree c;
23695
23696 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23697 "ordered", location);
23698
23699 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23700 OMP_CLAUSE_CHAIN (c) = list;
23701 return c;
23702 }
23703
23704 /* OpenMP 2.5:
23705 reduction ( reduction-operator : variable-list )
23706
23707 reduction-operator:
23708 One of: + * - & ^ | && || */
23709
23710 static tree
23711 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23712 {
23713 enum tree_code code;
23714 tree nlist, c;
23715
23716 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23717 return list;
23718
23719 switch (cp_lexer_peek_token (parser->lexer)->type)
23720 {
23721 case CPP_PLUS:
23722 code = PLUS_EXPR;
23723 break;
23724 case CPP_MULT:
23725 code = MULT_EXPR;
23726 break;
23727 case CPP_MINUS:
23728 code = MINUS_EXPR;
23729 break;
23730 case CPP_AND:
23731 code = BIT_AND_EXPR;
23732 break;
23733 case CPP_XOR:
23734 code = BIT_XOR_EXPR;
23735 break;
23736 case CPP_OR:
23737 code = BIT_IOR_EXPR;
23738 break;
23739 case CPP_AND_AND:
23740 code = TRUTH_ANDIF_EXPR;
23741 break;
23742 case CPP_OR_OR:
23743 code = TRUTH_ORIF_EXPR;
23744 break;
23745 default:
23746 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23747 "%<|%>, %<&&%>, or %<||%>");
23748 resync_fail:
23749 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23750 /*or_comma=*/false,
23751 /*consume_paren=*/true);
23752 return list;
23753 }
23754 cp_lexer_consume_token (parser->lexer);
23755
23756 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23757 goto resync_fail;
23758
23759 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23760 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23761 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23762
23763 return nlist;
23764 }
23765
23766 /* OpenMP 2.5:
23767 schedule ( schedule-kind )
23768 schedule ( schedule-kind , expression )
23769
23770 schedule-kind:
23771 static | dynamic | guided | runtime | auto */
23772
23773 static tree
23774 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23775 {
23776 tree c, t;
23777
23778 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23779 return list;
23780
23781 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23782
23783 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23784 {
23785 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23786 const char *p = IDENTIFIER_POINTER (id);
23787
23788 switch (p[0])
23789 {
23790 case 'd':
23791 if (strcmp ("dynamic", p) != 0)
23792 goto invalid_kind;
23793 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23794 break;
23795
23796 case 'g':
23797 if (strcmp ("guided", p) != 0)
23798 goto invalid_kind;
23799 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23800 break;
23801
23802 case 'r':
23803 if (strcmp ("runtime", p) != 0)
23804 goto invalid_kind;
23805 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23806 break;
23807
23808 default:
23809 goto invalid_kind;
23810 }
23811 }
23812 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23813 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23814 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23815 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23816 else
23817 goto invalid_kind;
23818 cp_lexer_consume_token (parser->lexer);
23819
23820 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23821 {
23822 cp_token *token;
23823 cp_lexer_consume_token (parser->lexer);
23824
23825 token = cp_lexer_peek_token (parser->lexer);
23826 t = cp_parser_assignment_expression (parser, false, NULL);
23827
23828 if (t == error_mark_node)
23829 goto resync_fail;
23830 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23831 error_at (token->location, "schedule %<runtime%> does not take "
23832 "a %<chunk_size%> parameter");
23833 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23834 error_at (token->location, "schedule %<auto%> does not take "
23835 "a %<chunk_size%> parameter");
23836 else
23837 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23838
23839 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23840 goto resync_fail;
23841 }
23842 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23843 goto resync_fail;
23844
23845 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23846 OMP_CLAUSE_CHAIN (c) = list;
23847 return c;
23848
23849 invalid_kind:
23850 cp_parser_error (parser, "invalid schedule kind");
23851 resync_fail:
23852 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23853 /*or_comma=*/false,
23854 /*consume_paren=*/true);
23855 return list;
23856 }
23857
23858 /* OpenMP 3.0:
23859 untied */
23860
23861 static tree
23862 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23863 tree list, location_t location)
23864 {
23865 tree c;
23866
23867 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23868
23869 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23870 OMP_CLAUSE_CHAIN (c) = list;
23871 return c;
23872 }
23873
23874 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23875 is a bitmask in MASK. Return the list of clauses found; the result
23876 of clause default goes in *pdefault. */
23877
23878 static tree
23879 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23880 const char *where, cp_token *pragma_tok)
23881 {
23882 tree clauses = NULL;
23883 bool first = true;
23884 cp_token *token = NULL;
23885
23886 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23887 {
23888 pragma_omp_clause c_kind;
23889 const char *c_name;
23890 tree prev = clauses;
23891
23892 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23893 cp_lexer_consume_token (parser->lexer);
23894
23895 token = cp_lexer_peek_token (parser->lexer);
23896 c_kind = cp_parser_omp_clause_name (parser);
23897 first = false;
23898
23899 switch (c_kind)
23900 {
23901 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23902 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23903 token->location);
23904 c_name = "collapse";
23905 break;
23906 case PRAGMA_OMP_CLAUSE_COPYIN:
23907 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23908 c_name = "copyin";
23909 break;
23910 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23911 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23912 clauses);
23913 c_name = "copyprivate";
23914 break;
23915 case PRAGMA_OMP_CLAUSE_DEFAULT:
23916 clauses = cp_parser_omp_clause_default (parser, clauses,
23917 token->location);
23918 c_name = "default";
23919 break;
23920 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23921 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23922 clauses);
23923 c_name = "firstprivate";
23924 break;
23925 case PRAGMA_OMP_CLAUSE_IF:
23926 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23927 c_name = "if";
23928 break;
23929 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23930 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23931 clauses);
23932 c_name = "lastprivate";
23933 break;
23934 case PRAGMA_OMP_CLAUSE_NOWAIT:
23935 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23936 c_name = "nowait";
23937 break;
23938 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23939 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23940 token->location);
23941 c_name = "num_threads";
23942 break;
23943 case PRAGMA_OMP_CLAUSE_ORDERED:
23944 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23945 token->location);
23946 c_name = "ordered";
23947 break;
23948 case PRAGMA_OMP_CLAUSE_PRIVATE:
23949 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23950 clauses);
23951 c_name = "private";
23952 break;
23953 case PRAGMA_OMP_CLAUSE_REDUCTION:
23954 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23955 c_name = "reduction";
23956 break;
23957 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23958 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23959 token->location);
23960 c_name = "schedule";
23961 break;
23962 case PRAGMA_OMP_CLAUSE_SHARED:
23963 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23964 clauses);
23965 c_name = "shared";
23966 break;
23967 case PRAGMA_OMP_CLAUSE_UNTIED:
23968 clauses = cp_parser_omp_clause_untied (parser, clauses,
23969 token->location);
23970 c_name = "nowait";
23971 break;
23972 default:
23973 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23974 goto saw_error;
23975 }
23976
23977 if (((mask >> c_kind) & 1) == 0)
23978 {
23979 /* Remove the invalid clause(s) from the list to avoid
23980 confusing the rest of the compiler. */
23981 clauses = prev;
23982 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23983 }
23984 }
23985 saw_error:
23986 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23987 return finish_omp_clauses (clauses);
23988 }
23989
23990 /* OpenMP 2.5:
23991 structured-block:
23992 statement
23993
23994 In practice, we're also interested in adding the statement to an
23995 outer node. So it is convenient if we work around the fact that
23996 cp_parser_statement calls add_stmt. */
23997
23998 static unsigned
23999 cp_parser_begin_omp_structured_block (cp_parser *parser)
24000 {
24001 unsigned save = parser->in_statement;
24002
24003 /* Only move the values to IN_OMP_BLOCK if they weren't false.
24004 This preserves the "not within loop or switch" style error messages
24005 for nonsense cases like
24006 void foo() {
24007 #pragma omp single
24008 break;
24009 }
24010 */
24011 if (parser->in_statement)
24012 parser->in_statement = IN_OMP_BLOCK;
24013
24014 return save;
24015 }
24016
24017 static void
24018 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24019 {
24020 parser->in_statement = save;
24021 }
24022
24023 static tree
24024 cp_parser_omp_structured_block (cp_parser *parser)
24025 {
24026 tree stmt = begin_omp_structured_block ();
24027 unsigned int save = cp_parser_begin_omp_structured_block (parser);
24028
24029 cp_parser_statement (parser, NULL_TREE, false, NULL);
24030
24031 cp_parser_end_omp_structured_block (parser, save);
24032 return finish_omp_structured_block (stmt);
24033 }
24034
24035 /* OpenMP 2.5:
24036 # pragma omp atomic new-line
24037 expression-stmt
24038
24039 expression-stmt:
24040 x binop= expr | x++ | ++x | x-- | --x
24041 binop:
24042 +, *, -, /, &, ^, |, <<, >>
24043
24044 where x is an lvalue expression with scalar type. */
24045
24046 static void
24047 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24048 {
24049 tree lhs, rhs;
24050 enum tree_code code;
24051
24052 cp_parser_require_pragma_eol (parser, pragma_tok);
24053
24054 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24055 /*cast_p=*/false, NULL);
24056 switch (TREE_CODE (lhs))
24057 {
24058 case ERROR_MARK:
24059 goto saw_error;
24060
24061 case PREINCREMENT_EXPR:
24062 case POSTINCREMENT_EXPR:
24063 lhs = TREE_OPERAND (lhs, 0);
24064 code = PLUS_EXPR;
24065 rhs = integer_one_node;
24066 break;
24067
24068 case PREDECREMENT_EXPR:
24069 case POSTDECREMENT_EXPR:
24070 lhs = TREE_OPERAND (lhs, 0);
24071 code = MINUS_EXPR;
24072 rhs = integer_one_node;
24073 break;
24074
24075 case COMPOUND_EXPR:
24076 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24077 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24078 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24079 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24080 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24081 (TREE_OPERAND (lhs, 1), 0), 0)))
24082 == BOOLEAN_TYPE)
24083 /* Undo effects of boolean_increment for post {in,de}crement. */
24084 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24085 /* FALLTHRU */
24086 case MODIFY_EXPR:
24087 if (TREE_CODE (lhs) == MODIFY_EXPR
24088 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24089 {
24090 /* Undo effects of boolean_increment. */
24091 if (integer_onep (TREE_OPERAND (lhs, 1)))
24092 {
24093 /* This is pre or post increment. */
24094 rhs = TREE_OPERAND (lhs, 1);
24095 lhs = TREE_OPERAND (lhs, 0);
24096 code = NOP_EXPR;
24097 break;
24098 }
24099 }
24100 /* FALLTHRU */
24101 default:
24102 switch (cp_lexer_peek_token (parser->lexer)->type)
24103 {
24104 case CPP_MULT_EQ:
24105 code = MULT_EXPR;
24106 break;
24107 case CPP_DIV_EQ:
24108 code = TRUNC_DIV_EXPR;
24109 break;
24110 case CPP_PLUS_EQ:
24111 code = PLUS_EXPR;
24112 break;
24113 case CPP_MINUS_EQ:
24114 code = MINUS_EXPR;
24115 break;
24116 case CPP_LSHIFT_EQ:
24117 code = LSHIFT_EXPR;
24118 break;
24119 case CPP_RSHIFT_EQ:
24120 code = RSHIFT_EXPR;
24121 break;
24122 case CPP_AND_EQ:
24123 code = BIT_AND_EXPR;
24124 break;
24125 case CPP_OR_EQ:
24126 code = BIT_IOR_EXPR;
24127 break;
24128 case CPP_XOR_EQ:
24129 code = BIT_XOR_EXPR;
24130 break;
24131 default:
24132 cp_parser_error (parser,
24133 "invalid operator for %<#pragma omp atomic%>");
24134 goto saw_error;
24135 }
24136 cp_lexer_consume_token (parser->lexer);
24137
24138 rhs = cp_parser_expression (parser, false, NULL);
24139 if (rhs == error_mark_node)
24140 goto saw_error;
24141 break;
24142 }
24143 finish_omp_atomic (code, lhs, rhs);
24144 cp_parser_consume_semicolon_at_end_of_statement (parser);
24145 return;
24146
24147 saw_error:
24148 cp_parser_skip_to_end_of_block_or_statement (parser);
24149 }
24150
24151
24152 /* OpenMP 2.5:
24153 # pragma omp barrier new-line */
24154
24155 static void
24156 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24157 {
24158 cp_parser_require_pragma_eol (parser, pragma_tok);
24159 finish_omp_barrier ();
24160 }
24161
24162 /* OpenMP 2.5:
24163 # pragma omp critical [(name)] new-line
24164 structured-block */
24165
24166 static tree
24167 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24168 {
24169 tree stmt, name = NULL;
24170
24171 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24172 {
24173 cp_lexer_consume_token (parser->lexer);
24174
24175 name = cp_parser_identifier (parser);
24176
24177 if (name == error_mark_node
24178 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24179 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24180 /*or_comma=*/false,
24181 /*consume_paren=*/true);
24182 if (name == error_mark_node)
24183 name = NULL;
24184 }
24185 cp_parser_require_pragma_eol (parser, pragma_tok);
24186
24187 stmt = cp_parser_omp_structured_block (parser);
24188 return c_finish_omp_critical (input_location, stmt, name);
24189 }
24190
24191 /* OpenMP 2.5:
24192 # pragma omp flush flush-vars[opt] new-line
24193
24194 flush-vars:
24195 ( variable-list ) */
24196
24197 static void
24198 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24199 {
24200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24201 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24202 cp_parser_require_pragma_eol (parser, pragma_tok);
24203
24204 finish_omp_flush ();
24205 }
24206
24207 /* Helper function, to parse omp for increment expression. */
24208
24209 static tree
24210 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24211 {
24212 tree cond = cp_parser_binary_expression (parser, false, true,
24213 PREC_NOT_OPERATOR, NULL);
24214 bool overloaded_p;
24215
24216 if (cond == error_mark_node
24217 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24218 {
24219 cp_parser_skip_to_end_of_statement (parser);
24220 return error_mark_node;
24221 }
24222
24223 switch (TREE_CODE (cond))
24224 {
24225 case GT_EXPR:
24226 case GE_EXPR:
24227 case LT_EXPR:
24228 case LE_EXPR:
24229 break;
24230 default:
24231 return error_mark_node;
24232 }
24233
24234 /* If decl is an iterator, preserve LHS and RHS of the relational
24235 expr until finish_omp_for. */
24236 if (decl
24237 && (type_dependent_expression_p (decl)
24238 || CLASS_TYPE_P (TREE_TYPE (decl))))
24239 return cond;
24240
24241 return build_x_binary_op (TREE_CODE (cond),
24242 TREE_OPERAND (cond, 0), ERROR_MARK,
24243 TREE_OPERAND (cond, 1), ERROR_MARK,
24244 &overloaded_p, tf_warning_or_error);
24245 }
24246
24247 /* Helper function, to parse omp for increment expression. */
24248
24249 static tree
24250 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24251 {
24252 cp_token *token = cp_lexer_peek_token (parser->lexer);
24253 enum tree_code op;
24254 tree lhs, rhs;
24255 cp_id_kind idk;
24256 bool decl_first;
24257
24258 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24259 {
24260 op = (token->type == CPP_PLUS_PLUS
24261 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24262 cp_lexer_consume_token (parser->lexer);
24263 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24264 if (lhs != decl)
24265 return error_mark_node;
24266 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24267 }
24268
24269 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24270 if (lhs != decl)
24271 return error_mark_node;
24272
24273 token = cp_lexer_peek_token (parser->lexer);
24274 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24275 {
24276 op = (token->type == CPP_PLUS_PLUS
24277 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24278 cp_lexer_consume_token (parser->lexer);
24279 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24280 }
24281
24282 op = cp_parser_assignment_operator_opt (parser);
24283 if (op == ERROR_MARK)
24284 return error_mark_node;
24285
24286 if (op != NOP_EXPR)
24287 {
24288 rhs = cp_parser_assignment_expression (parser, false, NULL);
24289 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24290 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24291 }
24292
24293 lhs = cp_parser_binary_expression (parser, false, false,
24294 PREC_ADDITIVE_EXPRESSION, NULL);
24295 token = cp_lexer_peek_token (parser->lexer);
24296 decl_first = lhs == decl;
24297 if (decl_first)
24298 lhs = NULL_TREE;
24299 if (token->type != CPP_PLUS
24300 && token->type != CPP_MINUS)
24301 return error_mark_node;
24302
24303 do
24304 {
24305 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24306 cp_lexer_consume_token (parser->lexer);
24307 rhs = cp_parser_binary_expression (parser, false, false,
24308 PREC_ADDITIVE_EXPRESSION, NULL);
24309 token = cp_lexer_peek_token (parser->lexer);
24310 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24311 {
24312 if (lhs == NULL_TREE)
24313 {
24314 if (op == PLUS_EXPR)
24315 lhs = rhs;
24316 else
24317 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24318 }
24319 else
24320 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24321 NULL, tf_warning_or_error);
24322 }
24323 }
24324 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24325
24326 if (!decl_first)
24327 {
24328 if (rhs != decl || op == MINUS_EXPR)
24329 return error_mark_node;
24330 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24331 }
24332 else
24333 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24334
24335 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24336 }
24337
24338 /* Parse the restricted form of the for statement allowed by OpenMP. */
24339
24340 static tree
24341 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24342 {
24343 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24344 tree real_decl, initv, condv, incrv, declv;
24345 tree this_pre_body, cl;
24346 location_t loc_first;
24347 bool collapse_err = false;
24348 int i, collapse = 1, nbraces = 0;
24349 VEC(tree,gc) *for_block = make_tree_vector ();
24350
24351 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24352 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24353 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24354
24355 gcc_assert (collapse >= 1);
24356
24357 declv = make_tree_vec (collapse);
24358 initv = make_tree_vec (collapse);
24359 condv = make_tree_vec (collapse);
24360 incrv = make_tree_vec (collapse);
24361
24362 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24363
24364 for (i = 0; i < collapse; i++)
24365 {
24366 int bracecount = 0;
24367 bool add_private_clause = false;
24368 location_t loc;
24369
24370 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24371 {
24372 cp_parser_error (parser, "for statement expected");
24373 return NULL;
24374 }
24375 loc = cp_lexer_consume_token (parser->lexer)->location;
24376
24377 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24378 return NULL;
24379
24380 init = decl = real_decl = NULL;
24381 this_pre_body = push_stmt_list ();
24382 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24383 {
24384 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24385
24386 init-expr:
24387 var = lb
24388 integer-type var = lb
24389 random-access-iterator-type var = lb
24390 pointer-type var = lb
24391 */
24392 cp_decl_specifier_seq type_specifiers;
24393
24394 /* First, try to parse as an initialized declaration. See
24395 cp_parser_condition, from whence the bulk of this is copied. */
24396
24397 cp_parser_parse_tentatively (parser);
24398 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24399 /*is_trailing_return=*/false,
24400 &type_specifiers);
24401 if (cp_parser_parse_definitely (parser))
24402 {
24403 /* If parsing a type specifier seq succeeded, then this
24404 MUST be a initialized declaration. */
24405 tree asm_specification, attributes;
24406 cp_declarator *declarator;
24407
24408 declarator = cp_parser_declarator (parser,
24409 CP_PARSER_DECLARATOR_NAMED,
24410 /*ctor_dtor_or_conv_p=*/NULL,
24411 /*parenthesized_p=*/NULL,
24412 /*member_p=*/false);
24413 attributes = cp_parser_attributes_opt (parser);
24414 asm_specification = cp_parser_asm_specification_opt (parser);
24415
24416 if (declarator == cp_error_declarator)
24417 cp_parser_skip_to_end_of_statement (parser);
24418
24419 else
24420 {
24421 tree pushed_scope, auto_node;
24422
24423 decl = start_decl (declarator, &type_specifiers,
24424 SD_INITIALIZED, attributes,
24425 /*prefix_attributes=*/NULL_TREE,
24426 &pushed_scope);
24427
24428 auto_node = type_uses_auto (TREE_TYPE (decl));
24429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24430 {
24431 if (cp_lexer_next_token_is (parser->lexer,
24432 CPP_OPEN_PAREN))
24433 error ("parenthesized initialization is not allowed in "
24434 "OpenMP %<for%> loop");
24435 else
24436 /* Trigger an error. */
24437 cp_parser_require (parser, CPP_EQ, RT_EQ);
24438
24439 init = error_mark_node;
24440 cp_parser_skip_to_end_of_statement (parser);
24441 }
24442 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24443 || type_dependent_expression_p (decl)
24444 || auto_node)
24445 {
24446 bool is_direct_init, is_non_constant_init;
24447
24448 init = cp_parser_initializer (parser,
24449 &is_direct_init,
24450 &is_non_constant_init);
24451
24452 if (auto_node && describable_type (init))
24453 {
24454 TREE_TYPE (decl)
24455 = do_auto_deduction (TREE_TYPE (decl), init,
24456 auto_node);
24457
24458 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24459 && !type_dependent_expression_p (decl))
24460 goto non_class;
24461 }
24462
24463 cp_finish_decl (decl, init, !is_non_constant_init,
24464 asm_specification,
24465 LOOKUP_ONLYCONVERTING);
24466 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24467 {
24468 VEC_safe_push (tree, gc, for_block, this_pre_body);
24469 init = NULL_TREE;
24470 }
24471 else
24472 init = pop_stmt_list (this_pre_body);
24473 this_pre_body = NULL_TREE;
24474 }
24475 else
24476 {
24477 /* Consume '='. */
24478 cp_lexer_consume_token (parser->lexer);
24479 init = cp_parser_assignment_expression (parser, false, NULL);
24480
24481 non_class:
24482 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24483 init = error_mark_node;
24484 else
24485 cp_finish_decl (decl, NULL_TREE,
24486 /*init_const_expr_p=*/false,
24487 asm_specification,
24488 LOOKUP_ONLYCONVERTING);
24489 }
24490
24491 if (pushed_scope)
24492 pop_scope (pushed_scope);
24493 }
24494 }
24495 else
24496 {
24497 cp_id_kind idk;
24498 /* If parsing a type specifier sequence failed, then
24499 this MUST be a simple expression. */
24500 cp_parser_parse_tentatively (parser);
24501 decl = cp_parser_primary_expression (parser, false, false,
24502 false, &idk);
24503 if (!cp_parser_error_occurred (parser)
24504 && decl
24505 && DECL_P (decl)
24506 && CLASS_TYPE_P (TREE_TYPE (decl)))
24507 {
24508 tree rhs;
24509
24510 cp_parser_parse_definitely (parser);
24511 cp_parser_require (parser, CPP_EQ, RT_EQ);
24512 rhs = cp_parser_assignment_expression (parser, false, NULL);
24513 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24514 rhs,
24515 tf_warning_or_error));
24516 add_private_clause = true;
24517 }
24518 else
24519 {
24520 decl = NULL;
24521 cp_parser_abort_tentative_parse (parser);
24522 init = cp_parser_expression (parser, false, NULL);
24523 if (init)
24524 {
24525 if (TREE_CODE (init) == MODIFY_EXPR
24526 || TREE_CODE (init) == MODOP_EXPR)
24527 real_decl = TREE_OPERAND (init, 0);
24528 }
24529 }
24530 }
24531 }
24532 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24533 if (this_pre_body)
24534 {
24535 this_pre_body = pop_stmt_list (this_pre_body);
24536 if (pre_body)
24537 {
24538 tree t = pre_body;
24539 pre_body = push_stmt_list ();
24540 add_stmt (t);
24541 add_stmt (this_pre_body);
24542 pre_body = pop_stmt_list (pre_body);
24543 }
24544 else
24545 pre_body = this_pre_body;
24546 }
24547
24548 if (decl)
24549 real_decl = decl;
24550 if (par_clauses != NULL && real_decl != NULL_TREE)
24551 {
24552 tree *c;
24553 for (c = par_clauses; *c ; )
24554 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24555 && OMP_CLAUSE_DECL (*c) == real_decl)
24556 {
24557 error_at (loc, "iteration variable %qD"
24558 " should not be firstprivate", real_decl);
24559 *c = OMP_CLAUSE_CHAIN (*c);
24560 }
24561 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24562 && OMP_CLAUSE_DECL (*c) == real_decl)
24563 {
24564 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24565 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24566 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24567 OMP_CLAUSE_DECL (l) = real_decl;
24568 OMP_CLAUSE_CHAIN (l) = clauses;
24569 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24570 clauses = l;
24571 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24572 CP_OMP_CLAUSE_INFO (*c) = NULL;
24573 add_private_clause = false;
24574 }
24575 else
24576 {
24577 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24578 && OMP_CLAUSE_DECL (*c) == real_decl)
24579 add_private_clause = false;
24580 c = &OMP_CLAUSE_CHAIN (*c);
24581 }
24582 }
24583
24584 if (add_private_clause)
24585 {
24586 tree c;
24587 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24588 {
24589 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24590 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24591 && OMP_CLAUSE_DECL (c) == decl)
24592 break;
24593 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24594 && OMP_CLAUSE_DECL (c) == decl)
24595 error_at (loc, "iteration variable %qD "
24596 "should not be firstprivate",
24597 decl);
24598 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24599 && OMP_CLAUSE_DECL (c) == decl)
24600 error_at (loc, "iteration variable %qD should not be reduction",
24601 decl);
24602 }
24603 if (c == NULL)
24604 {
24605 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24606 OMP_CLAUSE_DECL (c) = decl;
24607 c = finish_omp_clauses (c);
24608 if (c)
24609 {
24610 OMP_CLAUSE_CHAIN (c) = clauses;
24611 clauses = c;
24612 }
24613 }
24614 }
24615
24616 cond = NULL;
24617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24618 cond = cp_parser_omp_for_cond (parser, decl);
24619 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24620
24621 incr = NULL;
24622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24623 {
24624 /* If decl is an iterator, preserve the operator on decl
24625 until finish_omp_for. */
24626 if (decl
24627 && ((type_dependent_expression_p (decl)
24628 && !POINTER_TYPE_P (TREE_TYPE (decl)))
24629 || CLASS_TYPE_P (TREE_TYPE (decl))))
24630 incr = cp_parser_omp_for_incr (parser, decl);
24631 else
24632 incr = cp_parser_expression (parser, false, NULL);
24633 }
24634
24635 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24636 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24637 /*or_comma=*/false,
24638 /*consume_paren=*/true);
24639
24640 TREE_VEC_ELT (declv, i) = decl;
24641 TREE_VEC_ELT (initv, i) = init;
24642 TREE_VEC_ELT (condv, i) = cond;
24643 TREE_VEC_ELT (incrv, i) = incr;
24644
24645 if (i == collapse - 1)
24646 break;
24647
24648 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24649 in between the collapsed for loops to be still considered perfectly
24650 nested. Hopefully the final version clarifies this.
24651 For now handle (multiple) {'s and empty statements. */
24652 cp_parser_parse_tentatively (parser);
24653 do
24654 {
24655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24656 break;
24657 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24658 {
24659 cp_lexer_consume_token (parser->lexer);
24660 bracecount++;
24661 }
24662 else if (bracecount
24663 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24664 cp_lexer_consume_token (parser->lexer);
24665 else
24666 {
24667 loc = cp_lexer_peek_token (parser->lexer)->location;
24668 error_at (loc, "not enough collapsed for loops");
24669 collapse_err = true;
24670 cp_parser_abort_tentative_parse (parser);
24671 declv = NULL_TREE;
24672 break;
24673 }
24674 }
24675 while (1);
24676
24677 if (declv)
24678 {
24679 cp_parser_parse_definitely (parser);
24680 nbraces += bracecount;
24681 }
24682 }
24683
24684 /* Note that we saved the original contents of this flag when we entered
24685 the structured block, and so we don't need to re-save it here. */
24686 parser->in_statement = IN_OMP_FOR;
24687
24688 /* Note that the grammar doesn't call for a structured block here,
24689 though the loop as a whole is a structured block. */
24690 body = push_stmt_list ();
24691 cp_parser_statement (parser, NULL_TREE, false, NULL);
24692 body = pop_stmt_list (body);
24693
24694 if (declv == NULL_TREE)
24695 ret = NULL_TREE;
24696 else
24697 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24698 pre_body, clauses);
24699
24700 while (nbraces)
24701 {
24702 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24703 {
24704 cp_lexer_consume_token (parser->lexer);
24705 nbraces--;
24706 }
24707 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24708 cp_lexer_consume_token (parser->lexer);
24709 else
24710 {
24711 if (!collapse_err)
24712 {
24713 error_at (cp_lexer_peek_token (parser->lexer)->location,
24714 "collapsed loops not perfectly nested");
24715 }
24716 collapse_err = true;
24717 cp_parser_statement_seq_opt (parser, NULL);
24718 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24719 break;
24720 }
24721 }
24722
24723 while (!VEC_empty (tree, for_block))
24724 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24725 release_tree_vector (for_block);
24726
24727 return ret;
24728 }
24729
24730 /* OpenMP 2.5:
24731 #pragma omp for for-clause[optseq] new-line
24732 for-loop */
24733
24734 #define OMP_FOR_CLAUSE_MASK \
24735 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24736 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24737 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24738 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24739 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24740 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24741 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24742 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24743
24744 static tree
24745 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24746 {
24747 tree clauses, sb, ret;
24748 unsigned int save;
24749
24750 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24751 "#pragma omp for", pragma_tok);
24752
24753 sb = begin_omp_structured_block ();
24754 save = cp_parser_begin_omp_structured_block (parser);
24755
24756 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24757
24758 cp_parser_end_omp_structured_block (parser, save);
24759 add_stmt (finish_omp_structured_block (sb));
24760
24761 return ret;
24762 }
24763
24764 /* OpenMP 2.5:
24765 # pragma omp master new-line
24766 structured-block */
24767
24768 static tree
24769 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24770 {
24771 cp_parser_require_pragma_eol (parser, pragma_tok);
24772 return c_finish_omp_master (input_location,
24773 cp_parser_omp_structured_block (parser));
24774 }
24775
24776 /* OpenMP 2.5:
24777 # pragma omp ordered new-line
24778 structured-block */
24779
24780 static tree
24781 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24782 {
24783 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24784 cp_parser_require_pragma_eol (parser, pragma_tok);
24785 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24786 }
24787
24788 /* OpenMP 2.5:
24789
24790 section-scope:
24791 { section-sequence }
24792
24793 section-sequence:
24794 section-directive[opt] structured-block
24795 section-sequence section-directive structured-block */
24796
24797 static tree
24798 cp_parser_omp_sections_scope (cp_parser *parser)
24799 {
24800 tree stmt, substmt;
24801 bool error_suppress = false;
24802 cp_token *tok;
24803
24804 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24805 return NULL_TREE;
24806
24807 stmt = push_stmt_list ();
24808
24809 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24810 {
24811 unsigned save;
24812
24813 substmt = begin_omp_structured_block ();
24814 save = cp_parser_begin_omp_structured_block (parser);
24815
24816 while (1)
24817 {
24818 cp_parser_statement (parser, NULL_TREE, false, NULL);
24819
24820 tok = cp_lexer_peek_token (parser->lexer);
24821 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24822 break;
24823 if (tok->type == CPP_CLOSE_BRACE)
24824 break;
24825 if (tok->type == CPP_EOF)
24826 break;
24827 }
24828
24829 cp_parser_end_omp_structured_block (parser, save);
24830 substmt = finish_omp_structured_block (substmt);
24831 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24832 add_stmt (substmt);
24833 }
24834
24835 while (1)
24836 {
24837 tok = cp_lexer_peek_token (parser->lexer);
24838 if (tok->type == CPP_CLOSE_BRACE)
24839 break;
24840 if (tok->type == CPP_EOF)
24841 break;
24842
24843 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24844 {
24845 cp_lexer_consume_token (parser->lexer);
24846 cp_parser_require_pragma_eol (parser, tok);
24847 error_suppress = false;
24848 }
24849 else if (!error_suppress)
24850 {
24851 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24852 error_suppress = true;
24853 }
24854
24855 substmt = cp_parser_omp_structured_block (parser);
24856 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24857 add_stmt (substmt);
24858 }
24859 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24860
24861 substmt = pop_stmt_list (stmt);
24862
24863 stmt = make_node (OMP_SECTIONS);
24864 TREE_TYPE (stmt) = void_type_node;
24865 OMP_SECTIONS_BODY (stmt) = substmt;
24866
24867 add_stmt (stmt);
24868 return stmt;
24869 }
24870
24871 /* OpenMP 2.5:
24872 # pragma omp sections sections-clause[optseq] newline
24873 sections-scope */
24874
24875 #define OMP_SECTIONS_CLAUSE_MASK \
24876 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24877 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24878 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24879 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24880 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24881
24882 static tree
24883 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24884 {
24885 tree clauses, ret;
24886
24887 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24888 "#pragma omp sections", pragma_tok);
24889
24890 ret = cp_parser_omp_sections_scope (parser);
24891 if (ret)
24892 OMP_SECTIONS_CLAUSES (ret) = clauses;
24893
24894 return ret;
24895 }
24896
24897 /* OpenMP 2.5:
24898 # pragma parallel parallel-clause new-line
24899 # pragma parallel for parallel-for-clause new-line
24900 # pragma parallel sections parallel-sections-clause new-line */
24901
24902 #define OMP_PARALLEL_CLAUSE_MASK \
24903 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24904 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24905 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24906 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24907 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24908 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24909 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24910 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24911
24912 static tree
24913 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24914 {
24915 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24916 const char *p_name = "#pragma omp parallel";
24917 tree stmt, clauses, par_clause, ws_clause, block;
24918 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24919 unsigned int save;
24920 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24921
24922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24923 {
24924 cp_lexer_consume_token (parser->lexer);
24925 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24926 p_name = "#pragma omp parallel for";
24927 mask |= OMP_FOR_CLAUSE_MASK;
24928 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24929 }
24930 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24931 {
24932 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24933 const char *p = IDENTIFIER_POINTER (id);
24934 if (strcmp (p, "sections") == 0)
24935 {
24936 cp_lexer_consume_token (parser->lexer);
24937 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24938 p_name = "#pragma omp parallel sections";
24939 mask |= OMP_SECTIONS_CLAUSE_MASK;
24940 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24941 }
24942 }
24943
24944 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24945 block = begin_omp_parallel ();
24946 save = cp_parser_begin_omp_structured_block (parser);
24947
24948 switch (p_kind)
24949 {
24950 case PRAGMA_OMP_PARALLEL:
24951 cp_parser_statement (parser, NULL_TREE, false, NULL);
24952 par_clause = clauses;
24953 break;
24954
24955 case PRAGMA_OMP_PARALLEL_FOR:
24956 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24957 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24958 break;
24959
24960 case PRAGMA_OMP_PARALLEL_SECTIONS:
24961 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24962 stmt = cp_parser_omp_sections_scope (parser);
24963 if (stmt)
24964 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24965 break;
24966
24967 default:
24968 gcc_unreachable ();
24969 }
24970
24971 cp_parser_end_omp_structured_block (parser, save);
24972 stmt = finish_omp_parallel (par_clause, block);
24973 if (p_kind != PRAGMA_OMP_PARALLEL)
24974 OMP_PARALLEL_COMBINED (stmt) = 1;
24975 return stmt;
24976 }
24977
24978 /* OpenMP 2.5:
24979 # pragma omp single single-clause[optseq] new-line
24980 structured-block */
24981
24982 #define OMP_SINGLE_CLAUSE_MASK \
24983 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24984 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24985 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24986 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24987
24988 static tree
24989 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24990 {
24991 tree stmt = make_node (OMP_SINGLE);
24992 TREE_TYPE (stmt) = void_type_node;
24993
24994 OMP_SINGLE_CLAUSES (stmt)
24995 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24996 "#pragma omp single", pragma_tok);
24997 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24998
24999 return add_stmt (stmt);
25000 }
25001
25002 /* OpenMP 3.0:
25003 # pragma omp task task-clause[optseq] new-line
25004 structured-block */
25005
25006 #define OMP_TASK_CLAUSE_MASK \
25007 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
25008 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
25009 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
25010 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
25011 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
25012 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25013
25014 static tree
25015 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25016 {
25017 tree clauses, block;
25018 unsigned int save;
25019
25020 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25021 "#pragma omp task", pragma_tok);
25022 block = begin_omp_task ();
25023 save = cp_parser_begin_omp_structured_block (parser);
25024 cp_parser_statement (parser, NULL_TREE, false, NULL);
25025 cp_parser_end_omp_structured_block (parser, save);
25026 return finish_omp_task (clauses, block);
25027 }
25028
25029 /* OpenMP 3.0:
25030 # pragma omp taskwait new-line */
25031
25032 static void
25033 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25034 {
25035 cp_parser_require_pragma_eol (parser, pragma_tok);
25036 finish_omp_taskwait ();
25037 }
25038
25039 /* OpenMP 2.5:
25040 # pragma omp threadprivate (variable-list) */
25041
25042 static void
25043 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25044 {
25045 tree vars;
25046
25047 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25048 cp_parser_require_pragma_eol (parser, pragma_tok);
25049
25050 finish_omp_threadprivate (vars);
25051 }
25052
25053 /* Main entry point to OpenMP statement pragmas. */
25054
25055 static void
25056 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25057 {
25058 tree stmt;
25059
25060 switch (pragma_tok->pragma_kind)
25061 {
25062 case PRAGMA_OMP_ATOMIC:
25063 cp_parser_omp_atomic (parser, pragma_tok);
25064 return;
25065 case PRAGMA_OMP_CRITICAL:
25066 stmt = cp_parser_omp_critical (parser, pragma_tok);
25067 break;
25068 case PRAGMA_OMP_FOR:
25069 stmt = cp_parser_omp_for (parser, pragma_tok);
25070 break;
25071 case PRAGMA_OMP_MASTER:
25072 stmt = cp_parser_omp_master (parser, pragma_tok);
25073 break;
25074 case PRAGMA_OMP_ORDERED:
25075 stmt = cp_parser_omp_ordered (parser, pragma_tok);
25076 break;
25077 case PRAGMA_OMP_PARALLEL:
25078 stmt = cp_parser_omp_parallel (parser, pragma_tok);
25079 break;
25080 case PRAGMA_OMP_SECTIONS:
25081 stmt = cp_parser_omp_sections (parser, pragma_tok);
25082 break;
25083 case PRAGMA_OMP_SINGLE:
25084 stmt = cp_parser_omp_single (parser, pragma_tok);
25085 break;
25086 case PRAGMA_OMP_TASK:
25087 stmt = cp_parser_omp_task (parser, pragma_tok);
25088 break;
25089 default:
25090 gcc_unreachable ();
25091 }
25092
25093 if (stmt)
25094 SET_EXPR_LOCATION (stmt, pragma_tok->location);
25095 }
25096 \f
25097 /* The parser. */
25098
25099 static GTY (()) cp_parser *the_parser;
25100
25101 \f
25102 /* Special handling for the first token or line in the file. The first
25103 thing in the file might be #pragma GCC pch_preprocess, which loads a
25104 PCH file, which is a GC collection point. So we need to handle this
25105 first pragma without benefit of an existing lexer structure.
25106
25107 Always returns one token to the caller in *FIRST_TOKEN. This is
25108 either the true first token of the file, or the first token after
25109 the initial pragma. */
25110
25111 static void
25112 cp_parser_initial_pragma (cp_token *first_token)
25113 {
25114 tree name = NULL;
25115
25116 cp_lexer_get_preprocessor_token (NULL, first_token);
25117 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25118 return;
25119
25120 cp_lexer_get_preprocessor_token (NULL, first_token);
25121 if (first_token->type == CPP_STRING)
25122 {
25123 name = first_token->u.value;
25124
25125 cp_lexer_get_preprocessor_token (NULL, first_token);
25126 if (first_token->type != CPP_PRAGMA_EOL)
25127 error_at (first_token->location,
25128 "junk at end of %<#pragma GCC pch_preprocess%>");
25129 }
25130 else
25131 error_at (first_token->location, "expected string literal");
25132
25133 /* Skip to the end of the pragma. */
25134 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25135 cp_lexer_get_preprocessor_token (NULL, first_token);
25136
25137 /* Now actually load the PCH file. */
25138 if (name)
25139 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25140
25141 /* Read one more token to return to our caller. We have to do this
25142 after reading the PCH file in, since its pointers have to be
25143 live. */
25144 cp_lexer_get_preprocessor_token (NULL, first_token);
25145 }
25146
25147 /* Normal parsing of a pragma token. Here we can (and must) use the
25148 regular lexer. */
25149
25150 static bool
25151 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25152 {
25153 cp_token *pragma_tok;
25154 unsigned int id;
25155
25156 pragma_tok = cp_lexer_consume_token (parser->lexer);
25157 gcc_assert (pragma_tok->type == CPP_PRAGMA);
25158 parser->lexer->in_pragma = true;
25159
25160 id = pragma_tok->pragma_kind;
25161 switch (id)
25162 {
25163 case PRAGMA_GCC_PCH_PREPROCESS:
25164 error_at (pragma_tok->location,
25165 "%<#pragma GCC pch_preprocess%> must be first");
25166 break;
25167
25168 case PRAGMA_OMP_BARRIER:
25169 switch (context)
25170 {
25171 case pragma_compound:
25172 cp_parser_omp_barrier (parser, pragma_tok);
25173 return false;
25174 case pragma_stmt:
25175 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25176 "used in compound statements");
25177 break;
25178 default:
25179 goto bad_stmt;
25180 }
25181 break;
25182
25183 case PRAGMA_OMP_FLUSH:
25184 switch (context)
25185 {
25186 case pragma_compound:
25187 cp_parser_omp_flush (parser, pragma_tok);
25188 return false;
25189 case pragma_stmt:
25190 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25191 "used in compound statements");
25192 break;
25193 default:
25194 goto bad_stmt;
25195 }
25196 break;
25197
25198 case PRAGMA_OMP_TASKWAIT:
25199 switch (context)
25200 {
25201 case pragma_compound:
25202 cp_parser_omp_taskwait (parser, pragma_tok);
25203 return false;
25204 case pragma_stmt:
25205 error_at (pragma_tok->location,
25206 "%<#pragma omp taskwait%> may only be "
25207 "used in compound statements");
25208 break;
25209 default:
25210 goto bad_stmt;
25211 }
25212 break;
25213
25214 case PRAGMA_OMP_THREADPRIVATE:
25215 cp_parser_omp_threadprivate (parser, pragma_tok);
25216 return false;
25217
25218 case PRAGMA_OMP_ATOMIC:
25219 case PRAGMA_OMP_CRITICAL:
25220 case PRAGMA_OMP_FOR:
25221 case PRAGMA_OMP_MASTER:
25222 case PRAGMA_OMP_ORDERED:
25223 case PRAGMA_OMP_PARALLEL:
25224 case PRAGMA_OMP_SECTIONS:
25225 case PRAGMA_OMP_SINGLE:
25226 case PRAGMA_OMP_TASK:
25227 if (context == pragma_external)
25228 goto bad_stmt;
25229 cp_parser_omp_construct (parser, pragma_tok);
25230 return true;
25231
25232 case PRAGMA_OMP_SECTION:
25233 error_at (pragma_tok->location,
25234 "%<#pragma omp section%> may only be used in "
25235 "%<#pragma omp sections%> construct");
25236 break;
25237
25238 default:
25239 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25240 c_invoke_pragma_handler (id);
25241 break;
25242
25243 bad_stmt:
25244 cp_parser_error (parser, "expected declaration specifiers");
25245 break;
25246 }
25247
25248 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25249 return false;
25250 }
25251
25252 /* The interface the pragma parsers have to the lexer. */
25253
25254 enum cpp_ttype
25255 pragma_lex (tree *value)
25256 {
25257 cp_token *tok;
25258 enum cpp_ttype ret;
25259
25260 tok = cp_lexer_peek_token (the_parser->lexer);
25261
25262 ret = tok->type;
25263 *value = tok->u.value;
25264
25265 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25266 ret = CPP_EOF;
25267 else if (ret == CPP_STRING)
25268 *value = cp_parser_string_literal (the_parser, false, false);
25269 else
25270 {
25271 cp_lexer_consume_token (the_parser->lexer);
25272 if (ret == CPP_KEYWORD)
25273 ret = CPP_NAME;
25274 }
25275
25276 return ret;
25277 }
25278
25279 \f
25280 /* External interface. */
25281
25282 /* Parse one entire translation unit. */
25283
25284 void
25285 c_parse_file (void)
25286 {
25287 static bool already_called = false;
25288
25289 if (already_called)
25290 {
25291 sorry ("inter-module optimizations not implemented for C++");
25292 return;
25293 }
25294 already_called = true;
25295
25296 the_parser = cp_parser_new ();
25297 push_deferring_access_checks (flag_access_control
25298 ? dk_no_deferred : dk_no_check);
25299 cp_parser_translation_unit (the_parser);
25300 the_parser = NULL;
25301 }
25302
25303 #include "gt-cp-parser.h"